refactor(agent): 迁移AI会话引擎至pi-agent-core库
将原有基于Anthropic/OpenAI SDK的直播聊天代理重构为使用`@earendil-works/pi-agent-core`和`@earendil-works/pi-ai`库的统一API。 新增pi-bridge、pi-model、pi-persist、pi-tools四个模块,封装Agent路由、模型配置、消息持久化和工具适配逻辑。移除`chat.ts`中大量死代码,简化WebSocket处理流程。 BREAKING CHANGE: 移除`VideoAgent`类的`getAnthropicClient`、`getOpenAIClient`、`executeTool`等方法,外部调用需迁移至新pi-bridge API。`PROJECT_ROOT`路径计算方式变更,从`../../..`变为`../../`。
This commit is contained in:
57
web/server/agent/pi-model.ts
Normal file
57
web/server/agent/pi-model.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { registerBuiltInApiProviders } from '@earendil-works/pi-ai';
|
||||
import type { Model } from '@earendil-works/pi-ai';
|
||||
import { getDb } from '../db';
|
||||
|
||||
registerBuiltInApiProviders();
|
||||
|
||||
export interface PiModelConfig {
|
||||
model: Model<any>;
|
||||
apiKey: string;
|
||||
}
|
||||
|
||||
export function createPiModel(): PiModelConfig {
|
||||
const row = getDb().prepare('SELECT value FROM configs WHERE key = ?').get('api_keys') as { value: string } | undefined;
|
||||
|
||||
let apiKey = process.env.ANTHROPIC_API_KEY || '';
|
||||
let baseURL: string | undefined;
|
||||
let modelId = process.env.ANTHROPIC_MODEL || 'claude-sonnet-4-6';
|
||||
let protocol: 'anthropic' | 'openai' = 'anthropic';
|
||||
|
||||
if (row) {
|
||||
try {
|
||||
const cfg = JSON.parse(row.value);
|
||||
if (cfg.ANTHROPIC_AUTH_TOKEN) apiKey = cfg.ANTHROPIC_AUTH_TOKEN;
|
||||
if (cfg.ANTHROPIC_BASE_URL) baseURL = cfg.ANTHROPIC_BASE_URL;
|
||||
if (cfg.ANTHROPIC_MODEL) modelId = cfg.ANTHROPIC_MODEL;
|
||||
if (cfg.PROTOCOL === 'openai') protocol = 'openai';
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const model: Model<any> = protocol === 'openai'
|
||||
? {
|
||||
id: modelId,
|
||||
name: modelId,
|
||||
api: 'openai-completions',
|
||||
provider: 'openai',
|
||||
baseUrl: baseURL || 'https://api.openai.com/v1',
|
||||
reasoning: false,
|
||||
input: ['text'] as ('text' | 'image')[],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 128000,
|
||||
maxTokens: 8192,
|
||||
}
|
||||
: {
|
||||
id: modelId,
|
||||
name: modelId,
|
||||
api: 'anthropic-messages',
|
||||
provider: 'anthropic',
|
||||
baseUrl: baseURL || 'https://api.anthropic.com',
|
||||
reasoning: true,
|
||||
input: ['text', 'image'] as ('text' | 'image')[],
|
||||
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
||||
contextWindow: 200000,
|
||||
maxTokens: 8192,
|
||||
};
|
||||
|
||||
return { model, apiKey };
|
||||
}
|
||||
Reference in New Issue
Block a user