@lvce-editor/chat-tool-worker 8.2.0 → 8.4.0

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.
@@ -1426,27 +1426,31 @@ const DEFAULT_EXCLUDE_DIRS = new Set(['.git', 'node_modules', '.cache', '.venv',
1426
1426
  const shouldExcludeDir = dirName => {
1427
1427
  return DEFAULT_EXCLUDE_DIRS.has(dirName);
1428
1428
  };
1429
- const traverseDirectory = async (baseUri, currentPath, onEntry, visited = new Set()) => {
1430
- const uri = currentPath === '' ? baseUri : `${baseUri}/${currentPath}`;
1431
- if (visited.has(uri)) {
1432
- return;
1433
- }
1434
- visited.add(uri);
1435
- try {
1436
- const entries = await readDirWithFileTypes(uri);
1437
- for (const entry of entries) {
1438
- const entryPath = currentPath === '' ? entry.name : `${currentPath}/${entry.name}`;
1439
- await onEntry(entryPath, entry);
1440
- if (entry.type === Directory && !shouldExcludeDir(entry.name)) {
1441
- await traverseDirectory(baseUri, entryPath, onEntry, visited);
1442
- }
1429
+ const traverseDirectory = async (baseUri, currentPath, onEntry) => {
1430
+ const visited = new Set();
1431
+ const visit = async path => {
1432
+ const uri = path === '' ? baseUri : `${baseUri}/${path}`;
1433
+ if (visited.has(uri)) {
1434
+ return;
1443
1435
  }
1444
- } catch (error) {
1445
- if (currentPath === '') {
1446
- throw error;
1436
+ visited.add(uri);
1437
+ try {
1438
+ const entries = await readDirWithFileTypes(uri);
1439
+ for (const entry of entries) {
1440
+ const entryPath = path === '' ? entry.name : `${path}/${entry.name}`;
1441
+ await onEntry(entryPath, entry);
1442
+ if (entry.type === Directory && !shouldExcludeDir(entry.name)) {
1443
+ await visit(entryPath);
1444
+ }
1445
+ }
1446
+ } catch (error) {
1447
+ if (path === '') {
1448
+ throw error;
1449
+ }
1450
+ // Ignore unreadable directories and continue with other matches.
1447
1451
  }
1448
- // Ignore unreadable directories and continue with other matches.
1449
- }
1452
+ };
1453
+ return visit(currentPath);
1450
1454
  };
1451
1455
 
1452
1456
  const MULTIPLE_SLASHES_REGEX = /\/+/g;
@@ -1521,7 +1525,6 @@ const executeGlobTool = async (args, _options) => {
1521
1525
  }
1522
1526
  } else if (globPart.includes('**')) {
1523
1527
  const baseDirUri = joinUri(baseUri, baseDir);
1524
- const visited = new Set();
1525
1528
  await traverseDirectory(baseDirUri, '', async (relativePath, entry) => {
1526
1529
  if (entry.type !== File) {
1527
1530
  return;
@@ -1530,7 +1533,7 @@ const executeGlobTool = async (args, _options) => {
1530
1533
  if (matchesGlobPattern(fullPath, normalizedPattern)) {
1531
1534
  matches.push(fullPath);
1532
1535
  }
1533
- }, visited);
1536
+ });
1534
1537
  } else {
1535
1538
  const dirUri = joinUri(baseUri, baseDir);
1536
1539
  const entries = await readDirWithFileTypes(dirUri);
@@ -1554,12 +1557,65 @@ const executeGlobTool = async (args, _options) => {
1554
1557
  }
1555
1558
  };
1556
1559
 
1557
- const formatSearchProcessResults = results => {
1560
+ const escapeXml = value => {
1561
+ return value.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;');
1562
+ };
1563
+ const formatTextMatches = matches => {
1564
+ const lines = [];
1565
+ for (const match of matches) {
1566
+ if (typeof match.lineNumber === 'number') {
1567
+ lines.push(`${match.path}:${match.lineNumber}:${match.text}`);
1568
+ } else {
1569
+ lines.push(`${match.path}: ${match.text}`);
1570
+ }
1571
+ }
1572
+ return lines.join('\n');
1573
+ };
1574
+ const formatXmlMatches = matches => {
1575
+ const lines = [`${matches.length} matches`];
1576
+ for (const match of matches) {
1577
+ const attributes = [`path="${escapeXml(match.path)}"`];
1578
+ if (typeof match.lineNumber === 'number') {
1579
+ attributes.push(`line="${match.lineNumber}"`);
1580
+ }
1581
+ lines.push(`<match ${attributes.join(' ')}>`);
1582
+ lines.push(escapeXml(match.text));
1583
+ lines.push('</match>');
1584
+ }
1585
+ return lines.join('\n');
1586
+ };
1587
+ const formatJsonMatches = matches => {
1588
+ return JSON.stringify({
1589
+ count: matches.length,
1590
+ matches: matches.map(match => ({
1591
+ ...(typeof match.lineNumber === 'number' ? {
1592
+ line: match.lineNumber
1593
+ } : {}),
1594
+ path: match.path,
1595
+ text: match.text
1596
+ }))
1597
+ }, undefined, 2);
1598
+ };
1599
+ const formatGrepMatches = (matches, outputFormat) => {
1600
+ if (matches.length === 0) {
1601
+ return 'No matches found.';
1602
+ }
1603
+ switch (outputFormat) {
1604
+ case 'json':
1605
+ return formatJsonMatches(matches);
1606
+ case 'xml':
1607
+ return formatXmlMatches(matches);
1608
+ default:
1609
+ return formatTextMatches(matches);
1610
+ }
1611
+ };
1612
+
1613
+ const formatSearchProcessResults = (results, outputFormat) => {
1558
1614
  if (!results || results.length === 0) {
1559
1615
  return 'No matches found.';
1560
1616
  }
1561
1617
  let currentFile = '';
1562
- const lines = [];
1618
+ const matches = [];
1563
1619
  for (const result of results) {
1564
1620
  if (result.type === 1) {
1565
1621
  currentFile = result.text;
@@ -1567,16 +1623,24 @@ const formatSearchProcessResults = results => {
1567
1623
  }
1568
1624
  if (result.type === 2) {
1569
1625
  if (currentFile) {
1570
- lines.push(`${currentFile}:${result.lineNumber}:${result.text}`);
1626
+ matches.push({
1627
+ lineNumber: result.lineNumber,
1628
+ path: currentFile,
1629
+ text: result.text
1630
+ });
1571
1631
  } else {
1572
- lines.push(`${result.lineNumber}:${result.text}`);
1632
+ matches.push({
1633
+ lineNumber: result.lineNumber,
1634
+ path: '',
1635
+ text: result.text
1636
+ });
1573
1637
  }
1574
1638
  }
1575
1639
  }
1576
- if (lines.length === 0) {
1640
+ if (matches.length === 0) {
1577
1641
  return 'No matches found.';
1578
1642
  }
1579
- return lines.join('\n');
1643
+ return formatGrepMatches(matches, outputFormat);
1580
1644
  };
1581
1645
 
1582
1646
  const windowsDrivePathRegex = /^[a-zA-Z]:[\\/]/;
@@ -1648,7 +1712,7 @@ const executeFileGrepSearch = async (workspaceUri, grepSearchArgs) => {
1648
1712
  });
1649
1713
  return {
1650
1714
  arguments: grepSearchArgs,
1651
- result: formatSearchProcessResults(result.results),
1715
+ result: formatSearchProcessResults(result.results, grepSearchArgs.outputFormat),
1652
1716
  workspaceUri,
1653
1717
  ...(result.limitHit ? {
1654
1718
  warning: 'Search result limit reached.'
@@ -1656,19 +1720,25 @@ const executeFileGrepSearch = async (workspaceUri, grepSearchArgs) => {
1656
1720
  };
1657
1721
  };
1658
1722
 
1659
- const formatLegacyMemorySearchResults = results => {
1660
- const lines = [];
1723
+ const formatLegacyMemorySearchResults = (results, outputFormat) => {
1724
+ const matches = [];
1661
1725
  for (const result of results) {
1662
- const [path, matches] = result;
1663
- for (const match of matches) {
1726
+ const [path, resultMatches] = result;
1727
+ for (const match of resultMatches) {
1664
1728
  const preview = typeof match?.preview === 'string' ? match.preview : '';
1665
- lines.push(preview ? `${path}: ${preview}` : path);
1729
+ matches.push(preview ? {
1730
+ path,
1731
+ text: preview
1732
+ } : {
1733
+ path,
1734
+ text: ''
1735
+ });
1666
1736
  }
1667
1737
  }
1668
- if (lines.length === 0) {
1738
+ if (matches.length === 0) {
1669
1739
  return 'No matches found.';
1670
1740
  }
1671
- return lines.join('\n');
1741
+ return formatGrepMatches(matches, outputFormat);
1672
1742
  };
1673
1743
 
1674
1744
  const isLegacyMemorySearchResult = value => {
@@ -1690,7 +1760,7 @@ const executeMemoryGrepSearch = async (workspaceUri, grepSearchArgs, options) =>
1690
1760
  const result = await invoke('ExtensionHostTextSearch.textSearchMemory2', searchOptions.scheme, workspaceUri, grepSearchArgs.query, searchOptions, options.assetDir);
1691
1761
  return {
1692
1762
  arguments: grepSearchArgs,
1693
- result: formatLegacyMemorySearchResults(result.results || []),
1763
+ result: formatLegacyMemorySearchResults(result.results || [], grepSearchArgs.outputFormat),
1694
1764
  workspaceUri,
1695
1765
  ...(result.limitHit ? {
1696
1766
  warning: 'Search result limit reached.'
@@ -1700,7 +1770,7 @@ const executeMemoryGrepSearch = async (workspaceUri, grepSearchArgs, options) =>
1700
1770
  const legacyResults = await invoke('ExtensionHostTextSearch.textSearchMemory', searchOptions.scheme, workspaceUri, grepSearchArgs.query, searchOptions, options.assetDir);
1701
1771
  return {
1702
1772
  arguments: grepSearchArgs,
1703
- result: isLegacyMemorySearchResult(legacyResults) ? formatLegacyMemorySearchResults(legacyResults) : 'No matches found.',
1773
+ result: isLegacyMemorySearchResult(legacyResults) ? formatLegacyMemorySearchResults(legacyResults, grepSearchArgs.outputFormat) : 'No matches found.',
1704
1774
  workspaceUri
1705
1775
  };
1706
1776
  }
@@ -1712,6 +1782,7 @@ const getGrepSearchArgs = args => {
1712
1782
  includePattern,
1713
1783
  isRegexp,
1714
1784
  maxResults,
1785
+ outputFormat,
1715
1786
  query
1716
1787
  } = args;
1717
1788
  if (typeof query !== 'string' || typeof isRegexp !== 'boolean') {
@@ -1726,6 +1797,9 @@ const getGrepSearchArgs = args => {
1726
1797
  if (includeIgnoredFiles !== undefined && typeof includeIgnoredFiles !== 'boolean') {
1727
1798
  return undefined;
1728
1799
  }
1800
+ if (outputFormat !== undefined && outputFormat !== 'xml' && outputFormat !== 'json') {
1801
+ return undefined;
1802
+ }
1729
1803
  return {
1730
1804
  ...(includeIgnoredFiles === undefined ? {} : {
1731
1805
  includeIgnoredFiles
@@ -1736,12 +1810,15 @@ const getGrepSearchArgs = args => {
1736
1810
  ...(maxResults === undefined ? {} : {
1737
1811
  maxResults
1738
1812
  }),
1813
+ ...(outputFormat === undefined ? {} : {
1814
+ outputFormat
1815
+ }),
1739
1816
  isRegexp,
1740
1817
  query
1741
1818
  };
1742
1819
  };
1743
1820
 
1744
- const grepSearchArgumentError = 'Invalid argument: grep_search requires query (string), isRegexp (boolean), optional includePattern (string), optional maxResults (number), and optional includeIgnoredFiles (boolean).';
1821
+ const grepSearchArgumentError = 'Invalid argument: grep_search requires query (string), isRegexp (boolean), optional includePattern (string), optional maxResults (number), optional includeIgnoredFiles (boolean), and optional outputFormat ("xml" | "json").';
1745
1822
 
1746
1823
  const executeGrepSearchTool = async (args, options) => {
1747
1824
  const grepSearchArgs = getGrepSearchArgs(args);
@@ -1871,8 +1948,6 @@ const executeReadFileTool = async (args, _options) => {
1871
1948
  }
1872
1949
  };
1873
1950
 
1874
- /* eslint-disable @cspell/spellchecker */
1875
- /* eslint-disable e18e/prefer-url-canparse */
1876
1951
  const executeRenameTool = async (args, _options) => {
1877
1952
  const oldUri = typeof args.oldUri === 'string' ? args.oldUri : '';
1878
1953
  const newUri = typeof args.newUri === 'string' ? args.newUri : '';
@@ -1886,26 +1961,25 @@ const executeRenameTool = async (args, _options) => {
1886
1961
  error: 'Invalid argument: newUri must be an absolute URI.'
1887
1962
  };
1888
1963
  }
1889
- try {
1890
- new URL(oldUri);
1891
- new URL(newUri);
1892
- } catch {
1964
+ if (!URL.canParse(oldUri) || !URL.canParse(newUri)) {
1893
1965
  return {
1894
1966
  error: 'Invalid argument: invalid URL.'
1895
1967
  };
1896
1968
  }
1969
+ const oldUrl = new URL(oldUri);
1970
+ const newUrl = new URL(newUri);
1897
1971
  try {
1898
- await invoke('FileSystem.rename', oldUri, newUri);
1972
+ await invoke('FileSystem.rename', oldUrl.toString(), newUrl.toString());
1899
1973
  return {
1900
- newUri,
1974
+ newUri: newUrl.toString(),
1901
1975
  ok: true,
1902
- oldUri
1976
+ oldUri: oldUrl.toString()
1903
1977
  };
1904
1978
  } catch (error) {
1905
1979
  return {
1906
1980
  ...getToolErrorPayload(error),
1907
- newUri,
1908
- oldUri
1981
+ newUri: newUrl.toString(),
1982
+ oldUri: oldUrl.toString()
1909
1983
  };
1910
1984
  }
1911
1985
  };
@@ -2446,6 +2520,11 @@ const getGrepSearchTool = () => {
2446
2520
  description: "The maximum number of results to return. Do not use this unless necessary, it can slow things down. By default, only some matches are returned. If you use this and don't see what you're looking for, you can try again with a more specific query or a larger maxResults.",
2447
2521
  type: 'number'
2448
2522
  },
2523
+ outputFormat: {
2524
+ description: 'Optional structured output format. Use "xml" for match tags or "json" for machine-readable JSON. When omitted, grep_search returns plain text lines.',
2525
+ enum: ['xml', 'json'],
2526
+ type: 'string'
2527
+ },
2449
2528
  query: {
2450
2529
  description: "The pattern to search for in files in the workspace. Use regex with alternation (e.g., 'word1|word2|word3') or character classes to find multiple potential words in a single search. Be sure to set the isRegexp property properly to declare whether it's a regex or plain text pattern. Is case-insensitive.",
2451
2530
  type: 'string'
@@ -2781,9 +2860,14 @@ const handleMessagePort = async port => {
2781
2860
  });
2782
2861
  };
2783
2862
 
2863
+ const initialize = async () => {
2864
+ // TODO
2865
+ };
2866
+
2784
2867
  const commandMap = {
2785
2868
  'ChatTool.execute': executeChatTool,
2786
2869
  'ChatTool.getTools': getBasicChatTools,
2870
+ 'ChatTool.initialize': initialize,
2787
2871
  'HandleMessagePort.handleMessagePort': handleMessagePort
2788
2872
  };
2789
2873
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "8.2.0",
3
+ "version": "8.4.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",