feat: add mock key catalog and standard calls
This commit is contained in:
1 parent
78ed3c1103
commit
5e8e462548
8 files changed
+671
-8
No files matched your search
@@ -0,0 +1,325 @@
|
||||
import { randomBytes, randomUUID } from 'node:crypto'
|
||||
import { AuthError, hashSecret } from './auth.js'
|
||||
|
||||
export type ModelType = 'text' | 'code' | 'multimodal' | 'image' | 'audio' | 'video' | 'embedding' | '3d'
|
||||
export type CallMode = 'sync' | 'stream' | 'async'
|
||||
export type ProviderProtocol = 'openai-compatible' | 'http' | 'sdk'
|
||||
export type CatalogStatus = 'active' | 'disabled'
|
||||
|
||||
export interface ProviderRecord {
|
||||
id: string
|
||||
code: string
|
||||
name: string
|
||||
protocol: ProviderProtocol
|
||||
baseUrl?: string
|
||||
description?: string
|
||||
status: CatalogStatus
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export interface ModelRecord {
|
||||
id: string
|
||||
providerId: string
|
||||
publicName: string
|
||||
upstreamModel: string
|
||||
type: ModelType
|
||||
mode: CallMode
|
||||
description: string
|
||||
inputTypes: string[]
|
||||
outputTypes: string[]
|
||||
capabilities: Record<string, unknown>
|
||||
status: CatalogStatus
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export interface ApiKeyRecord {
|
||||
id: string
|
||||
userId: string
|
||||
name: string
|
||||
keyPrefix: string
|
||||
keyHash: string
|
||||
scopes: string[]
|
||||
modelIds: string[]
|
||||
status: CatalogStatus
|
||||
createdAt: string
|
||||
lastUsedAt?: string
|
||||
revokedAt?: string
|
||||
}
|
||||
|
||||
export interface PublicApiKey {
|
||||
id: string
|
||||
name: string
|
||||
keyPrefix: string
|
||||
scopes: string[]
|
||||
modelIds: string[]
|
||||
status: CatalogStatus
|
||||
createdAt: string
|
||||
lastUsedAt?: string
|
||||
revokedAt?: string
|
||||
}
|
||||
|
||||
export interface CatalogRepository {
|
||||
listProviders(includeDisabled?: boolean): Promise<ProviderRecord[]>
|
||||
findProviderById(id: string): Promise<ProviderRecord | undefined>
|
||||
createProvider(provider: ProviderRecord): Promise<void>
|
||||
listModels(includeDisabled?: boolean): Promise<ModelRecord[]>
|
||||
findModelById(id: string): Promise<ModelRecord | undefined>
|
||||
createModel(model: ModelRecord): Promise<void>
|
||||
createApiKey(key: ApiKeyRecord): Promise<void>
|
||||
listApiKeys(userId?: string): Promise<ApiKeyRecord[]>
|
||||
findApiKeyByHash(keyHash: string): Promise<ApiKeyRecord | undefined>
|
||||
revokeApiKey(id: string, userId: string, at: string): Promise<boolean>
|
||||
touchApiKey(id: string, at: string): Promise<void>
|
||||
}
|
||||
|
||||
export class MemoryCatalogRepository implements CatalogRepository {
|
||||
private readonly providers = new Map<string, ProviderRecord>()
|
||||
private readonly models = new Map<string, ModelRecord>()
|
||||
private readonly apiKeys = new Map<string, ApiKeyRecord>()
|
||||
|
||||
constructor() {
|
||||
const now = new Date().toISOString()
|
||||
const mockProvider: ProviderRecord = {
|
||||
id: 'provider-mock',
|
||||
code: 'moyi-mock',
|
||||
name: 'MOYI Mock Provider',
|
||||
protocol: 'http',
|
||||
baseUrl: 'http://mock.local',
|
||||
description: '用于控制台联调的 Mock 供应商,不代表真实上游能力。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
const vastProvider: ProviderRecord = {
|
||||
id: 'provider-vast',
|
||||
code: 'vast',
|
||||
name: 'VAST / Tripo',
|
||||
protocol: 'http',
|
||||
description: '预留 3D 生成供应商适配位,D4 先登记目录。',
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
this.providers.set(mockProvider.id, mockProvider)
|
||||
this.providers.set(vastProvider.id, vastProvider)
|
||||
this.models.set('model-mock-text', {
|
||||
id: 'model-mock-text',
|
||||
providerId: mockProvider.id,
|
||||
publicName: 'MOYI Text Pro (Mock)',
|
||||
upstreamModel: 'moyi-mock-text-v1',
|
||||
type: 'multimodal',
|
||||
mode: 'sync',
|
||||
description: '同步文本/代码/图像理解模型,用于首轮 HTTP 和 SDK 验收。',
|
||||
inputTypes: ['text', 'image'],
|
||||
outputTypes: ['text'],
|
||||
capabilities: { streaming: true, vision: true, toolCalling: true },
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
this.models.set('model-mock-3d', {
|
||||
id: 'model-mock-3d',
|
||||
providerId: vastProvider.id,
|
||||
publicName: 'VAST 3D Generator (Mock)',
|
||||
upstreamModel: 'tripo-mock-3d-v1',
|
||||
type: '3d',
|
||||
mode: 'async',
|
||||
description: '异步 3D 生成模型,预留任务创建、轮询和回调字段。',
|
||||
inputTypes: ['text', 'image'],
|
||||
outputTypes: ['model', 'thumbnail'],
|
||||
capabilities: { taskPolling: true, webhook: true, formats: ['glb', 'obj', 'fbx'] },
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
async listProviders(includeDisabled = false) {
|
||||
return [...this.providers.values()]
|
||||
.filter((provider) => includeDisabled || provider.status === 'active')
|
||||
.sort((left, right) => left.name.localeCompare(right.name))
|
||||
}
|
||||
|
||||
async findProviderById(id: string) {
|
||||
return this.providers.get(id)
|
||||
}
|
||||
|
||||
async createProvider(provider: ProviderRecord) {
|
||||
if (this.providers.has(provider.id) || [...this.providers.values()].some((item) => item.code === provider.code)) {
|
||||
throw new CatalogError('PROVIDER_EXISTS', '供应商标识已存在', 409)
|
||||
}
|
||||
this.providers.set(provider.id, provider)
|
||||
}
|
||||
|
||||
async listModels(includeDisabled = false) {
|
||||
return [...this.models.values()]
|
||||
.filter((model) => includeDisabled || model.status === 'active')
|
||||
.sort((left, right) => left.publicName.localeCompare(right.publicName))
|
||||
}
|
||||
|
||||
async findModelById(id: string) {
|
||||
return this.models.get(id)
|
||||
}
|
||||
|
||||
async createModel(model: ModelRecord) {
|
||||
if (this.models.has(model.id)) throw new CatalogError('MODEL_EXISTS', '模型标识已存在', 409)
|
||||
if (!this.providers.has(model.providerId)) throw new CatalogError('PROVIDER_NOT_FOUND', '供应商不存在', 404)
|
||||
this.models.set(model.id, model)
|
||||
}
|
||||
|
||||
async createApiKey(key: ApiKeyRecord) {
|
||||
this.apiKeys.set(key.id, key)
|
||||
}
|
||||
|
||||
async listApiKeys(userId?: string) {
|
||||
return [...this.apiKeys.values()]
|
||||
.filter((key) => !userId || key.userId === userId)
|
||||
.sort((left, right) => right.createdAt.localeCompare(left.createdAt))
|
||||
}
|
||||
|
||||
async findApiKeyByHash(keyHash: string) {
|
||||
return [...this.apiKeys.values()].find((key) => key.keyHash === keyHash)
|
||||
}
|
||||
|
||||
async revokeApiKey(id: string, userId: string, at: string) {
|
||||
const key = this.apiKeys.get(id)
|
||||
if (!key || key.userId !== userId || key.status === 'disabled') return false
|
||||
this.apiKeys.set(id, { ...key, status: 'disabled', revokedAt: at })
|
||||
return true
|
||||
}
|
||||
|
||||
async touchApiKey(id: string, at: string) {
|
||||
const key = this.apiKeys.get(id)
|
||||
if (key) this.apiKeys.set(id, { ...key, lastUsedAt: at })
|
||||
}
|
||||
}
|
||||
|
||||
export class CatalogError extends AuthError {}
|
||||
|
||||
export class CatalogService {
|
||||
constructor(private readonly repository: CatalogRepository) {}
|
||||
|
||||
async listModels(includeDisabled = false) {
|
||||
return this.repository.listModels(includeDisabled)
|
||||
}
|
||||
|
||||
async listProviders(includeDisabled = false) {
|
||||
return this.repository.listProviders(includeDisabled)
|
||||
}
|
||||
|
||||
async createProvider(input: { code: string; name: string; protocol: ProviderProtocol; baseUrl?: string; description?: 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 和名称不能为空且格式无效')
|
||||
const now = new Date().toISOString()
|
||||
const provider: ProviderRecord = {
|
||||
id: randomUUID(),
|
||||
code,
|
||||
name,
|
||||
protocol: input.protocol,
|
||||
...(input.baseUrl?.trim() ? { baseUrl: input.baseUrl.trim() } : {}),
|
||||
...(input.description?.trim() ? { description: input.description.trim() } : {}),
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
await this.repository.createProvider(provider)
|
||||
return provider
|
||||
}
|
||||
|
||||
async createModel(input: {
|
||||
providerId: string
|
||||
publicName: string
|
||||
upstreamModel: string
|
||||
type: ModelType
|
||||
mode: CallMode
|
||||
description?: string
|
||||
inputTypes?: string[]
|
||||
outputTypes?: string[]
|
||||
capabilities?: Record<string, unknown>
|
||||
}) {
|
||||
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)
|
||||
const now = new Date().toISOString()
|
||||
const model: ModelRecord = {
|
||||
id: randomUUID(),
|
||||
providerId: input.providerId,
|
||||
publicName: input.publicName.trim(),
|
||||
upstreamModel: input.upstreamModel.trim(),
|
||||
type: input.type,
|
||||
mode: input.mode,
|
||||
description: input.description?.trim() || '',
|
||||
inputTypes: input.inputTypes || [],
|
||||
outputTypes: input.outputTypes || [],
|
||||
capabilities: input.capabilities || {},
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}
|
||||
await this.repository.createModel(model)
|
||||
return model
|
||||
}
|
||||
|
||||
async createApiKey(userId: string, input: { name?: string; scopes?: string[]; modelIds?: string[] }) {
|
||||
const name = input.name?.trim() || '默认开发 Key'
|
||||
if (name.length > 80) throw new CatalogError('INVALID_KEY_NAME', 'Key 名称不能超过 80 个字符')
|
||||
const scopes = [...new Set((input.scopes?.length ? input.scopes : ['invoke']).map((scope) => scope.trim()).filter(Boolean))]
|
||||
if (!scopes.length || scopes.some((scope) => !/^[a-z][a-z:_-]{1,31}$/.test(scope))) throw new CatalogError('INVALID_KEY_SCOPE', 'Key 权限范围格式无效')
|
||||
const modelIds = [...new Set(input.modelIds || [])]
|
||||
for (const modelId of modelIds) {
|
||||
if (!await this.repository.findModelById(modelId)) throw new CatalogError('MODEL_NOT_FOUND', `模型不存在:${modelId}`, 404)
|
||||
}
|
||||
const plaintext = `sk-moyi-${randomBytes(24).toString('base64url')}`
|
||||
const now = new Date().toISOString()
|
||||
const record: ApiKeyRecord = {
|
||||
id: randomUUID(),
|
||||
userId,
|
||||
name,
|
||||
keyPrefix: plaintext.slice(0, 16),
|
||||
keyHash: hashSecret(plaintext),
|
||||
scopes,
|
||||
modelIds,
|
||||
status: 'active',
|
||||
createdAt: now,
|
||||
}
|
||||
await this.repository.createApiKey(record)
|
||||
return { key: plaintext, apiKey: toPublicApiKey(record) }
|
||||
}
|
||||
|
||||
async listApiKeys(userId: string) {
|
||||
return (await this.repository.listApiKeys(userId)).map(toPublicApiKey)
|
||||
}
|
||||
|
||||
async revokeApiKey(userId: string, id: string) {
|
||||
const changed = await this.repository.revokeApiKey(id, userId, new Date().toISOString())
|
||||
if (!changed) throw new CatalogError('KEY_NOT_FOUND', 'Key 不存在或已停用', 404)
|
||||
}
|
||||
|
||||
async authenticateApiKey(plaintext: string) {
|
||||
if (!plaintext.trim()) return undefined
|
||||
const record = await this.repository.findApiKeyByHash(hashSecret(plaintext.trim()))
|
||||
if (!record || record.status !== 'active') return undefined
|
||||
await this.repository.touchApiKey(record.id, new Date().toISOString())
|
||||
return record
|
||||
}
|
||||
|
||||
canInvoke(record: ApiKeyRecord, modelId: string) {
|
||||
return record.status === 'active' && record.scopes.includes('invoke') && (!record.modelIds.length || record.modelIds.includes(modelId))
|
||||
}
|
||||
}
|
||||
|
||||
export function toPublicApiKey(key: ApiKeyRecord): PublicApiKey {
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
keyPrefix: key.keyPrefix,
|
||||
scopes: [...key.scopes],
|
||||
modelIds: [...key.modelIds],
|
||||
status: key.status,
|
||||
createdAt: key.createdAt,
|
||||
...(key.lastUsedAt ? { lastUsedAt: key.lastUsedAt } : {}),
|
||||
...(key.revokedAt ? { revokedAt: key.revokedAt } : {}),
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { readFile, readdir } from 'node:fs/promises'
|
||||
import { createRequire } from 'node:module'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
@@ -17,7 +17,12 @@ interface PgPoolConstructor {
|
||||
const runtimeRequire = createRequire(import.meta.url)
|
||||
const { Pool } = runtimeRequire('pg') as { Pool: PgPoolConstructor }
|
||||
const pool = new Pool({ connectionString: databaseUrl })
|
||||
const migration = await readFile(resolve(process.cwd(), 'migrations/001_auth.sql'), 'utf8')
|
||||
await pool.query(migration)
|
||||
const migrationDirectory = resolve(process.cwd(), 'migrations')
|
||||
const migrationFiles = (await readdir(migrationDirectory))
|
||||
.filter((file) => /^\d+_.+\.sql$/.test(file))
|
||||
.sort()
|
||||
for (const file of migrationFiles) {
|
||||
await pool.query(await readFile(resolve(migrationDirectory, file), 'utf8'))
|
||||
console.log(`Applied ${file}`)
|
||||
}
|
||||
await pool.end()
|
||||
console.log('Applied migration 001_auth.sql')
|
||||
@@ -1,6 +1,8 @@
|
||||
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'
|
||||
|
||||
const config = {
|
||||
host: process.env.CONTROL_PLANE_HOST || '127.0.0.1',
|
||||
@@ -21,6 +23,17 @@ const auth = new AuthService(repository, {
|
||||
sessionTtlHours: config.sessionTtlHours,
|
||||
passwordPepper: config.passwordPepper,
|
||||
})
|
||||
const catalog = new CatalogService(new MemoryCatalogRepository())
|
||||
type MockTask = {
|
||||
id: string
|
||||
userId: string
|
||||
model: string
|
||||
status: 'queued' | 'running' | 'succeeded'
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
result?: { format: string; url: string }
|
||||
}
|
||||
const mockTasks = new Map<string, MockTask>()
|
||||
|
||||
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')
|
||||
@@ -73,6 +86,71 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
}
|
||||
const token = readCookie(request.headers.cookie, 'moyi_session')
|
||||
|
||||
if (method === 'GET' && path === '/v1/models') {
|
||||
const keyRecord = await authenticatePlatformKey(request)
|
||||
const models = await catalog.listModels()
|
||||
send(response, 200, {
|
||||
object: 'list',
|
||||
data: models.filter((model) => catalog.canInvoke(keyRecord, model.id)).map((model) => ({
|
||||
id: model.id,
|
||||
object: 'model',
|
||||
owned_by: model.providerId,
|
||||
type: model.type,
|
||||
mode: model.mode,
|
||||
})),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/v1/chat/completions') {
|
||||
const keyRecord = await authenticatePlatformKey(request)
|
||||
const body = await readJson(request)
|
||||
const modelId = stringValue(body.model)
|
||||
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)
|
||||
const prompt = extractPrompt(body.messages)
|
||||
send(response, 200, {
|
||||
id: `chatcmpl-${randomToken(10)}`,
|
||||
object: 'chat.completion',
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: model.id,
|
||||
choices: [{ index: 0, message: { role: 'assistant', content: `MOYI Mock 已收到:${prompt || '(空消息)'}` }, finish_reason: 'stop' }],
|
||||
usage: { prompt_tokens: prompt.length, completion_tokens: 12, total_tokens: prompt.length + 12 },
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/v1/3d/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 (!catalog.canInvoke(keyRecord, model.id)) throw new AuthError('MODEL_FORBIDDEN', '当前 Key 无权调用该模型', 403)
|
||||
const now = new Date().toISOString()
|
||||
const task: MockTask = { id: `task_${randomToken(14)}`, userId: keyRecord.userId, model: model.id, status: 'queued', createdAt: now, updatedAt: now }
|
||||
mockTasks.set(task.id, task)
|
||||
setTimeout(() => {
|
||||
const current = mockTasks.get(task.id)
|
||||
if (!current) return
|
||||
const updatedAt = new Date().toISOString()
|
||||
mockTasks.set(task.id, { ...current, status: 'succeeded', updatedAt, result: { format: 'glb', url: `https://mock.moyi.ai/tasks/${task.id}/result.glb` } })
|
||||
}, 500)
|
||||
send(response, 202, { task: publicTask(task) })
|
||||
return
|
||||
}
|
||||
|
||||
const taskMatch = path.match(/^\/v1\/tasks\/([^/]+)$/)
|
||||
const taskId = taskMatch?.[1]
|
||||
if (method === 'GET' && taskId) {
|
||||
const keyRecord = await authenticatePlatformKey(request)
|
||||
const task = mockTasks.get(taskId)
|
||||
if (!task || task.userId !== keyRecord.userId) throw new AuthError('TASK_NOT_FOUND', '任务不存在', 404)
|
||||
send(response, 200, { task: publicTask(task) })
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/health') {
|
||||
send(response, 200, { status: 'ok', service: 'moyi-control-plane' })
|
||||
return
|
||||
@@ -117,6 +195,42 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/api/models') {
|
||||
await auth.requireUser(token)
|
||||
send(response, 200, {
|
||||
providers: await catalog.listProviders(),
|
||||
models: await catalog.listModels(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/api/keys') {
|
||||
const user = await auth.requireUser(token)
|
||||
send(response, 200, { keys: await catalog.listApiKeys(user.id) })
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/api/keys') {
|
||||
const user = await auth.requireUser(token)
|
||||
const body = await readJson(request)
|
||||
const result = await catalog.createApiKey(user.id, {
|
||||
...(body.name ? { name: stringValue(body.name) } : {}),
|
||||
...(body.scopes !== undefined ? { scopes: stringArrayValue(body.scopes) } : {}),
|
||||
...(body.modelIds !== undefined ? { modelIds: stringArrayValue(body.modelIds) } : {}),
|
||||
})
|
||||
send(response, 201, result)
|
||||
return
|
||||
}
|
||||
|
||||
const keyRevokeMatch = path.match(/^\/api\/keys\/([^/]+)\/revoke$/)
|
||||
const keyId = keyRevokeMatch?.[1]
|
||||
if (method === 'POST' && keyId) {
|
||||
const user = await auth.requireUser(token)
|
||||
await catalog.revokeApiKey(user.id, keyId)
|
||||
send(response, 204, undefined)
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/api/admin/users') {
|
||||
send(response, 200, { users: await auth.listUsers(token) })
|
||||
return
|
||||
@@ -151,6 +265,52 @@ async function route(request: IncomingMessage, response: ServerResponse) {
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/api/admin/providers') {
|
||||
await auth.requireAdmin(token)
|
||||
send(response, 200, { providers: await catalog.listProviders(true) })
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/api/admin/providers') {
|
||||
await auth.requireAdmin(token)
|
||||
const body = await readJson(request)
|
||||
const result = await catalog.createProvider({
|
||||
code: stringValue(body.code),
|
||||
name: stringValue(body.name),
|
||||
protocol: providerProtocol(body.protocol),
|
||||
...(body.baseUrl ? { baseUrl: stringValue(body.baseUrl) } : {}),
|
||||
...(body.description ? { description: stringValue(body.description) } : {}),
|
||||
})
|
||||
send(response, 201, { provider: result })
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'GET' && path === '/api/admin/models') {
|
||||
await auth.requireAdmin(token)
|
||||
send(response, 200, { models: await catalog.listModels(true) })
|
||||
return
|
||||
}
|
||||
|
||||
if (method === 'POST' && path === '/api/admin/models') {
|
||||
await auth.requireAdmin(token)
|
||||
const body = await readJson(request)
|
||||
const result = await catalog.createModel({
|
||||
providerId: stringValue(body.providerId),
|
||||
publicName: stringValue(body.publicName),
|
||||
upstreamModel: stringValue(body.upstreamModel),
|
||||
type: modelType(body.type),
|
||||
mode: callMode(body.mode),
|
||||
...(body.description ? { description: stringValue(body.description) } : {}),
|
||||
...(body.inputTypes !== undefined ? { inputTypes: stringArrayValue(body.inputTypes) } : {}),
|
||||
...(body.outputTypes !== undefined ? { outputTypes: stringArrayValue(body.outputTypes) } : {}),
|
||||
...(body.capabilities && typeof body.capabilities === 'object' && !Array.isArray(body.capabilities)
|
||||
? { capabilities: body.capabilities as Record<string, unknown> }
|
||||
: {}),
|
||||
})
|
||||
send(response, 201, { model: result })
|
||||
return
|
||||
}
|
||||
|
||||
send(response, 404, { error: { code: 'NOT_FOUND', message: '接口不存在' } })
|
||||
}
|
||||
|
||||
@@ -174,6 +334,63 @@ function stringValue(value: unknown) {
|
||||
return typeof value === 'string' ? value : ''
|
||||
}
|
||||
|
||||
async function authenticatePlatformKey(request: IncomingMessage) {
|
||||
const authorization = request.headers.authorization || ''
|
||||
const match = authorization.match(/^Bearer\s+(.+)$/i)
|
||||
const record = await catalog.authenticateApiKey(match?.[1] || '')
|
||||
if (!record) throw new AuthError('INVALID_API_KEY', 'MOYI API Key 无效或已停用', 401)
|
||||
return record
|
||||
}
|
||||
|
||||
async function catalogModel(id: string) {
|
||||
const model = (await catalog.listModels()).find((item) => item.id === id)
|
||||
if (!model) throw new AuthError('MODEL_NOT_FOUND', '模型不存在或已下线', 404)
|
||||
return model
|
||||
}
|
||||
|
||||
function extractPrompt(messages: unknown) {
|
||||
if (!Array.isArray(messages)) return ''
|
||||
const last = [...messages].reverse().find((message) => message && typeof message === 'object' && 'content' in message) as { content?: unknown } | undefined
|
||||
return typeof last?.content === 'string' ? last.content : ''
|
||||
}
|
||||
|
||||
function randomToken(length: number) {
|
||||
return Buffer.from(randomBytes(Math.ceil(length * 0.75))).toString('base64url').slice(0, length)
|
||||
}
|
||||
|
||||
function publicTask(task: MockTask) {
|
||||
return {
|
||||
id: task.id,
|
||||
object: '3d.task',
|
||||
model: task.model,
|
||||
status: task.status,
|
||||
created_at: task.createdAt,
|
||||
updated_at: task.updatedAt,
|
||||
...(task.result ? { result: task.result } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function stringArrayValue(value: unknown) {
|
||||
if (!Array.isArray(value)) throw new AuthError('INVALID_ARRAY', '字段必须是字符串数组')
|
||||
return value.filter((item): item is string => typeof item === 'string')
|
||||
}
|
||||
|
||||
function providerProtocol(value: unknown): ProviderProtocol {
|
||||
if (value === 'openai-compatible' || value === 'http' || value === 'sdk') return value
|
||||
throw new AuthError('INVALID_PROVIDER_PROTOCOL', '供应商协议类型无效')
|
||||
}
|
||||
|
||||
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
|
||||
throw new AuthError('INVALID_MODEL_TYPE', '模型类型无效')
|
||||
}
|
||||
|
||||
function callMode(value: unknown): CallMode {
|
||||
if (value === 'sync' || value === 'stream' || value === 'async') return value
|
||||
throw new AuthError('INVALID_CALL_MODE', '调用模式无效')
|
||||
}
|
||||
|
||||
function enforceAuthRateLimit(request: IncomingMessage, action: string) {
|
||||
const now = Date.now()
|
||||
const key = `${action}:${request.socket.remoteAddress || 'unknown'}`
|
||||
|
||||
Reference in new issue
Block a user