106 lines
13 KiB
TypeScript
106 lines
13 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { api } from './api';
|
||
import type { CatalogPreview } from '../shared/contracts/model-catalog.mjs';
|
||
import { ModelErrorDetails } from './ModelErrorDetails';
|
||
import type { RuntimeFailure } from './types';
|
||
|
||
type Source = { id: string; revision: number; name: string; protocol: string; baseUrl: string; brand: string; enabled: boolean; credentialSource?: { connectionId: string } };
|
||
const initial = { name: '', brand: '自定义', protocol: 'chat-completions', baseUrl: 'https://api.openai.com/v1', messagesAuth: 'api-key', requiresKey: true };
|
||
const capabilityNames: Record<string, string> = { text: '文本', tools: '工具调用', streaming: '流式输出', vision: '图像输入', audio: '语音' };
|
||
export function ModelCatalog({ sources, sourceId = '', busy, onSaved, onClose }: {
|
||
sources: Source[]; sourceId?: string; busy: boolean; onSaved: () => Promise<void>; onClose: () => void;
|
||
}) {
|
||
const [source, setSource] = useState(sourceId), [draft, setDraft] = useState(initial);
|
||
const [key, setKey] = useState(''), [persistKey, setPersistKey] = useState(false);
|
||
const [preview, setPreview] = useState<CatalogPreview | null>(null), [chosen, setChosen] = useState<string[]>([]), [query, setQuery] = useState('');
|
||
const [routing, setRouting] = useState('keep'), [defaultModel, setDefaultModel] = useState('');
|
||
const [declare, setDeclare] = useState(false), [working, setWorking] = useState(false), [saving, setSaving] = useState(false);
|
||
const [error, setError] = useState(''), [diagnostic, setDiagnostic] = useState<RuntimeFailure | null>(null), [notice, setNotice] = useState('');
|
||
const live = useRef(true), generation = useRef(0), request = useRef(''), region = useRef<HTMLElement>(null);
|
||
const current = sources.find(value => value.id === source), locked = busy || saving;
|
||
function invalidate() {
|
||
generation.current++; const id = request.current; request.current = '';
|
||
if (id) void api('/model-center/catalog/cancel', { id }).catch(() => {});
|
||
setPreview(null); setChosen([]); setWorking(false); setError(''); setDiagnostic(null); setNotice('');
|
||
}
|
||
useEffect(() => {
|
||
live.current = true; region.current?.scrollIntoView({ block: 'start' });
|
||
return () => { live.current = false; generation.current++; const id = request.current; if (id) void api('/model-center/catalog/cancel', { id }).catch(() => {}); };
|
||
}, []);
|
||
function failed(cause: unknown) {
|
||
setError(cause instanceof Error ? cause.message : '操作未完成,请重新获取。');
|
||
if (cause && typeof cause === 'object' && 'diagnostic' in cause) setDiagnostic(cause.diagnostic as RuntimeFailure | null);
|
||
}
|
||
async function fetchModels() {
|
||
invalidate(); const marker = ++generation.current, requestId = crypto.randomUUID(); request.current = requestId;
|
||
setWorking(true);
|
||
try {
|
||
const result = await api<CatalogPreview>('/model-center/catalog', { requestId,
|
||
...(current ? { id: current.id, revision: current.revision } : { draft, ...(key ? { apiKey: key } : {}) }) });
|
||
if (live.current && generation.current === marker) { setPreview(result); setNotice(`已获取 ${result.entries.length} 个模型。勾选后保存。`); }
|
||
} catch (cause) { if (live.current && generation.current === marker) failed(cause); }
|
||
finally { if (live.current && generation.current === marker) setWorking(false); }
|
||
}
|
||
async function save() {
|
||
if (!preview) return; setSaving(true); setError(''); setDiagnostic(null);
|
||
try {
|
||
const result = await api<{ added: string[]; existing: string[] }>('/model-center/catalog/apply', {
|
||
previewId: preview.id, models: chosen, routing, defaultModel, persistKey: !source && persistKey,
|
||
...(declare ? { declarations: { text: 'supported', tools: 'supported' } } : {}),
|
||
});
|
||
if (live.current) {
|
||
request.current = ''; setKey(''); setPreview(null); setChosen([]);
|
||
setNotice(`已新增 ${result.added.length} 个模型,沿用 ${result.existing.length} 个已有模型。${routing === 'auto' ? '已设为自动适配。' : routing === 'manual' ? '默认模型已更新。' : '可在上方选择使用。'}已有模型的手动设置保持。`);
|
||
await onSaved();
|
||
}
|
||
} catch (cause) { if (live.current) failed(cause); }
|
||
finally { if (live.current) setSaving(false); }
|
||
}
|
||
const visible = preview?.entries.filter(value => (value.id + ' ' + value.name + ' ' + value.owner).toLowerCase().includes(query.trim().toLowerCase())) ?? [];
|
||
const selectable = visible.filter(value => value.lifecycle !== 'retired');
|
||
return <section className="mc-editor mc-catalog" aria-label="上游模型目录" ref={region}>
|
||
<div className="mc-section-title"><div><h3>从上游选择模型</h3><p>获取目录,勾选要用的型号,再决定手动指定或自动适配。</p></div><button className="button secondary" disabled={saving} onClick={onClose}>关闭目录</button></div>
|
||
<div className="mc-form-grid">
|
||
<label className="mc-full">上游连接<select aria-label="目录上游连接" value={source} disabled={locked || working} onChange={event => { invalidate(); setSource(event.target.value); setKey(''); setPersistKey(false); }}>
|
||
<option value="">添加新的上游</option>{sources.filter(value => value.enabled && !value.credentialSource).map(value => <option key={value.id} value={value.id}>{value.name} · {value.baseUrl}</option>)}
|
||
</select></label>
|
||
{!source ? <>
|
||
<label>上游名称<input aria-label="上游名称" value={draft.name} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, name: event.target.value }); }} /></label>
|
||
<label>服务品牌<select aria-label="目录服务品牌" value={draft.brand} disabled={locked || working} onChange={event => {
|
||
invalidate(); const brand = event.target.value;
|
||
setDraft({ ...draft, brand, ...(brand === 'OpenAI' ? { baseUrl: 'https://api.openai.com/v1', protocol: 'responses' } : brand === 'Anthropic' ? { baseUrl: 'https://api.anthropic.com', protocol: 'anthropic-messages' } : brand === 'DeepSeek' ? { baseUrl: 'https://api.deepseek.com', protocol: 'chat-completions' } : {}) });
|
||
}}>{['自定义', 'OpenAI', 'Anthropic', 'DeepSeek', 'Claude 兼容代理', 'Ollama', 'vLLM', '兼容服务'].map(value => <option key={value}>{value}</option>)}</select></label>
|
||
<label>接口协议<select aria-label="目录接口协议" value={draft.protocol} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, protocol: event.target.value }); }}><option value="chat-completions">Chat Completions</option><option value="responses">OpenAI Responses</option><option value="anthropic-messages">Anthropic Messages</option></select></label>
|
||
{draft.protocol === 'anthropic-messages' && <label>Messages 鉴权<select aria-label="目录 Messages 鉴权" value={draft.messagesAuth} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, messagesAuth: event.target.value }); }}><option value="api-key">x-api-key</option><option value="bearer">Bearer</option></select></label>}
|
||
<label className="mc-full">接口根地址<input aria-label="目录接口根地址" type="url" value={draft.baseUrl} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, baseUrl: event.target.value }); }} /></label>
|
||
<label>API Key<input aria-label="目录 API Key" type="password" autoComplete="new-password" value={key} disabled={locked || working} onChange={event => { invalidate(); setKey(event.target.value); }} /></label>
|
||
<label className="mc-checkbox"><input type="checkbox" checked={!draft.requiresKey} disabled={locked || working} onChange={event => { invalidate(); setDraft({ ...draft, requiresKey: !event.target.checked }); }} />此上游不需要 Key</label>
|
||
<label className="mc-checkbox mc-full"><input type="checkbox" checked={persistKey} disabled={locked || working || !key} onChange={event => setPersistKey(event.target.checked)} />保存模型时,将本次新填写的 Key 在本机加密保存</label>
|
||
</> : <p className="mc-help mc-full">使用该连接当前的凭据获取目录;新增型号引用同一凭据来源。</p>}
|
||
</div>
|
||
<div className="mc-row-actions"><button className="button primary" disabled={locked || working || !!source && !current || !source && (!draft.name.trim() || !draft.baseUrl || draft.requiresKey && !key)} onClick={() => void fetchModels()}>{working ? '正在获取模型…' : preview ? '重新获取模型目录' : '获取上游模型'}</button>
|
||
{working && <button className="button secondary" onClick={() => { invalidate(); setNotice('已取消获取。可修改上游后重新获取。'); }}>取消获取</button>}</div>
|
||
<p className="mc-help">只访问模型目录,不发起推理请求。兼容服务未开放目录时,可返回“手动填写(兼容)”。</p>
|
||
{error && <p className="mc-card-warning" role="alert">{error}</p>}{diagnostic && <ModelErrorDetails failure={diagnostic} />}
|
||
{notice && <p className="mc-help" role="status">{notice}</p>}
|
||
{preview && <>
|
||
<div className="mc-catalog-heading"><label>搜索模型<input aria-label="搜索上游模型" value={query} onChange={event => setQuery(event.target.value)} placeholder="型号、显示名称或提供方" /></label><span>显示 {visible.length} / {preview.entries.length} · 已选 {chosen.length}</span></div>
|
||
{preview.warning && <p className="mc-card-warning">{preview.warning}</p>}
|
||
<p className="mc-help">以下为上游目录声明,尚未经过模型调用验证。目录最多1000项,本机最多100个模型连接;单个自动池最多32个成员。</p>
|
||
<div className="mc-row-actions"><button className="button secondary" disabled={locked || !selectable.length || new Set([...chosen, ...selectable.map(value => value.id)]).size > 100} onClick={() => setChosen([...new Set([...chosen, ...selectable.map(value => value.id)])])}>选中当前筛选</button><button className="button secondary" disabled={locked || !chosen.length} onClick={() => setChosen([])}>清空选择</button></div>
|
||
<div className="mc-catalog-list" aria-label="上游模型列表">
|
||
{visible.map(value => <label className={'mc-catalog-model' + (chosen.includes(value.id) ? ' is-selected' : '')} key={value.id}>
|
||
<input type="checkbox" aria-label={'选择模型 ' + value.id} checked={chosen.includes(value.id)} disabled={locked || value.lifecycle === 'retired' || !chosen.includes(value.id) && chosen.length >= 100} onChange={event => setChosen(event.target.checked ? [...chosen, value.id] : chosen.filter(id => id !== value.id))} />
|
||
<span><strong>{value.id}</strong>{value.name !== value.id && <span>{value.name}</span>}<small>{[value.owner, value.contextLimit && `上下文 ${value.contextLimit.toLocaleString()} tokens`, value.maxOutputTokens && `输出上限 ${value.maxOutputTokens.toLocaleString()}`, value.lifecycle && value.lifecycle !== 'active' && (value.lifecycle === 'retired' ? '已退役' : '待退役')].filter(Boolean).join(' · ') || '上游未提供详细元数据'}</small>
|
||
<small>{Object.entries(value.capabilityDeclarations).filter(([, state]) => state !== 'unknown').map(([capability, state]) => `${capabilityNames[capability]}${state === 'supported' ? '支持' : '不支持'}`).join(' · ') || '文本、工具、流式等能力待确认'}{value.efforts.length ? ` · 推理强度 ${value.efforts.join(' / ')}` : ''}</small></span>
|
||
</label>)}{!visible.length && <p className="mc-empty">{preview.entries.length ? '没有匹配的型号。可更改搜索条件。' : '上游返回了空目录。可核对连接或使用手动兼容入口。'}</p>}
|
||
</div>
|
||
<label className="mc-checkbox"><input type="checkbox" checked={declare} disabled={locked} onChange={event => setDeclare(event.target.checked)} />我确认所选型号可通过此协议处理文本及工具调用,仅补充目录未声明的项目</label>
|
||
<div className="mc-form-grid"><label>保存后的使用方式<select aria-label="模型使用方式" value={routing} disabled={locked} onChange={event => setRouting(event.target.value)}><option value="keep">保留当前选择,稍后手动使用</option><option value="manual">手动指定默认模型</option><option value="auto">自动适配所选模型</option></select></label>
|
||
{routing === 'manual' && <label>默认模型<select aria-label="目录默认模型" value={defaultModel} disabled={locked} onChange={event => setDefaultModel(event.target.value)}><option value="">请选择型号</option>{chosen.map(id => <option key={id}>{id}</option>)}</select></label>}</div>
|
||
{routing === 'auto' && <p className="mc-help">自动适配复用模型池,按任务所需能力、连接健康和可用并发筛选,再按轮询分配;同一运行优先保持型号。未确认能力的型号不会用于需要该能力的任务。</p>}
|
||
<div className="mc-actions"><button className="button primary" disabled={locked || !chosen.length || routing === 'manual' && !chosen.includes(defaultModel) || routing === 'auto' && chosen.length > 32} onClick={() => void save()}>{saving ? '正在保存…' : `保存所选 ${chosen.length} 个模型`}</button></div>
|
||
</>}
|
||
</section>;
|
||
}
|