| 9 | } |
| 10 | |
| 11 | export class AIService { |
| 12 | private llmConfig: LLMConfig |
| 13 | private modelConfig: ModelConfig | undefined |
| 14 | private requestId: string |
| 15 | private isAborted: boolean = false |
| 16 | |
| 17 | constructor(llmConfig: LLMConfig, modelConfig: ModelConfig | undefined) { |
| 18 | this.llmConfig = llmConfig |
| 19 | this.modelConfig = modelConfig |
| 20 | this.requestId = uuidv4() |
| 21 | } |
| 22 | |
| 23 | // 添加getter方法来暴露requestId |
| 24 | get id(): string { |
| 25 | return this.requestId |
| 26 | } |
| 27 | |
| 28 | async stopStreaming(): Promise<void> { |
| 29 | try { |
| 30 | this.isAborted = true |
| 31 | await window.api.ai.stopStreaming(this.requestId) |
| 32 | } catch (error) { |
| 33 | console.error('Failed to stop streaming:', error) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | async sendMessage(messages: ChatMessage[], callbacks: StreamingResponse): Promise<void> { |
| 38 | if (this.isAborted) { |
| 39 | callbacks.onError(new Error('Request was aborted')) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | try { |
| 44 | // 直接调用新接口,传递回调函数对象 |
| 45 | await window.api.ai.sendMessageStreaming( |
| 46 | { |
| 47 | requestId: this.requestId, |
| 48 | llmConfig: this.llmConfig, |
| 49 | modelConfig: this.modelConfig, |
| 50 | messages: messages |
| 51 | }, |
| 52 | { |
| 53 | onChunk: (chunk: string) => { |
| 54 | if (!this.isAborted) { |
| 55 | callbacks.onChunk(chunk) |
| 56 | } |
| 57 | }, |
| 58 | onReasoning: (reasoning: string) => { |
| 59 | if (!this.isAborted) { |
| 60 | callbacks.onReasoning?.(reasoning) |
| 61 | } |
| 62 | }, |
| 63 | onComplete: (fullResponse: string, reasoning?: string) => { |
| 64 | if (!this.isAborted) { |
| 65 | callbacks.onComplete(fullResponse, reasoning) |
| 66 | } |
| 67 | }, |
| 68 | onError: (error: string) => { |
nothing calls this directly
no outgoing calls
no test coverage detected