fix: preserve original encoding when reading/writing non-UTF-8 files

Fixes #2069

- Add iconv-lite dependency for non-UTF-8 encoding support
- Add iconvHelper.ts as a CJS/ESM compatibility wrapper
- Update readFileWithEncoding() to detect and handle GBK/Big5/Shift_JIS
  using BOM detection -> UTF-8 validation -> chardet -> iconv-lite fallback
- Add detectFileEncoding() to identify file encoding before writes
- Update writeTextFile() to accept encoding option and encode with iconv-lite
- Update WriteFileTool and EditTool to detect and preserve original encoding
- Add tests for GBK read/write, detectFileEncoding, and encoding option
This commit is contained in:
LaZzyMan 2026-03-04 14:52:46 +08:00
parent 407a66c959
commit a5eb1733fa
11 changed files with 375 additions and 32 deletions

View file

@ -57,7 +57,7 @@ export class AcpFileSystemService implements FileSystemService {
async writeTextFile(
filePath: string,
content: string,
options?: { bom?: boolean },
options?: { bom?: boolean; encoding?: string },
): Promise<void> {
if (!this.capabilities.writeTextFile) {
return this.fallback.writeTextFile(filePath, content, options);