|
| 1 | +/** |
| 2 | + * GoalAiSection — 设置页「目标 AI」段(v0.18.2 REQ-254)。 |
| 3 | + * |
| 4 | + * @ai-context: 目标规划=内容上传类调用——独立开关默认关(content_gate 之外 |
| 5 | + * 第二闸门)+ 预算档位(4K/10K/30K,默认标准);关闭时 AI 规划 |
| 6 | + * 不可用但**规则草案完全可用**(本地优先降级链)。 |
| 7 | + * @ai-context: read-modify-write 最小面(ai_set_goal_plan——不覆盖其他设置)。 |
| 8 | + */ |
| 9 | +import { useEffect, useState } from "react"; |
| 10 | +import { invoke } from "@tauri-apps/api/core"; |
| 11 | +import type { AiSettingsView } from "../types/ai"; |
| 12 | + |
| 13 | +const TIERS = [ |
| 14 | + { id: "light", label: "轻量(~4K,快而省)" }, |
| 15 | + { id: "standard", label: "标准(~10K,推荐)" }, |
| 16 | + { id: "deep", label: "深度(~30K,计划更细)" }, |
| 17 | +]; |
| 18 | + |
| 19 | +export default function GoalAiSection() { |
| 20 | + const [enabled, setEnabled] = useState(false); |
| 21 | + const [tier, setTier] = useState("standard"); |
| 22 | + const [msg, setMsg] = useState(""); |
| 23 | + const [busy, setBusy] = useState(false); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + invoke<AiSettingsView>("ai_get_settings") |
| 27 | + .then((s) => { setEnabled(s.goalPlanEnabled); setTier(s.goalPlanTier || "standard"); }) |
| 28 | + .catch((e) => setMsg(`目标 AI 设置读取失败: ${e}`)); |
| 29 | + }, []); |
| 30 | + |
| 31 | + const save = async (nextEnabled: boolean, nextTier: string) => { |
| 32 | + setBusy(true); |
| 33 | + setMsg(""); |
| 34 | + try { |
| 35 | + await invoke("ai_set_goal_plan", { enabled: nextEnabled, tier: nextTier || null }); |
| 36 | + setEnabled(nextEnabled); |
| 37 | + setTier(nextTier); |
| 38 | + setMsg(nextEnabled ? "已开启——访谈/详情页出现「✨ 用 AI 规划」,草案确认后落库" : "已关闭——按规则草案规划(本地优先,零影响)"); |
| 39 | + } catch (e) { |
| 40 | + setMsg(`保存失败: ${e}`); |
| 41 | + } finally { |
| 42 | + setBusy(false); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <div style={{ marginTop: 8, padding: 8, background: "#fff", borderRadius: 6, border: "1px solid #e5e7eb" }}> |
| 48 | + <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 4 }}> |
| 49 | + <span style={{ fontWeight: 600, fontSize: 12, color: "#374151" }}>🎯 目标 AI(规划师)</span> |
| 50 | + <label style={{ fontSize: 12, display: "flex", alignItems: "center", gap: 4, cursor: "pointer" }}> |
| 51 | + <input |
| 52 | + data-testid="goal-ai-toggle" |
| 53 | + type="checkbox" |
| 54 | + checked={enabled} |
| 55 | + disabled={busy} |
| 56 | + onChange={(e) => void save(e.target.checked, tier)} |
| 57 | + /> |
| 58 | + 开启(默认关) |
| 59 | + </label> |
| 60 | + <span style={{ marginLeft: "auto", fontSize: 11, color: "#9ca3af" }}> |
| 61 | + {tier === "standard" ? "标准档 ~10K" : TIERS.find((t) => t.id === tier)?.label} |
| 62 | + </span> |
| 63 | + </div> |
| 64 | + <div style={{ display: "flex", gap: 8, alignItems: "center", flexWrap: "wrap" }}> |
| 65 | + <select |
| 66 | + data-testid="goal-ai-tier" |
| 67 | + value={tier} |
| 68 | + disabled={busy || !enabled} |
| 69 | + onChange={(e) => void save(enabled, e.target.value)} |
| 70 | + style={{ fontSize: 11, padding: "2px 6px", border: "1px solid #e5e7eb", borderRadius: 4 }} |
| 71 | + > |
| 72 | + {TIERS.map((t) => <option key={t.id} value={t.id}>{t.label}</option>)} |
| 73 | + </select> |
| 74 | + <span style={{ fontSize: 11, color: "#9ca3af" }}>预算硬顶·成本/轨迹可查·失败自动回退规则草案</span> |
| 75 | + </div> |
| 76 | + {msg && <p style={{ fontSize: 11, color: "#6b7280", margin: "4px 0 0" }}>{msg}</p>} |
| 77 | + </div> |
| 78 | + ); |
| 79 | +} |
0 commit comments