The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { LegendListRef } from "@legendapp/list/react"; |
| 2 | import { |
| 3 | deriveRunlessWorkStartedAt, |
| 4 | deriveThreadActivityRun, |
| 5 | deriveThreadStructureRuntime, |
| 6 | } from "@t3tools/client-runtime/state/thread-execution"; |
| 7 | import { createQuestionHistoryProjector } from "@t3tools/client-runtime/state/thread-requests"; |
| 8 | import { |
| 9 | EnvironmentId, |
| 10 | type OrchestrationV2ProjectedTurnItem, |
| 11 | type OrchestrationV2ThreadProjection, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { useRef, useState, type ReactNode } from "react"; |
| 14 | |
| 15 | import { useIncrementalTimelineEntries } from "~/hooks/useIncrementalTimelineEntries"; |
| 16 | import { SideThreadsOpenContext } from "~/hooks/useSideThreadTrigger"; |
| 17 | import { useTheme } from "~/hooks/useTheme"; |
| 18 | import { deriveActiveWorkStartedAt, derivePhase } from "~/session-logic"; |
| 19 | import type { ChatFileAttachment } from "~/types"; |
| 20 | |
| 21 | import type { ExpandedImagePreview } from "./ExpandedImagePreview"; |
| 22 | import { MessagesTimeline, type MessagesTimelineHistoryControls } from "./MessagesTimeline"; |
| 23 | import { ScrollToEndButton } from "./TimelineOverlays"; |
| 24 | import type { TimelineRow, TurnItemSource } from "./timelineRowContext"; |
| 25 | import { useTimelineFollow } from "./useTimelineFollow"; |
| 26 | |
| 27 | // The timeline needs an environment id to key its rows. No environment answers to this one, so a |
| 28 | // row that would ask the thread's environment for something gets nothing. |
| 29 | const NO_ENVIRONMENT_ID = EnvironmentId.make("guest"); |
| 30 | const NO_TURN_DIFFS: ReadonlyArray<never> = []; |
| 31 | const NO_OPTIMISTIC_MESSAGES: ReadonlyArray<never> = []; |
| 32 | const ignore = () => {}; |
| 33 | |
| 34 | /** |
| 35 | * A thread's own timeline, built from its projection alone and read-only: every action that would |
| 36 | * reach the thread's environment (fork, revert, reply, diffs, files, other threads, shell, |
| 37 | * approvals) is left out, so its control does not render. The guest page and a linked |
| 38 | * environment's thread both show a thread this way. |
| 39 | */ |
| 40 | export function ReadOnlyThreadTimeline(props: { |
| 41 | /** Keys the list. It holds no ":" so the timeline finds no environment thread behind it. */ |
| 42 | readonly threadKey: string; |
| 43 | readonly projection: OrchestrationV2ThreadProjection; |
| 44 | readonly historyControls: MessagesTimelineHistoryControls | undefined; |
| 45 | /** Where expanded rows read an item's detail. */ |
| 46 | readonly turnItemSource: TurnItemSource; |
| 47 | /** Preview URLs for the messages' attachments, keyed by attachment id. */ |
| 48 | readonly attachmentUrls: ( |
| 49 | items: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 50 | ) => ReadonlyMap<string, string>; |
| 51 | readonly onImageExpand: (preview: ExpandedImagePreview) => void; |
| 52 | readonly onFileOpen: (file: ChatFileAttachment) => void; |
| 53 | readonly renderHtmlRender: (row: Extract<TimelineRow, { kind: "html-render" }>) => ReactNode; |
| 54 | readonly renderFooter?: (items: ReadonlyArray<OrchestrationV2ProjectedTurnItem>) => ReactNode; |
| 55 | /** What stands below the timeline, where the composer would. */ |
| 56 | readonly children: (controls: { readonly scrollToEnd: () => void }) => ReactNode; |
| 57 | }) { |
| 58 | const { threadKey, projection } = props; |
| 59 | const listRef = useRef<LegendListRef | null>(null); |
| 60 | const { resolvedTheme } = useTheme(); |
| 61 | // The rest is what ChatView derives from the same projection. |
| 62 | const [projectQuestionHistory] = useState(() => createQuestionHistoryProjector()); |
| 63 | const items = projectQuestionHistory(projection); |
| 64 | const entries = useIncrementalTimelineEntries(threadKey, { |
| 65 | visibleTurnItems: items, |
| 66 | optimisticMessages: NO_OPTIMISTIC_MESSAGES, |
| 67 | attachmentUrlById: props.attachmentUrls(items), |
| 68 | attempts: projection.attempts, |
| 69 | nodes: projection.nodes, |
| 70 | plans: projection.plans, |
| 71 | }); |
| 72 | const activityRun = deriveThreadActivityRun(projection); |
| 73 | const runtime = deriveThreadStructureRuntime(projection); |
| 74 | const runlessWorkStartedAt = deriveRunlessWorkStartedAt(projection); |
| 75 | const isWorking = derivePhase(runtime) === "running" || runlessWorkStartedAt !== null; |
| 76 | const workStartedAt = |
| 77 | deriveActiveWorkStartedAt(activityRun, runtime, null) ?? runlessWorkStartedAt; |
| 78 | const runningRunId = runtime?.activeRunId ?? null; |
| 79 | const follow = useTimelineFollow({ |
| 80 | legendListRef: listRef, |
| 81 | composerTimelineInset: 0, |
| 82 | routeThreadKey: threadKey, |
| 83 | activeThreadKey: threadKey, |
| 84 | // Not a server thread: new turns follow the end instead of anchoring near the top. |
| 85 | isServerThread: false, |
| 86 | serverProjection: null, |
| 87 | threadStatus: "live", |
| 88 | activeActivityRun: activityRun, |
| 89 | activeRunningTurnId: runningRunId, |
| 90 | timelineEntries: entries, |
| 91 | timelineAnchorMessageId: null, |
| 92 | setTimelineAnchor: ignore, |
| 93 | }); |
| 94 | return ( |
| 95 | <> |
| 96 | <div className="relative min-h-0 flex-1"> |
| 97 | <SideThreadsOpenContext value={false}> |
| 98 | <MessagesTimeline |
| 99 | listRef={listRef} |
| 100 | timelineEntries={entries} |
| 101 | isWorking={isWorking} |
| 102 | activeTurnStartedAt={workStartedAt} |
| 103 | latestRun={activityRun} |
| 104 | runningRunId={runningRunId} |
| 105 | turnDiffSummaries={NO_TURN_DIFFS} |
| 106 | runs={projection.runs} |
| 107 | routeThreadKey={threadKey} |
| 108 | onOpenThread={ignore} |
| 109 | supportsConversationRollback={false} |
| 110 | onRevertToTurnCount={ignore} |
| 111 | isRevertingCheckpoint={false} |
| 112 | onImageExpand={props.onImageExpand} |
| 113 | onFileOpen={props.onFileOpen} |
| 114 | onFileDownload={props.onFileOpen} |
| 115 | renderHtmlRender={props.renderHtmlRender} |
| 116 | turnItemSource={props.turnItemSource} |
| 117 | historyControls={props.historyControls} |
| 118 | activeThreadEnvironmentId={NO_ENVIRONMENT_ID} |
| 119 | markdownCwd={undefined} |
| 120 | resolvedTheme={resolvedTheme} |
| 121 | timestampFormat="locale" |
| 122 | workspaceRoot={undefined} |
| 123 | anchorMessageId={null} |
| 124 | onAnchorReady={ignore} |
| 125 | onAnchorSizeChanged={ignore} |
| 126 | contentInsetEndAdjustment={0} |
| 127 | emptyPlaceholderInset={0} |
| 128 | liveFollowEnabled={follow.timelineLiveFollowEnabled} |
| 129 | onIsAtEndChange={follow.onIsAtEndChangeStable} |
| 130 | onViewportAttach={follow.attachTimelineViewport} |
| 131 | onFooterGrow={follow.onTimelineFooterGrowStable} |
| 132 | footer={props.renderFooter?.(items) ?? null} |
| 133 | /> |
| 134 | </SideThreadsOpenContext> |
| 135 | {follow.showScrollToBottom ? ( |
| 136 | <ScrollToEndButton |
| 137 | summary={null} |
| 138 | bottom={4} |
| 139 | onScrollToEnd={() => follow.scrollToEnd(true)} |
| 140 | /> |
| 141 | ) : null} |
| 142 | </div> |
| 143 | {props.children({ scrollToEnd: () => follow.scrollToEnd(true) })} |
| 144 | </> |
| 145 | ); |
| 146 | } |