@ai-sdk/workflow 1.0.0-beta.1 → 1.0.0-beta.10
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 +63 -0
- package/dist/index.d.mts +124 -28
- package/dist/index.mjs +81 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/do-stream-step.ts +6 -22
- package/src/index.ts +4 -3
- package/src/providers/mock.ts +79 -92
- package/src/stream-text-iterator.ts +8 -13
- package/src/workflow-agent.ts +427 -298
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/workflow",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"description": "WorkflowAgent for building AI agents with AI SDK",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"ajv": "^8.18.0",
|
|
30
30
|
"@ai-sdk/provider": "4.0.0-beta.12",
|
|
31
31
|
"@ai-sdk/provider-utils": "5.0.0-beta.20",
|
|
32
|
-
"ai": "7.0.0-beta.
|
|
32
|
+
"ai": "7.0.0-beta.96"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "20.17.24",
|
package/src/do-stream-step.ts
CHANGED
|
@@ -21,7 +21,6 @@ import {
|
|
|
21
21
|
resolveSerializableTools,
|
|
22
22
|
type SerializableToolDef,
|
|
23
23
|
} from './serializable-schema.js';
|
|
24
|
-
import type { CompatibleLanguageModel } from './types.js';
|
|
25
24
|
|
|
26
25
|
export type { Experimental_LanguageModelStreamPart as ModelCallStreamPart } from 'ai';
|
|
27
26
|
|
|
@@ -87,10 +86,7 @@ export interface StreamFinish {
|
|
|
87
86
|
|
|
88
87
|
export async function doStreamStep(
|
|
89
88
|
conversationPrompt: LanguageModelV4Prompt,
|
|
90
|
-
modelInit:
|
|
91
|
-
| string
|
|
92
|
-
| CompatibleLanguageModel
|
|
93
|
-
| (() => Promise<CompatibleLanguageModel>),
|
|
89
|
+
modelInit: LanguageModel,
|
|
94
90
|
writable?: WritableStream<ModelCallStreamPart<ToolSet>>,
|
|
95
91
|
serializedTools?: Record<string, SerializableToolDef>,
|
|
96
92
|
options?: DoStreamStepOptions,
|
|
@@ -98,22 +94,10 @@ export async function doStreamStep(
|
|
|
98
94
|
'use step';
|
|
99
95
|
|
|
100
96
|
// Resolve model inside step (must happen here for serialization boundary)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
model = await modelInit();
|
|
106
|
-
} else if (
|
|
107
|
-
typeof modelInit === 'object' &&
|
|
108
|
-
modelInit !== null &&
|
|
109
|
-
'modelId' in modelInit
|
|
110
|
-
) {
|
|
111
|
-
model = modelInit;
|
|
112
|
-
} else {
|
|
113
|
-
throw new Error(
|
|
114
|
-
'Invalid "model initialization" argument. Must be a string, a LanguageModel instance, or a function that returns a LanguageModel instance.',
|
|
115
|
-
);
|
|
116
|
-
}
|
|
97
|
+
const model: LanguageModel =
|
|
98
|
+
typeof modelInit === 'string'
|
|
99
|
+
? gateway.languageModel(modelInit)
|
|
100
|
+
: modelInit;
|
|
117
101
|
|
|
118
102
|
// Reconstruct tools from serializable definitions with Ajv validation.
|
|
119
103
|
// Tools are serialized before crossing the step boundary because zod schemas
|
|
@@ -126,7 +110,7 @@ export async function doStreamStep(
|
|
|
126
110
|
// model.doStream(), retry logic, and stream part transformation
|
|
127
111
|
// (tool call parsing, finish reason mapping, file wrapping).
|
|
128
112
|
const { stream: modelStream } = await streamModelCall({
|
|
129
|
-
model
|
|
113
|
+
model,
|
|
130
114
|
// streamModelCall expects Prompt (ModelMessage[]) but we pass the
|
|
131
115
|
// pre-converted LanguageModelV4Prompt. standardizePrompt inside
|
|
132
116
|
// streamModelCall handles both formats.
|
package/src/index.ts
CHANGED
|
@@ -17,9 +17,10 @@ export {
|
|
|
17
17
|
type PrepareStepInfo,
|
|
18
18
|
type PrepareStepResult,
|
|
19
19
|
type ProviderOptions,
|
|
20
|
-
type
|
|
21
|
-
type
|
|
22
|
-
type
|
|
20
|
+
type WorkflowAgentOnAbortCallback,
|
|
21
|
+
type WorkflowAgentOnErrorCallback,
|
|
22
|
+
type WorkflowAgentOnFinishCallback,
|
|
23
|
+
type WorkflowAgentOnStepFinishCallback,
|
|
23
24
|
type StreamTextTransform,
|
|
24
25
|
type TelemetrySettings,
|
|
25
26
|
type ToolCallRepairFunction,
|
package/src/providers/mock.ts
CHANGED
|
@@ -6,19 +6,55 @@ export type MockResponseDescriptor =
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Mock model that returns a fixed text response.
|
|
9
|
-
* Same 'use step' pattern as real providers (anthropic, openai, etc.).
|
|
10
|
-
* Only captures `text` (string) — fully serializable across step boundary.
|
|
11
9
|
*/
|
|
12
10
|
export function mockTextModel(text: string) {
|
|
13
|
-
return
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
return mockProvider({
|
|
12
|
+
doStream: async () => ({
|
|
13
|
+
stream: new ReadableStream({
|
|
14
|
+
start(c) {
|
|
15
|
+
for (const v of [
|
|
16
|
+
{ type: 'stream-start', warnings: [] },
|
|
17
|
+
{
|
|
18
|
+
type: 'response-metadata',
|
|
19
|
+
id: 'r',
|
|
20
|
+
modelId: 'mock',
|
|
21
|
+
timestamp: new Date(),
|
|
22
|
+
},
|
|
23
|
+
{ type: 'text-start', id: '1' },
|
|
24
|
+
{ type: 'text-delta', id: '1', delta: text },
|
|
25
|
+
{ type: 'text-end', id: '1' },
|
|
26
|
+
{
|
|
27
|
+
type: 'finish',
|
|
28
|
+
finishReason: { unified: 'stop', raw: 'stop' },
|
|
29
|
+
usage: {
|
|
30
|
+
inputTokens: { total: 5, noCache: 5 },
|
|
31
|
+
outputTokens: { total: 10, text: 10 },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
] as any[])
|
|
35
|
+
c.enqueue(v);
|
|
36
|
+
c.close();
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Mock model that plays through a sequence of responses.
|
|
45
|
+
* Determines which response to return by counting assistant messages in the prompt.
|
|
46
|
+
*/
|
|
47
|
+
export function mockSequenceModel(responses: MockResponseDescriptor[]) {
|
|
48
|
+
return mockProvider({
|
|
49
|
+
doStream: async (options: any) => {
|
|
50
|
+
const idx = Math.min(
|
|
51
|
+
options.prompt.filter((m: any) => m.role === 'assistant').length,
|
|
52
|
+
responses.length - 1,
|
|
53
|
+
);
|
|
54
|
+
const r = responses[idx];
|
|
55
|
+
const parts =
|
|
56
|
+
r.type === 'text'
|
|
57
|
+
? [
|
|
22
58
|
{ type: 'stream-start', warnings: [] },
|
|
23
59
|
{
|
|
24
60
|
type: 'response-metadata',
|
|
@@ -27,7 +63,7 @@ export function mockTextModel(text: string) {
|
|
|
27
63
|
timestamp: new Date(),
|
|
28
64
|
},
|
|
29
65
|
{ type: 'text-start', id: '1' },
|
|
30
|
-
{ type: 'text-delta', id: '1', delta:
|
|
66
|
+
{ type: 'text-delta', id: '1', delta: r.text },
|
|
31
67
|
{ type: 'text-end', id: '1' },
|
|
32
68
|
{
|
|
33
69
|
type: 'finish',
|
|
@@ -37,87 +73,38 @@ export function mockTextModel(text: string) {
|
|
|
37
73
|
outputTokens: { total: 10, text: 10 },
|
|
38
74
|
},
|
|
39
75
|
},
|
|
40
|
-
]
|
|
41
|
-
|
|
76
|
+
]
|
|
77
|
+
: [
|
|
78
|
+
{ type: 'stream-start', warnings: [] },
|
|
79
|
+
{
|
|
80
|
+
type: 'response-metadata',
|
|
81
|
+
id: 'r',
|
|
82
|
+
modelId: 'mock',
|
|
83
|
+
timestamp: new Date(),
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
type: 'tool-call',
|
|
87
|
+
toolCallId: `call-${idx + 1}`,
|
|
88
|
+
toolName: r.toolName,
|
|
89
|
+
input: r.input,
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
type: 'finish',
|
|
93
|
+
finishReason: { unified: 'tool-calls', raw: undefined },
|
|
94
|
+
usage: {
|
|
95
|
+
inputTokens: { total: 5, noCache: 5 },
|
|
96
|
+
outputTokens: { total: 10, text: 10 },
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
return {
|
|
101
|
+
stream: new ReadableStream({
|
|
102
|
+
start(c) {
|
|
103
|
+
for (const p of parts as any[]) c.enqueue(p);
|
|
42
104
|
c.close();
|
|
43
105
|
},
|
|
44
106
|
}),
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Mock model that plays through a sequence of responses.
|
|
52
|
-
* Determines which response to return by counting assistant messages in the prompt.
|
|
53
|
-
* Only captures `responses` (array of plain objects) — fully serializable.
|
|
54
|
-
*/
|
|
55
|
-
export function mockSequenceModel(responses: MockResponseDescriptor[]) {
|
|
56
|
-
return async () => {
|
|
57
|
-
'use step';
|
|
58
|
-
// Bind closure var at step body level so SWC plugin detects it
|
|
59
|
-
const _responses = responses;
|
|
60
|
-
return mockProvider({
|
|
61
|
-
doStream: async (options: any) => {
|
|
62
|
-
const idx = Math.min(
|
|
63
|
-
options.prompt.filter((m: any) => m.role === 'assistant').length,
|
|
64
|
-
_responses.length - 1,
|
|
65
|
-
);
|
|
66
|
-
const r = _responses[idx];
|
|
67
|
-
const parts =
|
|
68
|
-
r.type === 'text'
|
|
69
|
-
? [
|
|
70
|
-
{ type: 'stream-start', warnings: [] },
|
|
71
|
-
{
|
|
72
|
-
type: 'response-metadata',
|
|
73
|
-
id: 'r',
|
|
74
|
-
modelId: 'mock',
|
|
75
|
-
timestamp: new Date(),
|
|
76
|
-
},
|
|
77
|
-
{ type: 'text-start', id: '1' },
|
|
78
|
-
{ type: 'text-delta', id: '1', delta: r.text },
|
|
79
|
-
{ type: 'text-end', id: '1' },
|
|
80
|
-
{
|
|
81
|
-
type: 'finish',
|
|
82
|
-
finishReason: { unified: 'stop', raw: 'stop' },
|
|
83
|
-
usage: {
|
|
84
|
-
inputTokens: { total: 5, noCache: 5 },
|
|
85
|
-
outputTokens: { total: 10, text: 10 },
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
]
|
|
89
|
-
: [
|
|
90
|
-
{ type: 'stream-start', warnings: [] },
|
|
91
|
-
{
|
|
92
|
-
type: 'response-metadata',
|
|
93
|
-
id: 'r',
|
|
94
|
-
modelId: 'mock',
|
|
95
|
-
timestamp: new Date(),
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
type: 'tool-call',
|
|
99
|
-
toolCallId: `call-${idx + 1}`,
|
|
100
|
-
toolName: r.toolName,
|
|
101
|
-
input: r.input,
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
type: 'finish',
|
|
105
|
-
finishReason: { unified: 'tool-calls', raw: undefined },
|
|
106
|
-
usage: {
|
|
107
|
-
inputTokens: { total: 5, noCache: 5 },
|
|
108
|
-
outputTokens: { total: 10, text: 10 },
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
];
|
|
112
|
-
return {
|
|
113
|
-
stream: new ReadableStream({
|
|
114
|
-
start(c) {
|
|
115
|
-
for (const p of parts as any[]) c.enqueue(p);
|
|
116
|
-
c.close();
|
|
117
|
-
},
|
|
118
|
-
}),
|
|
119
|
-
};
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
};
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
});
|
|
123
110
|
}
|
|
@@ -5,9 +5,9 @@ import type {
|
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
6
|
import type {
|
|
7
7
|
Experimental_LanguageModelStreamPart as ModelCallStreamPart,
|
|
8
|
+
LanguageModel,
|
|
8
9
|
ModelMessage,
|
|
9
10
|
StepResult,
|
|
10
|
-
StreamTextOnStepFinishCallback,
|
|
11
11
|
ToolCallRepairFunction,
|
|
12
12
|
ToolChoice,
|
|
13
13
|
ToolSet,
|
|
@@ -22,11 +22,11 @@ import { serializeToolSet } from './serializable-schema.js';
|
|
|
22
22
|
import type {
|
|
23
23
|
GenerationSettings,
|
|
24
24
|
PrepareStepCallback,
|
|
25
|
-
|
|
25
|
+
WorkflowAgentOnErrorCallback,
|
|
26
|
+
WorkflowAgentOnStepFinishCallback,
|
|
26
27
|
TelemetrySettings,
|
|
27
28
|
WorkflowAgentOnStepStartCallback,
|
|
28
29
|
} from './workflow-agent.js';
|
|
29
|
-
import type { CompatibleLanguageModel } from './types.js';
|
|
30
30
|
|
|
31
31
|
// Re-export for consumers
|
|
32
32
|
export type { ProviderExecutedToolResult } from './do-stream-step.js';
|
|
@@ -71,15 +71,12 @@ export async function* streamTextIterator({
|
|
|
71
71
|
prompt: LanguageModelV4Prompt;
|
|
72
72
|
tools: ToolSet;
|
|
73
73
|
writable?: WritableStream<ModelCallStreamPart<ToolSet>>;
|
|
74
|
-
model:
|
|
75
|
-
| string
|
|
76
|
-
| CompatibleLanguageModel
|
|
77
|
-
| (() => Promise<CompatibleLanguageModel>);
|
|
74
|
+
model: LanguageModel;
|
|
78
75
|
stopConditions?: ModelStopCondition[] | ModelStopCondition;
|
|
79
76
|
maxSteps?: number;
|
|
80
|
-
onStepFinish?:
|
|
77
|
+
onStepFinish?: WorkflowAgentOnStepFinishCallback<any>;
|
|
81
78
|
onStepStart?: WorkflowAgentOnStepStartCallback;
|
|
82
|
-
onError?:
|
|
79
|
+
onError?: WorkflowAgentOnErrorCallback;
|
|
83
80
|
prepareStep?: PrepareStepCallback<any>;
|
|
84
81
|
generationSettings?: GenerationSettings;
|
|
85
82
|
toolChoice?: ToolChoice<ToolSet>;
|
|
@@ -94,10 +91,7 @@ export async function* streamTextIterator({
|
|
|
94
91
|
LanguageModelV4ToolResultPart[]
|
|
95
92
|
> {
|
|
96
93
|
let conversationPrompt = [...prompt]; // Create a mutable copy
|
|
97
|
-
let currentModel:
|
|
98
|
-
| string
|
|
99
|
-
| CompatibleLanguageModel
|
|
100
|
-
| (() => Promise<CompatibleLanguageModel>) = model;
|
|
94
|
+
let currentModel: LanguageModel = model;
|
|
101
95
|
let currentGenerationSettings = generationSettings ?? {};
|
|
102
96
|
let currentToolChoice = toolChoice;
|
|
103
97
|
let currentContext = experimental_context;
|
|
@@ -248,6 +242,7 @@ export async function* streamTextIterator({
|
|
|
248
242
|
stepNumber,
|
|
249
243
|
model: currentModel,
|
|
250
244
|
messages: conversationPrompt as unknown as ModelMessage[],
|
|
245
|
+
steps: [...steps],
|
|
251
246
|
});
|
|
252
247
|
}
|
|
253
248
|
|