Skip to content
Source
Sign inSign up

itemSupport.ts

133 lines4.6 KB

1import type { OrchestrationV2ThreadProjection, TurnItemId } from "@t3tools/contracts";
2​
3type Projection = OrchestrationV2ThreadProjection;
4​
5export interface V2ItemSupport {
6 readonly item: Projection["turnItems"][number] | null;
7 readonly run: Projection["runs"][number] | null;
8 readonly attempts: ReadonlyArray<Projection["attempts"][number]>;
9 readonly node: Projection["nodes"][number] | null;
10 readonly providerSession: Projection["providerSessions"][number] | null;
11 readonly providerThread: Projection["providerThreads"][number] | null;
12 readonly providerTurn: Projection["providerTurns"][number] | null;
13 readonly runtimeRequest: Projection["runtimeRequests"][number] | null;
14 readonly checkpoint: Projection["checkpoints"][number] | null;
15 readonly subagent: Projection["subagents"][number] | null;
16 readonly contextHandoff: Projection["contextHandoffs"][number] | null;
17 readonly contextTransfer: Projection["contextTransfers"][number] | null;
19​
20export const EMPTY_V2_ITEM_SUPPORT: V2ItemSupport = Object.freeze({
21 item: null,
22 run: null,
23 attempts: Object.freeze([]),
24 node: null,
25 providerSession: null,
26 providerThread: null,
27 providerTurn: null,
28 runtimeRequest: null,
29 checkpoint: null,
30 subagent: null,
31 contextHandoff: null,
32 contextTransfer: null,
33});
34​
35export function resolveV2ItemSupport(projection: Projection, itemId: TurnItemId): V2ItemSupport {
36 const item =
37 projection.turnItems.find((candidate) => candidate.id === itemId) ??
38 projection.visibleTurnItems.find((candidate) => candidate.sourceItemId === itemId)?.item ??
39 null;
40 if (item === null) return EMPTY_V2_ITEM_SUPPORT;
41​
42 const run =
43 item.runId === null
44 ? null
45 : (projection.runs.find((candidate) => candidate.id === item.runId) ?? null);
46 const attempts =
47 item.runId === null
48 ? EMPTY_V2_ITEM_SUPPORT.attempts
49 : projection.attempts.filter((candidate) => candidate.runId === item.runId);
50 const node =
51 item.nodeId === null
52 ? null
53 : (projection.nodes.find((candidate) => candidate.id === item.nodeId) ?? null);
54 const providerThread =
55 item.providerThreadId === null || item.providerThreadId === undefined
56 ? null
57 : (projection.providerThreads.find((candidate) => candidate.id === item.providerThreadId) ??
58 null);
59 const providerSession =
60 providerThread?.providerSessionId == null
61 ? null
62 : (projection.providerSessions.find(
63 (candidate) => candidate.id === providerThread.providerSessionId,
64 ) ?? null);
65 const providerTurn =
66 item.providerTurnId === null
67 ? null
68 : (projection.providerTurns.find((candidate) => candidate.id === item.providerTurnId) ??
69 null);
70 const requestId =
71 item.type === "approval_request" || item.type === "user_input_request"
72 ? item.requestId
73 : node?.runtimeRequestId;
74 const runtimeRequest =
75 requestId == null
76 ? null
77 : (projection.runtimeRequests.find((candidate) => candidate.id === requestId) ?? null);
78 const checkpoint =
79 item.type === "checkpoint"
80 ? (projection.checkpoints.find((candidate) => candidate.id === item.checkpointId) ?? null)
81 : null;
82 const subagent =
83 item.type === "subagent"
84 ? (projection.subagents.find((candidate) => candidate.id === item.subagentId) ?? null)
85 : null;
86 const contextHandoff =
87 item.type === "handoff"
88 ? (projection.contextHandoffs.find((candidate) => candidate.id === item.contextHandoffId) ??
89 null)
90 : null;
91 const contextTransfer =
92 contextHandoff?.transferId == null
93 ? null
94 : (projection.contextTransfers.find(
95 (candidate) => candidate.id === contextHandoff.transferId,
96 ) ?? null);
97​
98 return {
99 item,
100 run,
101 attempts,
102 node,
103 providerSession,
104 providerThread,
105 providerTurn,
106 runtimeRequest,
107 checkpoint,
108 subagent,
109 contextHandoff,
110 contextTransfer,
111 };
113​
114function sameEntities<A>(left: ReadonlyArray<A>, right: ReadonlyArray<A>): boolean {
115 return left.length === right.length && left.every((value, index) => value === right[index]);
117​
118export function v2ItemSupportEqual(left: V2ItemSupport, right: V2ItemSupport): boolean {
119 return (
120 left.item === right.item &&
121 left.run === right.run &&
122 sameEntities(left.attempts, right.attempts) &&
123 left.node === right.node &&
124 left.providerSession === right.providerSession &&
125 left.providerThread === right.providerThread &&
126 left.providerTurn === right.providerTurn &&
127 left.runtimeRequest === right.runtimeRequest &&
128 left.checkpoint === right.checkpoint &&
129 left.subagent === right.subagent &&
130 left.contextHandoff === right.contextHandoff &&
131 left.contextTransfer === right.contextTransfer
132 );