@ai-sdk-tool/parser 2.1.2 → 2.1.3

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.cjs CHANGED
@@ -1325,6 +1325,607 @@ var jsonMixProtocol = ({
1325
1325
  // src/protocols/morph-xml-protocol.ts
1326
1326
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1327
1327
  var import_fast_xml_parser = require("fast-xml-parser");
1328
+ var WARN_ON_DUPLICATE_STRING_TAGS = true;
1329
+ function getToolSchema(tools, originalSchemas, toolName) {
1330
+ var _a;
1331
+ const original = originalSchemas[toolName];
1332
+ if (original) return original;
1333
+ const fallback = (_a = tools.find((t) => t.name === toolName)) == null ? void 0 : _a.inputSchema;
1334
+ return fallback;
1335
+ }
1336
+ function getPropertySchema(toolSchema, key) {
1337
+ const unwrapped = unwrapJsonSchema(toolSchema);
1338
+ if (!unwrapped || typeof unwrapped !== "object") return void 0;
1339
+ const u = unwrapped;
1340
+ const props = u.properties;
1341
+ if (props && Object.prototype.hasOwnProperty.call(props, key)) {
1342
+ return props[key];
1343
+ }
1344
+ return void 0;
1345
+ }
1346
+ function extractRawInner(xmlContent, tagName) {
1347
+ const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
1348
+ const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
1349
+ const len = xmlContent.length;
1350
+ const target = tagName;
1351
+ let bestStart = -1;
1352
+ let bestEnd = -1;
1353
+ let bestDepth = Number.POSITIVE_INFINITY;
1354
+ const skipQuoted = (s, i2) => {
1355
+ const quote = s[i2];
1356
+ i2++;
1357
+ while (i2 < s.length) {
1358
+ const ch = s[i2];
1359
+ if (ch === "\\") {
1360
+ i2 += 2;
1361
+ continue;
1362
+ }
1363
+ if (ch === quote) {
1364
+ return i2 + 1;
1365
+ }
1366
+ i2++;
1367
+ }
1368
+ return i2;
1369
+ };
1370
+ let i = 0;
1371
+ let depth = 0;
1372
+ while (i < len) {
1373
+ const lt = xmlContent.indexOf("<", i);
1374
+ if (lt === -1) return void 0;
1375
+ i = lt + 1;
1376
+ if (i >= len) return void 0;
1377
+ const ch = xmlContent[i];
1378
+ if (ch === "!") {
1379
+ if (xmlContent.startsWith("!--", i + 1)) {
1380
+ const close = xmlContent.indexOf("-->", i + 4);
1381
+ i = close === -1 ? len : close + 3;
1382
+ continue;
1383
+ }
1384
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
1385
+ const close = xmlContent.indexOf("]]>", i + 9);
1386
+ i = close === -1 ? len : close + 3;
1387
+ continue;
1388
+ }
1389
+ const gt = xmlContent.indexOf(">", i + 1);
1390
+ i = gt === -1 ? len : gt + 1;
1391
+ continue;
1392
+ } else if (ch === "?") {
1393
+ const close = xmlContent.indexOf("?>", i + 1);
1394
+ i = close === -1 ? len : close + 2;
1395
+ continue;
1396
+ } else if (ch === "/") {
1397
+ let j = i + 1;
1398
+ if (j < len && isNameStartChar(xmlContent[j])) {
1399
+ j++;
1400
+ while (j < len && isNameChar(xmlContent[j])) j++;
1401
+ }
1402
+ const gt = xmlContent.indexOf(">", j);
1403
+ i = gt === -1 ? len : gt + 1;
1404
+ depth = Math.max(0, depth - 1);
1405
+ continue;
1406
+ } else {
1407
+ let j = i;
1408
+ if (j < len && isNameStartChar(xmlContent[j])) {
1409
+ j++;
1410
+ while (j < len && isNameChar(xmlContent[j])) j++;
1411
+ }
1412
+ const name = xmlContent.slice(i, j);
1413
+ let k = j;
1414
+ let isSelfClosing = false;
1415
+ while (k < len) {
1416
+ const c = xmlContent[k];
1417
+ if (c === '"' || c === "'") {
1418
+ k = skipQuoted(xmlContent, k);
1419
+ continue;
1420
+ }
1421
+ if (c === ">") {
1422
+ break;
1423
+ }
1424
+ if (c === "/" && xmlContent[k + 1] === ">") {
1425
+ isSelfClosing = true;
1426
+ k++;
1427
+ break;
1428
+ }
1429
+ k++;
1430
+ }
1431
+ const tagEnd = k;
1432
+ if (name === target) {
1433
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
1434
+ if (isSelfClosing) {
1435
+ if (depth < bestDepth) {
1436
+ bestStart = contentStart;
1437
+ bestEnd = contentStart;
1438
+ bestDepth = depth;
1439
+ if (bestDepth === 0) {
1440
+ }
1441
+ }
1442
+ } else {
1443
+ let pos = contentStart;
1444
+ let sameDepth = 1;
1445
+ while (pos < len) {
1446
+ const nextLt = xmlContent.indexOf("<", pos);
1447
+ if (nextLt === -1) break;
1448
+ const nx = nextLt + 1;
1449
+ if (nx >= len) break;
1450
+ const h = xmlContent[nx];
1451
+ if (h === "!") {
1452
+ if (xmlContent.startsWith("!--", nx + 1)) {
1453
+ const close = xmlContent.indexOf("-->", nx + 4);
1454
+ pos = close === -1 ? len : close + 3;
1455
+ continue;
1456
+ }
1457
+ if (xmlContent.startsWith("![CDATA[", nx + 1)) {
1458
+ const close = xmlContent.indexOf("]]>", nx + 9);
1459
+ pos = close === -1 ? len : close + 3;
1460
+ continue;
1461
+ }
1462
+ const gt2 = xmlContent.indexOf(">", nx + 1);
1463
+ pos = gt2 === -1 ? len : gt2 + 1;
1464
+ continue;
1465
+ } else if (h === "?") {
1466
+ const close = xmlContent.indexOf("?>", nx + 1);
1467
+ pos = close === -1 ? len : close + 2;
1468
+ continue;
1469
+ } else if (h === "/") {
1470
+ let t = nx + 1;
1471
+ if (t < len && isNameStartChar(xmlContent[t])) {
1472
+ t++;
1473
+ while (t < len && isNameChar(xmlContent[t])) t++;
1474
+ }
1475
+ const endName = xmlContent.slice(nx + 1, t);
1476
+ const gt2 = xmlContent.indexOf(">", t);
1477
+ if (endName === target) {
1478
+ sameDepth--;
1479
+ if (sameDepth === 0) {
1480
+ if (depth < bestDepth) {
1481
+ bestStart = contentStart;
1482
+ bestEnd = nextLt;
1483
+ bestDepth = depth;
1484
+ if (bestDepth === 0) {
1485
+ }
1486
+ }
1487
+ break;
1488
+ }
1489
+ }
1490
+ pos = gt2 === -1 ? len : gt2 + 1;
1491
+ continue;
1492
+ } else {
1493
+ let t = nx;
1494
+ if (t < len && isNameStartChar(xmlContent[t])) {
1495
+ t++;
1496
+ while (t < len && isNameChar(xmlContent[t])) t++;
1497
+ }
1498
+ const startName = xmlContent.slice(nx, t);
1499
+ let u = t;
1500
+ let selfClose = false;
1501
+ while (u < len) {
1502
+ const cu = xmlContent[u];
1503
+ if (cu === '"' || cu === "'") {
1504
+ u = skipQuoted(xmlContent, u);
1505
+ continue;
1506
+ }
1507
+ if (cu === ">") break;
1508
+ if (cu === "/" && xmlContent[u + 1] === ">") {
1509
+ selfClose = true;
1510
+ u++;
1511
+ break;
1512
+ }
1513
+ u++;
1514
+ }
1515
+ if (startName === target && !selfClose) {
1516
+ sameDepth++;
1517
+ }
1518
+ pos = xmlContent[u] === ">" ? u + 1 : u + 1;
1519
+ continue;
1520
+ }
1521
+ }
1522
+ }
1523
+ }
1524
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
1525
+ depth += isSelfClosing ? 0 : 1;
1526
+ continue;
1527
+ }
1528
+ }
1529
+ if (bestStart !== -1) {
1530
+ return xmlContent.slice(bestStart, bestEnd);
1531
+ }
1532
+ return void 0;
1533
+ }
1534
+ function findFirstTopLevelRange(xmlContent, tagName) {
1535
+ const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
1536
+ const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
1537
+ const len = xmlContent.length;
1538
+ const target = tagName;
1539
+ const skipQuoted = (s, i2) => {
1540
+ const quote = s[i2];
1541
+ i2++;
1542
+ while (i2 < s.length) {
1543
+ const ch = s[i2];
1544
+ if (ch === "\\") {
1545
+ i2 += 2;
1546
+ continue;
1547
+ }
1548
+ if (ch === quote) return i2 + 1;
1549
+ i2++;
1550
+ }
1551
+ return i2;
1552
+ };
1553
+ let i = 0;
1554
+ let depth = 0;
1555
+ while (i < len) {
1556
+ const lt = xmlContent.indexOf("<", i);
1557
+ if (lt === -1) return void 0;
1558
+ i = lt + 1;
1559
+ if (i >= len) return void 0;
1560
+ const ch = xmlContent[i];
1561
+ if (ch === "!") {
1562
+ if (xmlContent.startsWith("!--", i + 1)) {
1563
+ const close = xmlContent.indexOf("-->", i + 4);
1564
+ i = close === -1 ? len : close + 3;
1565
+ continue;
1566
+ }
1567
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
1568
+ const close = xmlContent.indexOf("]]>", i + 9);
1569
+ i = close === -1 ? len : close + 3;
1570
+ continue;
1571
+ }
1572
+ const gt = xmlContent.indexOf(">", i + 1);
1573
+ i = gt === -1 ? len : gt + 1;
1574
+ continue;
1575
+ } else if (ch === "?") {
1576
+ const close = xmlContent.indexOf("?>", i + 1);
1577
+ i = close === -1 ? len : close + 2;
1578
+ continue;
1579
+ } else if (ch === "/") {
1580
+ const gt = xmlContent.indexOf(">", i + 1);
1581
+ i = gt === -1 ? len : gt + 1;
1582
+ depth = Math.max(0, depth - 1);
1583
+ continue;
1584
+ } else {
1585
+ let j = i;
1586
+ if (j < len && isNameStartChar(xmlContent[j])) {
1587
+ j++;
1588
+ while (j < len && isNameChar(xmlContent[j])) j++;
1589
+ }
1590
+ const name = xmlContent.slice(i, j);
1591
+ let k = j;
1592
+ let isSelfClosing = false;
1593
+ while (k < len) {
1594
+ const c = xmlContent[k];
1595
+ if (c === '"' || c === "'") {
1596
+ k = skipQuoted(xmlContent, k);
1597
+ continue;
1598
+ }
1599
+ if (c === ">") break;
1600
+ if (c === "/" && xmlContent[k + 1] === ">") {
1601
+ isSelfClosing = true;
1602
+ k++;
1603
+ break;
1604
+ }
1605
+ k++;
1606
+ }
1607
+ const tagEnd = k;
1608
+ if (depth === 0 && name === target) {
1609
+ const contentStart = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
1610
+ if (isSelfClosing) return { start: contentStart, end: contentStart };
1611
+ let pos = contentStart;
1612
+ let sameDepth = 1;
1613
+ while (pos < len) {
1614
+ const nextLt = xmlContent.indexOf("<", pos);
1615
+ if (nextLt === -1) break;
1616
+ const nx = nextLt + 1;
1617
+ if (nx >= len) break;
1618
+ const h = xmlContent[nx];
1619
+ if (h === "!") {
1620
+ if (xmlContent.startsWith("!--", nx + 1)) {
1621
+ const close = xmlContent.indexOf("-->", nx + 4);
1622
+ pos = close === -1 ? len : close + 3;
1623
+ continue;
1624
+ }
1625
+ if (xmlContent.startsWith("![CDATA[", nx + 1)) {
1626
+ const close = xmlContent.indexOf("]]>", nx + 9);
1627
+ pos = close === -1 ? len : close + 3;
1628
+ continue;
1629
+ }
1630
+ const gt2 = xmlContent.indexOf(">", nx + 1);
1631
+ pos = gt2 === -1 ? len : gt2 + 1;
1632
+ continue;
1633
+ } else if (h === "?") {
1634
+ const close = xmlContent.indexOf("?>", nx + 1);
1635
+ pos = close === -1 ? len : close + 2;
1636
+ continue;
1637
+ } else if (h === "/") {
1638
+ let t = nx + 1;
1639
+ if (t < len && isNameStartChar(xmlContent[t])) {
1640
+ t++;
1641
+ while (t < len && isNameChar(xmlContent[t])) t++;
1642
+ }
1643
+ const endName = xmlContent.slice(nx + 1, t);
1644
+ const gt2 = xmlContent.indexOf(">", t);
1645
+ if (endName === target) {
1646
+ sameDepth--;
1647
+ if (sameDepth === 0) {
1648
+ return { start: contentStart, end: nextLt };
1649
+ }
1650
+ }
1651
+ pos = gt2 === -1 ? len : gt2 + 1;
1652
+ continue;
1653
+ } else {
1654
+ let t = nx;
1655
+ if (t < len && isNameStartChar(xmlContent[t])) {
1656
+ t++;
1657
+ while (t < len && isNameChar(xmlContent[t])) t++;
1658
+ }
1659
+ let u = t;
1660
+ let selfClose = false;
1661
+ while (u < len) {
1662
+ const cu = xmlContent[u];
1663
+ if (cu === '"' || cu === "'") {
1664
+ u = skipQuoted(xmlContent, u);
1665
+ continue;
1666
+ }
1667
+ if (cu === ">") break;
1668
+ if (cu === "/" && xmlContent[u + 1] === ">") {
1669
+ selfClose = true;
1670
+ u++;
1671
+ break;
1672
+ }
1673
+ u++;
1674
+ }
1675
+ if (!selfClose) {
1676
+ }
1677
+ pos = xmlContent[u] === ">" ? u + 1 : u + 1;
1678
+ continue;
1679
+ }
1680
+ }
1681
+ return void 0;
1682
+ }
1683
+ i = xmlContent[tagEnd] === ">" ? tagEnd + 1 : tagEnd + 1;
1684
+ depth += isSelfClosing ? 0 : 1;
1685
+ continue;
1686
+ }
1687
+ }
1688
+ return void 0;
1689
+ }
1690
+ function countTagOccurrences(xmlContent, tagName, excludeRanges, skipFirst = true) {
1691
+ const isNameStartChar = (ch) => /[A-Za-z_:]/.test(ch);
1692
+ const isNameChar = (ch) => /[A-Za-z0-9_.:-]/.test(ch);
1693
+ const len = xmlContent.length;
1694
+ const target = tagName;
1695
+ const skipQuoted = (s, i2) => {
1696
+ const quote = s[i2];
1697
+ i2++;
1698
+ while (i2 < s.length) {
1699
+ const ch = s[i2];
1700
+ if (ch === "\\") {
1701
+ i2 += 2;
1702
+ continue;
1703
+ }
1704
+ if (ch === quote) return i2 + 1;
1705
+ i2++;
1706
+ }
1707
+ return i2;
1708
+ };
1709
+ let i = 0;
1710
+ let count = 0;
1711
+ const isExcluded = (pos) => {
1712
+ if (!excludeRanges || excludeRanges.length === 0) return false;
1713
+ for (const r of excludeRanges) {
1714
+ if (pos >= r.start && pos < r.end) return true;
1715
+ }
1716
+ return false;
1717
+ };
1718
+ while (i < len) {
1719
+ const lt = xmlContent.indexOf("<", i);
1720
+ if (lt === -1) break;
1721
+ i = lt + 1;
1722
+ if (i >= len) break;
1723
+ const ch = xmlContent[i];
1724
+ if (ch === "!") {
1725
+ if (xmlContent.startsWith("!--", i + 1)) {
1726
+ const close = xmlContent.indexOf("-->", i + 4);
1727
+ i = close === -1 ? len : close + 3;
1728
+ continue;
1729
+ }
1730
+ if (xmlContent.startsWith("![CDATA[", i + 1)) {
1731
+ const close = xmlContent.indexOf("]]>", i + 9);
1732
+ i = close === -1 ? len : close + 3;
1733
+ continue;
1734
+ }
1735
+ const gt = xmlContent.indexOf(">", i + 1);
1736
+ i = gt === -1 ? len : gt + 1;
1737
+ continue;
1738
+ } else if (ch === "?") {
1739
+ const close = xmlContent.indexOf("?>", i + 1);
1740
+ i = close === -1 ? len : close + 2;
1741
+ continue;
1742
+ } else if (ch === "/") {
1743
+ const gt = xmlContent.indexOf(">", i + 1);
1744
+ i = gt === -1 ? len : gt + 1;
1745
+ continue;
1746
+ } else {
1747
+ let j = i;
1748
+ if (j < len && isNameStartChar(xmlContent[j])) {
1749
+ j++;
1750
+ while (j < len && isNameChar(xmlContent[j])) j++;
1751
+ }
1752
+ const name = xmlContent.slice(i, j);
1753
+ let k = j;
1754
+ while (k < len) {
1755
+ const c = xmlContent[k];
1756
+ if (c === '"' || c === "'") {
1757
+ k = skipQuoted(xmlContent, k);
1758
+ continue;
1759
+ }
1760
+ if (c === ">") break;
1761
+ if (c === "/" && xmlContent[k + 1] === ">") {
1762
+ k++;
1763
+ break;
1764
+ }
1765
+ k++;
1766
+ }
1767
+ if (name === target && !isExcluded(lt)) {
1768
+ if (skipFirst) {
1769
+ skipFirst = false;
1770
+ } else {
1771
+ count++;
1772
+ }
1773
+ }
1774
+ i = k + 1;
1775
+ continue;
1776
+ }
1777
+ }
1778
+ return count;
1779
+ }
1780
+ function processParsedArgs(parsedArgs, toolSchema, toolContent, toolName, options) {
1781
+ var _a, _b, _c;
1782
+ const args = {};
1783
+ let cancelToolCall = false;
1784
+ const stringTypedProps = (() => {
1785
+ const set = /* @__PURE__ */ new Set();
1786
+ const unwrapped = unwrapJsonSchema(toolSchema);
1787
+ if (unwrapped && typeof unwrapped === "object") {
1788
+ const u = unwrapped;
1789
+ const props = u.properties;
1790
+ if (props && typeof props === "object") {
1791
+ for (const key of Object.keys(props)) {
1792
+ const t = getSchemaType(props[key]);
1793
+ if (t === "string") set.add(key);
1794
+ }
1795
+ }
1796
+ }
1797
+ return set;
1798
+ })();
1799
+ for (const k of Object.keys(parsedArgs || {})) {
1800
+ const v = parsedArgs[k];
1801
+ let val = v;
1802
+ const propSchema = getPropertySchema(toolSchema, k);
1803
+ const propType = getSchemaType(propSchema);
1804
+ if (propType === "string" && !Array.isArray(v)) {
1805
+ const excludeRanges = [];
1806
+ for (const other of stringTypedProps) {
1807
+ if (other === k) continue;
1808
+ const range = findFirstTopLevelRange(toolContent, other);
1809
+ if (range) excludeRanges.push(range);
1810
+ }
1811
+ const occurrences = countTagOccurrences(
1812
+ toolContent,
1813
+ k,
1814
+ excludeRanges,
1815
+ true
1816
+ );
1817
+ if (occurrences > 0) {
1818
+ if (WARN_ON_DUPLICATE_STRING_TAGS) {
1819
+ (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
1820
+ options,
1821
+ `Duplicate string tags for <${k}> detected; cancelling tool call`,
1822
+ {
1823
+ toolName,
1824
+ toolCall: `<${toolName}>${toolContent}</${toolName}>`
1825
+ }
1826
+ );
1827
+ }
1828
+ cancelToolCall = true;
1829
+ break;
1830
+ }
1831
+ const raw = extractRawInner(toolContent, k);
1832
+ if (typeof raw === "string") {
1833
+ args[k] = raw;
1834
+ continue;
1835
+ }
1836
+ }
1837
+ if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
1838
+ val = v == null ? void 0 : v["#text"];
1839
+ }
1840
+ if (Array.isArray(v)) {
1841
+ if (propType === "string") {
1842
+ const mapped = v.map((item) => {
1843
+ if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1844
+ const textVal = item == null ? void 0 : item["#text"];
1845
+ return typeof textVal === "string" ? textVal : String(textVal);
1846
+ }
1847
+ return typeof item === "string" ? item : String(item);
1848
+ });
1849
+ if (mapped.length > 1 && WARN_ON_DUPLICATE_STRING_TAGS) {
1850
+ (_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
1851
+ options,
1852
+ `Duplicate string tags for <${k}> detected; cancelling tool call`,
1853
+ {
1854
+ toolName,
1855
+ toolCall: `<${toolName}>${toolContent}</${toolName}>`
1856
+ }
1857
+ );
1858
+ }
1859
+ if (mapped.length > 1) {
1860
+ cancelToolCall = true;
1861
+ break;
1862
+ } else {
1863
+ args[k] = (_c = mapped[0]) != null ? _c : "";
1864
+ continue;
1865
+ }
1866
+ } else {
1867
+ val = v.map((item) => {
1868
+ if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1869
+ const textVal = item == null ? void 0 : item["#text"];
1870
+ return typeof textVal === "string" ? textVal.trim() : textVal;
1871
+ }
1872
+ return typeof item === "string" ? item.trim() : item;
1873
+ });
1874
+ }
1875
+ } else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
1876
+ const obj = v;
1877
+ const keys = Object.keys(obj);
1878
+ if (keys.length === 1 && keys[0] === "item") {
1879
+ const itemValue = obj.item;
1880
+ if (Array.isArray(itemValue)) {
1881
+ val = itemValue.map((item) => {
1882
+ let currentVal = item;
1883
+ if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1884
+ currentVal = item == null ? void 0 : item["#text"];
1885
+ }
1886
+ const trimmed = typeof currentVal === "string" ? currentVal.trim() : currentVal;
1887
+ if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1888
+ const num = Number(trimmed);
1889
+ if (Number.isFinite(num)) return num;
1890
+ }
1891
+ return trimmed;
1892
+ });
1893
+ } else {
1894
+ const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
1895
+ if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1896
+ const num = Number(trimmed);
1897
+ val = Number.isFinite(num) ? num : trimmed;
1898
+ } else {
1899
+ val = trimmed;
1900
+ }
1901
+ }
1902
+ } else {
1903
+ let isIndexedTuple = false;
1904
+ if (keys.length > 0 && keys.every((key) => /^\d+$/.test(key))) {
1905
+ const indices = keys.map((k2) => parseInt(k2, 10)).sort((a, b) => a - b);
1906
+ isIndexedTuple = indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
1907
+ }
1908
+ if (isIndexedTuple) {
1909
+ const sortedKeys = keys.sort(
1910
+ (a, b) => parseInt(a, 10) - parseInt(b, 10)
1911
+ );
1912
+ val = sortedKeys.map((key) => {
1913
+ const item = obj[key];
1914
+ if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1915
+ const textVal = item == null ? void 0 : item["#text"];
1916
+ return typeof textVal === "string" ? textVal.trim() : textVal;
1917
+ }
1918
+ return typeof item === "string" ? item.trim() : item;
1919
+ });
1920
+ } else {
1921
+ val = v;
1922
+ }
1923
+ }
1924
+ }
1925
+ args[k] = typeof val === "string" ? val.trim() : val;
1926
+ }
1927
+ return { args, cancelToolCall };
1928
+ }
1328
1929
  var morphXmlProtocol = () => ({
1329
1930
  formatTools({ tools, toolSystemPromptTemplate }) {
1330
1931
  const toolsForPrompt = (tools || []).map((tool) => ({
@@ -1395,91 +1996,31 @@ var morphXmlProtocol = () => ({
1395
1996
  textNodeName: "#text"
1396
1997
  });
1397
1998
  const parsedArgs = ((_a = parser.parse(`<root>${toolContent}</root>`)) == null ? void 0 : _a.root) || {};
1398
- const args = {};
1399
- for (const k of Object.keys(parsedArgs || {})) {
1400
- const v = parsedArgs[k];
1401
- let val = v;
1402
- if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
1403
- val = v == null ? void 0 : v["#text"];
1404
- }
1405
- if (Array.isArray(v)) {
1406
- val = v.map((item) => {
1407
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1408
- const textVal = item == null ? void 0 : item["#text"];
1409
- return typeof textVal === "string" ? textVal.trim() : textVal;
1410
- }
1411
- return typeof item === "string" ? item.trim() : item;
1412
- });
1413
- } else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
1414
- const obj = v;
1415
- const keys = Object.keys(obj);
1416
- if (keys.length === 1 && keys[0] === "item") {
1417
- const itemValue = obj.item;
1418
- if (Array.isArray(itemValue)) {
1419
- val = itemValue.map((item) => {
1420
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1421
- const textVal = item == null ? void 0 : item["#text"];
1422
- const trimmed2 = typeof textVal === "string" ? textVal.trim() : textVal;
1423
- if (typeof trimmed2 === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed2)) {
1424
- const num = Number(trimmed2);
1425
- if (Number.isFinite(num)) return num;
1426
- }
1427
- return trimmed2;
1428
- }
1429
- const trimmed = typeof item === "string" ? item.trim() : item;
1430
- if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1431
- const num = Number(trimmed);
1432
- if (Number.isFinite(num)) return num;
1433
- }
1434
- return trimmed;
1435
- });
1436
- } else {
1437
- const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
1438
- if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1439
- const num = Number(trimmed);
1440
- if (Number.isFinite(num)) {
1441
- val = num;
1442
- } else {
1443
- val = trimmed;
1444
- }
1445
- } else {
1446
- val = trimmed;
1447
- }
1448
- }
1449
- } else {
1450
- const isIndexedTuple = keys.length > 0 && keys.every((key) => /^\d+$/.test(key)) && (() => {
1451
- const indices = keys.map((k2) => parseInt(k2)).sort((a, b) => a - b);
1452
- return indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
1453
- })();
1454
- if (isIndexedTuple) {
1455
- const sortedKeys = keys.sort(
1456
- (a, b) => parseInt(a) - parseInt(b)
1457
- );
1458
- val = sortedKeys.map((key) => {
1459
- const item = obj[key];
1460
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1461
- const textVal = item == null ? void 0 : item["#text"];
1462
- return typeof textVal === "string" ? textVal.trim() : textVal;
1463
- }
1464
- return typeof item === "string" ? item.trim() : item;
1465
- });
1466
- } else {
1467
- val = v;
1468
- }
1469
- }
1470
- }
1471
- args[k] = typeof val === "string" ? val.trim() : val;
1472
- }
1473
- const originalSchema = originalSchemas[toolName];
1474
- const fallbackSchema = (_b = tools.find((t) => t.name === toolName)) == null ? void 0 : _b.inputSchema;
1475
- const schema = originalSchema || fallbackSchema;
1476
- const coercedArgs = coerceBySchema(args, schema);
1477
- processedElements.push({
1478
- type: "tool-call",
1479
- toolCallId: (0, import_provider_utils3.generateId)(),
1999
+ const toolSchema = getToolSchema(tools, originalSchemas, toolName);
2000
+ const { args, cancelToolCall } = processParsedArgs(
2001
+ parsedArgs,
2002
+ toolSchema,
2003
+ toolContent,
1480
2004
  toolName,
1481
- input: JSON.stringify(coercedArgs)
1482
- });
2005
+ options
2006
+ );
2007
+ if (cancelToolCall) {
2008
+ const originalCallText = match[0];
2009
+ (_b = options == null ? void 0 : options.onError) == null ? void 0 : _b.call(
2010
+ options,
2011
+ `Duplicate string tags detected; cancelling tool call`,
2012
+ { toolCall: originalCallText, toolName }
2013
+ );
2014
+ processedElements.push({ type: "text", text: originalCallText });
2015
+ } else {
2016
+ const coercedArgs = coerceBySchema(args, toolSchema);
2017
+ processedElements.push({
2018
+ type: "tool-call",
2019
+ toolCallId: (0, import_provider_utils3.generateId)(),
2020
+ toolName,
2021
+ input: JSON.stringify(coercedArgs)
2022
+ });
2023
+ }
1483
2024
  } catch (error) {
1484
2025
  const message = `Could not process XML tool call, keeping original text: ${match[0]}`;
1485
2026
  (_c = options == null ? void 0 : options.onError) == null ? void 0 : _c.call(options, message, { toolCall: match[0], toolName, error });
@@ -1524,7 +2065,7 @@ var morphXmlProtocol = () => ({
1524
2065
  };
1525
2066
  return new TransformStream({
1526
2067
  transform(chunk, controller) {
1527
- var _a, _b;
2068
+ var _a;
1528
2069
  if (chunk.type !== "text-delta") {
1529
2070
  if (buffer) flushText(controller);
1530
2071
  controller.enqueue(chunk);
@@ -1546,94 +2087,43 @@ var morphXmlProtocol = () => ({
1546
2087
  textNodeName: "#text"
1547
2088
  });
1548
2089
  const parsedArgs = ((_a = parser.parse(`<root>${toolContent}</root>`)) == null ? void 0 : _a.root) || {};
1549
- const args = {};
1550
- for (const k of Object.keys(parsedArgs || {})) {
1551
- const v = parsedArgs[k];
1552
- let val = v;
1553
- if (v && typeof v === "object" && Object.prototype.hasOwnProperty.call(v, "#text")) {
1554
- val = v == null ? void 0 : v["#text"];
1555
- }
1556
- if (Array.isArray(v)) {
1557
- val = v.map((item) => {
1558
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1559
- const textVal = item == null ? void 0 : item["#text"];
1560
- return typeof textVal === "string" ? textVal.trim() : textVal;
1561
- }
1562
- return typeof item === "string" ? item.trim() : item;
1563
- });
1564
- } else if (v && typeof v === "object" && !Object.prototype.hasOwnProperty.call(v, "#text")) {
1565
- const obj = v;
1566
- const keys = Object.keys(obj);
1567
- if (keys.length === 1 && keys[0] === "item") {
1568
- const itemValue = obj.item;
1569
- if (Array.isArray(itemValue)) {
1570
- val = itemValue.map((item) => {
1571
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1572
- const textVal = item == null ? void 0 : item["#text"];
1573
- const trimmed2 = typeof textVal === "string" ? textVal.trim() : textVal;
1574
- if (typeof trimmed2 === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed2)) {
1575
- const num = Number(trimmed2);
1576
- if (Number.isFinite(num)) return num;
1577
- }
1578
- return trimmed2;
1579
- }
1580
- const trimmed = typeof item === "string" ? item.trim() : item;
1581
- if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1582
- const num = Number(trimmed);
1583
- if (Number.isFinite(num)) return num;
1584
- }
1585
- return trimmed;
1586
- });
1587
- } else {
1588
- const trimmed = typeof itemValue === "string" ? itemValue.trim() : itemValue;
1589
- if (typeof trimmed === "string" && /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(trimmed)) {
1590
- const num = Number(trimmed);
1591
- if (Number.isFinite(num)) {
1592
- val = num;
1593
- } else {
1594
- val = trimmed;
1595
- }
1596
- } else {
1597
- val = trimmed;
1598
- }
1599
- }
1600
- } else {
1601
- const isIndexedTuple = keys.length > 0 && keys.every((key) => /^\d+$/.test(key)) && (() => {
1602
- const indices = keys.map((k2) => parseInt(k2)).sort((a, b) => a - b);
1603
- return indices[0] === 0 && indices.every((val2, idx) => val2 === idx);
1604
- })();
1605
- if (isIndexedTuple) {
1606
- const sortedKeys = keys.sort(
1607
- (a, b) => parseInt(a) - parseInt(b)
1608
- );
1609
- val = sortedKeys.map((key) => {
1610
- const item = obj[key];
1611
- if (item && typeof item === "object" && Object.prototype.hasOwnProperty.call(item, "#text")) {
1612
- const textVal = item == null ? void 0 : item["#text"];
1613
- return typeof textVal === "string" ? textVal.trim() : textVal;
1614
- }
1615
- return typeof item === "string" ? item.trim() : item;
1616
- });
1617
- } else {
1618
- val = v;
2090
+ const toolSchema = getToolSchema(
2091
+ tools,
2092
+ originalSchemas,
2093
+ currentToolCall.name
2094
+ );
2095
+ const { args, cancelToolCall } = processParsedArgs(
2096
+ parsedArgs,
2097
+ toolSchema,
2098
+ toolContent,
2099
+ currentToolCall.name,
2100
+ options
2101
+ );
2102
+ if (cancelToolCall) {
2103
+ const originalCallText = `<${currentToolCall.name}>${toolContent}</${currentToolCall.name}>`;
2104
+ if (options == null ? void 0 : options.onError) {
2105
+ options.onError(
2106
+ "Duplicate string tags detected; cancelling tool call",
2107
+ {
2108
+ toolCall: originalCallText,
2109
+ toolName: currentToolCall.name
1619
2110
  }
1620
- }
2111
+ );
1621
2112
  }
1622
- args[k] = typeof val === "string" ? val.trim() : val;
2113
+ flushText(controller, originalCallText);
2114
+ } else {
2115
+ const coercedArgs = coerceBySchema(
2116
+ args,
2117
+ toolSchema
2118
+ );
2119
+ flushText(controller);
2120
+ controller.enqueue({
2121
+ type: "tool-call",
2122
+ toolCallId: (0, import_provider_utils3.generateId)(),
2123
+ toolName: currentToolCall.name,
2124
+ input: JSON.stringify(coercedArgs)
2125
+ });
1623
2126
  }
1624
- const originalSchema = originalSchemas[currentToolCall.name];
1625
- const fallbackSchema = (_b = tools.find(
1626
- (t) => t.name === currentToolCall.name
1627
- )) == null ? void 0 : _b.inputSchema;
1628
- const toolSchema = originalSchema || fallbackSchema;
1629
- const coercedArgs = coerceBySchema(args, toolSchema);
1630
- flushText(controller);
1631
- controller.enqueue({
1632
- type: "tool-call",
1633
- toolCallId: (0, import_provider_utils3.generateId)(),
1634
- toolName: currentToolCall.name,
1635
- input: JSON.stringify(coercedArgs)
1636
- });
1637
2127
  } catch (e) {
1638
2128
  const originalCallText = `<${currentToolCall.name}>${toolContent}${endTag}`;
1639
2129
  if (options == null ? void 0 : options.onError) {