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

timelineRowContext.ts

163 lines6.9 KB

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