Files
t3code/apps/renderer/src/components/ChatView.tsx
T
Cursor AgentandJulius Marminge 84b71e0a01 fix: add error handling and address review feedback
- Validate cwd is non-empty in shell:open-in-editor IPC handler
- Check shell.openPath() return value and throw on error
- Attach spawn error listener to prevent crashes from missing CLI
- Validate localStorage lastEditor against known EDITORS list
- Only call preventDefault() for Cmd+O when action will run

Co-authored-by: Julius Marminge <[email protected]>
2026-02-09 05:23:09 +00:00

758 lines
28 KiB
TypeScript

import {
type FormEvent,
type KeyboardEvent,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { EDITORS, type EditorId } from "@acme/contracts";
import {
DEFAULT_MODEL,
DEFAULT_REASONING,
MODEL_OPTIONS,
REASONING_OPTIONS,
resolveModelSlug,
} from "../model-logic";
import {
derivePhase,
deriveTimelineEntries,
deriveWorkLogEntries,
formatElapsed,
formatTimestamp,
readNativeApi,
} from "../session-logic";
import { useStore } from "../store";
import ChatMarkdown from "./ChatMarkdown";
function formatMessageMeta(createdAt: string, duration: string | null): string {
if (!duration) return formatTimestamp(createdAt);
return `${formatTimestamp(createdAt)} • ${duration}`;
}
const FILE_MANAGER_LABEL = navigator.platform.includes("Mac")
? "Finder"
: navigator.platform.includes("Win")
? "Explorer"
: "Files";
function editorLabel(editor: (typeof EDITORS)[number]): string {
return editor.command ? editor.label : FILE_MANAGER_LABEL;
}
const LAST_EDITOR_KEY = "codething:last-editor";
function statusLabel(phase: string): string {
if (phase === "running") return "Thinking / working";
if (phase === "connecting") return "Connecting";
if (phase === "ready") return "Ready";
return "Disconnected";
}
function workToneClass(tone: "thinking" | "tool" | "info" | "error"): string {
if (tone === "error") return "text-rose-300/50";
if (tone === "tool") return "text-[#8a8a8a]";
if (tone === "thinking") return "text-[#707070]";
return "text-[#606060]";
}
export default function ChatView() {
const { state, dispatch } = useStore();
const api = useMemo(() => readNativeApi(), []);
const [prompt, setPrompt] = useState("");
const [isSending, setIsSending] = useState(false);
const [isConnecting, setIsConnecting] = useState(false);
const [isModelMenuOpen, setIsModelMenuOpen] = useState(false);
const [isEditorMenuOpen, setIsEditorMenuOpen] = useState(false);
const [lastEditor, setLastEditor] = useState<EditorId>(() => {
const stored = localStorage.getItem(LAST_EDITOR_KEY);
return EDITORS.some((e) => e.id === stored)
? (stored as EditorId)
: EDITORS[0].id;
});
const [selectedEffort, setSelectedEffort] =
useState<string>(DEFAULT_REASONING);
const [nowTick, setNowTick] = useState(() => Date.now());
const messagesEndRef = useRef<HTMLDivElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const modelMenuRef = useRef<HTMLDivElement>(null);
const editorMenuRef = useRef<HTMLDivElement>(null);
const activeThread = state.threads.find((t) => t.id === state.activeThreadId);
const activeProject = state.projects.find(
(p) => p.id === activeThread?.projectId,
);
const selectedModel = resolveModelSlug(
activeThread?.model ?? activeProject?.model ?? DEFAULT_MODEL,
);
const phase = derivePhase(activeThread?.session ?? null);
const isWorking = phase === "running" || isSending || isConnecting;
const activeTurnId = activeThread?.session?.activeTurnId;
const nowIso = new Date(nowTick).toISOString();
const modelOptions = MODEL_OPTIONS;
const workLogEntries = useMemo(
() => deriveWorkLogEntries(activeThread?.events ?? [], activeTurnId),
[activeThread?.events, activeTurnId],
);
const assistantCompletionByItemId = useMemo(() => {
const map = new Map<string, string>();
const ordered = [...(activeThread?.events ?? [])].reverse();
for (const event of ordered) {
if (event.method !== "item/completed") continue;
if (!event.itemId) continue;
map.set(event.itemId, event.createdAt);
}
return map;
}, [activeThread?.events]);
const timelineEntries = useMemo(
() =>
deriveTimelineEntries(
activeThread?.messages ?? [],
isWorking ? workLogEntries : [],
),
[activeThread?.messages, isWorking, workLogEntries],
);
// Auto-scroll on new messages
const messageCount = activeThread?.messages.length ?? 0;
const workLogCount = workLogEntries.length;
// biome-ignore lint/correctness/useExhaustiveDependencies: trigger on message count change
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messageCount]);
// biome-ignore lint/correctness/useExhaustiveDependencies: auto-scroll while active work-log events stream in
useEffect(() => {
if (phase !== "running") return;
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [phase, workLogCount]);
// Auto-resize textarea
// biome-ignore lint/correctness/useExhaustiveDependencies: trigger on prompt change
useEffect(() => {
const ta = textareaRef.current;
if (!ta) return;
ta.style.height = "auto";
ta.style.height = `${Math.min(ta.scrollHeight, 200)}px`;
}, [prompt]);
useEffect(() => {
if (phase !== "running") return;
const timer = window.setInterval(() => {
setNowTick(Date.now());
}, 250);
return () => {
window.clearInterval(timer);
};
}, [phase]);
useEffect(() => {
if (!isModelMenuOpen) return;
const handleClickOutside = (event: MouseEvent) => {
if (!modelMenuRef.current) return;
if (
event.target instanceof Node &&
!modelMenuRef.current.contains(event.target)
) {
setIsModelMenuOpen(false);
}
};
window.addEventListener("mousedown", handleClickOutside);
return () => {
window.removeEventListener("mousedown", handleClickOutside);
};
}, [isModelMenuOpen]);
useEffect(() => {
if (!isEditorMenuOpen) return;
const handleClickOutside = (event: MouseEvent) => {
if (!editorMenuRef.current) return;
if (
event.target instanceof Node &&
!editorMenuRef.current.contains(event.target)
) {
setIsEditorMenuOpen(false);
}
};
window.addEventListener("mousedown", handleClickOutside);
return () => {
window.removeEventListener("mousedown", handleClickOutside);
};
}, [isEditorMenuOpen]);
// Cmd+O / Ctrl+O to open in last-used editor
useEffect(() => {
const handler = (e: globalThis.KeyboardEvent) => {
if (e.key === "o" && (e.metaKey || e.ctrlKey) && !e.shiftKey) {
if (api && activeProject) {
e.preventDefault();
void api.shell.openInEditor(activeProject.cwd, lastEditor);
}
}
};
window.addEventListener("keydown", handler);
return () => window.removeEventListener("keydown", handler);
}, [api, activeProject, lastEditor]);
const openInEditor = (editorId: EditorId) => {
if (!api || !activeProject) return;
void api.shell.openInEditor(activeProject.cwd, editorId);
setLastEditor(editorId);
localStorage.setItem(LAST_EDITOR_KEY, editorId);
setIsEditorMenuOpen(false);
};
const ensureSession = async (): Promise<string | null> => {
if (!api || !activeThread || !activeProject) return null;
if (activeThread.session && activeThread.session.status !== "closed") {
return activeThread.session.sessionId;
}
setIsConnecting(true);
try {
const session = await api.providers.startSession({
provider: "codex",
cwd: activeProject.cwd || undefined,
model: selectedModel || undefined,
});
dispatch({
type: "UPDATE_SESSION",
threadId: activeThread.id,
session,
});
return session.sessionId;
} catch (err) {
dispatch({
type: "SET_ERROR",
threadId: activeThread.id,
error: err instanceof Error ? err.message : "Failed to connect.",
});
return null;
} finally {
setIsConnecting(false);
}
};
const onSend = async (e: FormEvent) => {
e.preventDefault();
if (!api || !activeThread || isSending || isConnecting) return;
const trimmed = prompt.trim();
if (!trimmed) return;
// Auto-title from first message
if (activeThread.messages.length === 0) {
const title =
trimmed.length > 50 ? `${trimmed.slice(0, 50)}...` : trimmed;
dispatch({
type: "SET_THREAD_TITLE",
threadId: activeThread.id,
title,
});
}
dispatch({
type: "SET_ERROR",
threadId: activeThread.id,
error: null,
});
dispatch({
type: "PUSH_USER_MESSAGE",
threadId: activeThread.id,
id: crypto.randomUUID(),
text: trimmed,
});
setPrompt("");
const sessionId = await ensureSession();
if (!sessionId) return;
setIsSending(true);
try {
await api.providers.sendTurn({
sessionId,
input: trimmed,
model: selectedModel || undefined,
effort: selectedEffort || undefined,
});
} catch (err) {
dispatch({
type: "SET_ERROR",
threadId: activeThread.id,
error: err instanceof Error ? err.message : "Failed to send message.",
});
} finally {
setIsSending(false);
}
};
const onInterrupt = async () => {
if (!api || !activeThread?.session) return;
await api.providers.interruptTurn({
sessionId: activeThread.session.sessionId,
turnId: activeThread.session.activeTurnId,
});
};
const onModelSelect = (model: string) => {
if (!activeThread) return;
dispatch({
type: "SET_THREAD_MODEL",
threadId: activeThread.id,
model: resolveModelSlug(model),
});
setIsModelMenuOpen(false);
};
const onKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
void onSend(e as unknown as FormEvent);
}
};
// Empty state: no active thread
if (!activeThread) {
return (
<div className="flex flex-1 flex-col bg-[#0c0c0c] text-[#a0a0a0]/40">
<div className="drag-region h-[52px] shrink-0" />
<div className="flex flex-1 items-center justify-center">
<div className="text-center">
<p className="text-sm">
Select a thread or create a new one to get started.
</p>
</div>
</div>
</div>
);
}
return (
<div className="flex flex-1 flex-col bg-[#0c0c0c]">
{/* Top bar */}
<header className="drag-region flex items-center justify-between border-b border-white/[0.08] px-5 pt-[28px] pb-3">
<div className="flex items-center gap-3">
<h2 className="text-sm font-medium text-[#e0e0e0]">
{activeThread.title}
</h2>
{activeProject && (
<span className="rounded-full bg-white/[0.06] px-2 py-0.5 text-[10px] text-[#a0a0a0]/50">
{activeProject.name}
</span>
)}
</div>
<div className="flex items-center gap-3">
{/* Status indicator */}
<div
className={`inline-flex items-center gap-2 rounded-full border px-2.5 py-1 text-[10px] ${
phase === "running"
? "border-sky-400/35 bg-sky-500/[0.08] text-sky-100"
: phase === "connecting"
? "border-amber-400/35 bg-amber-500/[0.08] text-amber-100"
: phase === "ready"
? "border-emerald-400/35 bg-emerald-500/[0.08] text-emerald-100"
: "border-white/[0.08] bg-white/[0.04] text-[#a0a0a0]/70"
}`}
>
<span className="relative inline-flex h-2.5 w-2.5">
{(phase === "running" || phase === "connecting") && (
<span
className={`absolute inline-flex h-full w-full animate-ping rounded-full ${
phase === "running" ? "bg-sky-300/70" : "bg-amber-300/70"
}`}
/>
)}
<span
className={`relative inline-flex h-2.5 w-2.5 rounded-full ${
phase === "running"
? "bg-sky-200"
: phase === "connecting"
? "bg-amber-200"
: phase === "ready"
? "bg-emerald-200"
: "bg-[#a0a0a0]/40"
}`}
/>
</span>
<span>{statusLabel(phase)}</span>
</div>
{/* Open in editor */}
{activeProject && (
<div className="relative" ref={editorMenuRef}>
<button
type="button"
className="rounded-md px-2 py-1 text-[10px] text-[#a0a0a0]/40 transition-colors duration-150 hover:text-[#a0a0a0]/60"
onClick={() => setIsEditorMenuOpen((v) => !v)}
>
Open in&hellip;
</button>
{isEditorMenuOpen && (
<div className="absolute right-0 top-full z-50 mt-1 min-w-[120px] rounded-md border border-white/[0.08] bg-[#1b1b1d] py-1 shadow-xl">
{EDITORS.map((editor) => (
<button
key={editor.id}
type="button"
className="flex w-full items-center gap-2 px-3 py-1.5 text-left text-[11px] text-[#e0e0e0] hover:bg-white/[0.06]"
onClick={() => openInEditor(editor.id)}
>
{editorLabel(editor)}
{editor.id === lastEditor && (
<kbd className="ml-auto text-[9px] text-[#a0a0a0]/40">
{navigator.platform.includes("Mac")
? "\u2318O"
: "Ctrl+O"}
</kbd>
)}
</button>
))}
</div>
)}
</div>
)}
{/* Diff toggle */}
<button
type="button"
className={`rounded-md px-2 py-1 text-[10px] transition-colors duration-150 ${
state.diffOpen
? "bg-white/10 text-white"
: "text-[#a0a0a0]/40 hover:text-[#a0a0a0]/60"
}`}
onClick={() => dispatch({ type: "TOGGLE_DIFF" })}
>
Diff
</button>
</div>
</header>
{/* Error banner */}
{activeThread.error && (
<div className="mx-4 mt-3 rounded-lg border border-rose-400/20 bg-rose-900/20 px-3 py-2 text-xs text-rose-200">
{activeThread.error}
</div>
)}
{/* Messages */}
<div className="flex-1 overflow-y-auto px-5 py-4">
{activeThread.messages.length === 0 && !isWorking ? (
<div className="flex h-full items-center justify-center">
<p className="text-sm text-[#a0a0a0]/30">
Send a message to start the conversation.
</p>
</div>
) : (
<div className="mx-auto max-w-3xl space-y-4">
{timelineEntries.map((timelineEntry) =>
timelineEntry.kind === "work" ? (
<div
key={timelineEntry.id}
className="border-l-2 border-white/[0.05] pl-4 py-1"
>
<p
className={`py-[2px] text-[12px] leading-relaxed ${workToneClass(timelineEntry.entry.tone)}`}
>
{timelineEntry.entry.detail ? (
<>
{timelineEntry.entry.label}
<span className="ml-1.5 font-mono text-[11px] opacity-60">
{timelineEntry.entry.detail}
</span>
</>
) : (
timelineEntry.entry.label
)}
</p>
</div>
) : (
<div key={timelineEntry.id}>
{timelineEntry.message.role === "user" ? (
<div className="flex justify-end">
<div className="max-w-[80%] rounded-2xl rounded-br-sm border border-white/[0.08] bg-white/[0.05] px-4 py-3">
<pre className="whitespace-pre-wrap break-words font-mono text-sm leading-relaxed text-[#e0e0e0]">
{timelineEntry.message.text}
</pre>
<p className="mt-1.5 text-right text-[10px] text-[#a0a0a0]/30">
{formatTimestamp(timelineEntry.message.createdAt)}
</p>
</div>
</div>
) : (
<div className="border-l-2 border-white/[0.15] pl-4">
<ChatMarkdown
text={
timelineEntry.message.text ||
(timelineEntry.message.streaming
? ""
: "(empty response)")
}
/>
{timelineEntry.message.streaming && (
<div className="pt-1.5">
<span className="inline-flex items-center gap-2 rounded-full border border-sky-400/25 bg-sky-500/[0.08] px-2 py-0.5 text-[10px] text-sky-100/90">
<span className="inline-flex gap-1">
<span className="h-1.5 w-1.5 animate-pulse rounded-full bg-sky-100/80" />
<span className="h-1.5 w-1.5 animate-pulse rounded-full bg-sky-100/80 [animation-delay:150ms]" />
<span className="h-1.5 w-1.5 animate-pulse rounded-full bg-sky-100/80 [animation-delay:300ms]" />
</span>
<span>Thinking</span>
</span>
</div>
)}
<p className="mt-1.5 text-[10px] text-[#a0a0a0]/30">
{formatMessageMeta(
timelineEntry.message.createdAt,
timelineEntry.message.streaming
? formatElapsed(
timelineEntry.message.createdAt,
nowIso,
)
: formatElapsed(
timelineEntry.message.createdAt,
assistantCompletionByItemId.get(
timelineEntry.message.id,
),
),
)}
</p>
</div>
)}
</div>
),
)}
{isWorking && (
<div className="border-l-2 border-white/[0.05] pl-4 py-1">
<div className="flex items-center pt-1">
<span className="inline-flex items-center gap-[3px]">
<span className="h-1 w-1 rounded-full bg-white/20 animate-pulse" />
<span className="h-1 w-1 rounded-full bg-white/20 animate-pulse [animation-delay:200ms]" />
<span className="h-1 w-1 rounded-full bg-white/20 animate-pulse [animation-delay:400ms]" />
</span>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
)}
</div>
{/* Input bar */}
<div className="px-5 pb-4 pt-2">
<form onSubmit={onSend} className="mx-auto max-w-3xl">
<div className="group rounded-[20px] border border-white/[0.08] bg-[#141416] transition-colors duration-200 focus-within:border-white/[0.16]">
{/* Textarea area */}
<div className="px-4 pt-4 pb-2">
<textarea
ref={textareaRef}
className="w-full resize-none bg-transparent text-[14px] leading-relaxed text-[#e0e0e0] placeholder:text-[#a0a0a0]/35 focus:outline-none"
rows={2}
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
onKeyDown={onKeyDown}
placeholder={
phase === "disconnected"
? "Ask for follow-up changes"
: "Ask anything..."
}
disabled={isSending || isConnecting}
/>
</div>
{/* Bottom toolbar */}
<div className="flex items-center justify-between px-3 pb-3">
<div className="flex items-center gap-1">
{/* Model picker */}
<div className="relative" ref={modelMenuRef}>
<button
type="button"
className="inline-flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[13px] text-[#a0a0a0]/70 transition-colors duration-150 hover:bg-white/[0.06] hover:text-[#d0d0d0]"
onClick={() => setIsModelMenuOpen((open) => !open)}
>
<span className="max-w-[180px] truncate">
{selectedModel}
</span>
<svg
width="10"
height="10"
viewBox="0 0 10 10"
fill="none"
className="opacity-50"
aria-hidden="true"
>
<path
d="M2.5 4L5 6.5L7.5 4"
stroke="currentColor"
strokeWidth="1.2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
{isModelMenuOpen && (
<div className="absolute bottom-full left-0 z-20 mb-2 w-[320px] rounded-2xl border border-white/[0.1] bg-[#1b1b1d]/95 p-2 shadow-[0_16px_40px_rgba(0,0,0,0.55)] backdrop-blur">
<p className="px-2 py-1 text-[11px] text-[#a0a0a0]/70">
Select model
</p>
<div className="max-h-72 overflow-y-auto">
{modelOptions.map((model) => {
const isSelected = model === selectedModel;
return (
<button
key={model}
type="button"
className={`mb-0.5 flex w-full items-center justify-between gap-2 rounded-xl px-2 py-2 text-left font-mono text-sm transition-colors duration-150 ${
isSelected
? "bg-white/[0.08] text-white"
: "text-[#d4d4d4] hover:bg-white/[0.05]"
}`}
onClick={() => onModelSelect(model)}
>
<span className="truncate">{model}</span>
<span
className={`pt-0.5 text-sm ${
isSelected ? "text-white" : "text-transparent"
}`}
>
✓
</span>
</button>
);
})}
</div>
</div>
)}
</div>
{/* Divider */}
<div className="mx-0.5 h-4 w-px bg-white/[0.08]" />
{/* Reasoning effort */}
<label
className="inline-flex cursor-pointer items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[13px] text-[#a0a0a0]/70 transition-colors duration-150 hover:bg-white/[0.06] hover:text-[#d0d0d0]"
htmlFor="reasoning-effort"
>
<span>
{selectedEffort.charAt(0).toUpperCase() +
selectedEffort.slice(1)}
</span>
<select
id="reasoning-effort"
className="absolute opacity-0 w-0 h-0"
value={selectedEffort}
onChange={(event) => setSelectedEffort(event.target.value)}
>
{REASONING_OPTIONS.map((effort) => (
<option
key={effort}
value={effort}
className="bg-[#1b1b1d]"
>
{effort}
{effort === DEFAULT_REASONING ? " (default)" : ""}
</option>
))}
</select>
<svg
width="10"
height="10"
viewBox="0 0 10 10"
fill="none"
className="opacity-50"
aria-hidden="true"
>
<path
d="M2.5 4L5 6.5L7.5 4"
stroke="currentColor"
strokeWidth="1.2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</label>
</div>
{/* Right side: send / stop button */}
<div className="flex items-center gap-2">
{activeProject && (
<span className="text-[11px] text-[#a0a0a0]/25">
{activeProject.name}
</span>
)}
{phase === "running" ? (
<button
type="button"
className="flex h-8 w-8 items-center justify-center rounded-full bg-rose-500/90 text-white transition-all duration-150 hover:bg-rose-500 hover:scale-105"
onClick={() => void onInterrupt()}
aria-label="Stop generation"
>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="currentColor"
aria-hidden="true"
>
<rect x="2" y="2" width="8" height="8" rx="1.5" />
</svg>
</button>
) : (
<button
type="submit"
className="flex h-8 w-8 items-center justify-center rounded-full bg-white/90 text-[#0c0c0c] transition-all duration-150 hover:bg-white hover:scale-105 disabled:opacity-30 disabled:hover:scale-100"
disabled={isSending || isConnecting || !prompt.trim()}
aria-label={
isConnecting
? "Connecting"
: isSending
? "Sending"
: "Send message"
}
>
{isConnecting || isSending ? (
<svg
width="14"
height="14"
viewBox="0 0 14 14"
fill="none"
className="animate-spin"
aria-hidden="true"
>
<circle
cx="7"
cy="7"
r="5.5"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeDasharray="20 12"
/>
</svg>
) : (
<svg
width="14"
height="14"
viewBox="0 0 14 14"
fill="none"
aria-hidden="true"
>
<path
d="M7 11.5V2.5M7 2.5L3 6.5M7 2.5L11 6.5"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)}
</button>
)}
</div>
</div>
</div>
</form>
</div>
</div>
);
}