@ai-sdk-tool/parser 3.3.0 → 3.3.2

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.
@@ -850,6 +850,13 @@ function generateId() {
850
850
  return Math.random().toString(36).substring(2, 15);
851
851
  }
852
852
 
853
+ // src/core/utils/protocol-utils.ts
854
+ function addTextSegment(text, processedElements) {
855
+ if (text.trim()) {
856
+ processedElements.push({ type: "text", text });
857
+ }
858
+ }
859
+
853
860
  // src/core/utils/regex.ts
854
861
  function escapeRegExp(literal) {
855
862
  return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -881,11 +888,6 @@ function processToolCallJson(toolCallJson, fullMatch, processedElements, options
881
888
  processedElements.push({ type: "text", text: fullMatch });
882
889
  }
883
890
  }
884
- function addTextSegment(text, processedElements) {
885
- if (text.trim()) {
886
- processedElements.push({ type: "text", text });
887
- }
888
- }
889
891
  function processMatchedToolCall(context) {
890
892
  const { match, text, currentIndex, processedElements, options } = context;
891
893
  const startIndex = match.index;
@@ -1557,6 +1559,153 @@ function getSchemaType(schema) {
1557
1559
  }
1558
1560
  return;
1559
1561
  }
1562
+ function schemaAllowsPropertyViaCombinators(s, key, depth) {
1563
+ const anyOfValues = s.anyOf;
1564
+ const oneOfValues = s.oneOf;
1565
+ const allOfValues = s.allOf;
1566
+ let hasCombinator = false;
1567
+ let anyOfAllows = true;
1568
+ let oneOfAllows = true;
1569
+ let allOfAllows = true;
1570
+ if (Array.isArray(anyOfValues)) {
1571
+ hasCombinator = true;
1572
+ anyOfAllows = anyOfValues.some(
1573
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1574
+ );
1575
+ }
1576
+ if (Array.isArray(oneOfValues)) {
1577
+ hasCombinator = true;
1578
+ oneOfAllows = oneOfValues.some(
1579
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1580
+ );
1581
+ }
1582
+ if (Array.isArray(allOfValues)) {
1583
+ hasCombinator = true;
1584
+ allOfAllows = allOfValues.every(
1585
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1586
+ );
1587
+ }
1588
+ if (!hasCombinator) {
1589
+ return false;
1590
+ }
1591
+ return anyOfAllows && oneOfAllows && allOfAllows;
1592
+ }
1593
+ function schemaHasPropertyDirectly(s, key) {
1594
+ const props = s.properties;
1595
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] !== false) {
1596
+ return true;
1597
+ }
1598
+ const required = s.required;
1599
+ if (Array.isArray(required) && required.includes(key)) {
1600
+ return true;
1601
+ }
1602
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1603
+ return patternSchemas.some((schema) => schema !== false);
1604
+ }
1605
+ function schemaHasPropertyViaAdditional(s) {
1606
+ const additional = s.additionalProperties;
1607
+ if (additional === true || additional && typeof additional === "object" && !Array.isArray(additional)) {
1608
+ return true;
1609
+ }
1610
+ if (Object.hasOwn(s, "additionalProperties")) {
1611
+ return false;
1612
+ }
1613
+ const type = s.type;
1614
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
1615
+ const hasObjectKeywords = s.properties && typeof s.properties === "object" && !Array.isArray(s.properties) || s.patternProperties && typeof s.patternProperties === "object" && !Array.isArray(s.patternProperties) || Array.isArray(s.required) && s.required.length > 0;
1616
+ return !!(isObjectType || hasObjectKeywords);
1617
+ }
1618
+ function schemaDisallowsPropertyDirectly(s, key) {
1619
+ const props = s.properties;
1620
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] === false) {
1621
+ return true;
1622
+ }
1623
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1624
+ return patternSchemas.some((schema) => schema === false);
1625
+ }
1626
+ function schemaHasProperty(schema, key, depth = 0) {
1627
+ if (depth > 5) {
1628
+ return true;
1629
+ }
1630
+ const unwrapped = unwrapJsonSchema(schema);
1631
+ if (schemaIsUnconstrained(unwrapped)) {
1632
+ return true;
1633
+ }
1634
+ if (!unwrapped || typeof unwrapped !== "object") {
1635
+ return false;
1636
+ }
1637
+ const s = unwrapped;
1638
+ if (schemaDisallowsPropertyDirectly(s, key)) {
1639
+ return false;
1640
+ }
1641
+ if (schemaHasPropertyDirectly(s, key)) {
1642
+ return true;
1643
+ }
1644
+ if (schemaHasPropertyViaAdditional(s)) {
1645
+ return true;
1646
+ }
1647
+ return schemaAllowsPropertyViaCombinators(s, key, depth);
1648
+ }
1649
+ function schemaIsUnconstrained(schema) {
1650
+ const unwrapped = unwrapJsonSchema(schema);
1651
+ if (unwrapped == null || unwrapped === true) {
1652
+ return true;
1653
+ }
1654
+ if (typeof unwrapped !== "object" || Array.isArray(unwrapped)) {
1655
+ return false;
1656
+ }
1657
+ return Object.keys(unwrapped).length === 0;
1658
+ }
1659
+ function getPatternSchemasForKey(patternProperties, key) {
1660
+ if (!patternProperties || typeof patternProperties !== "object" || Array.isArray(patternProperties)) {
1661
+ return [];
1662
+ }
1663
+ const schemas = [];
1664
+ for (const [pattern, schema] of Object.entries(
1665
+ patternProperties
1666
+ )) {
1667
+ try {
1668
+ const regex = new RegExp(pattern);
1669
+ if (regex.test(key)) {
1670
+ schemas.push(schema);
1671
+ }
1672
+ } catch (e) {
1673
+ }
1674
+ }
1675
+ return schemas;
1676
+ }
1677
+ function coerceValueForKey(value, key, unwrapped) {
1678
+ const schemas = [];
1679
+ const props = unwrapped.properties;
1680
+ if (props && Object.hasOwn(props, key)) {
1681
+ schemas.push(props[key]);
1682
+ }
1683
+ const patternSchemas = getPatternSchemasForKey(
1684
+ unwrapped.patternProperties,
1685
+ key
1686
+ );
1687
+ if (patternSchemas.length > 0) {
1688
+ schemas.push(...patternSchemas);
1689
+ }
1690
+ if (schemas.length > 0) {
1691
+ let out = value;
1692
+ for (const schema of schemas) {
1693
+ if (typeof schema === "boolean") {
1694
+ continue;
1695
+ }
1696
+ out = coerceBySchema(out, schema);
1697
+ }
1698
+ return out;
1699
+ }
1700
+ const additional = unwrapped.additionalProperties;
1701
+ if (additional && typeof additional === "object" && !Array.isArray(additional)) {
1702
+ return coerceBySchema(value, additional);
1703
+ }
1704
+ if (additional === true || additional === false) {
1705
+ return value;
1706
+ }
1707
+ return coerceBySchema(value, void 0);
1708
+ }
1560
1709
  function coerceStringWithoutSchema(value) {
1561
1710
  const s = value.trim();
1562
1711
  const lower = s.toLowerCase();
@@ -1587,13 +1736,7 @@ function coerceStringToObject(s, unwrapped) {
1587
1736
  normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
1588
1737
  const obj = JSON.parse(normalized);
1589
1738
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
1590
- const props = unwrapped.properties;
1591
- const out = {};
1592
- for (const [k, v] of Object.entries(obj)) {
1593
- const propSchema = props ? props[k] : void 0;
1594
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
1595
- }
1596
- return out;
1739
+ return coerceObjectToObject(obj, unwrapped);
1597
1740
  }
1598
1741
  } catch (e) {
1599
1742
  }
@@ -1623,10 +1766,8 @@ function coerceStringToArray(s, unwrapped) {
1623
1766
  }
1624
1767
  function coerceObjectToObject(value, unwrapped) {
1625
1768
  const out = {};
1626
- const props = unwrapped.properties;
1627
1769
  for (const [k, v] of Object.entries(value)) {
1628
- const propSchema = props ? props[k] : void 0;
1629
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
1770
+ out[k] = coerceValueForKey(v, k, unwrapped);
1630
1771
  }
1631
1772
  return out;
1632
1773
  }
@@ -1643,16 +1784,22 @@ function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
1643
1784
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
1644
1785
  }
1645
1786
  const keys = Object.keys(maybe);
1646
- if (keys.length === 1) {
1647
- const singleValue = maybe[keys[0]];
1648
- if (Array.isArray(singleValue)) {
1649
- return singleValue.map((v) => coerceBySchema(v, itemsSchema));
1650
- }
1651
- }
1652
1787
  if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {
1653
1788
  const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
1654
1789
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
1655
1790
  }
1791
+ if (keys.length === 1) {
1792
+ const singleKey = keys[0];
1793
+ if (!(schemaIsUnconstrained(itemsSchema) || schemaHasProperty(itemsSchema, singleKey))) {
1794
+ const singleValue = maybe[singleKey];
1795
+ if (Array.isArray(singleValue)) {
1796
+ return singleValue.map((v) => coerceBySchema(v, itemsSchema));
1797
+ }
1798
+ if (singleValue && typeof singleValue === "object") {
1799
+ return [coerceBySchema(singleValue, itemsSchema)];
1800
+ }
1801
+ }
1802
+ }
1656
1803
  return null;
1657
1804
  }
1658
1805
  function coercePrimitiveToArray(value, prefixItems, itemsSchema) {
@@ -1712,11 +1859,15 @@ function coerceArrayValue(value, prefixItems, itemsSchema) {
1712
1859
  if (result !== null) {
1713
1860
  return result;
1714
1861
  }
1862
+ if (getSchemaType(itemsSchema) === "array") {
1863
+ return [value];
1864
+ }
1865
+ return [coerceBySchema(value, itemsSchema)];
1715
1866
  }
1716
1867
  if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1717
1868
  return coercePrimitiveToArray(value, prefixItems, itemsSchema);
1718
1869
  }
1719
- return value;
1870
+ return [value];
1720
1871
  }
1721
1872
  function coerceBySchema(value, schema) {
1722
1873
  const unwrapped = unwrapJsonSchema(schema);
@@ -3493,12 +3644,9 @@ function getStringPropertyNames(schema) {
3493
3644
  }
3494
3645
  return names;
3495
3646
  }
3496
- function escapeRegExp2(s) {
3497
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3498
- }
3499
3647
  function dedupeSingleTag(xml, key) {
3500
3648
  var _a, _b;
3501
- const escaped = escapeRegExp2(key);
3649
+ const escaped = escapeRegExp(key);
3502
3650
  const re = new RegExp(`<${escaped}>([\\s\\S]*?)<\\/${escaped}>`, "g");
3503
3651
  const matches = Array.from(xml.matchAll(re));
3504
3652
  if (matches.length <= 1) {
@@ -3615,9 +3763,11 @@ function parse3(xml, schema, options = {}) {
3615
3763
  throw new RXMLParseError("Failed to parse XML with repair heuristics", error);
3616
3764
  }
3617
3765
 
3618
- // src/core/protocols/xml-protocol.ts
3766
+ // src/core/utils/regex-constants.ts
3619
3767
  var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
3620
3768
  var WHITESPACE_REGEX4 = /\s/;
3769
+
3770
+ // src/core/protocols/xml-protocol.ts
3621
3771
  function getToolSchema(tools, toolName) {
3622
3772
  var _a;
3623
3773
  return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
@@ -4325,8 +4475,6 @@ var xmlProtocol = (protocolOptions) => {
4325
4475
 
4326
4476
  // src/core/protocols/yaml-protocol.ts
4327
4477
  var import_yaml = __toESM(require("yaml"), 1);
4328
- var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
4329
- var WHITESPACE_REGEX5 = /\s/;
4330
4478
  var LEADING_WHITESPACE_RE = /^(\s*)/;
4331
4479
  function findClosingTagEnd(text, contentStart, toolName) {
4332
4480
  let pos = contentStart;
@@ -4343,11 +4491,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
4343
4491
  break;
4344
4492
  }
4345
4493
  let p = ltIdx + 2;
4346
- while (p < gtIdx && WHITESPACE_REGEX5.test(text[p])) {
4494
+ while (p < gtIdx && WHITESPACE_REGEX4.test(text[p])) {
4347
4495
  p++;
4348
4496
  }
4349
4497
  const nameStart = p;
4350
- while (p < gtIdx && NAME_CHAR_RE3.test(text.charAt(p))) {
4498
+ while (p < gtIdx && NAME_CHAR_RE2.test(text.charAt(p))) {
4351
4499
  p++;
4352
4500
  }
4353
4501
  const name = text.slice(nameStart, p);
@@ -4363,11 +4511,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
4363
4511
  pos = gtIdx === -1 ? text.length : gtIdx + 1;
4364
4512
  } else {
4365
4513
  let p = ltIdx + 1;
4366
- while (p < text.length && WHITESPACE_REGEX5.test(text[p])) {
4514
+ while (p < text.length && WHITESPACE_REGEX4.test(text[p])) {
4367
4515
  p++;
4368
4516
  }
4369
4517
  const nameStart = p;
4370
- while (p < text.length && NAME_CHAR_RE3.test(text.charAt(p))) {
4518
+ while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
4371
4519
  p++;
4372
4520
  }
4373
4521
  const name = text.slice(nameStart, p);
@@ -4376,7 +4524,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
4376
4524
  break;
4377
4525
  }
4378
4526
  let r = gtIdx - 1;
4379
- while (r >= nameStart && WHITESPACE_REGEX5.test(text[r])) {
4527
+ while (r >= nameStart && WHITESPACE_REGEX4.test(text[r])) {
4380
4528
  r--;
4381
4529
  }
4382
4530
  const selfClosing = text[r] === "/";
@@ -4494,22 +4642,14 @@ function parseYamlContent(yamlContent, options) {
4494
4642
  return null;
4495
4643
  }
4496
4644
  }
4497
- function appendTextPart(processedElements, textPart) {
4498
- if (textPart.trim()) {
4499
- processedElements.push({
4500
- type: "text",
4501
- text: textPart
4502
- });
4503
- }
4504
- }
4505
4645
  function processToolCallMatch(text, tc, currentIndex, processedElements, options) {
4506
4646
  var _a;
4507
4647
  if (tc.startIndex < currentIndex) {
4508
4648
  return currentIndex;
4509
4649
  }
4510
- appendTextPart(
4511
- processedElements,
4512
- text.substring(currentIndex, tc.startIndex)
4650
+ addTextSegment(
4651
+ text.substring(currentIndex, tc.startIndex),
4652
+ processedElements
4513
4653
  );
4514
4654
  const parsedArgs = parseYamlContent(tc.content, options);
4515
4655
  if (parsedArgs !== null) {
@@ -4627,7 +4767,7 @@ ${yamlContent}</${toolCall.toolName}>`;
4627
4767
  );
4628
4768
  }
4629
4769
  if (currentIndex < text.length) {
4630
- appendTextPart(processedElements, text.substring(currentIndex));
4770
+ addTextSegment(text.substring(currentIndex), processedElements);
4631
4771
  }
4632
4772
  return processedElements;
4633
4773
  },