feat: harden provider setup validation

This commit is contained in:
john
2026-07-21 17:01:42 +08:00
parent a49bb8c8b0
commit e08eb50775
2 changed files with 66 additions and 5 deletions
+59 -5
View File
@@ -19,6 +19,7 @@ import {
setLlmVisionKey,
stopLlmExecutor,
syncLlmProviderToGoosed,
testLlmProviderDraft,
testLlmProviderKey,
updateLlmProviderKey,
} from '../../api/client';
@@ -34,6 +35,11 @@ import type {
} from '../../types';
const CUSTOM_PROVIDER_ID = '__custom__';
const KIMI_DIRECT_PRESET = {
apiUrl: 'https://api.moonshot.cn/v1',
modelsText: 'kimi-k3\nkimi-k2.6',
defaultModel: 'kimi-k3',
};
const EXECUTOR_VISUALS: Record<string, { icon: string; accent: string }> = {
goose: { icon: 'G', accent: 'blue' },
@@ -62,6 +68,14 @@ function parseModelsText(text: string) {
return [...new Set(text.split(/[\n,]/).map((item) => item.trim()).filter(Boolean))];
}
function formatProviderConnectionError(message?: string) {
const detail = String(message ?? '未知错误').trim();
if (/not found the model|permission denied|resource_not_found_error/i.test(detail)) {
return `模型不存在或当前 API Key 没有该模型权限,请检查模型 ID。上游响应:${detail}`;
}
return detail;
}
function statusText(status?: LlmExecutorLaunchState | null) {
if (!status) return '未启动';
if (status.running) return `运行中${status.pid ? ` · pid ${status.pid}` : ''}`;
@@ -170,6 +184,24 @@ function ProviderDialog({
{isCustom ? (
<>
<div className="form-span-all provider-preset-row">
<button
type="button"
className="ghost-btn"
disabled={busy}
onClick={() => onChange({
...form,
name: form.name || 'Kimi',
apiUrl: KIMI_DIRECT_PRESET.apiUrl,
modelsText: KIMI_DIRECT_PRESET.modelsText,
defaultModel: KIMI_DIRECT_PRESET.defaultModel,
relayProvider: '',
})}
>
使用 Kimi 直连预设
</button>
<span className="muted">自动填写官方 API 地址和模型 ID;直连 Moonshot 时 Relay Provider 必须留空。</span>
</div>
<label>
<span>API 地址</span>
<input placeholder="https://api.example.com/v1/chat/completions" value={form.apiUrl} onChange={(event) => onChange({ ...form, apiUrl: event.target.value })} />
@@ -177,6 +209,7 @@ function ProviderDialog({
<label>
<span>Relay Provider</span>
<input placeholder="可选,例如 ollama" value={form.relayProvider} onChange={(event) => onChange({ ...form, relayProvider: event.target.value })} />
<small className="muted">仅供中转/聚合接口路由使用;OpenAI、Moonshot、DeepSeek 等官方直连接口请留空。</small>
</label>
<label className="form-span-all">
<span>模型列表</span>
@@ -415,8 +448,8 @@ function VisionSection({
<div className="asset-plugin-card-head">
<div className="asset-plugin-icon" aria-hidden="true">◉</div>
<div>
<h3>图片任务模型</h3>
<p>发送含图片的消息时自动切换到此模型(需支持视觉能力,如 Qwen VL)</p>
<h3>图片任务模型(DashScope)</h3>
<p>Qwen VL 审图、聊天识图与 Qwen-Image 生图共用同一 DashScope API Key;此处只需选择视觉模型。</p>
</div>
<span className={`asset-status ${settings?.keyId ? 'is-on' : ''}`}>
{settings?.keyId ? `${settings.keyName} · ${settings.visionModel}` : '未配置'}
@@ -544,6 +577,21 @@ export function ProvidersPage() {
setError(null);
try {
if (providerDialog.mode === 'create') {
if (isCustom) {
const connection = await testLlmProviderDraft({
providerId: CUSTOM_PROVIDER_ID,
name: form.name,
apiKey: form.apiKey,
apiUrl: form.apiUrl,
models: customModels,
defaultModel: form.defaultModel || customModels[0],
testModel: form.defaultModel || customModels[0],
relayProvider: form.relayProvider || undefined,
});
if (!connection.ok) {
throw new Error(`保存前联通校验失败:${formatProviderConnectionError(connection.message)}`);
}
}
await createLlmProviderKey(
isCustom
? {
@@ -581,7 +629,13 @@ export function ProvidersPage() {
},
);
}
setMessage(providerDialog.mode === 'create' ? 'Provider 已添加' : 'Provider 已保存');
setMessage(
providerDialog.mode === 'create' && isCustom
? 'Provider 已添加,保存前联通校验通过'
: providerDialog.mode === 'create'
? 'Provider 已添加'
: 'Provider 已保存',
);
setProviderDialog(null);
await load();
} catch (err) {
@@ -640,7 +694,7 @@ export function ProvidersPage() {
try {
const result = await testLlmProviderKey(row.id, row.defaultModel);
if (result.ok) setMessage(`「${row.name}」联通成功`);
else setError(`「${row.name}」联通失败:${result.message ?? '未知错误'}`);
else setError(`「${row.name}」联通失败:${formatProviderConnectionError(result.message)}`);
} catch (err) {
setError(err instanceof Error ? err.message : '联通测试失败');
} finally {
@@ -894,7 +948,7 @@ export function ProvidersPage() {
})}
</div>
<h3 className="model-center-section-title">图片任务模型</h3>
<h3 className="model-center-section-title">图片任务模型(DashScope)</h3>
<VisionSection
keys={activeKeys}
settings={visionSettings}
+7
View File
@@ -620,6 +620,13 @@ body,
padding: 20px;
}
.provider-preset-row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
}
.modal-actions {
grid-column: 1 / -1;
display: flex;