feat: add model catalog filters and channel configuration

This commit is contained in:
wlx committed 2026-08-31 13:41:40 +08:00
1 parent 547f7c0977
commit 2897354dd8
7 files changed
+173 -15

No files matched your search

+45 -3
View File
@@ -5,14 +5,19 @@ export type ModelType = 'text' | 'code' | 'multimodal' | 'image' | 'audio' | 'vi
export type CallMode = 'sync' | 'stream' | 'async'
export type ProviderProtocol = 'openai-compatible' | 'http' | 'sdk'
export type CatalogStatus = 'active' | 'disabled'
export type ProviderSourceType = 'official' | 'relay' | 'self-hosted' | 'custom'
export interface ProviderRecord {
id: string
code: string
name: string
protocol: ProviderProtocol
sourceType: ProviderSourceType
baseUrl?: string
description?: string
credentialType?: 'none' | 'api-key' | 'bearer' | 'access-token'
credentialSecret?: string
credentialMasked?: string
status: CatalogStatus
createdAt: string
updatedAt: string
@@ -21,6 +26,8 @@ export interface ProviderRecord {
export interface ModelRecord {
id: string
providerId: string
modelKey?: string
sourceLabel?: string
publicName: string
upstreamModel: string
type: ModelType
@@ -29,6 +36,9 @@ export interface ModelRecord {
inputTypes: string[]
outputTypes: string[]
capabilities: Record<string, unknown>
pricing?: { input?: number; output?: number; unit?: string }
priority?: number
maxConcurrency?: number
status: CatalogStatus
createdAt: string
updatedAt: string
@@ -104,6 +114,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'moyi-mock',
name: 'MOYI Mock Provider',
protocol: 'http',
sourceType: 'custom',
baseUrl: 'http://mock.local',
description: '用于控制台联调的 Mock 供应商,不代表真实上游能力。',
status: 'active',
@@ -115,6 +126,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'vast',
name: 'VAST / Tripo',
protocol: 'http',
sourceType: 'official',
description: 'Tripo v3 文生 3D 供应商适配器;未配置服务端密钥时自动使用 Mock。',
status: 'active',
createdAt: now,
@@ -125,6 +137,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'deepseek',
name: 'DeepSeek',
protocol: 'openai-compatible',
sourceType: 'official',
baseUrl: 'https://api.deepseek.com',
description: 'DeepSeek 官方 OpenAI 兼容接口;密钥通过 DEEPSEEK_API_KEY 注入。',
status: 'active',
@@ -136,6 +149,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'kimi',
name: '月之暗面 Kimi',
protocol: 'openai-compatible',
sourceType: 'official',
baseUrl: 'https://api.moonshot.ai/v1',
description: 'Kimi 官方 OpenAI 兼容接口;密钥通过 KIMI_API_KEY 注入。',
status: 'active',
@@ -147,6 +161,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'zhipu',
name: '智谱 GLM',
protocol: 'openai-compatible',
sourceType: 'official',
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
description: '智谱 AI 官方兼容接口;密钥通过 ZHIPU_API_KEY 注入。',
status: 'active',
@@ -158,6 +173,7 @@ export class MemoryCatalogRepository implements CatalogRepository {
code: 'bytedance',
name: '字节跳动 Seedance',
protocol: 'http',
sourceType: 'official',
baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
description: '火山方舟 Seedance 异步视频生成接口;密钥通过 VOLCENGINE_API_KEY 注入。',
status: 'active',
@@ -319,10 +335,10 @@ export class CatalogService {
}
async listProviders(includeDisabled = false) {
return this.repository.listProviders(includeDisabled)
return (await this.repository.listProviders(includeDisabled)).map(toPublicProvider)
}
async createProvider(input: { code: string; name: string; protocol: ProviderProtocol; baseUrl?: string; description?: string }) {
async createProvider(input: { code: string; name: string; protocol: ProviderProtocol; sourceType?: ProviderSourceType; baseUrl?: string; description?: string; credentialType?: ProviderRecord['credentialType']; credentialSecret?: string }) {
const code = input.code.trim().toLowerCase()
const name = input.name.trim()
if (!/^[a-z0-9][a-z0-9._-]{1,63}$/.test(code) || !name) throw new CatalogError('INVALID_PROVIDER', '供应商 code 和名称不能为空且格式无效')
@@ -332,18 +348,23 @@ export class CatalogService {
code,
name,
protocol: input.protocol,
sourceType: input.sourceType || 'custom',
...(input.baseUrl?.trim() ? { baseUrl: input.baseUrl.trim() } : {}),
...(input.description?.trim() ? { description: input.description.trim() } : {}),
...(input.credentialType && input.credentialType !== 'none' ? { credentialType: input.credentialType } : {}),
...(input.credentialSecret?.trim() ? { credentialSecret: input.credentialSecret.trim(), credentialMasked: maskCredential(input.credentialSecret.trim()) } : {}),
status: 'active',
createdAt: now,
updatedAt: now,
}
await this.repository.createProvider(provider)
return provider
return toPublicProvider(provider)
}
async createModel(input: {
providerId: string
modelKey?: string
sourceLabel?: string
publicName: string
upstreamModel: string
type: ModelType
@@ -352,13 +373,21 @@ export class CatalogService {
inputTypes?: string[]
outputTypes?: string[]
capabilities?: Record<string, unknown>
pricing?: { input?: number; output?: number; unit?: string }
priority?: number
maxConcurrency?: number
}) {
if (!input.publicName.trim() || !input.upstreamModel.trim()) throw new CatalogError('INVALID_MODEL', '模型名称和上游模型标识不能为空')
if (!await this.repository.findProviderById(input.providerId)) throw new CatalogError('PROVIDER_NOT_FOUND', '供应商不存在', 404)
if (input.priority !== undefined && (!Number.isInteger(input.priority) || input.priority < 0)) throw new CatalogError('INVALID_MODEL_PRIORITY', '模型路由优先级必须是大于等于 0 的整数')
if (input.maxConcurrency !== undefined && (!Number.isInteger(input.maxConcurrency) || input.maxConcurrency < 0)) throw new CatalogError('INVALID_MODEL_CONCURRENCY', '模型并发上限必须是大于等于 0 的整数')
if ([input.pricing?.input, input.pricing?.output].some((value) => value !== undefined && (!Number.isFinite(value) || value < 0))) throw new CatalogError('INVALID_MODEL_PRICING', '模型价格必须是大于等于 0 的数字')
const now = new Date().toISOString()
const model: ModelRecord = {
id: randomUUID(),
providerId: input.providerId,
...(input.modelKey?.trim() ? { modelKey: input.modelKey.trim() } : {}),
...(input.sourceLabel?.trim() ? { sourceLabel: input.sourceLabel.trim() } : {}),
publicName: input.publicName.trim(),
upstreamModel: input.upstreamModel.trim(),
type: input.type,
@@ -367,6 +396,9 @@ export class CatalogService {
inputTypes: input.inputTypes || [],
outputTypes: input.outputTypes || [],
capabilities: input.capabilities || {},
...(input.pricing ? { pricing: input.pricing } : {}),
...(input.priority !== undefined ? { priority: input.priority } : {}),
...(input.maxConcurrency !== undefined ? { maxConcurrency: input.maxConcurrency } : {}),
status: 'active',
createdAt: now,
updatedAt: now,
@@ -476,3 +508,13 @@ export function toPublicApiKey(key: ApiKeyRecord): PublicApiKey {
...(key.revokedAt ? { revokedAt: key.revokedAt } : {}),
}
}
export function toPublicProvider(provider: ProviderRecord) {
const { credentialSecret: _credentialSecret, ...publicProvider } = provider
return publicProvider
}
function maskCredential(secret: string) {
if (secret.length <= 8) return '••••••••'
return `${secret.slice(0, 4)}••••••••${secret.slice(-4)}`
}
+19 -1
View File
@@ -2,7 +2,7 @@ import { randomBytes } from 'node:crypto'
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'
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 { CatalogService, MemoryCatalogRepository, type CallMode, type ModelType, type ProviderProtocol, type ProviderSourceType } 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'
@@ -469,8 +469,11 @@ async function route(request: IncomingMessage, response: ServerResponse) {
code: stringValue(body.code),
name: stringValue(body.name),
protocol: providerProtocol(body.protocol),
sourceType: providerSourceType(body.sourceType),
...(body.baseUrl ? { baseUrl: stringValue(body.baseUrl) } : {}),
...(body.description ? { description: stringValue(body.description) } : {}),
...(body.credentialType ? { credentialType: credentialType(body.credentialType) } : {}),
...(body.credentialSecret ? { credentialSecret: stringValue(body.credentialSecret) } : {}),
})
send(response, 201, { provider: result })
return
@@ -487,6 +490,8 @@ async function route(request: IncomingMessage, response: ServerResponse) {
const body = await readJson(request)
const result = await catalog.createModel({
providerId: stringValue(body.providerId),
...(body.modelKey ? { modelKey: stringValue(body.modelKey) } : {}),
...(body.sourceLabel ? { sourceLabel: stringValue(body.sourceLabel) } : {}),
publicName: stringValue(body.publicName),
upstreamModel: stringValue(body.upstreamModel),
type: modelType(body.type),
@@ -494,6 +499,9 @@ async function route(request: IncomingMessage, response: ServerResponse) {
...(body.description ? { description: stringValue(body.description) } : {}),
...(body.inputTypes !== undefined ? { inputTypes: stringArrayValue(body.inputTypes) } : {}),
...(body.outputTypes !== undefined ? { outputTypes: stringArrayValue(body.outputTypes) } : {}),
...(body.pricing && typeof body.pricing === 'object' && !Array.isArray(body.pricing) ? { pricing: (() => { const pricing = body.pricing as Record<string, unknown>; return { ...(pricing.input !== undefined ? { input: numberValue(pricing.input, 'pricing.input') } : {}), ...(pricing.output !== undefined ? { output: numberValue(pricing.output, 'pricing.output') } : {}), ...(pricing.unit ? { unit: stringValue(pricing.unit) } : {}) } })() } : {}),
...(body.priority !== undefined ? { priority: numberValue(body.priority, 'priority') } : {}),
...(body.maxConcurrency !== undefined ? { maxConcurrency: numberValue(body.maxConcurrency, 'maxConcurrency') } : {}),
...(body.capabilities && typeof body.capabilities === 'object' && !Array.isArray(body.capabilities)
? { capabilities: body.capabilities as Record<string, unknown> }
: {}),
@@ -647,6 +655,16 @@ function providerProtocol(value: unknown): ProviderProtocol {
throw new AuthError('INVALID_PROVIDER_PROTOCOL', '供应商协议类型无效')
}
function providerSourceType(value: unknown): ProviderSourceType {
if (value === 'official' || value === 'relay' || value === 'self-hosted' || value === 'custom') return value
return 'custom'
}
function credentialType(value: unknown): 'none' | 'api-key' | 'bearer' | 'access-token' {
if (value === 'api-key' || value === 'bearer' || value === 'access-token') return value
return 'none'
}
function modelType(value: unknown): ModelType {
const types: ModelType[] = ['text', 'code', 'multimodal', 'image', 'audio', 'video', 'embedding', '3d']
if (typeof value === 'string' && types.includes(value as ModelType)) return value as ModelType