"use client"; import * as React from "react"; import { Loader2Icon, PlugZapIcon, PlusIcon, RefreshCwIcon, SaveIcon, StarIcon, Trash2Icon } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { api } from "@/lib/api"; import type { LLMProfile } from "@/lib/types"; import { cn } from "@/lib/utils"; // 思考强度档位(仅在开关打开时生效)。 const EFFORT_LEVELS: { value: string; label: string }[] = [ { value: "low", label: "低" }, { value: "medium", label: "中" }, { value: "high", label: "高" }, { value: "max", label: "最大" }, ]; // 思考模式三态 ↔ 存库字符串 reasoning_effort: // none→""(thinking / reasoning_effort 两个字段都【不发送】,兼容不支持该字段的模型,如 MiniMax) // off →"off"(发送 thinking:{type:disabled},用于默认开启思考、需要显式关闭的模型) // on →强度值(发送 thinking:{enabled} + reasoning_effort) type ThinkMode = "none" | "off" | "on"; const THINK_MODES: { value: ThinkMode; label: string }[] = [ { value: "none", label: "不发送(默认)" }, { value: "off", label: "显式关闭" }, { value: "on", label: "开启" }, ]; const toStore = (mode: ThinkMode, effort: string) => (mode === "on" ? effort : mode === "off" ? "off" : ""); const modeFromStore = (v?: string): ThinkMode => (!v ? "none" : v === "off" ? "off" : "on"); const effortFromStore = (v?: string) => (v && v !== "off" ? v : "high"); function NewProfileDialog({ onCreated }: { onCreated: (id: string) => void }) { const [open, setOpen] = React.useState(false); const [name, setName] = React.useState(""); const [format, setFormat] = React.useState<"anthropic" | "openai">("anthropic"); const [model, setModel] = React.useState(""); const [baseUrl, setBaseUrl] = React.useState(""); const [proxy, setProxy] = React.useState(""); const [apiKey, setApiKey] = React.useState(""); const [rps, setRps] = React.useState("0"); const [rpm, setRpm] = React.useState("0"); const [cw, setCw] = React.useState("0"); const [thinkMode, setThinkMode] = React.useState("none"); const [effort, setEffort] = React.useState("high"); const [models, setModels] = React.useState([]); const [loadingModels, setLoadingModels] = React.useState(false); const [modelsOpen, setModelsOpen] = React.useState(false); function reset() { setName(""); setFormat("anthropic"); setModel(""); setBaseUrl(""); setProxy(""); setApiKey(""); setRps("0"); setRpm("0"); setCw("0"); setThinkMode("none"); setEffort("high"); setModels([]); setModelsOpen(false); } async function loadModels() { if (loadingModels) return; setLoadingModels(true); setModels([]); try { const r = await api.fetchLLMModels(format, baseUrl, apiKey, proxy); if (r.ok && r.models && r.models.length > 0) { setModels(r.models); setModelsOpen(true); toast.success(`已加载 ${r.models.length} 个模型`); } else { toast.error(`加载模型失败:${r.error ?? "未获取到模型"}`); } } catch (e) { toast.error(`加载模型出错:${(e as Error).message}`); } finally { setLoadingModels(false); } } async function create() { if (!name.trim() || !model.trim()) { toast.error("请填写名称与模型"); return; } try { const { id } = await api.saveLLMProfile({ name: name.trim(), format, model: model.trim(), base_url: baseUrl.trim(), proxy: proxy.trim(), api_key: apiKey, rate_per_second: Number(rps) || 0, rate_per_minute: Number(rpm) || 0, context_window_k: Number(cw) || 0, reasoning_effort: toStore(thinkMode, effort), }); toast.success(`已新建 Profile:${name.trim()}(在列表中「设为激活」以启用)`); reset(); setOpen(false); onCreated(String(id)); } catch (e) { toast.error(`新建失败:${(e as Error).message}`); } } return ( { setOpen(o); if (!o) reset(); }} > e.preventDefault()} > 新建 Profile 新建后不会自动激活,请在列表中「设为激活」以启用。
setName(e.target.value)} />
setModel(e.target.value)} /> {models.length > 0 && ( {models.map((m) => ( ))} )}
setBaseUrl(e.target.value)} />
setProxy(e.target.value)} />
setApiKey(e.target.value)} />
setRps(e.target.value)} />
setRpm(e.target.value)} />
setCw(e.target.value)} placeholder="200" />

不发送=不带思考字段(兼容 MiniMax 等不支持该字段的模型);显式关闭=发 disabled(默认开思考的模型才需要);开启=先推理再作答。

{thinkMode === "on" && (

推理投入档位,越高越深入。

)}
); } export default function LLMPage() { const [profiles, setProfiles] = React.useState([]); const [selectedId, setSelectedId] = React.useState(null); const [format, setFormat] = React.useState<"anthropic" | "openai">("anthropic"); const [name, setName] = React.useState(""); const [model, setModel] = React.useState(""); const [baseUrl, setBaseUrl] = React.useState(""); const [proxy, setProxy] = React.useState(""); const [apiKey, setApiKey] = React.useState(""); const [keyHint, setKeyHint] = React.useState(""); const [rps, setRps] = React.useState("0"); const [rpm, setRpm] = React.useState("0"); const [cw, setCw] = React.useState("0"); // 上下文窗口(K tokens);0=默认200K const [thinkMode, setThinkMode] = React.useState("none"); const [effort, setEffort] = React.useState("high"); const [testing, setTesting] = React.useState(false); const [models, setModels] = React.useState([]); const [loadingModels, setLoadingModels] = React.useState(false); const [modelsOpen, setModelsOpen] = React.useState(false); const load = React.useCallback(async () => { try { const ps = await api.llmProfiles(); setProfiles(ps); setSelectedId((prev) => { if (prev && ps.some((p) => p.id === prev)) return prev; return ps.find((p) => p.is_default)?.id ?? ps[0]?.id ?? null; }); } catch { /* ignore */ } }, []); React.useEffect(() => { void load(); }, [load]); // Fill the editor from the selected profile. Runs only on explicit reloads / // selection changes (this page does not poll), so it never clobbers edits. const selected = React.useMemo(() => profiles.find((p) => p.id === selectedId) ?? null, [profiles, selectedId]); React.useEffect(() => { if (!selected) return; setName(selected.name); setFormat(selected.format === "anthropic" ? "anthropic" : "openai"); setModel(selected.model); setBaseUrl(selected.base_url ?? ""); setProxy(selected.proxy ?? ""); setRps(String(selected.rate_per_second ?? 0)); setRpm(String(selected.rate_per_minute ?? 0)); setCw(String(selected.context_window_k ?? 0)); setThinkMode(modeFromStore(selected.reasoning_effort)); setEffort(effortFromStore(selected.reasoning_effort)); setApiKey(""); setKeyHint(selected.api_key_hint ?? ""); }, [selected]); async function loadModels() { if (loadingModels) return; setLoadingModels(true); setModels([]); try { const r = await api.fetchLLMModels( format, baseUrl, apiKey, proxy, selectedId ? Number(selectedId) : undefined, ); if (r.ok && r.models && r.models.length > 0) { setModels(r.models); setModelsOpen(true); toast.success(`已加载 ${r.models.length} 个模型`); } else { toast.error(`加载模型失败:${r.error ?? "未获取到模型"}`); } } catch (e) { toast.error(`加载模型出错:${(e as Error).message}`); } finally { setLoadingModels(false); } } async function testConnection() { if (testing) return; setTesting(true); try { // test with the SAME thinking params the profile will run with, so an // unsupported reasoning field fails here instead of only at run time. // 传当前选中 profile 的 id:输入框留空时,后端用该 profile 已存的 key 测试。 const r = await api.testLLM( format, model, baseUrl, apiKey, proxy, toStore(thinkMode, effort), selectedId ? Number(selectedId) : undefined, ); if (r.ok) toast.success(`连接成功 · ${r.latency_ms ?? "?"}ms · ${r.model ?? model}`); else toast.error(`连接失败:${r.error ?? "未知"}`); } catch (e) { toast.error(`测试出错:${(e as Error).message}`); } finally { setTesting(false); } } async function save() { if (!selectedId) { toast.error("请先在左侧选择或新建一个 Profile"); return; } if (!name.trim() || !model.trim()) { toast.error("请填写名称与模型"); return; } try { await api.saveLLMProfile({ id: Number(selectedId), name: name.trim(), format, model: model.trim(), base_url: baseUrl.trim(), proxy: proxy.trim(), api_key: apiKey, rate_per_second: Number(rps) || 0, rate_per_minute: Number(rpm) || 0, context_window_k: Number(cw) || 0, reasoning_effort: toStore(thinkMode, effort), }); toast.success(selected?.is_default ? "已保存,激活配置即时生效,无需重启" : "已保存"); setApiKey(""); await load(); } catch (e) { toast.error(`保存失败:${(e as Error).message}`); } } <<<<<<< Updated upstream async function activate(id: string) { ======= return ( e.preventDefault()} > {isNew ? "新建模型配置" : `编辑:${profile?.name}`} {profile?.is_default && ( 激活中 )} {isNew ? "新建后不会自动激活,请在卡片上「设为激活」以启用。" : "修改后点击保存;激活配置保存后对全部 Agent 立即生效。"}
setName(e.target.value)} />
setModel(e.target.value)} /> {/* modal: 这个 Popover 的内容被 portal 到 ,在 Sheet 的滚动锁之外, 不加 modal 时列表能渲染却滚不动。modal 让它自己持有最上层滚动锁。 */} {models.length > 0 && ( {models.map((m) => ( ))} )}
setBaseUrl(e.target.value)} />
setProxy(e.target.value)} />

仅 LLM 出站请求走此代理,支持 http/https/socks5,可带账号密码(如 socks5://user:pass@host:port,密码含特殊字符需 URL 编码);留空表示不使用代理(直连)。

setApiKey(e.target.value)} />
setRps(e.target.value)} />
setRpm(e.target.value)} />
setCw(e.target.value)} placeholder="200" />

限速 0 = 不限,全 Agent 共享。上下文窗口单位 K(千 token),0 = 默认 200K,上限 1000(即 1M);设太高会导致压缩不触发。

数字越大越先被选中;激活配置恒为第 1 顺位,与本值无关。相同优先级的配置会轮流打头,天然分摊额度。

setPriority(e.target.value)} />

开启后不会被当作故障转移目标(仍可被 Agent / 任务显式指定使用)。 适合「只给某个 Agent 专用、不希望别人失败时烧掉」的昂贵配置。

控制是否发送 thinking 字段。不发送=不带该字段(兼容 MiniMax 等不支持 的模型);关闭=发 disabled;开启=发 enabled。与下面的强度互相独立。

独立的强度档位(OpenAI reasoning_effort / Anthropic output_config.effort)。 有些接口没有 thinking 字段、只靠强度即可激活思考,故可单独设置、不发送思考开关。

); } // ───────────────────────────────────────────────────────────────────────────── export default function LLMPage() { const [profiles, setProfiles] = React.useState([]); const [pool, setPool] = React.useState(null); const [poolOpen, setPoolOpen] = React.useState(false); // 抽屉的开关和内容分开存:关闭时 editing 保持不变,否则关闭动画期间标题会从 // 「编辑 X」闪成「新建」。editing = null 表示新建。 const [editOpen, setEditOpen] = React.useState(false); const [editing, setEditing] = React.useState(null); const openEditor = React.useCallback((p: LLMProfile | null) => { setEditing(p); setEditOpen(true); }, []); const loadPool = React.useCallback(async () => { try { setPool(await api.llmPool()); } catch { /* ignore */ } }, []); const load = React.useCallback(async () => { try { setProfiles(await api.llmProfiles()); } catch { /* ignore */ } await loadPool(); }, [loadPool]); React.useEffect(() => { void load(); }, [load]); // 卡片上的健康徽章按 profile id 取轮询状态。 const health = React.useMemo(() => { const m = new Map(); for (const c of pool?.chain ?? []) m.set(c.profile_id, c); return m; }, [pool]); async function activate(id: string, name: string) { >>>>>>> Stashed changes try { await api.activateLLMProfile(id); const p = profiles.find((x) => x.id === id); toast.success(`已激活 Profile:${p?.name ?? id}`); await load(); } catch (e) { toast.error(`激活失败:${(e as Error).message}`); } } async function remove(p: LLMProfile) { if (p.is_default) { toast.error("无法删除当前激活的 Profile"); return; } try { await api.deleteLLMProfile(p.id); toast.success(`已删除 Profile:${p.name}`); await load(); } catch (e) { toast.error(`删除失败:${(e as Error).message}`); } } return (

LLM

全 Agent 共享的格式 / 模型 / 限速配置

{selected ? ( 编辑 Profile:{selected.name} {selected.is_default && ( 激活中 )} ) : ( "格式配置" )} {selected ? "修改后点击「保存」。若为激活 Profile,保存后对全部 Agent 立即生效。" : "从左侧选择一个 Profile 进行编辑,或点击「新建」。"} {selected ? (
setName(e.target.value)} />
setModel(e.target.value)} /> {models.length > 0 && ( {models.map((m) => ( ))} )}
setBaseUrl(e.target.value)} />
setProxy(e.target.value)} />

仅 LLM 出站请求走此代理,支持 http/https/socks5;留空则用环境变量(HTTP_PROXY/HTTPS_PROXY)。

setApiKey(e.target.value)} />
setRps(e.target.value)} />
setRpm(e.target.value)} />

0 = 不限

setCw(e.target.value)} placeholder="200" />

模型上下文窗口,用于压缩阈值。单位 K(千 token);0/留空 = 默认 200K;上限 1000(即 1M)。设太高会导致压缩不触发。

不发送=不带思考字段(兼容 MiniMax 等不支持该字段的模型);显式关闭=发 disabled(默认开思考的模型才需要);开启=先推理再作答。

{thinkMode === "on" && (

推理投入档位,越高越深入(OpenAI reasoning_effort / Anthropic output_config.effort)。

)}
) : (
还没有 Profile,点击左侧「新建」创建第一个。
)}
Profiles 点击选中以编辑;全 Agent 用同一激活 Profile,限速全局共享。 { setSelectedId(id); void load(); }} /> {profiles.map((p) => ( // biome-ignore lint/a11y/useSemanticElements: row wraps its own action buttons, so a native
))} {profiles.length === 0 && (
暂无 Profile
)}
); }