mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
feat: 添加 GBK 编码自动检测支持,文件读写工具透明处理非 UTF-8 文件
新增 encoding.ts 核心模块实现三层编码检测(BOM → UTF-8 fatal → GBK 回退), 改造同步/异步读取路径和写入路径,使 FileReadTool/FileEditTool/FileWriteTool 能正确处理 GBK 编码文件。包含完整单元测试和 spec 文档。 Co-Authored-By: glm-5-turbo <zai-org@claude-code-best.win>
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { detectFileEncoding } from './file.js'
|
||||
import { type FileEncoding, decodeBuffer } from './encoding.js'
|
||||
import { getFsImplementation } from './fsOperations.js'
|
||||
|
||||
type CachedFileData = {
|
||||
content: string
|
||||
encoding: BufferEncoding
|
||||
encoding: FileEncoding
|
||||
mtime: number
|
||||
}
|
||||
|
||||
@@ -19,7 +20,7 @@ class FileReadCache {
|
||||
* Reads a file with caching. Returns both content and encoding.
|
||||
* Cache key includes file path and modification time for automatic invalidation.
|
||||
*/
|
||||
readFile(filePath: string): { content: string; encoding: BufferEncoding } {
|
||||
readFile(filePath: string): { content: string; encoding: FileEncoding } {
|
||||
const fs = getFsImplementation()
|
||||
|
||||
// Get file stats for cache invalidation
|
||||
@@ -45,9 +46,8 @@ class FileReadCache {
|
||||
|
||||
// Cache miss or stale data - read the file
|
||||
const encoding = detectFileEncoding(filePath)
|
||||
const content = fs
|
||||
.readFileSync(filePath, { encoding })
|
||||
.replaceAll('\r\n', '\n')
|
||||
const rawBuffer = fs.readFileBytesSync(filePath)
|
||||
const content = decodeBuffer(rawBuffer, encoding).replaceAll('\r\n', '\n')
|
||||
|
||||
// Update cache
|
||||
this.cache.set(cacheKey, {
|
||||
|
||||
Reference in New Issue
Block a user