Spaces:
Running
Running
Download web/src/components/chat/gen-ui.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 8 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/chat/gen-ui.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/chat/gen-ui.tsx
-
curl -L -o gen-ui.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/chat/gen-ui.tsx
8 kB
| "use client"; | |
| import { GaugeIcon, PackageIcon, TruckIcon } from "lucide-react"; | |
| import { RiskBadge } from "@/components/commerce/risk-badge"; | |
| import { RunStatusBadge } from "@/components/runs/status-badge"; | |
| import { formatMoney, formatValue, humanize, toolLabel } from "@/lib/format"; | |
| import { useI18n } from "@/lib/i18n"; | |
| import type { RiskBand, RunAction } from "@/lib/types"; | |
| type Rec = Record<string, unknown>; | |
| const isRec = (v: unknown): v is Rec => typeof v === "object" && v !== null && !Array.isArray(v); | |
| export type GenKind = "order_table" | "risk_card" | "status_card" | "generic"; | |
| /** Pick a component for one tool result, from the tool name first and the data shape second. */ | |
| export function pickComponent(action: Pick<RunAction, "tool" | "data">): GenKind { | |
| const tool = action.tool.toLowerCase(); | |
| const d = action.data; | |
| if (!d) return "generic"; | |
| const list = Array.isArray(d.items) ? d.items : Array.isArray(d.orders) ? d.orders : null; | |
| if (/(list|search|find)_orders|orders$/.test(tool) || (list && list.every(isRec))) { | |
| if (list) return "order_table"; | |
| } | |
| if (/risk|score/.test(tool) || ("band" in d && "score" in d) || isRec(d.risk)) return "risk_card"; | |
| if (("number" in d || "order_number" in d) && "status" in d) return "status_card"; | |
| return "generic"; | |
| } | |
| export function OrderTable({ rows }: { rows: Rec[] }) { | |
| const { t, locale } = useI18n(); | |
| return ( | |
| <div className="overflow-x-auto rounded-md border" data-testid="gen-order-table"> | |
| <table className="w-full text-sm"> | |
| <thead className="bg-muted/60 text-xs text-muted-foreground"> | |
| <tr> | |
| <th className="px-3 py-1.5 text-start font-medium">{t("orders.number")}</th> | |
| <th className="px-3 py-1.5 text-start font-medium">{t("orders.customer")}</th> | |
| <th className="px-3 py-1.5 text-end font-medium">{t("orders.total")}</th> | |
| <th className="px-3 py-1.5 text-start font-medium">{t("orders.status")}</th> | |
| <th className="px-3 py-1.5 text-start font-medium">{t("orders.risk")}</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| {rows.slice(0, 20).map((r, i) => { | |
| const risk = isRec(r.risk) ? r.risk : null; | |
| return ( | |
| <tr key={String(r.number ?? i)} className="border-t"> | |
| <td className="px-3 py-1.5 font-mono text-xs" dir="ltr"> | |
| {String(r.number ?? r.order_number ?? "—")} | |
| </td> | |
| <td className="px-3 py-1.5">{String(r.customer_name ?? r.customer ?? "—")}</td> | |
| <td className="tabular px-3 py-1.5 text-end" dir="ltr"> | |
| {typeof r.total === "number" ? formatMoney(r.total, locale) : "—"} | |
| </td> | |
| <td className="px-3 py-1.5">{String(r.status ?? "—")}</td> | |
| <td className="px-3 py-1.5"> | |
| {risk?.band ? <RiskBand band={risk.band as RiskBand} /> : "—"} | |
| </td> | |
| </tr> | |
| ); | |
| })} | |
| </tbody> | |
| </table> | |
| </div> | |
| ); | |
| } | |
| function RiskBand({ band }: { band: RiskBand }) { | |
| return <RiskBadge band={band} />; | |
| } | |
| export function RiskCard({ data }: { data: Rec }) { | |
| const { t } = useI18n(); | |
| const r = isRec(data.risk) ? data.risk : data; | |
| const score = typeof r.score === "number" ? r.score : null; | |
| const band = (r.band as RiskBand | undefined) ?? "medium"; | |
| const reasons = Array.isArray(r.reasons) ? r.reasons.map(String) : []; | |
| return ( | |
| <div className="space-y-3 rounded-md border p-3" data-testid="gen-risk-card"> | |
| <div className="flex items-center gap-2"> | |
| <GaugeIcon className="size-4 text-muted-foreground" aria-hidden /> | |
| <span className="font-medium">{t("orders.risk")}</span> | |
| {typeof data.number === "string" && ( | |
| <code className="font-mono text-xs text-muted-foreground" dir="ltr"> | |
| {data.number} | |
| </code> | |
| )} | |
| <span className="ms-auto"> | |
| <RiskBadge band={band} /> | |
| </span> | |
| </div> | |
| {score !== null && ( | |
| <div className="space-y-1"> | |
| <div className="tabular text-2xl font-semibold" dir="ltr"> | |
| {score <= 1 ? Math.round(score * 100) : Math.round(score)} | |
| <span className="text-sm font-normal text-muted-foreground"> / 100</span> | |
| </div> | |
| <div className="h-1.5 rounded-full bg-muted"> | |
| <div | |
| className="h-full rounded-full bg-primary" | |
| style={{ width: `${Math.min(100, score <= 1 ? score * 100 : score)}%` }} | |
| /> | |
| </div> | |
| </div> | |
| )} | |
| {reasons.length > 0 && ( | |
| <ul className="list-disc space-y-0.5 ps-5 text-sm text-muted-foreground"> | |
| {reasons.map((x) => ( | |
| <li key={x}>{x}</li> | |
| ))} | |
| </ul> | |
| )} | |
| </div> | |
| ); | |
| } | |
| export function StatusCard({ data }: { data: Rec }) { | |
| const { locale } = useI18n(); | |
| const number = String(data.number ?? data.order_number ?? ""); | |
| const skip = new Set(["number", "order_number", "status", "customer_name"]); | |
| return ( | |
| <div className="space-y-2 rounded-md border p-3" data-testid="gen-status-card"> | |
| <div className="flex items-center gap-2"> | |
| <PackageIcon className="size-4 text-muted-foreground" aria-hidden /> | |
| <code className="font-mono text-sm font-medium" dir="ltr"> | |
| {number} | |
| </code> | |
| {typeof data.customer_name === "string" && ( | |
| <span className="text-sm text-muted-foreground">{data.customer_name}</span> | |
| )} | |
| <span className="ms-auto"> | |
| <RunStatusBadge status={String(data.status)} /> | |
| </span> | |
| </div> | |
| <dl className="grid grid-cols-2 gap-x-4 gap-y-1 text-sm sm:grid-cols-3"> | |
| {Object.entries(data) | |
| .filter(([k, v]) => !skip.has(k) && !isRec(v) && !Array.isArray(v)) | |
| .map(([k, v]) => ( | |
| <div key={k}> | |
| <dt className="text-xs text-muted-foreground">{humanize(k)}</dt> | |
| <dd className="tabular" dir="ltr"> | |
| {formatValue(k, v, locale)} | |
| </dd> | |
| </div> | |
| ))} | |
| </dl> | |
| {isRec(data.courier) && ( | |
| <p className="flex items-center gap-1.5 text-xs text-muted-foreground"> | |
| <TruckIcon className="size-3.5" aria-hidden /> | |
| {String(data.courier.name ?? "")} · {String(data.courier.status ?? "")} | |
| </p> | |
| )} | |
| </div> | |
| ); | |
| } | |
| export function GenericCard({ action }: { action: RunAction }) { | |
| const { locale } = useI18n(); | |
| return ( | |
| <div className="space-y-1 rounded-md border p-3 text-sm" data-testid="gen-generic"> | |
| <div className="flex items-center gap-2"> | |
| <span className="font-medium">{toolLabel(action.tool)}</span> | |
| <RunStatusBadge status={action.status} /> | |
| </div> | |
| {action.reason && <p className="text-muted-foreground">{action.reason}</p>} | |
| {action.data && ( | |
| <dl className="grid grid-cols-2 gap-x-4 gap-y-0.5"> | |
| {Object.entries(action.data).map(([k, v]) => ( | |
| <div key={k} className="contents"> | |
| <dt className="text-muted-foreground">{humanize(k)}</dt> | |
| <dd className="tabular break-all" dir="ltr"> | |
| {formatValue(k, v, locale)} | |
| </dd> | |
| </div> | |
| ))} | |
| </dl> | |
| )} | |
| </div> | |
| ); | |
| } | |
| export function ActionResult({ action }: { action: RunAction }) { | |
| const kind = pickComponent(action); | |
| const d = action.data ?? {}; | |
| if (kind === "order_table") { | |
| const rows = (Array.isArray(d.items) ? d.items : d.orders) as Rec[]; | |
| return <OrderTable rows={rows} />; | |
| } | |
| if (kind === "risk_card") return <RiskCard data={d} />; | |
| if (kind === "status_card") return <StatusCard data={d} />; | |
| return <GenericCard action={action} />; | |
| } | |