ai 6.0.25 → 6.0.27

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
@@ -921,7 +921,7 @@ import {
921
921
  } from "@ai-sdk/provider-utils";
922
922
 
923
923
  // src/version.ts
924
- var VERSION = true ? "6.0.25" : "0.0.0-test";
924
+ var VERSION = true ? "6.0.27" : "0.0.0-test";
925
925
 
926
926
  // src/util/download/download.ts
927
927
  var download = async ({ url }) => {
@@ -10384,6 +10384,130 @@ function defaultSettingsMiddleware({
10384
10384
  };
10385
10385
  }
10386
10386
 
10387
+ // src/middleware/extract-json-middleware.ts
10388
+ function defaultTransform(text2) {
10389
+ return text2.replace(/^```(?:json)?\s*\n?/, "").replace(/\n?```\s*$/, "").trim();
10390
+ }
10391
+ function extractJsonMiddleware(options) {
10392
+ var _a16;
10393
+ const transform = (_a16 = options == null ? void 0 : options.transform) != null ? _a16 : defaultTransform;
10394
+ const hasCustomTransform = (options == null ? void 0 : options.transform) !== void 0;
10395
+ return {
10396
+ specificationVersion: "v3",
10397
+ wrapGenerate: async ({ doGenerate }) => {
10398
+ const { content, ...rest } = await doGenerate();
10399
+ const transformedContent = [];
10400
+ for (const part of content) {
10401
+ if (part.type !== "text") {
10402
+ transformedContent.push(part);
10403
+ continue;
10404
+ }
10405
+ transformedContent.push({
10406
+ ...part,
10407
+ text: transform(part.text)
10408
+ });
10409
+ }
10410
+ return { content: transformedContent, ...rest };
10411
+ },
10412
+ wrapStream: async ({ doStream }) => {
10413
+ const { stream, ...rest } = await doStream();
10414
+ const textBlocks = {};
10415
+ const SUFFIX_BUFFER_SIZE = 12;
10416
+ return {
10417
+ stream: stream.pipeThrough(
10418
+ new TransformStream({
10419
+ transform: (chunk, controller) => {
10420
+ if (chunk.type === "text-start") {
10421
+ textBlocks[chunk.id] = {
10422
+ startEvent: chunk,
10423
+ // Custom transforms need to buffer all content
10424
+ phase: hasCustomTransform ? "buffering" : "prefix",
10425
+ buffer: "",
10426
+ prefixStripped: false
10427
+ };
10428
+ return;
10429
+ }
10430
+ if (chunk.type === "text-delta") {
10431
+ const block = textBlocks[chunk.id];
10432
+ if (!block) {
10433
+ controller.enqueue(chunk);
10434
+ return;
10435
+ }
10436
+ block.buffer += chunk.delta;
10437
+ if (block.phase === "buffering") {
10438
+ return;
10439
+ }
10440
+ if (block.phase === "prefix") {
10441
+ if (block.buffer.length > 0 && !block.buffer.startsWith("`")) {
10442
+ block.phase = "streaming";
10443
+ controller.enqueue(block.startEvent);
10444
+ } else if (block.buffer.startsWith("```")) {
10445
+ if (block.buffer.includes("\n")) {
10446
+ const prefixMatch = block.buffer.match(/^```(?:json)?\s*\n/);
10447
+ if (prefixMatch) {
10448
+ block.buffer = block.buffer.slice(
10449
+ prefixMatch[0].length
10450
+ );
10451
+ block.prefixStripped = true;
10452
+ block.phase = "streaming";
10453
+ controller.enqueue(block.startEvent);
10454
+ } else {
10455
+ block.phase = "streaming";
10456
+ controller.enqueue(block.startEvent);
10457
+ }
10458
+ }
10459
+ } else if (block.buffer.length >= 3 && !block.buffer.startsWith("```")) {
10460
+ block.phase = "streaming";
10461
+ controller.enqueue(block.startEvent);
10462
+ }
10463
+ }
10464
+ if (block.phase === "streaming" && block.buffer.length > SUFFIX_BUFFER_SIZE) {
10465
+ const toStream = block.buffer.slice(0, -SUFFIX_BUFFER_SIZE);
10466
+ block.buffer = block.buffer.slice(-SUFFIX_BUFFER_SIZE);
10467
+ controller.enqueue({
10468
+ type: "text-delta",
10469
+ id: chunk.id,
10470
+ delta: toStream
10471
+ });
10472
+ }
10473
+ return;
10474
+ }
10475
+ if (chunk.type === "text-end") {
10476
+ const block = textBlocks[chunk.id];
10477
+ if (block) {
10478
+ if (block.phase === "prefix" || block.phase === "buffering") {
10479
+ controller.enqueue(block.startEvent);
10480
+ }
10481
+ let remaining = block.buffer;
10482
+ if (block.phase === "buffering") {
10483
+ remaining = transform(remaining);
10484
+ } else if (block.prefixStripped) {
10485
+ remaining = remaining.replace(/\n?```\s*$/, "").trimEnd();
10486
+ } else {
10487
+ remaining = transform(remaining);
10488
+ }
10489
+ if (remaining.length > 0) {
10490
+ controller.enqueue({
10491
+ type: "text-delta",
10492
+ id: chunk.id,
10493
+ delta: remaining
10494
+ });
10495
+ }
10496
+ controller.enqueue(chunk);
10497
+ delete textBlocks[chunk.id];
10498
+ return;
10499
+ }
10500
+ }
10501
+ controller.enqueue(chunk);
10502
+ }
10503
+ })
10504
+ ),
10505
+ ...rest
10506
+ };
10507
+ }
10508
+ };
10509
+ }
10510
+
10387
10511
  // src/util/get-potential-start-index.ts
10388
10512
  function getPotentialStartIndex(text2, searchedText) {
10389
10513
  if (searchedText.length === 0) {
@@ -12185,6 +12309,7 @@ export {
12185
12309
  experimental_generateImage,
12186
12310
  generateSpeech as experimental_generateSpeech,
12187
12311
  transcribe as experimental_transcribe,
12312
+ extractJsonMiddleware,
12188
12313
  extractReasoningMiddleware,
12189
12314
  gateway2 as gateway,
12190
12315
  generateId,