ai 5.0.0-beta.21 → 5.0.0-beta.22

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
@@ -3086,24 +3086,24 @@ function processUIMessageStream({
3086
3086
  }) {
3087
3087
  return stream.pipeThrough(
3088
3088
  new TransformStream({
3089
- async transform(part, controller) {
3089
+ async transform(chunk, controller) {
3090
3090
  await runUpdateMessageJob(async ({ state, write }) => {
3091
3091
  var _a16, _b, _c, _d;
3092
3092
  function updateToolInvocationPart(options) {
3093
3093
  var _a17;
3094
- const part2 = state.message.parts.find(
3095
- (part3) => isToolUIPart(part3) && part3.toolCallId === options.toolCallId
3094
+ const part = state.message.parts.find(
3095
+ (part2) => isToolUIPart(part2) && part2.toolCallId === options.toolCallId
3096
3096
  );
3097
3097
  const anyOptions = options;
3098
- const anyPart = part2;
3099
- if (part2 != null) {
3100
- part2.state = options.state;
3098
+ const anyPart = part;
3099
+ if (part != null) {
3100
+ part.state = options.state;
3101
3101
  anyPart.input = anyOptions.input;
3102
3102
  anyPart.output = anyOptions.output;
3103
3103
  anyPart.errorText = anyOptions.errorText;
3104
- anyPart.providerExecuted = (_a17 = anyOptions.providerExecuted) != null ? _a17 : part2.providerExecuted;
3105
- if (anyOptions.providerMetadata != null && part2.state === "input-available") {
3106
- part2.callProviderMetadata = anyOptions.providerMetadata;
3104
+ anyPart.providerExecuted = (_a17 = anyOptions.providerExecuted) != null ? _a17 : part.providerExecuted;
3105
+ if (anyOptions.providerMetadata != null && part.state === "input-available") {
3106
+ part.callProviderMetadata = anyOptions.providerMetadata;
3107
3107
  }
3108
3108
  } else {
3109
3109
  state.message.parts.push({
@@ -3130,31 +3130,31 @@ function processUIMessageStream({
3130
3130
  state.message.metadata = mergedMetadata;
3131
3131
  }
3132
3132
  }
3133
- switch (part.type) {
3133
+ switch (chunk.type) {
3134
3134
  case "text-start": {
3135
3135
  const textPart = {
3136
3136
  type: "text",
3137
3137
  text: "",
3138
- providerMetadata: part.providerMetadata,
3138
+ providerMetadata: chunk.providerMetadata,
3139
3139
  state: "streaming"
3140
3140
  };
3141
- state.activeTextParts[part.id] = textPart;
3141
+ state.activeTextParts[chunk.id] = textPart;
3142
3142
  state.message.parts.push(textPart);
3143
3143
  write();
3144
3144
  break;
3145
3145
  }
3146
3146
  case "text-delta": {
3147
- const textPart = state.activeTextParts[part.id];
3148
- textPart.text += part.delta;
3149
- textPart.providerMetadata = (_a16 = part.providerMetadata) != null ? _a16 : textPart.providerMetadata;
3147
+ const textPart = state.activeTextParts[chunk.id];
3148
+ textPart.text += chunk.delta;
3149
+ textPart.providerMetadata = (_a16 = chunk.providerMetadata) != null ? _a16 : textPart.providerMetadata;
3150
3150
  write();
3151
3151
  break;
3152
3152
  }
3153
3153
  case "text-end": {
3154
- const textPart = state.activeTextParts[part.id];
3154
+ const textPart = state.activeTextParts[chunk.id];
3155
3155
  textPart.state = "done";
3156
- textPart.providerMetadata = (_b = part.providerMetadata) != null ? _b : textPart.providerMetadata;
3157
- delete state.activeTextParts[part.id];
3156
+ textPart.providerMetadata = (_b = chunk.providerMetadata) != null ? _b : textPart.providerMetadata;
3157
+ delete state.activeTextParts[chunk.id];
3158
3158
  write();
3159
3159
  break;
3160
3160
  }
@@ -3162,34 +3162,34 @@ function processUIMessageStream({
3162
3162
  const reasoningPart = {
3163
3163
  type: "reasoning",
3164
3164
  text: "",
3165
- providerMetadata: part.providerMetadata,
3165
+ providerMetadata: chunk.providerMetadata,
3166
3166
  state: "streaming"
3167
3167
  };
3168
- state.activeReasoningParts[part.id] = reasoningPart;
3168
+ state.activeReasoningParts[chunk.id] = reasoningPart;
3169
3169
  state.message.parts.push(reasoningPart);
3170
3170
  write();
3171
3171
  break;
3172
3172
  }
3173
3173
  case "reasoning-delta": {
3174
- const reasoningPart = state.activeReasoningParts[part.id];
3175
- reasoningPart.text += part.delta;
3176
- reasoningPart.providerMetadata = (_c = part.providerMetadata) != null ? _c : reasoningPart.providerMetadata;
3174
+ const reasoningPart = state.activeReasoningParts[chunk.id];
3175
+ reasoningPart.text += chunk.delta;
3176
+ reasoningPart.providerMetadata = (_c = chunk.providerMetadata) != null ? _c : reasoningPart.providerMetadata;
3177
3177
  write();
3178
3178
  break;
3179
3179
  }
3180
3180
  case "reasoning-end": {
3181
- const reasoningPart = state.activeReasoningParts[part.id];
3182
- reasoningPart.providerMetadata = (_d = part.providerMetadata) != null ? _d : reasoningPart.providerMetadata;
3181
+ const reasoningPart = state.activeReasoningParts[chunk.id];
3182
+ reasoningPart.providerMetadata = (_d = chunk.providerMetadata) != null ? _d : reasoningPart.providerMetadata;
3183
3183
  reasoningPart.state = "done";
3184
- delete state.activeReasoningParts[part.id];
3184
+ delete state.activeReasoningParts[chunk.id];
3185
3185
  write();
3186
3186
  break;
3187
3187
  }
3188
3188
  case "file": {
3189
3189
  state.message.parts.push({
3190
3190
  type: "file",
3191
- mediaType: part.mediaType,
3192
- url: part.url
3191
+ mediaType: chunk.mediaType,
3192
+ url: chunk.url
3193
3193
  });
3194
3194
  write();
3195
3195
  break;
@@ -3197,10 +3197,10 @@ function processUIMessageStream({
3197
3197
  case "source-url": {
3198
3198
  state.message.parts.push({
3199
3199
  type: "source-url",
3200
- sourceId: part.sourceId,
3201
- url: part.url,
3202
- title: part.title,
3203
- providerMetadata: part.providerMetadata
3200
+ sourceId: chunk.sourceId,
3201
+ url: chunk.url,
3202
+ title: chunk.title,
3203
+ providerMetadata: chunk.providerMetadata
3204
3204
  });
3205
3205
  write();
3206
3206
  break;
@@ -3208,40 +3208,40 @@ function processUIMessageStream({
3208
3208
  case "source-document": {
3209
3209
  state.message.parts.push({
3210
3210
  type: "source-document",
3211
- sourceId: part.sourceId,
3212
- mediaType: part.mediaType,
3213
- title: part.title,
3214
- filename: part.filename,
3215
- providerMetadata: part.providerMetadata
3211
+ sourceId: chunk.sourceId,
3212
+ mediaType: chunk.mediaType,
3213
+ title: chunk.title,
3214
+ filename: chunk.filename,
3215
+ providerMetadata: chunk.providerMetadata
3216
3216
  });
3217
3217
  write();
3218
3218
  break;
3219
3219
  }
3220
3220
  case "tool-input-start": {
3221
3221
  const toolInvocations = state.message.parts.filter(isToolUIPart);
3222
- state.partialToolCalls[part.toolCallId] = {
3222
+ state.partialToolCalls[chunk.toolCallId] = {
3223
3223
  text: "",
3224
- toolName: part.toolName,
3224
+ toolName: chunk.toolName,
3225
3225
  index: toolInvocations.length
3226
3226
  };
3227
3227
  updateToolInvocationPart({
3228
- toolCallId: part.toolCallId,
3229
- toolName: part.toolName,
3228
+ toolCallId: chunk.toolCallId,
3229
+ toolName: chunk.toolName,
3230
3230
  state: "input-streaming",
3231
3231
  input: void 0,
3232
- providerExecuted: part.providerExecuted
3232
+ providerExecuted: chunk.providerExecuted
3233
3233
  });
3234
3234
  write();
3235
3235
  break;
3236
3236
  }
3237
3237
  case "tool-input-delta": {
3238
- const partialToolCall = state.partialToolCalls[part.toolCallId];
3239
- partialToolCall.text += part.inputTextDelta;
3238
+ const partialToolCall = state.partialToolCalls[chunk.toolCallId];
3239
+ partialToolCall.text += chunk.inputTextDelta;
3240
3240
  const { value: partialArgs } = await parsePartialJson(
3241
3241
  partialToolCall.text
3242
3242
  );
3243
3243
  updateToolInvocationPart({
3244
- toolCallId: part.toolCallId,
3244
+ toolCallId: chunk.toolCallId,
3245
3245
  toolName: partialToolCall.toolName,
3246
3246
  state: "input-streaming",
3247
3247
  input: partialArgs
@@ -3251,24 +3251,24 @@ function processUIMessageStream({
3251
3251
  }
3252
3252
  case "tool-input-available": {
3253
3253
  updateToolInvocationPart({
3254
- toolCallId: part.toolCallId,
3255
- toolName: part.toolName,
3254
+ toolCallId: chunk.toolCallId,
3255
+ toolName: chunk.toolName,
3256
3256
  state: "input-available",
3257
- input: part.input,
3258
- providerExecuted: part.providerExecuted,
3259
- providerMetadata: part.providerMetadata
3257
+ input: chunk.input,
3258
+ providerExecuted: chunk.providerExecuted,
3259
+ providerMetadata: chunk.providerMetadata
3260
3260
  });
3261
3261
  write();
3262
- if (onToolCall && !part.providerExecuted) {
3262
+ if (onToolCall && !chunk.providerExecuted) {
3263
3263
  const result = await onToolCall({
3264
- toolCall: part
3264
+ toolCall: chunk
3265
3265
  });
3266
3266
  if (result != null) {
3267
3267
  updateToolInvocationPart({
3268
- toolCallId: part.toolCallId,
3269
- toolName: part.toolName,
3268
+ toolCallId: chunk.toolCallId,
3269
+ toolName: chunk.toolName,
3270
3270
  state: "output-available",
3271
- input: part.input,
3271
+ input: chunk.input,
3272
3272
  output: result
3273
3273
  });
3274
3274
  write();
@@ -3282,7 +3282,7 @@ function processUIMessageStream({
3282
3282
  throw new Error("tool_result must be preceded by a tool_call");
3283
3283
  }
3284
3284
  const toolInvocationIndex = toolInvocations.findIndex(
3285
- (invocation) => invocation.toolCallId === part.toolCallId
3285
+ (invocation) => invocation.toolCallId === chunk.toolCallId
3286
3286
  );
3287
3287
  if (toolInvocationIndex === -1) {
3288
3288
  throw new Error(
@@ -3293,12 +3293,12 @@ function processUIMessageStream({
3293
3293
  toolInvocations[toolInvocationIndex]
3294
3294
  );
3295
3295
  updateToolInvocationPart({
3296
- toolCallId: part.toolCallId,
3296
+ toolCallId: chunk.toolCallId,
3297
3297
  toolName,
3298
3298
  state: "output-available",
3299
3299
  input: toolInvocations[toolInvocationIndex].input,
3300
- output: part.output,
3301
- providerExecuted: part.providerExecuted
3300
+ output: chunk.output,
3301
+ providerExecuted: chunk.providerExecuted
3302
3302
  });
3303
3303
  write();
3304
3304
  break;
@@ -3309,7 +3309,7 @@ function processUIMessageStream({
3309
3309
  throw new Error("tool_result must be preceded by a tool_call");
3310
3310
  }
3311
3311
  const toolInvocationIndex = toolInvocations.findIndex(
3312
- (invocation) => invocation.toolCallId === part.toolCallId
3312
+ (invocation) => invocation.toolCallId === chunk.toolCallId
3313
3313
  );
3314
3314
  if (toolInvocationIndex === -1) {
3315
3315
  throw new Error(
@@ -3320,12 +3320,12 @@ function processUIMessageStream({
3320
3320
  toolInvocations[toolInvocationIndex]
3321
3321
  );
3322
3322
  updateToolInvocationPart({
3323
- toolCallId: part.toolCallId,
3323
+ toolCallId: chunk.toolCallId,
3324
3324
  toolName,
3325
3325
  state: "output-error",
3326
3326
  input: toolInvocations[toolInvocationIndex].input,
3327
- errorText: part.errorText,
3328
- providerExecuted: part.providerExecuted
3327
+ errorText: chunk.errorText,
3328
+ providerExecuted: chunk.providerExecuted
3329
3329
  });
3330
3330
  write();
3331
3331
  break;
@@ -3340,62 +3340,65 @@ function processUIMessageStream({
3340
3340
  break;
3341
3341
  }
3342
3342
  case "start": {
3343
- if (part.messageId != null) {
3344
- state.message.id = part.messageId;
3343
+ if (chunk.messageId != null) {
3344
+ state.message.id = chunk.messageId;
3345
3345
  }
3346
- await updateMessageMetadata(part.messageMetadata);
3347
- if (part.messageId != null || part.messageMetadata != null) {
3346
+ await updateMessageMetadata(chunk.messageMetadata);
3347
+ if (chunk.messageId != null || chunk.messageMetadata != null) {
3348
3348
  write();
3349
3349
  }
3350
3350
  break;
3351
3351
  }
3352
3352
  case "finish": {
3353
- await updateMessageMetadata(part.messageMetadata);
3354
- if (part.messageMetadata != null) {
3353
+ await updateMessageMetadata(chunk.messageMetadata);
3354
+ if (chunk.messageMetadata != null) {
3355
3355
  write();
3356
3356
  }
3357
3357
  break;
3358
3358
  }
3359
3359
  case "message-metadata": {
3360
- await updateMessageMetadata(part.messageMetadata);
3361
- if (part.messageMetadata != null) {
3360
+ await updateMessageMetadata(chunk.messageMetadata);
3361
+ if (chunk.messageMetadata != null) {
3362
3362
  write();
3363
3363
  }
3364
3364
  break;
3365
3365
  }
3366
3366
  case "error": {
3367
- onError == null ? void 0 : onError(new Error(part.errorText));
3367
+ onError == null ? void 0 : onError(new Error(chunk.errorText));
3368
3368
  break;
3369
3369
  }
3370
3370
  default: {
3371
- if (isDataUIMessageChunk(part)) {
3372
- const dataPart = part;
3373
- if (dataPart.transient) {
3374
- onData == null ? void 0 : onData(dataPart);
3371
+ if (isDataUIMessageChunk(chunk)) {
3372
+ if ((dataPartSchemas == null ? void 0 : dataPartSchemas[chunk.type]) != null) {
3373
+ await validateTypes({
3374
+ value: chunk.data,
3375
+ schema: dataPartSchemas[chunk.type]
3376
+ });
3377
+ }
3378
+ const dataChunk = chunk;
3379
+ if (dataChunk.transient) {
3380
+ onData == null ? void 0 : onData(dataChunk);
3375
3381
  break;
3376
3382
  }
3377
- const existingPart = dataPart.id != null ? state.message.parts.find(
3378
- (partArg) => dataPart.type === partArg.type && dataPart.id === partArg.id
3383
+ const existingUIPart = dataChunk.id != null ? state.message.parts.find(
3384
+ (chunkArg) => dataChunk.type === chunkArg.type && dataChunk.id === chunkArg.id
3379
3385
  ) : void 0;
3380
- if (existingPart != null) {
3381
- existingPart.data = isObject(existingPart.data) && isObject(dataPart.data) ? mergeObjects(existingPart.data, dataPart.data) : dataPart.data;
3386
+ if (existingUIPart != null) {
3387
+ existingUIPart.data = dataChunk.data;
3382
3388
  } else {
3383
- state.message.parts.push(dataPart);
3389
+ state.message.parts.push(dataChunk);
3384
3390
  }
3385
- onData == null ? void 0 : onData(dataPart);
3391
+ onData == null ? void 0 : onData(dataChunk);
3386
3392
  write();
3387
3393
  }
3388
3394
  }
3389
3395
  }
3390
- controller.enqueue(part);
3396
+ controller.enqueue(chunk);
3391
3397
  });
3392
3398
  }
3393
3399
  })
3394
3400
  );
3395
3401
  }
3396
- function isObject(value) {
3397
- return typeof value === "object" && value !== null;
3398
- }
3399
3402
 
3400
3403
  // src/ui-message-stream/handle-ui-message-stream-finish.ts
3401
3404
  function handleUIMessageStreamFinish({
@@ -4818,10 +4821,10 @@ var DefaultStreamTextResult = class {
4818
4821
  sendFinish = true,
4819
4822
  onError = getErrorMessage5
4820
4823
  } = {}) {
4821
- const responseMessageId = getResponseUIMessageId({
4824
+ const responseMessageId = generateMessageId != null ? getResponseUIMessageId({
4822
4825
  originalMessages,
4823
- responseMessageId: this.generateId
4824
- });
4826
+ responseMessageId: generateMessageId
4827
+ }) : void 0;
4825
4828
  const baseStream = this.fullStream.pipeThrough(
4826
4829
  new TransformStream({
4827
4830
  transform: async (part, controller) => {
@@ -4975,8 +4978,8 @@ var DefaultStreamTextResult = class {
4975
4978
  if (sendStart) {
4976
4979
  controller.enqueue({
4977
4980
  type: "start",
4978
- messageId: responseMessageId,
4979
- messageMetadata: messageMetadataValue
4981
+ ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
4982
+ ...responseMessageId != null ? { messageId: responseMessageId } : {}
4980
4983
  });
4981
4984
  }
4982
4985
  break;
@@ -4985,7 +4988,7 @@ var DefaultStreamTextResult = class {
4985
4988
  if (sendFinish) {
4986
4989
  controller.enqueue({
4987
4990
  type: "finish",
4988
- messageMetadata: messageMetadataValue
4991
+ ...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
4989
4992
  });
4990
4993
  }
4991
4994
  break;
@@ -7412,6 +7415,28 @@ var doWrap = ({
7412
7415
  };
7413
7416
  };
7414
7417
 
7418
+ // src/middleware/wrap-provider.ts
7419
+ function wrapProvider({
7420
+ provider,
7421
+ languageModelMiddleware
7422
+ }) {
7423
+ const wrappedProvider = {
7424
+ languageModel(modelId) {
7425
+ let model = provider.languageModel(modelId);
7426
+ model = wrapLanguageModel({
7427
+ model,
7428
+ middleware: languageModelMiddleware
7429
+ });
7430
+ return model;
7431
+ },
7432
+ textEmbeddingModel: provider.textEmbeddingModel,
7433
+ imageModel: provider.imageModel,
7434
+ transcriptionModel: provider.transcriptionModel,
7435
+ speechModel: provider.speechModel
7436
+ };
7437
+ return wrappedProvider;
7438
+ }
7439
+
7415
7440
  // src/registry/custom-provider.ts
7416
7441
  import {
7417
7442
  NoSuchModelError as NoSuchModelError2
@@ -7504,10 +7529,12 @@ import {
7504
7529
  NoSuchModelError as NoSuchModelError4
7505
7530
  } from "@ai-sdk/provider";
7506
7531
  function createProviderRegistry(providers, {
7507
- separator = ":"
7532
+ separator = ":",
7533
+ languageModelMiddleware
7508
7534
  } = {}) {
7509
7535
  const registry = new DefaultProviderRegistry({
7510
- separator
7536
+ separator,
7537
+ languageModelMiddleware
7511
7538
  });
7512
7539
  for (const [id, provider] of Object.entries(providers)) {
7513
7540
  registry.registerProvider({ id, provider });
@@ -7516,9 +7543,13 @@ function createProviderRegistry(providers, {
7516
7543
  }
7517
7544
  var experimental_createProviderRegistry = createProviderRegistry;
7518
7545
  var DefaultProviderRegistry = class {
7519
- constructor({ separator }) {
7546
+ constructor({
7547
+ separator,
7548
+ languageModelMiddleware
7549
+ }) {
7520
7550
  this.providers = {};
7521
7551
  this.separator = separator;
7552
+ this.languageModelMiddleware = languageModelMiddleware;
7522
7553
  }
7523
7554
  registerProvider({
7524
7555
  id,
@@ -7552,13 +7583,19 @@ var DefaultProviderRegistry = class {
7552
7583
  languageModel(id) {
7553
7584
  var _a16, _b;
7554
7585
  const [providerId, modelId] = this.splitId(id, "languageModel");
7555
- const model = (_b = (_a16 = this.getProvider(providerId, "languageModel")).languageModel) == null ? void 0 : _b.call(
7586
+ let model = (_b = (_a16 = this.getProvider(providerId, "languageModel")).languageModel) == null ? void 0 : _b.call(
7556
7587
  _a16,
7557
7588
  modelId
7558
7589
  );
7559
7590
  if (model == null) {
7560
7591
  throw new NoSuchModelError4({ modelId: id, modelType: "languageModel" });
7561
7592
  }
7593
+ if (this.languageModelMiddleware != null) {
7594
+ model = wrapLanguageModel({
7595
+ model,
7596
+ middleware: this.languageModelMiddleware
7597
+ });
7598
+ }
7562
7599
  return model;
7563
7600
  }
7564
7601
  textEmbeddingModel(id) {
@@ -9303,6 +9340,7 @@ export {
9303
9340
  tool2 as tool,
9304
9341
  toolModelMessageSchema,
9305
9342
  userModelMessageSchema,
9306
- wrapLanguageModel
9343
+ wrapLanguageModel,
9344
+ wrapProvider
9307
9345
  };
9308
9346
  //# sourceMappingURL=index.mjs.map