diff --git a/prototype/src/services/catalog.ts b/prototype/src/services/catalog.ts index 9d36ec6..3212574 100644 --- a/prototype/src/services/catalog.ts +++ b/prototype/src/services/catalog.ts @@ -17,6 +17,7 @@ export interface CatalogProvider { export interface CatalogModel { id: string + gatewayModelId?: string logicalModelId: string providerId: string modelKey?: string diff --git a/prototype/src/services/media.ts b/prototype/src/services/media.ts index 836a911..1bf901d 100644 --- a/prototype/src/services/media.ts +++ b/prototype/src/services/media.ts @@ -8,30 +8,56 @@ export interface MediaResult { resultFormat?: string } +const DATA_MEDIA_RE = /^data:(image|video|audio|model)\/[a-z0-9.+-]+;base64,[a-z0-9+/\s]+=*$/i + export function safeMediaUrl(value: unknown): string | undefined { if (typeof value !== 'string' || !value.trim()) return undefined + const candidate = value.trim() + // Providers sometimes return base64 media instead of a hosted URL. Only + // accept data URLs for media and require a base64 payload; never render + // arbitrary data/javascript URLs from an upstream response. + if (DATA_MEDIA_RE.test(candidate)) return candidate.replace(/\s+/g, '') try { - const url = new URL(value) + const url = new URL(candidate) return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : undefined } catch { return undefined } } +function base64DataUrl(value: unknown, mimeType: unknown, format: string): string | undefined { + if (typeof value !== 'string' || !value.trim()) return undefined + const payload = value.trim().replace(/\s+/g, '') + if (!/^[a-z0-9+/]+={0,2}$/i.test(payload) || payload.length % 4 === 1) return undefined + const mime = typeof mimeType === 'string' && /^(image|video|audio|model)\/[a-z0-9.+-]+$/i.test(mimeType) + ? mimeType.toLowerCase() + : ({ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', glb: 'model/gltf-binary', gltf: 'model/gltf+json' } as Record)[format] + return mime ? `data:${mime};base64,${payload}` : undefined +} + +function mediaValue(value: unknown, mimeType: unknown, format: string): string | undefined { + if (typeof value === 'string') return safeMediaUrl(value) || base64DataUrl(value, mimeType, format) + if (!value || typeof value !== 'object') return undefined + const object = value as { url?: unknown; b64_json?: unknown; base64?: unknown; mime_type?: unknown; mimeType?: unknown; format?: unknown } + const objectMime = object.mimeType || object.mime_type || mimeType + const objectFormat = typeof object.format === 'string' ? object.format.toLowerCase() : format + return mediaValue(object.url, objectMime, objectFormat) || base64DataUrl(object.b64_json || object.base64, objectMime, objectFormat) +} + /** Normalize generation assets without treating every provider's generic URL as a 3D model. */ export function mediaResult(result: Record = {}, type = ''): MediaResult { const format = typeof result.format === 'string' ? result.format.toLowerCase() : '' - const url = safeMediaUrl(result.url) - const mime = typeof result.mimeType === 'string' ? result.mimeType.toLowerCase() : '' + const mime = typeof result.mimeType === 'string' ? result.mimeType.toLowerCase() : typeof result.mime_type === 'string' ? result.mime_type.toLowerCase() : '' + const url = mediaValue(result.url, mime, format) || base64DataUrl(result.b64_json || result.base64 || result.data, mime, format) const suffix = url ? new URL(url).pathname.toLowerCase().split('.').pop() : '' const isImage = type === '图像' || type === 'image' || mime.startsWith('image/') || /^(png|jpe?g|webp|gif|avif)$/.test(format || suffix || '') const isVideo = type === '视频' || type === 'video' || mime.startsWith('video/') || /^(mp4|webm|mov)$/.test(format || suffix || '') const isAudio = type === '音频' || type === 'audio' || mime.startsWith('audio/') || /^(mp3|wav|ogg|m4a|flac|aac)$/.test(format || suffix || '') const isModel = type === '3D' || type === '3d' || /^(glb|gltf|obj|fbx|stl)$/.test(format || suffix || '') return { - imageUrl: safeMediaUrl(result.imageUrl) || (isImage ? url : undefined), - videoUrl: safeMediaUrl(result.videoUrl) || (isVideo ? url : undefined), - audioUrl: safeMediaUrl(result.audioUrl) || (isAudio ? url : undefined), - modelUrl: safeMediaUrl(result.modelUrl) || (isModel ? url : undefined), - previewUrl: safeMediaUrl(result.previewUrl), + imageUrl: mediaValue(result.imageUrl, mime, format) || mediaValue(result.image_url, mime, format) || (isImage ? url : undefined), + videoUrl: mediaValue(result.videoUrl, mime, format) || mediaValue(result.video_url, mime, format) || (isVideo ? url : undefined), + audioUrl: mediaValue(result.audioUrl, mime, format) || mediaValue(result.audio_url, mime, format) || (isAudio ? url : undefined), + modelUrl: mediaValue(result.modelUrl, mime, format) || (isModel ? url : undefined), + previewUrl: mediaValue(result.previewUrl, mime, format), resultUrl: url, resultFormat: format || suffix, } diff --git a/prototype/src/types.ts b/prototype/src/types.ts index e6d1cec..a21fae7 100644 --- a/prototype/src/types.ts +++ b/prototype/src/types.ts @@ -21,6 +21,8 @@ export interface NavGroup { export interface ModelItem { logicalModelId?: string id: string + /** Public OpenAI-compatible model id returned by GET /v1/models. */ + gatewayModelId?: string name: string provider: string vendorName?: string diff --git a/prototype/src/views/UserConsole.vue b/prototype/src/views/UserConsole.vue index f0642ff..20b0207 100644 --- a/prototype/src/views/UserConsole.vue +++ b/prototype/src/views/UserConsole.vue @@ -348,7 +348,7 @@ async function runModel() { const selectedMode = callMode.value try { if (callMode.value === '异步') { - const task = await catalogClient.playgroundCreateMediaTask(apiKey, { model: activeModel.value.id, prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, mediaType: activeMediaKind.value! }) + const task = await catalogClient.playgroundCreateMediaTask(apiKey, { model: gatewayModelId(activeModel.value), prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, mediaType: activeMediaKind.value! }) const taskItem = mapAsyncTask(task, activeModel.value, prompt.value) tasks.value.unshift(taskItem) activeTaskId.value = task.id @@ -356,9 +356,9 @@ async function runModel() { scheduleTaskPoll(task.id, apiKey) } else if (selectedMode === '流式') { streamAbort = new AbortController() - await catalogClient.playgroundStreamChat(apiKey, { model: activeModel.value.id, prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, maxTokens: maxOutputTokens.value }, (text, reasoning) => { response.value += text; reasoningOutput.value += reasoning }, streamAbort.signal) + await catalogClient.playgroundStreamChat(apiKey, { model: gatewayModelId(activeModel.value), prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, maxTokens: maxOutputTokens.value }, (text, reasoning) => { response.value += text; reasoningOutput.value += reasoning }, streamAbort.signal) } else { - const result = await catalogClient.playgroundChatCompletion(apiKey, { model: activeModel.value.id, prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, maxTokens: maxOutputTokens.value }) + const result = await catalogClient.playgroundChatCompletion(apiKey, { model: gatewayModelId(activeModel.value), prompt: prompt.value, mediaUrl: mediaUrl.value.trim() || undefined, maxTokens: maxOutputTokens.value }) const choice = result.choices[0] const message = choice?.message const reasoning = typeof message?.reasoning_content === 'string' ? message.reasoning_content : typeof message?.reasoning === 'string' ? message.reasoning : '' @@ -516,13 +516,23 @@ async function createKey() { function mapCatalogModels(items: CatalogModel[], providers: Array<{ id: string; name: string }>): ModelItem[] { const providerNames = new Map(providers.map((provider) => [provider.id, provider.name])) return items.map(item => ({ - ...mapCatalogModel(item), logicalModelId: item.logicalModelId, + ...mapCatalogModel(item), logicalModelId: item.logicalModelId, gatewayModelId: item.gatewayModelId, provider: providerNames.get(item.providerId) || item.providerId, vendorName: item.vendorName, sourceLabel: item.sourceLabel, pricing: item.pricing, pricingConfigured: item.pricingConfigured, billingReady: item.billingReady, priority: item.priority, maxConcurrency: item.maxConcurrency, executionReady: item.executionReady, inputTypes: item.inputTypes, outputTypes: item.outputTypes, })) } +/** + * The catalog id is an internal route id used for permissions. Requests sent + * to the public gateway must use the stable OpenAI-compatible id instead. + * Older control planes do not return gatewayModelId, so retain a safe fallback + * while they are being upgraded. + */ +function gatewayModelId(group: ModelItem): string { + return group.gatewayModelId || group.modelKey || group.id +} + function mapCatalogModel(item: LogicalModel): ModelItem { const typeLabels: Record = { text: '文本', code: '文本', multimodal: '多模态', image: '图像', audio: '音频', video: '视频', embedding: '文本', '3d': '3D' } const modeLabels: Record = { sync: '同步', stream: '流式', async: '异步' } @@ -848,14 +858,14 @@ $env:OPENAI_API_KEY="你的 MOYI 用户 API Key"

该模型已收录,尚无上线分组。管理员完成渠道、分组和计费配置后,即可授权 API Key 并调用。

{{ group.sourceLabel || '默认分组' }}{{ group.provider }} - - + + 标准价:{{ groupStandardPriceLabel(group) }} · {{ groupPriceLabel(group) }} · 对外 {{ groupDiscountLabel(group) }}
-
{{ selectedTask.status }}{{ selectedTask.progress }}%
{{ selectedTask.result }}
任务 ID
{{ selectedTask.id }}
模型
{{ selectedTask.model }}
上游通道
{{ selectedTask.provider }}
任务消耗
{{ selectedTask.creditsConsumed }} credits
创建时间
{{ selectedTask.createdAt }}
耗时
{{ selectedTask.duration }}
{{ selectedTask.prompt }}
+
{{ selectedTask.status }}{{ selectedTask.progress }}%
{{ selectedTask.result }}
任务 ID
{{ selectedTask.id }}
模型
{{ selectedTask.model }}
上游通道
{{ selectedTask.provider }}
任务消耗
{{ selectedTask.creditsConsumed }} credits
创建时间
{{ selectedTask.createdAt }}
耗时
{{ selectedTask.duration }}
{{ selectedTask.prompt }}

确认删除「{{ deleteKeyTarget.name }}」?删除后无法重新启用,已有调用记录和任务会保留。

Key 创建后默认不开放模型权限,请在下一步选择具体可调用模型。
diff --git a/prototype/tests/media.test.mjs b/prototype/tests/media.test.mjs index a3718cc..b9605d4 100644 --- a/prototype/tests/media.test.mjs +++ b/prototype/tests/media.test.mjs @@ -16,3 +16,11 @@ test('mediaResult identifies image/audio and rejects unsafe URLs', () => { assert.equal(image.imageUrl, 'https://cdn.example.test/image.png') assert.equal(audio.audioUrl, 'https://cdn.example.test/sound.mp3') }) + +test('mediaResult supports provider base64 output and nested media objects', () => { + const image = mediaResult({ b64_json: 'iVBORw0KGgo=', format: 'png' }, '') + assert.match(image.imageUrl, /^data:image\/png;base64,iVBORw0KGgo=$/) + const nested = mediaResult({ image_url: { b64_json: 'iVBORw0KGgo=', mime_type: 'image/png' } }, '') + assert.match(nested.imageUrl, /^data:image\/png;base64,iVBORw0KGgo=$/) + assert.equal(safeMediaUrl('data:text/html;base64,PGh0bWw+'), undefined) +})