@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.
package/dist/community.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  createToolMiddleware,
3
3
  xmlProtocol
4
- } from "./chunk-3QJVNEHE.js";
5
- import "./chunk-TR2ARLIF.js";
4
+ } from "./chunk-5WKXBBCU.js";
5
+ import "./chunk-ZDBNJWLY.js";
6
6
  import "./chunk-IX4FJELL.js";
7
- import "./chunk-DZB6Y354.js";
7
+ import "./chunk-OUGMLYAW.js";
8
8
 
9
9
  // src/community/sijawara.ts
10
10
  var sijawaraDetailedXmlToolMiddleware = createToolMiddleware({
package/dist/index.cjs CHANGED
@@ -908,6 +908,13 @@ function generateId() {
908
908
  return Math.random().toString(36).substring(2, 15);
909
909
  }
910
910
 
911
+ // src/core/utils/protocol-utils.ts
912
+ function addTextSegment(text, processedElements) {
913
+ if (text.trim()) {
914
+ processedElements.push({ type: "text", text });
915
+ }
916
+ }
917
+
911
918
  // src/core/utils/regex.ts
912
919
  function escapeRegExp(literal) {
913
920
  return literal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -939,11 +946,6 @@ function processToolCallJson(toolCallJson, fullMatch, processedElements, options
939
946
  processedElements.push({ type: "text", text: fullMatch });
940
947
  }
941
948
  }
942
- function addTextSegment(text, processedElements) {
943
- if (text.trim()) {
944
- processedElements.push({ type: "text", text });
945
- }
946
- }
947
949
  function processMatchedToolCall(context) {
948
950
  const { match, text, currentIndex, processedElements, options } = context;
949
951
  const startIndex = match.index;
@@ -1618,6 +1620,153 @@ function getSchemaType(schema) {
1618
1620
  }
1619
1621
  return;
1620
1622
  }
1623
+ function schemaAllowsPropertyViaCombinators(s, key, depth) {
1624
+ const anyOfValues = s.anyOf;
1625
+ const oneOfValues = s.oneOf;
1626
+ const allOfValues = s.allOf;
1627
+ let hasCombinator = false;
1628
+ let anyOfAllows = true;
1629
+ let oneOfAllows = true;
1630
+ let allOfAllows = true;
1631
+ if (Array.isArray(anyOfValues)) {
1632
+ hasCombinator = true;
1633
+ anyOfAllows = anyOfValues.some(
1634
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1635
+ );
1636
+ }
1637
+ if (Array.isArray(oneOfValues)) {
1638
+ hasCombinator = true;
1639
+ oneOfAllows = oneOfValues.some(
1640
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1641
+ );
1642
+ }
1643
+ if (Array.isArray(allOfValues)) {
1644
+ hasCombinator = true;
1645
+ allOfAllows = allOfValues.every(
1646
+ (sub) => schemaHasProperty(sub, key, depth + 1)
1647
+ );
1648
+ }
1649
+ if (!hasCombinator) {
1650
+ return false;
1651
+ }
1652
+ return anyOfAllows && oneOfAllows && allOfAllows;
1653
+ }
1654
+ function schemaHasPropertyDirectly(s, key) {
1655
+ const props = s.properties;
1656
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] !== false) {
1657
+ return true;
1658
+ }
1659
+ const required = s.required;
1660
+ if (Array.isArray(required) && required.includes(key)) {
1661
+ return true;
1662
+ }
1663
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1664
+ return patternSchemas.some((schema) => schema !== false);
1665
+ }
1666
+ function schemaHasPropertyViaAdditional(s) {
1667
+ const additional = s.additionalProperties;
1668
+ if (additional === true || additional && typeof additional === "object" && !Array.isArray(additional)) {
1669
+ return true;
1670
+ }
1671
+ if (Object.hasOwn(s, "additionalProperties")) {
1672
+ return false;
1673
+ }
1674
+ const type = s.type;
1675
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
1676
+ 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;
1677
+ return !!(isObjectType || hasObjectKeywords);
1678
+ }
1679
+ function schemaDisallowsPropertyDirectly(s, key) {
1680
+ const props = s.properties;
1681
+ if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] === false) {
1682
+ return true;
1683
+ }
1684
+ const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
1685
+ return patternSchemas.some((schema) => schema === false);
1686
+ }
1687
+ function schemaHasProperty(schema, key, depth = 0) {
1688
+ if (depth > 5) {
1689
+ return true;
1690
+ }
1691
+ const unwrapped = unwrapJsonSchema(schema);
1692
+ if (schemaIsUnconstrained(unwrapped)) {
1693
+ return true;
1694
+ }
1695
+ if (!unwrapped || typeof unwrapped !== "object") {
1696
+ return false;
1697
+ }
1698
+ const s = unwrapped;
1699
+ if (schemaDisallowsPropertyDirectly(s, key)) {
1700
+ return false;
1701
+ }
1702
+ if (schemaHasPropertyDirectly(s, key)) {
1703
+ return true;
1704
+ }
1705
+ if (schemaHasPropertyViaAdditional(s)) {
1706
+ return true;
1707
+ }
1708
+ return schemaAllowsPropertyViaCombinators(s, key, depth);
1709
+ }
1710
+ function schemaIsUnconstrained(schema) {
1711
+ const unwrapped = unwrapJsonSchema(schema);
1712
+ if (unwrapped == null || unwrapped === true) {
1713
+ return true;
1714
+ }
1715
+ if (typeof unwrapped !== "object" || Array.isArray(unwrapped)) {
1716
+ return false;
1717
+ }
1718
+ return Object.keys(unwrapped).length === 0;
1719
+ }
1720
+ function getPatternSchemasForKey(patternProperties, key) {
1721
+ if (!patternProperties || typeof patternProperties !== "object" || Array.isArray(patternProperties)) {
1722
+ return [];
1723
+ }
1724
+ const schemas = [];
1725
+ for (const [pattern, schema] of Object.entries(
1726
+ patternProperties
1727
+ )) {
1728
+ try {
1729
+ const regex = new RegExp(pattern);
1730
+ if (regex.test(key)) {
1731
+ schemas.push(schema);
1732
+ }
1733
+ } catch (e) {
1734
+ }
1735
+ }
1736
+ return schemas;
1737
+ }
1738
+ function coerceValueForKey(value, key, unwrapped) {
1739
+ const schemas = [];
1740
+ const props = unwrapped.properties;
1741
+ if (props && Object.hasOwn(props, key)) {
1742
+ schemas.push(props[key]);
1743
+ }
1744
+ const patternSchemas = getPatternSchemasForKey(
1745
+ unwrapped.patternProperties,
1746
+ key
1747
+ );
1748
+ if (patternSchemas.length > 0) {
1749
+ schemas.push(...patternSchemas);
1750
+ }
1751
+ if (schemas.length > 0) {
1752
+ let out = value;
1753
+ for (const schema of schemas) {
1754
+ if (typeof schema === "boolean") {
1755
+ continue;
1756
+ }
1757
+ out = coerceBySchema(out, schema);
1758
+ }
1759
+ return out;
1760
+ }
1761
+ const additional = unwrapped.additionalProperties;
1762
+ if (additional && typeof additional === "object" && !Array.isArray(additional)) {
1763
+ return coerceBySchema(value, additional);
1764
+ }
1765
+ if (additional === true || additional === false) {
1766
+ return value;
1767
+ }
1768
+ return coerceBySchema(value, void 0);
1769
+ }
1621
1770
  function coerceStringWithoutSchema(value) {
1622
1771
  const s = value.trim();
1623
1772
  const lower = s.toLowerCase();
@@ -1648,13 +1797,7 @@ function coerceStringToObject(s, unwrapped) {
1648
1797
  normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
1649
1798
  const obj = JSON.parse(normalized);
1650
1799
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
1651
- const props = unwrapped.properties;
1652
- const out = {};
1653
- for (const [k, v] of Object.entries(obj)) {
1654
- const propSchema = props ? props[k] : void 0;
1655
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
1656
- }
1657
- return out;
1800
+ return coerceObjectToObject(obj, unwrapped);
1658
1801
  }
1659
1802
  } catch (e) {
1660
1803
  }
@@ -1684,10 +1827,8 @@ function coerceStringToArray(s, unwrapped) {
1684
1827
  }
1685
1828
  function coerceObjectToObject(value, unwrapped) {
1686
1829
  const out = {};
1687
- const props = unwrapped.properties;
1688
1830
  for (const [k, v] of Object.entries(value)) {
1689
- const propSchema = props ? props[k] : void 0;
1690
- out[k] = typeof propSchema === "boolean" ? v : coerceBySchema(v, propSchema);
1831
+ out[k] = coerceValueForKey(v, k, unwrapped);
1691
1832
  }
1692
1833
  return out;
1693
1834
  }
@@ -1704,16 +1845,22 @@ function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
1704
1845
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
1705
1846
  }
1706
1847
  const keys = Object.keys(maybe);
1707
- if (keys.length === 1) {
1708
- const singleValue = maybe[keys[0]];
1709
- if (Array.isArray(singleValue)) {
1710
- return singleValue.map((v) => coerceBySchema(v, itemsSchema));
1711
- }
1712
- }
1713
1848
  if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {
1714
1849
  const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
1715
1850
  return coerceArrayToArray(arr, prefixItems, itemsSchema);
1716
1851
  }
1852
+ if (keys.length === 1) {
1853
+ const singleKey = keys[0];
1854
+ if (!(schemaIsUnconstrained(itemsSchema) || schemaHasProperty(itemsSchema, singleKey))) {
1855
+ const singleValue = maybe[singleKey];
1856
+ if (Array.isArray(singleValue)) {
1857
+ return singleValue.map((v) => coerceBySchema(v, itemsSchema));
1858
+ }
1859
+ if (singleValue && typeof singleValue === "object") {
1860
+ return [coerceBySchema(singleValue, itemsSchema)];
1861
+ }
1862
+ }
1863
+ }
1717
1864
  return null;
1718
1865
  }
1719
1866
  function coercePrimitiveToArray(value, prefixItems, itemsSchema) {
@@ -1773,11 +1920,15 @@ function coerceArrayValue(value, prefixItems, itemsSchema) {
1773
1920
  if (result !== null) {
1774
1921
  return result;
1775
1922
  }
1923
+ if (getSchemaType(itemsSchema) === "array") {
1924
+ return [value];
1925
+ }
1926
+ return [coerceBySchema(value, itemsSchema)];
1776
1927
  }
1777
1928
  if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
1778
1929
  return coercePrimitiveToArray(value, prefixItems, itemsSchema);
1779
1930
  }
1780
- return value;
1931
+ return [value];
1781
1932
  }
1782
1933
  function coerceBySchema(value, schema) {
1783
1934
  const unwrapped = unwrapJsonSchema(schema);
@@ -3554,12 +3705,9 @@ function getStringPropertyNames(schema) {
3554
3705
  }
3555
3706
  return names;
3556
3707
  }
3557
- function escapeRegExp2(s) {
3558
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3559
- }
3560
3708
  function dedupeSingleTag(xml, key) {
3561
3709
  var _a, _b;
3562
- const escaped = escapeRegExp2(key);
3710
+ const escaped = escapeRegExp(key);
3563
3711
  const re = new RegExp(`<${escaped}>([\\s\\S]*?)<\\/${escaped}>`, "g");
3564
3712
  const matches = Array.from(xml.matchAll(re));
3565
3713
  if (matches.length <= 1) {
@@ -3676,9 +3824,11 @@ function parse3(xml, schema, options = {}) {
3676
3824
  throw new RXMLParseError("Failed to parse XML with repair heuristics", error);
3677
3825
  }
3678
3826
 
3679
- // src/core/protocols/xml-protocol.ts
3827
+ // src/core/utils/regex-constants.ts
3680
3828
  var NAME_CHAR_RE2 = /[A-Za-z0-9_:-]/;
3681
3829
  var WHITESPACE_REGEX4 = /\s/;
3830
+
3831
+ // src/core/protocols/xml-protocol.ts
3682
3832
  function getToolSchema(tools, toolName) {
3683
3833
  var _a;
3684
3834
  return (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
@@ -4386,8 +4536,6 @@ var xmlProtocol = (protocolOptions) => {
4386
4536
 
4387
4537
  // src/core/protocols/yaml-protocol.ts
4388
4538
  var import_yaml = __toESM(require("yaml"), 1);
4389
- var NAME_CHAR_RE3 = /[A-Za-z0-9_:-]/;
4390
- var WHITESPACE_REGEX5 = /\s/;
4391
4539
  var LEADING_WHITESPACE_RE = /^(\s*)/;
4392
4540
  function findClosingTagEnd(text, contentStart, toolName) {
4393
4541
  let pos = contentStart;
@@ -4404,11 +4552,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
4404
4552
  break;
4405
4553
  }
4406
4554
  let p = ltIdx + 2;
4407
- while (p < gtIdx && WHITESPACE_REGEX5.test(text[p])) {
4555
+ while (p < gtIdx && WHITESPACE_REGEX4.test(text[p])) {
4408
4556
  p++;
4409
4557
  }
4410
4558
  const nameStart = p;
4411
- while (p < gtIdx && NAME_CHAR_RE3.test(text.charAt(p))) {
4559
+ while (p < gtIdx && NAME_CHAR_RE2.test(text.charAt(p))) {
4412
4560
  p++;
4413
4561
  }
4414
4562
  const name = text.slice(nameStart, p);
@@ -4424,11 +4572,11 @@ function findClosingTagEnd(text, contentStart, toolName) {
4424
4572
  pos = gtIdx === -1 ? text.length : gtIdx + 1;
4425
4573
  } else {
4426
4574
  let p = ltIdx + 1;
4427
- while (p < text.length && WHITESPACE_REGEX5.test(text[p])) {
4575
+ while (p < text.length && WHITESPACE_REGEX4.test(text[p])) {
4428
4576
  p++;
4429
4577
  }
4430
4578
  const nameStart = p;
4431
- while (p < text.length && NAME_CHAR_RE3.test(text.charAt(p))) {
4579
+ while (p < text.length && NAME_CHAR_RE2.test(text.charAt(p))) {
4432
4580
  p++;
4433
4581
  }
4434
4582
  const name = text.slice(nameStart, p);
@@ -4437,7 +4585,7 @@ function findClosingTagEnd(text, contentStart, toolName) {
4437
4585
  break;
4438
4586
  }
4439
4587
  let r = gtIdx - 1;
4440
- while (r >= nameStart && WHITESPACE_REGEX5.test(text[r])) {
4588
+ while (r >= nameStart && WHITESPACE_REGEX4.test(text[r])) {
4441
4589
  r--;
4442
4590
  }
4443
4591
  const selfClosing = text[r] === "/";
@@ -4555,22 +4703,14 @@ function parseYamlContent(yamlContent, options) {
4555
4703
  return null;
4556
4704
  }
4557
4705
  }
4558
- function appendTextPart(processedElements, textPart) {
4559
- if (textPart.trim()) {
4560
- processedElements.push({
4561
- type: "text",
4562
- text: textPart
4563
- });
4564
- }
4565
- }
4566
4706
  function processToolCallMatch(text, tc, currentIndex, processedElements, options) {
4567
4707
  var _a;
4568
4708
  if (tc.startIndex < currentIndex) {
4569
4709
  return currentIndex;
4570
4710
  }
4571
- appendTextPart(
4572
- processedElements,
4573
- text.substring(currentIndex, tc.startIndex)
4711
+ addTextSegment(
4712
+ text.substring(currentIndex, tc.startIndex),
4713
+ processedElements
4574
4714
  );
4575
4715
  const parsedArgs = parseYamlContent(tc.content, options);
4576
4716
  if (parsedArgs !== null) {
@@ -4688,7 +4828,7 @@ ${yamlContent}</${toolCall.toolName}>`;
4688
4828
  );
4689
4829
  }
4690
4830
  if (currentIndex < text.length) {
4691
- appendTextPart(processedElements, text.substring(currentIndex));
4831
+ addTextSegment(text.substring(currentIndex), processedElements);
4692
4832
  }
4693
4833
  return processedElements;
4694
4834
  },