opencode-gbk-tools 0.1.18 → 0.1.21

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.
@@ -16333,7 +16333,7 @@ function buildGbkLineDiffPreview(filePath, encoding, beforeText, afterText) {
16333
16333
  lines.push(`${ANSI_GREEN}+${after}${ANSI_RESET}`);
16334
16334
  }
16335
16335
  }
16336
- return lines.join("\n");
16336
+ return "\n" + lines.join("\n");
16337
16337
  }
16338
16338
  function toSafeNumber(value) {
16339
16339
  return typeof value === "bigint" ? Number(value) : value;
@@ -16357,10 +16357,10 @@ function assertInsertArguments(input) {
16357
16357
  throw createGbkError("GBK_INVALID_ARGUMENT", "content \u4E0D\u80FD\u4E3A\u7A7A");
16358
16358
  }
16359
16359
  if (input.replaceAll !== void 0) {
16360
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 replaceAll");
16360
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? replaceAll");
16361
16361
  }
16362
16362
  if (input.startLine !== void 0 || input.endLine !== void 0 || input.startAnchor !== void 0 || input.endAnchor !== void 0) {
16363
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 startLine/endLine/startAnchor/endAnchor");
16363
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? startLine/endLine/startAnchor/endAnchor");
16364
16364
  }
16365
16365
  }
16366
16366
  function findOccurrenceIndex(text, token, occurrence) {
@@ -16384,9 +16384,9 @@ function findOccurrenceIndex(text, token, occurrence) {
16384
16384
  searchFrom = index + token.length;
16385
16385
  }
16386
16386
  if (count === 0) {
16387
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${token}`);
16387
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${token}`);
16388
16388
  }
16389
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\u5230 ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${occurrence} \u5904`);
16389
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\uFFFD\uFFFD? ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${occurrence} \u5904`);
16390
16390
  }
16391
16391
  function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
16392
16392
  const alignedContent = newlineStyle === "crlf" ? content.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : newlineStyle === "lf" ? content.replace(/\r\n/g, "\n") : content;
@@ -16395,7 +16395,7 @@ function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newli
16395
16395
  const alreadyExists = mode === "insertAfter" ? text.slice(insertionPoint, insertionPoint + alignedContent.length) === alignedContent : text.slice(Math.max(0, insertionPoint - alignedContent.length), insertionPoint) === alignedContent;
16396
16396
  if (alreadyExists) {
16397
16397
  if (ifExists === "error") {
16398
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
16398
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
16399
16399
  }
16400
16400
  if (ifExists === "skip") {
16401
16401
  return {
@@ -16436,7 +16436,7 @@ function normalizeOptionalPositiveInteger(value, name) {
16436
16436
  }
16437
16437
  function assertNotBinary(buffer) {
16438
16438
  if (buffer.includes(0)) {
16439
- throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u6309 GBK \u6587\u672C\u5904\u7406");
16439
+ throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\uFFFD\uFFFD? GBK \u6587\u672C\u5904\u7406");
16440
16440
  }
16441
16441
  }
16442
16442
  function detectNewlineStyle(text) {
@@ -16517,14 +16517,14 @@ function applyAnchors(text, startAnchor, endAnchor) {
16517
16517
  if (startAnchor) {
16518
16518
  const found = text.indexOf(startAnchor);
16519
16519
  if (found === -1) {
16520
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\u70B9: ${startAnchor}`);
16520
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\uFFFD\uFFFD?: ${startAnchor}`);
16521
16521
  }
16522
16522
  rangeStart = found + startAnchor.length;
16523
16523
  }
16524
16524
  if (endAnchor) {
16525
16525
  const found = text.indexOf(endAnchor, rangeStart);
16526
16526
  if (found === -1) {
16527
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\u70B9: ${endAnchor}`);
16527
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\uFFFD\uFFFD?: ${endAnchor}`);
16528
16528
  }
16529
16529
  rangeEnd = found;
16530
16530
  }
@@ -16577,9 +16577,9 @@ function getNearestContext(content, oldString) {
16577
16577
  }
16578
16578
  function buildNoMatchMessage(content, oldString) {
16579
16579
  return [
16580
- "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\u3002",
16581
- "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\u884C/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\u3002",
16582
- "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFF1A",
16580
+ "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\uFFFD\uFFFD?",
16581
+ "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\uFFFD\uFFFD?/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\uFFFD\uFFFD?",
16582
+ "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFFFD\uFFFD?",
16583
16583
  getNearestContext(content, oldString)
16584
16584
  ].join("\n");
16585
16585
  }
@@ -16636,6 +16636,8 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16636
16636
  break;
16637
16637
  }
16638
16638
  if (oldIndex === oldLines.length) {
16639
+ const matchedLines = contentLines.slice(start, contentIndex);
16640
+ const matchedOriginal = matchedLines.join("\n");
16639
16641
  const replacedNormalized = [
16640
16642
  ...contentLines.slice(0, start),
16641
16643
  ...newLines,
@@ -16643,6 +16645,7 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16643
16645
  ].join("\n");
16644
16646
  return {
16645
16647
  occurrencesBefore: 1,
16648
+ matchedOriginal,
16646
16649
  content: newlineStyle === "crlf" ? replacedNormalized.replace(/\n/g, "\r\n") : replacedNormalized
16647
16650
  };
16648
16651
  }
@@ -16699,10 +16702,10 @@ async function resolveReadableGbkFile(input) {
16699
16702
  try {
16700
16703
  stat = await fs2.stat(candidatePath);
16701
16704
  } catch (error45) {
16702
- throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\u5728: ${candidatePath}`, error45);
16705
+ throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\uFFFD\uFFFD?: ${candidatePath}`, error45);
16703
16706
  }
16704
16707
  if (stat.isDirectory()) {
16705
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16708
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16706
16709
  }
16707
16710
  return {
16708
16711
  filePath: candidatePath,
@@ -16899,7 +16902,7 @@ function replaceScopedTextContent(scopeText, oldString, newString, replaceAll, n
16899
16902
  } else if (occurrencesBefore === 0) {
16900
16903
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scopeText, oldString));
16901
16904
  } else if (occurrencesBefore > 1) {
16902
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${oldString}`);
16905
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${oldString}`);
16903
16906
  }
16904
16907
  const alignedNewString = alignTextToNewlineStyle(newString, newlineStyle);
16905
16908
  return {
@@ -17004,7 +17007,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17004
17007
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17005
17008
  if (alreadyExists) {
17006
17009
  if (input.ifExists === "error") {
17007
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17010
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17008
17011
  }
17009
17012
  if (input.ifExists === "skip") {
17010
17013
  skipped = true;
@@ -17051,7 +17054,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17051
17054
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17052
17055
  if (alreadyExists) {
17053
17056
  if (input.ifExists === "error") {
17054
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17057
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17055
17058
  }
17056
17059
  if (input.ifExists === "skip") {
17057
17060
  skipped = true;
@@ -17065,10 +17068,10 @@ async function replaceLargeGbkFileByAnchor(input) {
17065
17068
  }
17066
17069
  }
17067
17070
  if (!inserted && totalMatches === 0) {
17068
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${input.anchor}`);
17071
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${input.anchor}`);
17069
17072
  }
17070
17073
  if (!inserted && totalMatches > 0) {
17071
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\u5230 ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${input.occurrence} \u5904`);
17074
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\uFFFD\uFFFD? ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${input.occurrence} \u5904`);
17072
17075
  }
17073
17076
  await finalizeInserted();
17074
17077
  await handle.close();
@@ -17131,10 +17134,10 @@ async function replaceLargeGbkFileText(input) {
17131
17134
  });
17132
17135
  await flushText("", true);
17133
17136
  if (occurrencesBefore === 0) {
17134
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\u5BB9: ${input.oldString}`);
17137
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\uFFFD\uFFFD?: ${input.oldString}`);
17135
17138
  }
17136
17139
  if (!input.replaceAll && occurrencesBefore > 1) {
17137
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${input.oldString}`);
17140
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${input.oldString}`);
17138
17141
  }
17139
17142
  await handle.close();
17140
17143
  const mode = typeof input.stat.mode === "bigint" ? Number(input.stat.mode) : input.stat.mode;
@@ -17272,7 +17275,7 @@ async function replaceGbkFileText(input) {
17272
17275
  occurrencesBefore: loose.occurrencesBefore,
17273
17276
  bytesRead: current.bytesRead,
17274
17277
  bytesWritten: buffer2.byteLength,
17275
- diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, effectiveOldString, input.newString ?? "")
17278
+ diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, loose.matchedOriginal, input.newString ?? "")
17276
17279
  };
17277
17280
  }
17278
17281
  }
@@ -17283,7 +17286,7 @@ async function replaceGbkFileText(input) {
17283
17286
  } else if (occurrencesBefore === 0) {
17284
17287
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scope.selectedText, effectiveOldString));
17285
17288
  } else if (occurrencesBefore > 1) {
17286
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${effectiveOldString}`);
17289
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${effectiveOldString}`);
17287
17290
  }
17288
17291
  const fileNewlineStyle = detectNewlineStyle(current.content);
17289
17292
  const alignedNewString = fileNewlineStyle === "crlf" ? input.newString.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : fileNewlineStyle === "lf" ? input.newString.replace(/\r\n/g, "\n") : input.newString;
@@ -17374,7 +17377,8 @@ Insert mode:
17374
17377
  diffPreview
17375
17378
  }
17376
17379
  });
17377
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
17380
+ if (diffPreview) return diffPreview;
17381
+ return JSON.stringify(result, null, 2);
17378
17382
  }
17379
17383
  });
17380
17384
  export {
@@ -16329,7 +16329,7 @@ function normalizeOptionalPositiveInteger(value, name) {
16329
16329
  }
16330
16330
  function assertNotBinary(buffer) {
16331
16331
  if (buffer.includes(0)) {
16332
- throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u6309 GBK \u6587\u672C\u5904\u7406");
16332
+ throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\uFFFD\uFFFD? GBK \u6587\u672C\u5904\u7406");
16333
16333
  }
16334
16334
  }
16335
16335
  function splitLinesWithNumbers(text, offset = 1, limit = 2e3) {
@@ -16420,10 +16420,10 @@ async function resolveReadableGbkFile(input) {
16420
16420
  try {
16421
16421
  stat = await fs2.stat(candidatePath);
16422
16422
  } catch (error45) {
16423
- throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\u5728: ${candidatePath}`, error45);
16423
+ throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\uFFFD\uFFFD?: ${candidatePath}`, error45);
16424
16424
  }
16425
16425
  if (stat.isDirectory()) {
16426
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16426
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16427
16427
  }
16428
16428
  return {
16429
16429
  filePath: candidatePath,
@@ -16313,7 +16313,7 @@ function assertEncodingSupported(encoding) {
16313
16313
  }
16314
16314
  function assertNotBinary(buffer) {
16315
16315
  if (buffer.includes(0)) {
16316
- throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u6309 GBK \u6587\u672C\u5904\u7406");
16316
+ throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\uFFFD\uFFFD? GBK \u6587\u672C\u5904\u7406");
16317
16317
  }
16318
16318
  }
16319
16319
  async function resolveReadableGbkFile(input) {
@@ -16324,10 +16324,10 @@ async function resolveReadableGbkFile(input) {
16324
16324
  try {
16325
16325
  stat = await fs2.stat(candidatePath);
16326
16326
  } catch (error45) {
16327
- throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\u5728: ${candidatePath}`, error45);
16327
+ throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\uFFFD\uFFFD?: ${candidatePath}`, error45);
16328
16328
  }
16329
16329
  if (stat.isDirectory()) {
16330
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16330
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16331
16331
  }
16332
16332
  return {
16333
16333
  filePath: candidatePath,
@@ -16370,10 +16370,10 @@ async function writeGbkFile(input) {
16370
16370
  try {
16371
16371
  const stat = await fs2.stat(candidatePath);
16372
16372
  if (stat.isDirectory()) {
16373
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16373
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16374
16374
  }
16375
16375
  if (!overwrite) {
16376
- throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\u5728: ${candidatePath}`);
16376
+ throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\uFFFD\uFFFD?: ${candidatePath}`);
16377
16377
  }
16378
16378
  } catch (error45) {
16379
16379
  if (error45 instanceof Error && "code" in error45) {
@@ -16603,7 +16603,7 @@ function buildLineDiffPreview(filePath, encoding, beforeText, afterText) {
16603
16603
  lines.push(`${ANSI_GREEN}+${after}${ANSI_RESET}`);
16604
16604
  }
16605
16605
  }
16606
- return lines.join("\n");
16606
+ return "\n" + lines.join("\n");
16607
16607
  }
16608
16608
  function buildInsertOutput(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
16609
16609
  const alignedContent = alignTextToNewlineStyle(content, newlineStyle);
@@ -17034,7 +17034,8 @@ var text_edit_default = tool({
17034
17034
  diffPreview
17035
17035
  }
17036
17036
  });
17037
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
17037
+ if (diffPreview) return diffPreview;
17038
+ return JSON.stringify(result, null, 2);
17038
17039
  }
17039
17040
  });
17040
17041
  export {
@@ -16334,7 +16334,7 @@ function buildGbkLineDiffPreview(filePath, encoding, beforeText, afterText) {
16334
16334
  lines.push(`${ANSI_GREEN}+${after}${ANSI_RESET}`);
16335
16335
  }
16336
16336
  }
16337
- return lines.join("\n");
16337
+ return "\n" + lines.join("\n");
16338
16338
  }
16339
16339
  function toSafeNumber(value) {
16340
16340
  return typeof value === "bigint" ? Number(value) : value;
@@ -16358,10 +16358,10 @@ function assertInsertArguments(input) {
16358
16358
  throw createGbkError("GBK_INVALID_ARGUMENT", "content \u4E0D\u80FD\u4E3A\u7A7A");
16359
16359
  }
16360
16360
  if (input.replaceAll !== void 0) {
16361
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 replaceAll");
16361
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? replaceAll");
16362
16362
  }
16363
16363
  if (input.startLine !== void 0 || input.endLine !== void 0 || input.startAnchor !== void 0 || input.endAnchor !== void 0) {
16364
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 startLine/endLine/startAnchor/endAnchor");
16364
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? startLine/endLine/startAnchor/endAnchor");
16365
16365
  }
16366
16366
  }
16367
16367
  function findOccurrenceIndex(text, token, occurrence) {
@@ -16385,9 +16385,9 @@ function findOccurrenceIndex(text, token, occurrence) {
16385
16385
  searchFrom = index + token.length;
16386
16386
  }
16387
16387
  if (count === 0) {
16388
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${token}`);
16388
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${token}`);
16389
16389
  }
16390
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\u5230 ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${occurrence} \u5904`);
16390
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\uFFFD\uFFFD? ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${occurrence} \u5904`);
16391
16391
  }
16392
16392
  function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
16393
16393
  const alignedContent = newlineStyle === "crlf" ? content.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : newlineStyle === "lf" ? content.replace(/\r\n/g, "\n") : content;
@@ -16396,7 +16396,7 @@ function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newli
16396
16396
  const alreadyExists = mode === "insertAfter" ? text.slice(insertionPoint, insertionPoint + alignedContent.length) === alignedContent : text.slice(Math.max(0, insertionPoint - alignedContent.length), insertionPoint) === alignedContent;
16397
16397
  if (alreadyExists) {
16398
16398
  if (ifExists === "error") {
16399
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
16399
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
16400
16400
  }
16401
16401
  if (ifExists === "skip") {
16402
16402
  return {
@@ -16437,7 +16437,7 @@ function normalizeOptionalPositiveInteger(value, name) {
16437
16437
  }
16438
16438
  function assertNotBinary(buffer) {
16439
16439
  if (buffer.includes(0)) {
16440
- throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u6309 GBK \u6587\u672C\u5904\u7406");
16440
+ throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\uFFFD\uFFFD? GBK \u6587\u672C\u5904\u7406");
16441
16441
  }
16442
16442
  }
16443
16443
  function splitLinesWithNumbers(text, offset = 1, limit = 2e3) {
@@ -16567,14 +16567,14 @@ function applyAnchors(text, startAnchor, endAnchor) {
16567
16567
  if (startAnchor) {
16568
16568
  const found = text.indexOf(startAnchor);
16569
16569
  if (found === -1) {
16570
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\u70B9: ${startAnchor}`);
16570
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\uFFFD\uFFFD?: ${startAnchor}`);
16571
16571
  }
16572
16572
  rangeStart = found + startAnchor.length;
16573
16573
  }
16574
16574
  if (endAnchor) {
16575
16575
  const found = text.indexOf(endAnchor, rangeStart);
16576
16576
  if (found === -1) {
16577
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\u70B9: ${endAnchor}`);
16577
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\uFFFD\uFFFD?: ${endAnchor}`);
16578
16578
  }
16579
16579
  rangeEnd = found;
16580
16580
  }
@@ -16627,9 +16627,9 @@ function getNearestContext(content, oldString) {
16627
16627
  }
16628
16628
  function buildNoMatchMessage(content, oldString) {
16629
16629
  return [
16630
- "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\u3002",
16631
- "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\u884C/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\u3002",
16632
- "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFF1A",
16630
+ "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\uFFFD\uFFFD?",
16631
+ "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\uFFFD\uFFFD?/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\uFFFD\uFFFD?",
16632
+ "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFFFD\uFFFD?",
16633
16633
  getNearestContext(content, oldString)
16634
16634
  ].join("\n");
16635
16635
  }
@@ -16686,6 +16686,8 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16686
16686
  break;
16687
16687
  }
16688
16688
  if (oldIndex === oldLines.length) {
16689
+ const matchedLines = contentLines.slice(start, contentIndex);
16690
+ const matchedOriginal = matchedLines.join("\n");
16689
16691
  const replacedNormalized = [
16690
16692
  ...contentLines.slice(0, start),
16691
16693
  ...newLines,
@@ -16693,6 +16695,7 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16693
16695
  ].join("\n");
16694
16696
  return {
16695
16697
  occurrencesBefore: 1,
16698
+ matchedOriginal,
16696
16699
  content: newlineStyle === "crlf" ? replacedNormalized.replace(/\n/g, "\r\n") : replacedNormalized
16697
16700
  };
16698
16701
  }
@@ -16749,10 +16752,10 @@ async function resolveReadableGbkFile(input) {
16749
16752
  try {
16750
16753
  stat = await fs2.stat(candidatePath);
16751
16754
  } catch (error45) {
16752
- throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\u5728: ${candidatePath}`, error45);
16755
+ throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\uFFFD\uFFFD?: ${candidatePath}`, error45);
16753
16756
  }
16754
16757
  if (stat.isDirectory()) {
16755
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16758
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16756
16759
  }
16757
16760
  return {
16758
16761
  filePath: candidatePath,
@@ -16957,7 +16960,7 @@ function replaceScopedTextContent(scopeText, oldString, newString, replaceAll, n
16957
16960
  } else if (occurrencesBefore === 0) {
16958
16961
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scopeText, oldString));
16959
16962
  } else if (occurrencesBefore > 1) {
16960
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${oldString}`);
16963
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${oldString}`);
16961
16964
  }
16962
16965
  const alignedNewString = alignTextToNewlineStyle(newString, newlineStyle);
16963
16966
  return {
@@ -17062,7 +17065,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17062
17065
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17063
17066
  if (alreadyExists) {
17064
17067
  if (input.ifExists === "error") {
17065
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17068
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17066
17069
  }
17067
17070
  if (input.ifExists === "skip") {
17068
17071
  skipped = true;
@@ -17109,7 +17112,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17109
17112
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17110
17113
  if (alreadyExists) {
17111
17114
  if (input.ifExists === "error") {
17112
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17115
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17113
17116
  }
17114
17117
  if (input.ifExists === "skip") {
17115
17118
  skipped = true;
@@ -17123,10 +17126,10 @@ async function replaceLargeGbkFileByAnchor(input) {
17123
17126
  }
17124
17127
  }
17125
17128
  if (!inserted && totalMatches === 0) {
17126
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${input.anchor}`);
17129
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${input.anchor}`);
17127
17130
  }
17128
17131
  if (!inserted && totalMatches > 0) {
17129
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\u5230 ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${input.occurrence} \u5904`);
17132
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\uFFFD\uFFFD? ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${input.occurrence} \u5904`);
17130
17133
  }
17131
17134
  await finalizeInserted();
17132
17135
  await handle.close();
@@ -17189,10 +17192,10 @@ async function replaceLargeGbkFileText(input) {
17189
17192
  });
17190
17193
  await flushText("", true);
17191
17194
  if (occurrencesBefore === 0) {
17192
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\u5BB9: ${input.oldString}`);
17195
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\uFFFD\uFFFD?: ${input.oldString}`);
17193
17196
  }
17194
17197
  if (!input.replaceAll && occurrencesBefore > 1) {
17195
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${input.oldString}`);
17198
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${input.oldString}`);
17196
17199
  }
17197
17200
  await handle.close();
17198
17201
  const mode = typeof input.stat.mode === "bigint" ? Number(input.stat.mode) : input.stat.mode;
@@ -17407,7 +17410,7 @@ async function replaceGbkFileText(input) {
17407
17410
  occurrencesBefore: loose.occurrencesBefore,
17408
17411
  bytesRead: current.bytesRead,
17409
17412
  bytesWritten: buffer2.byteLength,
17410
- diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, effectiveOldString, input.newString ?? "")
17413
+ diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, loose.matchedOriginal, input.newString ?? "")
17411
17414
  };
17412
17415
  }
17413
17416
  }
@@ -17418,7 +17421,7 @@ async function replaceGbkFileText(input) {
17418
17421
  } else if (occurrencesBefore === 0) {
17419
17422
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scope.selectedText, effectiveOldString));
17420
17423
  } else if (occurrencesBefore > 1) {
17421
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${effectiveOldString}`);
17424
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${effectiveOldString}`);
17422
17425
  }
17423
17426
  const fileNewlineStyle = detectNewlineStyle(current.content);
17424
17427
  const alignedNewString = fileNewlineStyle === "crlf" ? input.newString.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : fileNewlineStyle === "lf" ? input.newString.replace(/\r\n/g, "\n") : input.newString;
@@ -17551,10 +17554,10 @@ async function writeGbkFile(input) {
17551
17554
  try {
17552
17555
  const stat = await fs2.stat(candidatePath);
17553
17556
  if (stat.isDirectory()) {
17554
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
17557
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
17555
17558
  }
17556
17559
  if (!overwrite) {
17557
- throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\u5728: ${candidatePath}`);
17560
+ throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\uFFFD\uFFFD?: ${candidatePath}`);
17558
17561
  }
17559
17562
  } catch (error45) {
17560
17563
  if (error45 instanceof Error && "code" in error45) {
@@ -17670,7 +17673,8 @@ Insert mode:
17670
17673
  diffPreview
17671
17674
  }
17672
17675
  });
17673
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
17676
+ if (diffPreview) return diffPreview;
17677
+ return JSON.stringify(result, null, 2);
17674
17678
  }
17675
17679
  });
17676
17680
 
@@ -18077,7 +18081,7 @@ function buildLineDiffPreview(filePath, encoding, beforeText, afterText) {
18077
18081
  lines.push(`${ANSI_GREEN2}+${after}${ANSI_RESET2}`);
18078
18082
  }
18079
18083
  }
18080
- return lines.join("\n");
18084
+ return "\n" + lines.join("\n");
18081
18085
  }
18082
18086
  function buildInsertOutput(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
18083
18087
  const alignedContent = alignTextToNewlineStyle2(content, newlineStyle);
@@ -18779,7 +18783,8 @@ var text_edit_default = tool({
18779
18783
  diffPreview
18780
18784
  }
18781
18785
  });
18782
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
18786
+ if (diffPreview) return diffPreview;
18787
+ return JSON.stringify(result, null, 2);
18783
18788
  }
18784
18789
  });
18785
18790
 
@@ -16334,7 +16334,7 @@ function buildGbkLineDiffPreview(filePath, encoding, beforeText, afterText) {
16334
16334
  lines.push(`${ANSI_GREEN}+${after}${ANSI_RESET}`);
16335
16335
  }
16336
16336
  }
16337
- return lines.join("\n");
16337
+ return "\n" + lines.join("\n");
16338
16338
  }
16339
16339
  function toSafeNumber(value) {
16340
16340
  return typeof value === "bigint" ? Number(value) : value;
@@ -16358,10 +16358,10 @@ function assertInsertArguments(input) {
16358
16358
  throw createGbkError("GBK_INVALID_ARGUMENT", "content \u4E0D\u80FD\u4E3A\u7A7A");
16359
16359
  }
16360
16360
  if (input.replaceAll !== void 0) {
16361
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 replaceAll");
16361
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? replaceAll");
16362
16362
  }
16363
16363
  if (input.startLine !== void 0 || input.endLine !== void 0 || input.startAnchor !== void 0 || input.endAnchor !== void 0) {
16364
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\u6301 startLine/endLine/startAnchor/endAnchor");
16364
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u6A21\u5F0F\u4E0D\u652F\uFFFD\uFFFD? startLine/endLine/startAnchor/endAnchor");
16365
16365
  }
16366
16366
  }
16367
16367
  function findOccurrenceIndex(text, token, occurrence) {
@@ -16385,9 +16385,9 @@ function findOccurrenceIndex(text, token, occurrence) {
16385
16385
  searchFrom = index + token.length;
16386
16386
  }
16387
16387
  if (count === 0) {
16388
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${token}`);
16388
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${token}`);
16389
16389
  }
16390
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\u5230 ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${occurrence} \u5904`);
16390
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${token} \u53EA\u627E\uFFFD\uFFFD? ${count} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${occurrence} \u5904`);
16391
16391
  }
16392
16392
  function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
16393
16393
  const alignedContent = newlineStyle === "crlf" ? content.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : newlineStyle === "lf" ? content.replace(/\r\n/g, "\n") : content;
@@ -16396,7 +16396,7 @@ function insertByAnchor(text, mode, anchor, content, occurrence, ifExists, newli
16396
16396
  const alreadyExists = mode === "insertAfter" ? text.slice(insertionPoint, insertionPoint + alignedContent.length) === alignedContent : text.slice(Math.max(0, insertionPoint - alignedContent.length), insertionPoint) === alignedContent;
16397
16397
  if (alreadyExists) {
16398
16398
  if (ifExists === "error") {
16399
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
16399
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
16400
16400
  }
16401
16401
  if (ifExists === "skip") {
16402
16402
  return {
@@ -16437,7 +16437,7 @@ function normalizeOptionalPositiveInteger(value, name) {
16437
16437
  }
16438
16438
  function assertNotBinary(buffer) {
16439
16439
  if (buffer.includes(0)) {
16440
- throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\u6309 GBK \u6587\u672C\u5904\u7406");
16440
+ throw createGbkError("GBK_BINARY_FILE", "\u7591\u4F3C\u4E8C\u8FDB\u5236\u6587\u4EF6\uFF0C\u65E0\u6CD5\uFFFD\uFFFD? GBK \u6587\u672C\u5904\u7406");
16441
16441
  }
16442
16442
  }
16443
16443
  function splitLinesWithNumbers(text, offset = 1, limit = 2e3) {
@@ -16567,14 +16567,14 @@ function applyAnchors(text, startAnchor, endAnchor) {
16567
16567
  if (startAnchor) {
16568
16568
  const found = text.indexOf(startAnchor);
16569
16569
  if (found === -1) {
16570
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\u70B9: ${startAnchor}`);
16570
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8D77\u59CB\u951A\uFFFD\uFFFD?: ${startAnchor}`);
16571
16571
  }
16572
16572
  rangeStart = found + startAnchor.length;
16573
16573
  }
16574
16574
  if (endAnchor) {
16575
16575
  const found = text.indexOf(endAnchor, rangeStart);
16576
16576
  if (found === -1) {
16577
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\u70B9: ${endAnchor}`);
16577
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u7ED3\u675F\u951A\uFFFD\uFFFD?: ${endAnchor}`);
16578
16578
  }
16579
16579
  rangeEnd = found;
16580
16580
  }
@@ -16627,9 +16627,9 @@ function getNearestContext(content, oldString) {
16627
16627
  }
16628
16628
  function buildNoMatchMessage(content, oldString) {
16629
16629
  return [
16630
- "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\u3002",
16631
- "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\u884C/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\u3002",
16632
- "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFF1A",
16630
+ "\u672A\u627E\u5230\u9700\u8981\u66FF\u6362\u7684\u6587\u672C\uFFFD\uFFFD?",
16631
+ "oldString \u5FC5\u987B\u4E0E\u6587\u4EF6\u5B9E\u9645\u5185\u5BB9\u5B8C\u5168\u5BF9\u5E94\uFF0C\u6216\u4EC5\u5728\u6362\uFFFD\uFFFD?/\u5C3E\u968F\u7A7A\u884C\u4E0A\u5B58\u5728\u8F7B\u5FAE\u5DEE\u5F02\uFFFD\uFFFD?",
16632
+ "\u6700\u63A5\u8FD1\u7684\u4E0A\u4E0B\u6587\uFFFD\uFFFD?",
16633
16633
  getNearestContext(content, oldString)
16634
16634
  ].join("\n");
16635
16635
  }
@@ -16686,6 +16686,8 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16686
16686
  break;
16687
16687
  }
16688
16688
  if (oldIndex === oldLines.length) {
16689
+ const matchedLines = contentLines.slice(start, contentIndex);
16690
+ const matchedOriginal = matchedLines.join("\n");
16689
16691
  const replacedNormalized = [
16690
16692
  ...contentLines.slice(0, start),
16691
16693
  ...newLines,
@@ -16693,6 +16695,7 @@ function matchLooseBlock(contentLines, oldLines, newLines, newlineStyle, content
16693
16695
  ].join("\n");
16694
16696
  return {
16695
16697
  occurrencesBefore: 1,
16698
+ matchedOriginal,
16696
16699
  content: newlineStyle === "crlf" ? replacedNormalized.replace(/\n/g, "\r\n") : replacedNormalized
16697
16700
  };
16698
16701
  }
@@ -16749,10 +16752,10 @@ async function resolveReadableGbkFile(input) {
16749
16752
  try {
16750
16753
  stat = await fs2.stat(candidatePath);
16751
16754
  } catch (error45) {
16752
- throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\u5728: ${candidatePath}`, error45);
16755
+ throw createGbkError("GBK_FILE_NOT_FOUND", `\u6587\u4EF6\u4E0D\u5B58\uFFFD\uFFFD?: ${candidatePath}`, error45);
16753
16756
  }
16754
16757
  if (stat.isDirectory()) {
16755
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
16758
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
16756
16759
  }
16757
16760
  return {
16758
16761
  filePath: candidatePath,
@@ -16957,7 +16960,7 @@ function replaceScopedTextContent(scopeText, oldString, newString, replaceAll, n
16957
16960
  } else if (occurrencesBefore === 0) {
16958
16961
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scopeText, oldString));
16959
16962
  } else if (occurrencesBefore > 1) {
16960
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${oldString}`);
16963
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${oldString}`);
16961
16964
  }
16962
16965
  const alignedNewString = alignTextToNewlineStyle(newString, newlineStyle);
16963
16966
  return {
@@ -17062,7 +17065,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17062
17065
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17063
17066
  if (alreadyExists) {
17064
17067
  if (input.ifExists === "error") {
17065
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17068
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17066
17069
  }
17067
17070
  if (input.ifExists === "skip") {
17068
17071
  skipped = true;
@@ -17109,7 +17112,7 @@ async function replaceLargeGbkFileByAnchor(input) {
17109
17112
  const alreadyExists = input.mode === "insertAfter" ? after.startsWith(alignedContent) : before.endsWith(alignedContent);
17110
17113
  if (alreadyExists) {
17111
17114
  if (input.ifExists === "error") {
17112
- throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\u5BB9");
17115
+ throw createGbkError("GBK_INVALID_ARGUMENT", "\u63D2\u5165\u4F4D\u7F6E\u5DF2\u5B58\u5728\u76F8\u540C\u5185\uFFFD\uFFFD?");
17113
17116
  }
17114
17117
  if (input.ifExists === "skip") {
17115
17118
  skipped = true;
@@ -17123,10 +17126,10 @@ async function replaceLargeGbkFileByAnchor(input) {
17123
17126
  }
17124
17127
  }
17125
17128
  if (!inserted && totalMatches === 0) {
17126
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\u70B9: ${input.anchor}`);
17129
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u951A\uFFFD\uFFFD?: ${input.anchor}`);
17127
17130
  }
17128
17131
  if (!inserted && totalMatches > 0) {
17129
- throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\u5230 ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\u7B2C ${input.occurrence} \u5904`);
17132
+ throw createGbkError("GBK_NO_MATCH", `\u951A\u70B9 ${input.anchor} \u53EA\u627E\uFFFD\uFFFD? ${totalMatches} \u5904\uFF0C\u65E0\u6CD5\u4F7F\u7528\uFFFD\uFFFD? ${input.occurrence} \u5904`);
17130
17133
  }
17131
17134
  await finalizeInserted();
17132
17135
  await handle.close();
@@ -17189,10 +17192,10 @@ async function replaceLargeGbkFileText(input) {
17189
17192
  });
17190
17193
  await flushText("", true);
17191
17194
  if (occurrencesBefore === 0) {
17192
- throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\u5BB9: ${input.oldString}`);
17195
+ throw createGbkError("GBK_NO_MATCH", `\u672A\u627E\u5230\u8981\u66FF\u6362\u7684\u5185\uFFFD\uFFFD?: ${input.oldString}`);
17193
17196
  }
17194
17197
  if (!input.replaceAll && occurrencesBefore > 1) {
17195
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${input.oldString}`);
17198
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${input.oldString}`);
17196
17199
  }
17197
17200
  await handle.close();
17198
17201
  const mode = typeof input.stat.mode === "bigint" ? Number(input.stat.mode) : input.stat.mode;
@@ -17407,7 +17410,7 @@ async function replaceGbkFileText(input) {
17407
17410
  occurrencesBefore: loose.occurrencesBefore,
17408
17411
  bytesRead: current.bytesRead,
17409
17412
  bytesWritten: buffer2.byteLength,
17410
- diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, effectiveOldString, input.newString ?? "")
17413
+ diffPreview: buildGbkLineDiffPreview(current.filePath, current.encoding, loose.matchedOriginal, input.newString ?? "")
17411
17414
  };
17412
17415
  }
17413
17416
  }
@@ -17418,7 +17421,7 @@ async function replaceGbkFileText(input) {
17418
17421
  } else if (occurrencesBefore === 0) {
17419
17422
  throw createGbkError("GBK_NO_MATCH", buildNoMatchMessage(scope.selectedText, effectiveOldString));
17420
17423
  } else if (occurrencesBefore > 1) {
17421
- throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\u9879: ${effectiveOldString}`);
17424
+ throw createGbkError("GBK_MULTIPLE_MATCHES", `\u627E\u5230\u591A\u4E2A\u5339\u914D\uFFFD\uFFFD?: ${effectiveOldString}`);
17422
17425
  }
17423
17426
  const fileNewlineStyle = detectNewlineStyle(current.content);
17424
17427
  const alignedNewString = fileNewlineStyle === "crlf" ? input.newString.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n") : fileNewlineStyle === "lf" ? input.newString.replace(/\r\n/g, "\n") : input.newString;
@@ -17551,10 +17554,10 @@ async function writeGbkFile(input) {
17551
17554
  try {
17552
17555
  const stat = await fs2.stat(candidatePath);
17553
17556
  if (stat.isDirectory()) {
17554
- throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\u5F55: ${candidatePath}`);
17557
+ throw createGbkError("GBK_IS_DIRECTORY", `\u76EE\u6807\u8DEF\u5F84\u662F\u76EE\uFFFD\uFFFD?: ${candidatePath}`);
17555
17558
  }
17556
17559
  if (!overwrite) {
17557
- throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\u5728: ${candidatePath}`);
17560
+ throw createGbkError("GBK_FILE_EXISTS", `\u76EE\u6807\u6587\u4EF6\u5DF2\u5B58\uFFFD\uFFFD?: ${candidatePath}`);
17558
17561
  }
17559
17562
  } catch (error45) {
17560
17563
  if (error45 instanceof Error && "code" in error45) {
@@ -17670,7 +17673,8 @@ Insert mode:
17670
17673
  diffPreview
17671
17674
  }
17672
17675
  });
17673
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
17676
+ if (diffPreview) return diffPreview;
17677
+ return JSON.stringify(result, null, 2);
17674
17678
  }
17675
17679
  });
17676
17680
 
@@ -18077,7 +18081,7 @@ function buildLineDiffPreview(filePath, encoding, beforeText, afterText) {
18077
18081
  lines.push(`${ANSI_GREEN2}+${after}${ANSI_RESET2}`);
18078
18082
  }
18079
18083
  }
18080
- return lines.join("\n");
18084
+ return "\n" + lines.join("\n");
18081
18085
  }
18082
18086
  function buildInsertOutput(text, mode, anchor, content, occurrence, ifExists, newlineStyle) {
18083
18087
  const alignedContent = alignTextToNewlineStyle2(content, newlineStyle);
@@ -18779,7 +18783,8 @@ var text_edit_default = tool({
18779
18783
  diffPreview
18780
18784
  }
18781
18785
  });
18782
- return JSON.stringify({ ...result, diffPreview: diffPreview || void 0 }, null, 2);
18786
+ if (diffPreview) return diffPreview;
18787
+ return JSON.stringify(result, null, 2);
18783
18788
  }
18784
18789
  });
18785
18790
 
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "manifestVersion": 1,
3
3
  "packageName": "opencode-gbk-tools",
4
- "packageVersion": "0.1.18",
4
+ "packageVersion": "0.1.19",
5
5
  "artifacts": [
6
6
  {
7
7
  "relativePath": "plugins/opencode-gbk-tools.js",
8
8
  "kind": "plugin",
9
- "expectedHash": "c03ae5d53eef0adc0eb6791908c5a9c776af47f8e9e6f8191acb3a43736fa770",
9
+ "expectedHash": "481836ea0b68a3b872d46346cfa1bc270966d57d566b66846df882aa4885972e",
10
10
  "hashAlgorithm": "sha256"
11
11
  }
12
12
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-gbk-tools",
3
- "version": "0.1.18",
3
+ "version": "0.1.21",
4
4
  "description": "Auto-encoding text tools plus GBK/GB18030 tools for OpenCode",
5
5
  "type": "module",
6
6
  "main": "./dist/plugin/index.js",