@ai-sdk/workflow 1.0.0-canary.39 → 1.0.0-canary.41
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/CHANGELOG.md +24 -0
- package/dist/index.d.mts +131 -55
- package/dist/index.mjs +160 -84
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/do-stream-step.ts +5 -2
- package/src/stream-text-iterator.ts +46 -26
- package/src/test/agent-e2e-workflows.ts +80 -0
- package/src/workflow-agent.ts +478 -220
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/workflow-agent.ts
|
|
2
|
+
import {
|
|
3
|
+
validateTypes
|
|
4
|
+
} from "@ai-sdk/provider-utils";
|
|
2
5
|
import {
|
|
3
6
|
Output,
|
|
4
7
|
experimental_filterActiveTools as filterActiveTools2
|
|
@@ -85,7 +88,7 @@ function resolveSerializableTools(tools) {
|
|
|
85
88
|
// src/do-stream-step.ts
|
|
86
89
|
async function doStreamStep(conversationPrompt, modelInit, writable, serializedTools, options) {
|
|
87
90
|
"use step";
|
|
88
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
91
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
89
92
|
const model = typeof modelInit === "string" ? gateway.languageModel(modelInit) : modelInit;
|
|
90
93
|
const tools = serializedTools ? resolveSerializableTools(serializedTools) : void 0;
|
|
91
94
|
const { stream: modelStream } = await streamModelCall({
|
|
@@ -197,8 +200,8 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
197
200
|
},
|
|
198
201
|
functionId: void 0,
|
|
199
202
|
metadata: void 0,
|
|
200
|
-
runtimeContext: void 0,
|
|
201
|
-
toolsContext: {},
|
|
203
|
+
runtimeContext: (_d = options == null ? void 0 : options.runtimeContext) != null ? _d : {},
|
|
204
|
+
toolsContext: (_e = options == null ? void 0 : options.toolsContext) != null ? _e : {},
|
|
202
205
|
content: [
|
|
203
206
|
...text ? [{ type: "text", text }] : [],
|
|
204
207
|
...toolCalls.filter((tc) => !tc.invalid).map((tc) => ({
|
|
@@ -235,9 +238,9 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
235
238
|
toolResults: [],
|
|
236
239
|
staticToolResults: [],
|
|
237
240
|
dynamicToolResults: [],
|
|
238
|
-
finishReason: (
|
|
241
|
+
finishReason: (_f = finish == null ? void 0 : finish.finishReason) != null ? _f : "other",
|
|
239
242
|
rawFinishReason: finish == null ? void 0 : finish.rawFinishReason,
|
|
240
|
-
usage: (
|
|
243
|
+
usage: (_g = finish == null ? void 0 : finish.usage) != null ? _g : {
|
|
241
244
|
inputTokens: 0,
|
|
242
245
|
inputTokenDetails: {
|
|
243
246
|
noCacheTokens: void 0,
|
|
@@ -258,12 +261,12 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
258
261
|
// TODO implement step request messages
|
|
259
262
|
},
|
|
260
263
|
response: {
|
|
261
|
-
id: (
|
|
262
|
-
timestamp: (
|
|
263
|
-
modelId: (
|
|
264
|
+
id: (_h = responseMetadata == null ? void 0 : responseMetadata.id) != null ? _h : "unknown",
|
|
265
|
+
timestamp: (_i = responseMetadata == null ? void 0 : responseMetadata.timestamp) != null ? _i : /* @__PURE__ */ new Date(),
|
|
266
|
+
modelId: (_j = responseMetadata == null ? void 0 : responseMetadata.modelId) != null ? _j : "unknown",
|
|
264
267
|
messages: []
|
|
265
268
|
},
|
|
266
|
-
providerMetadata: (
|
|
269
|
+
providerMetadata: (_k = finish == null ? void 0 : finish.providerMetadata) != null ? _k : {}
|
|
267
270
|
};
|
|
268
271
|
return {
|
|
269
272
|
toolCalls,
|
|
@@ -286,7 +289,8 @@ async function* streamTextIterator({
|
|
|
286
289
|
prepareStep,
|
|
287
290
|
generationSettings,
|
|
288
291
|
toolChoice,
|
|
289
|
-
|
|
292
|
+
runtimeContext,
|
|
293
|
+
toolsContext,
|
|
290
294
|
telemetry,
|
|
291
295
|
includeRawChunks = false,
|
|
292
296
|
repairToolCall,
|
|
@@ -297,7 +301,8 @@ async function* streamTextIterator({
|
|
|
297
301
|
let currentModel = model;
|
|
298
302
|
let currentGenerationSettings = generationSettings != null ? generationSettings : {};
|
|
299
303
|
let currentToolChoice = toolChoice;
|
|
300
|
-
let
|
|
304
|
+
let currentRuntimeContext = runtimeContext != null ? runtimeContext : {};
|
|
305
|
+
let currentToolsContext = toolsContext != null ? toolsContext : {};
|
|
301
306
|
let currentActiveTools;
|
|
302
307
|
const steps = [];
|
|
303
308
|
let done = false;
|
|
@@ -315,15 +320,16 @@ async function* streamTextIterator({
|
|
|
315
320
|
stepNumber,
|
|
316
321
|
steps,
|
|
317
322
|
messages: conversationPrompt,
|
|
318
|
-
|
|
323
|
+
runtimeContext: currentRuntimeContext,
|
|
324
|
+
toolsContext: currentToolsContext
|
|
319
325
|
});
|
|
320
|
-
if (prepareResult.model !== void 0) {
|
|
326
|
+
if ((prepareResult == null ? void 0 : prepareResult.model) !== void 0) {
|
|
321
327
|
currentModel = prepareResult.model;
|
|
322
328
|
}
|
|
323
|
-
if (prepareResult.messages !== void 0) {
|
|
329
|
+
if ((prepareResult == null ? void 0 : prepareResult.messages) !== void 0) {
|
|
324
330
|
conversationPrompt = [...prepareResult.messages];
|
|
325
331
|
}
|
|
326
|
-
if (prepareResult.system !== void 0) {
|
|
332
|
+
if ((prepareResult == null ? void 0 : prepareResult.system) !== void 0) {
|
|
327
333
|
if (conversationPrompt.length > 0 && conversationPrompt[0].role === "system") {
|
|
328
334
|
conversationPrompt[0] = {
|
|
329
335
|
role: "system",
|
|
@@ -336,79 +342,82 @@ async function* streamTextIterator({
|
|
|
336
342
|
});
|
|
337
343
|
}
|
|
338
344
|
}
|
|
339
|
-
if (prepareResult.
|
|
340
|
-
|
|
345
|
+
if ((prepareResult == null ? void 0 : prepareResult.runtimeContext) !== void 0) {
|
|
346
|
+
currentRuntimeContext = prepareResult.runtimeContext;
|
|
341
347
|
}
|
|
342
|
-
if (prepareResult.
|
|
348
|
+
if ((prepareResult == null ? void 0 : prepareResult.toolsContext) !== void 0) {
|
|
349
|
+
currentToolsContext = prepareResult.toolsContext;
|
|
350
|
+
}
|
|
351
|
+
if ((prepareResult == null ? void 0 : prepareResult.activeTools) !== void 0) {
|
|
343
352
|
currentActiveTools = prepareResult.activeTools;
|
|
344
353
|
}
|
|
345
|
-
if (prepareResult.maxOutputTokens !== void 0) {
|
|
354
|
+
if ((prepareResult == null ? void 0 : prepareResult.maxOutputTokens) !== void 0) {
|
|
346
355
|
currentGenerationSettings = {
|
|
347
356
|
...currentGenerationSettings,
|
|
348
357
|
maxOutputTokens: prepareResult.maxOutputTokens
|
|
349
358
|
};
|
|
350
359
|
}
|
|
351
|
-
if (prepareResult.temperature !== void 0) {
|
|
360
|
+
if ((prepareResult == null ? void 0 : prepareResult.temperature) !== void 0) {
|
|
352
361
|
currentGenerationSettings = {
|
|
353
362
|
...currentGenerationSettings,
|
|
354
363
|
temperature: prepareResult.temperature
|
|
355
364
|
};
|
|
356
365
|
}
|
|
357
|
-
if (prepareResult.topP !== void 0) {
|
|
366
|
+
if ((prepareResult == null ? void 0 : prepareResult.topP) !== void 0) {
|
|
358
367
|
currentGenerationSettings = {
|
|
359
368
|
...currentGenerationSettings,
|
|
360
369
|
topP: prepareResult.topP
|
|
361
370
|
};
|
|
362
371
|
}
|
|
363
|
-
if (prepareResult.topK !== void 0) {
|
|
372
|
+
if ((prepareResult == null ? void 0 : prepareResult.topK) !== void 0) {
|
|
364
373
|
currentGenerationSettings = {
|
|
365
374
|
...currentGenerationSettings,
|
|
366
375
|
topK: prepareResult.topK
|
|
367
376
|
};
|
|
368
377
|
}
|
|
369
|
-
if (prepareResult.presencePenalty !== void 0) {
|
|
378
|
+
if ((prepareResult == null ? void 0 : prepareResult.presencePenalty) !== void 0) {
|
|
370
379
|
currentGenerationSettings = {
|
|
371
380
|
...currentGenerationSettings,
|
|
372
381
|
presencePenalty: prepareResult.presencePenalty
|
|
373
382
|
};
|
|
374
383
|
}
|
|
375
|
-
if (prepareResult.frequencyPenalty !== void 0) {
|
|
384
|
+
if ((prepareResult == null ? void 0 : prepareResult.frequencyPenalty) !== void 0) {
|
|
376
385
|
currentGenerationSettings = {
|
|
377
386
|
...currentGenerationSettings,
|
|
378
387
|
frequencyPenalty: prepareResult.frequencyPenalty
|
|
379
388
|
};
|
|
380
389
|
}
|
|
381
|
-
if (prepareResult.stopSequences !== void 0) {
|
|
390
|
+
if ((prepareResult == null ? void 0 : prepareResult.stopSequences) !== void 0) {
|
|
382
391
|
currentGenerationSettings = {
|
|
383
392
|
...currentGenerationSettings,
|
|
384
393
|
stopSequences: prepareResult.stopSequences
|
|
385
394
|
};
|
|
386
395
|
}
|
|
387
|
-
if (prepareResult.seed !== void 0) {
|
|
396
|
+
if ((prepareResult == null ? void 0 : prepareResult.seed) !== void 0) {
|
|
388
397
|
currentGenerationSettings = {
|
|
389
398
|
...currentGenerationSettings,
|
|
390
399
|
seed: prepareResult.seed
|
|
391
400
|
};
|
|
392
401
|
}
|
|
393
|
-
if (prepareResult.maxRetries !== void 0) {
|
|
402
|
+
if ((prepareResult == null ? void 0 : prepareResult.maxRetries) !== void 0) {
|
|
394
403
|
currentGenerationSettings = {
|
|
395
404
|
...currentGenerationSettings,
|
|
396
405
|
maxRetries: prepareResult.maxRetries
|
|
397
406
|
};
|
|
398
407
|
}
|
|
399
|
-
if (prepareResult.headers !== void 0) {
|
|
408
|
+
if ((prepareResult == null ? void 0 : prepareResult.headers) !== void 0) {
|
|
400
409
|
currentGenerationSettings = {
|
|
401
410
|
...currentGenerationSettings,
|
|
402
411
|
headers: prepareResult.headers
|
|
403
412
|
};
|
|
404
413
|
}
|
|
405
|
-
if (prepareResult.providerOptions !== void 0) {
|
|
414
|
+
if ((prepareResult == null ? void 0 : prepareResult.providerOptions) !== void 0) {
|
|
406
415
|
currentGenerationSettings = {
|
|
407
416
|
...currentGenerationSettings,
|
|
408
417
|
providerOptions: prepareResult.providerOptions
|
|
409
418
|
};
|
|
410
419
|
}
|
|
411
|
-
if (prepareResult.toolChoice !== void 0) {
|
|
420
|
+
if ((prepareResult == null ? void 0 : prepareResult.toolChoice) !== void 0) {
|
|
412
421
|
currentToolChoice = prepareResult.toolChoice;
|
|
413
422
|
}
|
|
414
423
|
}
|
|
@@ -417,7 +426,9 @@ async function* streamTextIterator({
|
|
|
417
426
|
stepNumber,
|
|
418
427
|
model: currentModel,
|
|
419
428
|
messages: conversationPrompt,
|
|
420
|
-
steps: [...steps]
|
|
429
|
+
steps: [...steps],
|
|
430
|
+
runtimeContext: currentRuntimeContext,
|
|
431
|
+
toolsContext: currentToolsContext
|
|
421
432
|
});
|
|
422
433
|
}
|
|
423
434
|
try {
|
|
@@ -437,7 +448,9 @@ async function* streamTextIterator({
|
|
|
437
448
|
includeRawChunks,
|
|
438
449
|
telemetry,
|
|
439
450
|
repairToolCall,
|
|
440
|
-
responseFormat
|
|
451
|
+
responseFormat,
|
|
452
|
+
runtimeContext: currentRuntimeContext,
|
|
453
|
+
toolsContext: currentToolsContext
|
|
441
454
|
}
|
|
442
455
|
);
|
|
443
456
|
_isFirstIteration = false;
|
|
@@ -467,7 +480,8 @@ async function* streamTextIterator({
|
|
|
467
480
|
toolCalls,
|
|
468
481
|
messages: conversationPrompt,
|
|
469
482
|
step,
|
|
470
|
-
|
|
483
|
+
runtimeContext: currentRuntimeContext,
|
|
484
|
+
toolsContext: currentToolsContext,
|
|
471
485
|
providerExecutedToolResults
|
|
472
486
|
};
|
|
473
487
|
conversationPrompt.push({
|
|
@@ -523,7 +537,8 @@ async function* streamTextIterator({
|
|
|
523
537
|
toolCalls: [],
|
|
524
538
|
messages: conversationPrompt,
|
|
525
539
|
step: lastStep,
|
|
526
|
-
|
|
540
|
+
runtimeContext: currentRuntimeContext,
|
|
541
|
+
toolsContext: currentToolsContext
|
|
527
542
|
};
|
|
528
543
|
}
|
|
529
544
|
return conversationPrompt;
|
|
@@ -559,7 +574,8 @@ var WorkflowAgent = class {
|
|
|
559
574
|
this.instructions = (_b = options.instructions) != null ? _b : options.system;
|
|
560
575
|
this.toolChoice = options.toolChoice;
|
|
561
576
|
this.telemetry = (_c = options.telemetry) != null ? _c : options.experimental_telemetry;
|
|
562
|
-
this.
|
|
577
|
+
this.runtimeContext = options.runtimeContext;
|
|
578
|
+
this.toolsContext = options.toolsContext;
|
|
563
579
|
this.stopWhen = options.stopWhen;
|
|
564
580
|
this.activeTools = options.activeTools;
|
|
565
581
|
this.output = options.output;
|
|
@@ -592,16 +608,17 @@ var WorkflowAgent = class {
|
|
|
592
608
|
throw new Error("Not implemented");
|
|
593
609
|
}
|
|
594
610
|
async stream(options) {
|
|
595
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C;
|
|
611
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F;
|
|
596
612
|
let effectiveModel = this.model;
|
|
597
613
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
598
614
|
let effectivePrompt = options.prompt;
|
|
599
615
|
let effectiveMessages = options.messages;
|
|
600
616
|
let effectiveGenerationSettings = { ...this.generationSettings };
|
|
601
|
-
let
|
|
602
|
-
let
|
|
603
|
-
let
|
|
604
|
-
|
|
617
|
+
let effectiveRuntimeContext = (_c = (_b = options.runtimeContext) != null ? _b : this.runtimeContext) != null ? _c : {};
|
|
618
|
+
let effectiveToolsContext = (_e = (_d = options.toolsContext) != null ? _d : this.toolsContext) != null ? _e : {};
|
|
619
|
+
let effectiveToolChoiceFromPrepare = (_f = options.toolChoice) != null ? _f : this.toolChoice;
|
|
620
|
+
let effectiveTelemetryFromPrepare = (_h = (_g = options.telemetry) != null ? _g : options.experimental_telemetry) != null ? _h : this.telemetry;
|
|
621
|
+
const resolvedMessagesForPrepareCall = (_i = effectiveMessages != null ? effectiveMessages : typeof effectivePrompt === "string" ? [{ role: "user", content: effectivePrompt }] : effectivePrompt) != null ? _i : [];
|
|
605
622
|
if (this.prepareCall) {
|
|
606
623
|
const prepared = await this.prepareCall({
|
|
607
624
|
model: effectiveModel,
|
|
@@ -610,7 +627,8 @@ var WorkflowAgent = class {
|
|
|
610
627
|
toolChoice: effectiveToolChoiceFromPrepare,
|
|
611
628
|
telemetry: effectiveTelemetryFromPrepare,
|
|
612
629
|
experimental_telemetry: effectiveTelemetryFromPrepare,
|
|
613
|
-
|
|
630
|
+
runtimeContext: effectiveRuntimeContext,
|
|
631
|
+
toolsContext: effectiveToolsContext,
|
|
614
632
|
messages: resolvedMessagesForPrepareCall,
|
|
615
633
|
...effectiveGenerationSettings
|
|
616
634
|
});
|
|
@@ -621,8 +639,10 @@ var WorkflowAgent = class {
|
|
|
621
639
|
effectiveMessages = prepared.messages;
|
|
622
640
|
effectivePrompt = void 0;
|
|
623
641
|
}
|
|
624
|
-
if (prepared.
|
|
625
|
-
|
|
642
|
+
if (prepared.runtimeContext !== void 0)
|
|
643
|
+
effectiveRuntimeContext = prepared.runtimeContext;
|
|
644
|
+
if (prepared.toolsContext !== void 0)
|
|
645
|
+
effectiveToolsContext = prepared.toolsContext;
|
|
626
646
|
if (prepared.toolChoice !== void 0)
|
|
627
647
|
effectiveToolChoiceFromPrepare = prepared.toolChoice;
|
|
628
648
|
if (prepared.telemetry !== void 0)
|
|
@@ -665,10 +685,15 @@ var WorkflowAgent = class {
|
|
|
665
685
|
if (tool2 && typeof tool2.execute === "function") {
|
|
666
686
|
try {
|
|
667
687
|
const { execute } = tool2;
|
|
688
|
+
const resolvedContext = await resolveToolContext({
|
|
689
|
+
toolName: approval.toolName,
|
|
690
|
+
tool: tool2,
|
|
691
|
+
toolsContext: effectiveToolsContext
|
|
692
|
+
});
|
|
668
693
|
const toolResult = await execute(approval.input, {
|
|
669
694
|
toolCallId: approval.toolCallId,
|
|
670
695
|
messages: [],
|
|
671
|
-
context:
|
|
696
|
+
context: resolvedContext
|
|
672
697
|
});
|
|
673
698
|
toolResultContent.push({
|
|
674
699
|
type: "tool-result",
|
|
@@ -750,10 +775,10 @@ var WorkflowAgent = class {
|
|
|
750
775
|
const modelPrompt = await convertToLanguageModelPrompt({
|
|
751
776
|
prompt,
|
|
752
777
|
supportedUrls: {},
|
|
753
|
-
download: (
|
|
778
|
+
download: (_j = options.experimental_download) != null ? _j : this.experimentalDownload
|
|
754
779
|
});
|
|
755
780
|
const effectiveAbortSignal = mergeAbortSignals(
|
|
756
|
-
(
|
|
781
|
+
(_k = options.abortSignal) != null ? _k : effectiveGenerationSettings.abortSignal,
|
|
757
782
|
options.timeout
|
|
758
783
|
);
|
|
759
784
|
const mergedGenerationSettings = {
|
|
@@ -813,38 +838,50 @@ var WorkflowAgent = class {
|
|
|
813
838
|
);
|
|
814
839
|
const effectiveToolChoice = effectiveToolChoiceFromPrepare;
|
|
815
840
|
const effectiveTelemetry = effectiveTelemetryFromPrepare;
|
|
816
|
-
const effectiveActiveTools = (
|
|
817
|
-
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (
|
|
841
|
+
const effectiveActiveTools = (_l = options.activeTools) != null ? _l : this.activeTools;
|
|
842
|
+
const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? (_m = filterActiveTools2({
|
|
818
843
|
tools: this.tools,
|
|
819
844
|
activeTools: effectiveActiveTools
|
|
820
|
-
})) != null ?
|
|
821
|
-
let
|
|
845
|
+
})) != null ? _m : this.tools : this.tools;
|
|
846
|
+
let runtimeContext = effectiveRuntimeContext;
|
|
847
|
+
let toolsContext = effectiveToolsContext;
|
|
822
848
|
const steps = [];
|
|
823
849
|
let lastStepToolCalls = [];
|
|
824
850
|
let lastStepToolResults = [];
|
|
825
851
|
if (mergedOnStart) {
|
|
826
852
|
await mergedOnStart({
|
|
827
853
|
model: effectiveModel,
|
|
828
|
-
messages: prompt.messages
|
|
854
|
+
messages: prompt.messages,
|
|
855
|
+
runtimeContext,
|
|
856
|
+
toolsContext
|
|
829
857
|
});
|
|
830
858
|
}
|
|
831
|
-
const executeToolWithCallbacks = async (toolCall, tools, messages2,
|
|
859
|
+
const executeToolWithCallbacks = async (toolCall, tools, messages2, perToolContexts, currentStepNumber = 0) => {
|
|
832
860
|
const toolCallEvent = {
|
|
833
861
|
type: "tool-call",
|
|
834
862
|
toolCallId: toolCall.toolCallId,
|
|
835
863
|
toolName: toolCall.toolName,
|
|
836
864
|
input: toolCall.input
|
|
837
865
|
};
|
|
866
|
+
const tool2 = tools[toolCall.toolName];
|
|
867
|
+
const resolvedContext = tool2 ? await resolveToolContext({
|
|
868
|
+
toolName: toolCall.toolName,
|
|
869
|
+
tool: tool2,
|
|
870
|
+
toolsContext: perToolContexts
|
|
871
|
+
}) : void 0;
|
|
872
|
+
const modelMessages = getToolCallbackMessages(messages2);
|
|
838
873
|
if (mergedOnToolExecutionStart) {
|
|
839
874
|
await mergedOnToolExecutionStart({
|
|
840
875
|
toolCall: toolCallEvent,
|
|
841
|
-
stepNumber: currentStepNumber
|
|
876
|
+
stepNumber: currentStepNumber,
|
|
877
|
+
messages: modelMessages,
|
|
878
|
+
toolContext: resolvedContext
|
|
842
879
|
});
|
|
843
880
|
}
|
|
844
881
|
const startTime = Date.now();
|
|
845
882
|
let result;
|
|
846
883
|
try {
|
|
847
|
-
result = await executeTool(toolCall, tools, messages2,
|
|
884
|
+
result = await executeTool(toolCall, tools, messages2, resolvedContext);
|
|
848
885
|
} catch (err) {
|
|
849
886
|
const durationMs2 = Date.now() - startTime;
|
|
850
887
|
if (mergedOnToolExecutionEnd) {
|
|
@@ -852,6 +889,8 @@ var WorkflowAgent = class {
|
|
|
852
889
|
toolCall: toolCallEvent,
|
|
853
890
|
stepNumber: currentStepNumber,
|
|
854
891
|
durationMs: durationMs2,
|
|
892
|
+
messages: modelMessages,
|
|
893
|
+
toolContext: resolvedContext,
|
|
855
894
|
success: false,
|
|
856
895
|
error: err
|
|
857
896
|
});
|
|
@@ -866,6 +905,8 @@ var WorkflowAgent = class {
|
|
|
866
905
|
toolCall: toolCallEvent,
|
|
867
906
|
stepNumber: currentStepNumber,
|
|
868
907
|
durationMs,
|
|
908
|
+
messages: modelMessages,
|
|
909
|
+
toolContext: resolvedContext,
|
|
869
910
|
success: false,
|
|
870
911
|
error: "value" in result.output ? result.output.value : void 0
|
|
871
912
|
});
|
|
@@ -874,6 +915,8 @@ var WorkflowAgent = class {
|
|
|
874
915
|
toolCall: toolCallEvent,
|
|
875
916
|
stepNumber: currentStepNumber,
|
|
876
917
|
durationMs,
|
|
918
|
+
messages: modelMessages,
|
|
919
|
+
toolContext: resolvedContext,
|
|
877
920
|
success: true,
|
|
878
921
|
output: result.output && "value" in result.output ? result.output.value : void 0
|
|
879
922
|
});
|
|
@@ -881,7 +924,7 @@ var WorkflowAgent = class {
|
|
|
881
924
|
}
|
|
882
925
|
return result;
|
|
883
926
|
};
|
|
884
|
-
if ((
|
|
927
|
+
if ((_n = mergedGenerationSettings.abortSignal) == null ? void 0 : _n.aborted) {
|
|
885
928
|
if (options.onAbort) {
|
|
886
929
|
await options.onAbort({ steps });
|
|
887
930
|
}
|
|
@@ -898,18 +941,19 @@ var WorkflowAgent = class {
|
|
|
898
941
|
tools: effectiveTools,
|
|
899
942
|
writable: options.writable,
|
|
900
943
|
prompt: modelPrompt,
|
|
901
|
-
stopConditions: (
|
|
944
|
+
stopConditions: (_o = options.stopWhen) != null ? _o : this.stopWhen,
|
|
902
945
|
onStepFinish: mergedOnStepFinish,
|
|
903
946
|
onStepStart: mergedOnStepStart,
|
|
904
947
|
onError: options.onError,
|
|
905
|
-
prepareStep: (
|
|
948
|
+
prepareStep: (_p = options.prepareStep) != null ? _p : this.prepareStep,
|
|
906
949
|
generationSettings: mergedGenerationSettings,
|
|
907
950
|
toolChoice: effectiveToolChoice,
|
|
908
|
-
|
|
951
|
+
runtimeContext,
|
|
952
|
+
toolsContext,
|
|
909
953
|
telemetry: effectiveTelemetry,
|
|
910
|
-
includeRawChunks: (
|
|
911
|
-
repairToolCall: (
|
|
912
|
-
responseFormat: await ((
|
|
954
|
+
includeRawChunks: (_q = options.includeRawChunks) != null ? _q : false,
|
|
955
|
+
repairToolCall: (_r = options.experimental_repairToolCall) != null ? _r : this.experimentalRepairToolCall,
|
|
956
|
+
responseFormat: await ((_t = (_s = options.output) != null ? _s : this.output) == null ? void 0 : _t.responseFormat)
|
|
913
957
|
});
|
|
914
958
|
let finalMessages;
|
|
915
959
|
let encounteredError;
|
|
@@ -917,7 +961,7 @@ var WorkflowAgent = class {
|
|
|
917
961
|
try {
|
|
918
962
|
let result = await iterator.next();
|
|
919
963
|
while (!result.done) {
|
|
920
|
-
if ((
|
|
964
|
+
if ((_u = mergedGenerationSettings.abortSignal) == null ? void 0 : _u.aborted) {
|
|
921
965
|
wasAborted = true;
|
|
922
966
|
if (options.onAbort) {
|
|
923
967
|
await options.onAbort({ steps });
|
|
@@ -928,15 +972,19 @@ var WorkflowAgent = class {
|
|
|
928
972
|
toolCalls,
|
|
929
973
|
messages: iterMessages,
|
|
930
974
|
step,
|
|
931
|
-
|
|
975
|
+
runtimeContext: yieldedRuntimeContext,
|
|
976
|
+
toolsContext: yieldedToolsContext,
|
|
932
977
|
providerExecutedToolResults
|
|
933
978
|
} = result.value;
|
|
934
979
|
const currentStepNumber = steps.length;
|
|
935
980
|
if (step) {
|
|
936
981
|
steps.push(step);
|
|
937
982
|
}
|
|
938
|
-
if (
|
|
939
|
-
|
|
983
|
+
if (yieldedRuntimeContext !== void 0) {
|
|
984
|
+
runtimeContext = yieldedRuntimeContext;
|
|
985
|
+
}
|
|
986
|
+
if (yieldedToolsContext !== void 0) {
|
|
987
|
+
toolsContext = yieldedToolsContext;
|
|
940
988
|
}
|
|
941
989
|
if (toolCalls.length > 0) {
|
|
942
990
|
const invalidToolCalls = toolCalls.filter((tc) => tc.invalid === true);
|
|
@@ -954,10 +1002,15 @@ var WorkflowAgent = class {
|
|
|
954
1002
|
if (tool2.needsApproval == null) return false;
|
|
955
1003
|
if (typeof tool2.needsApproval === "boolean")
|
|
956
1004
|
return tool2.needsApproval;
|
|
1005
|
+
const resolvedContext = await resolveToolContext({
|
|
1006
|
+
toolName: tc.toolName,
|
|
1007
|
+
tool: tool2,
|
|
1008
|
+
toolsContext
|
|
1009
|
+
});
|
|
957
1010
|
return tool2.needsApproval(tc.input, {
|
|
958
1011
|
toolCallId: tc.toolCallId,
|
|
959
1012
|
messages: iterMessages,
|
|
960
|
-
context:
|
|
1013
|
+
context: resolvedContext
|
|
961
1014
|
});
|
|
962
1015
|
})
|
|
963
1016
|
);
|
|
@@ -976,7 +1029,7 @@ var WorkflowAgent = class {
|
|
|
976
1029
|
toolCall,
|
|
977
1030
|
effectiveTools,
|
|
978
1031
|
iterMessages,
|
|
979
|
-
|
|
1032
|
+
toolsContext,
|
|
980
1033
|
currentStepNumber
|
|
981
1034
|
)
|
|
982
1035
|
)
|
|
@@ -1024,10 +1077,11 @@ var WorkflowAgent = class {
|
|
|
1024
1077
|
await mergedOnFinish({
|
|
1025
1078
|
steps,
|
|
1026
1079
|
messages: messages2,
|
|
1027
|
-
text: (
|
|
1028
|
-
finishReason: (
|
|
1080
|
+
text: (_v = lastStep == null ? void 0 : lastStep.text) != null ? _v : "",
|
|
1081
|
+
finishReason: (_w = lastStep == null ? void 0 : lastStep.finishReason) != null ? _w : "other",
|
|
1029
1082
|
totalUsage: aggregateUsage(steps),
|
|
1030
|
-
|
|
1083
|
+
runtimeContext,
|
|
1084
|
+
toolsContext,
|
|
1031
1085
|
output: void 0
|
|
1032
1086
|
});
|
|
1033
1087
|
}
|
|
@@ -1049,8 +1103,8 @@ var WorkflowAgent = class {
|
|
|
1049
1103
|
}
|
|
1050
1104
|
}
|
|
1051
1105
|
if (options.writable) {
|
|
1052
|
-
const sendFinish = (
|
|
1053
|
-
const preventClose = (
|
|
1106
|
+
const sendFinish = (_x = options.sendFinish) != null ? _x : true;
|
|
1107
|
+
const preventClose = (_y = options.preventClose) != null ? _y : false;
|
|
1054
1108
|
if (sendFinish || !preventClose) {
|
|
1055
1109
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1056
1110
|
}
|
|
@@ -1069,7 +1123,7 @@ var WorkflowAgent = class {
|
|
|
1069
1123
|
toolCall,
|
|
1070
1124
|
effectiveTools,
|
|
1071
1125
|
iterMessages,
|
|
1072
|
-
|
|
1126
|
+
toolsContext,
|
|
1073
1127
|
currentStepNumber
|
|
1074
1128
|
)
|
|
1075
1129
|
)
|
|
@@ -1150,7 +1204,7 @@ var WorkflowAgent = class {
|
|
|
1150
1204
|
}
|
|
1151
1205
|
}
|
|
1152
1206
|
const messages = finalMessages != null ? finalMessages : prompt.messages;
|
|
1153
|
-
const effectiveOutput = (
|
|
1207
|
+
const effectiveOutput = (_z = options.output) != null ? _z : this.output;
|
|
1154
1208
|
let experimentalOutput = void 0;
|
|
1155
1209
|
if (effectiveOutput && steps.length > 0) {
|
|
1156
1210
|
const lastStep = steps[steps.length - 1];
|
|
@@ -1177,17 +1231,18 @@ var WorkflowAgent = class {
|
|
|
1177
1231
|
await mergedOnFinish({
|
|
1178
1232
|
steps,
|
|
1179
1233
|
messages,
|
|
1180
|
-
text: (
|
|
1181
|
-
finishReason: (
|
|
1234
|
+
text: (_A = lastStep == null ? void 0 : lastStep.text) != null ? _A : "",
|
|
1235
|
+
finishReason: (_B = lastStep == null ? void 0 : lastStep.finishReason) != null ? _B : "other",
|
|
1182
1236
|
totalUsage: aggregateUsage(steps),
|
|
1183
|
-
|
|
1237
|
+
runtimeContext,
|
|
1238
|
+
toolsContext,
|
|
1184
1239
|
output: experimentalOutput
|
|
1185
1240
|
});
|
|
1186
1241
|
}
|
|
1187
1242
|
if (encounteredError) {
|
|
1188
1243
|
if (options.writable) {
|
|
1189
|
-
const sendFinish = (
|
|
1190
|
-
const preventClose = (
|
|
1244
|
+
const sendFinish = (_C = options.sendFinish) != null ? _C : true;
|
|
1245
|
+
const preventClose = (_D = options.preventClose) != null ? _D : false;
|
|
1191
1246
|
if (sendFinish || !preventClose) {
|
|
1192
1247
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1193
1248
|
}
|
|
@@ -1195,8 +1250,8 @@ var WorkflowAgent = class {
|
|
|
1195
1250
|
throw encounteredError;
|
|
1196
1251
|
}
|
|
1197
1252
|
if (options.writable) {
|
|
1198
|
-
const sendFinish = (
|
|
1199
|
-
const preventClose = (
|
|
1253
|
+
const sendFinish = (_E = options.sendFinish) != null ? _E : true;
|
|
1254
|
+
const preventClose = (_F = options.preventClose) != null ? _F : false;
|
|
1200
1255
|
if (sendFinish || !preventClose) {
|
|
1201
1256
|
await closeStream(options.writable, preventClose, sendFinish);
|
|
1202
1257
|
}
|
|
@@ -1283,6 +1338,22 @@ async function writeApprovalToolResults(writable, approvedResults, deniedResults
|
|
|
1283
1338
|
writer.releaseLock();
|
|
1284
1339
|
}
|
|
1285
1340
|
}
|
|
1341
|
+
async function resolveToolContext({
|
|
1342
|
+
toolName,
|
|
1343
|
+
tool: tool2,
|
|
1344
|
+
toolsContext
|
|
1345
|
+
}) {
|
|
1346
|
+
const contextSchema = tool2.contextSchema;
|
|
1347
|
+
const entry = toolsContext == null ? void 0 : toolsContext[toolName];
|
|
1348
|
+
if (contextSchema == null) {
|
|
1349
|
+
return entry;
|
|
1350
|
+
}
|
|
1351
|
+
return await validateTypes({
|
|
1352
|
+
value: entry,
|
|
1353
|
+
schema: contextSchema,
|
|
1354
|
+
context: { field: "tool context", entityName: toolName }
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1286
1357
|
function aggregateUsage(steps) {
|
|
1287
1358
|
var _a, _b, _c, _d;
|
|
1288
1359
|
let inputTokens = 0;
|
|
@@ -1351,7 +1422,12 @@ function createInvalidToolResult(toolCall) {
|
|
|
1351
1422
|
}
|
|
1352
1423
|
};
|
|
1353
1424
|
}
|
|
1354
|
-
|
|
1425
|
+
function getToolCallbackMessages(messages) {
|
|
1426
|
+
var _a;
|
|
1427
|
+
const withoutAssistantToolCall = ((_a = messages.at(-1)) == null ? void 0 : _a.role) === "assistant" ? messages.slice(0, -1) : messages;
|
|
1428
|
+
return withoutAssistantToolCall;
|
|
1429
|
+
}
|
|
1430
|
+
async function executeTool(toolCall, tools, messages, context) {
|
|
1355
1431
|
const tool2 = tools[toolCall.toolName];
|
|
1356
1432
|
if (!tool2) throw new Error(`Tool "${toolCall.toolName}" not found`);
|
|
1357
1433
|
if (typeof tool2.execute !== "function") {
|
|
@@ -1366,8 +1442,8 @@ async function executeTool(toolCall, tools, messages, experimentalContext) {
|
|
|
1366
1442
|
toolCallId: toolCall.toolCallId,
|
|
1367
1443
|
// Pass the conversation messages to the tool so it has context about the conversation
|
|
1368
1444
|
messages,
|
|
1369
|
-
// Pass context to the tool
|
|
1370
|
-
context
|
|
1445
|
+
// Pass per-tool context to the tool (resolved from `toolsContext`)
|
|
1446
|
+
context
|
|
1371
1447
|
});
|
|
1372
1448
|
const output = typeof toolResult === "string" ? { type: "text", value: toolResult } : { type: "json", value: toolResult };
|
|
1373
1449
|
return {
|