import { Fragment, type ReactNode, createElement, useEffect } from "react";import { type ProviderKind, ThreadId, type OrchestrationReadModel, type OrchestrationSessionStatus,} from "@t3tools/contracts";import { resolveModelSlugForProvider } from "@t3tools/shared/model";import { create } from "zustand";import { type ChatMessage, type PendingThreadSend, type PendingThreadSendPhase, type Project, type Thread,} from "./types";import { Debouncer } from "@tanstack/react-pacer"; // ── State ──────────────────────────────────────────────────────────── export interface AppState { pendingThreadSendById: Record<ThreadId, PendingThreadSend | undefined>; projects: Project[]; threads: Thread[]; threadsHydrated: boolean;} const PERSISTED_STATE_KEY = "t3code:renderer-state:v8";const LEGACY_PERSISTED_STATE_KEYS = [ "t3code:renderer-state:v7", "t3code:renderer-state:v6", "t3code:renderer-state:v5", "t3code:renderer-state:v4", "t3code:renderer-state:v3", "codething:renderer-state:v4", "codething:renderer-state:v3", "codething:renderer-state:v2", "codething:renderer-state:v1",] as const; const initialState: AppState = { pendingThreadSendById: {}, projects: [], threads: [], threadsHydrated: false,};const persistedExpandedProjectCwds = new Set<string>();const persistedProjectOrderCwds: string[] = []; // ── Persist helpers ────────────────────────────────────────────────── function readPersistedState(): AppState { if (typeof window === "undefined") return initialState; try { const raw = window.localStorage.getItem(PERSISTED_STATE_KEY); if (!raw) return initialState; const parsed = JSON.parse(raw) as { expandedProjectCwds?: string[]; projectOrderCwds?: string[]; }; persistedExpandedProjectCwds.clear(); persistedProjectOrderCwds.length = 0; for (const cwd of parsed.expandedProjectCwds ?? []) { if (typeof cwd === "string" && cwd.length > 0) { persistedExpandedProjectCwds.add(cwd); } } for (const cwd of parsed.projectOrderCwds ?? []) { if (typeof cwd === "string" && cwd.length > 0 && !persistedProjectOrderCwds.includes(cwd)) { persistedProjectOrderCwds.push(cwd); } } return { ...initialState }; } catch { return initialState; }} let legacyKeysCleanedUp = false; function persistState(state: AppState): void { if (typeof window === "undefined") return; try { window.localStorage.setItem( PERSISTED_STATE_KEY, JSON.stringify({ expandedProjectCwds: state.projects .filter((project) => project.expanded) .map((project) => project.cwd), projectOrderCwds: state.projects.map((project) => project.cwd), }), ); if (!legacyKeysCleanedUp) { legacyKeysCleanedUp = true; for (const legacyKey of LEGACY_PERSISTED_STATE_KEYS) { window.localStorage.removeItem(legacyKey); } } } catch { // Ignore quota/storage errors to avoid breaking chat UX. }}const debouncedPersistState = new Debouncer(persistState, { wait: 500 }); // ── Pure helpers ────────────────────────────────────────────────────── function updateThread( threads: Thread[], threadId: ThreadId, updater: (t: Thread) => Thread,): Thread[] { let changed = false; const next = threads.map((t) => { if (t.id !== threadId) return t; const updated = updater(t); if (updated !== t) changed = true; return updated; }); return changed ? next : threads;} function hasLatestTurnCaughtUpToPendingSend( latestTurnRequestedAt: string | null, pendingSendStartedAt: string,): boolean { if (latestTurnRequestedAt === null) { return false; } const latestTurnRequestedAtMs = Date.parse(latestTurnRequestedAt); const pendingSendStartedAtMs = Date.parse(pendingSendStartedAt); if (Number.isNaN(latestTurnRequestedAtMs) || Number.isNaN(pendingSendStartedAtMs)) { return false; } return latestTurnRequestedAtMs >= pendingSendStartedAtMs;} function reconcilePendingThreadSends( pendingThreadSendById: AppState["pendingThreadSendById"], readModel: OrchestrationReadModel,): AppState["pendingThreadSendById"] { const readModelThreadById = new Map( readModel.threads.map((thread) => [thread.id, thread] as const), ); let changed = false; const nextPendingThreadSendById: AppState["pendingThreadSendById"] = {}; for (const [threadId, pendingSend] of Object.entries(pendingThreadSendById) as Array< [ThreadId, PendingThreadSend | undefined] >) { if (!pendingSend) { continue; } const thread = readModelThreadById.get(threadId); if (!thread) { nextPendingThreadSendById[threadId] = pendingSend; continue; } const authoritativeSendStarted = thread.session?.status === "running"; const authoritativeSendFailed = thread.session?.lastError != null; const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( thread.latestTurn?.requestedAt ?? null, pendingSend.startedAt, ); if ( authoritativeSendStarted || authoritativeSendFailed || (pendingSend.requestSettled && latestTurnCaughtUp) ) { changed = true; continue; } nextPendingThreadSendById[threadId] = pendingSend; } if ( !changed && Object.keys(nextPendingThreadSendById).length === Object.keys(pendingThreadSendById).length ) { return pendingThreadSendById; } return nextPendingThreadSendById;} function mapProjectsFromReadModel( incoming: OrchestrationReadModel["projects"], previous: Project[],): Project[] { const previousById = new Map(previous.map((project) => [project.id, project] as const)); const previousByCwd = new Map(previous.map((project) => [project.cwd, project] as const)); const previousOrderById = new Map(previous.map((project, index) => [project.id, index] as const)); const previousOrderByCwd = new Map( previous.map((project, index) => [project.cwd, index] as const), ); const persistedOrderByCwd = new Map( persistedProjectOrderCwds.map((cwd, index) => [cwd, index] as const), ); const usePersistedOrder = previous.length === 0; const mappedProjects = incoming.map((project) => { const existing = previousById.get(project.id) ?? previousByCwd.get(project.workspaceRoot); return { id: project.id, name: project.title, cwd: project.workspaceRoot, defaultModelSelection: existing?.defaultModelSelection ?? (project.defaultModelSelection ? { ...project.defaultModelSelection, model: resolveModelSlugForProvider( project.defaultModelSelection.provider, project.defaultModelSelection.model, ), } : null), expanded: existing?.expanded ?? (persistedExpandedProjectCwds.size > 0 ? persistedExpandedProjectCwds.has(project.workspaceRoot) : true), createdAt: project.createdAt, updatedAt: project.updatedAt, scripts: project.scripts.map((script) => ({ ...script })), } satisfies Project; }); return mappedProjects .map((project, incomingIndex) => { const previousIndex = previousOrderById.get(project.id) ?? previousOrderByCwd.get(project.cwd); const persistedIndex = usePersistedOrder ? persistedOrderByCwd.get(project.cwd) : undefined; const orderIndex = previousIndex ?? persistedIndex ?? (usePersistedOrder ? persistedProjectOrderCwds.length : previous.length) + incomingIndex; return { project, incomingIndex, orderIndex }; }) .toSorted((a, b) => { const byOrder = a.orderIndex - b.orderIndex; if (byOrder !== 0) return byOrder; return a.incomingIndex - b.incomingIndex; }) .map((entry) => entry.project);} function toLegacySessionStatus( status: OrchestrationSessionStatus,): "connecting" | "ready" | "running" | "error" | "closed" { switch (status) { case "starting": return "connecting"; case "running": return "running"; case "error": return "error"; case "ready": case "interrupted": return "ready"; case "idle": case "stopped": return "closed"; }} function toLegacyProvider(providerName: string | null): ProviderKind { if (providerName === "codex" || providerName === "claudeAgent") { return providerName; } return "codex";} function resolveWsHttpOrigin(): string { if (typeof window === "undefined") return ""; const bridgeWsUrl = window.desktopBridge?.getWsUrl?.(); const envWsUrl = import.meta.env.VITE_WS_URL as string | undefined; const wsCandidate = typeof bridgeWsUrl === "string" && bridgeWsUrl.length > 0 ? bridgeWsUrl : typeof envWsUrl === "string" && envWsUrl.length > 0 ? envWsUrl : null; if (!wsCandidate) return window.location.origin; try { const wsUrl = new URL(wsCandidate); const protocol = wsUrl.protocol === "wss:" ? "https:" : wsUrl.protocol === "ws:" ? "http:" : wsUrl.protocol; return `${protocol}//${wsUrl.host}`; } catch { return window.location.origin; }} function toAttachmentPreviewUrl(rawUrl: string): string { if (rawUrl.startsWith("/")) { return `${resolveWsHttpOrigin()}${rawUrl}`; } return rawUrl;} function attachmentPreviewRoutePath(attachmentId: string): string { return `/attachments/${encodeURIComponent(attachmentId)}`;} // ── Pure state transition functions ──────────────────────────────────── export function syncServerReadModel(state: AppState, readModel: OrchestrationReadModel): AppState { const projects = mapProjectsFromReadModel( readModel.projects.filter((project) => project.deletedAt === null), state.projects, ); const existingThreadById = new Map(state.threads.map((thread) => [thread.id, thread] as const)); const threads = readModel.threads .filter((thread) => thread.deletedAt === null) .map((thread) => { const existing = existingThreadById.get(thread.id); return { id: thread.id, codexThreadId: null, projectId: thread.projectId, title: thread.title, modelSelection: { ...thread.modelSelection, model: resolveModelSlugForProvider( thread.modelSelection.provider, thread.modelSelection.model, ), }, runtimeMode: thread.runtimeMode, interactionMode: thread.interactionMode, session: thread.session ? { provider: toLegacyProvider(thread.session.providerName), status: toLegacySessionStatus(thread.session.status), orchestrationStatus: thread.session.status, activeTurnId: thread.session.activeTurnId ?? undefined, createdAt: thread.session.updatedAt, updatedAt: thread.session.updatedAt, ...(thread.session.lastError ? { lastError: thread.session.lastError } : {}), } : null, messages: thread.messages.map((message) => { const attachments = message.attachments?.map((attachment) => ({ type: "image" as const, id: attachment.id, name: attachment.name, mimeType: attachment.mimeType, sizeBytes: attachment.sizeBytes, previewUrl: toAttachmentPreviewUrl(attachmentPreviewRoutePath(attachment.id)), })); const normalizedMessage: ChatMessage = { id: message.id, role: message.role, text: message.text, createdAt: message.createdAt, streaming: message.streaming, ...(message.streaming ? {} : { completedAt: message.updatedAt }), ...(attachments && attachments.length > 0 ? { attachments } : {}), }; return normalizedMessage; }), proposedPlans: thread.proposedPlans.map((proposedPlan) => ({ id: proposedPlan.id, turnId: proposedPlan.turnId, planMarkdown: proposedPlan.planMarkdown, implementedAt: proposedPlan.implementedAt, implementationThreadId: proposedPlan.implementationThreadId, createdAt: proposedPlan.createdAt, updatedAt: proposedPlan.updatedAt, })), error: thread.session?.lastError ?? null, createdAt: thread.createdAt, archivedAt: thread.archivedAt, updatedAt: thread.updatedAt, latestTurn: thread.latestTurn, lastVisitedAt: existing?.lastVisitedAt ?? thread.updatedAt, branch: thread.branch, worktreePath: thread.worktreePath, turnDiffSummaries: thread.checkpoints.map((checkpoint) => ({ turnId: checkpoint.turnId, completedAt: checkpoint.completedAt, status: checkpoint.status, assistantMessageId: checkpoint.assistantMessageId ?? undefined, checkpointTurnCount: checkpoint.checkpointTurnCount, checkpointRef: checkpoint.checkpointRef, files: checkpoint.files.map((file) => ({ ...file })), })), activities: thread.activities.map((activity) => ({ ...activity })), }; }); return { ...state, pendingThreadSendById: reconcilePendingThreadSends(state.pendingThreadSendById, readModel), projects, threads, threadsHydrated: true, };} export function beginThreadSend( state: AppState, threadId: ThreadId, phase: PendingThreadSendPhase, startedAt: string,): AppState { const existing = state.pendingThreadSendById[threadId]; if ( existing?.phase === phase && existing.startedAt === startedAt && existing.requestSettled === false ) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { phase, startedAt, requestSettled: false }, }, };} export function setThreadSendPhase( state: AppState, threadId: ThreadId, phase: PendingThreadSendPhase,): AppState { const existing = state.pendingThreadSendById[threadId]; if (!existing || existing.phase === phase) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { ...existing, phase, }, }, };} export function markThreadSendSettled(state: AppState, threadId: ThreadId): AppState { const existing = state.pendingThreadSendById[threadId]; if (!existing) { return state; } const thread = state.threads.find((candidate) => candidate.id === threadId) ?? null; const authoritativeSendStarted = thread?.session?.orchestrationStatus === "running"; const authoritativeSendFailed = thread?.session?.lastError != null; const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( thread?.latestTurn?.requestedAt ?? null, existing.startedAt, ); if (authoritativeSendStarted || authoritativeSendFailed || latestTurnCaughtUp) { return clearThreadSend(state, threadId); } if (existing.requestSettled) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { ...existing, requestSettled: true, }, }, };} export function clearThreadSend(state: AppState, threadId: ThreadId): AppState { if (!(threadId in state.pendingThreadSendById)) { return state; } const pendingThreadSendById = { ...state.pendingThreadSendById }; delete pendingThreadSendById[threadId]; return { ...state, pendingThreadSendById, };} export function moveThreadSend( state: AppState, fromThreadId: ThreadId, toThreadId: ThreadId,): AppState { if (fromThreadId === toThreadId) { return state; } const pendingSend = state.pendingThreadSendById[fromThreadId]; if (!pendingSend) { return state; }Lines 1–500 of 664. Load more
import { Fragment, type ReactNode, createElement, useEffect } from "react"; import { type ProviderKind, ThreadId, type OrchestrationReadModel, type OrchestrationSessionStatus, } from "@t3tools/contracts"; import { resolveModelSlugForProvider } from "@t3tools/shared/model"; import { create } from "zustand"; import { type ChatMessage, type PendingThreadSend, type PendingThreadSendPhase, type Project, type Thread, } from "./types"; import { Debouncer } from "@tanstack/react-pacer"; // ── State ──────────────────────────────────────────────────────────── export interface AppState { pendingThreadSendById: Record<ThreadId, PendingThreadSend | undefined>; projects: Project[]; threads: Thread[]; threadsHydrated: boolean; } const PERSISTED_STATE_KEY = "t3code:renderer-state:v8"; const LEGACY_PERSISTED_STATE_KEYS = [ "t3code:renderer-state:v7", "t3code:renderer-state:v6", "t3code:renderer-state:v5", "t3code:renderer-state:v4", "t3code:renderer-state:v3", "codething:renderer-state:v4", "codething:renderer-state:v3", "codething:renderer-state:v2", "codething:renderer-state:v1", ] as const; const initialState: AppState = { pendingThreadSendById: {}, projects: [], threads: [], threadsHydrated: false, }; const persistedExpandedProjectCwds = new Set<string>(); const persistedProjectOrderCwds: string[] = []; // ── Persist helpers ────────────────────────────────────────────────── function readPersistedState(): AppState { if (typeof window === "undefined") return initialState; try { const raw = window.localStorage.getItem(PERSISTED_STATE_KEY); if (!raw) return initialState; const parsed = JSON.parse(raw) as { expandedProjectCwds?: string[]; projectOrderCwds?: string[]; }; persistedExpandedProjectCwds.clear(); persistedProjectOrderCwds.length = 0; for (const cwd of parsed.expandedProjectCwds ?? []) { if (typeof cwd === "string" && cwd.length > 0) { persistedExpandedProjectCwds.add(cwd); } } for (const cwd of parsed.projectOrderCwds ?? []) { if (typeof cwd === "string" && cwd.length > 0 && !persistedProjectOrderCwds.includes(cwd)) { persistedProjectOrderCwds.push(cwd); } } return { ...initialState }; } catch { return initialState; } } let legacyKeysCleanedUp = false; function persistState(state: AppState): void { if (typeof window === "undefined") return; try { window.localStorage.setItem( PERSISTED_STATE_KEY, JSON.stringify({ expandedProjectCwds: state.projects .filter((project) => project.expanded) .map((project) => project.cwd), projectOrderCwds: state.projects.map((project) => project.cwd), }), ); if (!legacyKeysCleanedUp) { legacyKeysCleanedUp = true; for (const legacyKey of LEGACY_PERSISTED_STATE_KEYS) { window.localStorage.removeItem(legacyKey); } } } catch { // Ignore quota/storage errors to avoid breaking chat UX. } } const debouncedPersistState = new Debouncer(persistState, { wait: 500 }); // ── Pure helpers ────────────────────────────────────────────────────── function updateThread( threads: Thread[], threadId: ThreadId, updater: (t: Thread) => Thread, ): Thread[] { let changed = false; const next = threads.map((t) => { if (t.id !== threadId) return t; const updated = updater(t); if (updated !== t) changed = true; return updated; }); return changed ? next : threads; } function hasLatestTurnCaughtUpToPendingSend( latestTurnRequestedAt: string | null, pendingSendStartedAt: string, ): boolean { if (latestTurnRequestedAt === null) { return false; } const latestTurnRequestedAtMs = Date.parse(latestTurnRequestedAt); const pendingSendStartedAtMs = Date.parse(pendingSendStartedAt); if (Number.isNaN(latestTurnRequestedAtMs) || Number.isNaN(pendingSendStartedAtMs)) { return false; } return latestTurnRequestedAtMs >= pendingSendStartedAtMs; } function reconcilePendingThreadSends( pendingThreadSendById: AppState["pendingThreadSendById"], readModel: OrchestrationReadModel, ): AppState["pendingThreadSendById"] { const readModelThreadById = new Map( readModel.threads.map((thread) => [thread.id, thread] as const), ); let changed = false; const nextPendingThreadSendById: AppState["pendingThreadSendById"] = {}; for (const [threadId, pendingSend] of Object.entries(pendingThreadSendById) as Array< [ThreadId, PendingThreadSend | undefined] >) { if (!pendingSend) { continue; } const thread = readModelThreadById.get(threadId); if (!thread) { nextPendingThreadSendById[threadId] = pendingSend; continue; } const authoritativeSendStarted = thread.session?.status === "running"; const authoritativeSendFailed = thread.session?.lastError != null; const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( thread.latestTurn?.requestedAt ?? null, pendingSend.startedAt, ); if ( authoritativeSendStarted || authoritativeSendFailed || (pendingSend.requestSettled && latestTurnCaughtUp) ) { changed = true; continue; } nextPendingThreadSendById[threadId] = pendingSend; } if ( !changed && Object.keys(nextPendingThreadSendById).length === Object.keys(pendingThreadSendById).length ) { return pendingThreadSendById; } return nextPendingThreadSendById; } function mapProjectsFromReadModel( incoming: OrchestrationReadModel["projects"], previous: Project[], ): Project[] { const previousById = new Map(previous.map((project) => [project.id, project] as const)); const previousByCwd = new Map(previous.map((project) => [project.cwd, project] as const)); const previousOrderById = new Map(previous.map((project, index) => [project.id, index] as const)); const previousOrderByCwd = new Map( previous.map((project, index) => [project.cwd, index] as const), ); const persistedOrderByCwd = new Map( persistedProjectOrderCwds.map((cwd, index) => [cwd, index] as const), ); const usePersistedOrder = previous.length === 0; const mappedProjects = incoming.map((project) => { const existing = previousById.get(project.id) ?? previousByCwd.get(project.workspaceRoot); return { id: project.id, name: project.title, cwd: project.workspaceRoot, defaultModelSelection: existing?.defaultModelSelection ?? (project.defaultModelSelection ? { ...project.defaultModelSelection, model: resolveModelSlugForProvider( project.defaultModelSelection.provider, project.defaultModelSelection.model, ), } : null), expanded: existing?.expanded ?? (persistedExpandedProjectCwds.size > 0 ? persistedExpandedProjectCwds.has(project.workspaceRoot) : true), createdAt: project.createdAt, updatedAt: project.updatedAt, scripts: project.scripts.map((script) => ({ ...script })), } satisfies Project; }); return mappedProjects .map((project, incomingIndex) => { const previousIndex = previousOrderById.get(project.id) ?? previousOrderByCwd.get(project.cwd); const persistedIndex = usePersistedOrder ? persistedOrderByCwd.get(project.cwd) : undefined; const orderIndex = previousIndex ?? persistedIndex ?? (usePersistedOrder ? persistedProjectOrderCwds.length : previous.length) + incomingIndex; return { project, incomingIndex, orderIndex }; }) .toSorted((a, b) => { const byOrder = a.orderIndex - b.orderIndex; if (byOrder !== 0) return byOrder; return a.incomingIndex - b.incomingIndex; }) .map((entry) => entry.project); } function toLegacySessionStatus( status: OrchestrationSessionStatus, ): "connecting" | "ready" | "running" | "error" | "closed" { switch (status) { case "starting": return "connecting"; case "running": return "running"; case "error": return "error"; case "ready": case "interrupted": return "ready"; case "idle": case "stopped": return "closed"; } } function toLegacyProvider(providerName: string | null): ProviderKind { if (providerName === "codex" || providerName === "claudeAgent") { return providerName; } return "codex"; } function resolveWsHttpOrigin(): string { if (typeof window === "undefined") return ""; const bridgeWsUrl = window.desktopBridge?.getWsUrl?.(); const envWsUrl = import.meta.env.VITE_WS_URL as string | undefined; const wsCandidate = typeof bridgeWsUrl === "string" && bridgeWsUrl.length > 0 ? bridgeWsUrl : typeof envWsUrl === "string" && envWsUrl.length > 0 ? envWsUrl : null; if (!wsCandidate) return window.location.origin; try { const wsUrl = new URL(wsCandidate); const protocol = wsUrl.protocol === "wss:" ? "https:" : wsUrl.protocol === "ws:" ? "http:" : wsUrl.protocol; return `${protocol}//${wsUrl.host}`; } catch { return window.location.origin; } } function toAttachmentPreviewUrl(rawUrl: string): string { if (rawUrl.startsWith("/")) { return `${resolveWsHttpOrigin()}${rawUrl}`; } return rawUrl; } function attachmentPreviewRoutePath(attachmentId: string): string { return `/attachments/${encodeURIComponent(attachmentId)}`; } // ── Pure state transition functions ──────────────────────────────────── export function syncServerReadModel(state: AppState, readModel: OrchestrationReadModel): AppState { const projects = mapProjectsFromReadModel( readModel.projects.filter((project) => project.deletedAt === null), state.projects, ); const existingThreadById = new Map(state.threads.map((thread) => [thread.id, thread] as const)); const threads = readModel.threads .filter((thread) => thread.deletedAt === null) .map((thread) => { const existing = existingThreadById.get(thread.id); return { id: thread.id, codexThreadId: null, projectId: thread.projectId, title: thread.title, modelSelection: { ...thread.modelSelection, model: resolveModelSlugForProvider( thread.modelSelection.provider, thread.modelSelection.model, ), }, runtimeMode: thread.runtimeMode, interactionMode: thread.interactionMode, session: thread.session ? { provider: toLegacyProvider(thread.session.providerName), status: toLegacySessionStatus(thread.session.status), orchestrationStatus: thread.session.status, activeTurnId: thread.session.activeTurnId ?? undefined, createdAt: thread.session.updatedAt, updatedAt: thread.session.updatedAt, ...(thread.session.lastError ? { lastError: thread.session.lastError } : {}), } : null, messages: thread.messages.map((message) => { const attachments = message.attachments?.map((attachment) => ({ type: "image" as const, id: attachment.id, name: attachment.name, mimeType: attachment.mimeType, sizeBytes: attachment.sizeBytes, previewUrl: toAttachmentPreviewUrl(attachmentPreviewRoutePath(attachment.id)), })); const normalizedMessage: ChatMessage = { id: message.id, role: message.role, text: message.text, createdAt: message.createdAt, streaming: message.streaming, ...(message.streaming ? {} : { completedAt: message.updatedAt }), ...(attachments && attachments.length > 0 ? { attachments } : {}), }; return normalizedMessage; }), proposedPlans: thread.proposedPlans.map((proposedPlan) => ({ id: proposedPlan.id, turnId: proposedPlan.turnId, planMarkdown: proposedPlan.planMarkdown, implementedAt: proposedPlan.implementedAt, implementationThreadId: proposedPlan.implementationThreadId, createdAt: proposedPlan.createdAt, updatedAt: proposedPlan.updatedAt, })), error: thread.session?.lastError ?? null, createdAt: thread.createdAt, archivedAt: thread.archivedAt, updatedAt: thread.updatedAt, latestTurn: thread.latestTurn, lastVisitedAt: existing?.lastVisitedAt ?? thread.updatedAt, branch: thread.branch, worktreePath: thread.worktreePath, turnDiffSummaries: thread.checkpoints.map((checkpoint) => ({ turnId: checkpoint.turnId, completedAt: checkpoint.completedAt, status: checkpoint.status, assistantMessageId: checkpoint.assistantMessageId ?? undefined, checkpointTurnCount: checkpoint.checkpointTurnCount, checkpointRef: checkpoint.checkpointRef, files: checkpoint.files.map((file) => ({ ...file })), })), activities: thread.activities.map((activity) => ({ ...activity })), }; }); return { ...state, pendingThreadSendById: reconcilePendingThreadSends(state.pendingThreadSendById, readModel), projects, threads, threadsHydrated: true, }; } export function beginThreadSend( state: AppState, threadId: ThreadId, phase: PendingThreadSendPhase, startedAt: string, ): AppState { const existing = state.pendingThreadSendById[threadId]; if ( existing?.phase === phase && existing.startedAt === startedAt && existing.requestSettled === false ) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { phase, startedAt, requestSettled: false }, }, }; } export function setThreadSendPhase( state: AppState, threadId: ThreadId, phase: PendingThreadSendPhase, ): AppState { const existing = state.pendingThreadSendById[threadId]; if (!existing || existing.phase === phase) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { ...existing, phase, }, }, }; } export function markThreadSendSettled(state: AppState, threadId: ThreadId): AppState { const existing = state.pendingThreadSendById[threadId]; if (!existing) { return state; } const thread = state.threads.find((candidate) => candidate.id === threadId) ?? null; const authoritativeSendStarted = thread?.session?.orchestrationStatus === "running"; const authoritativeSendFailed = thread?.session?.lastError != null; const latestTurnCaughtUp = hasLatestTurnCaughtUpToPendingSend( thread?.latestTurn?.requestedAt ?? null, existing.startedAt, ); if (authoritativeSendStarted || authoritativeSendFailed || latestTurnCaughtUp) { return clearThreadSend(state, threadId); } if (existing.requestSettled) { return state; } return { ...state, pendingThreadSendById: { ...state.pendingThreadSendById, [threadId]: { ...existing, requestSettled: true, }, }, }; } export function clearThreadSend(state: AppState, threadId: ThreadId): AppState { if (!(threadId in state.pendingThreadSendById)) { return state; } const pendingThreadSendById = { ...state.pendingThreadSendById }; delete pendingThreadSendById[threadId]; return { ...state, pendingThreadSendById, }; } export function moveThreadSend( state: AppState, fromThreadId: ThreadId, toThreadId: ThreadId, ): AppState { if (fromThreadId === toThreadId) { return state; } const pendingSend = state.pendingThreadSendById[fromThreadId]; if (!pendingSend) { return state; }