/** * Studio's Build chat (RFC-0020 §6, phase 20c): a person describes the page; the site's model * changes the draft step by step while the Build view's preview and activity follow along. A part * picked in the preview (`scope`) aims the next briefs at it until it is cleared. The transcript is * this browser's only: the draft and its snapshot are what last. */ import { Badge, Banner, type BuildChatItem, BuildChatLog, Button, InputArea, LinkButton, SectionCard, SectionEmpty, SkeletonLine, Text, } from "@cloudhal/ui"; import { CloudArrowUp, GearSix, Sparkle, StopCircle, Target, WarningCircle, X, } from "@phosphor-icons/react"; import { useQuery } from "@tanstack/react-query"; import { useRef, useState } from "react"; import { listOf, useT } from "../i18n/index.tsx"; import { api, errorCode } from "../lib/api.ts"; import { usePlayground } from "./usePlayground.ts"; /** * The model Build recommends (RFC-0020 open question 7): it finished every brief of the Build * model eval on Workers AI (2026-10-15). The site keeps its own choice; Build chat suggests this one. */ export const BUILD_RECOMMENDED_MODEL = "@cf/openai/gpt-oss-120b"; /** A part of the page picked in the preview, with the words that name it. */ export interface BuildScope { sectionId: string; componentId?: string | undefined; label: string; } const ERRORS = new Set([ "ai_budget", "ai_busy", "ai_off", "ai_model_unavailable", "ai_rate_limited", "ai_unavailable", "ai_failed", ]); /** `render_page`'s check ids (RFC-0021 §4), named in the chat in the person's language. */ const RENDER_CHECKS = [ "overflow-x", "img-alt", "img-broken", "heading-order", "contrast", "empty-section", "tap-target", "long-line", "input-label", "chart-empty", ] as const; export function BuildChat({ siteId, pageId, scope, onClearScope, }: { siteId: string; pageId: string; scope?: BuildScope | undefined; onClearScope?: () => void; }) { const t = useT("build"); const settings = useQuery({ queryKey: ["ai-settings", siteId], queryFn: () => api.ai.settings(siteId), }); const [items, setItems] = useState([]); const [message, setMessage] = useState(""); const [running, setRunning] = useState(false); const abort = useRef(null); const seq = useRef(0); const nextId = () => `${Date.now()}-${++seq.current}`; const errorText = (code: string | undefined) => t( `build.chat.errors.${code && ERRORS.has(code) ? code : "ai_failed"}` as "build.chat.errors.ai_failed", ); /** Replaces the live progress line (there is at most one, at the end). */ const progress = (text: string | undefined) => setItems((list) => { const rest = list.filter((i) => i.role !== "progress"); return text ? [...rest, { id: "progress", role: "progress", text }] : rest; }); const add = (item: Omit) => setItems((list) => [...list.filter((i) => i.role !== "progress"), { id: nextId(), ...item }]); const send = async () => { const brief = message.trim(); if (!brief || running) return; setMessage(""); setRunning(true); add({ role: "person", text: brief }); progress(t("build.chat.thinking")); const controller = new AbortController(); abort.current = controller; const repairs: string[] = []; let looked = false; let unlinked = false; try { const target = scope ? { sectionId: scope.sectionId, ...(scope.componentId ? { componentId: scope.componentId } : {}), } : undefined; for await (const e of api.ai.build(siteId, pageId, brief, controller.signal, target)) { if (e.type === "say") { add({ role: "model", text: e.text }); progress(t("build.chat.thinking")); } else if (e.type === "writing") progress(t("build.chat.writing", { components: listOf(e.components) })); else if (e.type === "look") { looked = true; add({ role: "result", text: e.failed.length ? t("build.chat.lookedIssues", { checks: listOf( e.failed.map((id) => (RENDER_CHECKS as readonly string[]).includes(id) ? t(`build.chat.checks.${id as (typeof RENDER_CHECKS)[number]}`) : id, ), ), }) : t("build.chat.lookedClean"), }); } else if (e.type === "unlinked") unlinked = true; else if (e.type === "round" && e.round > 1) { progress( t( looked ? "build.chat.improving" : unlinked ? "build.chat.linking" : "build.chat.fixing", ), ); unlinked = false; } else if (e.type === "step" && e.ok) repairs.push(...e.repairs); else if (e.type === "done") add({ role: "result", text: e.steps === 0 ? t("build.chat.nothing") : e.refused > 0 ? t("build.chat.doneRefused", { steps: e.steps, refused: e.refused }) : t("build.chat.done", { steps: e.steps }), ...(repairs.length ? { details: repairs } : {}), }); else if (e.type === "error") add({ role: "error", text: errorText(e.code) }); } } catch (e) { if (controller.signal.aborted) add({ role: "error", text: t("build.chat.stopped") }); else add({ role: "error", text: errorText(errorCode(e)) }); } finally { progress(undefined); setRunning(false); abort.current = null; } }; // RFC-0036 §5: a playground has no AI by design; say so, not that the deployment is broken. const playground = usePlayground(); const ai = settings.data; const reachable = ai !== undefined && ai.transport !== "none"; const on = reachable && ai.settings.enabled; return ( } description={on ? t("build.chat.description") : undefined} > {playground || !on ? ( ) : (
{ai.settings.model !== BUILD_RECOMMENDED_MODEL && ( {t("build.chat.modelHint", { model: BUILD_RECOMMENDED_MODEL })} )} {items.length > 0 && } {settings.error && ( } title={errorText(undefined)} /> )}
{scope && (
}> {t("build.chat.scope", { part: scope.label })}
)}
{running ? ( ) : ( )}
)}
); } /** * Build chat while it cannot run: loading, a playground (RFC-0036 §5 leaves AI out), a deployment * that cannot reach Workers AI, or AI turned off for the site. */ function BuildChatOff({ siteId, deployUrl, reachable, pending, }: { siteId: string; deployUrl: string | undefined; reachable: boolean; pending: boolean; }) { const t = useT("build"); if (deployUrl !== undefined) return ( } title={t("playground.unavailableTitle")} description={t("build.chat.playgroundHint")} contents={ {t("playground.deploy")} } /> ); if (pending) return (
); return ( } title={reachable ? t("build.chat.off") : t("build.chat.unavailable")} description={reachable ? t("build.chat.offHint") : t("build.chat.unavailableHint")} contents={ reachable ? ( {t("build.chat.settings")} ) : undefined } /> ); }