@anvia/core 0.3.1 → 0.4.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.
- package/README.md +7 -8
- package/dist/agent/index.d.ts +10 -10
- package/dist/agent/index.js +10 -18
- package/dist/{agent-D_jXqPAZ.d.ts → agent-B-ls5y_g.d.ts} +6 -19
- package/dist/audio-generation/index.d.ts +1 -1
- package/dist/audio-generation/index.js +37 -4
- package/dist/audio-generation/index.js.map +1 -1
- package/dist/{chunk-QRZ2HPUX.js → chunk-4PHDOEKY.js} +3 -3
- package/dist/chunk-4PHDOEKY.js.map +1 -0
- package/dist/chunk-BD66WU2B.js +188 -0
- package/dist/chunk-BD66WU2B.js.map +1 -0
- package/dist/{chunk-I2HOMD3R.js → chunk-CY7SB5FG.js} +3 -3
- package/dist/{chunk-A7VDIZQN.js → chunk-EFGX3EX5.js} +5 -14
- package/dist/chunk-EFGX3EX5.js.map +1 -0
- package/dist/{chunk-TILE6Z2N.js → chunk-L6JCKFAX.js} +2 -2
- package/dist/{chunk-65QV627O.js → chunk-OIA4CN5V.js} +2 -2
- package/dist/chunk-OIMLU4SF.js +20 -0
- package/dist/chunk-OIMLU4SF.js.map +1 -0
- package/dist/{chunk-GSFQZYS7.js → chunk-TVIADATY.js} +579 -723
- package/dist/chunk-TVIADATY.js.map +1 -0
- package/dist/completion/index.d.ts +2 -2
- package/dist/embeddings/index.d.ts +4 -24
- package/dist/embeddings/index.js +2 -1
- package/dist/evals/index.d.ts +26 -21
- package/dist/evals/index.js +298 -15
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +6 -5
- package/dist/extractor/index.js +8 -6
- package/dist/image-generation/index.d.ts +1 -1
- package/dist/image-generation/index.js +37 -4
- package/dist/image-generation/index.js.map +1 -1
- package/dist/index.d.ts +12 -21
- package/dist/index.js +14 -160
- package/dist/internal/agent.d.ts +8 -0
- package/dist/internal/agent.js +18 -0
- package/dist/internal/agent.js.map +1 -0
- package/dist/loaders/index.d.ts +1 -1
- package/dist/mcp/index.d.ts +4 -4
- package/dist/mcp/index.js +140 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory/index.d.ts +1 -1
- package/dist/{middleware-2FR89muX.d.ts → middleware-CGiEIaBx.d.ts} +4 -7
- package/dist/model-listing/index.js +13 -3
- package/dist/model-listing/index.js.map +1 -1
- package/dist/observability/index.d.ts +2 -2
- package/dist/observability/index.js +4 -3
- package/dist/observability/index.js.map +1 -1
- package/dist/pipeline/index.d.ts +9 -6
- package/dist/pipeline/index.js +334 -3
- package/dist/pipeline/index.js.map +1 -1
- package/dist/skills/index.d.ts +4 -4
- package/dist/skills/index.js +5 -4
- package/dist/think-tool-ByArsrxe.d.ts +26 -0
- package/dist/tool/index.d.ts +8 -29
- package/dist/tool/index.js +4 -3
- package/dist/{tool-ClZYES-Z.d.ts → tool-C3ciF-VG.d.ts} +2 -2
- package/dist/transcription/index.d.ts +1 -1
- package/dist/transcription/index.js +55 -4
- package/dist/transcription/index.js.map +1 -1
- package/dist/{types-BM8-Y8Hy.d.ts → types-C54aNoCd.d.ts} +1 -1
- package/dist/types-IB2e9u5M.d.ts +25 -0
- package/dist/{types-B0iXLkqi.d.ts → types-T9rlOIUc.d.ts} +2 -2
- package/dist/{types-fLi8uM5R.d.ts → types-UhcgW8Fo.d.ts} +3 -3
- package/dist/vector-store/index.d.ts +3 -3
- package/dist/vector-store/index.js +3 -2
- package/dist/zod-schema-C7F4clpm.d.ts +5 -0
- package/package.json +6 -2
- package/dist/chunk-6JCCM2K4.js +0 -304
- package/dist/chunk-6JCCM2K4.js.map +0 -1
- package/dist/chunk-7QI6ZAFI.js +0 -61
- package/dist/chunk-7QI6ZAFI.js.map +0 -1
- package/dist/chunk-A7VDIZQN.js.map +0 -1
- package/dist/chunk-B24Q2ZYM.js +0 -43
- package/dist/chunk-B24Q2ZYM.js.map +0 -1
- package/dist/chunk-GSFQZYS7.js.map +0 -1
- package/dist/chunk-I77SDTFE.js +0 -43
- package/dist/chunk-I77SDTFE.js.map +0 -1
- package/dist/chunk-NYUOBGVI.js +0 -146
- package/dist/chunk-NYUOBGVI.js.map +0 -1
- package/dist/chunk-QRZ2HPUX.js.map +0 -1
- package/dist/chunk-RTOGIJH2.js +0 -345
- package/dist/chunk-RTOGIJH2.js.map +0 -1
- package/dist/chunk-WJT22R3T.js +0 -18
- package/dist/chunk-WJT22R3T.js.map +0 -1
- package/dist/chunk-ZSYIQYTV.js +0 -9
- package/dist/chunk-ZSYIQYTV.js.map +0 -1
- /package/dist/{chunk-I2HOMD3R.js.map → chunk-CY7SB5FG.js.map} +0 -0
- /package/dist/{chunk-TILE6Z2N.js.map → chunk-L6JCKFAX.js.map} +0 -0
- /package/dist/{chunk-65QV627O.js.map → chunk-OIA4CN5V.js.map} +0 -0
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
toReadableStream
|
|
3
|
+
} from "./chunk-XUUY2L2D.js";
|
|
4
4
|
import {
|
|
5
5
|
isSkillTool
|
|
6
6
|
} from "./chunk-YK4WAAS4.js";
|
|
7
7
|
import {
|
|
8
8
|
ToolSet,
|
|
9
9
|
toolResultContentToText
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-CY7SB5FG.js";
|
|
11
11
|
import {
|
|
12
|
-
createTool
|
|
13
|
-
|
|
14
|
-
} from "./chunk-65QV627O.js";
|
|
15
|
-
import {
|
|
16
|
-
toReadableStream
|
|
17
|
-
} from "./chunk-XUUY2L2D.js";
|
|
12
|
+
createTool
|
|
13
|
+
} from "./chunk-OIA4CN5V.js";
|
|
18
14
|
import {
|
|
19
15
|
CompletionRequestBuilder,
|
|
20
16
|
Message,
|
|
@@ -23,10 +19,157 @@ import {
|
|
|
23
19
|
assertCompletionRequestSupported,
|
|
24
20
|
textFromAssistantContent
|
|
25
21
|
} from "./chunk-6GJDBBDC.js";
|
|
22
|
+
import {
|
|
23
|
+
mapWithConcurrency
|
|
24
|
+
} from "./chunk-OIMLU4SF.js";
|
|
25
|
+
|
|
26
|
+
// src/agent/errors.ts
|
|
27
|
+
var MaxTurnsError = class extends Error {
|
|
28
|
+
constructor(maxTurns, chatHistory, prompt) {
|
|
29
|
+
super(`Reached max turn limit: ${maxTurns}`);
|
|
30
|
+
this.maxTurns = maxTurns;
|
|
31
|
+
this.chatHistory = chatHistory;
|
|
32
|
+
this.prompt = prompt;
|
|
33
|
+
this.name = "MaxTurnsError";
|
|
34
|
+
}
|
|
35
|
+
maxTurns;
|
|
36
|
+
chatHistory;
|
|
37
|
+
prompt;
|
|
38
|
+
};
|
|
39
|
+
var PromptCancelledError = class extends Error {
|
|
40
|
+
constructor(chatHistory, reason) {
|
|
41
|
+
super(`Prompt cancelled: ${reason}`);
|
|
42
|
+
this.chatHistory = chatHistory;
|
|
43
|
+
this.reason = reason;
|
|
44
|
+
this.name = "PromptCancelledError";
|
|
45
|
+
}
|
|
46
|
+
chatHistory;
|
|
47
|
+
reason;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/agent/hooks.ts
|
|
51
|
+
function createHook(hook) {
|
|
52
|
+
return hook;
|
|
53
|
+
}
|
|
54
|
+
function cancelPrompt(reason) {
|
|
55
|
+
return { type: "terminate", reason };
|
|
56
|
+
}
|
|
57
|
+
function skipTool(reason) {
|
|
58
|
+
return { type: "skip", reason };
|
|
59
|
+
}
|
|
60
|
+
function requestToolApproval(options = {}) {
|
|
61
|
+
return {
|
|
62
|
+
type: "approval_request",
|
|
63
|
+
...options.reason === void 0 ? {} : { reason: options.reason },
|
|
64
|
+
...options.rejectMessage === void 0 ? {} : { rejectMessage: options.rejectMessage }
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
var runControl = {
|
|
68
|
+
continue() {
|
|
69
|
+
return { type: "continue" };
|
|
70
|
+
},
|
|
71
|
+
cancel(reason) {
|
|
72
|
+
return cancelPrompt(reason);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var toolCallControl = {
|
|
76
|
+
run() {
|
|
77
|
+
return { type: "continue" };
|
|
78
|
+
},
|
|
79
|
+
skip(reason) {
|
|
80
|
+
return skipTool(reason);
|
|
81
|
+
},
|
|
82
|
+
cancel(reason) {
|
|
83
|
+
return { type: "terminate", reason };
|
|
84
|
+
},
|
|
85
|
+
requestApproval(options) {
|
|
86
|
+
return requestToolApproval(options);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
26
89
|
|
|
27
90
|
// src/agent/agent.ts
|
|
28
91
|
import { z } from "zod";
|
|
29
92
|
|
|
93
|
+
// src/agent/ids.ts
|
|
94
|
+
function normalizeAgentId(id) {
|
|
95
|
+
if (typeof id !== "string") {
|
|
96
|
+
throw new TypeError("Agent id must be a string.");
|
|
97
|
+
}
|
|
98
|
+
const normalized = id.trim();
|
|
99
|
+
if (normalized.length === 0) {
|
|
100
|
+
throw new TypeError("Agent id must be a non-empty string.");
|
|
101
|
+
}
|
|
102
|
+
return normalized;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/internal/async-queue.ts
|
|
106
|
+
function createAsyncQueue() {
|
|
107
|
+
const values = [];
|
|
108
|
+
const waiters = [];
|
|
109
|
+
let closed = false;
|
|
110
|
+
let error;
|
|
111
|
+
function flush() {
|
|
112
|
+
while (waiters.length > 0 && values.length > 0) {
|
|
113
|
+
const waiter = waiters.shift();
|
|
114
|
+
const value = values.shift();
|
|
115
|
+
if (waiter !== void 0) {
|
|
116
|
+
waiter.resolve({ value, done: false });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (values.length > 0 || waiters.length === 0 || !closed) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
while (waiters.length > 0) {
|
|
123
|
+
const waiter = waiters.shift();
|
|
124
|
+
if (waiter === void 0) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (error !== void 0) {
|
|
128
|
+
waiter.reject(error);
|
|
129
|
+
} else {
|
|
130
|
+
waiter.resolve({ value: void 0, done: true });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
enqueue(value) {
|
|
136
|
+
if (closed) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
values.push(value);
|
|
140
|
+
flush();
|
|
141
|
+
},
|
|
142
|
+
close() {
|
|
143
|
+
closed = true;
|
|
144
|
+
flush();
|
|
145
|
+
},
|
|
146
|
+
throw(thrown) {
|
|
147
|
+
closed = true;
|
|
148
|
+
error = thrown;
|
|
149
|
+
flush();
|
|
150
|
+
},
|
|
151
|
+
[Symbol.asyncIterator]() {
|
|
152
|
+
return {
|
|
153
|
+
next() {
|
|
154
|
+
if (values.length > 0) {
|
|
155
|
+
const value = values.shift();
|
|
156
|
+
return Promise.resolve({ value, done: false });
|
|
157
|
+
}
|
|
158
|
+
if (error !== void 0) {
|
|
159
|
+
return Promise.reject(error);
|
|
160
|
+
}
|
|
161
|
+
if (closed) {
|
|
162
|
+
return Promise.resolve({ value: void 0, done: true });
|
|
163
|
+
}
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
waiters.push({ resolve, reject });
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
30
173
|
// src/observability/group.ts
|
|
31
174
|
async function startAgentRunObservers(registrations, args, failOnObserverError) {
|
|
32
175
|
const runObservers = [];
|
|
@@ -195,69 +338,157 @@ var ActiveToolObservers = class {
|
|
|
195
338
|
}
|
|
196
339
|
};
|
|
197
340
|
|
|
198
|
-
// src/agent/
|
|
199
|
-
var
|
|
200
|
-
constructor(
|
|
201
|
-
|
|
202
|
-
this.
|
|
203
|
-
this.
|
|
204
|
-
this.prompt = prompt;
|
|
205
|
-
this.name = "MaxTurnsError";
|
|
341
|
+
// src/agent/request-memory.ts
|
|
342
|
+
var PromptRequestMemory = class {
|
|
343
|
+
constructor(agent, memoryContext, initialHistory) {
|
|
344
|
+
this.agent = agent;
|
|
345
|
+
this.memoryContext = memoryContext;
|
|
346
|
+
this.initialHistory = initialHistory;
|
|
206
347
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
this.
|
|
215
|
-
|
|
216
|
-
|
|
348
|
+
agent;
|
|
349
|
+
memoryContext;
|
|
350
|
+
initialHistory;
|
|
351
|
+
memoryPolicy() {
|
|
352
|
+
return this.memory()?.options.savePolicy;
|
|
353
|
+
}
|
|
354
|
+
pendingTurnMessages(newMessages) {
|
|
355
|
+
return this.memoryPolicy() === "turn" ? [...newMessages] : [];
|
|
356
|
+
}
|
|
357
|
+
async prepareRun(runId, newMessages) {
|
|
358
|
+
const memory = this.memory();
|
|
359
|
+
if (memory === void 0 || this.memoryContext === void 0) {
|
|
360
|
+
return this.initialHistory;
|
|
361
|
+
}
|
|
362
|
+
const memoryHistory = await memory.store.load(this.memoryContext);
|
|
363
|
+
const chatHistory = [...memoryHistory, ...this.initialHistory];
|
|
364
|
+
if (memory.options.savePolicy === "message") {
|
|
365
|
+
await memory.store.append({
|
|
366
|
+
context: this.memoryContext,
|
|
367
|
+
runId,
|
|
368
|
+
turn: 1,
|
|
369
|
+
messages: newMessages
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return chatHistory;
|
|
373
|
+
}
|
|
374
|
+
async commitMessages(runId, turn, messages, pendingTurnMessages) {
|
|
375
|
+
const memory = this.memory();
|
|
376
|
+
if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (memory.options.savePolicy === "message") {
|
|
380
|
+
await memory.store.append({
|
|
381
|
+
context: this.memoryContext,
|
|
382
|
+
runId,
|
|
383
|
+
turn,
|
|
384
|
+
messages
|
|
385
|
+
});
|
|
386
|
+
} else if (memory.options.savePolicy === "turn") {
|
|
387
|
+
pendingTurnMessages.push(...messages);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async commitCompletedTurn(runId, turn, pendingTurnMessages) {
|
|
391
|
+
const memory = this.memory();
|
|
392
|
+
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
await memory.store.append({
|
|
396
|
+
context: this.memoryContext,
|
|
397
|
+
runId,
|
|
398
|
+
turn,
|
|
399
|
+
messages: [...pendingTurnMessages]
|
|
400
|
+
});
|
|
401
|
+
pendingTurnMessages.length = 0;
|
|
402
|
+
}
|
|
403
|
+
async commitCompletedRun(runId, turn, newMessages, pendingTurnMessages) {
|
|
404
|
+
await this.commitCompletedTurn(runId, turn, pendingTurnMessages);
|
|
405
|
+
const memory = this.memory();
|
|
406
|
+
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
await memory.store.append({
|
|
410
|
+
context: this.memoryContext,
|
|
411
|
+
runId,
|
|
412
|
+
turn,
|
|
413
|
+
messages: [...newMessages]
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
async recordError(runId, error, newMessages) {
|
|
417
|
+
const memory = this.memory();
|
|
418
|
+
if (memory === void 0 || this.memoryContext === void 0) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
await memory.store.recordError?.({
|
|
422
|
+
context: this.memoryContext,
|
|
423
|
+
runId,
|
|
424
|
+
error,
|
|
425
|
+
messages: [...newMessages]
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
memory() {
|
|
429
|
+
return this.memoryContext === void 0 ? void 0 : this.agent.memory;
|
|
217
430
|
}
|
|
218
|
-
chatHistory;
|
|
219
|
-
reason;
|
|
220
431
|
};
|
|
221
432
|
|
|
222
|
-
// src/agent/
|
|
223
|
-
function
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
433
|
+
// src/agent/retrieval.ts
|
|
434
|
+
async function fetchDynamicContext(agent, ragText) {
|
|
435
|
+
if (ragText === void 0 || ragText.length === 0 || agent.dynamicContexts.length === 0) {
|
|
436
|
+
return [];
|
|
437
|
+
}
|
|
438
|
+
const documents = [];
|
|
439
|
+
for (const registration of agent.dynamicContexts) {
|
|
440
|
+
const results = await registration.index.search({
|
|
441
|
+
query: ragText,
|
|
442
|
+
topK: registration.options.topK,
|
|
443
|
+
threshold: registration.options.threshold,
|
|
444
|
+
filter: registration.options.filter
|
|
445
|
+
});
|
|
446
|
+
for (const result of results) {
|
|
447
|
+
const formatted = registration.options.format?.(result);
|
|
448
|
+
if (formatted !== void 0) {
|
|
449
|
+
documents.push(formatted);
|
|
450
|
+
} else {
|
|
451
|
+
const metadata = formatMetadata(result.metadata);
|
|
452
|
+
documents.push({
|
|
453
|
+
id: result.id,
|
|
454
|
+
text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2),
|
|
455
|
+
...metadata === void 0 ? {} : { additionalProps: metadata }
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return documents;
|
|
238
461
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
462
|
+
async function fetchToolDefinitions(agent, ragText) {
|
|
463
|
+
const staticDefinitions = await agent.toolSet.getToolDefinitions(ragText);
|
|
464
|
+
if (ragText === void 0 || ragText.length === 0 || agent.dynamicTools.length === 0) {
|
|
465
|
+
return staticDefinitions;
|
|
466
|
+
}
|
|
467
|
+
const definitions = [...staticDefinitions];
|
|
468
|
+
const names = new Set(staticDefinitions.map((definition) => definition.name));
|
|
469
|
+
for (const registration of agent.dynamicTools) {
|
|
470
|
+
const results = await registration.index.search({
|
|
471
|
+
query: ragText,
|
|
472
|
+
topK: registration.options.topK,
|
|
473
|
+
threshold: registration.options.threshold,
|
|
474
|
+
filter: registration.options.filter
|
|
475
|
+
});
|
|
476
|
+
for (const result of results) {
|
|
477
|
+
if (names.has(result.document.toolName)) {
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
names.add(result.document.toolName);
|
|
481
|
+
definitions.push(result.document.definition);
|
|
482
|
+
}
|
|
245
483
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
skip(reason) {
|
|
252
|
-
return skipTool(reason);
|
|
253
|
-
},
|
|
254
|
-
cancel(reason) {
|
|
255
|
-
return { type: "terminate", reason };
|
|
256
|
-
},
|
|
257
|
-
requestApproval(options) {
|
|
258
|
-
return requestToolApproval(options);
|
|
484
|
+
return definitions;
|
|
485
|
+
}
|
|
486
|
+
function formatMetadata(metadata) {
|
|
487
|
+
if (metadata === void 0) {
|
|
488
|
+
return void 0;
|
|
259
489
|
}
|
|
260
|
-
|
|
490
|
+
return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
|
|
491
|
+
}
|
|
261
492
|
|
|
262
493
|
// src/agent/utils.ts
|
|
263
494
|
function isStreamingCompletionModel(model) {
|
|
@@ -282,23 +513,6 @@ function parseJsonValue(text) {
|
|
|
282
513
|
return text;
|
|
283
514
|
}
|
|
284
515
|
}
|
|
285
|
-
async function mapWithConcurrency(items, concurrency, mapper) {
|
|
286
|
-
const results = [];
|
|
287
|
-
let next = 0;
|
|
288
|
-
async function worker() {
|
|
289
|
-
while (next < items.length) {
|
|
290
|
-
const index = next;
|
|
291
|
-
next += 1;
|
|
292
|
-
const item = items[index];
|
|
293
|
-
if (item !== void 0) {
|
|
294
|
-
results[index] = await mapper(item);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
const workerCount = Math.min(concurrency, items.length);
|
|
299
|
-
await Promise.all(Array.from({ length: workerCount }, () => worker()));
|
|
300
|
-
return results;
|
|
301
|
-
}
|
|
302
516
|
|
|
303
517
|
// src/agent/stream-accumulator.ts
|
|
304
518
|
var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
@@ -488,68 +702,273 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
488
702
|
event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
|
|
489
703
|
);
|
|
490
704
|
}
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
if (contentType === "summary") {
|
|
494
|
-
if (last?.type === "summary") {
|
|
495
|
-
last.text += event.delta;
|
|
496
|
-
} else {
|
|
497
|
-
reasoning.content.push({ type: "summary", text: event.delta });
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (contentType === "summary") {
|
|
708
|
+
if (last?.type === "summary") {
|
|
709
|
+
last.text += event.delta;
|
|
710
|
+
} else {
|
|
711
|
+
reasoning.content.push({ type: "summary", text: event.delta });
|
|
712
|
+
}
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
if (contentType === "encrypted") {
|
|
716
|
+
reasoning.content.push({ type: "encrypted", data: event.delta });
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
reasoning.content.push({ type: "redacted", data: event.delta });
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
function reasoningDeltaEvent(event) {
|
|
723
|
+
const mapped = { type: "reasoning_delta", delta: event.delta };
|
|
724
|
+
if (event.id !== void 0) mapped.id = event.id;
|
|
725
|
+
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
726
|
+
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
727
|
+
return mapped;
|
|
728
|
+
}
|
|
729
|
+
function isEmptyToolArguments(value) {
|
|
730
|
+
if (value === void 0 || value === null) {
|
|
731
|
+
return true;
|
|
732
|
+
}
|
|
733
|
+
if (typeof value === "string") {
|
|
734
|
+
return value.trim().length === 0;
|
|
735
|
+
}
|
|
736
|
+
if (Array.isArray(value)) {
|
|
737
|
+
return value.length === 0;
|
|
738
|
+
}
|
|
739
|
+
if (typeof value === "object") {
|
|
740
|
+
return Object.values(value).every((item) => item === void 0);
|
|
741
|
+
}
|
|
742
|
+
return false;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
// src/agent/tool-execution.ts
|
|
746
|
+
var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
|
|
747
|
+
var ToolCallExecutor = class {
|
|
748
|
+
constructor(agent, activeHook, concurrency, requestToolMiddlewares, cancel) {
|
|
749
|
+
this.agent = agent;
|
|
750
|
+
this.activeHook = activeHook;
|
|
751
|
+
this.concurrency = concurrency;
|
|
752
|
+
this.requestToolMiddlewares = requestToolMiddlewares;
|
|
753
|
+
this.cancel = cancel;
|
|
754
|
+
}
|
|
755
|
+
agent;
|
|
756
|
+
activeHook;
|
|
757
|
+
concurrency;
|
|
758
|
+
requestToolMiddlewares;
|
|
759
|
+
cancel;
|
|
760
|
+
async execute(toolCalls, onResult, onStreamEvent, observation) {
|
|
761
|
+
return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
|
|
762
|
+
const args = JSON.stringify(toolCall.function.arguments ?? {});
|
|
763
|
+
const internalCallId = globalThis.crypto.randomUUID();
|
|
764
|
+
const hookArgs = {
|
|
765
|
+
toolName: toolCall.function.name,
|
|
766
|
+
internalCallId,
|
|
767
|
+
args
|
|
768
|
+
};
|
|
769
|
+
if (toolCall.callId !== void 0) {
|
|
770
|
+
hookArgs.toolCallId = toolCall.callId;
|
|
771
|
+
}
|
|
772
|
+
const tool = this.agent.getTool(toolCall.function.name);
|
|
773
|
+
const toolDefinition = observation?.toolDefinitions?.find(
|
|
774
|
+
(definition) => definition.name === toolCall.function.name
|
|
775
|
+
);
|
|
776
|
+
const toolMetadata = toolTraceMetadata(tool);
|
|
777
|
+
const toolObservers = await observation?.runObservers.startTool({
|
|
778
|
+
turn: observation.turn,
|
|
779
|
+
toolCall,
|
|
780
|
+
toolName: toolCall.function.name,
|
|
781
|
+
internalCallId,
|
|
782
|
+
args,
|
|
783
|
+
toolCallId: toolCall.callId,
|
|
784
|
+
...toolDefinition === void 0 ? {} : { toolDefinition },
|
|
785
|
+
...toolMetadata === void 0 ? {} : { toolMetadata }
|
|
786
|
+
});
|
|
787
|
+
const callAction = await this.activeHook?.onToolCall?.({
|
|
788
|
+
...hookArgs,
|
|
789
|
+
tool: toolCallControl
|
|
790
|
+
});
|
|
791
|
+
if (callAction?.type === "terminate") {
|
|
792
|
+
await recordToolError(
|
|
793
|
+
toolObservers,
|
|
794
|
+
observation?.turn,
|
|
795
|
+
toolCall,
|
|
796
|
+
internalCallId,
|
|
797
|
+
args,
|
|
798
|
+
callAction.reason
|
|
799
|
+
);
|
|
800
|
+
throw this.cancel(callAction.reason);
|
|
801
|
+
}
|
|
802
|
+
if (callAction?.type === "approval_request") {
|
|
803
|
+
const reason = `Tool approval was requested for ${toolCall.function.name}, but no approval handler is installed.`;
|
|
804
|
+
await recordToolError(
|
|
805
|
+
toolObservers,
|
|
806
|
+
observation?.turn,
|
|
807
|
+
toolCall,
|
|
808
|
+
internalCallId,
|
|
809
|
+
args,
|
|
810
|
+
reason
|
|
811
|
+
);
|
|
812
|
+
throw this.cancel(reason);
|
|
813
|
+
}
|
|
814
|
+
let output;
|
|
815
|
+
let skipped = false;
|
|
816
|
+
if (callAction?.type === "skip") {
|
|
817
|
+
output = callAction.reason;
|
|
818
|
+
skipped = true;
|
|
819
|
+
} else {
|
|
820
|
+
try {
|
|
821
|
+
output = await this.agent.callTool(toolCall.function.name, args, {
|
|
822
|
+
emitStreamEvent: async (event) => {
|
|
823
|
+
await toolObservers?.streamEvent({
|
|
824
|
+
turn: observation?.turn ?? 0,
|
|
825
|
+
toolCall,
|
|
826
|
+
toolName: toolCall.function.name,
|
|
827
|
+
internalCallId,
|
|
828
|
+
args,
|
|
829
|
+
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
830
|
+
event
|
|
831
|
+
});
|
|
832
|
+
const payload = agentToolEventPayload(toolCall, internalCallId, event);
|
|
833
|
+
if (payload !== void 0) {
|
|
834
|
+
onStreamEvent?.(payload);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
} catch (error) {
|
|
839
|
+
output = error instanceof Error ? error.toString() : String(error);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
let result = toolOutputToText(output);
|
|
843
|
+
let structuredResult = toolOutputToStructuredResult(output);
|
|
844
|
+
if (this.agent.shouldApplyToolMiddleware(toolCall.function.name)) {
|
|
845
|
+
const middlewareReplacement = await this.runToolResultMiddlewares({
|
|
846
|
+
...hookArgs,
|
|
847
|
+
result,
|
|
848
|
+
originalResult: result,
|
|
849
|
+
structuredResult,
|
|
850
|
+
originalStructuredResult: structuredResult,
|
|
851
|
+
turn: observation?.turn ?? 0
|
|
852
|
+
});
|
|
853
|
+
if (middlewareReplacement !== void 0) {
|
|
854
|
+
output = middlewareReplacement;
|
|
855
|
+
result = middlewareReplacement;
|
|
856
|
+
structuredResult = void 0;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
const resultAction = await this.activeHook?.onToolResult?.({
|
|
860
|
+
...hookArgs,
|
|
861
|
+
result,
|
|
862
|
+
structuredResult,
|
|
863
|
+
run: runControl
|
|
864
|
+
});
|
|
865
|
+
await toolObservers?.end({
|
|
866
|
+
turn: observation?.turn ?? 0,
|
|
867
|
+
toolCall,
|
|
868
|
+
toolName: toolCall.function.name,
|
|
869
|
+
internalCallId,
|
|
870
|
+
args,
|
|
871
|
+
result,
|
|
872
|
+
structuredResult,
|
|
873
|
+
skipped,
|
|
874
|
+
toolCallId: toolCall.callId
|
|
875
|
+
});
|
|
876
|
+
if (resultAction?.type === "terminate") {
|
|
877
|
+
throw this.cancel(resultAction.reason);
|
|
878
|
+
}
|
|
879
|
+
const resultPayload = {
|
|
880
|
+
type: "tool_result",
|
|
881
|
+
toolName: toolCall.function.name,
|
|
882
|
+
internalCallId,
|
|
883
|
+
args,
|
|
884
|
+
result,
|
|
885
|
+
structuredResult
|
|
886
|
+
};
|
|
887
|
+
if (toolCall.callId !== void 0) {
|
|
888
|
+
resultPayload.toolCallId = toolCall.callId;
|
|
889
|
+
}
|
|
890
|
+
onResult?.(resultPayload);
|
|
891
|
+
return ToolContent.toolResult(toolCall.id, output, toolCall.callId);
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
async runToolResultMiddlewares(args) {
|
|
895
|
+
let result = args.result;
|
|
896
|
+
let replaced = false;
|
|
897
|
+
for (const middleware of [...this.agent.toolMiddlewares, ...this.requestToolMiddlewares]) {
|
|
898
|
+
const replacement = await middleware.onResult?.({
|
|
899
|
+
...args,
|
|
900
|
+
result
|
|
901
|
+
});
|
|
902
|
+
if (replacement !== void 0) {
|
|
903
|
+
result = replacement;
|
|
904
|
+
replaced = true;
|
|
498
905
|
}
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
if (contentType === "encrypted") {
|
|
502
|
-
reasoning.content.push({ type: "encrypted", data: event.delta });
|
|
503
|
-
return;
|
|
504
906
|
}
|
|
505
|
-
|
|
907
|
+
return replaced ? result : void 0;
|
|
506
908
|
}
|
|
507
909
|
};
|
|
508
|
-
function
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
512
|
-
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
513
|
-
return mapped;
|
|
514
|
-
}
|
|
515
|
-
function isEmptyToolArguments(value) {
|
|
516
|
-
if (value === void 0 || value === null) {
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
if (typeof value === "string") {
|
|
520
|
-
return value.trim().length === 0;
|
|
521
|
-
}
|
|
522
|
-
if (Array.isArray(value)) {
|
|
523
|
-
return value.length === 0;
|
|
910
|
+
function toolTraceMetadata(tool) {
|
|
911
|
+
if (tool === void 0) {
|
|
912
|
+
return void 0;
|
|
524
913
|
}
|
|
525
|
-
|
|
526
|
-
|
|
914
|
+
const metadata = tool[MCP_TOOL_METADATA_KEY];
|
|
915
|
+
const mcpMetadata = typeof metadata === "object" && metadata !== null ? metadata : void 0;
|
|
916
|
+
return {
|
|
917
|
+
approvalRequired: tool.approval !== void 0,
|
|
918
|
+
...typeof mcpMetadata?.serverName === "string" && mcpMetadata.serverName.length > 0 ? { mcpServerName: mcpMetadata.serverName } : {}
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
async function recordToolError(toolObservers, turn, toolCall, internalCallId, args, error) {
|
|
922
|
+
await toolObservers?.error({
|
|
923
|
+
turn: turn ?? 0,
|
|
924
|
+
toolCall,
|
|
925
|
+
toolName: toolCall.function.name,
|
|
926
|
+
internalCallId,
|
|
927
|
+
args,
|
|
928
|
+
error,
|
|
929
|
+
toolCallId: toolCall.callId
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
function toolOutputToText(output) {
|
|
933
|
+
return typeof output === "string" ? output : toolResultContentToText(output);
|
|
934
|
+
}
|
|
935
|
+
function toolOutputToStructuredResult(output) {
|
|
936
|
+
return typeof output === "string" ? void 0 : output;
|
|
937
|
+
}
|
|
938
|
+
function agentToolEventPayload(toolCall, internalCallId, event) {
|
|
939
|
+
if (typeof event.agentId !== "string" || event.agentId.length === 0) {
|
|
940
|
+
return void 0;
|
|
527
941
|
}
|
|
528
|
-
return
|
|
942
|
+
return {
|
|
943
|
+
type: "agent_tool_event",
|
|
944
|
+
toolName: toolCall.function.name,
|
|
945
|
+
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
946
|
+
internalCallId,
|
|
947
|
+
agentId: event.agentId,
|
|
948
|
+
...event.agentName === void 0 ? {} : { agentName: event.agentName },
|
|
949
|
+
event: event.event
|
|
950
|
+
};
|
|
529
951
|
}
|
|
530
952
|
|
|
531
953
|
// src/agent/request.ts
|
|
532
|
-
var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
|
|
533
954
|
var PromptRequest = class _PromptRequest {
|
|
534
955
|
constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
|
|
535
956
|
this.agent = agent;
|
|
536
957
|
this.promptMessage = promptMessage;
|
|
537
|
-
this.initialHistory = initialHistory;
|
|
538
|
-
this.memoryContext = memoryContext;
|
|
539
958
|
this.chatHistory = initialHistory;
|
|
540
959
|
this.maxTurnCount = agent.defaultMaxTurns ?? 0;
|
|
541
960
|
this.activeHook = agent.hook;
|
|
961
|
+
this.memoryRecorder = new PromptRequestMemory(agent, memoryContext, initialHistory);
|
|
542
962
|
}
|
|
543
963
|
agent;
|
|
544
964
|
promptMessage;
|
|
545
|
-
initialHistory;
|
|
546
|
-
memoryContext;
|
|
547
965
|
chatHistory;
|
|
548
966
|
maxTurnCount;
|
|
549
967
|
activeHook;
|
|
550
968
|
concurrency = 1;
|
|
551
969
|
traceOptions;
|
|
552
970
|
requestToolMiddlewares = [];
|
|
971
|
+
memoryRecorder;
|
|
553
972
|
static fromAgent(agent, prompt, options = {}) {
|
|
554
973
|
const normalized = normalizePromptInput(prompt);
|
|
555
974
|
return new _PromptRequest(agent, normalized.prompt, normalized.history, options.memoryContext);
|
|
@@ -581,8 +1000,8 @@ var PromptRequest = class _PromptRequest {
|
|
|
581
1000
|
async send() {
|
|
582
1001
|
const runId = globalThis.crypto.randomUUID();
|
|
583
1002
|
const newMessages = [this.promptMessage];
|
|
584
|
-
await this.
|
|
585
|
-
const pendingTurnMessages = this.
|
|
1003
|
+
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1004
|
+
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
586
1005
|
let usage = Usage.empty();
|
|
587
1006
|
let currentTurns = 0;
|
|
588
1007
|
let lastPrompt = this.promptMessage;
|
|
@@ -598,15 +1017,15 @@ var PromptRequest = class _PromptRequest {
|
|
|
598
1017
|
const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
|
|
599
1018
|
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
600
1019
|
const ragText = extractRagText(prompt);
|
|
601
|
-
const dynamicContext = await this.
|
|
602
|
-
const toolDefs = await this.
|
|
1020
|
+
const dynamicContext = await fetchDynamicContext(this.agent, ragText);
|
|
1021
|
+
const toolDefs = await fetchToolDefinitions(this.agent, ragText);
|
|
603
1022
|
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();
|
|
604
1023
|
const response = await this.runCompletion(request, currentTurns, runObservers);
|
|
605
1024
|
usage = Usage.add(usage, response.usage);
|
|
606
1025
|
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
607
1026
|
const assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
608
1027
|
newMessages.push(assistantMessage);
|
|
609
|
-
await this.
|
|
1028
|
+
await this.memoryRecorder.commitMessages(
|
|
610
1029
|
runId,
|
|
611
1030
|
currentTurns,
|
|
612
1031
|
[assistantMessage],
|
|
@@ -616,7 +1035,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
616
1035
|
(item) => item.type === "tool_call"
|
|
617
1036
|
);
|
|
618
1037
|
if (toolCalls.length === 0) {
|
|
619
|
-
await this.
|
|
1038
|
+
await this.memoryRecorder.commitCompletedRun(
|
|
620
1039
|
runId,
|
|
621
1040
|
currentTurns,
|
|
622
1041
|
newMessages,
|
|
@@ -644,13 +1063,18 @@ var PromptRequest = class _PromptRequest {
|
|
|
644
1063
|
);
|
|
645
1064
|
const toolMessage = Message.tool(toolResults);
|
|
646
1065
|
newMessages.push(toolMessage);
|
|
647
|
-
await this.
|
|
648
|
-
|
|
1066
|
+
await this.memoryRecorder.commitMessages(
|
|
1067
|
+
runId,
|
|
1068
|
+
currentTurns,
|
|
1069
|
+
[toolMessage],
|
|
1070
|
+
pendingTurnMessages
|
|
1071
|
+
);
|
|
1072
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
649
1073
|
}
|
|
650
1074
|
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
651
1075
|
} catch (error) {
|
|
652
1076
|
await runObservers.error({ error, usage, messages: [...newMessages] });
|
|
653
|
-
await this.
|
|
1077
|
+
await this.memoryRecorder.recordError(runId, error, newMessages);
|
|
654
1078
|
throw error;
|
|
655
1079
|
}
|
|
656
1080
|
}
|
|
@@ -660,8 +1084,8 @@ var PromptRequest = class _PromptRequest {
|
|
|
660
1084
|
}
|
|
661
1085
|
const runId = globalThis.crypto.randomUUID();
|
|
662
1086
|
const newMessages = [this.promptMessage];
|
|
663
|
-
await this.
|
|
664
|
-
const pendingTurnMessages = this.
|
|
1087
|
+
this.chatHistory = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1088
|
+
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
665
1089
|
let usage = Usage.empty();
|
|
666
1090
|
let currentTurns = 0;
|
|
667
1091
|
let lastPrompt = this.promptMessage;
|
|
@@ -687,8 +1111,8 @@ var PromptRequest = class _PromptRequest {
|
|
|
687
1111
|
});
|
|
688
1112
|
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
689
1113
|
const ragText = extractRagText(prompt);
|
|
690
|
-
const dynamicContext = await this.
|
|
691
|
-
const toolDefs = await this.
|
|
1114
|
+
const dynamicContext = await fetchDynamicContext(this.agent, ragText);
|
|
1115
|
+
const toolDefs = await fetchToolDefinitions(this.agent, ragText);
|
|
692
1116
|
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();
|
|
693
1117
|
assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
|
|
694
1118
|
const providerRequest = this.providerTraceRequest(request, { stream: true });
|
|
@@ -732,7 +1156,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
732
1156
|
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
733
1157
|
const assistantMessage = Message.assistant(response.choice, response.messageId);
|
|
734
1158
|
newMessages.push(assistantMessage);
|
|
735
|
-
await this.
|
|
1159
|
+
await this.memoryRecorder.commitMessages(
|
|
736
1160
|
runId,
|
|
737
1161
|
currentTurns,
|
|
738
1162
|
[assistantMessage],
|
|
@@ -747,7 +1171,7 @@ var PromptRequest = class _PromptRequest {
|
|
|
747
1171
|
yield await emit({ type: "turn_end", turn: currentTurns, response });
|
|
748
1172
|
if (toolCalls.length === 0) {
|
|
749
1173
|
const output = textFromAssistantContent(response.choice);
|
|
750
|
-
await this.
|
|
1174
|
+
await this.memoryRecorder.commitCompletedRun(
|
|
751
1175
|
runId,
|
|
752
1176
|
currentTurns,
|
|
753
1177
|
newMessages,
|
|
@@ -790,13 +1214,18 @@ var PromptRequest = class _PromptRequest {
|
|
|
790
1214
|
const toolResults = await toolResultsPromise;
|
|
791
1215
|
const toolMessage = Message.tool(toolResults);
|
|
792
1216
|
newMessages.push(toolMessage);
|
|
793
|
-
await this.
|
|
794
|
-
|
|
1217
|
+
await this.memoryRecorder.commitMessages(
|
|
1218
|
+
runId,
|
|
1219
|
+
currentTurns,
|
|
1220
|
+
[toolMessage],
|
|
1221
|
+
pendingTurnMessages
|
|
1222
|
+
);
|
|
1223
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
795
1224
|
}
|
|
796
1225
|
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
797
1226
|
} catch (error) {
|
|
798
1227
|
await runObservers.error({ error, usage, messages: [...newMessages] });
|
|
799
|
-
await this.
|
|
1228
|
+
await this.memoryRecorder.recordError(runId, error, newMessages);
|
|
800
1229
|
yield await emit({ type: "error", error });
|
|
801
1230
|
throw error;
|
|
802
1231
|
}
|
|
@@ -834,166 +1263,16 @@ var PromptRequest = class _PromptRequest {
|
|
|
834
1263
|
error: error instanceof Error ? error.message : String(error)
|
|
835
1264
|
};
|
|
836
1265
|
}
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
async executeToolCalls(toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
850
|
-
return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
|
|
851
|
-
const args = JSON.stringify(toolCall.function.arguments ?? {});
|
|
852
|
-
const internalCallId = globalThis.crypto.randomUUID();
|
|
853
|
-
const hookArgs = {
|
|
854
|
-
toolName: toolCall.function.name,
|
|
855
|
-
internalCallId,
|
|
856
|
-
args
|
|
857
|
-
};
|
|
858
|
-
if (toolCall.callId !== void 0) {
|
|
859
|
-
hookArgs.toolCallId = toolCall.callId;
|
|
860
|
-
}
|
|
861
|
-
const tool = this.agent.getTool(toolCall.function.name);
|
|
862
|
-
const toolDefinition = observation?.toolDefinitions?.find(
|
|
863
|
-
(definition) => definition.name === toolCall.function.name
|
|
864
|
-
);
|
|
865
|
-
const toolMetadata = this.toolTraceMetadata(tool);
|
|
866
|
-
const toolObservers = await observation?.runObservers.startTool({
|
|
867
|
-
turn: observation.turn,
|
|
868
|
-
toolCall,
|
|
869
|
-
toolName: toolCall.function.name,
|
|
870
|
-
internalCallId,
|
|
871
|
-
args,
|
|
872
|
-
toolCallId: toolCall.callId,
|
|
873
|
-
...toolDefinition === void 0 ? {} : { toolDefinition },
|
|
874
|
-
...toolMetadata === void 0 ? {} : { toolMetadata }
|
|
875
|
-
});
|
|
876
|
-
const callAction = await this.activeHook?.onToolCall?.({
|
|
877
|
-
...hookArgs,
|
|
878
|
-
tool: toolCallControl
|
|
879
|
-
});
|
|
880
|
-
if (callAction?.type === "terminate") {
|
|
881
|
-
await this.recordToolError(
|
|
882
|
-
toolObservers,
|
|
883
|
-
observation?.turn,
|
|
884
|
-
toolCall,
|
|
885
|
-
internalCallId,
|
|
886
|
-
args,
|
|
887
|
-
callAction.reason
|
|
888
|
-
);
|
|
889
|
-
throw this.cancelled(newMessages, callAction.reason);
|
|
890
|
-
}
|
|
891
|
-
if (callAction?.type === "approval_request") {
|
|
892
|
-
const reason = `Tool approval was requested for ${toolCall.function.name}, but no approval handler is installed.`;
|
|
893
|
-
await this.recordToolError(
|
|
894
|
-
toolObservers,
|
|
895
|
-
observation?.turn,
|
|
896
|
-
toolCall,
|
|
897
|
-
internalCallId,
|
|
898
|
-
args,
|
|
899
|
-
reason
|
|
900
|
-
);
|
|
901
|
-
throw this.cancelled(newMessages, reason);
|
|
902
|
-
}
|
|
903
|
-
let output;
|
|
904
|
-
let skipped = false;
|
|
905
|
-
if (callAction?.type === "skip") {
|
|
906
|
-
output = callAction.reason;
|
|
907
|
-
skipped = true;
|
|
908
|
-
} else {
|
|
909
|
-
try {
|
|
910
|
-
output = await this.agent.callTool(toolCall.function.name, args, {
|
|
911
|
-
emitStreamEvent: async (event) => {
|
|
912
|
-
await toolObservers?.streamEvent({
|
|
913
|
-
turn: observation?.turn ?? 0,
|
|
914
|
-
toolCall,
|
|
915
|
-
toolName: toolCall.function.name,
|
|
916
|
-
internalCallId,
|
|
917
|
-
args,
|
|
918
|
-
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
919
|
-
event
|
|
920
|
-
});
|
|
921
|
-
const payload = agentToolEventPayload(toolCall, internalCallId, event);
|
|
922
|
-
if (payload !== void 0) {
|
|
923
|
-
onStreamEvent?.(payload);
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
});
|
|
927
|
-
} catch (error) {
|
|
928
|
-
output = error instanceof Error ? error.toString() : String(error);
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
let result = toolOutputToText(output);
|
|
932
|
-
let structuredResult = toolOutputToStructuredResult(output);
|
|
933
|
-
if (this.agent.shouldApplyToolMiddleware(toolCall.function.name)) {
|
|
934
|
-
const middlewareReplacement = await this.runToolResultMiddlewares({
|
|
935
|
-
...hookArgs,
|
|
936
|
-
result,
|
|
937
|
-
originalResult: result,
|
|
938
|
-
structuredResult,
|
|
939
|
-
originalStructuredResult: structuredResult,
|
|
940
|
-
turn: observation?.turn ?? 0
|
|
941
|
-
});
|
|
942
|
-
if (middlewareReplacement !== void 0) {
|
|
943
|
-
output = middlewareReplacement;
|
|
944
|
-
result = middlewareReplacement;
|
|
945
|
-
structuredResult = void 0;
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
const resultAction = await this.activeHook?.onToolResult?.({
|
|
949
|
-
...hookArgs,
|
|
950
|
-
result,
|
|
951
|
-
structuredResult,
|
|
952
|
-
run: runControl
|
|
953
|
-
});
|
|
954
|
-
await toolObservers?.end({
|
|
955
|
-
turn: observation?.turn ?? 0,
|
|
956
|
-
toolCall,
|
|
957
|
-
toolName: toolCall.function.name,
|
|
958
|
-
internalCallId,
|
|
959
|
-
args,
|
|
960
|
-
result,
|
|
961
|
-
structuredResult,
|
|
962
|
-
skipped,
|
|
963
|
-
toolCallId: toolCall.callId
|
|
964
|
-
});
|
|
965
|
-
if (resultAction?.type === "terminate") {
|
|
966
|
-
throw this.cancelled(newMessages, resultAction.reason);
|
|
967
|
-
}
|
|
968
|
-
const resultPayload = {
|
|
969
|
-
type: "tool_result",
|
|
970
|
-
toolName: toolCall.function.name,
|
|
971
|
-
internalCallId,
|
|
972
|
-
args,
|
|
973
|
-
result,
|
|
974
|
-
structuredResult
|
|
975
|
-
};
|
|
976
|
-
if (toolCall.callId !== void 0) {
|
|
977
|
-
resultPayload.toolCallId = toolCall.callId;
|
|
978
|
-
}
|
|
979
|
-
onResult?.(resultPayload);
|
|
980
|
-
return ToolContent.toolResult(toolCall.id, output, toolCall.callId);
|
|
981
|
-
});
|
|
982
|
-
}
|
|
983
|
-
async runToolResultMiddlewares(args) {
|
|
984
|
-
let result = args.result;
|
|
985
|
-
let replaced = false;
|
|
986
|
-
for (const middleware of [...this.agent.toolMiddlewares, ...this.requestToolMiddlewares]) {
|
|
987
|
-
const replacement = await middleware.onResult?.({
|
|
988
|
-
...args,
|
|
989
|
-
result
|
|
990
|
-
});
|
|
991
|
-
if (replacement !== void 0) {
|
|
992
|
-
result = replacement;
|
|
993
|
-
replaced = true;
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
return replaced ? result : void 0;
|
|
1266
|
+
}
|
|
1267
|
+
async executeToolCalls(toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
1268
|
+
const executor = new ToolCallExecutor(
|
|
1269
|
+
this.agent,
|
|
1270
|
+
this.activeHook,
|
|
1271
|
+
this.concurrency,
|
|
1272
|
+
this.requestToolMiddlewares,
|
|
1273
|
+
(reason) => this.cancelled(newMessages, reason)
|
|
1274
|
+
);
|
|
1275
|
+
return executor.execute(toolCalls, onResult, onStreamEvent, observation);
|
|
997
1276
|
}
|
|
998
1277
|
async startRunObservers() {
|
|
999
1278
|
const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
|
|
@@ -1035,69 +1314,6 @@ var PromptRequest = class _PromptRequest {
|
|
|
1035
1314
|
event
|
|
1036
1315
|
});
|
|
1037
1316
|
}
|
|
1038
|
-
async fetchDynamicContext(ragText) {
|
|
1039
|
-
if (ragText === void 0 || ragText.length === 0 || this.agent.dynamicContexts.length === 0) {
|
|
1040
|
-
return [];
|
|
1041
|
-
}
|
|
1042
|
-
const documents = [];
|
|
1043
|
-
for (const registration of this.agent.dynamicContexts) {
|
|
1044
|
-
const results = await registration.index.search({
|
|
1045
|
-
query: ragText,
|
|
1046
|
-
topK: registration.options.topK,
|
|
1047
|
-
threshold: registration.options.threshold,
|
|
1048
|
-
filter: registration.options.filter
|
|
1049
|
-
});
|
|
1050
|
-
for (const result of results) {
|
|
1051
|
-
const formatted = registration.options.format?.(result);
|
|
1052
|
-
if (formatted !== void 0) {
|
|
1053
|
-
documents.push(formatted);
|
|
1054
|
-
} else {
|
|
1055
|
-
const metadata = formatMetadata(result.metadata);
|
|
1056
|
-
documents.push({
|
|
1057
|
-
id: result.id,
|
|
1058
|
-
text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2),
|
|
1059
|
-
...metadata === void 0 ? {} : { additionalProps: metadata }
|
|
1060
|
-
});
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
return documents;
|
|
1065
|
-
}
|
|
1066
|
-
async fetchToolDefinitions(ragText) {
|
|
1067
|
-
const staticDefinitions = await this.agent.toolSet.getToolDefinitions(ragText);
|
|
1068
|
-
if (ragText === void 0 || ragText.length === 0 || this.agent.dynamicTools.length === 0) {
|
|
1069
|
-
return staticDefinitions;
|
|
1070
|
-
}
|
|
1071
|
-
const definitions = [...staticDefinitions];
|
|
1072
|
-
const names = new Set(staticDefinitions.map((definition) => definition.name));
|
|
1073
|
-
for (const registration of this.agent.dynamicTools) {
|
|
1074
|
-
const results = await registration.index.search({
|
|
1075
|
-
query: ragText,
|
|
1076
|
-
topK: registration.options.topK,
|
|
1077
|
-
threshold: registration.options.threshold,
|
|
1078
|
-
filter: registration.options.filter
|
|
1079
|
-
});
|
|
1080
|
-
for (const result of results) {
|
|
1081
|
-
if (names.has(result.document.toolName)) {
|
|
1082
|
-
continue;
|
|
1083
|
-
}
|
|
1084
|
-
names.add(result.document.toolName);
|
|
1085
|
-
definitions.push(result.document.definition);
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
return definitions;
|
|
1089
|
-
}
|
|
1090
|
-
async recordToolError(toolObservers, turn, toolCall, internalCallId, args, error) {
|
|
1091
|
-
await toolObservers?.error({
|
|
1092
|
-
turn: turn ?? 0,
|
|
1093
|
-
toolCall,
|
|
1094
|
-
toolName: toolCall.function.name,
|
|
1095
|
-
internalCallId,
|
|
1096
|
-
args,
|
|
1097
|
-
error,
|
|
1098
|
-
toolCallId: toolCall.callId
|
|
1099
|
-
});
|
|
1100
|
-
}
|
|
1101
1317
|
async runCompletionCallHook(prompt, history, newMessages) {
|
|
1102
1318
|
const action = await this.activeHook?.onCompletionCall?.({
|
|
1103
1319
|
prompt,
|
|
@@ -1121,83 +1337,6 @@ var PromptRequest = class _PromptRequest {
|
|
|
1121
1337
|
cancelled(newMessages, reason) {
|
|
1122
1338
|
return new PromptCancelledError([...this.chatHistory, ...newMessages], reason);
|
|
1123
1339
|
}
|
|
1124
|
-
memory() {
|
|
1125
|
-
return this.memoryContext === void 0 ? void 0 : this.agent.memory;
|
|
1126
|
-
}
|
|
1127
|
-
memoryPolicy() {
|
|
1128
|
-
return this.memory()?.options.savePolicy;
|
|
1129
|
-
}
|
|
1130
|
-
async prepareMemoryRun(runId, newMessages) {
|
|
1131
|
-
const memory = this.memory();
|
|
1132
|
-
if (memory === void 0 || this.memoryContext === void 0) {
|
|
1133
|
-
this.chatHistory = this.initialHistory;
|
|
1134
|
-
return;
|
|
1135
|
-
}
|
|
1136
|
-
const memoryHistory = await memory.store.load(this.memoryContext);
|
|
1137
|
-
this.chatHistory = [...memoryHistory, ...this.initialHistory];
|
|
1138
|
-
if (memory.options.savePolicy === "message") {
|
|
1139
|
-
await memory.store.append({
|
|
1140
|
-
context: this.memoryContext,
|
|
1141
|
-
runId,
|
|
1142
|
-
turn: 1,
|
|
1143
|
-
messages: newMessages
|
|
1144
|
-
});
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
async commitMemoryMessages(runId, turn, messages, pendingTurnMessages) {
|
|
1148
|
-
const memory = this.memory();
|
|
1149
|
-
if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
|
|
1150
|
-
return;
|
|
1151
|
-
}
|
|
1152
|
-
if (memory.options.savePolicy === "message") {
|
|
1153
|
-
await memory.store.append({
|
|
1154
|
-
context: this.memoryContext,
|
|
1155
|
-
runId,
|
|
1156
|
-
turn,
|
|
1157
|
-
messages
|
|
1158
|
-
});
|
|
1159
|
-
} else if (memory.options.savePolicy === "turn") {
|
|
1160
|
-
pendingTurnMessages.push(...messages);
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
async commitCompletedMemoryTurn(runId, turn, pendingTurnMessages) {
|
|
1164
|
-
const memory = this.memory();
|
|
1165
|
-
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
|
|
1166
|
-
return;
|
|
1167
|
-
}
|
|
1168
|
-
await memory.store.append({
|
|
1169
|
-
context: this.memoryContext,
|
|
1170
|
-
runId,
|
|
1171
|
-
turn,
|
|
1172
|
-
messages: [...pendingTurnMessages]
|
|
1173
|
-
});
|
|
1174
|
-
pendingTurnMessages.length = 0;
|
|
1175
|
-
}
|
|
1176
|
-
async commitCompletedMemoryRun(runId, turn, newMessages, pendingTurnMessages) {
|
|
1177
|
-
await this.commitCompletedMemoryTurn(runId, turn, pendingTurnMessages);
|
|
1178
|
-
const memory = this.memory();
|
|
1179
|
-
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
|
|
1180
|
-
return;
|
|
1181
|
-
}
|
|
1182
|
-
await memory.store.append({
|
|
1183
|
-
context: this.memoryContext,
|
|
1184
|
-
runId,
|
|
1185
|
-
turn,
|
|
1186
|
-
messages: [...newMessages]
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
async recordMemoryError(runId, error, newMessages) {
|
|
1190
|
-
const memory = this.memory();
|
|
1191
|
-
if (memory === void 0 || this.memoryContext === void 0) {
|
|
1192
|
-
return;
|
|
1193
|
-
}
|
|
1194
|
-
await memory.store.recordError?.({
|
|
1195
|
-
context: this.memoryContext,
|
|
1196
|
-
runId,
|
|
1197
|
-
error,
|
|
1198
|
-
messages: [...newMessages]
|
|
1199
|
-
});
|
|
1200
|
-
}
|
|
1201
1340
|
};
|
|
1202
1341
|
function normalizePromptInput(prompt) {
|
|
1203
1342
|
if (typeof prompt === "string") {
|
|
@@ -1218,92 +1357,6 @@ function normalizePromptInput(prompt) {
|
|
|
1218
1357
|
history: prompt.slice(0, -1)
|
|
1219
1358
|
};
|
|
1220
1359
|
}
|
|
1221
|
-
function toolOutputToText(output) {
|
|
1222
|
-
return typeof output === "string" ? output : toolResultContentToText(output);
|
|
1223
|
-
}
|
|
1224
|
-
function toolOutputToStructuredResult(output) {
|
|
1225
|
-
return typeof output === "string" ? void 0 : output;
|
|
1226
|
-
}
|
|
1227
|
-
function agentToolEventPayload(toolCall, internalCallId, event) {
|
|
1228
|
-
if (typeof event.agentId !== "string" || event.agentId.length === 0) {
|
|
1229
|
-
return void 0;
|
|
1230
|
-
}
|
|
1231
|
-
return {
|
|
1232
|
-
type: "agent_tool_event",
|
|
1233
|
-
toolName: toolCall.function.name,
|
|
1234
|
-
...toolCall.callId === void 0 ? {} : { toolCallId: toolCall.callId },
|
|
1235
|
-
internalCallId,
|
|
1236
|
-
agentId: event.agentId,
|
|
1237
|
-
...event.agentName === void 0 ? {} : { agentName: event.agentName },
|
|
1238
|
-
event: event.event
|
|
1239
|
-
};
|
|
1240
|
-
}
|
|
1241
|
-
function createAsyncQueue() {
|
|
1242
|
-
const values = [];
|
|
1243
|
-
const waiters = [];
|
|
1244
|
-
let closed = false;
|
|
1245
|
-
let error;
|
|
1246
|
-
function flush() {
|
|
1247
|
-
while (waiters.length > 0 && values.length > 0) {
|
|
1248
|
-
const waiter = waiters.shift();
|
|
1249
|
-
const value = values.shift();
|
|
1250
|
-
if (waiter !== void 0) {
|
|
1251
|
-
waiter.resolve({ value, done: false });
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
if (values.length > 0 || waiters.length === 0 || !closed) {
|
|
1255
|
-
return;
|
|
1256
|
-
}
|
|
1257
|
-
while (waiters.length > 0) {
|
|
1258
|
-
const waiter = waiters.shift();
|
|
1259
|
-
if (waiter === void 0) {
|
|
1260
|
-
continue;
|
|
1261
|
-
}
|
|
1262
|
-
if (error !== void 0) {
|
|
1263
|
-
waiter.reject(error);
|
|
1264
|
-
} else {
|
|
1265
|
-
waiter.resolve({ value: void 0, done: true });
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
return {
|
|
1270
|
-
enqueue(value) {
|
|
1271
|
-
if (closed) {
|
|
1272
|
-
return;
|
|
1273
|
-
}
|
|
1274
|
-
values.push(value);
|
|
1275
|
-
flush();
|
|
1276
|
-
},
|
|
1277
|
-
close() {
|
|
1278
|
-
closed = true;
|
|
1279
|
-
flush();
|
|
1280
|
-
},
|
|
1281
|
-
throw(thrown) {
|
|
1282
|
-
closed = true;
|
|
1283
|
-
error = thrown;
|
|
1284
|
-
flush();
|
|
1285
|
-
},
|
|
1286
|
-
[Symbol.asyncIterator]() {
|
|
1287
|
-
return {
|
|
1288
|
-
next() {
|
|
1289
|
-
if (values.length > 0) {
|
|
1290
|
-
const value = values.shift();
|
|
1291
|
-
return Promise.resolve({ value, done: false });
|
|
1292
|
-
}
|
|
1293
|
-
if (error !== void 0) {
|
|
1294
|
-
return Promise.reject(error);
|
|
1295
|
-
}
|
|
1296
|
-
if (closed) {
|
|
1297
|
-
return Promise.resolve({ value: void 0, done: true });
|
|
1298
|
-
}
|
|
1299
|
-
return new Promise((resolve, reject) => {
|
|
1300
|
-
waiters.push({ resolve, reject });
|
|
1301
|
-
});
|
|
1302
|
-
}
|
|
1303
|
-
};
|
|
1304
|
-
}
|
|
1305
|
-
};
|
|
1306
|
-
}
|
|
1307
1360
|
function addTurn(turn, event) {
|
|
1308
1361
|
if (event.type === "text_delta") {
|
|
1309
1362
|
return { type: "text_delta", turn, delta: event.delta };
|
|
@@ -1320,12 +1373,6 @@ function addTurn(turn, event) {
|
|
|
1320
1373
|
function isGenerationDeltaEvent(type) {
|
|
1321
1374
|
return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call";
|
|
1322
1375
|
}
|
|
1323
|
-
function formatMetadata(metadata) {
|
|
1324
|
-
if (metadata === void 0) {
|
|
1325
|
-
return void 0;
|
|
1326
|
-
}
|
|
1327
|
-
return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
|
|
1328
|
-
}
|
|
1329
1376
|
|
|
1330
1377
|
// src/agent/agent.ts
|
|
1331
1378
|
var DEFAULT_MAX_TURNS = 20;
|
|
@@ -1481,198 +1528,9 @@ function dynamicToolSetFromIndex(index) {
|
|
|
1481
1528
|
const maybeIndex = index;
|
|
1482
1529
|
return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : void 0;
|
|
1483
1530
|
}
|
|
1484
|
-
function normalizeAgentId(id) {
|
|
1485
|
-
if (typeof id !== "string") {
|
|
1486
|
-
throw new TypeError("Agent id must be a string.");
|
|
1487
|
-
}
|
|
1488
|
-
const normalized = id.trim();
|
|
1489
|
-
if (normalized.length === 0) {
|
|
1490
|
-
throw new TypeError("Agent id must be a non-empty string.");
|
|
1491
|
-
}
|
|
1492
|
-
return normalized;
|
|
1493
|
-
}
|
|
1494
|
-
|
|
1495
|
-
// src/agent/builder.ts
|
|
1496
|
-
var AgentBuilder = class {
|
|
1497
|
-
constructor(agentId, completionModel) {
|
|
1498
|
-
this.completionModel = completionModel;
|
|
1499
|
-
this.agentId = normalizeAgentId2(agentId);
|
|
1500
|
-
}
|
|
1501
|
-
completionModel;
|
|
1502
|
-
agentId;
|
|
1503
|
-
agentName;
|
|
1504
|
-
agentDescription;
|
|
1505
|
-
instructionBlocks = [];
|
|
1506
|
-
contextDocs = [];
|
|
1507
|
-
temp;
|
|
1508
|
-
maxTokenCount;
|
|
1509
|
-
params;
|
|
1510
|
-
choice;
|
|
1511
|
-
turns;
|
|
1512
|
-
requestHook;
|
|
1513
|
-
schema;
|
|
1514
|
-
skillInstructionBlocks = [];
|
|
1515
|
-
observerRegistrations = [];
|
|
1516
|
-
dynamicContextRegistrations = [];
|
|
1517
|
-
dynamicToolRegistrations = [];
|
|
1518
|
-
middlewareRegistrations = [];
|
|
1519
|
-
memoryRegistration;
|
|
1520
|
-
eventStoreRegistration;
|
|
1521
|
-
activeToolSet = new ToolSet();
|
|
1522
|
-
name(name) {
|
|
1523
|
-
this.agentName = name;
|
|
1524
|
-
return this;
|
|
1525
|
-
}
|
|
1526
|
-
description(description) {
|
|
1527
|
-
this.agentDescription = description;
|
|
1528
|
-
return this;
|
|
1529
|
-
}
|
|
1530
|
-
instructions(instructions) {
|
|
1531
|
-
if (instructions.length > 0) {
|
|
1532
|
-
this.instructionBlocks.push(instructions);
|
|
1533
|
-
}
|
|
1534
|
-
return this;
|
|
1535
|
-
}
|
|
1536
|
-
context(text, id = `static_doc_${this.contextDocs.length}`) {
|
|
1537
|
-
this.contextDocs.push({ id, text });
|
|
1538
|
-
return this;
|
|
1539
|
-
}
|
|
1540
|
-
dynamicContext(index, options) {
|
|
1541
|
-
this.dynamicContextRegistrations.push({ index, options });
|
|
1542
|
-
return this;
|
|
1543
|
-
}
|
|
1544
|
-
dynamicTools(index, options) {
|
|
1545
|
-
this.dynamicToolRegistrations.push({ index, options });
|
|
1546
|
-
return this;
|
|
1547
|
-
}
|
|
1548
|
-
tool(tool) {
|
|
1549
|
-
this.activeToolSet.addTool(tool);
|
|
1550
|
-
return this;
|
|
1551
|
-
}
|
|
1552
|
-
tools(tools) {
|
|
1553
|
-
this.activeToolSet.addTools(tools);
|
|
1554
|
-
return this;
|
|
1555
|
-
}
|
|
1556
|
-
mcp(servers) {
|
|
1557
|
-
for (const server of servers) {
|
|
1558
|
-
this.activeToolSet.addTools(server.tools);
|
|
1559
|
-
}
|
|
1560
|
-
return this;
|
|
1561
|
-
}
|
|
1562
|
-
skills(skillSet) {
|
|
1563
|
-
if (skillSet.instructions.length > 0) {
|
|
1564
|
-
this.skillInstructionBlocks.push(skillSet.instructions);
|
|
1565
|
-
}
|
|
1566
|
-
this.activeToolSet.addTools(skillSet.tools);
|
|
1567
|
-
return this;
|
|
1568
|
-
}
|
|
1569
|
-
useToolSet(toolSet) {
|
|
1570
|
-
toolSet.addTools(this.activeToolSet);
|
|
1571
|
-
this.activeToolSet = toolSet;
|
|
1572
|
-
return this;
|
|
1573
|
-
}
|
|
1574
|
-
temperature(temperature) {
|
|
1575
|
-
this.temp = temperature;
|
|
1576
|
-
return this;
|
|
1577
|
-
}
|
|
1578
|
-
maxTokens(maxTokens) {
|
|
1579
|
-
this.maxTokenCount = maxTokens;
|
|
1580
|
-
return this;
|
|
1581
|
-
}
|
|
1582
|
-
additionalParams(params) {
|
|
1583
|
-
this.params = params;
|
|
1584
|
-
return this;
|
|
1585
|
-
}
|
|
1586
|
-
toolChoice(toolChoice) {
|
|
1587
|
-
this.choice = toolChoice;
|
|
1588
|
-
return this;
|
|
1589
|
-
}
|
|
1590
|
-
defaultMaxTurns(defaultMaxTurns) {
|
|
1591
|
-
this.turns = defaultMaxTurns;
|
|
1592
|
-
return this;
|
|
1593
|
-
}
|
|
1594
|
-
hook(hook) {
|
|
1595
|
-
this.requestHook = hook;
|
|
1596
|
-
return this;
|
|
1597
|
-
}
|
|
1598
|
-
toolMiddleware(middleware) {
|
|
1599
|
-
this.middlewareRegistrations.push(middleware);
|
|
1600
|
-
return this;
|
|
1601
|
-
}
|
|
1602
|
-
toolMiddlewares(middlewares) {
|
|
1603
|
-
this.middlewareRegistrations.push(...middlewares);
|
|
1604
|
-
return this;
|
|
1605
|
-
}
|
|
1606
|
-
observe(observer, options = {}) {
|
|
1607
|
-
this.observerRegistrations.push({
|
|
1608
|
-
observer,
|
|
1609
|
-
failOnObserverError: options.failOnObserverError
|
|
1610
|
-
});
|
|
1611
|
-
return this;
|
|
1612
|
-
}
|
|
1613
|
-
memory(store, options = {}) {
|
|
1614
|
-
this.memoryRegistration = {
|
|
1615
|
-
store,
|
|
1616
|
-
options: resolveMemoryOptions(options)
|
|
1617
|
-
};
|
|
1618
|
-
return this;
|
|
1619
|
-
}
|
|
1620
|
-
eventStore(store, options = {}) {
|
|
1621
|
-
this.eventStoreRegistration = {
|
|
1622
|
-
store,
|
|
1623
|
-
options: {
|
|
1624
|
-
include: options.include ?? "all"
|
|
1625
|
-
}
|
|
1626
|
-
};
|
|
1627
|
-
return this;
|
|
1628
|
-
}
|
|
1629
|
-
outputSchema(schema) {
|
|
1630
|
-
this.schema = toProviderJsonSchema(schema);
|
|
1631
|
-
return this;
|
|
1632
|
-
}
|
|
1633
|
-
build() {
|
|
1634
|
-
return new Agent({
|
|
1635
|
-
id: this.agentId,
|
|
1636
|
-
name: this.agentName,
|
|
1637
|
-
description: this.agentDescription,
|
|
1638
|
-
model: this.completionModel,
|
|
1639
|
-
instructions: this.buildInstructions(),
|
|
1640
|
-
staticContext: this.contextDocs,
|
|
1641
|
-
temperature: this.temp,
|
|
1642
|
-
maxTokens: this.maxTokenCount,
|
|
1643
|
-
additionalParams: this.params,
|
|
1644
|
-
toolSet: this.activeToolSet,
|
|
1645
|
-
toolChoice: this.choice,
|
|
1646
|
-
defaultMaxTurns: this.turns,
|
|
1647
|
-
hook: this.requestHook,
|
|
1648
|
-
outputSchema: this.schema,
|
|
1649
|
-
observers: this.observerRegistrations,
|
|
1650
|
-
dynamicContexts: this.dynamicContextRegistrations,
|
|
1651
|
-
dynamicTools: this.dynamicToolRegistrations,
|
|
1652
|
-
toolMiddlewares: this.middlewareRegistrations,
|
|
1653
|
-
memory: this.memoryRegistration,
|
|
1654
|
-
eventStore: this.eventStoreRegistration
|
|
1655
|
-
});
|
|
1656
|
-
}
|
|
1657
|
-
buildInstructions() {
|
|
1658
|
-
const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(
|
|
1659
|
-
(part) => part !== void 0 && part.length > 0
|
|
1660
|
-
);
|
|
1661
|
-
return parts.length === 0 ? void 0 : parts.join("\n\n");
|
|
1662
|
-
}
|
|
1663
|
-
};
|
|
1664
|
-
function normalizeAgentId2(id) {
|
|
1665
|
-
if (typeof id !== "string") {
|
|
1666
|
-
throw new TypeError("Agent id must be a string.");
|
|
1667
|
-
}
|
|
1668
|
-
const normalized = id.trim();
|
|
1669
|
-
if (normalized.length === 0) {
|
|
1670
|
-
throw new TypeError("Agent id must be a non-empty string.");
|
|
1671
|
-
}
|
|
1672
|
-
return normalized;
|
|
1673
|
-
}
|
|
1674
1531
|
|
|
1675
1532
|
export {
|
|
1533
|
+
normalizeAgentId,
|
|
1676
1534
|
MaxTurnsError,
|
|
1677
1535
|
PromptCancelledError,
|
|
1678
1536
|
createHook,
|
|
@@ -1681,10 +1539,8 @@ export {
|
|
|
1681
1539
|
requestToolApproval,
|
|
1682
1540
|
runControl,
|
|
1683
1541
|
toolCallControl,
|
|
1684
|
-
PromptRequest,
|
|
1685
1542
|
DEFAULT_MAX_TURNS,
|
|
1686
1543
|
Agent,
|
|
1687
|
-
AgentSession
|
|
1688
|
-
AgentBuilder
|
|
1544
|
+
AgentSession
|
|
1689
1545
|
};
|
|
1690
|
-
//# sourceMappingURL=chunk-
|
|
1546
|
+
//# sourceMappingURL=chunk-TVIADATY.js.map
|