The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ToolCallDiffTarget } from "../../diffPanelStore"; |
| 2 | import type { SideThreadOpenIntent } from "@t3tools/client-runtime/side-thread"; |
| 3 | import type { |
| 4 | ProviderApprovalDecision, |
| 5 | ResponseContextRecord, |
| 6 | RuntimeRequestId, |
| 7 | WorktreeSetupSnapshot, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { |
| 10 | type EnvironmentId, |
| 11 | type MessageId, |
| 12 | type OrchestrationV2TurnItem, |
| 13 | type RunAttemptId, |
| 14 | type ScopedThreadRef, |
| 15 | type ServerProviderSkill, |
| 16 | type RunId, |
| 17 | ThreadId, |
| 18 | type TurnItemId, |
| 19 | } from "@t3tools/contracts"; |
| 20 | import type { AsyncResult, Atom } from "effect/reactivity"; |
| 21 | import type { CommandOutputView } from "@t3tools/client-runtime/state/command-output"; |
| 22 | import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime"; |
| 23 | import { useEffect, useRef, createContext, type MouseEvent, type ReactNode } from "react"; |
| 24 | import { type LegendListRef } from "@legendapp/list/react"; |
| 25 | import type { CodexArtifactTemplate } from "@t3tools/shared/codexArtifactTemplates"; |
| 26 | import type { PendingApproval } from "../../session-logic"; |
| 27 | import { type ChatFileAttachment } from "../../types"; |
| 28 | import { type ExpandedImageItem, ExpandedImagePreview } from "./ExpandedImagePreview"; |
| 29 | import { type AssistantCitationTarget } from "./AssistantCitationSource"; |
| 30 | import { type MessagesTimelineRow, type WorkGroupScrollAnchor } from "./MessagesTimeline.logic"; |
| 31 | import { type TimestampFormat } from "@t3tools/contracts/settings"; |
| 32 | import type { TurnItemDetailSource } from "../../state/queries"; |
| 33 | import { type HandoffTimelineRun } from "./V2LifecycleRow"; |
| 34 | |
| 35 | /** |
| 36 | * Where an expanded row reads an item's detail and a command's output. The owner's rows read them |
| 37 | * from the environment; the guest page reads them through its own routes. |
| 38 | */ |
| 39 | export interface TurnItemSource { |
| 40 | readonly detail: TurnItemDetailSource; |
| 41 | /** Unset where output does not stream: a command row then shows the output its detail holds. */ |
| 42 | readonly commandOutput?: (target: { |
| 43 | readonly threadId: ThreadId; |
| 44 | readonly itemId: TurnItemId; |
| 45 | }) => Atom.Atom<AsyncResult.AsyncResult<CommandOutputView, unknown>>; |
| 46 | } |
| 47 | |
| 48 | export interface TimelineRowSharedState { |
| 49 | pendingApprovals: ReadonlyMap<RuntimeRequestId, PendingApproval>; |
| 50 | respondingRequestIds: ReadonlyArray<RuntimeRequestId>; |
| 51 | canOperateThread: boolean; |
| 52 | onRespondToApproval: |
| 53 | | ((requestId: RuntimeRequestId, decision: ProviderApprovalDecision) => Promise<unknown>) |
| 54 | | undefined; |
| 55 | citationRequest: AssistantCitationTarget | null; |
| 56 | /** Absent hides "Reply", as in the guest view. */ |
| 57 | onReplyToResponse: ((response: ResponseContextRecord) => boolean) | undefined; |
| 58 | listRef: React.RefObject<LegendListRef | null>; |
| 59 | timestampFormat: TimestampFormat; |
| 60 | routeThreadKey: string; |
| 61 | threadRef: ScopedThreadRef | null; |
| 62 | markdownCwd: string | undefined; |
| 63 | resolvedTheme: "light" | "dark"; |
| 64 | workspaceRoot: string | undefined; |
| 65 | skills: ReadonlyArray<ServerProviderSkill>; |
| 66 | runs: ReadonlyArray<HandoffTimelineRun>; |
| 67 | activeThreadEnvironmentId: EnvironmentId; |
| 68 | onRevertToTurnCount: (targetTurnCount: number, messageId: MessageId) => void; |
| 69 | onUseArtifactTemplate: (template: CodexArtifactTemplate) => void; |
| 70 | onSendAppMessage: ((text: string) => Promise<void>) | undefined; |
| 71 | onAppFullscreenChange: (rowId: string, fullscreen: boolean) => void; |
| 72 | onRunShellCommand: ((command: string) => void) | undefined; |
| 73 | findActive: boolean; |
| 74 | onImageExpand: (preview: ExpandedImagePreview) => void; |
| 75 | displayThreadKey?: string; |
| 76 | onOpenToolDiff: ((target: ToolCallDiffTarget) => void) | undefined; |
| 77 | /** Unset where there is no diff to open, so the timeline shows no "Open diff" button. */ |
| 78 | onOpenTurnDiff: ((runId: RunId, filePath?: string) => void) | undefined; |
| 79 | /** |
| 80 | * Draws a published HTML visual. Unset, the row loads it from the environment; the guest page |
| 81 | * loads it through its own route. |
| 82 | */ |
| 83 | renderHtmlRender: ((row: Extract<TimelineRow, { kind: "html-render" }>) => ReactNode) | undefined; |
| 84 | /** Unset, expanded rows read detail and command output from the environment. */ |
| 85 | turnItemSource: TurnItemSource | undefined; |
| 86 | onOpenThread: ( |
| 87 | threadId: OrchestrationV2TurnItem["threadId"], |
| 88 | environmentId?: EnvironmentId, |
| 89 | /** How the click asked to open a subagent or side chat; a plain click when absent. */ |
| 90 | intent?: SideThreadOpenIntent, |
| 91 | ) => void; |
| 92 | workflowOpening: WorkflowOpening | null; |
| 93 | /** Absent hides "Fork from this response", as in the guest view. */ |
| 94 | onForkFromRun: |
| 95 | | ((input: { readonly sourceThreadId: ThreadId; readonly runId: RunId }) => Promise<void>) |
| 96 | | undefined; |
| 97 | onToggleTurnFold: (runId: RunId) => void; |
| 98 | onToggleAttemptFold: (attemptId: RunAttemptId) => void; |
| 99 | onFileOpen: (attachment: ChatFileAttachment) => void; |
| 100 | onFileDownload: (attachment: ChatFileAttachment) => void; |
| 101 | openPullRequest: (event: MouseEvent<HTMLElement>, url: string) => void; |
| 102 | onToggleWorkGroup: (groupId: string, anchorKey: string) => void; |
| 103 | onToggleWorkEntry: (anchorKey: string, collapsed: boolean, entryId?: string) => void; |
| 104 | onWorkEntryContentReady?: (anchorKey: string, entryId: string) => void; |
| 105 | onCancelWorktreeSetup: (() => void) | null; |
| 106 | retryableWorkspacePreparationRunIds: ReadonlySet<RunId>; |
| 107 | onRetryWorkspacePreparation: ((runId: RunId) => void) | null; |
| 108 | onWorktreeSetupWorkLocally: (() => void) | null; |
| 109 | onOpenWorktreeSetupTerminal: ((terminalId: string) => void) | null; |
| 110 | workGroupViewState: WorkGroupViewState; |
| 111 | } |
| 112 | |
| 113 | export interface TimelineRowActivityState { |
| 114 | isWorking: boolean; |
| 115 | isCompacting: boolean; |
| 116 | isRevertingCheckpoint: boolean; |
| 117 | awaitingUser: boolean; |
| 118 | isPreparingWorktree: boolean; |
| 119 | providerStartingLabel: string | null; |
| 120 | latestRunId: RunId | null; |
| 121 | backgroundWorktreeSetup: WorktreeSetupSnapshot | null; |
| 122 | liveAnswerPreview: string | null; |
| 123 | } |
| 124 | |
| 125 | export const TimelineRowCtx = createContext<TimelineRowSharedState>(null!); |
| 126 | |
| 127 | export const TimelineRowActivityCtx = createContext<TimelineRowActivityState>(null!); |
| 128 | |
| 129 | export interface WorkGroupViewState { |
| 130 | scrollPositions: Map<string, WorkGroupScrollAnchor>; |
| 131 | expandedEntries: Set<string>; |
| 132 | } |
| 133 | |
| 134 | /** One image the group's agent viewed; `item` is null until its URL resolves. */ |
| 135 | export interface WorkGroupViewedImage { |
| 136 | readonly path: string; |
| 137 | readonly item: ExpandedImageItem | null; |
| 138 | } |
| 139 | |
| 140 | export const WorkGroupViewCtx = createContext<{ |
| 141 | state: WorkGroupViewState; |
| 142 | viewedImages: ReadonlyArray<WorkGroupViewedImage>; |
| 143 | onToggleEntry: (collapsed: boolean, entryId: string) => void; |
| 144 | onContentReady?: ((entryId: string) => void) | undefined; |
| 145 | } | null>(null); |
| 146 | |
| 147 | export type TimelineWorkEntry = Extract< |
| 148 | MessagesTimelineRow, |
| 149 | { kind: "work" } |
| 150 | >["groupedEntries"][number]; |
| 151 | |
| 152 | export type TimelineRow = MessagesTimelineRow; |
| 153 | |
| 154 | export function useInitialOutputReady(pending: boolean, onReady: (() => void) | undefined) { |
| 155 | const notified = useRef(false); |
| 156 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reveals a disclosure after its first async output commits |
| 157 | useEffect(() => { |
| 158 | if (!pending && !notified.current) { |
| 159 | notified.current = true; |
| 160 | onReady?.(); |
| 161 | } |
| 162 | }, [pending, onReady]); |
| 163 | } |