@ai-sdk/workflow 1.0.0-beta.0 → 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/dist/index.mjs CHANGED
@@ -77,18 +77,7 @@ function resolveSerializableTools(tools) {
77
77
  async function doStreamStep(conversationPrompt, modelInit, writable, serializedTools, options) {
78
78
  "use step";
79
79
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
80
- let model;
81
- if (typeof modelInit === "string") {
82
- model = gateway.languageModel(modelInit);
83
- } else if (typeof modelInit === "function") {
84
- model = await modelInit();
85
- } else if (typeof modelInit === "object" && modelInit !== null && "modelId" in modelInit) {
86
- model = modelInit;
87
- } else {
88
- throw new Error(
89
- 'Invalid "model initialization" argument. Must be a string, a LanguageModel instance, or a function that returns a LanguageModel instance.'
90
- );
91
- }
80
+ const model = typeof modelInit === "string" ? gateway.languageModel(modelInit) : modelInit;
92
81
  const tools = serializedTools ? resolveSerializableTools(serializedTools) : void 0;
93
82
  const { stream: modelStream } = await streamModelCall({
94
83
  model,
@@ -417,7 +406,8 @@ async function* streamTextIterator({
417
406
  await onStepStart({
418
407
  stepNumber,
419
408
  model: currentModel,
420
- messages: conversationPrompt
409
+ messages: conversationPrompt,
410
+ steps: [...steps]
421
411
  });
422
412
  }
423
413
  try {
@@ -559,12 +549,18 @@ function sanitizeProviderMetadataForToolCall(metadata) {
559
549
  var WorkflowAgent = class {
560
550
  constructor(options) {
561
551
  var _a, _b;
552
+ this.id = options.id;
562
553
  this.model = options.model;
563
554
  this.tools = (_a = options.tools) != null ? _a : {};
564
555
  this.instructions = (_b = options.instructions) != null ? _b : options.system;
565
556
  this.toolChoice = options.toolChoice;
566
557
  this.telemetry = options.experimental_telemetry;
567
558
  this.experimentalContext = options.experimental_context;
559
+ this.stopWhen = options.stopWhen;
560
+ this.activeTools = options.activeTools;
561
+ this.output = options.output;
562
+ this.experimentalRepairToolCall = options.experimental_repairToolCall;
563
+ this.experimentalDownload = options.experimental_download;
568
564
  this.prepareStep = options.prepareStep;
569
565
  this.constructorOnStepFinish = options.onStepFinish;
570
566
  this.constructorOnFinish = options.onFinish;
@@ -592,14 +588,16 @@ var WorkflowAgent = class {
592
588
  throw new Error("Not implemented");
593
589
  }
594
590
  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;
591
+ 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;
596
592
  let effectiveModel = this.model;
597
593
  let effectiveInstructions = (_a = options.system) != null ? _a : this.instructions;
594
+ let effectivePrompt = options.prompt;
598
595
  let effectiveMessages = options.messages;
599
596
  let effectiveGenerationSettings = { ...this.generationSettings };
600
597
  let effectiveExperimentalContext = (_b = options.experimental_context) != null ? _b : this.experimentalContext;
601
598
  let effectiveToolChoiceFromPrepare = (_c = options.toolChoice) != null ? _c : this.toolChoice;
602
599
  let effectiveTelemetryFromPrepare = (_d = options.experimental_telemetry) != null ? _d : this.telemetry;
600
+ const resolvedMessagesForPrepareCall = (_e = effectiveMessages != null ? effectiveMessages : typeof effectivePrompt === "string" ? [{ role: "user", content: effectivePrompt }] : effectivePrompt) != null ? _e : [];
603
601
  if (this.prepareCall) {
604
602
  const prepared = await this.prepareCall({
605
603
  model: effectiveModel,
@@ -608,14 +606,16 @@ var WorkflowAgent = class {
608
606
  toolChoice: effectiveToolChoiceFromPrepare,
609
607
  experimental_telemetry: effectiveTelemetryFromPrepare,
610
608
  experimental_context: effectiveExperimentalContext,
611
- messages: effectiveMessages,
609
+ messages: resolvedMessagesForPrepareCall,
612
610
  ...effectiveGenerationSettings
613
611
  });
614
612
  if (prepared.model !== void 0) effectiveModel = prepared.model;
615
613
  if (prepared.instructions !== void 0)
616
614
  effectiveInstructions = prepared.instructions;
617
- if (prepared.messages !== void 0)
615
+ if (prepared.messages !== void 0) {
618
616
  effectiveMessages = prepared.messages;
617
+ effectivePrompt = void 0;
618
+ }
619
619
  if (prepared.experimental_context !== void 0)
620
620
  effectiveExperimentalContext = prepared.experimental_context;
621
621
  if (prepared.toolChoice !== void 0)
@@ -645,7 +645,7 @@ var WorkflowAgent = class {
645
645
  }
646
646
  const prompt = await standardizePrompt({
647
647
  system: effectiveInstructions,
648
- messages: effectiveMessages
648
+ ...effectivePrompt != null ? { prompt: effectivePrompt } : { messages: effectiveMessages }
649
649
  });
650
650
  const { approvedToolApprovals, deniedToolApprovals } = collectToolApprovalsFromMessages(prompt.messages);
651
651
  if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {
@@ -741,10 +741,10 @@ var WorkflowAgent = class {
741
741
  const modelPrompt = await convertToLanguageModelPrompt({
742
742
  prompt,
743
743
  supportedUrls: {},
744
- download: options.experimental_download
744
+ download: (_f = options.experimental_download) != null ? _f : this.experimentalDownload
745
745
  });
746
746
  const effectiveAbortSignal = mergeAbortSignals(
747
- (_e = options.abortSignal) != null ? _e : effectiveGenerationSettings.abortSignal,
747
+ (_g = options.abortSignal) != null ? _g : effectiveGenerationSettings.abortSignal,
748
748
  options.timeout != null ? AbortSignal.timeout(options.timeout) : void 0
749
749
  );
750
750
  const mergedGenerationSettings = {
@@ -804,7 +804,8 @@ var WorkflowAgent = class {
804
804
  );
805
805
  const effectiveToolChoice = effectiveToolChoiceFromPrepare;
806
806
  const effectiveTelemetry = effectiveTelemetryFromPrepare;
807
- const effectiveTools = options.activeTools && options.activeTools.length > 0 ? filterTools(this.tools, options.activeTools) : this.tools;
807
+ const effectiveActiveTools = (_h = options.activeTools) != null ? _h : this.activeTools;
808
+ const effectiveTools = effectiveActiveTools && effectiveActiveTools.length > 0 ? filterTools(this.tools, effectiveActiveTools) : this.tools;
808
809
  let experimentalContext = effectiveExperimentalContext;
809
810
  const steps = [];
810
811
  let lastStepToolCalls = [];
@@ -812,61 +813,68 @@ var WorkflowAgent = class {
812
813
  if (mergedOnStart) {
813
814
  await mergedOnStart({
814
815
  model: effectiveModel,
815
- messages: effectiveMessages
816
+ messages: prompt.messages
816
817
  });
817
818
  }
818
- const executeToolWithCallbacks = async (toolCall, tools, messages2, context) => {
819
+ const executeToolWithCallbacks = async (toolCall, tools, messages2, context, currentStepNumber = 0) => {
820
+ const toolCallEvent = {
821
+ type: "tool-call",
822
+ toolCallId: toolCall.toolCallId,
823
+ toolName: toolCall.toolName,
824
+ input: toolCall.input
825
+ };
819
826
  if (mergedOnToolCallStart) {
820
827
  await mergedOnToolCallStart({
821
- toolCall: {
822
- type: "tool-call",
823
- toolCallId: toolCall.toolCallId,
824
- toolName: toolCall.toolName,
825
- input: toolCall.input
826
- }
828
+ toolCall: toolCallEvent,
829
+ stepNumber: currentStepNumber
827
830
  });
828
831
  }
832
+ const startTime = Date.now();
829
833
  let result;
830
834
  try {
831
835
  result = await executeTool(toolCall, tools, messages2, context);
832
836
  } catch (err) {
837
+ const durationMs2 = Date.now() - startTime;
833
838
  if (mergedOnToolCallFinish) {
834
839
  await mergedOnToolCallFinish({
835
- toolCall: {
836
- type: "tool-call",
837
- toolCallId: toolCall.toolCallId,
838
- toolName: toolCall.toolName,
839
- input: toolCall.input
840
- },
840
+ toolCall: toolCallEvent,
841
+ stepNumber: currentStepNumber,
842
+ durationMs: durationMs2,
843
+ success: false,
841
844
  error: err
842
845
  });
843
846
  }
844
847
  throw err;
845
848
  }
849
+ const durationMs = Date.now() - startTime;
846
850
  if (mergedOnToolCallFinish) {
847
851
  const isError = result.output && "type" in result.output && (result.output.type === "error-text" || result.output.type === "error-json");
848
- await mergedOnToolCallFinish({
849
- toolCall: {
850
- type: "tool-call",
851
- toolCallId: toolCall.toolCallId,
852
- toolName: toolCall.toolName,
853
- input: toolCall.input
854
- },
855
- ...isError ? {
852
+ if (isError) {
853
+ await mergedOnToolCallFinish({
854
+ toolCall: toolCallEvent,
855
+ stepNumber: currentStepNumber,
856
+ durationMs,
857
+ success: false,
856
858
  error: "value" in result.output ? result.output.value : void 0
857
- } : {
858
- result: result.output && "value" in result.output ? result.output.value : void 0
859
- }
860
- });
859
+ });
860
+ } else {
861
+ await mergedOnToolCallFinish({
862
+ toolCall: toolCallEvent,
863
+ stepNumber: currentStepNumber,
864
+ durationMs,
865
+ success: true,
866
+ output: result.output && "value" in result.output ? result.output.value : void 0
867
+ });
868
+ }
861
869
  }
862
870
  return result;
863
871
  };
864
- if ((_f = mergedGenerationSettings.abortSignal) == null ? void 0 : _f.aborted) {
872
+ if ((_i = mergedGenerationSettings.abortSignal) == null ? void 0 : _i.aborted) {
865
873
  if (options.onAbort) {
866
874
  await options.onAbort({ steps });
867
875
  }
868
876
  return {
869
- messages: options.messages,
877
+ messages: prompt.messages,
870
878
  steps,
871
879
  toolCalls: [],
872
880
  toolResults: [],
@@ -878,19 +886,19 @@ var WorkflowAgent = class {
878
886
  tools: effectiveTools,
879
887
  writable: options.writable,
880
888
  prompt: modelPrompt,
881
- stopConditions: options.stopWhen,
889
+ stopConditions: (_j = options.stopWhen) != null ? _j : this.stopWhen,
882
890
  maxSteps: options.maxSteps,
883
891
  onStepFinish: mergedOnStepFinish,
884
892
  onStepStart: mergedOnStepStart,
885
893
  onError: options.onError,
886
- prepareStep: (_g = options.prepareStep) != null ? _g : this.prepareStep,
894
+ prepareStep: (_k = options.prepareStep) != null ? _k : this.prepareStep,
887
895
  generationSettings: mergedGenerationSettings,
888
896
  toolChoice: effectiveToolChoice,
889
897
  experimental_context: experimentalContext,
890
898
  experimental_telemetry: effectiveTelemetry,
891
- includeRawChunks: (_h = options.includeRawChunks) != null ? _h : false,
892
- repairToolCall: options.experimental_repairToolCall,
893
- responseFormat: await ((_i = options.output) == null ? void 0 : _i.responseFormat)
899
+ includeRawChunks: (_l = options.includeRawChunks) != null ? _l : false,
900
+ repairToolCall: (_m = options.experimental_repairToolCall) != null ? _m : this.experimentalRepairToolCall,
901
+ responseFormat: await ((_o = (_n = options.output) != null ? _n : this.output) == null ? void 0 : _o.responseFormat)
894
902
  });
895
903
  let finalMessages;
896
904
  let encounteredError;
@@ -898,7 +906,7 @@ var WorkflowAgent = class {
898
906
  try {
899
907
  let result = await iterator.next();
900
908
  while (!result.done) {
901
- if ((_j = mergedGenerationSettings.abortSignal) == null ? void 0 : _j.aborted) {
909
+ if ((_p = mergedGenerationSettings.abortSignal) == null ? void 0 : _p.aborted) {
902
910
  wasAborted = true;
903
911
  if (options.onAbort) {
904
912
  await options.onAbort({ steps });
@@ -912,6 +920,7 @@ var WorkflowAgent = class {
912
920
  context,
913
921
  providerExecutedToolResults
914
922
  } = result.value;
923
+ const currentStepNumber = steps.length;
915
924
  if (step) {
916
925
  steps.push(step);
917
926
  }
@@ -952,7 +961,8 @@ var WorkflowAgent = class {
952
961
  toolCall,
953
962
  effectiveTools,
954
963
  iterMessages,
955
- experimentalContext
964
+ experimentalContext,
965
+ currentStepNumber
956
966
  )
957
967
  )
958
968
  );
@@ -991,8 +1001,8 @@ var WorkflowAgent = class {
991
1001
  await mergedOnFinish({
992
1002
  steps,
993
1003
  messages: messages2,
994
- text: (_k = lastStep == null ? void 0 : lastStep.text) != null ? _k : "",
995
- finishReason: (_l = lastStep == null ? void 0 : lastStep.finishReason) != null ? _l : "other",
1004
+ text: (_q = lastStep == null ? void 0 : lastStep.text) != null ? _q : "",
1005
+ finishReason: (_r = lastStep == null ? void 0 : lastStep.finishReason) != null ? _r : "other",
996
1006
  totalUsage: aggregateUsage(steps),
997
1007
  experimental_context: experimentalContext,
998
1008
  output: void 0
@@ -1016,8 +1026,8 @@ var WorkflowAgent = class {
1016
1026
  }
1017
1027
  }
1018
1028
  if (options.writable) {
1019
- const sendFinish = (_m = options.sendFinish) != null ? _m : true;
1020
- const preventClose = (_n = options.preventClose) != null ? _n : false;
1029
+ const sendFinish = (_s = options.sendFinish) != null ? _s : true;
1030
+ const preventClose = (_t = options.preventClose) != null ? _t : false;
1021
1031
  if (sendFinish || !preventClose) {
1022
1032
  await closeStream(options.writable, preventClose, sendFinish);
1023
1033
  }
@@ -1036,7 +1046,8 @@ var WorkflowAgent = class {
1036
1046
  toolCall,
1037
1047
  effectiveTools,
1038
1048
  iterMessages,
1039
- experimentalContext
1049
+ experimentalContext,
1050
+ currentStepNumber
1040
1051
  )
1041
1052
  )
1042
1053
  );
@@ -1110,14 +1121,15 @@ var WorkflowAgent = class {
1110
1121
  await options.onError({ error });
1111
1122
  }
1112
1123
  }
1113
- const messages = finalMessages != null ? finalMessages : options.messages;
1124
+ const messages = finalMessages != null ? finalMessages : prompt.messages;
1125
+ const effectiveOutput = (_u = options.output) != null ? _u : this.output;
1114
1126
  let experimentalOutput = void 0;
1115
- if (options.output && steps.length > 0) {
1127
+ if (effectiveOutput && steps.length > 0) {
1116
1128
  const lastStep = steps[steps.length - 1];
1117
1129
  const text = lastStep.text;
1118
1130
  if (text) {
1119
1131
  try {
1120
- experimentalOutput = await options.output.parseCompleteOutput(
1132
+ experimentalOutput = await effectiveOutput.parseCompleteOutput(
1121
1133
  { text },
1122
1134
  {
1123
1135
  response: lastStep.response,
@@ -1137,8 +1149,8 @@ var WorkflowAgent = class {
1137
1149
  await mergedOnFinish({
1138
1150
  steps,
1139
1151
  messages,
1140
- text: (_o = lastStep == null ? void 0 : lastStep.text) != null ? _o : "",
1141
- finishReason: (_p = lastStep == null ? void 0 : lastStep.finishReason) != null ? _p : "other",
1152
+ text: (_v = lastStep == null ? void 0 : lastStep.text) != null ? _v : "",
1153
+ finishReason: (_w = lastStep == null ? void 0 : lastStep.finishReason) != null ? _w : "other",
1142
1154
  totalUsage: aggregateUsage(steps),
1143
1155
  experimental_context: experimentalContext,
1144
1156
  output: experimentalOutput
@@ -1146,8 +1158,8 @@ var WorkflowAgent = class {
1146
1158
  }
1147
1159
  if (encounteredError) {
1148
1160
  if (options.writable) {
1149
- const sendFinish = (_q = options.sendFinish) != null ? _q : true;
1150
- const preventClose = (_r = options.preventClose) != null ? _r : false;
1161
+ const sendFinish = (_x = options.sendFinish) != null ? _x : true;
1162
+ const preventClose = (_y = options.preventClose) != null ? _y : false;
1151
1163
  if (sendFinish || !preventClose) {
1152
1164
  await closeStream(options.writable, preventClose, sendFinish);
1153
1165
  }
@@ -1155,8 +1167,8 @@ var WorkflowAgent = class {
1155
1167
  throw encounteredError;
1156
1168
  }
1157
1169
  if (options.writable) {
1158
- const sendFinish = (_s = options.sendFinish) != null ? _s : true;
1159
- const preventClose = (_t = options.preventClose) != null ? _t : false;
1170
+ const sendFinish = (_z = options.sendFinish) != null ? _z : true;
1171
+ const preventClose = (_A = options.preventClose) != null ? _A : false;
1160
1172
  if (sendFinish || !preventClose) {
1161
1173
  await closeStream(options.writable, preventClose, sendFinish);
1162
1174
  }