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

SubagentProjection.ts

185 lines5.3 KB

1import type {
2 MessageId,
3 ModelSelection,
4 NodeId,
5 OrchestrationV2AppThread,
6 OrchestrationV2Actor,
7 OrchestrationV2ConversationMessage,
8 OrchestrationV2CreationSource,
9 OrchestrationV2ProviderRef,
10 OrchestrationV2Run,
11 OrchestrationV2ThreadProjection,
12 OrchestrationV2TurnItem,
13 ProviderInstanceId,
14 ProviderThreadId,
15 ProviderTurnId,
16 ThreadId,
17 TurnItemId,
18} from "@t3tools/contracts";
19import * as DateTime from "effect/DateTime";
20​
21function trimmed(value: string | null | undefined): string | undefined {
22 const result = value?.trim();
23 return result && result.length > 0 ? result : undefined;
25​
26export function subagentThreadTitle(input: {
27 readonly parentTitle: string;
28 readonly title?: string | null;
29 readonly prompt: string;
30 readonly ordinal: number;
31}): string {
32 const detail = trimmed(input.title) ?? trimmed(input.prompt);
33 if (detail === undefined) {
34 return `${input.parentTitle} subagent ${input.ordinal}`;
35 }
36 const clipped = detail.length > 72 ? `${detail.slice(0, 69)}...` : detail;
37 return clipped;
39​
40export function makeSubagentChildThread(input: {
41 readonly parentThread: OrchestrationV2AppThread;
42 readonly childThreadId: ThreadId;
43 readonly parentNodeId: NodeId;
44 readonly activeProviderThreadId: ProviderThreadId | null;
45 readonly providerInstanceId: ProviderInstanceId;
46 readonly modelSelection: ModelSelection;
47 readonly title: string;
48 readonly now: DateTime.Utc;
49 readonly createdBy: OrchestrationV2Actor;
50 readonly creationSource: OrchestrationV2CreationSource;
51}): OrchestrationV2AppThread {
52 return {
53 ...input.parentThread,
54 createdBy: input.createdBy,
55 creationSource: input.creationSource,
56 id: input.childThreadId,
57 title: input.title,
58 providerInstanceId: input.providerInstanceId,
59 modelSelection: input.modelSelection,
60 activeProviderThreadId: input.activeProviderThreadId,
61 lineage: {
62 parentThreadId: input.parentThread.id,
63 relationshipToParent: "subagent",
64 rootThreadId: input.parentThread.lineage.rootThreadId,
65 },
66 forkedFrom: {
67 type: "node",
68 nodeId: input.parentNodeId,
69 },
70 createdAt: input.now,
71 updatedAt: input.now,
72 archivedAt: null,
73 deletedAt: null,
74 };
76​
77export function makeSubagentConversationArtifacts(input: {
78 readonly messageId: MessageId;
79 readonly turnItemId: TurnItemId;
80 readonly threadId: ThreadId;
81 readonly rootNodeId: NodeId;
82 readonly providerThreadId: ProviderThreadId | null;
83 readonly providerTurnId: ProviderTurnId | null;
84 readonly nativeItemRef: OrchestrationV2ProviderRef | null;
85 readonly role: "user" | "assistant";
86 readonly text: string;
87 readonly ordinal: number;
88 readonly now: DateTime.Utc;
89}): {
90 readonly message: OrchestrationV2ConversationMessage;
91 readonly turnItem: OrchestrationV2TurnItem;
92} {
93 const message: OrchestrationV2ConversationMessage = {
94 createdBy: "agent",
95 creationSource: "provider",
96 id: input.messageId,
97 threadId: input.threadId,
98 runId: null,
99 nodeId: input.rootNodeId,
100 role: input.role,
101 text: input.text,
102 attachments: [],
103 streaming: false,
104 createdAt: input.now,
105 updatedAt: input.now,
106 };
107 const base = {
108 id: input.turnItemId,
109 threadId: input.threadId,
110 runId: null,
111 nodeId: input.rootNodeId,
112 providerThreadId: input.providerThreadId,
113 providerTurnId: input.providerTurnId,
114 nativeItemRef: input.nativeItemRef,
115 parentItemId: null,
116 ordinal: input.ordinal,
117 status: "completed" as const,
118 title: null,
119 startedAt: input.now,
120 completedAt: input.now,
121 updatedAt: input.now,
122 messageId: input.messageId,
123 text: input.text,
124 };
125 const turnItem: OrchestrationV2TurnItem =
126 input.role === "user"
127 ? {
128 ...base,
129 createdBy: "agent",
130 creationSource: "provider",
131 type: "user_message",
132 inputIntent: "turn_start",
133 attachments: [],
134 }
135 : {
136 ...base,
137 type: "assistant_message",
138 streaming: false,
139 };
140 return { message, turnItem };
142​
143export function subagentResultForRun(
144 projection: OrchestrationV2ThreadProjection,
145 run: OrchestrationV2Run,
146): {
147 readonly text: string;
148 readonly messageId: OrchestrationV2ConversationMessage["id"] | null;
149 readonly turnItemId: OrchestrationV2TurnItem["id"] | null;
150} {
151 const message =
152 projection.messages
153 .filter(
154 (candidate) =>
155 candidate.runId === run.id &&
156 candidate.role === "assistant" &&
157 candidate.text.trim().length > 0,
158 )
159 .toSorted(
160 (left, right) =>
161 DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
162 )[0] ?? null;
163 const turnItem =
164 projection.turnItems
165 .filter(
166 (
167 candidate,
168 ): candidate is Extract<OrchestrationV2TurnItem, { readonly type: "assistant_message" }> =>
169 candidate.runId === run.id &&
170 candidate.type === "assistant_message" &&
171 candidate.text.trim().length > 0,
172 )
173 .toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
174 const text =
175 message?.text ??
176 turnItem?.text ??
177 (run.status === "completed"
178 ? "Child task completed without an assistant result."
179 : `Child task ended with status ${run.status}.`);
180 return {
181 text,
182 messageId: message?.id ?? turnItem?.messageId ?? null,
183 turnItemId: turnItem?.id ?? null,
184 };