Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timelineRowContext.ts

159 lines6.6 KB

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