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
@@ -13,3 +13,22 @@ ADMIN_DISPLAY_NAME=平台管理员
|
||||
# Tripo 3D Provider(仅服务端读取,禁止提交真实值)
|
||||
TRIPO_API_KEY=
|
||||
TRIPO_BASE_URL=https://openapi.tripo3d.com/v3
|
||||
|
||||
# 首批文本/多模态供应商(仅服务端读取,禁止提交真实值)
|
||||
DEEPSEEK_API_KEY=
|
||||
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
||||
DEEPSEEK_FLASH_MODEL=deepseek-v4-flash
|
||||
DEEPSEEK_PRO_MODEL=deepseek-v4-pro
|
||||
KIMI_API_KEY=
|
||||
KIMI_BASE_URL=https://api.moonshot.cn/v1
|
||||
KIMI_PRIMARY_MODEL=kimi-k3
|
||||
KIMI_FALLBACK_MODEL=kimi-k2.6
|
||||
ZHIPU_API_KEY=
|
||||
ZHIPU_BASE_URL=https://open.bigmodel.cn/api/paas/v4
|
||||
ZHIPU_PRIMARY_MODEL=glm-4.5
|
||||
ZHIPU_VISION_MODEL=glm-4.5v
|
||||
|
||||
# 字节 Seedance(火山方舟异步视频接口)
|
||||
VOLCENGINE_API_KEY=
|
||||
VOLCENGINE_BASE_URL=https://ark.cn-beijing.volces.com/api/v3
|
||||
SEEDANCE_MODEL=doubao-seedance-1-0-pro-250528
|
||||
@@ -13,3 +13,20 @@ ADMIN_DISPLAY_NAME=平台管理员
|
||||
# Tripo 3D Provider(仅服务端读取,禁止提交真实值)
|
||||
TRIPO_API_KEY=
|
||||
TRIPO_BASE_URL=https://openapi.tripo3d.com/v3
|
||||
|
||||
# 首批供应商密钥仅在本地注入,不要提交真实值
|
||||
DEEPSEEK_API_KEY=
|
||||
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
||||
DEEPSEEK_FLASH_MODEL=deepseek-v4-flash
|
||||
DEEPSEEK_PRO_MODEL=deepseek-v4-pro
|
||||
KIMI_API_KEY=
|
||||
KIMI_BASE_URL=https://api.moonshot.cn/v1
|
||||
KIMI_PRIMARY_MODEL=kimi-k3
|
||||
KIMI_FALLBACK_MODEL=kimi-k2.6
|
||||
ZHIPU_API_KEY=
|
||||
ZHIPU_BASE_URL=https://open.bigmodel.cn/api/paas/v4
|
||||
ZHIPU_PRIMARY_MODEL=glm-4.5
|
||||
ZHIPU_VISION_MODEL=glm-4.5v
|
||||
VOLCENGINE_API_KEY=
|
||||
VOLCENGINE_BASE_URL=https://ark.cn-beijing.volces.com/api/v3
|
||||
SEEDANCE_MODEL=doubao-seedance-1-0-pro-250528
|
||||
+16
-2
@@ -37,13 +37,15 @@ pnpm dev
|
||||
- `POST /api/auth/login`:账号密码登录,返回 HttpOnly 会话 Cookie。
|
||||
- `GET /api/auth/me`:读取当前会话用户。
|
||||
- `POST /api/auth/logout`:撤销当前会话。
|
||||
- `GET /api/models`:读取当前可用的 Mock 模型和供应商目录。
|
||||
- `GET /api/models`:读取当前供应商和统一模型目录。
|
||||
- `GET|POST /api/projects`:查看或创建当前用户拥有的工作区/项目。
|
||||
- `GET|POST /api/keys`:查看或创建平台调用 Key;明文 Key 只在创建响应中返回一次。
|
||||
- `POST /api/keys/:id/models`:在 Key 创建后配置可调用的具体模型。
|
||||
- `POST /api/keys/:id/revoke`:撤销当前用户自己的平台 Key。
|
||||
- `GET /v1/models`:使用 `Authorization: Bearer <MOYI_KEY>` 查询可调用模型。
|
||||
- `POST /v1/chat/completions`:兼容 OpenAI 风格的 Mock 同步文本/多模态调用。
|
||||
- `POST /v1/chat/completions`:OpenAI 兼容的同步文本/多模态调用;按目录路由至 DeepSeek、Kimi 或 GLM。
|
||||
- `POST /v1/3d/tasks`、`GET /v1/tasks/:id`:Mock 异步 3D 任务创建与查询。
|
||||
- `POST /v1/video/tasks`、`GET /v1/tasks/:id`:Seedance 异步视频任务创建与查询。
|
||||
- `GET /api/admin/users`:管理员用户列表。
|
||||
- `GET|POST /api/admin/invitations`:管理员查看或生成邀请码。
|
||||
- `POST /api/admin/invitations/:id/revoke`:停用邀请码。
|
||||
@@ -53,6 +55,17 @@ pnpm dev
|
||||
|
||||
第一版暂不实现短信/邮件真实验证码;注册页面保留验证码入口但不以它作为后端注册条件,邀请码、邮箱/手机号和账号密码是当前有效的注册条件。
|
||||
|
||||
## 首批供应商
|
||||
|
||||
| 供应商 | 接口类型 | 服务端配置 | 当前状态 |
|
||||
|---|---|---|---|
|
||||
| DeepSeek | OpenAI 兼容 Chat Completions | `DEEPSEEK_API_KEY`、`DEEPSEEK_BASE_URL` | 同步转发已接入,流式转发待下一节点 |
|
||||
| 月之暗面 Kimi | OpenAI 兼容 Chat Completions | `KIMI_API_KEY`、`KIMI_BASE_URL` | 同步转发已接入,流式转发待下一节点 |
|
||||
| 智谱 GLM | OpenAI 兼容 Chat Completions | `ZHIPU_API_KEY`、`ZHIPU_BASE_URL` | 同步转发已接入,流式转发待下一节点 |
|
||||
| 字节 Seedance | 火山方舟异步视频任务 | `VOLCENGINE_API_KEY`、`VOLCENGINE_BASE_URL` | 创建任务与轮询已接入 |
|
||||
|
||||
模型名称可通过 `.env` 中的 `*_MODEL` 变量覆盖,不需要修改网关代码。没有配置对应 API Key 时,模型仍可在目录中展示,但调用会返回 `PROVIDER_NOT_CONFIGURED`,不会伪造供应商结果。真实密钥只能放在未提交的本地 `.env` 或服务器密钥管理服务中。
|
||||
|
||||
## 安全约束
|
||||
|
||||
- 数据库仅保存密码哈希、邀请码摘要和会话摘要,不保存明文密码、明文邀请码或会话 Token。
|
||||
@@ -62,4 +75,5 @@ pnpm dev
|
||||
- 额度界面统一按人民币 `¥` 展示;内测额度由管理员手动发放,真实计量、扣费和账单在后续节点实现。
|
||||
- 上游供应商密钥不进入模型目录或用户响应,后续由 Provider Adapter 通过环境变量/密钥管理服务读取。
|
||||
- Tripo 3D 适配器读取 `TRIPO_API_KEY` 和可选的 `TRIPO_BASE_URL`;真实值只放本地 `.env` 或服务器密钥管理,不得写入 Git。
|
||||
- DeepSeek、Kimi、GLM 和 Seedance 适配器只读取服务端环境变量,API Key 不进入模型目录、日志或用户响应。
|
||||
- 当前 Tripo 任务归属映射仍随 Control Plane 进程保存在内存中,服务重启后会清空;D6 接入持久化任务表与队列后解决。
|
||||
@@ -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: '服务暂时不可用' } })
|
||||
}
|
||||
|
||||
@@ -15,6 +15,20 @@ test('mock catalog contains sync multimodal and async 3D models', async () => {
|
||||
assert.ok(models.some((model) => model.type === '3d' && model.mode === 'async'))
|
||||
})
|
||||
|
||||
test('initial provider catalog includes DeepSeek, Kimi, GLM and Seedance models', async () => {
|
||||
const catalog = createCatalog()
|
||||
const [providers, models] = await Promise.all([catalog.listProviders(), catalog.listModels()])
|
||||
|
||||
assert.ok(providers.some((provider) => provider.code === 'deepseek' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'kimi' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'zhipu' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'bytedance' && provider.protocol === 'http'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-deepseek' && model.mode === 'sync'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-kimi' && model.type === 'multimodal'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-zhipu' && model.type === 'multimodal'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-bytedance' && model.type === 'video' && model.mode === 'async'))
|
||||
})
|
||||
|
||||
test('platform key is returned once, hashed at rest, and can be revoked', async () => {
|
||||
const repository = new MemoryCatalogRepository()
|
||||
const catalog = new CatalogService(repository)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { OpenAICompatibleProvider, OpenAICompatibleProviderError } from '../src/providers/openai-compatible.js'
|
||||
|
||||
test('OpenAI-compatible adapter forwards chat requests without exposing provider credentials', async () => {
|
||||
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
|
||||
const provider = new OpenAICompatibleProvider({
|
||||
providerName: 'DeepSeek',
|
||||
apiKey: 'test-deepseek-key',
|
||||
baseUrl: 'https://api.deepseek.com/',
|
||||
fetcher: async (url, init) => {
|
||||
requests.push({ url: String(url), init })
|
||||
return new Response(JSON.stringify({ id: 'chat-1', choices: [{ message: { role: 'assistant', content: 'done' } }] }), { status: 200 })
|
||||
},
|
||||
})
|
||||
|
||||
const response = await provider.chatCompletion({ model: 'deepseek-v4-flash', messages: [{ role: 'user', content: 'hello' }] })
|
||||
|
||||
assert.equal(response.choices?.[0]?.message?.content, 'done')
|
||||
assert.equal(requests[0]?.url, 'https://api.deepseek.com/chat/completions')
|
||||
assert.equal(new Headers(requests[0]?.init?.headers).get('Authorization'), 'Bearer test-deepseek-key')
|
||||
assert.deepEqual(JSON.parse(String(requests[0]?.init?.body)), {
|
||||
model: 'deepseek-v4-flash',
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
stream: false,
|
||||
})
|
||||
})
|
||||
|
||||
test('OpenAI-compatible adapter maps upstream failures without leaking credentials', async () => {
|
||||
const provider = new OpenAICompatibleProvider({
|
||||
providerName: 'Kimi',
|
||||
apiKey: 'test-kimi-secret',
|
||||
baseUrl: 'https://api.moonshot.cn/v1',
|
||||
fetcher: async () => new Response(JSON.stringify({ error: { message: 'invalid token' } }), { status: 401 }),
|
||||
})
|
||||
|
||||
await assert.rejects(
|
||||
provider.chatCompletion({ model: 'kimi-k2.6', messages: [] }),
|
||||
(error: unknown) => error instanceof OpenAICompatibleProviderError && error.statusCode === 401 && !error.message.includes('test-kimi-secret'),
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { SeedanceProvider, SeedanceProviderError, mapSeedanceTask } from '../src/providers/seedance.js'
|
||||
|
||||
test('Seedance adapter creates an official asynchronous video generation task', async () => {
|
||||
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
|
||||
const provider = new SeedanceProvider({
|
||||
apiKey: 'test-volcengine-key',
|
||||
fetcher: async (url, init) => {
|
||||
requests.push({ url: String(url), init })
|
||||
return new Response(JSON.stringify({ id: 'cgt-20260828-demo', status: 'queued' }), { status: 200 })
|
||||
},
|
||||
})
|
||||
|
||||
const taskId = await provider.createTextToVideo({ model: 'doubao-seedance-1-0-pro-250528', prompt: 'a cinematic product video' })
|
||||
|
||||
assert.equal(taskId, 'cgt-20260828-demo')
|
||||
assert.equal(requests[0]?.url, 'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks')
|
||||
assert.equal(new Headers(requests[0]?.init?.headers).get('Authorization'), 'Bearer test-volcengine-key')
|
||||
assert.deepEqual(JSON.parse(String(requests[0]?.init?.body)), {
|
||||
model: 'doubao-seedance-1-0-pro-250528',
|
||||
content: [{ type: 'text', text: 'a cinematic product video' }],
|
||||
})
|
||||
})
|
||||
|
||||
test('Seedance task response maps video output and timestamps to MOYI task view', () => {
|
||||
const mapped = mapSeedanceTask({
|
||||
id: 'cgt-20260828-demo',
|
||||
model: 'doubao-seedance-1-0-pro-250528',
|
||||
status: 'succeeded',
|
||||
created_at: 1787880000,
|
||||
updated_at: 1787880060,
|
||||
content: { video_url: 'https://example.test/result.mp4' },
|
||||
})
|
||||
|
||||
assert.equal(mapped.status, 'succeeded')
|
||||
assert.equal(mapped.progress, 100)
|
||||
assert.equal(mapped.output?.videoUrl, 'https://example.test/result.mp4')
|
||||
assert.match(mapped.createdAt || '', /^2026-/)
|
||||
})
|
||||
|
||||
test('Seedance adapter validates task identifiers before making requests', async () => {
|
||||
const provider = new SeedanceProvider({ apiKey: 'test-key', fetcher: async () => new Response('{}') })
|
||||
await assert.rejects(provider.getTask('../invalid'), (error: unknown) => error instanceof SeedanceProviderError)
|
||||
})
|
||||
Reference in new issue
Block a user