@anvia/core 0.11.1 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/agent/index.d.ts +13 -31
  2. package/dist/agent/index.js +12 -34
  3. package/dist/{agent-DoF-xWGj.d.ts → agent-Bq2KtSaw.d.ts} +8 -125
  4. package/dist/audio-generation/index.d.ts +2 -1
  5. package/dist/audio-generation/index.js +1 -1
  6. package/dist/audio-generation/index.js.map +1 -1
  7. package/dist/{chunk-T22FYPE2.js → chunk-5EDHXYH5.js} +6 -6
  8. package/dist/{chunk-M4T4ID34.js → chunk-62RUYGMM.js} +6 -6
  9. package/dist/chunk-62RUYGMM.js.map +1 -0
  10. package/dist/{chunk-AREAH2X5.js → chunk-DOS77JZC.js} +3 -239
  11. package/dist/chunk-DOS77JZC.js.map +1 -0
  12. package/dist/{chunk-W4DZU6IW.js → chunk-JS5MGVNM.js} +50 -48
  13. package/dist/chunk-JS5MGVNM.js.map +1 -0
  14. package/dist/{chunk-XXT2UCAR.js → chunk-M6RPFHG7.js} +2 -2
  15. package/dist/chunk-M6RPFHG7.js.map +1 -0
  16. package/dist/chunk-N6REB3DH.js +205 -0
  17. package/dist/chunk-N6REB3DH.js.map +1 -0
  18. package/dist/chunk-NPZDYOE6.js +53 -0
  19. package/dist/chunk-NPZDYOE6.js.map +1 -0
  20. package/dist/{chunk-VJ7AZVLS.js → chunk-R75SGHCI.js} +4 -4
  21. package/dist/chunk-R75SGHCI.js.map +1 -0
  22. package/dist/{chunk-IFQVPV32.js → chunk-SISUMRUX.js} +308 -539
  23. package/dist/chunk-SISUMRUX.js.map +1 -0
  24. package/dist/{chunk-246MPVBE.js → chunk-V3WTT7TE.js} +2 -2
  25. package/dist/{chunk-BVGDCFQW.js → chunk-X3SWV6W6.js} +6 -6
  26. package/dist/completion/index.d.ts +3 -3
  27. package/dist/{create-completion-7GNUka0b.d.ts → create-completion-BEbtU5pX.d.ts} +1 -1
  28. package/dist/embeddings/index.d.ts +6 -5
  29. package/dist/embeddings/index.js +1 -1
  30. package/dist/evals/index.d.ts +9 -9
  31. package/dist/evals/index.js +16 -14
  32. package/dist/evals/index.js.map +1 -1
  33. package/dist/extractor/index.d.ts +9 -9
  34. package/dist/extractor/index.js +12 -10
  35. package/dist/hooks/index.d.ts +12 -0
  36. package/dist/hooks/index.js +18 -0
  37. package/dist/hooks/index.js.map +1 -0
  38. package/dist/image-generation/index.d.ts +2 -1
  39. package/dist/image-generation/index.js +1 -1
  40. package/dist/image-generation/index.js.map +1 -1
  41. package/dist/index-CTNzKidE.d.ts +198 -0
  42. package/dist/index.d.ts +17 -15
  43. package/dist/index.js +24 -29
  44. package/dist/internal/agent.d.ts +9 -9
  45. package/dist/internal/agent.js +9 -7
  46. package/dist/loaders/index.d.ts +8 -7
  47. package/dist/loaders/index.js.map +1 -1
  48. package/dist/mcp/index.d.ts +4 -4
  49. package/dist/memory/index.d.ts +4 -40
  50. package/dist/memory/index.js +1 -1
  51. package/dist/{middleware-D7nRBusA.d.ts → middleware-CG7l0rR3.d.ts} +4 -4
  52. package/dist/model-listing/index.d.ts +12 -11
  53. package/dist/model-listing/index.js +1 -1
  54. package/dist/model-listing/index.js.map +1 -1
  55. package/dist/observability/index.d.ts +3 -138
  56. package/dist/observability/index.js.map +1 -1
  57. package/dist/pipeline/index.d.ts +9 -9
  58. package/dist/pipeline/index.js +3 -3
  59. package/dist/request/index.d.ts +27 -0
  60. package/dist/request/index.js +23 -0
  61. package/dist/request/index.js.map +1 -0
  62. package/dist/skills/index.d.ts +4 -4
  63. package/dist/skills/index.js +6 -6
  64. package/dist/{think-tool-DmBR-YzU.d.ts → think-tool-B8Rqw2i_.d.ts} +1 -1
  65. package/dist/tool/index.d.ts +6 -6
  66. package/dist/tool/index.js +5 -5
  67. package/dist/{tool-Hlcb459D.d.ts → tool-BLypI31e.d.ts} +1 -1
  68. package/dist/transcription/index.d.ts +1 -1
  69. package/dist/{types-CP3iyh6j.d.ts → types-C8loR4il.d.ts} +1 -1
  70. package/dist/types-CCxnnJjU.d.ts +114 -0
  71. package/dist/{types-CiFIAGqq.d.ts → types-CIwm9p4w.d.ts} +2 -2
  72. package/dist/types-CNfjQ5rB.d.ts +42 -0
  73. package/dist/{types-DrkBkjzk.d.ts → types-D-YTJdLj.d.ts} +1 -1
  74. package/dist/{types-IB2e9u5M.d.ts → types-DHHZztGO.d.ts} +1 -1
  75. package/dist/{types-B0cuzkjP.d.ts → types-KKjICcrA.d.ts} +2 -2
  76. package/dist/ui/index.d.ts +2 -28
  77. package/dist/ui/index.js +1 -11
  78. package/dist/vector-store/index.d.ts +5 -5
  79. package/dist/vector-store/index.js +4 -4
  80. package/package.json +10 -2
  81. package/dist/chunk-AREAH2X5.js.map +0 -1
  82. package/dist/chunk-IFQVPV32.js.map +0 -1
  83. package/dist/chunk-M4T4ID34.js.map +0 -1
  84. package/dist/chunk-VJ7AZVLS.js.map +0 -1
  85. package/dist/chunk-W4DZU6IW.js.map +0 -1
  86. package/dist/chunk-XXT2UCAR.js.map +0 -1
  87. package/dist/types-C2ArxcON.d.ts +0 -65
  88. /package/dist/{chunk-T22FYPE2.js.map → chunk-5EDHXYH5.js.map} +0 -0
  89. /package/dist/{chunk-246MPVBE.js.map → chunk-V3WTT7TE.js.map} +0 -0
  90. /package/dist/{chunk-BVGDCFQW.js.map → chunk-X3SWV6W6.js.map} +0 -0
@@ -2,19 +2,12 @@ import {
2
2
  toReadableStream
3
3
  } from "./chunk-XUUY2L2D.js";
4
4
  import {
5
- isSkillTool
6
- } from "./chunk-YK4WAAS4.js";
7
- import {
8
- ToolSet,
9
5
  parseToolArgs,
10
6
  toolResultContentToText
11
- } from "./chunk-BVGDCFQW.js";
12
- import {
13
- createTool
14
- } from "./chunk-T22FYPE2.js";
7
+ } from "./chunk-X3SWV6W6.js";
15
8
  import {
16
- compact
17
- } from "./chunk-MMHG7WAM.js";
9
+ mapWithConcurrency
10
+ } from "./chunk-OIMLU4SF.js";
18
11
  import {
19
12
  CompletionRequestBuilder,
20
13
  isStreamingCompletionModel
@@ -27,8 +20,12 @@ import {
27
20
  textFromAssistantContent
28
21
  } from "./chunk-WJO6NVE2.js";
29
22
  import {
30
- mapWithConcurrency
31
- } from "./chunk-OIMLU4SF.js";
23
+ runControl,
24
+ toolCallControl
25
+ } from "./chunk-NPZDYOE6.js";
26
+ import {
27
+ compact
28
+ } from "./chunk-MMHG7WAM.js";
32
29
 
33
30
  // src/request/errors.ts
34
31
  var MaxTurnsError = class extends Error {
@@ -62,50 +59,75 @@ var ToolApprovalRequiredError = class extends Error {
62
59
  request;
63
60
  };
64
61
 
65
- // src/hooks/index.ts
66
- function createHook(hook) {
67
- return hook;
68
- }
69
- function cancelPrompt(reason) {
70
- return { type: "terminate", reason };
71
- }
72
- function skipTool(reason) {
73
- return { type: "skip", reason };
74
- }
75
- function requestToolApproval(options = {}) {
76
- return compact({
77
- type: "approval_request",
78
- reason: options.reason,
79
- rejectMessage: options.rejectMessage
80
- });
81
- }
82
- var runControl = {
83
- continue() {
84
- return { type: "continue" };
85
- },
86
- cancel(reason) {
87
- return cancelPrompt(reason);
88
- }
89
- };
90
- var toolCallControl = {
91
- run() {
92
- return { type: "continue" };
93
- },
94
- skip(reason) {
95
- return skipTool(reason);
96
- },
97
- cancel(reason) {
98
- return { type: "terminate", reason };
99
- },
100
- requestApproval(options) {
101
- return requestToolApproval(options);
62
+ // src/internal/async-queue.ts
63
+ function createAsyncQueue() {
64
+ const values = [];
65
+ const waiters = [];
66
+ let closed = false;
67
+ let error;
68
+ function flush() {
69
+ while (waiters.length > 0 && values.length > 0) {
70
+ const waiter = waiters.shift();
71
+ const value = values.shift();
72
+ if (waiter !== void 0) {
73
+ waiter.resolve({ value, done: false });
74
+ }
75
+ }
76
+ if (values.length > 0 || waiters.length === 0 || !closed) {
77
+ return;
78
+ }
79
+ while (waiters.length > 0) {
80
+ const waiter = waiters.shift();
81
+ if (waiter === void 0) {
82
+ continue;
83
+ }
84
+ if (error !== void 0) {
85
+ waiter.reject(error);
86
+ } else {
87
+ waiter.resolve({ value: void 0, done: true });
88
+ }
89
+ }
102
90
  }
103
- };
104
-
105
- // src/agent/agent.ts
106
- import { z } from "zod";
91
+ return {
92
+ enqueue(value) {
93
+ if (closed) {
94
+ return;
95
+ }
96
+ values.push(value);
97
+ flush();
98
+ },
99
+ close() {
100
+ closed = true;
101
+ flush();
102
+ },
103
+ throw(thrown) {
104
+ closed = true;
105
+ error = thrown;
106
+ flush();
107
+ },
108
+ [Symbol.asyncIterator]() {
109
+ return {
110
+ next() {
111
+ if (values.length > 0) {
112
+ const value = values.shift();
113
+ return Promise.resolve({ value, done: false });
114
+ }
115
+ if (error !== void 0) {
116
+ return Promise.reject(error);
117
+ }
118
+ if (closed) {
119
+ return Promise.resolve({ value: void 0, done: true });
120
+ }
121
+ return new Promise((resolve, reject) => {
122
+ waiters.push({ resolve, reject });
123
+ });
124
+ }
125
+ };
126
+ }
127
+ };
128
+ }
107
129
 
108
- // src/request/memory.ts
130
+ // src/internal/prompt-runtime/memory.ts
109
131
  var PromptRequestMemory = class {
110
132
  constructor(agent, memoryContext, initialHistory) {
111
133
  this.agent = agent;
@@ -197,267 +219,7 @@ var PromptRequestMemory = class {
197
219
  }
198
220
  };
199
221
 
200
- // src/internal/async-queue.ts
201
- function createAsyncQueue() {
202
- const values = [];
203
- const waiters = [];
204
- let closed = false;
205
- let error;
206
- function flush() {
207
- while (waiters.length > 0 && values.length > 0) {
208
- const waiter = waiters.shift();
209
- const value = values.shift();
210
- if (waiter !== void 0) {
211
- waiter.resolve({ value, done: false });
212
- }
213
- }
214
- if (values.length > 0 || waiters.length === 0 || !closed) {
215
- return;
216
- }
217
- while (waiters.length > 0) {
218
- const waiter = waiters.shift();
219
- if (waiter === void 0) {
220
- continue;
221
- }
222
- if (error !== void 0) {
223
- waiter.reject(error);
224
- } else {
225
- waiter.resolve({ value: void 0, done: true });
226
- }
227
- }
228
- }
229
- return {
230
- enqueue(value) {
231
- if (closed) {
232
- return;
233
- }
234
- values.push(value);
235
- flush();
236
- },
237
- close() {
238
- closed = true;
239
- flush();
240
- },
241
- throw(thrown) {
242
- closed = true;
243
- error = thrown;
244
- flush();
245
- },
246
- [Symbol.asyncIterator]() {
247
- return {
248
- next() {
249
- if (values.length > 0) {
250
- const value = values.shift();
251
- return Promise.resolve({ value, done: false });
252
- }
253
- if (error !== void 0) {
254
- return Promise.reject(error);
255
- }
256
- if (closed) {
257
- return Promise.resolve({ value: void 0, done: true });
258
- }
259
- return new Promise((resolve, reject) => {
260
- waiters.push({ resolve, reject });
261
- });
262
- }
263
- };
264
- }
265
- };
266
- }
267
-
268
- // src/observability/group.ts
269
- async function startAgentRunObservers(registrations, args, failOnObserverError) {
270
- const runObservers = [];
271
- for (const registration of registrations) {
272
- try {
273
- const runObserver = await registration.observer.startRun(args);
274
- if (runObserver !== void 0) {
275
- runObservers.push(runObserver);
276
- }
277
- } catch (error) {
278
- if (failOnObserverError || registration.failOnObserverError === true) {
279
- throw error;
280
- }
281
- }
282
- }
283
- return new ActiveAgentRunObservers(runObservers, failOnObserverError);
284
- }
285
- var ActiveAgentRunObservers = class {
286
- constructor(runObservers, failOnObserverError) {
287
- this.runObservers = runObservers;
288
- this.failOnObserverError = failOnObserverError;
289
- this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
290
- }
291
- runObservers;
292
- failOnObserverError;
293
- trace;
294
- async startGeneration(args) {
295
- const generationObservers = [];
296
- for (const runObserver of this.runObservers) {
297
- if (runObserver.startGeneration === void 0) {
298
- continue;
299
- }
300
- try {
301
- const generationObserver = await runObserver.startGeneration(args);
302
- if (generationObserver !== void 0) {
303
- generationObservers.push(generationObserver);
304
- }
305
- } catch (error) {
306
- this.handleError(error);
307
- }
308
- }
309
- return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
310
- }
311
- async startTool(args) {
312
- const toolObservers = [];
313
- for (const runObserver of this.runObservers) {
314
- if (runObserver.startTool === void 0) {
315
- continue;
316
- }
317
- try {
318
- const toolObserver = await runObserver.startTool(args);
319
- if (toolObserver !== void 0) {
320
- toolObservers.push(toolObserver);
321
- }
322
- } catch (error) {
323
- this.handleError(error);
324
- }
325
- }
326
- return new ActiveToolObservers(toolObservers, this.failOnObserverError);
327
- }
328
- async end(args) {
329
- for (const runObserver of this.runObservers) {
330
- try {
331
- await runObserver.end(args);
332
- } catch (error) {
333
- this.handleError(error);
334
- }
335
- }
336
- }
337
- async error(args) {
338
- for (const runObserver of this.runObservers) {
339
- if (runObserver.error === void 0) {
340
- continue;
341
- }
342
- try {
343
- await runObserver.error(args);
344
- } catch (error) {
345
- this.handleError(error);
346
- }
347
- }
348
- }
349
- async event(args) {
350
- for (const runObserver of this.runObservers) {
351
- if (runObserver.event === void 0) {
352
- continue;
353
- }
354
- try {
355
- await runObserver.event(args);
356
- } catch (error) {
357
- this.handleError(error);
358
- }
359
- }
360
- }
361
- handleError(error) {
362
- if (this.failOnObserverError) {
363
- throw error;
364
- }
365
- }
366
- };
367
- var ActiveGenerationObservers = class {
368
- constructor(generationObservers, failOnObserverError) {
369
- this.generationObservers = generationObservers;
370
- this.failOnObserverError = failOnObserverError;
371
- }
372
- generationObservers;
373
- failOnObserverError;
374
- async end(args) {
375
- for (const observer of this.generationObservers) {
376
- try {
377
- await observer.end(args);
378
- } catch (error) {
379
- this.handleError(error);
380
- }
381
- }
382
- }
383
- async error(args) {
384
- for (const observer of this.generationObservers) {
385
- if (observer.error === void 0) {
386
- continue;
387
- }
388
- try {
389
- await observer.error(args);
390
- } catch (error) {
391
- this.handleError(error);
392
- }
393
- }
394
- }
395
- async update(args) {
396
- for (const observer of this.generationObservers) {
397
- if (observer.update === void 0) {
398
- continue;
399
- }
400
- try {
401
- await observer.update(args);
402
- } catch (error) {
403
- this.handleError(error);
404
- }
405
- }
406
- }
407
- handleError(error) {
408
- if (this.failOnObserverError) {
409
- throw error;
410
- }
411
- }
412
- };
413
- var ActiveToolObservers = class {
414
- constructor(toolObservers, failOnObserverError) {
415
- this.toolObservers = toolObservers;
416
- this.failOnObserverError = failOnObserverError;
417
- }
418
- toolObservers;
419
- failOnObserverError;
420
- async streamEvent(args) {
421
- for (const observer of this.toolObservers) {
422
- if (observer.streamEvent === void 0) {
423
- continue;
424
- }
425
- try {
426
- await observer.streamEvent(args);
427
- } catch (error) {
428
- this.handleError(error);
429
- }
430
- }
431
- }
432
- async end(args) {
433
- for (const observer of this.toolObservers) {
434
- try {
435
- await observer.end(args);
436
- } catch (error) {
437
- this.handleError(error);
438
- }
439
- }
440
- }
441
- async error(args) {
442
- for (const observer of this.toolObservers) {
443
- if (observer.error === void 0) {
444
- continue;
445
- }
446
- try {
447
- await observer.error(args);
448
- } catch (error) {
449
- this.handleError(error);
450
- }
451
- }
452
- }
453
- handleError(error) {
454
- if (this.failOnObserverError) {
455
- throw error;
456
- }
457
- }
458
- };
459
-
460
- // src/request/retrieval.ts
222
+ // src/internal/prompt-runtime/retrieval.ts
461
223
  async function fetchDynamicContext(agent, ragText) {
462
224
  if (ragText === void 0 || ragText.length === 0 || agent.dynamicContexts.length === 0) {
463
225
  return [];
@@ -519,28 +281,7 @@ function formatMetadata(metadata) {
519
281
  return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
520
282
  }
521
283
 
522
- // src/request/utils.ts
523
- function extractRagText(message) {
524
- if (message.role === "user") {
525
- return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
526
- }
527
- if (message.role === "tool") {
528
- return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
529
- }
530
- return void 0;
531
- }
532
- function parseJsonValue(text) {
533
- if (text.trim().length === 0) {
534
- return {};
535
- }
536
- try {
537
- return JSON.parse(text);
538
- } catch {
539
- return text;
540
- }
541
- }
542
-
543
- // src/request/stream-accumulator.ts
284
+ // src/internal/prompt-runtime/stream-accumulator.ts
544
285
  var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
545
286
  static defaultReasoningKey = "reasoning";
546
287
  text = "";
@@ -765,10 +506,38 @@ function isEmptyToolArguments(value) {
765
506
  if (typeof value === "object") {
766
507
  return Object.values(value).every((item) => item === void 0);
767
508
  }
768
- return false;
509
+ return false;
510
+ }
511
+ function parseJsonValue(text) {
512
+ if (text.trim().length === 0) {
513
+ return {};
514
+ }
515
+ try {
516
+ return JSON.parse(text);
517
+ } catch {
518
+ return text;
519
+ }
520
+ }
521
+
522
+ // src/internal/prompt-runtime/stream-events.ts
523
+ function addTurn(turn, event) {
524
+ if (event.type === "text_delta") {
525
+ return { type: "text_delta", turn, delta: event.delta };
526
+ }
527
+ if (event.type === "reasoning_delta") {
528
+ const mapped = { type: "reasoning_delta", turn, delta: event.delta };
529
+ if (event.id !== void 0) mapped.id = event.id;
530
+ if (event.contentType !== void 0) mapped.contentType = event.contentType;
531
+ if (event.signature !== void 0) mapped.signature = event.signature;
532
+ return mapped;
533
+ }
534
+ return { type: "tool_call", turn, toolCall: event.toolCall };
535
+ }
536
+ function isGenerationDeltaEvent(type) {
537
+ return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
769
538
  }
770
539
 
771
- // src/request/tool-execution.ts
540
+ // src/internal/prompt-runtime/tool-execution.ts
772
541
  var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
773
542
  var ToolCallExecutor = class {
774
543
  constructor(agent, activeHook, approvals, runContext, concurrency, requestMiddlewares, cancel) {
@@ -1118,23 +887,208 @@ function agentToolEventPayload(toolCall, internalCallId, event) {
1118
887
  });
1119
888
  }
1120
889
 
1121
- // src/request/types.ts
1122
- function addTurn(turn, event) {
1123
- if (event.type === "text_delta") {
1124
- return { type: "text_delta", turn, delta: event.delta };
890
+ // src/internal/rag-text.ts
891
+ function extractRagText(message) {
892
+ if (message.role === "user") {
893
+ return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
1125
894
  }
1126
- if (event.type === "reasoning_delta") {
1127
- const mapped = { type: "reasoning_delta", turn, delta: event.delta };
1128
- if (event.id !== void 0) mapped.id = event.id;
1129
- if (event.contentType !== void 0) mapped.contentType = event.contentType;
1130
- if (event.signature !== void 0) mapped.signature = event.signature;
1131
- return mapped;
895
+ if (message.role === "tool") {
896
+ return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
1132
897
  }
1133
- return { type: "tool_call", turn, toolCall: event.toolCall };
898
+ return void 0;
1134
899
  }
1135
- function isGenerationDeltaEvent(type) {
1136
- return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
900
+
901
+ // src/observability/group.ts
902
+ async function startAgentRunObservers(registrations, args, failOnObserverError) {
903
+ const runObservers = [];
904
+ for (const registration of registrations) {
905
+ try {
906
+ const runObserver = await registration.observer.startRun(args);
907
+ if (runObserver !== void 0) {
908
+ runObservers.push(runObserver);
909
+ }
910
+ } catch (error) {
911
+ if (failOnObserverError || registration.failOnObserverError === true) {
912
+ throw error;
913
+ }
914
+ }
915
+ }
916
+ return new ActiveAgentRunObservers(runObservers, failOnObserverError);
1137
917
  }
918
+ var ActiveAgentRunObservers = class {
919
+ constructor(runObservers, failOnObserverError) {
920
+ this.runObservers = runObservers;
921
+ this.failOnObserverError = failOnObserverError;
922
+ this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
923
+ }
924
+ runObservers;
925
+ failOnObserverError;
926
+ trace;
927
+ async startGeneration(args) {
928
+ const generationObservers = [];
929
+ for (const runObserver of this.runObservers) {
930
+ if (runObserver.startGeneration === void 0) {
931
+ continue;
932
+ }
933
+ try {
934
+ const generationObserver = await runObserver.startGeneration(args);
935
+ if (generationObserver !== void 0) {
936
+ generationObservers.push(generationObserver);
937
+ }
938
+ } catch (error) {
939
+ this.handleError(error);
940
+ }
941
+ }
942
+ return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
943
+ }
944
+ async startTool(args) {
945
+ const toolObservers = [];
946
+ for (const runObserver of this.runObservers) {
947
+ if (runObserver.startTool === void 0) {
948
+ continue;
949
+ }
950
+ try {
951
+ const toolObserver = await runObserver.startTool(args);
952
+ if (toolObserver !== void 0) {
953
+ toolObservers.push(toolObserver);
954
+ }
955
+ } catch (error) {
956
+ this.handleError(error);
957
+ }
958
+ }
959
+ return new ActiveToolObservers(toolObservers, this.failOnObserverError);
960
+ }
961
+ async end(args) {
962
+ for (const runObserver of this.runObservers) {
963
+ try {
964
+ await runObserver.end(args);
965
+ } catch (error) {
966
+ this.handleError(error);
967
+ }
968
+ }
969
+ }
970
+ async error(args) {
971
+ for (const runObserver of this.runObservers) {
972
+ if (runObserver.error === void 0) {
973
+ continue;
974
+ }
975
+ try {
976
+ await runObserver.error(args);
977
+ } catch (error) {
978
+ this.handleError(error);
979
+ }
980
+ }
981
+ }
982
+ async event(args) {
983
+ for (const runObserver of this.runObservers) {
984
+ if (runObserver.event === void 0) {
985
+ continue;
986
+ }
987
+ try {
988
+ await runObserver.event(args);
989
+ } catch (error) {
990
+ this.handleError(error);
991
+ }
992
+ }
993
+ }
994
+ handleError(error) {
995
+ if (this.failOnObserverError) {
996
+ throw error;
997
+ }
998
+ }
999
+ };
1000
+ var ActiveGenerationObservers = class {
1001
+ constructor(generationObservers, failOnObserverError) {
1002
+ this.generationObservers = generationObservers;
1003
+ this.failOnObserverError = failOnObserverError;
1004
+ }
1005
+ generationObservers;
1006
+ failOnObserverError;
1007
+ async end(args) {
1008
+ for (const observer of this.generationObservers) {
1009
+ try {
1010
+ await observer.end(args);
1011
+ } catch (error) {
1012
+ this.handleError(error);
1013
+ }
1014
+ }
1015
+ }
1016
+ async error(args) {
1017
+ for (const observer of this.generationObservers) {
1018
+ if (observer.error === void 0) {
1019
+ continue;
1020
+ }
1021
+ try {
1022
+ await observer.error(args);
1023
+ } catch (error) {
1024
+ this.handleError(error);
1025
+ }
1026
+ }
1027
+ }
1028
+ async update(args) {
1029
+ for (const observer of this.generationObservers) {
1030
+ if (observer.update === void 0) {
1031
+ continue;
1032
+ }
1033
+ try {
1034
+ await observer.update(args);
1035
+ } catch (error) {
1036
+ this.handleError(error);
1037
+ }
1038
+ }
1039
+ }
1040
+ handleError(error) {
1041
+ if (this.failOnObserverError) {
1042
+ throw error;
1043
+ }
1044
+ }
1045
+ };
1046
+ var ActiveToolObservers = class {
1047
+ constructor(toolObservers, failOnObserverError) {
1048
+ this.toolObservers = toolObservers;
1049
+ this.failOnObserverError = failOnObserverError;
1050
+ }
1051
+ toolObservers;
1052
+ failOnObserverError;
1053
+ async streamEvent(args) {
1054
+ for (const observer of this.toolObservers) {
1055
+ if (observer.streamEvent === void 0) {
1056
+ continue;
1057
+ }
1058
+ try {
1059
+ await observer.streamEvent(args);
1060
+ } catch (error) {
1061
+ this.handleError(error);
1062
+ }
1063
+ }
1064
+ }
1065
+ async end(args) {
1066
+ for (const observer of this.toolObservers) {
1067
+ try {
1068
+ await observer.end(args);
1069
+ } catch (error) {
1070
+ this.handleError(error);
1071
+ }
1072
+ }
1073
+ }
1074
+ async error(args) {
1075
+ for (const observer of this.toolObservers) {
1076
+ if (observer.error === void 0) {
1077
+ continue;
1078
+ }
1079
+ try {
1080
+ await observer.error(args);
1081
+ } catch (error) {
1082
+ this.handleError(error);
1083
+ }
1084
+ }
1085
+ }
1086
+ handleError(error) {
1087
+ if (this.failOnObserverError) {
1088
+ throw error;
1089
+ }
1090
+ }
1091
+ };
1138
1092
 
1139
1093
  // src/request/prompt-request.ts
1140
1094
  var PromptRequest = class _PromptRequest {
@@ -1817,196 +1771,11 @@ function responseStreamEvents(turn, response) {
1817
1771
  return events;
1818
1772
  }
1819
1773
 
1820
- // src/agent/ids.ts
1821
- function normalizeAgentId(id) {
1822
- if (typeof id !== "string") {
1823
- throw new TypeError("Agent id must be a string.");
1824
- }
1825
- const normalized = id.trim();
1826
- if (normalized.length === 0) {
1827
- throw new TypeError("Agent id must be a non-empty string.");
1828
- }
1829
- return normalized;
1830
- }
1831
-
1832
- // src/agent/agent.ts
1833
- var DEFAULT_MAX_TURNS = 20;
1834
- var Agent = class {
1835
- id;
1836
- name;
1837
- description;
1838
- model;
1839
- instructions;
1840
- staticContext;
1841
- temperature;
1842
- maxTokens;
1843
- additionalParams;
1844
- toolSet;
1845
- toolChoice;
1846
- defaultMaxTurns;
1847
- hook;
1848
- outputSchema;
1849
- observers;
1850
- approvals;
1851
- dynamicContexts;
1852
- dynamicTools;
1853
- middlewares;
1854
- /**
1855
- * @deprecated Use `middlewares` instead.
1856
- */
1857
- toolMiddlewares;
1858
- memory;
1859
- eventStore;
1860
- constructor(options) {
1861
- this.id = normalizeAgentId(options.id);
1862
- this.name = options.name;
1863
- this.description = options.description;
1864
- this.model = options.model;
1865
- this.instructions = options.instructions;
1866
- this.staticContext = options.staticContext ?? [];
1867
- this.temperature = options.temperature;
1868
- this.maxTokens = options.maxTokens;
1869
- this.additionalParams = options.additionalParams;
1870
- this.toolSet = options.toolSet ?? new ToolSet();
1871
- this.toolChoice = options.toolChoice;
1872
- this.defaultMaxTurns = options.defaultMaxTurns ?? DEFAULT_MAX_TURNS;
1873
- this.hook = options.hook;
1874
- this.outputSchema = options.outputSchema;
1875
- this.observers = options.observers ?? [];
1876
- this.approvals = options.approvals;
1877
- this.dynamicContexts = options.dynamicContexts ?? [];
1878
- this.dynamicTools = options.dynamicTools ?? [];
1879
- this.middlewares = options.middlewares ?? options.toolMiddlewares ?? [];
1880
- this.toolMiddlewares = this.middlewares;
1881
- this.memory = options.memory;
1882
- this.eventStore = options.eventStore;
1883
- }
1884
- prompt(prompt) {
1885
- return PromptRequest.fromAgent(this, prompt);
1886
- }
1887
- session(sessionId, options = {}) {
1888
- if (this.memory === void 0) {
1889
- throw new Error(`Agent "${this.id}" has no memory store configured.`);
1890
- }
1891
- const normalized = sessionId.trim();
1892
- if (normalized.length === 0) {
1893
- throw new TypeError("Session id must be a non-empty string.");
1894
- }
1895
- return new AgentSession(this, {
1896
- sessionId: normalized,
1897
- ...options.userId !== void 0 && { userId: options.userId },
1898
- ...options.metadata !== void 0 && { metadata: options.metadata }
1899
- });
1900
- }
1901
- asTool(options) {
1902
- const description = options.description ?? this.description ?? `Prompt the ${options.name} agent.`;
1903
- return createTool({
1904
- name: options.name,
1905
- description,
1906
- input: z.object({
1907
- prompt: z.string().describe("The prompt to send to the agent.")
1908
- }),
1909
- output: z.string(),
1910
- execute: async ({ prompt }, context) => {
1911
- const request = this.prompt(prompt);
1912
- const childRequest = options.maxTurns === void 0 ? request : request.maxTurns(options.maxTurns);
1913
- if (options.stream === true && context.emitStreamEvent !== void 0 && this.model.capabilities.streaming && isStreamingCompletionModel(this.model)) {
1914
- let output = "";
1915
- for await (const event of childRequest.stream()) {
1916
- await context.emitStreamEvent(
1917
- compact({
1918
- agentId: this.id,
1919
- agentName: this.name,
1920
- event
1921
- })
1922
- );
1923
- if (event.type === "final") {
1924
- output = event.output;
1925
- }
1926
- }
1927
- return output;
1928
- }
1929
- const response = await childRequest.send();
1930
- return response.output;
1931
- }
1932
- });
1933
- }
1934
- getTool(toolName) {
1935
- const staticTool = this.toolSet.get(toolName);
1936
- if (staticTool !== void 0) {
1937
- return staticTool;
1938
- }
1939
- for (const registration of this.dynamicTools) {
1940
- const dynamicTool = dynamicToolSetFromIndex(registration.index)?.get(toolName);
1941
- if (dynamicTool !== void 0) {
1942
- return dynamicTool;
1943
- }
1944
- }
1945
- return void 0;
1946
- }
1947
- async callTool(toolName, args, context) {
1948
- if (this.toolSet.contains(toolName)) {
1949
- return this.toolSet.call(toolName, args, context);
1950
- }
1951
- for (const registration of this.dynamicTools) {
1952
- const toolSet = dynamicToolSetFromIndex(registration.index);
1953
- if (toolSet?.contains(toolName)) {
1954
- return toolSet.call(toolName, args, context);
1955
- }
1956
- }
1957
- return this.toolSet.call(toolName, args, context);
1958
- }
1959
- shouldApplyToolMiddleware(toolName) {
1960
- return !isSkillTool(this.getTool(toolName));
1961
- }
1962
- };
1963
- var AgentSession = class {
1964
- constructor(agent, context) {
1965
- this.agent = agent;
1966
- this.context = context;
1967
- }
1968
- agent;
1969
- context;
1970
- prompt(prompt) {
1971
- if (Array.isArray(prompt)) {
1972
- throw new TypeError("AgentSession.prompt does not accept Message[] transcripts.");
1973
- }
1974
- return PromptRequest.fromAgent(this.agent, prompt, { memoryContext: this.context });
1975
- }
1976
- async messages() {
1977
- const memory = this.agent.memory;
1978
- if (memory === void 0) {
1979
- throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1980
- }
1981
- return memory.store.load(this.context);
1982
- }
1983
- async clear() {
1984
- const memory = this.agent.memory;
1985
- if (memory === void 0) {
1986
- throw new Error(`Agent "${this.agent.id}" has no memory store configured.`);
1987
- }
1988
- await memory.store.clear(this.context);
1989
- }
1990
- };
1991
- function dynamicToolSetFromIndex(index) {
1992
- const maybeIndex = index;
1993
- return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : void 0;
1994
- }
1995
-
1996
1774
  export {
1997
1775
  MaxTurnsError,
1998
1776
  PromptCancelledError,
1999
1777
  ToolApprovalRequiredError,
2000
- createHook,
2001
- cancelPrompt,
2002
- skipTool,
2003
- requestToolApproval,
2004
- runControl,
2005
- toolCallControl,
2006
1778
  extractRagText,
2007
- normalizeAgentId,
2008
- DEFAULT_MAX_TURNS,
2009
- Agent,
2010
- AgentSession
1779
+ PromptRequest
2011
1780
  };
2012
- //# sourceMappingURL=chunk-IFQVPV32.js.map
1781
+ //# sourceMappingURL=chunk-SISUMRUX.js.map