Files
video-create/web/server/agent/index.ts

121 lines
4.0 KiB
TypeScript
Raw Normal View History

import Anthropic from '@anthropic-ai/sdk';
import { tools, ToolDefinition } from './tools';
import { getDb } from '../db';
import fs from 'fs';
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, '..', '..', '..', '..');
function getAnthropicClient(): Anthropic {
const configRow = 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;
if (configRow) {
try {
const cfg = JSON.parse(configRow.value);
if (cfg.ANTHROPIC_AUTH_TOKEN) apiKey = cfg.ANTHROPIC_AUTH_TOKEN;
if (cfg.ANTHROPIC_BASE_URL) baseURL = cfg.ANTHROPIC_BASE_URL;
} catch {}
}
return new Anthropic({
apiKey,
baseURL,
});
}
function getModel(): string {
const configRow = getDb().prepare('SELECT value FROM configs WHERE key = ?').get('api_keys') as { value: string } | undefined;
if (configRow) {
try {
const cfg = JSON.parse(configRow.value);
if (cfg.ANTHROPIC_MODEL) return cfg.ANTHROPIC_MODEL;
} catch {}
}
return process.env.ANTHROPIC_MODEL || 'claude-sonnet-4-6';
}
export class VideoAgent {
private tools: ToolDefinition[];
constructor() {
this.tools = tools;
}
getAnthropicTools(): Anthropic.Tool[] {
return this.tools.map((t) => ({
name: t.name,
description: t.description,
input_schema: t.input_schema,
}));
}
async executeTool(name: string, params: Record<string, unknown>): Promise<string> {
const tool = this.tools.find((t) => t.name === name);
if (!tool) throw new Error(`Unknown tool: ${name}`);
return tool.execute(params);
}
getSystemPrompt(): string {
// Dynamically list accounts
const accountsDir = path.join(PROJECT_ROOT, 'accounts');
let accountList = '暂无账号';
if (fs.existsSync(accountsDir)) {
const dirs = fs.readdirSync(accountsDir, { withFileTypes: true })
.filter((d) => d.isDirectory() && !d.name.startsWith('_') && !d.name.startsWith('.'));
if (dirs.length > 0) {
accountList = dirs.map((d) => {
const configPath = path.join(accountsDir, d.name, 'account.json');
if (fs.existsSync(configPath)) {
const cfg = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
return `- ${d.name}: ${cfg.description || '无描述'} (生图:${cfg.imageModel}, 视频:${cfg.videoModel}, 画幅:${cfg.defaultFormat})`;
}
return `- ${d.name}`;
}).join('\n');
}
}
return `你是美图 Agent一个专业的短视频创作助手。你可以帮助用户完成从创意到成片的完整流程。
##
${accountList}
##
1. **** - 使 list_accounts
2. **** - 使 create_account /
3. **** - 使 get_account_config
4. ** Pipeline ** - 使 pipeline_status
5. **** - 使 run_pipeline_phase pipeline
##
1. A. / B.AI视频
2. /
3.
4. images
5. videos B
6. tts
7. assemble
##
-
-
- pipeline manifest
-
- 使`;
}
getClient(): Anthropic {
return getAnthropicClient();
}
getModel(): string {
return getModel();
}
}
export const videoAgent = new VideoAgent();