Spaces:
Running
Running
Download web/src/components/chat/chat-view.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 6.73 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/chat/chat-view.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/chat/chat-view.tsx
-
curl -L -o chat-view.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/chat/chat-view.tsx
6.73 kB
| "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 ( | |
| <div className="space-y-2"> | |
| <div className="flex flex-wrap items-center gap-2 rounded-md border border-warning/40 bg-warning-soft/50 px-3 py-2 text-sm"> | |
| <InboxIcon className="size-4 text-warning" aria-hidden /> | |
| <span>{status === "waiting_approval" ? t("chat.needsApproval") : null}</span> | |
| <RunStatusBadge status={status} /> | |
| {status === "waiting_approval" && ( | |
| <Button size="xs" variant="outline" asChild className="ms-auto"> | |
| <Link href="/inbox">{t("chat.openInbox")}</Link> | |
| </Button> | |
| )} | |
| </div> | |
| {TERMINAL.has(status) && timeline?.run.output && <p>{timeline.run.output}</p>} | |
| </div> | |
| ); | |
| } | |
| function Answer({ r }: { r: ChatResult }) { | |
| const { t } = useI18n(); | |
| const shown = r.actions.filter((a) => a.data || a.status !== "executed"); | |
| return ( | |
| <div className="space-y-3" data-testid="chat-answer"> | |
| {r.output && <p className="whitespace-pre-wrap">{r.output}</p>} | |
| {r.error && <p className="text-destructive">{r.error}</p>} | |
| {shown.map((a, i) => ( | |
| <ActionResult key={`${a.step}-${i}`} action={a} /> | |
| ))} | |
| {r.status === "waiting_approval" && <LiveOutcome result={r} />} | |
| <div className="flex items-center gap-2 text-xs text-muted-foreground"> | |
| <RunStatusBadge status={r.status} /> | |
| <Link href={`/runs/${r.run_id}`} className="underline-offset-4 hover:underline"> | |
| {t("inbox.viewRun")} | |
| </Link> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| export function ChatView() { | |
| const { t, m } = useI18n(); | |
| const [turns, setTurns] = useState<Turn[]>([]); | |
| const [text, setText] = useState(""); | |
| const [busy, setBusy] = useState(false); | |
| const endRef = useRef<HTMLDivElement>(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<ChatResult>("/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 ( | |
| <div className="flex h-[calc(100dvh-8.5rem)] flex-col gap-4"> | |
| <PageHeader title={t("chat.title")} subtitle={t("chat.subtitle")} /> | |
| <div className="flex-1 space-y-5 overflow-y-auto rounded-lg border bg-card p-4" aria-live="polite"> | |
| {turns.length === 0 && ( | |
| <div className="flex h-full flex-col items-center justify-center gap-3 text-center"> | |
| <BotIcon className="size-6 text-muted-foreground" aria-hidden /> | |
| <p className="text-sm text-muted-foreground">{t("chat.empty")}</p> | |
| <div className="flex flex-wrap justify-center gap-2"> | |
| {m.chat.suggestions.map((s) => ( | |
| <Button key={s} variant="outline" size="sm" onClick={() => send(s)}> | |
| {s} | |
| </Button> | |
| ))} | |
| </div> | |
| </div> | |
| )} | |
| {turns.map((turn) => ( | |
| <div key={turn.id} className="space-y-3"> | |
| <div className="flex items-start gap-2.5"> | |
| <span className="grid size-7 shrink-0 place-items-center rounded-full bg-muted" aria-hidden> | |
| <UserIcon className="size-3.5" /> | |
| </span> | |
| <p className="pt-1">{turn.text}</p> | |
| </div> | |
| <div className="flex items-start gap-2.5"> | |
| <span | |
| className="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground" | |
| aria-hidden | |
| > | |
| <BotIcon className="size-3.5" /> | |
| </span> | |
| <div className="min-w-0 flex-1 pt-1"> | |
| {turn.result ? ( | |
| <Answer r={turn.result} /> | |
| ) : turn.error ? ( | |
| <p className="text-destructive" role="alert"> | |
| {turn.error} | |
| </p> | |
| ) : ( | |
| <p className="flex items-center gap-2 text-muted-foreground"> | |
| <Loader2Icon className="size-4 animate-spin" aria-hidden /> | |
| {t("chat.thinking")} | |
| </p> | |
| )} | |
| </div> | |
| </div> | |
| </div> | |
| ))} | |
| <div ref={endRef} /> | |
| </div> | |
| <form onSubmit={submit} className="flex items-end gap-2"> | |
| <Textarea | |
| value={text} | |
| onChange={(e) => setText(e.target.value)} | |
| onKeyDown={(e) => { | |
| if (e.key === "Enter" && !e.shiftKey) { | |
| e.preventDefault(); | |
| void send(text); | |
| } | |
| }} | |
| placeholder={t("chat.placeholder")} | |
| rows={2} | |
| maxLength={4000} | |
| className="min-h-11 resize-none" | |
| aria-label={t("chat.placeholder")} | |
| /> | |
| <Button type="submit" size="icon-lg" disabled={!text.trim() || busy} aria-label={t("chat.send")}> | |
| <ArrowUpIcon aria-hidden /> | |
| </Button> | |
| </form> | |
| </div> | |
| ); | |
| } | |