import { ArrowDownIcon, ArrowUpIcon, ArrowUpRightIcon, CheckIcon, LayoutGridIcon, LoaderCircleIcon, Table2Icon, } from "lucide-react"; import { useId, useMemo, useState, type ReactNode } from "react"; import { bestInteractiveItems, formatInteractiveValue, interactiveActionLabel, interactiveAnswerKey, interactiveFilterOptions, interactiveSelectionMessage, interactiveSelectionWasSent, nextInteractiveSort, parseInteractiveAnswer, settleInteractiveAnswer, toggleInteractiveSelection, useInteractiveAnswerHost, useInteractiveAnswerState, visibleInteractiveItems, type InteractiveAnswerField, type InteractiveAnswerItem, type InteractiveAnswerSpec, type InteractiveAnswerValue, type InteractiveAnswerView, } from "../interactive-answer-model"; import { cn } from "../lib/cn"; import { useOpenGeniText } from "../messages/runtime"; import { DecisionButton } from "./decision-card"; /* * An agent's `opengeni-ui` answer: the options it found as cards or a table, * an optional bar chart, filter chips, and a pick that goes back into the * conversation as the person's next message. Every view shares one selection * and one hover, so an option lights up wherever it appears. */ export type InteractiveAnswerProps = { source: string; /** "streaming" grows the answer in place; "incomplete" shows what arrived. */ state: "complete" | "streaming" | "incomplete"; /** The fence as plain code, shown when the body cannot be drawn. */ renderSource: () => ReactNode; }; export function InteractiveAnswer({ source, state, renderSource }: InteractiveAnswerProps) { const parsed = useMemo( () => settleInteractiveAnswer( parseInteractiveAnswer(source, { partial: state !== "complete" }), state, ), [source, state], ); if (parsed.status === "invalid") return ; if (!parsed.spec || parsed.spec.items.length === 0) { return state === "streaming" ? ( ) : ( ); } return ( ); } function LoadedAnswer({ spec, answerKey, state: streamState, complete, }: { spec: InteractiveAnswerSpec; answerKey: string; state: InteractiveAnswerProps["state"]; complete: boolean; }) { const t = useOpenGeniText(); const host = useInteractiveAnswerHost(); const ids = useMemo( () => (complete ? spec.items.map((item) => item.id) : null), [complete, spec], ); const [state, update] = useInteractiveAnswerState(answerKey, ids); const [hovered, setHovered] = useState(null); const [sending, setSending] = useState(false); const [failed, setFailed] = useState(false); const titleId = useId(); const interactive = complete; const canPick = interactive && Boolean(spec.select) && Boolean(host.send) && !host.readOnly; const max = spec.select?.max ?? 0; const view: InteractiveAnswerView = state.view ?? spec.view; const showTable = view === "table" && spec.fields.length > 0; const filterFields = interactive ? spec.fields.filter((field) => field.filter) : []; const visible = interactive ? visibleInteractiveItems(spec, state) : spec.items; const best = useMemo( () => (complete ? bestInteractiveItems(spec, spec.items) : new Map>()), [complete, spec], ); const words = { yes: t("interactiveAnswer.yes"), no: t("interactiveAnswer.no"), empty: "—" }; const fallbackAction = max === 1 ? t("interactiveAnswer.continueWithThis") : t("interactiveAnswer.continueWithThese"); const sent = interactiveSelectionWasSent(state); const activeFilters = Object.values(state.filters).some((values) => values.length > 0); const toggle = (id: string) => { if (!canPick) return; setFailed(false); update((current) => ({ ...current, selected: toggleInteractiveSelection(current.selected, id, max), })); }; const send = async () => { if (!host.send || state.selected.length === 0 || sending) return; const selection = [...state.selected]; setSending(true); setFailed(false); try { const result = await host.send( interactiveSelectionMessage(spec, selection, fallbackAction, t.locale), ); if (result === false) setFailed(true); else update((current) => ({ ...current, sent: selection })); } catch { setFailed(true); } finally { setSending(false); } }; const itemProps = { spec, selected: state.selected, hovered, onHover: setHovered, onToggle: canPick ? toggle : undefined, atMax: max > 1 && state.selected.length >= max, best, words, single: max === 1, }; return (
{spec.title || (interactive && spec.fields.length > 0) ? ( // Without a title, narrow answers have no header: the switch is hidden there.
{spec.title ? (

{spec.title}

) : ( )} {interactive && spec.fields.length > 0 ? ( update((current) => ({ ...current, view: next }))} /> ) : null}
) : null} {filterFields.length > 0 ? ( update((current) => ({ ...current, filters: { ...current.filters, [key]: values } })) } /> ) : null} {spec.chart ? ( field.key === spec.chart)!} items={visible} /> ) : null} {visible.length === 0 ? (

{t("interactiveAnswer.noMatches")}

) : ( <> {showTable ? ( // Narrow answers (a phone, a side panel) read the same options as cards, // so no column hides off-screen.
update((current) => ({ ...current, sort: nextInteractiveSort(current.sort, key, field), })) } />
) : null}
{visible.map((item) => ( ))}
)} {interactive && (activeFilters || spec.omitted > 0) ? (

{activeFilters ? t("interactiveAnswer.showing", { shown: visible.length, total: spec.items.length }) : t("interactiveAnswer.omitted", { count: spec.omitted })}

) : null} {streamState === "streaming" && !complete ? : null} {streamState === "incomplete" && !complete ? (

{t("interactiveAnswer.stopped")}

) : null} {canPick ? ( update((current) => ({ ...current, selected: [] }))} onSend={() => void send()} /> ) : null}
); } type ItemProps = { spec: InteractiveAnswerSpec; selected: readonly string[]; hovered: string | null; onHover: (id: string | null) => void; onToggle: ((id: string) => void) | undefined; atMax: boolean; best: Map>; words: { yes: string; no: string; empty: string }; single: boolean; }; function ViewSwitch({ view, onChange, }: { view: InteractiveAnswerView; onChange: (view: InteractiveAnswerView) => void; }) { const t = useOpenGeniText(); const options = [ { id: "cards" as const, label: t("interactiveAnswer.cards"), icon: LayoutGridIcon }, { id: "table" as const, label: t("interactiveAnswer.table"), icon: Table2Icon }, ]; return (
{options.map(({ id, label, icon: Icon }) => ( ))}
); } function Filters({ fields, items, filters, words, onChange, }: { fields: InteractiveAnswerField[]; items: InteractiveAnswerItem[]; filters: Record; words: ItemProps["words"]; onChange: (key: string, values: InteractiveAnswerValue[]) => void; }) { return (
{fields.map((field) => { const options = interactiveFilterOptions(field, items); if (options.length === 0) return null; const active = filters[field.key] ?? []; return (
{field.label} {options.map((option) => { const on = active.includes(option); return ( ); })}
); })}
); } function SelectMark({ on, single }: { on: boolean; single: boolean }) { return (