-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(cli): stream UTF-8 reads in read tool again #10077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+84
−7
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@kilocode/cli": patch | ||
| --- | ||
|
|
||
| Speed up reading large files: the `read` tool now streams UTF-8 content from disk and stops once the line/byte cap is reached, instead of loading the whole file into memory first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { createReadStream } from "fs" | ||
| import { PassThrough, Readable } from "stream" | ||
| import * as Encoding from "./encoding" | ||
|
|
||
| /** | ||
| * Encoding-aware text streaming for tools that walk a file line by line. | ||
| * Optimistically stream as UTF-8; fall back to a buffered iconv decode only | ||
| * when the bytes turn out not to be valid UTF-8. | ||
| * | ||
| * import * as TextStream from "../kilocode/text-stream" | ||
| */ | ||
|
|
||
| /** Distinct class so {@link withFallback} can tell us apart from real I/O failures. */ | ||
| export class InvalidUtf8Error extends Error { | ||
| constructor() { | ||
| super("invalid utf-8") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * UTF-8 text Readable for `filepath`. A leading UTF-8 BOM passes through as | ||
| * U+FEFF — same as `createReadStream({ encoding: "utf8" })`. | ||
| */ | ||
| export function openUtf8(filepath: string): Readable { | ||
| const out = new PassThrough({ encoding: "utf8" }) | ||
| const raw = createReadStream(filepath) | ||
| const decoder = new TextDecoder("utf-8", { fatal: true }) | ||
| raw.on("data", (chunk) => { | ||
| try { | ||
| const text = decoder.decode(chunk as Buffer, { stream: true }) | ||
| if (text) out.write(text) | ||
| } catch { | ||
| raw.destroy() | ||
| out.destroy(new InvalidUtf8Error()) | ||
| } | ||
| }) | ||
| raw.on("end", () => { | ||
| try { | ||
| const tail = decoder.decode() | ||
| if (tail) out.write(tail) | ||
| out.end() | ||
| } catch { | ||
| out.destroy(new InvalidUtf8Error()) | ||
| } | ||
| }) | ||
| raw.on("error", (err) => out.destroy(err)) | ||
| // Propagate consumer-side teardown so early-exit (line / byte cap, fallback) | ||
| // stops pulling chunks from disk instead of running to EOF. | ||
| out.on("close", () => raw.destroy()) | ||
| return out | ||
| } | ||
|
|
||
| /** Whole-file UTF-8 Readable via {@link Encoding.read}; buffers the entire decoded file. */ | ||
| export async function openDecoded(filepath: string): Promise<Readable> { | ||
| const decoded = await Encoding.read(filepath) | ||
| return Readable.from([decoded.text]) | ||
| } | ||
|
|
||
| /** | ||
| * Run `fn` against an optimistic UTF-8 stream; on {@link InvalidUtf8Error} | ||
| * retry once against {@link openDecoded}. Other errors propagate. | ||
| */ | ||
| export async function withFallback<T>(filepath: string, fn: (input: Readable) => Promise<T>): Promise<T> { | ||
| try { | ||
| return await fn(openUtf8(filepath)) | ||
| } catch (err) { | ||
| if (!(err instanceof InvalidUtf8Error)) throw err | ||
| } | ||
| return fn(await openDecoded(filepath)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.