/** * Create or edit an automation: what the agent does, when it runs, and an * optional name. Where its runs go is the host's choice (`destination`), and * the runs inherit their access from there; nothing here narrows it. * Presentational: the host saves through `onSubmit`. */ import { useMemo, useState, type FormEvent } from "react"; import { CircleAlertIcon } from "lucide-react"; import type { EventSource, EventTypeDescriptor } from "@opengeni/sdk"; import { useOpenGeniText } from "../../messages/runtime"; import { apiErrorFacts } from "../../ui/api-error"; import { Button } from "../../ui/button"; import { Field, TextArea, TextInput } from "../../ui/field"; import { cn } from "../../lib/cn"; import { deriveAutomationName, hasAutomationDraftIssues, validateAutomationDraft, type AutomationDraft, type AutomationDraftIssues, } from "./automation-model"; import { AutomationWhenField } from "./automation-when-field"; import { withTemplateSources } from "./automation-template-model"; import type { EventSourceChoice } from "./event-source-model"; import type { EventSourceLogoResolver } from "./event-source-tile"; export interface AutomationFormProps { /** Where it starts: a saved automation's draft, or `newAutomationDraft()`. */ initial: AutomationDraft; /** It edits a saved automation: the button says Save changes. */ editing?: boolean | undefined; /** Where its runs go: on in one chat, or a new chat each run. */ destination: "chat" | "new_chat"; /** Event triggers run here. False offers the schedule only. */ eventTriggers: boolean; /** Sources the person can point triggers at; undefined while loading. */ sources: EventSource[] | undefined; loadEventTypes: (sourceId: string) => Promise; sourceLogo?: EventSourceLogoResolver | undefined; /** Opens the host's "Add a source" page; offered when no source fits. */ onAddSource?: ((choice?: EventSourceChoice) => void) | undefined; /** Arguments a new MCP subscription starts with, such as `{ projectId }`. */ argumentDefaults?: Readonly> | undefined; /** Saves the draft. Throw to keep the form open; the error's message is shown. */ onSubmit: (draft: AutomationDraft) => Promise; onCancel: () => void; /** The clock for schedule previews. */ now?: Date | undefined; className?: string | undefined; } export function AutomationForm({ initial, editing = false, destination, eventTriggers, sources, loadEventTypes, sourceLogo, onAddSource, argumentDefaults, onSubmit, onCancel, now, className, }: AutomationFormProps) { const t = useOpenGeniText(); const [draft, setDraft] = useState(initial); // A template's triggers take the person's own sources once those load. const [filledFrom, setFilledFrom] = useState(sources); if (sources !== filledFrom) { setFilledFrom(sources); const events = withTemplateSources(draft.events, sources); if (events !== draft.events) setDraft({ ...draft, events }); } const [issues, setIssues] = useState({}); const [saving, setSaving] = useState(false); const [failure, setFailure] = useState(null); const derivedName = useMemo(() => deriveAutomationName(draft.instructions), [draft.instructions]); const update = (patch: Partial) => { setDraft((current) => ({ ...current, ...patch })); setIssues((current) => ({ ...current, ...("instructions" in patch ? { instructions: undefined } : {}), ...("name" in patch ? { name: undefined } : {}), ...("cadence" in patch ? { cadence: undefined, when: undefined } : {}), ...("events" in patch ? { events: undefined, when: undefined } : {}), })); }; const submit = async (event: FormEvent) => { event.preventDefault(); if (saving) return; const found = validateAutomationDraft(t, draft, sources ?? [], now ?? new Date()); setIssues(found); if (hasAutomationDraftIssues(found)) return; setSaving(true); setFailure(null); try { await onSubmit(draft); } catch (error) { setFailure(errorSentence(error)); } finally { setSaving(false); } }; return (
void submit(event)} aria-busy={saving || undefined} className={cn("flex min-w-0 flex-col gap-5", className)} >