"use client"; import { ArrowUpIcon, BotIcon, InboxIcon, Loader2Icon, UserIcon } from "lucide-react"; import Link from "next/link"; import { useEffect, useRef, useState, type FormEvent } from "react"; import { ActionResult } from "@/components/chat/gen-ui"; import { RunStatusBadge } from "@/components/runs/status-badge"; import { PageHeader } from "@/components/shell/page"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { api, ApiError } from "@/lib/api"; import { useI18n } from "@/lib/i18n"; import type { ChatResult } from "@/lib/types"; import { TERMINAL, useRunStream } from "@/lib/use-run-stream"; interface Turn { id: string; text: string; result?: ChatResult; error?: string; } /** Follows a run that paused for approval, so the answer updates when someone approves it. */ function LiveOutcome({ result }: { result: ChatResult }) { const { t } = useI18n(); const { timeline } = useRunStream(result.run_id); const status = timeline?.run.status ?? result.status; return (
{status === "waiting_approval" ? t("chat.needsApproval") : null} {status === "waiting_approval" && ( )}
{TERMINAL.has(status) && timeline?.run.output &&

{timeline.run.output}

}
); } function Answer({ r }: { r: ChatResult }) { const { t } = useI18n(); const shown = r.actions.filter((a) => a.data || a.status !== "executed"); return (
{r.output &&

{r.output}

} {r.error &&

{r.error}

} {shown.map((a, i) => ( ))} {r.status === "waiting_approval" && }
{t("inbox.viewRun")}
); } export function ChatView() { const { t, m } = useI18n(); const [turns, setTurns] = useState([]); const [text, setText] = useState(""); const [busy, setBusy] = useState(false); const endRef = useRef(null); useEffect(() => { endRef.current?.scrollIntoView({ behavior: "smooth", block: "end" }); }, [turns, busy]); const send = async (message: string) => { const msg = message.trim(); if (!msg || busy) return; const id = crypto.randomUUID(); setTurns((ts) => [...ts, { id, text: msg }]); setText(""); setBusy(true); try { const result = await api("/chat", { method: "POST", body: { message: msg } }); setTurns((ts) => ts.map((x) => (x.id === id ? { ...x, result } : x))); } catch (e) { const error = e instanceof ApiError ? e.detail : t("errors.network"); setTurns((ts) => ts.map((x) => (x.id === id ? { ...x, error } : x))); } finally { setBusy(false); } }; const submit = (e: FormEvent) => { e.preventDefault(); void send(text); }; return (
{turns.length === 0 && (

{t("chat.empty")}

{m.chat.suggestions.map((s) => ( ))}
)} {turns.map((turn) => (

{turn.text}

{turn.result ? ( ) : turn.error ? (

{turn.error}

) : (

{t("chat.thinking")}

)}
))}