feat(web): 重构前端UI并支持OpenAI协议
- 添加账号管理详情页(基本信息、提示词、CapCut、参考图标签页) - 重构资产页面,按项目组分开展示图片/视频 - 聊天界面支持深度思考内容折叠展示、复制、删除消息 - 设置页面支持Agent配置(Anthropic/OpenAI协议)和工具配置 - 后端支持OpenAI兼容协议流式输出和DeepSeek思考模式 - 添加对话置顶/删除功能、数据库迁移、资产清单API - 添加账号参考图上传/删除、技能配置持久化、连接测试API
This commit is contained in:
@@ -1,49 +1,60 @@
|
||||
import { Router } from 'express';
|
||||
import fs from 'fs';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import multer from 'multer';
|
||||
|
||||
const ACCOUNTS_DIR = path.resolve(__dirname, '..', '..', '..', 'accounts');
|
||||
|
||||
export const accountsRouter = Router();
|
||||
|
||||
function readAccountJson(id: string): Record<string, unknown> | null {
|
||||
async function readAccountJson(id: string): Promise<Record<string, unknown> | null> {
|
||||
const p = path.join(ACCOUNTS_DIR, id, 'account.json');
|
||||
if (!fs.existsSync(p)) return null;
|
||||
return JSON.parse(fs.readFileSync(p, 'utf-8'));
|
||||
try {
|
||||
const data = await fs.readFile(p, 'utf-8');
|
||||
return JSON.parse(data);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function writeAccountJson(id: string, data: Record<string, unknown>): void {
|
||||
async function writeAccountJson(id: string, data: Record<string, unknown>): Promise<void> {
|
||||
const dir = path.join(ACCOUNTS_DIR, id);
|
||||
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, 'account.json'), JSON.stringify(data, null, 2), 'utf-8');
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
await fs.writeFile(path.join(dir, 'account.json'), JSON.stringify(data, null, 2), 'utf-8');
|
||||
}
|
||||
|
||||
// List
|
||||
accountsRouter.get('/', (_req, res) => {
|
||||
const dirs = fs.readdirSync(ACCOUNTS_DIR, { withFileTypes: true })
|
||||
.filter((d) => d.isDirectory() && !d.name.startsWith('_') && !d.name.startsWith('.'))
|
||||
.map((d) => d.name);
|
||||
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 = dirs
|
||||
.map((id) => readAccountJson(id))
|
||||
.filter(Boolean);
|
||||
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', (req, res) => {
|
||||
const data = readAccountJson(req.params.id);
|
||||
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('/', (req, res) => {
|
||||
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);
|
||||
if (fs.existsSync(dir)) return res.status(409).json({ error: 'Account already exists' });
|
||||
try {
|
||||
await fs.access(dir);
|
||||
return res.status(409).json({ error: 'Account already exists' });
|
||||
} catch {
|
||||
// Directory doesn't exist, proceed
|
||||
}
|
||||
|
||||
const data = {
|
||||
id,
|
||||
@@ -62,24 +73,75 @@ accountsRouter.post('/', (req, res) => {
|
||||
capcut: rest.capcut || {},
|
||||
};
|
||||
|
||||
writeAccountJson(id, data);
|
||||
await writeAccountJson(id, data);
|
||||
res.status(201).json(data);
|
||||
});
|
||||
|
||||
// Update
|
||||
accountsRouter.put('/:id', (req, res) => {
|
||||
const existing = readAccountJson(req.params.id);
|
||||
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 };
|
||||
writeAccountJson(req.params.id, merged);
|
||||
await writeAccountJson(req.params.id, merged);
|
||||
res.json(merged);
|
||||
});
|
||||
|
||||
// Delete
|
||||
accountsRouter.delete('/:id', (req, res) => {
|
||||
accountsRouter.delete('/:id', async (req, res) => {
|
||||
const dir = path.join(ACCOUNTS_DIR, req.params.id);
|
||||
if (!fs.existsSync(dir)) return res.status(404).json({ error: 'Account not found' });
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
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 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user