@anvia/core 0.26.0 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +303 -94
- package/dist/agent/index.d.ts +36 -70
- package/dist/agent/index.js +25 -18
- package/dist/agent-yBG8e9Jj.d.ts +153 -0
- package/dist/chunk-4WI3VGZK.js +127 -0
- package/dist/chunk-4WI3VGZK.js.map +1 -0
- package/dist/chunk-77AIYARB.js +654 -0
- package/dist/chunk-77AIYARB.js.map +1 -0
- package/dist/chunk-AI5JMUW7.js +45 -0
- package/dist/chunk-AI5JMUW7.js.map +1 -0
- package/dist/chunk-BEYBU7VJ.js +53 -0
- package/dist/chunk-BEYBU7VJ.js.map +1 -0
- package/dist/chunk-CWM7B2OO.js +315 -0
- package/dist/chunk-CWM7B2OO.js.map +1 -0
- package/dist/chunk-D37WMSP6.js +131 -0
- package/dist/chunk-D37WMSP6.js.map +1 -0
- package/dist/chunk-DBSCKTUL.js +3793 -0
- package/dist/chunk-DBSCKTUL.js.map +1 -0
- package/dist/chunk-EINTGEGY.js +629 -0
- package/dist/chunk-EINTGEGY.js.map +1 -0
- package/dist/chunk-FMHLROPQ.js +351 -0
- package/dist/chunk-FMHLROPQ.js.map +1 -0
- package/dist/chunk-FN7HLLAZ.js +211 -0
- package/dist/chunk-FN7HLLAZ.js.map +1 -0
- package/dist/chunk-GKDERULJ.js +50 -0
- package/dist/chunk-GKDERULJ.js.map +1 -0
- package/dist/{chunk-CWUJUSOS.js → chunk-KIDGIJM7.js} +26 -5
- package/dist/chunk-KIDGIJM7.js.map +1 -0
- package/dist/chunk-KR3RCCHJ.js +500 -0
- package/dist/chunk-KR3RCCHJ.js.map +1 -0
- package/dist/chunk-MGNTXGTX.js +37 -0
- package/dist/chunk-MGNTXGTX.js.map +1 -0
- package/dist/{chunk-MRQLEK2B.js → chunk-OJH6HWHX.js} +63 -51
- package/dist/chunk-OJH6HWHX.js.map +1 -0
- package/dist/chunk-QOUPWOGW.js +29 -0
- package/dist/chunk-QOUPWOGW.js.map +1 -0
- package/dist/chunk-TIZYTPJO.js +208 -0
- package/dist/chunk-TIZYTPJO.js.map +1 -0
- package/dist/{chunk-NCPN4SJR.js → chunk-XKYYBABH.js} +13 -13
- package/dist/chunk-XKYYBABH.js.map +1 -0
- package/dist/client-B8DT1vUG.d.ts +83 -0
- package/dist/completion/index.d.ts +6 -36
- package/dist/completion/index.js +25 -29
- package/dist/dynamic-tools-KwEGRbHp.d.ts +45 -0
- package/dist/embeddings/index.d.ts +11 -9
- package/dist/embeddings/index.js +2 -4
- package/dist/evals/index.d.ts +47 -20
- package/dist/evals/index.js +104 -65
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +27 -42
- package/dist/extractor/index.js +7 -22
- package/dist/guardrails/index.d.ts +3 -2
- package/dist/guardrails/index.js +1 -1
- package/dist/image-generation/index.d.ts +23 -23
- package/dist/image-generation/index.js +5 -39
- package/dist/image-generation/index.js.map +1 -1
- package/dist/index.d.ts +25 -20
- package/dist/index.js +60 -63
- package/dist/internal/agent.d.ts +163 -9
- package/dist/internal/agent.js +25 -18
- package/dist/loaders/index.d.ts +6 -10
- package/dist/loaders/index.js.map +1 -1
- package/dist/mcp/index.d.ts +19 -13
- package/dist/mcp/index.js +13 -348
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory/index.d.ts +20 -6
- package/dist/memory/index.js +9 -10
- package/dist/message-schema-Cn7ezRfJ.d.ts +52 -0
- package/dist/middleware-C7PdmKF7.d.ts +56 -0
- package/dist/model-call-options-CZkSw_xN.d.ts +6 -0
- package/dist/model-listing/index.d.ts +3 -1
- package/dist/observability/index.d.ts +20 -3
- package/dist/observability/index.js +8 -5
- package/dist/observability/index.js.map +1 -1
- package/dist/pipeline/index.d.ts +114 -72
- package/dist/pipeline/index.js +478 -286
- package/dist/pipeline/index.js.map +1 -1
- package/dist/retry-CjvSlKGW.d.ts +16 -0
- package/dist/skills/index.d.ts +5 -4
- package/dist/skills/index.js +7 -8
- package/dist/speech-generation/index.d.ts +37 -0
- package/dist/speech-generation/index.js +8 -0
- package/dist/streaming/index.js +37 -3
- package/dist/streaming/index.js.map +1 -1
- package/dist/{think-tool-DfCMuO8e.d.ts → think-tool-6H2QNQzQ.d.ts} +5 -5
- package/dist/tool/index.d.ts +10 -6
- package/dist/tool/index.js +15 -21
- package/dist/tool--Mz4v1eL.d.ts +57 -0
- package/dist/transcription/index.d.ts +29 -22
- package/dist/transcription/index.js +5 -57
- package/dist/transcription/index.js.map +1 -1
- package/dist/type-utils-CtHVDRn_.d.ts +6 -0
- package/dist/types-3x3mOLHU.d.ts +379 -0
- package/dist/types-BVzM4RsC.d.ts +155 -0
- package/dist/types-CQ_ioXWK.d.ts +319 -0
- package/dist/types-Cr4uiYo5.d.ts +99 -0
- package/dist/{types-04LqWrmy.d.ts → types-DiJqJejp.d.ts} +1 -1
- package/dist/types-Dld1TpWj.d.ts +130 -0
- package/dist/vector-store/index.d.ts +33 -34
- package/dist/vector-store/index.js +6 -6
- package/package.json +6 -18
- package/dist/agent-x3YTyv5i.d.ts +0 -222
- package/dist/audio-generation/index.d.ts +0 -33
- package/dist/audio-generation/index.js +0 -42
- package/dist/audio-generation/index.js.map +0 -1
- package/dist/chunk-2ODTMRHP.js +0 -54
- package/dist/chunk-2ODTMRHP.js.map +0 -1
- package/dist/chunk-ADH7NNCS.js +0 -512
- package/dist/chunk-ADH7NNCS.js.map +0 -1
- package/dist/chunk-BALBBVI3.js +0 -2732
- package/dist/chunk-BALBBVI3.js.map +0 -1
- package/dist/chunk-CWUJUSOS.js.map +0 -1
- package/dist/chunk-DALYP4PX.js +0 -225
- package/dist/chunk-DALYP4PX.js.map +0 -1
- package/dist/chunk-HYUYZEAJ.js +0 -223
- package/dist/chunk-HYUYZEAJ.js.map +0 -1
- package/dist/chunk-I6XOQ4LK.js +0 -466
- package/dist/chunk-I6XOQ4LK.js.map +0 -1
- package/dist/chunk-JI5YZWNT.js +0 -220
- package/dist/chunk-JI5YZWNT.js.map +0 -1
- package/dist/chunk-MC3CCKEB.js +0 -221
- package/dist/chunk-MC3CCKEB.js.map +0 -1
- package/dist/chunk-MELYDKWQ.js +0 -337
- package/dist/chunk-MELYDKWQ.js.map +0 -1
- package/dist/chunk-MNWK2USR.js +0 -243
- package/dist/chunk-MNWK2USR.js.map +0 -1
- package/dist/chunk-MRQLEK2B.js.map +0 -1
- package/dist/chunk-NCPN4SJR.js.map +0 -1
- package/dist/chunk-OIMLU4SF.js +0 -20
- package/dist/chunk-OIMLU4SF.js.map +0 -1
- package/dist/chunk-Q25IWLBT.js +0 -138
- package/dist/chunk-Q25IWLBT.js.map +0 -1
- package/dist/chunk-UQX6EXWG.js +0 -33
- package/dist/chunk-UQX6EXWG.js.map +0 -1
- package/dist/chunk-WQKHFADH.js +0 -12
- package/dist/chunk-WQKHFADH.js.map +0 -1
- package/dist/chunk-XUUY2L2D.js +0 -42
- package/dist/chunk-XUUY2L2D.js.map +0 -1
- package/dist/chunk-XWUC7CIT.js +0 -1
- package/dist/chunk-XWUC7CIT.js.map +0 -1
- package/dist/chunk-YLMDBQO4.js +0 -141
- package/dist/chunk-YLMDBQO4.js.map +0 -1
- package/dist/errors-2moLDuuN.d.ts +0 -17
- package/dist/hooks/index.d.ts +0 -12
- package/dist/hooks/index.js +0 -17
- package/dist/index-BxSbmOMr.d.ts +0 -257
- package/dist/json-BVPssigK.d.ts +0 -39
- package/dist/middleware-Dp3d_pxi.d.ts +0 -90
- package/dist/request/index.d.ts +0 -28
- package/dist/request/index.js +0 -25
- package/dist/request/index.js.map +0 -1
- package/dist/tool-Cohdv5GC.d.ts +0 -65
- package/dist/types-BCTRUGex.d.ts +0 -47
- package/dist/types-BrLbKnEz.d.ts +0 -119
- package/dist/types-D9JpeAPF.d.ts +0 -114
- package/dist/types-G12i56s2.d.ts +0 -354
- package/dist/types-JJCxRnaZ.d.ts +0 -70
- package/dist/types-O-abTIin.d.ts +0 -79
- package/dist/ui/index.d.ts +0 -107
- package/dist/ui/index.js +0 -10
- package/dist/ui/index.js.map +0 -1
- /package/dist/{hooks → speech-generation}/index.js.map +0 -0
package/dist/chunk-BALBBVI3.js
DELETED
|
@@ -1,2732 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
toReadableStream
|
|
3
|
-
} from "./chunk-XUUY2L2D.js";
|
|
4
|
-
import {
|
|
5
|
-
parseToolArgs,
|
|
6
|
-
toolResultContentToText
|
|
7
|
-
} from "./chunk-HYUYZEAJ.js";
|
|
8
|
-
import {
|
|
9
|
-
runControl,
|
|
10
|
-
toolCallControl
|
|
11
|
-
} from "./chunk-2ODTMRHP.js";
|
|
12
|
-
import {
|
|
13
|
-
MemoryCompactionConflictError,
|
|
14
|
-
MemoryCompactionError,
|
|
15
|
-
createMemoryCompactionSummary,
|
|
16
|
-
cumulativeCompactedMessageCount
|
|
17
|
-
} from "./chunk-MRQLEK2B.js";
|
|
18
|
-
import {
|
|
19
|
-
mapWithConcurrency
|
|
20
|
-
} from "./chunk-OIMLU4SF.js";
|
|
21
|
-
import {
|
|
22
|
-
CompletionRequestBuilder
|
|
23
|
-
} from "./chunk-Q25IWLBT.js";
|
|
24
|
-
import {
|
|
25
|
-
isStreamingCompletionModel
|
|
26
|
-
} from "./chunk-MC3CCKEB.js";
|
|
27
|
-
import {
|
|
28
|
-
AssistantContent,
|
|
29
|
-
Message,
|
|
30
|
-
ToolContent,
|
|
31
|
-
Usage,
|
|
32
|
-
assertCompletionRequestSupported,
|
|
33
|
-
getAssistantGenerationMetadata,
|
|
34
|
-
textFromAssistantContent
|
|
35
|
-
} from "./chunk-ADH7NNCS.js";
|
|
36
|
-
import {
|
|
37
|
-
appendGuardrailPolicies,
|
|
38
|
-
hasEnforcedOutputGuardrails,
|
|
39
|
-
runInputGuardrails,
|
|
40
|
-
runOutputGuardrails
|
|
41
|
-
} from "./chunk-CWUJUSOS.js";
|
|
42
|
-
|
|
43
|
-
// src/request/errors.ts
|
|
44
|
-
var MaxTurnsError = class extends Error {
|
|
45
|
-
constructor(maxTurns, chatHistory, prompt) {
|
|
46
|
-
super(`Reached max turn limit: ${maxTurns}`);
|
|
47
|
-
this.maxTurns = maxTurns;
|
|
48
|
-
this.chatHistory = chatHistory;
|
|
49
|
-
this.prompt = prompt;
|
|
50
|
-
this.name = "MaxTurnsError";
|
|
51
|
-
}
|
|
52
|
-
maxTurns;
|
|
53
|
-
chatHistory;
|
|
54
|
-
prompt;
|
|
55
|
-
};
|
|
56
|
-
var PromptCancelledError = class extends Error {
|
|
57
|
-
constructor(chatHistory, reason) {
|
|
58
|
-
super(`Prompt cancelled: ${reason}`);
|
|
59
|
-
this.chatHistory = chatHistory;
|
|
60
|
-
this.reason = reason;
|
|
61
|
-
this.name = "PromptCancelledError";
|
|
62
|
-
}
|
|
63
|
-
chatHistory;
|
|
64
|
-
reason;
|
|
65
|
-
};
|
|
66
|
-
var ToolApprovalRequiredError = class extends Error {
|
|
67
|
-
constructor(request) {
|
|
68
|
-
super(`Tool approval required for ${request.toolName}, but no approval handler is configured.`);
|
|
69
|
-
this.request = request;
|
|
70
|
-
this.name = "ToolApprovalRequiredError";
|
|
71
|
-
}
|
|
72
|
-
request;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
// src/internal/async-queue.ts
|
|
76
|
-
function createAsyncQueue() {
|
|
77
|
-
const values = [];
|
|
78
|
-
const waiters = [];
|
|
79
|
-
let closed = false;
|
|
80
|
-
let error;
|
|
81
|
-
function flush() {
|
|
82
|
-
while (waiters.length > 0 && values.length > 0) {
|
|
83
|
-
const waiter = waiters.shift();
|
|
84
|
-
const value = values.shift();
|
|
85
|
-
if (waiter !== void 0) {
|
|
86
|
-
waiter.resolve({ value, done: false });
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (values.length > 0 || waiters.length === 0 || !closed) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
while (waiters.length > 0) {
|
|
93
|
-
const waiter = waiters.shift();
|
|
94
|
-
if (waiter === void 0) {
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
if (error !== void 0) {
|
|
98
|
-
waiter.reject(error);
|
|
99
|
-
} else {
|
|
100
|
-
waiter.resolve({ value: void 0, done: true });
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
enqueue(value) {
|
|
106
|
-
if (closed) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
values.push(value);
|
|
110
|
-
flush();
|
|
111
|
-
},
|
|
112
|
-
close() {
|
|
113
|
-
closed = true;
|
|
114
|
-
flush();
|
|
115
|
-
},
|
|
116
|
-
throw(thrown) {
|
|
117
|
-
closed = true;
|
|
118
|
-
error = thrown;
|
|
119
|
-
flush();
|
|
120
|
-
},
|
|
121
|
-
[Symbol.asyncIterator]() {
|
|
122
|
-
return {
|
|
123
|
-
next() {
|
|
124
|
-
if (values.length > 0) {
|
|
125
|
-
const value = values.shift();
|
|
126
|
-
return Promise.resolve({ value, done: false });
|
|
127
|
-
}
|
|
128
|
-
if (error !== void 0) {
|
|
129
|
-
return Promise.reject(error);
|
|
130
|
-
}
|
|
131
|
-
if (closed) {
|
|
132
|
-
return Promise.resolve({ value: void 0, done: true });
|
|
133
|
-
}
|
|
134
|
-
return new Promise((resolve, reject) => {
|
|
135
|
-
waiters.push({ resolve, reject });
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// src/internal/prompt-runtime/memory.ts
|
|
144
|
-
var PromptRequestMemory = class {
|
|
145
|
-
constructor(agent, memoryContext, initialHistory) {
|
|
146
|
-
this.agent = agent;
|
|
147
|
-
this.memoryContext = memoryContext;
|
|
148
|
-
this.initialHistory = initialHistory;
|
|
149
|
-
}
|
|
150
|
-
agent;
|
|
151
|
-
memoryContext;
|
|
152
|
-
initialHistory;
|
|
153
|
-
memoryPolicy() {
|
|
154
|
-
return this.memory()?.options.savePolicy;
|
|
155
|
-
}
|
|
156
|
-
pendingTurnMessages(newMessages) {
|
|
157
|
-
return this.memoryPolicy() === "turn" ? [...newMessages] : [];
|
|
158
|
-
}
|
|
159
|
-
async prepareRun(runId, newMessages) {
|
|
160
|
-
const memory = this.memory();
|
|
161
|
-
if (memory === void 0 || this.memoryContext === void 0) {
|
|
162
|
-
return {
|
|
163
|
-
history: this.initialHistory,
|
|
164
|
-
usage: Usage.empty()
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
const preparation = await this.prepareStoredHistory(memory, runId, newMessages.length);
|
|
168
|
-
const memoryHistory = preparation.history;
|
|
169
|
-
const chatHistory = [...memoryHistory, ...this.initialHistory];
|
|
170
|
-
if (memory.options.savePolicy === "message") {
|
|
171
|
-
await memory.store.append({
|
|
172
|
-
context: this.memoryContext,
|
|
173
|
-
runId,
|
|
174
|
-
turn: 1,
|
|
175
|
-
messages: newMessages
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
return {
|
|
179
|
-
...preparation,
|
|
180
|
-
history: chatHistory
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
async commitMessages(runId, turn, messages, pendingTurnMessages) {
|
|
184
|
-
const memory = this.memory();
|
|
185
|
-
if (memory === void 0 || this.memoryContext === void 0 || messages.length === 0) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
if (memory.options.savePolicy === "message") {
|
|
189
|
-
await memory.store.append({
|
|
190
|
-
context: this.memoryContext,
|
|
191
|
-
runId,
|
|
192
|
-
turn,
|
|
193
|
-
messages
|
|
194
|
-
});
|
|
195
|
-
} else if (memory.options.savePolicy === "turn") {
|
|
196
|
-
pendingTurnMessages.push(...messages);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
async commitCompletedTurn(runId, turn, pendingTurnMessages) {
|
|
200
|
-
const memory = this.memory();
|
|
201
|
-
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "turn" || pendingTurnMessages.length === 0) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
await memory.store.append({
|
|
205
|
-
context: this.memoryContext,
|
|
206
|
-
runId,
|
|
207
|
-
turn,
|
|
208
|
-
messages: [...pendingTurnMessages]
|
|
209
|
-
});
|
|
210
|
-
pendingTurnMessages.length = 0;
|
|
211
|
-
}
|
|
212
|
-
async commitCompletedRun(runId, turn, newMessages, pendingTurnMessages) {
|
|
213
|
-
await this.commitCompletedTurn(runId, turn, pendingTurnMessages);
|
|
214
|
-
const memory = this.memory();
|
|
215
|
-
if (memory === void 0 || this.memoryContext === void 0 || memory.options.savePolicy !== "run") {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
await memory.store.append({
|
|
219
|
-
context: this.memoryContext,
|
|
220
|
-
runId,
|
|
221
|
-
turn,
|
|
222
|
-
messages: [...newMessages]
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
async recordError(runId, error, newMessages) {
|
|
226
|
-
const memory = this.memory();
|
|
227
|
-
if (memory === void 0 || this.memoryContext === void 0) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
await memory.store.recordError?.({
|
|
231
|
-
context: this.memoryContext,
|
|
232
|
-
runId,
|
|
233
|
-
error,
|
|
234
|
-
messages: [...newMessages]
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
memory() {
|
|
238
|
-
return this.memoryContext === void 0 ? void 0 : this.agent.memory;
|
|
239
|
-
}
|
|
240
|
-
async prepareStoredHistory(memory, runId, incomingMessageCount) {
|
|
241
|
-
const context = this.memoryContext;
|
|
242
|
-
if (context === void 0) {
|
|
243
|
-
return { history: [], usage: Usage.empty() };
|
|
244
|
-
}
|
|
245
|
-
const options = memory.options.compaction;
|
|
246
|
-
const capability = memory.store.compaction;
|
|
247
|
-
if (options === void 0 || capability === void 0) {
|
|
248
|
-
return {
|
|
249
|
-
history: await memory.store.load(context),
|
|
250
|
-
usage: Usage.empty()
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
let usage = Usage.empty();
|
|
254
|
-
let conflictRetries = 0;
|
|
255
|
-
for (let attempt = 0; attempt <= options.conflictRetries; attempt += 1) {
|
|
256
|
-
const snapshot = await capability.load(context);
|
|
257
|
-
const compactedMessageCount = compactedPrefixLength(
|
|
258
|
-
snapshot.messages,
|
|
259
|
-
incomingMessageCount,
|
|
260
|
-
options.maxMessages,
|
|
261
|
-
options.keepRecentUserTurns
|
|
262
|
-
);
|
|
263
|
-
if (compactedMessageCount === 0) {
|
|
264
|
-
return { history: snapshot.messages, usage };
|
|
265
|
-
}
|
|
266
|
-
const prefix = snapshot.messages.slice(0, compactedMessageCount);
|
|
267
|
-
let result;
|
|
268
|
-
try {
|
|
269
|
-
result = await options.compactor({
|
|
270
|
-
context,
|
|
271
|
-
messages: prefix
|
|
272
|
-
});
|
|
273
|
-
} catch (error) {
|
|
274
|
-
throw remappedCompactorError(error, usage);
|
|
275
|
-
}
|
|
276
|
-
if (result.usage !== void 0) {
|
|
277
|
-
usage = Usage.add(usage, result.usage);
|
|
278
|
-
}
|
|
279
|
-
const summaryText = result.summary.trim();
|
|
280
|
-
if (summaryText.length === 0) {
|
|
281
|
-
throw new MemoryCompactionError("Memory compactor returned an empty summary.", {
|
|
282
|
-
usage
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
const summary = createMemoryCompactionSummary(
|
|
286
|
-
summaryText,
|
|
287
|
-
cumulativeCompactedMessageCount(prefix)
|
|
288
|
-
);
|
|
289
|
-
let commit;
|
|
290
|
-
try {
|
|
291
|
-
commit = await capability.commit({
|
|
292
|
-
context,
|
|
293
|
-
revision: snapshot.revision,
|
|
294
|
-
compactedMessageCount,
|
|
295
|
-
summary,
|
|
296
|
-
runId: `memory-compaction:${runId}:${attempt + 1}`
|
|
297
|
-
});
|
|
298
|
-
} catch (error) {
|
|
299
|
-
throw new MemoryCompactionError("Memory compaction store commit failed.", {
|
|
300
|
-
cause: error,
|
|
301
|
-
usage
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
if (commit === "committed") {
|
|
305
|
-
const retained = snapshot.messages.slice(compactedMessageCount);
|
|
306
|
-
return {
|
|
307
|
-
history: [summary, ...retained],
|
|
308
|
-
usage,
|
|
309
|
-
compaction: {
|
|
310
|
-
originalMessageCount: snapshot.messages.length,
|
|
311
|
-
compactedMessageCount,
|
|
312
|
-
retainedMessageCount: retained.length,
|
|
313
|
-
conflictRetries
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
conflictRetries += 1;
|
|
318
|
-
}
|
|
319
|
-
throw new MemoryCompactionConflictError(options.conflictRetries + 1, usage);
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
function compactedPrefixLength(messages, incomingMessageCount, maxMessages, keepRecentUserTurns) {
|
|
323
|
-
if (messages.length + incomingMessageCount <= maxMessages) {
|
|
324
|
-
return 0;
|
|
325
|
-
}
|
|
326
|
-
const userMessageIndexes = messages.flatMap(
|
|
327
|
-
(message, index) => message.role === "user" ? [index] : []
|
|
328
|
-
);
|
|
329
|
-
if (userMessageIndexes.length <= keepRecentUserTurns) {
|
|
330
|
-
return 0;
|
|
331
|
-
}
|
|
332
|
-
return userMessageIndexes[userMessageIndexes.length - keepRecentUserTurns] ?? 0;
|
|
333
|
-
}
|
|
334
|
-
function remappedCompactorError(error, usage) {
|
|
335
|
-
if (error instanceof MemoryCompactionError) {
|
|
336
|
-
const mergedUsage = error.usage === void 0 ? usage : isEmptyUsage(usage) ? error.usage : Usage.add(usage, error.usage);
|
|
337
|
-
if (mergedUsage === error.usage || error.usage === void 0 && isEmptyUsage(usage)) {
|
|
338
|
-
return error;
|
|
339
|
-
}
|
|
340
|
-
return new MemoryCompactionError(error.message, {
|
|
341
|
-
cause: error,
|
|
342
|
-
usage: mergedUsage
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
return new MemoryCompactionError("Memory compactor failed.", {
|
|
346
|
-
cause: error,
|
|
347
|
-
usage
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
function isEmptyUsage(usage) {
|
|
351
|
-
return usage.inputTokens === 0 && usage.outputTokens === 0 && usage.totalTokens === 0 && usage.cachedInputTokens === 0 && usage.cacheCreationInputTokens === 0;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// src/internal/prompt-runtime/retrieval.ts
|
|
355
|
-
async function fetchDynamicContext(agent, ragText) {
|
|
356
|
-
if (ragText === void 0 || ragText.length === 0 || agent.dynamicContexts.length === 0) {
|
|
357
|
-
return [];
|
|
358
|
-
}
|
|
359
|
-
const documents = [];
|
|
360
|
-
for (const registration of agent.dynamicContexts) {
|
|
361
|
-
const results = await registration.index.search({
|
|
362
|
-
query: ragText,
|
|
363
|
-
topK: registration.options.topK,
|
|
364
|
-
threshold: registration.options.threshold,
|
|
365
|
-
filter: registration.options.filter
|
|
366
|
-
});
|
|
367
|
-
for (const result of results) {
|
|
368
|
-
const formatted = registration.options.format?.(result);
|
|
369
|
-
if (formatted !== void 0) {
|
|
370
|
-
documents.push(formatted);
|
|
371
|
-
} else {
|
|
372
|
-
const metadata = formatMetadata(result.metadata);
|
|
373
|
-
const document = {
|
|
374
|
-
id: result.id,
|
|
375
|
-
text: typeof result.document === "string" ? result.document : JSON.stringify(result.document, null, 2)
|
|
376
|
-
};
|
|
377
|
-
if (metadata !== void 0) {
|
|
378
|
-
document.additionalProps = metadata;
|
|
379
|
-
}
|
|
380
|
-
documents.push(document);
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
return documents;
|
|
385
|
-
}
|
|
386
|
-
async function fetchToolDefinitions(agent, ragText) {
|
|
387
|
-
const staticDefinitions = await agent.toolSet.getToolDefinitions(ragText);
|
|
388
|
-
if (ragText === void 0 || ragText.length === 0 || agent.dynamicTools.length === 0) {
|
|
389
|
-
return staticDefinitions;
|
|
390
|
-
}
|
|
391
|
-
const definitions = [...staticDefinitions];
|
|
392
|
-
const names = new Set(staticDefinitions.map((definition) => definition.name));
|
|
393
|
-
for (const registration of agent.dynamicTools) {
|
|
394
|
-
const results = await registration.index.search({
|
|
395
|
-
query: ragText,
|
|
396
|
-
topK: registration.options.topK,
|
|
397
|
-
threshold: registration.options.threshold,
|
|
398
|
-
filter: registration.options.filter
|
|
399
|
-
});
|
|
400
|
-
for (const result of results) {
|
|
401
|
-
if (names.has(result.document.toolName)) {
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
names.add(result.document.toolName);
|
|
405
|
-
definitions.push(result.document.definition);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
return definitions;
|
|
409
|
-
}
|
|
410
|
-
function formatMetadata(metadata) {
|
|
411
|
-
if (metadata === void 0) {
|
|
412
|
-
return void 0;
|
|
413
|
-
}
|
|
414
|
-
return Object.fromEntries(Object.entries(metadata).map(([key, value]) => [key, String(value)]));
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// src/internal/prompt-runtime/stream-accumulator.ts
|
|
418
|
-
var CompletionStreamAccumulator = class {
|
|
419
|
-
orderedParts = [];
|
|
420
|
-
textParts = /* @__PURE__ */ new Map();
|
|
421
|
-
reasoningByKey = /* @__PURE__ */ new Map();
|
|
422
|
-
reasoningKeyById = /* @__PURE__ */ new Map();
|
|
423
|
-
toolCalls = /* @__PURE__ */ new Map();
|
|
424
|
-
sources = /* @__PURE__ */ new Map();
|
|
425
|
-
providerToolCalls = /* @__PURE__ */ new Map();
|
|
426
|
-
finalResponse;
|
|
427
|
-
messageId;
|
|
428
|
-
nextTextKey = 0;
|
|
429
|
-
nextReasoningKey = 0;
|
|
430
|
-
accept(event) {
|
|
431
|
-
if (event.type === "text_delta") {
|
|
432
|
-
this.appendText(event.delta);
|
|
433
|
-
return { type: "text_delta", delta: event.delta };
|
|
434
|
-
}
|
|
435
|
-
if (event.type === "reasoning_delta") {
|
|
436
|
-
const reasoning = this.reasoningStateForEvent(event);
|
|
437
|
-
this.appendReasoning(reasoning, event);
|
|
438
|
-
return reasoningDeltaEvent(event);
|
|
439
|
-
}
|
|
440
|
-
if (event.type === "tool_call_delta") {
|
|
441
|
-
const toolCall = this.toolCallStateForId(event.id);
|
|
442
|
-
if (event.callId !== void 0 && event.callId.length > 0) toolCall.callId = event.callId;
|
|
443
|
-
if (event.name !== void 0 && event.name.length > 0) toolCall.name = event.name;
|
|
444
|
-
if (event.signature !== void 0) toolCall.signature = event.signature;
|
|
445
|
-
if (event.argumentsDelta !== void 0) {
|
|
446
|
-
if (event.argumentsMode === "replace") {
|
|
447
|
-
toolCall.argumentsText = event.argumentsDelta;
|
|
448
|
-
} else {
|
|
449
|
-
toolCall.argumentsText += event.argumentsDelta;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
return void 0;
|
|
453
|
-
}
|
|
454
|
-
if (event.type === "tool_call") {
|
|
455
|
-
this.upsertToolCall(event.toolCall);
|
|
456
|
-
return { type: "tool_call", toolCall: event.toolCall };
|
|
457
|
-
}
|
|
458
|
-
if (event.type === "source") {
|
|
459
|
-
this.sources.set(sourceKey(event.source), event.source);
|
|
460
|
-
return { type: "source", source: event.source };
|
|
461
|
-
}
|
|
462
|
-
if (event.type === "provider_tool_call") {
|
|
463
|
-
this.providerToolCalls.set(event.toolCall.id, event.toolCall);
|
|
464
|
-
return { type: "provider_tool_call", toolCall: event.toolCall };
|
|
465
|
-
}
|
|
466
|
-
if (event.type === "message_id") {
|
|
467
|
-
this.messageId = event.id;
|
|
468
|
-
return void 0;
|
|
469
|
-
}
|
|
470
|
-
if (event.type === "final") {
|
|
471
|
-
this.finalResponse = event.response;
|
|
472
|
-
return void 0;
|
|
473
|
-
}
|
|
474
|
-
return void 0;
|
|
475
|
-
}
|
|
476
|
-
response() {
|
|
477
|
-
const accumulatedResponse = this.buildAccumulatedResponse();
|
|
478
|
-
if (this.finalResponse !== void 0) {
|
|
479
|
-
if (accumulatedResponse.choice.length === 0) {
|
|
480
|
-
return this.withAccumulatedArtifacts(this.finalResponse, accumulatedResponse);
|
|
481
|
-
}
|
|
482
|
-
return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
|
|
483
|
-
}
|
|
484
|
-
return accumulatedResponse;
|
|
485
|
-
}
|
|
486
|
-
buildAccumulatedResponse() {
|
|
487
|
-
const choice = [];
|
|
488
|
-
for (const part of this.orderedParts) {
|
|
489
|
-
if (part.type === "text") {
|
|
490
|
-
const text = this.textParts.get(part.key) ?? "";
|
|
491
|
-
if (text.length > 0) {
|
|
492
|
-
choice.push({ type: "text", text });
|
|
493
|
-
}
|
|
494
|
-
continue;
|
|
495
|
-
}
|
|
496
|
-
if (part.type === "reasoning") {
|
|
497
|
-
const reasoning = this.reasoningByKey.get(part.key);
|
|
498
|
-
if (reasoning !== void 0) {
|
|
499
|
-
choice.push(reasoningContent(reasoning));
|
|
500
|
-
}
|
|
501
|
-
continue;
|
|
502
|
-
}
|
|
503
|
-
const toolCall = this.toolCalls.get(part.key);
|
|
504
|
-
if (toolCall !== void 0) {
|
|
505
|
-
choice.push(
|
|
506
|
-
toolCallContent(toolCall, matchingFinalToolCall(toolCall, this.finalResponse?.choice))
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
const response = {
|
|
511
|
-
choice,
|
|
512
|
-
usage: Usage.empty(),
|
|
513
|
-
rawResponse: void 0
|
|
514
|
-
};
|
|
515
|
-
if (this.messageId !== void 0) {
|
|
516
|
-
response.messageId = this.messageId;
|
|
517
|
-
}
|
|
518
|
-
const sources = [...this.sources.values()];
|
|
519
|
-
if (sources.length > 0) {
|
|
520
|
-
response.sources = sources;
|
|
521
|
-
}
|
|
522
|
-
const providerToolCalls = [...this.providerToolCalls.values()];
|
|
523
|
-
if (providerToolCalls.length > 0) {
|
|
524
|
-
response.providerToolCalls = providerToolCalls;
|
|
525
|
-
}
|
|
526
|
-
return response;
|
|
527
|
-
}
|
|
528
|
-
upsertToolCall(toolCall) {
|
|
529
|
-
if (!this.toolCalls.has(toolCall.id)) {
|
|
530
|
-
this.orderedParts.push({ type: "tool_call", key: toolCall.id });
|
|
531
|
-
}
|
|
532
|
-
const partial = {
|
|
533
|
-
id: toolCall.id,
|
|
534
|
-
name: toolCall.function.name,
|
|
535
|
-
argumentsText: JSON.stringify(toolCall.function.arguments ?? {})
|
|
536
|
-
};
|
|
537
|
-
if (toolCall.callId !== void 0) {
|
|
538
|
-
partial.callId = toolCall.callId;
|
|
539
|
-
}
|
|
540
|
-
if (toolCall.signature !== void 0) {
|
|
541
|
-
partial.signature = toolCall.signature;
|
|
542
|
-
}
|
|
543
|
-
if (toolCall.additionalParams !== void 0) {
|
|
544
|
-
partial.additionalParams = toolCall.additionalParams;
|
|
545
|
-
}
|
|
546
|
-
this.toolCalls.set(toolCall.id, partial);
|
|
547
|
-
}
|
|
548
|
-
mergeFinalResponse(accumulatedResponse, finalResponse) {
|
|
549
|
-
if (finalResponse.choice.length === 0) {
|
|
550
|
-
const mergedResponse = {
|
|
551
|
-
...accumulatedResponse,
|
|
552
|
-
usage: finalResponse.usage,
|
|
553
|
-
rawResponse: finalResponse.rawResponse
|
|
554
|
-
};
|
|
555
|
-
if (finalResponse.messageId !== void 0) {
|
|
556
|
-
mergedResponse.messageId = finalResponse.messageId;
|
|
557
|
-
}
|
|
558
|
-
return this.withAccumulatedArtifacts(mergedResponse, accumulatedResponse);
|
|
559
|
-
}
|
|
560
|
-
const finalById = /* @__PURE__ */ new Map();
|
|
561
|
-
const finalByCallId = /* @__PURE__ */ new Map();
|
|
562
|
-
for (const content of finalResponse.choice) {
|
|
563
|
-
if (content.type !== "tool_call") {
|
|
564
|
-
continue;
|
|
565
|
-
}
|
|
566
|
-
finalById.set(content.id, content);
|
|
567
|
-
if (content.callId !== void 0) {
|
|
568
|
-
finalByCallId.set(content.callId, content);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
const matchedFinalToolCalls = /* @__PURE__ */ new Set();
|
|
572
|
-
const choice = accumulatedResponse.choice.map((content) => {
|
|
573
|
-
if (content.type !== "tool_call") {
|
|
574
|
-
return content;
|
|
575
|
-
}
|
|
576
|
-
const finalToolCall = finalById.get(content.id) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
|
|
577
|
-
if (finalToolCall === void 0) {
|
|
578
|
-
return content;
|
|
579
|
-
}
|
|
580
|
-
matchedFinalToolCalls.add(finalToolCall);
|
|
581
|
-
return mergeFinalToolCall(content, finalToolCall);
|
|
582
|
-
});
|
|
583
|
-
for (const content of finalResponse.choice) {
|
|
584
|
-
if (content.type !== "tool_call") {
|
|
585
|
-
continue;
|
|
586
|
-
}
|
|
587
|
-
if (!matchedFinalToolCalls.has(content)) {
|
|
588
|
-
choice.push(content);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
return this.withAccumulatedArtifacts({ ...finalResponse, choice }, accumulatedResponse);
|
|
592
|
-
}
|
|
593
|
-
appendText(delta) {
|
|
594
|
-
const lastPart = this.orderedParts.at(-1);
|
|
595
|
-
const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
|
|
596
|
-
if (lastPart?.type !== "text") {
|
|
597
|
-
this.orderedParts.push({ type: "text", key });
|
|
598
|
-
}
|
|
599
|
-
this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
|
|
600
|
-
}
|
|
601
|
-
reasoningStateForEvent(event) {
|
|
602
|
-
if (event.id !== void 0) {
|
|
603
|
-
const existingKey = this.reasoningKeyById.get(event.id);
|
|
604
|
-
if (existingKey !== void 0) {
|
|
605
|
-
const existing = this.reasoningByKey.get(existingKey);
|
|
606
|
-
if (existing !== void 0) {
|
|
607
|
-
return existing;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
const key2 = this.createReasoningKey();
|
|
611
|
-
const reasoning2 = { id: event.id, text: "" };
|
|
612
|
-
this.reasoningKeyById.set(event.id, key2);
|
|
613
|
-
this.reasoningByKey.set(key2, reasoning2);
|
|
614
|
-
this.orderedParts.push({ type: "reasoning", key: key2 });
|
|
615
|
-
return reasoning2;
|
|
616
|
-
}
|
|
617
|
-
const lastPart = this.orderedParts.at(-1);
|
|
618
|
-
if (lastPart?.type === "reasoning") {
|
|
619
|
-
const lastReasoning = this.reasoningByKey.get(lastPart.key);
|
|
620
|
-
if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
|
|
621
|
-
return lastReasoning;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
const key = this.createReasoningKey();
|
|
625
|
-
const reasoning = { text: "" };
|
|
626
|
-
this.reasoningByKey.set(key, reasoning);
|
|
627
|
-
this.orderedParts.push({ type: "reasoning", key });
|
|
628
|
-
return reasoning;
|
|
629
|
-
}
|
|
630
|
-
toolCallStateForId(id) {
|
|
631
|
-
const existing = this.toolCalls.get(id);
|
|
632
|
-
if (existing !== void 0) {
|
|
633
|
-
return existing;
|
|
634
|
-
}
|
|
635
|
-
const toolCall = {
|
|
636
|
-
id,
|
|
637
|
-
name: "",
|
|
638
|
-
argumentsText: ""
|
|
639
|
-
};
|
|
640
|
-
this.toolCalls.set(id, toolCall);
|
|
641
|
-
this.orderedParts.push({ type: "tool_call", key: id });
|
|
642
|
-
return toolCall;
|
|
643
|
-
}
|
|
644
|
-
withMessageIdFallback(response, accumulatedResponse) {
|
|
645
|
-
if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
|
|
646
|
-
return response;
|
|
647
|
-
}
|
|
648
|
-
return { ...response, messageId: accumulatedResponse.messageId };
|
|
649
|
-
}
|
|
650
|
-
withAccumulatedArtifacts(response, accumulatedResponse) {
|
|
651
|
-
const withMessageId = this.withMessageIdFallback(response, accumulatedResponse);
|
|
652
|
-
const sources = mergeSources(accumulatedResponse.sources, response.sources);
|
|
653
|
-
const providerToolCalls = mergeProviderToolCalls(
|
|
654
|
-
accumulatedResponse.providerToolCalls,
|
|
655
|
-
response.providerToolCalls
|
|
656
|
-
);
|
|
657
|
-
return {
|
|
658
|
-
...withMessageId,
|
|
659
|
-
...sources.length === 0 ? {} : { sources },
|
|
660
|
-
...providerToolCalls.length === 0 ? {} : { providerToolCalls }
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
createTextKey() {
|
|
664
|
-
this.nextTextKey += 1;
|
|
665
|
-
return `text_${this.nextTextKey.toString()}`;
|
|
666
|
-
}
|
|
667
|
-
createReasoningKey() {
|
|
668
|
-
this.nextReasoningKey += 1;
|
|
669
|
-
return `reasoning_${this.nextReasoningKey.toString()}`;
|
|
670
|
-
}
|
|
671
|
-
appendReasoning(reasoning, event) {
|
|
672
|
-
const contentType = event.contentType ?? "text";
|
|
673
|
-
if (contentType === "text" || contentType === "summary") {
|
|
674
|
-
reasoning.text += event.delta;
|
|
675
|
-
}
|
|
676
|
-
if (event.contentType === void 0 && event.signature === void 0) {
|
|
677
|
-
return;
|
|
678
|
-
}
|
|
679
|
-
reasoning.content ??= [];
|
|
680
|
-
const last = reasoning.content.at(-1);
|
|
681
|
-
if (contentType === "text") {
|
|
682
|
-
if (last?.type === "text") {
|
|
683
|
-
last.text += event.delta;
|
|
684
|
-
if (event.signature !== void 0) {
|
|
685
|
-
last.signature = event.signature;
|
|
686
|
-
}
|
|
687
|
-
} else {
|
|
688
|
-
reasoning.content.push(
|
|
689
|
-
event.signature === void 0 ? { type: "text", text: event.delta } : { type: "text", text: event.delta, signature: event.signature }
|
|
690
|
-
);
|
|
691
|
-
}
|
|
692
|
-
return;
|
|
693
|
-
}
|
|
694
|
-
if (contentType === "summary") {
|
|
695
|
-
if (last?.type === "summary") {
|
|
696
|
-
last.text += event.delta;
|
|
697
|
-
} else {
|
|
698
|
-
reasoning.content.push({ type: "summary", text: event.delta });
|
|
699
|
-
}
|
|
700
|
-
return;
|
|
701
|
-
}
|
|
702
|
-
if (contentType === "encrypted") {
|
|
703
|
-
reasoning.content.push({ type: "encrypted", data: event.delta });
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
reasoning.content.push({ type: "redacted", data: event.delta });
|
|
707
|
-
}
|
|
708
|
-
};
|
|
709
|
-
function sourceKey(source) {
|
|
710
|
-
return `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
|
|
711
|
-
}
|
|
712
|
-
function mergeSources(accumulated, final) {
|
|
713
|
-
const sources = /* @__PURE__ */ new Map();
|
|
714
|
-
for (const source of [...accumulated ?? [], ...final ?? []]) {
|
|
715
|
-
sources.set(sourceKey(source), source);
|
|
716
|
-
}
|
|
717
|
-
return [...sources.values()];
|
|
718
|
-
}
|
|
719
|
-
function mergeProviderToolCalls(accumulated, final) {
|
|
720
|
-
const toolCalls = /* @__PURE__ */ new Map();
|
|
721
|
-
for (const toolCall of [...accumulated ?? [], ...final ?? []]) {
|
|
722
|
-
toolCalls.set(toolCall.id, toolCall);
|
|
723
|
-
}
|
|
724
|
-
return [...toolCalls.values()];
|
|
725
|
-
}
|
|
726
|
-
function reasoningContent(reasoning) {
|
|
727
|
-
const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
|
|
728
|
-
return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
|
|
729
|
-
}
|
|
730
|
-
function toolCallContent(toolCall, finalToolCall) {
|
|
731
|
-
const argumentsValue = finalToolCall !== void 0 && !isEmptyToolArguments(finalToolCall.function.arguments) ? finalToolCall.function.arguments : parseToolArguments(toolCall.id, toolCall.argumentsText);
|
|
732
|
-
const content = {
|
|
733
|
-
type: "tool_call",
|
|
734
|
-
id: toolCall.id,
|
|
735
|
-
function: {
|
|
736
|
-
name: toolCall.name,
|
|
737
|
-
arguments: argumentsValue
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
if (toolCall.callId !== void 0) {
|
|
741
|
-
content.callId = toolCall.callId;
|
|
742
|
-
}
|
|
743
|
-
if (toolCall.signature !== void 0) {
|
|
744
|
-
content.signature = toolCall.signature;
|
|
745
|
-
}
|
|
746
|
-
if (toolCall.additionalParams !== void 0) {
|
|
747
|
-
content.additionalParams = toolCall.additionalParams;
|
|
748
|
-
}
|
|
749
|
-
return content;
|
|
750
|
-
}
|
|
751
|
-
function matchingFinalToolCall(accumulated, finalChoice) {
|
|
752
|
-
const byId = finalChoice?.find(
|
|
753
|
-
(content) => content.type === "tool_call" && content.id === accumulated.id
|
|
754
|
-
);
|
|
755
|
-
if (byId !== void 0 || accumulated.callId === void 0) {
|
|
756
|
-
return byId;
|
|
757
|
-
}
|
|
758
|
-
return finalChoice?.find(
|
|
759
|
-
(content) => content.type === "tool_call" && content.callId === accumulated.callId
|
|
760
|
-
);
|
|
761
|
-
}
|
|
762
|
-
function mergeFinalToolCall(accumulated, finalToolCall) {
|
|
763
|
-
const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
|
|
764
|
-
return {
|
|
765
|
-
...accumulated,
|
|
766
|
-
...finalToolCall,
|
|
767
|
-
function: {
|
|
768
|
-
...accumulated.function,
|
|
769
|
-
...finalToolCall.function,
|
|
770
|
-
arguments: argumentsValue
|
|
771
|
-
}
|
|
772
|
-
};
|
|
773
|
-
}
|
|
774
|
-
function reasoningDeltaEvent(event) {
|
|
775
|
-
const mapped = { type: "reasoning_delta", delta: event.delta };
|
|
776
|
-
if (event.id !== void 0) mapped.id = event.id;
|
|
777
|
-
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
778
|
-
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
779
|
-
return mapped;
|
|
780
|
-
}
|
|
781
|
-
function isEmptyToolArguments(value) {
|
|
782
|
-
if (value === void 0 || value === null) {
|
|
783
|
-
return true;
|
|
784
|
-
}
|
|
785
|
-
if (typeof value === "string") {
|
|
786
|
-
return value.trim().length === 0;
|
|
787
|
-
}
|
|
788
|
-
if (Array.isArray(value)) {
|
|
789
|
-
return value.length === 0;
|
|
790
|
-
}
|
|
791
|
-
if (typeof value === "object") {
|
|
792
|
-
return Object.values(value).every((item) => item === void 0);
|
|
793
|
-
}
|
|
794
|
-
return false;
|
|
795
|
-
}
|
|
796
|
-
function parseToolArguments(toolCallId, text) {
|
|
797
|
-
if (text.trim().length === 0) {
|
|
798
|
-
return {};
|
|
799
|
-
}
|
|
800
|
-
try {
|
|
801
|
-
return JSON.parse(text);
|
|
802
|
-
} catch {
|
|
803
|
-
throw new Error(
|
|
804
|
-
`Completion returned tool call "${toolCallId}" with malformed JSON arguments; this indicates invalid provider output or incomplete stream assembly.`
|
|
805
|
-
);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
// src/internal/prompt-runtime/stream-events.ts
|
|
810
|
-
function addTurn(turn, event) {
|
|
811
|
-
if (event.type === "text_delta") {
|
|
812
|
-
return { type: "text_delta", turn, delta: event.delta };
|
|
813
|
-
}
|
|
814
|
-
if (event.type === "reasoning_delta") {
|
|
815
|
-
const mapped = { type: "reasoning_delta", turn, delta: event.delta };
|
|
816
|
-
if (event.id !== void 0) mapped.id = event.id;
|
|
817
|
-
if (event.contentType !== void 0) mapped.contentType = event.contentType;
|
|
818
|
-
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
819
|
-
return mapped;
|
|
820
|
-
}
|
|
821
|
-
if (event.type === "source") {
|
|
822
|
-
return { type: "source", turn, source: event.source };
|
|
823
|
-
}
|
|
824
|
-
if (event.type === "provider_tool_call") {
|
|
825
|
-
return { type: "provider_tool_call", turn, toolCall: event.toolCall };
|
|
826
|
-
}
|
|
827
|
-
return { type: "tool_call", turn, toolCall: event.toolCall };
|
|
828
|
-
}
|
|
829
|
-
function addTurnToToolCallDelta(turn, event) {
|
|
830
|
-
const mapped = {
|
|
831
|
-
type: "tool_call_delta",
|
|
832
|
-
turn,
|
|
833
|
-
id: event.id
|
|
834
|
-
};
|
|
835
|
-
if (event.callId !== void 0) mapped.callId = event.callId;
|
|
836
|
-
if (event.name !== void 0) mapped.name = event.name;
|
|
837
|
-
if (event.argumentsDelta !== void 0) mapped.argumentsDelta = event.argumentsDelta;
|
|
838
|
-
if (event.argumentsMode !== void 0) mapped.argumentsMode = event.argumentsMode;
|
|
839
|
-
if (event.signature !== void 0) mapped.signature = event.signature;
|
|
840
|
-
return mapped;
|
|
841
|
-
}
|
|
842
|
-
function isGenerationDeltaEvent(type) {
|
|
843
|
-
return type === "text_delta" || type === "reasoning_delta" || type === "tool_call_delta" || type === "tool_call" || type === "source" || type === "provider_tool_call";
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
// src/internal/prompt-runtime/tool-execution.ts
|
|
847
|
-
var MCP_TOOL_METADATA_KEY = /* @__PURE__ */ Symbol.for("anvia.mcp.tool.metadata");
|
|
848
|
-
var ToolCallExecutor = class {
|
|
849
|
-
constructor(agent, activeHook, approvals, runContext, concurrency, requestMiddlewares, includeToolCallDeltas, cancel) {
|
|
850
|
-
this.agent = agent;
|
|
851
|
-
this.activeHook = activeHook;
|
|
852
|
-
this.approvals = approvals;
|
|
853
|
-
this.runContext = runContext;
|
|
854
|
-
this.concurrency = concurrency;
|
|
855
|
-
this.requestMiddlewares = requestMiddlewares;
|
|
856
|
-
this.includeToolCallDeltas = includeToolCallDeltas;
|
|
857
|
-
this.cancel = cancel;
|
|
858
|
-
}
|
|
859
|
-
agent;
|
|
860
|
-
activeHook;
|
|
861
|
-
approvals;
|
|
862
|
-
runContext;
|
|
863
|
-
concurrency;
|
|
864
|
-
requestMiddlewares;
|
|
865
|
-
includeToolCallDeltas;
|
|
866
|
-
cancel;
|
|
867
|
-
async execute(toolCalls, onResult, onStreamEvent, observation) {
|
|
868
|
-
for (const toolCall of toolCalls) {
|
|
869
|
-
if (toolCall.function.name.length === 0) {
|
|
870
|
-
throw new Error(
|
|
871
|
-
`Completion returned tool call "${toolCall.id}" with an empty function name; this indicates invalid provider output or provider mapping.`
|
|
872
|
-
);
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
return mapWithConcurrency(toolCalls, this.concurrency, async (toolCall) => {
|
|
876
|
-
const args = JSON.stringify(toolCall.function.arguments ?? {});
|
|
877
|
-
const internalCallId = globalThis.crypto.randomUUID();
|
|
878
|
-
const hookArgs = {
|
|
879
|
-
toolName: toolCall.function.name,
|
|
880
|
-
internalCallId,
|
|
881
|
-
args
|
|
882
|
-
};
|
|
883
|
-
if (toolCall.callId !== void 0) {
|
|
884
|
-
hookArgs.toolCallId = toolCall.callId;
|
|
885
|
-
}
|
|
886
|
-
const tool = this.agent.getTool(toolCall.function.name);
|
|
887
|
-
const toolDefinition = observation?.toolDefinitions?.find(
|
|
888
|
-
(definition) => definition.name === toolCall.function.name
|
|
889
|
-
);
|
|
890
|
-
const toolMetadata = toolTraceMetadata(tool);
|
|
891
|
-
const toolStartArgs = {
|
|
892
|
-
turn: observation?.turn ?? 0,
|
|
893
|
-
toolCall,
|
|
894
|
-
toolName: toolCall.function.name,
|
|
895
|
-
internalCallId,
|
|
896
|
-
args
|
|
897
|
-
};
|
|
898
|
-
if (toolCall.callId !== void 0) {
|
|
899
|
-
toolStartArgs.toolCallId = toolCall.callId;
|
|
900
|
-
}
|
|
901
|
-
if (toolDefinition !== void 0) {
|
|
902
|
-
toolStartArgs.toolDefinition = toolDefinition;
|
|
903
|
-
}
|
|
904
|
-
if (toolMetadata !== void 0) {
|
|
905
|
-
toolStartArgs.toolMetadata = toolMetadata;
|
|
906
|
-
}
|
|
907
|
-
const toolObservers = await observation?.runObservers.startTool(toolStartArgs);
|
|
908
|
-
let output;
|
|
909
|
-
let skipped = false;
|
|
910
|
-
let effectiveArgs = args;
|
|
911
|
-
const callAction = await this.activeHook?.onToolCall?.({
|
|
912
|
-
...hookArgs,
|
|
913
|
-
tool: toolCallControl
|
|
914
|
-
});
|
|
915
|
-
if (callAction?.type === "terminate") {
|
|
916
|
-
await recordToolError(
|
|
917
|
-
toolObservers,
|
|
918
|
-
observation?.turn,
|
|
919
|
-
toolCall,
|
|
920
|
-
internalCallId,
|
|
921
|
-
effectiveArgs,
|
|
922
|
-
callAction.reason
|
|
923
|
-
);
|
|
924
|
-
throw this.cancel(callAction.reason);
|
|
925
|
-
}
|
|
926
|
-
if (callAction?.type === "skip") {
|
|
927
|
-
output = callAction.reason;
|
|
928
|
-
skipped = true;
|
|
929
|
-
} else {
|
|
930
|
-
let approvalDecision;
|
|
931
|
-
try {
|
|
932
|
-
effectiveArgs = await this.runToolInputMiddlewares({
|
|
933
|
-
...hookArgs,
|
|
934
|
-
turn: observation?.turn ?? 0,
|
|
935
|
-
originalArgs: args
|
|
936
|
-
});
|
|
937
|
-
hookArgs.args = effectiveArgs;
|
|
938
|
-
approvalDecision = callAction?.type === "approval_request" ? await this.requestApproval(tool, hookArgs, callAction) : await this.evaluateToolApproval(tool, hookArgs) ?? { approved: true };
|
|
939
|
-
if (!approvalDecision.approved) {
|
|
940
|
-
output = approvalDecision.result;
|
|
941
|
-
skipped = true;
|
|
942
|
-
} else {
|
|
943
|
-
output = await this.runApprovedToolCall(
|
|
944
|
-
toolCall,
|
|
945
|
-
hookArgs,
|
|
946
|
-
effectiveArgs,
|
|
947
|
-
args,
|
|
948
|
-
toolObservers,
|
|
949
|
-
observation,
|
|
950
|
-
onStreamEvent,
|
|
951
|
-
false
|
|
952
|
-
);
|
|
953
|
-
effectiveArgs = hookArgs.args;
|
|
954
|
-
}
|
|
955
|
-
} catch (error) {
|
|
956
|
-
await recordToolError(
|
|
957
|
-
toolObservers,
|
|
958
|
-
observation?.turn,
|
|
959
|
-
toolCall,
|
|
960
|
-
internalCallId,
|
|
961
|
-
effectiveArgs,
|
|
962
|
-
error
|
|
963
|
-
);
|
|
964
|
-
throw error;
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
let result = toolOutputToText(output);
|
|
968
|
-
let structuredResult = toolOutputToStructuredResult(output);
|
|
969
|
-
if (this.agent.shouldApplyToolMiddleware(toolCall.function.name)) {
|
|
970
|
-
const middlewareReplacement = await this.runToolResultMiddlewares({
|
|
971
|
-
...hookArgs,
|
|
972
|
-
args: effectiveArgs,
|
|
973
|
-
result,
|
|
974
|
-
originalResult: result,
|
|
975
|
-
structuredResult,
|
|
976
|
-
originalStructuredResult: structuredResult,
|
|
977
|
-
turn: observation?.turn ?? 0
|
|
978
|
-
});
|
|
979
|
-
if (middlewareReplacement !== void 0) {
|
|
980
|
-
output = middlewareReplacement;
|
|
981
|
-
result = toolOutputToText(middlewareReplacement);
|
|
982
|
-
structuredResult = toolOutputToStructuredResult(middlewareReplacement);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
const resultAction = await this.activeHook?.onToolResult?.({
|
|
986
|
-
...hookArgs,
|
|
987
|
-
args: effectiveArgs,
|
|
988
|
-
result,
|
|
989
|
-
structuredResult,
|
|
990
|
-
run: runControl
|
|
991
|
-
});
|
|
992
|
-
await toolObservers?.end({
|
|
993
|
-
turn: observation?.turn ?? 0,
|
|
994
|
-
toolCall,
|
|
995
|
-
toolName: toolCall.function.name,
|
|
996
|
-
internalCallId,
|
|
997
|
-
args: effectiveArgs,
|
|
998
|
-
result,
|
|
999
|
-
structuredResult,
|
|
1000
|
-
skipped,
|
|
1001
|
-
toolCallId: toolCall.callId
|
|
1002
|
-
});
|
|
1003
|
-
if (resultAction?.type === "terminate") {
|
|
1004
|
-
throw this.cancel(resultAction.reason);
|
|
1005
|
-
}
|
|
1006
|
-
const resultPayload = {
|
|
1007
|
-
type: "tool_result",
|
|
1008
|
-
toolName: toolCall.function.name,
|
|
1009
|
-
internalCallId,
|
|
1010
|
-
args: effectiveArgs,
|
|
1011
|
-
result,
|
|
1012
|
-
structuredResult
|
|
1013
|
-
};
|
|
1014
|
-
if (toolCall.callId !== void 0) {
|
|
1015
|
-
resultPayload.toolCallId = toolCall.callId;
|
|
1016
|
-
}
|
|
1017
|
-
onResult?.(resultPayload);
|
|
1018
|
-
return ToolContent.toolResult(toolCall.id, output, {
|
|
1019
|
-
callId: toolCall.callId,
|
|
1020
|
-
toolName: toolCall.function.name
|
|
1021
|
-
});
|
|
1022
|
-
});
|
|
1023
|
-
}
|
|
1024
|
-
async runApprovedToolCall(toolCall, hookArgs, effectiveArgs, originalArgs, toolObservers, observation, onStreamEvent, applyInputMiddleware = true) {
|
|
1025
|
-
const middlewareArgs = applyInputMiddleware ? await this.runToolInputMiddlewares({
|
|
1026
|
-
...hookArgs,
|
|
1027
|
-
args: effectiveArgs,
|
|
1028
|
-
turn: observation?.turn ?? 0,
|
|
1029
|
-
originalArgs
|
|
1030
|
-
}) : effectiveArgs;
|
|
1031
|
-
hookArgs.args = middlewareArgs;
|
|
1032
|
-
try {
|
|
1033
|
-
const toolContext = {
|
|
1034
|
-
emitStreamEvent: async (event) => {
|
|
1035
|
-
const streamEventArgs = {
|
|
1036
|
-
turn: observation?.turn ?? 0,
|
|
1037
|
-
toolCall,
|
|
1038
|
-
toolName: toolCall.function.name,
|
|
1039
|
-
internalCallId: hookArgs.internalCallId,
|
|
1040
|
-
args: middlewareArgs,
|
|
1041
|
-
event
|
|
1042
|
-
};
|
|
1043
|
-
if (toolCall.callId !== void 0) {
|
|
1044
|
-
streamEventArgs.toolCallId = toolCall.callId;
|
|
1045
|
-
}
|
|
1046
|
-
await toolObservers?.streamEvent(streamEventArgs);
|
|
1047
|
-
const payload = agentToolEventPayload(toolCall, hookArgs.internalCallId, event);
|
|
1048
|
-
if (payload !== void 0) {
|
|
1049
|
-
onStreamEvent?.(payload);
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
};
|
|
1053
|
-
toolContext.includeToolCallDeltas = this.includeToolCallDeltas;
|
|
1054
|
-
return await this.agent.callTool(toolCall.function.name, middlewareArgs, toolContext);
|
|
1055
|
-
} catch (error) {
|
|
1056
|
-
const errorAction = await this.activeHook?.onToolError?.({
|
|
1057
|
-
...hookArgs,
|
|
1058
|
-
args: middlewareArgs,
|
|
1059
|
-
error,
|
|
1060
|
-
run: runControl
|
|
1061
|
-
});
|
|
1062
|
-
await toolObservers?.error(
|
|
1063
|
-
toolErrorArgs(
|
|
1064
|
-
observation?.turn ?? 0,
|
|
1065
|
-
toolCall,
|
|
1066
|
-
hookArgs.internalCallId,
|
|
1067
|
-
middlewareArgs,
|
|
1068
|
-
error
|
|
1069
|
-
)
|
|
1070
|
-
);
|
|
1071
|
-
if (errorAction?.type === "terminate") {
|
|
1072
|
-
throw this.cancel(errorAction.reason);
|
|
1073
|
-
}
|
|
1074
|
-
return error instanceof Error ? error.toString() : String(error);
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
async runToolResultMiddlewares(args) {
|
|
1078
|
-
let result = args.result;
|
|
1079
|
-
let structuredResult = args.structuredResult;
|
|
1080
|
-
let replaced = false;
|
|
1081
|
-
for (const middleware of this.activeMiddlewares()) {
|
|
1082
|
-
const outputReplacement = await middleware.onToolOutput?.({
|
|
1083
|
-
...args,
|
|
1084
|
-
result,
|
|
1085
|
-
structuredResult
|
|
1086
|
-
});
|
|
1087
|
-
if (outputReplacement !== void 0) {
|
|
1088
|
-
const normalized = normalizeToolOutputMiddlewareResult(outputReplacement);
|
|
1089
|
-
if (normalized.result !== void 0) {
|
|
1090
|
-
result = normalized.result;
|
|
1091
|
-
structuredResult = void 0;
|
|
1092
|
-
}
|
|
1093
|
-
if (normalized.structuredResult !== void 0) {
|
|
1094
|
-
structuredResult = normalized.structuredResult;
|
|
1095
|
-
result = toolResultContentToText(normalized.structuredResult);
|
|
1096
|
-
}
|
|
1097
|
-
replaced = true;
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
return replaced ? structuredResult ?? result : void 0;
|
|
1101
|
-
}
|
|
1102
|
-
async runToolInputMiddlewares(args) {
|
|
1103
|
-
let current = args.args;
|
|
1104
|
-
for (const middleware of this.activeMiddlewares()) {
|
|
1105
|
-
const replacement = await middleware.onToolInput?.({
|
|
1106
|
-
...args,
|
|
1107
|
-
args: current
|
|
1108
|
-
});
|
|
1109
|
-
if (replacement?.args !== void 0) {
|
|
1110
|
-
current = typeof replacement.args === "string" ? replacement.args : JSON.stringify(replacement.args);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
return current;
|
|
1114
|
-
}
|
|
1115
|
-
activeMiddlewares() {
|
|
1116
|
-
return [...this.agent.middlewares, ...this.requestMiddlewares];
|
|
1117
|
-
}
|
|
1118
|
-
async evaluateToolApproval(tool, hookArgs) {
|
|
1119
|
-
if (tool?.approval === void 0) {
|
|
1120
|
-
return void 0;
|
|
1121
|
-
}
|
|
1122
|
-
const policy = tool.approval;
|
|
1123
|
-
const context = approvalContext(tool, hookArgs, this.agent, this.runContext);
|
|
1124
|
-
const required = await policy.when(context);
|
|
1125
|
-
if (!required) {
|
|
1126
|
-
return { approved: true };
|
|
1127
|
-
}
|
|
1128
|
-
const reason = await resolveApprovalText(policy.reason, context);
|
|
1129
|
-
const rejectMessage = await resolveApprovalText(policy.rejectMessage, context);
|
|
1130
|
-
const options = {};
|
|
1131
|
-
if (reason !== void 0) {
|
|
1132
|
-
options.reason = reason;
|
|
1133
|
-
}
|
|
1134
|
-
if (rejectMessage !== void 0) {
|
|
1135
|
-
options.rejectMessage = rejectMessage;
|
|
1136
|
-
}
|
|
1137
|
-
return this.requestApproval(tool, hookArgs, options);
|
|
1138
|
-
}
|
|
1139
|
-
async requestApproval(tool, hookArgs, options) {
|
|
1140
|
-
const request = approvalContext(
|
|
1141
|
-
tool,
|
|
1142
|
-
hookArgs,
|
|
1143
|
-
this.agent,
|
|
1144
|
-
this.runContext
|
|
1145
|
-
);
|
|
1146
|
-
if (options.reason !== void 0) {
|
|
1147
|
-
request.reason = options.reason;
|
|
1148
|
-
}
|
|
1149
|
-
if (options.rejectMessage !== void 0) {
|
|
1150
|
-
request.rejectMessage = options.rejectMessage;
|
|
1151
|
-
}
|
|
1152
|
-
if (this.approvals === void 0) {
|
|
1153
|
-
throw new ToolApprovalRequiredError(request);
|
|
1154
|
-
}
|
|
1155
|
-
const decision = normalizeApprovalDecision(await this.approvals.handler(request));
|
|
1156
|
-
if (decision.approved) {
|
|
1157
|
-
return { approved: true };
|
|
1158
|
-
}
|
|
1159
|
-
return {
|
|
1160
|
-
approved: false,
|
|
1161
|
-
result: decision.rejectMessage ?? decision.reason ?? request.rejectMessage ?? "Tool approval was rejected."
|
|
1162
|
-
};
|
|
1163
|
-
}
|
|
1164
|
-
};
|
|
1165
|
-
function approvalContext(tool, hookArgs, agent, run) {
|
|
1166
|
-
const rawParsedArgs = parseToolArgs(hookArgs.args);
|
|
1167
|
-
const parsedArgs = tool?.parseApprovalArgs?.(rawParsedArgs) ?? rawParsedArgs;
|
|
1168
|
-
const approvalRun = {
|
|
1169
|
-
agentId: agent.id,
|
|
1170
|
-
runId: run.runId
|
|
1171
|
-
};
|
|
1172
|
-
if (run.sessionId !== void 0) {
|
|
1173
|
-
approvalRun.sessionId = run.sessionId;
|
|
1174
|
-
}
|
|
1175
|
-
if (run.metadata !== void 0) {
|
|
1176
|
-
approvalRun.metadata = run.metadata;
|
|
1177
|
-
}
|
|
1178
|
-
const context = {
|
|
1179
|
-
toolName: hookArgs.toolName,
|
|
1180
|
-
args: parsedArgs,
|
|
1181
|
-
rawArgs: hookArgs.args,
|
|
1182
|
-
internalCallId: hookArgs.internalCallId,
|
|
1183
|
-
run: approvalRun
|
|
1184
|
-
};
|
|
1185
|
-
if (hookArgs.toolCallId !== void 0) {
|
|
1186
|
-
context.toolCallId = hookArgs.toolCallId;
|
|
1187
|
-
}
|
|
1188
|
-
return context;
|
|
1189
|
-
}
|
|
1190
|
-
function normalizeApprovalDecision(decision) {
|
|
1191
|
-
if (typeof decision === "boolean") {
|
|
1192
|
-
return { approved: decision };
|
|
1193
|
-
}
|
|
1194
|
-
return decision;
|
|
1195
|
-
}
|
|
1196
|
-
async function resolveApprovalText(value, context) {
|
|
1197
|
-
return typeof value === "function" ? value(context) : value;
|
|
1198
|
-
}
|
|
1199
|
-
function normalizeToolOutputMiddlewareResult(result) {
|
|
1200
|
-
if (typeof result === "string") {
|
|
1201
|
-
return { result };
|
|
1202
|
-
}
|
|
1203
|
-
return result ?? {};
|
|
1204
|
-
}
|
|
1205
|
-
function toolTraceMetadata(tool) {
|
|
1206
|
-
if (tool === void 0) {
|
|
1207
|
-
return void 0;
|
|
1208
|
-
}
|
|
1209
|
-
const metadata = tool[MCP_TOOL_METADATA_KEY];
|
|
1210
|
-
const mcpMetadata = typeof metadata === "object" && metadata !== null ? metadata : void 0;
|
|
1211
|
-
const result = {
|
|
1212
|
-
approvalRequired: tool.approval !== void 0
|
|
1213
|
-
};
|
|
1214
|
-
if (typeof mcpMetadata?.serverName === "string" && mcpMetadata.serverName.length > 0) {
|
|
1215
|
-
result.mcpServerName = mcpMetadata.serverName;
|
|
1216
|
-
}
|
|
1217
|
-
return result;
|
|
1218
|
-
}
|
|
1219
|
-
function toolErrorArgs(turn, toolCall, internalCallId, args, error) {
|
|
1220
|
-
const observerArgs = {
|
|
1221
|
-
turn,
|
|
1222
|
-
toolCall,
|
|
1223
|
-
toolName: toolCall.function.name,
|
|
1224
|
-
internalCallId,
|
|
1225
|
-
args,
|
|
1226
|
-
error
|
|
1227
|
-
};
|
|
1228
|
-
if (toolCall.callId !== void 0) {
|
|
1229
|
-
observerArgs.toolCallId = toolCall.callId;
|
|
1230
|
-
}
|
|
1231
|
-
return observerArgs;
|
|
1232
|
-
}
|
|
1233
|
-
async function recordToolError(toolObservers, turn, toolCall, internalCallId, args, error) {
|
|
1234
|
-
await toolObservers?.error({
|
|
1235
|
-
turn: turn ?? 0,
|
|
1236
|
-
toolCall,
|
|
1237
|
-
toolName: toolCall.function.name,
|
|
1238
|
-
internalCallId,
|
|
1239
|
-
args,
|
|
1240
|
-
error,
|
|
1241
|
-
toolCallId: toolCall.callId
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
function toolOutputToText(output) {
|
|
1245
|
-
return typeof output === "string" ? output : toolResultContentToText(output);
|
|
1246
|
-
}
|
|
1247
|
-
function toolOutputToStructuredResult(output) {
|
|
1248
|
-
return typeof output === "string" ? void 0 : output;
|
|
1249
|
-
}
|
|
1250
|
-
function agentToolEventPayload(toolCall, internalCallId, event) {
|
|
1251
|
-
if (typeof event.agentId !== "string" || event.agentId.length === 0) {
|
|
1252
|
-
return void 0;
|
|
1253
|
-
}
|
|
1254
|
-
const payload = {
|
|
1255
|
-
type: "agent_tool_event",
|
|
1256
|
-
toolName: toolCall.function.name,
|
|
1257
|
-
internalCallId,
|
|
1258
|
-
agentId: event.agentId,
|
|
1259
|
-
event: event.event
|
|
1260
|
-
};
|
|
1261
|
-
if (toolCall.callId !== void 0) {
|
|
1262
|
-
payload.toolCallId = toolCall.callId;
|
|
1263
|
-
}
|
|
1264
|
-
if (event.agentName !== void 0) {
|
|
1265
|
-
payload.agentName = event.agentName;
|
|
1266
|
-
}
|
|
1267
|
-
return payload;
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
// src/internal/rag-text.ts
|
|
1271
|
-
function extractRagText(message) {
|
|
1272
|
-
if (message.role === "user") {
|
|
1273
|
-
return message.content.flatMap((item) => item.type === "text" ? [item.text] : []).join("\n");
|
|
1274
|
-
}
|
|
1275
|
-
if (message.role === "tool") {
|
|
1276
|
-
return message.content.flatMap((item) => item.content.flatMap((part) => part.type === "text" ? [part.text] : [])).join("\n");
|
|
1277
|
-
}
|
|
1278
|
-
return void 0;
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
// src/observability/group.ts
|
|
1282
|
-
async function startAgentRunObservers(registrations, args, failOnObserverError) {
|
|
1283
|
-
const runObservers = [];
|
|
1284
|
-
for (const registration of registrations) {
|
|
1285
|
-
try {
|
|
1286
|
-
const runObserver = await registration.observer.startRun(args);
|
|
1287
|
-
if (runObserver !== void 0) {
|
|
1288
|
-
runObservers.push(runObserver);
|
|
1289
|
-
}
|
|
1290
|
-
} catch (error) {
|
|
1291
|
-
if (failOnObserverError || registration.failOnObserverError === true) {
|
|
1292
|
-
throw error;
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
1296
|
-
return new ActiveAgentRunObservers(runObservers, failOnObserverError);
|
|
1297
|
-
}
|
|
1298
|
-
var ActiveAgentRunObservers = class {
|
|
1299
|
-
constructor(runObservers, failOnObserverError) {
|
|
1300
|
-
this.runObservers = runObservers;
|
|
1301
|
-
this.failOnObserverError = failOnObserverError;
|
|
1302
|
-
this.trace = runObservers.find((observer) => observer.trace !== void 0)?.trace;
|
|
1303
|
-
}
|
|
1304
|
-
runObservers;
|
|
1305
|
-
failOnObserverError;
|
|
1306
|
-
trace;
|
|
1307
|
-
async startGeneration(args) {
|
|
1308
|
-
const generationObservers = [];
|
|
1309
|
-
for (const runObserver of this.runObservers) {
|
|
1310
|
-
if (runObserver.startGeneration === void 0) {
|
|
1311
|
-
continue;
|
|
1312
|
-
}
|
|
1313
|
-
try {
|
|
1314
|
-
const generationObserver = await runObserver.startGeneration(args);
|
|
1315
|
-
if (generationObserver !== void 0) {
|
|
1316
|
-
generationObservers.push(generationObserver);
|
|
1317
|
-
}
|
|
1318
|
-
} catch (error) {
|
|
1319
|
-
this.handleError(error);
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
return new ActiveGenerationObservers(generationObservers, this.failOnObserverError);
|
|
1323
|
-
}
|
|
1324
|
-
async startTool(args) {
|
|
1325
|
-
const toolObservers = [];
|
|
1326
|
-
for (const runObserver of this.runObservers) {
|
|
1327
|
-
if (runObserver.startTool === void 0) {
|
|
1328
|
-
continue;
|
|
1329
|
-
}
|
|
1330
|
-
try {
|
|
1331
|
-
const toolObserver = await runObserver.startTool(args);
|
|
1332
|
-
if (toolObserver !== void 0) {
|
|
1333
|
-
toolObservers.push(toolObserver);
|
|
1334
|
-
}
|
|
1335
|
-
} catch (error) {
|
|
1336
|
-
this.handleError(error);
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
return new ActiveToolObservers(toolObservers, this.failOnObserverError);
|
|
1340
|
-
}
|
|
1341
|
-
async end(args) {
|
|
1342
|
-
for (const runObserver of this.runObservers) {
|
|
1343
|
-
try {
|
|
1344
|
-
await runObserver.end(args);
|
|
1345
|
-
} catch (error) {
|
|
1346
|
-
this.handleError(error);
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
}
|
|
1350
|
-
async error(args) {
|
|
1351
|
-
for (const runObserver of this.runObservers) {
|
|
1352
|
-
if (runObserver.error === void 0) {
|
|
1353
|
-
continue;
|
|
1354
|
-
}
|
|
1355
|
-
try {
|
|
1356
|
-
await runObserver.error(args);
|
|
1357
|
-
} catch (error) {
|
|
1358
|
-
this.handleError(error);
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
async event(args) {
|
|
1363
|
-
for (const runObserver of this.runObservers) {
|
|
1364
|
-
if (runObserver.event === void 0) {
|
|
1365
|
-
continue;
|
|
1366
|
-
}
|
|
1367
|
-
try {
|
|
1368
|
-
await runObserver.event(args);
|
|
1369
|
-
} catch (error) {
|
|
1370
|
-
this.handleError(error);
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
handleError(error) {
|
|
1375
|
-
if (this.failOnObserverError) {
|
|
1376
|
-
throw error;
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
};
|
|
1380
|
-
var ActiveGenerationObservers = class {
|
|
1381
|
-
constructor(generationObservers, failOnObserverError) {
|
|
1382
|
-
this.generationObservers = generationObservers;
|
|
1383
|
-
this.failOnObserverError = failOnObserverError;
|
|
1384
|
-
}
|
|
1385
|
-
generationObservers;
|
|
1386
|
-
failOnObserverError;
|
|
1387
|
-
async end(args) {
|
|
1388
|
-
for (const observer of this.generationObservers) {
|
|
1389
|
-
try {
|
|
1390
|
-
await observer.end(args);
|
|
1391
|
-
} catch (error) {
|
|
1392
|
-
this.handleError(error);
|
|
1393
|
-
}
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
async error(args) {
|
|
1397
|
-
for (const observer of this.generationObservers) {
|
|
1398
|
-
if (observer.error === void 0) {
|
|
1399
|
-
continue;
|
|
1400
|
-
}
|
|
1401
|
-
try {
|
|
1402
|
-
await observer.error(args);
|
|
1403
|
-
} catch (error) {
|
|
1404
|
-
this.handleError(error);
|
|
1405
|
-
}
|
|
1406
|
-
}
|
|
1407
|
-
}
|
|
1408
|
-
async update(args) {
|
|
1409
|
-
for (const observer of this.generationObservers) {
|
|
1410
|
-
if (observer.update === void 0) {
|
|
1411
|
-
continue;
|
|
1412
|
-
}
|
|
1413
|
-
try {
|
|
1414
|
-
await observer.update(args);
|
|
1415
|
-
} catch (error) {
|
|
1416
|
-
this.handleError(error);
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
}
|
|
1420
|
-
handleError(error) {
|
|
1421
|
-
if (this.failOnObserverError) {
|
|
1422
|
-
throw error;
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
};
|
|
1426
|
-
var ActiveToolObservers = class {
|
|
1427
|
-
constructor(toolObservers, failOnObserverError) {
|
|
1428
|
-
this.toolObservers = toolObservers;
|
|
1429
|
-
this.failOnObserverError = failOnObserverError;
|
|
1430
|
-
}
|
|
1431
|
-
toolObservers;
|
|
1432
|
-
failOnObserverError;
|
|
1433
|
-
async streamEvent(args) {
|
|
1434
|
-
for (const observer of this.toolObservers) {
|
|
1435
|
-
if (observer.streamEvent === void 0) {
|
|
1436
|
-
continue;
|
|
1437
|
-
}
|
|
1438
|
-
try {
|
|
1439
|
-
await observer.streamEvent(args);
|
|
1440
|
-
} catch (error) {
|
|
1441
|
-
this.handleError(error);
|
|
1442
|
-
}
|
|
1443
|
-
}
|
|
1444
|
-
}
|
|
1445
|
-
async end(args) {
|
|
1446
|
-
for (const observer of this.toolObservers) {
|
|
1447
|
-
try {
|
|
1448
|
-
await observer.end(args);
|
|
1449
|
-
} catch (error) {
|
|
1450
|
-
this.handleError(error);
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
async error(args) {
|
|
1455
|
-
for (const observer of this.toolObservers) {
|
|
1456
|
-
if (observer.error === void 0) {
|
|
1457
|
-
continue;
|
|
1458
|
-
}
|
|
1459
|
-
try {
|
|
1460
|
-
await observer.error(args);
|
|
1461
|
-
} catch (error) {
|
|
1462
|
-
this.handleError(error);
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
}
|
|
1466
|
-
handleError(error) {
|
|
1467
|
-
if (this.failOnObserverError) {
|
|
1468
|
-
throw error;
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
};
|
|
1472
|
-
|
|
1473
|
-
// src/request/retry.ts
|
|
1474
|
-
var DEFAULT_MAX_ATTEMPTS = 3;
|
|
1475
|
-
var DEFAULT_INITIAL_DELAY_MS = 100;
|
|
1476
|
-
var DEFAULT_MAX_DELAY_MS = 1e3;
|
|
1477
|
-
var MAX_TIMER_DELAY_MS = 2147483647;
|
|
1478
|
-
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([408, 409, 425, 429]);
|
|
1479
|
-
var RETRYABLE_NUMERIC_ERROR_CODES = /* @__PURE__ */ new Set([4, 8, 14]);
|
|
1480
|
-
var RETRYABLE_ERROR_NAMES = /* @__PURE__ */ new Set([
|
|
1481
|
-
"APIConnectionError",
|
|
1482
|
-
"APIConnectionTimeoutError",
|
|
1483
|
-
"TimeoutError"
|
|
1484
|
-
]);
|
|
1485
|
-
var RETRYABLE_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
1486
|
-
"DEADLINE_EXCEEDED",
|
|
1487
|
-
"EAI_AGAIN",
|
|
1488
|
-
"ECONNREFUSED",
|
|
1489
|
-
"ECONNRESET",
|
|
1490
|
-
"EHOSTUNREACH",
|
|
1491
|
-
"ENETDOWN",
|
|
1492
|
-
"ENETUNREACH",
|
|
1493
|
-
"EPIPE",
|
|
1494
|
-
"ETIMEDOUT",
|
|
1495
|
-
"RESOURCE_EXHAUSTED",
|
|
1496
|
-
"UNAVAILABLE",
|
|
1497
|
-
"UND_ERR_CONNECT_TIMEOUT",
|
|
1498
|
-
"UND_ERR_HEADERS_TIMEOUT",
|
|
1499
|
-
"UND_ERR_SOCKET"
|
|
1500
|
-
]);
|
|
1501
|
-
function resolveCompletionRetryOptions(options) {
|
|
1502
|
-
const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
|
|
1503
|
-
const initialDelayMs = options.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
|
|
1504
|
-
const maxDelayMs = options.maxDelayMs ?? DEFAULT_MAX_DELAY_MS;
|
|
1505
|
-
if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1) {
|
|
1506
|
-
throw new RangeError("Completion retry maxAttempts must be a positive integer.");
|
|
1507
|
-
}
|
|
1508
|
-
assertDelay(initialDelayMs, "initialDelayMs");
|
|
1509
|
-
assertDelay(maxDelayMs, "maxDelayMs");
|
|
1510
|
-
if (maxDelayMs < initialDelayMs) {
|
|
1511
|
-
throw new RangeError(
|
|
1512
|
-
"Completion retry maxDelayMs must be greater than or equal to initialDelayMs."
|
|
1513
|
-
);
|
|
1514
|
-
}
|
|
1515
|
-
if (options.shouldRetry !== void 0 && typeof options.shouldRetry !== "function") {
|
|
1516
|
-
throw new TypeError("Completion retry shouldRetry must be a function.");
|
|
1517
|
-
}
|
|
1518
|
-
return {
|
|
1519
|
-
maxAttempts,
|
|
1520
|
-
initialDelayMs,
|
|
1521
|
-
maxDelayMs,
|
|
1522
|
-
shouldRetry: options.shouldRetry ?? defaultShouldRetry
|
|
1523
|
-
};
|
|
1524
|
-
}
|
|
1525
|
-
function completionRetryDelayMs(options, failedAttempt) {
|
|
1526
|
-
const exponentialDelay = options.initialDelayMs * 2 ** (failedAttempt - 1);
|
|
1527
|
-
const cappedDelay = Math.min(options.maxDelayMs, exponentialDelay);
|
|
1528
|
-
return Math.random() * cappedDelay;
|
|
1529
|
-
}
|
|
1530
|
-
function completionRetryErrorAttributes(error) {
|
|
1531
|
-
const errors = errorChain(error);
|
|
1532
|
-
const errorName = errors.map((candidate) => stringProperty(candidate, "name")).find((value) => value !== void 0);
|
|
1533
|
-
const statusCode = firstStatusCode(errors);
|
|
1534
|
-
const errorCode = errors.map((candidate) => errorCodeProperty(candidate)).find((value) => value !== void 0);
|
|
1535
|
-
return {
|
|
1536
|
-
errorName,
|
|
1537
|
-
statusCode,
|
|
1538
|
-
errorCode
|
|
1539
|
-
};
|
|
1540
|
-
}
|
|
1541
|
-
async function waitForCompletionRetry(delayMs) {
|
|
1542
|
-
if (delayMs === 0) {
|
|
1543
|
-
return;
|
|
1544
|
-
}
|
|
1545
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
1546
|
-
}
|
|
1547
|
-
function defaultShouldRetry(context) {
|
|
1548
|
-
const errors = errorChain(context.error);
|
|
1549
|
-
if (errors.some((error) => stringProperty(error, "name") === "AbortError")) {
|
|
1550
|
-
return false;
|
|
1551
|
-
}
|
|
1552
|
-
const statusCode = firstStatusCode(errors);
|
|
1553
|
-
if (statusCode !== void 0) {
|
|
1554
|
-
return RETRYABLE_STATUS_CODES.has(statusCode) || statusCode >= 500 && statusCode <= 599;
|
|
1555
|
-
}
|
|
1556
|
-
return errors.some((error) => {
|
|
1557
|
-
const name = stringProperty(error, "name");
|
|
1558
|
-
if (name !== void 0 && RETRYABLE_ERROR_NAMES.has(name)) {
|
|
1559
|
-
return true;
|
|
1560
|
-
}
|
|
1561
|
-
const code = stringProperty(error, "code")?.toUpperCase();
|
|
1562
|
-
if (code !== void 0 && RETRYABLE_ERROR_CODES.has(code)) {
|
|
1563
|
-
return true;
|
|
1564
|
-
}
|
|
1565
|
-
const numericCode = numberProperty(error, "code");
|
|
1566
|
-
return numericCode !== void 0 && RETRYABLE_NUMERIC_ERROR_CODES.has(numericCode);
|
|
1567
|
-
});
|
|
1568
|
-
}
|
|
1569
|
-
function firstStatusCode(errors) {
|
|
1570
|
-
for (const error of errors) {
|
|
1571
|
-
const status = numberProperty(error, "status") ?? numberProperty(error, "statusCode");
|
|
1572
|
-
if (status !== void 0) {
|
|
1573
|
-
return status;
|
|
1574
|
-
}
|
|
1575
|
-
const code = numberProperty(error, "code");
|
|
1576
|
-
if (code !== void 0 && code >= 100 && code <= 599) {
|
|
1577
|
-
return code;
|
|
1578
|
-
}
|
|
1579
|
-
}
|
|
1580
|
-
return void 0;
|
|
1581
|
-
}
|
|
1582
|
-
function errorChain(error) {
|
|
1583
|
-
const errors = [];
|
|
1584
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1585
|
-
let current = error;
|
|
1586
|
-
while (isObject(current) && !seen.has(current)) {
|
|
1587
|
-
seen.add(current);
|
|
1588
|
-
errors.push(current);
|
|
1589
|
-
current = property(current, "cause");
|
|
1590
|
-
}
|
|
1591
|
-
return errors;
|
|
1592
|
-
}
|
|
1593
|
-
function assertDelay(value, name) {
|
|
1594
|
-
if (!Number.isFinite(value) || value < 0 || value > MAX_TIMER_DELAY_MS) {
|
|
1595
|
-
throw new RangeError(
|
|
1596
|
-
`Completion retry ${name} must be between 0 and ${MAX_TIMER_DELAY_MS} milliseconds.`
|
|
1597
|
-
);
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
function numberProperty(value, name) {
|
|
1601
|
-
const candidate = property(value, name);
|
|
1602
|
-
return typeof candidate === "number" && Number.isFinite(candidate) ? candidate : void 0;
|
|
1603
|
-
}
|
|
1604
|
-
function stringProperty(value, name) {
|
|
1605
|
-
const candidate = property(value, name);
|
|
1606
|
-
return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
|
|
1607
|
-
}
|
|
1608
|
-
function errorCodeProperty(value) {
|
|
1609
|
-
return stringProperty(value, "code") ?? numberProperty(value, "code");
|
|
1610
|
-
}
|
|
1611
|
-
function property(value, name) {
|
|
1612
|
-
try {
|
|
1613
|
-
return value[name];
|
|
1614
|
-
} catch {
|
|
1615
|
-
return void 0;
|
|
1616
|
-
}
|
|
1617
|
-
}
|
|
1618
|
-
function isObject(value) {
|
|
1619
|
-
return typeof value === "object" && value !== null;
|
|
1620
|
-
}
|
|
1621
|
-
|
|
1622
|
-
// src/request/prompt-request.ts
|
|
1623
|
-
var PromptRequest = class _PromptRequest {
|
|
1624
|
-
constructor(agent, promptMessage, initialHistory = [], memoryContext = void 0) {
|
|
1625
|
-
this.agent = agent;
|
|
1626
|
-
this.promptMessage = promptMessage;
|
|
1627
|
-
this.memoryContext = memoryContext;
|
|
1628
|
-
this.chatHistory = initialHistory;
|
|
1629
|
-
this.maxTurnCount = agent.defaultMaxTurns ?? 0;
|
|
1630
|
-
this.activeHook = agent.hook;
|
|
1631
|
-
this.approvalOptions = agent.approvals;
|
|
1632
|
-
this.guardrailPolicies = [...agent.guardrails];
|
|
1633
|
-
this.memoryRecorder = new PromptRequestMemory(agent, memoryContext, initialHistory);
|
|
1634
|
-
}
|
|
1635
|
-
agent;
|
|
1636
|
-
promptMessage;
|
|
1637
|
-
memoryContext;
|
|
1638
|
-
chatHistory;
|
|
1639
|
-
maxTurnCount;
|
|
1640
|
-
activeHook;
|
|
1641
|
-
approvalOptions;
|
|
1642
|
-
guardrailPolicies;
|
|
1643
|
-
guardrailDecisions = [];
|
|
1644
|
-
concurrency = 1;
|
|
1645
|
-
traceOptions;
|
|
1646
|
-
completionRetryOptions;
|
|
1647
|
-
requestMiddlewares = [];
|
|
1648
|
-
steeringMessages = [];
|
|
1649
|
-
runState = "idle";
|
|
1650
|
-
memoryRecorder;
|
|
1651
|
-
static fromAgent(agent, prompt, options = {}) {
|
|
1652
|
-
const normalized = normalizePromptInput(prompt);
|
|
1653
|
-
return new _PromptRequest(agent, normalized.prompt, normalized.history, options.memoryContext);
|
|
1654
|
-
}
|
|
1655
|
-
maxTurns(maxTurns) {
|
|
1656
|
-
this.maxTurnCount = maxTurns;
|
|
1657
|
-
return this;
|
|
1658
|
-
}
|
|
1659
|
-
withCompletionRetries(options = {}) {
|
|
1660
|
-
this.completionRetryOptions = resolveCompletionRetryOptions(options);
|
|
1661
|
-
return this;
|
|
1662
|
-
}
|
|
1663
|
-
withHook(hook) {
|
|
1664
|
-
this.activeHook = hook;
|
|
1665
|
-
return this;
|
|
1666
|
-
}
|
|
1667
|
-
approvals(options) {
|
|
1668
|
-
this.approvalOptions = options;
|
|
1669
|
-
return this;
|
|
1670
|
-
}
|
|
1671
|
-
guardrails(policies) {
|
|
1672
|
-
this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);
|
|
1673
|
-
return this;
|
|
1674
|
-
}
|
|
1675
|
-
withToolConcurrency(concurrency) {
|
|
1676
|
-
this.concurrency = Math.max(1, concurrency);
|
|
1677
|
-
return this;
|
|
1678
|
-
}
|
|
1679
|
-
withMiddleware(middleware) {
|
|
1680
|
-
this.requestMiddlewares.push(middleware);
|
|
1681
|
-
return this;
|
|
1682
|
-
}
|
|
1683
|
-
withMiddlewares(middlewares) {
|
|
1684
|
-
this.requestMiddlewares.push(...middlewares);
|
|
1685
|
-
return this;
|
|
1686
|
-
}
|
|
1687
|
-
withTrace(trace) {
|
|
1688
|
-
this.traceOptions = trace;
|
|
1689
|
-
return this;
|
|
1690
|
-
}
|
|
1691
|
-
steer(input) {
|
|
1692
|
-
if (this.isTerminal()) {
|
|
1693
|
-
return false;
|
|
1694
|
-
}
|
|
1695
|
-
this.steeringMessages.push(...normalizeSteeringInput(input));
|
|
1696
|
-
return true;
|
|
1697
|
-
}
|
|
1698
|
-
async send() {
|
|
1699
|
-
this.startRun();
|
|
1700
|
-
const runId = globalThis.crypto.randomUUID();
|
|
1701
|
-
let usage = Usage.empty();
|
|
1702
|
-
let currentTurns = 0;
|
|
1703
|
-
let lastPrompt = this.promptMessage;
|
|
1704
|
-
let newMessages = [this.promptMessage];
|
|
1705
|
-
const runObservers = await this.startRunObservers();
|
|
1706
|
-
try {
|
|
1707
|
-
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
1708
|
-
prompt: this.promptMessage,
|
|
1709
|
-
history: this.chatHistory,
|
|
1710
|
-
inputText: textFromMessage(this.promptMessage),
|
|
1711
|
-
run: this.guardrailRunContext(runId)
|
|
1712
|
-
});
|
|
1713
|
-
for (const decision of inputResult.decisions) {
|
|
1714
|
-
await this.recordGuardrailDecision(decision, runObservers);
|
|
1715
|
-
}
|
|
1716
|
-
this.promptMessage = inputResult.prompt;
|
|
1717
|
-
if (inputResult.blocked) {
|
|
1718
|
-
const output = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
1719
|
-
const result = {
|
|
1720
|
-
output,
|
|
1721
|
-
usage: Usage.empty(),
|
|
1722
|
-
messages: [this.promptMessage, Message.assistant(output)],
|
|
1723
|
-
guardrails: [...this.guardrailDecisions]
|
|
1724
|
-
};
|
|
1725
|
-
await runObservers.end(result);
|
|
1726
|
-
this.runState = "completed";
|
|
1727
|
-
return result;
|
|
1728
|
-
}
|
|
1729
|
-
newMessages = [this.promptMessage];
|
|
1730
|
-
const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1731
|
-
this.chatHistory = memoryPreparation.history;
|
|
1732
|
-
usage = Usage.add(usage, memoryPreparation.usage);
|
|
1733
|
-
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
1734
|
-
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1735
|
-
await this.runRunStartHook(newMessages);
|
|
1736
|
-
while (currentTurns <= this.maxTurnCount + 1) {
|
|
1737
|
-
const prompt = newMessages.at(-1);
|
|
1738
|
-
if (prompt === void 0) {
|
|
1739
|
-
throw new Error("PromptRequest requires at least one message");
|
|
1740
|
-
}
|
|
1741
|
-
lastPrompt = prompt;
|
|
1742
|
-
currentTurns += 1;
|
|
1743
|
-
const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
|
|
1744
|
-
await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
|
|
1745
|
-
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
1746
|
-
const ragText = extractRagText(prompt);
|
|
1747
|
-
const dynamicContext = await fetchDynamicContext(this.agent, ragText);
|
|
1748
|
-
const toolDefs = await fetchToolDefinitions(this.agent, ragText);
|
|
1749
|
-
let request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools([...toolDefs, ...this.agent.providerTools]).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
|
|
1750
|
-
request = await this.runCompletionRequestMiddlewares(
|
|
1751
|
-
request,
|
|
1752
|
-
currentTurns
|
|
1753
|
-
);
|
|
1754
|
-
let response;
|
|
1755
|
-
try {
|
|
1756
|
-
response = await this.runCompletion(request, currentTurns, runObservers);
|
|
1757
|
-
} catch (error) {
|
|
1758
|
-
await this.runCompletionErrorHook(prompt, error, newMessages);
|
|
1759
|
-
throw error;
|
|
1760
|
-
}
|
|
1761
|
-
response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
|
|
1762
|
-
usage = Usage.add(usage, response.usage);
|
|
1763
|
-
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
1764
|
-
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
1765
|
-
const toolCalls = response.choice.filter(
|
|
1766
|
-
(item) => item.type === "tool_call"
|
|
1767
|
-
);
|
|
1768
|
-
const assistantMessage = this.generatedAssistantMessage(response, request);
|
|
1769
|
-
newMessages.push(assistantMessage);
|
|
1770
|
-
if (toolCalls.length === 0) {
|
|
1771
|
-
if (this.steeringMessages.length > 0) {
|
|
1772
|
-
await this.memoryRecorder.commitMessages(
|
|
1773
|
-
runId,
|
|
1774
|
-
currentTurns,
|
|
1775
|
-
[assistantMessage],
|
|
1776
|
-
pendingTurnMessages
|
|
1777
|
-
);
|
|
1778
|
-
}
|
|
1779
|
-
if (await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages)) {
|
|
1780
|
-
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
1781
|
-
continue;
|
|
1782
|
-
}
|
|
1783
|
-
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
1784
|
-
runId,
|
|
1785
|
-
usage,
|
|
1786
|
-
response,
|
|
1787
|
-
newMessages,
|
|
1788
|
-
runObservers
|
|
1789
|
-
);
|
|
1790
|
-
response = guardedOutput.response;
|
|
1791
|
-
const finalAssistantMessage = this.generatedAssistantMessage(response, request);
|
|
1792
|
-
newMessages[newMessages.length - 1] = finalAssistantMessage;
|
|
1793
|
-
await this.memoryRecorder.commitMessages(
|
|
1794
|
-
runId,
|
|
1795
|
-
currentTurns,
|
|
1796
|
-
[finalAssistantMessage],
|
|
1797
|
-
pendingTurnMessages
|
|
1798
|
-
);
|
|
1799
|
-
const result = {
|
|
1800
|
-
output: guardedOutput.output,
|
|
1801
|
-
usage,
|
|
1802
|
-
messages: [...newMessages],
|
|
1803
|
-
trace: runObservers.trace,
|
|
1804
|
-
guardrails: [...this.guardrailDecisions],
|
|
1805
|
-
...generationArtifacts(newMessages)
|
|
1806
|
-
};
|
|
1807
|
-
await this.runRunEndHook(result, newMessages);
|
|
1808
|
-
await runObservers.end(result);
|
|
1809
|
-
await this.memoryRecorder.commitCompletedRun(
|
|
1810
|
-
runId,
|
|
1811
|
-
currentTurns,
|
|
1812
|
-
newMessages,
|
|
1813
|
-
pendingTurnMessages
|
|
1814
|
-
);
|
|
1815
|
-
this.runState = "completed";
|
|
1816
|
-
return result;
|
|
1817
|
-
}
|
|
1818
|
-
await this.memoryRecorder.commitMessages(
|
|
1819
|
-
runId,
|
|
1820
|
-
currentTurns,
|
|
1821
|
-
[assistantMessage],
|
|
1822
|
-
pendingTurnMessages
|
|
1823
|
-
);
|
|
1824
|
-
const toolResults = await this.executeToolCalls(
|
|
1825
|
-
runId,
|
|
1826
|
-
toolCalls,
|
|
1827
|
-
newMessages,
|
|
1828
|
-
void 0,
|
|
1829
|
-
void 0,
|
|
1830
|
-
{
|
|
1831
|
-
turn: currentTurns,
|
|
1832
|
-
runObservers,
|
|
1833
|
-
toolDefinitions: request.tools
|
|
1834
|
-
}
|
|
1835
|
-
);
|
|
1836
|
-
const toolMessage = Message.tool(toolResults);
|
|
1837
|
-
newMessages.push(toolMessage);
|
|
1838
|
-
await this.memoryRecorder.commitMessages(
|
|
1839
|
-
runId,
|
|
1840
|
-
currentTurns,
|
|
1841
|
-
[toolMessage],
|
|
1842
|
-
pendingTurnMessages
|
|
1843
|
-
);
|
|
1844
|
-
await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages);
|
|
1845
|
-
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
1846
|
-
}
|
|
1847
|
-
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
1848
|
-
} catch (error) {
|
|
1849
|
-
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
1850
|
-
usage = Usage.add(usage, error.usage);
|
|
1851
|
-
}
|
|
1852
|
-
const finalError = await this.runRunErrorHook(error, usage, newMessages);
|
|
1853
|
-
this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
|
|
1854
|
-
await runObservers.error({ error: finalError, usage, messages: [...newMessages] });
|
|
1855
|
-
await this.memoryRecorder.recordError(runId, finalError, newMessages);
|
|
1856
|
-
throw finalError;
|
|
1857
|
-
}
|
|
1858
|
-
}
|
|
1859
|
-
async *stream(options = {}) {
|
|
1860
|
-
if (!this.agent.model.capabilities.streaming || !isStreamingCompletionModel(this.agent.model)) {
|
|
1861
|
-
throw new Error("This completion model does not support streaming");
|
|
1862
|
-
}
|
|
1863
|
-
this.startRun();
|
|
1864
|
-
const includeToolCallDeltas = options.includeToolCallDeltas !== false;
|
|
1865
|
-
const runId = globalThis.crypto.randomUUID();
|
|
1866
|
-
const emit = async (event) => {
|
|
1867
|
-
await this.recordAgentEvent(runId, event);
|
|
1868
|
-
return event;
|
|
1869
|
-
};
|
|
1870
|
-
let usage = Usage.empty();
|
|
1871
|
-
let currentTurns = 0;
|
|
1872
|
-
let lastPrompt = this.promptMessage;
|
|
1873
|
-
let newMessages = [this.promptMessage];
|
|
1874
|
-
const runObservers = await this.startRunObservers();
|
|
1875
|
-
const bufferOutputDeltas = hasEnforcedOutputGuardrails(this.guardrailPolicies);
|
|
1876
|
-
try {
|
|
1877
|
-
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
1878
|
-
prompt: this.promptMessage,
|
|
1879
|
-
history: this.chatHistory,
|
|
1880
|
-
inputText: textFromMessage(this.promptMessage),
|
|
1881
|
-
run: this.guardrailRunContext(runId)
|
|
1882
|
-
});
|
|
1883
|
-
for (const decision of inputResult.decisions) {
|
|
1884
|
-
await this.recordGuardrailDecision(decision, runObservers);
|
|
1885
|
-
yield await emit({ type: "guardrail_decision", decision });
|
|
1886
|
-
}
|
|
1887
|
-
this.promptMessage = inputResult.prompt;
|
|
1888
|
-
if (inputResult.blocked) {
|
|
1889
|
-
const output = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
1890
|
-
const result = {
|
|
1891
|
-
output,
|
|
1892
|
-
usage: Usage.empty(),
|
|
1893
|
-
messages: [this.promptMessage, Message.assistant(output)],
|
|
1894
|
-
guardrails: [...this.guardrailDecisions]
|
|
1895
|
-
};
|
|
1896
|
-
await runObservers.end(result);
|
|
1897
|
-
this.runState = "completed";
|
|
1898
|
-
yield await emit({
|
|
1899
|
-
type: "final",
|
|
1900
|
-
runId,
|
|
1901
|
-
output: result.output,
|
|
1902
|
-
usage: result.usage,
|
|
1903
|
-
messages: result.messages,
|
|
1904
|
-
guardrails: result.guardrails
|
|
1905
|
-
});
|
|
1906
|
-
return;
|
|
1907
|
-
}
|
|
1908
|
-
newMessages = [this.promptMessage];
|
|
1909
|
-
const memoryPreparation = await this.memoryRecorder.prepareRun(runId, newMessages);
|
|
1910
|
-
this.chatHistory = memoryPreparation.history;
|
|
1911
|
-
usage = Usage.add(usage, memoryPreparation.usage);
|
|
1912
|
-
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
1913
|
-
const pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
1914
|
-
await this.runRunStartHook(newMessages);
|
|
1915
|
-
while (currentTurns <= this.maxTurnCount + 1) {
|
|
1916
|
-
const prompt = newMessages.at(-1);
|
|
1917
|
-
if (prompt === void 0) {
|
|
1918
|
-
throw new Error("PromptRequest requires at least one message");
|
|
1919
|
-
}
|
|
1920
|
-
lastPrompt = prompt;
|
|
1921
|
-
currentTurns += 1;
|
|
1922
|
-
const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
|
|
1923
|
-
yield await emit({
|
|
1924
|
-
type: "turn_start",
|
|
1925
|
-
turn: currentTurns,
|
|
1926
|
-
prompt,
|
|
1927
|
-
history: historyForRequest
|
|
1928
|
-
});
|
|
1929
|
-
await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
|
|
1930
|
-
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
1931
|
-
const ragText = extractRagText(prompt);
|
|
1932
|
-
const dynamicContext = await fetchDynamicContext(this.agent, ragText);
|
|
1933
|
-
const toolDefs = await fetchToolDefinitions(this.agent, ragText);
|
|
1934
|
-
let request = new CompletionRequestBuilder(this.agent.model, prompt).instructions(this.agent.instructions).messages(historyForRequest).documents([...this.agent.staticContext, ...dynamicContext]).tools([...toolDefs, ...this.agent.providerTools]).temperature(this.agent.temperature).maxTokens(this.agent.maxTokens).additionalParams(this.agent.additionalParams).toolChoice(this.agent.toolChoice).outputSchema(this.agent.outputSchema).build();
|
|
1935
|
-
request = await this.runCompletionRequestMiddlewares(
|
|
1936
|
-
request,
|
|
1937
|
-
currentTurns
|
|
1938
|
-
);
|
|
1939
|
-
assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
|
|
1940
|
-
const providerRequest = this.providerTraceRequest(request, { stream: true });
|
|
1941
|
-
const generationStartArgs = this.generationStartArgs(
|
|
1942
|
-
currentTurns,
|
|
1943
|
-
request,
|
|
1944
|
-
providerRequest
|
|
1945
|
-
);
|
|
1946
|
-
const generationObservers = await runObservers.startGeneration(generationStartArgs);
|
|
1947
|
-
const generationStartedAt = Date.now();
|
|
1948
|
-
yield await emit({
|
|
1949
|
-
type: "generation_start",
|
|
1950
|
-
turn: currentTurns,
|
|
1951
|
-
request,
|
|
1952
|
-
modelInfo: generationStartArgs.modelInfo
|
|
1953
|
-
});
|
|
1954
|
-
const accumulator = new CompletionStreamAccumulator();
|
|
1955
|
-
let firstDeltaMs;
|
|
1956
|
-
const bufferResponseEvents = this.shouldBufferStreamResponseEvents();
|
|
1957
|
-
const emittedToolCallIds = /* @__PURE__ */ new Set();
|
|
1958
|
-
let response;
|
|
1959
|
-
try {
|
|
1960
|
-
for (let attempt = 1; ; attempt += 1) {
|
|
1961
|
-
let hasProviderProgress = false;
|
|
1962
|
-
try {
|
|
1963
|
-
for await (const event of this.agent.model.streamCompletion(request)) {
|
|
1964
|
-
if (event.type === "error") {
|
|
1965
|
-
if (event.usage !== void 0) {
|
|
1966
|
-
usage = Usage.add(usage, event.usage);
|
|
1967
|
-
}
|
|
1968
|
-
throw event.error;
|
|
1969
|
-
}
|
|
1970
|
-
hasProviderProgress = true;
|
|
1971
|
-
if (firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
|
|
1972
|
-
firstDeltaMs = Date.now() - generationStartedAt;
|
|
1973
|
-
}
|
|
1974
|
-
const mapped = accumulator.accept(event);
|
|
1975
|
-
if (includeToolCallDeltas && event.type === "tool_call_delta") {
|
|
1976
|
-
yield await emit(addTurnToToolCallDelta(currentTurns, event));
|
|
1977
|
-
}
|
|
1978
|
-
if (mapped !== void 0) {
|
|
1979
|
-
await generationObservers.update?.({ turn: currentTurns, delta: mapped });
|
|
1980
|
-
if (mapped.type === "tool_call") {
|
|
1981
|
-
emittedToolCallIds.add(mapped.toolCall.id);
|
|
1982
|
-
}
|
|
1983
|
-
const shouldBuffer = bufferResponseEvents || bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
|
|
1984
|
-
if (!shouldBuffer) {
|
|
1985
|
-
yield await emit(addTurn(currentTurns, mapped));
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
}
|
|
1989
|
-
response = accumulator.response();
|
|
1990
|
-
break;
|
|
1991
|
-
} catch (error) {
|
|
1992
|
-
const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, currentTurns, true);
|
|
1993
|
-
if (retryOptions === void 0) {
|
|
1994
|
-
throw error;
|
|
1995
|
-
}
|
|
1996
|
-
await this.scheduleCompletionRetry(
|
|
1997
|
-
error,
|
|
1998
|
-
attempt,
|
|
1999
|
-
currentTurns,
|
|
2000
|
-
true,
|
|
2001
|
-
retryOptions,
|
|
2002
|
-
runObservers
|
|
2003
|
-
);
|
|
2004
|
-
}
|
|
2005
|
-
}
|
|
2006
|
-
} catch (error) {
|
|
2007
|
-
await generationObservers.error({ turn: currentTurns, error });
|
|
2008
|
-
await this.runCompletionErrorHook(prompt, error, newMessages);
|
|
2009
|
-
throw error;
|
|
2010
|
-
}
|
|
2011
|
-
const generationEndArgs = {
|
|
2012
|
-
turn: currentTurns,
|
|
2013
|
-
response
|
|
2014
|
-
};
|
|
2015
|
-
if (firstDeltaMs !== void 0) {
|
|
2016
|
-
generationEndArgs.firstDeltaMs = firstDeltaMs;
|
|
2017
|
-
}
|
|
2018
|
-
await generationObservers.end(generationEndArgs);
|
|
2019
|
-
response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
|
|
2020
|
-
usage = Usage.add(usage, response.usage);
|
|
2021
|
-
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
2022
|
-
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
2023
|
-
const toolCalls = response.choice.filter(
|
|
2024
|
-
(item) => item.type === "tool_call"
|
|
2025
|
-
);
|
|
2026
|
-
let assistantMessage = this.generatedAssistantMessage(response, request);
|
|
2027
|
-
newMessages.push(assistantMessage);
|
|
2028
|
-
if (toolCalls.length === 0) {
|
|
2029
|
-
let emittedTurnEnd = false;
|
|
2030
|
-
if (!bufferOutputDeltas) {
|
|
2031
|
-
if (bufferResponseEvents) {
|
|
2032
|
-
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
2033
|
-
yield await emit(event);
|
|
2034
|
-
}
|
|
2035
|
-
}
|
|
2036
|
-
yield await emit({
|
|
2037
|
-
type: "turn_end",
|
|
2038
|
-
turn: currentTurns,
|
|
2039
|
-
response,
|
|
2040
|
-
firstDeltaMs
|
|
2041
|
-
});
|
|
2042
|
-
emittedTurnEnd = true;
|
|
2043
|
-
}
|
|
2044
|
-
if (this.steeringMessages.length > 0) {
|
|
2045
|
-
await this.memoryRecorder.commitMessages(
|
|
2046
|
-
runId,
|
|
2047
|
-
currentTurns,
|
|
2048
|
-
[assistantMessage],
|
|
2049
|
-
pendingTurnMessages
|
|
2050
|
-
);
|
|
2051
|
-
}
|
|
2052
|
-
if (await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages)) {
|
|
2053
|
-
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
2054
|
-
continue;
|
|
2055
|
-
}
|
|
2056
|
-
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
2057
|
-
runId,
|
|
2058
|
-
usage,
|
|
2059
|
-
response,
|
|
2060
|
-
newMessages,
|
|
2061
|
-
runObservers
|
|
2062
|
-
);
|
|
2063
|
-
for (const decision of guardedOutput.decisions) {
|
|
2064
|
-
yield await emit({ type: "guardrail_decision", decision });
|
|
2065
|
-
}
|
|
2066
|
-
response = guardedOutput.response;
|
|
2067
|
-
assistantMessage = this.generatedAssistantMessage(response, request);
|
|
2068
|
-
newMessages[newMessages.length - 1] = assistantMessage;
|
|
2069
|
-
await this.memoryRecorder.commitMessages(
|
|
2070
|
-
runId,
|
|
2071
|
-
currentTurns,
|
|
2072
|
-
[assistantMessage],
|
|
2073
|
-
pendingTurnMessages
|
|
2074
|
-
);
|
|
2075
|
-
if (!emittedTurnEnd && (bufferResponseEvents || bufferOutputDeltas)) {
|
|
2076
|
-
for (const event of responseStreamEvents(
|
|
2077
|
-
currentTurns,
|
|
2078
|
-
response,
|
|
2079
|
-
bufferResponseEvents
|
|
2080
|
-
)) {
|
|
2081
|
-
yield await emit(event);
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
if (!emittedTurnEnd) {
|
|
2085
|
-
yield await emit({
|
|
2086
|
-
type: "turn_end",
|
|
2087
|
-
turn: currentTurns,
|
|
2088
|
-
response,
|
|
2089
|
-
firstDeltaMs
|
|
2090
|
-
});
|
|
2091
|
-
}
|
|
2092
|
-
const result = {
|
|
2093
|
-
output: guardedOutput.output,
|
|
2094
|
-
usage,
|
|
2095
|
-
messages: [...newMessages],
|
|
2096
|
-
trace: runObservers.trace,
|
|
2097
|
-
guardrails: [...this.guardrailDecisions],
|
|
2098
|
-
...generationArtifacts(newMessages)
|
|
2099
|
-
};
|
|
2100
|
-
await this.runRunEndHook(result, newMessages);
|
|
2101
|
-
await runObservers.end(result);
|
|
2102
|
-
await this.memoryRecorder.commitCompletedRun(
|
|
2103
|
-
runId,
|
|
2104
|
-
currentTurns,
|
|
2105
|
-
newMessages,
|
|
2106
|
-
pendingTurnMessages
|
|
2107
|
-
);
|
|
2108
|
-
this.runState = "completed";
|
|
2109
|
-
yield await emit({
|
|
2110
|
-
type: "final",
|
|
2111
|
-
runId,
|
|
2112
|
-
output: result.output,
|
|
2113
|
-
usage: result.usage,
|
|
2114
|
-
contextUsage: result.contextUsage,
|
|
2115
|
-
messages: result.messages,
|
|
2116
|
-
trace: result.trace,
|
|
2117
|
-
guardrails: result.guardrails,
|
|
2118
|
-
sources: result.sources,
|
|
2119
|
-
providerToolCalls: result.providerToolCalls
|
|
2120
|
-
});
|
|
2121
|
-
return;
|
|
2122
|
-
}
|
|
2123
|
-
if (bufferResponseEvents) {
|
|
2124
|
-
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
2125
|
-
yield await emit(event);
|
|
2126
|
-
}
|
|
2127
|
-
} else {
|
|
2128
|
-
for (const toolCall of toolCalls) {
|
|
2129
|
-
if (!emittedToolCallIds.has(toolCall.id)) {
|
|
2130
|
-
yield await emit({ type: "tool_call", turn: currentTurns, toolCall });
|
|
2131
|
-
}
|
|
2132
|
-
}
|
|
2133
|
-
}
|
|
2134
|
-
await this.memoryRecorder.commitMessages(
|
|
2135
|
-
runId,
|
|
2136
|
-
currentTurns,
|
|
2137
|
-
[assistantMessage],
|
|
2138
|
-
pendingTurnMessages
|
|
2139
|
-
);
|
|
2140
|
-
yield await emit({
|
|
2141
|
-
type: "turn_end",
|
|
2142
|
-
turn: currentTurns,
|
|
2143
|
-
response,
|
|
2144
|
-
firstDeltaMs
|
|
2145
|
-
});
|
|
2146
|
-
const toolResultEvents = createAsyncQueue();
|
|
2147
|
-
const toolResultsPromise = this.executeToolCalls(
|
|
2148
|
-
runId,
|
|
2149
|
-
toolCalls,
|
|
2150
|
-
newMessages,
|
|
2151
|
-
(result) => {
|
|
2152
|
-
toolResultEvents.enqueue(result);
|
|
2153
|
-
},
|
|
2154
|
-
(event) => {
|
|
2155
|
-
toolResultEvents.enqueue(event);
|
|
2156
|
-
},
|
|
2157
|
-
{
|
|
2158
|
-
turn: currentTurns,
|
|
2159
|
-
runObservers,
|
|
2160
|
-
toolDefinitions: request.tools,
|
|
2161
|
-
includeToolCallDeltas
|
|
2162
|
-
}
|
|
2163
|
-
);
|
|
2164
|
-
toolResultsPromise.then(
|
|
2165
|
-
() => toolResultEvents.close(),
|
|
2166
|
-
(error) => toolResultEvents.throw(error)
|
|
2167
|
-
);
|
|
2168
|
-
for await (const result of toolResultEvents) {
|
|
2169
|
-
yield await emit({ turn: currentTurns, ...result });
|
|
2170
|
-
}
|
|
2171
|
-
const toolResults = await toolResultsPromise;
|
|
2172
|
-
const toolMessage = Message.tool(toolResults);
|
|
2173
|
-
newMessages.push(toolMessage);
|
|
2174
|
-
await this.memoryRecorder.commitMessages(
|
|
2175
|
-
runId,
|
|
2176
|
-
currentTurns,
|
|
2177
|
-
[toolMessage],
|
|
2178
|
-
pendingTurnMessages
|
|
2179
|
-
);
|
|
2180
|
-
await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages);
|
|
2181
|
-
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
2182
|
-
}
|
|
2183
|
-
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
2184
|
-
} catch (error) {
|
|
2185
|
-
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
2186
|
-
usage = Usage.add(usage, error.usage);
|
|
2187
|
-
}
|
|
2188
|
-
const finalError = await this.runRunErrorHook(error, usage, newMessages);
|
|
2189
|
-
this.runState = finalError instanceof PromptCancelledError ? "cancelled" : "errored";
|
|
2190
|
-
const finalUsage = usage;
|
|
2191
|
-
await runObservers.error({
|
|
2192
|
-
error: finalError,
|
|
2193
|
-
usage: finalUsage,
|
|
2194
|
-
messages: [...newMessages]
|
|
2195
|
-
});
|
|
2196
|
-
await this.memoryRecorder.recordError(runId, finalError, newMessages);
|
|
2197
|
-
yield await emit({ type: "error", error: finalError, usage: finalUsage });
|
|
2198
|
-
throw finalError;
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
readableStream(options = {}) {
|
|
2202
|
-
return toReadableStream(this.stream(options));
|
|
2203
|
-
}
|
|
2204
|
-
async runCompletion(request, turn, runObservers) {
|
|
2205
|
-
assertCompletionRequestSupported(this.agent.model, request);
|
|
2206
|
-
const providerRequest = this.providerTraceRequest(request);
|
|
2207
|
-
const generationObservers = await runObservers.startGeneration(
|
|
2208
|
-
this.generationStartArgs(turn, request, providerRequest)
|
|
2209
|
-
);
|
|
2210
|
-
try {
|
|
2211
|
-
for (let attempt = 1; ; attempt += 1) {
|
|
2212
|
-
let response;
|
|
2213
|
-
try {
|
|
2214
|
-
response = await this.agent.model.completion(request);
|
|
2215
|
-
} catch (error) {
|
|
2216
|
-
const retryOptions = this.retryOptionsForFailure(error, attempt, turn, false);
|
|
2217
|
-
if (retryOptions === void 0) {
|
|
2218
|
-
throw error;
|
|
2219
|
-
}
|
|
2220
|
-
await this.scheduleCompletionRetry(
|
|
2221
|
-
error,
|
|
2222
|
-
attempt,
|
|
2223
|
-
turn,
|
|
2224
|
-
false,
|
|
2225
|
-
retryOptions,
|
|
2226
|
-
runObservers
|
|
2227
|
-
);
|
|
2228
|
-
continue;
|
|
2229
|
-
}
|
|
2230
|
-
await generationObservers.end({ turn, response });
|
|
2231
|
-
return response;
|
|
2232
|
-
}
|
|
2233
|
-
} catch (error) {
|
|
2234
|
-
await generationObservers.error({ turn, error });
|
|
2235
|
-
throw error;
|
|
2236
|
-
}
|
|
2237
|
-
}
|
|
2238
|
-
generatedAssistantMessage(response, request) {
|
|
2239
|
-
const metadata = {
|
|
2240
|
-
anvia: {
|
|
2241
|
-
generation: {
|
|
2242
|
-
provider: this.agent.model.provider,
|
|
2243
|
-
model: request.model ?? this.agent.model.defaultModel,
|
|
2244
|
-
usage: { ...response.usage },
|
|
2245
|
-
...response.contextUsage === void 0 ? {} : { contextUsage: response.contextUsage },
|
|
2246
|
-
...response.sources === void 0 ? {} : { sources: response.sources },
|
|
2247
|
-
...response.providerToolCalls === void 0 ? {} : { providerToolCalls: response.providerToolCalls }
|
|
2248
|
-
}
|
|
2249
|
-
}
|
|
2250
|
-
};
|
|
2251
|
-
return Message.assistant(response.choice, {
|
|
2252
|
-
...response.messageId === void 0 ? {} : { id: response.messageId },
|
|
2253
|
-
metadata
|
|
2254
|
-
});
|
|
2255
|
-
}
|
|
2256
|
-
providerTraceRequest(request, options = {}) {
|
|
2257
|
-
try {
|
|
2258
|
-
return this.agent.model.traceRequest?.(request, options);
|
|
2259
|
-
} catch (error) {
|
|
2260
|
-
return {
|
|
2261
|
-
error: error instanceof Error ? error.message : String(error)
|
|
2262
|
-
};
|
|
2263
|
-
}
|
|
2264
|
-
}
|
|
2265
|
-
generationStartArgs(turn, request, providerRequest) {
|
|
2266
|
-
const args = {
|
|
2267
|
-
turn,
|
|
2268
|
-
request,
|
|
2269
|
-
modelInfo: {
|
|
2270
|
-
provider: this.agent.model.provider,
|
|
2271
|
-
defaultModel: this.agent.model.defaultModel,
|
|
2272
|
-
capabilities: this.agent.model.capabilities
|
|
2273
|
-
}
|
|
2274
|
-
};
|
|
2275
|
-
if (providerRequest !== void 0) {
|
|
2276
|
-
args.providerRequest = providerRequest;
|
|
2277
|
-
}
|
|
2278
|
-
return args;
|
|
2279
|
-
}
|
|
2280
|
-
retryOptionsForFailure(error, attempt, turn, streaming) {
|
|
2281
|
-
const options = this.completionRetryOptions;
|
|
2282
|
-
if (options === void 0 || attempt >= options.maxAttempts) {
|
|
2283
|
-
return void 0;
|
|
2284
|
-
}
|
|
2285
|
-
const shouldRetry = options.shouldRetry({
|
|
2286
|
-
error,
|
|
2287
|
-
attempt,
|
|
2288
|
-
maxAttempts: options.maxAttempts,
|
|
2289
|
-
turn,
|
|
2290
|
-
streaming
|
|
2291
|
-
});
|
|
2292
|
-
return shouldRetry ? options : void 0;
|
|
2293
|
-
}
|
|
2294
|
-
async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers) {
|
|
2295
|
-
const delayMs = completionRetryDelayMs(options, attempt);
|
|
2296
|
-
await runObservers.event({
|
|
2297
|
-
name: "completion.retry",
|
|
2298
|
-
level: "WARNING",
|
|
2299
|
-
attributes: {
|
|
2300
|
-
turn,
|
|
2301
|
-
attempt,
|
|
2302
|
-
nextAttempt: attempt + 1,
|
|
2303
|
-
maxAttempts: options.maxAttempts,
|
|
2304
|
-
delayMs,
|
|
2305
|
-
streaming,
|
|
2306
|
-
...completionRetryErrorAttributes(error)
|
|
2307
|
-
}
|
|
2308
|
-
});
|
|
2309
|
-
await waitForCompletionRetry(delayMs);
|
|
2310
|
-
}
|
|
2311
|
-
async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
2312
|
-
const executor = new ToolCallExecutor(
|
|
2313
|
-
this.agent,
|
|
2314
|
-
this.activeHook,
|
|
2315
|
-
this.approvalOptions,
|
|
2316
|
-
{
|
|
2317
|
-
runId,
|
|
2318
|
-
sessionId: this.memoryContext?.sessionId,
|
|
2319
|
-
metadata: this.memoryContext?.metadata
|
|
2320
|
-
},
|
|
2321
|
-
this.concurrency,
|
|
2322
|
-
this.requestMiddlewares,
|
|
2323
|
-
observation?.includeToolCallDeltas ?? false,
|
|
2324
|
-
(reason) => this.cancelled(newMessages, reason)
|
|
2325
|
-
);
|
|
2326
|
-
return executor.execute(toolCalls, onResult, onStreamEvent, observation);
|
|
2327
|
-
}
|
|
2328
|
-
async runOutputGuardrailsForResponse(runId, usage, response, messages, runObservers) {
|
|
2329
|
-
const originalOutput = textFromAssistantContent(response.choice);
|
|
2330
|
-
const result = await runOutputGuardrails(this.guardrailPolicies, {
|
|
2331
|
-
outputText: originalOutput,
|
|
2332
|
-
messages: [...this.chatHistory, ...messages],
|
|
2333
|
-
usage,
|
|
2334
|
-
run: this.guardrailRunContext(runId)
|
|
2335
|
-
});
|
|
2336
|
-
for (const decision of result.decisions) {
|
|
2337
|
-
await this.recordGuardrailDecision(decision, runObservers);
|
|
2338
|
-
}
|
|
2339
|
-
const output = result.blocked ? result.message ?? "The response was blocked by a guardrail." : result.outputText;
|
|
2340
|
-
if (output === originalOutput) {
|
|
2341
|
-
return { output, response, decisions: result.decisions };
|
|
2342
|
-
}
|
|
2343
|
-
return {
|
|
2344
|
-
output,
|
|
2345
|
-
response: {
|
|
2346
|
-
...response,
|
|
2347
|
-
choice: [AssistantContent.text(output)]
|
|
2348
|
-
},
|
|
2349
|
-
decisions: result.decisions
|
|
2350
|
-
};
|
|
2351
|
-
}
|
|
2352
|
-
async recordGuardrailDecision(decision, runObservers) {
|
|
2353
|
-
this.guardrailDecisions.push(decision);
|
|
2354
|
-
await runObservers.event({
|
|
2355
|
-
name: "guardrail.decision",
|
|
2356
|
-
level: decision.action === "block" ? "WARNING" : "DEFAULT",
|
|
2357
|
-
attributes: guardrailDecisionAttributes(decision)
|
|
2358
|
-
});
|
|
2359
|
-
}
|
|
2360
|
-
async recordMemoryCompaction(preparation, runObservers) {
|
|
2361
|
-
const compaction = preparation.compaction;
|
|
2362
|
-
if (compaction === void 0) {
|
|
2363
|
-
return;
|
|
2364
|
-
}
|
|
2365
|
-
await runObservers.event({
|
|
2366
|
-
name: "memory.compaction",
|
|
2367
|
-
attributes: {
|
|
2368
|
-
originalMessageCount: compaction.originalMessageCount,
|
|
2369
|
-
compactedMessageCount: compaction.compactedMessageCount,
|
|
2370
|
-
retainedMessageCount: compaction.retainedMessageCount,
|
|
2371
|
-
conflictRetries: compaction.conflictRetries,
|
|
2372
|
-
inputTokens: preparation.usage.inputTokens,
|
|
2373
|
-
outputTokens: preparation.usage.outputTokens,
|
|
2374
|
-
totalTokens: preparation.usage.totalTokens
|
|
2375
|
-
}
|
|
2376
|
-
});
|
|
2377
|
-
}
|
|
2378
|
-
guardrailRunContext(runId) {
|
|
2379
|
-
const context = {
|
|
2380
|
-
agentId: this.agent.id,
|
|
2381
|
-
runId
|
|
2382
|
-
};
|
|
2383
|
-
if (this.memoryContext !== void 0) {
|
|
2384
|
-
context.sessionId = this.memoryContext.sessionId;
|
|
2385
|
-
if (this.memoryContext.metadata !== void 0) {
|
|
2386
|
-
context.metadata = this.memoryContext.metadata;
|
|
2387
|
-
}
|
|
2388
|
-
}
|
|
2389
|
-
return context;
|
|
2390
|
-
}
|
|
2391
|
-
async startRunObservers() {
|
|
2392
|
-
const failOnObserverError = this.traceOptions?.failOnObserverError === true || this.agent.observers.some((registration) => registration.failOnObserverError === true);
|
|
2393
|
-
return startAgentRunObservers(
|
|
2394
|
-
this.agent.observers,
|
|
2395
|
-
{
|
|
2396
|
-
agentName: this.agent.name,
|
|
2397
|
-
agentDescription: this.agent.description,
|
|
2398
|
-
instructions: this.agent.instructions,
|
|
2399
|
-
trace: this.traceOptions,
|
|
2400
|
-
promptRef: this.traceOptions?.promptRef,
|
|
2401
|
-
prompt: this.promptMessage,
|
|
2402
|
-
history: this.chatHistory,
|
|
2403
|
-
maxTurns: this.maxTurnCount
|
|
2404
|
-
},
|
|
2405
|
-
failOnObserverError
|
|
2406
|
-
);
|
|
2407
|
-
}
|
|
2408
|
-
async recordAgentEvent(runId, event) {
|
|
2409
|
-
const registration = this.agent.eventStore;
|
|
2410
|
-
if (registration === void 0) {
|
|
2411
|
-
return;
|
|
2412
|
-
}
|
|
2413
|
-
if (registration.options.include === "agent_tool_events" && event.type !== "agent_tool_event") {
|
|
2414
|
-
return;
|
|
2415
|
-
}
|
|
2416
|
-
const turn = "turn" in event ? event.turn : void 0;
|
|
2417
|
-
const agentId = event.type === "agent_tool_event" ? event.agentId : this.agent.id;
|
|
2418
|
-
const agentName = event.type === "agent_tool_event" ? event.agentName : this.agent.name;
|
|
2419
|
-
const input = {
|
|
2420
|
-
runId,
|
|
2421
|
-
agentId,
|
|
2422
|
-
event
|
|
2423
|
-
};
|
|
2424
|
-
if (agentName !== void 0) {
|
|
2425
|
-
input.agentName = agentName;
|
|
2426
|
-
}
|
|
2427
|
-
if (turn !== void 0) {
|
|
2428
|
-
input.turn = turn;
|
|
2429
|
-
}
|
|
2430
|
-
if (event.type === "agent_tool_event") {
|
|
2431
|
-
input.toolName = event.toolName;
|
|
2432
|
-
input.internalCallId = event.internalCallId;
|
|
2433
|
-
if (event.toolCallId !== void 0) {
|
|
2434
|
-
input.toolCallId = event.toolCallId;
|
|
2435
|
-
}
|
|
2436
|
-
}
|
|
2437
|
-
await registration.store.append(input);
|
|
2438
|
-
}
|
|
2439
|
-
async runCompletionCallHook(prompt, history, newMessages) {
|
|
2440
|
-
const action = await this.activeHook?.onCompletionCall?.({
|
|
2441
|
-
prompt,
|
|
2442
|
-
history,
|
|
2443
|
-
run: runControl
|
|
2444
|
-
});
|
|
2445
|
-
if (action?.type === "terminate") {
|
|
2446
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2447
|
-
}
|
|
2448
|
-
}
|
|
2449
|
-
async runRunStartHook(newMessages) {
|
|
2450
|
-
const action = await this.activeHook?.onRunStart?.({
|
|
2451
|
-
prompt: this.promptMessage,
|
|
2452
|
-
history: this.chatHistory,
|
|
2453
|
-
maxTurns: this.maxTurnCount,
|
|
2454
|
-
run: runControl
|
|
2455
|
-
});
|
|
2456
|
-
if (action?.type === "terminate") {
|
|
2457
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2458
|
-
}
|
|
2459
|
-
}
|
|
2460
|
-
async runRunEndHook(result, newMessages) {
|
|
2461
|
-
const action = await this.activeHook?.onRunEnd?.({
|
|
2462
|
-
output: result.output,
|
|
2463
|
-
usage: result.usage,
|
|
2464
|
-
messages: result.messages,
|
|
2465
|
-
run: runControl
|
|
2466
|
-
});
|
|
2467
|
-
if (action?.type === "terminate") {
|
|
2468
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2469
|
-
}
|
|
2470
|
-
}
|
|
2471
|
-
async runRunErrorHook(error, usage, newMessages) {
|
|
2472
|
-
const action = await this.activeHook?.onRunError?.({
|
|
2473
|
-
error,
|
|
2474
|
-
usage,
|
|
2475
|
-
messages: [...this.chatHistory, ...newMessages],
|
|
2476
|
-
run: runControl
|
|
2477
|
-
});
|
|
2478
|
-
if (action?.type === "terminate") {
|
|
2479
|
-
return this.cancelled(newMessages, action.reason);
|
|
2480
|
-
}
|
|
2481
|
-
return error;
|
|
2482
|
-
}
|
|
2483
|
-
async runTurnStartHook(turn, prompt, history, newMessages) {
|
|
2484
|
-
const action = await this.activeHook?.onTurnStart?.({
|
|
2485
|
-
turn,
|
|
2486
|
-
prompt,
|
|
2487
|
-
history,
|
|
2488
|
-
run: runControl
|
|
2489
|
-
});
|
|
2490
|
-
if (action?.type === "terminate") {
|
|
2491
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2492
|
-
}
|
|
2493
|
-
}
|
|
2494
|
-
async runTurnEndHook(turn, response, newMessages) {
|
|
2495
|
-
const action = await this.activeHook?.onTurnEnd?.({
|
|
2496
|
-
turn,
|
|
2497
|
-
response,
|
|
2498
|
-
run: runControl
|
|
2499
|
-
});
|
|
2500
|
-
if (action?.type === "terminate") {
|
|
2501
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2502
|
-
}
|
|
2503
|
-
}
|
|
2504
|
-
async runCompletionRequestMiddlewares(request, turn) {
|
|
2505
|
-
let current = request;
|
|
2506
|
-
for (const middleware of this.activeMiddlewares()) {
|
|
2507
|
-
const replacement = await middleware.onCompletionRequest?.({
|
|
2508
|
-
turn,
|
|
2509
|
-
request: current,
|
|
2510
|
-
originalRequest: request
|
|
2511
|
-
});
|
|
2512
|
-
if (replacement?.request !== void 0) {
|
|
2513
|
-
current = replacement.request;
|
|
2514
|
-
}
|
|
2515
|
-
}
|
|
2516
|
-
return current;
|
|
2517
|
-
}
|
|
2518
|
-
async runCompletionResponseMiddlewares(request, response, turn) {
|
|
2519
|
-
let current = response;
|
|
2520
|
-
for (const middleware of this.activeMiddlewares()) {
|
|
2521
|
-
const replacement = await middleware.onCompletionResponse?.({
|
|
2522
|
-
turn,
|
|
2523
|
-
request,
|
|
2524
|
-
response: current,
|
|
2525
|
-
originalResponse: response
|
|
2526
|
-
});
|
|
2527
|
-
if (replacement?.response !== void 0) {
|
|
2528
|
-
current = replacement.response;
|
|
2529
|
-
}
|
|
2530
|
-
}
|
|
2531
|
-
return current;
|
|
2532
|
-
}
|
|
2533
|
-
async runCompletionResponseHook(prompt, response, newMessages) {
|
|
2534
|
-
const action = await this.activeHook?.onCompletionResponse?.({
|
|
2535
|
-
prompt,
|
|
2536
|
-
response,
|
|
2537
|
-
run: runControl
|
|
2538
|
-
});
|
|
2539
|
-
if (action?.type === "terminate") {
|
|
2540
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2541
|
-
}
|
|
2542
|
-
}
|
|
2543
|
-
async runCompletionErrorHook(prompt, error, newMessages) {
|
|
2544
|
-
const action = await this.activeHook?.onCompletionError?.({
|
|
2545
|
-
prompt,
|
|
2546
|
-
error,
|
|
2547
|
-
run: runControl
|
|
2548
|
-
});
|
|
2549
|
-
if (action?.type === "terminate") {
|
|
2550
|
-
throw this.cancelled(newMessages, action.reason);
|
|
2551
|
-
}
|
|
2552
|
-
}
|
|
2553
|
-
activeMiddlewares() {
|
|
2554
|
-
return [...this.agent.middlewares, ...this.requestMiddlewares];
|
|
2555
|
-
}
|
|
2556
|
-
shouldBufferStreamResponseEvents() {
|
|
2557
|
-
return this.activeHook?.onCompletionResponse !== void 0 || this.activeMiddlewares().some((middleware) => middleware.onCompletionResponse !== void 0);
|
|
2558
|
-
}
|
|
2559
|
-
async drainSteeringMessages(runId, turn, newMessages, pendingTurnMessages) {
|
|
2560
|
-
const messages = this.steeringMessages.splice(0);
|
|
2561
|
-
if (messages.length === 0) {
|
|
2562
|
-
return false;
|
|
2563
|
-
}
|
|
2564
|
-
newMessages.push(...messages);
|
|
2565
|
-
await this.memoryRecorder.commitMessages(runId, turn, messages, pendingTurnMessages);
|
|
2566
|
-
return true;
|
|
2567
|
-
}
|
|
2568
|
-
startRun() {
|
|
2569
|
-
if (this.runState === "idle") {
|
|
2570
|
-
this.runState = "running";
|
|
2571
|
-
return;
|
|
2572
|
-
}
|
|
2573
|
-
if (this.runState === "running") {
|
|
2574
|
-
throw new Error("PromptRequest is already running.");
|
|
2575
|
-
}
|
|
2576
|
-
throw new Error("PromptRequest has already been used.");
|
|
2577
|
-
}
|
|
2578
|
-
isTerminal() {
|
|
2579
|
-
return this.runState === "completed" || this.runState === "errored" || this.runState === "cancelled";
|
|
2580
|
-
}
|
|
2581
|
-
cancelled(newMessages, reason) {
|
|
2582
|
-
return new PromptCancelledError([...this.chatHistory, ...newMessages], reason);
|
|
2583
|
-
}
|
|
2584
|
-
};
|
|
2585
|
-
function normalizePromptInput(prompt) {
|
|
2586
|
-
if (typeof prompt === "string") {
|
|
2587
|
-
return { prompt: Message.user(prompt), history: [] };
|
|
2588
|
-
}
|
|
2589
|
-
if (!Array.isArray(prompt)) {
|
|
2590
|
-
return { prompt, history: [] };
|
|
2591
|
-
}
|
|
2592
|
-
if (prompt.length === 0) {
|
|
2593
|
-
throw new TypeError("Prompt transcript must contain at least one message.");
|
|
2594
|
-
}
|
|
2595
|
-
const activePrompt = prompt.at(-1);
|
|
2596
|
-
if (activePrompt === void 0) {
|
|
2597
|
-
throw new TypeError("Prompt transcript must contain at least one message.");
|
|
2598
|
-
}
|
|
2599
|
-
return {
|
|
2600
|
-
prompt: activePrompt,
|
|
2601
|
-
history: prompt.slice(0, -1)
|
|
2602
|
-
};
|
|
2603
|
-
}
|
|
2604
|
-
function normalizeSteeringInput(input) {
|
|
2605
|
-
if (typeof input === "string") {
|
|
2606
|
-
return [Message.user(input)];
|
|
2607
|
-
}
|
|
2608
|
-
return Array.isArray(input) ? [...input] : [input];
|
|
2609
|
-
}
|
|
2610
|
-
function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
|
|
2611
|
-
const events = [];
|
|
2612
|
-
for (const item of response.choice) {
|
|
2613
|
-
if (item.type === "text") {
|
|
2614
|
-
if (item.text.length > 0) {
|
|
2615
|
-
events.push({ type: "text_delta", turn, delta: item.text });
|
|
2616
|
-
}
|
|
2617
|
-
continue;
|
|
2618
|
-
}
|
|
2619
|
-
if (item.type === "reasoning") {
|
|
2620
|
-
if (item.content === void 0) {
|
|
2621
|
-
if (item.text.length > 0) {
|
|
2622
|
-
events.push(reasoningDeltaEvent2(turn, item.text, { id: item.id }));
|
|
2623
|
-
}
|
|
2624
|
-
continue;
|
|
2625
|
-
}
|
|
2626
|
-
for (const content of item.content) {
|
|
2627
|
-
const delta = content.type === "encrypted" || content.type === "redacted" ? content.data : content.text;
|
|
2628
|
-
events.push(
|
|
2629
|
-
reasoningDeltaEvent2(turn, delta, {
|
|
2630
|
-
id: item.id,
|
|
2631
|
-
contentType: content.type,
|
|
2632
|
-
signature: content.type === "text" ? content.signature : void 0
|
|
2633
|
-
})
|
|
2634
|
-
);
|
|
2635
|
-
}
|
|
2636
|
-
continue;
|
|
2637
|
-
}
|
|
2638
|
-
if (item.type === "tool_call") {
|
|
2639
|
-
events.push({ type: "tool_call", turn, toolCall: item });
|
|
2640
|
-
}
|
|
2641
|
-
}
|
|
2642
|
-
if (includeProviderArtifacts) {
|
|
2643
|
-
for (const source of response.sources ?? []) {
|
|
2644
|
-
events.push({ type: "source", turn, source });
|
|
2645
|
-
}
|
|
2646
|
-
for (const toolCall of response.providerToolCalls ?? []) {
|
|
2647
|
-
events.push({ type: "provider_tool_call", turn, toolCall });
|
|
2648
|
-
}
|
|
2649
|
-
}
|
|
2650
|
-
return events;
|
|
2651
|
-
}
|
|
2652
|
-
function generationArtifacts(messages) {
|
|
2653
|
-
const sources = /* @__PURE__ */ new Map();
|
|
2654
|
-
const providerToolCalls = /* @__PURE__ */ new Map();
|
|
2655
|
-
let contextUsage;
|
|
2656
|
-
for (const message of messages) {
|
|
2657
|
-
const metadata = getAssistantGenerationMetadata(message);
|
|
2658
|
-
if (metadata !== void 0) {
|
|
2659
|
-
contextUsage = metadata.contextUsage;
|
|
2660
|
-
}
|
|
2661
|
-
for (const source of metadata?.sources ?? []) {
|
|
2662
|
-
const key = `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
|
|
2663
|
-
sources.set(key, source);
|
|
2664
|
-
}
|
|
2665
|
-
for (const toolCall of metadata?.providerToolCalls ?? []) {
|
|
2666
|
-
providerToolCalls.set(toolCall.id, toolCall);
|
|
2667
|
-
}
|
|
2668
|
-
}
|
|
2669
|
-
return {
|
|
2670
|
-
...sources.size === 0 ? {} : { sources: [...sources.values()] },
|
|
2671
|
-
...providerToolCalls.size === 0 ? {} : { providerToolCalls: [...providerToolCalls.values()] },
|
|
2672
|
-
...contextUsage === void 0 ? {} : { contextUsage }
|
|
2673
|
-
};
|
|
2674
|
-
}
|
|
2675
|
-
function reasoningDeltaEvent2(turn, delta, details = {}) {
|
|
2676
|
-
const event = {
|
|
2677
|
-
type: "reasoning_delta",
|
|
2678
|
-
turn,
|
|
2679
|
-
delta
|
|
2680
|
-
};
|
|
2681
|
-
if (details.id !== void 0) {
|
|
2682
|
-
event.id = details.id;
|
|
2683
|
-
}
|
|
2684
|
-
if (details.contentType !== void 0) {
|
|
2685
|
-
event.contentType = details.contentType;
|
|
2686
|
-
}
|
|
2687
|
-
if (details.signature !== void 0) {
|
|
2688
|
-
event.signature = details.signature;
|
|
2689
|
-
}
|
|
2690
|
-
return event;
|
|
2691
|
-
}
|
|
2692
|
-
function textFromMessage(message) {
|
|
2693
|
-
if (message.role === "system") {
|
|
2694
|
-
return message.content;
|
|
2695
|
-
}
|
|
2696
|
-
return message.content.flatMap((content) => {
|
|
2697
|
-
if (content.type === "text") {
|
|
2698
|
-
return [content.text];
|
|
2699
|
-
}
|
|
2700
|
-
if (content.type === "document" && content.source.type === "text") {
|
|
2701
|
-
return [content.source.text];
|
|
2702
|
-
}
|
|
2703
|
-
return [];
|
|
2704
|
-
}).join("\n");
|
|
2705
|
-
}
|
|
2706
|
-
function guardrailDecisionAttributes(decision) {
|
|
2707
|
-
const attributes = {
|
|
2708
|
-
policyId: decision.policyId,
|
|
2709
|
-
guardrailId: decision.guardrailId,
|
|
2710
|
-
boundary: decision.boundary,
|
|
2711
|
-
mode: decision.mode,
|
|
2712
|
-
action: decision.action,
|
|
2713
|
-
applied: decision.applied,
|
|
2714
|
-
latencyMs: decision.latencyMs
|
|
2715
|
-
};
|
|
2716
|
-
if (decision.reason !== void 0) {
|
|
2717
|
-
attributes.reason = decision.reason;
|
|
2718
|
-
}
|
|
2719
|
-
if (decision.message !== void 0) {
|
|
2720
|
-
attributes.message = decision.message;
|
|
2721
|
-
}
|
|
2722
|
-
return attributes;
|
|
2723
|
-
}
|
|
2724
|
-
|
|
2725
|
-
export {
|
|
2726
|
-
MaxTurnsError,
|
|
2727
|
-
PromptCancelledError,
|
|
2728
|
-
ToolApprovalRequiredError,
|
|
2729
|
-
extractRagText,
|
|
2730
|
-
PromptRequest
|
|
2731
|
-
};
|
|
2732
|
-
//# sourceMappingURL=chunk-BALBBVI3.js.map
|