@lvce-editor/chat-tool-worker 9.8.0 → 9.10.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.
@@ -1538,7 +1538,7 @@ const normalizePattern = pattern => {
1538
1538
  matchDirectories
1539
1539
  };
1540
1540
  };
1541
- const joinUri = (baseUri, path) => {
1541
+ const joinUri$1 = (baseUri, path) => {
1542
1542
  if (!path) {
1543
1543
  return baseUri;
1544
1544
  }
@@ -1572,13 +1572,13 @@ const getBaseUriError = args => {
1572
1572
  return undefined;
1573
1573
  };
1574
1574
  const getDirectoryMatches = async (baseUri, globPart) => {
1575
- const dirUri = joinUri(baseUri, globPart);
1575
+ const dirUri = joinUri$1(baseUri, globPart);
1576
1576
  const entries = await readDirWithFileTypes(dirUri);
1577
1577
  return entries.map(entry => globPart ? `${globPart}/${entry.name}` : entry.name);
1578
1578
  };
1579
1579
  const getRecursiveMatches = async (baseUri, baseDir, normalizedPattern) => {
1580
1580
  const matches = [];
1581
- const baseDirUri = joinUri(baseUri, baseDir);
1581
+ const baseDirUri = joinUri$1(baseUri, baseDir);
1582
1582
  await traverseDirectory(baseDirUri, '', async (relativePath, entry) => {
1583
1583
  if (entry.type !== File) {
1584
1584
  return;
@@ -1591,7 +1591,7 @@ const getRecursiveMatches = async (baseUri, baseDir, normalizedPattern) => {
1591
1591
  return matches;
1592
1592
  };
1593
1593
  const getDirectMatches = async (baseUri, baseDir, normalizedPattern) => {
1594
- const dirUri = joinUri(baseUri, baseDir);
1594
+ const dirUri = joinUri$1(baseUri, baseDir);
1595
1595
  const entries = await readDirWithFileTypes(dirUri);
1596
1596
  const matches = [];
1597
1597
  for (const entry of entries) {
@@ -2199,6 +2199,152 @@ const executeRunInTerminalTool = async (args, _options) => {
2199
2199
  }
2200
2200
  };
2201
2201
 
2202
+ const WORD_CHARACTER_REGEX = /\w/;
2203
+ const isWholeWordMatch = (text, startIndex, matchLength) => {
2204
+ const beforeChar = startIndex > 0 ? text[startIndex - 1] : ' ';
2205
+ const afterChar = startIndex + matchLength < text.length ? text[startIndex + matchLength] : ' ';
2206
+ return !WORD_CHARACTER_REGEX.test(beforeChar) && !WORD_CHARACTER_REGEX.test(afterChar);
2207
+ };
2208
+ const createSearchResult = (uri, lineNumber, column, text) => {
2209
+ return {
2210
+ column,
2211
+ line: lineNumber,
2212
+ text,
2213
+ uri
2214
+ };
2215
+ };
2216
+ const searchPlainText = (text, uri, options) => {
2217
+ const results = [];
2218
+ const lines = text.split('\n');
2219
+ const needle = options.matchCase ? options.value : options.value.toLowerCase();
2220
+ if (needle === '') {
2221
+ return results;
2222
+ }
2223
+ for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
2224
+ const lineText = lines[lineNumber];
2225
+ const searchLineText = options.matchCase ? lineText : lineText.toLowerCase();
2226
+ let searchStartIndex = 0;
2227
+ while (searchStartIndex <= searchLineText.length) {
2228
+ const matchIndex = searchLineText.indexOf(needle, searchStartIndex);
2229
+ if (matchIndex === -1) {
2230
+ break;
2231
+ }
2232
+ if (!options.matchWholeWord || isWholeWordMatch(lineText, matchIndex, needle.length)) {
2233
+ results.push(createSearchResult(uri, lineNumber + 1, matchIndex + 1, lineText));
2234
+ }
2235
+ searchStartIndex = matchIndex + needle.length;
2236
+ }
2237
+ }
2238
+ return results;
2239
+ };
2240
+ const searchRegExp = (text, uri, options) => {
2241
+ const results = [];
2242
+ const lines = text.split('\n');
2243
+ const flags = options.matchCase ? 'g' : 'gi';
2244
+ const regex = new RegExp(options.value, flags);
2245
+ for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
2246
+ const lineText = lines[lineNumber];
2247
+ regex.lastIndex = 0;
2248
+ let match;
2249
+ while ((match = regex.exec(lineText)) !== null) {
2250
+ const matchedText = match[0];
2251
+ if (matchedText !== '' && (!options.matchWholeWord || isWholeWordMatch(lineText, match.index, matchedText.length))) {
2252
+ results.push(createSearchResult(uri, lineNumber + 1, match.index + 1, lineText));
2253
+ }
2254
+ if (matchedText === '') {
2255
+ regex.lastIndex++;
2256
+ }
2257
+ }
2258
+ }
2259
+ return results;
2260
+ };
2261
+ const searchInText = (text, uri, options) => {
2262
+ if (options.isRegex) {
2263
+ return searchRegExp(text, uri, options);
2264
+ }
2265
+ return searchPlainText(text, uri, options);
2266
+ };
2267
+
2268
+ const joinUri = (baseUri, path) => {
2269
+ if (!path) {
2270
+ return baseUri;
2271
+ }
2272
+ return baseUri.endsWith('/') ? `${baseUri}${path}` : `${baseUri}/${path}`;
2273
+ };
2274
+ const isExcludedPath = (path, exclude) => {
2275
+ return exclude.some(pattern => matchesGlobPattern(path, pattern));
2276
+ };
2277
+ const readDirEntries = async (uri, relativePath) => {
2278
+ try {
2279
+ return await readDirWithFileTypes(uri);
2280
+ } catch (error) {
2281
+ if (relativePath === '') {
2282
+ throw error;
2283
+ }
2284
+ return undefined;
2285
+ }
2286
+ };
2287
+ const searchFileContents = async (entryUri, searchOptions) => {
2288
+ try {
2289
+ const content = await readFile$1(entryUri);
2290
+ return searchInText(content, entryUri, searchOptions);
2291
+ } catch {
2292
+ // Ignore unreadable files and continue with remaining matches.
2293
+ return [];
2294
+ }
2295
+ };
2296
+ const visitDirEntry = async (uri, relativePath, dirent, searchOptions, visit) => {
2297
+ const entryPath = relativePath ? `${relativePath}/${dirent.name}` : dirent.name;
2298
+ if (isExcludedPath(entryPath, searchOptions.exclude)) {
2299
+ return [];
2300
+ }
2301
+ const entryUri = joinUri(uri, dirent.name);
2302
+ if (dirent.type === Directory) {
2303
+ if (!shouldExcludeDir(dirent.name)) {
2304
+ await visit(entryUri, entryPath);
2305
+ }
2306
+ return [];
2307
+ }
2308
+ if (dirent.type === File) {
2309
+ return searchFileContents(entryUri, searchOptions);
2310
+ }
2311
+ return [];
2312
+ };
2313
+ const sortSearchResults = results => {
2314
+ return results.toSorted((left, right) => {
2315
+ const uriComparison = left.uri.localeCompare(right.uri);
2316
+ if (uriComparison !== 0) {
2317
+ return uriComparison;
2318
+ }
2319
+ if (left.line !== right.line) {
2320
+ return left.line - right.line;
2321
+ }
2322
+ if (left.column !== right.column) {
2323
+ return left.column - right.column;
2324
+ }
2325
+ return left.text.localeCompare(right.text);
2326
+ });
2327
+ };
2328
+ const searchTextManual = async (workspaceUri, searchOptions) => {
2329
+ const results = [];
2330
+ const visited = new Set();
2331
+ const visit = async (uri, relativePath) => {
2332
+ if (visited.has(uri)) {
2333
+ return;
2334
+ }
2335
+ visited.add(uri);
2336
+ const dirents = await readDirEntries(uri, relativePath);
2337
+ if (!dirents) {
2338
+ return;
2339
+ }
2340
+ for (const dirent of dirents) {
2341
+ results.push(...(await visitDirEntry(uri, relativePath, dirent, searchOptions, visit)));
2342
+ }
2343
+ };
2344
+ await visit(workspaceUri, '');
2345
+ return sortSearchResults(results);
2346
+ };
2347
+
2202
2348
  const getSearchOptions = args => {
2203
2349
  const {
2204
2350
  options
@@ -2210,11 +2356,11 @@ const getSearchOptions = args => {
2210
2356
  const {
2211
2357
  exclude,
2212
2358
  isRegex,
2213
- machWholeWord,
2214
2359
  matchCase,
2360
+ matchWholeWord,
2215
2361
  value
2216
2362
  } = candidate;
2217
- if (typeof value !== 'string' || typeof isRegex !== 'boolean' || typeof matchCase !== 'boolean' || typeof machWholeWord !== 'boolean') {
2363
+ if (typeof value !== 'string' || typeof isRegex !== 'boolean' || typeof matchCase !== 'boolean' || typeof matchWholeWord !== 'boolean') {
2218
2364
  return undefined;
2219
2365
  }
2220
2366
  if (!Array.isArray(exclude) || exclude.some(item => typeof item !== 'string')) {
@@ -2223,33 +2369,44 @@ const getSearchOptions = args => {
2223
2369
  return {
2224
2370
  exclude,
2225
2371
  isRegex,
2226
- machWholeWord,
2227
2372
  matchCase,
2373
+ matchWholeWord,
2228
2374
  value
2229
2375
  };
2230
2376
  };
2377
+ const validateRegex = searchOptions => {
2378
+ if (!searchOptions.isRegex) {
2379
+ return undefined;
2380
+ }
2381
+ try {
2382
+ new RegExp(searchOptions.value);
2383
+ return undefined;
2384
+ } catch {
2385
+ return 'Invalid argument: options.value must be a valid regular expression.';
2386
+ }
2387
+ };
2231
2388
  const executeSearchTextTool = async (args, _options) => {
2232
2389
  const searchOptions = getSearchOptions(args);
2233
2390
  if (!searchOptions) {
2234
2391
  return {
2235
- error: 'Invalid argument: options must include value (string), isRegex (boolean), matchCase (boolean), machWholeWord (boolean), and exclude (string[]).'
2392
+ error: 'Invalid argument: options must include value (string), isRegex (boolean), matchCase (boolean), matchWholeWord (boolean), and exclude (string[]).'
2236
2393
  };
2237
2394
  }
2238
- const results = [{
2239
- column: 12,
2240
- line: 5,
2241
- text: `Mock match for "${searchOptions.value}" in src/main.ts`,
2242
- uri: 'file:///workspace/src/main.ts'
2243
- }, {
2244
- column: 3,
2245
- line: 18,
2246
- text: `Mock match for "${searchOptions.value}" in src/utils/search.ts`,
2247
- uri: 'file:///workspace/src/utils/search.ts'
2248
- }];
2249
- return {
2250
- options: searchOptions,
2251
- results
2252
- };
2395
+ const regexError = validateRegex(searchOptions);
2396
+ if (regexError) {
2397
+ return {
2398
+ error: regexError
2399
+ };
2400
+ }
2401
+ try {
2402
+ const workspaceUri = await getWorkspacePath();
2403
+ const results = await searchTextManual(workspaceUri, searchOptions);
2404
+ return {
2405
+ results
2406
+ };
2407
+ } catch (error) {
2408
+ return getToolErrorPayload(error);
2409
+ }
2253
2410
  };
2254
2411
 
2255
2412
  const placeholderResponse = 'Hello From Sub agent - not yet implemented';
@@ -2432,6 +2589,9 @@ const executeWriteFileTool = async (args, _options) => {
2432
2589
  };
2433
2590
 
2434
2591
  const parseToolArguments = rawArguments => {
2592
+ if (typeof rawArguments !== 'string' && (!rawArguments || typeof rawArguments !== 'object')) {
2593
+ return {};
2594
+ }
2435
2595
  if (typeof rawArguments !== 'string') {
2436
2596
  return rawArguments;
2437
2597
  }
@@ -2879,17 +3039,17 @@ const getSearchTextTool = () => {
2879
3039
  isRegex: {
2880
3040
  type: 'boolean'
2881
3041
  },
2882
- machWholeWord: {
3042
+ matchCase: {
2883
3043
  type: 'boolean'
2884
3044
  },
2885
- matchCase: {
3045
+ matchWholeWord: {
2886
3046
  type: 'boolean'
2887
3047
  },
2888
3048
  value: {
2889
3049
  type: 'string'
2890
3050
  }
2891
3051
  },
2892
- required: ['value', 'isRegex', 'matchCase', 'machWholeWord', 'exclude'],
3052
+ required: ['value', 'isRegex', 'matchCase', 'matchWholeWord', 'exclude'],
2893
3053
  type: 'object'
2894
3054
  }
2895
3055
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "9.8.0",
3
+ "version": "9.10.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",