(
event: Electron.IpcMainInvokeEvent,
request: AIRequest,
eventChannel: string
)
| 276 | } |
| 277 | |
| 278 | public async sendMessageStreaming( |
| 279 | event: Electron.IpcMainInvokeEvent, |
| 280 | request: AIRequest, |
| 281 | eventChannel: string |
| 282 | ): Promise<void> { |
| 283 | const abortController = new AbortController() |
| 284 | this.abortControllers.set(request.requestId, abortController) |
| 285 | this.initRequestState(request.requestId, event, eventChannel) |
| 286 | |
| 287 | try { |
| 288 | const modelConfig = this.getModelConfig(request.modelConfig) |
| 289 | const apiMessages = await this.prepareApiMessages(request.messages, modelConfig) |
| 290 | const response = await this.fetchStreamingResponse( |
| 291 | request, |
| 292 | apiMessages, |
| 293 | modelConfig, |
| 294 | abortController |
| 295 | ) |
| 296 | |
| 297 | if (!response.ok) { |
| 298 | let errorDetail = '' |
| 299 | try { |
| 300 | const errorBody = await response.text() |
| 301 | const parsed = JSON.parse(errorBody) |
| 302 | errorDetail = parsed.error?.message || parsed.message || errorBody |
| 303 | } catch { |
| 304 | errorDetail = response.statusText |
| 305 | } |
| 306 | |
| 307 | this.finalizeTerminalError(request.requestId, `HTTP ${response.status}: ${errorDetail}`) |
| 308 | return |
| 309 | } |
| 310 | |
| 311 | const reader = response.body?.getReader() |
| 312 | if (!reader) { |
| 313 | this.finalizeTerminalError(request.requestId, 'No response body reader available') |
| 314 | return |
| 315 | } |
| 316 | |
| 317 | const parser = this.createStreamParser(request.requestId) |
| 318 | await this.processStreamResponse(request.requestId, reader, parser) |
| 319 | } catch (error) { |
| 320 | if (error instanceof Error && error.name === 'AbortError') { |
| 321 | this.finalizeComplete(request.requestId) |
| 322 | return |
| 323 | } |
| 324 | |
| 325 | this.finalizeTerminalError( |
| 326 | request.requestId, |
| 327 | error instanceof Error ? error.message : 'Unknown error' |
| 328 | ) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | public async stopStreaming(requestId: string): Promise<void> { |
| 333 | const abortController = this.abortControllers.get(requestId) |
no test coverage detected