ai 3.3.15 → 3.3.16

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
@@ -1333,21 +1333,22 @@ function prepareResponseHeaders(init, {
1333
1333
  return headers;
1334
1334
  }
1335
1335
 
1336
- // core/generate-object/inject-json-schema-into-system.ts
1336
+ // core/generate-object/inject-json-instruction.ts
1337
1337
  var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
1338
1338
  var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
1339
- function injectJsonSchemaIntoSystem({
1340
- system,
1339
+ var DEFAULT_GENERIC_SUFFIX = "You MUST answer with JSON.";
1340
+ function injectJsonInstruction({
1341
+ prompt,
1341
1342
  schema,
1342
- schemaPrefix = DEFAULT_SCHEMA_PREFIX,
1343
- schemaSuffix = DEFAULT_SCHEMA_SUFFIX
1343
+ schemaPrefix = schema != null ? DEFAULT_SCHEMA_PREFIX : void 0,
1344
+ schemaSuffix = schema != null ? DEFAULT_SCHEMA_SUFFIX : DEFAULT_GENERIC_SUFFIX
1344
1345
  }) {
1345
1346
  return [
1346
- system,
1347
- system != null ? "" : null,
1348
- // add a newline if system is not null
1347
+ prompt != null && prompt.length > 0 ? prompt : void 0,
1348
+ prompt != null && prompt.length > 0 ? "" : void 0,
1349
+ // add a newline if prompt is not null
1349
1350
  schemaPrefix,
1350
- JSON.stringify(schema),
1351
+ schema != null ? JSON.stringify(schema) : void 0,
1351
1352
  schemaSuffix
1352
1353
  ].filter((line) => line != null).join("\n");
1353
1354
  }
@@ -1387,6 +1388,62 @@ var NoObjectGeneratedError = class extends AISDKError6 {
1387
1388
  };
1388
1389
  _a6 = symbol6;
1389
1390
 
1391
+ // core/generate-object/validate-object-generation-input.ts
1392
+ function validateObjectGenerationInput({
1393
+ output,
1394
+ mode,
1395
+ schema,
1396
+ schemaName,
1397
+ schemaDescription
1398
+ }) {
1399
+ if (output != null && output !== "object" && output !== "no-schema") {
1400
+ throw new InvalidArgumentError({
1401
+ parameter: "output",
1402
+ value: output,
1403
+ message: "Invalid output type."
1404
+ });
1405
+ }
1406
+ if (output === "no-schema") {
1407
+ if (mode === "auto" || mode === "tool") {
1408
+ throw new InvalidArgumentError({
1409
+ parameter: "mode",
1410
+ value: mode,
1411
+ message: 'Mode must be "json" for no-schema output.'
1412
+ });
1413
+ }
1414
+ if (schema != null) {
1415
+ throw new InvalidArgumentError({
1416
+ parameter: "schema",
1417
+ value: schema,
1418
+ message: "Schema is not supported for no-schema output."
1419
+ });
1420
+ }
1421
+ if (schemaDescription != null) {
1422
+ throw new InvalidArgumentError({
1423
+ parameter: "schemaDescription",
1424
+ value: schemaDescription,
1425
+ message: "Schema description is not supported for no-schema output."
1426
+ });
1427
+ }
1428
+ if (schemaName != null) {
1429
+ throw new InvalidArgumentError({
1430
+ parameter: "schemaName",
1431
+ value: schemaName,
1432
+ message: "Schema name is not supported for no-schema output."
1433
+ });
1434
+ }
1435
+ }
1436
+ if (output === "object") {
1437
+ if (schema == null) {
1438
+ throw new InvalidArgumentError({
1439
+ parameter: "schema",
1440
+ value: schema,
1441
+ message: "Schema is required for object output."
1442
+ });
1443
+ }
1444
+ }
1445
+ }
1446
+
1390
1447
  // core/generate-object/generate-object.ts
1391
1448
  async function generateObject({
1392
1449
  model,
@@ -1394,6 +1451,7 @@ async function generateObject({
1394
1451
  schemaName,
1395
1452
  schemaDescription,
1396
1453
  mode,
1454
+ output = "object",
1397
1455
  system,
1398
1456
  prompt,
1399
1457
  messages,
@@ -1404,13 +1462,23 @@ async function generateObject({
1404
1462
  ...settings
1405
1463
  }) {
1406
1464
  var _a12;
1465
+ validateObjectGenerationInput({
1466
+ output,
1467
+ mode,
1468
+ schema: inputSchema,
1469
+ schemaName,
1470
+ schemaDescription
1471
+ });
1472
+ if (output === "no-schema" && mode === void 0) {
1473
+ mode = "json";
1474
+ }
1407
1475
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
1408
1476
  model,
1409
1477
  telemetry,
1410
1478
  headers,
1411
1479
  settings: { ...settings, maxRetries }
1412
1480
  });
1413
- const schema = asSchema(inputSchema);
1481
+ const schema = inputSchema != null ? asSchema(inputSchema) : void 0;
1414
1482
  const tracer = getTracer({ isEnabled: (_a12 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a12 : false });
1415
1483
  return recordSpan({
1416
1484
  name: "ai.generateObject",
@@ -1426,11 +1494,10 @@ async function generateObject({
1426
1494
  "ai.prompt": {
1427
1495
  input: () => JSON.stringify({ system, prompt, messages })
1428
1496
  },
1429
- "ai.schema": {
1430
- input: () => JSON.stringify(schema.jsonSchema)
1431
- },
1497
+ "ai.schema": schema != null ? { input: () => JSON.stringify(schema.jsonSchema) } : void 0,
1432
1498
  "ai.schema.name": schemaName,
1433
1499
  "ai.schema.description": schemaDescription,
1500
+ "ai.settings.output": output,
1434
1501
  "ai.settings.mode": mode
1435
1502
  }
1436
1503
  }),
@@ -1450,8 +1517,8 @@ async function generateObject({
1450
1517
  switch (mode) {
1451
1518
  case "json": {
1452
1519
  const validatedPrompt = validatePrompt({
1453
- system: model.supportsStructuredOutputs ? system : injectJsonSchemaIntoSystem({
1454
- system,
1520
+ system: schema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs && schema != null ? system : injectJsonInstruction({
1521
+ prompt: system,
1455
1522
  schema: schema.jsonSchema
1456
1523
  }),
1457
1524
  prompt,
@@ -1493,7 +1560,7 @@ async function generateObject({
1493
1560
  const result2 = await model.doGenerate({
1494
1561
  mode: {
1495
1562
  type: "object-json",
1496
- schema: schema.jsonSchema,
1563
+ schema: schema == null ? void 0 : schema.jsonSchema,
1497
1564
  name: schemaName,
1498
1565
  description: schemaDescription
1499
1566
  },
@@ -1632,7 +1699,12 @@ async function generateObject({
1632
1699
  throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
1633
1700
  }
1634
1701
  }
1635
- const parseResult = safeParseJSON({ text: result, schema });
1702
+ const parseResult = safeParseJSON({
1703
+ text: result,
1704
+ // type casting required for `undefined` schema (no-schema mode),
1705
+ // in which case <T> is <JSONValue> as desired.
1706
+ schema
1707
+ });
1636
1708
  if (!parseResult.success) {
1637
1709
  throw parseResult.error;
1638
1710
  }
@@ -1768,6 +1840,7 @@ async function streamObject({
1768
1840
  schemaName,
1769
1841
  schemaDescription,
1770
1842
  mode,
1843
+ output = "object",
1771
1844
  system,
1772
1845
  prompt,
1773
1846
  messages,
@@ -1779,6 +1852,16 @@ async function streamObject({
1779
1852
  ...settings
1780
1853
  }) {
1781
1854
  var _a12;
1855
+ validateObjectGenerationInput({
1856
+ output,
1857
+ mode,
1858
+ schema: inputSchema,
1859
+ schemaName,
1860
+ schemaDescription
1861
+ });
1862
+ if (output === "no-schema" && mode === void 0) {
1863
+ mode = "json";
1864
+ }
1782
1865
  const baseTelemetryAttributes = getBaseTelemetryAttributes({
1783
1866
  model,
1784
1867
  telemetry,
@@ -1787,7 +1870,7 @@ async function streamObject({
1787
1870
  });
1788
1871
  const tracer = getTracer({ isEnabled: (_a12 = telemetry == null ? void 0 : telemetry.isEnabled) != null ? _a12 : false });
1789
1872
  const retry = retryWithExponentialBackoff({ maxRetries });
1790
- const schema = asSchema2(inputSchema);
1873
+ const schema = inputSchema != null ? asSchema2(inputSchema) : void 0;
1791
1874
  return recordSpan({
1792
1875
  name: "ai.streamObject",
1793
1876
  attributes: selectTelemetryAttributes({
@@ -1802,9 +1885,10 @@ async function streamObject({
1802
1885
  "ai.prompt": {
1803
1886
  input: () => JSON.stringify({ system, prompt, messages })
1804
1887
  },
1805
- "ai.schema": { input: () => JSON.stringify(schema.jsonSchema) },
1888
+ "ai.schema": schema != null ? { input: () => JSON.stringify(schema.jsonSchema) } : void 0,
1806
1889
  "ai.schema.name": schemaName,
1807
1890
  "ai.schema.description": schemaDescription,
1891
+ "ai.settings.output": output,
1808
1892
  "ai.settings.mode": mode
1809
1893
  }
1810
1894
  }),
@@ -1819,8 +1903,8 @@ async function streamObject({
1819
1903
  switch (mode) {
1820
1904
  case "json": {
1821
1905
  const validatedPrompt = validatePrompt({
1822
- system: model.supportsStructuredOutputs ? system : injectJsonSchemaIntoSystem({
1823
- system,
1906
+ system: schema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs && schema != null ? system : injectJsonInstruction({
1907
+ prompt: system,
1824
1908
  schema: schema.jsonSchema
1825
1909
  }),
1826
1910
  prompt,
@@ -1829,7 +1913,7 @@ async function streamObject({
1829
1913
  callOptions = {
1830
1914
  mode: {
1831
1915
  type: "object-json",
1832
- schema: schema.jsonSchema,
1916
+ schema: schema == null ? void 0 : schema.jsonSchema,
1833
1917
  name: schemaName,
1834
1918
  description: schemaDescription
1835
1919
  },
@@ -1951,6 +2035,8 @@ async function streamObject({
1951
2035
  stream: stream.pipeThrough(new TransformStream(transformer)),
1952
2036
  warnings,
1953
2037
  rawResponse,
2038
+ // type casting required for `undefined` schema (no-schema mode),
2039
+ // in which case <T> is <JSONValue> as desired.
1954
2040
  schema,
1955
2041
  onFinish,
1956
2042
  rootSpan,