"use client"; import { Settings2Icon } from "lucide-react"; import type { ReactNode } from "react"; import { useCallback, useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; type ClawState = { base_url: string; api_key?: string; cwd?: string; allow_shell?: boolean; allow_write?: boolean; }; const DEFAULT_STATE: ClawState = { base_url: "http://model.mify.ai.srv/v1", api_key: "", }; export function ClawLlmSettings() { const [open, setOpen] = useState(false); const [state, setState] = useState(DEFAULT_STATE); const [status, setStatus] = useState(""); const [isSaving, setIsSaving] = useState(false); const loadState = useCallback(async () => { setStatus(""); const response = await fetch("/api/claw/state"); const payload = await response.json(); if (!response.ok) { throw new Error(payload.error ?? "读取后端配置失败"); } setState((current) => ({ ...DEFAULT_STATE, ...payload, api_key: payload.api_key ?? current.api_key ?? DEFAULT_STATE.api_key, })); }, []); useEffect(() => { if (!open) return; loadState().catch((err: unknown) => { setStatus(err instanceof Error ? err.message : "读取后端配置失败"); }); }, [loadState, open]); async function saveState() { setIsSaving(true); setStatus(""); try { const response = await fetch("/api/claw/state", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ base_url: state.base_url, api_key: state.api_key, }), }); const payload = await response.json(); if (!response.ok) { throw new Error(payload.error ?? payload.detail ?? "保存失败"); } setState({ ...DEFAULT_STATE, ...payload, api_key: state.api_key, }); setStatus("已更新后端 API 配置"); } catch (err) { setStatus(err instanceof Error ? err.message : "保存失败"); } finally { setIsSaving(false); } } return ( 后端 LLM API 调试时更新 ZK Data Agent 后端使用的 Base URL 和 API Key。
setState((current) => ({ ...current, base_url: event.target.value, })) } /> setState((current) => ({ ...current, api_key: event.target.value, })) } />
{status ? (

{status}

) : null}
); } function Field({ label, children }: { label: string; children: ReactNode }) { return (
{label} {children}
); }