@anvia/studio 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -53,9 +53,30 @@ type StudioTranscriptToolEntry = {
53
53
  callId?: string;
54
54
  args?: string;
55
55
  result?: string;
56
+ childEvents?: StudioTranscriptChildAgentEvent[];
56
57
  approval?: StudioToolApprovalTranscript;
57
58
  question?: StudioToolQuestionTranscript;
58
59
  };
60
+ type StudioTranscriptChildAgentEvent = {
61
+ kind: "message";
62
+ agentId: string;
63
+ agentName?: string;
64
+ text: string;
65
+ } | {
66
+ kind: "reasoning";
67
+ agentId: string;
68
+ agentName?: string;
69
+ reasoningId?: string;
70
+ text: string;
71
+ } | {
72
+ kind: "tool";
73
+ agentId: string;
74
+ agentName?: string;
75
+ toolName: string;
76
+ callId?: string;
77
+ args?: string;
78
+ result?: string;
79
+ };
59
80
  type StudioTranscriptEntry = StudioTranscriptChatEntry | StudioTranscriptReasoningEntry | StudioTranscriptToolEntry;
60
81
  type StudioSessionSummary = {
61
82
  id: string;
@@ -98,9 +119,10 @@ type StudioSessionStore = MemoryStore & {
98
119
  deleteSession?(id: string): boolean | Promise<boolean>;
99
120
  };
100
121
  type StudioTraceStatus = "running" | "success" | "error";
101
- type StudioTraceObservationKind = "generation" | "tool";
122
+ type StudioTraceObservationKind = "agent" | "generation" | "tool";
102
123
  type StudioTraceObservation = {
103
124
  id: string;
125
+ parentObservationId?: string;
104
126
  kind: StudioTraceObservationKind;
105
127
  name: string;
106
128
  status: StudioTraceStatus;
@@ -344,4 +366,4 @@ type SqliteSessionStoreOptions = {
344
366
  };
345
367
  declare function createSqliteSessionStore(options?: SqliteSessionStoreOptions): StudioSessionStore & StudioTraceStore;
346
368
 
347
- export { type AgentRunRequest, type AgentRunResponse, type AgentRunStreamEvent, type AnviaStudio, type SqliteSessionStoreOptions, Studio, type StudioAgent, type StudioAgentConfig, type StudioAgentKnowledgeConfig, type StudioCapability, type StudioCapabilityConfig, type StudioConfig, type StudioErrorCode, type StudioErrorResponse, type StudioKnowledgeEvidence, type StudioKnowledgeEvidenceDocument, type StudioKnowledgeSourceKind, type StudioKnowledgeSourceSummary, type StudioKnowledgeSummary, type StudioOptions, type StudioServeOptions, type StudioSession, type StudioSessionCreateInput, type StudioSessionListOptions, type StudioSessionRunStatus, type StudioSessionRunTranscriptInput, type StudioSessionStore, type StudioSessionSummary, type StudioSessionTraceListOptions, type StudioStaticKnowledgeDocument, type StudioStores, type StudioToolApproval, type StudioToolApprovalDecision, type StudioToolApprovalRequestEvent, type StudioToolApprovalResultEvent, type StudioToolApprovalStatus, type StudioToolApprovalTranscript, type StudioToolQuestion, type StudioToolQuestionAnswer, type StudioToolQuestionChoice, type StudioToolQuestionPrompt, type StudioToolQuestionRequestEvent, type StudioToolQuestionResultEvent, type StudioToolQuestionStatus, type StudioToolQuestionTranscript, type StudioTrace, type StudioTraceListOptions, type StudioTraceObservation, type StudioTraceObservationKind, StudioTraceObserver, type StudioTraceObserverOptions, type StudioTraceStatus, type StudioTraceStore, type StudioTraceSummary, type StudioTranscriptChatEntry, type StudioTranscriptEntry, type StudioTranscriptReasoningEntry, type StudioTranscriptToolEntry, type StudioUiOptions, createSqliteSessionStore };
369
+ export { type AgentRunRequest, type AgentRunResponse, type AgentRunStreamEvent, type AnviaStudio, type SqliteSessionStoreOptions, Studio, type StudioAgent, type StudioAgentConfig, type StudioAgentKnowledgeConfig, type StudioCapability, type StudioCapabilityConfig, type StudioConfig, type StudioErrorCode, type StudioErrorResponse, type StudioKnowledgeEvidence, type StudioKnowledgeEvidenceDocument, type StudioKnowledgeSourceKind, type StudioKnowledgeSourceSummary, type StudioKnowledgeSummary, type StudioOptions, type StudioServeOptions, type StudioSession, type StudioSessionCreateInput, type StudioSessionListOptions, type StudioSessionRunStatus, type StudioSessionRunTranscriptInput, type StudioSessionStore, type StudioSessionSummary, type StudioSessionTraceListOptions, type StudioStaticKnowledgeDocument, type StudioStores, type StudioToolApproval, type StudioToolApprovalDecision, type StudioToolApprovalRequestEvent, type StudioToolApprovalResultEvent, type StudioToolApprovalStatus, type StudioToolApprovalTranscript, type StudioToolQuestion, type StudioToolQuestionAnswer, type StudioToolQuestionChoice, type StudioToolQuestionPrompt, type StudioToolQuestionRequestEvent, type StudioToolQuestionResultEvent, type StudioToolQuestionStatus, type StudioToolQuestionTranscript, type StudioTrace, type StudioTraceListOptions, type StudioTraceObservation, type StudioTraceObservationKind, StudioTraceObserver, type StudioTraceObserverOptions, type StudioTraceStatus, type StudioTraceStore, type StudioTraceSummary, type StudioTranscriptChatEntry, type StudioTranscriptChildAgentEvent, type StudioTranscriptEntry, type StudioTranscriptReasoningEntry, type StudioTranscriptToolEntry, type StudioUiOptions, createSqliteSessionStore };
package/dist/index.js CHANGED
@@ -79,34 +79,38 @@ var StudioRunTraceObserver = class {
79
79
  }
80
80
  startTool(args) {
81
81
  const startedAt = /* @__PURE__ */ new Date();
82
+ const childTrace = new ChildAgentToolTraceAccumulator(args);
82
83
  return {
84
+ streamEvent: (streamArgs) => {
85
+ childTrace.accept(streamArgs);
86
+ },
83
87
  end: (endArgs) => {
84
- this.observations.push(
85
- traceObservation({
86
- kind: "tool",
87
- name: args.toolName,
88
- status: "success",
89
- turn: args.turn,
90
- startedAt,
91
- input: parseOrString(args.args),
92
- output: parseOrString(endArgs.result),
93
- metadata: toolMetadata(args, endArgs.skipped)
94
- })
95
- );
88
+ const parentObservation = traceObservation({
89
+ kind: "tool",
90
+ name: args.toolName,
91
+ status: "success",
92
+ turn: args.turn,
93
+ startedAt,
94
+ input: parseOrString(args.args),
95
+ output: parseOrString(endArgs.result),
96
+ metadata: toolMetadata(args, endArgs.skipped)
97
+ });
98
+ this.observations.push(parentObservation);
99
+ this.observations.push(...childTrace.observations(parentObservation.id));
96
100
  },
97
101
  error: (errorArgs) => {
98
- this.observations.push(
99
- traceObservation({
100
- kind: "tool",
101
- name: args.toolName,
102
- status: "error",
103
- turn: args.turn,
104
- startedAt,
105
- input: parseOrString(args.args),
106
- error: serializeError(errorArgs.error),
107
- metadata: toolMetadata(args, false)
108
- })
109
- );
102
+ const parentObservation = traceObservation({
103
+ kind: "tool",
104
+ name: args.toolName,
105
+ status: "error",
106
+ turn: args.turn,
107
+ startedAt,
108
+ input: parseOrString(args.args),
109
+ error: serializeError(errorArgs.error),
110
+ metadata: toolMetadata(args, false)
111
+ });
112
+ this.observations.push(parentObservation);
113
+ this.observations.push(...childTrace.observations(parentObservation.id));
110
114
  }
111
115
  };
112
116
  }
@@ -157,10 +161,191 @@ var StudioRunTraceObserver = class {
157
161
  await store.saveTrace(trace);
158
162
  }
159
163
  };
164
+ var ChildAgentToolTraceAccumulator = class {
165
+ constructor(parent) {
166
+ this.parent = parent;
167
+ }
168
+ parent;
169
+ agentStarts = /* @__PURE__ */ new Map();
170
+ generationStarts = /* @__PURE__ */ new Map();
171
+ toolStarts = [];
172
+ completedObservations = [];
173
+ accept(args) {
174
+ const wrapper = args.event;
175
+ const child = isRecord(wrapper.event) ? wrapper.event : void 0;
176
+ if (child === void 0) {
177
+ return;
178
+ }
179
+ const agentId = wrapper.agentId;
180
+ const agentName = wrapper.agentName;
181
+ const childTurn = typeof child.turn === "number" ? child.turn : this.parent.turn;
182
+ if (!this.agentStarts.has(agentId)) {
183
+ this.agentStarts.set(agentId, {
184
+ startedAt: /* @__PURE__ */ new Date(),
185
+ agentId,
186
+ ...agentName === void 0 ? {} : { agentName }
187
+ });
188
+ }
189
+ if (child.type === "turn_start") {
190
+ this.generationStarts.set(generationKey(agentId, childTurn), {
191
+ startedAt: /* @__PURE__ */ new Date(),
192
+ input: toJsonValue({
193
+ prompt: child.prompt,
194
+ history: child.history
195
+ }),
196
+ agentId,
197
+ ...agentName === void 0 ? {} : { agentName },
198
+ childTurn
199
+ });
200
+ return;
201
+ }
202
+ if (child.type === "turn_end") {
203
+ const key = generationKey(agentId, childTurn);
204
+ const start = this.generationStarts.get(key);
205
+ this.generationStarts.delete(key);
206
+ this.completedObservations.push(
207
+ traceObservation({
208
+ kind: "generation",
209
+ name: `${agentLabel(agentId, agentName)}.model.turn.${childTurn}`,
210
+ status: "success",
211
+ turn: this.parent.turn,
212
+ startedAt: start?.startedAt ?? /* @__PURE__ */ new Date(),
213
+ ...start?.input === void 0 ? {} : { input: start.input },
214
+ output: toJsonValue(child.response),
215
+ metadata: this.childMetadata(agentId, agentName, childTurn)
216
+ })
217
+ );
218
+ return;
219
+ }
220
+ if (child.type === "tool_call" && isRecord(child.toolCall)) {
221
+ const toolCall = child.toolCall;
222
+ const toolCallFunction = isRecord(toolCall.function) ? toolCall.function : void 0;
223
+ const toolName = typeof toolCallFunction?.name === "string" ? toolCallFunction.name : "tool";
224
+ const callId = typeof toolCall.callId === "string" ? toolCall.callId : typeof toolCall.id === "string" ? toolCall.id : void 0;
225
+ this.toolStarts.push({
226
+ startedAt: /* @__PURE__ */ new Date(),
227
+ agentId,
228
+ ...agentName === void 0 ? {} : { agentName },
229
+ childTurn,
230
+ toolName,
231
+ ...callId === void 0 ? {} : { toolCallId: callId },
232
+ input: toJsonValue(toolCallFunction?.arguments ?? {}),
233
+ completed: false
234
+ });
235
+ return;
236
+ }
237
+ if (child.type === "tool_result") {
238
+ const toolName = typeof child.toolName === "string" ? child.toolName : "tool";
239
+ const toolCallId = typeof child.toolCallId === "string" ? child.toolCallId : void 0;
240
+ const internalCallId = typeof child.internalCallId === "string" ? child.internalCallId : void 0;
241
+ const start = this.findToolStart(agentId, toolName, toolCallId);
242
+ const input = start?.input ?? (typeof child.args === "string" ? parseOrString(child.args) : void 0);
243
+ if (start !== void 0) {
244
+ start.completed = true;
245
+ }
246
+ this.completedObservations.push(
247
+ traceObservation({
248
+ kind: "tool",
249
+ name: `${agentLabel(agentId, agentName)}.${toolName}`,
250
+ status: "success",
251
+ turn: this.parent.turn,
252
+ startedAt: start?.startedAt ?? /* @__PURE__ */ new Date(),
253
+ ...input === void 0 ? {} : { input },
254
+ ...typeof child.result === "string" ? { output: parseOrString(child.result) } : {},
255
+ metadata: {
256
+ ...this.childMetadata(agentId, agentName, childTurn),
257
+ ...toolCallId === void 0 ? {} : { toolCallId },
258
+ ...internalCallId === void 0 ? {} : { internalCallId }
259
+ }
260
+ })
261
+ );
262
+ return;
263
+ }
264
+ if (child.type === "error") {
265
+ this.completedObservations.push(
266
+ traceObservation({
267
+ kind: "tool",
268
+ name: `${agentLabel(agentId, agentName)}.error`,
269
+ status: "error",
270
+ turn: this.parent.turn,
271
+ startedAt: /* @__PURE__ */ new Date(),
272
+ error: serializeError(child.error),
273
+ metadata: this.childMetadata(agentId, agentName, childTurn)
274
+ })
275
+ );
276
+ }
277
+ }
278
+ observations(parentObservationId) {
279
+ const observations = [];
280
+ const agentObservationIds = /* @__PURE__ */ new Map();
281
+ for (const agentStart of this.agentStarts.values()) {
282
+ const agentChildren = this.completedObservations.filter(
283
+ (observation) => isRecord(observation.metadata) && observation.metadata.childAgentId === agentStart.agentId
284
+ );
285
+ const childStartTimes = agentChildren.map((observation) => Date.parse(observation.startedAt));
286
+ const childEndTimes = agentChildren.map(
287
+ (observation) => Date.parse(observation.endedAt ?? observation.startedAt)
288
+ );
289
+ const startedAt = childStartTimes.length === 0 ? agentStart.startedAt : new Date(Math.min(agentStart.startedAt.getTime(), ...childStartTimes));
290
+ const endedAt = childEndTimes.length === 0 ? /* @__PURE__ */ new Date() : new Date(Math.max(...childEndTimes));
291
+ const agentObservation = traceObservation({
292
+ parentObservationId,
293
+ kind: "agent",
294
+ name: `${agentLabel(agentStart.agentId, agentStart.agentName)}.run`,
295
+ status: agentChildren.some((observation) => observation.status === "error") ? "error" : "success",
296
+ turn: this.parent.turn,
297
+ startedAt,
298
+ endedAt,
299
+ metadata: this.childMetadata(agentStart.agentId, agentStart.agentName, this.parent.turn)
300
+ });
301
+ observations.push(agentObservation);
302
+ agentObservationIds.set(agentStart.agentId, agentObservation.id);
303
+ }
304
+ for (const observation of this.completedObservations) {
305
+ const childAgentId = isRecord(observation.metadata) ? stringValue(observation.metadata.childAgentId) : void 0;
306
+ const childAgentObservationId = childAgentId === void 0 ? void 0 : agentObservationIds.get(childAgentId);
307
+ observations.push({
308
+ ...observation,
309
+ parentObservationId: childAgentObservationId ?? parentObservationId
310
+ });
311
+ }
312
+ return observations;
313
+ }
314
+ findToolStart(agentId, toolName, toolCallId) {
315
+ for (let index = this.toolStarts.length - 1; index >= 0; index -= 1) {
316
+ const start = this.toolStarts[index];
317
+ if (start === void 0 || start.completed || start.agentId !== agentId || start.toolName !== toolName) {
318
+ continue;
319
+ }
320
+ if (toolCallId === void 0 || start.toolCallId === toolCallId) {
321
+ return start;
322
+ }
323
+ }
324
+ return void 0;
325
+ }
326
+ childMetadata(agentId, agentName, childTurn) {
327
+ return compactJsonObject({
328
+ source: "agent_tool_event",
329
+ childAgentId: agentId,
330
+ childAgentName: agentName,
331
+ childTurn,
332
+ parentToolName: this.parent.toolName,
333
+ parentInternalCallId: this.parent.internalCallId,
334
+ parentToolCallId: this.parent.toolCallId
335
+ });
336
+ }
337
+ };
338
+ function generationKey(agentId, turn) {
339
+ return `${agentId}:${turn}`;
340
+ }
341
+ function agentLabel(agentId, agentName) {
342
+ return (agentName ?? agentId).replaceAll(/\s+/g, "_");
343
+ }
160
344
  function traceObservation(props) {
161
- const endedAt = /* @__PURE__ */ new Date();
345
+ const endedAt = props.endedAt ?? /* @__PURE__ */ new Date();
162
346
  return {
163
347
  id: globalThis.crypto.randomUUID(),
348
+ ...props.parentObservationId === void 0 ? {} : { parentObservationId: props.parentObservationId },
164
349
  kind: props.kind,
165
350
  name: props.name,
166
351
  status: props.status,
@@ -209,6 +394,12 @@ function parseOrString(value) {
209
394
  return value;
210
395
  }
211
396
  }
397
+ function isRecord(value) {
398
+ return typeof value === "object" && value !== null && !Array.isArray(value);
399
+ }
400
+ function stringValue(value) {
401
+ return typeof value === "string" ? value : void 0;
402
+ }
212
403
  function serializeError(error) {
213
404
  if (error instanceof Error) {
214
405
  return compactJsonObject({
@@ -1593,7 +1784,7 @@ async function recentKnowledgeEvidence(traceStore, limit) {
1593
1784
  }
1594
1785
  function evidenceFromTrace(trace) {
1595
1786
  return trace.observations.flatMap((observation) => {
1596
- if (observation.kind !== "generation" || !isRecord(observation.input)) {
1787
+ if (observation.kind !== "generation" || !isRecord2(observation.input)) {
1597
1788
  return [];
1598
1789
  }
1599
1790
  const documents = Array.isArray(observation.input.documents) ? observation.input.documents.flatMap((document) => evidenceDocument(document)) : [];
@@ -1646,7 +1837,7 @@ function messageText(value) {
1646
1837
  if (typeof value === "string") {
1647
1838
  return value.trim();
1648
1839
  }
1649
- if (!isRecord(value)) {
1840
+ if (!isRecord2(value)) {
1650
1841
  return "";
1651
1842
  }
1652
1843
  if (typeof value.text === "string") {
@@ -1664,7 +1855,7 @@ function contentText(value) {
1664
1855
  if (typeof value === "string") {
1665
1856
  return value.trim();
1666
1857
  }
1667
- if (!isRecord(value)) {
1858
+ if (!isRecord2(value)) {
1668
1859
  return "";
1669
1860
  }
1670
1861
  if (typeof value.text === "string") {
@@ -1673,12 +1864,12 @@ function contentText(value) {
1673
1864
  return "";
1674
1865
  }
1675
1866
  function evidenceDocument(value) {
1676
- if (!isRecord(value)) {
1867
+ if (!isRecord2(value)) {
1677
1868
  return [];
1678
1869
  }
1679
1870
  const id = typeof value.id === "string" ? value.id : void 0;
1680
1871
  const text = typeof value.text === "string" ? value.text : void 0;
1681
- const additionalProps = isRecord(value.additionalProps) ? jsonObjectFromRecord(value.additionalProps) : void 0;
1872
+ const additionalProps = isRecord2(value.additionalProps) ? jsonObjectFromRecord(value.additionalProps) : void 0;
1682
1873
  if (id === void 0 && text === void 0 && additionalProps === void 0) {
1683
1874
  return [];
1684
1875
  }
@@ -1691,7 +1882,7 @@ function evidenceDocument(value) {
1691
1882
  ];
1692
1883
  }
1693
1884
  function evidenceToolName(value) {
1694
- if (!isRecord(value) || typeof value.name !== "string") {
1885
+ if (!isRecord2(value) || typeof value.name !== "string") {
1695
1886
  return [];
1696
1887
  }
1697
1888
  return [value.name];
@@ -1699,7 +1890,7 @@ function evidenceToolName(value) {
1699
1890
  function jsonObjectFromRecord(value) {
1700
1891
  return compactJsonObject2(value);
1701
1892
  }
1702
- function isRecord(value) {
1893
+ function isRecord2(value) {
1703
1894
  return typeof value === "object" && value !== null && !Array.isArray(value);
1704
1895
  }
1705
1896
 
@@ -2117,6 +2308,22 @@ function acceptTranscriptStreamEvent(transcript, event) {
2117
2308
  matched.args = matched.args ?? event.args;
2118
2309
  matched.result = event.result;
2119
2310
  }
2311
+ if (event.type === "agent_tool_event") {
2312
+ const matched = findTranscriptToolEntry(transcript, event.toolName, event.toolCallId);
2313
+ if (matched === void 0) {
2314
+ transcript.push({
2315
+ entryId: transcript.length,
2316
+ kind: "tool",
2317
+ toolName: event.toolName,
2318
+ ...event.toolCallId === void 0 ? {} : { callId: event.toolCallId },
2319
+ childEvents: [childAgentTranscriptEvent(event)].filter(
2320
+ (childEvent) => childEvent !== void 0
2321
+ )
2322
+ });
2323
+ return;
2324
+ }
2325
+ appendChildAgentTranscriptEvent(matched, event);
2326
+ }
2120
2327
  if (event.type === "tool_approval_request") {
2121
2328
  const matched = findTranscriptToolEntry(
2122
2329
  transcript,
@@ -2189,6 +2396,112 @@ function approvalCallId(approval) {
2189
2396
  function questionCallId(question) {
2190
2397
  return question.callId ?? question.toolCallId;
2191
2398
  }
2399
+ function appendChildAgentTranscriptEvent(entry, event) {
2400
+ const childEvent = childAgentTranscriptEvent(event);
2401
+ if (childEvent === void 0) {
2402
+ return;
2403
+ }
2404
+ const childEvents = entry.childEvents ?? [];
2405
+ if (childEvent.kind === "message") {
2406
+ const last = childEvents.at(-1);
2407
+ if (last?.kind === "message" && last.agentId === childEvent.agentId) {
2408
+ last.text = `${last.text}${childEvent.text}`;
2409
+ } else {
2410
+ childEvents.push(childEvent);
2411
+ }
2412
+ } else if (childEvent.kind === "reasoning") {
2413
+ const last = childEvents.at(-1);
2414
+ if (last?.kind === "reasoning" && last.agentId === childEvent.agentId && (last.reasoningId ?? "") === (childEvent.reasoningId ?? "")) {
2415
+ last.text = `${last.text}${childEvent.text}`;
2416
+ } else {
2417
+ childEvents.push(childEvent);
2418
+ }
2419
+ } else {
2420
+ const matched = findChildAgentToolEvent(childEvents, childEvent);
2421
+ if (matched === void 0) {
2422
+ childEvents.push(childEvent);
2423
+ } else {
2424
+ if (matched.args === void 0 && childEvent.args !== void 0) {
2425
+ matched.args = childEvent.args;
2426
+ }
2427
+ if (childEvent.result !== void 0) {
2428
+ matched.result = childEvent.result;
2429
+ }
2430
+ }
2431
+ }
2432
+ entry.childEvents = childEvents;
2433
+ }
2434
+ function childAgentTranscriptEvent(event) {
2435
+ const child = event.event;
2436
+ if (child.type === "text_delta") {
2437
+ return {
2438
+ kind: "message",
2439
+ agentId: event.agentId,
2440
+ ...event.agentName === void 0 ? {} : { agentName: event.agentName },
2441
+ text: child.delta
2442
+ };
2443
+ }
2444
+ if (child.type === "reasoning_delta") {
2445
+ return {
2446
+ kind: "reasoning",
2447
+ agentId: event.agentId,
2448
+ ...event.agentName === void 0 ? {} : { agentName: event.agentName },
2449
+ ...child.id === void 0 ? {} : { reasoningId: child.id },
2450
+ text: child.delta
2451
+ };
2452
+ }
2453
+ if (child.type === "tool_call") {
2454
+ return {
2455
+ kind: "tool",
2456
+ agentId: event.agentId,
2457
+ ...event.agentName === void 0 ? {} : { agentName: event.agentName },
2458
+ toolName: child.toolCall.function.name,
2459
+ ...child.toolCall.callId === void 0 && child.toolCall.id === void 0 ? {} : { callId: child.toolCall.callId ?? child.toolCall.id },
2460
+ args: formatJson2(child.toolCall.function.arguments)
2461
+ };
2462
+ }
2463
+ if (child.type === "tool_result") {
2464
+ return {
2465
+ kind: "tool",
2466
+ agentId: event.agentId,
2467
+ ...event.agentName === void 0 ? {} : { agentName: event.agentName },
2468
+ toolName: child.toolName,
2469
+ ...child.toolCallId === void 0 ? {} : { callId: child.toolCallId },
2470
+ args: child.args,
2471
+ result: child.result
2472
+ };
2473
+ }
2474
+ if (child.type === "error") {
2475
+ return {
2476
+ kind: "message",
2477
+ agentId: event.agentId,
2478
+ ...event.agentName === void 0 ? {} : { agentName: event.agentName },
2479
+ text: `Error: ${errorText(child.error)}`
2480
+ };
2481
+ }
2482
+ return void 0;
2483
+ }
2484
+ function errorText(error) {
2485
+ if (error instanceof Error) {
2486
+ return error.message;
2487
+ }
2488
+ if (typeof error === "string") {
2489
+ return error;
2490
+ }
2491
+ return JSON.stringify(serializeError2(error));
2492
+ }
2493
+ function findChildAgentToolEvent(childEvents, event) {
2494
+ for (let index = childEvents.length - 1; index >= 0; index -= 1) {
2495
+ const childEvent = childEvents[index];
2496
+ if (childEvent?.kind !== "tool" || childEvent.agentId !== event.agentId || childEvent.toolName !== event.toolName || childEvent.result !== void 0) {
2497
+ continue;
2498
+ }
2499
+ if (event.callId === void 0 || childEvent.callId === event.callId) {
2500
+ return childEvent;
2501
+ }
2502
+ }
2503
+ return void 0;
2504
+ }
2192
2505
  function transcriptFromMessages(messages) {
2193
2506
  const transcript = [];
2194
2507
  for (const message of messages) {