@ai-sdk/workflow 1.0.0-canary.71 → 1.0.0-canary.73
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 +18 -0
- package/dist/index.d.mts +26 -3
- package/dist/index.mjs +44 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/do-stream-step.ts +1 -1
- package/src/index.ts +1 -0
- package/src/providers/mock.ts +8 -8
- package/src/to-ui-message-chunk.ts +8 -8
- package/src/workflow-agent.ts +59 -17
- package/src/workflow-chat-transport.ts +15 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @ai-sdk/workflow
|
|
2
2
|
|
|
3
|
+
## 1.0.0-canary.73
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [023550e]
|
|
8
|
+
- Updated dependencies [e92fc45]
|
|
9
|
+
- ai@7.0.0-canary.156
|
|
10
|
+
|
|
11
|
+
## 1.0.0-canary.72
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [e67d80e]
|
|
16
|
+
- Updated dependencies [6cca112]
|
|
17
|
+
- Updated dependencies [82fc0ab]
|
|
18
|
+
- Updated dependencies [76fd58c]
|
|
19
|
+
- ai@7.0.0-canary.155
|
|
20
|
+
|
|
3
21
|
## 1.0.0-canary.71
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -362,6 +362,12 @@ type WorkflowAgentOptions<TTools extends ToolSet = ToolSet, TRuntimeContext exte
|
|
|
362
362
|
/**
|
|
363
363
|
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
364
364
|
*/
|
|
365
|
+
onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext>;
|
|
366
|
+
/**
|
|
367
|
+
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
368
|
+
*
|
|
369
|
+
* @deprecated Use `onEnd` instead.
|
|
370
|
+
*/
|
|
365
371
|
onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext>;
|
|
366
372
|
/**
|
|
367
373
|
* Callback called when the agent starts streaming, before any LLM calls.
|
|
@@ -389,7 +395,7 @@ type WorkflowAgentOptions<TTools extends ToolSet = ToolSet, TRuntimeContext exte
|
|
|
389
395
|
/**
|
|
390
396
|
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
391
397
|
*/
|
|
392
|
-
type
|
|
398
|
+
type WorkflowAgentOnEndCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = (event: {
|
|
393
399
|
/**
|
|
394
400
|
* Details for all steps.
|
|
395
401
|
*/
|
|
@@ -406,6 +412,10 @@ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeCon
|
|
|
406
412
|
* The finish reason from the last step.
|
|
407
413
|
*/
|
|
408
414
|
readonly finishReason: FinishReason;
|
|
415
|
+
/**
|
|
416
|
+
* The total token usage across all steps.
|
|
417
|
+
*/
|
|
418
|
+
readonly usage: LanguageModelUsage;
|
|
409
419
|
/**
|
|
410
420
|
* The total token usage across all steps.
|
|
411
421
|
*/
|
|
@@ -424,6 +434,12 @@ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeCon
|
|
|
424
434
|
*/
|
|
425
435
|
readonly output: OUTPUT;
|
|
426
436
|
}) => PromiseLike<void> | void;
|
|
437
|
+
/**
|
|
438
|
+
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
439
|
+
*
|
|
440
|
+
* @deprecated Use `WorkflowAgentOnEndCallback` instead.
|
|
441
|
+
*/
|
|
442
|
+
type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
|
|
427
443
|
/**
|
|
428
444
|
* Callback that is invoked when an error occurs during streaming.
|
|
429
445
|
*/
|
|
@@ -678,6 +694,13 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, TRuntimeContex
|
|
|
678
694
|
* Callback that is called when the LLM response and all request tool executions
|
|
679
695
|
* (for tools that have an `execute` function) are finished.
|
|
680
696
|
*/
|
|
697
|
+
onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
|
|
698
|
+
/**
|
|
699
|
+
* Callback that is called when the LLM response and all request tool executions
|
|
700
|
+
* (for tools that have an `execute` function) are finished.
|
|
701
|
+
*
|
|
702
|
+
* @deprecated Use `onEnd` instead.
|
|
703
|
+
*/
|
|
681
704
|
onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>;
|
|
682
705
|
/**
|
|
683
706
|
* Callback that is called when the operation is aborted.
|
|
@@ -856,7 +879,7 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet, TRuntimeContex
|
|
|
856
879
|
private experimentalDownload?;
|
|
857
880
|
private prepareStep?;
|
|
858
881
|
private constructorOnStepFinish?;
|
|
859
|
-
private
|
|
882
|
+
private constructorOnEnd?;
|
|
860
883
|
private constructorOnStart?;
|
|
861
884
|
private constructorOnStepStart?;
|
|
862
885
|
private constructorOnToolExecutionStart?;
|
|
@@ -1033,4 +1056,4 @@ declare class WorkflowChatTransport<UI_MESSAGE extends UIMessage> implements Cha
|
|
|
1033
1056
|
private onFinish;
|
|
1034
1057
|
}
|
|
1035
1058
|
|
|
1036
|
-
export { type CompatibleLanguageModel, type DownloadFunction, type GenerationSettings, type InferWorkflowAgentTools, type InferWorkflowAgentUIMessage, type OutputSpecification, type PrepareCallCallback, type PrepareCallOptions, type PrepareCallResult, type PrepareStepCallback, type PrepareStepInfo, type PrepareStepResult, type ProviderOptions, type ReconnectToStreamOptions, type SendMessagesOptions, type StreamTextTransform, type TelemetryOptions, WorkflowAgent, type WorkflowAgentOnAbortCallback, type WorkflowAgentOnErrorCallback, type WorkflowAgentOnFinishCallback, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepFinishCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolExecutionEndCallback, type WorkflowAgentOnToolExecutionStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };
|
|
1059
|
+
export { type CompatibleLanguageModel, type DownloadFunction, type GenerationSettings, type InferWorkflowAgentTools, type InferWorkflowAgentUIMessage, type OutputSpecification, type PrepareCallCallback, type PrepareCallOptions, type PrepareCallResult, type PrepareStepCallback, type PrepareStepInfo, type PrepareStepResult, type ProviderOptions, type ReconnectToStreamOptions, type SendMessagesOptions, type StreamTextTransform, type TelemetryOptions, WorkflowAgent, type WorkflowAgentOnAbortCallback, type WorkflowAgentOnEndCallback, type WorkflowAgentOnErrorCallback, type WorkflowAgentOnFinishCallback, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepFinishCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolExecutionEndCallback, type WorkflowAgentOnToolExecutionStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };
|
package/dist/index.mjs
CHANGED
|
@@ -269,7 +269,7 @@ async function doStreamStep(conversationPrompt, modelInit, writable, serializedT
|
|
|
269
269
|
stepTimeMs: 0,
|
|
270
270
|
responseTimeMs: 0,
|
|
271
271
|
toolExecutionMs: {},
|
|
272
|
-
|
|
272
|
+
timeToFirstOutputMs: void 0
|
|
273
273
|
},
|
|
274
274
|
warnings,
|
|
275
275
|
request: {
|
|
@@ -663,7 +663,8 @@ var WorkflowAgent = class {
|
|
|
663
663
|
this.experimentalDownload = options.experimental_download;
|
|
664
664
|
this.prepareStep = options.prepareStep;
|
|
665
665
|
this.constructorOnStepFinish = options.onStepFinish;
|
|
666
|
-
|
|
666
|
+
const { onFinish, onEnd = onFinish } = options;
|
|
667
|
+
this.constructorOnEnd = onEnd;
|
|
667
668
|
this.constructorOnStart = options.experimental_onStart;
|
|
668
669
|
this.constructorOnStepStart = options.experimental_onStepStart;
|
|
669
670
|
this.constructorOnToolExecutionStart = options.onToolExecutionStart;
|
|
@@ -689,6 +690,7 @@ var WorkflowAgent = class {
|
|
|
689
690
|
}
|
|
690
691
|
async stream(options) {
|
|
691
692
|
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, _G, _H, _I, _J, _K, _L, _M, _N;
|
|
693
|
+
const { onFinish, onEnd = onFinish } = options;
|
|
692
694
|
let effectiveModel = this.model;
|
|
693
695
|
let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
|
|
694
696
|
let effectivePrompt = options.prompt;
|
|
@@ -941,9 +943,9 @@ var WorkflowAgent = class {
|
|
|
941
943
|
this.constructorOnStepFinish,
|
|
942
944
|
options.onStepFinish
|
|
943
945
|
);
|
|
944
|
-
const
|
|
945
|
-
this.
|
|
946
|
-
|
|
946
|
+
const mergedOnEnd = mergeCallbacks(
|
|
947
|
+
this.constructorOnEnd,
|
|
948
|
+
onEnd
|
|
947
949
|
);
|
|
948
950
|
const mergedOnStart = mergeCallbacks(
|
|
949
951
|
this.constructorOnStart,
|
|
@@ -1306,14 +1308,16 @@ var WorkflowAgent = class {
|
|
|
1306
1308
|
});
|
|
1307
1309
|
}
|
|
1308
1310
|
const messages2 = iterMessages;
|
|
1309
|
-
if (
|
|
1311
|
+
if (mergedOnEnd && !wasAborted) {
|
|
1310
1312
|
const lastStep = steps[steps.length - 1];
|
|
1311
|
-
|
|
1313
|
+
const totalUsage = aggregateUsage(steps);
|
|
1314
|
+
await mergedOnEnd({
|
|
1312
1315
|
steps,
|
|
1313
1316
|
messages: messages2,
|
|
1314
1317
|
text: (_A = lastStep == null ? void 0 : lastStep.text) != null ? _A : "",
|
|
1315
1318
|
finishReason: (_B = lastStep == null ? void 0 : lastStep.finishReason) != null ? _B : "other",
|
|
1316
|
-
|
|
1319
|
+
usage: totalUsage,
|
|
1320
|
+
totalUsage,
|
|
1317
1321
|
runtimeContext,
|
|
1318
1322
|
toolsContext,
|
|
1319
1323
|
output: void 0
|
|
@@ -1322,10 +1326,12 @@ var WorkflowAgent = class {
|
|
|
1322
1326
|
if (!wasAborted && steps.length > 0) {
|
|
1323
1327
|
const telemetrySteps = steps.map(normalizeStepForTelemetry2);
|
|
1324
1328
|
const lastStep = telemetrySteps[telemetrySteps.length - 1];
|
|
1329
|
+
const totalUsage = aggregateUsage(steps);
|
|
1325
1330
|
await ((_C = telemetryDispatcher.onEnd) == null ? void 0 : _C.call(telemetryDispatcher, {
|
|
1326
1331
|
...lastStep,
|
|
1327
1332
|
steps: telemetrySteps,
|
|
1328
|
-
|
|
1333
|
+
usage: totalUsage,
|
|
1334
|
+
totalUsage
|
|
1329
1335
|
}));
|
|
1330
1336
|
}
|
|
1331
1337
|
if (options.writable) {
|
|
@@ -1480,14 +1486,16 @@ var WorkflowAgent = class {
|
|
|
1480
1486
|
}
|
|
1481
1487
|
}
|
|
1482
1488
|
}
|
|
1483
|
-
if (
|
|
1489
|
+
if (mergedOnEnd && !wasAborted) {
|
|
1484
1490
|
const lastStep = steps[steps.length - 1];
|
|
1485
|
-
|
|
1491
|
+
const totalUsage = aggregateUsage(steps);
|
|
1492
|
+
await mergedOnEnd({
|
|
1486
1493
|
steps,
|
|
1487
1494
|
messages,
|
|
1488
1495
|
text: (_H = lastStep == null ? void 0 : lastStep.text) != null ? _H : "",
|
|
1489
1496
|
finishReason: (_I = lastStep == null ? void 0 : lastStep.finishReason) != null ? _I : "other",
|
|
1490
|
-
|
|
1497
|
+
usage: totalUsage,
|
|
1498
|
+
totalUsage,
|
|
1491
1499
|
runtimeContext,
|
|
1492
1500
|
toolsContext,
|
|
1493
1501
|
output: experimentalOutput
|
|
@@ -1496,10 +1504,12 @@ var WorkflowAgent = class {
|
|
|
1496
1504
|
if (!wasAborted && steps.length > 0) {
|
|
1497
1505
|
const telemetrySteps = steps.map(normalizeStepForTelemetry2);
|
|
1498
1506
|
const lastStep = telemetrySteps[telemetrySteps.length - 1];
|
|
1507
|
+
const totalUsage = aggregateUsage(steps);
|
|
1499
1508
|
await ((_J = telemetryDispatcher.onEnd) == null ? void 0 : _J.call(telemetryDispatcher, {
|
|
1500
1509
|
...lastStep,
|
|
1501
1510
|
steps: telemetrySteps,
|
|
1502
|
-
|
|
1511
|
+
usage: totalUsage,
|
|
1512
|
+
totalUsage
|
|
1503
1513
|
}));
|
|
1504
1514
|
}
|
|
1505
1515
|
if (encounteredError) {
|
|
@@ -1935,16 +1945,16 @@ function toUIMessageChunk(part) {
|
|
|
1935
1945
|
case "raw":
|
|
1936
1946
|
return void 0;
|
|
1937
1947
|
default: {
|
|
1938
|
-
const
|
|
1939
|
-
if (
|
|
1948
|
+
const passthroughPart = part;
|
|
1949
|
+
if (passthroughPart.type === "tool-approval-request") {
|
|
1940
1950
|
return {
|
|
1941
1951
|
type: "tool-approval-request",
|
|
1942
|
-
approvalId:
|
|
1943
|
-
toolCallId:
|
|
1952
|
+
approvalId: passthroughPart.approvalId,
|
|
1953
|
+
toolCallId: passthroughPart.toolCallId
|
|
1944
1954
|
};
|
|
1945
1955
|
}
|
|
1946
|
-
if (
|
|
1947
|
-
return
|
|
1956
|
+
if (passthroughPart.type === "finish-step" || passthroughPart.type === "start-step" || passthroughPart.type === "tool-output-denied") {
|
|
1957
|
+
return passthroughPart;
|
|
1948
1958
|
}
|
|
1949
1959
|
return void 0;
|
|
1950
1960
|
}
|
|
@@ -2043,7 +2053,7 @@ var WorkflowChatTransport = class {
|
|
|
2043
2053
|
messageId
|
|
2044
2054
|
}) : void 0;
|
|
2045
2055
|
const url = (_a = requestConfig == null ? void 0 : requestConfig.api) != null ? _a : this.api;
|
|
2046
|
-
const
|
|
2056
|
+
const response = await this.fetch(url, {
|
|
2047
2057
|
method: "POST",
|
|
2048
2058
|
body: JSON.stringify(
|
|
2049
2059
|
(_b = requestConfig == null ? void 0 : requestConfig.body) != null ? _b : { messages, ...options.body }
|
|
@@ -2052,21 +2062,21 @@ var WorkflowChatTransport = class {
|
|
|
2052
2062
|
credentials: requestConfig == null ? void 0 : requestConfig.credentials,
|
|
2053
2063
|
signal: abortSignal
|
|
2054
2064
|
});
|
|
2055
|
-
if (!
|
|
2065
|
+
if (!response.ok || !response.body) {
|
|
2056
2066
|
throw new Error(
|
|
2057
|
-
`Failed to fetch chat: ${
|
|
2067
|
+
`Failed to fetch chat: ${response.status} ${await response.text()}`
|
|
2058
2068
|
);
|
|
2059
2069
|
}
|
|
2060
|
-
const workflowRunId =
|
|
2070
|
+
const workflowRunId = response.headers.get("x-workflow-run-id");
|
|
2061
2071
|
if (!workflowRunId) {
|
|
2062
2072
|
throw new Error(
|
|
2063
2073
|
'Workflow run ID not found in "x-workflow-run-id" response header'
|
|
2064
2074
|
);
|
|
2065
2075
|
}
|
|
2066
|
-
await ((_c = this.onChatSendMessage) == null ? void 0 : _c.call(this,
|
|
2076
|
+
await ((_c = this.onChatSendMessage) == null ? void 0 : _c.call(this, response, options));
|
|
2067
2077
|
try {
|
|
2068
2078
|
const chunkStream = parseJsonEventStream({
|
|
2069
|
-
stream:
|
|
2079
|
+
stream: response.body,
|
|
2070
2080
|
schema: uiMessageChunkSchema
|
|
2071
2081
|
});
|
|
2072
2082
|
for await (const chunk of createAsyncIterableStream(chunkStream)) {
|
|
@@ -2101,8 +2111,8 @@ var WorkflowChatTransport = class {
|
|
|
2101
2111
|
* @throws Error if the reconnection request fails or returns a non-OK status
|
|
2102
2112
|
*/
|
|
2103
2113
|
async reconnectToStream(options) {
|
|
2104
|
-
const
|
|
2105
|
-
return convertAsyncIteratorToReadableStream(
|
|
2114
|
+
const reconnectIterator = this.reconnectToStreamIterator(options);
|
|
2115
|
+
return convertAsyncIteratorToReadableStream(reconnectIterator);
|
|
2106
2116
|
}
|
|
2107
2117
|
async *reconnectToStreamIterator(options, workflowRunId, initialChunkIndex = 0) {
|
|
2108
2118
|
var _a, _b;
|
|
@@ -2125,20 +2135,22 @@ var WorkflowChatTransport = class {
|
|
|
2125
2135
|
while (!gotFinish) {
|
|
2126
2136
|
const startIndex = useExplicitStartIndex ? explicitStartIndex : replayFromStart ? 0 : chunkIndex;
|
|
2127
2137
|
const url = `${baseUrl}?startIndex=${startIndex}`;
|
|
2128
|
-
const
|
|
2138
|
+
const response = await this.fetch(url, {
|
|
2129
2139
|
headers: requestConfig == null ? void 0 : requestConfig.headers,
|
|
2130
2140
|
credentials: requestConfig == null ? void 0 : requestConfig.credentials,
|
|
2131
2141
|
signal: options.abortSignal
|
|
2132
2142
|
});
|
|
2133
|
-
if (!
|
|
2143
|
+
if (!response.ok || !response.body) {
|
|
2134
2144
|
throw new Error(
|
|
2135
|
-
`Failed to fetch chat: ${
|
|
2145
|
+
`Failed to fetch chat: ${response.status} ${await response.text()}`
|
|
2136
2146
|
);
|
|
2137
2147
|
}
|
|
2138
2148
|
if (useExplicitStartIndex && explicitStartIndex > 0) {
|
|
2139
2149
|
chunkIndex = explicitStartIndex;
|
|
2140
2150
|
} else if (useExplicitStartIndex && explicitStartIndex < 0) {
|
|
2141
|
-
const tailIndexHeader =
|
|
2151
|
+
const tailIndexHeader = response.headers.get(
|
|
2152
|
+
"x-workflow-stream-tail-index"
|
|
2153
|
+
);
|
|
2142
2154
|
const tailIndex = tailIndexHeader !== null ? parseInt(tailIndexHeader, 10) : NaN;
|
|
2143
2155
|
if (!Number.isNaN(tailIndex)) {
|
|
2144
2156
|
chunkIndex = Math.max(0, tailIndex + 1 + explicitStartIndex);
|
|
@@ -2152,7 +2164,7 @@ var WorkflowChatTransport = class {
|
|
|
2152
2164
|
useExplicitStartIndex = false;
|
|
2153
2165
|
try {
|
|
2154
2166
|
const chunkStream = parseJsonEventStream({
|
|
2155
|
-
stream:
|
|
2167
|
+
stream: response.body,
|
|
2156
2168
|
schema: uiMessageChunkSchema
|
|
2157
2169
|
});
|
|
2158
2170
|
for await (const chunk of createAsyncIterableStream(chunkStream)) {
|