@trim21/personal-pi-extensions 0.0.314 → 0.0.315
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.
- package/package.json +1 -1
- package/src/claude-code/files.ts +151 -150
- package/src/opencode/files.ts +94 -88
package/package.json
CHANGED
package/src/claude-code/files.ts
CHANGED
|
@@ -416,129 +416,131 @@ export function registerFileTools(
|
|
|
416
416
|
replaceAll: params.replace_all,
|
|
417
417
|
},
|
|
418
418
|
});
|
|
419
|
-
const [message, details] = await withFileMutationQueue<
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
try {
|
|
431
|
-
const value = await stat(filePath);
|
|
432
|
-
if (value.isFile()) {
|
|
433
|
-
const content = await readFile(filePath, "utf8");
|
|
434
|
-
if (content.trim() !== "") {
|
|
435
|
-
throw new Error("Cannot create new file - file already exists.");
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
} catch (error) {
|
|
439
|
-
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
440
|
-
exists = false;
|
|
441
|
-
} else {
|
|
442
|
-
throw error;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
if (!exists) await mkdir(dirname(filePath), { recursive: true });
|
|
446
|
-
await writeFile(filePath, newString, "utf8");
|
|
447
|
-
const snapshot = snapshotOf(newString);
|
|
448
|
-
const key = await readStateKey(filePath);
|
|
449
|
-
state.reads.set(key, snapshot);
|
|
450
|
-
const diff = generateDiffString("", newString);
|
|
451
|
-
return [
|
|
452
|
-
`The file ${filePath} has been updated successfully.`,
|
|
453
|
-
{
|
|
454
|
-
diff: diff.diff,
|
|
455
|
-
patch: generateUnifiedPatch(filePath, "", newString),
|
|
456
|
-
firstChangedLine: diff.firstChangedLine,
|
|
457
|
-
reads: { [key]: snapshot },
|
|
458
|
-
},
|
|
459
|
-
];
|
|
460
|
-
}
|
|
461
|
-
const replaceAll = params.replace_all ?? false;
|
|
462
|
-
// 防止 OOM 的大文件检查(对齐 Claude Code)
|
|
419
|
+
const [message, details, diagnosticText] = await withFileMutationQueue<
|
|
420
|
+
[string, FileToolDetails, string]
|
|
421
|
+
>(filePath, async () => {
|
|
422
|
+
const oldString = params.old_string;
|
|
423
|
+
const newString = params.new_string;
|
|
424
|
+
if (oldString === newString) {
|
|
425
|
+
throw new Error("No changes to make: old_string and new_string are exactly the same.");
|
|
426
|
+
}
|
|
427
|
+
// 空 old_string:创建新文件或填充空文件(不需要先 Read,对齐 Claude Code)
|
|
428
|
+
if (oldString === "") {
|
|
429
|
+
let exists = true;
|
|
463
430
|
try {
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
} catch (error) {
|
|
471
|
-
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") {
|
|
472
|
-
throw error;
|
|
431
|
+
const value = await stat(filePath);
|
|
432
|
+
if (value.isFile()) {
|
|
433
|
+
const content = await readFile(filePath, "utf8");
|
|
434
|
+
if (content.trim() !== "") {
|
|
435
|
+
throw new Error("Cannot create new file - file already exists.");
|
|
436
|
+
}
|
|
473
437
|
}
|
|
474
|
-
}
|
|
475
|
-
let content: Buffer;
|
|
476
|
-
try {
|
|
477
|
-
content = await readFile(filePath);
|
|
478
438
|
} catch (error) {
|
|
479
439
|
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
{ cause: error },
|
|
484
|
-
);
|
|
440
|
+
exists = false;
|
|
441
|
+
} else {
|
|
442
|
+
throw error;
|
|
485
443
|
}
|
|
486
|
-
throw error;
|
|
487
|
-
}
|
|
488
|
-
if (extname(filePath).toLowerCase() === ".ipynb") {
|
|
489
|
-
throw new Error(
|
|
490
|
-
"File is a Jupyter Notebook. Use the NotebookEditTool to edit this file.",
|
|
491
|
-
);
|
|
492
444
|
}
|
|
445
|
+
if (!exists) await mkdir(dirname(filePath), { recursive: true });
|
|
446
|
+
await writeFile(filePath, newString, "utf8");
|
|
447
|
+
const snapshot = snapshotOf(newString);
|
|
493
448
|
const key = await readStateKey(filePath);
|
|
494
|
-
requireCurrentRead(state, key, filePath, content);
|
|
495
|
-
await access(filePath, constants.R_OK | constants.W_OK);
|
|
496
|
-
throwIfAborted(signal);
|
|
497
|
-
const original = content.toString("utf8");
|
|
498
|
-
|
|
499
|
-
// CRLF 规范化后匹配(old_string 不需要带 \r),写回时恢复原行尾
|
|
500
|
-
const crlfCount = (original.match(/\r\n/g) ?? []).length;
|
|
501
|
-
const lfCount = (original.match(/(?<!\r)\n/g) ?? []).length;
|
|
502
|
-
const lineEnding = crlfCount > lfCount ? "\r\n" : "\n";
|
|
503
|
-
const normalized = original.replaceAll("\r\n", "\n");
|
|
504
|
-
const actualOldString = findActualString(normalized, oldString) ?? oldString;
|
|
505
|
-
const matches = normalized.split(actualOldString).length - 1;
|
|
506
|
-
if (matches === 0) {
|
|
507
|
-
throw new Error(`String to replace not found in file.\nString: ${oldString}`);
|
|
508
|
-
}
|
|
509
|
-
if (!replaceAll && matches > 1) {
|
|
510
|
-
throw new Error(
|
|
511
|
-
`Found ${matches} matches of the string to replace, but replace_all is false. To replace all occurrences, set replace_all to true. To replace only one occurrence, please provide more context to uniquely identify the instance.\nString: ${oldString}`,
|
|
512
|
-
);
|
|
513
|
-
}
|
|
514
|
-
const actualNewString = preserveQuoteStyle(oldString, actualOldString, newString);
|
|
515
|
-
// split/join 与函数替换:replacement 含 $ 时不会触发 $& 等特殊语义
|
|
516
|
-
const updated = replaceAll
|
|
517
|
-
? normalized.split(actualOldString).join(actualNewString)
|
|
518
|
-
: normalized.replace(actualOldString, () => actualNewString);
|
|
519
|
-
const restored = lineEnding === "\r\n" ? updated.replaceAll("\n", "\r\n") : updated;
|
|
520
|
-
await writeFile(filePath, restored, "utf8");
|
|
521
|
-
const snapshot = snapshotOf(restored);
|
|
522
449
|
state.reads.set(key, snapshot);
|
|
523
|
-
const diff = generateDiffString(
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
: `The file ${filePath} has been updated successfully.`;
|
|
450
|
+
const diff = generateDiffString("", newString);
|
|
451
|
+
throwIfAborted(signal);
|
|
452
|
+
const diagnosticText = await service.lspDiagnosticsForFile(filePath, ctx.cwd);
|
|
527
453
|
return [
|
|
528
|
-
|
|
454
|
+
`The file ${filePath} has been updated successfully.`,
|
|
529
455
|
{
|
|
530
456
|
diff: diff.diff,
|
|
531
|
-
patch: generateUnifiedPatch(filePath,
|
|
457
|
+
patch: generateUnifiedPatch(filePath, "", newString),
|
|
532
458
|
firstChangedLine: diff.firstChangedLine,
|
|
533
459
|
reads: { [key]: snapshot },
|
|
534
460
|
},
|
|
461
|
+
diagnosticText,
|
|
535
462
|
];
|
|
536
|
-
}
|
|
537
|
-
|
|
463
|
+
}
|
|
464
|
+
const replaceAll = params.replace_all ?? false;
|
|
465
|
+
// 防止 OOM 的大文件检查(对齐 Claude Code)
|
|
466
|
+
try {
|
|
467
|
+
const { size } = await stat(filePath);
|
|
468
|
+
if (size > MAX_EDIT_FILE_SIZE) {
|
|
469
|
+
throw new Error(
|
|
470
|
+
`File is too large to edit (${formatFileSize(size)}). Maximum editable file size is ${formatFileSize(MAX_EDIT_FILE_SIZE)}.`,
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
} catch (error) {
|
|
474
|
+
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") {
|
|
475
|
+
throw error;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
let content: Buffer;
|
|
479
|
+
try {
|
|
480
|
+
content = await readFile(filePath);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
483
|
+
const suggestion = await didYouMean(filePath, ctx.cwd);
|
|
484
|
+
throw new Error(
|
|
485
|
+
`File does not exist. Note: your current working directory is ${ctx.cwd}.${suggestion ? ` Did you mean ${suggestion}?` : ""}`,
|
|
486
|
+
{ cause: error },
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
if (extname(filePath).toLowerCase() === ".ipynb") {
|
|
492
|
+
throw new Error(
|
|
493
|
+
"File is a Jupyter Notebook. Use the NotebookEditTool to edit this file.",
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
const key = await readStateKey(filePath);
|
|
497
|
+
requireCurrentRead(state, key, filePath, content);
|
|
498
|
+
await access(filePath, constants.R_OK | constants.W_OK);
|
|
499
|
+
throwIfAborted(signal);
|
|
500
|
+
const original = content.toString("utf8");
|
|
501
|
+
|
|
502
|
+
// CRLF 规范化后匹配(old_string 不需要带 \r),写回时恢复原行尾
|
|
503
|
+
const crlfCount = (original.match(/\r\n/g) ?? []).length;
|
|
504
|
+
const lfCount = (original.match(/(?<!\r)\n/g) ?? []).length;
|
|
505
|
+
const lineEnding = crlfCount > lfCount ? "\r\n" : "\n";
|
|
506
|
+
const normalized = original.replaceAll("\r\n", "\n");
|
|
507
|
+
const actualOldString = findActualString(normalized, oldString) ?? oldString;
|
|
508
|
+
const matches = normalized.split(actualOldString).length - 1;
|
|
509
|
+
if (matches === 0) {
|
|
510
|
+
throw new Error(`String to replace not found in file.\nString: ${oldString}`);
|
|
511
|
+
}
|
|
512
|
+
if (!replaceAll && matches > 1) {
|
|
513
|
+
throw new Error(
|
|
514
|
+
`Found ${matches} matches of the string to replace, but replace_all is false. To replace all occurrences, set replace_all to true. To replace only one occurrence, please provide more context to uniquely identify the instance.\nString: ${oldString}`,
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
const actualNewString = preserveQuoteStyle(oldString, actualOldString, newString);
|
|
518
|
+
// split/join 与函数替换:replacement 含 $ 时不会触发 $& 等特殊语义
|
|
519
|
+
const updated = replaceAll
|
|
520
|
+
? normalized.split(actualOldString).join(actualNewString)
|
|
521
|
+
: normalized.replace(actualOldString, () => actualNewString);
|
|
522
|
+
const restored = lineEnding === "\r\n" ? updated.replaceAll("\n", "\r\n") : updated;
|
|
523
|
+
await writeFile(filePath, restored, "utf8");
|
|
524
|
+
const snapshot = snapshotOf(restored);
|
|
525
|
+
state.reads.set(key, snapshot);
|
|
526
|
+
const diff = generateDiffString(original, restored);
|
|
527
|
+
const text = replaceAll
|
|
528
|
+
? `The file ${filePath} has been updated. All occurrences were successfully replaced.`
|
|
529
|
+
: `The file ${filePath} has been updated successfully.`;
|
|
530
|
+
throwIfAborted(signal);
|
|
531
|
+
const diagnosticText = await service.lspDiagnosticsForFile(filePath, ctx.cwd);
|
|
532
|
+
return [
|
|
533
|
+
text,
|
|
534
|
+
{
|
|
535
|
+
diff: diff.diff,
|
|
536
|
+
patch: generateUnifiedPatch(filePath, original, restored),
|
|
537
|
+
firstChangedLine: diff.firstChangedLine,
|
|
538
|
+
reads: { [key]: snapshot },
|
|
539
|
+
},
|
|
540
|
+
diagnosticText,
|
|
541
|
+
];
|
|
542
|
+
});
|
|
538
543
|
|
|
539
|
-
throwIfAborted(signal);
|
|
540
|
-
// 写后等待文档诊断,ERROR 级错误追加到输出让模型可见
|
|
541
|
-
const diagnosticText = await service.lspDiagnosticsForFile(filePath, ctx.cwd);
|
|
542
544
|
const text = diagnosticText
|
|
543
545
|
? `${message}\n\nLSP errors detected in this file, please fix:\n${diagnosticText}`
|
|
544
546
|
: message;
|
|
@@ -583,51 +585,50 @@ export function registerFileTools(
|
|
|
583
585
|
absolutePath: filePath,
|
|
584
586
|
change: { oldText: "", newText: params.content },
|
|
585
587
|
});
|
|
586
|
-
const [message, details] = await withFileMutationQueue<
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
}
|
|
599
|
-
} catch (error) {
|
|
600
|
-
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") {
|
|
601
|
-
throw error;
|
|
602
|
-
}
|
|
588
|
+
const [message, details, diagnosticText] = await withFileMutationQueue<
|
|
589
|
+
[string, FileToolDetails, string]
|
|
590
|
+
>(filePath, async () => {
|
|
591
|
+
let original: string | undefined;
|
|
592
|
+
let key: string | undefined;
|
|
593
|
+
try {
|
|
594
|
+
const value = await stat(filePath);
|
|
595
|
+
if (value.isFile()) {
|
|
596
|
+
const content = await readFile(filePath);
|
|
597
|
+
key = await readStateKey(filePath);
|
|
598
|
+
requireCurrentRead(state, key, filePath, content);
|
|
599
|
+
original = content.toString("utf8");
|
|
603
600
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
601
|
+
} catch (error) {
|
|
602
|
+
if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") {
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
throwIfAborted(signal);
|
|
607
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
608
|
+
await writeFile(filePath, params.content, "utf8");
|
|
609
|
+
const snapshot = snapshotOf(params.content);
|
|
610
|
+
// 新建文件:writeFile 之后 realpath 才能解析;覆盖写则复用上面的 key
|
|
611
|
+
const resolvedKey = key ?? (await readStateKey(filePath));
|
|
612
|
+
state.reads.set(resolvedKey, snapshot);
|
|
613
|
+
const diff = generateDiffString(original ?? "", params.content);
|
|
614
|
+
const text =
|
|
615
|
+
original === undefined
|
|
616
|
+
? `File created successfully at: ${filePath}`
|
|
617
|
+
: `The file ${filePath} has been updated successfully.`;
|
|
618
|
+
throwIfAborted(signal);
|
|
619
|
+
const diagnosticText = await service.lspDiagnosticsForFile(filePath, ctx.cwd);
|
|
620
|
+
return [
|
|
621
|
+
text,
|
|
622
|
+
{
|
|
623
|
+
diff: diff.diff,
|
|
624
|
+
patch: generateUnifiedPatch(filePath, original ?? "", params.content),
|
|
625
|
+
firstChangedLine: diff.firstChangedLine,
|
|
626
|
+
reads: { [resolvedKey]: snapshot },
|
|
627
|
+
},
|
|
628
|
+
diagnosticText,
|
|
629
|
+
];
|
|
630
|
+
});
|
|
627
631
|
|
|
628
|
-
throwIfAborted(signal);
|
|
629
|
-
// 写后等待文档诊断,ERROR 级错误追加到输出让模型可见
|
|
630
|
-
const diagnosticText = await service.lspDiagnosticsForFile(filePath, ctx.cwd);
|
|
631
632
|
const text = diagnosticText
|
|
632
633
|
? `${message}\n\nLSP errors detected in this file, please fix:\n${diagnosticText}`
|
|
633
634
|
: message;
|
package/src/opencode/files.ts
CHANGED
|
@@ -506,79 +506,84 @@ function registerEditTool(pi: ExtensionAPI, service: LspService): void {
|
|
|
506
506
|
if (signal?.aborted) throw new Error("Operation aborted");
|
|
507
507
|
};
|
|
508
508
|
|
|
509
|
-
const [message, details] = await withFileMutationQueue(
|
|
510
|
-
|
|
509
|
+
const [message, details, diagnosticText] = await withFileMutationQueue(
|
|
510
|
+
absolutePath,
|
|
511
|
+
async () => {
|
|
512
|
+
throwIfAborted();
|
|
511
513
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
514
|
+
// opencode: 前置校验,先于空 oldString 分支
|
|
515
|
+
if (oldString === newString) {
|
|
516
|
+
throw new Error("No changes to apply: oldString and newString are identical.");
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// opencode: 空 oldString + 文件不存在 → 创建新文件;文件存在 → 报错
|
|
520
|
+
if (oldString === "") {
|
|
521
|
+
let exists = true;
|
|
522
|
+
try {
|
|
523
|
+
await access(absolutePath, constants.F_OK);
|
|
524
|
+
} catch {
|
|
525
|
+
exists = false;
|
|
526
|
+
}
|
|
527
|
+
throwIfAborted();
|
|
528
|
+
if (exists) {
|
|
529
|
+
throw new Error(
|
|
530
|
+
"oldString cannot be empty when editing an existing file. Provide the exact text to replace, or use write for an intentional full-file replacement.",
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
// opencode: writeWithDirs 自动创建父目录;newString 开头的 BOM 原样保留
|
|
534
|
+
await mkdir(dirname(absolutePath), { recursive: true });
|
|
535
|
+
throwIfAborted();
|
|
536
|
+
await writeFile(absolutePath, newString, "utf8");
|
|
537
|
+
throwIfAborted();
|
|
538
|
+
const diagnosticText = await service.lspDiagnosticsForFile(absolutePath, ctx.cwd);
|
|
539
|
+
return [
|
|
540
|
+
"Edit applied successfully.",
|
|
541
|
+
{ diff: "", patch: "", firstChangedLine: 0 },
|
|
542
|
+
diagnosticText,
|
|
543
|
+
] as const;
|
|
544
|
+
}
|
|
516
545
|
|
|
517
|
-
// opencode: 空 oldString + 文件不存在 → 创建新文件;文件存在 → 报错
|
|
518
|
-
if (oldString === "") {
|
|
519
|
-
let exists = true;
|
|
520
546
|
try {
|
|
521
|
-
await access(absolutePath, constants.
|
|
522
|
-
} catch {
|
|
523
|
-
|
|
547
|
+
await access(absolutePath, constants.R_OK | constants.W_OK);
|
|
548
|
+
} catch (error: unknown) {
|
|
549
|
+
throwIfAborted();
|
|
550
|
+
const msg =
|
|
551
|
+
error instanceof Error && "code" in error && typeof error.code === "string"
|
|
552
|
+
? `Error code: ${error.code}`
|
|
553
|
+
: String(error);
|
|
554
|
+
throw new Error(`Could not edit file: ${filePath}. ${msg}.`, { cause: error });
|
|
524
555
|
}
|
|
525
556
|
throwIfAborted();
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
// opencode: writeWithDirs 自动创建父目录;newString 开头的 BOM 原样保留
|
|
532
|
-
await mkdir(dirname(absolutePath), { recursive: true });
|
|
557
|
+
|
|
558
|
+
const buffer = await readFile(absolutePath);
|
|
559
|
+
const rawContent = buffer.toString("utf8");
|
|
533
560
|
throwIfAborted();
|
|
534
|
-
|
|
561
|
+
|
|
562
|
+
// Strip BOM then normalize line endings to LF.
|
|
563
|
+
// The opencode replacers split on \n and expect only LF.
|
|
564
|
+
const { bom, text: content } = stripBom(rawContent);
|
|
565
|
+
const originalEnding = detectLineEnding(content);
|
|
566
|
+
const normalizedContent = normalizeToLF(content);
|
|
567
|
+
|
|
568
|
+
const newContent = replace(normalizedContent, oldString, newString, replaceAll);
|
|
535
569
|
throwIfAborted();
|
|
536
|
-
return [
|
|
537
|
-
"Edit applied successfully.",
|
|
538
|
-
{ diff: "", patch: "", firstChangedLine: 0 },
|
|
539
|
-
] as const;
|
|
540
|
-
}
|
|
541
570
|
|
|
542
|
-
|
|
543
|
-
await
|
|
544
|
-
} catch (error: unknown) {
|
|
571
|
+
const finalContent = bom + restoreLineEndings(newContent, originalEnding);
|
|
572
|
+
await writeFile(absolutePath, finalContent, "utf8");
|
|
545
573
|
throwIfAborted();
|
|
546
|
-
const msg =
|
|
547
|
-
error instanceof Error && "code" in error && typeof error.code === "string"
|
|
548
|
-
? `Error code: ${error.code}`
|
|
549
|
-
: String(error);
|
|
550
|
-
throw new Error(`Could not edit file: ${filePath}. ${msg}.`, { cause: error });
|
|
551
|
-
}
|
|
552
|
-
throwIfAborted();
|
|
553
574
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
throwIfAborted();
|
|
566
|
-
|
|
567
|
-
const finalContent = bom + restoreLineEndings(newContent, originalEnding);
|
|
568
|
-
await writeFile(absolutePath, finalContent, "utf8");
|
|
569
|
-
throwIfAborted();
|
|
570
|
-
|
|
571
|
-
const diffResult = generateDiffString(normalizedContent, newContent);
|
|
572
|
-
const patch = generateUnifiedPatch(filePath, normalizedContent, newContent);
|
|
573
|
-
return [
|
|
574
|
-
"Edit applied successfully.",
|
|
575
|
-
{ diff: diffResult.diff, patch, firstChangedLine: diffResult.firstChangedLine },
|
|
576
|
-
] as const;
|
|
577
|
-
});
|
|
575
|
+
const diffResult = generateDiffString(normalizedContent, newContent);
|
|
576
|
+
const patch = generateUnifiedPatch(filePath, normalizedContent, newContent);
|
|
577
|
+
throwIfAborted();
|
|
578
|
+
const diagnosticText = await service.lspDiagnosticsForFile(absolutePath, ctx.cwd);
|
|
579
|
+
return [
|
|
580
|
+
"Edit applied successfully.",
|
|
581
|
+
{ diff: diffResult.diff, patch, firstChangedLine: diffResult.firstChangedLine },
|
|
582
|
+
diagnosticText,
|
|
583
|
+
] as const;
|
|
584
|
+
},
|
|
585
|
+
);
|
|
578
586
|
|
|
579
|
-
throwIfAborted();
|
|
580
|
-
// opencode: 写后等待文档诊断,ERROR 级错误追加到输出让模型可见
|
|
581
|
-
const diagnosticText = await service.lspDiagnosticsForFile(absolutePath, ctx.cwd);
|
|
582
587
|
const text = diagnosticText
|
|
583
588
|
? `${message}\n\nLSP errors detected in this file, please fix:\n${diagnosticText}`
|
|
584
589
|
: message;
|
|
@@ -639,38 +644,39 @@ function registerWriteTool(pi: ExtensionAPI, service: LspService): void {
|
|
|
639
644
|
if (signal?.aborted) throw new Error("Operation aborted");
|
|
640
645
|
};
|
|
641
646
|
|
|
642
|
-
const [message, details] = await withFileMutationQueue(
|
|
643
|
-
|
|
647
|
+
const [message, details, diagnosticText] = await withFileMutationQueue(
|
|
648
|
+
absolutePath,
|
|
649
|
+
async () => {
|
|
650
|
+
throwIfAborted();
|
|
644
651
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
try {
|
|
649
|
-
const fh = await open(absolutePath, "r");
|
|
652
|
+
// opencode: desiredBom = source.bom || next.bom —— 保留原文件 BOM,
|
|
653
|
+
// 否则用新内容自带的 BOM
|
|
654
|
+
let existing: Buffer | undefined;
|
|
650
655
|
try {
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
+
const fh = await open(absolutePath, "r");
|
|
657
|
+
try {
|
|
658
|
+
existing = Buffer.alloc(3);
|
|
659
|
+
const { bytesRead } = await fh.read(existing, 0, 3, 0);
|
|
660
|
+
if (bytesRead < 3) existing = undefined;
|
|
661
|
+
} finally {
|
|
662
|
+
await fh.close();
|
|
663
|
+
}
|
|
664
|
+
} catch {
|
|
665
|
+
// 文件不存在:无旧 BOM
|
|
656
666
|
}
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
660
|
-
throwIfAborted();
|
|
661
|
-
const { bom: desiredBom, text: nextText } = resolveBom(existing, content);
|
|
667
|
+
throwIfAborted();
|
|
668
|
+
const { bom: desiredBom, text: nextText } = resolveBom(existing, content);
|
|
662
669
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
670
|
+
await mkdir(dir, { recursive: true });
|
|
671
|
+
throwIfAborted();
|
|
672
|
+
await writeFile(absolutePath, desiredBom + nextText, "utf8");
|
|
673
|
+
throwIfAborted();
|
|
674
|
+
const diagnosticText = await service.lspDiagnosticsForFile(absolutePath, ctx.cwd);
|
|
667
675
|
|
|
668
|
-
|
|
669
|
-
|
|
676
|
+
return ["Wrote file successfully.", undefined, diagnosticText] as const;
|
|
677
|
+
},
|
|
678
|
+
);
|
|
670
679
|
|
|
671
|
-
throwIfAborted();
|
|
672
|
-
// opencode: 写后等待文档诊断,ERROR 级错误追加到输出让模型可见
|
|
673
|
-
const diagnosticText = await service.lspDiagnosticsForFile(absolutePath, ctx.cwd);
|
|
674
680
|
const text = diagnosticText
|
|
675
681
|
? `${message}\n\nLSP errors detected in this file, please fix:\n${diagnosticText}`
|
|
676
682
|
: message;
|