ai 5.0.287 → 5.0.289

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
@@ -758,7 +758,7 @@ import {
758
758
  } from "@ai-sdk/provider-utils";
759
759
 
760
760
  // src/version.ts
761
- var VERSION = true ? "5.0.287" : "0.0.0-test";
761
+ var VERSION = true ? "5.0.289" : "0.0.0-test";
762
762
 
763
763
  // src/util/download/download.ts
764
764
  var download = async ({
@@ -7296,8 +7296,117 @@ import {
7296
7296
  } from "@ai-sdk/provider";
7297
7297
  import {
7298
7298
  asSchema as asSchema3,
7299
+ safeValidateTypes as safeValidateTypes4
7300
+ } from "@ai-sdk/provider-utils";
7301
+
7302
+ // src/util/array-output-stream.ts
7303
+ import {
7304
+ safeParseJSON as safeParseJSON3,
7299
7305
  safeValidateTypes as safeValidateTypes3
7300
7306
  } from "@ai-sdk/provider-utils";
7307
+ function createArrayOutputStreamParser(schema, skipInvalidElements) {
7308
+ let phase = 0 /* Prefix */;
7309
+ let prefix = "";
7310
+ let elementText = "";
7311
+ let depth = 0;
7312
+ let inString = false;
7313
+ let escaped = false;
7314
+ let afterComma = false;
7315
+ let pending = [];
7316
+ const elements = [];
7317
+ let publishedCount;
7318
+ async function finishElement() {
7319
+ const parsed = await safeParseJSON3({ text: elementText });
7320
+ if (!parsed.success) {
7321
+ return false;
7322
+ }
7323
+ const validated = await safeValidateTypes3({ value: parsed.value, schema });
7324
+ if (validated.success) {
7325
+ pending = [validated.value];
7326
+ } else if (!skipInvalidElements) {
7327
+ return false;
7328
+ }
7329
+ elementText = "";
7330
+ return true;
7331
+ }
7332
+ function publishPending() {
7333
+ elements.push(...pending);
7334
+ pending = [];
7335
+ }
7336
+ return async (textDelta) => {
7337
+ for (const char of textDelta) {
7338
+ if (phase === 0 /* Prefix */) {
7339
+ prefix += char;
7340
+ if (char === "[") {
7341
+ if (!/^\s*\{\s*"elements"\s*:\s*\[$/.test(prefix)) {
7342
+ return "fallback";
7343
+ }
7344
+ phase = 1 /* Elements */;
7345
+ } else if (prefix.length > 256) {
7346
+ return "fallback";
7347
+ }
7348
+ continue;
7349
+ }
7350
+ if (phase === 2 /* Suffix */ || phase === 3 /* Done */) {
7351
+ if (/\s/.test(char)) {
7352
+ continue;
7353
+ }
7354
+ if (phase === 2 /* Suffix */ && char === "}") {
7355
+ publishPending();
7356
+ phase = 3 /* Done */;
7357
+ continue;
7358
+ }
7359
+ return "fallback";
7360
+ }
7361
+ if (!inString && depth === 0 && (char === "," || char === "]")) {
7362
+ if (elementText.length > 0) {
7363
+ if (!await finishElement()) {
7364
+ return "fallback";
7365
+ }
7366
+ } else if (char === "," || afterComma) {
7367
+ return "fallback";
7368
+ }
7369
+ afterComma = char === ",";
7370
+ if (char === "]") {
7371
+ phase = 2 /* Suffix */;
7372
+ }
7373
+ continue;
7374
+ }
7375
+ if (elementText.length === 0) {
7376
+ if (/\s/.test(char)) {
7377
+ continue;
7378
+ }
7379
+ publishPending();
7380
+ afterComma = false;
7381
+ }
7382
+ elementText += char;
7383
+ if (inString) {
7384
+ if (escaped) {
7385
+ escaped = false;
7386
+ } else if (char === "\\") {
7387
+ escaped = true;
7388
+ } else if (char === '"') {
7389
+ inString = false;
7390
+ }
7391
+ } else if (char === '"') {
7392
+ inString = true;
7393
+ } else if (char === "{" || char === "[") {
7394
+ depth++;
7395
+ } else if (char === "}" || char === "]") {
7396
+ depth--;
7397
+ }
7398
+ }
7399
+ if (phase === 0 /* Prefix */ || publishedCount === elements.length) {
7400
+ return void 0;
7401
+ }
7402
+ const previousCount = publishedCount != null ? publishedCount : 0;
7403
+ const delta = (publishedCount == null ? "[" : "") + (previousCount > 0 && elements.length > previousCount ? "," : "") + elements.slice(previousCount).map((value) => JSON.stringify(value)).join(",") + (phase === 3 /* Done */ ? "]" : "");
7404
+ publishedCount = elements.length;
7405
+ return { partial: elements.slice(), textDelta: delta };
7406
+ };
7407
+ }
7408
+
7409
+ // src/generate-object/output-strategy.ts
7301
7410
  var noSchemaOutputStrategy = {
7302
7411
  type: "no-schema",
7303
7412
  jsonSchema: void 0,
@@ -7336,7 +7445,7 @@ var objectOutputStrategy = (schema) => ({
7336
7445
  };
7337
7446
  },
7338
7447
  async validateFinalResult(value) {
7339
- return safeValidateTypes3({ value, schema });
7448
+ return safeValidateTypes4({ value, schema });
7340
7449
  },
7341
7450
  createElementStream() {
7342
7451
  throw new UnsupportedFunctionalityError2({
@@ -7348,6 +7457,7 @@ var arrayOutputStrategy = (schema) => {
7348
7457
  const { $schema, ...itemSchema } = schema.jsonSchema;
7349
7458
  return {
7350
7459
  type: "array",
7460
+ createArrayStreamParser: () => createArrayOutputStreamParser(schema, false),
7351
7461
  // wrap in object that contains array of elements, since most LLMs will not
7352
7462
  // be able to generate an array directly:
7353
7463
  // possible future optimization: use arrays directly when model supports grammar-guided generation
@@ -7380,7 +7490,7 @@ var arrayOutputStrategy = (schema) => {
7380
7490
  const resultArray = [];
7381
7491
  for (let i = 0; i < inputArray.length; i++) {
7382
7492
  const element = inputArray[i];
7383
- const result = await safeValidateTypes3({ value: element, schema });
7493
+ const result = await safeValidateTypes4({ value: element, schema });
7384
7494
  if (i === inputArray.length - 1 && !isFinalDelta) {
7385
7495
  continue;
7386
7496
  }
@@ -7422,7 +7532,7 @@ var arrayOutputStrategy = (schema) => {
7422
7532
  const inputArray = value.elements;
7423
7533
  const resultArray = [];
7424
7534
  for (const element of inputArray) {
7425
- const result = await safeValidateTypes3({ value: element, schema });
7535
+ const result = await safeValidateTypes4({ value: element, schema });
7426
7536
  if (!result.success) {
7427
7537
  return result;
7428
7538
  }
@@ -7557,9 +7667,9 @@ function getOutputStrategy({
7557
7667
 
7558
7668
  // src/generate-object/parse-and-validate-object-result.ts
7559
7669
  import { JSONParseError as JSONParseError2, TypeValidationError as TypeValidationError3 } from "@ai-sdk/provider";
7560
- import { safeParseJSON as safeParseJSON3 } from "@ai-sdk/provider-utils";
7670
+ import { safeParseJSON as safeParseJSON4 } from "@ai-sdk/provider-utils";
7561
7671
  async function parseAndValidateObjectResult(result, outputStrategy, context) {
7562
- const parseResult = await safeParseJSON3({ text: result });
7672
+ const parseResult = await safeParseJSON4({ text: result });
7563
7673
  if (!parseResult.success) {
7564
7674
  throw new NoObjectGeneratedError({
7565
7675
  message: "No object generated: could not parse the response.",
@@ -8311,6 +8421,7 @@ var DefaultStreamObjectResult = class {
8311
8421
  endWhenDone: false,
8312
8422
  endOnError: true,
8313
8423
  fn: async (rootSpan) => {
8424
+ var _a17;
8314
8425
  const standardizedPrompt = await standardizePrompt({
8315
8426
  system,
8316
8427
  prompt,
@@ -8401,6 +8512,7 @@ var DefaultStreamObjectResult = class {
8401
8512
  let object2;
8402
8513
  let error;
8403
8514
  let terminalError;
8515
+ let arrayStreamParser = (_a17 = outputStrategy.createArrayStreamParser) == null ? void 0 : _a17.call(outputStrategy);
8404
8516
  let accumulatedText = "";
8405
8517
  let textDelta = "";
8406
8518
  let fullResponse = {
@@ -8415,7 +8527,7 @@ var DefaultStreamObjectResult = class {
8415
8527
  const transformedStream = stream.pipeThrough(new TransformStream(transformer)).pipeThrough(
8416
8528
  new TransformStream({
8417
8529
  async transform(chunk, controller) {
8418
- var _a17, _b, _c;
8530
+ var _a18, _b, _c;
8419
8531
  if (typeof chunk === "object" && chunk.type === "stream-start") {
8420
8532
  warnings = chunk.warnings;
8421
8533
  return;
@@ -8433,6 +8545,26 @@ var DefaultStreamObjectResult = class {
8433
8545
  if (typeof chunk === "string") {
8434
8546
  accumulatedText += chunk;
8435
8547
  textDelta += chunk;
8548
+ if (arrayStreamParser != null) {
8549
+ const result = await arrayStreamParser(chunk);
8550
+ if (result !== "fallback") {
8551
+ if (result != null) {
8552
+ latestObject = result.partial;
8553
+ controller.enqueue({
8554
+ type: "object",
8555
+ object: latestObject
8556
+ });
8557
+ controller.enqueue({
8558
+ type: "text-delta",
8559
+ textDelta: result.textDelta
8560
+ });
8561
+ textDelta = "";
8562
+ isFirstDelta = false;
8563
+ }
8564
+ return;
8565
+ }
8566
+ arrayStreamParser = void 0;
8567
+ }
8436
8568
  const { value: currentObjectJson, state: parseState } = await parsePartialJson(accumulatedText);
8437
8569
  if (currentObjectJson !== void 0 && !isDeepEqualData(latestObjectJson, currentObjectJson)) {
8438
8570
  const validationResult = await outputStrategy.validatePartialResult({
@@ -8465,7 +8597,7 @@ var DefaultStreamObjectResult = class {
8465
8597
  switch (chunk.type) {
8466
8598
  case "response-metadata": {
8467
8599
  fullResponse = {
8468
- id: (_a17 = chunk.id) != null ? _a17 : fullResponse.id,
8600
+ id: (_a18 = chunk.id) != null ? _a18 : fullResponse.id,
8469
8601
  timestamp: (_b = chunk.timestamp) != null ? _b : fullResponse.timestamp,
8470
8602
  modelId: (_c = chunk.modelId) != null ? _c : fullResponse.modelId
8471
8603
  };
@@ -8844,8 +8976,8 @@ __export(output_exports, {
8844
8976
  });
8845
8977
  import {
8846
8978
  asSchema as asSchema4,
8847
- safeParseJSON as safeParseJSON4,
8848
- safeValidateTypes as safeValidateTypes4
8979
+ safeParseJSON as safeParseJSON5,
8980
+ safeValidateTypes as safeValidateTypes5
8849
8981
  } from "@ai-sdk/provider-utils";
8850
8982
  var text = () => ({
8851
8983
  type: "text",
@@ -8886,7 +9018,7 @@ var object = ({
8886
9018
  }
8887
9019
  },
8888
9020
  async parseOutput({ text: text2 }, context) {
8889
- const parseResult = await safeParseJSON4({ text: text2 });
9021
+ const parseResult = await safeParseJSON5({ text: text2 });
8890
9022
  if (!parseResult.success) {
8891
9023
  throw new NoObjectGeneratedError({
8892
9024
  message: "No object generated: could not parse the response.",
@@ -8897,7 +9029,7 @@ var object = ({
8897
9029
  finishReason: context.finishReason
8898
9030
  });
8899
9031
  }
8900
- const validationResult = await safeValidateTypes4({
9032
+ const validationResult = await safeValidateTypes5({
8901
9033
  value: parseResult.value,
8902
9034
  schema
8903
9035
  });
@@ -10660,7 +10792,7 @@ var TextStreamChatTransport = class extends HttpChatTransport {
10660
10792
  import { TypeValidationError as TypeValidationError4 } from "@ai-sdk/provider";
10661
10793
  import {
10662
10794
  lazyValidator as lazyValidator2,
10663
- safeValidateTypes as safeValidateTypes5,
10795
+ safeValidateTypes as safeValidateTypes6,
10664
10796
  validateTypes as validateTypes2,
10665
10797
  zodSchema as zodSchema2
10666
10798
  } from "@ai-sdk/provider-utils";
@@ -11000,7 +11132,7 @@ async function safeValidateUIMessages({
11000
11132
  let dynamicToolPart;
11001
11133
  if (toolPart.state === "output-error") {
11002
11134
  if (toolPart.input !== void 0) {
11003
- const result = await safeValidateTypes5({
11135
+ const result = await safeValidateTypes6({
11004
11136
  value: toolPart.input,
11005
11137
  schema: tool2.inputSchema
11006
11138
  });
@@ -11009,7 +11141,7 @@ async function safeValidateUIMessages({
11009
11141
  }
11010
11142
  }
11011
11143
  } else if (toolPart.state === "output-available") {
11012
- const result = await safeValidateTypes5({
11144
+ const result = await safeValidateTypes6({
11013
11145
  value: toolPart.input,
11014
11146
  schema: tool2.inputSchema
11015
11147
  });