@lvce-editor/chat-tool-worker 9.5.0 → 9.6.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.
@@ -1366,6 +1366,27 @@ const MULTIPLE_SLASHES_REGEX$1 = /\/+/g;
1366
1366
  const escapeRegExp = str => {
1367
1367
  return str.replaceAll(REGEX_SPECIAL_CHARS_REGEX, '\\$&');
1368
1368
  };
1369
+ const getCharacterClassRegex = (classContents, negated) => {
1370
+ if (negated) {
1371
+ return `[^${escapeRegExp(classContents)}]`;
1372
+ }
1373
+ return `[${classContents.replaceAll('\\', '\\\\')}]`;
1374
+ };
1375
+ const readCharacterClass = (segment, index) => {
1376
+ let classIndex = index + 1;
1377
+ const negated = segment[classIndex] === '!';
1378
+ if (negated) {
1379
+ classIndex++;
1380
+ }
1381
+ const closingIndex = segment.indexOf(']', classIndex);
1382
+ if (closingIndex === -1) {
1383
+ return undefined;
1384
+ }
1385
+ return {
1386
+ nextIndex: closingIndex + 1,
1387
+ regex: getCharacterClassRegex(segment.slice(classIndex, closingIndex), negated)
1388
+ };
1389
+ };
1369
1390
 
1370
1391
  /*
1371
1392
  * Convert a glob pattern to a regex, but only for segments between separators
@@ -1375,60 +1396,58 @@ const segmentToRegex = segment => {
1375
1396
  let i = 0;
1376
1397
  while (i < segment.length) {
1377
1398
  const char = segment[i];
1378
- switch (char) {
1379
- case '?':
1380
- {
1381
- regexStr += '[^/]';
1382
- i++;
1383
- break;
1384
- }
1385
- case '[':
1386
- {
1387
- let j = i + 1;
1388
- let negated = false;
1389
- if (j < segment.length && segment[j] === '!') {
1390
- negated = true;
1391
- j++;
1392
- }
1393
- let classStr = '';
1394
- while (j < segment.length && segment[j] !== ']') {
1395
- classStr += segment[j];
1396
- j++;
1397
- }
1398
- if (j < segment.length) {
1399
- if (negated) {
1400
- regexStr += `[^${escapeRegExp(classStr)}]`;
1401
- } else {
1402
- regexStr += `[${classStr.replaceAll('\\', '\\\\')}]`;
1403
- }
1404
- i = j + 1;
1405
- } else {
1406
- regexStr += escapeRegExp(char);
1407
- i++;
1408
- }
1409
- break;
1410
- }
1411
- case '*':
1412
- {
1413
- regexStr += '[^/]*';
1414
- i++;
1415
- break;
1416
- }
1417
- default:
1418
- {
1419
- regexStr += escapeRegExp(char);
1420
- i++;
1421
- }
1399
+ if (char === '?') {
1400
+ regexStr += '[^/]';
1401
+ i++;
1402
+ continue;
1422
1403
  }
1404
+ if (char === '*') {
1405
+ regexStr += '[^/]*';
1406
+ i++;
1407
+ continue;
1408
+ }
1409
+ if (char === '[') {
1410
+ const characterClass = readCharacterClass(segment, i);
1411
+ if (characterClass) {
1412
+ regexStr += characterClass.regex;
1413
+ i = characterClass.nextIndex;
1414
+ continue;
1415
+ }
1416
+ }
1417
+ regexStr += escapeRegExp(char);
1418
+ i++;
1423
1419
  }
1424
1420
  return new RegExp(`^${regexStr}$`);
1425
1421
  };
1426
- const matchesGlobPattern = (path, pattern) => {
1427
- const normalizedPath = path.replaceAll('\\', '/').replaceAll(LEADING_DOT_SLASH_REGEX$1, '');
1428
- let normalizedPattern = pattern.replaceAll('\\', '/').replaceAll(LEADING_DOT_SLASH_REGEX$1, '').replaceAll(MULTIPLE_SLASHES_REGEX$1, '/');
1429
- if (normalizedPattern.endsWith('/')) {
1430
- normalizedPattern = normalizedPattern.slice(0, -1) + '/*';
1422
+ const normalizePath = path => {
1423
+ return path.replaceAll('\\', '/').replaceAll(LEADING_DOT_SLASH_REGEX$1, '');
1424
+ };
1425
+ const normalizePattern$1 = pattern => {
1426
+ const normalizedPattern = pattern.replaceAll('\\', '/').replaceAll(LEADING_DOT_SLASH_REGEX$1, '').replaceAll(MULTIPLE_SLASHES_REGEX$1, '/');
1427
+ if (!normalizedPattern.endsWith('/')) {
1428
+ return normalizedPattern;
1429
+ }
1430
+ return normalizedPattern.slice(0, -1) + '/*';
1431
+ };
1432
+ const matchesSinglePatternPart = (pathPart, patternPart) => {
1433
+ return segmentToRegex(patternPart).test(pathPart);
1434
+ };
1435
+ const matchesEmbeddedDoubleStarPattern = (pathParts, patternParts, pathIdx, patternIdx) => {
1436
+ const patternPart = patternParts[patternIdx];
1437
+ const pathPart = pathParts[pathIdx];
1438
+ return matchesSinglePatternPart(pathPart, patternPart.replace('**', '')) && matchesPatternParts(pathParts, patternParts, pathIdx + 1, patternIdx + 1);
1439
+ };
1440
+ const matchesDoubleStarPattern = (pathParts, patternParts, pathIdx, patternIdx) => {
1441
+ for (let i = pathIdx; i <= pathParts.length; i++) {
1442
+ if (matchesPatternParts(pathParts, patternParts, i, patternIdx + 1)) {
1443
+ return true;
1444
+ }
1431
1445
  }
1446
+ return false;
1447
+ };
1448
+ const matchesGlobPattern = (path, pattern) => {
1449
+ const normalizedPath = normalizePath(path);
1450
+ const normalizedPattern = normalizePattern$1(pattern);
1432
1451
  const patternParts = normalizedPattern.split('/');
1433
1452
  const pathParts = normalizedPath.split('/');
1434
1453
  return matchesPatternParts(pathParts, patternParts, 0, 0);
@@ -1441,34 +1460,17 @@ const matchesPatternParts = (pathParts, patternParts, pathIdx, patternIdx) => {
1441
1460
  return false;
1442
1461
  }
1443
1462
  const patternPart = patternParts[patternIdx];
1444
- if (patternPart === undefined) {
1445
- return false;
1446
- }
1447
1463
  if (patternPart === '**') {
1448
- for (let i = pathIdx; i <= pathParts.length; i++) {
1449
- if (matchesPatternParts(pathParts, patternParts, i, patternIdx + 1)) {
1450
- return true;
1451
- }
1452
- }
1453
- return false;
1464
+ return matchesDoubleStarPattern(pathParts, patternParts, pathIdx, patternIdx);
1454
1465
  }
1455
1466
  if (pathIdx === pathParts.length) {
1456
1467
  return false;
1457
1468
  }
1458
1469
  if (patternPart.includes('**')) {
1459
- const regex = segmentToRegex(patternPart.replace('**', ''));
1460
- const pathPart = pathParts[pathIdx];
1461
- if (pathPart !== undefined && regex.test(pathPart)) {
1462
- return matchesPatternParts(pathParts, patternParts, pathIdx + 1, patternIdx + 1);
1463
- }
1464
- return false;
1470
+ return matchesEmbeddedDoubleStarPattern(pathParts, patternParts, pathIdx, patternIdx);
1465
1471
  }
1466
- const regex = segmentToRegex(patternPart);
1467
1472
  const pathPart = pathParts[pathIdx];
1468
- if (pathPart !== undefined && regex.test(pathPart)) {
1469
- return matchesPatternParts(pathParts, patternParts, pathIdx + 1, patternIdx + 1);
1470
- }
1471
- return false;
1473
+ return matchesSinglePatternPart(pathPart, patternPart) && matchesPatternParts(pathParts, patternParts, pathIdx + 1, patternIdx + 1);
1472
1474
  };
1473
1475
 
1474
1476
  // cspell:ignore venv
@@ -1540,59 +1542,99 @@ const joinUri = (baseUri, path) => {
1540
1542
  }
1541
1543
  return baseUri.endsWith('/') ? `${baseUri}${path}` : `${baseUri}/${path}`;
1542
1544
  };
1543
- const executeGlobTool = async (args, _options) => {
1545
+ const isPlaceholderWorkspaceUri = baseUri => {
1546
+ return baseUri === PLACEHOLDER_WORKSPACE_URI || baseUri.startsWith(`${PLACEHOLDER_WORKSPACE_URI}/`);
1547
+ };
1548
+ const getPattern = args => {
1544
1549
  const pattern = typeof args.pattern === 'string' ? args.pattern : '';
1545
- if (!pattern) {
1546
- return {
1547
- error: 'Invalid argument: pattern must be a non-empty string.'
1548
- };
1550
+ return pattern || undefined;
1551
+ };
1552
+ const getBaseUri = args => {
1553
+ const baseUri = typeof args.baseUri === 'string' ? args.baseUri : '';
1554
+ if (!baseUri || !isAbsoluteUri(baseUri) || isPlaceholderWorkspaceUri(baseUri)) {
1555
+ return undefined;
1549
1556
  }
1557
+ return baseUri;
1558
+ };
1559
+ const getBaseUriError = args => {
1550
1560
  const baseUri = typeof args.baseUri === 'string' ? args.baseUri : '';
1551
1561
  if (!baseUri || !isAbsoluteUri(baseUri)) {
1552
1562
  return getInvalidUriErrorPayload('baseUri');
1553
1563
  }
1554
- if (baseUri === PLACEHOLDER_WORKSPACE_URI || baseUri.startsWith(`${PLACEHOLDER_WORKSPACE_URI}/`)) {
1564
+ if (isPlaceholderWorkspaceUri(baseUri)) {
1555
1565
  return {
1556
1566
  baseUri,
1557
1567
  error: 'Invalid argument: baseUri must be a real workspace folder URI. Call getWorkspaceUri first and use the returned workspaceUri value.'
1558
1568
  };
1559
1569
  }
1570
+ return undefined;
1571
+ };
1572
+ const getDirectoryMatches = async (baseUri, globPart) => {
1573
+ const dirUri = joinUri(baseUri, globPart);
1574
+ const entries = await readDirWithFileTypes(dirUri);
1575
+ return entries.map(entry => globPart ? `${globPart}/${entry.name}` : entry.name);
1576
+ };
1577
+ const getRecursiveMatches = async (baseUri, baseDir, normalizedPattern) => {
1578
+ const matches = [];
1579
+ const baseDirUri = joinUri(baseUri, baseDir);
1580
+ await traverseDirectory(baseDirUri, '', async (relativePath, entry) => {
1581
+ if (entry.type !== File) {
1582
+ return;
1583
+ }
1584
+ const fullPath = baseDir ? `${baseDir}/${relativePath}` : relativePath;
1585
+ if (matchesGlobPattern(fullPath, normalizedPattern)) {
1586
+ matches.push(fullPath);
1587
+ }
1588
+ });
1589
+ return matches;
1590
+ };
1591
+ const getDirectMatches = async (baseUri, baseDir, normalizedPattern) => {
1592
+ const dirUri = joinUri(baseUri, baseDir);
1593
+ const entries = await readDirWithFileTypes(dirUri);
1594
+ const matches = [];
1595
+ for (const entry of entries) {
1596
+ const fullPath = baseDir ? `${baseDir}/${entry.name}` : entry.name;
1597
+ if (matchesGlobPattern(fullPath, normalizedPattern)) {
1598
+ matches.push(fullPath);
1599
+ }
1600
+ }
1601
+ return matches;
1602
+ };
1603
+ const getMatches = async (baseUri, pattern) => {
1560
1604
  const normalizedPattern = normalizeInputPattern(pattern);
1561
1605
  const {
1562
1606
  baseDir,
1563
1607
  globPart,
1564
1608
  matchDirectories
1565
1609
  } = normalizePattern(pattern);
1566
- const matches = [];
1610
+ if (matchDirectories) {
1611
+ return getDirectoryMatches(baseUri, globPart);
1612
+ }
1613
+ if (globPart.includes('**')) {
1614
+ return getRecursiveMatches(baseUri, baseDir, normalizedPattern);
1615
+ }
1616
+ return getDirectMatches(baseUri, baseDir, normalizedPattern);
1617
+ };
1618
+ const sortMatches = matches => {
1619
+ return matches.toSorted((left, right) => left.localeCompare(right));
1620
+ };
1621
+ const executeGlobTool = async (args, _options) => {
1622
+ const pattern = getPattern(args);
1623
+ if (!pattern) {
1624
+ return {
1625
+ error: 'Invalid argument: pattern must be a non-empty string.'
1626
+ };
1627
+ }
1628
+ const baseUriError = getBaseUriError(args);
1629
+ if (baseUriError) {
1630
+ return baseUriError;
1631
+ }
1632
+ const baseUri = getBaseUri(args);
1633
+ if (!baseUri) {
1634
+ return getInvalidUriErrorPayload('baseUri');
1635
+ }
1567
1636
  try {
1568
- if (matchDirectories) {
1569
- const dirUri = joinUri(baseUri, globPart);
1570
- const entries = await readDirWithFileTypes(dirUri);
1571
- for (const entry of entries) {
1572
- matches.push(globPart ? `${globPart}/${entry.name}` : entry.name);
1573
- }
1574
- } else if (globPart.includes('**')) {
1575
- const baseDirUri = joinUri(baseUri, baseDir);
1576
- await traverseDirectory(baseDirUri, '', async (relativePath, entry) => {
1577
- if (entry.type !== File) {
1578
- return;
1579
- }
1580
- const fullPath = baseDir ? `${baseDir}/${relativePath}` : relativePath;
1581
- if (matchesGlobPattern(fullPath, normalizedPattern)) {
1582
- matches.push(fullPath);
1583
- }
1584
- });
1585
- } else {
1586
- const dirUri = joinUri(baseUri, baseDir);
1587
- const entries = await readDirWithFileTypes(dirUri);
1588
- for (const entry of entries) {
1589
- const fullPath = baseDir ? `${baseDir}/${entry.name}` : entry.name;
1590
- if (matchesGlobPattern(fullPath, normalizedPattern)) {
1591
- matches.push(fullPath);
1592
- }
1593
- }
1594
- }
1595
- matches.sort();
1637
+ const matches = sortMatches(await getMatches(baseUri, pattern));
1596
1638
  return {
1597
1639
  paths: matches,
1598
1640
  pattern
@@ -1645,10 +1687,7 @@ const formatJsonMatches = matches => {
1645
1687
  matchesFound: matches.length > 0
1646
1688
  };
1647
1689
  };
1648
- const formatGrepMatches = (matches, outputFormat) => {
1649
- if (outputFormat === 'json') {
1650
- return formatJsonMatches(matches);
1651
- }
1690
+ const formatStringMatches = (matches, outputFormat) => {
1652
1691
  if (matches.length === 0) {
1653
1692
  return 'No matches found.';
1654
1693
  }
@@ -1659,6 +1698,16 @@ const formatGrepMatches = (matches, outputFormat) => {
1659
1698
  return formatTextMatches(matches);
1660
1699
  }
1661
1700
  };
1701
+ const getMatchesFormatter = outputFormat => {
1702
+ if (outputFormat === 'json') {
1703
+ return formatJsonMatches;
1704
+ }
1705
+ return matches => formatStringMatches(matches, outputFormat);
1706
+ };
1707
+ const formatGrepMatches = (matches, outputFormat) => {
1708
+ const formatter = getMatchesFormatter(outputFormat);
1709
+ return formatter(matches);
1710
+ };
1662
1711
 
1663
1712
  const formatSearchProcessResults = (results, outputFormat) => {
1664
1713
  if (!results || results.length === 0) {
@@ -1831,6 +1880,21 @@ const executeMemoryGrepSearch = async (workspaceUri, grepSearchArgs, options) =>
1831
1880
  }
1832
1881
  };
1833
1882
 
1883
+ const isBoolean = value => {
1884
+ return typeof value === 'boolean';
1885
+ };
1886
+ const isNumber = value => {
1887
+ return typeof value === 'number';
1888
+ };
1889
+ const isString = value => {
1890
+ return typeof value === 'string';
1891
+ };
1892
+ const isOptional = (value, predicate) => {
1893
+ return value === undefined || predicate(value);
1894
+ };
1895
+ const isOutputFormat = value => {
1896
+ return value === 'xml' || value === 'json';
1897
+ };
1834
1898
  const getGrepSearchArgs = args => {
1835
1899
  const {
1836
1900
  includeIgnoredFiles,
@@ -1841,22 +1905,22 @@ const getGrepSearchArgs = args => {
1841
1905
  query,
1842
1906
  useDefaultExcludes
1843
1907
  } = args;
1844
- if (typeof query !== 'string' || typeof isRegexp !== 'boolean') {
1908
+ if (!isString(query) || !isBoolean(isRegexp)) {
1845
1909
  return undefined;
1846
1910
  }
1847
- if (includePattern !== undefined && typeof includePattern !== 'string') {
1911
+ if (!isOptional(includePattern, isString)) {
1848
1912
  return undefined;
1849
1913
  }
1850
- if (maxResults !== undefined && typeof maxResults !== 'number') {
1914
+ if (!isOptional(maxResults, isNumber)) {
1851
1915
  return undefined;
1852
1916
  }
1853
- if (includeIgnoredFiles !== undefined && typeof includeIgnoredFiles !== 'boolean') {
1917
+ if (!isOptional(includeIgnoredFiles, isBoolean)) {
1854
1918
  return undefined;
1855
1919
  }
1856
- if (useDefaultExcludes !== undefined && typeof useDefaultExcludes !== 'boolean') {
1920
+ if (!isOptional(useDefaultExcludes, isBoolean)) {
1857
1921
  return undefined;
1858
1922
  }
1859
- if (outputFormat !== undefined && outputFormat !== 'xml' && outputFormat !== 'json') {
1923
+ if (!isOptional(outputFormat, isOutputFormat)) {
1860
1924
  return undefined;
1861
1925
  }
1862
1926
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "9.5.0",
3
+ "version": "9.6.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",