diff --git a/packages/core/src/tool/read-filesystem.ts b/packages/core/src/tool/read-filesystem.ts index b70e8ac1bdc..7fe5d34b51f 100644 --- a/packages/core/src/tool/read-filesystem.ts +++ b/packages/core/src/tool/read-filesystem.ts @@ -354,8 +354,12 @@ const textPage = (bytes: Uint8Array, eof: boolean, page: PageInput) => { return { entries, offset, next, consumed } } -const clampLine = (line: string) => - line.length > MAX_LINE_LENGTH ? line.slice(0, MAX_LINE_LENGTH) + MAX_LINE_SUFFIX : line +const clampLine = (line: string) => { + if (line.length <= MAX_LINE_LENGTH) return line + const code = line.charCodeAt(MAX_LINE_LENGTH - 1) + const end = code >= 0xd800 && code <= 0xdbff ? MAX_LINE_LENGTH - 1 : MAX_LINE_LENGTH + return line.slice(0, end) + MAX_LINE_SUFFIX +} // A trailing newline ends the last line instead of starting an empty one. const lineCount = (newlines: number, last: number | undefined) => newlines + (last === undefined || last === 10 ? 0 : 1) diff --git a/packages/core/test/tool-read-filesystem.test.ts b/packages/core/test/tool-read-filesystem.test.ts index cb0e8826e75..f60db5839a5 100644 --- a/packages/core/test/tool-read-filesystem.test.ts +++ b/packages/core/test/tool-read-filesystem.test.ts @@ -424,13 +424,13 @@ describe("ReadToolFileSystem", () => { Effect.gen(function* () { const { environment, files, directory } = yield* fixture const file = path.join(directory, "long.txt") - yield* files.writeFileString(file, "a".repeat(2_001)) + yield* files.writeFileString(file, `${"a".repeat(1_999)}😀tail`) const result = yield* ReadToolFileSystem.read(environment, absolute(file), "long.txt", { limit: 1 }) expect(result).toMatchObject({ type: "text-page", - content: `${"a".repeat(2_000)}... (line truncated to 2000 chars)`, + content: `${"a".repeat(1_999)}... (line truncated to 2000 chars)`, truncated: false, }) }),