fix: 完善知识库向量模型配置提示与刷新

This commit is contained in:
2026-09-12 20:35:11 +08:00
parent a0b869a333
commit 3507f70e11
5 changed files with 181 additions and 9 deletions

View File

@@ -17,6 +17,8 @@ export function CloudKnowledgePanel({ slug, onCreated, onDeleted }: { slug: stri
const [name, setName] = useState('');
const [model, setModel] = useState('');
const [error, setError] = useState('');
const [catalogError, setCatalogError] = useState('');
const [catalogLoading, setCatalogLoading] = useState(true);
const [busy, setBusy] = useState(false);
const [refresh, setRefresh] = useState(0);
const [creating, setCreating] = useState(false);
@@ -32,8 +34,14 @@ export function CloudKnowledgePanel({ slug, onCreated, onDeleted }: { slug: stri
useEffect(() => { alive.current = true; return () => { alive.current = false; }; }, []);
useEffect(() => {
let current = true;
cloudAgentsApi.call('knowledge', { slug }).then(v => { if (current) setCatalog(v); })
.catch(e => { if (current) setError(String(e.message || e)); });
setCatalogLoading(true); setCatalogError('');
cloudAgentsApi.call('knowledge', { slug }).then(v => {
if (!current) return;
setCatalog(v);
if (!createIntent.current) setModel(previous => v.models.some(item => item.id === previous) ? previous : '');
})
.catch(e => { if (current) setCatalogError(String(e.message || e)); })
.finally(() => { if (current) setCatalogLoading(false); });
return () => { current = false; };
}, [slug, refresh]);
useEffect(() => {
@@ -58,21 +66,35 @@ export function CloudKnowledgePanel({ slug, onCreated, onDeleted }: { slug: stri
<div className="mt-4 space-y-4 text-sm">
<p className="text-xs leading-5 text-muted-foreground">上传到知识库的文档可供获准使用此智能体的人检索。解析后建立索引才会生效,向量调用扣除你的词元点数。</p>
{error && <p role="alert" className="text-destructive">{error}</p>}
<div className="flex gap-2">
<select aria-label="管理的知识库" className="h-10 min-w-0 flex-1 rounded-md border bg-background px-3" value={kbId} disabled={busy}
{catalogError && <p role="alert" className="text-destructive">知识库与模型目录加载失败:{catalogError}。请刷新重试。</p>}
<div className="flex flex-wrap gap-2">
<select aria-label="管理的知识库" className="h-10 min-w-0 flex-1 basis-48 rounded-md border bg-background px-3" value={kbId} disabled={busy}
onChange={e => { setKbId(e.target.value); uploadIntent.current = null; setNext(null); }}>
<option value="">选择我的知识库</option>
{catalog?.databases.map(kb => <option key={kb.kb_id} value={kb.kb_id}>{kb.name}</option>)}
</select>
<Button type="button" variant="outline" disabled={busy} onClick={() => setCreating(v => !v)}>新建知识库</Button>
<Button type="button" variant="ghost" disabled={busy || catalogLoading} onClick={() => setRefresh(v => v + 1)}>刷新知识库与模型</Button>
</div>
{creating && <div className="space-y-3 rounded-md bg-muted/30 p-3">
<Input aria-label="知识库名称" placeholder="知识库名称" value={name} disabled={busy || !!createIntent.current} onChange={e => setName(e.target.value)} />
<select aria-label="向量模型" className="h-10 w-full rounded-md border bg-background px-3" value={model} disabled={busy || !!createIntent.current} onChange={e => setModel(e.target.value)}>
<option value="">选择向量模型</option>{catalog?.models.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
<select aria-label="向量模型" className="h-10 w-full rounded-md border bg-background px-3" value={model}
disabled={busy || !!createIntent.current || catalogLoading || !!catalogError || !catalog?.models.length} onChange={e => setModel(e.target.value)}>
<option value="">{catalogLoading ? '正在加载向量模型…' : catalogError ? '向量模型加载失败' : !catalog?.models.length ? '暂无可用向量模型' : '选择向量模型'}</option>
{catalog?.models.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
</select>
{catalog && !catalog.models.length && <p className="text-xs text-muted-foreground">服务端尚未接入可计费的向量模型。</p>}
<Button type="button" disabled={busy || !name.trim() || !model} onClick={() => void act(async () => {
{!catalogLoading && !catalogError && catalog && !catalog.models.length && <div className="space-y-2 text-xs leading-5 text-muted-foreground">
<p>暂时没有可用的向量模型。请联系平台管理员完成云端配置,然后刷新。</p>
<details>
<summary className="cursor-pointer font-medium text-foreground">管理员配置说明</summary>
<ol className="mt-2 list-decimal space-y-1 pl-5">
<li>Yuxi 后台「智能体管理 → 模型供应商」:启用供应商,添加并启用 embedding 模型,填写模型实际向量维度。</li>
<li>Works Square 运营后台「模型管理」:从 one-api 刷新,激活同一模型并保存。</li>
<li>两端模型 ID 必须完全一致,one-api 需支持该模型的向量接口。配置完成后点击「刷新知识库与模型」。</li>
</ol>
</details>
</div>}
<Button type="button" disabled={busy || !name.trim() || (!createIntent.current && (catalogLoading || !!catalogError || !catalog?.models.some(item => item.id === model)))} onClick={() => void act(async () => {
createIntent.current ??= { operation_id: crypto.randomUUID(), name: name.trim(), embedding_model: model };
const kb = await cloudAgentsApi.call('createKnowledge', { slug, ...createIntent.current });
createIntent.current = null;