From 5e8e4625487ebe9f4d9912e160deb0927df6b4b5 Mon Sep 17 00:00:00 2001 From: WangLX <519407855@qq.com> Date: Sun, 23 Aug 2026 19:21:31 +0800 Subject: [PATCH] feat: add mock key catalog and standard calls --- control-plane/README.md | 11 +- control-plane/migrations/002_catalog_keys.sql | 47 +++ control-plane/src/catalog.ts | 325 ++++++++++++++++++ control-plane/src/migrate.ts | 13 +- control-plane/src/server.ts | 217 ++++++++++++ control-plane/tests/catalog.test.ts | 45 +++ ...n-source-baseline-and-development-nodes.md | 6 +- docs/06-d4-integration-plan.md | 15 + 8 files changed, 671 insertions(+), 8 deletions(-) create mode 100644 control-plane/migrations/002_catalog_keys.sql create mode 100644 control-plane/src/catalog.ts create mode 100644 control-plane/tests/catalog.test.ts diff --git a/control-plane/README.md b/control-plane/README.md index d1fe858..368df0a 100644 --- a/control-plane/README.md +++ b/control-plane/README.md @@ -37,10 +37,18 @@ pnpm dev - `POST /api/auth/login`:账号密码登录,返回 HttpOnly 会话 Cookie。 - `GET /api/auth/me`:读取当前会话用户。 - `POST /api/auth/logout`:撤销当前会话。 +- `GET /api/models`:读取当前可用的 Mock 模型和供应商目录。 +- `GET|POST /api/keys`:查看或创建平台调用 Key;明文 Key 只在创建响应中返回一次。 +- `POST /api/keys/:id/revoke`:撤销当前用户自己的平台 Key。 +- `GET /v1/models`:使用 `Authorization: Bearer ` 查询可调用模型。 +- `POST /v1/chat/completions`:兼容 OpenAI 风格的 Mock 同步文本/多模态调用。 +- `POST /v1/3d/tasks`、`GET /v1/tasks/:id`:Mock 异步 3D 任务创建与查询。 - `GET /api/admin/users`:管理员用户列表。 - `GET|POST /api/admin/invitations`:管理员查看或生成邀请码。 - `POST /api/admin/invitations/:id/revoke`:停用邀请码。 - `GET /api/admin/audit`:管理员查看认证审计。 +- `GET|POST /api/admin/providers`:管理员查看或登记供应商适配位。 +- `GET|POST /api/admin/models`:管理员查看或登记统一模型目录。 第一版暂不实现短信/邮件真实验证码;注册页面保留验证码入口但不以它作为后端注册条件,邀请码、邮箱/手机号和账号密码是当前有效的注册条件。 @@ -49,4 +57,5 @@ pnpm dev - 数据库仅保存密码哈希、邀请码摘要和会话摘要,不保存明文密码、明文邀请码或会话 Token。 - 生产环境必须设置随机的 `AUTH_PASSWORD_PEPPER`、强管理员密码和 HTTPS。 - 管理员接口由 Control Plane 服务端角色校验保护,隐藏前端菜单不构成权限控制。 -- 迁移文件只创建认证相关表,额度、模型和 Key 表在后续 D4 节点加入。 +- D4 的 Mock Key、模型目录暂存于内存仓库;PostgreSQL 迁移已准备 `model_providers`、`model_catalog` 和 `platform_api_keys` 表,接真实持久化时复用同一接口。 +- 上游供应商密钥不进入模型目录或用户响应,后续由 Provider Adapter 通过环境变量/密钥管理服务读取。 diff --git a/control-plane/migrations/002_catalog_keys.sql b/control-plane/migrations/002_catalog_keys.sql new file mode 100644 index 0000000..c24922e --- /dev/null +++ b/control-plane/migrations/002_catalog_keys.sql @@ -0,0 +1,47 @@ +CREATE TABLE IF NOT EXISTS model_providers ( + id varchar(64) PRIMARY KEY, + code varchar(64) NOT NULL UNIQUE, + name varchar(120) NOT NULL, + protocol varchar(32) NOT NULL CHECK (protocol IN ('openai-compatible', 'http', 'sdk')), + base_url text, + description text, + status varchar(16) NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'disabled')), + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS model_catalog ( + id varchar(64) PRIMARY KEY, + provider_id varchar(64) NOT NULL REFERENCES model_providers(id), + public_name varchar(160) NOT NULL, + upstream_model varchar(160) NOT NULL, + model_type varchar(24) NOT NULL CHECK (model_type IN ('text', 'code', 'multimodal', 'image', 'audio', 'video', 'embedding', '3d')), + call_mode varchar(16) NOT NULL CHECK (call_mode IN ('sync', 'stream', 'async')), + description text NOT NULL DEFAULT '', + input_types jsonb NOT NULL DEFAULT '[]'::jsonb, + output_types jsonb NOT NULL DEFAULT '[]'::jsonb, + capabilities jsonb NOT NULL DEFAULT '{}'::jsonb, + status varchar(16) NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'disabled')), + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS model_catalog_provider_id_idx ON model_catalog(provider_id); +CREATE INDEX IF NOT EXISTS model_catalog_status_idx ON model_catalog(status); + +CREATE TABLE IF NOT EXISTS platform_api_keys ( + id uuid PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + name varchar(80) NOT NULL, + key_prefix varchar(24) NOT NULL, + key_hash char(64) NOT NULL UNIQUE, + scopes jsonb NOT NULL DEFAULT '["invoke"]'::jsonb, + model_ids jsonb NOT NULL DEFAULT '[]'::jsonb, + status varchar(16) NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'disabled')), + created_at timestamptz NOT NULL DEFAULT now(), + last_used_at timestamptz, + revoked_at timestamptz +); + +CREATE INDEX IF NOT EXISTS platform_api_keys_user_id_idx ON platform_api_keys(user_id); +CREATE INDEX IF NOT EXISTS platform_api_keys_status_idx ON platform_api_keys(status); diff --git a/control-plane/src/catalog.ts b/control-plane/src/catalog.ts new file mode 100644 index 0000000..b219387 --- /dev/null +++ b/control-plane/src/catalog.ts @@ -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 + 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 + findProviderById(id: string): Promise + createProvider(provider: ProviderRecord): Promise + listModels(includeDisabled?: boolean): Promise + findModelById(id: string): Promise + createModel(model: ModelRecord): Promise + createApiKey(key: ApiKeyRecord): Promise + listApiKeys(userId?: string): Promise + findApiKeyByHash(keyHash: string): Promise + revokeApiKey(id: string, userId: string, at: string): Promise + touchApiKey(id: string, at: string): Promise +} + +export class MemoryCatalogRepository implements CatalogRepository { + private readonly providers = new Map() + private readonly models = new Map() + private readonly apiKeys = new Map() + + 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 + }) { + 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 } : {}), + } +} diff --git a/control-plane/src/migrate.ts b/control-plane/src/migrate.ts index cfedef8..ee79ddc 100644 --- a/control-plane/src/migrate.ts +++ b/control-plane/src/migrate.ts @@ -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') diff --git a/control-plane/src/server.ts b/control-plane/src/server.ts index 5f0fd5a..a71d966 100644 --- a/control-plane/src/server.ts +++ b/control-plane/src/server.ts @@ -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() 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 } + : {}), + }) + 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'}` diff --git a/control-plane/tests/catalog.test.ts b/control-plane/tests/catalog.test.ts new file mode 100644 index 0000000..a1a168a --- /dev/null +++ b/control-plane/tests/catalog.test.ts @@ -0,0 +1,45 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { CatalogService, MemoryCatalogRepository } from '../src/catalog.js' +import { hashSecret } from '../src/auth.js' + +function createCatalog() { + return new CatalogService(new MemoryCatalogRepository()) +} + +test('mock catalog contains sync multimodal and async 3D models', async () => { + const catalog = createCatalog() + const models = await catalog.listModels() + + assert.ok(models.some((model) => model.type === 'multimodal' && model.mode === 'sync')) + assert.ok(models.some((model) => model.type === '3d' && 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) + const created = await catalog.createApiKey('user-1', { name: '验收 Key', modelIds: ['model-mock-text'] }) + const listed = await catalog.listApiKeys('user-1') + + assert.match(created.key, /^sk-moyi-/) + assert.equal(listed[0]?.keyPrefix, created.apiKey.keyPrefix) + assert.equal('key' in listed[0]!, false) + const stored = await repository.findApiKeyByHash(hashSecret(created.key)) + assert.equal(stored?.keyHash, hashSecret(created.key)) + assert.notEqual(stored?.keyHash, created.key) + const authenticated = await catalog.authenticateApiKey(created.key) + assert.ok(authenticated) + assert.equal(catalog.canInvoke(authenticated!, 'model-mock-text'), true) + assert.equal(catalog.canInvoke(authenticated!, 'model-mock-3d'), false) + + await catalog.revokeApiKey('user-1', created.apiKey.id) + assert.equal(await catalog.authenticateApiKey(created.key), undefined) +}) + +test('key model allowlist is enforced by the service', async () => { + const catalog = createCatalog() + const created = await catalog.createApiKey('user-1', { modelIds: ['model-mock-text'] }) + + assert.equal(created.apiKey.modelIds[0], 'model-mock-text') + assert.equal(created.apiKey.scopes.includes('invoke'), true) +}) diff --git a/docs/04-open-source-baseline-and-development-nodes.md b/docs/04-open-source-baseline-and-development-nodes.md index 69428eb..3849b7a 100644 --- a/docs/04-open-source-baseline-and-development-nodes.md +++ b/docs/04-open-source-baseline-and-development-nodes.md @@ -6,7 +6,7 @@ - 阶段:阶段 3,系统设计与工程基线 - 状态:开发基线 v1 - 更新时间:2026-08-20 -- 当前节点:D1,网关适配契约 +- 当前节点:D4,密钥与模型目录 Mock 闭环 ## 一、开源项目使用决策 @@ -114,7 +114,7 @@ POST /v1/tasks | D1 网关适配契约 | 固化开源边界和前端网关接口 | 本文档、类型化 Gateway Client、演示适配器 | 类型检查和前端构建通过 | | D2 Otari 本地基座 | 运行锁定版本的 Otari 与持久化数据库 | Compose、环境模板、健康检查、启动说明 | 重启后配置和用量可持久化 | | D3 身份与权限 | 实现邀请码、登录、会话、角色与管理端守卫 | Control Plane 认证 API、数据库迁移、前端认证客户端、测试 | 普通用户无法访问管理 API | -| D4 密钥与模型目录 | 打通 MOYI Key、网关 Key、模型目录和权限 | Key API、模型 Schema、Otari 映射、管理页面 | 创建 Key 后可完成一次真实调用 | +| D4 密钥与模型目录 | 打通 MOYI Key、网关 Key、模型目录和权限 | Key API、模型 Schema、Otari 映射、管理页面 | Mock Key 可完成一次标准同步调用和异步 3D 任务调用 | | D5 调用与计量 | 打通同步、流式、用量事件和额度账本 | 代理链路、SSE、幂等计量、用量页面 | 调用量与额度变更可追溯且不重复扣减 | | D6 异步任务与 3D | 打通任务提交、排队、进度和结果文件 | Task API、队列、对象存储、AI SoulMate 适配器 | 3D 任务完成正常/失败闭环 | | D7 管理运营 | 接入用户额度、供应商、路由、并发、审计 | 管理端真实 API 与高风险操作审计 | 关键管理操作可追踪和回滚 | @@ -140,4 +140,4 @@ POST /v1/tasks ## 六、当前结论 -当前 D3 已完成认证与权限基线。D4 输入已确认:先使用 Mock 建立 Key/模型目录闭环,真实接入方向包含国内文本/多模态模型和 VAST 相关 3D 模型,测试环境使用现有服务器,首轮规模按 4—5 人、3—4 个项目估算。下一节点进入 D4,补充 MOYI Key、统一模型目录与 Otari 映射。详见 [D4 密钥与模型目录实施计划](06-d4-integration-plan.md)。 +当前 D3 已完成认证与权限基线。D4 输入已确认:先使用 Mock 建立 Key/模型目录闭环,真实接入方向包含国内文本/多模态模型和 VAST 相关 3D 模型,测试环境使用现有服务器,首轮规模按 4—5 人、3—4 个项目估算。D4 Mock 已提供平台 Key 生命周期、统一模型目录、标准同步调用和异步 3D 任务接口;目录使用内存仓库,PostgreSQL 表结构已准备,下一步进入真实 Provider Adapter 与用量计量。详见 [D4 密钥与模型目录实施计划](06-d4-integration-plan.md)。 diff --git a/docs/06-d4-integration-plan.md b/docs/06-d4-integration-plan.md index 6a18127..fff1e80 100644 --- a/docs/06-d4-integration-plan.md +++ b/docs/06-d4-integration-plan.md @@ -78,3 +78,18 @@ SDK 不直接暴露给 MOYI 用户。后续如果上游从 HTTP API 切换为 SD - 企业认证、团队审批和 BYOK。 - 正式公网开放、生产 SLA 和大规模并发承诺。 - 自托管模型和 GPU 调度。 + +## 七、D4 Mock 已实现接口 + +当前 Control Plane 已提供一套可供前端控制台联调的内存 Mock: + +- `GET /api/models`:登录用户读取统一模型与供应商目录,目录中包含一个同步多模态模型和一个异步 3D 模型。 +- `GET|POST /api/keys`:用户查看或创建 MOYI Key;明文只在创建响应返回一次,服务端只保存 SHA-256 摘要。 +- `POST /api/keys/:id/revoke`:用户撤销自己的 Key。 +- `GET /v1/models`:使用 `Authorization: Bearer ` 查询可调用模型。 +- `POST /v1/chat/completions`:兼容 OpenAI 风格的 Mock 同步文本/多模态调用。 +- `POST /v1/3d/tasks`、`GET /v1/tasks/:id`:Mock 异步 3D 任务创建与查询。 +- `GET|POST /api/admin/providers`:管理员查看或登记供应商适配位。 +- `GET|POST /api/admin/models`:管理员查看或登记统一模型目录。 + +Mock 目录使用内存仓库,重启会重置,适合原型与接口联调;`migrations/002_catalog_keys.sql` 已准备 PostgreSQL 表结构,后续接持久化仓库时保持 API 不变。