test gateway readiness and classify upstream failures

This commit is contained in:
wlx committed 2026-09-26 02:35:30 +08:00
1 parent e86a7b1e79
commit 5652a52998
8 files changed
+226 -12

No files matched your search

@@ -482,6 +482,9 @@ public class CatalogService {
putIfNotNull(model, "priority", resultSet.getObject("priority")); putIfNotNull(model, "priority", resultSet.getObject("priority"));
putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency")); putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency"));
model.put("status", resultSet.getString("status")); model.put("status", resultSet.getString("status"));
// Do not advertise real asynchronous routes before their worker exists.
model.put("executionReady", !"async".equals(resultSet.getString("call_mode"))
|| "provider-mock".equals(resultSet.getString("provider_id")));
model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class)); model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
return model; return model;
} }
@@ -59,7 +59,7 @@ public class OpenAiCompatibleProvider {
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502); if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {}); return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
} catch(ApiException e) { throw e; } } catch(ApiException e) { throw e; }
catch(Exception e) { throw upstreamError(); } catch(Exception e) { throw classifyUpstreamError(502, ""); }
} }
public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) { public StreamResponse stream(String providerId, String upstreamModel, Map<String,Object> input) {
@@ -88,8 +88,8 @@ public class OpenAiCompatibleProvider {
long started=System.nanoTime(); long started=System.nanoTime();
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() < 200 || response.statusCode() >= 300) { if (response.statusCode() < 200 || response.statusCode() >= 300) {
response.body().close(); String details = readErrorBody(response.body());
throw upstreamError(); throw classifyUpstreamError(response.statusCode(), details);
} }
if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) { if(streaming && !response.headers().firstValue("Content-Type").orElse("").toLowerCase().startsWith("text/event-stream")) {
response.body().close(); response.body().close();
@@ -132,7 +132,7 @@ public class OpenAiCompatibleProvider {
String data=event.substring(0,event.length()-1); event.setLength(0); String data=event.substring(0,event.length()-1); event.setLength(0);
if("[DONE]".equals(data)) return; if("[DONE]".equals(data)) return;
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){}); Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
if(chunk.containsKey("error")) throw upstreamError(); if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
consumer.accept(chunk); consumer.accept(chunk);
} }
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n'); } else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
@@ -144,7 +144,31 @@ public class OpenAiCompatibleProvider {
} }
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; } @FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
@PreDestroy public void close() { deadlines.shutdownNow(); } @PreDestroy public void close() { deadlines.shutdownNow(); }
private ApiException upstreamError() { return new ApiException("UPSTREAM_PROVIDER_ERROR","上游模型供应商请求失败",502); } private String readErrorBody(InputStream body) {
try (body) {
return new String(body.readNBytes(8192), StandardCharsets.UTF_8);
} catch (IOException ignored) {
return "";
}
}
/**
* Keep provider diagnostics useful without returning the upstream response,
* which may contain account details or other sensitive data.
*/
static ApiException classifyUpstreamError(int status, String body) {
String details = body == null ? "" : body.toLowerCase(java.util.Locale.ROOT);
if (status == 401 || status == 403 || details.matches(".*(invalid.{0,8}(api[ _-]?key|token)|unauthori[sz]ed|forbidden|无效.*(key|凭据)|无权限|认证失败).*")) {
return new ApiException("UPSTREAM_AUTH_FAILED", "上游渠道凭据无效或无权限", 502);
}
if (status == 402 || details.matches(".*(insufficient|余额不足|额度不足|quota.{0,12}(exceed|insufficient|已用尽)|credit.{0,12}(exhaust|insufficient)|billing|payment required|充值).*")) {
return new ApiException("UPSTREAM_CREDIT_INSUFFICIENT", "上游渠道余额或额度不足", 502);
}
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
}
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}
private String normalizeUrl(String value) { private String normalizeUrl(String value) {
return value == null ? "" : value.replaceAll("/+$", ""); return value == null ? "" : value.replaceAll("/+$", "");
@@ -0,0 +1,29 @@
package com.moyi.controlplane.provider;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class ProviderErrorClassificationTest {
@Test
void classifiesProviderCredentialFailuresWithoutEchoingBody() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(401, "{\"error\":\"invalid api key\"}");
assertEquals("UPSTREAM_AUTH_FAILED", error.getCode());
assertEquals(502, error.getStatus());
assertEquals("上游渠道凭据无效或无权限", error.getMessage());
}
@Test
void classifiesProviderCreditFailures() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(200, "余额不足,请充值");
assertEquals("UPSTREAM_CREDIT_INSUFFICIENT", error.getCode());
assertEquals(502, error.getStatus());
}
@Test
void classifiesProviderRateLimitsAsRetryable() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(429, "too many requests");
assertEquals("UPSTREAM_RATE_LIMITED", error.getCode());
assertEquals(429, error.getStatus());
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
"version": "0.1.0", "version": "0.1.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "node --experimental-strip-types --test tests/*.test.mjs", "test": "node --experimental-strip-types --test tests/chat-stream.test.mjs tests/model-catalog.test.mjs",
"dev": "vite", "dev": "vite",
"build": "vue-tsc --noEmit && vite build", "build": "vue-tsc --noEmit && vite build",
"preview": "vite preview" "preview": "vite preview"
+1
View File
@@ -43,6 +43,7 @@ export interface CatalogModel {
pricingConfigured?: boolean pricingConfigured?: boolean
priority?: number priority?: number
maxConcurrency?: number maxConcurrency?: number
executionReady?: boolean
status: 'active' | 'disabled' status: 'active' | 'disabled'
} }
+1
View File
@@ -48,6 +48,7 @@ export interface ModelItem {
pricingConfigured?: boolean pricingConfigured?: boolean
priority?: number priority?: number
maxConcurrency?: number maxConcurrency?: number
executionReady?: boolean
} }
export interface ApiKeyItem { export interface ApiKeyItem {
+18 -6
View File
@@ -66,7 +66,7 @@ const galleryModels = computed<GalleryModel[]>(() => buildModelCatalog(catalogDe
const routes = groups.map(group => catalogModels.value.find(item => item.id === group.id)!).filter(Boolean) const routes = groups.map(group => catalogModels.value.find(item => item.id === group.id)!).filter(Boolean)
const providerNames = [...new Set(routes.map(group => group.provider))] const providerNames = [...new Set(routes.map(group => group.provider))]
return { ...mapCatalogModel(definition), groups: routes, providerNames, return { ...mapCatalogModel(definition), groups: routes, providerNames,
provider: providerNames.join(' / ') || '暂无上线分组', availability: routes.length ? '在线' : '待上线' } provider: providerNames.join(' / ') || '暂无上线分组', availability: routes.some(group => group.pricingConfigured && group.executionReady !== false) ? '在线' : '待上线' }
})) }))
const featuredModels = computed(() => [...galleryModels.value].sort((a, b) => Number(!!b.groups.length) - Number(!!a.groups.length)).slice(0, 3)) const featuredModels = computed(() => [...galleryModels.value].sort((a, b) => Number(!!b.groups.length) - Number(!!a.groups.length)).slice(0, 3))
const activeModel = ref<ModelItem>(emptyModel) const activeModel = ref<ModelItem>(emptyModel)
@@ -237,7 +237,19 @@ function groupPriceLabel(group: ModelItem) {
} }
function hasCallableGroup(model: GalleryModel) { function hasCallableGroup(model: GalleryModel) {
return model.groups.some(group => group.pricingConfigured) return model.groups.some(group => group.pricingConfigured && group.executionReady !== false)
}
function modelAvailabilityLabel(model: GalleryModel) {
if (!model.groups.length) return '待上线'
if (hasCallableGroup(model)) return '在线'
if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.executionReady === false)) return '异步执行器待接入'
return '价格待配置'
}
function modelUnavailableLabel(model: GalleryModel) {
if (model.mode === '异步' && model.groups.some(group => group.pricingConfigured && group.executionReady === false)) return '异步执行器待接入'
return '价格待配置'
} }
async function copy(value: string, label: string, target: string) { async function copy(value: string, label: string, target: string) {
@@ -425,7 +437,7 @@ function mapCatalogModels(items: CatalogModel[], providers: Array<{ id: string;
...mapCatalogModel(item), logicalModelId: item.logicalModelId, ...mapCatalogModel(item), logicalModelId: item.logicalModelId,
provider: providerNames.get(item.providerId) || item.providerId, provider: providerNames.get(item.providerId) || item.providerId,
vendorName: item.vendorName, vendorName: item.vendorName,
sourceLabel: item.sourceLabel, pricing: item.pricing, pricingConfigured: item.pricingConfigured, priority: item.priority, maxConcurrency: item.maxConcurrency, sourceLabel: item.sourceLabel, pricing: item.pricing, pricingConfigured: item.pricingConfigured, priority: item.priority, maxConcurrency: item.maxConcurrency, executionReady: item.executionReady,
})) }))
} }
@@ -591,7 +603,7 @@ async function createProject() {
<template v-else-if="view === 'models'"> <template v-else-if="view === 'models'">
<section class="page-heading"><div><span>MODEL CATALOG</span><h1>模型广场</h1><p>按能力选择模型,用一个 Key 完成接入。</p></div><button class="button secondary" @click="emit('navigate', 'docs')"><BookOpen :size="16" />接入指南</button></section> <section class="page-heading"><div><span>MODEL CATALOG</span><h1>模型广场</h1><p>按能力选择模型,用一个 Key 完成接入。</p></div><button class="button secondary" @click="emit('navigate', 'docs')"><BookOpen :size="16" />接入指南</button></section>
<section class="model-filter-panel"><div class="model-filter-head"><div><strong>筛选模型</strong><span>模型厂商是实际研发方,供应商是请求经过的渠道或中转站</span></div><button v-if="activeModelFilters.length || modelQuery" class="text-link" @click="resetModelFilters">清空筛选</button></div><div class="model-filter-grid"><label class="search-input model-filter-search"><Search :size="16" /><input v-model="modelQuery" placeholder="搜索模型名称、厂商、渠道或能力" /></label><label class="select-filter"><span>模型厂商</span><select v-model="modelVendor"><option value="全部">全部模型厂商</option><option v-for="vendor in modelVendors" :key="vendor" :value="vendor">{{ vendor }}</option></select></label><label class="select-filter"><span>供应商 / 渠道</span><select v-model="modelProvider"><option value="全部">全部渠道</option><option v-for="provider in modelProviders" :key="provider" :value="provider">{{ provider }}</option></select></label><label class="select-filter"><span>模型类型</span><select v-model="modelType"><option v-for="type in modelTypes" :key="type" :value="type">{{ type === '全部' ? '全部类型' : type }}</option></select></label><label class="select-filter"><span>调用方式</span><select v-model="modelMode"><option v-for="mode in callModes" :key="mode" :value="mode">{{ mode === '全部' ? '全部方式' : mode }}</option></select></label><label class="select-filter"><span>可用状态</span><select v-model="modelAvailability"><option value="全部">全部状态</option><option value="在线">在线</option><option value="待上线">待上线</option></select></label></div><div class="filter-summary"><span>共 {{ filteredModels.length }} 个模型</span><span v-if="activeModelFilters.length">已选:{{ activeModelFilters.join(' · ') }}</span></div></section> <section class="model-filter-panel"><div class="model-filter-head"><div><strong>筛选模型</strong><span>模型厂商是实际研发方,供应商是请求经过的渠道或中转站</span></div><button v-if="activeModelFilters.length || modelQuery" class="text-link" @click="resetModelFilters">清空筛选</button></div><div class="model-filter-grid"><label class="search-input model-filter-search"><Search :size="16" /><input v-model="modelQuery" placeholder="搜索模型名称、厂商、渠道或能力" /></label><label class="select-filter"><span>模型厂商</span><select v-model="modelVendor"><option value="全部">全部模型厂商</option><option v-for="vendor in modelVendors" :key="vendor" :value="vendor">{{ vendor }}</option></select></label><label class="select-filter"><span>供应商 / 渠道</span><select v-model="modelProvider"><option value="全部">全部渠道</option><option v-for="provider in modelProviders" :key="provider" :value="provider">{{ provider }}</option></select></label><label class="select-filter"><span>模型类型</span><select v-model="modelType"><option v-for="type in modelTypes" :key="type" :value="type">{{ type === '全部' ? '全部类型' : type }}</option></select></label><label class="select-filter"><span>调用方式</span><select v-model="modelMode"><option v-for="mode in callModes" :key="mode" :value="mode">{{ mode === '全部' ? '全部方式' : mode }}</option></select></label><label class="select-filter"><span>可用状态</span><select v-model="modelAvailability"><option value="全部">全部状态</option><option value="在线">在线</option><option value="待上线">待上线</option></select></label></div><div class="filter-summary"><span>共 {{ filteredModels.length }} 个模型</span><span v-if="activeModelFilters.length">已选:{{ activeModelFilters.join(' · ') }}</span></div></section>
<section class="model-grid catalog"><article v-for="model in pagedModels" :key="model.id" class="model-card catalog-card" @click="selectedModel = model"><div class="model-card-top"><span class="model-icon" :class="model.accent"><component :is="model.icon" :size="21" /></span><span class="availability" :class="{ pending: !hasCallableGroup(model) }"><i></i>{{ !model.groups.length ? '待上线' : hasCallableGroup(model) ? '在线' : '价格待配置' }}</span></div><div class="model-title"><h3>{{ model.name }}</h3><span>{{ model.type }}</span></div><small class="provider"><strong>{{ model.vendorName || '未标注厂商' }}</strong><span>渠道:{{ model.provider }}</span><span v-if="model.sourceLabel"> · {{ model.sourceLabel }}</span></small><p>{{ model.description }}</p><div class="model-pricing"><span><strong>标准价</strong>{{ standardPriceLabel(model) }}</span><span><strong>分组折扣</strong><template v-if="model.groups.length"><em v-for="group in model.groups" :key="group.id">{{ group.sourceLabel || '默认分组' }}:{{ groupDiscountLabel(group) }}</em></template><em v-else>暂无上线分组</em></span></div><div class="model-specs"><span><Timer :size="14" />{{ model.latency }}</span><span><Layers3 :size="14" />{{ model.specification }}</span></div><footer><span class="mode-tag" :class="{ async: model.mode === '异步' }">{{ model.mode }}调用</span><button class="text-link" :disabled="!hasCallableGroup(model)" @click.stop="tryModel(model)">{{ !model.groups.length ? '分组待上线' : hasCallableGroup(model) ? `在线试用 · ${model.groups.length} 个分组` : '价格待配置' }}<ArrowRight :size="14" /></button></footer></article><div v-if="!filteredModels.length" class="empty-state"><Search :size="24" /><strong>没有找到匹配模型</strong><span>换个关键词或筛选条件试试</span></div></section> <section class="model-grid catalog"><article v-for="model in pagedModels" :key="model.id" class="model-card catalog-card" @click="selectedModel = model"><div class="model-card-top"><span class="model-icon" :class="model.accent"><component :is="model.icon" :size="21" /></span><span class="availability" :class="{ pending: !hasCallableGroup(model) }"><i></i>{{ modelAvailabilityLabel(model) }}</span></div><div class="model-title"><h3>{{ model.name }}</h3><span>{{ model.type }}</span></div><small class="provider"><strong>{{ model.vendorName || '未标注厂商' }}</strong><span>渠道:{{ model.provider }}</span><span v-if="model.sourceLabel"> · {{ model.sourceLabel }}</span></small><p>{{ model.description }}</p><div class="model-pricing"><span><strong>标准价</strong>{{ standardPriceLabel(model) }}</span><span><strong>分组折扣</strong><template v-if="model.groups.length"><em v-for="group in model.groups" :key="group.id">{{ group.sourceLabel || '默认分组' }}:{{ groupDiscountLabel(group) }}</em></template><em v-else>暂无上线分组</em></span></div><div class="model-specs"><span><Timer :size="14" />{{ model.latency }}</span><span><Layers3 :size="14" />{{ model.specification }}</span></div><footer><span class="mode-tag" :class="{ async: model.mode === '异步' }">{{ model.mode }}调用</span><button class="text-link" :disabled="!hasCallableGroup(model)" @click.stop="tryModel(model)">{{ !model.groups.length ? '分组待上线' : hasCallableGroup(model) ? `在线试用 · ${model.groups.length} 个分组` : modelUnavailableLabel(model) }}<ArrowRight :size="14" /></button></footer></article><div v-if="!filteredModels.length" class="empty-state"><Search :size="24" /><strong>没有找到匹配模型</strong><span>换个关键词或筛选条件试试</span></div></section>
<ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="filteredModels.length" label="模型广场分页" /> <ListPagination v-model:page="modelPage" v-model:page-size="modelPageSize" :total="filteredModels.length" label="模型广场分页" />
</template> </template>
@@ -750,9 +762,9 @@ $env:OPENAI_API_KEY="你的 MOYI 用户 API Key"
<label>调用模型 ID <code>{{ group.id }}</code></label> <label>调用模型 ID <code>{{ group.id }}</code></label>
<button class="text-link" @click="copy(group.id, '调用模型 ID', group.id)"><Copy :size="14" />{{ copySucceeded(group.id) ? '已复制' : '复制调用 ID' }}</button> <button class="text-link" @click="copy(group.id, '调用模型 ID', group.id)"><Copy :size="14" />{{ copySucceeded(group.id) ? '已复制' : '复制调用 ID' }}</button>
<small class="drawer-pricing">标准价:{{ group.pricing?.listPriceUsd ? `${formatUsdPrice(group.pricing.listPriceUsd.input)} 输入 / ${formatUsdPrice(group.pricing.listPriceUsd.output)} 输出 · USD / 1M` : '待同步' }} · {{ groupPriceLabel(group) }} · 对外 {{ groupDiscountLabel(group) }}</small> <small class="drawer-pricing">标准价:{{ group.pricing?.listPriceUsd ? `${formatUsdPrice(group.pricing.listPriceUsd.input)} 输入 / ${formatUsdPrice(group.pricing.listPriceUsd.output)} 输出 · USD / 1M` : '待同步' }} · {{ groupPriceLabel(group) }} · 对外 {{ groupDiscountLabel(group) }}</small>
<button class="button secondary" :disabled="!group.pricingConfigured" @click="tryModel({ ...selectedModel, groups: [group] })">{{ group.pricingConfigured ? '试用此分组' : '价格待配置' }}</button> <button class="button secondary" :disabled="!group.pricingConfigured || group.executionReady === false" @click="tryModel({ ...selectedModel, groups: [group] })">{{ !group.pricingConfigured ? '价格待配置' : group.executionReady === false ? '异步执行器待接入' : '试用此分组' }}</button>
</section> </section>
<template #footer><button class="button primary full" :disabled="!hasCallableGroup(selectedModel)" @click="tryModel(selectedModel)"><Play :size="16" />{{ !selectedModel.groups.length ? '分组待上线' : hasCallableGroup(selectedModel) ? '在线试用' : '价格待配置' }}</button><button class="button secondary full" @click="selectedModel = null; emit('navigate', 'docs')"><BookOpen :size="16" />查看文档</button></template> <template #footer><button class="button primary full" :disabled="!hasCallableGroup(selectedModel)" @click="tryModel(selectedModel)"><Play :size="16" />{{ !selectedModel.groups.length ? '分组待上线' : hasCallableGroup(selectedModel) ? '在线试用' : modelUnavailableLabel(selectedModel) }}</button><button class="button secondary full" @click="selectedModel = null; emit('navigate', 'docs')"><BookOpen :size="16" />查看文档</button></template>
</SlidePanel> </SlidePanel>
<SlidePanel v-if="selectedTask" :title="selectedTask.name" eyebrow="TASK DETAILS" @close="selectedTask = null"><div v-if="selectedTask.videoUrl || selectedTask.previewUrl" class="drawer-task-preview"><video v-if="selectedTask.videoUrl" :src="selectedTask.videoUrl" controls></video><img v-else :src="selectedTask.previewUrl" alt="任务结果预览" /></div><div class="task-drawer-status"><span class="status-chip" :class="selectedTask.status"><i></i>{{ selectedTask.status }}</span><strong>{{ selectedTask.progress }}%</strong></div><div class="drawer-progress"><span><i :style="{ width: `${selectedTask.progress}%` }"></i></span><small>{{ selectedTask.result }}</small></div><dl class="detail-list"><div><dt>任务 ID</dt><dd><code>{{ selectedTask.id }}</code></dd></div><div><dt>模型</dt><dd>{{ selectedTask.model }}</dd></div><div v-if="selectedTask.provider"><dt>上游通道</dt><dd>{{ selectedTask.provider }}</dd></div><div v-if="selectedTask.creditsConsumed !== undefined"><dt>任务消耗</dt><dd>{{ selectedTask.creditsConsumed }} credits</dd></div><div><dt>创建时间</dt><dd>{{ selectedTask.createdAt }}</dd></div><div><dt>耗时</dt><dd>{{ selectedTask.duration }}</dd></div></dl><div v-if="selectedTask.prompt" class="drawer-note"><Info :size="16" /><span>{{ selectedTask.prompt }}</span></div><template #footer><button v-if="selectedTask.modelUrl" class="button primary full" @click="openTaskAsset(selectedTask.modelUrl)"><Download :size="16" />下载 GLB 模型</button><button v-if="selectedTask.videoUrl" class="button primary full" @click="openTaskAsset(selectedTask.videoUrl)"><ExternalLink :size="16" />打开生成视频</button><button class="button secondary full" @click="selectedTask = null">关闭详情</button></template></SlidePanel> <SlidePanel v-if="selectedTask" :title="selectedTask.name" eyebrow="TASK DETAILS" @close="selectedTask = null"><div v-if="selectedTask.videoUrl || selectedTask.previewUrl" class="drawer-task-preview"><video v-if="selectedTask.videoUrl" :src="selectedTask.videoUrl" controls></video><img v-else :src="selectedTask.previewUrl" alt="任务结果预览" /></div><div class="task-drawer-status"><span class="status-chip" :class="selectedTask.status"><i></i>{{ selectedTask.status }}</span><strong>{{ selectedTask.progress }}%</strong></div><div class="drawer-progress"><span><i :style="{ width: `${selectedTask.progress}%` }"></i></span><small>{{ selectedTask.result }}</small></div><dl class="detail-list"><div><dt>任务 ID</dt><dd><code>{{ selectedTask.id }}</code></dd></div><div><dt>模型</dt><dd>{{ selectedTask.model }}</dd></div><div v-if="selectedTask.provider"><dt>上游通道</dt><dd>{{ selectedTask.provider }}</dd></div><div v-if="selectedTask.creditsConsumed !== undefined"><dt>任务消耗</dt><dd>{{ selectedTask.creditsConsumed }} credits</dd></div><div><dt>创建时间</dt><dd>{{ selectedTask.createdAt }}</dd></div><div><dt>耗时</dt><dd>{{ selectedTask.duration }}</dd></div></dl><div v-if="selectedTask.prompt" class="drawer-note"><Info :size="16" /><span>{{ selectedTask.prompt }}</span></div><template #footer><button v-if="selectedTask.modelUrl" class="button primary full" @click="openTaskAsset(selectedTask.modelUrl)"><Download :size="16" />下载 GLB 模型</button><button v-if="selectedTask.videoUrl" class="button primary full" @click="openTaskAsset(selectedTask.videoUrl)"><ExternalLink :size="16" />打开生成视频</button><button class="button secondary full" @click="selectedTask = null">关闭详情</button></template></SlidePanel>
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env python3
"""Exercise the public OpenAI-compatible gateway without printing secrets.
The script deliberately requires the caller to opt in to billed model calls.
Use MOYI_API_KEY from an environment variable, never as a command-line argument.
It checks health, verification configuration, model authorization, sync/stream
responses, and an optional concurrency sweep. Results describe observed behavior;
they do not replace an upstream provider quota agreement.
"""
from __future__ import annotations
import argparse
import concurrent.futures
import json
import os
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass
from typing import Any
@dataclass
class Result:
status: int
elapsed: float
code: str = ""
message: str = ""
streamed: bool = False
def request(url: str, *, method: str = "GET", api_key: str = "", body: dict[str, Any] | None = None, timeout: float = 45) -> tuple[int, bytes]:
data = json.dumps(body).encode() if body is not None else None
headers = {"Accept": "application/json", "User-Agent": "moyi-live-gateway-smoke/1"}
if body is not None:
headers["Content-Type"] = "application/json"
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
req = urllib.request.Request(url, data=data, headers=headers, method=method)
try:
with urllib.request.urlopen(req, timeout=timeout) as response:
return response.status, response.read()
except urllib.error.HTTPError as error:
return error.code, error.read(8192)
def decoded_error(raw: bytes) -> tuple[str, str]:
try:
payload = json.loads(raw.decode("utf-8", "replace"))
error = payload.get("error", payload) if isinstance(payload, dict) else {}
if isinstance(error, dict):
return str(error.get("code", "")), str(error.get("message", ""))
except (ValueError, UnicodeDecodeError):
pass
return "", raw.decode("utf-8", "replace")[:180]
def call(base: str, key: str, model: str, *, stream: bool = False, timeout: float = 60) -> Result:
body = {
"model": model,
"messages": [{"role": "user", "content": "ping"}],
"max_tokens": 1,
"stream": stream,
}
started = time.monotonic()
status, raw = request(f"{base}/chat/completions", method="POST", api_key=key, body=body, timeout=timeout)
elapsed = time.monotonic() - started
code, message = decoded_error(raw) if status >= 300 else ("", "")
if status < 300 and stream and b"data:" not in raw:
return Result(status, elapsed, "INVALID_STREAM", "stream response did not contain SSE data", True)
return Result(status, elapsed, code, message, stream)
def print_result(label: str, result: Result) -> None:
suffix = f" {result.code}: {result.message}" if result.code else ""
print(f"{label}: HTTP {result.status}, {result.elapsed:.2f}s{suffix}")
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="https://moyi.soulofmodel.com/v1", help="public API base URL")
parser.add_argument("--model", help="authorized model/group ID; defaults to the first returned model")
parser.add_argument("--run-calls", action="store_true", help="send billed sync and stream requests")
parser.add_argument("--max-concurrency", type=int, default=0, help="also run levels 1..N in parallel (billed)")
parser.add_argument("--timeout", type=float, default=60)
args = parser.parse_args()
base = args.base_url.rstrip("/")
if not base.endswith("/v1"):
base += "/v1"
root = base[:-3].rstrip("/")
key = os.environ.get("MOYI_API_KEY", "").strip()
print(f"Base URL: {base}")
for path in ("/health", "/api/auth/verification"):
status, raw = request(f"{root}{path}", timeout=args.timeout)
print(f"{path}: HTTP {status} {raw.decode('utf-8', 'replace')[:240]}")
if not key:
print("未设置 MOYI_API_KEY;只完成公开健康检查。", file=sys.stderr)
return 0
status, raw = request(f"{base}/models", api_key=key, timeout=args.timeout)
if status >= 300:
code, message = decoded_error(raw)
print(f"/models: HTTP {status} {code}: {message}", file=sys.stderr)
return 2
try:
models = json.loads(raw.decode("utf-8"))
choices = models.get("data", [])
model = args.model or (choices[0].get("id") if choices else "")
except (ValueError, AttributeError):
print("/models 返回不是有效 JSON", file=sys.stderr)
return 2
print(f"/models: HTTP {status}, authorized models={len(choices)}, selected={model or 'none'}")
if not args.run_calls and args.max_concurrency <= 0:
print("未使用 --run-calls;没有发送会产生上游费用的模型请求。")
return 0
if not model:
print("没有可调用模型,请先为该 Key 授权一个同步或流式模型。", file=sys.stderr)
return 2
if not args.run_calls:
print("并发测试需要 --run-calls;跳过。", file=sys.stderr)
return 2
print_result("sync", call(base, key, model, timeout=args.timeout))
print_result("stream", call(base, key, model, stream=True, timeout=args.timeout))
if args.max_concurrency > 0:
print(f"并发扫测 1..{args.max_concurrency};每个请求 max_tokens=1,可能消耗上游余额。")
for level in range(1, args.max_concurrency + 1):
started = time.monotonic()
with concurrent.futures.ThreadPoolExecutor(max_workers=level) as executor:
results = list(executor.map(lambda _: call(base, key, model, timeout=args.timeout), range(level)))
ok = sum(result.status < 300 for result in results)
buckets: dict[str, int] = {}
for result in results:
label = "ok" if result.status < 300 else result.code or f"HTTP_{result.status}"
buckets[label] = buckets.get(label, 0) + 1
print(f"parallel={level}: {ok}/{level} success, {time.monotonic() - started:.2f}s, {buckets}")
return 0
if __name__ == "__main__":
raise SystemExit(main())