ai 6.0.24 → 6.0.26

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.js CHANGED
@@ -89,6 +89,7 @@ __export(src_exports, {
89
89
  experimental_generateImage: () => experimental_generateImage,
90
90
  experimental_generateSpeech: () => generateSpeech,
91
91
  experimental_transcribe: () => transcribe,
92
+ extractJsonMiddleware: () => extractJsonMiddleware,
92
93
  extractReasoningMiddleware: () => extractReasoningMiddleware,
93
94
  gateway: () => import_gateway3.gateway,
94
95
  generateId: () => import_provider_utils38.generateId,
@@ -1024,7 +1025,7 @@ var import_provider_utils3 = require("@ai-sdk/provider-utils");
1024
1025
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1025
1026
 
1026
1027
  // src/version.ts
1027
- var VERSION = true ? "6.0.24" : "0.0.0-test";
1028
+ var VERSION = true ? "6.0.26" : "0.0.0-test";
1028
1029
 
1029
1030
  // src/util/download/download.ts
1030
1031
  var download = async ({ url }) => {
@@ -2995,12 +2996,16 @@ async function parsePartialJson(jsonText) {
2995
2996
 
2996
2997
  // src/generate-text/output.ts
2997
2998
  var text = () => ({
2999
+ name: "text",
2998
3000
  responseFormat: Promise.resolve({ type: "text" }),
2999
3001
  async parseCompleteOutput({ text: text2 }) {
3000
3002
  return text2;
3001
3003
  },
3002
3004
  async parsePartialOutput({ text: text2 }) {
3003
3005
  return { partial: text2 };
3006
+ },
3007
+ createElementStreamTransform() {
3008
+ return void 0;
3004
3009
  }
3005
3010
  });
3006
3011
  var object = ({
@@ -3010,6 +3015,7 @@ var object = ({
3010
3015
  }) => {
3011
3016
  const schema = (0, import_provider_utils13.asSchema)(inputSchema);
3012
3017
  return {
3018
+ name: "object",
3013
3019
  responseFormat: (0, import_provider_utils13.resolve)(schema.jsonSchema).then((jsonSchema2) => ({
3014
3020
  type: "json",
3015
3021
  schema: jsonSchema2,
@@ -3059,6 +3065,9 @@ var object = ({
3059
3065
  };
3060
3066
  }
3061
3067
  }
3068
+ },
3069
+ createElementStreamTransform() {
3070
+ return void 0;
3062
3071
  }
3063
3072
  };
3064
3073
  };
@@ -3069,6 +3078,7 @@ var array = ({
3069
3078
  }) => {
3070
3079
  const elementSchema = (0, import_provider_utils13.asSchema)(inputElementSchema);
3071
3080
  return {
3081
+ name: "array",
3072
3082
  // JSON schema that describes an array of elements:
3073
3083
  responseFormat: (0, import_provider_utils13.resolve)(elementSchema.jsonSchema).then((jsonSchema2) => {
3074
3084
  const { $schema, ...itemSchema } = jsonSchema2;
@@ -3158,6 +3168,18 @@ var array = ({
3158
3168
  return { partial: parsedElements };
3159
3169
  }
3160
3170
  }
3171
+ },
3172
+ createElementStreamTransform() {
3173
+ let publishedElements = 0;
3174
+ return new TransformStream({
3175
+ transform({ partialOutput }, controller) {
3176
+ if (partialOutput != null) {
3177
+ for (; publishedElements < partialOutput.length; publishedElements++) {
3178
+ controller.enqueue(partialOutput[publishedElements]);
3179
+ }
3180
+ }
3181
+ }
3182
+ });
3161
3183
  }
3162
3184
  };
3163
3185
  };
@@ -3167,6 +3189,7 @@ var choice = ({
3167
3189
  description
3168
3190
  }) => {
3169
3191
  return {
3192
+ name: "choice",
3170
3193
  // JSON schema that describes an enumeration:
3171
3194
  responseFormat: Promise.resolve({
3172
3195
  type: "json",
@@ -3233,6 +3256,9 @@ var choice = ({
3233
3256
  }
3234
3257
  }
3235
3258
  }
3259
+ },
3260
+ createElementStreamTransform() {
3261
+ return void 0;
3236
3262
  }
3237
3263
  };
3238
3264
  };
@@ -3241,6 +3267,7 @@ var json = ({
3241
3267
  description
3242
3268
  } = {}) => {
3243
3269
  return {
3270
+ name: "json",
3244
3271
  responseFormat: Promise.resolve({
3245
3272
  type: "json",
3246
3273
  ...name16 != null && { name: name16 },
@@ -3272,6 +3299,9 @@ var json = ({
3272
3299
  return result.value === void 0 ? void 0 : { partial: result.value };
3273
3300
  }
3274
3301
  }
3302
+ },
3303
+ createElementStreamTransform() {
3304
+ return void 0;
3275
3305
  }
3276
3306
  };
3277
3307
  };
@@ -6936,6 +6966,16 @@ var DefaultStreamTextResult = class {
6936
6966
  )
6937
6967
  );
6938
6968
  }
6969
+ get elementStream() {
6970
+ var _a16, _b, _c;
6971
+ const transform = (_a16 = this.outputSpecification) == null ? void 0 : _a16.createElementStreamTransform();
6972
+ if (transform == null) {
6973
+ throw new import_provider24.UnsupportedFunctionalityError({
6974
+ functionality: `element streams in ${(_c = (_b = this.outputSpecification) == null ? void 0 : _b.name) != null ? _c : "text"} mode`
6975
+ });
6976
+ }
6977
+ return createAsyncIterableStream(this.teeStream().pipeThrough(transform));
6978
+ }
6939
6979
  get output() {
6940
6980
  return this.finalStep.then((step) => {
6941
6981
  var _a16;
@@ -10394,6 +10434,130 @@ function defaultSettingsMiddleware({
10394
10434
  };
10395
10435
  }
10396
10436
 
10437
+ // src/middleware/extract-json-middleware.ts
10438
+ function defaultTransform(text2) {
10439
+ return text2.replace(/^```(?:json)?\s*\n?/, "").replace(/\n?```\s*$/, "").trim();
10440
+ }
10441
+ function extractJsonMiddleware(options) {
10442
+ var _a16;
10443
+ const transform = (_a16 = options == null ? void 0 : options.transform) != null ? _a16 : defaultTransform;
10444
+ const hasCustomTransform = (options == null ? void 0 : options.transform) !== void 0;
10445
+ return {
10446
+ specificationVersion: "v3",
10447
+ wrapGenerate: async ({ doGenerate }) => {
10448
+ const { content, ...rest } = await doGenerate();
10449
+ const transformedContent = [];
10450
+ for (const part of content) {
10451
+ if (part.type !== "text") {
10452
+ transformedContent.push(part);
10453
+ continue;
10454
+ }
10455
+ transformedContent.push({
10456
+ ...part,
10457
+ text: transform(part.text)
10458
+ });
10459
+ }
10460
+ return { content: transformedContent, ...rest };
10461
+ },
10462
+ wrapStream: async ({ doStream }) => {
10463
+ const { stream, ...rest } = await doStream();
10464
+ const textBlocks = {};
10465
+ const SUFFIX_BUFFER_SIZE = 12;
10466
+ return {
10467
+ stream: stream.pipeThrough(
10468
+ new TransformStream({
10469
+ transform: (chunk, controller) => {
10470
+ if (chunk.type === "text-start") {
10471
+ textBlocks[chunk.id] = {
10472
+ startEvent: chunk,
10473
+ // Custom transforms need to buffer all content
10474
+ phase: hasCustomTransform ? "buffering" : "prefix",
10475
+ buffer: "",
10476
+ prefixStripped: false
10477
+ };
10478
+ return;
10479
+ }
10480
+ if (chunk.type === "text-delta") {
10481
+ const block = textBlocks[chunk.id];
10482
+ if (!block) {
10483
+ controller.enqueue(chunk);
10484
+ return;
10485
+ }
10486
+ block.buffer += chunk.delta;
10487
+ if (block.phase === "buffering") {
10488
+ return;
10489
+ }
10490
+ if (block.phase === "prefix") {
10491
+ if (block.buffer.length > 0 && !block.buffer.startsWith("`")) {
10492
+ block.phase = "streaming";
10493
+ controller.enqueue(block.startEvent);
10494
+ } else if (block.buffer.startsWith("```")) {
10495
+ if (block.buffer.includes("\n")) {
10496
+ const prefixMatch = block.buffer.match(/^```(?:json)?\s*\n/);
10497
+ if (prefixMatch) {
10498
+ block.buffer = block.buffer.slice(
10499
+ prefixMatch[0].length
10500
+ );
10501
+ block.prefixStripped = true;
10502
+ block.phase = "streaming";
10503
+ controller.enqueue(block.startEvent);
10504
+ } else {
10505
+ block.phase = "streaming";
10506
+ controller.enqueue(block.startEvent);
10507
+ }
10508
+ }
10509
+ } else if (block.buffer.length >= 3 && !block.buffer.startsWith("```")) {
10510
+ block.phase = "streaming";
10511
+ controller.enqueue(block.startEvent);
10512
+ }
10513
+ }
10514
+ if (block.phase === "streaming" && block.buffer.length > SUFFIX_BUFFER_SIZE) {
10515
+ const toStream = block.buffer.slice(0, -SUFFIX_BUFFER_SIZE);
10516
+ block.buffer = block.buffer.slice(-SUFFIX_BUFFER_SIZE);
10517
+ controller.enqueue({
10518
+ type: "text-delta",
10519
+ id: chunk.id,
10520
+ delta: toStream
10521
+ });
10522
+ }
10523
+ return;
10524
+ }
10525
+ if (chunk.type === "text-end") {
10526
+ const block = textBlocks[chunk.id];
10527
+ if (block) {
10528
+ if (block.phase === "prefix" || block.phase === "buffering") {
10529
+ controller.enqueue(block.startEvent);
10530
+ }
10531
+ let remaining = block.buffer;
10532
+ if (block.phase === "buffering") {
10533
+ remaining = transform(remaining);
10534
+ } else if (block.prefixStripped) {
10535
+ remaining = remaining.replace(/\n?```\s*$/, "").trimEnd();
10536
+ } else {
10537
+ remaining = transform(remaining);
10538
+ }
10539
+ if (remaining.length > 0) {
10540
+ controller.enqueue({
10541
+ type: "text-delta",
10542
+ id: chunk.id,
10543
+ delta: remaining
10544
+ });
10545
+ }
10546
+ controller.enqueue(chunk);
10547
+ delete textBlocks[chunk.id];
10548
+ return;
10549
+ }
10550
+ }
10551
+ controller.enqueue(chunk);
10552
+ }
10553
+ })
10554
+ ),
10555
+ ...rest
10556
+ };
10557
+ }
10558
+ };
10559
+ }
10560
+
10397
10561
  // src/util/get-potential-start-index.ts
10398
10562
  function getPotentialStartIndex(text2, searchedText) {
10399
10563
  if (searchedText.length === 0) {
@@ -12181,6 +12345,7 @@ var TextStreamChatTransport = class extends HttpChatTransport {
12181
12345
  experimental_generateImage,
12182
12346
  experimental_generateSpeech,
12183
12347
  experimental_transcribe,
12348
+ extractJsonMiddleware,
12184
12349
  extractReasoningMiddleware,
12185
12350
  gateway,
12186
12351
  generateId,