@devries-cameron20 / t3code-1 / commits / aced302b63
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Address PR review feedback for draft model/error/persistence handling

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Mar 1, 2026 at 16:09 UTC aced302b638608042c8a1d30b6fc91dc5661657a
2 files changed +112 -26
apps/web/src/components/ChatView.tsx
+101 -23
@@ -17,6 +17,7 @@ import {
17 type ProviderApprovalDecision,
18 type ThreadId,
19 type TurnId,
20 + normalizeModelSlug,
21 resolveModelSlug,
22 } from "@t3tools/contracts";
23 import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
@@ -197,6 +198,7 @@ function buildLocalDraftThread(
198 threadId: ThreadId,
199 draftThread: DraftThreadState,
200 fallbackModel: string,
201 + error: string | null,
202 ): Thread {
203 return {
204 id: threadId,
@@ -218,7 +220,7 @@ function buildLocalDraftThread(
220 activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
221 session: null,
222 messages: [],
221 - error: null,
223 + error,
224 createdAt: draftThread.createdAt,
225 latestTurn: null,
226 lastVisitedAt: draftThread.createdAt,
@@ -229,6 +231,25 @@ function buildLocalDraftThread(
231 };
232 }
233
234 +function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
235 + if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
236 + return;
237 + }
238 + URL.revokeObjectURL(previewUrl);
239 +}
240 +
241 +function revokeUserMessagePreviewUrls(message: ChatMessage): void {
242 + if (message.role !== "user" || !message.attachments) {
243 + return;
244 + }
245 + for (const attachment of message.attachments) {
246 + if (attachment.type !== "image") {
247 + continue;
248 + }
249 + revokeBlobPreviewUrl(attachment.previewUrl);
250 + }
251 +}
252 +
253 type ComposerCommandItem =
254 | {
255 id: string;
@@ -449,6 +470,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
470 const [isDragOverComposer, setIsDragOverComposer] = useState(false);
471 const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
472 const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);
473 + const [localDraftErrorsByThreadId, setLocalDraftErrorsByThreadId] = useState<
474 + Record<ThreadId, string | null>
475 + >({});
476 const [sendPhase, setSendPhase] = useState<SendPhase>("idle");
477 const [isConnecting, _setIsConnecting] = useState(false);
478 const [isRevertingCheckpoint, setIsRevertingCheckpoint] = useState(false);
@@ -504,12 +528,18 @@ export default function ChatView({ threadId }: ChatViewProps) {
528
529 const serverThread = state.threads.find((t) => t.id === threadId);
530 const fallbackDraftProject = state.projects.find((project) => project.id === draftThread?.projectId);
531 + const localDraftError = localDraftErrorsByThreadId[threadId] ?? null;
532 const localDraftThread = useMemo(
533 () =>
534 draftThread
510 - ? buildLocalDraftThread(threadId, draftThread, fallbackDraftProject?.model ?? DEFAULT_MODEL)
535 + ? buildLocalDraftThread(
536 + threadId,
537 + draftThread,
538 + fallbackDraftProject?.model ?? DEFAULT_MODEL,
539 + localDraftError,
540 + )
541 : undefined,
512 - [draftThread, fallbackDraftProject?.model, threadId],
542 + [draftThread, fallbackDraftProject?.model, localDraftError, threadId],
543 );
544 const activeThread = serverThread ?? localDraftThread;
545 const isServerThread = serverThread !== undefined;
@@ -524,6 +554,19 @@ export default function ChatView({ threadId }: ChatViewProps) {
554 const latestTurnSettled = isLatestTurnSettled(activeLatestTurn, activeThread?.session ?? null);
555 const activeProject = state.projects.find((p) => p.id === activeThread?.projectId);
556
557 + useEffect(() => {
558 + if (!serverThread) {
559 + return;
560 + }
561 + setLocalDraftErrorsByThreadId((existing) => {
562 + if (!(threadId in existing)) {
563 + return existing;
564 + }
565 + const { [threadId]: _removed, ...rest } = existing;
566 + return rest as Record<ThreadId, string | null>;
567 + });
568 + }, [serverThread, threadId]);
569 +
570 useEffect(() => {
571 if (!activeThread?.id) return;
572 if (!latestTurnSettled) return;
@@ -545,9 +588,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
588 dispatch,
589 ]);
590
548 - const selectedModel = resolveModelSlug(
549 - composerDraft.model ?? activeThread?.model ?? activeProject?.model ?? DEFAULT_MODEL,
550 - );
591 + const baseThreadModel = resolveModelSlug(activeThread?.model ?? activeProject?.model ?? DEFAULT_MODEL);
592 + const selectedModel = resolveModelSlug(composerDraft.model ?? baseThreadModel);
593 const selectedEffort = composerDraft.effort ?? DEFAULT_REASONING;
594 const modelOptions = useMemo(
595 () => getAppModelOptions(settings.customCodexModels, selectedModel),
@@ -1184,7 +1226,12 @@ export default function ChatView({ threadId }: ChatViewProps) {
1226
1227 useEffect(() => {
1228 if (!activeThread?.id) {
1187 - setOptimisticUserMessages([]);
1229 + setOptimisticUserMessages((existing) => {
1230 + for (const message of existing) {
1231 + revokeUserMessagePreviewUrls(message);
1232 + }
1233 + return [];
1234 + });
1235 return;
1236 }
1237 if (activeThread.messages.length === 0) {
@@ -1192,6 +1239,10 @@ export default function ChatView({ threadId }: ChatViewProps) {
1239 }
1240 const serverIds = new Set(activeThread.messages.map((message) => message.id));
1241 setOptimisticUserMessages((existing) => {
1242 + const removed = existing.filter((message) => serverIds.has(message.id));
1243 + for (const message of removed) {
1244 + revokeUserMessagePreviewUrls(message);
1245 + }
1246 const next = existing.filter((message) => !serverIds.has(message.id));
1247 return next.length === existing.length ? existing : next;
1248 });
@@ -1202,7 +1253,12 @@ export default function ChatView({ threadId }: ChatViewProps) {
1253 }, [prompt]);
1254
1255 useEffect(() => {
1205 - setOptimisticUserMessages([]);
1256 + setOptimisticUserMessages((existing) => {
1257 + for (const message of existing) {
1258 + revokeUserMessagePreviewUrls(message);
1259 + }
1260 + return [];
1261 + });
1262 setSendPhase("idle");
1263 setComposerHighlightedItemId(null);
1264 dragDepthRef.current = 0;
@@ -1224,7 +1280,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
1280 currentPersistedAttachments.map((attachment) => [attachment.id, attachment]),
1281 );
1282 const stagedAttachmentById = new Map<string, PersistedComposerImageAttachment>();
1227 - const serializationFailedImageIds: string[] = [];
1283 await Promise.all(
1284 composerImages.map(async (image) => {
1285 try {
@@ -1240,8 +1295,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
1295 const existingPersisted = existingPersistedById.get(image.id);
1296 if (existingPersisted) {
1297 stagedAttachmentById.set(image.id, existingPersisted);
1243 - } else {
1244 - serializationFailedImageIds.push(image.id);
1298 }
1299 }
1300 }),
@@ -1468,13 +1521,25 @@ export default function ChatView({ threadId }: ChatViewProps) {
1521 const setThreadError = useCallback(
1522 (threadId: ThreadId | null, error: string | null) => {
1523 if (!threadId) return;
1471 - dispatch({
1472 - type: "SET_ERROR",
1473 - threadId,
1474 - error,
1524 + if (state.threads.some((thread) => thread.id === threadId)) {
1525 + dispatch({
1526 + type: "SET_ERROR",
1527 + threadId,
1528 + error,
1529 + });
1530 + return;
1531 + }
1532 + setLocalDraftErrorsByThreadId((existing) => {
1533 + if ((existing[threadId] ?? null) === error) {
1534 + return existing;
1535 + }
1536 + return {
1537 + ...existing,
1538 + [threadId]: error,
1539 + };
1540 });
1541 },
1477 - [dispatch],
1542 + [dispatch, state.threads],
1543 );
1544
1545 const addComposerImages = (files: File[]) => {
@@ -1805,9 +1870,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
1870 promptRef.current.length === 0 &&
1871 composerImagesRef.current.length === 0
1872 ) {
1808 - setOptimisticUserMessages((existing) =>
1809 - existing.filter((message) => message.id !== messageIdForSend),
1810 - );
1873 + setOptimisticUserMessages((existing) => {
1874 + const removed = existing.filter((message) => message.id === messageIdForSend);
1875 + for (const message of removed) {
1876 + revokeUserMessagePreviewUrls(message);
1877 + }
1878 + const next = existing.filter((message) => message.id !== messageIdForSend);
1879 + return next.length === existing.length ? existing : next;
1880 + });
1881 promptRef.current = trimmed;
1882 setPrompt(trimmed);
1883 addComposerImagesToDraft(composerImagesSnapshot.map(cloneComposerImageForRetry));
@@ -1866,20 +1936,28 @@ export default function ChatView({ threadId }: ChatViewProps) {
1936
1937 const onModelSelect = useCallback(
1938 (model: string) => {
1869 - const normalizedModel = resolveModelSlug(model);
1870 - setComposerDraftModel(threadId, normalizedModel);
1939 + const normalizedModel = normalizeModelSlug(model);
1940 + const resolvedModel = normalizedModel ?? baseThreadModel;
1941 + setComposerDraftModel(threadId, resolvedModel === baseThreadModel ? null : resolvedModel);
1942 const api = readNativeApi();
1943 if (api && isServerThread && activeThread) {
1944 void api.orchestration.dispatchCommand({
1945 type: "thread.meta.update",
1946 commandId: newCommandId(),
1947 threadId: activeThread.id,
1877 - model: normalizedModel,
1948 + model: resolvedModel,
1949 });
1950 }
1951 scheduleComposerFocus();
1952 },
1882 - [activeThread, isServerThread, scheduleComposerFocus, setComposerDraftModel, threadId],
1953 + [
1954 + activeThread,
1955 + baseThreadModel,
1956 + isServerThread,
1957 + scheduleComposerFocus,
1958 + setComposerDraftModel,
1959 + threadId,
1960 + ],
1961 );
1962 const onEffortSelect = useCallback(
1963 (effort: ReasoningEffort) => {
apps/web/src/composerDraftStore.ts
+11 -3
@@ -1,4 +1,5 @@
1 import {
2 + DEFAULT_MODEL,
3 DEFAULT_REASONING,
4 ProjectId,
5 REASONING_OPTIONS,
@@ -119,7 +120,10 @@ const EMPTY_PERSISTED_DRAFT_STORE_STATE: PersistedComposerDraftStoreState = {
120 const EMPTY_IMAGES: ComposerImageAttachment[] = [];
121 const EMPTY_IDS: string[] = [];
122 const EMPTY_PERSISTED_ATTACHMENTS: PersistedComposerImageAttachment[] = [];
122 -const EMPTY_THREAD_DRAFT: ComposerThreadDraftState = {
123 +Object.freeze(EMPTY_IMAGES);
124 +Object.freeze(EMPTY_IDS);
125 +Object.freeze(EMPTY_PERSISTED_ATTACHMENTS);
126 +const EMPTY_THREAD_DRAFT = Object.freeze({
127 prompt: "",
128 cursor: 0,
129 images: EMPTY_IMAGES,
@@ -127,7 +131,7 @@ const EMPTY_THREAD_DRAFT: ComposerThreadDraftState = {
131 persistedAttachments: EMPTY_PERSISTED_ATTACHMENTS,
132 model: null,
133 effort: null,
130 -};
134 +}) as ComposerThreadDraftState;
135
136 const REASONING_EFFORT_VALUES = new Set<ReasoningEffort>(REASONING_OPTIONS);
137
@@ -667,7 +671,11 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
671 if (threadId.length === 0) {
672 return;
673 }
670 - const normalizedModel = normalizeModelSlug(model) ?? null;
674 + const normalizedModelCandidate = normalizeModelSlug(model);
675 + const normalizedModel =
676 + normalizedModelCandidate && normalizedModelCandidate !== DEFAULT_MODEL
677 + ? normalizedModelCandidate
678 + : null;
679 set((state) => {
680 const existing = state.draftsByThreadId[threadId];
681 if (!existing && normalizedModel === null) {