@anvia/core 0.26.0 → 1.0.0-rc.1

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.
Files changed (135) hide show
  1. package/README.md +21 -11
  2. package/dist/agent/index.d.ts +26 -68
  3. package/dist/agent/index.js +20 -17
  4. package/dist/agent-DGYY_onS.d.ts +145 -0
  5. package/dist/audio-generation/index.d.ts +12 -14
  6. package/dist/audio-generation/index.js +5 -39
  7. package/dist/audio-generation/index.js.map +1 -1
  8. package/dist/{chunk-MRQLEK2B.js → chunk-5XKDW36Z.js} +30 -7
  9. package/dist/chunk-5XKDW36Z.js.map +1 -0
  10. package/dist/chunk-6WGGTJE6.js +139 -0
  11. package/dist/chunk-6WGGTJE6.js.map +1 -0
  12. package/dist/chunk-A3UBYTJT.js +157 -0
  13. package/dist/chunk-A3UBYTJT.js.map +1 -0
  14. package/dist/{chunk-NCPN4SJR.js → chunk-DMPUP4R3.js} +10 -10
  15. package/dist/chunk-DMPUP4R3.js.map +1 -0
  16. package/dist/{chunk-MNWK2USR.js → chunk-DZ3CKYTH.js} +29 -5
  17. package/dist/chunk-DZ3CKYTH.js.map +1 -0
  18. package/dist/{chunk-CWUJUSOS.js → chunk-FNQB2JEH.js} +1 -1
  19. package/dist/chunk-FNQB2JEH.js.map +1 -0
  20. package/dist/chunk-I6G7BC42.js +41 -0
  21. package/dist/chunk-I6G7BC42.js.map +1 -0
  22. package/dist/{chunk-BALBBVI3.js → chunk-IL3ZSNJI.js} +2720 -1704
  23. package/dist/chunk-IL3ZSNJI.js.map +1 -0
  24. package/dist/chunk-J6GNQART.js +122 -0
  25. package/dist/chunk-J6GNQART.js.map +1 -0
  26. package/dist/{chunk-MC3CCKEB.js → chunk-M3IOWB4Y.js} +124 -65
  27. package/dist/chunk-M3IOWB4Y.js.map +1 -0
  28. package/dist/chunk-QY5GZ7HR.js +39 -0
  29. package/dist/chunk-QY5GZ7HR.js.map +1 -0
  30. package/dist/{chunk-MELYDKWQ.js → chunk-R2LJSQUW.js} +190 -20
  31. package/dist/chunk-R2LJSQUW.js.map +1 -0
  32. package/dist/{chunk-WQKHFADH.js → chunk-RFDVKCIE.js} +3 -3
  33. package/dist/chunk-RFDVKCIE.js.map +1 -0
  34. package/dist/chunk-RMUBRRSK.js +34 -0
  35. package/dist/chunk-RMUBRRSK.js.map +1 -0
  36. package/dist/{chunk-I6XOQ4LK.js → chunk-RXKHOYEH.js} +1 -1
  37. package/dist/chunk-RXKHOYEH.js.map +1 -0
  38. package/dist/chunk-XDRSPSR7.js +38 -0
  39. package/dist/chunk-XDRSPSR7.js.map +1 -0
  40. package/dist/completion/index.d.ts +5 -36
  41. package/dist/completion/index.js +4 -5
  42. package/dist/dynamic-tools-i9woysFa.d.ts +33 -0
  43. package/dist/embeddings/index.d.ts +2 -2
  44. package/dist/embeddings/index.js +1 -2
  45. package/dist/{errors-2moLDuuN.d.ts → errors-CYKUcU89.d.ts} +2 -2
  46. package/dist/evals/index.d.ts +22 -15
  47. package/dist/evals/index.js +57 -35
  48. package/dist/evals/index.js.map +1 -1
  49. package/dist/extractor/index.d.ts +23 -35
  50. package/dist/extractor/index.js +7 -21
  51. package/dist/guardrails/index.d.ts +2 -2
  52. package/dist/guardrails/index.js +1 -1
  53. package/dist/image-generation/index.d.ts +12 -14
  54. package/dist/image-generation/index.js +5 -39
  55. package/dist/image-generation/index.js.map +1 -1
  56. package/dist/index-DxB74ybJ.d.ts +349 -0
  57. package/dist/index.d.ts +20 -17
  58. package/dist/index.js +31 -36
  59. package/dist/internal/agent.d.ts +152 -9
  60. package/dist/internal/agent.js +22 -17
  61. package/dist/{json-BVPssigK.d.ts → json-Zo7K3xmt.d.ts} +12 -11
  62. package/dist/loaders/index.d.ts +5 -10
  63. package/dist/loaders/index.js.map +1 -1
  64. package/dist/mcp/index.d.ts +4 -4
  65. package/dist/mcp/index.js.map +1 -1
  66. package/dist/memory/index.d.ts +4 -4
  67. package/dist/memory/index.js +6 -7
  68. package/dist/middleware-KynLg_RW.d.ts +56 -0
  69. package/dist/observability/index.d.ts +6 -3
  70. package/dist/observability/index.js.map +1 -1
  71. package/dist/pipeline/index.d.ts +40 -47
  72. package/dist/pipeline/index.js +99 -99
  73. package/dist/pipeline/index.js.map +1 -1
  74. package/dist/retry-D3Ruy-ba.d.ts +15 -0
  75. package/dist/skills/index.d.ts +4 -4
  76. package/dist/skills/index.js +5 -6
  77. package/dist/streaming/index.js +37 -3
  78. package/dist/streaming/index.js.map +1 -1
  79. package/dist/{think-tool-DfCMuO8e.d.ts → think-tool-DZqZj2LV.d.ts} +5 -5
  80. package/dist/tool/index.d.ts +8 -6
  81. package/dist/tool/index.js +12 -15
  82. package/dist/tool-C_8GudXn.d.ts +49 -0
  83. package/dist/transcription/index.d.ts +14 -16
  84. package/dist/transcription/index.js +5 -57
  85. package/dist/transcription/index.js.map +1 -1
  86. package/dist/type-utils-CtHVDRn_.d.ts +6 -0
  87. package/dist/{types-G12i56s2.d.ts → types-B3A4YxaA.d.ts} +1 -1
  88. package/dist/{types-BrLbKnEz.d.ts → types-BBEGKB9m.d.ts} +1 -1
  89. package/dist/{types-O-abTIin.d.ts → types-DT3nEemY.d.ts} +3 -3
  90. package/dist/{types-04LqWrmy.d.ts → types-DhQfwiZn.d.ts} +1 -1
  91. package/dist/{types-JJCxRnaZ.d.ts → types-DulRCYdB.d.ts} +2 -2
  92. package/dist/{types-BCTRUGex.d.ts → types-p55Jc-sr.d.ts} +1 -1
  93. package/dist/ui/index.d.ts +1 -1
  94. package/dist/ui/index.js +1 -1
  95. package/dist/vector-store/index.d.ts +5 -5
  96. package/dist/vector-store/index.js +4 -4
  97. package/package.json +2 -10
  98. package/dist/agent-x3YTyv5i.d.ts +0 -222
  99. package/dist/chunk-2ODTMRHP.js +0 -54
  100. package/dist/chunk-2ODTMRHP.js.map +0 -1
  101. package/dist/chunk-BALBBVI3.js.map +0 -1
  102. package/dist/chunk-CWUJUSOS.js.map +0 -1
  103. package/dist/chunk-DALYP4PX.js +0 -225
  104. package/dist/chunk-DALYP4PX.js.map +0 -1
  105. package/dist/chunk-HYUYZEAJ.js +0 -223
  106. package/dist/chunk-HYUYZEAJ.js.map +0 -1
  107. package/dist/chunk-I6XOQ4LK.js.map +0 -1
  108. package/dist/chunk-JI5YZWNT.js +0 -220
  109. package/dist/chunk-JI5YZWNT.js.map +0 -1
  110. package/dist/chunk-MC3CCKEB.js.map +0 -1
  111. package/dist/chunk-MELYDKWQ.js.map +0 -1
  112. package/dist/chunk-MNWK2USR.js.map +0 -1
  113. package/dist/chunk-MRQLEK2B.js.map +0 -1
  114. package/dist/chunk-NCPN4SJR.js.map +0 -1
  115. package/dist/chunk-OIMLU4SF.js +0 -20
  116. package/dist/chunk-OIMLU4SF.js.map +0 -1
  117. package/dist/chunk-Q25IWLBT.js +0 -138
  118. package/dist/chunk-Q25IWLBT.js.map +0 -1
  119. package/dist/chunk-UQX6EXWG.js +0 -33
  120. package/dist/chunk-UQX6EXWG.js.map +0 -1
  121. package/dist/chunk-WQKHFADH.js.map +0 -1
  122. package/dist/chunk-XUUY2L2D.js +0 -42
  123. package/dist/chunk-XUUY2L2D.js.map +0 -1
  124. package/dist/chunk-YLMDBQO4.js +0 -141
  125. package/dist/chunk-YLMDBQO4.js.map +0 -1
  126. package/dist/hooks/index.d.ts +0 -12
  127. package/dist/hooks/index.js +0 -17
  128. package/dist/hooks/index.js.map +0 -1
  129. package/dist/index-BxSbmOMr.d.ts +0 -257
  130. package/dist/middleware-Dp3d_pxi.d.ts +0 -90
  131. package/dist/request/index.d.ts +0 -28
  132. package/dist/request/index.js +0 -25
  133. package/dist/request/index.js.map +0 -1
  134. package/dist/tool-Cohdv5GC.d.ts +0 -65
  135. package/dist/types-D9JpeAPF.d.ts +0 -114
@@ -1,29 +1,37 @@
1
1
  import {
2
- toReadableStream
3
- } from "./chunk-XUUY2L2D.js";
2
+ isSkillTool
3
+ } from "./chunk-YK4WAAS4.js";
4
4
  import {
5
+ assertFiniteSearchThreshold,
6
+ assertPositiveSearchLimit,
7
+ isToolIndex
8
+ } from "./chunk-6WGGTJE6.js";
9
+ import {
10
+ ToolNotFoundError,
11
+ assertToolApprovalRequirement,
12
+ createTool,
5
13
  parseToolArgs,
14
+ prepareToolCall,
15
+ toolMayRequireApproval,
6
16
  toolResultContentToText
7
- } from "./chunk-HYUYZEAJ.js";
8
- import {
9
- runControl,
10
- toolCallControl
11
- } from "./chunk-2ODTMRHP.js";
17
+ } from "./chunk-R2LJSQUW.js";
12
18
  import {
13
19
  MemoryCompactionConflictError,
14
20
  MemoryCompactionError,
15
21
  createMemoryCompactionSummary,
16
- cumulativeCompactedMessageCount
17
- } from "./chunk-MRQLEK2B.js";
22
+ cumulativeCompactedMessageCount,
23
+ resolveMemoryOptions
24
+ } from "./chunk-5XKDW36Z.js";
18
25
  import {
19
26
  mapWithConcurrency
20
- } from "./chunk-OIMLU4SF.js";
21
- import {
22
- CompletionRequestBuilder
23
- } from "./chunk-Q25IWLBT.js";
27
+ } from "./chunk-DZ3CKYTH.js";
24
28
  import {
29
+ createCompletionRequest,
25
30
  isStreamingCompletionModel
26
- } from "./chunk-MC3CCKEB.js";
31
+ } from "./chunk-M3IOWB4Y.js";
32
+ import {
33
+ toProviderJsonSchema
34
+ } from "./chunk-RFDVKCIE.js";
27
35
  import {
28
36
  AssistantContent,
29
37
  Message,
@@ -31,16 +39,23 @@ import {
31
39
  Usage,
32
40
  assertCompletionRequestSupported,
33
41
  getAssistantGenerationMetadata,
42
+ isProviderTool,
34
43
  textFromAssistantContent
35
44
  } from "./chunk-ADH7NNCS.js";
45
+ import {
46
+ resolveRetryOptions,
47
+ retryDelayMs,
48
+ retryErrorAttributes,
49
+ waitForRetry
50
+ } from "./chunk-A3UBYTJT.js";
36
51
  import {
37
52
  appendGuardrailPolicies,
38
53
  hasEnforcedOutputGuardrails,
39
54
  runInputGuardrails,
40
55
  runOutputGuardrails
41
- } from "./chunk-CWUJUSOS.js";
56
+ } from "./chunk-FNQB2JEH.js";
42
57
 
43
- // src/request/errors.ts
58
+ // src/agent/errors.ts
44
59
  var MaxTurnsError = class extends Error {
45
60
  constructor(maxTurns, chatHistory, prompt) {
46
61
  super(`Reached max turn limit: ${maxTurns}`);
@@ -53,1657 +68,1904 @@ var MaxTurnsError = class extends Error {
53
68
  chatHistory;
54
69
  prompt;
55
70
  };
56
- var PromptCancelledError = class extends Error {
71
+ var AgentRunCancelledError = class extends Error {
57
72
  constructor(chatHistory, reason) {
58
- super(`Prompt cancelled: ${reason}`);
73
+ super(`Agent run cancelled: ${reason}`);
59
74
  this.chatHistory = chatHistory;
60
75
  this.reason = reason;
61
- this.name = "PromptCancelledError";
76
+ this.name = "AgentRunCancelledError";
62
77
  }
63
78
  chatHistory;
64
79
  reason;
65
80
  };
66
- var ToolApprovalRequiredError = class extends Error {
67
- constructor(request) {
68
- super(`Tool approval required for ${request.toolName}, but no approval handler is configured.`);
69
- this.request = request;
70
- this.name = "ToolApprovalRequiredError";
81
+
82
+ // src/agent/tool-state.ts
83
+ var agentToolStates = /* @__PURE__ */ new WeakMap();
84
+ function registerAgentToolState(agent, publicState, toolsByName) {
85
+ agentToolStates.set(agent, { publicState, toolsByName });
86
+ }
87
+ function getAgentToolState(agent) {
88
+ return getStoredAgentToolState(agent).publicState;
89
+ }
90
+ function getRegisteredAgentTool(agent, toolName) {
91
+ return getStoredAgentToolState(agent).toolsByName.get(toolName);
92
+ }
93
+ function getStoredAgentToolState(agent) {
94
+ const state = agentToolStates.get(agent);
95
+ if (state === void 0) {
96
+ throw new TypeError("Agent tool state is unavailable.");
97
+ }
98
+ return state;
99
+ }
100
+
101
+ // src/hooks/control.ts
102
+ function createHook(hook) {
103
+ return hook;
104
+ }
105
+ function cancelRun(reason) {
106
+ return { type: "terminate", reason };
107
+ }
108
+ function skipTool(reason) {
109
+ return { type: "skip", reason };
110
+ }
111
+ function requestToolApproval(options = {}) {
112
+ const action = {
113
+ type: "approval_request"
114
+ };
115
+ if (options.reason !== void 0) {
116
+ action.reason = options.reason;
117
+ }
118
+ if (options.rejectMessage !== void 0) {
119
+ action.rejectMessage = options.rejectMessage;
120
+ }
121
+ return action;
122
+ }
123
+ var runControl = {
124
+ continue() {
125
+ return { type: "continue" };
126
+ },
127
+ cancel(reason) {
128
+ return cancelRun(reason);
129
+ }
130
+ };
131
+ var toolCallControl = {
132
+ run() {
133
+ return { type: "continue" };
134
+ },
135
+ skip(reason) {
136
+ return skipTool(reason);
137
+ },
138
+ cancel(reason) {
139
+ return { type: "terminate", reason };
140
+ },
141
+ requestApproval(options) {
142
+ return requestToolApproval(options);
71
143
  }
72
- request;
73
144
  };
74
145
 
75
- // src/internal/async-queue.ts
76
- function createAsyncQueue() {
77
- const values = [];
78
- const waiters = [];
79
- let closed = false;
80
- let error;
81
- function flush() {
82
- while (waiters.length > 0 && values.length > 0) {
83
- const waiter = waiters.shift();
84
- const value = values.shift();
85
- if (waiter !== void 0) {
86
- waiter.resolve({ value, done: false });
87
- }
88
- }
89
- if (values.length > 0 || waiters.length === 0 || !closed) {
90
- return;
91
- }
92
- while (waiters.length > 0) {
93
- const waiter = waiters.shift();
94
- if (waiter === void 0) {
95
- continue;
96
- }
97
- if (error !== void 0) {
98
- waiter.reject(error);
99
- } else {
100
- waiter.resolve({ value: void 0, done: true });
101
- }
102
- }
146
+ // src/internal/agent-runtime/approval-details.ts
147
+ var approvalDetails = /* @__PURE__ */ new WeakMap();
148
+ function registerAgentApprovalRequestDetails(approval, request) {
149
+ approvalDetails.set(approval, request);
150
+ }
151
+ function getAgentApprovalRequestDetails(approval) {
152
+ return approvalDetails.get(approval);
153
+ }
154
+
155
+ // src/agent/context-index.ts
156
+ function createContextIndex(index, options) {
157
+ const topK = assertPositiveSearchLimit(options.topK);
158
+ const threshold = assertFiniteSearchThreshold(options.threshold);
159
+ return Object.freeze({
160
+ ...options,
161
+ topK,
162
+ ...threshold === void 0 ? {} : { threshold },
163
+ kind: "context-index",
164
+ index
165
+ });
166
+ }
167
+ function isContextIndex(value) {
168
+ if (typeof value !== "object" || value === null) {
169
+ return false;
170
+ }
171
+ const candidate = value;
172
+ if (candidate.kind !== "context-index" || typeof candidate.index !== "object") {
173
+ return false;
103
174
  }
175
+ return candidate.index !== null && typeof candidate.index.search === "function";
176
+ }
177
+
178
+ // src/internal/agent-runtime/run-options.ts
179
+ var internalAgentRunOptions = /* @__PURE__ */ Symbol("internalAgentRunOptions");
180
+ function withInternalAgentRunOptions(options, internal) {
104
181
  return {
105
- enqueue(value) {
106
- if (closed) {
107
- return;
108
- }
109
- values.push(value);
110
- flush();
182
+ ...options,
183
+ [internalAgentRunOptions]: internal
184
+ };
185
+ }
186
+ function getInternalAgentRunOptions(options) {
187
+ return options[internalAgentRunOptions];
188
+ }
189
+
190
+ // src/agent/agent.ts
191
+ import { z } from "zod";
192
+
193
+ // src/agent/lifecycle.ts
194
+ function composeAgentLifecycle(first, second) {
195
+ if (first === void 0) return second;
196
+ if (second === void 0) return first;
197
+ return {
198
+ async onStart(event) {
199
+ await first.onStart?.(lifecycleSnapshot(event));
200
+ await second.onStart?.(lifecycleSnapshot(event));
111
201
  },
112
- close() {
113
- closed = true;
114
- flush();
202
+ async onStepFinish(event) {
203
+ await first.onStepFinish?.(lifecycleSnapshot(event));
204
+ await second.onStepFinish?.(lifecycleSnapshot(event));
115
205
  },
116
- throw(thrown) {
117
- closed = true;
118
- error = thrown;
119
- flush();
206
+ async onToolStart(event) {
207
+ await first.onToolStart?.(lifecycleSnapshot(event));
208
+ await second.onToolStart?.(lifecycleSnapshot(event));
120
209
  },
121
- [Symbol.asyncIterator]() {
122
- return {
123
- next() {
124
- if (values.length > 0) {
125
- const value = values.shift();
126
- return Promise.resolve({ value, done: false });
127
- }
128
- if (error !== void 0) {
129
- return Promise.reject(error);
130
- }
131
- if (closed) {
132
- return Promise.resolve({ value: void 0, done: true });
133
- }
134
- return new Promise((resolve, reject) => {
135
- waiters.push({ resolve, reject });
136
- });
137
- }
138
- };
210
+ async onToolFinish(event) {
211
+ await first.onToolFinish?.(lifecycleSnapshot(event));
212
+ await second.onToolFinish?.(lifecycleSnapshot(event));
213
+ },
214
+ async onFinish(event) {
215
+ await first.onFinish?.(lifecycleSnapshot(event));
216
+ await second.onFinish?.(lifecycleSnapshot(event));
217
+ },
218
+ async onError(event) {
219
+ await first.onError?.(lifecycleSnapshot(event));
220
+ await second.onError?.(lifecycleSnapshot(event));
139
221
  }
140
222
  };
141
223
  }
224
+ function lifecycleSnapshot(value) {
225
+ try {
226
+ return globalThis.structuredClone(value);
227
+ } catch {
228
+ return cloneLifecycleFallback(value, /* @__PURE__ */ new WeakMap());
229
+ }
230
+ }
231
+ function cloneLifecycleFallback(value, seen) {
232
+ if (typeof value !== "object" || value === null) {
233
+ return value;
234
+ }
235
+ const existing = seen.get(value);
236
+ if (existing !== void 0) {
237
+ return existing;
238
+ }
239
+ if (Array.isArray(value)) {
240
+ const clone2 = [];
241
+ seen.set(value, clone2);
242
+ clone2.push(...value.map((item) => cloneLifecycleFallback(item, seen)));
243
+ return clone2;
244
+ }
245
+ if (Object.getPrototypeOf(value) !== Object.prototype) {
246
+ return value;
247
+ }
248
+ const clone = {};
249
+ seen.set(value, clone);
250
+ for (const [key, item] of Object.entries(value)) {
251
+ clone[key] = cloneLifecycleFallback(item, seen);
252
+ }
253
+ return clone;
254
+ }
142
255
 
143
- // src/internal/prompt-runtime/memory.ts
144
- var PromptRequestMemory = class {
145
- constructor(agent, memoryContext, initialHistory) {
146
- this.agent = agent;
147
- this.memoryContext = memoryContext;
148
- this.initialHistory = initialHistory;
256
+ // src/observability/snapshot.ts
257
+ function observerSnapshot(value) {
258
+ if (containsError(value, /* @__PURE__ */ new WeakSet())) {
259
+ return cloneObserverFallback(value, /* @__PURE__ */ new WeakMap());
149
260
  }
150
- agent;
151
- memoryContext;
152
- initialHistory;
153
- memoryPolicy() {
154
- return this.memory()?.options.savePolicy;
261
+ try {
262
+ return globalThis.structuredClone(value);
263
+ } catch {
264
+ return cloneObserverFallback(value, /* @__PURE__ */ new WeakMap());
155
265
  }
156
- pendingTurnMessages(newMessages) {
157
- return this.memoryPolicy() === "turn" ? [...newMessages] : [];
266
+ }
267
+ function cloneObserverFallback(value, seen) {
268
+ if (typeof value !== "object" || value === null) {
269
+ return value;
158
270
  }
159
- async prepareRun(runId, newMessages) {
160
- const memory = this.memory();
161
- if (memory === void 0 || this.memoryContext === void 0) {
162
- return {
163
- history: this.initialHistory,
164
- usage: Usage.empty()
165
- };
166
- }
167
- const preparation = await this.prepareStoredHistory(memory, runId, newMessages.length);
168
- const memoryHistory = preparation.history;
169
- const chatHistory = [...memoryHistory, ...this.initialHistory];
170
- if (memory.options.savePolicy === "message") {
171
- await memory.store.append({
172
- context: this.memoryContext,
173
- runId,
174
- turn: 1,
175
- messages: newMessages
176
- });
177
- }
178
- return {
179
- ...preparation,
180
- history: chatHistory
181
- };
271
+ const existing = seen.get(value);
272
+ if (existing !== void 0) {
273
+ return existing;
182
274
  }
183
- async commitMessages(runId, turn, messages, pendingTurnMessages) {
184
- const memory = this.memory();
185
- if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
186
- return;
275
+ if (Array.isArray(value)) {
276
+ const clone2 = [];
277
+ seen.set(value, clone2);
278
+ clone2.push(...value.map((item) => cloneObserverFallback(item, seen)));
279
+ return clone2;
280
+ }
281
+ if (value instanceof Date) {
282
+ return new Date(value.getTime());
283
+ }
284
+ if (value instanceof Error) {
285
+ const clone2 = Object.create(Object.getPrototypeOf(value));
286
+ seen.set(value, clone2);
287
+ Object.defineProperties(clone2, {
288
+ name: { configurable: true, writable: true, value: value.name },
289
+ message: { configurable: true, writable: true, value: value.message }
290
+ });
291
+ if (value.stack !== void 0) {
292
+ Object.defineProperty(clone2, "stack", {
293
+ configurable: true,
294
+ writable: true,
295
+ value: value.stack
296
+ });
187
297
  }
188
- if (memory.options.savePolicy === "message") {
189
- await memory.store.append({
190
- context: this.memoryContext,
191
- runId,
192
- turn,
193
- messages
298
+ if (value.cause !== void 0) {
299
+ Object.defineProperty(clone2, "cause", {
300
+ configurable: true,
301
+ writable: true,
302
+ value: cloneObserverFallback(value.cause, seen)
194
303
  });
195
- } else if (memory.options.savePolicy === "turn") {
196
- pendingTurnMessages.push(...messages);
197
304
  }
198
- }
199
- async commitCompletedTurn(runId, turn, pendingTurnMessages) {
200
- const memory = this.memory();
201
- if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
202
- return;
305
+ for (const key of Object.getOwnPropertyNames(value)) {
306
+ if (key === "name" || key === "message" || key === "stack" || key === "cause") continue;
307
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
308
+ if (descriptor === void 0) continue;
309
+ if ("value" in descriptor) {
310
+ descriptor.value = cloneObserverFallback(descriptor.value, seen);
311
+ }
312
+ Object.defineProperty(clone2, key, descriptor);
203
313
  }
204
- await memory.store.append({
205
- context: this.memoryContext,
206
- runId,
207
- turn,
208
- messages: [...pendingTurnMessages]
209
- });
210
- pendingTurnMessages.length = 0;
314
+ return clone2;
211
315
  }
212
- async commitCompletedRun(runId, turn, newMessages, pendingTurnMessages) {
213
- await this.commitCompletedTurn(runId, turn, pendingTurnMessages);
214
- const memory = this.memory();
215
- if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
216
- return;
217
- }
218
- await memory.store.append({
219
- context: this.memoryContext,
220
- runId,
221
- turn,
222
- messages: [...newMessages]
223
- });
316
+ if (Object.getPrototypeOf(value) !== Object.prototype) {
317
+ return value;
224
318
  }
225
- async recordError(runId, error, newMessages) {
226
- const memory = this.memory();
227
- if (memory === void 0 || this.memoryContext === void 0) {
228
- return;
229
- }
230
- await memory.store.recordError?.({
231
- context: this.memoryContext,
232
- runId,
233
- error,
234
- messages: [...newMessages]
235
- });
319
+ const clone = {};
320
+ seen.set(value, clone);
321
+ for (const [key, item] of Object.entries(value)) {
322
+ clone[key] = cloneObserverFallback(item, seen);
236
323
  }
237
- memory() {
238
- return this.memoryContext === void 0 ? void 0 : this.agent.memory;
324
+ return clone;
325
+ }
326
+ function containsError(value, seen) {
327
+ if (typeof value !== "object" || value === null) return false;
328
+ if (value instanceof Error) return true;
329
+ if (seen.has(value)) return false;
330
+ seen.add(value);
331
+ if (Array.isArray(value)) {
332
+ return value.some((item) => containsError(item, seen));
239
333
  }
240
- async prepareStoredHistory(memory, runId, incomingMessageCount) {
241
- const context = this.memoryContext;
242
- if (context === void 0) {
243
- return { history: [], usage: Usage.empty() };
244
- }
245
- const options = memory.options.compaction;
246
- const capability = memory.store.compaction;
247
- if (options === void 0 || capability === void 0) {
248
- return {
249
- history: await memory.store.load(context),
250
- usage: Usage.empty()
251
- };
334
+ if (Object.getPrototypeOf(value) !== Object.prototype) return false;
335
+ return Object.values(value).some((item) => containsError(item, seen));
336
+ }
337
+
338
+ // src/observability/group.ts
339
+ async function startAgentRunObservers(registrations, args, failOnObserverError) {
340
+ const runObservers = [];
341
+ for (const registration of registrations) {
342
+ try {
343
+ const runObserver = await registration.observer.startRun(observerSnapshot(args));
344
+ if (runObserver !== void 0) {
345
+ runObservers.push(runObserver);
346
+ }
347
+ } catch (error) {
348
+ if (failOnObserverError || registration.failOnObserverError === true) {
349
+ throw error;
350
+ }
252
351
  }
253
- let usage = Usage.empty();
254
- let conflictRetries = 0;
255
- for (let attempt = 0; attempt <= options.conflictRetries; attempt += 1) {
256
- const snapshot = await capability.load(context);
257
- const compactedMessageCount = compactedPrefixLength(
258
- snapshot.messages,
259
- incomingMessageCount,
260
- options.maxMessages,
261
- options.keepRecentUserTurns
262
- );
263
- if (compactedMessageCount === 0) {
264
- return { history: snapshot.messages, usage };
352
+ }
353
+ return new ActiveAgentRunObservers(runObservers, failOnObserverError);
354
+ }
355
+ var ActiveAgentRunObservers = class {
356
+ constructor(runObservers, failOnObserverError) {
357
+ this.runObservers = runObservers;
358
+ this.failOnObserverError = failOnObserverError;
359
+ this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
360
+ }
361
+ runObservers;
362
+ failOnObserverError;
363
+ trace;
364
+ async startGeneration(args) {
365
+ const generationObservers = [];
366
+ for (const runObserver of this.runObservers) {
367
+ if (runObserver.startGeneration === void 0) {
368
+ continue;
265
369
  }
266
- const prefix = snapshot.messages.slice(0, compactedMessageCount);
267
- let result;
268
370
  try {
269
- result = await options.compactor({
270
- context,
271
- messages: prefix
272
- });
371
+ const generationObserver = await runObserver.startGeneration(observerSnapshot(args));
372
+ if (generationObserver !== void 0) {
373
+ generationObservers.push(generationObserver);
374
+ }
273
375
  } catch (error) {
274
- throw remappedCompactorError(error, usage);
275
- }
276
- if (result.usage !== void 0) {
277
- usage = Usage.add(usage, result.usage);
376
+ this.handleError(error);
278
377
  }
279
- const summaryText = result.summary.trim();
280
- if (summaryText.length === 0) {
281
- throw new MemoryCompactionError("Memory compactor returned an empty summary.", {
282
- usage
283
- });
378
+ }
379
+ return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
380
+ }
381
+ async startTool(args) {
382
+ const toolObservers = [];
383
+ for (const runObserver of this.runObservers) {
384
+ if (runObserver.startTool === void 0) {
385
+ continue;
284
386
  }
285
- const summary = createMemoryCompactionSummary(
286
- summaryText,
287
- cumulativeCompactedMessageCount(prefix)
288
- );
289
- let commit;
290
387
  try {
291
- commit = await capability.commit({
292
- context,
293
- revision: snapshot.revision,
294
- compactedMessageCount,
295
- summary,
296
- runId: `memory-compaction:${runId}:${attempt + 1}`
297
- });
388
+ const toolObserver = await runObserver.startTool(observerSnapshot(args));
389
+ if (toolObserver !== void 0) {
390
+ toolObservers.push(toolObserver);
391
+ }
298
392
  } catch (error) {
299
- throw new MemoryCompactionError("Memory compaction store commit failed.", {
300
- cause: error,
301
- usage
302
- });
393
+ this.handleError(error);
303
394
  }
304
- if (commit === "committed") {
305
- const retained = snapshot.messages.slice(compactedMessageCount);
306
- return {
307
- history: [summary, ...retained],
308
- usage,
309
- compaction: {
310
- originalMessageCount: snapshot.messages.length,
311
- compactedMessageCount,
312
- retainedMessageCount: retained.length,
313
- conflictRetries
314
- }
315
- };
395
+ }
396
+ return new ActiveToolObservers(toolObservers, this.failOnObserverError);
397
+ }
398
+ async end(args) {
399
+ for (const runObserver of this.runObservers) {
400
+ try {
401
+ await runObserver.end(observerSnapshot(args));
402
+ } catch (error) {
403
+ this.handleError(error);
316
404
  }
317
- conflictRetries += 1;
318
405
  }
319
- throw new MemoryCompactionConflictError(options.conflictRetries + 1, usage);
320
406
  }
321
- };
322
- function compactedPrefixLength(messages, incomingMessageCount, maxMessages, keepRecentUserTurns) {
323
- if (messages.length + incomingMessageCount <= maxMessages) {
324
- return 0;
407
+ async error(args) {
408
+ for (const runObserver of this.runObservers) {
409
+ if (runObserver.error === void 0) {
410
+ continue;
411
+ }
412
+ try {
413
+ await runObserver.error(observerSnapshot(args));
414
+ } catch (error) {
415
+ this.handleError(error);
416
+ }
417
+ }
325
418
  }
326
- const userMessageIndexes = messages.flatMap(
327
- (message, index) => message.role === "user" ? [index] : []
328
- );
329
- if (userMessageIndexes.length <= keepRecentUserTurns) {
330
- return 0;
419
+ async event(args) {
420
+ for (const runObserver of this.runObservers) {
421
+ if (runObserver.event === void 0) {
422
+ continue;
423
+ }
424
+ try {
425
+ await runObserver.event(observerSnapshot(args));
426
+ } catch (error) {
427
+ this.handleError(error);
428
+ }
429
+ }
331
430
  }
332
- return userMessageIndexes[userMessageIndexes.length - keepRecentUserTurns] ?? 0;
333
- }
334
- function remappedCompactorError(error, usage) {
335
- if (error instanceof MemoryCompactionError) {
336
- const mergedUsage = error.usage === void 0 ? usage : isEmptyUsage(usage) ? error.usage : Usage.add(usage, error.usage);
337
- if (mergedUsage === error.usage || error.usage === void 0 && isEmptyUsage(usage)) {
338
- return error;
431
+ handleError(error) {
432
+ if (this.failOnObserverError) {
433
+ throw error;
339
434
  }
340
- return new MemoryCompactionError(error.message, {
341
- cause: error,
342
- usage: mergedUsage
343
- });
344
435
  }
345
- return new MemoryCompactionError("Memory compactor failed.", {
346
- cause: error,
347
- usage
348
- });
349
- }
350
- function isEmptyUsage(usage) {
351
- return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
352
- }
353
-
354
- // src/internal/prompt-runtime/retrieval.ts
355
- async function fetchDynamicContext(agent, ragText) {
356
- if (ragText === void 0 || ragText.length === 0 || agent.dynamicContexts.length === 0) {
357
- return [];
436
+ };
437
+ var ActiveGenerationObservers = class {
438
+ constructor(generationObservers, failOnObserverError) {
439
+ this.generationObservers = generationObservers;
440
+ this.failOnObserverError = failOnObserverError;
358
441
  }
359
- const documents = [];
360
- for (const registration of agent.dynamicContexts) {
361
- const results = await registration.index.search({
362
- query: ragText,
363
- topK: registration.options.topK,
364
- threshold: registration.options.threshold,
365
- filter: registration.options.filter
366
- });
367
- for (const result of results) {
368
- const formatted = registration.options.format?.(result);
369
- if (formatted !== void 0) {
370
- documents.push(formatted);
371
- } else {
372
- const metadata = formatMetadata(result.metadata);
373
- const document = {
374
- id: result.id,
375
- text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2)
376
- };
377
- if (metadata !== void 0) {
378
- document.additionalProps = metadata;
379
- }
380
- documents.push(document);
442
+ generationObservers;
443
+ failOnObserverError;
444
+ async end(args) {
445
+ for (const observer of this.generationObservers) {
446
+ try {
447
+ await observer.end(observerSnapshot(args));
448
+ } catch (error) {
449
+ this.handleError(error);
381
450
  }
382
451
  }
383
452
  }
384
- return documents;
385
- }
386
- async function fetchToolDefinitions(agent, ragText) {
387
- const staticDefinitions = await agent.toolSet.getToolDefinitions(ragText);
388
- if (ragText === void 0 || ragText.length === 0 || agent.dynamicTools.length === 0) {
389
- return staticDefinitions;
453
+ async error(args) {
454
+ for (const observer of this.generationObservers) {
455
+ if (observer.error === void 0) {
456
+ continue;
457
+ }
458
+ try {
459
+ await observer.error(observerSnapshot(args));
460
+ } catch (error) {
461
+ this.handleError(error);
462
+ }
463
+ }
390
464
  }
391
- const definitions = [...staticDefinitions];
392
- const names = new Set(staticDefinitions.map((definition) => definition.name));
393
- for (const registration of agent.dynamicTools) {
394
- const results = await registration.index.search({
395
- query: ragText,
396
- topK: registration.options.topK,
397
- threshold: registration.options.threshold,
398
- filter: registration.options.filter
399
- });
400
- for (const result of results) {
401
- if (names.has(result.document.toolName)) {
465
+ async update(args) {
466
+ for (const observer of this.generationObservers) {
467
+ if (observer.update === void 0) {
402
468
  continue;
403
469
  }
404
- names.add(result.document.toolName);
405
- definitions.push(result.document.definition);
470
+ try {
471
+ await observer.update(observerSnapshot(args));
472
+ } catch (error) {
473
+ this.handleError(error);
474
+ }
406
475
  }
407
476
  }
408
- return definitions;
409
- }
410
- function formatMetadata(metadata) {
411
- if (metadata === void 0) {
412
- return void 0;
477
+ handleError(error) {
478
+ if (this.failOnObserverError) {
479
+ throw error;
480
+ }
413
481
  }
414
- return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
415
- }
416
-
417
- // src/internal/prompt-runtime/stream-accumulator.ts
418
- var CompletionStreamAccumulator = class {
419
- orderedParts = [];
420
- textParts = /* @__PURE__ */ new Map();
421
- reasoningByKey = /* @__PURE__ */ new Map();
422
- reasoningKeyById = /* @__PURE__ */ new Map();
423
- toolCalls = /* @__PURE__ */ new Map();
424
- sources = /* @__PURE__ */ new Map();
425
- providerToolCalls = /* @__PURE__ */ new Map();
426
- finalResponse;
427
- messageId;
428
- nextTextKey = 0;
429
- nextReasoningKey = 0;
430
- accept(event) {
431
- if (event.type === "text_delta") {
432
- this.appendText(event.delta);
433
- return { type: "text_delta", delta: event.delta };
482
+ };
483
+ var ActiveToolObservers = class {
484
+ constructor(toolObservers, failOnObserverError) {
485
+ this.toolObservers = toolObservers;
486
+ this.failOnObserverError = failOnObserverError;
487
+ }
488
+ toolObservers;
489
+ failOnObserverError;
490
+ async streamEvent(args) {
491
+ for (const observer of this.toolObservers) {
492
+ if (observer.streamEvent === void 0) {
493
+ continue;
494
+ }
495
+ try {
496
+ await observer.streamEvent(observerSnapshot(args));
497
+ } catch (error) {
498
+ this.handleError(error);
499
+ }
434
500
  }
435
- if (event.type === "reasoning_delta") {
436
- const reasoning = this.reasoningStateForEvent(event);
437
- this.appendReasoning(reasoning, event);
438
- return reasoningDeltaEvent(event);
501
+ }
502
+ async end(args) {
503
+ for (const observer of this.toolObservers) {
504
+ try {
505
+ await observer.end(observerSnapshot(args));
506
+ } catch (error) {
507
+ this.handleError(error);
508
+ }
439
509
  }
440
- if (event.type === "tool_call_delta") {
441
- const toolCall = this.toolCallStateForId(event.id);
442
- if (event.callId !== void 0 && event.callId.length > 0) toolCall.callId = event.callId;
443
- if (event.name !== void 0 && event.name.length > 0) toolCall.name = event.name;
444
- if (event.signature !== void 0) toolCall.signature = event.signature;
445
- if (event.argumentsDelta !== void 0) {
446
- if (event.argumentsMode === "replace") {
447
- toolCall.argumentsText = event.argumentsDelta;
448
- } else {
449
- toolCall.argumentsText += event.argumentsDelta;
450
- }
510
+ }
511
+ async error(args) {
512
+ for (const observer of this.toolObservers) {
513
+ if (observer.error === void 0) {
514
+ continue;
515
+ }
516
+ try {
517
+ await observer.error(observerSnapshot(args));
518
+ } catch (error) {
519
+ this.handleError(error);
451
520
  }
452
- return void 0;
453
521
  }
454
- if (event.type === "tool_call") {
455
- this.upsertToolCall(event.toolCall);
456
- return { type: "tool_call", toolCall: event.toolCall };
522
+ }
523
+ handleError(error) {
524
+ if (this.failOnObserverError) {
525
+ throw error;
457
526
  }
458
- if (event.type === "source") {
459
- this.sources.set(sourceKey(event.source), event.source);
460
- return { type: "source", source: event.source };
461
- }
462
- if (event.type === "provider_tool_call") {
463
- this.providerToolCalls.set(event.toolCall.id, event.toolCall);
464
- return { type: "provider_tool_call", toolCall: event.toolCall };
465
- }
466
- if (event.type === "message_id") {
467
- this.messageId = event.id;
468
- return void 0;
469
- }
470
- if (event.type === "final") {
471
- this.finalResponse = event.response;
472
- return void 0;
473
- }
474
- return void 0;
475
527
  }
476
- response() {
477
- const accumulatedResponse = this.buildAccumulatedResponse();
478
- if (this.finalResponse !== void 0) {
479
- if (accumulatedResponse.choice.length === 0) {
480
- return this.withAccumulatedArtifacts(this.finalResponse, accumulatedResponse);
528
+ };
529
+
530
+ // src/internal/async-queue.ts
531
+ function createAsyncQueue() {
532
+ const values = [];
533
+ const waiters = [];
534
+ let closed = false;
535
+ let error;
536
+ function flush() {
537
+ while (waiters.length > 0 && values.length > 0) {
538
+ const waiter = waiters.shift();
539
+ const value = values.shift();
540
+ if (waiter !== void 0) {
541
+ waiter.resolve({ value, done: false });
481
542
  }
482
- return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
483
543
  }
484
- return accumulatedResponse;
485
- }
486
- buildAccumulatedResponse() {
487
- const choice = [];
488
- for (const part of this.orderedParts) {
489
- if (part.type === "text") {
490
- const text = this.textParts.get(part.key) ?? "";
491
- if (text.length > 0) {
492
- choice.push({ type: "text", text });
493
- }
494
- continue;
495
- }
496
- if (part.type === "reasoning") {
497
- const reasoning = this.reasoningByKey.get(part.key);
498
- if (reasoning !== void 0) {
499
- choice.push(reasoningContent(reasoning));
500
- }
544
+ if (values.length > 0 || waiters.length === 0 || !closed) {
545
+ return;
546
+ }
547
+ while (waiters.length > 0) {
548
+ const waiter = waiters.shift();
549
+ if (waiter === void 0) {
501
550
  continue;
502
551
  }
503
- const toolCall = this.toolCalls.get(part.key);
504
- if (toolCall !== void 0) {
505
- choice.push(
506
- toolCallContent(toolCall, matchingFinalToolCall(toolCall, this.finalResponse?.choice))
507
- );
552
+ if (error !== void 0) {
553
+ waiter.reject(error);
554
+ } else {
555
+ waiter.resolve({ value: void 0, done: true });
508
556
  }
509
557
  }
510
- const response = {
511
- choice,
512
- usage: Usage.empty(),
513
- rawResponse: void 0
514
- };
515
- if (this.messageId !== void 0) {
516
- response.messageId = this.messageId;
517
- }
518
- const sources = [...this.sources.values()];
519
- if (sources.length > 0) {
520
- response.sources = sources;
521
- }
522
- const providerToolCalls = [...this.providerToolCalls.values()];
523
- if (providerToolCalls.length > 0) {
524
- response.providerToolCalls = providerToolCalls;
558
+ }
559
+ return {
560
+ enqueue(value) {
561
+ if (closed) {
562
+ return;
563
+ }
564
+ values.push(value);
565
+ flush();
566
+ },
567
+ close() {
568
+ closed = true;
569
+ flush();
570
+ },
571
+ throw(thrown) {
572
+ closed = true;
573
+ error = thrown;
574
+ flush();
575
+ },
576
+ [Symbol.asyncIterator]() {
577
+ return {
578
+ next() {
579
+ if (values.length > 0) {
580
+ const value = values.shift();
581
+ return Promise.resolve({ value, done: false });
582
+ }
583
+ if (error !== void 0) {
584
+ return Promise.reject(error);
585
+ }
586
+ if (closed) {
587
+ return Promise.resolve({ value: void 0, done: true });
588
+ }
589
+ return new Promise((resolve, reject) => {
590
+ waiters.push({ resolve, reject });
591
+ });
592
+ }
593
+ };
525
594
  }
526
- return response;
595
+ };
596
+ }
597
+
598
+ // src/internal/rag-text.ts
599
+ function extractRagText(message) {
600
+ if (message.role === "user") {
601
+ return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
527
602
  }
528
- upsertToolCall(toolCall) {
529
- if (!this.toolCalls.has(toolCall.id)) {
530
- this.orderedParts.push({ type: "tool_call", key: toolCall.id });
603
+ if (message.role === "tool") {
604
+ return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
605
+ }
606
+ return void 0;
607
+ }
608
+
609
+ // src/internal/agent-runtime/memory.ts
610
+ var AgentRunMemory = class {
611
+ constructor(agent, memoryContext, initialHistory) {
612
+ this.agent = agent;
613
+ this.memoryContext = memoryContext;
614
+ this.initialHistory = initialHistory;
615
+ }
616
+ agent;
617
+ memoryContext;
618
+ initialHistory;
619
+ memoryPolicy() {
620
+ return this.memory()?.options.savePolicy;
621
+ }
622
+ pendingTurnMessages(newMessages) {
623
+ return this.memoryPolicy() === "turn" ? [...newMessages] : [];
624
+ }
625
+ async prepareHistory(runId, incomingMessageCount) {
626
+ const memory = this.memory();
627
+ if (memory === void 0 || this.memoryContext === void 0) {
628
+ return {
629
+ history: this.initialHistory,
630
+ usage: Usage.empty()
631
+ };
531
632
  }
532
- const partial = {
533
- id: toolCall.id,
534
- name: toolCall.function.name,
535
- argumentsText: JSON.stringify(toolCall.function.arguments ?? {})
633
+ const preparation = await this.prepareStoredHistory(memory, runId, incomingMessageCount);
634
+ const memoryHistory = preparation.history;
635
+ const chatHistory = [...memoryHistory, ...this.initialHistory];
636
+ return {
637
+ ...preparation,
638
+ history: chatHistory
536
639
  };
537
- if (toolCall.callId !== void 0) {
538
- partial.callId = toolCall.callId;
640
+ }
641
+ async commitAcceptedInput(runId, messages) {
642
+ const memory = this.memory();
643
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "message" || messages.length === 0) {
644
+ return;
539
645
  }
540
- if (toolCall.signature !== void 0) {
541
- partial.signature = toolCall.signature;
646
+ await memory.store.append({
647
+ context: this.memoryContext,
648
+ runId,
649
+ turn: 1,
650
+ messages
651
+ });
652
+ }
653
+ async commitMessages(runId, turn, messages, pendingTurnMessages) {
654
+ const memory = this.memory();
655
+ if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
656
+ return;
542
657
  }
543
- if (toolCall.additionalParams !== void 0) {
544
- partial.additionalParams = toolCall.additionalParams;
658
+ if (memory.options.savePolicy === "message") {
659
+ await memory.store.append({
660
+ context: this.memoryContext,
661
+ runId,
662
+ turn,
663
+ messages
664
+ });
665
+ } else if (memory.options.savePolicy === "turn") {
666
+ pendingTurnMessages.push(...messages);
545
667
  }
546
- this.toolCalls.set(toolCall.id, partial);
547
668
  }
548
- mergeFinalResponse(accumulatedResponse, finalResponse) {
549
- if (finalResponse.choice.length === 0) {
550
- const mergedResponse = {
551
- ...accumulatedResponse,
552
- usage: finalResponse.usage,
553
- rawResponse: finalResponse.rawResponse
554
- };
555
- if (finalResponse.messageId !== void 0) {
556
- mergedResponse.messageId = finalResponse.messageId;
557
- }
558
- return this.withAccumulatedArtifacts(mergedResponse, accumulatedResponse);
559
- }
560
- const finalById = /* @__PURE__ */ new Map();
561
- const finalByCallId = /* @__PURE__ */ new Map();
562
- for (const content of finalResponse.choice) {
563
- if (content.type !== "tool_call") {
564
- continue;
565
- }
566
- finalById.set(content.id, content);
567
- if (content.callId !== void 0) {
568
- finalByCallId.set(content.callId, content);
569
- }
669
+ async commitCompletedTurn(runId, turn, pendingTurnMessages) {
670
+ const memory = this.memory();
671
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
672
+ return;
570
673
  }
571
- const matchedFinalToolCalls = /* @__PURE__ */ new Set();
572
- const choice = accumulatedResponse.choice.map((content) => {
573
- if (content.type !== "tool_call") {
574
- return content;
575
- }
576
- const finalToolCall = finalById.get(content.id) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
577
- if (finalToolCall === void 0) {
578
- return content;
579
- }
580
- matchedFinalToolCalls.add(finalToolCall);
581
- return mergeFinalToolCall(content, finalToolCall);
674
+ await memory.store.append({
675
+ context: this.memoryContext,
676
+ runId,
677
+ turn,
678
+ messages: [...pendingTurnMessages]
582
679
  });
583
- for (const content of finalResponse.choice) {
584
- if (content.type !== "tool_call") {
585
- continue;
586
- }
587
- if (!matchedFinalToolCalls.has(content)) {
588
- choice.push(content);
589
- }
590
- }
591
- return this.withAccumulatedArtifacts({ ...finalResponse, choice }, accumulatedResponse);
680
+ pendingTurnMessages.length = 0;
592
681
  }
593
- appendText(delta) {
594
- const lastPart = this.orderedParts.at(-1);
595
- const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
596
- if (lastPart?.type !== "text") {
597
- this.orderedParts.push({ type: "text", key });
682
+ async commitCompletedRun(runId, turn, newMessages, pendingTurnMessages) {
683
+ await this.commitCompletedTurn(runId, turn, pendingTurnMessages);
684
+ const memory = this.memory();
685
+ if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
686
+ return;
598
687
  }
599
- this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
600
- }
601
- reasoningStateForEvent(event) {
602
- if (event.id !== void 0) {
603
- const existingKey = this.reasoningKeyById.get(event.id);
604
- if (existingKey !== void 0) {
605
- const existing = this.reasoningByKey.get(existingKey);
606
- if (existing !== void 0) {
607
- return existing;
608
- }
609
- }
610
- const key2 = this.createReasoningKey();
611
- const reasoning2 = { id: event.id, text: "" };
612
- this.reasoningKeyById.set(event.id, key2);
613
- this.reasoningByKey.set(key2, reasoning2);
614
- this.orderedParts.push({ type: "reasoning", key: key2 });
615
- return reasoning2;
616
- }
617
- const lastPart = this.orderedParts.at(-1);
618
- if (lastPart?.type === "reasoning") {
619
- const lastReasoning = this.reasoningByKey.get(lastPart.key);
620
- if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
621
- return lastReasoning;
622
- }
623
- }
624
- const key = this.createReasoningKey();
625
- const reasoning = { text: "" };
626
- this.reasoningByKey.set(key, reasoning);
627
- this.orderedParts.push({ type: "reasoning", key });
628
- return reasoning;
629
- }
630
- toolCallStateForId(id) {
631
- const existing = this.toolCalls.get(id);
632
- if (existing !== void 0) {
633
- return existing;
634
- }
635
- const toolCall = {
636
- id,
637
- name: "",
638
- argumentsText: ""
639
- };
640
- this.toolCalls.set(id, toolCall);
641
- this.orderedParts.push({ type: "tool_call", key: id });
642
- return toolCall;
688
+ await memory.store.append({
689
+ context: this.memoryContext,
690
+ runId,
691
+ turn,
692
+ messages: [...newMessages]
693
+ });
643
694
  }
644
- withMessageIdFallback(response, accumulatedResponse) {
645
- if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
646
- return response;
695
+ async recordError(runId, error, newMessages) {
696
+ const memory = this.memory();
697
+ if (memory === void 0 || this.memoryContext === void 0) {
698
+ return;
647
699
  }
648
- return { ...response, messageId: accumulatedResponse.messageId };
649
- }
650
- withAccumulatedArtifacts(response, accumulatedResponse) {
651
- const withMessageId = this.withMessageIdFallback(response, accumulatedResponse);
652
- const sources = mergeSources(accumulatedResponse.sources, response.sources);
653
- const providerToolCalls = mergeProviderToolCalls(
654
- accumulatedResponse.providerToolCalls,
655
- response.providerToolCalls
656
- );
657
- return {
658
- ...withMessageId,
659
- ...sources.length === 0 ? {} : { sources },
660
- ...providerToolCalls.length === 0 ? {} : { providerToolCalls }
661
- };
662
- }
663
- createTextKey() {
664
- this.nextTextKey += 1;
665
- return `text_${this.nextTextKey.toString()}`;
700
+ await memory.store.recordError?.({
701
+ context: this.memoryContext,
702
+ runId,
703
+ error,
704
+ messages: [...newMessages]
705
+ });
666
706
  }
667
- createReasoningKey() {
668
- this.nextReasoningKey += 1;
669
- return `reasoning_${this.nextReasoningKey.toString()}`;
707
+ memory() {
708
+ return this.memoryContext === void 0 ? void 0 : this.agent.memory;
670
709
  }
671
- appendReasoning(reasoning, event) {
672
- const contentType = event.contentType ?? "text";
673
- if (contentType === "text" || contentType === "summary") {
674
- reasoning.text += event.delta;
710
+ async prepareStoredHistory(memory, runId, incomingMessageCount) {
711
+ const context = this.memoryContext;
712
+ if (context === void 0) {
713
+ return { history: [], usage: Usage.empty() };
675
714
  }
676
- if (event.contentType === void 0 && event.signature === void 0) {
677
- return;
715
+ const options = memory.options.compaction;
716
+ const capability = memory.store.compaction;
717
+ if (options === void 0 || capability === void 0) {
718
+ return {
719
+ history: await memory.store.load(context),
720
+ usage: Usage.empty()
721
+ };
678
722
  }
679
- reasoning.content ??= [];
680
- const last = reasoning.content.at(-1);
681
- if (contentType === "text") {
682
- if (last?.type === "text") {
683
- last.text += event.delta;
684
- if (event.signature !== void 0) {
685
- last.signature = event.signature;
686
- }
687
- } else {
688
- reasoning.content.push(
689
- event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
690
- );
723
+ let usage = Usage.empty();
724
+ let conflictRetries = 0;
725
+ for (let attempt = 0; attempt <= options.conflictRetries; attempt += 1) {
726
+ const snapshot = await capability.load(context);
727
+ const compactedMessageCount = compactedPrefixLength(
728
+ snapshot.messages,
729
+ incomingMessageCount,
730
+ options.maxMessages,
731
+ options.keepRecentUserTurns
732
+ );
733
+ if (compactedMessageCount === 0) {
734
+ return { history: snapshot.messages, usage };
691
735
  }
692
- return;
693
- }
694
- if (contentType === "summary") {
695
- if (last?.type === "summary") {
696
- last.text += event.delta;
697
- } else {
698
- reasoning.content.push({ type: "summary", text: event.delta });
736
+ const prefix = snapshot.messages.slice(0, compactedMessageCount);
737
+ let result;
738
+ try {
739
+ result = await options.compactor({
740
+ context,
741
+ messages: prefix
742
+ });
743
+ } catch (error) {
744
+ throw remappedCompactorError(error, usage);
699
745
  }
700
- return;
701
- }
702
- if (contentType === "encrypted") {
703
- reasoning.content.push({ type: "encrypted", data: event.delta });
704
- return;
746
+ if (result.usage !== void 0) {
747
+ usage = Usage.add(usage, result.usage);
748
+ }
749
+ const summaryText = result.summary.trim();
750
+ if (summaryText.length === 0) {
751
+ throw new MemoryCompactionError("Memory compactor returned an empty summary.", {
752
+ usage
753
+ });
754
+ }
755
+ const summary = createMemoryCompactionSummary(
756
+ summaryText,
757
+ cumulativeCompactedMessageCount(prefix)
758
+ );
759
+ let commit;
760
+ try {
761
+ commit = await capability.commit({
762
+ context,
763
+ revision: snapshot.revision,
764
+ compactedMessageCount,
765
+ summary,
766
+ runId: `memory-compaction:${runId}:${attempt + 1}`
767
+ });
768
+ } catch (error) {
769
+ throw new MemoryCompactionError("Memory compaction store commit failed.", {
770
+ cause: error,
771
+ usage
772
+ });
773
+ }
774
+ if (commit === "committed") {
775
+ const retained = snapshot.messages.slice(compactedMessageCount);
776
+ return {
777
+ history: [summary, ...retained],
778
+ usage,
779
+ compaction: {
780
+ originalMessageCount: snapshot.messages.length,
781
+ compactedMessageCount,
782
+ retainedMessageCount: retained.length,
783
+ conflictRetries
784
+ }
785
+ };
786
+ }
787
+ conflictRetries += 1;
705
788
  }
706
- reasoning.content.push({ type: "redacted", data: event.delta });
789
+ throw new MemoryCompactionConflictError(options.conflictRetries + 1, usage);
707
790
  }
708
791
  };
709
- function sourceKey(source) {
710
- return `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
711
- }
712
- function mergeSources(accumulated, final) {
713
- const sources = /* @__PURE__ */ new Map();
714
- for (const source of [...accumulated ?? [], ...final ?? []]) {
715
- sources.set(sourceKey(source), source);
792
+ function compactedPrefixLength(messages, incomingMessageCount, maxMessages, keepRecentUserTurns) {
793
+ if (messages.length + incomingMessageCount <= maxMessages) {
794
+ return 0;
716
795
  }
717
- return [...sources.values()];
796
+ const userMessageIndexes = messages.flatMap(
797
+ (message, index) => message.role === "user" ? [index] : []
798
+ );
799
+ if (userMessageIndexes.length <= keepRecentUserTurns) {
800
+ return 0;
801
+ }
802
+ return userMessageIndexes[userMessageIndexes.length - keepRecentUserTurns] ?? 0;
718
803
  }
719
- function mergeProviderToolCalls(accumulated, final) {
720
- const toolCalls = /* @__PURE__ */ new Map();
721
- for (const toolCall of [...accumulated ?? [], ...final ?? []]) {
722
- toolCalls.set(toolCall.id, toolCall);
804
+ function remappedCompactorError(error, usage) {
805
+ if (error instanceof MemoryCompactionError) {
806
+ const mergedUsage = error.usage === void 0 ? usage : isEmptyUsage(usage) ? error.usage : Usage.add(usage, error.usage);
807
+ if (mergedUsage === error.usage || error.usage === void 0 && isEmptyUsage(usage)) {
808
+ return error;
809
+ }
810
+ return new MemoryCompactionError(error.message, {
811
+ cause: error,
812
+ usage: mergedUsage
813
+ });
723
814
  }
724
- return [...toolCalls.values()];
815
+ return new MemoryCompactionError("Memory compactor failed.", {
816
+ cause: error,
817
+ usage
818
+ });
725
819
  }
726
- function reasoningContent(reasoning) {
727
- const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
728
- return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
820
+ function isEmptyUsage(usage) {
821
+ return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
729
822
  }
730
- function toolCallContent(toolCall, finalToolCall) {
731
- const argumentsValue = finalToolCall !== void 0 && !isEmptyToolArguments(finalToolCall.function.arguments) ? finalToolCall.function.arguments : parseToolArguments(toolCall.id, toolCall.argumentsText);
732
- const content = {
733
- type: "tool_call",
734
- id: toolCall.id,
735
- function: {
736
- name: toolCall.name,
737
- arguments: argumentsValue
823
+
824
+ // src/internal/agent-runtime/retrieval.ts
825
+ async function fetchContextDocuments(agent, ragText) {
826
+ const documents = [];
827
+ for (const input of agent.context) {
828
+ if (!isContextIndex(input)) {
829
+ documents.push(input);
830
+ continue;
831
+ }
832
+ if (ragText === void 0 || ragText.length === 0) {
833
+ continue;
834
+ }
835
+ const results = await input.index.search({
836
+ query: ragText,
837
+ topK: input.topK,
838
+ threshold: input.threshold,
839
+ filter: input.filter
840
+ });
841
+ for (const result of results) {
842
+ const formatted = input.format?.(result);
843
+ if (formatted !== void 0) {
844
+ documents.push(formatted);
845
+ } else {
846
+ const metadata = formatMetadata(result.metadata);
847
+ const document = {
848
+ id: result.id,
849
+ text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2)
850
+ };
851
+ if (metadata !== void 0) {
852
+ document.additionalProps = metadata;
853
+ }
854
+ documents.push(document);
855
+ }
738
856
  }
739
- };
740
- if (toolCall.callId !== void 0) {
741
- content.callId = toolCall.callId;
742
- }
743
- if (toolCall.signature !== void 0) {
744
- content.signature = toolCall.signature;
745
- }
746
- if (toolCall.additionalParams !== void 0) {
747
- content.additionalParams = toolCall.additionalParams;
748
857
  }
749
- return content;
858
+ return documents;
750
859
  }
751
- function matchingFinalToolCall(accumulated, finalChoice) {
752
- const byId = finalChoice?.find(
753
- (content) => content.type === "tool_call" && content.id === accumulated.id
860
+ async function fetchToolDefinitions(agent, ragText) {
861
+ const state = getAgentToolState(agent);
862
+ const staticDefinitions = await Promise.all(
863
+ state.staticTools.map((tool) => tool.definition(ragText ?? ""))
754
864
  );
755
- if (byId !== void 0 || accumulated.callId === void 0) {
756
- return byId;
865
+ if (ragText === void 0 || ragText.length === 0 || state.toolIndexes.length === 0) {
866
+ return staticDefinitions;
757
867
  }
758
- return finalChoice?.find(
759
- (content) => content.type === "tool_call" && content.callId === accumulated.callId
760
- );
761
- }
762
- function mergeFinalToolCall(accumulated, finalToolCall) {
763
- const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
764
- return {
765
- ...accumulated,
766
- ...finalToolCall,
767
- function: {
768
- ...accumulated.function,
769
- ...finalToolCall.function,
770
- arguments: argumentsValue
868
+ const definitions = [...staticDefinitions];
869
+ const names = new Set(staticDefinitions.map((definition) => definition.name));
870
+ for (const index of state.toolIndexes) {
871
+ const results = await index.search({
872
+ query: ragText,
873
+ topK: index.topK,
874
+ threshold: index.threshold,
875
+ filter: index.filter
876
+ });
877
+ for (const result of results) {
878
+ const toolName = result.document.toolName;
879
+ if (names.has(toolName)) {
880
+ continue;
881
+ }
882
+ const tool = index.tools.find((candidate) => candidate.name === toolName);
883
+ if (tool === void 0) {
884
+ continue;
885
+ }
886
+ names.add(toolName);
887
+ definitions.push(await tool.definition(ragText));
771
888
  }
772
- };
773
- }
774
- function reasoningDeltaEvent(event) {
775
- const mapped = { type: "reasoning_delta", delta: event.delta };
776
- if (event.id !== void 0) mapped.id = event.id;
777
- if (event.contentType !== void 0) mapped.contentType = event.contentType;
778
- if (event.signature !== void 0) mapped.signature = event.signature;
779
- return mapped;
780
- }
781
- function isEmptyToolArguments(value) {
782
- if (value === void 0 || value === null) {
783
- return true;
784
- }
785
- if (typeof value === "string") {
786
- return value.trim().length === 0;
787
- }
788
- if (Array.isArray(value)) {
789
- return value.length === 0;
790
- }
791
- if (typeof value === "object") {
792
- return Object.values(value).every((item) => item === void 0);
793
889
  }
794
- return false;
890
+ return definitions;
795
891
  }
796
- function parseToolArguments(toolCallId, text) {
797
- if (text.trim().length === 0) {
798
- return {};
799
- }
800
- try {
801
- return JSON.parse(text);
802
- } catch {
803
- throw new Error(
804
- `Completion returned tool call "${toolCallId}" with malformed JSON arguments; this indicates invalid provider output or incomplete stream assembly.`
805
- );
892
+ function formatMetadata(metadata) {
893
+ if (metadata === void 0) {
894
+ return void 0;
806
895
  }
896
+ return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
807
897
  }
808
898
 
809
- // src/internal/prompt-runtime/stream-events.ts
810
- function addTurn(turn, event) {
811
- if (event.type === "text_delta") {
812
- return { type: "text_delta", turn, delta: event.delta };
813
- }
814
- if (event.type === "reasoning_delta") {
815
- const mapped = { type: "reasoning_delta", turn, delta: event.delta };
816
- if (event.id !== void 0) mapped.id = event.id;
817
- if (event.contentType !== void 0) mapped.contentType = event.contentType;
818
- if (event.signature !== void 0) mapped.signature = event.signature;
819
- return mapped;
820
- }
821
- if (event.type === "source") {
822
- return { type: "source", turn, source: event.source };
823
- }
824
- if (event.type === "provider_tool_call") {
825
- return { type: "provider_tool_call", turn, toolCall: event.toolCall };
899
+ // src/internal/agent-runtime/run-validation.ts
900
+ function assertNonnegativeSafeInteger(value, name) {
901
+ if (!Number.isSafeInteger(value) || value < 0) {
902
+ throw new TypeError(`${name} must be a nonnegative safe integer.`);
826
903
  }
827
- return { type: "tool_call", turn, toolCall: event.toolCall };
828
- }
829
- function addTurnToToolCallDelta(turn, event) {
830
- const mapped = {
831
- type: "tool_call_delta",
832
- turn,
833
- id: event.id
834
- };
835
- if (event.callId !== void 0) mapped.callId = event.callId;
836
- if (event.name !== void 0) mapped.name = event.name;
837
- if (event.argumentsDelta !== void 0) mapped.argumentsDelta = event.argumentsDelta;
838
- if (event.argumentsMode !== void 0) mapped.argumentsMode = event.argumentsMode;
839
- if (event.signature !== void 0) mapped.signature = event.signature;
840
- return mapped;
904
+ return value;
841
905
  }
842
- function isGenerationDeltaEvent(type) {
843
- return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call" || type === "source" || type === "provider_tool_call";
906
+ function assertPositiveSafeInteger(value, name) {
907
+ if (!Number.isSafeInteger(value) || value <= 0) {
908
+ throw new TypeError(`${name} must be a positive safe integer.`);
909
+ }
910
+ return value;
844
911
  }
845
912
 
846
- // src/internal/prompt-runtime/tool-execution.ts
847
- var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
848
- var ToolCallExecutor = class {
849
- constructor(agent, activeHook, approvals, runContext, concurrency, requestMiddlewares, includeToolCallDeltas, cancel) {
850
- this.agent = agent;
851
- this.activeHook = activeHook;
852
- this.approvals = approvals;
853
- this.runContext = runContext;
854
- this.concurrency = concurrency;
855
- this.requestMiddlewares = requestMiddlewares;
856
- this.includeToolCallDeltas = includeToolCallDeltas;
857
- this.cancel = cancel;
913
+ // src/internal/agent-runtime/stream-accumulator.ts
914
+ var CompletionStreamAccumulator = class {
915
+ orderedParts = [];
916
+ textParts = /* @__PURE__ */ new Map();
917
+ reasoningByKey = /* @__PURE__ */ new Map();
918
+ reasoningKeyById = /* @__PURE__ */ new Map();
919
+ toolCalls = /* @__PURE__ */ new Map();
920
+ sources = /* @__PURE__ */ new Map();
921
+ providerToolCalls = /* @__PURE__ */ new Map();
922
+ finalResponse;
923
+ messageId;
924
+ nextTextKey = 0;
925
+ nextReasoningKey = 0;
926
+ accept(event) {
927
+ if (event.type === "text_delta") {
928
+ this.appendText(event.delta);
929
+ return { type: "text_delta", delta: event.delta };
930
+ }
931
+ if (event.type === "reasoning_delta") {
932
+ const reasoning = this.reasoningStateForEvent(event);
933
+ this.appendReasoning(reasoning, event);
934
+ return reasoningDeltaEvent(event);
935
+ }
936
+ if (event.type === "tool_call_delta") {
937
+ const toolCall = this.toolCallStateForId(event.id);
938
+ if (event.callId !== void 0 && event.callId.length > 0) toolCall.callId = event.callId;
939
+ if (event.name !== void 0 && event.name.length > 0) toolCall.name = event.name;
940
+ if (event.signature !== void 0) toolCall.signature = event.signature;
941
+ if (event.argumentsDelta !== void 0) {
942
+ if (event.argumentsMode === "replace") {
943
+ toolCall.argumentsText = event.argumentsDelta;
944
+ } else {
945
+ toolCall.argumentsText += event.argumentsDelta;
946
+ }
947
+ }
948
+ return void 0;
949
+ }
950
+ if (event.type === "tool_call") {
951
+ this.upsertToolCall(event.toolCall);
952
+ return { type: "tool_call", toolCall: event.toolCall };
953
+ }
954
+ if (event.type === "source") {
955
+ this.sources.set(sourceKey(event.source), event.source);
956
+ return { type: "source", source: event.source };
957
+ }
958
+ if (event.type === "provider_tool_call") {
959
+ this.providerToolCalls.set(event.toolCall.id, event.toolCall);
960
+ return { type: "provider_tool_call", toolCall: event.toolCall };
961
+ }
962
+ if (event.type === "message_id") {
963
+ this.messageId = event.id;
964
+ return void 0;
965
+ }
966
+ if (event.type === "final") {
967
+ this.finalResponse = event.response;
968
+ return void 0;
969
+ }
970
+ return void 0;
858
971
  }
859
- agent;
860
- activeHook;
861
- approvals;
862
- runContext;
863
- concurrency;
864
- requestMiddlewares;
865
- includeToolCallDeltas;
866
- cancel;
867
- async execute(toolCalls, onResult, onStreamEvent, observation) {
868
- for (const toolCall of toolCalls) {
869
- if (toolCall.function.name.length === 0) {
870
- throw new Error(
871
- `Completion returned tool call "${toolCall.id}" with an empty function name; this indicates invalid provider output or provider mapping.`
972
+ response() {
973
+ const accumulatedResponse = this.buildAccumulatedResponse();
974
+ if (this.finalResponse !== void 0) {
975
+ if (accumulatedResponse.choice.length === 0) {
976
+ return this.withAccumulatedArtifacts(this.finalResponse, accumulatedResponse);
977
+ }
978
+ return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
979
+ }
980
+ return accumulatedResponse;
981
+ }
982
+ buildAccumulatedResponse() {
983
+ const choice = [];
984
+ for (const part of this.orderedParts) {
985
+ if (part.type === "text") {
986
+ const text = this.textParts.get(part.key) ?? "";
987
+ if (text.length > 0) {
988
+ choice.push({ type: "text", text });
989
+ }
990
+ continue;
991
+ }
992
+ if (part.type === "reasoning") {
993
+ const reasoning = this.reasoningByKey.get(part.key);
994
+ if (reasoning !== void 0) {
995
+ choice.push(reasoningContent(reasoning));
996
+ }
997
+ continue;
998
+ }
999
+ const toolCall = this.toolCalls.get(part.key);
1000
+ if (toolCall !== void 0) {
1001
+ choice.push(
1002
+ toolCallContent(toolCall, matchingFinalToolCall(toolCall, this.finalResponse?.choice))
872
1003
  );
873
1004
  }
874
1005
  }
875
- return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
876
- const args = JSON.stringify(toolCall.function.arguments ?? {});
877
- const internalCallId = globalThis.crypto.randomUUID();
878
- const hookArgs = {
879
- toolName: toolCall.function.name,
880
- internalCallId,
881
- args
882
- };
883
- if (toolCall.callId !== void 0) {
884
- hookArgs.toolCallId = toolCall.callId;
885
- }
886
- const tool = this.agent.getTool(toolCall.function.name);
887
- const toolDefinition = observation?.toolDefinitions?.find(
888
- (definition) => definition.name === toolCall.function.name
889
- );
890
- const toolMetadata = toolTraceMetadata(tool);
891
- const toolStartArgs = {
892
- turn: observation?.turn ?? 0,
893
- toolCall,
894
- toolName: toolCall.function.name,
895
- internalCallId,
896
- args
1006
+ const response = {
1007
+ choice,
1008
+ usage: Usage.empty(),
1009
+ rawResponse: void 0
1010
+ };
1011
+ if (this.messageId !== void 0) {
1012
+ response.messageId = this.messageId;
1013
+ }
1014
+ const sources = [...this.sources.values()];
1015
+ if (sources.length > 0) {
1016
+ response.sources = sources;
1017
+ }
1018
+ const providerToolCalls = [...this.providerToolCalls.values()];
1019
+ if (providerToolCalls.length > 0) {
1020
+ response.providerToolCalls = providerToolCalls;
1021
+ }
1022
+ return response;
1023
+ }
1024
+ upsertToolCall(toolCall) {
1025
+ if (!this.toolCalls.has(toolCall.id)) {
1026
+ this.orderedParts.push({ type: "tool_call", key: toolCall.id });
1027
+ }
1028
+ const partial = {
1029
+ id: toolCall.id,
1030
+ name: toolCall.function.name,
1031
+ argumentsText: JSON.stringify(toolCall.function.arguments ?? {})
1032
+ };
1033
+ if (toolCall.callId !== void 0) {
1034
+ partial.callId = toolCall.callId;
1035
+ }
1036
+ if (toolCall.signature !== void 0) {
1037
+ partial.signature = toolCall.signature;
1038
+ }
1039
+ if (toolCall.additionalParams !== void 0) {
1040
+ partial.additionalParams = toolCall.additionalParams;
1041
+ }
1042
+ this.toolCalls.set(toolCall.id, partial);
1043
+ }
1044
+ mergeFinalResponse(accumulatedResponse, finalResponse) {
1045
+ if (finalResponse.choice.length === 0) {
1046
+ const mergedResponse = {
1047
+ ...accumulatedResponse,
1048
+ usage: finalResponse.usage,
1049
+ rawResponse: finalResponse.rawResponse
897
1050
  };
898
- if (toolCall.callId !== void 0) {
899
- toolStartArgs.toolCallId = toolCall.callId;
900
- }
901
- if (toolDefinition !== void 0) {
902
- toolStartArgs.toolDefinition = toolDefinition;
1051
+ if (finalResponse.messageId !== void 0) {
1052
+ mergedResponse.messageId = finalResponse.messageId;
903
1053
  }
904
- if (toolMetadata !== void 0) {
905
- toolStartArgs.toolMetadata = toolMetadata;
1054
+ return this.withAccumulatedArtifacts(mergedResponse, accumulatedResponse);
1055
+ }
1056
+ const finalById = /* @__PURE__ */ new Map();
1057
+ const finalByCallId = /* @__PURE__ */ new Map();
1058
+ for (const content of finalResponse.choice) {
1059
+ if (content.type !== "tool_call") {
1060
+ continue;
906
1061
  }
907
- const toolObservers = await observation?.runObservers.startTool(toolStartArgs);
908
- let output;
909
- let skipped = false;
910
- let effectiveArgs = args;
911
- const callAction = await this.activeHook?.onToolCall?.({
912
- ...hookArgs,
913
- tool: toolCallControl
914
- });
915
- if (callAction?.type === "terminate") {
916
- await recordToolError(
917
- toolObservers,
918
- observation?.turn,
919
- toolCall,
920
- internalCallId,
921
- effectiveArgs,
922
- callAction.reason
923
- );
924
- throw this.cancel(callAction.reason);
1062
+ finalById.set(content.id, content);
1063
+ if (content.callId !== void 0) {
1064
+ finalByCallId.set(content.callId, content);
925
1065
  }
926
- if (callAction?.type === "skip") {
927
- output = callAction.reason;
928
- skipped = true;
929
- } else {
930
- let approvalDecision;
931
- try {
932
- effectiveArgs = await this.runToolInputMiddlewares({
933
- ...hookArgs,
934
- turn: observation?.turn ?? 0,
935
- originalArgs: args
936
- });
937
- hookArgs.args = effectiveArgs;
938
- approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(tool, hookArgs, callAction) : await this.evaluateToolApproval(tool, hookArgs) ?? { approved: true };
939
- if (!approvalDecision.approved) {
940
- output = approvalDecision.result;
941
- skipped = true;
942
- } else {
943
- output = await this.runApprovedToolCall(
944
- toolCall,
945
- hookArgs,
946
- effectiveArgs,
947
- args,
948
- toolObservers,
949
- observation,
950
- onStreamEvent,
951
- false
952
- );
953
- effectiveArgs = hookArgs.args;
954
- }
955
- } catch (error) {
956
- await recordToolError(
957
- toolObservers,
958
- observation?.turn,
959
- toolCall,
960
- internalCallId,
961
- effectiveArgs,
962
- error
963
- );
964
- throw error;
965
- }
1066
+ }
1067
+ const matchedFinalToolCalls = /* @__PURE__ */ new Set();
1068
+ const choice = accumulatedResponse.choice.map((content) => {
1069
+ if (content.type !== "tool_call") {
1070
+ return content;
966
1071
  }
967
- let result = toolOutputToText(output);
968
- let structuredResult = toolOutputToStructuredResult(output);
969
- if (this.agent.shouldApplyToolMiddleware(toolCall.function.name)) {
970
- const middlewareReplacement = await this.runToolResultMiddlewares({
971
- ...hookArgs,
972
- args: effectiveArgs,
973
- result,
974
- originalResult: result,
975
- structuredResult,
976
- originalStructuredResult: structuredResult,
977
- turn: observation?.turn ?? 0
978
- });
979
- if (middlewareReplacement !== void 0) {
980
- output = middlewareReplacement;
981
- result = toolOutputToText(middlewareReplacement);
982
- structuredResult = toolOutputToStructuredResult(middlewareReplacement);
983
- }
1072
+ const finalToolCall = finalById.get(content.id) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
1073
+ if (finalToolCall === void 0) {
1074
+ return content;
984
1075
  }
985
- const resultAction = await this.activeHook?.onToolResult?.({
986
- ...hookArgs,
987
- args: effectiveArgs,
988
- result,
989
- structuredResult,
990
- run: runControl
991
- });
992
- await toolObservers?.end({
993
- turn: observation?.turn ?? 0,
994
- toolCall,
995
- toolName: toolCall.function.name,
996
- internalCallId,
997
- args: effectiveArgs,
998
- result,
999
- structuredResult,
1000
- skipped,
1001
- toolCallId: toolCall.callId
1002
- });
1003
- if (resultAction?.type === "terminate") {
1004
- throw this.cancel(resultAction.reason);
1076
+ matchedFinalToolCalls.add(finalToolCall);
1077
+ return mergeFinalToolCall(content, finalToolCall);
1078
+ });
1079
+ for (const content of finalResponse.choice) {
1080
+ if (content.type !== "tool_call") {
1081
+ continue;
1005
1082
  }
1006
- const resultPayload = {
1007
- type: "tool_result",
1008
- toolName: toolCall.function.name,
1009
- internalCallId,
1010
- args: effectiveArgs,
1011
- result,
1012
- structuredResult
1013
- };
1014
- if (toolCall.callId !== void 0) {
1015
- resultPayload.toolCallId = toolCall.callId;
1083
+ if (!matchedFinalToolCalls.has(content)) {
1084
+ choice.push(content);
1016
1085
  }
1017
- onResult?.(resultPayload);
1018
- return ToolContent.toolResult(toolCall.id, output, {
1019
- callId: toolCall.callId,
1020
- toolName: toolCall.function.name
1021
- });
1022
- });
1086
+ }
1087
+ return this.withAccumulatedArtifacts({ ...finalResponse, choice }, accumulatedResponse);
1023
1088
  }
1024
- async runApprovedToolCall(toolCall, hookArgs, effectiveArgs, originalArgs, toolObservers, observation, onStreamEvent, applyInputMiddleware = true) {
1025
- const middlewareArgs = applyInputMiddleware ? await this.runToolInputMiddlewares({
1026
- ...hookArgs,
1027
- args: effectiveArgs,
1028
- turn: observation?.turn ?? 0,
1029
- originalArgs
1030
- }) : effectiveArgs;
1031
- hookArgs.args = middlewareArgs;
1032
- try {
1033
- const toolContext = {
1034
- emitStreamEvent: async (event) => {
1035
- const streamEventArgs = {
1036
- turn: observation?.turn ?? 0,
1037
- toolCall,
1038
- toolName: toolCall.function.name,
1039
- internalCallId: hookArgs.internalCallId,
1040
- args: middlewareArgs,
1041
- event
1042
- };
1043
- if (toolCall.callId !== void 0) {
1044
- streamEventArgs.toolCallId = toolCall.callId;
1045
- }
1046
- await toolObservers?.streamEvent(streamEventArgs);
1047
- const payload = agentToolEventPayload(toolCall, hookArgs.internalCallId, event);
1048
- if (payload !== void 0) {
1049
- onStreamEvent?.(payload);
1050
- }
1051
- }
1052
- };
1053
- toolContext.includeToolCallDeltas = this.includeToolCallDeltas;
1054
- return await this.agent.callTool(toolCall.function.name, middlewareArgs, toolContext);
1055
- } catch (error) {
1056
- const errorAction = await this.activeHook?.onToolError?.({
1057
- ...hookArgs,
1058
- args: middlewareArgs,
1059
- error,
1060
- run: runControl
1061
- });
1062
- await toolObservers?.error(
1063
- toolErrorArgs(
1064
- observation?.turn ?? 0,
1065
- toolCall,
1066
- hookArgs.internalCallId,
1067
- middlewareArgs,
1068
- error
1069
- )
1070
- );
1071
- if (errorAction?.type === "terminate") {
1072
- throw this.cancel(errorAction.reason);
1073
- }
1074
- return error instanceof Error ? error.toString() : String(error);
1089
+ appendText(delta) {
1090
+ const lastPart = this.orderedParts.at(-1);
1091
+ const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
1092
+ if (lastPart?.type !== "text") {
1093
+ this.orderedParts.push({ type: "text", key });
1075
1094
  }
1095
+ this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
1076
1096
  }
1077
- async runToolResultMiddlewares(args) {
1078
- let result = args.result;
1079
- let structuredResult = args.structuredResult;
1080
- let replaced = false;
1081
- for (const middleware of this.activeMiddlewares()) {
1082
- const outputReplacement = await middleware.onToolOutput?.({
1083
- ...args,
1084
- result,
1085
- structuredResult
1086
- });
1087
- if (outputReplacement !== void 0) {
1088
- const normalized = normalizeToolOutputMiddlewareResult(outputReplacement);
1089
- if (normalized.result !== void 0) {
1090
- result = normalized.result;
1091
- structuredResult = void 0;
1092
- }
1093
- if (normalized.structuredResult !== void 0) {
1094
- structuredResult = normalized.structuredResult;
1095
- result = toolResultContentToText(normalized.structuredResult);
1097
+ reasoningStateForEvent(event) {
1098
+ if (event.id !== void 0) {
1099
+ const existingKey = this.reasoningKeyById.get(event.id);
1100
+ if (existingKey !== void 0) {
1101
+ const existing = this.reasoningByKey.get(existingKey);
1102
+ if (existing !== void 0) {
1103
+ return existing;
1096
1104
  }
1097
- replaced = true;
1098
1105
  }
1106
+ const key2 = this.createReasoningKey();
1107
+ const reasoning2 = { id: event.id, text: "" };
1108
+ this.reasoningKeyById.set(event.id, key2);
1109
+ this.reasoningByKey.set(key2, reasoning2);
1110
+ this.orderedParts.push({ type: "reasoning", key: key2 });
1111
+ return reasoning2;
1099
1112
  }
1100
- return replaced ? structuredResult ?? result : void 0;
1101
- }
1102
- async runToolInputMiddlewares(args) {
1103
- let current = args.args;
1104
- for (const middleware of this.activeMiddlewares()) {
1105
- const replacement = await middleware.onToolInput?.({
1106
- ...args,
1107
- args: current
1108
- });
1109
- if (replacement?.args !== void 0) {
1110
- current = typeof replacement.args === "string" ? replacement.args : JSON.stringify(replacement.args);
1113
+ const lastPart = this.orderedParts.at(-1);
1114
+ if (lastPart?.type === "reasoning") {
1115
+ const lastReasoning = this.reasoningByKey.get(lastPart.key);
1116
+ if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
1117
+ return lastReasoning;
1111
1118
  }
1112
1119
  }
1113
- return current;
1114
- }
1115
- activeMiddlewares() {
1116
- return [...this.agent.middlewares, ...this.requestMiddlewares];
1120
+ const key = this.createReasoningKey();
1121
+ const reasoning = { text: "" };
1122
+ this.reasoningByKey.set(key, reasoning);
1123
+ this.orderedParts.push({ type: "reasoning", key });
1124
+ return reasoning;
1117
1125
  }
1118
- async evaluateToolApproval(tool, hookArgs) {
1119
- if (tool?.approval === void 0) {
1120
- return void 0;
1121
- }
1122
- const policy = tool.approval;
1123
- const context = approvalContext(tool, hookArgs, this.agent, this.runContext);
1124
- const required = await policy.when(context);
1125
- if (!required) {
1126
- return { approved: true };
1127
- }
1128
- const reason = await resolveApprovalText(policy.reason, context);
1129
- const rejectMessage = await resolveApprovalText(policy.rejectMessage, context);
1130
- const options = {};
1131
- if (reason !== void 0) {
1132
- options.reason = reason;
1126
+ toolCallStateForId(id) {
1127
+ const existing = this.toolCalls.get(id);
1128
+ if (existing !== void 0) {
1129
+ return existing;
1133
1130
  }
1134
- if (rejectMessage !== void 0) {
1135
- options.rejectMessage = rejectMessage;
1131
+ const toolCall = {
1132
+ id,
1133
+ name: "",
1134
+ argumentsText: ""
1135
+ };
1136
+ this.toolCalls.set(id, toolCall);
1137
+ this.orderedParts.push({ type: "tool_call", key: id });
1138
+ return toolCall;
1139
+ }
1140
+ withMessageIdFallback(response, accumulatedResponse) {
1141
+ if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
1142
+ return response;
1136
1143
  }
1137
- return this.requestApproval(tool, hookArgs, options);
1144
+ return { ...response, messageId: accumulatedResponse.messageId };
1138
1145
  }
1139
- async requestApproval(tool, hookArgs, options) {
1140
- const request = approvalContext(
1141
- tool,
1142
- hookArgs,
1143
- this.agent,
1144
- this.runContext
1146
+ withAccumulatedArtifacts(response, accumulatedResponse) {
1147
+ const withMessageId = this.withMessageIdFallback(response, accumulatedResponse);
1148
+ const sources = mergeSources(accumulatedResponse.sources, response.sources);
1149
+ const providerToolCalls = mergeProviderToolCalls(
1150
+ accumulatedResponse.providerToolCalls,
1151
+ response.providerToolCalls
1145
1152
  );
1146
- if (options.reason !== void 0) {
1147
- request.reason = options.reason;
1153
+ return {
1154
+ ...withMessageId,
1155
+ ...sources.length === 0 ? {} : { sources },
1156
+ ...providerToolCalls.length === 0 ? {} : { providerToolCalls }
1157
+ };
1158
+ }
1159
+ createTextKey() {
1160
+ this.nextTextKey += 1;
1161
+ return `text_${this.nextTextKey.toString()}`;
1162
+ }
1163
+ createReasoningKey() {
1164
+ this.nextReasoningKey += 1;
1165
+ return `reasoning_${this.nextReasoningKey.toString()}`;
1166
+ }
1167
+ appendReasoning(reasoning, event) {
1168
+ const contentType = event.contentType ?? "text";
1169
+ if (contentType === "text" || contentType === "summary") {
1170
+ reasoning.text += event.delta;
1148
1171
  }
1149
- if (options.rejectMessage !== void 0) {
1150
- request.rejectMessage = options.rejectMessage;
1172
+ if (event.contentType === void 0 && event.signature === void 0) {
1173
+ return;
1174
+ }
1175
+ reasoning.content ??= [];
1176
+ const last = reasoning.content.at(-1);
1177
+ if (contentType === "text") {
1178
+ if (last?.type === "text") {
1179
+ last.text += event.delta;
1180
+ if (event.signature !== void 0) {
1181
+ last.signature = event.signature;
1182
+ }
1183
+ } else {
1184
+ reasoning.content.push(
1185
+ event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
1186
+ );
1187
+ }
1188
+ return;
1151
1189
  }
1152
- if (this.approvals === void 0) {
1153
- throw new ToolApprovalRequiredError(request);
1190
+ if (contentType === "summary") {
1191
+ if (last?.type === "summary") {
1192
+ last.text += event.delta;
1193
+ } else {
1194
+ reasoning.content.push({ type: "summary", text: event.delta });
1195
+ }
1196
+ return;
1154
1197
  }
1155
- const decision = normalizeApprovalDecision(await this.approvals.handler(request));
1156
- if (decision.approved) {
1157
- return { approved: true };
1198
+ if (contentType === "encrypted") {
1199
+ reasoning.content.push({ type: "encrypted", data: event.delta });
1200
+ return;
1158
1201
  }
1159
- return {
1160
- approved: false,
1161
- result: decision.rejectMessage ?? decision.reason ?? request.rejectMessage ?? "Tool approval was rejected."
1162
- };
1202
+ reasoning.content.push({ type: "redacted", data: event.delta });
1163
1203
  }
1164
1204
  };
1165
- function approvalContext(tool, hookArgs, agent, run) {
1166
- const rawParsedArgs = parseToolArgs(hookArgs.args);
1167
- const parsedArgs = tool?.parseApprovalArgs?.(rawParsedArgs) ?? rawParsedArgs;
1168
- const approvalRun = {
1169
- agentId: agent.id,
1170
- runId: run.runId
1171
- };
1172
- if (run.sessionId !== void 0) {
1173
- approvalRun.sessionId = run.sessionId;
1174
- }
1175
- if (run.metadata !== void 0) {
1176
- approvalRun.metadata = run.metadata;
1177
- }
1178
- const context = {
1179
- toolName: hookArgs.toolName,
1180
- args: parsedArgs,
1181
- rawArgs: hookArgs.args,
1182
- internalCallId: hookArgs.internalCallId,
1183
- run: approvalRun
1184
- };
1185
- if (hookArgs.toolCallId !== void 0) {
1186
- context.toolCallId = hookArgs.toolCallId;
1187
- }
1188
- return context;
1205
+ function sourceKey(source) {
1206
+ return `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
1189
1207
  }
1190
- function normalizeApprovalDecision(decision) {
1191
- if (typeof decision === "boolean") {
1192
- return { approved: decision };
1208
+ function mergeSources(accumulated, final) {
1209
+ const sources = /* @__PURE__ */ new Map();
1210
+ for (const source of [...accumulated ?? [], ...final ?? []]) {
1211
+ sources.set(sourceKey(source), source);
1193
1212
  }
1194
- return decision;
1195
- }
1196
- async function resolveApprovalText(value, context) {
1197
- return typeof value === "function" ? value(context) : value;
1213
+ return [...sources.values()];
1198
1214
  }
1199
- function normalizeToolOutputMiddlewareResult(result) {
1200
- if (typeof result === "string") {
1201
- return { result };
1215
+ function mergeProviderToolCalls(accumulated, final) {
1216
+ const toolCalls = /* @__PURE__ */ new Map();
1217
+ for (const toolCall of [...accumulated ?? [], ...final ?? []]) {
1218
+ toolCalls.set(toolCall.id, toolCall);
1202
1219
  }
1203
- return result ?? {};
1220
+ return [...toolCalls.values()];
1204
1221
  }
1205
- function toolTraceMetadata(tool) {
1206
- if (tool === void 0) {
1207
- return void 0;
1208
- }
1209
- const metadata = tool[MCP_TOOL_METADATA_KEY];
1210
- const mcpMetadata = typeof metadata === "object" && metadata !== null ? metadata : void 0;
1211
- const result = {
1212
- approvalRequired: tool.approval !== void 0
1213
- };
1214
- if (typeof mcpMetadata?.serverName === "string" && mcpMetadata.serverName.length > 0) {
1215
- result.mcpServerName = mcpMetadata.serverName;
1216
- }
1217
- return result;
1222
+ function reasoningContent(reasoning) {
1223
+ const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
1224
+ return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
1218
1225
  }
1219
- function toolErrorArgs(turn, toolCall, internalCallId, args, error) {
1220
- const observerArgs = {
1221
- turn,
1222
- toolCall,
1223
- toolName: toolCall.function.name,
1224
- internalCallId,
1225
- args,
1226
- error
1226
+ function toolCallContent(toolCall, finalToolCall) {
1227
+ const argumentsValue = finalToolCall !== void 0 && !isEmptyToolArguments(finalToolCall.function.arguments) ? finalToolCall.function.arguments : parseToolArguments(toolCall.id, toolCall.argumentsText);
1228
+ const content = {
1229
+ type: "tool_call",
1230
+ id: toolCall.id,
1231
+ function: {
1232
+ name: toolCall.name,
1233
+ arguments: argumentsValue
1234
+ }
1227
1235
  };
1228
1236
  if (toolCall.callId !== void 0) {
1229
- observerArgs.toolCallId = toolCall.callId;
1237
+ content.callId = toolCall.callId;
1230
1238
  }
1231
- return observerArgs;
1239
+ if (toolCall.signature !== void 0) {
1240
+ content.signature = toolCall.signature;
1241
+ }
1242
+ if (toolCall.additionalParams !== void 0) {
1243
+ content.additionalParams = toolCall.additionalParams;
1244
+ }
1245
+ return content;
1232
1246
  }
1233
- async function recordToolError(toolObservers, turn, toolCall, internalCallId, args, error) {
1234
- await toolObservers?.error({
1235
- turn: turn ?? 0,
1236
- toolCall,
1237
- toolName: toolCall.function.name,
1238
- internalCallId,
1239
- args,
1240
- error,
1241
- toolCallId: toolCall.callId
1242
- });
1247
+ function matchingFinalToolCall(accumulated, finalChoice) {
1248
+ const byId = finalChoice?.find(
1249
+ (content) => content.type === "tool_call" && content.id === accumulated.id
1250
+ );
1251
+ if (byId !== void 0 || accumulated.callId === void 0) {
1252
+ return byId;
1253
+ }
1254
+ return finalChoice?.find(
1255
+ (content) => content.type === "tool_call" && content.callId === accumulated.callId
1256
+ );
1243
1257
  }
1244
- function toolOutputToText(output) {
1245
- return typeof output === "string" ? output : toolResultContentToText(output);
1258
+ function mergeFinalToolCall(accumulated, finalToolCall) {
1259
+ const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
1260
+ return {
1261
+ ...accumulated,
1262
+ ...finalToolCall,
1263
+ function: {
1264
+ ...accumulated.function,
1265
+ ...finalToolCall.function,
1266
+ arguments: argumentsValue
1267
+ }
1268
+ };
1246
1269
  }
1247
- function toolOutputToStructuredResult(output) {
1248
- return typeof output === "string" ? void 0 : output;
1270
+ function reasoningDeltaEvent(event) {
1271
+ const mapped = { type: "reasoning_delta", delta: event.delta };
1272
+ if (event.id !== void 0) mapped.id = event.id;
1273
+ if (event.contentType !== void 0) mapped.contentType = event.contentType;
1274
+ if (event.signature !== void 0) mapped.signature = event.signature;
1275
+ return mapped;
1249
1276
  }
1250
- function agentToolEventPayload(toolCall, internalCallId, event) {
1251
- if (typeof event.agentId !== "string" || event.agentId.length === 0) {
1252
- return void 0;
1277
+ function isEmptyToolArguments(value) {
1278
+ if (value === void 0 || value === null) {
1279
+ return true;
1253
1280
  }
1254
- const payload = {
1255
- type: "agent_tool_event",
1256
- toolName: toolCall.function.name,
1257
- internalCallId,
1258
- agentId: event.agentId,
1259
- event: event.event
1260
- };
1261
- if (toolCall.callId !== void 0) {
1262
- payload.toolCallId = toolCall.callId;
1281
+ if (typeof value === "string") {
1282
+ return value.trim().length === 0;
1263
1283
  }
1264
- if (event.agentName !== void 0) {
1265
- payload.agentName = event.agentName;
1284
+ if (Array.isArray(value)) {
1285
+ return value.length === 0;
1266
1286
  }
1267
- return payload;
1287
+ if (typeof value === "object") {
1288
+ return Object.values(value).every((item) => item === void 0);
1289
+ }
1290
+ return false;
1268
1291
  }
1269
-
1270
- // src/internal/rag-text.ts
1271
- function extractRagText(message) {
1272
- if (message.role === "user") {
1273
- return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
1292
+ function parseToolArguments(toolCallId, text) {
1293
+ if (text.trim().length === 0) {
1294
+ return {};
1274
1295
  }
1275
- if (message.role === "tool") {
1276
- return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
1296
+ try {
1297
+ return JSON.parse(text);
1298
+ } catch {
1299
+ throw new Error(
1300
+ `Completion returned tool call "${toolCallId}" with malformed JSON arguments; this indicates invalid provider output or incomplete stream assembly.`
1301
+ );
1277
1302
  }
1278
- return void 0;
1279
1303
  }
1280
1304
 
1281
- // src/observability/group.ts
1282
- async function startAgentRunObservers(registrations, args, failOnObserverError) {
1283
- const runObservers = [];
1284
- for (const registration of registrations) {
1285
- try {
1286
- const runObserver = await registration.observer.startRun(args);
1287
- if (runObserver !== void 0) {
1288
- runObservers.push(runObserver);
1289
- }
1290
- } catch (error) {
1291
- if (failOnObserverError || registration.failOnObserverError === true) {
1292
- throw error;
1293
- }
1294
- }
1295
- }
1296
- return new ActiveAgentRunObservers(runObservers, failOnObserverError);
1297
- }
1298
- var ActiveAgentRunObservers = class {
1299
- constructor(runObservers, failOnObserverError) {
1300
- this.runObservers = runObservers;
1301
- this.failOnObserverError = failOnObserverError;
1302
- this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
1303
- }
1304
- runObservers;
1305
- failOnObserverError;
1306
- trace;
1307
- async startGeneration(args) {
1308
- const generationObservers = [];
1309
- for (const runObserver of this.runObservers) {
1310
- if (runObserver.startGeneration === void 0) {
1311
- continue;
1312
- }
1313
- try {
1314
- const generationObserver = await runObserver.startGeneration(args);
1315
- if (generationObserver !== void 0) {
1316
- generationObservers.push(generationObserver);
1317
- }
1318
- } catch (error) {
1319
- this.handleError(error);
1320
- }
1321
- }
1322
- return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
1323
- }
1324
- async startTool(args) {
1325
- const toolObservers = [];
1326
- for (const runObserver of this.runObservers) {
1327
- if (runObserver.startTool === void 0) {
1328
- continue;
1329
- }
1330
- try {
1331
- const toolObserver = await runObserver.startTool(args);
1332
- if (toolObserver !== void 0) {
1333
- toolObservers.push(toolObserver);
1334
- }
1335
- } catch (error) {
1336
- this.handleError(error);
1337
- }
1338
- }
1339
- return new ActiveToolObservers(toolObservers, this.failOnObserverError);
1340
- }
1341
- async end(args) {
1342
- for (const runObserver of this.runObservers) {
1343
- try {
1344
- await runObserver.end(args);
1345
- } catch (error) {
1346
- this.handleError(error);
1347
- }
1348
- }
1305
+ // src/internal/agent-runtime/stream-events.ts
1306
+ function addTurn(turn, event) {
1307
+ if (event.type === "text_delta") {
1308
+ return { type: "text_delta", turn, delta: event.delta };
1349
1309
  }
1350
- async error(args) {
1351
- for (const runObserver of this.runObservers) {
1352
- if (runObserver.error === void 0) {
1353
- continue;
1354
- }
1355
- try {
1356
- await runObserver.error(args);
1357
- } catch (error) {
1358
- this.handleError(error);
1359
- }
1360
- }
1310
+ if (event.type === "reasoning_delta") {
1311
+ const mapped = { type: "reasoning_delta", turn, delta: event.delta };
1312
+ if (event.id !== void 0) mapped.id = event.id;
1313
+ if (event.contentType !== void 0) mapped.contentType = event.contentType;
1314
+ if (event.signature !== void 0) mapped.signature = event.signature;
1315
+ return mapped;
1361
1316
  }
1362
- async event(args) {
1363
- for (const runObserver of this.runObservers) {
1364
- if (runObserver.event === void 0) {
1365
- continue;
1366
- }
1367
- try {
1368
- await runObserver.event(args);
1369
- } catch (error) {
1370
- this.handleError(error);
1371
- }
1372
- }
1317
+ if (event.type === "source") {
1318
+ return { type: "source", turn, source: event.source };
1373
1319
  }
1374
- handleError(error) {
1375
- if (this.failOnObserverError) {
1376
- throw error;
1377
- }
1320
+ if (event.type === "provider_tool_call") {
1321
+ return { type: "provider_tool_call", turn, toolCall: event.toolCall };
1378
1322
  }
1379
- };
1380
- var ActiveGenerationObservers = class {
1381
- constructor(generationObservers, failOnObserverError) {
1382
- this.generationObservers = generationObservers;
1383
- this.failOnObserverError = failOnObserverError;
1323
+ return { type: "tool_call", turn, toolCall: event.toolCall };
1324
+ }
1325
+ function addTurnToToolCallDelta(turn, event) {
1326
+ const mapped = {
1327
+ type: "tool_call_delta",
1328
+ turn,
1329
+ id: event.id
1330
+ };
1331
+ if (event.callId !== void 0) mapped.callId = event.callId;
1332
+ if (event.name !== void 0) mapped.name = event.name;
1333
+ if (event.argumentsDelta !== void 0) mapped.argumentsDelta = event.argumentsDelta;
1334
+ if (event.argumentsMode !== void 0) mapped.argumentsMode = event.argumentsMode;
1335
+ if (event.signature !== void 0) mapped.signature = event.signature;
1336
+ return mapped;
1337
+ }
1338
+ function isGenerationDeltaEvent(type) {
1339
+ return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call" || type === "source" || type === "provider_tool_call";
1340
+ }
1341
+
1342
+ // src/internal/agent-runtime/tool-execution.ts
1343
+ var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
1344
+ var ToolCallExecutor = class {
1345
+ constructor(agent, activeHook, approvalHandler, lifecycle, runContext, concurrency, requestMiddlewares, includeToolCallDeltas, cancel) {
1346
+ this.agent = agent;
1347
+ this.activeHook = activeHook;
1348
+ this.approvalHandler = approvalHandler;
1349
+ this.lifecycle = lifecycle;
1350
+ this.runContext = runContext;
1351
+ this.concurrency = concurrency;
1352
+ this.requestMiddlewares = requestMiddlewares;
1353
+ this.includeToolCallDeltas = includeToolCallDeltas;
1354
+ this.cancel = cancel;
1384
1355
  }
1385
- generationObservers;
1386
- failOnObserverError;
1387
- async end(args) {
1388
- for (const observer of this.generationObservers) {
1389
- try {
1390
- await observer.end(args);
1391
- } catch (error) {
1392
- this.handleError(error);
1356
+ agent;
1357
+ activeHook;
1358
+ approvalHandler;
1359
+ lifecycle;
1360
+ runContext;
1361
+ concurrency;
1362
+ requestMiddlewares;
1363
+ includeToolCallDeltas;
1364
+ cancel;
1365
+ async execute(toolCalls, onResult, onStreamEvent, observation) {
1366
+ for (const toolCall of toolCalls) {
1367
+ if (toolCall.function.name.length === 0) {
1368
+ throw new Error(
1369
+ `Completion returned tool call "${toolCall.id}" with an empty function name; this indicates invalid provider output or provider mapping.`
1370
+ );
1393
1371
  }
1394
1372
  }
1395
- }
1396
- async error(args) {
1397
- for (const observer of this.generationObservers) {
1398
- if (observer.error === void 0) {
1399
- continue;
1373
+ return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
1374
+ const args = JSON.stringify(toolCall.function.arguments ?? {});
1375
+ const internalCallId = globalThis.crypto.randomUUID();
1376
+ const hookArgs = {
1377
+ toolName: toolCall.function.name,
1378
+ internalCallId,
1379
+ args
1380
+ };
1381
+ if (toolCall.callId !== void 0) {
1382
+ hookArgs.toolCallId = toolCall.callId;
1400
1383
  }
1384
+ const tool = this.agent.getTool(toolCall.function.name);
1385
+ const toolDefinition = observation?.toolDefinitions?.find(
1386
+ (definition) => definition.name === toolCall.function.name
1387
+ );
1388
+ const toolMetadata = toolTraceMetadata(tool);
1389
+ const toolStartArgs = {
1390
+ turn: observation?.turn ?? 0,
1391
+ toolCall,
1392
+ toolName: toolCall.function.name,
1393
+ internalCallId,
1394
+ args,
1395
+ ...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
1396
+ ...toolDefinition === void 0 ? {} : { toolDefinition },
1397
+ ...toolMetadata === void 0 ? {} : { toolMetadata }
1398
+ };
1399
+ const toolObservers = await observation?.runObservers.startTool(toolStartArgs);
1400
+ const toolObservation = new ToolObserverScope(toolObservers);
1401
+ let output;
1402
+ let skipped = false;
1403
+ let toolExecutionFailed = false;
1404
+ let effectiveArgs = args;
1401
1405
  try {
1402
- await observer.error(args);
1406
+ const callAction = await this.activeHook?.onToolCall?.({
1407
+ ...hookArgs,
1408
+ tool: toolCallControl
1409
+ });
1410
+ if (callAction?.type === "terminate") {
1411
+ throw this.cancel(callAction.reason);
1412
+ }
1413
+ if (callAction?.type === "skip") {
1414
+ output = callAction.reason;
1415
+ skipped = true;
1416
+ } else {
1417
+ try {
1418
+ effectiveArgs = await this.runToolInputMiddlewares({
1419
+ ...hookArgs,
1420
+ turn: observation?.turn ?? 0,
1421
+ originalArgs: args
1422
+ });
1423
+ hookArgs.args = effectiveArgs;
1424
+ let prepared;
1425
+ try {
1426
+ prepared = this.prepareToolCall(tool, toolCall.function.name, effectiveArgs);
1427
+ } catch (error) {
1428
+ const outcome = await this.handleToolError(
1429
+ toolCall,
1430
+ hookArgs,
1431
+ effectiveArgs,
1432
+ error,
1433
+ toolObservation,
1434
+ observation
1435
+ );
1436
+ output = outcome.output;
1437
+ toolExecutionFailed = outcome.failed;
1438
+ }
1439
+ if (prepared !== void 0) {
1440
+ const approvalContext = createApprovalContext(
1441
+ prepared.input,
1442
+ hookArgs,
1443
+ this.agent,
1444
+ this.runContext
1445
+ );
1446
+ const approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(approvalContext, callAction, observation) : await this.evaluateToolApproval(tool, approvalContext, observation) ?? {
1447
+ approved: true
1448
+ };
1449
+ if (!approvalDecision.approved) {
1450
+ output = approvalDecision.result;
1451
+ skipped = true;
1452
+ } else {
1453
+ const step = observation?.turn ?? 0;
1454
+ const lifecycleEvent = {
1455
+ runId: this.runContext.runId,
1456
+ step,
1457
+ toolName: toolCall.function.name,
1458
+ input: lifecycleSnapshot(prepared.input),
1459
+ ...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId }
1460
+ };
1461
+ await this.lifecycle?.onToolStart?.(lifecycleEvent);
1462
+ const startedAt = Date.now();
1463
+ const outcome = await this.runApprovedToolCall(
1464
+ prepared,
1465
+ toolCall,
1466
+ hookArgs,
1467
+ effectiveArgs,
1468
+ toolObservation,
1469
+ observation,
1470
+ onStreamEvent
1471
+ );
1472
+ output = outcome.output;
1473
+ toolExecutionFailed = outcome.failed;
1474
+ const durationMs = Date.now() - startedAt;
1475
+ await this.lifecycle?.onToolFinish?.(
1476
+ outcome.failed ? {
1477
+ ...lifecycleEvent,
1478
+ durationMs,
1479
+ success: false,
1480
+ error: lifecycleSnapshot(outcome.error)
1481
+ } : {
1482
+ ...lifecycleEvent,
1483
+ durationMs,
1484
+ success: true,
1485
+ output: lifecycleSnapshot(output)
1486
+ }
1487
+ );
1488
+ }
1489
+ }
1490
+ } catch (error) {
1491
+ await toolObservation.error(
1492
+ toolErrorArgs(observation?.turn ?? 0, toolCall, internalCallId, effectiveArgs, error)
1493
+ );
1494
+ throw error;
1495
+ }
1496
+ }
1497
+ if (output === void 0) {
1498
+ throw new Error(`Tool "${toolCall.function.name}" did not produce an execution result.`);
1499
+ }
1500
+ let result = toolOutputToText(output);
1501
+ let structuredResult = toolOutputToStructuredResult(output);
1502
+ if (!isSkillTool(tool)) {
1503
+ const middlewareReplacement = await this.runToolResultMiddlewares({
1504
+ ...hookArgs,
1505
+ args: effectiveArgs,
1506
+ result,
1507
+ originalResult: result,
1508
+ structuredResult,
1509
+ originalStructuredResult: structuredResult,
1510
+ turn: observation?.turn ?? 0
1511
+ });
1512
+ if (middlewareReplacement !== void 0) {
1513
+ output = middlewareReplacement;
1514
+ result = toolOutputToText(middlewareReplacement);
1515
+ structuredResult = toolOutputToStructuredResult(middlewareReplacement);
1516
+ }
1517
+ }
1518
+ const resultAction = await this.activeHook?.onToolResult?.({
1519
+ ...hookArgs,
1520
+ args: effectiveArgs,
1521
+ result,
1522
+ structuredResult,
1523
+ run: runControl
1524
+ });
1525
+ if (!toolExecutionFailed) {
1526
+ await toolObservation.end({
1527
+ turn: observation?.turn ?? 0,
1528
+ toolCall,
1529
+ toolName: toolCall.function.name,
1530
+ internalCallId,
1531
+ args: effectiveArgs,
1532
+ result,
1533
+ structuredResult,
1534
+ skipped,
1535
+ toolCallId: toolCall.callId
1536
+ });
1537
+ }
1538
+ if (resultAction?.type === "terminate") {
1539
+ throw this.cancel(resultAction.reason);
1540
+ }
1541
+ const resultPayload = {
1542
+ type: "tool_result",
1543
+ toolName: toolCall.function.name,
1544
+ internalCallId,
1545
+ args: effectiveArgs,
1546
+ result,
1547
+ structuredResult
1548
+ };
1549
+ if (toolCall.callId !== void 0) {
1550
+ resultPayload.toolCallId = toolCall.callId;
1551
+ }
1552
+ onResult?.(resultPayload);
1553
+ return ToolContent.toolResult(toolCall.id, output, {
1554
+ callId: toolCall.callId,
1555
+ toolName: toolCall.function.name
1556
+ });
1403
1557
  } catch (error) {
1404
- this.handleError(error);
1558
+ await toolObservation.error(
1559
+ toolErrorArgs(observation?.turn ?? 0, toolCall, internalCallId, effectiveArgs, error)
1560
+ );
1561
+ throw error;
1405
1562
  }
1406
- }
1563
+ });
1407
1564
  }
1408
- async update(args) {
1409
- for (const observer of this.generationObservers) {
1410
- if (observer.update === void 0) {
1411
- continue;
1412
- }
1413
- try {
1414
- await observer.update(args);
1415
- } catch (error) {
1416
- this.handleError(error);
1417
- }
1565
+ async runApprovedToolCall(prepared, toolCall, hookArgs, effectiveArgs, toolObservation, observation, onStreamEvent) {
1566
+ try {
1567
+ const toolContext = {
1568
+ emitStreamEvent: async (event) => {
1569
+ const streamEventArgs = {
1570
+ turn: observation?.turn ?? 0,
1571
+ toolCall,
1572
+ toolName: toolCall.function.name,
1573
+ internalCallId: hookArgs.internalCallId,
1574
+ args: effectiveArgs,
1575
+ event,
1576
+ ...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId }
1577
+ };
1578
+ await toolObservation.streamEvent(streamEventArgs);
1579
+ const payload = agentToolEventPayload(toolCall, hookArgs.internalCallId, event);
1580
+ if (payload !== void 0) {
1581
+ onStreamEvent?.(payload);
1582
+ }
1583
+ }
1584
+ };
1585
+ toolContext.includeToolCallDeltas = this.includeToolCallDeltas;
1586
+ return {
1587
+ output: await prepared.call(toolContext),
1588
+ failed: false
1589
+ };
1590
+ } catch (error) {
1591
+ return this.handleToolError(
1592
+ toolCall,
1593
+ hookArgs,
1594
+ effectiveArgs,
1595
+ error,
1596
+ toolObservation,
1597
+ observation
1598
+ );
1418
1599
  }
1419
1600
  }
1420
- handleError(error) {
1421
- if (this.failOnObserverError) {
1422
- throw error;
1601
+ prepareToolCall(tool, toolName, args) {
1602
+ if (tool !== void 0) {
1603
+ return prepareToolCall(tool, args);
1423
1604
  }
1605
+ const input = parseToolArgs(args);
1606
+ return {
1607
+ input,
1608
+ call: (context) => this.agent.callTool(toolName, args, context)
1609
+ };
1424
1610
  }
1425
- };
1426
- var ActiveToolObservers = class {
1427
- constructor(toolObservers, failOnObserverError) {
1428
- this.toolObservers = toolObservers;
1429
- this.failOnObserverError = failOnObserverError;
1430
- }
1431
- toolObservers;
1432
- failOnObserverError;
1433
- async streamEvent(args) {
1434
- for (const observer of this.toolObservers) {
1435
- if (observer.streamEvent === void 0) {
1436
- continue;
1437
- }
1438
- try {
1439
- await observer.streamEvent(args);
1440
- } catch (error) {
1441
- this.handleError(error);
1442
- }
1611
+ async handleToolError(toolCall, hookArgs, args, error, toolObservation, observation) {
1612
+ const errorAction = await this.activeHook?.onToolError?.({
1613
+ ...hookArgs,
1614
+ args,
1615
+ error,
1616
+ run: runControl
1617
+ });
1618
+ await toolObservation.error(
1619
+ toolErrorArgs(observation?.turn ?? 0, toolCall, hookArgs.internalCallId, args, error)
1620
+ );
1621
+ if (errorAction?.type === "terminate") {
1622
+ throw this.cancel(errorAction.reason);
1443
1623
  }
1624
+ return {
1625
+ output: error instanceof Error ? error.toString() : String(error),
1626
+ failed: true,
1627
+ error
1628
+ };
1444
1629
  }
1445
- async end(args) {
1446
- for (const observer of this.toolObservers) {
1447
- try {
1448
- await observer.end(args);
1449
- } catch (error) {
1450
- this.handleError(error);
1630
+ async runToolResultMiddlewares(args) {
1631
+ let result = args.result;
1632
+ let structuredResult = args.structuredResult;
1633
+ let replaced = false;
1634
+ for (const middleware of this.activeMiddlewares()) {
1635
+ const outputReplacement = await middleware.onToolOutput?.({
1636
+ ...args,
1637
+ result,
1638
+ structuredResult
1639
+ });
1640
+ if (outputReplacement !== void 0) {
1641
+ const normalized = normalizeToolOutputMiddlewareResult(outputReplacement);
1642
+ if (normalized.result !== void 0) {
1643
+ result = normalized.result;
1644
+ structuredResult = void 0;
1645
+ }
1646
+ if (normalized.structuredResult !== void 0) {
1647
+ structuredResult = normalized.structuredResult;
1648
+ result = toolResultContentToText(normalized.structuredResult);
1649
+ }
1650
+ replaced = true;
1451
1651
  }
1452
1652
  }
1653
+ return replaced ? structuredResult ?? result : void 0;
1453
1654
  }
1454
- async error(args) {
1455
- for (const observer of this.toolObservers) {
1456
- if (observer.error === void 0) {
1457
- continue;
1458
- }
1459
- try {
1460
- await observer.error(args);
1461
- } catch (error) {
1462
- this.handleError(error);
1655
+ async runToolInputMiddlewares(args) {
1656
+ let current = args.args;
1657
+ for (const middleware of this.activeMiddlewares()) {
1658
+ const replacement = await middleware.onToolInput?.({
1659
+ ...args,
1660
+ args: current
1661
+ });
1662
+ if (replacement?.args !== void 0) {
1663
+ current = typeof replacement.args === "string" ? replacement.args : JSON.stringify(replacement.args);
1463
1664
  }
1464
1665
  }
1666
+ return current;
1465
1667
  }
1466
- handleError(error) {
1467
- if (this.failOnObserverError) {
1668
+ activeMiddlewares() {
1669
+ return [...this.agent.middlewares, ...this.requestMiddlewares];
1670
+ }
1671
+ async evaluateToolApproval(tool, context, observation) {
1672
+ const requirement = tool?.requiresApproval;
1673
+ if (requirement === void 0) return void 0;
1674
+ const resolved = typeof requirement === "function" ? await requirement(context.args, context) : requirement;
1675
+ assertToolApprovalRequirement(resolved, { allowFunction: false });
1676
+ if (resolved === false) return { approved: true };
1677
+ const reason = resolved === true ? void 0 : resolved.reason;
1678
+ return this.requestApproval(context, reason === void 0 ? {} : { reason }, observation);
1679
+ }
1680
+ async requestApproval(context, options, observation) {
1681
+ const request = {
1682
+ ...context,
1683
+ id: globalThis.crypto.randomUUID()
1684
+ };
1685
+ if (options.reason !== void 0) {
1686
+ request.reason = options.reason;
1687
+ }
1688
+ if (options.rejectMessage !== void 0) {
1689
+ request.rejectMessage = options.rejectMessage;
1690
+ }
1691
+ await observation?.runObservers.event({
1692
+ name: "tool.approval_requested",
1693
+ attributes: approvalEventAttributes(request, observation.turn)
1694
+ });
1695
+ let decision;
1696
+ try {
1697
+ decision = await this.approvalHandler(request);
1698
+ } catch (error) {
1699
+ await observation?.runObservers.event({
1700
+ name: "tool.approval_failed",
1701
+ level: "ERROR",
1702
+ attributes: {
1703
+ ...approvalEventAttributes(request, observation.turn),
1704
+ errorName: error instanceof Error ? error.name : typeof error
1705
+ }
1706
+ });
1468
1707
  throw error;
1469
1708
  }
1709
+ await observation?.runObservers.event({
1710
+ name: "tool.approval_resolved",
1711
+ attributes: {
1712
+ ...approvalEventAttributes(request, observation.turn),
1713
+ approved: decision.approved,
1714
+ ...decision.reason === void 0 ? {} : { decisionReason: decision.reason }
1715
+ }
1716
+ });
1717
+ if (decision.approved) {
1718
+ return { approved: true };
1719
+ }
1720
+ return {
1721
+ approved: false,
1722
+ result: decision.reason ?? request.rejectMessage ?? "Tool approval was rejected."
1723
+ };
1470
1724
  }
1471
1725
  };
1472
-
1473
- // src/request/retry.ts
1474
- var DEFAULT_MAX_ATTEMPTS = 3;
1475
- var DEFAULT_INITIAL_DELAY_MS = 100;
1476
- var DEFAULT_MAX_DELAY_MS = 1e3;
1477
- var MAX_TIMER_DELAY_MS = 2147483647;
1478
- var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 409, 425, 429]);
1479
- var RETRYABLE_NUMERIC_ERROR_CODES = /* @__PURE__ */ new Set([4, 8, 14]);
1480
- var RETRYABLE_ERROR_NAMES = /* @__PURE__ */ new Set([
1481
- "APIConnectionError",
1482
- "APIConnectionTimeoutError",
1483
- "TimeoutError"
1484
- ]);
1485
- var RETRYABLE_ERROR_CODES = /* @__PURE__ */ new Set([
1486
- "DEADLINE_EXCEEDED",
1487
- "EAI_AGAIN",
1488
- "ECONNREFUSED",
1489
- "ECONNRESET",
1490
- "EHOSTUNREACH",
1491
- "ENETDOWN",
1492
- "ENETUNREACH",
1493
- "EPIPE",
1494
- "ETIMEDOUT",
1495
- "RESOURCE_EXHAUSTED",
1496
- "UNAVAILABLE",
1497
- "UND_ERR_CONNECT_TIMEOUT",
1498
- "UND_ERR_HEADERS_TIMEOUT",
1499
- "UND_ERR_SOCKET"
1500
- ]);
1501
- function resolveCompletionRetryOptions(options) {
1502
- const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
1503
- const initialDelayMs = options.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
1504
- const maxDelayMs = options.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
1505
- if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) {
1506
- throw new RangeError("Completion retry maxAttempts must be a positive integer.");
1507
- }
1508
- assertDelay(initialDelayMs, "initialDelayMs");
1509
- assertDelay(maxDelayMs, "maxDelayMs");
1510
- if (maxDelayMs < initialDelayMs) {
1511
- throw new RangeError(
1512
- "Completion retry maxDelayMs must be greater than or equal to initialDelayMs."
1513
- );
1726
+ function createApprovalContext(input, hookArgs, agent, run) {
1727
+ const approvalRun = {
1728
+ agentId: agent.id,
1729
+ runId: run.runId
1730
+ };
1731
+ if (run.sessionId !== void 0) {
1732
+ approvalRun.sessionId = run.sessionId;
1514
1733
  }
1515
- if (options.shouldRetry !== void 0 && typeof options.shouldRetry !== "function") {
1516
- throw new TypeError("Completion retry shouldRetry must be a function.");
1734
+ if (run.metadata !== void 0) {
1735
+ approvalRun.metadata = run.metadata;
1517
1736
  }
1518
- return {
1519
- maxAttempts,
1520
- initialDelayMs,
1521
- maxDelayMs,
1522
- shouldRetry: options.shouldRetry ?? defaultShouldRetry
1737
+ const context = {
1738
+ toolName: hookArgs.toolName,
1739
+ args: lifecycleSnapshot(input),
1740
+ rawArgs: hookArgs.args,
1741
+ internalCallId: hookArgs.internalCallId,
1742
+ run: approvalRun
1523
1743
  };
1744
+ if (hookArgs.toolCallId !== void 0) {
1745
+ context.toolCallId = hookArgs.toolCallId;
1746
+ }
1747
+ return context;
1524
1748
  }
1525
- function completionRetryDelayMs(options, failedAttempt) {
1526
- const exponentialDelay = options.initialDelayMs * 2 ** (failedAttempt - 1);
1527
- const cappedDelay = Math.min(options.maxDelayMs, exponentialDelay);
1528
- return Math.random() * cappedDelay;
1529
- }
1530
- function completionRetryErrorAttributes(error) {
1531
- const errors = errorChain(error);
1532
- const errorName = errors.map((candidate) => stringProperty(candidate, "name")).find((value) => value !== void 0);
1533
- const statusCode = firstStatusCode(errors);
1534
- const errorCode = errors.map((candidate) => errorCodeProperty(candidate)).find((value) => value !== void 0);
1749
+ function approvalEventAttributes(request, turn) {
1535
1750
  return {
1536
- errorName,
1537
- statusCode,
1538
- errorCode
1751
+ turn,
1752
+ approvalId: request.id,
1753
+ toolName: request.toolName,
1754
+ toolCallId: request.toolCallId,
1755
+ internalCallId: request.internalCallId,
1756
+ reason: request.reason
1539
1757
  };
1540
1758
  }
1541
- async function waitForCompletionRetry(delayMs) {
1542
- if (delayMs === 0) {
1543
- return;
1759
+ function normalizeToolOutputMiddlewareResult(result) {
1760
+ if (typeof result === "string") {
1761
+ return { result };
1544
1762
  }
1545
- await new Promise((resolve) => setTimeout(resolve, delayMs));
1763
+ return result ?? {};
1546
1764
  }
1547
- function defaultShouldRetry(context) {
1548
- const errors = errorChain(context.error);
1549
- if (errors.some((error) => stringProperty(error, "name") === "AbortError")) {
1550
- return false;
1765
+ function toolTraceMetadata(tool) {
1766
+ if (tool === void 0) {
1767
+ return void 0;
1551
1768
  }
1552
- const statusCode = firstStatusCode(errors);
1553
- if (statusCode !== void 0) {
1554
- return RETRYABLE_STATUS_CODES.has(statusCode) || statusCode >= 500 && statusCode <= 599;
1769
+ const metadata = tool[MCP_TOOL_METADATA_KEY];
1770
+ const mcpMetadata = typeof metadata === "object" && metadata !== null ? metadata : void 0;
1771
+ const result = {
1772
+ approvalRequired: toolMayRequireApproval(tool.requiresApproval)
1773
+ };
1774
+ if (typeof mcpMetadata?.serverName === "string" && mcpMetadata.serverName.length > 0) {
1775
+ result.mcpServerName = mcpMetadata.serverName;
1555
1776
  }
1556
- return errors.some((error) => {
1557
- const name = stringProperty(error, "name");
1558
- if (name !== void 0 && RETRYABLE_ERROR_NAMES.has(name)) {
1559
- return true;
1560
- }
1561
- const code = stringProperty(error, "code")?.toUpperCase();
1562
- if (code !== void 0 && RETRYABLE_ERROR_CODES.has(code)) {
1563
- return true;
1564
- }
1565
- const numericCode = numberProperty(error, "code");
1566
- return numericCode !== void 0 && RETRYABLE_NUMERIC_ERROR_CODES.has(numericCode);
1567
- });
1777
+ return result;
1568
1778
  }
1569
- function firstStatusCode(errors) {
1570
- for (const error of errors) {
1571
- const status = numberProperty(error, "status") ?? numberProperty(error, "statusCode");
1572
- if (status !== void 0) {
1573
- return status;
1574
- }
1575
- const code = numberProperty(error, "code");
1576
- if (code !== void 0 && code >= 100 && code <= 599) {
1577
- return code;
1578
- }
1579
- }
1580
- return void 0;
1779
+ function toolErrorArgs(turn, toolCall, internalCallId, args, error) {
1780
+ const observerArgs = {
1781
+ turn,
1782
+ toolCall,
1783
+ toolName: toolCall.function.name,
1784
+ internalCallId,
1785
+ args,
1786
+ error,
1787
+ ...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId }
1788
+ };
1789
+ return observerArgs;
1581
1790
  }
1582
- function errorChain(error) {
1583
- const errors = [];
1584
- const seen = /* @__PURE__ */ new Set();
1585
- let current = error;
1586
- while (isObject(current) && !seen.has(current)) {
1587
- seen.add(current);
1588
- errors.push(current);
1589
- current = property(current, "cause");
1590
- }
1591
- return errors;
1592
- }
1593
- function assertDelay(value, name) {
1594
- if (!Number.isFinite(value) || value < 0 || value > MAX_TIMER_DELAY_MS) {
1595
- throw new RangeError(
1596
- `Completion retry ${name} must be between 0 and ${MAX_TIMER_DELAY_MS} milliseconds.`
1597
- );
1791
+ var ToolObserverScope = class {
1792
+ constructor(observers) {
1793
+ this.observers = observers;
1598
1794
  }
1795
+ observers;
1796
+ terminal = false;
1797
+ streamEvent(args) {
1798
+ return this.observers?.streamEvent(args);
1799
+ }
1800
+ async end(args) {
1801
+ if (this.terminal) return;
1802
+ this.terminal = true;
1803
+ await this.observers?.end(args);
1804
+ }
1805
+ async error(args) {
1806
+ if (this.terminal) return;
1807
+ this.terminal = true;
1808
+ await this.observers?.error(args);
1809
+ }
1810
+ };
1811
+ function toolOutputToText(output) {
1812
+ return typeof output === "string" ? output : toolResultContentToText(output);
1599
1813
  }
1600
- function numberProperty(value, name) {
1601
- const candidate = property(value, name);
1602
- return typeof candidate === "number" && Number.isFinite(candidate) ? candidate : void 0;
1603
- }
1604
- function stringProperty(value, name) {
1605
- const candidate = property(value, name);
1606
- return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
1607
- }
1608
- function errorCodeProperty(value) {
1609
- return stringProperty(value, "code") ?? numberProperty(value, "code");
1814
+ function toolOutputToStructuredResult(output) {
1815
+ return typeof output === "string" ? void 0 : output;
1610
1816
  }
1611
- function property(value, name) {
1612
- try {
1613
- return value[name];
1614
- } catch {
1817
+ function agentToolEventPayload(toolCall, internalCallId, event) {
1818
+ if (typeof event.agentId !== "string" || event.agentId.length === 0) {
1615
1819
  return void 0;
1616
1820
  }
1617
- }
1618
- function isObject(value) {
1619
- return typeof value === "object" && value !== null;
1821
+ const payload = {
1822
+ type: "agent_tool_event",
1823
+ toolName: toolCall.function.name,
1824
+ internalCallId,
1825
+ agentId: event.agentId,
1826
+ event: event.event
1827
+ };
1828
+ if (toolCall.callId !== void 0) {
1829
+ payload.toolCallId = toolCall.callId;
1830
+ }
1831
+ if (event.agentName !== void 0) {
1832
+ payload.agentName = event.agentName;
1833
+ }
1834
+ return payload;
1620
1835
  }
1621
1836
 
1622
- // src/request/prompt-request.ts
1623
- var PromptRequest = class _PromptRequest {
1624
- constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
1837
+ // src/internal/agent-runtime/agent-run.ts
1838
+ function deferredSignal() {
1839
+ let resolve = () => {
1840
+ };
1841
+ const promise = new Promise((done) => {
1842
+ resolve = done;
1843
+ });
1844
+ return { promise, resolve };
1845
+ }
1846
+ async function settleFailureCleanup(operations) {
1847
+ for (const operation of operations) {
1848
+ try {
1849
+ await operation();
1850
+ } catch {
1851
+ }
1852
+ }
1853
+ }
1854
+ var AgentRun = class _AgentRun {
1855
+ constructor(agent, promptMessage, initialHistory = [], options = {}) {
1625
1856
  this.agent = agent;
1626
1857
  this.promptMessage = promptMessage;
1627
- this.memoryContext = memoryContext;
1628
1858
  this.chatHistory = initialHistory;
1629
- this.maxTurnCount = agent.defaultMaxTurns ?? 0;
1630
- this.activeHook = agent.hook;
1631
- this.approvalOptions = agent.approvals;
1632
- this.guardrailPolicies = [...agent.guardrails];
1633
- this.memoryRecorder = new PromptRequestMemory(agent, memoryContext, initialHistory);
1859
+ this.maxTurnCount = assertNonnegativeSafeInteger(
1860
+ options.maxTurns ?? agent.defaultMaxTurns ?? 0,
1861
+ "maxTurns"
1862
+ );
1863
+ const internalOptions = getInternalAgentRunOptions(options);
1864
+ this.activeHook = internalOptions?.hook;
1865
+ this.requestedRunId = normalizeRequestedRunId(internalOptions?.runId);
1866
+ this.activeLifecycle = composeAgentLifecycle(agent.lifecycle, options.lifecycle);
1867
+ this.guardrailPolicies = options.guardrails === void 0 ? [...agent.guardrails] : appendGuardrailPolicies([...agent.guardrails], options.guardrails);
1868
+ const configuredConcurrency = assertPositiveSafeInteger(
1869
+ options.toolConcurrency ?? 1,
1870
+ "toolConcurrency"
1871
+ );
1872
+ this.concurrency = this.activeHook !== void 0 || agent.tools.some((tool) => toolMayRequireApproval(tool.requiresApproval)) ? 1 : configuredConcurrency;
1873
+ this.traceOptions = options.trace;
1874
+ this.completionRetryOptions = options.retries === void 0 ? void 0 : resolveRetryOptions(options.retries);
1875
+ this.requestMiddlewares = [...options.middlewares ?? []];
1876
+ this.memoryContext = options.memoryContext;
1877
+ this.memoryRecorder = new AgentRunMemory(agent, options.memoryContext, initialHistory);
1634
1878
  }
1635
1879
  agent;
1636
1880
  promptMessage;
1637
- memoryContext;
1638
1881
  chatHistory;
1639
1882
  maxTurnCount;
1640
1883
  activeHook;
1641
- approvalOptions;
1884
+ activeLifecycle;
1642
1885
  guardrailPolicies;
1643
1886
  guardrailDecisions = [];
1644
- concurrency = 1;
1887
+ concurrency;
1645
1888
  traceOptions;
1646
1889
  completionRetryOptions;
1647
- requestMiddlewares = [];
1890
+ requestMiddlewares;
1648
1891
  steeringMessages = [];
1649
1892
  runState = "idle";
1650
1893
  memoryRecorder;
1651
- static fromAgent(agent, prompt, options = {}) {
1652
- const normalized = normalizePromptInput(prompt);
1653
- return new _PromptRequest(agent, normalized.prompt, normalized.history, options.memoryContext);
1654
- }
1655
- maxTurns(maxTurns) {
1656
- this.maxTurnCount = maxTurns;
1657
- return this;
1658
- }
1659
- withCompletionRetries(options = {}) {
1660
- this.completionRetryOptions = resolveCompletionRetryOptions(options);
1661
- return this;
1662
- }
1663
- withHook(hook) {
1664
- this.activeHook = hook;
1665
- return this;
1666
- }
1667
- approvals(options) {
1668
- this.approvalOptions = options;
1669
- return this;
1894
+ memoryContext;
1895
+ pendingApproval;
1896
+ approvalSignal = deferredSignal();
1897
+ activeRunId;
1898
+ requestedRunId;
1899
+ currentUsage = Usage.empty();
1900
+ currentMessages = [];
1901
+ cancellationError;
1902
+ activeGeneration;
1903
+ static fromAgent(agent, input, options = {}) {
1904
+ const normalized = normalizeAgentInput(input);
1905
+ return new _AgentRun(agent, normalized.prompt, normalized.history, options);
1670
1906
  }
1671
- guardrails(policies) {
1672
- this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);
1673
- return this;
1907
+ steer(input) {
1908
+ if (this.isTerminal() || this.cancellationError !== void 0) {
1909
+ return false;
1910
+ }
1911
+ this.steeringMessages.push(...normalizeSteeringInput(input));
1912
+ return true;
1674
1913
  }
1675
- withToolConcurrency(concurrency) {
1676
- this.concurrency = Math.max(1, concurrency);
1677
- return this;
1914
+ waitForApproval() {
1915
+ return this.pendingApproval === void 0 ? this.approvalSignal.promise : Promise.resolve();
1678
1916
  }
1679
- withMiddleware(middleware) {
1680
- this.requestMiddlewares.push(middleware);
1681
- return this;
1917
+ approvalResult() {
1918
+ const snapshot = this.approvalSnapshot();
1919
+ return { status: "approval_required", ...snapshot };
1682
1920
  }
1683
- withMiddlewares(middlewares) {
1684
- this.requestMiddlewares.push(...middlewares);
1685
- return this;
1921
+ approvalEvent() {
1922
+ const snapshot = this.approvalSnapshot();
1923
+ return { type: "approval_required", ...snapshot };
1686
1924
  }
1687
- withTrace(trace) {
1688
- this.traceOptions = trace;
1689
- return this;
1925
+ resolveApproval(decision) {
1926
+ const pending = this.pendingApproval;
1927
+ if (pending === void 0) {
1928
+ throw new TypeError("Agent run has no pending tool approval.");
1929
+ }
1930
+ this.pendingApproval = void 0;
1931
+ this.approvalSignal = deferredSignal();
1932
+ pending.resolve(decision);
1690
1933
  }
1691
- steer(input) {
1692
- if (this.isTerminal()) {
1693
- return false;
1934
+ cancel(reason) {
1935
+ if (this.isTerminal() || this.cancellationError !== void 0) {
1936
+ return;
1937
+ }
1938
+ const messages = this.currentMessages.length === 0 ? [this.promptMessage] : [...this.currentMessages];
1939
+ const error = new AgentRunCancelledError([...this.chatHistory, ...messages], reason);
1940
+ this.cancellationError = error;
1941
+ const pending = this.pendingApproval;
1942
+ if (pending !== void 0) {
1943
+ this.pendingApproval = void 0;
1944
+ this.approvalSignal = deferredSignal();
1945
+ pending.reject(error);
1694
1946
  }
1695
- this.steeringMessages.push(...normalizeSteeringInput(input));
1696
- return true;
1697
1947
  }
1698
- async send() {
1948
+ async generate() {
1699
1949
  this.startRun();
1700
- const runId = globalThis.crypto.randomUUID();
1950
+ const runId = this.requestedRunId ?? globalThis.crypto.randomUUID();
1951
+ this.activeRunId = runId;
1701
1952
  let usage = Usage.empty();
1702
1953
  let currentTurns = 0;
1703
1954
  let lastPrompt = this.promptMessage;
1704
1955
  let newMessages = [this.promptMessage];
1705
- const runObservers = await this.startRunObservers();
1956
+ let runObservers;
1706
1957
  try {
1958
+ const memoryPreparation = await this.memoryRecorder.prepareHistory(runId, newMessages.length);
1959
+ this.chatHistory = memoryPreparation.history;
1960
+ usage = Usage.add(usage, memoryPreparation.usage);
1961
+ runObservers = await this.startRunObservers(runId);
1962
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
1963
+ await this.activeLifecycle?.onStart?.({
1964
+ runId,
1965
+ input: lifecycleSnapshot(this.promptMessage),
1966
+ history: lifecycleSnapshot(this.chatHistory),
1967
+ maxTurns: this.maxTurnCount
1968
+ });
1707
1969
  const inputResult = await runInputGuardrails(this.guardrailPolicies, {
1708
1970
  prompt: this.promptMessage,
1709
1971
  history: this.chatHistory,
@@ -1717,51 +1979,54 @@ var PromptRequest = class _PromptRequest {
1717
1979
  if (inputResult.blocked) {
1718
1980
  const output = inputResult.message ?? "The request was blocked by a guardrail.";
1719
1981
  const result = {
1982
+ status: "completed",
1983
+ runId,
1720
1984
  output,
1721
- usage: Usage.empty(),
1985
+ usage,
1722
1986
  messages: [this.promptMessage, Message.assistant(output)],
1987
+ trace: runObservers.trace,
1723
1988
  guardrails: [...this.guardrailDecisions]
1724
1989
  };
1990
+ await this.runLifecycleFinish(result);
1725
1991
  await runObservers.end(result);
1726
1992
  this.runState = "completed";
1727
1993
  return result;
1728
1994
  }
1729
1995
  newMessages = [this.promptMessage];
1730
- const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1731
- this.chatHistory = memoryPreparation.history;
1732
- usage = Usage.add(usage, memoryPreparation.usage);
1733
- await this.recordMemoryCompaction(memoryPreparation, runObservers);
1996
+ await this.memoryRecorder.commitAcceptedInput(runId, newMessages);
1734
1997
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1735
1998
  await this.runRunStartHook(newMessages);
1736
1999
  while (currentTurns <= this.maxTurnCount + 1) {
1737
2000
  const prompt = newMessages.at(-1);
1738
2001
  if (prompt === void 0) {
1739
- throw new Error("PromptRequest requires at least one message");
2002
+ throw new Error("AgentRun requires at least one message");
1740
2003
  }
1741
2004
  lastPrompt = prompt;
1742
2005
  currentTurns += 1;
1743
2006
  const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
1744
2007
  await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
1745
2008
  await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
1746
- const ragText = extractRagText(prompt);
1747
- const dynamicContext = await fetchDynamicContext(this.agent, ragText);
1748
- const toolDefs = await fetchToolDefinitions(this.agent, ragText);
1749
- let request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools([...toolDefs, ...this.agent.providerTools]).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
1750
- request = await this.runCompletionRequestMiddlewares(
1751
- request,
1752
- currentTurns
1753
- );
2009
+ const request = await this.createTurnRequest(prompt, historyForRequest, currentTurns);
1754
2010
  let response;
1755
2011
  try {
1756
2012
  response = await this.runCompletion(request, currentTurns, runObservers);
1757
2013
  } catch (error) {
1758
- await this.runCompletionErrorHook(prompt, error, newMessages);
2014
+ await settleFailureCleanup([
2015
+ () => this.runCompletionErrorHook(prompt, error, newMessages)
2016
+ ]);
1759
2017
  throw error;
1760
2018
  }
1761
2019
  response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
1762
2020
  usage = Usage.add(usage, response.usage);
2021
+ this.updateApprovalProgress(runId, usage, newMessages);
1763
2022
  await this.runCompletionResponseHook(prompt, response, newMessages);
1764
2023
  await this.runTurnEndHook(currentTurns, response, newMessages);
2024
+ await this.activeLifecycle?.onStepFinish?.({
2025
+ runId,
2026
+ step: currentTurns,
2027
+ response: lifecycleSnapshot(response),
2028
+ usage: lifecycleSnapshot(usage)
2029
+ });
1765
2030
  const toolCalls = response.choice.filter(
1766
2031
  (item) => item.type === "tool_call"
1767
2032
  );
@@ -1797,6 +2062,8 @@ var PromptRequest = class _PromptRequest {
1797
2062
  pendingTurnMessages
1798
2063
  );
1799
2064
  const result = {
2065
+ status: "completed",
2066
+ runId,
1800
2067
  output: guardedOutput.output,
1801
2068
  usage,
1802
2069
  messages: [...newMessages],
@@ -1805,22 +2072,18 @@ var PromptRequest = class _PromptRequest {
1805
2072
  ...generationArtifacts(newMessages)
1806
2073
  };
1807
2074
  await this.runRunEndHook(result, newMessages);
1808
- await runObservers.end(result);
1809
2075
  await this.memoryRecorder.commitCompletedRun(
1810
2076
  runId,
1811
2077
  currentTurns,
1812
2078
  newMessages,
1813
2079
  pendingTurnMessages
1814
2080
  );
2081
+ await this.runLifecycleFinish(result);
2082
+ await runObservers.end(result);
1815
2083
  this.runState = "completed";
1816
2084
  return result;
1817
2085
  }
1818
- await this.memoryRecorder.commitMessages(
1819
- runId,
1820
- currentTurns,
1821
- [assistantMessage],
1822
- pendingTurnMessages
1823
- );
2086
+ this.updateApprovalProgress(runId, usage, newMessages);
1824
2087
  const toolResults = await this.executeToolCalls(
1825
2088
  runId,
1826
2089
  toolCalls,
@@ -1838,7 +2101,7 @@ var PromptRequest = class _PromptRequest {
1838
2101
  await this.memoryRecorder.commitMessages(
1839
2102
  runId,
1840
2103
  currentTurns,
1841
- [toolMessage],
2104
+ [assistantMessage, toolMessage],
1842
2105
  pendingTurnMessages
1843
2106
  );
1844
2107
  await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages);
@@ -1849,31 +2112,42 @@ var PromptRequest = class _PromptRequest {
1849
2112
  if (error instanceof MemoryCompactionError && error.usage !== void 0) {
1850
2113
  usage = Usage.add(usage, error.usage);
1851
2114
  }
1852
- const finalError = await this.runRunErrorHook(error, usage, newMessages);
1853
- this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
1854
- await runObservers.error({ error: finalError, usage, messages: [...newMessages] });
1855
- await this.memoryRecorder.recordError(runId, finalError, newMessages);
1856
- throw finalError;
2115
+ const reportedError = await this.reportRunFailure(
2116
+ error,
2117
+ runId,
2118
+ usage,
2119
+ newMessages,
2120
+ runObservers
2121
+ );
2122
+ this.runState = reportedError instanceof AgentRunCancelledError ? "cancelled" : "errored";
2123
+ throw reportedError;
1857
2124
  }
1858
2125
  }
1859
- async *stream(options = {}) {
2126
+ async *events(includeToolCallDeltas = true) {
1860
2127
  if (!this.agent.model.capabilities.streaming || !isStreamingCompletionModel(this.agent.model)) {
1861
2128
  throw new Error("This completion model does not support streaming");
1862
2129
  }
1863
2130
  this.startRun();
1864
- const includeToolCallDeltas = options.includeToolCallDeltas !== false;
1865
- const runId = globalThis.crypto.randomUUID();
1866
- const emit = async (event) => {
1867
- await this.recordAgentEvent(runId, event);
1868
- return event;
1869
- };
2131
+ const runId = this.requestedRunId ?? globalThis.crypto.randomUUID();
2132
+ this.activeRunId = runId;
1870
2133
  let usage = Usage.empty();
1871
2134
  let currentTurns = 0;
1872
2135
  let lastPrompt = this.promptMessage;
1873
2136
  let newMessages = [this.promptMessage];
1874
- const runObservers = await this.startRunObservers();
1875
2137
  const bufferOutputDeltas = hasEnforcedOutputGuardrails(this.guardrailPolicies);
2138
+ let runObservers;
1876
2139
  try {
2140
+ const memoryPreparation = await this.memoryRecorder.prepareHistory(runId, newMessages.length);
2141
+ this.chatHistory = memoryPreparation.history;
2142
+ usage = Usage.add(usage, memoryPreparation.usage);
2143
+ runObservers = await this.startRunObservers(runId);
2144
+ await this.recordMemoryCompaction(memoryPreparation, runObservers);
2145
+ await this.activeLifecycle?.onStart?.({
2146
+ runId,
2147
+ input: lifecycleSnapshot(this.promptMessage),
2148
+ history: lifecycleSnapshot(this.chatHistory),
2149
+ maxTurns: this.maxTurnCount
2150
+ });
1877
2151
  const inputResult = await runInputGuardrails(this.guardrailPolicies, {
1878
2152
  prompt: this.promptMessage,
1879
2153
  history: this.chatHistory,
@@ -1882,60 +2156,55 @@ var PromptRequest = class _PromptRequest {
1882
2156
  });
1883
2157
  for (const decision of inputResult.decisions) {
1884
2158
  await this.recordGuardrailDecision(decision, runObservers);
1885
- yield await emit({ type: "guardrail_decision", decision });
2159
+ yield { type: "guardrail_decision", decision };
1886
2160
  }
1887
2161
  this.promptMessage = inputResult.prompt;
1888
2162
  if (inputResult.blocked) {
1889
2163
  const output = inputResult.message ?? "The request was blocked by a guardrail.";
1890
2164
  const result = {
2165
+ status: "completed",
2166
+ runId,
1891
2167
  output,
1892
- usage: Usage.empty(),
2168
+ usage,
1893
2169
  messages: [this.promptMessage, Message.assistant(output)],
2170
+ trace: runObservers.trace,
1894
2171
  guardrails: [...this.guardrailDecisions]
1895
2172
  };
2173
+ await this.runLifecycleFinish(result);
1896
2174
  await runObservers.end(result);
1897
2175
  this.runState = "completed";
1898
- yield await emit({
2176
+ yield {
1899
2177
  type: "final",
1900
2178
  runId,
1901
2179
  output: result.output,
1902
2180
  usage: result.usage,
1903
2181
  messages: result.messages,
2182
+ trace: result.trace,
1904
2183
  guardrails: result.guardrails
1905
- });
2184
+ };
1906
2185
  return;
1907
2186
  }
1908
2187
  newMessages = [this.promptMessage];
1909
- const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
1910
- this.chatHistory = memoryPreparation.history;
1911
- usage = Usage.add(usage, memoryPreparation.usage);
1912
- await this.recordMemoryCompaction(memoryPreparation, runObservers);
2188
+ await this.memoryRecorder.commitAcceptedInput(runId, newMessages);
1913
2189
  const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
1914
2190
  await this.runRunStartHook(newMessages);
1915
2191
  while (currentTurns <= this.maxTurnCount + 1) {
1916
2192
  const prompt = newMessages.at(-1);
1917
2193
  if (prompt === void 0) {
1918
- throw new Error("PromptRequest requires at least one message");
2194
+ throw new Error("AgentRun requires at least one message");
1919
2195
  }
1920
2196
  lastPrompt = prompt;
1921
2197
  currentTurns += 1;
1922
2198
  const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
1923
- yield await emit({
2199
+ yield {
1924
2200
  type: "turn_start",
1925
2201
  turn: currentTurns,
1926
2202
  prompt,
1927
2203
  history: historyForRequest
1928
- });
2204
+ };
1929
2205
  await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
1930
2206
  await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
1931
- const ragText = extractRagText(prompt);
1932
- const dynamicContext = await fetchDynamicContext(this.agent, ragText);
1933
- const toolDefs = await fetchToolDefinitions(this.agent, ragText);
1934
- let request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools([...toolDefs, ...this.agent.providerTools]).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
1935
- request = await this.runCompletionRequestMiddlewares(
1936
- request,
1937
- currentTurns
1938
- );
2207
+ const request = await this.createTurnRequest(prompt, historyForRequest, currentTurns);
1939
2208
  assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
1940
2209
  const providerRequest = this.providerTraceRequest(request, { stream: true });
1941
2210
  const generationStartArgs = this.generationStartArgs(
@@ -1944,101 +2213,89 @@ var PromptRequest = class _PromptRequest {
1944
2213
  providerRequest
1945
2214
  );
1946
2215
  const generationObservers = await runObservers.startGeneration(generationStartArgs);
2216
+ this.activeGeneration = { turn: currentTurns, observers: generationObservers };
1947
2217
  const generationStartedAt = Date.now();
1948
- yield await emit({
2218
+ yield {
1949
2219
  type: "generation_start",
1950
2220
  turn: currentTurns,
1951
2221
  request,
1952
2222
  modelInfo: generationStartArgs.modelInfo
1953
- });
1954
- const accumulator = new CompletionStreamAccumulator();
1955
- let firstDeltaMs;
2223
+ };
1956
2224
  const bufferResponseEvents = this.shouldBufferStreamResponseEvents();
1957
- const emittedToolCallIds = /* @__PURE__ */ new Set();
2225
+ const completionState = {
2226
+ response: void 0,
2227
+ firstDeltaMs: void 0,
2228
+ emittedToolCallIds: /* @__PURE__ */ new Set(),
2229
+ providerErrorUsage: Usage.empty()
2230
+ };
1958
2231
  let response;
1959
2232
  try {
1960
- for (let attempt = 1; ; attempt += 1) {
1961
- let hasProviderProgress = false;
1962
- try {
1963
- for await (const event of this.agent.model.streamCompletion(request)) {
1964
- if (event.type === "error") {
1965
- if (event.usage !== void 0) {
1966
- usage = Usage.add(usage, event.usage);
1967
- }
1968
- throw event.error;
1969
- }
1970
- hasProviderProgress = true;
1971
- if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
1972
- firstDeltaMs = Date.now() - generationStartedAt;
1973
- }
1974
- const mapped = accumulator.accept(event);
1975
- if (includeToolCallDeltas && event.type === "tool_call_delta") {
1976
- yield await emit(addTurnToToolCallDelta(currentTurns, event));
1977
- }
1978
- if (mapped !== void 0) {
1979
- await generationObservers.update?.({ turn: currentTurns, delta: mapped });
1980
- if (mapped.type === "tool_call") {
1981
- emittedToolCallIds.add(mapped.toolCall.id);
1982
- }
1983
- const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
1984
- if (!shouldBuffer) {
1985
- yield await emit(addTurn(currentTurns, mapped));
1986
- }
1987
- }
1988
- }
1989
- response = accumulator.response();
1990
- break;
1991
- } catch (error) {
1992
- const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, currentTurns, true);
1993
- if (retryOptions === void 0) {
1994
- throw error;
1995
- }
1996
- await this.scheduleCompletionRetry(
1997
- error,
1998
- attempt,
1999
- currentTurns,
2000
- true,
2001
- retryOptions,
2002
- runObservers
2003
- );
2233
+ try {
2234
+ for await (const event of this.streamCompletion({
2235
+ request,
2236
+ turn: currentTurns,
2237
+ includeToolCallDeltas,
2238
+ bufferResponseEvents,
2239
+ bufferOutputDeltas,
2240
+ generationStartedAt,
2241
+ generationObservers,
2242
+ runObservers,
2243
+ state: completionState
2244
+ })) {
2245
+ yield event;
2004
2246
  }
2247
+ } finally {
2248
+ usage = Usage.add(usage, completionState.providerErrorUsage);
2005
2249
  }
2250
+ if (completionState.response === void 0) {
2251
+ throw new Error("Streaming completion ended without a response.");
2252
+ }
2253
+ response = completionState.response;
2006
2254
  } catch (error) {
2007
- await generationObservers.error({ turn: currentTurns, error });
2008
- await this.runCompletionErrorHook(prompt, error, newMessages);
2255
+ await settleFailureCleanup([
2256
+ () => this.closeActiveGeneration(error),
2257
+ () => this.runCompletionErrorHook(prompt, error, newMessages)
2258
+ ]);
2009
2259
  throw error;
2010
2260
  }
2261
+ const { firstDeltaMs, emittedToolCallIds } = completionState;
2011
2262
  const generationEndArgs = {
2012
2263
  turn: currentTurns,
2013
- response
2264
+ response,
2265
+ ...firstDeltaMs === void 0 ? {} : { firstDeltaMs }
2014
2266
  };
2015
- if (firstDeltaMs !== void 0) {
2016
- generationEndArgs.firstDeltaMs = firstDeltaMs;
2017
- }
2267
+ this.activeGeneration = void 0;
2018
2268
  await generationObservers.end(generationEndArgs);
2019
2269
  response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
2020
2270
  usage = Usage.add(usage, response.usage);
2271
+ this.updateApprovalProgress(runId, usage, newMessages);
2021
2272
  await this.runCompletionResponseHook(prompt, response, newMessages);
2022
2273
  await this.runTurnEndHook(currentTurns, response, newMessages);
2274
+ await this.activeLifecycle?.onStepFinish?.({
2275
+ runId,
2276
+ step: currentTurns,
2277
+ response: lifecycleSnapshot(response),
2278
+ usage: lifecycleSnapshot(usage)
2279
+ });
2023
2280
  const toolCalls = response.choice.filter(
2024
2281
  (item) => item.type === "tool_call"
2025
2282
  );
2026
- let assistantMessage = this.generatedAssistantMessage(response, request);
2283
+ const assistantMessage = this.generatedAssistantMessage(response, request);
2027
2284
  newMessages.push(assistantMessage);
2028
2285
  if (toolCalls.length === 0) {
2029
2286
  let emittedTurnEnd = false;
2030
2287
  if (!bufferOutputDeltas) {
2031
2288
  if (bufferResponseEvents) {
2032
2289
  for (const event of responseStreamEvents(currentTurns, response)) {
2033
- yield await emit(event);
2290
+ yield event;
2034
2291
  }
2035
2292
  }
2036
- yield await emit({
2293
+ yield {
2037
2294
  type: "turn_end",
2038
2295
  turn: currentTurns,
2039
2296
  response,
2040
2297
  firstDeltaMs
2041
- });
2298
+ };
2042
2299
  emittedTurnEnd = true;
2043
2300
  }
2044
2301
  if (this.steeringMessages.length > 0) {
@@ -2053,128 +2310,58 @@ var PromptRequest = class _PromptRequest {
2053
2310
  await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
2054
2311
  continue;
2055
2312
  }
2056
- const guardedOutput = await this.runOutputGuardrailsForResponse(
2313
+ for await (const event of this.completeStreamingRun({
2057
2314
  runId,
2058
- usage,
2315
+ turn: currentTurns,
2316
+ request,
2059
2317
  response,
2060
- newMessages,
2061
- runObservers
2062
- );
2063
- for (const decision of guardedOutput.decisions) {
2064
- yield await emit({ type: "guardrail_decision", decision });
2065
- }
2066
- response = guardedOutput.response;
2067
- assistantMessage = this.generatedAssistantMessage(response, request);
2068
- newMessages[newMessages.length - 1] = assistantMessage;
2069
- await this.memoryRecorder.commitMessages(
2070
- runId,
2071
- currentTurns,
2072
- [assistantMessage],
2073
- pendingTurnMessages
2074
- );
2075
- if (!emittedTurnEnd && (bufferResponseEvents || bufferOutputDeltas)) {
2076
- for (const event of responseStreamEvents(
2077
- currentTurns,
2078
- response,
2079
- bufferResponseEvents
2080
- )) {
2081
- yield await emit(event);
2082
- }
2083
- }
2084
- if (!emittedTurnEnd) {
2085
- yield await emit({
2086
- type: "turn_end",
2087
- turn: currentTurns,
2088
- response,
2089
- firstDeltaMs
2090
- });
2091
- }
2092
- const result = {
2093
- output: guardedOutput.output,
2318
+ firstDeltaMs,
2094
2319
  usage,
2095
- messages: [...newMessages],
2096
- trace: runObservers.trace,
2097
- guardrails: [...this.guardrailDecisions],
2098
- ...generationArtifacts(newMessages)
2099
- };
2100
- await this.runRunEndHook(result, newMessages);
2101
- await runObservers.end(result);
2102
- await this.memoryRecorder.commitCompletedRun(
2103
- runId,
2104
- currentTurns,
2105
2320
  newMessages,
2106
- pendingTurnMessages
2107
- );
2108
- this.runState = "completed";
2109
- yield await emit({
2110
- type: "final",
2111
- runId,
2112
- output: result.output,
2113
- usage: result.usage,
2114
- contextUsage: result.contextUsage,
2115
- messages: result.messages,
2116
- trace: result.trace,
2117
- guardrails: result.guardrails,
2118
- sources: result.sources,
2119
- providerToolCalls: result.providerToolCalls
2120
- });
2321
+ pendingTurnMessages,
2322
+ runObservers,
2323
+ bufferResponseEvents,
2324
+ bufferOutputDeltas,
2325
+ emittedTurnEnd
2326
+ })) {
2327
+ yield event;
2328
+ }
2121
2329
  return;
2122
2330
  }
2123
2331
  if (bufferResponseEvents) {
2124
2332
  for (const event of responseStreamEvents(currentTurns, response)) {
2125
- yield await emit(event);
2333
+ yield event;
2126
2334
  }
2127
2335
  } else {
2128
2336
  for (const toolCall of toolCalls) {
2129
2337
  if (!emittedToolCallIds.has(toolCall.id)) {
2130
- yield await emit({ type: "tool_call", turn: currentTurns, toolCall });
2338
+ yield { type: "tool_call", turn: currentTurns, toolCall };
2131
2339
  }
2132
2340
  }
2133
2341
  }
2134
- await this.memoryRecorder.commitMessages(
2135
- runId,
2136
- currentTurns,
2137
- [assistantMessage],
2138
- pendingTurnMessages
2139
- );
2140
- yield await emit({
2342
+ this.updateApprovalProgress(runId, usage, newMessages);
2343
+ yield {
2141
2344
  type: "turn_end",
2142
2345
  turn: currentTurns,
2143
2346
  response,
2144
2347
  firstDeltaMs
2348
+ };
2349
+ const toolExecution = this.executeStreamingToolCalls(runId, toolCalls, newMessages, {
2350
+ turn: currentTurns,
2351
+ runObservers,
2352
+ toolDefinitions: request.tools,
2353
+ includeToolCallDeltas
2145
2354
  });
2146
- const toolResultEvents = createAsyncQueue();
2147
- const toolResultsPromise = this.executeToolCalls(
2148
- runId,
2149
- toolCalls,
2150
- newMessages,
2151
- (result) => {
2152
- toolResultEvents.enqueue(result);
2153
- },
2154
- (event) => {
2155
- toolResultEvents.enqueue(event);
2156
- },
2157
- {
2158
- turn: currentTurns,
2159
- runObservers,
2160
- toolDefinitions: request.tools,
2161
- includeToolCallDeltas
2162
- }
2163
- );
2164
- toolResultsPromise.then(
2165
- () => toolResultEvents.close(),
2166
- (error) => toolResultEvents.throw(error)
2167
- );
2168
- for await (const result of toolResultEvents) {
2169
- yield await emit({ turn: currentTurns, ...result });
2355
+ for await (const result of toolExecution.events) {
2356
+ yield { turn: currentTurns, ...result };
2170
2357
  }
2171
- const toolResults = await toolResultsPromise;
2358
+ const toolResults = await toolExecution.results;
2172
2359
  const toolMessage = Message.tool(toolResults);
2173
2360
  newMessages.push(toolMessage);
2174
2361
  await this.memoryRecorder.commitMessages(
2175
2362
  runId,
2176
2363
  currentTurns,
2177
- [toolMessage],
2364
+ [assistantMessage, toolMessage],
2178
2365
  pendingTurnMessages
2179
2366
  );
2180
2367
  await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages);
@@ -2185,22 +2372,27 @@ var PromptRequest = class _PromptRequest {
2185
2372
  if (error instanceof MemoryCompactionError && error.usage !== void 0) {
2186
2373
  usage = Usage.add(usage, error.usage);
2187
2374
  }
2188
- const finalError = await this.runRunErrorHook(error, usage, newMessages);
2189
- this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
2375
+ const reportedError = await this.reportRunFailure(
2376
+ error,
2377
+ runId,
2378
+ usage,
2379
+ newMessages,
2380
+ runObservers
2381
+ );
2382
+ this.runState = reportedError instanceof AgentRunCancelledError ? "cancelled" : "errored";
2190
2383
  const finalUsage = usage;
2191
- await runObservers.error({
2192
- error: finalError,
2193
- usage: finalUsage,
2194
- messages: [...newMessages]
2195
- });
2196
- await this.memoryRecorder.recordError(runId, finalError, newMessages);
2197
- yield await emit({ type: "error", error: finalError, usage: finalUsage });
2198
- throw finalError;
2384
+ yield { type: "error", error: reportedError, usage: finalUsage };
2385
+ throw reportedError;
2386
+ } finally {
2387
+ if (this.runState === "running") {
2388
+ const cancellation = this.cancellationError ?? new AgentRunCancelledError([...this.chatHistory, ...newMessages], "Agent stream closed.");
2389
+ this.cancellationError = cancellation;
2390
+ await settleFailureCleanup([() => this.closeActiveGeneration(cancellation)]);
2391
+ await this.reportRunFailure(cancellation, runId, usage, newMessages, runObservers);
2392
+ this.runState = "cancelled";
2393
+ }
2199
2394
  }
2200
2395
  }
2201
- readableStream(options = {}) {
2202
- return toReadableStream(this.stream(options));
2203
- }
2204
2396
  async runCompletion(request, turn, runObservers) {
2205
2397
  assertCompletionRequestSupported(this.agent.model, request);
2206
2398
  const providerRequest = this.providerTraceRequest(request);
@@ -2231,10 +2423,146 @@ var PromptRequest = class _PromptRequest {
2231
2423
  return response;
2232
2424
  }
2233
2425
  } catch (error) {
2234
- await generationObservers.error({ turn, error });
2426
+ await settleFailureCleanup([() => generationObservers.error({ turn, error })]);
2235
2427
  throw error;
2236
2428
  }
2237
2429
  }
2430
+ async createTurnRequest(prompt, history, turn) {
2431
+ const ragText = extractRagText(prompt);
2432
+ const documents = await fetchContextDocuments(this.agent, ragText);
2433
+ const toolDefinitions = await fetchToolDefinitions(this.agent, ragText);
2434
+ const request = createCompletionRequest([...history, prompt], {
2435
+ model: this.agent.model,
2436
+ instructions: this.agent.instructions,
2437
+ documents,
2438
+ tools: [...toolDefinitions, ...getAgentToolState(this.agent).providerTools],
2439
+ temperature: this.agent.temperature,
2440
+ maxTokens: this.agent.maxTokens,
2441
+ additionalParams: this.agent.additionalParams,
2442
+ toolChoice: this.agent.toolChoice,
2443
+ outputSchema: this.agent.outputSchema
2444
+ });
2445
+ return await this.runCompletionRequestMiddlewares(request, turn);
2446
+ }
2447
+ async *streamCompletion(args) {
2448
+ const model = this.agent.model;
2449
+ if (!isStreamingCompletionModel(model)) {
2450
+ throw new TypeError("Streaming completion requires a streaming-capable model.");
2451
+ }
2452
+ const accumulator = new CompletionStreamAccumulator();
2453
+ for (let attempt = 1; ; attempt += 1) {
2454
+ let hasProviderProgress = false;
2455
+ try {
2456
+ for await (const event of model.streamCompletion(args.request)) {
2457
+ if (event.type === "error") {
2458
+ if (event.usage !== void 0) {
2459
+ args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, event.usage);
2460
+ }
2461
+ throw event.error;
2462
+ }
2463
+ hasProviderProgress = true;
2464
+ if (args.state.firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
2465
+ args.state.firstDeltaMs = Date.now() - args.generationStartedAt;
2466
+ }
2467
+ const mapped = accumulator.accept(event);
2468
+ if (args.includeToolCallDeltas && event.type === "tool_call_delta") {
2469
+ yield addTurnToToolCallDelta(args.turn, event);
2470
+ }
2471
+ if (mapped !== void 0) {
2472
+ await args.generationObservers.update?.({ turn: args.turn, delta: mapped });
2473
+ if (mapped.type === "tool_call") {
2474
+ args.state.emittedToolCallIds.add(mapped.toolCall.id);
2475
+ }
2476
+ const shouldBuffer = args.bufferResponseEvents || args.bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
2477
+ if (!shouldBuffer) {
2478
+ yield addTurn(args.turn, mapped);
2479
+ }
2480
+ }
2481
+ }
2482
+ args.state.response = accumulator.response();
2483
+ return;
2484
+ } catch (error) {
2485
+ const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, args.turn, true);
2486
+ if (retryOptions === void 0) {
2487
+ throw error;
2488
+ }
2489
+ await this.scheduleCompletionRetry(
2490
+ error,
2491
+ attempt,
2492
+ args.turn,
2493
+ true,
2494
+ retryOptions,
2495
+ args.runObservers
2496
+ );
2497
+ }
2498
+ }
2499
+ }
2500
+ async *completeStreamingRun(args) {
2501
+ const guardedOutput = await this.runOutputGuardrailsForResponse(
2502
+ args.runId,
2503
+ args.usage,
2504
+ args.response,
2505
+ args.newMessages,
2506
+ args.runObservers
2507
+ );
2508
+ for (const decision of guardedOutput.decisions) {
2509
+ yield { type: "guardrail_decision", decision };
2510
+ }
2511
+ const response = guardedOutput.response;
2512
+ const assistantMessage = this.generatedAssistantMessage(response, args.request);
2513
+ args.newMessages[args.newMessages.length - 1] = assistantMessage;
2514
+ await this.memoryRecorder.commitMessages(
2515
+ args.runId,
2516
+ args.turn,
2517
+ [assistantMessage],
2518
+ args.pendingTurnMessages
2519
+ );
2520
+ if (!args.emittedTurnEnd && (args.bufferResponseEvents || args.bufferOutputDeltas)) {
2521
+ for (const event of responseStreamEvents(args.turn, response, args.bufferResponseEvents)) {
2522
+ yield event;
2523
+ }
2524
+ }
2525
+ if (!args.emittedTurnEnd) {
2526
+ yield {
2527
+ type: "turn_end",
2528
+ turn: args.turn,
2529
+ response,
2530
+ firstDeltaMs: args.firstDeltaMs
2531
+ };
2532
+ }
2533
+ const result = {
2534
+ status: "completed",
2535
+ runId: args.runId,
2536
+ output: guardedOutput.output,
2537
+ usage: args.usage,
2538
+ messages: [...args.newMessages],
2539
+ trace: args.runObservers.trace,
2540
+ guardrails: [...this.guardrailDecisions],
2541
+ ...generationArtifacts(args.newMessages)
2542
+ };
2543
+ await this.runRunEndHook(result, args.newMessages);
2544
+ await this.memoryRecorder.commitCompletedRun(
2545
+ args.runId,
2546
+ args.turn,
2547
+ args.newMessages,
2548
+ args.pendingTurnMessages
2549
+ );
2550
+ await this.runLifecycleFinish(result);
2551
+ await args.runObservers.end(result);
2552
+ this.runState = "completed";
2553
+ yield {
2554
+ type: "final",
2555
+ runId: args.runId,
2556
+ output: result.output,
2557
+ usage: result.usage,
2558
+ contextUsage: result.contextUsage,
2559
+ messages: result.messages,
2560
+ trace: result.trace,
2561
+ guardrails: result.guardrails,
2562
+ sources: result.sources,
2563
+ providerToolCalls: result.providerToolCalls
2564
+ };
2565
+ }
2238
2566
  generatedAssistantMessage(response, request) {
2239
2567
  const metadata = {
2240
2568
  anvia: {
@@ -2263,19 +2591,16 @@ var PromptRequest = class _PromptRequest {
2263
2591
  }
2264
2592
  }
2265
2593
  generationStartArgs(turn, request, providerRequest) {
2266
- const args = {
2594
+ return {
2267
2595
  turn,
2268
2596
  request,
2269
2597
  modelInfo: {
2270
2598
  provider: this.agent.model.provider,
2271
2599
  defaultModel: this.agent.model.defaultModel,
2272
2600
  capabilities: this.agent.model.capabilities
2273
- }
2601
+ },
2602
+ ...providerRequest === void 0 ? {} : { providerRequest }
2274
2603
  };
2275
- if (providerRequest !== void 0) {
2276
- args.providerRequest = providerRequest;
2277
- }
2278
- return args;
2279
2604
  }
2280
2605
  retryOptionsForFailure(error, attempt, turn, streaming) {
2281
2606
  const options = this.completionRetryOptions;
@@ -2292,7 +2617,7 @@ var PromptRequest = class _PromptRequest {
2292
2617
  return shouldRetry ? options : void 0;
2293
2618
  }
2294
2619
  async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers) {
2295
- const delayMs = completionRetryDelayMs(options, attempt);
2620
+ const delayMs = retryDelayMs(options, attempt);
2296
2621
  await runObservers.event({
2297
2622
  name: "completion.retry",
2298
2623
  level: "WARNING",
@@ -2303,16 +2628,17 @@ var PromptRequest = class _PromptRequest {
2303
2628
  maxAttempts: options.maxAttempts,
2304
2629
  delayMs,
2305
2630
  streaming,
2306
- ...completionRetryErrorAttributes(error)
2631
+ ...retryErrorAttributes(error)
2307
2632
  }
2308
2633
  });
2309
- await waitForCompletionRetry(delayMs);
2634
+ await waitForRetry(delayMs);
2310
2635
  }
2311
2636
  async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
2312
2637
  const executor = new ToolCallExecutor(
2313
2638
  this.agent,
2314
2639
  this.activeHook,
2315
- this.approvalOptions,
2640
+ (request) => this.suspendForApproval(request),
2641
+ this.activeLifecycle,
2316
2642
  {
2317
2643
  runId,
2318
2644
  sessionId: this.memoryContext?.sessionId,
@@ -2325,6 +2651,109 @@ var PromptRequest = class _PromptRequest {
2325
2651
  );
2326
2652
  return executor.execute(toolCalls, onResult, onStreamEvent, observation);
2327
2653
  }
2654
+ executeStreamingToolCalls(runId, toolCalls, newMessages, observation) {
2655
+ const events = createAsyncQueue();
2656
+ const results = this.executeToolCalls(
2657
+ runId,
2658
+ toolCalls,
2659
+ newMessages,
2660
+ (result) => events.enqueue(result),
2661
+ (event) => events.enqueue(event),
2662
+ observation
2663
+ );
2664
+ results.then(
2665
+ () => events.close(),
2666
+ (error) => events.throw(error)
2667
+ );
2668
+ return { events, results };
2669
+ }
2670
+ suspendForApproval(request) {
2671
+ if (this.cancellationError !== void 0) {
2672
+ return Promise.reject(this.cancellationError);
2673
+ }
2674
+ if (this.pendingApproval !== void 0) {
2675
+ throw new Error("Agent run already has a pending tool approval.");
2676
+ }
2677
+ return new Promise((resolve, reject) => {
2678
+ this.pendingApproval = { request, resolve, reject };
2679
+ this.approvalSignal.resolve();
2680
+ });
2681
+ }
2682
+ async closeActiveGeneration(error) {
2683
+ const active = this.activeGeneration;
2684
+ if (active === void 0) {
2685
+ return;
2686
+ }
2687
+ this.activeGeneration = void 0;
2688
+ await active.observers.error({ turn: active.turn, error });
2689
+ }
2690
+ approvalSnapshot() {
2691
+ const pending = this.pendingApproval;
2692
+ const runId = this.activeRunId;
2693
+ if (pending === void 0 || runId === void 0) {
2694
+ throw new TypeError("Agent run has no pending tool approval.");
2695
+ }
2696
+ const approval = {
2697
+ id: pending.request.id,
2698
+ toolName: pending.request.toolName,
2699
+ input: lifecycleSnapshot(pending.request.args),
2700
+ ...pending.request.toolCallId === void 0 ? {} : { toolCallId: pending.request.toolCallId },
2701
+ ...pending.request.reason === void 0 ? {} : { reason: pending.request.reason }
2702
+ };
2703
+ registerAgentApprovalRequestDetails(approval, pending.request);
2704
+ return {
2705
+ runId,
2706
+ approval,
2707
+ usage: lifecycleSnapshot(this.currentUsage),
2708
+ messages: lifecycleSnapshot(this.currentMessages)
2709
+ };
2710
+ }
2711
+ updateApprovalProgress(runId, usage, messages) {
2712
+ this.activeRunId = runId;
2713
+ this.currentUsage = usage;
2714
+ this.currentMessages = [...messages];
2715
+ }
2716
+ async runLifecycleFinish(result) {
2717
+ await this.activeLifecycle?.onFinish?.({
2718
+ runId: result.runId,
2719
+ output: result.output,
2720
+ usage: lifecycleSnapshot(result.usage),
2721
+ messages: lifecycleSnapshot(result.messages)
2722
+ });
2723
+ }
2724
+ async runLifecycleError(error, runId, usage, messages) {
2725
+ try {
2726
+ await this.activeLifecycle?.onError?.({
2727
+ runId,
2728
+ error: lifecycleSnapshot(error),
2729
+ usage: lifecycleSnapshot(usage),
2730
+ messages: lifecycleSnapshot([...this.chatHistory, ...messages])
2731
+ });
2732
+ return void 0;
2733
+ } catch (lifecycleError) {
2734
+ return lifecycleError;
2735
+ }
2736
+ }
2737
+ async reportRunFailure(error, runId, usage, messages, runObservers) {
2738
+ const reportedError = await this.resolveReportedRunError(error, runId, usage, messages);
2739
+ await settleFailureCleanup([
2740
+ () => runObservers?.error({
2741
+ error: reportedError,
2742
+ usage,
2743
+ messages: [...messages]
2744
+ }),
2745
+ () => this.memoryRecorder.recordError(runId, reportedError, messages)
2746
+ ]);
2747
+ return reportedError;
2748
+ }
2749
+ async resolveReportedRunError(error, runId, usage, messages) {
2750
+ try {
2751
+ await this.runRunErrorHook(error, usage, messages);
2752
+ } catch {
2753
+ }
2754
+ await this.runLifecycleError(error, runId, usage, messages);
2755
+ return error;
2756
+ }
2328
2757
  async runOutputGuardrailsForResponse(runId, usage, response, messages, runObservers) {
2329
2758
  const originalOutput = textFromAssistantContent(response.choice);
2330
2759
  const result = await runOutputGuardrails(this.guardrailPolicies, {
@@ -2388,11 +2817,12 @@ var PromptRequest = class _PromptRequest {
2388
2817
  }
2389
2818
  return context;
2390
2819
  }
2391
- async startRunObservers() {
2820
+ async startRunObservers(runId) {
2392
2821
  const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
2393
2822
  return startAgentRunObservers(
2394
2823
  this.agent.observers,
2395
2824
  {
2825
+ runId,
2396
2826
  agentName: this.agent.name,
2397
2827
  agentDescription: this.agent.description,
2398
2828
  instructions: this.agent.instructions,
@@ -2405,37 +2835,6 @@ var PromptRequest = class _PromptRequest {
2405
2835
  failOnObserverError
2406
2836
  );
2407
2837
  }
2408
- async recordAgentEvent(runId, event) {
2409
- const registration = this.agent.eventStore;
2410
- if (registration === void 0) {
2411
- return;
2412
- }
2413
- if (registration.options.include === "agent_tool_events" && event.type !== "agent_tool_event") {
2414
- return;
2415
- }
2416
- const turn = "turn" in event ? event.turn : void 0;
2417
- const agentId = event.type === "agent_tool_event" ? event.agentId : this.agent.id;
2418
- const agentName = event.type === "agent_tool_event" ? event.agentName : this.agent.name;
2419
- const input = {
2420
- runId,
2421
- agentId,
2422
- event
2423
- };
2424
- if (agentName !== void 0) {
2425
- input.agentName = agentName;
2426
- }
2427
- if (turn !== void 0) {
2428
- input.turn = turn;
2429
- }
2430
- if (event.type === "agent_tool_event") {
2431
- input.toolName = event.toolName;
2432
- input.internalCallId = event.internalCallId;
2433
- if (event.toolCallId !== void 0) {
2434
- input.toolCallId = event.toolCallId;
2435
- }
2436
- }
2437
- await registration.store.append(input);
2438
- }
2439
2838
  async runCompletionCallHook(prompt, history, newMessages) {
2440
2839
  const action = await this.activeHook?.onCompletionCall?.({
2441
2840
  prompt,
@@ -2476,9 +2875,8 @@ var PromptRequest = class _PromptRequest {
2476
2875
  run: runControl
2477
2876
  });
2478
2877
  if (action?.type === "terminate") {
2479
- return this.cancelled(newMessages, action.reason);
2878
+ this.cancelled(newMessages, action.reason);
2480
2879
  }
2481
- return error;
2482
2880
  }
2483
2881
  async runTurnStartHook(turn, prompt, history, newMessages) {
2484
2882
  const action = await this.activeHook?.onTurnStart?.({
@@ -2571,18 +2969,18 @@ var PromptRequest = class _PromptRequest {
2571
2969
  return;
2572
2970
  }
2573
2971
  if (this.runState === "running") {
2574
- throw new Error("PromptRequest is already running.");
2972
+ throw new Error("Agent stream is already running.");
2575
2973
  }
2576
- throw new Error("PromptRequest has already been used.");
2974
+ throw new Error("Agent stream has already been consumed.");
2577
2975
  }
2578
2976
  isTerminal() {
2579
2977
  return this.runState === "completed" || this.runState === "errored" || this.runState === "cancelled";
2580
2978
  }
2581
2979
  cancelled(newMessages, reason) {
2582
- return new PromptCancelledError([...this.chatHistory, ...newMessages], reason);
2980
+ return new AgentRunCancelledError([...this.chatHistory, ...newMessages], reason);
2583
2981
  }
2584
2982
  };
2585
- function normalizePromptInput(prompt) {
2983
+ function normalizeAgentInput(prompt) {
2586
2984
  if (typeof prompt === "string") {
2587
2985
  return { prompt: Message.user(prompt), history: [] };
2588
2986
  }
@@ -2590,11 +2988,11 @@ function normalizePromptInput(prompt) {
2590
2988
  return { prompt, history: [] };
2591
2989
  }
2592
2990
  if (prompt.length === 0) {
2593
- throw new TypeError("Prompt transcript must contain at least one message.");
2991
+ throw new TypeError("Agent input transcript must contain at least one message.");
2594
2992
  }
2595
2993
  const activePrompt = prompt.at(-1);
2596
2994
  if (activePrompt === void 0) {
2597
- throw new TypeError("Prompt transcript must contain at least one message.");
2995
+ throw new TypeError("Agent input transcript must contain at least one message.");
2598
2996
  }
2599
2997
  return {
2600
2998
  prompt: activePrompt,
@@ -2607,6 +3005,15 @@ function normalizeSteeringInput(input) {
2607
3005
  }
2608
3006
  return Array.isArray(input) ? [...input] : [input];
2609
3007
  }
3008
+ function normalizeRequestedRunId(runId) {
3009
+ if (runId === void 0) {
3010
+ return void 0;
3011
+ }
3012
+ if (typeof runId !== "string" || runId.trim().length === 0) {
3013
+ throw new TypeError("runId must be a non-empty string.");
3014
+ }
3015
+ return runId;
3016
+ }
2610
3017
  function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
2611
3018
  const events = [];
2612
3019
  for (const item of response.choice) {
@@ -2722,11 +3129,620 @@ function guardrailDecisionAttributes(decision) {
2722
3129
  return attributes;
2723
3130
  }
2724
3131
 
3132
+ // src/agent/ids.ts
3133
+ function normalizeAgentId(id) {
3134
+ if (typeof id !== "string") {
3135
+ throw new TypeError("Agent id must be a string.");
3136
+ }
3137
+ const normalized = id.trim();
3138
+ if (normalized.length === 0) {
3139
+ throw new TypeError("Agent id must be a non-empty string.");
3140
+ }
3141
+ return normalized;
3142
+ }
3143
+
3144
+ // src/agent/agent.ts
3145
+ var DEFAULT_MAX_TURNS = 20;
3146
+ var Agent = class {
3147
+ id;
3148
+ name;
3149
+ description;
3150
+ model;
3151
+ instructions;
3152
+ context;
3153
+ temperature;
3154
+ maxTokens;
3155
+ additionalParams;
3156
+ tools;
3157
+ toolChoice;
3158
+ defaultMaxTurns;
3159
+ lifecycle;
3160
+ outputSchema;
3161
+ observers;
3162
+ guardrails;
3163
+ middlewares;
3164
+ memory;
3165
+ constructor(options) {
3166
+ const resolved = resolveAgentOptions(options);
3167
+ this.id = normalizeAgentId(resolved.id);
3168
+ this.name = resolved.name;
3169
+ this.description = resolved.description;
3170
+ this.model = resolved.model;
3171
+ this.instructions = resolved.instructions;
3172
+ const context = (resolved.context ?? []).map(snapshotContextInput);
3173
+ assertValidContextIndexes(context);
3174
+ this.context = Object.freeze(context);
3175
+ this.temperature = resolved.temperature;
3176
+ this.maxTokens = resolved.maxTokens;
3177
+ this.additionalParams = cloneFrozenPlainData(resolved.additionalParams);
3178
+ const staticTools = dedupeTools(resolved.tools ?? []);
3179
+ const toolIndexes = (resolved.toolIndexes ?? []).map(snapshotToolIndex);
3180
+ assertUniqueIndexedToolNames(toolIndexes);
3181
+ const toolsByName = new Map(staticTools.map((tool) => [tool.name, tool]));
3182
+ for (const index of toolIndexes) {
3183
+ for (const tool of index.tools) {
3184
+ if (!toolsByName.has(tool.name)) {
3185
+ toolsByName.set(tool.name, tool);
3186
+ }
3187
+ }
3188
+ }
3189
+ this.tools = Object.freeze([...toolsByName.values()]);
3190
+ const publicToolState = Object.freeze({
3191
+ staticTools: Object.freeze(staticTools),
3192
+ providerTools: Object.freeze((resolved.providerTools ?? []).map(snapshotProviderTool)),
3193
+ toolIndexes: Object.freeze(toolIndexes)
3194
+ });
3195
+ registerAgentToolState(this, publicToolState, toolsByName);
3196
+ this.toolChoice = cloneFrozenPlainData(resolved.toolChoice);
3197
+ this.defaultMaxTurns = assertNonnegativeSafeInteger(
3198
+ resolved.defaultMaxTurns ?? DEFAULT_MAX_TURNS,
3199
+ "maxTurns"
3200
+ );
3201
+ this.lifecycle = resolved.lifecycle;
3202
+ this.outputSchema = cloneFrozenPlainData(resolved.outputSchema);
3203
+ this.observers = Object.freeze(
3204
+ (resolved.observers ?? []).map((registration) => Object.freeze({ ...registration }))
3205
+ );
3206
+ this.guardrails = Object.freeze((resolved.guardrails ?? []).map(snapshotGuardrailPolicy));
3207
+ this.middlewares = Object.freeze([...resolved.middlewares ?? []]);
3208
+ this.memory = snapshotMemoryRegistration(resolved.memory);
3209
+ }
3210
+ generate(input, options = {}) {
3211
+ return createGenerateExecution(this, AgentRun.fromAgent(this, input, options)).next();
3212
+ }
3213
+ stream(input, options = {}) {
3214
+ return createStreamExecution(
3215
+ this,
3216
+ AgentRun.fromAgent(this, input, options),
3217
+ options.includeToolCallDeltas !== false
3218
+ );
3219
+ }
3220
+ resume(pending, decision) {
3221
+ assertAgentApprovalDecision(decision);
3222
+ const continuation = approvalContinuations.get(pending);
3223
+ if (continuation === void 0 || continuation.agent !== this) {
3224
+ throw new TypeError("Approval continuation does not belong to this agent.");
3225
+ }
3226
+ approvalContinuations.delete(pending);
3227
+ continuation.run.resolveApproval(decision);
3228
+ return continuation.mode === "generate" ? continuation.execution.next() : continuation.execution.segment();
3229
+ }
3230
+ session(sessionId, options = {}) {
3231
+ if (this.memory === void 0) {
3232
+ throw new Error(`Agent "${this.id}" has no memory store configured.`);
3233
+ }
3234
+ const normalized = sessionId.trim();
3235
+ if (normalized.length === 0) {
3236
+ throw new TypeError("Session id must be a non-empty string.");
3237
+ }
3238
+ const context = {
3239
+ sessionId: normalized
3240
+ };
3241
+ if (options.userId !== void 0) {
3242
+ context.userId = options.userId;
3243
+ }
3244
+ if (options.metadata !== void 0) {
3245
+ context.metadata = cloneFrozenPlainData(options.metadata);
3246
+ }
3247
+ return new AgentSession(this, context);
3248
+ }
3249
+ asTool(options) {
3250
+ const description = options.description ?? this.description ?? `Prompt the ${options.name} agent.`;
3251
+ return createTool({
3252
+ name: options.name,
3253
+ description,
3254
+ inputSchema: z.object({
3255
+ prompt: z.string().describe("The prompt to send to the agent.")
3256
+ }),
3257
+ outputSchema: z.string(),
3258
+ execute: async ({ prompt }, context) => {
3259
+ if (options.stream === true && context.emitStreamEvent !== void 0 && this.model.capabilities.streaming && isStreamingCompletionModel(this.model)) {
3260
+ let output = "";
3261
+ const childStream = this.stream(prompt, {
3262
+ maxTurns: options.maxTurns,
3263
+ includeToolCallDeltas: context.includeToolCallDeltas !== false
3264
+ });
3265
+ for await (const event of childStream) {
3266
+ if (event.type === "approval_required") {
3267
+ await cancelAgentApproval(
3268
+ event,
3269
+ `Agent tool "${options.name}" cannot suspend for tool approval.`
3270
+ );
3271
+ throw new Error(
3272
+ `Agent tool "${options.name}" cannot suspend for tool approval. Run the agent directly to handle approvals.`
3273
+ );
3274
+ }
3275
+ const streamEvent = {
3276
+ agentId: this.id,
3277
+ event
3278
+ };
3279
+ if (this.name !== void 0) {
3280
+ streamEvent.agentName = this.name;
3281
+ }
3282
+ await context.emitStreamEvent(streamEvent);
3283
+ if (event.type === "final") {
3284
+ output = event.output;
3285
+ }
3286
+ }
3287
+ return output;
3288
+ }
3289
+ const response = await this.generate(prompt, { maxTurns: options.maxTurns });
3290
+ if (response.status === "approval_required") {
3291
+ await cancelAgentApproval(
3292
+ response,
3293
+ `Agent tool "${options.name}" cannot suspend for tool approval.`
3294
+ );
3295
+ throw new Error(
3296
+ `Agent tool "${options.name}" cannot suspend for tool approval. Run the agent directly to handle approvals.`
3297
+ );
3298
+ }
3299
+ return response.output;
3300
+ }
3301
+ });
3302
+ }
3303
+ getTool(toolName) {
3304
+ return getRegisteredAgentTool(this, toolName);
3305
+ }
3306
+ async callTool(toolName, args, context) {
3307
+ const tool = this.getTool(toolName);
3308
+ if (tool === void 0) {
3309
+ throw new ToolNotFoundError(toolName);
3310
+ }
3311
+ return prepareToolCall(tool, args).call(context ?? {});
3312
+ }
3313
+ };
3314
+ var resolvedAgentOptions = /* @__PURE__ */ Symbol("resolvedAgentOptions");
3315
+ function createResolvedAgent(options) {
3316
+ return new Agent({
3317
+ ...options,
3318
+ [resolvedAgentOptions]: true
3319
+ });
3320
+ }
3321
+ function getResolvedAgentOptions(agent) {
3322
+ const toolState = getAgentToolState(agent);
3323
+ return {
3324
+ id: agent.id,
3325
+ name: agent.name,
3326
+ description: agent.description,
3327
+ model: agent.model,
3328
+ instructions: agent.instructions,
3329
+ context: [...agent.context],
3330
+ temperature: agent.temperature,
3331
+ maxTokens: agent.maxTokens,
3332
+ additionalParams: agent.additionalParams,
3333
+ tools: [...toolState.staticTools],
3334
+ providerTools: [...toolState.providerTools],
3335
+ toolIndexes: [...toolState.toolIndexes],
3336
+ toolChoice: agent.toolChoice,
3337
+ defaultMaxTurns: agent.defaultMaxTurns,
3338
+ lifecycle: agent.lifecycle,
3339
+ outputSchema: agent.outputSchema,
3340
+ observers: [...agent.observers],
3341
+ guardrails: [...agent.guardrails],
3342
+ middlewares: [...agent.middlewares],
3343
+ memory: agent.memory
3344
+ };
3345
+ }
3346
+ function resolveAgentOptions(options) {
3347
+ if (isInternalAgentOptions(options)) {
3348
+ return options;
3349
+ }
3350
+ const toolsByName = /* @__PURE__ */ new Map();
3351
+ const providerTools = [];
3352
+ const toolIndexes = [];
3353
+ for (const tool of options.tools ?? []) {
3354
+ if (isProviderTool(tool)) {
3355
+ providerTools.push(tool);
3356
+ } else if (isToolIndex(tool)) {
3357
+ toolIndexes.push(tool);
3358
+ } else if (tool.kind === "tool-index") {
3359
+ throw new TypeError(
3360
+ "Invalid tool index: search, searchIds, asTool, tools, and a numeric topK are required."
3361
+ );
3362
+ } else {
3363
+ toolsByName.set(tool.name, tool);
3364
+ }
3365
+ }
3366
+ for (const server of options.mcpServers ?? []) {
3367
+ for (const tool of server.tools) {
3368
+ toolsByName.set(tool.name, tool);
3369
+ }
3370
+ }
3371
+ if (options.skills !== void 0) {
3372
+ for (const tool of options.skills.tools) {
3373
+ toolsByName.set(tool.name, tool);
3374
+ }
3375
+ }
3376
+ const memory = resolveAgentMemory(options);
3377
+ const instructions = [options.instructions, options.skills?.instructions].filter((part) => part !== void 0 && part.length > 0).join("\n\n");
3378
+ return {
3379
+ id: options.id,
3380
+ name: options.name,
3381
+ description: options.description,
3382
+ model: options.model,
3383
+ instructions: instructions.length === 0 ? void 0 : instructions,
3384
+ context: [...options.context ?? []],
3385
+ temperature: options.temperature,
3386
+ maxTokens: options.maxTokens,
3387
+ additionalParams: options.additionalParams,
3388
+ tools: [...toolsByName.values()],
3389
+ providerTools,
3390
+ toolIndexes,
3391
+ toolChoice: options.toolChoice,
3392
+ defaultMaxTurns: options.maxTurns,
3393
+ lifecycle: options.lifecycle,
3394
+ outputSchema: options.outputSchema === void 0 ? void 0 : toProviderJsonSchema(options.outputSchema),
3395
+ observers: (options.observers ?? []).map(
3396
+ (input) => "observer" in input ? { observer: input.observer, failOnObserverError: input.failOnObserverError } : { observer: input }
3397
+ ),
3398
+ guardrails: options.guardrails === void 0 ? [] : appendGuardrailPolicies([], options.guardrails),
3399
+ middlewares: [...options.middlewares ?? []],
3400
+ memory
3401
+ };
3402
+ }
3403
+ function assertUniqueIndexedToolNames(indexes) {
3404
+ const owners = /* @__PURE__ */ new Map();
3405
+ for (const [indexPosition, index] of indexes.entries()) {
3406
+ if (!isToolIndex(index)) {
3407
+ throw new TypeError(
3408
+ "Invalid tool index: search, searchIds, asTool, tools, and a numeric topK are required."
3409
+ );
3410
+ }
3411
+ assertPositiveSearchLimit(index.topK);
3412
+ assertFiniteSearchThreshold(index.threshold);
3413
+ for (const tool of index.tools) {
3414
+ const existingPosition = owners.get(tool.name);
3415
+ if (existingPosition !== void 0) {
3416
+ if (existingPosition === indexPosition) {
3417
+ throw new TypeError(
3418
+ `Tool "${tool.name}" is registered more than once by tool index ${indexPosition + 1}. Tool names must be unique within each index.`
3419
+ );
3420
+ }
3421
+ throw new TypeError(
3422
+ `Tool "${tool.name}" is registered by multiple tool indexes (${existingPosition + 1} and ${indexPosition + 1}). Tool names must be unique across indexes.`
3423
+ );
3424
+ }
3425
+ owners.set(tool.name, indexPosition);
3426
+ }
3427
+ }
3428
+ }
3429
+ function assertValidContextIndexes(inputs) {
3430
+ for (const input of inputs) {
3431
+ if (!isContextIndex(input)) continue;
3432
+ assertPositiveSearchLimit(input.topK);
3433
+ assertFiniteSearchThreshold(input.threshold);
3434
+ }
3435
+ }
3436
+ function isInternalAgentOptions(options) {
3437
+ return options[resolvedAgentOptions] === true;
3438
+ }
3439
+ function resolveAgentMemory(options) {
3440
+ if (options.memory === void 0) {
3441
+ return void 0;
3442
+ }
3443
+ const { store, ...memoryOptions } = options.memory;
3444
+ const resolvedOptions = resolveMemoryOptions(memoryOptions);
3445
+ if (resolvedOptions.compaction !== void 0 && store.compaction === void 0) {
3446
+ throw new TypeError(
3447
+ "Memory compaction requires a store with the optional compaction capability."
3448
+ );
3449
+ }
3450
+ return { store, options: resolvedOptions };
3451
+ }
3452
+ var approvalContinuations = /* @__PURE__ */ new WeakMap();
3453
+ function assertAgentApprovalDecision(decision) {
3454
+ if (typeof decision !== "object" || decision === null || typeof decision.approved !== "boolean") {
3455
+ throw new TypeError("Approval decision must include an approved boolean.");
3456
+ }
3457
+ if (decision.reason !== void 0 && typeof decision.reason !== "string") {
3458
+ throw new TypeError("Approval decision reason must be a string.");
3459
+ }
3460
+ }
3461
+ function createGenerateExecution(agent, run) {
3462
+ const completion = run.generate();
3463
+ const execution = {
3464
+ async next() {
3465
+ const outcome = await Promise.race([
3466
+ completion.then((result) => ({ type: "completed", result })),
3467
+ run.waitForApproval().then(() => ({ type: "approval" }))
3468
+ ]);
3469
+ if (outcome.type === "completed") {
3470
+ return outcome.result;
3471
+ }
3472
+ const pending = run.approvalResult();
3473
+ approvalContinuations.set(pending, { agent, run, mode: "generate", execution });
3474
+ return pending;
3475
+ },
3476
+ async cancel(reason) {
3477
+ run.cancel(reason);
3478
+ await completion.catch(() => void 0);
3479
+ }
3480
+ };
3481
+ return execution;
3482
+ }
3483
+ var StreamExecution = class {
3484
+ constructor(agent, run, includeToolCallDeltas) {
3485
+ this.agent = agent;
3486
+ this.run = run;
3487
+ this.iterator = run.events(includeToolCallDeltas)[Symbol.asyncIterator]();
3488
+ }
3489
+ agent;
3490
+ run;
3491
+ iterator;
3492
+ nextEvent;
3493
+ phase = "active";
3494
+ segment() {
3495
+ if (this.phase === "approval") {
3496
+ this.phase = "active";
3497
+ }
3498
+ return new DefaultAgentStream(this);
3499
+ }
3500
+ steer(input) {
3501
+ return this.run.steer(input);
3502
+ }
3503
+ async *events() {
3504
+ while (true) {
3505
+ this.nextEvent ??= this.iterator.next();
3506
+ let outcome;
3507
+ try {
3508
+ outcome = await Promise.race([
3509
+ this.nextEvent.then((event) => ({ type: "event", event })),
3510
+ this.run.waitForApproval().then(() => ({ type: "approval" }))
3511
+ ]);
3512
+ } catch (error) {
3513
+ this.phase = "terminal";
3514
+ throw error;
3515
+ }
3516
+ if (outcome.type === "approval") {
3517
+ this.phase = "approval";
3518
+ const pending = this.run.approvalEvent();
3519
+ approvalContinuations.set(pending, {
3520
+ agent: this.agent,
3521
+ run: this.run,
3522
+ mode: "stream",
3523
+ execution: this
3524
+ });
3525
+ yield pending;
3526
+ return;
3527
+ }
3528
+ this.nextEvent = void 0;
3529
+ if (outcome.event.done) {
3530
+ this.phase = "terminal";
3531
+ return;
3532
+ }
3533
+ yield outcome.event.value;
3534
+ }
3535
+ }
3536
+ shouldCancelActiveSegment() {
3537
+ return this.phase === "active";
3538
+ }
3539
+ async cancel(reason) {
3540
+ if (this.phase === "terminal" || this.phase === "cancelled") {
3541
+ return;
3542
+ }
3543
+ this.phase = "cancelled";
3544
+ this.run.cancel(reason);
3545
+ try {
3546
+ await this.iterator.return?.();
3547
+ } catch {
3548
+ }
3549
+ }
3550
+ };
3551
+ function createStreamExecution(agent, run, includeToolCallDeltas) {
3552
+ return new StreamExecution(agent, run, includeToolCallDeltas).segment();
3553
+ }
3554
+ var DefaultAgentStream = class {
3555
+ constructor(execution) {
3556
+ this.execution = execution;
3557
+ }
3558
+ execution;
3559
+ consuming = false;
3560
+ completed = false;
3561
+ steer(input) {
3562
+ return this.execution.steer(input);
3563
+ }
3564
+ [Symbol.asyncIterator]() {
3565
+ return this.consume()[Symbol.asyncIterator]();
3566
+ }
3567
+ async *consume() {
3568
+ if (this.completed) {
3569
+ throw new Error("Agent stream has already been consumed.");
3570
+ }
3571
+ if (this.consuming) {
3572
+ throw new Error("Agent stream is already running.");
3573
+ }
3574
+ this.consuming = true;
3575
+ try {
3576
+ for await (const event of this.execution.events()) {
3577
+ yield event;
3578
+ }
3579
+ } finally {
3580
+ if (this.execution.shouldCancelActiveSegment()) {
3581
+ await this.execution.cancel("Agent stream consumer closed the stream.");
3582
+ }
3583
+ this.consuming = false;
3584
+ this.completed = true;
3585
+ }
3586
+ }
3587
+ };
3588
+ async function cancelAgentApproval(pending, reason) {
3589
+ const continuation = approvalContinuations.get(pending);
3590
+ if (continuation === void 0) {
3591
+ return;
3592
+ }
3593
+ approvalContinuations.delete(pending);
3594
+ await continuation.execution.cancel(reason);
3595
+ }
3596
+ var AgentSession = class {
3597
+ constructor(agent, context) {
3598
+ this.agent = agent;
3599
+ this.context = context;
3600
+ }
3601
+ agent;
3602
+ context;
3603
+ generate(input, options = {}) {
3604
+ if (Array.isArray(input)) {
3605
+ throw new TypeError("AgentSession.generate does not accept Message[] transcripts.");
3606
+ }
3607
+ const run = AgentRun.fromAgent(this.agent, input, {
3608
+ ...options,
3609
+ memoryContext: this.context
3610
+ });
3611
+ return createGenerateExecution(this.agent, run).next();
3612
+ }
3613
+ stream(input, options = {}) {
3614
+ if (Array.isArray(input)) {
3615
+ throw new TypeError("AgentSession.stream does not accept Message[] transcripts.");
3616
+ }
3617
+ return createStreamExecution(
3618
+ this.agent,
3619
+ AgentRun.fromAgent(this.agent, input, { ...options, memoryContext: this.context }),
3620
+ options.includeToolCallDeltas !== false
3621
+ );
3622
+ }
3623
+ async messages() {
3624
+ const memory = this.agent.memory;
3625
+ if (memory === void 0) {
3626
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
3627
+ }
3628
+ return memory.store.load(this.context);
3629
+ }
3630
+ async contextUsage() {
3631
+ const messages = await this.messages();
3632
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
3633
+ const message = messages[index];
3634
+ if (message === void 0) {
3635
+ continue;
3636
+ }
3637
+ const generation = getAssistantGenerationMetadata(message);
3638
+ if (generation !== void 0) {
3639
+ return generation.contextUsage;
3640
+ }
3641
+ }
3642
+ return void 0;
3643
+ }
3644
+ async clear() {
3645
+ const memory = this.agent.memory;
3646
+ if (memory === void 0) {
3647
+ throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
3648
+ }
3649
+ await memory.store.clear(this.context);
3650
+ }
3651
+ };
3652
+ function snapshotContextInput(input) {
3653
+ if (!isContextIndex(input)) {
3654
+ return Object.freeze({
3655
+ id: input.id,
3656
+ text: input.text,
3657
+ ...input.additionalProps === void 0 ? {} : { additionalProps: cloneFrozenPlainData(input.additionalProps) }
3658
+ });
3659
+ }
3660
+ const format = input.format;
3661
+ return Object.freeze({
3662
+ kind: "context-index",
3663
+ index: input.index,
3664
+ topK: input.topK,
3665
+ ...input.threshold === void 0 ? {} : { threshold: input.threshold },
3666
+ ...input.filter === void 0 ? {} : { filter: cloneFrozenPlainData(input.filter) },
3667
+ ...format === void 0 ? {} : { format: (result) => format.call(input, result) }
3668
+ });
3669
+ }
3670
+ function snapshotProviderTool(tool) {
3671
+ return Object.freeze({
3672
+ ...tool,
3673
+ ...tool.configuration === void 0 ? {} : { configuration: cloneFrozenPlainData(tool.configuration) }
3674
+ });
3675
+ }
3676
+ function snapshotToolIndex(index) {
3677
+ const inspect = index.inspect;
3678
+ return Object.freeze({
3679
+ kind: "tool-index",
3680
+ tools: Object.freeze([...index.tools]),
3681
+ topK: index.topK,
3682
+ ...index.threshold === void 0 ? {} : { threshold: index.threshold },
3683
+ ...index.filter === void 0 ? {} : { filter: cloneFrozenPlainData(index.filter) },
3684
+ search: (request) => index.search(request),
3685
+ searchIds: (request) => index.searchIds(request),
3686
+ asTool: (options) => index.asTool(options),
3687
+ ...inspect === void 0 ? {} : { inspect: (request) => inspect.call(index, request) }
3688
+ });
3689
+ }
3690
+ function snapshotGuardrailPolicy(policy) {
3691
+ return Object.freeze({
3692
+ ...policy,
3693
+ input: Object.freeze([...policy.input]),
3694
+ output: Object.freeze([...policy.output])
3695
+ });
3696
+ }
3697
+ function snapshotMemoryRegistration(memory) {
3698
+ if (memory === void 0) {
3699
+ return void 0;
3700
+ }
3701
+ const compaction = memory.options.compaction === void 0 ? void 0 : Object.freeze({ ...memory.options.compaction });
3702
+ return Object.freeze({
3703
+ store: memory.store,
3704
+ options: Object.freeze({
3705
+ ...memory.options,
3706
+ ...compaction === void 0 ? {} : { compaction }
3707
+ })
3708
+ });
3709
+ }
3710
+ function cloneFrozenPlainData(value) {
3711
+ if (Array.isArray(value)) {
3712
+ return Object.freeze(value.map(cloneFrozenPlainData));
3713
+ }
3714
+ if (typeof value !== "object" || value === null) {
3715
+ return value;
3716
+ }
3717
+ const prototype = Object.getPrototypeOf(value);
3718
+ if (prototype !== Object.prototype && prototype !== null) {
3719
+ return value;
3720
+ }
3721
+ const clone = Object.fromEntries(
3722
+ Object.entries(value).map(([key, item]) => [key, cloneFrozenPlainData(item)])
3723
+ );
3724
+ return Object.freeze(clone);
3725
+ }
3726
+ function dedupeTools(tools) {
3727
+ const byName = /* @__PURE__ */ new Map();
3728
+ for (const tool of tools) {
3729
+ byName.set(tool.name, tool);
3730
+ }
3731
+ return [...byName.values()];
3732
+ }
3733
+
2725
3734
  export {
2726
3735
  MaxTurnsError,
2727
- PromptCancelledError,
2728
- ToolApprovalRequiredError,
2729
- extractRagText,
2730
- PromptRequest
3736
+ AgentRunCancelledError,
3737
+ getAgentToolState,
3738
+ createHook,
3739
+ getAgentApprovalRequestDetails,
3740
+ createContextIndex,
3741
+ isContextIndex,
3742
+ withInternalAgentRunOptions,
3743
+ Agent,
3744
+ createResolvedAgent,
3745
+ getResolvedAgentOptions,
3746
+ cancelAgentApproval
2731
3747
  };
2732
- //# sourceMappingURL=chunk-BALBBVI3.js.map
3748
+ //# sourceMappingURL=chunk-IL3ZSNJI.js.map