Open History to see the last commit
1import type { WorktreeSetupSnapshot } from "@t3tools/contracts";
2import {
3 type EnvironmentId,
4 type MessageId,
5 type OrchestrationV2TurnItem,
6 type RunAttemptId,
7 type ScopedThreadRef,
8 type ServerProviderSkill,
9 type RunId,
10 ThreadId,
11 type TurnItemId,
12} from "@t3tools/contracts";
13import type { AsyncResult, Atom } from "effect/reactivity";
14import type { CommandOutputView } from "@t3tools/client-runtime/state/command-output";
15import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime";
16import { createContext, type MouseEvent, type ReactNode } from "react";
17import { type LegendListRef } from "@legendapp/list/react";
18import type { CodexArtifactTemplate } from "@t3tools/shared/codexArtifactTemplates";
19import { type ChatFileAttachment } from "../../types";
20import { ExpandedImagePreview } from "./ExpandedImagePreview";
21import { type AssistantCitationTarget } from "./AssistantCitationSource";
22import { type MessagesTimelineRow, type WorkGroupScrollAnchor } from "./MessagesTimeline.logic";
23import { type TimestampFormat } from "@t3tools/contracts/settings";
24import type { TurnItemDetailSource } from "../../state/queries";
25import { type HandoffTimelineRun } from "./V2LifecycleRow";
26
27/**
28 * Where an expanded row reads an item's detail and a command's output. The owner's rows read them
29 * from the environment; the guest page reads them through its own routes.
30 */
31export interface TurnItemSource {
32 readonly detail: TurnItemDetailSource;
33 readonly commandOutput: (target: {
34 readonly threadId: ThreadId;
35 readonly itemId: TurnItemId;
36 }) => Atom.Atom<AsyncResult.AsyncResult<CommandOutputView, unknown>>;
37}
38
39export interface TimelineRowSharedState {
40 citationRequest: AssistantCitationTarget | null;
41 listRef: React.RefObject<LegendListRef | null>;
42 timestampFormat: TimestampFormat;
43 routeThreadKey: string;
44 threadRef: ScopedThreadRef | null;
45 markdownCwd: string | undefined;
46 resolvedTheme: "light" | "dark";
47 workspaceRoot: string | undefined;
48 skills: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">>;
49 runs: ReadonlyArray<HandoffTimelineRun>;
50 activeThreadEnvironmentId: EnvironmentId;
51 onRevertToTurnCount: (targetTurnCount: number, messageId: MessageId) => void;
52 onUseArtifactTemplate: (template: CodexArtifactTemplate) => void;
53 onSendAppMessage: ((text: string) => Promise<void>) | undefined;
54 onAppFullscreenChange: (rowId: string, fullscreen: boolean) => void;
55 onRunShellCommand: ((command: string) => void) | undefined;
56 findActive: boolean;
57 onImageExpand: (preview: ExpandedImagePreview) => void;
58 displayThreadKey?: string;
59 /** Unset where there is no diff to open, so the timeline shows no "Open diff" button. */
60 onOpenTurnDiff: ((runId: RunId, filePath?: string) => void) | undefined;
61 /**
62 * Draws a published HTML visual. Unset, the row loads it from the environment; the guest page
63 * loads it through its own route.
64 */
65 renderHtmlRender: ((row: Extract<TimelineRow, { kind: "html-render" }>) => ReactNode) | undefined;
66 /** Unset, expanded rows read detail and command output from the environment. */
67 turnItemSource: TurnItemSource | undefined;
68 onOpenThread: (threadId: OrchestrationV2TurnItem["threadId"]) => void;
69 workflowOpening: WorkflowOpening | null;
70 /** Absent hides "Fork from this response", as in the guest view. */
71 onForkFromRun:
72 | ((input: { readonly sourceThreadId: ThreadId; readonly runId: RunId }) => Promise<void>)
73 | undefined;
74 onRollbackCheckpoint: (input: {
75 readonly checkpointId: string;
76 readonly scopeId: string;
77 }) => void;
78 onToggleTurnFold: (runId: RunId) => void;
79 onToggleAttemptFold: (attemptId: RunAttemptId) => void;
80 onFileOpen: (attachment: ChatFileAttachment) => void;
81 onFileDownload: (attachment: ChatFileAttachment) => void;
82 openPullRequest: (event: MouseEvent<HTMLElement>, url: string) => void;
83 onToggleWorkGroup: (groupId: string, anchorKey: string) => void;
84 onToggleWorkEntry: (anchorKey: string, collapsed: boolean) => void;
85 onCancelWorktreeSetup: (() => void) | null;
86 retryableWorkspacePreparationRunIds: ReadonlySet<RunId>;
87 onRetryWorkspacePreparation: ((runId: RunId) => void) | null;
88 onWorktreeSetupWorkLocally: (() => void) | null;
89 onOpenWorktreeSetupTerminal: ((terminalId: string) => void) | null;
90 workGroupViewState: WorkGroupViewState;
91}
92
93export interface TimelineRowActivityState {
94 isWorking: boolean;
95 isCompacting: boolean;
96 isRevertingCheckpoint: boolean;
97 activeTurnInProgress: boolean;
98 awaitingUser: boolean;
99 isPreparingWorktree: boolean;
100 latestRunId: RunId | null;
101 backgroundWorktreeSetup: WorktreeSetupSnapshot | null;
102}
103
104export const TimelineRowCtx = createContext<TimelineRowSharedState>(null!);
105
106export const TimelineRowActivityCtx = createContext<TimelineRowActivityState>(null!);
107
108export interface WorkGroupViewState {
109 scrollPositions: Map<string, WorkGroupScrollAnchor>;
110 expandedEntries: Set<string>;
111}
112
113export const WorkGroupViewCtx = createContext<{
114 state: WorkGroupViewState;
115 onToggleEntry: (collapsed: boolean) => void;
116} | null>(null);
117
118export type TimelineWorkEntry = Extract<
119 MessagesTimelineRow,
120 { kind: "work" }
121>["groupedEntries"][number];
122
123export type TimelineRow = MessagesTimelineRow;