feat: 设置新增开机启动与自动模型加载列表(多个本地模型按顺序逐个加载,最后一个保持运行)

This commit is contained in:
xianrenge
2026-08-17 12:54:12 +08:00
parent 1a13915f72
commit 6eb197a91a
9 changed files with 454 additions and 104 deletions
+2
View File
@@ -366,6 +366,8 @@ export interface ServerStatus {
export const api = {
appInfo: () => invoke<AppInfo>("app_info"),
autostartStatus: () => invoke<boolean>("autostart_status"),
autostartSet: (enabled: boolean) => invoke<boolean>("autostart_set", { enabled }),
listAgents: () => invoke<Agent[]>("list_agents"),
addAgent: (input: AgentInput) => invoke<Agent>("add_agent", { input }),
updateAgent: (id: string, input: AgentInput) =>
+183 -3
View File
@@ -1,27 +1,92 @@
import { useEffect, useRef, useState } from "react";
import { api, AppInfo } from "../api";
import { useEffect, useMemo, useRef, useState } from "react";
import { api, AppInfo, ModelInfo } from "../api";
import { useStore } from "../store";
export default function SettingsPage() {
const [info, setInfo] = useState<AppInfo | null>(null);
const [settings, setSettings] = useState<Record<string, string>>({});
const [recommendDirDraft, setRecommendDirDraft] = useState("");
const [uploadMsg, setUploadMsg] = useState<string | null>(null);
const [startupMsg, setStartupMsg] = useState<string | null>(null);
const [autostart, setAutostart] = useState<boolean | null>(null);
const [autoLoadDraft, setAutoLoadDraft] = useState<string[]>([]);
const [pickId, setPickId] = useState("");
const uploadInputRef = useRef<HTMLInputElement>(null);
const models = useStore((s) => s.models);
const refreshModels = useStore((s) => s.refreshModels);
useEffect(() => {
api.appInfo().then(setInfo);
api.settingsGet().then(setSettings);
api.autostartStatus().then(setAutostart).catch(() => setAutostart(false));
refreshModels();
}, []);
useEffect(() => {
setRecommendDirDraft(settings.recommend_dir ?? "");
}, [settings.recommend_dir]);
useEffect(() => {
try {
const parsed = JSON.parse(settings.auto_load_models ?? "[]");
setAutoLoadDraft(Array.isArray(parsed) ? parsed : []);
} catch {
setAutoLoadDraft([]);
}
}, [settings.auto_load_models]);
async function handleSave(key: string, value: string) {
await api.settingsSet(key, value);
setSettings((prev) => ({ ...prev, [key]: value }));
}
async function handleAutostartToggle(checked: boolean) {
try {
setAutostart(await api.autostartSet(checked));
setStartupMsg(checked ? "已开启开机启动" : "已关闭开机启动");
} catch (e) {
setStartupMsg(`开机启动设置失败:${String(e)}`);
}
}
const localModels = useMemo(
() => models.filter((m) => m.kind === "local"),
[models],
);
const draftRows = useMemo(
() =>
autoLoadDraft
.map((id) => localModels.find((m) => m.id === id))
.filter((m): m is ModelInfo => Boolean(m)),
[autoLoadDraft, localModels],
);
const available = useMemo(
() => localModels.filter((m) => !autoLoadDraft.includes(m.id)),
[localModels, autoLoadDraft],
);
function moveRow(index: number, dir: -1 | 1) {
setAutoLoadDraft((prev) => {
const next = [...prev];
const target = index + dir;
if (target < 0 || target >= next.length) return prev;
[next[index], next[target]] = [next[target], next[index]];
return next;
});
}
function removeRow(index: number) {
setAutoLoadDraft((prev) => prev.filter((_, i) => i !== index));
}
async function handleSaveAutoLoad() {
await handleSave("auto_load_models", JSON.stringify(draftRows.map((m) => m.id)));
}
function modelDisplayName(m: ModelInfo) {
return m.file_name.replace(/\.gguf$/i, "");
}
async function handleUpload(file: File) {
setUploadMsg(null);
try {
@@ -105,6 +170,118 @@ export default function SettingsPage() {
</div>
</div>
<div className="rounded-xl border border-border bg-panel p-5">
<div className="text-xs uppercase text-slate-400"></div>
{startupMsg ? (
<div className="mt-2 text-xs text-slate-400">{startupMsg}</div>
) : null}
<div className="mt-3 flex items-center justify-between rounded-lg border border-border bg-panel-2 px-3 py-2.5">
<div>
<div className="text-sm"></div>
<div className="mt-0.5 text-xs text-slate-400">
Windows
</div>
</div>
<Toggle
checked={autostart === true}
disabled={autostart === null}
onChange={handleAutostartToggle}
/>
</div>
<div className="mt-4">
<div className="text-sm"></div>
<div className="mt-0.5 text-xs text-slate-400">
</div>
<div className="mt-3 space-y-2">
{draftRows.length === 0 ? (
<div className="rounded-lg border border-dashed border-border bg-panel-2/50 px-3 py-5 text-center text-xs text-slate-500">
</div>
) : (
draftRows.map((m, i) => (
<div
key={m.id}
className="flex items-center gap-2 rounded-lg border border-border bg-panel-2 px-3 py-2"
>
<span className="text-xs text-slate-500">{i + 1}</span>
<span className="min-w-0 flex-1 truncate text-sm">
{modelDisplayName(m)}
</span>
<button
className="rounded border border-border px-1.5 py-0.5 text-xs text-slate-400 hover:text-slate-200 disabled:opacity-40"
onClick={() => moveRow(i, -1)}
disabled={i === 0}
title="上移"
>
</button>
<button
className="rounded border border-border px-1.5 py-0.5 text-xs text-slate-400 hover:text-slate-200 disabled:opacity-40"
onClick={() => moveRow(i, 1)}
disabled={i === draftRows.length - 1}
title="下移"
>
</button>
<button
className="rounded border border-border px-1.5 py-0.5 text-xs text-slate-400 hover:text-red-400"
onClick={() => removeRow(i)}
title="移除"
>
×
</button>
</div>
))
)}
</div>
<div className="mt-3 flex items-center gap-2">
<select
className="input flex-1"
value={pickId}
onChange={(e) => setPickId(e.target.value)}
>
<option value="">
{available.length > 0
? "选择要添加的本地模型…"
: "没有可添加的本地模型"}
</option>
{available.map((m) => (
<option key={m.id} value={m.id}>
{modelDisplayName(m)}
</option>
))}
</select>
<button
className="btn-secondary shrink-0"
disabled={!pickId}
onClick={() => {
setAutoLoadDraft((prev) => [...prev, pickId]);
setPickId("");
}}
>
</button>
</div>
<div className="mt-3 flex items-center justify-between gap-2">
<span className="text-[11px] text-slate-500">
</span>
<button
className="btn-primary px-3 py-1.5 text-xs"
onClick={handleSaveAutoLoad}
disabled={
JSON.stringify(draftRows.map((m) => m.id)) ===
JSON.stringify(autoLoadDraft)
}
>
</button>
</div>
</div>
</div>
<div className="rounded-xl border border-border bg-panel p-5">
<div className="text-xs uppercase text-slate-400"></div>
<div className="mt-3 flex items-center justify-between rounded-lg border border-border bg-panel-2 px-3 py-2.5">
@@ -242,19 +419,22 @@ export default function SettingsPage() {
function Toggle({
checked,
onChange,
disabled = false,
}: {
checked: boolean;
onChange: (checked: boolean) => void;
disabled?: boolean;
}) {
return (
<button
type="button"
role="switch"
aria-checked={checked}
disabled={disabled}
onClick={() => onChange(!checked)}
className={`relative h-6 w-11 shrink-0 rounded-full transition-colors ${
checked ? "bg-accent" : "bg-panel-2"
}`}
} ${disabled ? "cursor-not-allowed opacity-50" : ""}`}
>
<span
className={`absolute top-0.5 h-5 w-5 rounded-full bg-white shadow transition-all ${