From bdc762854f3f72cdc9dad5987e4d6ff4e92e5757 Mon Sep 17 00:00:00 2001 From: sisantaChhatoi Date: Thu, 3 Dec 2026 14:38:02 +0530 Subject: [PATCH] refactor(web): useReducer for the system LLM, tools and agents pages Converts the LLM page and profile sheet, the tools page, tool editor and custom tool dialog, and the create-agent dialog to typed reducers in _state/, on the shared request slice. Part of #546. Co-Authored-By: Claude Opus 5.5 --- .../_state/create-agent-dialog.reducer.ts | 53 ++++ web/src/app/(main)/system/agents/page.tsx | 28 +- .../system/llm/_state/llm-page.reducer.ts | 51 ++++ .../llm/_state/profile-sheet.reducer.test.mjs | 81 ++++++ .../llm/_state/profile-sheet.reducer.ts | 124 ++++++++ web/src/app/(main)/system/llm/page.tsx | 275 +++++++++--------- .../custom-tool-dialog.reducer.test.mjs | 59 ++++ .../_state/custom-tool-dialog.reducer.ts | 148 ++++++++++ .../tools/_state/tool-editor.reducer.ts | 69 +++++ .../tools/_state/tools-page.reducer.test.mjs | 15 + .../system/tools/_state/tools-page.reducer.ts | 67 +++++ web/src/app/(main)/system/tools/page.tsx | 220 +++++--------- 12 files changed, 885 insertions(+), 305 deletions(-) create mode 100644 web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts create mode 100644 web/src/app/(main)/system/llm/_state/llm-page.reducer.ts create mode 100644 web/src/app/(main)/system/llm/_state/profile-sheet.reducer.test.mjs create mode 100644 web/src/app/(main)/system/llm/_state/profile-sheet.reducer.ts create mode 100644 web/src/app/(main)/system/tools/_state/custom-tool-dialog.reducer.test.mjs create mode 100644 web/src/app/(main)/system/tools/_state/custom-tool-dialog.reducer.ts create mode 100644 web/src/app/(main)/system/tools/_state/tool-editor.reducer.ts create mode 100644 web/src/app/(main)/system/tools/_state/tools-page.reducer.test.mjs create mode 100644 web/src/app/(main)/system/tools/_state/tools-page.reducer.ts diff --git a/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts b/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts new file mode 100644 index 00000000..4d554cc2 --- /dev/null +++ b/web/src/app/(main)/system/agents/_state/create-agent-dialog.reducer.ts @@ -0,0 +1,53 @@ +import { + createRequests, + isRequestAction, + type RequestAction, + type Requests, + requestReducer, +} from "@/lib/reducers/request"; +import { setSlice } from "@/lib/reducers/slice"; + +export type CreateAgentForm = { + key: string; + name: string; + description: string; +}; + +type CreateAgentRequest = "create"; + +export type CreateAgentDialogState = { + open: boolean; + form: CreateAgentForm; + requests: Requests; +}; + +export type CreateAgentDialogAction = + | RequestAction + | { type: "openChanged"; open: boolean } + | { type: "formFieldChanged"; patch: Partial } + | { type: "created" }; + +const EMPTY_FORM: CreateAgentForm = { key: "", name: "", description: "" }; + +export const initialCreateAgentDialogState: CreateAgentDialogState = { + open: false, + form: EMPTY_FORM, + requests: createRequests(["create"]), +}; + +export function createAgentDialogReducer( + state: CreateAgentDialogState, + action: CreateAgentDialogAction, +): CreateAgentDialogState { + if (isRequestAction(action)) { + return setSlice(state, "requests", requestReducer(state.requests, action)); + } + switch (action.type) { + case "openChanged": + return action.open === state.open ? state : { ...state, open: action.open }; + case "formFieldChanged": + return { ...state, form: { ...state.form, ...action.patch } }; + case "created": + return { ...state, open: false, form: EMPTY_FORM }; + } +} diff --git a/web/src/app/(main)/system/agents/page.tsx b/web/src/app/(main)/system/agents/page.tsx index c16e4042..50d93aad 100644 --- a/web/src/app/(main)/system/agents/page.tsx +++ b/web/src/app/(main)/system/agents/page.tsx @@ -37,6 +37,8 @@ import { t } from "@/i18n"; import { api } from "@/lib/api"; import type { Agent } from "@/lib/types"; +import { createAgentDialogReducer, initialCreateAgentDialogState } from "./_state/create-agent-dialog.reducer"; + // AgentGridCard is one clickable tile opening the agent's editor drawer. Custom // (non-builtin) agents get a delete button. function AgentGridCard({ agent, onOpen, onDeleted }: { agent: Agent; onOpen: () => void; onDeleted: () => void }) { @@ -113,26 +115,22 @@ function AgentGridCard({ agent, onOpen, onDeleted }: { agent: Agent; onOpen: () } function CreateAgentDialog({ onCreated }: { onCreated: (key: string) => void }) { - const [open, setOpen] = React.useState(false); - const [key, setKey] = React.useState(""); - const [name, setName] = React.useState(""); - const [description, setDescription] = React.useState(""); - const [busy, setBusy] = React.useState(false); + const [state, dispatch] = React.useReducer(createAgentDialogReducer, initialCreateAgentDialogState); + const { open, form } = state; + const { key, name, description } = form; + const busy = state.requests.create.pending; async function create() { - setBusy(true); + dispatch({ type: "request/started", key: "create" }); try { const a = await api.createAgent(key.trim(), name.trim(), description.trim()); toast.success(t('Agent "{name}" created', { name: a.name })); - setOpen(false); - setKey(""); - setName(""); - setDescription(""); + dispatch({ type: "created" }); onCreated(a.key); } catch (e) { toast.error(t("Create failed: {error}", { error: (e as Error).message })); } finally { - setBusy(false); + dispatch({ type: "request/finished", key: "create" }); } } @@ -140,7 +138,7 @@ function CreateAgentDialog({ onCreated }: { onCreated: (key: string) => void }) const canCreate = keyOk && name.trim().length > 0 && !busy; return ( - + dispatch({ type: "openChanged", open: o })}> @@ -714,7 +708,7 @@ function ProfileSheet({ className="font-mono" placeholder={basePlaceholder} value={baseUrl} - onChange={(e) => setBaseUrl(e.target.value)} + onChange={(e) => setField({ baseUrl: e.target.value })} /> {compatPreset && (

@@ -733,10 +727,7 @@ function ProfileSheet({ className="min-h-20 w-full rounded-md border bg-background px-3 py-2 font-mono text-sm" placeholder="X-Example-Route: demo" value={customHeaders} - onChange={(e) => { - setCustomHeaders(e.target.value); - setClearCustomHeaders(false); - }} + onChange={(e) => dispatch({ type: "customHeadersChanged", customHeaders: e.target.value })} spellCheck={false} />

@@ -748,10 +739,7 @@ function ProfileSheet({ { - setClearCustomHeaders(e.target.checked); - if (e.target.checked) setCustomHeaders(""); - }} + onChange={(e) => dispatch({ type: "clearCustomHeadersChanged", clear: e.target.checked })} /> Clear saved custom headers @@ -766,7 +754,7 @@ function ProfileSheet({ className="font-mono" placeholder="socks5://user:pass@127.0.0.1:1080 · http://127.0.0.1:8080" value={proxy} - onChange={(e) => setProxy(e.target.value)} + onChange={(e) => setField({ proxy: e.target.value })} />

{t( @@ -782,7 +770,7 @@ function ProfileSheet({ className="font-mono" placeholder={t("e.g. x-session-id (empty = not sent)")} value={sessionHeaderKey} - onChange={(e) => setSessionHeaderKey(e.target.value)} + onChange={(e) => setField({ sessionHeaderKey: e.target.value })} />

{rich( @@ -800,7 +788,7 @@ function ProfileSheet({ className="font-mono" placeholder={t("e.g. my-app/1.0 (empty = default UA)")} value={userAgent} - onChange={(e) => setUserAgent(e.target.value)} + onChange={(e) => setField({ userAgent: e.target.value })} />

{rich( @@ -818,7 +806,7 @@ function ProfileSheet({ type="password" placeholder={keyPlaceholder} value={apiKey} - onChange={(e) => setApiKey(e.target.value)} + onChange={(e) => setField({ apiKey: e.target.value })} // A masked field with no autocomplete hint looks like this site's // login password: password managers fill it and Save persists the // wrong secret over the stored key. new-password keeps them out. @@ -832,11 +820,11 @@ function ProfileSheet({

- setRps(e.target.value)} /> + setField({ rps: e.target.value })} />
- setRpm(e.target.value)} /> + setField({ rpm: e.target.value })} />
@@ -846,7 +834,7 @@ function ProfileSheet({ min={0} max={1000} value={cw} - onChange={(e) => setCw(e.target.value)} + onChange={(e) => setField({ cw: e.target.value })} placeholder="200" />
@@ -874,7 +862,7 @@ function ProfileSheet({ min={0} step="any" value={prices[key]} - onChange={(e) => setPrices((p) => ({ ...p, [key]: e.target.value }))} + onChange={(e) => setField({ prices: { ...prices, [key]: e.target.value } })} />
))} @@ -900,7 +888,7 @@ function ProfileSheet({ type="number" className="w-24 shrink-0" value={priority} - onChange={(e) => setPriority(e.target.value)} + onChange={(e) => setField({ priority: e.target.value })} />
@@ -912,7 +900,11 @@ function ProfileSheet({ )}

- + setField({ poolExclude: v })} + aria-label={t("Excluded from pool")} + />
@@ -923,7 +915,11 @@ function ProfileSheet({ )}

- + setField({ streaming: v })} + aria-label={t("Streaming output")} + />
@@ -945,7 +941,7 @@ function ProfileSheet({ min={0} className="w-28 shrink-0" value={maxTokens} - onChange={(e) => setMaxTokens(e.target.value)} + onChange={(e) => setField({ maxTokens: e.target.value })} placeholder="0" /> @@ -956,7 +952,7 @@ function ProfileSheet({ + +