feat: add initial model provider adapters
This commit is contained in:
1 parent
7f35960ba6
commit
547f7c0977
16 files changed
+577
-21
No files matched your search
@@ -120,8 +120,56 @@ export class MemoryCatalogRepository implements CatalogRepository {
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
const deepseekProvider: ProviderRecord = {
|
||||
id: 'provider-deepseek',
|
||||
code: 'deepseek',
|
||||
name: 'DeepSeek',
|
||||
protocol: 'openai-compatible',
|
||||
baseUrl: 'https://api.deepseek.com',
|
||||
description: 'DeepSeek 官方 OpenAI 兼容接口;密钥通过 DEEPSEEK_API_KEY 注入。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
const kimiProvider: ProviderRecord = {
|
||||
id: 'provider-kimi',
|
||||
code: 'kimi',
|
||||
name: '月之暗面 Kimi',
|
||||
protocol: 'openai-compatible',
|
||||
baseUrl: 'https://api.moonshot.ai/v1',
|
||||
description: 'Kimi 官方 OpenAI 兼容接口;密钥通过 KIMI_API_KEY 注入。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
const zhipuProvider: ProviderRecord = {
|
||||
id: 'provider-zhipu',
|
||||
code: 'zhipu',
|
||||
name: '智谱 GLM',
|
||||
protocol: 'openai-compatible',
|
||||
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
|
||||
description: '智谱 AI 官方兼容接口;密钥通过 ZHIPU_API_KEY 注入。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
const bytedanceProvider: ProviderRecord = {
|
||||
id: 'provider-bytedance',
|
||||
code: 'bytedance',
|
||||
name: '字节跳动 Seedance',
|
||||
protocol: 'http',
|
||||
baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
|
||||
description: '火山方舟 Seedance 异步视频生成接口;密钥通过 VOLCENGINE_API_KEY 注入。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
this.providers.set(mockProvider.id, mockProvider)
|
||||
this.providers.set(vastProvider.id, vastProvider)
|
||||
this.providers.set(deepseekProvider.id, deepseekProvider)
|
||||
this.providers.set(kimiProvider.id, kimiProvider)
|
||||
this.providers.set(zhipuProvider.id, zhipuProvider)
|
||||
this.providers.set(bytedanceProvider.id, bytedanceProvider)
|
||||
this.models.set('model-mock-text', {
|
||||
id: 'model-mock-text',
|
||||
providerId: mockProvider.id,
|
||||
@@ -152,6 +200,30 @@ export class MemoryCatalogRepository implements CatalogRepository {
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
const providerModels: ModelRecord[] = [
|
||||
{
|
||||
id: 'model-deepseek-v4-flash', providerId: deepseekProvider.id, publicName: 'DeepSeek V4 Flash', upstreamModel: 'deepseek-v4-flash', type: 'text', mode: 'sync', description: 'DeepSeek 官方高速文本模型,支持 OpenAI 兼容聊天接口。', inputTypes: ['text'], outputTypes: ['text'], capabilities: { streaming: true, reasoning: false }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-deepseek-v4-pro', providerId: deepseekProvider.id, publicName: 'DeepSeek V4 Pro', upstreamModel: 'deepseek-v4-pro', type: 'text', mode: 'sync', description: 'DeepSeek 官方高能力文本与推理模型。', inputTypes: ['text'], outputTypes: ['text'], capabilities: { streaming: true, reasoning: true }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-kimi-k3', providerId: kimiProvider.id, publicName: 'Kimi K3', upstreamModel: 'kimi-k3', type: 'multimodal', mode: 'sync', description: '月之暗面 Kimi 新一代长上下文与多模态模型。', inputTypes: ['text', 'image'], outputTypes: ['text'], capabilities: { streaming: true, vision: true }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-kimi-k2-6', providerId: kimiProvider.id, publicName: 'Kimi K2.6', upstreamModel: 'kimi-k2.6', type: 'multimodal', mode: 'sync', description: 'Kimi 长上下文、多模态和代码任务模型。', inputTypes: ['text', 'image', 'video'], outputTypes: ['text'], capabilities: { streaming: true, vision: true, contextWindow: 256000 }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-glm-4-5', providerId: zhipuProvider.id, publicName: 'GLM-4.5', upstreamModel: 'glm-4.5', type: 'multimodal', mode: 'sync', description: '智谱 GLM 通用多模态模型,兼容标准聊天接口。', inputTypes: ['text', 'image'], outputTypes: ['text'], capabilities: { streaming: true, toolCalling: true, vision: true }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-glm-4-5v', providerId: zhipuProvider.id, publicName: 'GLM-4.5V', upstreamModel: 'glm-4.5v', type: 'multimodal', mode: 'sync', description: '智谱视觉语言模型,适合图像理解与多模态应用。', inputTypes: ['text', 'image'], outputTypes: ['text'], capabilities: { streaming: true, vision: true }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
{
|
||||
id: 'model-seedance-1-0-pro', providerId: bytedanceProvider.id, publicName: 'Seedance 1.0 Pro', upstreamModel: 'doubao-seedance-1-0-pro-250528', type: 'video', mode: 'async', description: '火山方舟异步文生视频模型,支持文本和参考素材扩展。', inputTypes: ['text', 'image', 'video', 'audio'], outputTypes: ['video'], capabilities: { taskPolling: true, webhook: true, duration: [2, 12] }, status: 'active', createdAt: now, updatedAt: now,
|
||||
},
|
||||
]
|
||||
for (const model of providerModels) this.models.set(model.id, model)
|
||||
}
|
||||
|
||||
async listProviders(includeDisabled = false) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
export interface OpenAICompatibleMessage {
|
||||
role: string
|
||||
content: unknown
|
||||
}
|
||||
|
||||
export interface OpenAICompatibleResponse {
|
||||
id?: string
|
||||
object?: string
|
||||
created?: number
|
||||
model?: string
|
||||
choices?: Array<{ index?: number; message?: { role?: string; content?: string }; finish_reason?: string | null }>
|
||||
usage?: { prompt_tokens?: number; completion_tokens?: number; total_tokens?: number }
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export interface OpenAICompatibleProviderOptions {
|
||||
providerName: string
|
||||
apiKey: string
|
||||
baseUrl: string
|
||||
timeoutMs?: number
|
||||
fetcher?: typeof fetch
|
||||
}
|
||||
|
||||
export class OpenAICompatibleProviderError extends Error {
|
||||
constructor(message: string, readonly statusCode?: number) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
export class OpenAICompatibleProvider {
|
||||
private readonly baseUrl: string
|
||||
private readonly timeoutMs: number
|
||||
private readonly fetcher: typeof fetch
|
||||
|
||||
constructor(private readonly options: OpenAICompatibleProviderOptions) {
|
||||
if (!options.apiKey.trim()) throw new OpenAICompatibleProviderError(`${options.providerName} API Key 未配置`)
|
||||
this.baseUrl = options.baseUrl.replace(/\/$/, '')
|
||||
this.timeoutMs = options.timeoutMs ?? 45_000
|
||||
this.fetcher = options.fetcher || fetch
|
||||
}
|
||||
|
||||
async chatCompletion(input: { model: string; messages: OpenAICompatibleMessage[]; stream?: boolean }) {
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), this.timeoutMs)
|
||||
try {
|
||||
const response = await this.fetcher(`${this.baseUrl}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${this.options.apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ model: input.model, messages: input.messages, stream: input.stream ?? false }),
|
||||
signal: controller.signal,
|
||||
})
|
||||
const payload = await response.json().catch(() => ({})) as OpenAICompatibleResponse & { error?: { message?: string } }
|
||||
if (!response.ok) throw new OpenAICompatibleProviderError(payload.error?.message || `${this.options.providerName} 请求失败(HTTP ${response.status})`, response.status)
|
||||
return payload
|
||||
} catch (error) {
|
||||
if (error instanceof OpenAICompatibleProviderError) throw error
|
||||
if (error instanceof Error && error.name === 'AbortError') throw new OpenAICompatibleProviderError(`${this.options.providerName} 请求超时`)
|
||||
throw new OpenAICompatibleProviderError(error instanceof Error ? error.message : `${this.options.providerName} 请求失败`)
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
export type SeedanceTaskStatus = 'queued' | 'running' | 'succeeded' | 'failed' | 'cancelled'
|
||||
|
||||
export interface SeedanceTask {
|
||||
id: string
|
||||
model?: string
|
||||
status: string
|
||||
created_at?: string | number
|
||||
updated_at?: string | number
|
||||
content?: { video_url?: string }
|
||||
error?: { code?: string | number; message?: string }
|
||||
usage?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface SeedanceProviderOptions {
|
||||
apiKey: string
|
||||
baseUrl?: string
|
||||
timeoutMs?: number
|
||||
fetcher?: typeof fetch
|
||||
}
|
||||
|
||||
export interface SeedanceTaskView {
|
||||
id: string
|
||||
provider: 'seedance'
|
||||
status: SeedanceTaskStatus
|
||||
progress: number
|
||||
model?: string
|
||||
output?: { videoUrl?: string }
|
||||
error?: { code?: string | number; message?: string }
|
||||
createdAt?: string
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
export class SeedanceProviderError extends Error {
|
||||
constructor(message: string, readonly statusCode?: number) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
export class SeedanceProvider {
|
||||
private readonly baseUrl: string
|
||||
private readonly timeoutMs: number
|
||||
private readonly fetcher: typeof fetch
|
||||
|
||||
constructor(private readonly options: SeedanceProviderOptions) {
|
||||
if (!options.apiKey.trim()) throw new SeedanceProviderError('Seedance API Key 未配置')
|
||||
this.baseUrl = (options.baseUrl || 'https://ark.cn-beijing.volces.com/api/v3').replace(/\/$/, '')
|
||||
this.timeoutMs = options.timeoutMs ?? 60_000
|
||||
this.fetcher = options.fetcher || fetch
|
||||
}
|
||||
|
||||
async createTextToVideo(input: { model: string; prompt: string }) {
|
||||
const prompt = input.prompt.trim()
|
||||
if (!prompt) throw new SeedanceProviderError('Seedance 提示词不能为空')
|
||||
const response = await this.request<SeedanceTask>('/contents/generations/tasks', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ model: input.model, content: [{ type: 'text', text: prompt }] }),
|
||||
})
|
||||
if (!response.id) throw new SeedanceProviderError('Seedance 未返回任务 ID')
|
||||
return response.id
|
||||
}
|
||||
|
||||
async getTask(taskId: string): Promise<SeedanceTaskView> {
|
||||
if (!/^[a-zA-Z0-9][a-zA-Z0-9_-]{7,127}$/.test(taskId)) throw new SeedanceProviderError('Seedance task ID 格式无效')
|
||||
const task = await this.request<SeedanceTask>(`/contents/generations/tasks/${encodeURIComponent(taskId)}`)
|
||||
return mapSeedanceTask(task)
|
||||
}
|
||||
|
||||
private async request<T>(path: string, init: { method?: string; body?: string } = {}) {
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), this.timeoutMs)
|
||||
try {
|
||||
const response = await this.fetcher(`${this.baseUrl}${path}`, {
|
||||
method: init.method || 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${this.options.apiKey}`,
|
||||
...(init.body ? { 'Content-Type': 'application/json' } : {}),
|
||||
},
|
||||
...(init.body ? { body: init.body } : {}),
|
||||
signal: controller.signal,
|
||||
})
|
||||
const payload = await response.json().catch(() => ({})) as T & { error?: { message?: string } }
|
||||
if (!response.ok) throw new SeedanceProviderError(payload.error?.message || `Seedance 请求失败(HTTP ${response.status})`, response.status)
|
||||
return payload as T
|
||||
} catch (error) {
|
||||
if (error instanceof SeedanceProviderError) throw error
|
||||
if (error instanceof Error && error.name === 'AbortError') throw new SeedanceProviderError('Seedance 请求超时')
|
||||
throw new SeedanceProviderError(error instanceof Error ? error.message : 'Seedance 请求失败')
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isoDate(value?: string | number) {
|
||||
if (value === undefined) return undefined
|
||||
const numeric = typeof value === 'number' || /^\d+$/.test(value) ? Number(value) : NaN
|
||||
const date = Number.isFinite(numeric) ? new Date(numeric < 10_000_000_000 ? numeric * 1000 : numeric) : new Date(value)
|
||||
return Number.isNaN(date.getTime()) ? undefined : date.toISOString()
|
||||
}
|
||||
|
||||
export function mapSeedanceTask(task: SeedanceTask): SeedanceTaskView {
|
||||
const statusText = task.status.toLowerCase()
|
||||
const status: SeedanceTaskStatus = statusText === 'succeeded' || statusText === 'success' ? 'succeeded' : statusText === 'failed' ? 'failed' : statusText === 'cancelled' || statusText === 'canceled' ? 'cancelled' : statusText === 'running' ? 'running' : 'queued'
|
||||
const createdAt = isoDate(task.created_at)
|
||||
const updatedAt = isoDate(task.updated_at)
|
||||
return {
|
||||
id: task.id,
|
||||
provider: 'seedance',
|
||||
status,
|
||||
progress: status === 'succeeded' ? 100 : status === 'running' ? 50 : 0,
|
||||
...(task.model ? { model: task.model } : {}),
|
||||
...(task.content?.video_url ? { output: { videoUrl: task.content.video_url } } : {}),
|
||||
...(task.error ? { error: task.error } : {}),
|
||||
...(createdAt ? { createdAt } : {}),
|
||||
...(updatedAt ? { updatedAt } : {}),
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import { createServer, type IncomingMessage, type ServerResponse } from 'node:ht
|
||||
import { parse as parseUrl } from 'node:url'
|
||||
import { AuthError, AuthService, MemoryAuthRepository, type AuthRepository } from './auth.js'
|
||||
import { CatalogService, MemoryCatalogRepository, type CallMode, type ModelType, type ProviderProtocol } from './catalog.js'
|
||||
import { OpenAICompatibleProvider, OpenAICompatibleProviderError } from './providers/openai-compatible.js'
|
||||
import { SeedanceProvider, SeedanceProviderError } from './providers/seedance.js'
|
||||
import { TripoProvider, TripoProviderError } from './providers/tripo.js'
|
||||
|
||||
const config = {
|
||||
@@ -15,6 +17,21 @@ const config = {
|
||||
nodeEnv: process.env.NODE_ENV || 'development',
|
||||
tripoApiKey: process.env.TRIPO_API_KEY,
|
||||
tripoBaseUrl: process.env.TRIPO_BASE_URL,
|
||||
deepseekApiKey: process.env.DEEPSEEK_API_KEY,
|
||||
deepseekBaseUrl: process.env.DEEPSEEK_BASE_URL || 'https://api.deepseek.com',
|
||||
deepseekFlashModel: process.env.DEEPSEEK_FLASH_MODEL || 'deepseek-v4-flash',
|
||||
deepseekProModel: process.env.DEEPSEEK_PRO_MODEL || 'deepseek-v4-pro',
|
||||
kimiApiKey: process.env.KIMI_API_KEY,
|
||||
kimiBaseUrl: process.env.KIMI_BASE_URL || 'https://api.moonshot.cn/v1',
|
||||
kimiPrimaryModel: process.env.KIMI_PRIMARY_MODEL || 'kimi-k3',
|
||||
kimiFallbackModel: process.env.KIMI_FALLBACK_MODEL || 'kimi-k2.6',
|
||||
zhipuApiKey: process.env.ZHIPU_API_KEY,
|
||||
zhipuBaseUrl: process.env.ZHIPU_BASE_URL || 'https://open.bigmodel.cn/api/paas/v4',
|
||||
zhipuPrimaryModel: process.env.ZHIPU_PRIMARY_MODEL || 'glm-4.5',
|
||||
zhipuVisionModel: process.env.ZHIPU_VISION_MODEL || 'glm-4.5v',
|
||||
seedanceApiKey: process.env.VOLCENGINE_API_KEY,
|
||||
seedanceBaseUrl: process.env.VOLCENGINE_BASE_URL,
|
||||
seedanceModel: process.env.SEEDANCE_MODEL || 'doubao-seedance-1-0-pro-250528',
|
||||
}
|
||||
|
||||
let repository: AuthRepository = new MemoryAuthRepository()
|
||||
@@ -55,7 +72,22 @@ const tripo = config.tripoApiKey ? new TripoProvider({
|
||||
apiKey: config.tripoApiKey,
|
||||
...(config.tripoBaseUrl ? { baseUrl: config.tripoBaseUrl } : {}),
|
||||
}) : undefined
|
||||
const openaiProviders = new Map<string, OpenAICompatibleProvider>()
|
||||
if (config.deepseekApiKey) openaiProviders.set('provider-deepseek', new OpenAICompatibleProvider({ providerName: 'DeepSeek', apiKey: config.deepseekApiKey, baseUrl: config.deepseekBaseUrl }))
|
||||
if (config.kimiApiKey) openaiProviders.set('provider-kimi', new OpenAICompatibleProvider({ providerName: 'Kimi', apiKey: config.kimiApiKey, baseUrl: config.kimiBaseUrl }))
|
||||
if (config.zhipuApiKey) openaiProviders.set('provider-zhipu', new OpenAICompatibleProvider({ providerName: '智谱 GLM', apiKey: config.zhipuApiKey, baseUrl: config.zhipuBaseUrl }))
|
||||
const seedance = config.seedanceApiKey ? new SeedanceProvider({ apiKey: config.seedanceApiKey, ...(config.seedanceBaseUrl ? { baseUrl: config.seedanceBaseUrl } : {}) }) : undefined
|
||||
const upstreamModelOverrides = new Map<string, string>([
|
||||
['model-deepseek-v4-flash', config.deepseekFlashModel],
|
||||
['model-deepseek-v4-pro', config.deepseekProModel],
|
||||
['model-kimi-k3', config.kimiPrimaryModel],
|
||||
['model-kimi-k2-6', config.kimiFallbackModel],
|
||||
['model-glm-4-5', config.zhipuPrimaryModel],
|
||||
['model-glm-4-5v', config.zhipuVisionModel],
|
||||
['model-seedance-1-0-pro', config.seedanceModel],
|
||||
])
|
||||
const tripoTaskOwners = new Map<string, { userId: string; model: string }>()
|
||||
const seedanceTaskOwners = new Map<string, { userId: string; model: string }>()
|
||||
|
||||
if (config.nodeEnv === 'production' && (!process.env.AUTH_PASSWORD_PEPPER || process.env.AUTH_PASSWORD_PEPPER.length < 32)) {
|
||||
throw new Error('AUTH_PASSWORD_PEPPER must contain at least 32 characters in production')
|
||||
@@ -97,6 +129,8 @@ server.listen(config.port, config.host, () => {
|
||||
console.log(`MOYI Control Plane listening on http://${config.host}:${config.port}`)
|
||||
console.log(config.databaseUrl ? 'Persistence: PostgreSQL' : 'Persistence: in-memory development store')
|
||||
console.log(tripo ? 'Tripo 3D Provider: enabled' : 'Tripo 3D Provider: mock mode')
|
||||
console.log(`OpenAI-compatible Providers: ${openaiProviders.size ? [...openaiProviders.keys()].join(', ') : 'none configured'}`)
|
||||
console.log(seedance ? 'Seedance Provider: enabled' : 'Seedance Provider: not configured')
|
||||
})
|
||||
|
||||
async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
@@ -133,6 +167,31 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
const model = await catalogModel(modelId)
|
||||
if (model.mode === 'async') throw new AuthError('ASYNC_MODEL', '该模型需要使用异步任务接口', 400)
|
||||
if (!catalog.canInvoke(keyRecord, model.id)) throw new AuthError('MODEL_FORBIDDEN', '当前 Key 无权调用该模型', 403)
|
||||
if (model.providerId !== 'provider-mock') {
|
||||
const provider = openaiProviders.get(model.providerId)
|
||||
if (!provider) throw new AuthError('PROVIDER_NOT_CONFIGURED', '该模型的供应商尚未配置服务端 API Key', 503)
|
||||
if (body.stream === true) throw new AuthError('STREAM_NOT_IMPLEMENTED', '该供应商的流式转发将在下一节点开放', 501)
|
||||
const upstream = await provider.chatCompletion({
|
||||
model: upstreamModelOverrides.get(model.id) || model.upstreamModel,
|
||||
messages: Array.isArray(body.messages) ? body.messages as Array<{ role: string; content: unknown }> : [],
|
||||
})
|
||||
const upstreamContent = upstream.choices?.[0]?.message?.content || ''
|
||||
const completedAt = new Date().toISOString()
|
||||
usageEvents.push({
|
||||
id: `usage_${randomToken(14)}`,
|
||||
userId: keyRecord.userId,
|
||||
kind: 'call',
|
||||
model: model.id,
|
||||
status: 'succeeded',
|
||||
createdAt: completedAt,
|
||||
updatedAt: completedAt,
|
||||
latencyMs: Date.now() - startedAt,
|
||||
inputUnits: extractPrompt(body.messages).length,
|
||||
outputUnits: upstreamContent.length,
|
||||
})
|
||||
send(response, 200, { ...upstream, model: model.id })
|
||||
return
|
||||
}
|
||||
const prompt = extractPrompt(body.messages)
|
||||
const content = `MOYI Mock 已收到:${prompt || '(空消息)'}`
|
||||
const completedAt = new Date().toISOString()
|
||||
@@ -159,13 +218,21 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/v1/3d/tasks') {
|
||||
if (method === 'POST' && (path === '/v1/3d/tasks' || path === '/v1/video/tasks')) {
|
||||
const keyRecord = await authenticatePlatformKey(request)
|
||||
const body = await readJson(request)
|
||||
const modelId = stringValue(body.model)
|
||||
const model = await catalogModel(modelId)
|
||||
if (model.type !== '3d' || model.mode !== 'async') throw new AuthError('NOT_ASYNC_3D_MODEL', '该模型不是异步 3D 模型', 400)
|
||||
if ((!['3d', 'video'].includes(model.type) || model.mode !== 'async')) throw new AuthError('NOT_ASYNC_MEDIA_MODEL', '该模型不是异步媒体模型', 400)
|
||||
if (!catalog.canInvoke(keyRecord, model.id)) throw new AuthError('MODEL_FORBIDDEN', '当前 Key 无权调用该模型', 403)
|
||||
if (model.type === 'video') {
|
||||
if (!seedance) throw new AuthError('PROVIDER_NOT_CONFIGURED', 'Seedance 尚未配置服务端 API Key', 503)
|
||||
const taskId = await seedance.createTextToVideo({ model: upstreamModelOverrides.get(model.id) || model.upstreamModel, prompt: stringValue(body.prompt) })
|
||||
seedanceTaskOwners.set(taskId, { userId: keyRecord.userId, model: model.id })
|
||||
recordTaskUsage(keyRecord.userId, model.id, taskId, 'queued')
|
||||
send(response, 202, { task: { id: taskId, provider: 'seedance', model: model.id, status: 'queued', progress: 0, createdAt: new Date().toISOString() } })
|
||||
return
|
||||
}
|
||||
if (tripo) {
|
||||
const prompt = stringValue(body.prompt).trim()
|
||||
if (!prompt) throw new AuthError('PROMPT_REQUIRED', '3D 生成提示词不能为空')
|
||||
@@ -211,6 +278,16 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
const taskId = taskMatch?.[1]
|
||||
if (method === 'GET' && taskId) {
|
||||
const keyRecord = await authenticatePlatformKey(request)
|
||||
const seedanceOwner = seedanceTaskOwners.get(taskId)
|
||||
if (seedance && seedanceOwner) {
|
||||
if (seedanceOwner.userId !== keyRecord.userId) throw new AuthError('TASK_NOT_FOUND', '任务不存在', 404)
|
||||
const providerTask = await seedance.getTask(taskId)
|
||||
if (providerTask.status === 'queued' || providerTask.status === 'running' || providerTask.status === 'succeeded' || providerTask.status === 'failed') {
|
||||
updateTaskUsage(taskId, providerTask.status)
|
||||
}
|
||||
send(response, 200, { task: { ...providerTask, model: seedanceOwner.model } })
|
||||
return
|
||||
}
|
||||
const tripoOwner = tripoTaskOwners.get(taskId)
|
||||
if (tripo && tripoOwner) {
|
||||
if (tripoOwner.userId !== keyRecord.userId) throw new AuthError('TASK_NOT_FOUND', '任务不存在', 404)
|
||||
@@ -627,6 +704,16 @@ function handleError(response: ServerResponse, error: unknown) {
|
||||
send(response, 502, { error: { code: 'TRIPO_PROVIDER_ERROR', message: '3D 模型供应商请求失败,请稍后重试' } })
|
||||
return
|
||||
}
|
||||
if (error instanceof OpenAICompatibleProviderError) {
|
||||
console.error(`OpenAI-compatible provider error: ${error.message}`)
|
||||
send(response, 502, { error: { code: 'UPSTREAM_PROVIDER_ERROR', message: '上游模型供应商请求失败,请稍后重试' } })
|
||||
return
|
||||
}
|
||||
if (error instanceof SeedanceProviderError) {
|
||||
console.error(`Seedance provider error: ${error.message}`)
|
||||
send(response, 502, { error: { code: 'SEEDANCE_PROVIDER_ERROR', message: 'Seedance 视频供应商请求失败,请稍后重试' } })
|
||||
return
|
||||
}
|
||||
console.error(error)
|
||||
send(response, 500, { error: { code: 'INTERNAL_ERROR', message: '服务暂时不可用' } })
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user