@anvia/core 0.1.0

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 (93) hide show
  1. package/README.md +101 -0
  2. package/dist/agent/index.d.ts +70 -0
  3. package/dist/agent/index.js +31 -0
  4. package/dist/agent/index.js.map +1 -0
  5. package/dist/agent-C6h6YrRU.d.ts +218 -0
  6. package/dist/audio-generation/index.d.ts +32 -0
  7. package/dist/audio-generation/index.js +9 -0
  8. package/dist/audio-generation/index.js.map +1 -0
  9. package/dist/chunk-7QI6ZAFI.js +61 -0
  10. package/dist/chunk-7QI6ZAFI.js.map +1 -0
  11. package/dist/chunk-A7VDIZQN.js +145 -0
  12. package/dist/chunk-A7VDIZQN.js.map +1 -0
  13. package/dist/chunk-B24Q2ZYM.js +43 -0
  14. package/dist/chunk-B24Q2ZYM.js.map +1 -0
  15. package/dist/chunk-B4QHQN5K.js +37 -0
  16. package/dist/chunk-B4QHQN5K.js.map +1 -0
  17. package/dist/chunk-CP47FBJV.js +324 -0
  18. package/dist/chunk-CP47FBJV.js.map +1 -0
  19. package/dist/chunk-FI2BTRT5.js +86 -0
  20. package/dist/chunk-FI2BTRT5.js.map +1 -0
  21. package/dist/chunk-GNWMOSNR.js +113 -0
  22. package/dist/chunk-GNWMOSNR.js.map +1 -0
  23. package/dist/chunk-I77SDTFE.js +43 -0
  24. package/dist/chunk-I77SDTFE.js.map +1 -0
  25. package/dist/chunk-IA76K5UX.js +142 -0
  26. package/dist/chunk-IA76K5UX.js.map +1 -0
  27. package/dist/chunk-IQBY2GCF.js +1207 -0
  28. package/dist/chunk-IQBY2GCF.js.map +1 -0
  29. package/dist/chunk-KSIY7KJA.js +387 -0
  30. package/dist/chunk-KSIY7KJA.js.map +1 -0
  31. package/dist/chunk-LMBOJMNB.js +304 -0
  32. package/dist/chunk-LMBOJMNB.js.map +1 -0
  33. package/dist/chunk-S55WOHX5.js +9 -0
  34. package/dist/chunk-S55WOHX5.js.map +1 -0
  35. package/dist/chunk-SRGJPXKT.js +256 -0
  36. package/dist/chunk-SRGJPXKT.js.map +1 -0
  37. package/dist/chunk-WZTPK5HV.js +125 -0
  38. package/dist/chunk-WZTPK5HV.js.map +1 -0
  39. package/dist/chunk-X6FBOU2P.js +96 -0
  40. package/dist/chunk-X6FBOU2P.js.map +1 -0
  41. package/dist/chunk-XUUY2L2D.js +42 -0
  42. package/dist/chunk-XUUY2L2D.js.map +1 -0
  43. package/dist/completion/index.d.ts +36 -0
  44. package/dist/completion/index.js +29 -0
  45. package/dist/completion/index.js.map +1 -0
  46. package/dist/embeddings/index.d.ts +34 -0
  47. package/dist/embeddings/index.js +23 -0
  48. package/dist/embeddings/index.js.map +1 -0
  49. package/dist/evals/index.d.ts +153 -0
  50. package/dist/evals/index.js +30 -0
  51. package/dist/evals/index.js.map +1 -0
  52. package/dist/extractor/index.d.ts +45 -0
  53. package/dist/extractor/index.js +19 -0
  54. package/dist/extractor/index.js.map +1 -0
  55. package/dist/image-generation/index.d.ts +37 -0
  56. package/dist/image-generation/index.js +9 -0
  57. package/dist/image-generation/index.js.map +1 -0
  58. package/dist/index.d.ts +24 -0
  59. package/dist/index.js +183 -0
  60. package/dist/index.js.map +1 -0
  61. package/dist/loaders/index.d.ts +82 -0
  62. package/dist/loaders/index.js +287 -0
  63. package/dist/loaders/index.js.map +1 -0
  64. package/dist/mcp/index.d.ts +15 -0
  65. package/dist/mcp/index.js +9 -0
  66. package/dist/mcp/index.js.map +1 -0
  67. package/dist/observability/index.d.ts +93 -0
  68. package/dist/observability/index.js +7 -0
  69. package/dist/observability/index.js.map +1 -0
  70. package/dist/pipeline/index.d.ts +51 -0
  71. package/dist/pipeline/index.js +9 -0
  72. package/dist/pipeline/index.js.map +1 -0
  73. package/dist/skills/index.d.ts +12 -0
  74. package/dist/skills/index.js +16 -0
  75. package/dist/skills/index.js.map +1 -0
  76. package/dist/streaming/index.d.ts +6 -0
  77. package/dist/streaming/index.js +7 -0
  78. package/dist/streaming/index.js.map +1 -0
  79. package/dist/tool/index.d.ts +42 -0
  80. package/dist/tool/index.js +33 -0
  81. package/dist/tool/index.js.map +1 -0
  82. package/dist/tool-DhuBQ3yb.d.ts +35 -0
  83. package/dist/transcription/index.d.ts +35 -0
  84. package/dist/transcription/index.js +9 -0
  85. package/dist/transcription/index.js.map +1 -0
  86. package/dist/types-B5B8Sdl4.d.ts +64 -0
  87. package/dist/types-BrxLd7ay.d.ts +250 -0
  88. package/dist/types-HvopERm0.d.ts +30 -0
  89. package/dist/vector-store/index.d.ts +111 -0
  90. package/dist/vector-store/index.js +15 -0
  91. package/dist/vector-store/index.js.map +1 -0
  92. package/dist/zod-schema-DJTEgQBq.d.ts +40 -0
  93. package/package.json +102 -0
@@ -0,0 +1,1207 @@
1
+ import {
2
+ toReadableStream
3
+ } from "./chunk-XUUY2L2D.js";
4
+ import {
5
+ ToolSet
6
+ } from "./chunk-GNWMOSNR.js";
7
+ import {
8
+ createTool,
9
+ toProviderJsonSchema
10
+ } from "./chunk-B4QHQN5K.js";
11
+ import {
12
+ CompletionRequestBuilder,
13
+ Message,
14
+ ToolContent,
15
+ Usage,
16
+ assertCompletionRequestSupported,
17
+ textFromAssistantContent
18
+ } from "./chunk-CP47FBJV.js";
19
+
20
+ // src/agent/agent.ts
21
+ import { z } from "zod";
22
+
23
+ // src/observability/group.ts
24
+ async function startAgentRunObservers(registrations, args, failOnObserverError) {
25
+ const runObservers = [];
26
+ for (const registration of registrations) {
27
+ try {
28
+ const runObserver = await registration.observer.startRun(args);
29
+ if (runObserver !== void 0) {
30
+ runObservers.push(runObserver);
31
+ }
32
+ } catch (error) {
33
+ if (failOnObserverError || registration.failOnObserverError === true) {
34
+ throw error;
35
+ }
36
+ }
37
+ }
38
+ return new ActiveAgentRunObservers(runObservers, failOnObserverError);
39
+ }
40
+ var ActiveAgentRunObservers = class {
41
+ constructor(runObservers, failOnObserverError) {
42
+ this.runObservers = runObservers;
43
+ this.failOnObserverError = failOnObserverError;
44
+ this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
45
+ }
46
+ runObservers;
47
+ failOnObserverError;
48
+ trace;
49
+ async startGeneration(args) {
50
+ const generationObservers = [];
51
+ for (const runObserver of this.runObservers) {
52
+ if (runObserver.startGeneration === void 0) {
53
+ continue;
54
+ }
55
+ try {
56
+ const generationObserver = await runObserver.startGeneration(args);
57
+ if (generationObserver !== void 0) {
58
+ generationObservers.push(generationObserver);
59
+ }
60
+ } catch (error) {
61
+ this.handleError(error);
62
+ }
63
+ }
64
+ return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
65
+ }
66
+ async startTool(args) {
67
+ const toolObservers = [];
68
+ for (const runObserver of this.runObservers) {
69
+ if (runObserver.startTool === void 0) {
70
+ continue;
71
+ }
72
+ try {
73
+ const toolObserver = await runObserver.startTool(args);
74
+ if (toolObserver !== void 0) {
75
+ toolObservers.push(toolObserver);
76
+ }
77
+ } catch (error) {
78
+ this.handleError(error);
79
+ }
80
+ }
81
+ return new ActiveToolObservers(toolObservers, this.failOnObserverError);
82
+ }
83
+ async end(args) {
84
+ for (const runObserver of this.runObservers) {
85
+ try {
86
+ await runObserver.end(args);
87
+ } catch (error) {
88
+ this.handleError(error);
89
+ }
90
+ }
91
+ }
92
+ async error(args) {
93
+ for (const runObserver of this.runObservers) {
94
+ if (runObserver.error === void 0) {
95
+ continue;
96
+ }
97
+ try {
98
+ await runObserver.error(args);
99
+ } catch (error) {
100
+ this.handleError(error);
101
+ }
102
+ }
103
+ }
104
+ handleError(error) {
105
+ if (this.failOnObserverError) {
106
+ throw error;
107
+ }
108
+ }
109
+ };
110
+ var ActiveGenerationObservers = class {
111
+ constructor(generationObservers, failOnObserverError) {
112
+ this.generationObservers = generationObservers;
113
+ this.failOnObserverError = failOnObserverError;
114
+ }
115
+ generationObservers;
116
+ failOnObserverError;
117
+ async end(args) {
118
+ for (const observer of this.generationObservers) {
119
+ try {
120
+ await observer.end(args);
121
+ } catch (error) {
122
+ this.handleError(error);
123
+ }
124
+ }
125
+ }
126
+ async error(args) {
127
+ for (const observer of this.generationObservers) {
128
+ if (observer.error === void 0) {
129
+ continue;
130
+ }
131
+ try {
132
+ await observer.error(args);
133
+ } catch (error) {
134
+ this.handleError(error);
135
+ }
136
+ }
137
+ }
138
+ handleError(error) {
139
+ if (this.failOnObserverError) {
140
+ throw error;
141
+ }
142
+ }
143
+ };
144
+ var ActiveToolObservers = class {
145
+ constructor(toolObservers, failOnObserverError) {
146
+ this.toolObservers = toolObservers;
147
+ this.failOnObserverError = failOnObserverError;
148
+ }
149
+ toolObservers;
150
+ failOnObserverError;
151
+ async end(args) {
152
+ for (const observer of this.toolObservers) {
153
+ try {
154
+ await observer.end(args);
155
+ } catch (error) {
156
+ this.handleError(error);
157
+ }
158
+ }
159
+ }
160
+ async error(args) {
161
+ for (const observer of this.toolObservers) {
162
+ if (observer.error === void 0) {
163
+ continue;
164
+ }
165
+ try {
166
+ await observer.error(args);
167
+ } catch (error) {
168
+ this.handleError(error);
169
+ }
170
+ }
171
+ }
172
+ handleError(error) {
173
+ if (this.failOnObserverError) {
174
+ throw error;
175
+ }
176
+ }
177
+ };
178
+
179
+ // src/agent/errors.ts
180
+ var MaxTurnsError = class extends Error {
181
+ constructor(maxTurns, chatHistory, prompt) {
182
+ super(`Reached max turn limit: ${maxTurns}`);
183
+ this.maxTurns = maxTurns;
184
+ this.chatHistory = chatHistory;
185
+ this.prompt = prompt;
186
+ this.name = "MaxTurnsError";
187
+ }
188
+ maxTurns;
189
+ chatHistory;
190
+ prompt;
191
+ };
192
+ var PromptCancelledError = class extends Error {
193
+ constructor(chatHistory, reason) {
194
+ super(`Prompt cancelled: ${reason}`);
195
+ this.chatHistory = chatHistory;
196
+ this.reason = reason;
197
+ this.name = "PromptCancelledError";
198
+ }
199
+ chatHistory;
200
+ reason;
201
+ };
202
+
203
+ // src/agent/hooks.ts
204
+ function createHook(hook) {
205
+ return hook;
206
+ }
207
+ function cancelPrompt(reason) {
208
+ return { type: "terminate", reason };
209
+ }
210
+ function skipTool(reason) {
211
+ return { type: "skip", reason };
212
+ }
213
+ var runControl = {
214
+ continue() {
215
+ return { type: "continue" };
216
+ },
217
+ cancel(reason) {
218
+ return cancelPrompt(reason);
219
+ }
220
+ };
221
+ var toolCallControl = {
222
+ run() {
223
+ return { type: "continue" };
224
+ },
225
+ skip(reason) {
226
+ return skipTool(reason);
227
+ },
228
+ cancel(reason) {
229
+ return { type: "terminate", reason };
230
+ }
231
+ };
232
+
233
+ // src/agent/utils.ts
234
+ function isStreamingCompletionModel(model) {
235
+ return "streamCompletion" in model && typeof model.streamCompletion === "function";
236
+ }
237
+ function extractRagText(message) {
238
+ if (message.role === "user") {
239
+ return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
240
+ }
241
+ if (message.role === "tool") {
242
+ return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
243
+ }
244
+ return void 0;
245
+ }
246
+ function parseJsonValue(text) {
247
+ if (text.trim().length === 0) {
248
+ return {};
249
+ }
250
+ try {
251
+ return JSON.parse(text);
252
+ } catch {
253
+ return text;
254
+ }
255
+ }
256
+ async function mapWithConcurrency(items, concurrency, mapper) {
257
+ const results = [];
258
+ let next = 0;
259
+ async function worker() {
260
+ while (next < items.length) {
261
+ const index = next;
262
+ next += 1;
263
+ const item = items[index];
264
+ if (item !== void 0) {
265
+ results[index] = await mapper(item);
266
+ }
267
+ }
268
+ }
269
+ const workerCount = Math.min(concurrency, items.length);
270
+ await Promise.all(Array.from({ length: workerCount }, () => worker()));
271
+ return results;
272
+ }
273
+
274
+ // src/agent/stream-accumulator.ts
275
+ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
276
+ static defaultReasoningKey = "reasoning";
277
+ text = "";
278
+ reasoningById = /* @__PURE__ */ new Map();
279
+ reasoningOrder = [];
280
+ toolCalls = /* @__PURE__ */ new Map();
281
+ toolCallOrder = [];
282
+ finalResponse;
283
+ messageId;
284
+ accept(event) {
285
+ if (event.type === "text_delta") {
286
+ this.text += event.delta;
287
+ return { type: "text_delta", delta: event.delta };
288
+ }
289
+ if (event.type === "reasoning_delta") {
290
+ const key = event.id ?? _CompletionStreamAccumulator.defaultReasoningKey;
291
+ const existing = this.reasoningById.get(key);
292
+ const reasoning = existing ?? { text: "" };
293
+ if (!existing) {
294
+ this.reasoningOrder.push(key);
295
+ }
296
+ this.appendReasoning(reasoning, event);
297
+ this.reasoningById.set(key, reasoning);
298
+ return reasoningDeltaEvent(event);
299
+ }
300
+ if (event.type === "tool_call_delta") {
301
+ const existing = this.toolCalls.get(event.id);
302
+ const toolCall = existing ?? {
303
+ id: event.id,
304
+ name: "",
305
+ argumentsText: ""
306
+ };
307
+ if (!existing) {
308
+ this.toolCallOrder.push(event.id);
309
+ }
310
+ if (event.callId !== void 0) toolCall.callId = event.callId;
311
+ if (event.name !== void 0) toolCall.name = event.name;
312
+ if (event.signature !== void 0) toolCall.signature = event.signature;
313
+ if (event.argumentsDelta !== void 0) {
314
+ toolCall.argumentsText += event.argumentsDelta;
315
+ }
316
+ this.toolCalls.set(event.id, toolCall);
317
+ return void 0;
318
+ }
319
+ if (event.type === "tool_call") {
320
+ this.upsertToolCall(event.toolCall);
321
+ return void 0;
322
+ }
323
+ if (event.type === "message_id") {
324
+ this.messageId = event.id;
325
+ return void 0;
326
+ }
327
+ if (event.type === "final") {
328
+ this.finalResponse = event.response;
329
+ return void 0;
330
+ }
331
+ return void 0;
332
+ }
333
+ response() {
334
+ if (this.finalResponse !== void 0) {
335
+ if (this.finalResponse.choice.length === 0) {
336
+ const response = {
337
+ ...this.buildAccumulatedResponse(),
338
+ usage: this.finalResponse.usage,
339
+ rawResponse: this.finalResponse.rawResponse
340
+ };
341
+ if (this.finalResponse.messageId !== void 0) {
342
+ response.messageId = this.finalResponse.messageId;
343
+ }
344
+ return response;
345
+ }
346
+ return this.finalResponse;
347
+ }
348
+ return this.buildAccumulatedResponse();
349
+ }
350
+ buildAccumulatedResponse() {
351
+ const choice = [];
352
+ if (this.text.length > 0) {
353
+ choice.push({ type: "text", text: this.text });
354
+ }
355
+ for (const key of this.reasoningOrder) {
356
+ const reasoning = this.reasoningById.get(key) ?? { text: "" };
357
+ const id = key === _CompletionStreamAccumulator.defaultReasoningKey ? void 0 : key;
358
+ const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
359
+ choice.push(id === void 0 ? content : { ...content, id });
360
+ }
361
+ for (const id of this.toolCallOrder) {
362
+ const toolCall = this.toolCalls.get(id);
363
+ if (toolCall !== void 0) {
364
+ const content = {
365
+ type: "tool_call",
366
+ id: toolCall.id,
367
+ function: {
368
+ name: toolCall.name,
369
+ arguments: parseJsonValue(toolCall.argumentsText)
370
+ }
371
+ };
372
+ if (toolCall.callId !== void 0) {
373
+ content.callId = toolCall.callId;
374
+ }
375
+ if (toolCall.signature !== void 0) {
376
+ content.signature = toolCall.signature;
377
+ }
378
+ choice.push(content);
379
+ }
380
+ }
381
+ const response = {
382
+ choice,
383
+ usage: Usage.empty(),
384
+ rawResponse: void 0
385
+ };
386
+ if (this.messageId !== void 0) {
387
+ response.messageId = this.messageId;
388
+ }
389
+ return response;
390
+ }
391
+ upsertToolCall(toolCall) {
392
+ if (!this.toolCalls.has(toolCall.id)) {
393
+ this.toolCallOrder.push(toolCall.id);
394
+ }
395
+ const partial = {
396
+ id: toolCall.id,
397
+ name: toolCall.function.name,
398
+ argumentsText: JSON.stringify(toolCall.function.arguments ?? {})
399
+ };
400
+ if (toolCall.callId !== void 0) {
401
+ partial.callId = toolCall.callId;
402
+ }
403
+ if (toolCall.signature !== void 0) {
404
+ partial.signature = toolCall.signature;
405
+ }
406
+ this.toolCalls.set(toolCall.id, partial);
407
+ }
408
+ appendReasoning(reasoning, event) {
409
+ const contentType = event.contentType ?? "text";
410
+ if (contentType === "text" || contentType === "summary") {
411
+ reasoning.text += event.delta;
412
+ }
413
+ if (event.contentType === void 0 && event.signature === void 0) {
414
+ return;
415
+ }
416
+ reasoning.content ??= [];
417
+ const last = reasoning.content.at(-1);
418
+ if (contentType === "text") {
419
+ if (last?.type === "text") {
420
+ last.text += event.delta;
421
+ if (event.signature !== void 0) {
422
+ last.signature = event.signature;
423
+ }
424
+ } else {
425
+ reasoning.content.push(
426
+ event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
427
+ );
428
+ }
429
+ return;
430
+ }
431
+ if (contentType === "summary") {
432
+ if (last?.type === "summary") {
433
+ last.text += event.delta;
434
+ } else {
435
+ reasoning.content.push({ type: "summary", text: event.delta });
436
+ }
437
+ return;
438
+ }
439
+ if (contentType === "encrypted") {
440
+ reasoning.content.push({ type: "encrypted", data: event.delta });
441
+ return;
442
+ }
443
+ reasoning.content.push({ type: "redacted", data: event.delta });
444
+ }
445
+ };
446
+ function reasoningDeltaEvent(event) {
447
+ const mapped = { type: "reasoning_delta", delta: event.delta };
448
+ if (event.id !== void 0) mapped.id = event.id;
449
+ if (event.contentType !== void 0) mapped.contentType = event.contentType;
450
+ if (event.signature !== void 0) mapped.signature = event.signature;
451
+ return mapped;
452
+ }
453
+
454
+ // src/agent/request.ts
455
+ var PromptRequest = class _PromptRequest {
456
+ constructor(agent, promptMessage) {
457
+ this.agent = agent;
458
+ this.promptMessage = promptMessage;
459
+ this.maxTurnCount = agent.defaultMaxTurns ?? 0;
460
+ this.activeHook = agent.hook;
461
+ }
462
+ agent;
463
+ promptMessage;
464
+ chatHistory;
465
+ maxTurnCount;
466
+ activeHook;
467
+ concurrency = 1;
468
+ traceOptions;
469
+ static fromAgent(agent, prompt) {
470
+ return new _PromptRequest(agent, typeof prompt === "string" ? Message.user(prompt) : prompt);
471
+ }
472
+ withHistory(history) {
473
+ this.chatHistory = history;
474
+ return this;
475
+ }
476
+ maxTurns(maxTurns) {
477
+ this.maxTurnCount = maxTurns;
478
+ return this;
479
+ }
480
+ requestHook(hook) {
481
+ this.activeHook = hook;
482
+ return this;
483
+ }
484
+ withToolConcurrency(concurrency) {
485
+ this.concurrency = Math.max(1, concurrency);
486
+ return this;
487
+ }
488
+ withTrace(trace) {
489
+ this.traceOptions = trace;
490
+ return this;
491
+ }
492
+ async send() {
493
+ const newMessages = [this.promptMessage];
494
+ let usage = Usage.empty();
495
+ let currentTurns = 0;
496
+ let lastPrompt = this.promptMessage;
497
+ const runObservers = await this.startRunObservers();
498
+ try {
499
+ while (currentTurns <= this.maxTurnCount + 1) {
500
+ const prompt = newMessages.at(-1);
501
+ if (prompt === void 0) {
502
+ throw new Error("PromptRequest requires at least one message");
503
+ }
504
+ lastPrompt = prompt;
505
+ currentTurns += 1;
506
+ const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
507
+ await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
508
+ const ragText = extractRagText(prompt);
509
+ const dynamicContext = await this.fetchDynamicContext(ragText);
510
+ const toolDefs = await this.fetchToolDefinitions(ragText);
511
+ const request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools(toolDefs).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
512
+ const response = await this.runCompletion(request, currentTurns, runObservers);
513
+ usage = Usage.add(usage, response.usage);
514
+ await this.runCompletionResponseHook(prompt, response, newMessages);
515
+ newMessages.push(Message.assistant(response.choice, response.messageId));
516
+ const toolCalls = response.choice.filter(
517
+ (item) => item.type === "tool_call"
518
+ );
519
+ if (toolCalls.length === 0) {
520
+ const result = {
521
+ output: textFromAssistantContent(response.choice),
522
+ usage,
523
+ messages: [...newMessages],
524
+ trace: runObservers.trace
525
+ };
526
+ await runObservers.end(result);
527
+ return result;
528
+ }
529
+ const toolResults = await this.executeToolCalls(toolCalls, newMessages, void 0, {
530
+ turn: currentTurns,
531
+ runObservers
532
+ });
533
+ newMessages.push(Message.tool(toolResults));
534
+ }
535
+ throw new MaxTurnsError(
536
+ this.maxTurnCount,
537
+ [...this.chatHistory ?? [], ...newMessages],
538
+ lastPrompt
539
+ );
540
+ } catch (error) {
541
+ await runObservers.error({ error, usage, messages: [...newMessages] });
542
+ throw error;
543
+ }
544
+ }
545
+ async *stream() {
546
+ if (!this.agent.model.capabilities.streaming || !isStreamingCompletionModel(this.agent.model)) {
547
+ throw new Error("This completion model does not support streaming");
548
+ }
549
+ const newMessages = [this.promptMessage];
550
+ let usage = Usage.empty();
551
+ let currentTurns = 0;
552
+ let lastPrompt = this.promptMessage;
553
+ const runObservers = await this.startRunObservers();
554
+ try {
555
+ while (currentTurns <= this.maxTurnCount + 1) {
556
+ const prompt = newMessages.at(-1);
557
+ if (prompt === void 0) {
558
+ throw new Error("PromptRequest requires at least one message");
559
+ }
560
+ lastPrompt = prompt;
561
+ currentTurns += 1;
562
+ const historyForRequest = [...this.chatHistory ?? [], ...newMessages.slice(0, -1)];
563
+ yield {
564
+ type: "turn_start",
565
+ turn: currentTurns,
566
+ prompt,
567
+ history: historyForRequest
568
+ };
569
+ await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
570
+ const ragText = extractRagText(prompt);
571
+ const dynamicContext = await this.fetchDynamicContext(ragText);
572
+ const toolDefs = await this.fetchToolDefinitions(ragText);
573
+ const request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools(toolDefs).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
574
+ assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
575
+ const generationObservers = await runObservers.startGeneration({
576
+ turn: currentTurns,
577
+ request
578
+ });
579
+ const accumulator = new CompletionStreamAccumulator();
580
+ const generationStartedAt = Date.now();
581
+ let firstDeltaMs;
582
+ try {
583
+ for await (const event of this.agent.model.streamCompletion(request)) {
584
+ if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
585
+ firstDeltaMs = Date.now() - generationStartedAt;
586
+ }
587
+ const mapped = accumulator.accept(event);
588
+ if (event.type === "error") {
589
+ throw event.error;
590
+ }
591
+ if (mapped !== void 0) {
592
+ yield addTurn(currentTurns, mapped);
593
+ }
594
+ }
595
+ } catch (error) {
596
+ await generationObservers.error({ turn: currentTurns, error });
597
+ throw error;
598
+ }
599
+ const response = accumulator.response();
600
+ await generationObservers.end({
601
+ turn: currentTurns,
602
+ response,
603
+ ...firstDeltaMs === void 0 ? {} : { firstDeltaMs }
604
+ });
605
+ usage = Usage.add(usage, response.usage);
606
+ await this.runCompletionResponseHook(prompt, response, newMessages);
607
+ newMessages.push(Message.assistant(response.choice, response.messageId));
608
+ const toolCalls = response.choice.filter(
609
+ (item) => item.type === "tool_call"
610
+ );
611
+ for (const toolCall of toolCalls) {
612
+ yield { type: "tool_call", turn: currentTurns, toolCall };
613
+ }
614
+ yield { type: "turn_end", turn: currentTurns, response };
615
+ if (toolCalls.length === 0) {
616
+ const output = textFromAssistantContent(response.choice);
617
+ yield {
618
+ type: "final",
619
+ output,
620
+ usage,
621
+ messages: [...newMessages],
622
+ trace: runObservers.trace
623
+ };
624
+ await runObservers.end({ output, usage, messages: [...newMessages] });
625
+ return;
626
+ }
627
+ const toolResultEvents = createAsyncQueue();
628
+ const toolResultsPromise = this.executeToolCalls(
629
+ toolCalls,
630
+ newMessages,
631
+ (result) => {
632
+ toolResultEvents.enqueue(result);
633
+ },
634
+ {
635
+ turn: currentTurns,
636
+ runObservers
637
+ }
638
+ );
639
+ toolResultsPromise.then(
640
+ () => toolResultEvents.close(),
641
+ (error) => toolResultEvents.throw(error)
642
+ );
643
+ for await (const result of toolResultEvents) {
644
+ yield { type: "tool_result", turn: currentTurns, ...result };
645
+ }
646
+ const toolResults = await toolResultsPromise;
647
+ newMessages.push(Message.tool(toolResults));
648
+ }
649
+ throw new MaxTurnsError(
650
+ this.maxTurnCount,
651
+ [...this.chatHistory ?? [], ...newMessages],
652
+ lastPrompt
653
+ );
654
+ } catch (error) {
655
+ await runObservers.error({ error, usage, messages: [...newMessages] });
656
+ yield { type: "error", error };
657
+ throw error;
658
+ }
659
+ }
660
+ readableStream() {
661
+ return toReadableStream(this.stream());
662
+ }
663
+ async runCompletion(request, turn, runObservers) {
664
+ assertCompletionRequestSupported(this.agent.model, request);
665
+ const generationObservers = await runObservers.startGeneration({ turn, request });
666
+ try {
667
+ const response = await this.agent.model.completion(request);
668
+ await generationObservers.end({ turn, response });
669
+ return response;
670
+ } catch (error) {
671
+ await generationObservers.error({ turn, error });
672
+ throw error;
673
+ }
674
+ }
675
+ async executeToolCalls(toolCalls, newMessages, onResult, observation) {
676
+ return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
677
+ const args = JSON.stringify(toolCall.function.arguments ?? {});
678
+ const internalCallId = globalThis.crypto.randomUUID();
679
+ const hookArgs = {
680
+ toolName: toolCall.function.name,
681
+ internalCallId,
682
+ args
683
+ };
684
+ if (toolCall.callId !== void 0) {
685
+ hookArgs.toolCallId = toolCall.callId;
686
+ }
687
+ const toolObservers = await observation?.runObservers.startTool({
688
+ turn: observation.turn,
689
+ toolCall,
690
+ toolName: toolCall.function.name,
691
+ internalCallId,
692
+ args,
693
+ toolCallId: toolCall.callId
694
+ });
695
+ const callAction = await this.activeHook?.onToolCall?.({
696
+ ...hookArgs,
697
+ tool: toolCallControl
698
+ });
699
+ if (callAction?.type === "terminate") {
700
+ await this.recordToolError(
701
+ toolObservers,
702
+ observation?.turn,
703
+ toolCall,
704
+ internalCallId,
705
+ args,
706
+ callAction.reason
707
+ );
708
+ throw this.cancelled(newMessages, callAction.reason);
709
+ }
710
+ let output;
711
+ let skipped = false;
712
+ if (callAction?.type === "skip") {
713
+ output = callAction.reason;
714
+ skipped = true;
715
+ } else {
716
+ try {
717
+ output = await this.agent.callTool(toolCall.function.name, args);
718
+ } catch (error) {
719
+ output = error instanceof Error ? error.toString() : String(error);
720
+ }
721
+ }
722
+ const resultAction = await this.activeHook?.onToolResult?.({
723
+ ...hookArgs,
724
+ result: output,
725
+ run: runControl
726
+ });
727
+ await toolObservers?.end({
728
+ turn: observation?.turn ?? 0,
729
+ toolCall,
730
+ toolName: toolCall.function.name,
731
+ internalCallId,
732
+ args,
733
+ result: output,
734
+ skipped,
735
+ toolCallId: toolCall.callId
736
+ });
737
+ if (resultAction?.type === "terminate") {
738
+ throw this.cancelled(newMessages, resultAction.reason);
739
+ }
740
+ const resultPayload = {
741
+ toolName: toolCall.function.name,
742
+ internalCallId,
743
+ args,
744
+ result: output
745
+ };
746
+ if (toolCall.callId !== void 0) {
747
+ resultPayload.toolCallId = toolCall.callId;
748
+ }
749
+ onResult?.(resultPayload);
750
+ return ToolContent.toolResult(toolCall.id, output, toolCall.callId);
751
+ });
752
+ }
753
+ async startRunObservers() {
754
+ const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
755
+ return startAgentRunObservers(
756
+ this.agent.observers,
757
+ {
758
+ agentName: this.agent.name,
759
+ agentDescription: this.agent.description,
760
+ instructions: this.agent.instructions,
761
+ trace: this.traceOptions,
762
+ prompt: this.promptMessage,
763
+ history: this.chatHistory ?? [],
764
+ maxTurns: this.maxTurnCount
765
+ },
766
+ failOnObserverError
767
+ );
768
+ }
769
+ async fetchDynamicContext(ragText) {
770
+ if (ragText === void 0 || ragText.length === 0 || this.agent.dynamicContexts.length === 0) {
771
+ return [];
772
+ }
773
+ const documents = [];
774
+ for (const registration of this.agent.dynamicContexts) {
775
+ const results = await registration.index.search({
776
+ query: ragText,
777
+ topK: registration.options.topK,
778
+ threshold: registration.options.threshold,
779
+ filter: registration.options.filter
780
+ });
781
+ for (const result of results) {
782
+ const formatted = registration.options.format?.(result);
783
+ if (formatted !== void 0) {
784
+ documents.push(formatted);
785
+ } else {
786
+ const metadata = formatMetadata(result.metadata);
787
+ documents.push({
788
+ id: result.id,
789
+ text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2),
790
+ ...metadata === void 0 ? {} : { additionalProps: metadata }
791
+ });
792
+ }
793
+ }
794
+ }
795
+ return documents;
796
+ }
797
+ async fetchToolDefinitions(ragText) {
798
+ const staticDefinitions = await this.agent.toolSet.getToolDefinitions(ragText);
799
+ if (ragText === void 0 || ragText.length === 0 || this.agent.dynamicTools.length === 0) {
800
+ return staticDefinitions;
801
+ }
802
+ const definitions = [...staticDefinitions];
803
+ const names = new Set(staticDefinitions.map((definition) => definition.name));
804
+ for (const registration of this.agent.dynamicTools) {
805
+ const results = await registration.index.search({
806
+ query: ragText,
807
+ topK: registration.options.topK,
808
+ threshold: registration.options.threshold,
809
+ filter: registration.options.filter
810
+ });
811
+ for (const result of results) {
812
+ if (names.has(result.document.toolName)) {
813
+ continue;
814
+ }
815
+ names.add(result.document.toolName);
816
+ definitions.push(result.document.definition);
817
+ }
818
+ }
819
+ return definitions;
820
+ }
821
+ async recordToolError(toolObservers, turn, toolCall, internalCallId, args, error) {
822
+ await toolObservers?.error({
823
+ turn: turn ?? 0,
824
+ toolCall,
825
+ toolName: toolCall.function.name,
826
+ internalCallId,
827
+ args,
828
+ error,
829
+ toolCallId: toolCall.callId
830
+ });
831
+ }
832
+ async runCompletionCallHook(prompt, history, newMessages) {
833
+ const action = await this.activeHook?.onCompletionCall?.({
834
+ prompt,
835
+ history,
836
+ run: runControl
837
+ });
838
+ if (action?.type === "terminate") {
839
+ throw this.cancelled(newMessages, action.reason);
840
+ }
841
+ }
842
+ async runCompletionResponseHook(prompt, response, newMessages) {
843
+ const action = await this.activeHook?.onCompletionResponse?.({
844
+ prompt,
845
+ response,
846
+ run: runControl
847
+ });
848
+ if (action?.type === "terminate") {
849
+ throw this.cancelled(newMessages, action.reason);
850
+ }
851
+ }
852
+ cancelled(newMessages, reason) {
853
+ return new PromptCancelledError([...this.chatHistory ?? [], ...newMessages], reason);
854
+ }
855
+ };
856
+ function createAsyncQueue() {
857
+ const values = [];
858
+ const waiters = [];
859
+ let closed = false;
860
+ let error;
861
+ function flush() {
862
+ while (waiters.length > 0 && values.length > 0) {
863
+ const waiter = waiters.shift();
864
+ const value = values.shift();
865
+ if (waiter !== void 0) {
866
+ waiter.resolve({ value, done: false });
867
+ }
868
+ }
869
+ if (values.length > 0 || waiters.length === 0 || !closed) {
870
+ return;
871
+ }
872
+ while (waiters.length > 0) {
873
+ const waiter = waiters.shift();
874
+ if (waiter === void 0) {
875
+ continue;
876
+ }
877
+ if (error !== void 0) {
878
+ waiter.reject(error);
879
+ } else {
880
+ waiter.resolve({ value: void 0, done: true });
881
+ }
882
+ }
883
+ }
884
+ return {
885
+ enqueue(value) {
886
+ if (closed) {
887
+ return;
888
+ }
889
+ values.push(value);
890
+ flush();
891
+ },
892
+ close() {
893
+ closed = true;
894
+ flush();
895
+ },
896
+ throw(thrown) {
897
+ closed = true;
898
+ error = thrown;
899
+ flush();
900
+ },
901
+ [Symbol.asyncIterator]() {
902
+ return {
903
+ next() {
904
+ if (values.length > 0) {
905
+ const value = values.shift();
906
+ return Promise.resolve({ value, done: false });
907
+ }
908
+ if (error !== void 0) {
909
+ return Promise.reject(error);
910
+ }
911
+ if (closed) {
912
+ return Promise.resolve({ value: void 0, done: true });
913
+ }
914
+ return new Promise((resolve, reject) => {
915
+ waiters.push({ resolve, reject });
916
+ });
917
+ }
918
+ };
919
+ }
920
+ };
921
+ }
922
+ function addTurn(turn, event) {
923
+ if (event.type === "text_delta") {
924
+ return { type: "text_delta", turn, delta: event.delta };
925
+ }
926
+ if (event.type === "reasoning_delta") {
927
+ const mapped = { type: "reasoning_delta", turn, delta: event.delta };
928
+ if (event.id !== void 0) mapped.id = event.id;
929
+ if (event.contentType !== void 0) mapped.contentType = event.contentType;
930
+ if (event.signature !== void 0) mapped.signature = event.signature;
931
+ return mapped;
932
+ }
933
+ return { type: "tool_call", turn, toolCall: event.toolCall };
934
+ }
935
+ function isGenerationDeltaEvent(type) {
936
+ return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
937
+ }
938
+ function formatMetadata(metadata) {
939
+ if (metadata === void 0) {
940
+ return void 0;
941
+ }
942
+ return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
943
+ }
944
+
945
+ // src/agent/agent.ts
946
+ var DEFAULT_MAX_TURNS = 20;
947
+ var Agent = class {
948
+ id;
949
+ name;
950
+ description;
951
+ model;
952
+ instructions;
953
+ staticContext;
954
+ temperature;
955
+ maxTokens;
956
+ additionalParams;
957
+ toolSet;
958
+ toolChoice;
959
+ defaultMaxTurns;
960
+ hook;
961
+ outputSchema;
962
+ observers;
963
+ dynamicContexts;
964
+ dynamicTools;
965
+ constructor(options) {
966
+ this.id = normalizeAgentId(options.id);
967
+ this.name = options.name;
968
+ this.description = options.description;
969
+ this.model = options.model;
970
+ this.instructions = options.instructions;
971
+ this.staticContext = options.staticContext ?? [];
972
+ this.temperature = options.temperature;
973
+ this.maxTokens = options.maxTokens;
974
+ this.additionalParams = options.additionalParams;
975
+ this.toolSet = options.toolSet ?? new ToolSet();
976
+ this.toolChoice = options.toolChoice;
977
+ this.defaultMaxTurns = options.defaultMaxTurns ?? DEFAULT_MAX_TURNS;
978
+ this.hook = options.hook;
979
+ this.outputSchema = options.outputSchema;
980
+ this.observers = options.observers ?? [];
981
+ this.dynamicContexts = options.dynamicContexts ?? [];
982
+ this.dynamicTools = options.dynamicTools ?? [];
983
+ }
984
+ prompt(prompt) {
985
+ return PromptRequest.fromAgent(this, prompt);
986
+ }
987
+ asTool(options) {
988
+ const description = options.description ?? this.description ?? `Prompt the ${options.name} agent.`;
989
+ return createTool({
990
+ name: options.name,
991
+ description,
992
+ input: z.object({
993
+ prompt: z.string().describe("The prompt to send to the agent.")
994
+ }),
995
+ output: z.string(),
996
+ execute: async ({ prompt }) => {
997
+ const request = this.prompt(prompt);
998
+ const response = options.maxTurns === void 0 ? await request.send() : await request.maxTurns(options.maxTurns).send();
999
+ return response.output;
1000
+ }
1001
+ });
1002
+ }
1003
+ getTool(toolName) {
1004
+ const staticTool = this.toolSet.get(toolName);
1005
+ if (staticTool !== void 0) {
1006
+ return staticTool;
1007
+ }
1008
+ for (const registration of this.dynamicTools) {
1009
+ const dynamicTool = dynamicToolSetFromIndex(registration.index)?.get(toolName);
1010
+ if (dynamicTool !== void 0) {
1011
+ return dynamicTool;
1012
+ }
1013
+ }
1014
+ return void 0;
1015
+ }
1016
+ async callTool(toolName, args) {
1017
+ if (this.toolSet.contains(toolName)) {
1018
+ return this.toolSet.call(toolName, args);
1019
+ }
1020
+ for (const registration of this.dynamicTools) {
1021
+ const toolSet = dynamicToolSetFromIndex(registration.index);
1022
+ if (toolSet?.contains(toolName)) {
1023
+ return toolSet.call(toolName, args);
1024
+ }
1025
+ }
1026
+ return this.toolSet.call(toolName, args);
1027
+ }
1028
+ };
1029
+ function dynamicToolSetFromIndex(index) {
1030
+ const maybeIndex = index;
1031
+ return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : void 0;
1032
+ }
1033
+ function normalizeAgentId(id) {
1034
+ if (typeof id !== "string") {
1035
+ throw new TypeError("Agent id must be a string.");
1036
+ }
1037
+ const normalized = id.trim();
1038
+ if (normalized.length === 0) {
1039
+ throw new TypeError("Agent id must be a non-empty string.");
1040
+ }
1041
+ return normalized;
1042
+ }
1043
+
1044
+ // src/agent/builder.ts
1045
+ var AgentBuilder = class {
1046
+ constructor(agentId, completionModel) {
1047
+ this.completionModel = completionModel;
1048
+ this.agentId = normalizeAgentId2(agentId);
1049
+ }
1050
+ completionModel;
1051
+ agentId;
1052
+ agentName;
1053
+ agentDescription;
1054
+ instructionBlocks = [];
1055
+ contextDocs = [];
1056
+ temp;
1057
+ maxTokenCount;
1058
+ params;
1059
+ choice;
1060
+ turns;
1061
+ requestHook;
1062
+ schema;
1063
+ skillInstructionBlocks = [];
1064
+ observerRegistrations = [];
1065
+ dynamicContextRegistrations = [];
1066
+ dynamicToolRegistrations = [];
1067
+ activeToolSet = new ToolSet();
1068
+ name(name) {
1069
+ this.agentName = name;
1070
+ return this;
1071
+ }
1072
+ description(description) {
1073
+ this.agentDescription = description;
1074
+ return this;
1075
+ }
1076
+ instructions(instructions) {
1077
+ if (instructions.length > 0) {
1078
+ this.instructionBlocks.push(instructions);
1079
+ }
1080
+ return this;
1081
+ }
1082
+ context(text, id = `static_doc_${this.contextDocs.length}`) {
1083
+ this.contextDocs.push({ id, text });
1084
+ return this;
1085
+ }
1086
+ dynamicContext(index, options) {
1087
+ this.dynamicContextRegistrations.push({ index, options });
1088
+ return this;
1089
+ }
1090
+ dynamicTools(index, options) {
1091
+ this.dynamicToolRegistrations.push({ index, options });
1092
+ return this;
1093
+ }
1094
+ tool(tool) {
1095
+ this.activeToolSet.addTool(tool);
1096
+ return this;
1097
+ }
1098
+ tools(tools) {
1099
+ this.activeToolSet.addTools(tools);
1100
+ return this;
1101
+ }
1102
+ mcp(servers) {
1103
+ for (const server of servers) {
1104
+ this.activeToolSet.addTools(server.tools);
1105
+ }
1106
+ return this;
1107
+ }
1108
+ skills(skillSet) {
1109
+ if (skillSet.instructions.length > 0) {
1110
+ this.skillInstructionBlocks.push(skillSet.instructions);
1111
+ }
1112
+ this.activeToolSet.addTools(skillSet.tools);
1113
+ return this;
1114
+ }
1115
+ useToolSet(toolSet) {
1116
+ toolSet.addTools(this.activeToolSet);
1117
+ this.activeToolSet = toolSet;
1118
+ return this;
1119
+ }
1120
+ temperature(temperature) {
1121
+ this.temp = temperature;
1122
+ return this;
1123
+ }
1124
+ maxTokens(maxTokens) {
1125
+ this.maxTokenCount = maxTokens;
1126
+ return this;
1127
+ }
1128
+ additionalParams(params) {
1129
+ this.params = params;
1130
+ return this;
1131
+ }
1132
+ toolChoice(toolChoice) {
1133
+ this.choice = toolChoice;
1134
+ return this;
1135
+ }
1136
+ defaultMaxTurns(defaultMaxTurns) {
1137
+ this.turns = defaultMaxTurns;
1138
+ return this;
1139
+ }
1140
+ hook(hook) {
1141
+ this.requestHook = hook;
1142
+ return this;
1143
+ }
1144
+ observe(observer, options = {}) {
1145
+ this.observerRegistrations.push({
1146
+ observer,
1147
+ failOnObserverError: options.failOnObserverError
1148
+ });
1149
+ return this;
1150
+ }
1151
+ outputSchema(schema) {
1152
+ this.schema = toProviderJsonSchema(schema);
1153
+ return this;
1154
+ }
1155
+ build() {
1156
+ return new Agent({
1157
+ id: this.agentId,
1158
+ name: this.agentName,
1159
+ description: this.agentDescription,
1160
+ model: this.completionModel,
1161
+ instructions: this.buildInstructions(),
1162
+ staticContext: this.contextDocs,
1163
+ temperature: this.temp,
1164
+ maxTokens: this.maxTokenCount,
1165
+ additionalParams: this.params,
1166
+ toolSet: this.activeToolSet,
1167
+ toolChoice: this.choice,
1168
+ defaultMaxTurns: this.turns,
1169
+ hook: this.requestHook,
1170
+ outputSchema: this.schema,
1171
+ observers: this.observerRegistrations,
1172
+ dynamicContexts: this.dynamicContextRegistrations,
1173
+ dynamicTools: this.dynamicToolRegistrations
1174
+ });
1175
+ }
1176
+ buildInstructions() {
1177
+ const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(
1178
+ (part) => part !== void 0 && part.length > 0
1179
+ );
1180
+ return parts.length === 0 ? void 0 : parts.join("\n\n");
1181
+ }
1182
+ };
1183
+ function normalizeAgentId2(id) {
1184
+ if (typeof id !== "string") {
1185
+ throw new TypeError("Agent id must be a string.");
1186
+ }
1187
+ const normalized = id.trim();
1188
+ if (normalized.length === 0) {
1189
+ throw new TypeError("Agent id must be a non-empty string.");
1190
+ }
1191
+ return normalized;
1192
+ }
1193
+
1194
+ export {
1195
+ MaxTurnsError,
1196
+ PromptCancelledError,
1197
+ createHook,
1198
+ cancelPrompt,
1199
+ skipTool,
1200
+ runControl,
1201
+ toolCallControl,
1202
+ PromptRequest,
1203
+ DEFAULT_MAX_TURNS,
1204
+ Agent,
1205
+ AgentBuilder
1206
+ };
1207
+ //# sourceMappingURL=chunk-IQBY2GCF.js.map