feat: 重构对话功能

This commit is contained in:
DEV_DSW
2026-04-14 17:02:20 +08:00
parent b3f07c4cfe
commit c61e41049f
53 changed files with 5200 additions and 1982 deletions

View File

@@ -1,13 +1,12 @@
import { BaseProvider } from "./BaseProvider";
import { BaseProvider, ChatOptions, GatewayChatMessage } from "./BaseProvider";
import OpenAI from "openai";
import logManager from "@electron/service/logger"
function _transformChunk(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): UniversalChunk {
const choice = chunk.choices[0];
return {
isEnd: choice?.finish_reason === 'stop',
isEnd: choice?.finish_reason != null,
result: choice?.delta?.content ?? '',
}
}
@@ -15,12 +14,12 @@ function _transformChunk(chunk: OpenAI.Chat.Completions.ChatCompletionChunk): Un
export class OpenAIProvider extends BaseProvider {
private client: OpenAI;
constructor(apiKey: string, baseURL: string) {
constructor(apiKey: string, baseURL: string, headers?: Record<string, string>) {
super();
this.client = new OpenAI({ apiKey, baseURL });
this.client = new OpenAI({ apiKey, baseURL, defaultHeaders: headers });
}
async chat(messages: DialogueMessageProps[], model: string): Promise<AsyncIterable<UniversalChunk>> {
async chat(messages: GatewayChatMessage[], model: string, options?: ChatOptions): Promise<AsyncIterable<UniversalChunk>> {
const startTime = Date.now();
const lastMessage = messages[messages.length - 1];
@@ -34,17 +33,25 @@ export class OpenAIProvider extends BaseProvider {
try {
const chunks = await this.client.chat.completions.create({
model,
messages,
messages: messages as any,
stream: true,
}, {
signal: options?.signal,
});
const responseTime = Date.now() - startTime;
logManager.logApiResponse('chat.completions.create', { success: true }, 200, responseTime);
// return chunk;
return {
async *[Symbol.asyncIterator]() {
for await (const chunk of chunks) {
yield _transformChunk(chunk);
try {
for await (const chunk of chunks) {
if (options?.signal?.aborted) break;
yield _transformChunk(chunk);
}
const responseTime = Date.now() - startTime;
logManager.logApiResponse('chat.completions.create', { success: true }, 200, responseTime);
} catch (error) {
const responseTime = Date.now() - startTime;
logManager.logApiResponse('chat.completions.create', { error: error instanceof Error ? error.message : String(error) }, 500, responseTime);
throw error;
}
}
}