ai 3.4.1 → 3.4.2

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
@@ -2990,6 +2990,7 @@ async function generateText({
2990
2990
  maxAutomaticRoundtrips = 0,
2991
2991
  maxToolRoundtrips = maxAutomaticRoundtrips,
2992
2992
  maxSteps = maxToolRoundtrips != null ? maxToolRoundtrips + 1 : 1,
2993
+ experimental_continuationSteps: continuationSteps = false,
2993
2994
  experimental_telemetry: telemetry,
2994
2995
  experimental_providerMetadata: providerMetadata,
2995
2996
  _internal: {
@@ -3033,7 +3034,7 @@ async function generateText({
3033
3034
  }),
3034
3035
  tracer,
3035
3036
  fn: async (span) => {
3036
- var _a12, _b, _c, _d, _e;
3037
+ var _a12, _b, _c, _d, _e, _f;
3037
3038
  const retry = retryWithExponentialBackoff({ maxRetries });
3038
3039
  const validatedPrompt = validatePrompt({
3039
3040
  system,
@@ -3054,12 +3055,14 @@ async function generateText({
3054
3055
  let currentToolResults = [];
3055
3056
  let stepCount = 0;
3056
3057
  const responseMessages = [];
3058
+ let text = "";
3057
3059
  const steps = [];
3058
3060
  const usage = {
3059
3061
  completionTokens: 0,
3060
3062
  promptTokens: 0,
3061
3063
  totalTokens: 0
3062
3064
  };
3065
+ let stepType = "initial";
3063
3066
  do {
3064
3067
  const currentInputFormat = stepCount === 0 ? validatedPrompt.type : "messages";
3065
3068
  currentModelResponse = await retry(
@@ -3091,7 +3094,7 @@ async function generateText({
3091
3094
  }),
3092
3095
  tracer,
3093
3096
  fn: async (span2) => {
3094
- var _a13, _b2, _c2, _d2, _e2, _f;
3097
+ var _a13, _b2, _c2, _d2, _e2, _f2;
3095
3098
  const result = await model.doGenerate({
3096
3099
  mode,
3097
3100
  ...callSettings,
@@ -3104,7 +3107,7 @@ async function generateText({
3104
3107
  const responseData = {
3105
3108
  id: (_b2 = (_a13 = result.response) == null ? void 0 : _a13.id) != null ? _b2 : generateId3(),
3106
3109
  timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
3107
- modelId: (_f = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f : model.modelId
3110
+ modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : model.modelId
3108
3111
  };
3109
3112
  span2.setAttributes(
3110
3113
  selectTelemetryAttributes({
@@ -3158,8 +3161,13 @@ async function generateText({
3158
3161
  usage.completionTokens += currentUsage.completionTokens;
3159
3162
  usage.promptTokens += currentUsage.promptTokens;
3160
3163
  usage.totalTokens += currentUsage.totalTokens;
3164
+ if (stepType === "continuation") {
3165
+ text += " " + ((_b = currentModelResponse.text) != null ? _b : "");
3166
+ } else {
3167
+ text = (_c = currentModelResponse.text) != null ? _c : "";
3168
+ }
3161
3169
  const currentStep = {
3162
- text: (_b = currentModelResponse.text) != null ? _b : "",
3170
+ text: (_d = currentModelResponse.text) != null ? _d : "",
3163
3171
  toolCalls: currentToolCalls,
3164
3172
  toolResults: currentToolResults,
3165
3173
  finishReason: currentModelResponse.finishReason,
@@ -3168,29 +3176,55 @@ async function generateText({
3168
3176
  logprobs: currentModelResponse.logprobs,
3169
3177
  response: {
3170
3178
  ...currentModelResponse.response,
3171
- headers: (_c = currentModelResponse.rawResponse) == null ? void 0 : _c.headers
3179
+ headers: (_e = currentModelResponse.rawResponse) == null ? void 0 : _e.headers
3172
3180
  },
3173
3181
  experimental_providerMetadata: currentModelResponse.providerMetadata
3174
3182
  };
3175
3183
  steps.push(currentStep);
3176
3184
  await (onStepFinish == null ? void 0 : onStepFinish(currentStep));
3177
- const newResponseMessages = toResponseMessages({
3178
- text: currentModelResponse.text,
3179
- toolCalls: currentToolCalls,
3180
- toolResults: currentToolResults
3181
- });
3182
- responseMessages.push(...newResponseMessages);
3183
- promptMessages.push(
3184
- ...newResponseMessages.map(
3185
- (message) => convertToLanguageModelMessage(message, null)
3186
- )
3187
- );
3188
- } while (
3189
- // there are tool calls:
3190
- currentToolCalls.length > 0 && // all current tool calls have results:
3191
- currentToolResults.length === currentToolCalls.length && // the number of steps is less than the maximum:
3192
- ++stepCount < maxSteps
3193
- );
3185
+ if (stepType === "continuation") {
3186
+ const lastResponseMessage = responseMessages.pop();
3187
+ promptMessages.pop();
3188
+ if (typeof lastResponseMessage.content === "string") {
3189
+ lastResponseMessage.content = text;
3190
+ } else {
3191
+ lastResponseMessage.content.push({
3192
+ text: " " + currentModelResponse.text,
3193
+ type: "text"
3194
+ });
3195
+ }
3196
+ responseMessages.push(lastResponseMessage);
3197
+ promptMessages.push(
3198
+ convertToLanguageModelMessage(lastResponseMessage, null)
3199
+ );
3200
+ } else {
3201
+ const newResponseMessages = toResponseMessages({
3202
+ text: currentModelResponse.text,
3203
+ toolCalls: currentToolCalls,
3204
+ toolResults: currentToolResults
3205
+ });
3206
+ responseMessages.push(...newResponseMessages);
3207
+ promptMessages.push(
3208
+ ...newResponseMessages.map(
3209
+ (message) => convertToLanguageModelMessage(message, null)
3210
+ )
3211
+ );
3212
+ }
3213
+ if (++stepCount >= maxSteps) {
3214
+ stepType = "done";
3215
+ } else if (continuationSteps === true && currentStep.finishReason === "length" && // only use continuation when there are no tool calls:
3216
+ currentToolCalls.length === 0) {
3217
+ stepType = "continuation";
3218
+ } else if (
3219
+ // there are tool calls:
3220
+ currentToolCalls.length > 0 && // all current tool calls have results:
3221
+ currentToolResults.length === currentToolCalls.length
3222
+ ) {
3223
+ stepType = "tool-result";
3224
+ } else {
3225
+ stepType = "done";
3226
+ }
3227
+ } while (stepType !== "done");
3194
3228
  span.setAttributes(
3195
3229
  selectTelemetryAttributes({
3196
3230
  telemetry,
@@ -3216,10 +3250,7 @@ async function generateText({
3216
3250
  })
3217
3251
  );
3218
3252
  return new DefaultGenerateTextResult({
3219
- // Always return a string so that the caller doesn't have to check for undefined.
3220
- // If they need to check if the model did not return any text,
3221
- // they can check the length of the string:
3222
- text: (_d = currentModelResponse.text) != null ? _d : "",
3253
+ text,
3223
3254
  toolCalls: currentToolCalls,
3224
3255
  toolResults: currentToolResults,
3225
3256
  finishReason: currentModelResponse.finishReason,
@@ -3227,7 +3258,7 @@ async function generateText({
3227
3258
  warnings: currentModelResponse.warnings,
3228
3259
  response: {
3229
3260
  ...currentModelResponse.response,
3230
- headers: (_e = currentModelResponse.rawResponse) == null ? void 0 : _e.headers
3261
+ headers: (_f = currentModelResponse.rawResponse) == null ? void 0 : _f.headers
3231
3262
  },
3232
3263
  logprobs: currentModelResponse.logprobs,
3233
3264
  responseMessages,