@lvce-editor/chat-tool-worker 8.0.0 → 8.2.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.
@@ -1127,6 +1127,10 @@ const create = rpcId => {
1127
1127
  };
1128
1128
  };
1129
1129
 
1130
+ const Directory = 3;
1131
+ const File = 7;
1132
+ const Symlink = 9;
1133
+
1130
1134
  const ChatStorageWorker = 6003;
1131
1135
  const FileSystemWorker = 209;
1132
1136
  const RendererWorker = 1;
@@ -1136,8 +1140,21 @@ const {
1136
1140
  } = create(ChatStorageWorker);
1137
1141
 
1138
1142
  const {
1143
+ invoke: invoke$1,
1139
1144
  set: set$1
1140
1145
  } = create(FileSystemWorker);
1146
+ const readDirWithFileTypes = async uri => {
1147
+ return invoke$1('FileSystem.readDirWithFileTypes', uri);
1148
+ };
1149
+ const readFile$1 = async uri => {
1150
+ return invoke$1('FileSystem.readFile', uri);
1151
+ };
1152
+ const writeFile$1 = async (uri, content) => {
1153
+ return invoke$1('FileSystem.writeFile', uri, content);
1154
+ };
1155
+ const exists = async uri => {
1156
+ return invoke$1('FileSystem.exists', uri);
1157
+ };
1141
1158
 
1142
1159
  const {
1143
1160
  invoke,
@@ -1416,15 +1433,18 @@ const traverseDirectory = async (baseUri, currentPath, onEntry, visited = new Se
1416
1433
  }
1417
1434
  visited.add(uri);
1418
1435
  try {
1419
- const entries = await invoke('FileSystem.readDirWithFileTypes', uri);
1436
+ const entries = await readDirWithFileTypes(uri);
1420
1437
  for (const entry of entries) {
1421
1438
  const entryPath = currentPath === '' ? entry.name : `${currentPath}/${entry.name}`;
1422
1439
  await onEntry(entryPath, entry);
1423
- if (entry.isDirectory() && !entry.isSymbolicLink() && !shouldExcludeDir(entry.name)) {
1440
+ if (entry.type === Directory && !shouldExcludeDir(entry.name)) {
1424
1441
  await traverseDirectory(baseUri, entryPath, onEntry, visited);
1425
1442
  }
1426
1443
  }
1427
- } catch {
1444
+ } catch (error) {
1445
+ if (currentPath === '') {
1446
+ throw error;
1447
+ }
1428
1448
  // Ignore unreadable directories and continue with other matches.
1429
1449
  }
1430
1450
  };
@@ -1495,7 +1515,7 @@ const executeGlobTool = async (args, _options) => {
1495
1515
  try {
1496
1516
  if (matchDirectories) {
1497
1517
  const dirUri = joinUri(baseUri, globPart);
1498
- const entries = await invoke('FileSystem.readDirWithFileTypes', dirUri);
1518
+ const entries = await readDirWithFileTypes(dirUri);
1499
1519
  for (const entry of entries) {
1500
1520
  matches.push(globPart ? `${globPart}/${entry.name}` : entry.name);
1501
1521
  }
@@ -1503,7 +1523,7 @@ const executeGlobTool = async (args, _options) => {
1503
1523
  const baseDirUri = joinUri(baseUri, baseDir);
1504
1524
  const visited = new Set();
1505
1525
  await traverseDirectory(baseDirUri, '', async (relativePath, entry) => {
1506
- if (!entry.isFile()) {
1526
+ if (entry.type !== File) {
1507
1527
  return;
1508
1528
  }
1509
1529
  const fullPath = baseDir ? `${baseDir}/${relativePath}` : relativePath;
@@ -1513,7 +1533,7 @@ const executeGlobTool = async (args, _options) => {
1513
1533
  }, visited);
1514
1534
  } else {
1515
1535
  const dirUri = joinUri(baseUri, baseDir);
1516
- const entries = await invoke('FileSystem.readDirWithFileTypes', dirUri);
1536
+ const entries = await readDirWithFileTypes(dirUri);
1517
1537
  for (const entry of entries) {
1518
1538
  const fullPath = baseDir ? `${baseDir}/${entry.name}` : entry.name;
1519
1539
  if (matchesGlobPattern(fullPath, normalizedPattern)) {
@@ -1745,6 +1765,22 @@ const executeGrepSearchTool = async (args, options) => {
1745
1765
  }
1746
1766
  };
1747
1767
 
1768
+ const getEntryType = type => {
1769
+ switch (type) {
1770
+ case Directory:
1771
+ return 'folder';
1772
+ case File:
1773
+ return 'file';
1774
+ case Symlink:
1775
+ return 'symlink';
1776
+ default:
1777
+ return 'unknown';
1778
+ }
1779
+ };
1780
+ const mapEntry = entry => ({
1781
+ name: entry.name,
1782
+ type: getEntryType(entry.type)
1783
+ });
1748
1784
  const executeListFilesTool = async (args, _options) => {
1749
1785
  const uri = typeof args.uri === 'string' ? args.uri : '';
1750
1786
  if (!uri || !isAbsoluteUri(uri)) {
@@ -1761,7 +1797,7 @@ const executeListFilesTool = async (args, _options) => {
1761
1797
  try {
1762
1798
  const entries = await invoke('FileSystem.readDirWithFileTypes', uri);
1763
1799
  return {
1764
- entries,
1800
+ entries: entries.map(mapEntry),
1765
1801
  uri
1766
1802
  };
1767
1803
  } catch (error) {
@@ -2136,6 +2172,49 @@ const executeUpdateTodoTool = async (args, _options) => {
2136
2172
  }
2137
2173
  };
2138
2174
 
2175
+ const carriageReturnNewLineRegex = /\r\n/g;
2176
+ const toLines = content => {
2177
+ if (content === '') {
2178
+ return [];
2179
+ }
2180
+ const normalizedContent = content.replaceAll(carriageReturnNewLineRegex, '\n');
2181
+ const lines = normalizedContent.split('\n');
2182
+ if (lines.at(-1) === '') {
2183
+ lines.pop();
2184
+ }
2185
+ return lines;
2186
+ };
2187
+ const getLongestCommonSubsequenceLength = (beforeLines, afterLines) => {
2188
+ if (beforeLines.length === 0 || afterLines.length === 0) {
2189
+ return 0;
2190
+ }
2191
+ const outerLines = beforeLines.length >= afterLines.length ? beforeLines : afterLines;
2192
+ const innerLines = beforeLines.length >= afterLines.length ? afterLines : beforeLines;
2193
+ let previousRow = new Uint32Array(innerLines.length + 1);
2194
+ for (let i = 1; i <= outerLines.length; i++) {
2195
+ const currentRow = new Uint32Array(innerLines.length + 1);
2196
+ const outerLine = outerLines[i - 1];
2197
+ for (let j = 1; j <= innerLines.length; j++) {
2198
+ if (outerLine === innerLines[j - 1]) {
2199
+ currentRow[j] = previousRow[j - 1] + 1;
2200
+ } else {
2201
+ currentRow[j] = Math.max(previousRow[j], currentRow[j - 1]);
2202
+ }
2203
+ }
2204
+ previousRow = currentRow;
2205
+ }
2206
+ return previousRow[innerLines.length];
2207
+ };
2208
+ const getLineDiffStats = (beforeContent, afterContent) => {
2209
+ const beforeLines = toLines(beforeContent);
2210
+ const afterLines = toLines(afterContent);
2211
+ const longestCommonSubsequenceLength = getLongestCommonSubsequenceLength(beforeLines, afterLines);
2212
+ return {
2213
+ addedLines: afterLines.length - longestCommonSubsequenceLength,
2214
+ removedLines: beforeLines.length - longestCommonSubsequenceLength
2215
+ };
2216
+ };
2217
+
2139
2218
  const executeWriteFileTool = async (args, _options) => {
2140
2219
  const uri = typeof args.uri === 'string' ? args.uri : '';
2141
2220
  const content = typeof args.content === 'string' ? args.content : '';
@@ -2152,9 +2231,17 @@ const executeWriteFileTool = async (args, _options) => {
2152
2231
  };
2153
2232
  }
2154
2233
  try {
2155
- await writeFile(uri, content);
2234
+ const exists$1 = await exists(uri);
2235
+ const previousContent = exists$1 ? await readFile$1(uri) : '';
2236
+ const {
2237
+ addedLines,
2238
+ removedLines
2239
+ } = getLineDiffStats(previousContent, content);
2240
+ await writeFile$1(uri, content);
2156
2241
  return {
2242
+ addedLines,
2157
2243
  ok: true,
2244
+ removedLines,
2158
2245
  uri
2159
2246
  };
2160
2247
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "8.0.0",
3
+ "version": "8.2.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",