将原有基于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`路径计算方式变更,从`../../..`变为`../../`。
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { Router } from 'express';
|
|
import fs from 'fs/promises';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const PROJECT_ROOT = path.resolve(__dirname, '..', '..', '..');
|
|
|
|
export const promptsRouter = Router();
|
|
|
|
const PROMPT_FILES: Record<string, string> = {
|
|
storyboard: 'prompts/分镜.md',
|
|
image: 'prompts/图片提示词.md',
|
|
video: 'prompts/视频提示词.md',
|
|
};
|
|
|
|
promptsRouter.get('/:accountId/:type', async (req, res) => {
|
|
const { accountId, type } = req.params;
|
|
const relPath = PROMPT_FILES[type];
|
|
if (!relPath) return res.status(400).json({ error: 'Unknown type: ' + type });
|
|
|
|
const fullPath = path.join(PROJECT_ROOT, 'accounts', accountId, relPath);
|
|
try {
|
|
const content = await fs.readFile(fullPath, 'utf-8');
|
|
res.json({ path: relPath, content });
|
|
} catch {
|
|
res.status(404).json({ error: 'File not found' });
|
|
}
|
|
});
|
|
|
|
promptsRouter.put('/:accountId/:type', async (req, res) => {
|
|
const { accountId, type } = req.params;
|
|
const { content } = req.body;
|
|
const relPath = PROMPT_FILES[type];
|
|
if (!relPath) return res.status(400).json({ error: 'Unknown type: ' + type });
|
|
|
|
const fullPath = path.join(PROJECT_ROOT, 'accounts', accountId, relPath);
|
|
const dir = path.dirname(fullPath);
|
|
await fs.mkdir(dir, { recursive: true });
|
|
await fs.writeFile(fullPath, content, 'utf-8');
|
|
res.json({ ok: true });
|
|
});
|
|
|
|
promptsRouter.get('/:accountId', (req, res) => {
|
|
res.json(Object.keys(PROMPT_FILES).map((type) => ({
|
|
type,
|
|
path: PROMPT_FILES[type],
|
|
})));
|
|
});
|