|
| 1 | +import { NumberField } from "@/components/fields/number-field"; |
| 2 | +import { SwitchField } from "@/components/fields/switch-field"; |
| 3 | +import { FormSaveDock } from "@/components/form-save-dock"; |
| 4 | +import { useApiMutation } from "@/hooks/use-api-mutation"; |
| 5 | +import { fetchAgentControl, updateAgentControl } from "@/lib/graphql/agent-control"; |
| 6 | +import { agentControlSchema, type AgentControlFormValues } from "@/types/agent-control"; |
| 7 | +import { Alert, AlertDescription, AlertTitle } from "@trenova/shared/components/ui/alert"; |
| 8 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@trenova/shared/components/ui/card"; |
| 9 | +import { Form, FormControl, FormGroup } from "@trenova/shared/components/ui/form"; |
| 10 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 11 | +import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query"; |
| 12 | +import { useCallback } from "react"; |
| 13 | +import { FormProvider, type Resolver, useForm, useFormContext, useWatch } from "react-hook-form"; |
| 14 | +import { toast } from "sonner"; |
| 15 | + |
| 16 | +const AGENT_CONTROL_QUERY_KEY = ["agent-control"]; |
| 17 | + |
| 18 | +export default function AgentControlForm() { |
| 19 | + const queryClient = useQueryClient(); |
| 20 | + const { data } = useSuspenseQuery({ |
| 21 | + queryKey: AGENT_CONTROL_QUERY_KEY, |
| 22 | + queryFn: fetchAgentControl, |
| 23 | + }); |
| 24 | + |
| 25 | + const defaultValues: AgentControlFormValues = { |
| 26 | + billingAgentEnabled: data.billingAgentEnabled, |
| 27 | + shadowMode: data.shadowMode, |
| 28 | + decisionTimeoutSeconds: data.decisionTimeoutSeconds, |
| 29 | + }; |
| 30 | + |
| 31 | + const form = useForm<AgentControlFormValues>({ |
| 32 | + resolver: zodResolver(agentControlSchema) as Resolver<AgentControlFormValues>, |
| 33 | + defaultValues, |
| 34 | + values: defaultValues, |
| 35 | + }); |
| 36 | + const { handleSubmit, setError, reset } = form; |
| 37 | + |
| 38 | + const mutation = useApiMutation({ |
| 39 | + mutationFn: (values: AgentControlFormValues) => updateAgentControl(values), |
| 40 | + onSuccess: (_, values) => { |
| 41 | + toast.success("Agent control updated"); |
| 42 | + reset(values); |
| 43 | + void queryClient.invalidateQueries({ queryKey: AGENT_CONTROL_QUERY_KEY }); |
| 44 | + }, |
| 45 | + setFormError: setError, |
| 46 | + resourceName: "Agent Control", |
| 47 | + }); |
| 48 | + |
| 49 | + const onSubmit = useCallback( |
| 50 | + (values: AgentControlFormValues) => mutation.mutate(values), |
| 51 | + [mutation], |
| 52 | + ); |
| 53 | + |
| 54 | + return ( |
| 55 | + <FormProvider {...form}> |
| 56 | + <Form onSubmit={handleSubmit(onSubmit)}> |
| 57 | + <div className="flex flex-col gap-4 pb-14"> |
| 58 | + <BillingAgentCard /> |
| 59 | + <FormSaveDock saveButtonContent="Save Changes" /> |
| 60 | + </div> |
| 61 | + </Form> |
| 62 | + </FormProvider> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function BillingAgentCard() { |
| 67 | + const { control } = useFormContext<AgentControlFormValues>(); |
| 68 | + const shadowMode = useWatch({ control, name: "shadowMode" }); |
| 69 | + const billingAgentEnabled = useWatch({ control, name: "billingAgentEnabled" }); |
| 70 | + |
| 71 | + return ( |
| 72 | + <Card> |
| 73 | + <CardHeader> |
| 74 | + <CardTitle>Billing Exception Agent</CardTitle> |
| 75 | + <CardDescription> |
| 76 | + The billing exception agent inspects blocked billing queue items, diagnoses why they are |
| 77 | + held, and proposes resolutions for a human to approve. It never approves or transitions an |
| 78 | + item itself. |
| 79 | + </CardDescription> |
| 80 | + </CardHeader> |
| 81 | + <CardContent className="max-w-prose"> |
| 82 | + <FormGroup cols={1}> |
| 83 | + <FormControl> |
| 84 | + <SwitchField |
| 85 | + control={control} |
| 86 | + name="billingAgentEnabled" |
| 87 | + label="Enable Billing Exception Agent" |
| 88 | + description="Allow the agent to run against this organization's blocked billing queue items." |
| 89 | + position="left" |
| 90 | + /> |
| 91 | + </FormControl> |
| 92 | + <FormControl> |
| 93 | + <SwitchField |
| 94 | + control={control} |
| 95 | + name="shadowMode" |
| 96 | + label="Shadow Mode" |
| 97 | + description="While on, the agent runs and stores its proposals for observation but they are never surfaced or actionable. Turn off only once you trust the agent's suggestions." |
| 98 | + position="left" |
| 99 | + /> |
| 100 | + </FormControl> |
| 101 | + {shadowMode && billingAgentEnabled ? ( |
| 102 | + <Alert variant="warning"> |
| 103 | + <AlertTitle>Proposals are hidden</AlertTitle> |
| 104 | + <AlertDescription> |
| 105 | + Shadow mode is on, so runs complete and persist proposals but nothing appears for |
| 106 | + review and no decisions are awaited. Turn shadow mode off to surface proposals and |
| 107 | + enable human decisions. |
| 108 | + </AlertDescription> |
| 109 | + </Alert> |
| 110 | + ) : null} |
| 111 | + <FormControl className="max-w-[420px]"> |
| 112 | + <NumberField |
| 113 | + control={control} |
| 114 | + name="decisionTimeoutSeconds" |
| 115 | + label="Decision Timeout (seconds)" |
| 116 | + description="How long a proposal waits for a human decision before its proposals expire and the run is parked. Defaults to 86400 (24 hours)." |
| 117 | + rules={{ required: true }} |
| 118 | + /> |
| 119 | + </FormControl> |
| 120 | + </FormGroup> |
| 121 | + </CardContent> |
| 122 | + </Card> |
| 123 | + ); |
| 124 | +} |
0 commit comments