ai 3.3.30 → 3.3.31

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,
@@ -1676,9 +1788,14 @@ async function generateObject({
1676
1788
  mode,
1677
1789
  schema: inputSchema,
1678
1790
  schemaName,
1679
- schemaDescription
1791
+ schemaDescription,
1792
+ enumValues
1793
+ });
1794
+ const outputStrategy = getOutputStrategy({
1795
+ output,
1796
+ schema: inputSchema,
1797
+ enumValues
1680
1798
  });
1681
- const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
1682
1799
  if (outputStrategy.type === "no-schema" && mode === void 0) {
1683
1800
  mode = "json";
1684
1801
  }