feat: 重构对话功能
This commit is contained in:
170
electron/gateway/handlers/chat.ts
Normal file
170
electron/gateway/handlers/chat.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { createProvider } from '@electron/providers';
|
||||
import type { BaseProvider } from '@electron/providers/BaseProvider';
|
||||
import { providerApiService } from '@electron/service/provider-api-service';
|
||||
import logManager from '@electron/service/logger';
|
||||
import type { RawMessage } from '@src/pages/home/model/ChatModel';
|
||||
import { sessionStore } from '../session-store';
|
||||
import type { GatewayEvent, GatewayRpcParams, GatewayRpcReturns } from '../types';
|
||||
|
||||
export interface GatewayChatMessage {
|
||||
role: 'system' | 'user' | 'assistant' | 'tool';
|
||||
content: string;
|
||||
}
|
||||
|
||||
function buildChatMessages(sessionMessages: RawMessage[]): GatewayChatMessage[] {
|
||||
return sessionMessages
|
||||
.map((msg): GatewayChatMessage | null => {
|
||||
if (!msg.role || !msg.content) return null;
|
||||
const role = msg.role;
|
||||
if (role === 'user' || role === 'assistant' || role === 'system') {
|
||||
return {
|
||||
role,
|
||||
content: typeof msg.content === 'string' ? msg.content : '',
|
||||
};
|
||||
}
|
||||
// Skip toolresult and unsupported roles for now
|
||||
return null;
|
||||
})
|
||||
.filter((m): m is GatewayChatMessage => m !== null);
|
||||
}
|
||||
|
||||
async function processChatStream(
|
||||
sessionKey: string,
|
||||
runId: string,
|
||||
provider: BaseProvider,
|
||||
model: string,
|
||||
messages: GatewayChatMessage[],
|
||||
signal: AbortSignal,
|
||||
broadcast: (event: GatewayEvent) => void
|
||||
) {
|
||||
let assistantContent = '';
|
||||
|
||||
try {
|
||||
const chunks = await provider.chat(messages, model, { signal });
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
if (signal.aborted) break;
|
||||
|
||||
if (chunk.result) {
|
||||
assistantContent += chunk.result;
|
||||
broadcast({
|
||||
type: 'chat:delta',
|
||||
sessionKey,
|
||||
runId,
|
||||
delta: chunk.result,
|
||||
});
|
||||
}
|
||||
|
||||
if (chunk.isEnd) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!signal.aborted) {
|
||||
const finalMessage: RawMessage = {
|
||||
role: 'assistant',
|
||||
content: assistantContent,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
sessionStore.appendMessage(sessionKey, finalMessage);
|
||||
sessionStore.clearActiveRun(sessionKey);
|
||||
|
||||
broadcast({
|
||||
type: 'chat:final',
|
||||
sessionKey,
|
||||
runId,
|
||||
message: finalMessage,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
sessionStore.clearActiveRun(sessionKey);
|
||||
broadcast({
|
||||
type: 'chat:error',
|
||||
sessionKey,
|
||||
runId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function handleChatSend(
|
||||
params: GatewayRpcParams['chat.send'],
|
||||
broadcast: (event: GatewayEvent) => void
|
||||
): GatewayRpcReturns['chat.send'] {
|
||||
const { sessionKey, message, options } = params;
|
||||
const runId = randomUUID();
|
||||
|
||||
// 1. Append user message
|
||||
sessionStore.appendMessage(sessionKey, {
|
||||
...message,
|
||||
timestamp: message.timestamp || Date.now(),
|
||||
});
|
||||
|
||||
// 2. Resolve provider account
|
||||
const accountId = options?.providerAccountId || providerApiService.getDefault().accountId;
|
||||
if (!accountId) {
|
||||
throw new Error('No provider account selected');
|
||||
}
|
||||
|
||||
const account = providerApiService.getAccounts().find((a) => a.id === accountId);
|
||||
if (!account) {
|
||||
throw new Error(`Provider account ${accountId} not found`);
|
||||
}
|
||||
|
||||
const model = account.model;
|
||||
if (!model) {
|
||||
throw new Error(`Provider account ${accountId} has no model configured`);
|
||||
}
|
||||
|
||||
// 3. Build messages array from session history
|
||||
const session = sessionStore.getOrCreate(sessionKey);
|
||||
const messages = buildChatMessages(session.messages);
|
||||
|
||||
// 4. Start streaming
|
||||
const abortController = new AbortController();
|
||||
sessionStore.setActiveRun(sessionKey, runId, abortController);
|
||||
|
||||
// Run async stream processing in background
|
||||
const provider = createProvider(accountId);
|
||||
processChatStream(sessionKey, runId, provider, model, messages, abortController.signal, broadcast).catch(
|
||||
(err) => {
|
||||
logManager.error('Unexpected error in processChatStream:', err);
|
||||
sessionStore.clearActiveRun(sessionKey);
|
||||
broadcast({
|
||||
type: 'chat:error',
|
||||
sessionKey,
|
||||
runId,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
return { runId };
|
||||
}
|
||||
|
||||
export function handleChatHistory(
|
||||
params: GatewayRpcParams['chat.history']
|
||||
): GatewayRpcReturns['chat.history'] {
|
||||
return sessionStore.getMessages(params.sessionKey, params.limit ?? 50);
|
||||
}
|
||||
|
||||
export function handleChatAbort(
|
||||
params: GatewayRpcParams['chat.abort'],
|
||||
broadcast: (event: GatewayEvent) => void
|
||||
): GatewayRpcReturns['chat.abort'] {
|
||||
const activeRun = sessionStore.getActiveRun(params.sessionKey);
|
||||
if (activeRun) {
|
||||
activeRun.abortController.abort();
|
||||
sessionStore.clearActiveRun(params.sessionKey);
|
||||
broadcast({
|
||||
type: 'chat:aborted',
|
||||
sessionKey: params.sessionKey,
|
||||
runId: activeRun.runId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function handleSessionList(): GatewayRpcReturns['session.list'] {
|
||||
return sessionStore.getAllKeys();
|
||||
}
|
||||
13
electron/gateway/handlers/provider.ts
Normal file
13
electron/gateway/handlers/provider.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { providerApiService } from '@electron/service/provider-api-service';
|
||||
import type { GatewayRpcReturns } from '../types';
|
||||
|
||||
export function handleProviderList(): GatewayRpcReturns['provider.list'] {
|
||||
return {
|
||||
accounts: providerApiService.getAccounts(),
|
||||
defaultAccountId: providerApiService.getDefault().accountId,
|
||||
};
|
||||
}
|
||||
|
||||
export function handleProviderGetDefault(): GatewayRpcReturns['provider.getDefault'] {
|
||||
return providerApiService.getDefault();
|
||||
}
|
||||
59
electron/gateway/manager.ts
Normal file
59
electron/gateway/manager.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { BrowserWindow } from 'electron';
|
||||
import { windowManager } from '@electron/service/window-service';
|
||||
import logManager from '@electron/service/logger';
|
||||
import type { GatewayEvent } from './types';
|
||||
import * as chatHandlers from './handlers/chat';
|
||||
import * as providerHandlers from './handlers/provider';
|
||||
|
||||
class GatewayManager {
|
||||
private initialized = false;
|
||||
|
||||
async init(): Promise<void> {
|
||||
if (this.initialized) return;
|
||||
this.initialized = true;
|
||||
logManager.info('GatewayManager initialized');
|
||||
this.broadcast({ type: 'gateway:status', status: 'connected' });
|
||||
}
|
||||
|
||||
async rpc(method: string, params: any): Promise<any> {
|
||||
if (!this.initialized) {
|
||||
await this.init();
|
||||
}
|
||||
|
||||
logManager.info(`Gateway RPC: ${method}`, params);
|
||||
|
||||
switch (method) {
|
||||
case 'chat.send':
|
||||
return chatHandlers.handleChatSend(params, (event) => this.broadcast(event));
|
||||
case 'chat.history':
|
||||
return chatHandlers.handleChatHistory(params);
|
||||
case 'chat.abort':
|
||||
return chatHandlers.handleChatAbort(params, (event) => this.broadcast(event));
|
||||
case 'session.list':
|
||||
return chatHandlers.handleSessionList();
|
||||
case 'provider.list':
|
||||
return providerHandlers.handleProviderList();
|
||||
case 'provider.getDefault':
|
||||
return providerHandlers.handleProviderGetDefault();
|
||||
default:
|
||||
throw new Error(`Unknown gateway RPC method: ${method}`);
|
||||
}
|
||||
}
|
||||
|
||||
broadcast(event: GatewayEvent): void {
|
||||
const mainWindow = BrowserWindow.getAllWindows().find(
|
||||
(win) => windowManager.getName(win) === 'main'
|
||||
);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.webContents.send('gateway:event', event);
|
||||
}
|
||||
}
|
||||
|
||||
reloadProviders(): void {
|
||||
logManager.info('GatewayManager reloading providers');
|
||||
// For now, providers are resolved on each chat.send call,
|
||||
// so no in-memory cache to invalidate. Future: notify active sessions.
|
||||
}
|
||||
}
|
||||
|
||||
export const gatewayManager = new GatewayManager();
|
||||
133
electron/gateway/session-store.ts
Normal file
133
electron/gateway/session-store.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { app } from 'electron';
|
||||
import logManager from '@electron/service/logger';
|
||||
import type { RawMessage } from '@src/pages/home/model/ChatModel';
|
||||
|
||||
let sessionsFilePath: string | null = null;
|
||||
|
||||
function getSessionsFilePath(): string {
|
||||
if (!sessionsFilePath) {
|
||||
sessionsFilePath = path.join(app.getPath('userData'), 'chat-sessions.json');
|
||||
}
|
||||
return sessionsFilePath;
|
||||
}
|
||||
|
||||
export interface SessionEntry {
|
||||
key: string;
|
||||
messages: RawMessage[];
|
||||
updatedAt: number;
|
||||
activeRun?: {
|
||||
runId: string;
|
||||
abortController: AbortController;
|
||||
};
|
||||
}
|
||||
|
||||
class SessionStore {
|
||||
private sessions = new Map<string, SessionEntry>();
|
||||
private loaded = false;
|
||||
|
||||
private ensureLoaded(): void {
|
||||
if (this.loaded) return;
|
||||
this.loaded = true;
|
||||
this.loadFromDisk();
|
||||
}
|
||||
|
||||
private loadFromDisk(): void {
|
||||
try {
|
||||
const filePath = getSessionsFilePath();
|
||||
if (fs.existsSync(filePath)) {
|
||||
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record<
|
||||
string,
|
||||
Omit<SessionEntry, 'activeRun'>
|
||||
>;
|
||||
for (const [key, entry] of Object.entries(data)) {
|
||||
this.sessions.set(key, {
|
||||
...entry,
|
||||
activeRun: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logManager.error('Failed to load sessions from disk:', e);
|
||||
}
|
||||
}
|
||||
|
||||
saveToDisk(): void {
|
||||
try {
|
||||
const filePath = getSessionsFilePath();
|
||||
const data: Record<string, Omit<SessionEntry, 'activeRun'>> = {};
|
||||
for (const [key, entry] of this.sessions) {
|
||||
data[key] = {
|
||||
key: entry.key,
|
||||
messages: entry.messages,
|
||||
updatedAt: entry.updatedAt,
|
||||
};
|
||||
}
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
||||
} catch (e) {
|
||||
logManager.error('Failed to save sessions to disk:', e);
|
||||
}
|
||||
}
|
||||
|
||||
getOrCreate(key: string): SessionEntry {
|
||||
this.ensureLoaded();
|
||||
let session = this.sessions.get(key);
|
||||
if (!session) {
|
||||
session = {
|
||||
key,
|
||||
messages: [],
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
this.sessions.set(key, session);
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
get(key: string): SessionEntry | undefined {
|
||||
this.ensureLoaded();
|
||||
return this.sessions.get(key);
|
||||
}
|
||||
|
||||
getAllKeys(): string[] {
|
||||
this.ensureLoaded();
|
||||
return Array.from(this.sessions.keys());
|
||||
}
|
||||
|
||||
appendMessage(key: string, message: RawMessage): void {
|
||||
const session = this.getOrCreate(key);
|
||||
session.messages.push(message);
|
||||
session.updatedAt = Date.now();
|
||||
this.saveToDisk();
|
||||
}
|
||||
|
||||
getMessages(key: string, limit = 50): RawMessage[] {
|
||||
const session = this.get(key);
|
||||
if (!session) return [];
|
||||
return session.messages.slice(-limit);
|
||||
}
|
||||
|
||||
setActiveRun(key: string, runId: string, abortController: AbortController): void {
|
||||
const session = this.getOrCreate(key);
|
||||
session.activeRun = { runId, abortController };
|
||||
}
|
||||
|
||||
clearActiveRun(key: string): void {
|
||||
const session = this.sessions.get(key);
|
||||
if (session) {
|
||||
session.activeRun = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
getActiveRun(key: string): { runId: string; abortController: AbortController } | undefined {
|
||||
return this.sessions.get(key)?.activeRun;
|
||||
}
|
||||
|
||||
deleteSession(key: string): void {
|
||||
this.sessions.delete(key);
|
||||
this.saveToDisk();
|
||||
}
|
||||
}
|
||||
|
||||
export const sessionStore = new SessionStore();
|
||||
62
electron/gateway/types.ts
Normal file
62
electron/gateway/types.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { RawMessage } from '@src/pages/home/model/ChatModel';
|
||||
|
||||
/// Gateway 向 Renderer 推送的事件类型
|
||||
export type GatewayEvent =
|
||||
| {
|
||||
type: 'chat:delta';
|
||||
sessionKey: string;
|
||||
runId: string;
|
||||
delta: string;
|
||||
}
|
||||
| {
|
||||
type: 'chat:final';
|
||||
sessionKey: string;
|
||||
runId: string;
|
||||
message: RawMessage;
|
||||
}
|
||||
| {
|
||||
type: 'chat:error';
|
||||
sessionKey: string;
|
||||
runId: string;
|
||||
error: string;
|
||||
}
|
||||
| {
|
||||
type: 'chat:aborted';
|
||||
sessionKey: string;
|
||||
runId: string;
|
||||
}
|
||||
| {
|
||||
type: 'gateway:status';
|
||||
status: 'connected' | 'disconnected' | 'reconnecting';
|
||||
};
|
||||
|
||||
/// Gateway RPC 方法参数映射
|
||||
export interface GatewayRpcParams {
|
||||
'chat.send': {
|
||||
sessionKey: string;
|
||||
message: RawMessage;
|
||||
options?: {
|
||||
providerAccountId?: string;
|
||||
};
|
||||
};
|
||||
'chat.history': {
|
||||
sessionKey: string;
|
||||
limit?: number;
|
||||
};
|
||||
'chat.abort': {
|
||||
sessionKey: string;
|
||||
};
|
||||
'session.list': Record<string, never>;
|
||||
'provider.list': Record<string, never>;
|
||||
'provider.getDefault': Record<string, never>;
|
||||
}
|
||||
|
||||
/// Gateway RPC 方法返回值映射
|
||||
export interface GatewayRpcReturns {
|
||||
'chat.send': { runId: string };
|
||||
'chat.history': RawMessage[];
|
||||
'chat.abort': void;
|
||||
'session.list': string[];
|
||||
'provider.list': { accounts: any[]; defaultAccountId: string | null };
|
||||
'provider.getDefault': { accountId: string | null };
|
||||
}
|
||||
131
electron/main.ts
131
electron/main.ts
@@ -1,4 +1,4 @@
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import { app, BrowserWindow, ipcMain } from 'electron'
|
||||
import { CONFIG_KEYS } from '@lib/constants'
|
||||
import { setupMainWindow } from './wins';
|
||||
import started from 'electron-squirrel-startup'
|
||||
@@ -8,10 +8,133 @@ import { initScriptStoreService } from '@electron/service/script-store-service'
|
||||
import log from 'electron-log';
|
||||
import 'bytenode'; // Ensure bytenode is bundled/externalized correctly
|
||||
import { appUpdater } from '@electron/service/updater';
|
||||
import axios from 'axios';
|
||||
import { providerApiService, onProviderChange } from '@electron/service/provider-api-service';
|
||||
import { gatewayManager } from '@electron/gateway/manager';
|
||||
|
||||
// 初始化 updater,确保在 app ready 之前或者之中注册好 IPC
|
||||
appUpdater.init();
|
||||
|
||||
// 注册 hostapi:fetch IPC 代理
|
||||
// 模型管理相关接口在本地处理(对齐 ClawX),其余接口代理到远端后端
|
||||
const HOST_API_BASE_URL = process.env.VITE_SERVICE_URL || 'http://8.138.234.141/ingress';
|
||||
|
||||
async function handleLocalProviderApi(path: string, method: string, body: any) {
|
||||
const parsedBody = typeof body === 'string' && body ? JSON.parse(body) : body;
|
||||
|
||||
if (path === '/api/provider-vendors' && method === 'GET') {
|
||||
return { success: true, ok: true, json: providerApiService.getVendors(), data: providerApiService.getVendors() };
|
||||
}
|
||||
if (path === '/api/provider-accounts' && method === 'GET') {
|
||||
return { success: true, ok: true, json: providerApiService.getAccounts(), data: providerApiService.getAccounts() };
|
||||
}
|
||||
if (path === '/api/providers' && method === 'GET') {
|
||||
return { success: true, ok: true, json: providerApiService.getProviders(), data: providerApiService.getProviders() };
|
||||
}
|
||||
if (path === '/api/provider-accounts/default' && method === 'GET') {
|
||||
return { success: true, ok: true, json: providerApiService.getDefault(), data: providerApiService.getDefault() };
|
||||
}
|
||||
if (path === '/api/provider-accounts' && method === 'POST') {
|
||||
const result = providerApiService.createAccount(parsedBody || {});
|
||||
return { success: true, ok: true, json: result, data: result };
|
||||
}
|
||||
if (path === '/api/provider-accounts/default' && method === 'PUT') {
|
||||
const result = providerApiService.setDefault(parsedBody || {});
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path.startsWith('/api/provider-accounts/') && method === 'PUT') {
|
||||
const id = decodeURIComponent(path.replace('/api/provider-accounts/', ''));
|
||||
const result = providerApiService.updateAccount(id, parsedBody || {});
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path.startsWith('/api/provider-accounts/') && method === 'DELETE') {
|
||||
const id = decodeURIComponent(path.replace('/api/provider-accounts/', ''));
|
||||
const result = providerApiService.deleteAccount(id);
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path === '/api/providers/default' && method === 'PUT') {
|
||||
const result = providerApiService.setDefault({ accountId: parsedBody?.providerId });
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path.startsWith('/api/providers/') && path.endsWith('/api-key') && method === 'GET') {
|
||||
const id = decodeURIComponent(path.replace('/api/providers/', '').replace('/api-key', ''));
|
||||
const result = providerApiService.getApiKey(id);
|
||||
return { success: true, ok: true, json: result, data: result };
|
||||
}
|
||||
if (path.startsWith('/api/providers/') && method === 'PUT') {
|
||||
// Provider updates are mapped to account updates for local storage
|
||||
const id = decodeURIComponent(path.replace('/api/providers/', ''));
|
||||
const result = providerApiService.updateAccount(id, parsedBody || {});
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path.startsWith('/api/providers/') && method === 'DELETE') {
|
||||
const [rawId, query] = path.replace('/api/providers/', '').split('?');
|
||||
const id = decodeURIComponent(rawId);
|
||||
if (query && query.includes('apiKeyOnly=1')) {
|
||||
const result = providerApiService.deleteApiKey(id);
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
const result = providerApiService.deleteAccount(id);
|
||||
return { success: result.success, ok: result.success, json: result, data: result };
|
||||
}
|
||||
if (path === '/api/providers/validate' && method === 'POST') {
|
||||
const result = await providerApiService.validateApiKey(parsedBody || {});
|
||||
return { success: true, ok: true, json: result, data: result };
|
||||
}
|
||||
if (path === '/api/usage/recent-token-history' && method === 'GET') {
|
||||
return { success: true, ok: true, json: providerApiService.getUsageHistory(), data: providerApiService.getUsageHistory() };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
ipcMain.handle('hostapi:fetch', async (_event, { path, method, headers, body }) => {
|
||||
// 1. 优先本地处理模型管理接口
|
||||
const localResult = await handleLocalProviderApi(path, method || 'GET', body);
|
||||
if (localResult) return localResult;
|
||||
|
||||
// 2. 其余接口代理到远端后端
|
||||
const url = `${HOST_API_BASE_URL}${path}`;
|
||||
try {
|
||||
const response = await axios({
|
||||
url,
|
||||
method: method || 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers,
|
||||
},
|
||||
data: body ?? undefined,
|
||||
timeout: 30000,
|
||||
});
|
||||
return {
|
||||
success: true,
|
||||
ok: true,
|
||||
json: response.data,
|
||||
data: response.data,
|
||||
};
|
||||
} catch (error: any) {
|
||||
if (error.response) {
|
||||
return {
|
||||
success: false,
|
||||
ok: false,
|
||||
status: error.response.status,
|
||||
error: error.response.data?.message || error.message,
|
||||
text: error.response.statusText,
|
||||
data: error.response.data,
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
ok: false,
|
||||
error: error.message || 'Unknown error',
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Gateway RPC IPC handler
|
||||
ipcMain.handle('gateway:rpc', async (_event, method: string, params: any) => {
|
||||
return gatewayManager.rpc(method, params);
|
||||
});
|
||||
|
||||
// import logManager from '@electron/service/logger'
|
||||
|
||||
|
||||
@@ -29,6 +152,12 @@ if (started) {
|
||||
// });
|
||||
|
||||
app.whenReady().then(() => {
|
||||
gatewayManager.init();
|
||||
|
||||
onProviderChange(() => {
|
||||
gatewayManager.reloadProviders();
|
||||
});
|
||||
|
||||
setupMainWindow();
|
||||
|
||||
// 初始化脚本存储服务
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
export interface ChatOptions {
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export interface GatewayChatMessage {
|
||||
role: 'system' | 'user' | 'assistant' | 'tool';
|
||||
content: string;
|
||||
}
|
||||
|
||||
export abstract class BaseProvider {
|
||||
abstract chat(messages: DialogueMessageProps[], modelName: string): Promise<AsyncIterable<UniversalChunk>>
|
||||
abstract chat(messages: GatewayChatMessage[], modelName: string, options?: ChatOptions): Promise<AsyncIterable<UniversalChunk>>
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,123 +1,34 @@
|
||||
import type { Provider } from "@lib/types"
|
||||
import { OpenAIProvider } from "./OpenAIProvider"
|
||||
import { parseOpenAISetting } from '@lib/utils'
|
||||
import { decode } from 'js-base64'
|
||||
import { configManager } from '@electron/service/config-service'
|
||||
import { logManager } from '@electron/service/logger'
|
||||
import { CONFIG_KEYS } from "@lib/constants"
|
||||
import { BaseProvider } from "./BaseProvider";
|
||||
import { OpenAIProvider } from "./OpenAIProvider";
|
||||
import { providerApiService } from '@electron/service/provider-api-service';
|
||||
import { getProviderTypeInfo } from '@lib/providers';
|
||||
|
||||
const providers = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'bigmodel',
|
||||
title: '智谱AI',
|
||||
models: ['glm-4.5-flash'],
|
||||
openAISetting: {
|
||||
baseURL: 'https://open.bigmodel.cn/api/paas/v4',
|
||||
apiKey: process.env.BIGMODEL_API_KEY || '',
|
||||
},
|
||||
createdAt: new Date().getTime(),
|
||||
updatedAt: new Date().getTime()
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'deepseek',
|
||||
title: '深度求索 (DeepSeek)',
|
||||
models: ['deepseek-chat'],
|
||||
openAISetting: {
|
||||
baseURL: 'https://api.deepseek.com/v1',
|
||||
apiKey: process.env.DEEPSEEK_API_KEY || '',
|
||||
},
|
||||
createdAt: new Date().getTime(),
|
||||
updatedAt: new Date().getTime()
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: 'siliconflow',
|
||||
title: '硅基流动',
|
||||
models: ['Qwen/Qwen3-8B', 'deepseek-ai/DeepSeek-R1-0528-Qwen3-8B'],
|
||||
openAISetting: {
|
||||
baseURL: 'https://api.siliconflow.cn/v1',
|
||||
apiKey: process.env.SILICONFLOW_API_KEY || '',
|
||||
},
|
||||
createdAt: new Date().getTime(),
|
||||
updatedAt: new Date().getTime()
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: 'qianfan',
|
||||
title: '百度千帆',
|
||||
models: ['ernie-speed-128k', 'ernie-4.0-8k', 'ernie-3.5-8k'],
|
||||
openAISetting: {
|
||||
baseURL: 'https://qianfan.baidubce.com/v2',
|
||||
apiKey: process.env.QIANFAN_API_KEY || '',
|
||||
},
|
||||
createdAt: new Date().getTime(),
|
||||
updatedAt: new Date().getTime()
|
||||
},
|
||||
];
|
||||
|
||||
interface _Provider extends Omit<Provider, 'openAISetting'> {
|
||||
openAISetting?: {
|
||||
apiKey: string,
|
||||
baseURL: string,
|
||||
};
|
||||
}
|
||||
|
||||
const _parseProvider = () => {
|
||||
let result: Provider[] = [];
|
||||
let isBase64Parsed = false;
|
||||
const providerConfig = configManager.get(CONFIG_KEYS.PROVIDER);
|
||||
|
||||
const mapCallback = (provider: Provider) => ({
|
||||
...provider,
|
||||
openAISetting: typeof provider.openAISetting === 'string'
|
||||
? parseOpenAISetting(provider.openAISetting ?? '')
|
||||
: provider.openAISetting,
|
||||
})
|
||||
|
||||
try {
|
||||
result = JSON.parse(decode(providerConfig)) as Provider[];
|
||||
isBase64Parsed = true;
|
||||
} catch (error) {
|
||||
logManager.error(`parse base64 provider failed: ${error}`);
|
||||
export function createProvider(accountId: string): BaseProvider {
|
||||
const account = providerApiService.getAccounts().find((a) => a.id === accountId);
|
||||
if (!account) {
|
||||
throw new Error(`Provider account ${accountId} not found`);
|
||||
}
|
||||
|
||||
if (!isBase64Parsed) try {
|
||||
result = JSON.parse(providerConfig) as Provider[]
|
||||
} catch (error) {
|
||||
logManager.error(`parse provider failed: ${error}`);
|
||||
const apiKeyResult = providerApiService.getApiKey(accountId);
|
||||
const apiKey = apiKeyResult.apiKey;
|
||||
if (!apiKey) {
|
||||
throw new Error(`API key for account ${accountId} not found`);
|
||||
}
|
||||
|
||||
if (!result.length) return;
|
||||
const baseURL = account.baseUrl || getProviderTypeInfo(account.vendorId)?.defaultBaseUrl;
|
||||
if (!baseURL) {
|
||||
throw new Error(`Base URL for account ${accountId} not found`);
|
||||
}
|
||||
|
||||
return result.map(mapCallback) as _Provider[]
|
||||
}
|
||||
|
||||
const getProviderConfig = () => {
|
||||
try {
|
||||
return _parseProvider();
|
||||
} catch (error) {
|
||||
logManager.error(`get provider config failed: ${error}`);
|
||||
return null;
|
||||
switch (account.apiProtocol) {
|
||||
case 'anthropic-messages':
|
||||
throw new Error('Anthropic provider not yet implemented');
|
||||
case 'openai-completions':
|
||||
case 'openai-responses':
|
||||
default:
|
||||
return new OpenAIProvider(apiKey, baseURL, account.headers);
|
||||
}
|
||||
}
|
||||
|
||||
export function createProvider(name: string) {
|
||||
const providers = getProviderConfig();
|
||||
|
||||
if (!providers) {
|
||||
throw new Error('provider config not found');
|
||||
}
|
||||
|
||||
for (const provider of providers) {
|
||||
if (provider.name === name) {
|
||||
if (!provider.openAISetting?.apiKey || !provider.openAISetting?.baseURL) {
|
||||
throw new Error('apiKey or baseURL not found');
|
||||
}
|
||||
// TODO: visible
|
||||
|
||||
return new OpenAIProvider(provider.openAISetting.apiKey, provider.openAISetting.baseURL);
|
||||
}
|
||||
}
|
||||
}
|
||||
export * from './BaseProvider';
|
||||
export { OpenAIProvider };
|
||||
|
||||
239
electron/service/provider-api-service/index.ts
Normal file
239
electron/service/provider-api-service/index.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
import { app } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import logManager from '@electron/service/logger';
|
||||
import { PROVIDER_TYPE_INFO } from '@lib/providers';
|
||||
import type {
|
||||
ProviderAccount,
|
||||
ProviderVendorInfo,
|
||||
ProviderWithKeyInfo,
|
||||
} from '@lib/providers';
|
||||
|
||||
interface ProviderStore {
|
||||
accounts: ProviderAccount[];
|
||||
defaultAccountId: string | null;
|
||||
}
|
||||
|
||||
const defaultStore: ProviderStore = {
|
||||
accounts: [],
|
||||
defaultAccountId: null,
|
||||
};
|
||||
|
||||
const storePath = path.join(app.getPath('userData'), 'provider-accounts.json');
|
||||
const keysPath = path.join(app.getPath('userData'), 'provider-keys.json');
|
||||
|
||||
function readJson<T>(filePath: string, defaultValue: T): T {
|
||||
try {
|
||||
if (fs.existsSync(filePath)) {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
||||
}
|
||||
} catch (e) {
|
||||
logManager.error(`Failed to read ${filePath}:`, e);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
function writeJson(filePath: string, data: unknown) {
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
||||
} catch (e) {
|
||||
logManager.error(`Failed to write ${filePath}:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
function getStore(): ProviderStore {
|
||||
return readJson(storePath, defaultStore);
|
||||
}
|
||||
|
||||
function saveStore(store: ProviderStore) {
|
||||
writeJson(storePath, store);
|
||||
}
|
||||
|
||||
function getKeys(): Record<string, string> {
|
||||
return readJson(keysPath, {});
|
||||
}
|
||||
|
||||
function saveKeys(keys: Record<string, string>) {
|
||||
writeJson(keysPath, keys);
|
||||
}
|
||||
|
||||
function mapToProviderWithKeyInfo(account: ProviderAccount): ProviderWithKeyInfo {
|
||||
const keys = getKeys();
|
||||
const hasKey = !!keys[account.id];
|
||||
return {
|
||||
id: account.id,
|
||||
name: account.label,
|
||||
type: account.vendorId as any,
|
||||
baseUrl: account.baseUrl,
|
||||
apiProtocol: account.apiProtocol,
|
||||
headers: account.headers,
|
||||
model: account.model,
|
||||
fallbackModels: account.fallbackModels,
|
||||
fallbackProviderIds: account.fallbackAccountIds,
|
||||
enabled: account.enabled,
|
||||
createdAt: account.createdAt,
|
||||
updatedAt: account.updatedAt,
|
||||
hasKey,
|
||||
keyMasked: hasKey ? '••••••••' : null,
|
||||
};
|
||||
}
|
||||
|
||||
type ProviderChangeListener = () => void;
|
||||
const listeners: ProviderChangeListener[] = [];
|
||||
|
||||
export function onProviderChange(listener: ProviderChangeListener): () => void {
|
||||
listeners.push(listener);
|
||||
return () => {
|
||||
const idx = listeners.indexOf(listener);
|
||||
if (idx > -1) listeners.splice(idx, 1);
|
||||
};
|
||||
}
|
||||
|
||||
function notifyChange() {
|
||||
listeners.forEach((l) => l());
|
||||
}
|
||||
|
||||
function mapToVendorInfo(info: typeof PROVIDER_TYPE_INFO[number]): ProviderVendorInfo {
|
||||
return {
|
||||
...info,
|
||||
category:
|
||||
info.id === 'ollama'
|
||||
? 'local'
|
||||
: info.id === 'custom'
|
||||
? 'custom'
|
||||
: 'compatible',
|
||||
supportedAuthModes: info.requiresApiKey
|
||||
? info.isOAuth
|
||||
? ['api_key', 'oauth_browser']
|
||||
: ['api_key']
|
||||
: info.isOAuth
|
||||
? ['local', 'oauth_browser']
|
||||
: ['local'],
|
||||
defaultAuthMode: info.requiresApiKey ? 'api_key' : 'local',
|
||||
supportsMultipleAccounts: true,
|
||||
} as ProviderVendorInfo;
|
||||
}
|
||||
|
||||
function sanitizeAccount(account: ProviderAccount): ProviderAccount {
|
||||
let model = account.model;
|
||||
if (model) {
|
||||
// Fix corrupted DeepSeek model IDs stored in legacy accounts
|
||||
if (model === 'deepseek-chat/deepseek-reasoner' || model.startsWith('deepseek-chat/')) {
|
||||
model = 'deepseek-chat';
|
||||
} else if (model.startsWith('deepseek-reasoner/')) {
|
||||
model = 'deepseek-reasoner';
|
||||
}
|
||||
}
|
||||
if (model !== account.model) {
|
||||
return { ...account, model };
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
export const providerApiService = {
|
||||
getVendors(): ProviderVendorInfo[] {
|
||||
return PROVIDER_TYPE_INFO.map(mapToVendorInfo);
|
||||
},
|
||||
|
||||
getAccounts(): ProviderAccount[] {
|
||||
return getStore().accounts.map(sanitizeAccount);
|
||||
},
|
||||
|
||||
getProviders(): ProviderWithKeyInfo[] {
|
||||
return getStore().accounts.map(sanitizeAccount).map(mapToProviderWithKeyInfo);
|
||||
},
|
||||
|
||||
getDefault(): { accountId: string | null } {
|
||||
return { accountId: getStore().defaultAccountId };
|
||||
},
|
||||
|
||||
createAccount(body: { account: ProviderAccount; apiKey?: string }) {
|
||||
const store = getStore();
|
||||
const account = { ...body.account, updatedAt: new Date().toISOString() };
|
||||
store.accounts.push(account);
|
||||
if (body.apiKey) {
|
||||
const keys = getKeys();
|
||||
keys[account.id] = body.apiKey;
|
||||
saveKeys(keys);
|
||||
}
|
||||
saveStore(store);
|
||||
notifyChange();
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
updateAccount(
|
||||
accountId: string,
|
||||
body: { updates: Partial<ProviderAccount>; apiKey?: string }
|
||||
) {
|
||||
const store = getStore();
|
||||
const idx = store.accounts.findIndex((a) => a.id === accountId);
|
||||
if (idx === -1) return { success: false, error: 'Account not found' };
|
||||
store.accounts[idx] = {
|
||||
...store.accounts[idx],
|
||||
...body.updates,
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
if (body.apiKey) {
|
||||
const keys = getKeys();
|
||||
keys[accountId] = body.apiKey;
|
||||
saveKeys(keys);
|
||||
}
|
||||
saveStore(store);
|
||||
notifyChange();
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
deleteAccount(accountId: string) {
|
||||
const store = getStore();
|
||||
store.accounts = store.accounts.filter((a) => a.id !== accountId);
|
||||
if (store.defaultAccountId === accountId) store.defaultAccountId = null;
|
||||
saveStore(store);
|
||||
const keys = getKeys();
|
||||
delete keys[accountId];
|
||||
saveKeys(keys);
|
||||
notifyChange();
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
setDefault(body: { accountId: string }) {
|
||||
const store = getStore();
|
||||
const accountExists = store.accounts.some((a) => a.id === body.accountId);
|
||||
if (!accountExists) {
|
||||
return { success: false, error: 'Account not found' };
|
||||
}
|
||||
store.defaultAccountId = body.accountId;
|
||||
saveStore(store);
|
||||
notifyChange();
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
validateApiKey(body: {
|
||||
providerId: string;
|
||||
apiKey: string;
|
||||
options?: { baseUrl?: string; apiProtocol?: string };
|
||||
}) {
|
||||
if (!body.apiKey || body.apiKey.trim().length === 0) {
|
||||
return { valid: false, error: 'API key is required' };
|
||||
}
|
||||
// TODO: perform real validation against provider endpoint
|
||||
return { valid: true };
|
||||
},
|
||||
|
||||
getApiKey(providerId: string) {
|
||||
const keys = getKeys();
|
||||
return { apiKey: keys[providerId] || null };
|
||||
},
|
||||
|
||||
deleteApiKey(accountId: string) {
|
||||
const keys = getKeys();
|
||||
delete keys[accountId];
|
||||
saveKeys(keys);
|
||||
notifyChange();
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
getUsageHistory() {
|
||||
return [] as any[];
|
||||
},
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { BrowserWindow } from 'electron'
|
||||
import { ipcMain } from 'electron';
|
||||
import { WINDOW_NAMES, MAIN_WIN_SIZE, IPC_EVENTS, MENU_IDS, CONVERSATION_ITEM_MENU_IDS, CONVERSATION_LIST_MENU_IDS, MESSAGE_ITEM_MENU_IDS, CONFIG_KEYS } from '@lib/constants'
|
||||
import { createProvider } from '../providers'
|
||||
import { windowManager } from '@electron/service/window-service'
|
||||
import { menuManager } from '@electron/service/menu-service'
|
||||
import { logManager } from '@electron/service/logger'
|
||||
@@ -120,43 +119,4 @@ export function setupMainWindow() {
|
||||
});
|
||||
|
||||
windowManager.create(WINDOW_NAMES.MAIN, MAIN_WIN_SIZE);
|
||||
|
||||
ipcMain.on(IPC_EVENTS.START_A_DIALOGUE, async (_event, props: CreateDialogueProps) => {
|
||||
const { providerName, messages, messageId, selectedModel } = props;
|
||||
const mainWindow = windowManager.get(WINDOW_NAMES.MAIN);
|
||||
|
||||
if (!mainWindow) {
|
||||
throw new Error('mainWindow not found');
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const provider = createProvider(providerName);
|
||||
const chunks = await provider?.chat(messages, selectedModel);
|
||||
|
||||
if (!chunks) {
|
||||
throw new Error('chunks or stream not found');
|
||||
}
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
const chunkContent = {
|
||||
messageId,
|
||||
data: chunk
|
||||
}
|
||||
mainWindow.webContents.send(IPC_EVENTS.START_A_DIALOGUE + 'back' + messageId, chunkContent);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
const errorContent = {
|
||||
messageId,
|
||||
data: {
|
||||
isEnd: true,
|
||||
isError: true,
|
||||
result: error instanceof Error ? error.message : String(error),
|
||||
}
|
||||
}
|
||||
|
||||
mainWindow.webContents.send(IPC_EVENTS.START_A_DIALOGUE + 'back' + messageId, errorContent);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user