Files
sundynix-agentix/sundynix-admin/src/components/ModelManager.tsx
T
Blizzard 84463394d4 fix(admin): 审计筛选下沉 SQL + 工具数不再写死 + 模型删除加确认
清单里那批小毛病,逐条复核后修(「审计详情列缺失」那条已不成立,早补上了)。

1. 审计筛选只在当前页生效 —— 影响最大的一条。分页是服务端的,筛选却在
   前端对已取回的 50 条做,于是搜一个用户 ID 显示"无结果"时,后面几页
   可能还有几百条。审计的用途就是查证,"搜不到"会被读成"没发生过"。
   改为 action/path/q 三个条件全部落到 SQL,前端只管发条件(防抖 300ms)。

2. 服务状态页把 mcp-go/mcp-py 的工具数写死成 23/4 —— 增删工具后一直骗人,
   且服务离线时照样显示,看不出工具其实一个都没注册上。改取实际上报值。

3. 模型删除一点即删,无任何确认。补二次确认,并对"正在使用中"的模型
   单独说明后果(删掉会立刻打断线上对话/向量能力)。

审计筛选补了 4 组回归测试,两条是踩出来的坑:
  - q 的 OR 组必须带括号:gorm 以 AND 拼接各 Where,裸 OR 会让 action
    条件被绕过(测试里用"同 IP 不同方法"两行钉死这个语义);
  - LIKE 必须显式写 ESCAPE '\':Postgres 默认拿反斜杠当转义符,SQLite
    不写就没有转义符——原来的写法在单测里静默失效,搜 "100%" 命中 0 条。
    顺带把 ILIKE 换成 LOWER()+LIKE,这段才能被内存库覆盖。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 13:51:59 +08:00

192 lines
7.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from "react";
import {
listModels,
saveModel,
setActive,
deleteModel,
testModel,
type Kind,
type Model,
type ModelInput,
} from "../api";
// 复用的模型控制面:列表(激活/脱敏key) + 登记表单 + 测试连接 + 激活/删除。
// 按 kind(chat/embedding) 区分,激活后经 NATS 热更新对应消费方。
export function ModelManager({
kind,
title,
baseUrlHint,
modelHint,
}: {
kind: Kind;
title: string;
baseUrlHint: string;
modelHint: string;
}) {
const empty: ModelInput = { kind, provider: "openai-compatible", base_url: "", api_key: "", model: "" };
const [models, setModels] = useState<Model[]>([]);
const [form, setForm] = useState<ModelInput>(empty);
const [msg, setMsg] = useState("");
const [testing, setTesting] = useState(false);
const refresh = () => listModels(kind).then(setModels).catch((e) => setMsg(`${e.message}`));
useEffect(() => {
refresh();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [kind]);
const set = (k: keyof ModelInput, v: string) => setForm((f) => ({ ...f, [k]: v }));
const onSave = async () => {
try {
await saveModel({ ...form, kind });
setMsg("✓ 已保存");
setForm(empty);
refresh();
} catch (e) {
setMsg(`${(e as Error).message}`);
}
};
const onTest = async () => {
setTesting(true);
try {
const r = await testModel({ ...form, kind });
setMsg(r.ok ? `✓ 连接成功(${r.message}` : `✗ 连接失败:${r.message}`);
} catch (e) {
setMsg(`${(e as Error).message}`);
} finally {
setTesting(false);
}
};
return (
<div className="flex flex-col gap-6">
<section>
<h2 className="mb-3 text-sm font-semibold text-gray-700">{title}</h2>
<div className="overflow-hidden rounded border">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-left text-xs text-gray-500">
<tr>
<th className="px-3 py-2"></th>
<th className="px-3 py-2">Provider</th>
<th className="px-3 py-2">Base URL</th>
<th className="px-3 py-2">Model</th>
<th className="px-3 py-2">API Key</th>
<th className="px-3 py-2"></th>
</tr>
</thead>
<tbody>
{models.length === 0 && (
<tr>
<td colSpan={6} className="px-3 py-4 text-center text-xs text-gray-400">
使
</td>
</tr>
)}
{models.map((m) => (
<tr key={m.id} className="border-t">
<td className="px-3 py-2">
{m.active ? (
<span className="rounded bg-emerald-100 px-1.5 py-0.5 text-[10px] text-emerald-700"></span>
) : (
<span className="text-[10px] text-gray-400"></span>
)}
</td>
<td className="px-3 py-2 text-gray-600">{m.provider}</td>
<td className="px-3 py-2 font-mono text-xs text-gray-600">{m.base_url}</td>
<td className="px-3 py-2 text-gray-800">{m.model}</td>
<td className="px-3 py-2 font-mono text-xs text-gray-400">{m.api_key || "—"}</td>
<td className="px-3 py-2">
<div className="flex gap-2">
{!m.active && (
<button
onClick={() => setActive(m.id).then(() => { setMsg("✓ 已激活并热更新"); refresh(); })}
className="rounded border px-2 py-0.5 text-xs text-violet-600 hover:bg-violet-50"
>
</button>
)}
<button
onClick={() => {
// 删除不可撤销,且删掉在用模型会直接打断线上推理——所以要二次确认,
// 且把"这条正在用"讲明白(此前是一点就删,无任何提示)。
const warn = m.active
? `${m.model}」正在使用中,删除后该 ${m.kind === "embedding" ? "向量" : "对话"}能力立即不可用。`
: `将删除「${m.model}」(${m.provider})。`;
if (!window.confirm(`${warn}\n此操作不可撤销,确定继续?`)) return;
void deleteModel(m.id).then(refresh);
}}
className="rounded border px-2 py-0.5 text-xs text-rose-500 hover:bg-rose-50"
>
</button>
</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
</section>
<section className="max-w-xl">
<h2 className="mb-3 text-sm font-semibold text-gray-700">线 APIOpenAI </h2>
<div className="grid grid-cols-2 gap-3">
<label className="text-xs text-gray-500">
Provider
<select
className="mt-1 w-full rounded border px-2 py-1 text-sm text-gray-900"
value={form.provider}
onChange={(e) => set("provider", e.target.value)}
>
<option value="openai-compatible">openai-compatible线 API</option>
<option value="vllm">vllm</option>
<option value="ollama">ollama</option>
</select>
</label>
<label className="text-xs text-gray-500">
Model
<input
className="mt-1 w-full rounded border px-2 py-1 text-sm"
value={form.model}
onChange={(e) => set("model", e.target.value)}
placeholder={modelHint}
/>
</label>
<label className="col-span-2 text-xs text-gray-500">
Base URL
<input
className="mt-1 w-full rounded border px-2 py-1 text-sm font-mono"
value={form.base_url}
onChange={(e) => set("base_url", e.target.value)}
placeholder={baseUrlHint}
/>
</label>
<label className="col-span-2 text-xs text-gray-500">
API Key
<input
type="password"
className="mt-1 w-full rounded border px-2 py-1 text-sm font-mono"
value={form.api_key}
onChange={(e) => set("api_key", e.target.value)}
placeholder="sk-…"
/>
</label>
</div>
<div className="mt-3 flex items-center gap-2">
<button onClick={onSave} className="rounded bg-violet-600 px-3 py-1 text-sm text-white"></button>
<button
onClick={onTest}
disabled={testing || !form.base_url}
className="rounded border px-3 py-1 text-sm disabled:opacity-40"
>
{testing ? "测试中…" : "测试连接"}
</button>
{msg && <span className="text-xs text-gray-600">{msg}</span>}
</div>
</section>
</div>
);
}