@ai-sdk/workflow 2.0.58 → 2.0.60
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 +27 -0
- package/dist/index.d.ts +61 -43
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +686 -305
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/build-model-step-result.ts +329 -0
- package/src/do-generate-step.ts +212 -0
- package/src/do-stream-step.ts +50 -179
- package/src/index.ts +3 -0
- package/src/{stream-text-iterator.ts → model-call-iterator.ts} +117 -297
- package/src/model-call.ts +196 -0
- package/src/providers/mock-function-wrapper.ts +32 -0
- package/src/test/agent-e2e-workflows.ts +60 -1
- package/src/test/approval-workflows.ts +105 -0
- package/src/test/generation-boundary-model.ts +115 -0
- package/src/test/generation-boundary-workflows.ts +106 -0
- package/src/test/model-call-payload-workflow.ts +132 -0
- package/src/to-model-response-messages.ts +28 -0
- package/src/workflow-agent.ts +521 -206
- package/src/workflow-execution-result.ts +55 -0
package/src/do-stream-step.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
JSONObject,
|
|
3
|
-
LanguageModelV4CallOptions,
|
|
4
2
|
LanguageModelV4Prompt,
|
|
5
|
-
LanguageModelV4Source,
|
|
6
3
|
LanguageModelV4StreamPart,
|
|
7
4
|
SharedV4ProviderMetadata,
|
|
8
5
|
} from '@ai-sdk/provider';
|
|
@@ -11,173 +8,41 @@ import {
|
|
|
11
8
|
experimental_streamLanguageModelCall as streamModelCall,
|
|
12
9
|
gateway,
|
|
13
10
|
wrapLanguageModel,
|
|
14
|
-
type Experimental_LanguageModelStreamPart,
|
|
15
|
-
type FinishReason,
|
|
16
11
|
type LanguageModel,
|
|
17
|
-
type LanguageModelUsage,
|
|
18
12
|
type ModelMessage,
|
|
19
|
-
type StopCondition,
|
|
20
|
-
type ToolCallRepairFunction,
|
|
21
|
-
type ToolChoice,
|
|
22
13
|
type ToolSet,
|
|
23
14
|
} from 'ai';
|
|
24
15
|
import { prepareRetries } from 'ai/internal';
|
|
25
|
-
import type {
|
|
16
|
+
import type { StreamTextTransform } from './workflow-agent.js';
|
|
26
17
|
import {
|
|
27
18
|
resolveSerializableTools,
|
|
28
19
|
type SerializableToolDef,
|
|
29
20
|
} from './serializable-schema.js';
|
|
30
21
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Options for the doStreamStep function.
|
|
57
|
-
*/
|
|
58
|
-
export interface DoStreamStepOptions {
|
|
59
|
-
maxOutputTokens?: number;
|
|
60
|
-
temperature?: number;
|
|
61
|
-
topP?: number;
|
|
62
|
-
topK?: number;
|
|
63
|
-
presencePenalty?: number;
|
|
64
|
-
frequencyPenalty?: number;
|
|
65
|
-
stopSequences?: string[];
|
|
66
|
-
seed?: number;
|
|
67
|
-
maxRetries?: number;
|
|
68
|
-
abortSignal?: AbortSignal;
|
|
69
|
-
timeoutAt?: number;
|
|
70
|
-
headers?: Record<string, string | undefined>;
|
|
71
|
-
reasoning?: LanguageModelV4CallOptions['reasoning'];
|
|
72
|
-
providerOptions?: ProviderOptions;
|
|
73
|
-
toolChoice?: ToolChoice<ToolSet>;
|
|
74
|
-
includeRawChunks?: boolean;
|
|
75
|
-
repairToolCall?: ToolCallRepairFunction<ToolSet>;
|
|
76
|
-
responseFormat?: LanguageModelV4CallOptions['responseFormat'];
|
|
77
|
-
experimental_transform?:
|
|
78
|
-
| StreamTextTransform<ToolSet>
|
|
79
|
-
| Array<StreamTextTransform<ToolSet>>;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Parsed tool call from the stream (parsed by streamModelCall's transform).
|
|
84
|
-
*/
|
|
85
|
-
export interface ParsedToolCall {
|
|
86
|
-
type: 'tool-call';
|
|
87
|
-
toolCallId: string;
|
|
88
|
-
toolName: string;
|
|
89
|
-
input: unknown;
|
|
90
|
-
providerExecuted?: boolean;
|
|
91
|
-
providerMetadata?: SharedV4ProviderMetadata;
|
|
92
|
-
title?: string;
|
|
93
|
-
toolMetadata?: JSONObject;
|
|
94
|
-
dynamic?: boolean;
|
|
95
|
-
invalid?: boolean;
|
|
96
|
-
error?: unknown;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Finish metadata from the stream.
|
|
101
|
-
*/
|
|
102
|
-
export interface StreamFinish {
|
|
103
|
-
finishReason: FinishReason;
|
|
104
|
-
rawFinishReason: string | undefined;
|
|
105
|
-
usage: LanguageModelUsage;
|
|
106
|
-
providerMetadata?: Record<string, unknown>;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export type DoStreamStepRawContentPart =
|
|
110
|
-
| {
|
|
111
|
-
type: 'text';
|
|
112
|
-
text: string;
|
|
113
|
-
providerMetadata?: SharedV4ProviderMetadata;
|
|
114
|
-
}
|
|
115
|
-
| {
|
|
116
|
-
type: 'reasoning';
|
|
117
|
-
reasoningIndex: number;
|
|
118
|
-
}
|
|
119
|
-
| {
|
|
120
|
-
type: 'file';
|
|
121
|
-
data: string;
|
|
122
|
-
mediaType: string;
|
|
123
|
-
providerMetadata?: SharedV4ProviderMetadata;
|
|
124
|
-
}
|
|
125
|
-
| LanguageModelV4Source
|
|
126
|
-
| {
|
|
127
|
-
type: 'tool-call';
|
|
128
|
-
toolCallIndex: number;
|
|
129
|
-
}
|
|
130
|
-
| {
|
|
131
|
-
type: 'provider-tool-result';
|
|
132
|
-
toolCallId: string;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Compact callback replay data. The start event establishes the tool name for
|
|
137
|
-
* a call, so delta events do not repeat it and available events reuse the
|
|
138
|
-
* parsed input already present in `toolCalls`.
|
|
139
|
-
*/
|
|
140
|
-
export type ToolInputLifecycleEvent =
|
|
141
|
-
| ['start', toolCallId: string, toolName: string]
|
|
142
|
-
| ['delta', toolCallId: string, inputTextDelta: string]
|
|
143
|
-
| ['available', toolCallId: string];
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Minimal aggregates needed to reconstruct a `StepResult` outside the step
|
|
147
|
-
* boundary. By returning only these fields (instead of a fully-populated
|
|
148
|
-
* StepResult plus the raw `chunks[]` array), the durable event log doesn't
|
|
149
|
-
* carry StepResult's redundant derived fields — duplicate tool-call lists,
|
|
150
|
-
* `text`, `files`, `sources`, `reasoningText`, or the tool-result arrays
|
|
151
|
-
* populated after execution. It also avoids the per-chunk `chunks[]` snapshot
|
|
152
|
-
* the iterator never reads. The caller reconstructs the full StepResult via
|
|
153
|
-
* `buildStepResult`.
|
|
154
|
-
*/
|
|
155
|
-
export interface DoStreamStepRawResult {
|
|
156
|
-
content: DoStreamStepRawContentPart[];
|
|
157
|
-
reasoning: Array<{
|
|
158
|
-
text: string;
|
|
159
|
-
providerMetadata?: SharedV4ProviderMetadata;
|
|
160
|
-
}>;
|
|
161
|
-
responseMetadata?: { id?: string; timestamp?: Date; modelId?: string };
|
|
162
|
-
warnings?: unknown[];
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export type DoStreamStepResult =
|
|
166
|
-
| { aborted: true }
|
|
167
|
-
| {
|
|
168
|
-
aborted?: false;
|
|
169
|
-
toolCalls: ParsedToolCall[];
|
|
170
|
-
finish: StreamFinish | undefined;
|
|
171
|
-
raw: DoStreamStepRawResult;
|
|
172
|
-
providerExecutedToolResults: Map<string, ProviderExecutedToolResult>;
|
|
173
|
-
/**
|
|
174
|
-
* Optional for compatibility with model-step results persisted before
|
|
175
|
-
* tool input lifecycle callback replay was added.
|
|
176
|
-
*/
|
|
177
|
-
toolInputLifecycleEvents?: ToolInputLifecycleEvent[];
|
|
178
|
-
/** Present when the model stream emitted an error part. */
|
|
179
|
-
terminalError?: unknown;
|
|
180
|
-
};
|
|
22
|
+
import type {
|
|
23
|
+
ModelCallFinish as StreamFinish,
|
|
24
|
+
ModelCallOptions as DoStreamStepOptions,
|
|
25
|
+
ModelCallRawContentPart as DoStreamStepRawContentPart,
|
|
26
|
+
ModelCallResult as DoStreamStepResult,
|
|
27
|
+
ModelCallStreamPart,
|
|
28
|
+
ParsedToolCall,
|
|
29
|
+
ProviderExecutedToolResult,
|
|
30
|
+
ToolInputLifecycleEvent,
|
|
31
|
+
} from './model-call.js';
|
|
32
|
+
|
|
33
|
+
// Preserve existing imports while the durable step keeps its name and payload.
|
|
34
|
+
export type {
|
|
35
|
+
ModelCallFinish as StreamFinish,
|
|
36
|
+
ModelCallOptions as DoStreamStepOptions,
|
|
37
|
+
ModelCallRawContentPart as DoStreamStepRawContentPart,
|
|
38
|
+
ModelCallRawResult as DoStreamStepRawResult,
|
|
39
|
+
ModelCallResult as DoStreamStepResult,
|
|
40
|
+
ModelCallStreamPart,
|
|
41
|
+
ModelStopCondition,
|
|
42
|
+
ParsedToolCall,
|
|
43
|
+
ProviderExecutedToolResult,
|
|
44
|
+
ToolInputLifecycleEvent,
|
|
45
|
+
} from './model-call.js';
|
|
181
46
|
|
|
182
47
|
export async function doStreamStep(
|
|
183
48
|
conversationPrompt: LanguageModelV4Prompt,
|
|
@@ -277,7 +142,21 @@ export async function doStreamStep(
|
|
|
277
142
|
// streamModelCall expects Prompt (ModelMessage[]) but we pass the
|
|
278
143
|
// pre-converted LanguageModelV4Prompt. standardizePrompt inside
|
|
279
144
|
// streamModelCall handles both formats.
|
|
280
|
-
messages: conversationPrompt
|
|
145
|
+
messages: conversationPrompt.map(message =>
|
|
146
|
+
message.role !== 'tool'
|
|
147
|
+
? message
|
|
148
|
+
: {
|
|
149
|
+
...message,
|
|
150
|
+
// Provider prompt approval responses have already been filtered by
|
|
151
|
+
// convertToLanguageModelPrompt. Restore the marker expected by the
|
|
152
|
+
// model-call helper when it converts these messages again.
|
|
153
|
+
content: message.content.map(part =>
|
|
154
|
+
part.type === 'tool-approval-response'
|
|
155
|
+
? { ...part, providerExecuted: true }
|
|
156
|
+
: part,
|
|
157
|
+
),
|
|
158
|
+
},
|
|
159
|
+
) as unknown as ModelMessage[],
|
|
281
160
|
allowSystemInMessages: true,
|
|
282
161
|
tools,
|
|
283
162
|
toolChoice: options?.toolChoice,
|
|
@@ -436,6 +315,13 @@ export async function doStreamStep(
|
|
|
436
315
|
case 'source':
|
|
437
316
|
content.push(part);
|
|
438
317
|
break;
|
|
318
|
+
case 'tool-approval-request':
|
|
319
|
+
content.push({
|
|
320
|
+
type: 'tool-approval-request',
|
|
321
|
+
approvalId: part.approvalId,
|
|
322
|
+
toolCallId: part.toolCall.toolCallId,
|
|
323
|
+
});
|
|
324
|
+
break;
|
|
439
325
|
case 'tool-call': {
|
|
440
326
|
// parseToolCall adds dynamic/invalid/error at runtime
|
|
441
327
|
const toolCallPart = part as typeof part & Partial<ParsedToolCall>;
|
|
@@ -595,10 +481,7 @@ function applyStreamTransforms({
|
|
|
595
481
|
let stopped = false;
|
|
596
482
|
|
|
597
483
|
const stopStream = () => {
|
|
598
|
-
if (stopped)
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
|
-
|
|
484
|
+
if (stopped) return;
|
|
602
485
|
stopped = true;
|
|
603
486
|
void sourceReader.cancel().catch(() => {});
|
|
604
487
|
};
|
|
@@ -610,14 +493,11 @@ function applyStreamTransforms({
|
|
|
610
493
|
controller.close();
|
|
611
494
|
return;
|
|
612
495
|
}
|
|
613
|
-
|
|
614
496
|
const { done, value } = await sourceReader.read();
|
|
615
|
-
|
|
616
497
|
if (done || stopped) {
|
|
617
498
|
controller.close();
|
|
618
499
|
return;
|
|
619
500
|
}
|
|
620
|
-
|
|
621
501
|
controller.enqueue(value);
|
|
622
502
|
},
|
|
623
503
|
cancel(reason) {
|
|
@@ -633,7 +513,6 @@ function applyStreamTransforms({
|
|
|
633
513
|
transform({ tools, stopStream }),
|
|
634
514
|
);
|
|
635
515
|
}
|
|
636
|
-
|
|
637
516
|
return transformedStream;
|
|
638
517
|
}
|
|
639
518
|
|
|
@@ -696,7 +575,6 @@ function upsertReasoningContentPart({
|
|
|
696
575
|
providerMetadata?: SharedV4ProviderMetadata;
|
|
697
576
|
}) {
|
|
698
577
|
let partIndex = reasoningPartIndexes.get(id);
|
|
699
|
-
|
|
700
578
|
if (partIndex == null) {
|
|
701
579
|
partIndex =
|
|
702
580
|
reasoningParts.push({
|
|
@@ -706,14 +584,7 @@ function upsertReasoningContentPart({
|
|
|
706
584
|
reasoningPartIndexes.set(id, partIndex);
|
|
707
585
|
content.push({ type: 'reasoning', reasoningIndex: partIndex });
|
|
708
586
|
}
|
|
709
|
-
|
|
710
587
|
const part = reasoningParts[partIndex];
|
|
711
|
-
|
|
712
|
-
if (
|
|
713
|
-
part.text += textDelta;
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
if (providerMetadata != null) {
|
|
717
|
-
part.providerMetadata = providerMetadata;
|
|
718
|
-
}
|
|
588
|
+
if (textDelta != null) part.text += textDelta;
|
|
589
|
+
if (providerMetadata != null) part.providerMetadata = providerMetadata;
|
|
719
590
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,9 @@ export {
|
|
|
5
5
|
type DownloadFunction,
|
|
6
6
|
type WorkflowAgentOptions,
|
|
7
7
|
type WorkflowAgentStreamOptions,
|
|
8
|
+
type WorkflowAgentCallOptions,
|
|
9
|
+
type WorkflowAgentGenerateOptions,
|
|
10
|
+
type WorkflowAgentGenerateResult,
|
|
8
11
|
type WorkflowAgentStreamResult,
|
|
9
12
|
type WorkflowToolApprovalSecret,
|
|
10
13
|
type GenerationSettings,
|