Open History to see the last commit
1import type {
2 OrchestrationV2Run,
3 OrchestrationV2RunAttempt,
4 OrchestrationV2TurnItem,
5 OrchestrationV2UserMessageInputIntent,
6} from "@t3tools/contracts";
7
8type TimelineRun = Pick<OrchestrationV2Run, "id" | "status">;
9type TimelineRunAttempt = Pick<OrchestrationV2RunAttempt, "runId" | "rootNodeId" | "status">;
10type TimelineTurnItem = Pick<OrchestrationV2TurnItem, "type" | "runId" | "nodeId"> & {
11 readonly inputIntent?: OrchestrationV2UserMessageInputIntent;
12};
13
14export function isOrchestrationV2SupersededInterrupt(input: {
15 readonly item: TimelineTurnItem;
16 readonly attempts: ReadonlyArray<TimelineRunAttempt>;
17 readonly items: ReadonlyArray<TimelineTurnItem>;
18}): boolean {
19 const { item } = input;
20 if (item.type !== "run_interrupt_result" || item.runId === null || item.nodeId === null) {
21 return false;
22 }
23
24 const isSuperseded = input.attempts.some(
25 (attempt) =>
26 attempt.runId === item.runId &&
27 attempt.rootNodeId === item.nodeId &&
28 attempt.status === "superseded",
29 );
30 if (!isSuperseded) {
31 return false;
32 }
33
34 // Show paired stop-then-steer results; hide legacy plain-steer results without matching requests.
35 const hasMatchingRequest = input.items.some(
36 (candidate) => candidate.type === "run_interrupt_request" && candidate.runId === item.runId,
37 );
38 return !hasMatchingRequest;
39}
40
41export function isOrchestrationV2TurnItemVisible(input: {
42 readonly item: TimelineTurnItem;
43 readonly runs: ReadonlyArray<TimelineRun>;
44 readonly attempts: ReadonlyArray<TimelineRunAttempt>;
45 readonly items: ReadonlyArray<TimelineTurnItem>;
46}): boolean {
47 const { item } = input;
48 if (
49 item.runId !== null &&
50 input.runs.some((run) => run.id === item.runId && run.status === "rolled_back")
51 ) {
52 return false;
53 }
54
55 // Cancelled queued input never reached the provider and must stay out of the transcript.
56 if (
57 item.type === "user_message" &&
58 item.inputIntent === "queued_turn" &&
59 item.runId !== null &&
60 input.runs.some((run) => run.id === item.runId && run.status === "cancelled")
61 ) {
62 return false;
63 }
64
65 return !isOrchestrationV2SupersededInterrupt({
66 item,
67 attempts: input.attempts,
68 items: input.items,
69 });
70}
71
72export function createOrchestrationV2TurnItemVisibility(input: {
73 readonly runs: ReadonlyArray<TimelineRun>;
74 readonly attempts: ReadonlyArray<TimelineRunAttempt>;
75 readonly items: ReadonlyArray<TimelineTurnItem>;
76}): (item: TimelineTurnItem) => boolean {
77 const statuses = new Map(input.runs.map((run) => [run.id, run.status]));
78 const supersededRoots = new Map<
79 TimelineRunAttempt["runId"],
80 Set<TimelineRunAttempt["rootNodeId"]>
81 >();
82 for (const attempt of input.attempts) {
83 if (attempt.status !== "superseded") continue;
84 let roots = supersededRoots.get(attempt.runId);
85 if (roots === undefined) supersededRoots.set(attempt.runId, (roots = new Set()));
86 roots.add(attempt.rootNodeId);
87 }
88 const interruptRuns = new Set(
89 input.items.filter((item) => item.type === "run_interrupt_request").map((item) => item.runId),
90 );
91 return (item) => {
92 const status = item.runId === null ? undefined : statuses.get(item.runId);
93 if (status === "rolled_back") return false;
94 if (
95 status === "cancelled" &&
96 item.type === "user_message" &&
97 item.inputIntent === "queued_turn"
98 )
99 return false;
100 return !(
101 item.type === "run_interrupt_result" &&
102 item.runId !== null &&
103 item.nodeId !== null &&
104 supersededRoots.get(item.runId)?.has(item.nodeId) === true &&
105 !interruptRuns.has(item.runId)
106 );
107 };
108}