Files
video-create/web/server/routes/accounts.ts
sion123 088bdb9a8e feat(web): 重构前端UI并支持OpenAI协议
- 添加账号管理详情页(基本信息、提示词、CapCut、参考图标签页)
- 重构资产页面,按项目组分开展示图片/视频
- 聊天界面支持深度思考内容折叠展示、复制、删除消息
- 设置页面支持Agent配置(Anthropic/OpenAI协议)和工具配置
- 后端支持OpenAI兼容协议流式输出和DeepSeek思考模式
- 添加对话置顶/删除功能、数据库迁移、资产清单API
- 添加账号参考图上传/删除、技能配置持久化、连接测试API
2026-05-07 23:48:26 +08:00

148 lines
4.8 KiB
TypeScript

import { Router } from 'express';
import fs from 'fs/promises';
import path from 'path';
import multer from 'multer';
const ACCOUNTS_DIR = path.resolve(__dirname, '..', '..', '..', 'accounts');
export const accountsRouter = Router();
async function readAccountJson(id: string): Promise<Record<string, unknown> | null> {
const p = path.join(ACCOUNTS_DIR, id, 'account.json');
try {
const data = await fs.readFile(p, 'utf-8');
return JSON.parse(data);
} catch {
return null;
}
}
async function writeAccountJson(id: string, data: Record<string, unknown>): Promise<void> {
const dir = path.join(ACCOUNTS_DIR, id);
await fs.mkdir(dir, { recursive: true });
await fs.writeFile(path.join(dir, 'account.json'), JSON.stringify(data, null, 2), 'utf-8');
}
// List
accountsRouter.get('/', async (_req, res) => {
const dirs = await fs.readdir(ACCOUNTS_DIR, { withFileTypes: true });
const accountDirs = dirs.filter((d) => d.isDirectory() && !d.name.startsWith('_') && !d.name.startsWith('.'));
const accounts: Record<string, unknown>[] = [];
for (const d of accountDirs) {
const data = await readAccountJson(d.name);
if (data) accounts.push(data);
}
res.json(accounts);
});
// Get
accountsRouter.get('/:id', async (req, res) => {
const data = await readAccountJson(req.params.id);
if (!data) return res.status(404).json({ error: 'Account not found' });
res.json(data);
});
// Create
accountsRouter.post('/', async (req, res) => {
const { id, ...rest } = req.body;
if (!id) return res.status(400).json({ error: 'id is required' });
const dir = path.join(ACCOUNTS_DIR, id);
try {
await fs.access(dir);
return res.status(409).json({ error: 'Account already exists' });
} catch {
// Directory doesn't exist, proceed
}
const data = {
id,
name: rest.name || id,
description: rest.description || '',
defaultFormat: rest.defaultFormat || '9:16',
imageModel: rest.imageModel || 'gemini',
videoModel: rest.videoModel || 'veo3-fast',
batchSize: rest.batchSize || 30,
ttsVoice: rest.ttsVoice || '',
ttsInstruction: rest.ttsInstruction || '',
storyboardPrompt: rest.storyboardPrompt || 'prompts/分镜.md',
imageStylePrompt: rest.imageStylePrompt || 'prompts/图片提示词.md',
videoStylePrompt: rest.videoStylePrompt || 'prompts/视频提示词.md',
references: rest.references || [],
capcut: rest.capcut || {},
};
await writeAccountJson(id, data);
res.status(201).json(data);
});
// Update
accountsRouter.put('/:id', async (req, res) => {
const existing = await readAccountJson(req.params.id);
if (!existing) return res.status(404).json({ error: 'Account not found' });
const merged = { ...existing, ...req.body, id: req.params.id };
await writeAccountJson(req.params.id, merged);
res.json(merged);
});
// Delete
accountsRouter.delete('/:id', async (req, res) => {
const dir = path.join(ACCOUNTS_DIR, req.params.id);
try {
await fs.access(dir);
} catch {
return res.status(404).json({ error: 'Account not found' });
}
await fs.rm(dir, { recursive: true });
res.status(204).send();
});
// Reference image upload
const upload = multer({ dest: path.join(ACCOUNTS_DIR, '_uploads') });
accountsRouter.post('/:id/references/upload', upload.single('file'), async (req, res) => {
const id = req.params.id as string;
const file = req.file as Express.Multer.File | undefined;
if (!file) return res.status(400).json({ error: 'No file uploaded' });
const account = await readAccountJson(id);
if (!account) return res.status(404).json({ error: 'Account not found' });
const refsDir = path.join(ACCOUNTS_DIR, id, 'references');
await fs.mkdir(refsDir, { recursive: true });
const destPath = path.join(refsDir, file.originalname);
await fs.rename(file.path, destPath);
const refEntry = { file: `references/${file.originalname}` };
const refs = (account.references as unknown[]) || [];
refs.push(refEntry);
account.references = refs;
await writeAccountJson(id, account);
res.json({ ok: true, reference: refEntry });
});
// Reference image delete
accountsRouter.delete('/:id/references/:index', async (req, res) => {
const id = req.params.id as string;
const idx = parseInt(req.params.index as string);
const account = await readAccountJson(id);
if (!account) return res.status(404).json({ error: 'Account not found' });
const refs = (account.references as unknown[]) || [];
if (idx < 0 || idx >= refs.length) return res.status(400).json({ error: 'Invalid index' });
const removed = refs.splice(idx, 1)[0] as { file?: string };
if (removed?.file) {
const filePath = path.join(ACCOUNTS_DIR, id, removed.file);
try { await fs.unlink(filePath); } catch { /* ignore */ }
}
account.references = refs;
await writeAccountJson(id, account);
res.json({ ok: true });
});