@lvce-editor/chat-tool-worker 8.1.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.
@@ -1146,6 +1146,15 @@ const {
1146
1146
  const readDirWithFileTypes = async uri => {
1147
1147
  return invoke$1('FileSystem.readDirWithFileTypes', uri);
1148
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
+ };
1149
1158
 
1150
1159
  const {
1151
1160
  invoke,
@@ -2163,6 +2172,49 @@ const executeUpdateTodoTool = async (args, _options) => {
2163
2172
  }
2164
2173
  };
2165
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
+
2166
2218
  const executeWriteFileTool = async (args, _options) => {
2167
2219
  const uri = typeof args.uri === 'string' ? args.uri : '';
2168
2220
  const content = typeof args.content === 'string' ? args.content : '';
@@ -2179,9 +2231,17 @@ const executeWriteFileTool = async (args, _options) => {
2179
2231
  };
2180
2232
  }
2181
2233
  try {
2182
- 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);
2183
2241
  return {
2242
+ addedLines,
2184
2243
  ok: true,
2244
+ removedLines,
2185
2245
  uri
2186
2246
  };
2187
2247
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/chat-tool-worker",
3
- "version": "8.1.0",
3
+ "version": "8.2.0",
4
4
  "description": "Chat Tool Worker",
5
5
  "repository": {
6
6
  "type": "git",