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

150 lines
4.7 KiB
TypeScript
Raw Normal View History

import Anthropic from '@anthropic-ai/sdk';
import OpenAI from 'openai';
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, '..', '..', '..', '..');
export type Protocol = 'anthropic' | 'openai';
interface ApiConfig {
protocol: Protocol;
apiKey: string;
baseURL: string | undefined;
model: string;
}
function getApiConfig(): ApiConfig {
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;
let model = process.env.ANTHROPIC_MODEL || 'claude-sonnet-4-6';
let protocol: Protocol = 'anthropic';
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;
if (cfg.ANTHROPIC_MODEL) model = cfg.ANTHROPIC_MODEL;
if (cfg.PROTOCOL === 'openai') protocol = 'openai';
} catch {}
}
return { protocol, apiKey, baseURL, model };
}
function getAnthropicClient(): Anthropic {
const { apiKey, baseURL } = getApiConfig();
return new Anthropic({ apiKey, baseURL });
}
function getOpenAIClient(): OpenAI {
const { apiKey, baseURL } = getApiConfig();
return new OpenAI({ apiKey, baseURL: baseURL || 'https://api.openai.com/v1' });
}
export class VideoAgent {
private tools: ToolDefinition[];
constructor() {
this.tools = tools;
}
getProtocol(): Protocol {
return getApiConfig().protocol;
}
getModel(): string {
return getApiConfig().model;
}
getAnthropicClient(): Anthropic {
return getAnthropicClient();
}
getOpenAIClient(): OpenAI {
return getOpenAIClient();
}
getAnthropicTools(): Anthropic.Tool[] {
return this.tools.map((t) => ({
name: t.name,
description: t.description,
input_schema: t.input_schema,
}));
}
getOpenAITools(): OpenAI.ChatCompletionTool[] {
return this.tools.map((t) => ({
type: 'function' as const,
function: {
name: t.name,
description: t.description,
parameters: 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 {
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
-
- 使`;
}
}
export const videoAgent = new VideoAgent();