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

timelineRowContext.ts

131 lines5.5 KB

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