@ai-sdk/workflow 2.0.23 → 2.0.24
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 +16 -0
- package/dist/index.d.ts +64 -31
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +2 -0
- package/src/workflow-agent.ts +116 -69
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/workflow",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "WorkflowAgent for building AI agents with AI SDK",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@ai-sdk/provider": "4.0.10",
|
|
35
35
|
"@ai-sdk/provider-utils": "5.0.36",
|
|
36
|
-
"ai": "7.0.
|
|
36
|
+
"ai": "7.0.93",
|
|
37
37
|
"ajv": "^8.20.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -29,6 +29,8 @@ export {
|
|
|
29
29
|
type ToolCallRepairFunction,
|
|
30
30
|
type WorkflowAgentOnStartCallback,
|
|
31
31
|
type WorkflowAgentOnStepStartCallback,
|
|
32
|
+
type WorkflowAgentToolExecutionStartEvent,
|
|
33
|
+
type WorkflowAgentToolExecutionEndEvent,
|
|
32
34
|
type WorkflowAgentOnToolExecutionStartCallback,
|
|
33
35
|
type WorkflowAgentOnToolExecutionEndCallback,
|
|
34
36
|
} from './workflow-agent.js';
|
package/src/workflow-agent.ts
CHANGED
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
withUserAgentSuffix,
|
|
12
12
|
type Context,
|
|
13
13
|
type HasRequiredKey,
|
|
14
|
+
type InferToolContext,
|
|
15
|
+
type InferToolInput,
|
|
16
|
+
type InferToolOutput,
|
|
14
17
|
type InferToolSetContext,
|
|
15
18
|
} from '@ai-sdk/provider-utils';
|
|
16
19
|
import {
|
|
@@ -797,23 +800,109 @@ export type WorkflowAgentOnStepStartCallback<
|
|
|
797
800
|
readonly toolsContext: InferToolSetContext<TTools>;
|
|
798
801
|
}) => PromiseLike<void> | void;
|
|
799
802
|
|
|
803
|
+
type WorkflowToolContext<TOOL extends ToolSet[keyof ToolSet]> = [
|
|
804
|
+
InferToolContext<TOOL>,
|
|
805
|
+
] extends [never]
|
|
806
|
+
? undefined
|
|
807
|
+
: InferToolContext<TOOL>;
|
|
808
|
+
|
|
809
|
+
type WorkflowToolCall<
|
|
810
|
+
TTools extends ToolSet,
|
|
811
|
+
NAME extends keyof TTools,
|
|
812
|
+
> = ToolCall & {
|
|
813
|
+
readonly toolName: NAME & string;
|
|
814
|
+
readonly input: InferToolInput<TTools[NAME]>;
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Event passed to a WorkflowAgent tool-execution-start callback.
|
|
819
|
+
*
|
|
820
|
+
* For a concrete tool set, each union member correlates the tool call with that
|
|
821
|
+
* tool's context. TypeScript narrows the nested tool call directly; use
|
|
822
|
+
* `Extract` or a user-defined type guard to narrow correlated sibling fields.
|
|
823
|
+
*/
|
|
824
|
+
export type WorkflowAgentToolExecutionStartEvent<
|
|
825
|
+
TTools extends ToolSet = ToolSet,
|
|
826
|
+
> = [ToolSet] extends [TTools]
|
|
827
|
+
? {
|
|
828
|
+
readonly toolCall: ToolCall;
|
|
829
|
+
readonly stepNumber: number;
|
|
830
|
+
readonly messages: ModelMessage[];
|
|
831
|
+
readonly toolContext: unknown;
|
|
832
|
+
}
|
|
833
|
+
: {
|
|
834
|
+
[NAME in keyof TTools]: {
|
|
835
|
+
readonly toolCall: WorkflowToolCall<TTools, NAME>;
|
|
836
|
+
readonly stepNumber: number;
|
|
837
|
+
readonly messages: ModelMessage[];
|
|
838
|
+
readonly toolContext: WorkflowToolContext<TTools[NAME]>;
|
|
839
|
+
};
|
|
840
|
+
}[keyof TTools];
|
|
841
|
+
|
|
800
842
|
/**
|
|
801
843
|
* Callback that is called before a tool's execute function runs.
|
|
802
844
|
*/
|
|
803
845
|
export type WorkflowAgentOnToolExecutionStartCallback<
|
|
804
846
|
TTools extends ToolSet = ToolSet,
|
|
805
|
-
> = (
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
847
|
+
> = (
|
|
848
|
+
event: WorkflowAgentToolExecutionStartEvent<TTools>,
|
|
849
|
+
) => PromiseLike<void> | void;
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Event passed to a WorkflowAgent tool-execution-end callback.
|
|
853
|
+
*
|
|
854
|
+
* For a concrete tool set, each union member correlates the tool call with that
|
|
855
|
+
* tool's context and successful output. Check `success` to distinguish output
|
|
856
|
+
* and error events. Use `Extract` or a user-defined type guard to narrow
|
|
857
|
+
* correlated sibling fields by tool name.
|
|
858
|
+
*/
|
|
859
|
+
export type WorkflowAgentToolExecutionEndEvent<
|
|
860
|
+
TTools extends ToolSet = ToolSet,
|
|
861
|
+
> = [ToolSet] extends [TTools]
|
|
862
|
+
?
|
|
863
|
+
| {
|
|
864
|
+
readonly toolCall: ToolCall;
|
|
865
|
+
readonly stepNumber: number;
|
|
866
|
+
readonly durationMs: number;
|
|
867
|
+
readonly messages: ModelMessage[];
|
|
868
|
+
readonly toolContext: unknown;
|
|
869
|
+
readonly success: true;
|
|
870
|
+
readonly output: unknown;
|
|
871
|
+
readonly error?: never;
|
|
872
|
+
}
|
|
873
|
+
| {
|
|
874
|
+
readonly toolCall: ToolCall;
|
|
875
|
+
readonly stepNumber: number;
|
|
876
|
+
readonly durationMs: number;
|
|
877
|
+
readonly messages: ModelMessage[];
|
|
878
|
+
readonly toolContext: unknown;
|
|
879
|
+
readonly success: false;
|
|
880
|
+
readonly error: unknown;
|
|
881
|
+
readonly output?: never;
|
|
882
|
+
}
|
|
883
|
+
: {
|
|
884
|
+
[NAME in keyof TTools]:
|
|
885
|
+
| {
|
|
886
|
+
readonly toolCall: WorkflowToolCall<TTools, NAME>;
|
|
887
|
+
readonly stepNumber: number;
|
|
888
|
+
readonly durationMs: number;
|
|
889
|
+
readonly messages: ModelMessage[];
|
|
890
|
+
readonly toolContext: WorkflowToolContext<TTools[NAME]>;
|
|
891
|
+
readonly success: true;
|
|
892
|
+
readonly output: InferToolOutput<TTools[NAME]>;
|
|
893
|
+
readonly error?: never;
|
|
894
|
+
}
|
|
895
|
+
| {
|
|
896
|
+
readonly toolCall: WorkflowToolCall<TTools, NAME>;
|
|
897
|
+
readonly stepNumber: number;
|
|
898
|
+
readonly durationMs: number;
|
|
899
|
+
readonly messages: ModelMessage[];
|
|
900
|
+
readonly toolContext: WorkflowToolContext<TTools[NAME]>;
|
|
901
|
+
readonly success: false;
|
|
902
|
+
readonly error: unknown;
|
|
903
|
+
readonly output?: never;
|
|
904
|
+
};
|
|
905
|
+
}[keyof TTools];
|
|
817
906
|
|
|
818
907
|
/**
|
|
819
908
|
* Callback that is called after a tool execution completes.
|
|
@@ -823,45 +912,7 @@ export type WorkflowAgentOnToolExecutionStartCallback<
|
|
|
823
912
|
export type WorkflowAgentOnToolExecutionEndCallback<
|
|
824
913
|
TTools extends ToolSet = ToolSet,
|
|
825
914
|
> = (
|
|
826
|
-
event:
|
|
827
|
-
| {
|
|
828
|
-
/** The tool call that was executed */
|
|
829
|
-
readonly toolCall: ToolCall;
|
|
830
|
-
/** The current step number (0-based) */
|
|
831
|
-
readonly stepNumber: number;
|
|
832
|
-
/** Execution time in milliseconds */
|
|
833
|
-
readonly durationMs: number;
|
|
834
|
-
/** Messages sent to the language model for the step that produced the call */
|
|
835
|
-
readonly messages: ModelMessage[];
|
|
836
|
-
/** Tool-specific context passed to the tool */
|
|
837
|
-
readonly toolContext:
|
|
838
|
-
| InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>]
|
|
839
|
-
| undefined;
|
|
840
|
-
/** Whether the tool call succeeded */
|
|
841
|
-
readonly success: true;
|
|
842
|
-
/** The tool result */
|
|
843
|
-
readonly output: unknown;
|
|
844
|
-
readonly error?: never;
|
|
845
|
-
}
|
|
846
|
-
| {
|
|
847
|
-
/** The tool call that was executed */
|
|
848
|
-
readonly toolCall: ToolCall;
|
|
849
|
-
/** The current step number (0-based) */
|
|
850
|
-
readonly stepNumber: number;
|
|
851
|
-
/** Execution time in milliseconds */
|
|
852
|
-
readonly durationMs: number;
|
|
853
|
-
/** Messages sent to the language model for the step that produced the call */
|
|
854
|
-
readonly messages: ModelMessage[];
|
|
855
|
-
/** Tool-specific context passed to the tool */
|
|
856
|
-
readonly toolContext:
|
|
857
|
-
| InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>]
|
|
858
|
-
| undefined;
|
|
859
|
-
/** Whether the tool call succeeded */
|
|
860
|
-
readonly success: false;
|
|
861
|
-
/** The error that occurred */
|
|
862
|
-
readonly error: unknown;
|
|
863
|
-
readonly output?: never;
|
|
864
|
-
},
|
|
915
|
+
event: WorkflowAgentToolExecutionEndEvent<TTools>,
|
|
865
916
|
) => PromiseLike<void> | void;
|
|
866
917
|
|
|
867
918
|
/**
|
|
@@ -1636,11 +1687,15 @@ export class WorkflowAgent<
|
|
|
1636
1687
|
const timeoutAt =
|
|
1637
1688
|
options.timeout == null ? undefined : Date.now() + options.timeout;
|
|
1638
1689
|
const mergedOnToolExecutionStart = mergeCallbacks(
|
|
1639
|
-
this.constructorOnToolExecutionStart
|
|
1690
|
+
this.constructorOnToolExecutionStart as
|
|
1691
|
+
| WorkflowAgentOnToolExecutionStartCallback<TTools>
|
|
1692
|
+
| undefined,
|
|
1640
1693
|
options.onToolExecutionStart,
|
|
1641
1694
|
);
|
|
1642
1695
|
const mergedOnToolExecutionEnd = mergeCallbacks(
|
|
1643
|
-
this.constructorOnToolExecutionEnd
|
|
1696
|
+
this.constructorOnToolExecutionEnd as
|
|
1697
|
+
| WorkflowAgentOnToolExecutionEndCallback<TTools>
|
|
1698
|
+
| undefined,
|
|
1644
1699
|
options.onToolExecutionEnd,
|
|
1645
1700
|
);
|
|
1646
1701
|
|
|
@@ -2054,10 +2109,8 @@ export class WorkflowAgent<
|
|
|
2054
2109
|
toolCall: toolCallEvent,
|
|
2055
2110
|
stepNumber: currentStepNumber,
|
|
2056
2111
|
messages: modelMessages,
|
|
2057
|
-
toolContext: resolvedContext
|
|
2058
|
-
|
|
2059
|
-
| undefined,
|
|
2060
|
-
});
|
|
2112
|
+
toolContext: resolvedContext,
|
|
2113
|
+
} as WorkflowAgentToolExecutionStartEvent<TTools>);
|
|
2061
2114
|
}
|
|
2062
2115
|
await telemetryDispatcher.onToolExecutionStart?.({
|
|
2063
2116
|
toolCall: toolCallEvent,
|
|
@@ -2097,12 +2150,10 @@ export class WorkflowAgent<
|
|
|
2097
2150
|
stepNumber: currentStepNumber,
|
|
2098
2151
|
durationMs,
|
|
2099
2152
|
messages: modelMessages,
|
|
2100
|
-
toolContext: resolvedContext
|
|
2101
|
-
| InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>]
|
|
2102
|
-
| undefined,
|
|
2153
|
+
toolContext: resolvedContext,
|
|
2103
2154
|
success: false,
|
|
2104
2155
|
error: err,
|
|
2105
|
-
});
|
|
2156
|
+
} as WorkflowAgentToolExecutionEndEvent<TTools>);
|
|
2106
2157
|
}
|
|
2107
2158
|
await telemetryDispatcher.onToolExecutionEnd?.({
|
|
2108
2159
|
toolCall: toolCallEvent,
|
|
@@ -2126,24 +2177,20 @@ export class WorkflowAgent<
|
|
|
2126
2177
|
stepNumber: currentStepNumber,
|
|
2127
2178
|
durationMs,
|
|
2128
2179
|
messages: modelMessages,
|
|
2129
|
-
toolContext: resolvedContext
|
|
2130
|
-
| InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>]
|
|
2131
|
-
| undefined,
|
|
2180
|
+
toolContext: resolvedContext,
|
|
2132
2181
|
success: false,
|
|
2133
2182
|
error: result.rawOutput,
|
|
2134
|
-
});
|
|
2183
|
+
} as WorkflowAgentToolExecutionEndEvent<TTools>);
|
|
2135
2184
|
} else {
|
|
2136
2185
|
await mergedOnToolExecutionEnd({
|
|
2137
2186
|
toolCall: toolCallEvent,
|
|
2138
2187
|
stepNumber: currentStepNumber,
|
|
2139
2188
|
durationMs,
|
|
2140
2189
|
messages: modelMessages,
|
|
2141
|
-
toolContext: resolvedContext
|
|
2142
|
-
| InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>]
|
|
2143
|
-
| undefined,
|
|
2190
|
+
toolContext: resolvedContext,
|
|
2144
2191
|
success: true,
|
|
2145
2192
|
output: result.rawOutput,
|
|
2146
|
-
});
|
|
2193
|
+
} as WorkflowAgentToolExecutionEndEvent<TTools>);
|
|
2147
2194
|
}
|
|
2148
2195
|
}
|
|
2149
2196
|
if (result.isError) {
|