ai 3.3.30 → 3.3.32

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
@@ -1461,7 +1461,7 @@ var objectOutputStrategy = (schema) => ({
1461
1461
  var arrayOutputStrategy = (schema) => {
1462
1462
  const { $schema, ...itemSchema } = schema.jsonSchema;
1463
1463
  return {
1464
- type: "array",
1464
+ type: "enum",
1465
1465
  // wrap in object that contains array of elements, since most LLMs will not
1466
1466
  // be able to generate an array directly:
1467
1467
  // possible future optimization: use arrays directly when model supports grammar-guided generation
@@ -1565,15 +1565,64 @@ var arrayOutputStrategy = (schema) => {
1565
1565
  }
1566
1566
  };
1567
1567
  };
1568
+ var enumOutputStrategy = (enumValues) => {
1569
+ return {
1570
+ type: "enum",
1571
+ // wrap in object that contains result, since most LLMs will not
1572
+ // be able to generate an enum value directly:
1573
+ // possible future optimization: use enums directly when model supports top-level enums
1574
+ jsonSchema: {
1575
+ $schema: "http://json-schema.org/draft-07/schema#",
1576
+ type: "object",
1577
+ properties: {
1578
+ result: { type: "string", enum: enumValues }
1579
+ },
1580
+ required: ["result"],
1581
+ additionalProperties: false
1582
+ },
1583
+ validateFinalResult(value) {
1584
+ if (!isJSONObject(value) || typeof value.result !== "string") {
1585
+ return {
1586
+ success: false,
1587
+ error: new TypeValidationError({
1588
+ value,
1589
+ cause: 'value must be an object that contains a string in the "result" property.'
1590
+ })
1591
+ };
1592
+ }
1593
+ const result = value.result;
1594
+ return enumValues.includes(result) ? { success: true, value: result } : {
1595
+ success: false,
1596
+ error: new TypeValidationError({
1597
+ value,
1598
+ cause: "value must be a string in the enum"
1599
+ })
1600
+ };
1601
+ },
1602
+ validatePartialResult() {
1603
+ throw new UnsupportedFunctionalityError({
1604
+ functionality: "partial results in enum mode"
1605
+ });
1606
+ },
1607
+ createElementStream() {
1608
+ throw new UnsupportedFunctionalityError({
1609
+ functionality: "element streams in enum mode"
1610
+ });
1611
+ }
1612
+ };
1613
+ };
1568
1614
  function getOutputStrategy({
1569
1615
  output,
1570
- schema
1616
+ schema,
1617
+ enumValues
1571
1618
  }) {
1572
1619
  switch (output) {
1573
1620
  case "object":
1574
1621
  return objectOutputStrategy(asSchema(schema));
1575
1622
  case "array":
1576
1623
  return arrayOutputStrategy(asSchema(schema));
1624
+ case "enum":
1625
+ return enumOutputStrategy(enumValues);
1577
1626
  case "no-schema":
1578
1627
  return noSchemaOutputStrategy;
1579
1628
  default: {
@@ -1589,9 +1638,10 @@ function validateObjectGenerationInput({
1589
1638
  mode,
1590
1639
  schema,
1591
1640
  schemaName,
1592
- schemaDescription
1641
+ schemaDescription,
1642
+ enumValues
1593
1643
  }) {
1594
- if (output != null && output !== "object" && output !== "array" && output !== "no-schema") {
1644
+ if (output != null && output !== "object" && output !== "array" && output !== "enum" && output !== "no-schema") {
1595
1645
  throw new InvalidArgumentError({
1596
1646
  parameter: "output",
1597
1647
  value: output,
@@ -1627,6 +1677,13 @@ function validateObjectGenerationInput({
1627
1677
  message: "Schema name is not supported for no-schema output."
1628
1678
  });
1629
1679
  }
1680
+ if (enumValues != null) {
1681
+ throw new InvalidArgumentError({
1682
+ parameter: "enumValues",
1683
+ value: enumValues,
1684
+ message: "Enum values are not supported for no-schema output."
1685
+ });
1686
+ }
1630
1687
  }
1631
1688
  if (output === "object") {
1632
1689
  if (schema == null) {
@@ -1636,6 +1693,13 @@ function validateObjectGenerationInput({
1636
1693
  message: "Schema is required for object output."
1637
1694
  });
1638
1695
  }
1696
+ if (enumValues != null) {
1697
+ throw new InvalidArgumentError({
1698
+ parameter: "enumValues",
1699
+ value: enumValues,
1700
+ message: "Enum values are not supported for object output."
1701
+ });
1702
+ }
1639
1703
  }
1640
1704
  if (output === "array") {
1641
1705
  if (schema == null) {
@@ -1645,6 +1709,52 @@ function validateObjectGenerationInput({
1645
1709
  message: "Element schema is required for array output."
1646
1710
  });
1647
1711
  }
1712
+ if (enumValues != null) {
1713
+ throw new InvalidArgumentError({
1714
+ parameter: "enumValues",
1715
+ value: enumValues,
1716
+ message: "Enum values are not supported for array output."
1717
+ });
1718
+ }
1719
+ }
1720
+ if (output === "enum") {
1721
+ if (schema != null) {
1722
+ throw new InvalidArgumentError({
1723
+ parameter: "schema",
1724
+ value: schema,
1725
+ message: "Schema is not supported for enum output."
1726
+ });
1727
+ }
1728
+ if (schemaDescription != null) {
1729
+ throw new InvalidArgumentError({
1730
+ parameter: "schemaDescription",
1731
+ value: schemaDescription,
1732
+ message: "Schema description is not supported for enum output."
1733
+ });
1734
+ }
1735
+ if (schemaName != null) {
1736
+ throw new InvalidArgumentError({
1737
+ parameter: "schemaName",
1738
+ value: schemaName,
1739
+ message: "Schema name is not supported for enum output."
1740
+ });
1741
+ }
1742
+ if (enumValues == null) {
1743
+ throw new InvalidArgumentError({
1744
+ parameter: "enumValues",
1745
+ value: enumValues,
1746
+ message: "Enum values are required for enum output."
1747
+ });
1748
+ }
1749
+ for (const value of enumValues) {
1750
+ if (typeof value !== "string") {
1751
+ throw new InvalidArgumentError({
1752
+ parameter: "enumValues",
1753
+ value,
1754
+ message: "Enum values must be strings."
1755
+ });
1756
+ }
1757
+ }
1648
1758
  }
1649
1759
  }
1650
1760
 
@@ -1652,6 +1762,8 @@ function validateObjectGenerationInput({
1652
1762
  var originalGenerateId = createIdGenerator({ prefix: "aiobj-", length: 24 });
1653
1763
  async function generateObject({
1654
1764
  model,
1765
+ enum: enumValues,
1766
+ // rename bc enum is reserved by typescript
1655
1767
  schema: inputSchema,
1656
1768
  schemaName,
1657
1769
  schemaDescription,
@@ -1664,6 +1776,7 @@ async function generateObject({
1664
1776
  abortSignal,
1665
1777
  headers,
1666
1778
  experimental_telemetry: telemetry,
1779
+ experimental_providerMetadata: providerMetadata,
1667
1780
  _internal: {
1668
1781
  generateId: generateId3 = originalGenerateId,
1669
1782
  currentDate = () => /* @__PURE__ */ new Date()
@@ -1676,9 +1789,14 @@ async function generateObject({
1676
1789
  mode,
1677
1790
  schema: inputSchema,
1678
1791
  schemaName,
1679
- schemaDescription
1792
+ schemaDescription,
1793
+ enumValues
1794
+ });
1795
+ const outputStrategy = getOutputStrategy({
1796
+ output,
1797
+ schema: inputSchema,
1798
+ enumValues
1680
1799
  });
1681
- const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
1682
1800
  if (outputStrategy.type === "no-schema" && mode === void 0) {
1683
1801
  mode = "json";
1684
1802
  }
@@ -1723,7 +1841,7 @@ async function generateObject({
1723
1841
  let rawResponse;
1724
1842
  let response;
1725
1843
  let logprobs;
1726
- let providerMetadata;
1844
+ let resultProviderMetadata;
1727
1845
  switch (mode) {
1728
1846
  case "json": {
1729
1847
  const validatedPrompt = validatePrompt({
@@ -1781,6 +1899,7 @@ async function generateObject({
1781
1899
  ...prepareCallSettings(settings),
1782
1900
  inputFormat,
1783
1901
  prompt: promptMessages,
1902
+ providerMetadata,
1784
1903
  abortSignal,
1785
1904
  headers
1786
1905
  });
@@ -1825,7 +1944,7 @@ async function generateObject({
1825
1944
  warnings = generateResult.warnings;
1826
1945
  rawResponse = generateResult.rawResponse;
1827
1946
  logprobs = generateResult.logprobs;
1828
- providerMetadata = generateResult.providerMetadata;
1947
+ resultProviderMetadata = generateResult.providerMetadata;
1829
1948
  response = generateResult.responseData;
1830
1949
  break;
1831
1950
  }
@@ -1885,6 +2004,7 @@ async function generateObject({
1885
2004
  ...prepareCallSettings(settings),
1886
2005
  inputFormat,
1887
2006
  prompt: promptMessages,
2007
+ providerMetadata,
1888
2008
  abortSignal,
1889
2009
  headers
1890
2010
  });
@@ -1930,7 +2050,7 @@ async function generateObject({
1930
2050
  warnings = generateResult.warnings;
1931
2051
  rawResponse = generateResult.rawResponse;
1932
2052
  logprobs = generateResult.logprobs;
1933
- providerMetadata = generateResult.providerMetadata;
2053
+ resultProviderMetadata = generateResult.providerMetadata;
1934
2054
  response = generateResult.responseData;
1935
2055
  break;
1936
2056
  }
@@ -1982,7 +2102,7 @@ async function generateObject({
1982
2102
  headers: rawResponse == null ? void 0 : rawResponse.headers
1983
2103
  },
1984
2104
  logprobs,
1985
- providerMetadata
2105
+ providerMetadata: resultProviderMetadata
1986
2106
  });
1987
2107
  }
1988
2108
  });
@@ -2143,6 +2263,7 @@ async function streamObject({
2143
2263
  abortSignal,
2144
2264
  headers,
2145
2265
  experimental_telemetry: telemetry,
2266
+ experimental_providerMetadata: providerMetadata,
2146
2267
  onFinish,
2147
2268
  _internal: {
2148
2269
  generateId: generateId3 = originalGenerateId2,
@@ -2223,6 +2344,7 @@ async function streamObject({
2223
2344
  prompt: validatedPrompt,
2224
2345
  modelSupportsImageUrls: model.supportsImageUrls
2225
2346
  }),
2347
+ providerMetadata,
2226
2348
  abortSignal,
2227
2349
  headers
2228
2350
  };
@@ -2264,6 +2386,7 @@ async function streamObject({
2264
2386
  prompt: validatedPrompt,
2265
2387
  modelSupportsImageUrls: model.supportsImageUrls
2266
2388
  }),
2389
+ providerMetadata,
2267
2390
  abortSignal,
2268
2391
  headers
2269
2392
  };
@@ -2826,6 +2949,7 @@ async function generateText({
2826
2949
  maxAutomaticRoundtrips = 0,
2827
2950
  maxToolRoundtrips = maxAutomaticRoundtrips,
2828
2951
  experimental_telemetry: telemetry,
2952
+ experimental_providerMetadata: providerMetadata,
2829
2953
  _internal: {
2830
2954
  generateId: generateId3 = originalGenerateId3,
2831
2955
  currentDate = () => /* @__PURE__ */ new Date()
@@ -2923,6 +3047,7 @@ async function generateText({
2923
3047
  ...callSettings,
2924
3048
  inputFormat: currentInputFormat,
2925
3049
  prompt: promptMessages,
3050
+ providerMetadata,
2926
3051
  abortSignal,
2927
3052
  headers
2928
3053
  });
@@ -3494,6 +3619,7 @@ async function streamText({
3494
3619
  headers,
3495
3620
  maxToolRoundtrips = 0,
3496
3621
  experimental_telemetry: telemetry,
3622
+ experimental_providerMetadata: providerMetadata,
3497
3623
  experimental_toolCallStreaming: toolCallStreaming = false,
3498
3624
  onChunk,
3499
3625
  onFinish,
@@ -3580,6 +3706,7 @@ async function streamText({
3580
3706
  ...prepareCallSettings(settings),
3581
3707
  inputFormat: promptType,
3582
3708
  prompt: promptMessages2,
3709
+ providerMetadata,
3583
3710
  abortSignal,
3584
3711
  headers
3585
3712
  })