Files
video-create/web/server/agent/pi-model.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

import { registerBuiltInApiProviders } from '@earendil-works/pi-ai';
import type { Model } from '@earendil-works/pi-ai';
import { getDb } from '../db';
registerBuiltInApiProviders();
export interface PiModelConfig {
model: Model<any>;
apiKey: string;
}
export function createPiModel(): PiModelConfig {
const row = 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 modelId = process.env.ANTHROPIC_MODEL || 'claude-sonnet-4-6';
let protocol: 'anthropic' | 'openai' = 'anthropic';
if (row) {
try {
const cfg = JSON.parse(row.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) modelId = cfg.ANTHROPIC_MODEL;
if (cfg.PROTOCOL === 'openai') protocol = 'openai';
} catch {}
}
const model: Model<any> = protocol === 'openai'
? {
id: modelId,
name: modelId,
api: 'openai-completions',
provider: 'openai',
baseUrl: baseURL || 'https://api.openai.com/v1',
reasoning: false,
input: ['text'] as ('text' | 'image')[],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 128000,
maxTokens: 8192,
}
: {
id: modelId,
name: modelId,
api: 'anthropic-messages',
provider: 'anthropic',
baseUrl: baseURL || 'https://api.anthropic.com',
reasoning: true,
input: ['text', 'image'] as ('text' | 'image')[],
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
contextWindow: 200000,
maxTokens: 8192,
};
return { model, apiKey };
}