Open source CapCut alternative.
TypeScript
"use client";
import { useQueryClient, type QueryClient } from "@tanstack/react-query";
import { notesKey } from "@/cut/lib/queries";
import { ChatStatusBadge, type ChatStatus } from "./ChatStatusBadge";
import { CHAT_RUNTIME, CUT_JUDGE } from "@/cut/lib/chatRuntime";
import { ChatRequests } from "@/cut/lib/chatRequests";
import { Fragment, memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode } from "react";
import { Chat, useChat } from "@ai-sdk/react";
import { DefaultChatTransport, type ChatInit, type ChatTransport, type UIMessage, type UIMessageChunk } from "ai";
import {
ArrowUp,
Brain,
Check,
ChevronLeft,
ChevronDown,
CircleDashed,
Copy,
Ellipsis,
ExternalLink,
FolderPlus,
History,
Maximize2,
Mic,
Plus,
Sparkles,
Square,
Star,
Trash2,
TriangleAlert,
Wrench,
X,
} from "lucide-react";
import Markdown, { type Components } from "react-markdown";
import { useFormatter, useLocale, useTranslations } from "next-intl";
import { baseMarkdownComponents } from "./markdownComponents";
import { LiveElapsed } from "./Elapsed";
import { SceneCard } from "./SceneCard";
import { useElapsed } from "@/cut/hooks/useElapsed";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { apiJson, engineReady } from "@/cut/lib/api";
import { track } from "@/lib/analytics";
import { useCutCaps, useLocalCompute } from "@/cut/lib/backend/hooks";
import { localBackend } from "@/cut/lib/backend/local";
import { setAssetDragData } from "@/cut/lib/assetDrag";
import { useRefCopy } from "@/cut/lib/refCopy";
import { registerChatIntake } from "@/cut/lib/chatIntake";
import { videoModel } from "@/cut/lib/videoModels";
import {
chatThreadDeleted,
deleteStoredThread,
isStoredThread,
loadProjectThreads,
flushThreadSaves,
readActiveChat,
readQueuedRows,
readRawThreads,
recordThread,
seenRepliesKey,
subscribeThreadSaveFailure,
threadSaveFailure,
threadsLoaded,
writeActiveChat,
writeQueuedRows,
writeRawThreads,
subscribeChatThreads,
type ThreadRecorder,
} from "@/cut/lib/chatThreads";
import {
deleteCloudThread,
ensureCloudThreads,
flushCloudThreadSaves,
queueCloudThreadSave,
} from "@/cut/lib/chatCloud";
import {
beginChatTurn,
deleteChatAssets,
endChatTurn,
setActiveChatThread,
threadOwnsAssets,
} from "@/cut/lib/chatAssets";
import { threadHasLiveRun } from "@/cut/lib/genScene";
import {
addRefOnce,
collectRefs,
normalizeRef,
sameRef,
setRefDragData,
upsertRef,
useRefCandidates,
useAssetDrop,
type AssetRef,
} from "@/cut/lib/assetRef";
import {
useGenerate,
useSignedIn,
} from "@/cut/lib/generate";
import { knownBalance, useCreditsRecheck, useOutOfCredits } from "@/cut/lib/hosted";
import { cutChatLive, dropPiSession, foldIntoCutChat, hydratePiSession, readPiSession, streamCutChat, triageQueuedMessages } from "@/cut/lib/pi/cutAgent";
import { dueForRetriage, toolProgress } from "@/cut/lib/queueTriage";
import { registerQueueSink } from "@/cut/lib/chatQueue";
import { previewAt } from "@/cut/lib/playhead";
import { onComposerFill, reportChatRunning, reportChatSent } from "@/cut/lib/onboarding";
import { resolvePlayheadTokens, syncTimes, type TimeMark } from "@/cut/lib/timeMark";
import { productionDeps } from "@/cut/lib/pi/prodDeps";
import { withChatProject } from "@/cut/lib/projectChatTools";
import { runAiTool } from "@/cut/lib/aiTools";
import { editorChatActive, flushEditorSave, holdEditorChat } from "@/cut/lib/editorWork";
import { nextSyncMs } from "@/cut/lib/syncCadence";
import { recoverSceneCall } from "@/cut/lib/chatRecovery";
import { countChunks, replayChatStream } from "@/cut/lib/chatReplay";
import { INTERRUPTED_ERROR, isResumeMessage, journalAhead, RESUME_LIMIT, resumePrompt, scrubLostTurn, settledMark, unfinishedAsk, type JournalMark, type ResumeMetadata, type StreamedMark, type TurnSettled } from "@/cut/lib/chatResume";
import { answerEngineGate, claimEngineTool, cancelEngineChat, foldIntoEngineChat, joinRunningEngineChat, peekEngineChat } from "@/cut/lib/engineChat";
import { messageText } from "@/cut/lib/messageText";
import { EngineGate, engineInstant, handledSince, judgeEngineTurn } from "@/cut/lib/pi/engineTurn";
import type { EngineRoute } from "@/cut/server/ai/turnCatalog";
import { creditBalanceQueryKey, fetchCreditBalance } from "@/queries/credits";
import { projectBackend } from "@/cut/lib/residency";
import { beginBrowserChat, browserChatRunning, cancelBrowserChat, watchBrowserChatTurns } from "@/cut/lib/browserChatTurns";
import { putCloudThread } from "@/cut/lib/chatCloud";
import type { AgentMessage } from "@earendil-works/pi-agent-core";
import {
AI_MODELS,
accountRuns,
aiModelProvider as provider,
currentChatModel,
runnableChatModel,
type ChatProvider,
} from "@/cut/lib/aiModels";
import { saveAssetToLibrary } from "@/cut/lib/library";
import { formatDuration, useGenScene } from "@/cut/lib/genScene";
import { lightboxItemFromRef, useLightbox } from "@/cut/lib/lightbox";
import { refsFromDroppedFiles } from "@/cut/lib/refMedia";
import { revealRef } from "@/cut/lib/refReveal";
import { CHAT_TURN_LABEL, useEditor } from "@/cut/lib/store";
import { reportActivity } from "@/cut/lib/tabActivity";
import { cn } from "@/lib/utils";
import { cardIconButton } from "@/cut/components/iconButton";
import { MentionedText, MentionTextarea, RefChips, RefThumb, refHasCard, UnmentionedRefPills } from "./AssetRefs";
import { ComposerQueue, type QueuedMessage } from "./ComposerQueue";
import { DictationBody } from "./MicDictation";
import { RECORD_RUNNING_TTL_MS, ToolOutputAssets } from "./ChatAssets";
import { attachedRefKeys, cardIsAttached } from "@/cut/lib/chatCards";
import { HostedErrorText, openCreditsWall } from "./hostedError";
import { useMicTranscription } from "@/cut/lib/micTranscribe";
import { registerLanguageSink } from "@/cut/lib/interfaceLanguage";
import { UserError } from "@/cut/lib/userError";
import { useChatError } from "./chatError";
import { switchLocale } from "@/i18n/localeCookie";
import type { Locale } from "@/i18n/locales";
import { useUpdateLocale } from "@/queries/accountProfile";
// Chat attachments are asset refs — anything in the project, the library, or
// the stock catalog. They arrive by drag (media cards, library clips, stock
// tiles, timeline clips, the preview) or as @name mentions in the message.
interface ModelsInfo {
// A CLI provider's group lists only on `installed` true — unknown (probe
// still out, or no engine) reads as absent. A provider that is installed
// but unavailable (e.g. signed out) still lists with its note.
// A provider's `models` lists what its signed-in account runs.
providers: Record<string, ChatProvider>;
}
/** What an engine send carries past the messages: the editor snapshot the
* turn was judged against, and its route when it was judged. */
interface EngineSend {
context: unknown;
route: EngineRoute | undefined;
}
/** A failure the engine coded for the reader: the English message it ended
* the stream with, and the code and values of its errors-catalog message. */
interface ChatFailure {
message: string;
code: string;
params: Record<string, string | number>;
}
/** A saved chat thread, persisted per project in localStorage. */
interface ChatThread {
id: string;
title: string;
updatedAt: number;
messages: UIMessage[];
completedMessageId?: string;
/** How the newest turn ended when it did not complete. Unset while it runs
* and when the page lost it, which is what resumes it on return. */
turnSettled?: TurnSettled;
/** Continuations the newest ask has had. */
resumed?: number;
/** Provider-native session ids so a resumed thread keeps its context. */
sessions: Record<string, string>;
/** The pi loop's LLM context (structured tool history included), when the
* thread has run on it. */
pi?: AgentMessage[];
/** How much of its newest engine turn's journal the thread was saved from,
* once that turn ended. */
journal?: JournalMark;
}
const THREAD_LIMIT = 30;
// The title older threads stored when their first message had no text; the
// list words it, and an empty title, in the page's language.
const UNTITLED_THREAD = "New chat";
// Which reply each thread has been read up to, per project: a small store
// over localStorage, so the panel reads it as a snapshot and any writer —
// this tab or another — updates every reader.
const NO_SEEN_REPLIES: Record<string, string> = {};
const seenCache = new Map<string, { raw: string; value: Record<string, string> }>();
const seenListeners = new Set<() => void>();
function readSeenReplies(projectId: string): Record<string, string> {
let raw: string | null = null;
try {
raw = localStorage.getItem(seenRepliesKey(projectId));
} catch {
return NO_SEEN_REPLIES;
}
if (raw === null) return NO_SEEN_REPLIES;
const cached = seenCache.get(projectId);
if (cached && cached.raw === raw) return cached.value;
let value: Record<string, string> = NO_SEEN_REPLIES;
try {
const stored: unknown = JSON.parse(raw);
if (stored && typeof stored === "object" && !Array.isArray(stored)) {
value = Object.fromEntries(Object.entries(stored).filter((entry): entry is [string, string] =>
typeof entry[1] === "string",
));
}
} catch {
// Unreadable state reads as nothing seen.
}
seenCache.set(projectId, { raw, value });
return value;
}
function writeSeenReplies(projectId: string, next: Record<string, string>): void {
try {
localStorage.setItem(seenRepliesKey(projectId), JSON.stringify(next));
} catch {
// A browser that keeps nothing forgets the read state on reload.
}
for (const listener of seenListeners) listener();
}
function subscribeSeenReplies(listener: () => void): () => void {
seenListeners.add(listener);
const remote = (event: StorageEvent) => {
if (event.key?.startsWith("cut-ai-seen-replies-")) listener();
};
window.addEventListener("storage", remote);
return () => {
seenListeners.delete(listener);
window.removeEventListener("storage", remote);
};
}
// How long a streaming turn's newest snapshot may park before it must land —
// the same cadence the cloud mirror debounces on.
const THREAD_SAVE_MS = 1500;
// How long a dropped connection rests before the turn picks itself back up.
const RESUME_RETRY_MS = 1500;
// How long after its last save a lost turn still resumes; an older thread is
// one the user walked away from, and it waits for them.
const RESUME_WINDOW_MS = 24 * 60 * 60 * 1000;
/** The first message of a thread the triage spawns: a queued row that can
* run alongside the current turn, sent the moment its session mounts. */
interface ThreadSeed {
text: string;
attachments: AssetRef[];
model: string;
}
/** Attachments a restored message can still deliver: file blobs die with the
* page that made them, and a project asset may have been deleted since. */
function liveRefs(refs: AssetRef[]): AssetRef[] {
return refs.filter(
(a) =>
!a.url?.startsWith("blob:") &&
(a.scope !== "project" ||
useEditor.getState().assets.some((x) => x.id === a.id)),
);
}
/** A waiting row's text with its `@1:34` moments read against the cut as it
* stands this instant — what a dispatch sends, whichever render it fires on. */
function dueText(m: QueuedMessage): string {
if (!m.times?.length) return m.text;
const { clips, audioClips } = useEditor.getState();
return syncTimes(m.text, m.times, { clips, audioClips }).text;
}
function readThreads(projectId: string): ChatThread[] {
return readRawThreads(projectId).filter((t) => isStoredThread(t) && !t.deleted) as ChatThread[];
}
/** Persisted copies drop frame and sound payloads (data URLs) from tool
* outputs — one watch_video result carries a few MB of frames, and every
* project's history shares this browser's storage. The live thread keeps its images; replayed turns only
* ever reuse text parts, so nothing downstream misses them. */
function slimForStorage(list: ChatThread[]): ChatThread[] {
const bulky = (v: unknown) =>
typeof v === "string" && v.startsWith("data:image/");
// The pi session's inline media (tool frames and sound, attachment payloads)
// stays out of storage the same way. Each drop leaves a note where the media
// was, so a replayed turn never promises an attachment that is no longer
// there — the model re-fetches what it still needs.
const slimNote =
"[The media here was dropped from the saved chat. Fetch what you still need with the media tools (listen_audio, watch_video, capture_frame).]";
const slimPi = (msgs: AgentMessage[]): AgentMessage[] =>
msgs.map((m) => {
const anyM = m as unknown as Record<string, unknown>;
let out = anyM;
const details = anyM.details as { mediaParts?: unknown[] } | undefined;
if (details?.mediaParts?.length)
out = { ...out, details: { ...details, mediaParts: [{ text: slimNote }] } };
const wireParts = anyM.wireParts as Record<string, unknown>[] | undefined;
if (wireParts?.some((p) => typeof p.dataBase64 === "string"))
out = {
...out,
wireParts: [
...wireParts.filter((p) => typeof p.dataBase64 !== "string"),
{ text: slimNote },
],
};
if (
anyM.role === "user" &&
Array.isArray(anyM.content) &&
(anyM.content as { type: string }[]).some((c) => c.type === "image")
)
out = {
...out,
content: [
...(anyM.content as { type: string }[]).filter((c) => c.type !== "image"),
{ type: "text", text: slimNote },
],
};
return out as unknown as AgentMessage;
});
return list.map((t) => ({
...t,
...(t.pi ? { pi: slimPi(t.pi) } : {}),
messages: t.messages.map((m) => ({
...m,
parts: m.parts.map((p) => {
if (m.role === "assistant" && p.type === "text" && p.text.includes("<")) {
const clean = sanitizeAssistantText(p.text);
if (clean !== p.text) return { ...p, text: clean };
}
const out = (p as { output?: unknown }).output;
if (!out || typeof out !== "object") return p;
const o = out as Record<string, unknown>;
const frames = bulky(o.image) || (Array.isArray(o.images) && o.images.some(bulky));
// listen_audio's clip, as the server's stream drops it too.
const sound = typeof o.audio === "string" && o.audio.startsWith("data:");
if (!frames && !sound) return p;
return {
...p,
output: {
...o,
...(frames ? { image: undefined, images: undefined, imagesOmitted: true } : {}),
...(sound ? { audio: undefined, audioOmitted: true } : {}),
},
} as typeof p;
}),
})),
}));
}
/** A saved thread can hold tool parts captured mid-run — the page closed or
* the stream died before the result landed. Nothing settles them after a
* reload, so they'd show a live spinner forever; restore them as failed. */
function settleInterruptedTools(messages: UIMessage[]): UIMessage[] {
return messages.map((m) => ({
...m,
parts: m.parts.map((p) => {
const t = p as { type: string; state?: string };
if (!isToolPartType(t.type)) return p;
if (t.state === "output-available" || t.state === "output-error") return p;
return { ...p, state: "output-error", errorText: INTERRUPTED_ERROR } as typeof p;
}),
}));
}
function writeThreads(projectId: string, list: ChatThread[]) {
const editor = useEditor.getState();
if (editor.projectId !== projectId || !editor.loaded) {
// Pruning needs this project's media ownership; a background save keeps its history.
writeRawThreads(projectId, slimForStorage(list));
return;
}
// Cap history, but retain any overflow thread that still owns chat media or
// a working scene run — killing media or work is an explicit act (deleting
// its thread), never a side effect of the history cap.
const kept = [
...list.slice(0, THREAD_LIMIT),
...list
.slice(THREAD_LIMIT)
.filter((t) => threadOwnsAssets(t.id) || threadHasLiveRun(t.id)),
];
// A pruned thread is gone the way a deleted one is — anything it still
// owned (by construction nothing live) dies with it, cloud copy included.
const keptIds = new Set(kept.map((t) => t.id));
for (const t of list) {
if (!keptIds.has(t.id)) {
useGenScene.getState().killThread(t.id);
deleteCloudThread(projectId, t.id);
writeQueuedRows(projectId, t.id, []);
}
}
writeRawThreads(projectId, slimForStorage(kept));
}
const MODEL_KEY = "cut-ai-model";
const FAVS_KEY = "cut-ai-favs";
// Waiting composer-queue rows, saved per thread so a reload or a project
// switch brings them back. Running and done rows stay out — their turns die
// with the page.
function readStoredQueue(
projectId: string,
threadId: string,
): QueuedMessage[] {
if (typeof window === "undefined") return [];
try {
return (readQueuedRows(projectId, threadId) as QueuedMessage[])
.filter(
(m) =>
m?.status === "queued" &&
typeof m.id === "string" &&
typeof m.text === "string",
)
.map((m) => ({
...m,
attachments: Array.isArray(m.attachments) ? m.attachments : [],
// A stored mark with no token or no time would format as nonsense;
// the row's written time stands on its own without it.
times: (Array.isArray(m.times) ? m.times : []).filter(
(t) => typeof t?.token === "string" && Number.isFinite(t?.at),
),
}));
} catch {
return [];
}
}
// A fresh user starts on Gemini — it runs on their signed-in Donkey account, so
// it works without a local CLI. Claude/Codex show up automatically once the
// engine probes them installed. The choice persists, so this is first-run only.
const DEFAULT_MODEL =
AI_MODELS.find((m) => m.provider === "gemini" && !m.hidden)?.id ?? "gemini";
const PROVIDER_LABEL: Record<string, string> = {
claude: "Claude Code",
codex: "Codex",
gemini: "Gemini",
};
/** A provider's group name: product names as they are, the hermetic test
* provider in the page's language. */
function providerLabel(id: string, t: ReturnType<typeof useTranslations<"chat.panel">>): string {
if (id === "test") {
return t("providerTesting");
}
return PROVIDER_LABEL[id] ?? id;
}
// The prompt chips on an empty thread, by message id (chat.panel.suggestions).
// A chip sends its text in the page's language; the model reads any.
const SUGGESTIONS = [
"whatsInVideo",
"addTransitions",
"improveTitle",
"createSubtitles",
"rewriteSubtitles",
"writeCaption",
] as const;
export function AiPanel({
projectId,
visible,
onClose,
onStatusChange,
}: {
projectId: string;
visible: boolean;
onClose: () => void;
onStatusChange: (projectId: string, status: ChatStatus) => void;
}) {
const t = useTranslations("chat.panel");
const format = useFormatter();
const [info, setInfo] = useState<ModelsInfo | null>(null);
const engineUp = useLocalCompute();
const readOnly = useEditor((s) => s.readOnly);
const signedIn = useSignedIn();
// A saved id that was retired moves to its replacement and the move is
// written back; an id the catalog has never known falls back to the
// default, since passed through the hosted route rejects it.
const [model, setModel] = useState<string>(() => {
if (typeof window === "undefined") return DEFAULT_MODEL;
const saved = localStorage.getItem(MODEL_KEY);
const current = saved ? currentChatModel(saved) : undefined;
if (!current) return DEFAULT_MODEL;
if (current !== saved) localStorage.setItem(MODEL_KEY, current);
return current;
});
// One chat is active at a time; every past chat lives in the Threads panel.
// The id persists so closing and reopening the panel resumes the same chat.
const [activeChat, setActiveChat] = useState<string>(() =>
typeof window === "undefined"
? crypto.randomUUID()
: (readActiveChat(projectId) ?? crypto.randomUUID()),
);
const [runningThreads, setRunningThreads] = useState<Record<string, string>>({});
// Threads the triage spawned for a message that could run alongside the
// current turn. A seed mounts its session, which sends the message on
// arrival and then counts as running like any other thread.
const [seeds, setSeeds] = useState<Record<string, ThreadSeed>>({});
const spawnThread = useCallback((seed: ThreadSeed) => {
const id = crypto.randomUUID();
setSeeds((all) => ({ ...all, [id]: seed }));
return id;
}, []);
const onSeedTaken = useCallback((id: string, turnModel: string) => {
setSeeds((all) => {
if (!all[id]) return all;
const rest = { ...all };
delete rest[id];
return rest;
});
setRunningThreads((threads) => (threads[id] ? threads : { ...threads, [id]: turnModel }));
}, []);
const onRunningChange = useCallback((id: string, running: boolean, turnModel: string) => {
setRunningThreads((threads) => {
if (running) return threads[id] ? threads : { ...threads, [id]: turnModel };
if (!threads[id]) return threads;
const remaining = { ...threads };
delete remaining[id];
return remaining;
});
}, []);
useEffect(() => {
writeActiveChat(projectId, activeChat);
}, [activeChat, projectId]);
const onThreadDeleted = useCallback((id: string) => {
setActiveChat((current) => current === id ? crypto.randomUUID() : current);
onRunningChange(id, false, "");
}, [onRunningChange]);
const anyRunning = Object.keys(runningThreads).length > 0;
const queryClient = useQueryClient();
const wasRunning = useRef(false);
useEffect(() => {
if (wasRunning.current && !anyRunning) void queryClient.invalidateQueries({ queryKey: notesKey });
wasRunning.current = anyRunning;
}, [anyRunning, queryClient]);
useEffect(() => {
reportActivity("chat", anyRunning);
return () => reportActivity("chat", false);
}, [anyRunning]);
const [historyOpen, setHistoryOpen] = useState(false);
const [threads, setThreads] = useState<ChatThread[]>(() => readThreads(projectId));
// What every running thread is working on, for the triage's collision
// check: a seed's text until its thread saves, the stored ask after.
const runningAsks = useMemo<Record<string, string>>(() => {
const asks: Record<string, string> = {};
for (const id of Object.keys(runningThreads)) {
const ask = threads.find((t) => t.id === id)?.messages.findLast((m) => m.role === "user");
const text = ask ? messageText(ask) : "";
if (text) asks[id] = text;
}
for (const [id, seed] of Object.entries(seeds)) asks[id] = seed.text;
return asks;
}, [runningThreads, threads, seeds]);
const seenReplies = useSyncExternalStore(
subscribeSeenReplies,
() => readSeenReplies(projectId),
() => NO_SEEN_REPLIES,
);
const markThreadRead = (thread: ChatThread) => {
const reply = thread.completedMessageId;
if (!reply || runningThreads[thread.id] || thread.messages.at(-1)?.id !== reply ||
seenReplies[thread.id] === reply) return;
const next = { ...readSeenReplies(projectId) };
delete next[thread.id];
next[thread.id] = reply;
writeSeenReplies(projectId, Object.fromEntries(Object.entries(next).slice(-THREAD_LIMIT)));
};
useEffect(() => {
return subscribeChatThreads(projectId, () => setThreads(readThreads(projectId)));
}, [projectId]);
// A thread is unread while its finished reply is the last word and no one
// has read it: opened from the list, or on screen when the reply landed.
const unread = (thread: ChatThread) =>
!runningThreads[thread.id] && !!thread.completedMessageId &&
thread.messages.at(-1)?.id === thread.completedMessageId &&
seenReplies[thread.id] !== thread.completedMessageId;
const hasUnread = threads.some(unread);
const chatStatus: ChatStatus = anyRunning ? "working" : hasUnread ? "unread" : null;
useEffect(() => {
onStatusChange(projectId, chatStatus);
}, [projectId, chatStatus, onStatusChange]);
// The starter tour holds its last step until the reply lands.
useEffect(() => {
reportChatRunning(anyRunning);
}, [anyRunning]);
// The owner's chat opens once this browser's stored history has loaded
// (IndexedDB, so a beat after mount — opening before it would show an empty
// thread and save it over the real one); the cloud copy merges in behind it
// and a session reloads its thread when the merge writes it. A shared viewer
// has no thread of their own, so their view waits for the owner's threads
// to land before it can pick one.
const [chatsReady, setChatsReady] = useState(() => !readOnly && threadsLoaded(projectId));
useEffect(() => {
let alive = true;
void loadProjectThreads(projectId)
.then(() => {
if (!alive) return;
setThreads(readThreads(projectId));
if (!readOnly) setChatsReady(true);
return ensureCloudThreads(projectId);
})
.then(() => {
if (alive) setChatsReady(true);
});
return () => {
alive = false;
};
}, [projectId, readOnly]);
// Re-read the cloud copy of the project's chats: at the sync interval while
// they move or this tab's chat runs, widening while they hold still. The
// window coming back re-reads at once.
useEffect(() => {
let stopped = false;
let running = false;
let timer: ReturnType<typeof setTimeout>;
let waitMs: number = CHAT_RUNTIME.syncIntervalMs;
const refresh = async () => {
if (stopped || running) return;
running = true;
try {
if (!document.hidden && useEditor.getState().projectId === projectId) {
const pulse = await ensureCloudThreads(projectId, true);
waitMs = nextSyncMs(waitMs, editorChatActive(projectId) ? "moved" : pulse);
}
} finally {
running = false;
if (!stopped) timer = setTimeout(() => void refresh(), waitMs);
}
};
const wake = () => {
if (document.hidden) return;
clearTimeout(timer);
waitMs = CHAT_RUNTIME.syncIntervalMs;
void refresh();
};
document.addEventListener("visibilitychange", wake);
window.addEventListener("focus", wake);
timer = setTimeout(() => void refresh(), waitMs);
return () => {
stopped = true;
clearTimeout(timer);
document.removeEventListener("visibilitychange", wake);
window.removeEventListener("focus", wake);
};
}, [projectId]);
// A shared view opens on the owner's newest thread — the viewer has no chat
// of their own to resume. Derived, so the fallback holds until the viewer
// explicitly opens another thread from the list.
const sessionThread = useMemo(() => {
if (!readOnly || !chatsReady) return activeChat;
const list = readThreads(projectId);
if (list.length === 0 || list.some((t) => t.id === activeChat)) return activeChat;
return list[0].id;
}, [readOnly, chatsReady, projectId, activeChat]);
useEffect(() => {
// The models probe asks the engine which CLIs are installed. The CLIs live
// on this Mac, so the engine being reachable is what decides — whatever
// backend holds the project. Without one there is no engine to ask
// (mergedInfo synthesizes the Gemini-only provider set instead).
if (!engineUp) return;
let alive = true;
void localBackend
.fetch("/api/cut/ai/models")
.then((r) => {
// An error body carries no provider list.
if (!r.ok) throw new Error(`models probe answered ${r.status}`);
return r.json();
})
.then((d: ModelsInfo) => alive && setInfo(d))
.catch(() => {});
return () => {
alive = false;
};
}, [engineUp]);
const newChat = () => {
setActiveChat(crypto.randomUUID());
setHistoryOpen(false);
};
const openThread = (t: ChatThread) => {
markThreadRead(t);
setActiveChat(t.id);
setHistoryOpen(false);
};
const openSpawned = useCallback((id: string) => {
setActiveChat(id);
setHistoryOpen(false);
}, []);
// The thread on screen reads its own reply: a reply that lands while the
// panel shows the thread with the list closed is read once the tab is in
// front, whether that is now or when the person comes back to it.
const onScreen = visible && !historyOpen;
useEffect(() => {
if (!onScreen) return;
const shown = threads.find((t) => t.id === sessionThread);
if (!shown || !unread(shown)) return;
if (!document.hidden) {
markThreadRead(shown);
return;
}
const onShow = () => {
if (!document.hidden) markThreadRead(shown);
};
document.addEventListener("visibilitychange", onShow);
return () => document.removeEventListener("visibilitychange", onShow);
});
const toggleHistory = () => {
if (!historyOpen) setThreads(readThreads(projectId));
setHistoryOpen((v) => !v);
};
const deleteThread = (id: string) => {
deleteStoredThread(projectId, id);
cancelBrowserChat(projectId, id);
if (engineUp) void cancelEngineChat(projectId, id).catch(() => {});
onRunningChange(id, false, model);
setThreads((p) => p.filter((t) => t.id !== id));
// The thread's chat-only assets go with it; anything placed or filed
// into Media/Library stays.
deleteChatAssets(id);
deleteCloudThread(projectId, id);
writeQueuedRows(projectId, id, []);
// Its work dies with it too: a scene run the thread owned aborts and its
// plan clears — nothing keeps running behind a conversation the user killed.
useGenScene.getState().killThread(id);
// If the open chat was deleted, start a fresh one so it can't re-save
// itself on the next message and resurrect the thread.
if (activeChat === id) setActiveChat(crypto.randomUUID());
};
const selectModel = (id: string) => {
setModel(id);
localStorage.setItem(MODEL_KEY, id);
};
// Gemini runs on the user's Donkey account, so its availability is the
// sign-in probe, not the engine's CLI checks. Signed-in state (or a probe
// still in flight) leaves it usable; a definite signed-out disables it.
const mergedInfo = useMemo<ModelsInfo | null>(() => {
const gemini =
signedIn === false
? { available: false, note: t("signInToChat"), installed: true }
: (info?.providers.gemini ?? { available: true, note: "", installed: true });
// With no engine on this Mac the CLI providers don't exist: their groups
// hide, and the saved-model fallback effect moves a CLI selection over to
// Gemini.
if (!engineUp) {
const off = { available: false, note: "", installed: false };
const providers: ModelsInfo["providers"] = { claude: off, codex: off, test: off, gemini };
return { providers };
}
if (!info) return null;
return { ...info, providers: { ...info.providers, gemini } };
}, [info, signedIn, engineUp, t]);
return (
// A column beside the editor, as it has always been. Docking costs the
// editor 340px and it wears that down to a narrow window, taking the
// content over with it; only once the window itself is under the width the
// editor needs to be worth opening — NARROW_MAX_WIDTH, 900 — is there
// nothing left to take, and the panel lifts off and overlays instead.
//
// A plain overlay, with no scrim: the cut stays readable behind it, which
// is the point of having it open. Fixed rather than absolute so it is the
// viewport it pins to, not the editor's box — narrower than the panel and
// that box is wider than the screen, and the panel would hang off it.
<aside style={visible ? undefined : { display: "none" }} className="ai-panel relative z-50 flex min-h-0 w-[340px] shrink-0 animate-in flex-col border-l border-border bg-card duration-300 ease-out slide-in-from-right-full max-[900px]:fixed max-[900px]:inset-y-0 max-[900px]:right-0 max-[900px]:w-full max-[900px]:max-w-[340px] max-[900px]:shadow-[-16px_0_40px_rgba(0,0,0,0.14)]">
<div className="flex h-[46px] shrink-0 items-center gap-1.5 border-b border-border pr-2 pl-3.5">
<div className="flex-1" />
<Button
variant="ghost"
size="sm"
className="ai-threads"
title={t("pastThreads")}
aria-pressed={historyOpen}
onClick={toggleHistory}
>
{/* A finished reply waiting in another thread marks the button that
opens the list, the same blue dot the thread itself carries. */}
<span className="relative grid place-items-center">
<History />
{hasUnread && <ChatStatusBadge status="unread" className="bg-card" />}
</span>
</Button>
{!readOnly && (
<Button
variant="ghost"
size="sm"
className="ai-new-thread"
title={t("newChat")}
onClick={newChat}
>
<Plus />
</Button>
)}
<Button variant="ghost" size="sm" title={t("closeShortcut", { key: "⌘J" })} onClick={onClose}>
<X />
</Button>
</div>
{historyOpen && (
<>
<div
className="fixed inset-0 z-30"
onClick={() => setHistoryOpen(false)}
/>
{/* Beside the panel where the two fit — the list needs its 280 to the
left of the panel's 340 — and over it where they do not, as a
screen the panel pushes to and comes back from. */}
<div className="ai-thread-list absolute top-0 right-full bottom-0 z-40 flex w-[280px] animate-in flex-col border-x border-border bg-card shadow-[-16px_0_40px_rgba(0,0,0,0.14)] duration-200 ease-out fade-in-0 slide-in-from-right-6 max-[620px]:inset-0 max-[620px]:w-auto max-[620px]:border-x-0 max-[620px]:shadow-none">
<div className="flex h-[46px] shrink-0 items-center gap-1 border-b border-border pr-2 pl-3.5 max-[620px]:pl-1">
{/* Only where the list is covering the panel: sitting beside it,
there is nothing behind to go back to. */}
<Button
variant="ghost"
size="icon-sm"
className="hidden max-[620px]:inline-flex"
aria-label={t("backToChat")}
title={t("backToChat")}
onClick={() => setHistoryOpen(false)}
>
<ChevronLeft />
</Button>
<span className="text-sm font-semibold tracking-tight">
{t("threads")}
</span>
<div className="flex-1" />
<Button
variant="ghost"
size="sm"
title={t("close")}
className="max-[620px]:hidden"
onClick={() => setHistoryOpen(false)}
>
<X />
</Button>
</div>
<div className="ai-thread-items flex min-h-0 flex-1 flex-col gap-1 overflow-y-auto p-2">
{threads.length === 0 ? (
<p className="px-2 py-3 text-[11.5px] leading-relaxed text-muted-foreground">
{t("noPastThreads")}
</p>
) : (
threads.map((thread) => (
<button
key={thread.id}
className="group relative flex w-full items-center gap-2 rounded-lg py-2 pr-9 pl-2.5 text-left transition-colors hover:bg-muted"
onClick={() => openThread(thread)}
>
<span className="flex min-w-0 flex-1 flex-col gap-0.5">
<span className="truncate text-[12px] font-medium">
{thread.title && thread.title !== UNTITLED_THREAD ? thread.title : t("newChat")}
</span>
<span className="text-[10.5px] text-muted-foreground">
{format.dateTime(new Date(thread.updatedAt), {
month: "short",
day: "numeric",
hour: "numeric",
minute: "2-digit",
})}
</span>
</span>
<span className="grid size-3.5 shrink-0 place-items-center">
{runningThreads[thread.id] ? (
<CircleDashed
role="img"
aria-label={t("working")}
className="size-3.5 animate-spin text-muted-foreground"
/>
) : unread(thread) ? (
<span
role="img"
aria-label={t("completed")}
className="size-2 rounded-full bg-blue-500"
/>
) : null}
</span>
{!readOnly && (
<span
role="button"
aria-label={t("deleteThread")}
title={t("deleteThread")}
className="absolute top-1/2 right-1.5 grid size-6 -translate-y-1/2 place-items-center rounded-md text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100 hover:bg-black/10 hover:text-red-600"
onClick={(e) => {
e.stopPropagation();
deleteThread(thread.id);
}}
>
<Trash2 className="size-3.5" />
</span>
)}
</button>
))
)}
</div>
</div>
</>
)}
{chatsReady && [...new Set([sessionThread, ...Object.keys(runningThreads), ...Object.keys(seeds)])].map((id) => (
<ChatSession
key={id}
projectId={projectId}
threadId={id}
visible={visible && id === sessionThread}
onRunningChange={onRunningChange}
onDeleted={onThreadDeleted}
info={mergedInfo}
model={runningThreads[id] ?? seeds[id]?.model ?? model}
onModelChange={selectModel}
seed={seeds[id]}
onSeedTaken={onSeedTaken}
onSpawn={spawnThread}
onOpenThread={openSpawned}
runningAsks={runningAsks}
/>
))}
</aside>
);
}
/** Animates the composer's chip area to its content height, so every growth —
* the first attachment opening the row, a full row wrapping a new one open —
* raises the box smoothly instead of snapping. While growing, the box is
* shorter than its content and clips it; settled, it relaxes so chip hover
* cards and moment pickers can pop outside. Shrinks (a removed chip, the
* emptied row collapsing) animate too — their content already fits, so they
* never need the clip. */
function ChipsReveal({ open, children }: { open: boolean; children: ReactNode }) {
const outer = useRef<HTMLDivElement>(null);
const inner = useRef<HTMLDivElement>(null);
const [h, setH] = useState(0);
useEffect(() => {
const el = inner.current;
if (!el) return;
const ro = new ResizeObserver(() => setH(el.offsetHeight));
ro.observe(el);
return () => ro.disconnect();
}, []);
const target = open ? h : 0;
useLayoutEffect(() => {
const el = outer.current;
if (el && el.offsetHeight < target) el.style.overflow = "hidden";
}, [target]);
return (
<div
ref={outer}
className="transition-[height] duration-300 ease-[cubic-bezier(0.32,0.72,0,1)]"
style={{ height: target }}
onTransitionEnd={(e) => {
if (e.propertyName === "height" && outer.current) outer.current.style.overflow = "";
}}
>
<div ref={inner}>{children}</div>
</div>
);
}
/** Whether this account's turns are judged: signed in, with credits. An
* account with none chats on its own CLI model, which decides alone. A
* balance a charged call reported after the last read decides over it. */
async function accountJudges(queryClient: QueryClient): Promise<boolean> {
if (useGenerate.getState().signedIn !== true || useOutOfCredits.getState().out) return false;
try {
const read = await fetchCreditBalance(queryClient);
const readAt = queryClient.getQueryState(creditBalanceQueryKey)?.dataUpdatedAt ?? 0;
return knownBalance(read.balance, readAt) > 0;
} catch {
return false;
}
}
type ChatEvents = Pick<ChatInit<UIMessage>, "onError" | "onFinish" | "onData" | "onToolCall">;
/** A session's Chat. Its callbacks and transport route to the ones the latest
* commit bound, as useChat does with its own, so the Chat can live outside
* the hook and the thread's record can follow it directly. */
function sessionChat(id: string, messages: UIMessage[] | undefined) {
type Bound = ChatEvents & { transport: ChatTransport<UIMessage> };
let bound: Bound | null = null;
const transport = () => {
if (!bound) throw new UserError("chat_not_ready", "The chat is not ready yet.");
return bound.transport;
};
const chat = new Chat<UIMessage>({
id,
messages,
transport: {
sendMessages: (options) => transport().sendMessages(options),
reconnectToStream: (options) => transport().reconnectToStream(options),
},
onError: (error) => bound?.onError?.(error),
onFinish: (event) => bound?.onFinish?.(event),
onData: (part) => bound?.onData?.(part),
onToolCall: (call) => bound?.onToolCall?.(call),
});
return { chat, bind: (next: Bound) => { bound = next; } };
}
/** Running sessions stay mounted for the lifetime of their editor. */
function ChatSession({
projectId,
threadId,
visible,
onRunningChange,
onDeleted,
info,
model,
onModelChange,
seed,
onSeedTaken,
onSpawn,
onOpenThread,
runningAsks,
}: {
projectId: string;
threadId: string;
visible: boolean;
onRunningChange: (id: string, running: boolean, model: string) => void;
onDeleted: (id: string) => void;
info: ModelsInfo | null;
model: string;
onModelChange: (id: string) => void;
/** The message this thread was spawned to run; sent once on mount. */
seed?: ThreadSeed;
onSeedTaken: (id: string, model: string) => void;
/** Start a parallel thread on a message; returns its id. */
onSpawn: (seed: ThreadSeed) => string;
onOpenThread: (id: string) => void;
/** What every running thread of the project is working on, by thread id. */
runningAsks: Record<string, string>;
}) {
const t = useTranslations("chat.panel");
const chatError = useChatError();
const locale = useLocale();
const [input, setInput] = useState("");
const composerRef = useRef<HTMLTextAreaElement>(null);
const caps = useCutCaps();
const readOnly = useEditor((s) => s.readOnly);
// Live dictation → drops the finished transcript into the composer, appended
// after whatever the user had already typed.
const mic = useMicTranscription((text) =>
setInput((prev) => (prev.trim() ? `${prev.trim()} ${text}` : text)),
);
// When dictation ends the composer remounts; put the caret back at the end
// so Enter (confirm) → Enter (send) chains without a click.
const micWasActive = useRef(false);
useEffect(() => {
if (micWasActive.current && mic.state === "idle") {
const el = composerRef.current;
if (el) {
el.focus();
el.setSelectionRange(el.value.length, el.value.length);
}
}
micWasActive.current = mic.state !== "idle";
}, [mic.state]);
// The starter tour types its example message here, caret at the end, so
// Enter sends it.
useEffect(
() =>
onComposerFill((text) => {
setInput(text);
requestAnimationFrame(() => {
const el = composerRef.current;
if (el) {
el.focus();
el.setSelectionRange(el.value.length, el.value.length);
}
});
}),
[],
);
const [attachments, setAttachments] = useState<AssetRef[]>([]);
// Moments the composer's `@` tokens point at, in the order they were
// written. They ride with the message so the time can be re-read against
// the cut the message actually runs on.
const [times, setTimes] = useState<TimeMark[]>([]);
const candidates = useRefCandidates(visible, true);
// Any OS file drag over the window hints the composer as a drop target;
// hovering it (dropActive below) strengthens the ring and shows the label.
const fileDropHint = useEditor((s) => s.dropActive !== null);
// A resumed run can pin the scene card with no chat messages behind it — the
// empty-state intro/suggestions must yield to it so the two don't stack. The
// card shows in the thread that asked (unowned = pre-chatId runs, shown
// anywhere), so only that thread suppresses the intro.
const hasSceneRun = useGenScene(
(s) =>
!!s.run &&
s.run.projectId === projectId &&
(!s.run.chatId || s.run.chatId === threadId),
);
// OS files handed to the composer — dropped on it or pasted into it —
// attach as references (media files import into the project on the way,
// chat-owned so they stay off the Media panel; text files ride as-is).
const attachFiles = (files: File[]) => {
void refsFromDroppedFiles(projectId, files, { chatId: threadId }).then(
(refs) => setAttachments((prev) => refs.reduce(addRefOnce, prev)),
);
};
const {
active: dropActive,
attachTarget,
targetProps,
} = useAssetDrop(
(ref) => setAttachments((prev) => addRefOnce(prev, ref)),
attachFiles,
);
// A transient warning in the folder tab above the composer — the same slot
// the credits tab uses — for refusals like the frame capacity below.
const [notice, setNotice] = useState<string | null>(null);
// History that can't be saved is lost on reload, so say so while it lasts.
const saveFailure = useSyncExternalStore(subscribeThreadSaveFailure, threadSaveFailure, () => null);
const noticeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const showNotice = (msg: string) => {
setNotice(msg);
if (noticeTimer.current) clearTimeout(noticeTimer.current);
noticeTimer.current = setTimeout(() => setNotice(null), 5000);
};
useEffect(
() => () => {
if (noticeTimer.current) clearTimeout(noticeTimer.current);
},
[],
);
// The timeline's "Add video frame to chat" lands its pinned ref here; the
// registration follows the composer box's lifetime (absent on read-only
// shares, where there is no composer to receive it). `incoming` marks a
// frame still in flight, opening the chip row ahead of the landing.
// Grabbed frames stop at the render models' reference capacity, read from
// the registry, so every attached frame actually rides a render.
const composerBoxRef = useRef<HTMLDivElement>(null);
const [incoming, setIncoming] = useState(false);
const attachmentsRef = useRef(attachments);
useEffect(() => {
attachmentsRef.current = attachments;
}, [attachments]);
// The intake outlives renders; it words its refusals through the newest
// translator.
const wordingRef = useRef({ t, chatError });
useEffect(() => {
wordingRef.current = { t, chatError };
});
useEffect(() => {
const el = composerBoxRef.current;
if (!el) return;
const isFrame = (r: AssetRef) => r.scope === "file" && r.kind === "image";
const cap = videoModel("omni").maxReferenceImages;
return registerChatIntake({
el,
add: (ref) => setAttachments((p) => upsertRef(p, ref)),
expect: setIncoming,
acceptFrame: () => {
if (attachmentsRef.current.filter(isFrame).length < cap) return true;
showNotice(wordingRef.current.t("frameCapacity", { count: cap }));
return false;
},
notice: (failure) => showNotice(wordingRef.current.chatError(failure)),
});
}, [readOnly, visible]);
const sessionKeyRef = useRef<string | null>(null);
const toolAbort = useRef(new AbortController());
const queryClient = useQueryClient();
// The judged route the next engine send carries, and the quality gate of
// the engine turn in flight. Both stay empty for an unjudged turn.
const engineGateRef = useRef<EngineGate | null>(null);
// Resume from the saved thread when this id exists in history.
const [initialThread] = useState<ChatThread | undefined>(() =>
typeof window === "undefined"
? undefined
: readThreads(projectId).find((t) => t.id === threadId),
);
const seenThreadAt = useRef(initialThread?.updatedAt ?? 0);
const completedMessageId = useRef(initialThread?.completedMessageId);
const turnSettled = useRef<TurnSettled | undefined>(initialThread?.turnSettled);
const resumed = useRef(initialThread?.resumed ?? 0);
// The journal chunks the newest engine turn's stream handed the chat, and
// the mark the thread keeps once that turn ends (see journalAhead).
const streamed = useRef<StreamedMark | null>(null);
const journalMark = useRef(initialThread?.journal);
// A replay catching the thread up to the engine's journal: "finished" when
// that turn is over, so its tool calls and gate are history, not requests.
const catchingUp = useRef<"running" | "finished" | null>(null);
const userStopped = useRef(false);
// A dropped connection with continuations left: the turn rests, then resumes.
const [retryDisconnect, setRetryDisconnect] = useState(false);
// The engine journal reconnect for a turn the page lost: idle until the
// resume effect asks for one, pending while it runs, done once it answered.
const [reconnect, setReconnect] = useState<"idle" | "pending" | "done">("idle");
const providerSessions = useRef<Record<string, string>>({
...(initialThread?.sessions ?? {}),
});
// Seed the pi session registry from the stored thread before any turn runs.
useEffect(() => {
if (initialThread?.pi) hydratePiSession(threadId, initialThread.pi);
// eslint-disable-next-line react-hooks/exhaustive-deps -- initialThread is the mount-time snapshot
}, [threadId]);
// The transport below is memoized per thread and reads the model at send
// time. The ref is kept current from an effect, and the transport reaches it
// and the other refs through these stable accessors, so nothing touches a
// ref while rendering.
const modelRef = useRef(model);
useEffect(() => {
modelRef.current = model;
}, [model]);
const currentModel = useCallback(() => modelRef.current, []);
const sessionFor = useCallback((m: string) => providerSessions.current[provider(m)], []);
// Gemini turns run their editor tools inside the transport loop (no engine
// bridge); this flags them so onToolCall doesn't execute those calls again.
const clientToolsRef = useRef(false);
const setClientTools = useCallback((on: boolean) => {
clientToolsRef.current = on;
}, []);
const replayHandler = useRef<(id: string, requestId?: string) => boolean>(() => false);
const closeReplay = useRef<(() => void) | null>(null);
const finishBrowserTurn = useRef<(() => void) | null>(null);
const registerBrowserTurn = useCallback((signal?: AbortSignal) => {
const turn = beginBrowserChat(projectId, threadId, signal);
finishBrowserTurn.current = turn.finish;
return turn.signal;
}, [projectId, threadId]);
const prepareReplay = useCallback((id: string, requestId?: string) => replayHandler.current(id, requestId), []);
const attachReplay = useCallback((close: () => void) => { closeReplay.current = close; }, []);
const transport = useMemo<ChatTransport<UIMessage>>(() => {
// Built on the first send: a request callback handed to a constructor
// here would count as running during render, and it reads the model.
let engineTransport: DefaultChatTransport<UIMessage> | null = null;
const engine = () =>
(engineTransport ??= new DefaultChatTransport<UIMessage>({
// A refused turn reads as its message; the transport would show the raw body.
fetch: async (input, init) => {
let res = await fetch(input, init);
if (res.ok) return res;
// A turn this page lost (a reload, another tab) still runs the
// thread on the Mac: the message joins it, or goes out once it ends.
if (res.status === 409 && typeof init?.body === "string") {
const sent = JSON.parse(init.body) as Parameters<typeof joinRunningEngineChat>[2];
const joined = await joinRunningEngineChat(projectId, threadId, sent, init.signal ?? undefined);
if (joined) return joined;
res = await fetch(input, init);
if (res.ok) return res;
}
throw new Error((await apiJson(res)).error ?? t("engineReplied", { status: res.status }));
},
// The engine origin is discovered asynchronously; await it per request
// (not at mount) so an early send still targets the local engine rather
// than the hosted origin, where the Cut APIs 404. engineReady memoizes,
// so only the first request pays for discovery. CLI chat always runs on
// this Mac's engine — even for a cloud project — so the URL comes from
// localBackend (read after the origin resolves), which carries the
// account scope the engine requires on every data route.
// sendMessages hands over the editor snapshot it judged against and
// the route; an unjudged turn (an account with no credits) carries
// none and the model reads skills itself.
prepareSendMessagesRequest: async ({ messages, body }) => {
const { context, route } = body as EngineSend;
await engineReady();
return {
api: localBackend.url("/api/cut/ai/chat"),
body: {
threadId,
runtime: CHAT_RUNTIME,
messages,
model: currentModel(),
context,
providerSession: sessionFor(currentModel()),
route,
handled: handledSince(messages),
locale,
},
};
},
prepareReconnectToStreamRequest: async () => {
await engineReady();
return { api: localBackend.url(`/api/cut/ai/chat/${encodeURIComponent(threadId)}/stream?projectId=${encodeURIComponent(projectId)}`) };
},
}));
return {
// Claude/Codex chat through the local engine; Gemini goes straight from
// the page to Donkey's hosted inference with the user's session.
sendMessages: async (options) => {
// A new turn opens with no verdict; a continuation keeps its ask's count.
turnSettled.current = undefined;
userStopped.current = false;
streamed.current = null;
const newest = options.messages.at(-1);
if (!newest || !isResumeMessage(newest)) resumed.current = 0;
if (provider(currentModel()) === "gemini") {
setClientTools(true);
const signal = registerBrowserTurn(options.abortSignal);
turnSignalRef.current = signal;
return streamCutChat({
threadId,
model: currentModel(),
messages: options.messages,
locale,
abortSignal: signal,
deps: productionDeps(projectId, signal),
});
}
setClientTools(false);
toolAbort.current = new AbortController();
// An account with credits has the turn judged before the send: one
// known action runs here with no model round, and the rest go out
// routed, with the gate answering when the CLI signs off.
const deps = productionDeps(projectId, options.abortSignal);
const decision = (await accountJudges(queryClient))
? await judgeEngineTurn(options.messages, deps, options.abortSignal)
: null;
if (decision?.instant) {
const handled = await engineInstant(decision.instant, deps);
if (handled) {
setClientTools(true);
return handled;
}
}
const ask = options.messages.findLast((m) => m.role === "user" && !isResumeMessage(m));
engineGateRef.current = decision?.route.gate ? new EngineGate(ask ? messageText(ask) : "") : null;
const send: EngineSend = { context: deps.buildContext(), route: decision?.route };
return countChunks(await engine().sendMessages({ ...options, body: { ...options.body, ...send } }), (mark) => { streamed.current = mark; });
},
reconnectToStream: async (options) => {
if (provider(currentModel()) === "gemini") return Promise.resolve(null);
setClientTools(false);
streamed.current = null;
let replay: ReadableStream<UIMessageChunk> | null = null;
try {
const stream = await engine().reconnectToStream(options);
// A catch-up replays the reply the thread already counts as done.
replay = stream ? await replayChatStream(stream, prepareReplay, attachReplay, catchingUp.current ? undefined : completedMessageId.current) : null;
} finally {
// A replay settles the reconnect when it finishes (onFinish); with
// nothing to replay the resume effect moves on to a continuation.
if (!replay) {
catchingUp.current = null;
setReconnect("done");
}
}
return replay && countChunks(replay, (mark) => { streamed.current = mark; });
},
};
}, [threadId, projectId, currentModel, sessionFor, setClientTools, prepareReplay, attachReplay, registerBrowserTurn, queryClient, locale, t]);
// The session owns its Chat so the thread's record can follow the chat's
// own state (recordThread, below) rather than this component's renders: a
// tab that cannot paint keeps streaming and running tools while React may
// commit nothing.
const [session] = useState(() => sessionChat(threadId, initialThread && settleInterruptedTools(initialThread.messages)));
const chat = session.chat;
const { messages, setMessages, sendMessage, stop, status, error, clearError, resumeStream } = useChat({ chat });
// Bound on every commit, so the Chat always reaches the newest closures.
const chatEvents: ChatEvents = {
onError: () => {
catchingUp.current = null;
finishBrowserTurn.current?.();
finishBrowserTurn.current = null;
endChatTurn(threadId);
},
onFinish: ({ messages: finished, message, isAbort, isDisconnect, isError }) => {
// This callback outlives the panel when a browser turn finishes after navigation.
endChatTurn(threadId);
finishBrowserTurn.current?.();
finishBrowserTurn.current = null;
if (chatThreadDeleted(projectId, threadId)) return;
catchingUp.current = null;
const stop = isAbort || isDisconnect || isError ? "cut" : "finished";
if (stop === "finished") completedMessageId.current = message.id;
// The stream reached the journal's end — cleanly, or through its closing
// error — so the thread now holds that much of it and replays it no more.
journalMark.current = settledMark(streamed.current, message.id, stop) ?? journalMark.current;
// A verdict holds the thread for the user. A detach (this panel closing
// its reader while the engine keeps working) and a dropped connection
// with continuations left leave none, so the turn resumes.
const retrying = isDisconnect && resumed.current < RESUME_LIMIT;
if (isAbort) {
if (userStopped.current) turnSettled.current = "stopped";
} else if (isError && !retrying) {
turnSettled.current = "failed";
}
userStopped.current = false;
setRetryDisconnect(retrying);
setReconnect(retrying ? "idle" : "done");
const stored = readThreads(projectId);
const previous = stored.find((thread) => thread.id === threadId);
const firstUser = finished.find((message) => message.role === "user");
const thread: ChatThread = {
...previous,
id: threadId,
title: (firstUser ? messageText(firstUser) : "").slice(0, 80),
updatedAt: Date.now(),
completedMessageId: completedMessageId.current,
turnSettled: turnSettled.current,
resumed: resumed.current,
messages: recoverSceneCall(finished, threadId, useEditor.getState().projectId === projectId ? useEditor.getState().genvideo : undefined),
sessions: { ...providerSessions.current },
pi: readPiSession(threadId) ?? previous?.pi,
journal: journalMark.current,
};
seenThreadAt.current = thread.updatedAt;
writeThreads(projectId, [thread, ...stored.filter((item) => item.id !== threadId)]);
void projectBackend(projectId).then((backend) => {
if (backend.kind === "cloud") return putCloudThread(backend, projectId, slimForStorage([thread])[0]);
}).catch(() => {});
},
onData: (part) => {
// A coded failure arrives ahead of the error that ends the stream, so
// the error line can read in the page's language.
if (part.type === "data-failure") {
setFailure(part.data as ChatFailure);
}
if (part.type === "data-session") {
const d = part.data as {
sessionKey?: string;
providerSession?: string;
};
if (d.sessionKey) sessionKeyRef.current = d.sessionKey;
if (d.providerSession)
providerSessions.current[provider(modelRef.current)] =
d.providerSession;
}
// The engine's turn is signing off: judge its record and answer with
// the next pass's steer, or none to let it close.
if (part.type === "data-gate" && catchingUp.current !== "finished") {
const { gateId, reply } = part.data as { gateId: string; reply: string };
const gate = engineGateRef.current;
const sessionKey = sessionKeyRef.current;
const signal = toolAbort.current.signal;
if (!sessionKey) return;
void (async () => {
const steer = gate ? await gate.steer(gateId, reply, productionDeps(projectId), signal).catch(() => null) : null;
if (steer === undefined) return;
await answerEngineGate(sessionKey, gateId, steer).catch(() => {});
})();
}
},
onToolCall: ({ toolCall }) => {
// Gemini turns already executed the tool in the transport loop; their
// tool chunks are display-only.
if (clientToolsRef.current) return;
// A finished turn replayed to catch up already ran its tools.
if (catchingUp.current === "finished") return;
// Execute on the editor store, then hand the result back to the
// server-side bridge (which is holding the provider's tool call open).
const sessionKey = sessionKeyRef.current;
const signal = toolAbort.current.signal;
void (async () => {
const post = (payload: Record<string, unknown>) =>
localBackend.fetch("/api/cut/ai/tool-result", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
sessionKey,
toolCallId: toolCall.toolCallId,
...payload,
}),
});
try {
await withChatProject(projectId, async () => {
if (!(await claimEngineTool(sessionKey, toolCall.toolCallId))) return;
signal.throwIfAborted();
beginChatTurn(threadId);
const output = await runAiTool(toolCall.toolName, (toolCall.input ?? {}) as Record<string, unknown>);
engineGateRef.current?.record(toolCall.toolName, output, undefined);
await post({ output });
}, signal);
} catch (err) {
const errorText = err instanceof Error ? err.message : String(err);
engineGateRef.current?.record(toolCall.toolName, undefined, errorText);
await post({ errorText });
}
})();
},
};
useLayoutEffect(() => session.bind({ transport, ...chatEvents }));
const savedScene = useEditor((s) => s.projectId === projectId ? s.genvideo : undefined);
useLayoutEffect(() => {
replayHandler.current = (id, requestId) => {
const lastUser = messages.findLast((m) => m.role === "user");
// A catch-up already matched the journal's reply to the thread's last
// message; a message folded into that turn sits after its ask.
const caughtUp = catchingUp.current !== null && messages.at(-1)?.id === id;
if (requestId && lastUser?.id !== requestId && !caughtUp) return false;
setMessages((current) => current.map((m) => m.id === id ? { ...m, parts: [] } : m));
return true;
};
}, [messages, setMessages]);
const displayMessages = useMemo(
() => visible ? recoverSceneCall(messages, threadId, savedScene) : messages,
[messages, threadId, savedScene, visible],
);
// What the user attached in this thread: an import of one of these needs no
// card of its own under the reply.
const attachedKeys = useMemo(() => attachedRefKeys(displayMessages), [displayMessages]);
// The scene card is part of the conversation, not a pinned banner: it
// renders right under the turn that planned the scene (the newest
// generate_scene call), so later queries and answers read below it in
// order. A run with no such turn in this thread (a resumed or re-homed
// one) falls back to the end of the list.
const sceneAnchorId = useMemo(
() =>
visible ? [...messages].reverse().find((m) =>
m.parts.some((part) => {
const name =
part.type === "dynamic-tool"
? (part as { toolName?: string }).toolName
: part.type.startsWith("tool-")
? part.type.slice(5)
: undefined;
return name === "generate_scene";
}),
)?.id : undefined,
[messages, visible],
);
const browserRunning = useSyncExternalStore(watchBrowserChatTurns, () => browserChatRunning(projectId, threadId), () => false);
// A failed send, worded at render in the page's language.
const [sendError, setSendError] = useState<unknown>(null);
const [failure, setFailure] = useState<ChatFailure | null>(null);
const [requests] = useState(() => new ChatRequests());
const requestPending = useSyncExternalStore(requests.subscribe, requests.snapshot, () => false);
const dispatchChat = useCallback((request: () => Promise<void>) => {
void requests.run(request).catch((error: unknown) => {
setSendError(error);
});
}, [requests, setSendError]);
const busy = requestPending || status === "submitted" || status === "streaming" || browserRunning;
useEffect(() => {
toolAbort.current = new AbortController();
return () => {
toolAbort.current.abort();
// The engine owns the provider run; closing its reader detaches this editor.
if (provider(modelRef.current) !== "gemini") void stop();
closeReplay.current?.();
};
}, [stop]);
useEffect(() => {
onRunningChange(threadId, busy, model);
}, [busy, threadId, model, onRunningChange]);
useEffect(() => {
if (busy) return holdEditorChat(projectId);
}, [busy, projectId]);
// Keep the thread saved (so it shows up in the Threads panel) as it grows.
// A save rereads, re-slims, and rewrites the project's whole stored history —
// synchronous main-thread work that scales with everything the chats have
// ever produced — and a streaming turn changes the chat every chunk, so
// saving on each change ground the editor down as transcripts grew. A
// change arms one save, at most once per THREAD_SAVE_MS, that reads the
// chat as it stands when it lands; turn end, thread switch, and pagehide
// flush it, so nothing is lost. The recorder follows the Chat itself, not
// this component's renders: a screen-locked or background tab keeps
// streaming and running tools while it commits nothing, and its record
// used to stop at the last render before the screen went dark.
const saveThreadNow = useCallback((live: UIMessage[]) => {
if (live.length === 0 || chatThreadDeleted(projectId, threadId)) return;
const firstUser = live.find((m) => m.role === "user");
const stored = readThreads(projectId);
// The pi transcript is held for the recent threads only; a thread whose
// session has been let go reads back as undefined, and writing that would
// erase the tool-call context the stored one still has. What is on disk
// stands until a live session replaces it.
const thread: ChatThread = {
id: threadId,
title: (firstUser ? messageText(firstUser) : "").slice(0, 80),
updatedAt: Date.now(),
messages: recoverSceneCall(live, threadId, useEditor.getState().genvideo),
sessions: { ...providerSessions.current },
completedMessageId: completedMessageId.current,
turnSettled: turnSettled.current,
resumed: resumed.current,
pi: readPiSession(threadId) ?? stored.find((t) => t.id === threadId)?.pi,
journal: journalMark.current,
};
seenThreadAt.current = thread.updatedAt;
writeThreads(projectId, [thread, ...stored.filter((t) => t.id !== threadId)]);
// Cloud projects mirror the thread server-side (debounced while it streams).
queueCloudThreadSave(projectId, slimForStorage([thread])[0]);
}, [projectId, threadId]);
const recorder = useRef<ThreadRecorder | null>(null);
// A thread switch unmounts this session mid-throttle; the page going away
// skips unmounts entirely. Both flush the waiting save.
useEffect(() => {
const record = recordThread(chat, saveThreadNow, THREAD_SAVE_MS);
recorder.current = record;
const onPageHide = () => {
record.flush();
flushCloudThreadSaves(true);
};
window.addEventListener("pagehide", onPageHide);
return () => {
window.removeEventListener("pagehide", onPageHide);
recorder.current = null;
record.stop();
flushCloudThreadSaves();
};
}, [chat, saveThreadNow]);
// Land the final transcript locally and on the server as soon as the turn
// settles instead of waiting out the save throttle.
useEffect(() => {
if (busy) return;
recorder.current?.flush();
flushCloudThreadSaves();
}, [busy]);
// set_interface_language in this thread's turn: the language waits for the
// turn to settle, then the saves land, the account keeps it, and the page
// reloads in it.
const updateLocale = useUpdateLocale();
const pendingLocale = useRef<Locale | null>(null);
useEffect(() => registerLanguageSink(threadId, (next) => {
pendingLocale.current = next;
}), [threadId]);
useEffect(() => {
const next = pendingLocale.current;
if (busy || !next) {
return;
}
pendingLocale.current = null;
void (async () => {
await Promise.all([flushThreadSaves(), flushCloudThreadSaves(), flushEditorSave(projectId)]);
await updateLocale.mutateAsync(next);
if (!switchLocale(next)) {
throw new UserError("language_cookie_blocked", "This browser isn't saving cookies, so the language can't change.");
}
})().catch((error: unknown) => setSendError(error));
}, [busy, projectId, updateLocale]);
useEffect(() => {
const receive = () => {
if (chatThreadDeleted(projectId, threadId)) {
toolAbort.current.abort();
cancelBrowserChat(projectId, threadId);
if (provider(modelRef.current) !== "gemini") void cancelEngineChat(projectId, threadId).catch(() => {});
void stop();
useGenScene.getState().killThread(threadId);
onDeleted(threadId);
return;
}
if (busy) return;
const thread = readThreads(projectId).find((t) => t.id === threadId);
if (!thread || thread.updatedAt <= seenThreadAt.current) return;
seenThreadAt.current = thread.updatedAt;
completedMessageId.current = thread.completedMessageId;
turnSettled.current = thread.turnSettled;
resumed.current = thread.resumed ?? 0;
journalMark.current = thread.journal;
providerSessions.current = { ...thread.sessions };
dropPiSession(threadId);
hydratePiSession(threadId, thread.pi);
// Read back from storage: the copy is not saved back over itself.
const apply = () => setMessages(thread.messages);
if (recorder.current) recorder.current.quietly(apply);
else apply();
};
const unsubscribe = subscribeChatThreads(projectId, receive);
receive();
return unsubscribe;
}, [projectId, threadId, busy, setMessages, stop, onDeleted]);
// While this thread is open its tools tag created assets with it, so
// deleting the thread later can clean them up.
useEffect(() => {
if (!visible) return;
setActiveChatThread(threadId);
return () => setActiveChatThread(null);
}, [threadId, visible]);
// Browser turns can finish after project navigation, so ownership lasts
// until the turn settles.
useEffect(() => {
if (busy) beginChatTurn(threadId);
else endChatTurn(threadId);
}, [busy, threadId]);
// Coalesce every edit the assistant makes in one turn into a single undo
// step, so ⌘Z reverts the whole turn rather than one tool call at a time.
// Each tool call is a step inside it, so the chat's own undo mid-turn takes
// back its last call alone.
useEffect(() => {
if (!busy) return;
useEditor.getState().beginHistoryBatch(CHAT_TURN_LABEL);
return () => useEditor.getState().endHistoryBatch();
}, [busy]);
// Stay glued to the newest message while the user sits at the bottom;
// scrolling up releases the glue until they return. The ResizeObserver
// catches growth the messages effect misses — streamed text reflowing and
// media cards getting their height after load — so answers never end up
// clipped at the bottom.
const scrollRef = useRef<HTMLDivElement>(null);
const pinnedRef = useRef(true);
const onMessagesScroll = () => {
const el = scrollRef.current;
if (el)
pinnedRef.current = el.scrollHeight - el.scrollTop - el.clientHeight < 24;
};
useEffect(() => {
const content = scrollRef.current?.firstElementChild;
if (!content) return;
const ro = new ResizeObserver(() => {
const el = scrollRef.current;
if (el && pinnedRef.current) el.scrollTop = el.scrollHeight;
});
ro.observe(content);
return () => ro.disconnect();
}, [visible]);
useEffect(() => {
const el = scrollRef.current;
if (el && pinnedRef.current) el.scrollTop = el.scrollHeight;
}, [messages, busy, visible]);
// Messages submitted while a turn runs wait here and dispatch one at a time
// as turns settle. Waiting rows persist per thread, so a reload or a trip
// to another project brings them back — paused, so nothing fires on its
// own until the user resumes.
const [initialQueue] = useState(() => readStoredQueue(projectId, threadId));
const [queue, setQueue] = useState<QueuedMessage[]>(initialQueue);
const [queuePaused, setQueuePaused] = useState(initialQueue.length > 0);
const [queueEditing, setQueueEditing] = useState<string | null>(null);
const drainedRef = useRef(false);
// The triage reads the live queue and transcript when its call returns,
// long after the render that started it.
const queueRef = useRef(queue);
const messagesRef = useRef(messages);
const queueEditingRef = useRef(queueEditing);
const runningAsksRef = useRef(runningAsks);
useEffect(() => {
queueRef.current = queue;
messagesRef.current = messages;
queueEditingRef.current = queueEditing;
runningAsksRef.current = runningAsks;
}, [queue, messages, queueEditing, runningAsks]);
/** The ask the running turn is working on. */
const runningAskRef = useRef("");
/** The running Gemini turn's abort signal, for the work a fold does. */
const turnSignalRef = useRef<AbortSignal | undefined>(undefined);
/** How many triage calls are out, and when the last one went, so the
* re-placement cadence counts from every caller's call. A count rather
* than a flag: the send path and the cadence both call, and the first to
* finish must not clear the other's. */
const triagingRef = useRef(0);
const triagedAtRef = useRef(0);
// Where a message sent mid-turn goes. One light-model call places the rows
// against the running ask: a fold is steered into the running turn and
// joins the transcript as its own message, a spawn starts a parallel
// thread, and the rest wait in the tray. Each verdict is checked against
// the live queue before it acts, since a row can be edited, removed, or
// drained while the call is out.
const triage = async (rows: QueuedMessage[]) => {
const ask = runningAskRef.current;
if (!ask) return;
// Placing rows is a judgment; with no credits to judge with they wait in
// the tray for the turn to end.
if (!(await accountJudges(queryClient))) return;
triagingRef.current += 1;
triagedAtRef.current = Date.now();
try {
await placeRows(rows, ask);
} finally {
triagingRef.current -= 1;
}
};
const placeRows = async (rows: QueuedMessage[], ask: string) => {
const last = messagesRef.current.at(-1);
const progress =
last?.role === "assistant"
? toolProgress(last.parts as unknown as { type: string; state?: string; toolName?: string }[])
: [];
const elsewhere = Object.entries(runningAsksRef.current)
.filter(([id]) => id !== threadId)
.map(([, text]) => text);
const waiting = queueRef.current
.filter((m) => m.status === "queued" && !rows.some((r) => r.id === m.id))
.map((m) => ({ id: m.id, text: m.text }));
const verdicts = await triageQueuedMessages({
anchor: { ask, progress, elsewhere },
waiting,
rows: rows.map((r) => ({ id: r.id, text: r.text })),
deps: productionDeps(projectId, turnSignalRef.current),
});
for (const row of rows) {
const verdict = verdicts.get(row.id);
const current = queueRef.current.find((m) => m.id === row.id);
if (
!current ||
current.status !== "queued" ||
current.text !== row.text ||
queueEditingRef.current === row.id
)
continue;
const live = liveRefs(row.attachments);
const text = dueText(current);
if (verdict === "spawn") {
const id = onSpawn({ text, attachments: live, model });
setQueue((q) => q.map((m) => (m.id === row.id ? { ...m, status: "spawned", threadId: id } : m)));
} else if (verdict === "fold" && (provider(model) !== "gemini" || cutChatLive(threadId))) {
setQueue((q) => q.map((m) => (m.id === row.id ? { ...m, status: "folding" } : m)));
const folded =
provider(model) === "gemini"
? await foldIntoCutChat({
threadId,
text,
attachments: live,
deps: productionDeps(projectId, turnSignalRef.current),
})
: await foldIntoEngineChat(projectId, threadId, {
text,
attachments: live,
context: productionDeps(projectId).buildContext(),
}).catch(() => false);
if (!folded) {
setQueue((q) => q.map((m) => (m.id === row.id ? { ...m, status: "queued" } : m)));
continue;
}
setQueue((q) => q.filter((m) => m.id !== row.id));
setMessages((msgs) => {
const user: UIMessage = {
id: crypto.randomUUID(),
role: "user",
parts: [{ type: "text", text }],
...(live.length > 0 && { metadata: { attachments: live } }),
};
const tail = msgs.at(-1);
return tail?.role === "assistant" ? [...msgs.slice(0, -1), user, tail] : [...msgs, user];
});
}
}
};
const triageRef = useRef(triage);
useEffect(() => {
triageRef.current = triage;
});
// A row is placed against the tools the turn had run when it was sent, and
// the turn keeps working — so that reading goes stale. A row that read
// "unclear, hold it" is a plain fold once the work it was waiting on lands.
// The tray is therefore placed again on a cadence for as long as the turn
// runs, which is the difference between a refinement joining the live turn
// and landing a minute later. A turn can sit inside one long tool call
// with nothing about it changing, so the timer re-arms itself rather than
// waiting on a render. The judgment is a few hundred tokens and a quarter
// second; the cadence and the in-flight count keep it to one call at a time.
useEffect(() => {
if (!busy || queuePaused || readOnly) return;
const { queueRetriageMs } = CUT_JUDGE;
if (queueRetriageMs <= 0) return;
const rows = () => queueRef.current.filter((m) => m.status === "queued" && m.id !== queueEditingRef.current);
let timer: ReturnType<typeof setTimeout>;
const tick = () => {
const waiting = rows();
if (
dueForRetriage({
waiting: waiting.length,
cadenceMs: queueRetriageMs,
inFlight: triagingRef.current > 0,
lastAt: triagedAtRef.current,
now: Date.now(),
})
)
void triageRef.current(waiting);
timer = setTimeout(tick, queueRetriageMs);
};
timer = setTimeout(tick, Math.max(0, triagedAtRef.current + queueRetriageMs - Date.now()));
return () => clearTimeout(timer);
}, [busy, queuePaused, readOnly]);
// The running agent can park a message here itself (queue_message): the
// row goes out as its own turn after the running one settles.
useEffect(
() =>
registerQueueSink(threadId, (text) =>
setQueue((q) => [...q, { id: crypto.randomUUID(), text, attachments: [], status: "queued" }]),
),
[threadId],
);
// A waiting row keeps pointing at the same moment of the video while the
// running turn edits the cut under it. Each row's marks are re-read against
// the live clips and its `@1:34` tokens rewritten, so the tray shows — and
// the dispatch sends — the time that moment sits at now. The row being
// edited is left alone; its text belongs to the editor until it commits.
const docClips = useEditor((s) => s.clips);
const docAudio = useEditor((s) => s.audioClips);
useEffect(() => {
// eslint-disable-next-line react-hooks/set-state-in-effect -- the rows react to the cut moving; the write is skipped unless a token's time actually changed
setQueue((q) => {
let moved = false;
const next = q.map((m) => {
if (!m.times?.length || m.status !== "queued" || m.id === queueEditing) return m;
const synced = syncTimes(m.text, m.times, { clips: docClips, audioClips: docAudio });
if (synced.text === m.text) return m;
moved = true;
return { ...m, text: synced.text, times: synced.marks };
});
return moved ? next : q;
});
}, [docClips, docAudio, queueEditing]);
// Height of the floating stack above the composer (warning tabs + queue
// tray); the messages pad their bottom by it so the newest message can
// scroll out from behind the stack.
const overlayRef = useRef<HTMLDivElement>(null);
const [overlayH, setOverlayH] = useState(0);
useEffect(() => {
const el = overlayRef.current;
if (!el) return;
const ro = new ResizeObserver(() => setOverlayH(el.offsetHeight));
ro.observe(el);
return () => ro.disconnect();
}, [readOnly, visible]);
const currentAvailable = info
? info.providers[provider(model)]?.available !== false
: true;
const unavailableMessage = (): string => {
const p = provider(model);
if (p === "gemini") return t("signInForGemini");
const note = info?.providers[p]?.note?.trim();
const name = providerLabel(p, t);
return note ? t("providerNote", { provider: name, note }) : t("providerUnavailable", { provider: name });
};
const send = (text: string) => {
// Inline @mentions attach their assets alongside the dropped chips. The
// message keeps the raw tokens — they render as interactive chips and the
// model reads the handle↔asset mapping from <attached_assets>.
// Moments are re-read here too: the cut can have moved since the token was
// written, and a token typed by hand picks up its mark now.
const doc = useEditor.getState();
const cut = { clips: doc.clips, audioClips: doc.audioClips };
// A `@here` still standing in the text — typed and sent in one breath,
// never completed by a following character — takes the moment now.
const written = resolvePlayheadTokens(text.trim(), previewAt(), cut);
const synced = syncTimes(
written?.text ?? text.trim(),
[...times, ...(written?.marks ?? [])],
cut,
);
const body = synced.text;
const { refs: all } = collectRefs(body, attachments, candidates);
if (!body && all.length === 0) return;
// An unavailable provider surfaces as an error in the thread at send time
// rather than a standing footer note: say why nothing sent, then stop.
if (!currentAvailable) {
setSendError(unavailableMessage());
return;
}
const queued = busy || requests.snapshot();
track("chat_message_sent", { provider: provider(currentModel()), attachments: all.length, queued });
reportChatSent();
if (queued) {
// A paused queue stays paused: parked rows fire only from the tray's
// resume button, never as a side effect of submitting something new.
// With the queue live, the triage places the row right away.
const row: QueuedMessage = {
id: crypto.randomUUID(),
text: body,
attachments: all,
status: "queued",
...(synced.marks.length > 0 && { times: synced.marks }),
};
setQueue((q) => [...q, row]);
setInput("");
setAttachments([]);
setTimes([]);
pinnedRef.current = true;
if (!queuePaused) void triageRef.current([row]);
return;
}
clearError();
setSendError(null);
// A direct send starts a new turn; a row still marked running belongs to
// the settled one — sweep it, or cross it out under a frozen view.
setQueue((q) =>
q.flatMap((m) =>
m.status !== "running"
? [m]
: queueEditing === null
? []
: [{ ...m, status: "done" as const }],
),
);
pinnedRef.current = true;
runningAskRef.current = body;
dispatchChat(() => sendMessage({
text: body,
...(all.length > 0 && { metadata: { attachments: all } }),
}));
setInput("");
setAttachments([]);
setTimes([]);
};
// A turn the page lost picks itself back up. With an engine journal the
// panel reconnects and replays it; when there is none, or the replay shows
// a tool that needed this page, the model gets a continuation quoting the
// ask. The count rides the thread, so a reload loop ends at RESUME_LIMIT;
// a turn the user stopped, one that failed outright, and one left longer
// than RESUME_WINDOW_MS all wait for them.
const projectLoaded = useEditor((s) => s.projectId === projectId && s.loaded);
const signedIn = useGenerate((s) => s.signedIn);
const resumeOwed = useCallback(
() => (resumed.current < RESUME_LIMIT ? unfinishedAsk(messages, completedMessageId.current, turnSettled.current) : undefined),
[messages],
);
// A dropped connection rests briefly; clearing its error settles the
// status to ready, and the resume below takes it from there.
useEffect(() => {
if (status !== "error" || !retryDisconnect) return;
const timer = window.setTimeout(() => {
setRetryDisconnect(false);
clearError();
}, RESUME_RETRY_MS);
return () => window.clearTimeout(timer);
}, [status, retryDisconnect, clearError]);
useEffect(() => {
if (status !== "ready" || busy || requests.snapshot() || readOnly || !projectLoaded || !currentAvailable) return;
if (provider(model) === "gemini" ? signedIn !== true : !info) return;
if (Date.now() - seenThreadAt.current > RESUME_WINDOW_MS) return;
const ask = resumeOwed();
if (!ask) return;
if (provider(model) !== "gemini") {
if (reconnect === "pending") return;
if (reconnect === "idle") {
// eslint-disable-next-line react-hooks/set-state-in-effect -- the reconnect reacts to the thread settling unfinished; the state gates it to one attempt
setReconnect("pending");
dispatchChat(() => resumeStream());
return;
}
}
// Another tab holding this thread may have resumed it already.
const stored = readThreads(projectId).find((t) => t.id === threadId);
if ((stored?.resumed ?? 0) > resumed.current) {
resumed.current = stored?.resumed ?? 0;
return;
}
resumed.current += 1;
const refs = ((ask.metadata as { attachments?: unknown[] } | undefined)?.attachments ?? [])
.map(normalizeRef)
.filter((r): r is AssetRef => r !== null);
const live = liveRefs(refs);
setSendError(null);
pinnedRef.current = true;
// The continuation finishes the job in the lost turn's place, so the
// transcript drops that turn's failure before it goes out: the cut-off
// reply keeps the calls that landed and nothing else.
setMessages((current) => scrubLostTurn(current, ask.id));
const metadata: ResumeMetadata = { resume: true, ...(live.length > 0 ? { attachments: live } : {}) };
runningAskRef.current = messageText(ask);
dispatchChat(() => sendMessage({ text: resumePrompt(ask), metadata }));
}, [status, busy, requests, dispatchChat, readOnly, projectLoaded, currentAvailable, model, signedIn, info, reconnect, resumeOwed, resumeStream, setMessages, sendMessage, projectId, threadId]);
// A thread that ended but whose saved copy falls short of the engine's
// journal of that turn — the page lost renders or saves while it ran, or
// the copy predates journal marks — replays the journal to catch up. The
// engine is asked when the thread opens and whenever the tab comes back
// into view; an unfinished turn takes the reconnect above instead, which
// replays the same journal.
const [journal, setJournal] = useState<(JournalMark & { running: boolean }) | null>(null);
const engineThread = provider(model) !== "gemini" && !!info && !readOnly;
useEffect(() => {
if (!engineThread) return;
let alive = true;
const peek = () => {
if (document.hidden) return;
void peekEngineChat(projectId, threadId).then((found) => {
if (alive) setJournal(found);
});
};
peek();
document.addEventListener("visibilitychange", peek);
return () => {
alive = false;
document.removeEventListener("visibilitychange", peek);
};
}, [engineThread, projectId, threadId]);
useEffect(() => {
if (!journal || status !== "ready" || busy || requests.snapshot() || !projectLoaded || resumeOwed()) return;
// eslint-disable-next-line react-hooks/set-state-in-effect -- the peek answered once; clearing it gates the catch-up to one replay
setJournal(null);
if (!journalAhead(messages, journalMark.current, journal)) return;
catchingUp.current = journal.running ? "running" : "finished";
dispatchChat(() => resumeStream());
}, [journal, status, busy, requests, projectLoaded, resumeOwed, messages, dispatchChat, resumeStream]);
// Drain the queue as turns settle. drainedRef keeps it to one dispatch per
// ready period — the effect re-runs when the queue changes before useChat
// surfaces the new turn's status. A dispatched row stays in the queue as
// running until the next dispatch settles it: it leaves the tray, or stays
// crossed out in place while an open row edit has the view frozen. The edit
// skips only its own row — the rest keep draining. An unavailable provider
// holds the drain the same way it stops a direct send; the tray reads as
// paused until the user switches models or the provider comes back.
// Defined after the settle-flush effect above, so the finished transcript
// saves before the next turn starts mutating messages.
useEffect(() => {
if (status !== "ready") {
drainedRef.current = false;
return;
}
if (busy || requests.snapshot() || drainedRef.current || queuePaused || !currentAvailable) return;
// A thread still owed a reply resumes that first.
if (resumeOwed()) return;
const next = queue.find(
(m) => m.status === "queued" && m.id !== queueEditing,
);
if (!next) return;
drainedRef.current = true;
// eslint-disable-next-line react-hooks/set-state-in-effect -- the drain reacts to the chat stream settling; drainedRef caps it at one guarded dispatch per ready period
setQueue((q) =>
q.flatMap((m) => {
if (m.id === next.id) return [{ ...m, status: "running" as const }];
if (m.status !== "running") return [m];
return queueEditing === null
? []
: [{ ...m, status: "done" as const }];
}),
);
setSendError(null);
pinnedRef.current = true;
// A restored row's attachments may point at assets deleted since it was
// parked (or at file blobs that died with the last page); dropping the
// dead refs keeps the message from claiming media it can't deliver.
const live = liveRefs(next.attachments);
const text = dueText(next);
runningAskRef.current = text;
dispatchChat(() => sendMessage({
text,
...(live.length > 0 && { metadata: { attachments: live } }),
}));
// The rows behind it are placed against the turn that just started:
// related ones fold into it, independent ones spawn, the rest wait.
const rest = queue.filter((m) => m.status === "queued" && m.id !== next.id && m.id !== queueEditing);
if (rest.length > 0) void triageRef.current(rest);
}, [status, busy, requests, dispatchChat, queue, queuePaused, queueEditing, currentAvailable, resumeOwed, sendMessage]);
// A spawned thread sends its seed the moment it mounts and hands the seed
// back, so the parent counts it as running from then on.
const seedSent = useRef(false);
useEffect(() => {
if (!seed || seedSent.current || readOnly || busy || requests.snapshot()) return;
seedSent.current = true;
onSeedTaken(threadId, model);
if (!currentAvailable) return;
runningAskRef.current = seed.text;
pinnedRef.current = true;
dispatchChat(() => sendMessage({
text: seed.text,
...(seed.attachments.length > 0 && { metadata: { attachments: seed.attachments } }),
}));
}, [seed, readOnly, busy, requests, currentAvailable, onSeedTaken, threadId, model, dispatchChat, sendMessage]);
// Mirror the waiting rows to storage as they change; an empty queue clears
// its slot.
useEffect(() => {
if (readOnly) return;
writeQueuedRows(projectId, threadId, queue.filter((m) => m.status === "queued"));
}, [queue, projectId, threadId, readOnly]);
// An errored turn holds the queue for review — the drain only fires on
// 'ready', and the tray reads the error as paused. Resuming clears the
// error, which settles status back to ready and releases the drain. An
// unavailable provider holds it the same way.
const queueHeld = queuePaused || status === "error" || !currentAvailable;
// The tray shows only while a row is waiting; settled running rows linger
// in the array until the next dispatch sweeps them, and alone they are
// nothing to show.
const trayVisible = queue.some(
(m) => m.status === "queued" || m.status === "folding" || m.status === "spawned",
);
// The saved model's provider may be uninstalled, or its account may not run
// it (a ChatGPT login without GPT-6.1 Sol) — the picker hides it, so move to
// a model this Mac and account run rather than sit on one that fails.
useEffect(() => {
if (!visible || !info) return;
const runnable = runnableChatModel(model, info.providers);
if (runnable && runnable !== model) onModelChange(runnable);
}, [info, model, onModelChange, visible]);
const outOfCredits = useOutOfCredits((s) => s.out);
useCreditsRecheck();
if (!visible) return null;
return (
<div
ref={attachTarget}
{...targetProps}
className="relative flex min-h-0 flex-1 flex-col"
>
<ScrollArea
className="min-h-0 flex-1"
// Scene cards and the pinning logic reach the scrolling element by
// class and ref, so both ride the viewport.
viewportClassName="ai-messages"
viewportRef={scrollRef}
onViewportScroll={onMessagesScroll}
contentClassName="px-3.5 py-3"
>
{/* Single wrapper so the ResizeObserver sees all content growth. The
bottom padding matches the floating stack over the composer, so
the newest message can scroll out from behind it. */}
<div style={{ paddingBottom: overlayH }}>
{messages.length === 0 && !hasSceneRun && (
<div className="flex flex-col gap-3 pt-6">
<p className="text-[12.5px] leading-relaxed text-muted-foreground">
{t("intro")}
</p>
<div className="flex flex-wrap gap-1.5">
{SUGGESTIONS.map((id) => (
<button
key={id}
className="ai-suggestion rounded-full border border-border px-2.5 py-1 text-[11.5px] text-muted-foreground transition-colors hover:border-input hover:text-foreground"
onClick={() => send(t(`suggestions.${id}`))}
>
{t(`suggestions.${id}`)}
</button>
))}
</div>
</div>
)}
{displayMessages.map((m) => (
<Fragment key={m.id}>
<MessageView message={m} attachedKeys={attachedKeys} />
{m.id === sceneAnchorId && <SceneCard threadId={threadId} />}
</Fragment>
))}
{sceneAnchorId === undefined && <SceneCard threadId={threadId} />}
<ThreadRenders threadId={threadId} />
{busy && (
<div className="ai-busy mt-1 flex items-center gap-1.5 text-[11.5px] text-muted-foreground">
<CircleDashed className="size-3 animate-spin" /> {t("workingEllipsis")}{" "}
<LiveElapsed />
</div>
)}
{((error && !retryDisconnect) || sendError !== null) && (
<div className="ai-error mt-2 flex items-start gap-2 rounded-lg bg-red-50 px-2.5 py-2 text-[11.5px] leading-relaxed text-red-700">
<TriangleAlert className="mt-0.5 size-3.5 shrink-0" />
<span className="min-w-0 break-words">
{error ? (
<HostedErrorText
error={failure?.message === error.message ? chatError(new UserError(failure.code, failure.message, failure.params)) : error.message}
link={false}
/>
) : (
chatError(sendError)
)}
</span>
</div>
)}
</div>
</ScrollArea>
{!readOnly && (
<div className="shrink-0 px-2.5 pb-2.5">
<div className="relative">
{/* The stack above the composer — warning tabs first, then the
queue tray — floats over the messages, which scroll behind it;
the messages pad their bottom by its measured height so the
newest message still lands in view. Its bottom few pixels slide
under the composer box, which paints over them, so the bottom
piece meets the border even where the box's corner radius
curves away. */}
<div
ref={overlayRef}
className="pointer-events-none absolute inset-x-0 bottom-full z-10 -mb-1 flex flex-col"
>
{outOfCredits && (
<button
type="button"
className={cn(
"ai-credits-tab pointer-events-auto mx-1.5 flex cursor-pointer items-center gap-1.5 rounded-t-lg border border-b-0 border-amber-500/30 bg-amber-50 px-3 pt-1.5 pb-2.5 text-[11px] text-amber-800",
// With the tray below, the tab tucks under it the same way
// the stack tucks under the composer.
trayVisible && "-mb-1",
)}
onClick={openCreditsWall}
>
<TriangleAlert className="size-3 shrink-0" />
<span>
{t.rich("insufficientCredits", {
link: (chunks) => <span className="font-medium underline">{chunks}</span>,
})}
</span>
</button>
)}
{!outOfCredits && notice && (
// The credits tab's folder shape, borrowed for transient
// refusals; the credits warning outranks it when both apply.
<div
className={cn(
"ai-notice-tab pointer-events-auto mx-1.5 flex items-center gap-1.5 rounded-t-lg border border-b-0 border-amber-500/30 bg-amber-50 px-3 pt-1.5 pb-2.5 text-[11px] text-amber-800",
trayVisible && "-mb-1",
)}
>
<TriangleAlert className="size-3 shrink-0" />
<span>{notice}</span>
</div>
)}
{!outOfCredits && !notice && saveFailure && (
<div
className={cn(
"ai-save-failure-tab pointer-events-auto mx-1.5 flex items-center gap-1.5 rounded-t-lg border border-b-0 border-amber-500/30 bg-amber-50 px-3 pt-1.5 pb-2.5 text-[11px] text-amber-800",
trayVisible && "-mb-1",
)}
>
<TriangleAlert className="size-3 shrink-0" />
<span>{chatError(saveFailure)}</span>
</div>
)}
{trayVisible && (
<div className="pointer-events-auto">
<ComposerQueue
items={queue}
paused={queueHeld}
busy={busy}
editingId={queueEditing}
onEditingChange={(id) => {
setQueueEditing(id);
// Closing the freeze sweeps out the crossed-out rows it kept
// around for display.
if (id === null)
setQueue((q) => q.filter((m) => m.status !== "running" && m.status !== "done"));
}}
onCommitEdit={(id, text) =>
setQueue((q) =>
q.map((m) => {
if (m.id !== id) return m;
// A rewrite can drop a moment or type a new one; re-read
// the row's marks off the text it now carries.
const synced = syncTimes(text.trim() || m.text, m.times ?? [], {
clips: docClips,
audioClips: docAudio,
});
return { ...m, text: synced.text, times: synced.marks };
}),
)
}
onRemove={(id) => setQueue((q) => q.filter((m) => m.id !== id))}
onOpen={(id) => {
setQueue((q) => q.filter((m) => m.threadId !== id));
onOpenThread(id);
}}
onReorder={(ids) =>
setQueue((q) => {
// The tray only shows part of the queue, so the dropped
// order refills the slots those rows already occupied and
// everything else stays where it is.
const byId = new Map(q.map((m) => [m.id, m]));
const landed = ids.filter((id) => byId.has(id));
const moved = new Set(landed);
let next = 0;
return q.map((m) =>
moved.has(m.id) ? (byId.get(landed[next++]) ?? m) : m,
);
})
}
onTogglePaused={() => {
if (status === "error") {
clearError();
setQueuePaused(false);
} else {
setQueuePaused((p) => !p);
}
}}
/>
</div>
)}
</div>
<div
ref={composerBoxRef}
data-tour="chat"
className={cn(
// z above the floating stack so the box paints over its tucked
// bottom edge. Dark mode lifts the opaque fill a step above the
// panel's card surface so the field reads as raised.
"relative z-10 rounded-xl border bg-background transition-colors dark:bg-[color-mix(in_oklab,var(--card),white_5%)]",
dropActive
? "border-[#0a84ff] ring-2 ring-[#0a84ff]/30"
: fileDropHint
? "border-[#0a84ff]/45 ring-2 ring-[#0a84ff]/15"
: "border-input focus-within:border-ring",
)}
>
{dropActive && (
<div className="px-3 pt-2 text-[11.5px] font-medium text-[#0a84ff]">
{t("dropToAttach")}
</div>
)}
{mic.state === "idle" ? (
<>
<ChipsReveal open={attachments.length > 0 || incoming}>
<RefChips
refs={attachments}
onRemove={(ref) =>
setAttachments((p) => p.filter((x) => !sameRef(x, ref)))
}
onUpdate={(ref) =>
setAttachments((p) => p.map((x) => (sameRef(x, ref) ? ref : x)))
}
className="px-2.5 pt-2.5"
// Chip-sized spacer riding the chip row while a frame is
// in flight: it claims the arriving chip's exact slot —
// opening the row, or wrapping a fresh one when the row
// is full — so the space is ready before the landing.
trailing={incoming ? <div className="size-14" /> : undefined}
/>
</ChipsReveal>
<MentionTextarea
className="ai-input max-h-56 w-full resize-none overflow-y-auto bg-transparent px-3 pt-2 text-[12.5px] leading-relaxed outline-none placeholder:text-muted-foreground/70"
rows={5}
autoGrow
placeholder={t("composerPlaceholder")}
value={input}
onChange={setInput}
candidates={candidates}
submitKey="enter"
menuSide="top"
inputRef={composerRef}
onSubmit={() => send(input)}
attachedRefs={attachments}
onUpsertRef={(ref) => setAttachments((p) => upsertRef(p, ref))}
onPasteFiles={attachFiles}
onRemoveLastRef={() => setAttachments((p) => p.slice(0, -1))}
onInsertTime={(mark) => setTimes((p) => [...p, mark])}
/>
<div className="flex items-center gap-1 px-1.5 pb-1.5">
<ModelSelector
info={info}
model={model}
onSelect={onModelChange}
/>
<div className="flex-1" />
{caps.liveMic && (
<Button
type="button"
variant="ghost"
size="sm"
className="ai-mic text-muted-foreground"
title={t("dictate")}
disabled={busy}
onClick={() => void mic.start()}
>
<Mic className="size-3.5" />
</Button>
)}
{busy ? (
<Button
variant="outline"
size="sm"
className="ai-stop"
title={t("stop")}
onClick={() => {
// stop() settles to 'ready', which the drain reads as
// a clean finish — park the queue here so it stays put
// for review. An empty queue has nothing to park, and
// pausing it anyway would leave a stale flag.
if (queue.some((m) => m.status === "queued"))
setQueuePaused(true);
userStopped.current = true;
toolAbort.current.abort();
if (provider(model) !== "gemini") void cancelEngineChat(projectId, threadId).catch((error) => showNotice(chatError(error)));
cancelBrowserChat(projectId, threadId);
void stop();
}}
>
<Square className="size-3" />
</Button>
) : (
<Button
size="sm"
className="ai-send"
title={t("sendShortcut", { key: "Enter" })}
disabled={!input.trim() && attachments.length === 0}
onClick={() => send(input)}
>
<ArrowUp className="size-3.5" />
</Button>
)}
</div>
</>
) : (
<DictationBody text={input} mic={mic} />
)}
</div>
</div>
{mic.error && (
<p className="mt-1.5 px-1 text-[10.5px] leading-relaxed text-amber-700">
{chatError(mic.error)}
</p>
)}
</div>
)}
</div>
);
}
/** Asset card inside a sent message — click to jump back to the original
* asset, double-click to expand, drag onto the timeline, "…" menu for more
* actions. */
function MessageAssetCard({ asset }: { asset: AssetRef }) {
const t = useTranslations("chat.panel");
// The reveal waits out the double-click window, so expanding doesn't also
// jump the side panel to the asset.
const clickTimer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(clickTimer.current), []);
// ⌘C over the card copies what it stands for, the way its drag carries it.
const copyRef = useRefCopy(() => [asset]);
return (
<div
ref={copyRef}
className={cn(
"ai-msg-asset group/chip relative",
// Audio gets the wide timeline-pill treatment; the row still wraps
// inside the message's max width.
asset.kind === "audio" ? "w-44 max-w-full" : "w-16",
)}
>
<button
className="flex w-full flex-col gap-1 text-left"
title={t("assetCardHint", { name: asset.name })}
draggable
onDragStart={(e) => {
// Project assets keep the timeline-placement payload; the ref rides
// along either way (chat, creators), from the card's own data so it
// survives the asset leaving the project.
if (asset.scope === "project") setAssetDragData(e, asset.id);
setRefDragData(e, asset);
}}
onClick={() => {
window.clearTimeout(clickTimer.current);
clickTimer.current = window.setTimeout(() => revealRef(asset), 250);
}}
onDoubleClick={() => {
window.clearTimeout(clickTimer.current);
useLightbox.getState().open(lightboxItemFromRef(asset));
}}
>
<RefThumb
item={asset}
className={cn(
asset.kind === "audio" ? "h-12 w-full" : "size-16",
"transition-colors group-hover/chip:border-input",
)}
/>
<span className="w-full truncate text-[10px] text-muted-foreground">
{asset.name}
</span>
</button>
<DropdownMenu>
<DropdownMenuTrigger
render={
<button
aria-label={t("assetOptions")}
className="absolute top-1 right-1 grid size-5 place-items-center rounded-md bg-black/55 text-white opacity-0 transition-opacity group-hover/chip:opacity-100 hover:bg-black/75"
/>
}
>
<Ellipsis className="size-3" />
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-40">
<DropdownMenuItem
onClick={() =>
useLightbox.getState().open(lightboxItemFromRef(asset))
}
>
<Maximize2 /> {t("expand")}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => window.open(asset.url, "_blank", "noopener")}
>
<ExternalLink /> {t("openFile")}
</DropdownMenuItem>
{asset.scope === "project" && (
<DropdownMenuItem
onClick={() => {
const s = useEditor.getState();
const full = s.assets.find((a) => a.id === asset.id);
if (!full || !s.projectId) return;
void saveAssetToLibrary(s.projectId, full).catch(() => {
// Library write failed; nothing to roll back.
});
}}
>
<FolderPlus /> {t("addToLibrary")}
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}
/** Copy-to-clipboard affordance revealed on message hover. */
function MessageCopy({ text }: { text: string }) {
const t = useTranslations("chat.panel");
const [copied, setCopied] = useState(false);
if (!text) return null;
return (
<button
aria-label={t("copyMessage")}
title={t("copy")}
className={cn(
"ai-msg-copy",
cardIconButton,
"opacity-0 group-hover:opacity-100",
)}
onClick={() => {
void navigator.clipboard.writeText(text).then(() => {
setCopied(true);
setTimeout(() => setCopied(false), 1500);
});
}}
>
{copied ? (
<Check className="size-3.5 text-emerald-600" />
) : (
<Copy className="size-3.5" />
)}
</button>
);
}
/** Memoized per message: a streaming turn replaces `messages` every chunk,
* and only the growing message should re-render — settled ones hold whole
* asset-card subtrees. */
// How long each tool call ran, tracked across this session's renders (keyed by
// tool-call id). We stamp the start the first time a call renders while still
// running and the end when it settles, so the chip can show its duration.
const toolTimes = new Map<
string,
{ start: number; end?: number; sawRunning: boolean }
>();
// The map lives for the page; long sessions evict the oldest settled entries
// (their chips have already captured the duration they show).
const TOOL_TIMES_CAP = 500;
function toolDuration(id: string | undefined, settled: boolean): string | null {
if (!id) return null;
let t = toolTimes.get(id);
if (!t) {
if (toolTimes.size >= TOOL_TIMES_CAP) {
for (const key of toolTimes.keys()) {
if (toolTimes.size < TOOL_TIMES_CAP) break;
if (toolTimes.get(key)?.end !== undefined) toolTimes.delete(key);
}
}
t = { start: Date.now(), sawRunning: !settled };
toolTimes.set(id, t);
}
if (settled && t.end === undefined) t.end = Date.now();
// Null for a call first seen already-done (e.g. loaded on reload): its real
// start is unknown, so a "0:00" would lie.
return t.sawRunning && t.end !== undefined
? formatDuration(t.end - t.start)
: null;
}
/** Live clock on a still-running tool chip. Its own component so the ticking
* re-render stays inside the chip — MessageView is memoized and only
* re-renders on stream chunks, which stop arriving while a tool runs. */
function RunningToolClock({ start }: { start: number }) {
const elapsed = useElapsed(start);
return elapsed ? (
<span className="ml-auto tabular-nums text-[10px]">{elapsed}</span>
) : null;
}
// Markdown for assistant chat text — the shared base plus a compact inline
// `code` style. Defined once so the answer body and the reasoning body render
// identically.
const chatMarkdownComponents: Components = {
...baseMarkdownComponents,
code: (p) => (
<code
className="rounded bg-muted px-1 py-px font-mono text-[11px] break-words whitespace-pre-wrap"
{...p}
/>
),
pre: (p) => (
<pre
className="my-1.5 max-w-full overflow-x-auto rounded-md bg-muted/70 p-2 text-[11px] leading-relaxed whitespace-pre-wrap break-words"
{...p}
/>
),
};
// The model's replayed context wraps bookkeeping in tag blocks (<tools_ran>,
// <editor_state>, …), and a reply sometimes mimics one verbatim. The blocks are
// input-side scaffolding, so any that reach assistant text get stripped wherever
// that text leaves the transcript: render, copy, and storage. Three passes:
// closed blocks anywhere; then a still-open tag swallows through end-of-string
// only when it starts a line — a mimicked block always does, and this covers a
// block arriving across stream deltas without eating a mid-sentence mention
// like `<editor_state>`; then stray closers.
const TAG_NAMES = "tools_ran|turn_ledger|editor_state|attached_assets";
const CLOSED_BLOCK = new RegExp(`<(${TAG_NAMES})>[^]*?</\\1>`, "g");
const OPEN_TAIL = new RegExp(`(?:^|\\n)[ \\t]*<(?:${TAG_NAMES})>[^]*$`);
const STRAY_CLOSE = new RegExp(`</(?:${TAG_NAMES})>`, "g");
export function sanitizeAssistantText(text: string): string {
if (!text.includes("<")) return text;
return text.replace(CLOSED_BLOCK, "").replace(OPEN_TAIL, "").replace(STRAY_CLOSE, "").trim();
}
// Some models verbalize their chain-of-thought inline in the reply, tagging
// each block with a `NN_thought` marker (e.g. `96_thought The user wants…`).
// It lands in a plain text part; splitting on the marker lets the reasoning
// collapse behind a disclosure instead of flooding the conversation.
const THOUGHT_MARKER = /(?<!\w)\d+_thought\b[ \t]*/g;
type TextSegment = { kind: "text" | "thought"; text: string };
function splitThoughtSegments(text: string): TextSegment[] {
const markers = [...text.matchAll(THOUGHT_MARKER)];
if (markers.length === 0) return [{ kind: "text", text }];
const segments: TextSegment[] = [];
const firstAt = markers[0].index ?? 0;
const lead = text.slice(0, firstAt);
if (lead.trim()) segments.push({ kind: "text", text: lead });
// Each marker opens a thought that runs to the next marker (or the end):
// the reasoning is the whole tail of the part, and any real answer arrives
// as its own later, unmarked part.
markers.forEach((m, idx) => {
const start = (m.index ?? 0) + m[0].length;
const end = idx + 1 < markers.length ? (markers[idx + 1].index ?? text.length) : text.length;
const body = text.slice(start, end);
if (body.trim()) segments.push({ kind: "thought", text: body });
});
return segments;
}
/** A pulled post's own words, clamped to a few lines when the source ran
* long, with a chevron to unfold the full text in place. */
function SourceQuote({ text }: { text: string }) {
const t = useTranslations("chat.panel");
const [expanded, setExpanded] = useState(false);
const [clamped, setClamped] = useState(false);
const bodyRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = bodyRef.current;
if (!el) return;
setClamped(el.scrollHeight > el.clientHeight + 1);
}, [text]);
return (
<blockquote className="border-l-2 border-border pl-3 text-[12.5px] leading-relaxed text-muted-foreground">
<div
ref={bodyRef}
className={cn(
"break-words whitespace-pre-wrap",
!expanded && "line-clamp-4",
)}
>
{text}
</div>
{(clamped || expanded) && (
<button
type="button"
aria-label={expanded ? t("collapseSource") : t("expandSource")}
onClick={() => setExpanded((v) => !v)}
className="mt-1 flex cursor-pointer items-center rounded text-muted-foreground/70 transition-colors hover:text-foreground"
>
<ChevronDown
className={cn("size-3.5 transition-transform", expanded && "rotate-180")}
/>
</button>
)}
</blockquote>
);
}
type ToolPartView = {
type: string;
toolName?: string;
toolCallId?: string;
state: string;
input?: unknown;
output?: unknown;
errorText?: string;
};
const isToolPartType = (t: string) => t.startsWith("tool-") || t === "dynamic-tool";
const toolPartName = (p: ToolPartView) => p.toolName ?? p.type.slice(5);
/** One tool chip. `parts` holds a single call, or a run of settled same-name,
* same-outcome calls collapsed into one counted chip — nine add_title runs
* read as one row. The disclosure shows every call's payload. */
function ToolChipGroup({ parts }: { parts: ToolPartView[] }) {
const p = parts[parts.length - 1];
const name = toolPartName(p);
const failed = p.state === "output-error";
const done = p.state === "output-available";
const single = parts.length === 1;
const took = single ? toolDuration(p.toolCallId, done || failed) : null;
// A call still running shows a live clock from its observed start (settled
// chips show `took`; a call first seen already-done shows neither — its
// real start is unknown).
const runningSince =
single && !done && !failed && p.toolCallId
? (toolTimes.get(p.toolCallId)?.start ?? null)
: null;
const payload = single
? { input: p.input, output: p.output, error: p.errorText }
: parts.map((c) => ({ input: c.input, output: c.output, error: c.errorText }));
return (
<details className="ai-tool group max-w-full">
<summary
className={cn(
"flex cursor-pointer list-none items-center gap-1.5 rounded-md border border-border px-2 py-1 text-[11px] text-muted-foreground transition-colors select-none hover:bg-muted/60 [&::-webkit-details-marker]:hidden",
failed && "border-red-200 text-red-700",
)}
>
<Wrench className="size-3 shrink-0" />
<span className="font-mono">{name}</span>
{!single && (
<span className="tabular-nums text-[10px]">×{parts.length}</span>
)}
{done && <Check className="size-3 text-emerald-600" />}
{failed && <TriangleAlert className="size-3" />}
{!done && !failed && <CircleDashed className="size-3 animate-spin" />}
{took && (
<span className="ml-auto tabular-nums text-[10px]">{took}</span>
)}
{runningSince != null && <RunningToolClock start={runningSince} />}
</summary>
<pre className="mt-1 max-h-40 overflow-auto rounded-md bg-muted/70 p-2 font-mono text-[10px] leading-relaxed whitespace-pre-wrap">
{JSON.stringify(payload, null, 2)}
</pre>
</details>
);
}
/** A run of 3+ settled tool chips folded behind one summary row. Opening it
* reveals the individual chips, each still expandable to its payload. */
function ToolRunGroup({ groups }: { groups: ToolPartView[][] }) {
const t = useTranslations("chat.panel");
const calls = groups.reduce((n, g) => n + g.length, 0);
// A failure the turn went back and made good is not a failure of the run: a
// tool that errored and then ran clean got where it was going, so the header
// only reddens for an error nothing after it answered. The chip itself keeps
// its own mark, so the attempt is still visible inside.
const flat = groups.flat();
const failed = flat.some(
(p, i) =>
p.state === "output-error" &&
!flat
.slice(i + 1)
.some((q) => toolPartName(q) === toolPartName(p) && q.state === "output-available"),
);
return (
<details className="ai-tool-run group/run max-w-full">
<summary
className={cn(
"flex cursor-pointer list-none items-center gap-1.5 rounded-md border border-border px-2 py-1 text-[11px] text-muted-foreground transition-colors select-none hover:bg-muted/60 [&::-webkit-details-marker]:hidden",
failed && "border-red-200 text-red-700",
)}
>
<Wrench className="size-3 shrink-0" />
<span>
{t("toolCalls", { count: calls })}
</span>
{failed ? (
<TriangleAlert className="size-3" />
) : (
<Check className="size-3 text-emerald-600" />
)}
<ChevronDown className="ml-auto size-3 transition-transform group-open/run:rotate-180" />
</summary>
<div className="mt-1.5 flex flex-col gap-1.5 border-l border-border pl-2">
{groups.map((parts, i) => (
<ToolChipGroup key={i} parts={parts} />
))}
</div>
</details>
);
}
/** A collapsed disclosure for a reasoning block, styled like the tool row. */
function ThoughtBlock({ text }: { text: string }) {
const t = useTranslations("chat.panel");
return (
<details className="ai-thought group max-w-full">
<summary className="flex cursor-pointer list-none items-center gap-1.5 rounded-md border border-border px-2 py-1 text-[11px] text-muted-foreground transition-colors select-none hover:bg-muted/60 [&::-webkit-details-marker]:hidden">
<Brain className="size-3 shrink-0" />
<span>{t("thought")}</span>
<ChevronDown className="ml-auto size-3 transition-transform group-open:rotate-180" />
</summary>
<div className="ai-md mt-1 max-w-full px-2 text-[12px] leading-relaxed text-muted-foreground">
<Markdown components={chatMarkdownComponents}>{text}</Markdown>
</div>
</details>
);
}
const MessageView = memo(function MessageView({
message,
attachedKeys,
}: {
message: UIMessage;
attachedKeys: string;
}) {
// A continuation is the panel's own doing; the reply it brings reads as the
// ask's reply.
if (isResumeMessage(message)) return null;
if (message.role === "user") {
const text = message.parts
.map((p) => (p.type === "text" ? p.text : ""))
.join("");
// normalizeRef also reads attachments saved by older threads (pre-ref shape).
const attachments = (
(message.metadata as { attachments?: unknown[] } | undefined)
?.attachments ?? []
)
.map(normalizeRef)
.filter((r): r is AssetRef => r !== null);
// Only a ref a click can act on gets a card above the bubble; the rest
// read as pills, in the text or in a row of their own.
const cards = attachments.filter(refHasCard);
return (
<div className="ai-msg-user group mb-3 flex flex-col items-end gap-1">
{cards.length > 0 && (
<div className="flex max-w-[85%] flex-wrap justify-end gap-1.5">
{cards.map((a) => (
<MessageAssetCard key={`${a.scope}:${a.id}`} asset={a} />
))}
</div>
)}
<UnmentionedRefPills text={text} refs={attachments} className="max-w-[85%] justify-end" />
{text && (
<div className="max-w-[85%] rounded-2xl rounded-br-md bg-neutral-900 px-3 py-2 text-[12.5px] leading-relaxed break-words whitespace-pre-wrap text-white">
<MentionedText text={text} attachments={attachments} onDark />
</div>
)}
<MessageCopy text={text} />
</div>
);
}
const text = sanitizeAssistantText(
message.parts.map((p) => (p.type === "text" ? p.text : "")).join(""),
);
// Media the turn produced, gathered from every finished tool call. It renders
// as one left-to-right row after the chips — the way the mock presents a set
// of generated stills — instead of one card stacked under each tool.
const toolOutputs = message.parts
.filter((part) => part.type.startsWith("tool-") || part.type === "dynamic-tool")
.map((part) => part as unknown as { state: string; output?: unknown })
.filter((p) => p.state === "output-available")
.map((p) => p.output)
.filter((o) => !cardIsAttached(o, attachedKeys));
// The source's own words for anything the turn pulled off the web (a tweet's
// body, a video's title/description). It renders as a quote beside the media
// — straight from the tool output, so the model never has to retype it.
const sourceTexts = toolOutputs
.map((o) => (o && typeof o === "object" ? (o as { sourceText?: unknown }).sourceText : undefined))
.filter((t): t is string => typeof t === "string" && t.trim().length > 0);
// Consecutive settled calls of the same tool with the same outcome collapse
// into one counted chip; running calls always stand alone.
type Block =
| { kind: "part"; index: number; part: (typeof message.parts)[number] }
| { kind: "tools"; index: number; parts: ToolPartView[] };
const blocks: Block[] = [];
message.parts.forEach((part, index) => {
if (!isToolPartType(part.type)) {
blocks.push({ kind: "part", index, part });
return;
}
const p = part as unknown as ToolPartView;
const settled = p.state === "output-available" || p.state === "output-error";
const last = blocks[blocks.length - 1];
if (
settled &&
last?.kind === "tools" &&
last.parts[0].state === p.state &&
toolPartName(last.parts[0]) === toolPartName(p)
) {
last.parts.push(p);
return;
}
blocks.push({ kind: "tools", index, parts: [p] });
});
// A run of 3+ consecutive settled chips folds behind one "N tool calls" row;
// a still-running call stays outside the fold so its spinner remains visible.
type RenderBlock = Block | { kind: "toolrun"; index: number; groups: ToolPartView[][] };
const renderBlocks: RenderBlock[] = [];
let run: Extract<Block, { kind: "tools" }>[] = [];
const flushRun = () => {
if (run.length >= 3) {
renderBlocks.push({
kind: "toolrun",
index: run[0].index,
groups: run.map((b) => b.parts),
});
} else {
renderBlocks.push(...run);
}
run = [];
};
for (const block of blocks) {
const settled =
block.kind === "tools" &&
block.parts.every(
(p) => p.state === "output-available" || p.state === "output-error",
);
if (settled && block.kind === "tools") {
run.push(block);
continue;
}
flushRun();
renderBlocks.push(block);
}
flushRun();
return (
<div className="ai-msg-assistant group mb-3 flex min-w-0 flex-col gap-1.5">
{renderBlocks.map((block) => {
if (block.kind === "toolrun") {
return <ToolRunGroup key={block.index} groups={block.groups} />;
}
if (block.kind === "tools") {
return <ToolChipGroup key={block.index} parts={block.parts} />;
}
const part = block.part;
if (part.type === "text") {
return (
<Fragment key={block.index}>
{splitThoughtSegments(sanitizeAssistantText(part.text)).map((seg, j) =>
seg.kind === "thought" ? (
<ThoughtBlock key={j} text={seg.text} />
) : (
<div
key={j}
className="ai-md min-w-0 max-w-full overflow-hidden text-[12.5px] leading-relaxed break-words"
>
<Markdown components={chatMarkdownComponents}>
{seg.text}
</Markdown>
</div>
),
)}
</Fragment>
);
}
return null;
})}
{/* The imported post's own text, quoted above its media so the two read
as one thing — the tweet body, or the video's title and description. */}
{sourceTexts.map((t, i) => (
<SourceQuote key={`src-${i}`} text={t} />
))}
{/* Everything the turn generated, in one left-to-right row (images and
clips flow and wrap; audio and documents take their own line). It
stays in the chat until the user drags it out or files it away. */}
{toolOutputs.length > 0 && (
<div className="flex flex-wrap items-start gap-1.5">
{toolOutputs.map((output, i) => (
<ToolOutputAssets key={i} output={output} />
))}
</div>
)}
<MessageCopy text={text} />
</div>
);
});
/** Ambient proof this thread's renders are still working: chat-launched video
* jobs run in the background long after the tool call returns, so the bottom
* of the conversation shows a live count and clock until every render settles
* — the cards up-thread flip to their result, but this line is what says
* "something is still happening" without scrolling. */
function ThreadRenders({ threadId }: { threadId: string }) {
const t = useTranslations("chat.panel");
const jobs = useGenerate((s) => s.jobs);
const records = useEditor((s) => s.renders);
const live = jobs.filter(
(j) =>
j.kind === "video" && j.status === "running" && j.chatId === threadId,
);
// Renders another machine is still working on: doc-mirrored records with no
// local job. The TTL keeps a record whose job died from spinning forever;
// a mount-time clock is enough for that check.
const [now] = useState(() => Date.now());
const liveIds = new Set(jobs.map((j) => j.id));
const remote = records.filter(
(r) =>
r.chatId === threadId &&
r.status === "running" &&
!liveIds.has(r.id) &&
now - r.startedAt < RECORD_RUNNING_TTL_MS,
);
const running = [...live, ...remote];
const oldest = running.length
? Math.min(...running.map((j) => j.startedAt))
: null;
const elapsed = useElapsed(oldest);
if (running.length === 0) return null;
return (
<div className="ai-renders mt-1 flex items-center gap-1.5 text-[11.5px] text-muted-foreground">
<CircleDashed className="size-3 animate-spin" />
{t("renderingVideos", { count: running.length })}{" "}
{elapsed && <span className="tabular-nums">{elapsed}</span>}
</div>
);
}
function ModelSelector({
info,
model,
onSelect,
}: {
info: ModelsInfo | null;
model: string;
onSelect: (id: string) => void;
}) {
const t = useTranslations("chat.panel");
// Model names are product names; the hidden test model is described.
const modelLabel = (m: (typeof AI_MODELS)[number]) => (m.provider === "test" ? t("testModel") : m.label);
// Favorites follow the catalog: a retired id becomes its replacement, an
// unknown id drops, and one replacement standing in for two ids lists once.
const [favs, setFavs] = useState<string[]>(() => {
if (typeof window === "undefined") return [];
try {
const saved = JSON.parse(localStorage.getItem(FAVS_KEY) ?? "[]") as string[];
return [...new Set(saved.map(currentChatModel).filter((id): id is string => !!id))];
} catch {
return [];
}
});
const showTest =
typeof window !== "undefined" &&
localStorage.getItem("cut-ai-test") === "1";
// A model the provider's account doesn't run stays out of the list.
const models = AI_MODELS.filter(
(m) => (!m.hidden || showTest) && (!info || accountRuns(m.id, info.providers)),
);
const groups = [
"claude",
"codex",
"gemini",
...(showTest ? ["test"] : []),
]
.map((p) => ({
provider: p,
models: models.filter((m) => m.provider === p),
// CLI providers list only once the engine has confirmed the CLI is
// installed — until the probe answers there is no evidence the group
// exists on this Mac. Gemini is hosted, so it needs no confirmation.
installed:
p === "gemini"
? info?.providers[p]?.installed !== false
: info?.providers[p]?.installed === true,
}))
// The picker lists every confirmed provider and lets any of them be
// picked; any other problem (signed out, etc.) surfaces as a chat error
// when the user sends.
.filter((group) => group.models.length > 0 && group.installed);
const flat = groups.flatMap((group) => group.models);
const current = models.find((m) => m.id === model);
const currentLabel = current ? modelLabel(current) : model;
const toggleFav = (id: string) => {
const next = favs.includes(id)
? favs.filter((f) => f !== id)
: [...favs, id];
setFavs(next);
localStorage.setItem(FAVS_KEY, JSON.stringify(next));
};
return (
<DropdownMenu>
<DropdownMenuTrigger className="ai-model-trigger flex items-center gap-1 rounded-md px-1.5 py-1 text-[11px] font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
<Sparkles className="size-3" />
{currentLabel}
<ChevronDown className="size-3" />
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="ai-model-menu w-60"
onKeyDown={(e) => {
const n = Number(e.key);
if (n >= 1 && n <= flat.length) onSelect(flat[n - 1].id);
}}
>
{groups.map((group, gi) =>
group.models.length === 0 ? null : (
<DropdownMenuGroup key={group.provider}>
{gi > 0 && <DropdownMenuSeparator />}
<DropdownMenuLabel className="flex items-center gap-1.5 text-[10.5px] tracking-wider text-muted-foreground uppercase">
<Sparkles className="size-3" /> {providerLabel(group.provider, t)}
</DropdownMenuLabel>
{group.models.map((m) => (
<DropdownMenuItem
key={m.id}
className="ai-model-item gap-2"
onClick={() => onSelect(m.id)}
>
<span className="flex-1 text-[12px]">{modelLabel(m)}</span>
{model === m.id && (
<Check className="size-3.5 text-[#0a84ff]" />
)}
<button
className="rounded p-0.5 hover:bg-muted"
title={favs.includes(m.id) ? t("unfavorite") : t("favorite")}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
toggleFav(m.id);
}}
>
<Star
className={cn(
"size-3",
favs.includes(m.id)
? "fill-amber-400 text-amber-400"
: "text-muted-foreground/50",
)}
/>
</button>
<span className="w-3 text-right font-mono text-[10px] text-muted-foreground/60">
{flat.indexOf(m) + 1}
</span>
</DropdownMenuItem>
))}
</DropdownMenuGroup>
),
)}
</DropdownMenuContent>
</DropdownMenu>
);
}