-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(cli): abbreviate large pasted text and optimize paste performance #4916
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a56107
feat(cli): abbreviate large pasted text and optimize paste performance
marius-kilocode 6ce23bb
Merge branch 'main' into cli-bracketed-paste
marius-kilocode 8d05df6
Merge branch 'main' into cli-bracketed-paste
marius-kilocode cf2cdda
Merge main into cli-bracketed-paste
marius-kilocode 23b2132
Merge branch 'main' into cli-bracketed-paste
marius-kilocode 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": minor | ||
| --- | ||
|
|
||
| Abbreviate large pasted text in CLI input as `[Pasted text #N +X lines]` to prevent input field overflow when pasting logs or large code blocks |
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,159 @@ | ||
| import { | ||
| extractPastedTextReferences, | ||
| removePastedTextReferences, | ||
| expandPastedTextReferences, | ||
| PASTED_TEXT_REFERENCE_REGEX, | ||
| } from "../processPastedText" | ||
|
|
||
| describe("processPastedText helpers", () => { | ||
| describe("PASTED_TEXT_REFERENCE_REGEX", () => { | ||
| it("should match valid pasted text references", () => { | ||
| const text = "[Pasted text #1 +25 lines]" | ||
| PASTED_TEXT_REFERENCE_REGEX.lastIndex = 0 | ||
| const match = PASTED_TEXT_REFERENCE_REGEX.exec(text) | ||
| expect(match).not.toBeNull() | ||
| expect(match?.[1]).toBe("1") | ||
| expect(match?.[2]).toBe("25") | ||
| }) | ||
|
|
||
| it("should match references with large numbers", () => { | ||
| const text = "[Pasted text #999 +1000 lines]" | ||
| PASTED_TEXT_REFERENCE_REGEX.lastIndex = 0 | ||
| const match = PASTED_TEXT_REFERENCE_REGEX.exec(text) | ||
| expect(match).not.toBeNull() | ||
| expect(match?.[1]).toBe("999") | ||
| expect(match?.[2]).toBe("1000") | ||
| }) | ||
|
|
||
| it("should not match malformed references", () => { | ||
| const malformed = [ | ||
| "[Pasted text #1]", // missing line count | ||
| "[Pasted text 1 +25 lines]", // missing # | ||
| "[Pasted #1 +25 lines]", // missing "text" | ||
| "Pasted text #1 +25 lines", // missing brackets | ||
| "[Pasted text #a +25 lines]", // non-numeric ref | ||
| ] | ||
|
|
||
| for (const text of malformed) { | ||
| PASTED_TEXT_REFERENCE_REGEX.lastIndex = 0 | ||
| const match = PASTED_TEXT_REFERENCE_REGEX.exec(text) | ||
| expect(match).toBeNull() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe("extractPastedTextReferences", () => { | ||
| it("should extract single reference number", () => { | ||
| const input = "Hello [Pasted text #1 +25 lines] world" | ||
| const result = extractPastedTextReferences(input) | ||
| expect(result).toEqual([1]) | ||
| }) | ||
|
|
||
| it("should extract multiple reference numbers in order", () => { | ||
| const input = "[Pasted text #1 +10 lines] and [Pasted text #3 +50 lines] and [Pasted text #2 +30 lines]" | ||
| const result = extractPastedTextReferences(input) | ||
| expect(result).toEqual([1, 3, 2]) | ||
| }) | ||
|
|
||
| it("should return empty array when no references", () => { | ||
| const input = "Hello world" | ||
| const result = extractPastedTextReferences(input) | ||
| expect(result).toEqual([]) | ||
| }) | ||
|
|
||
| it("should handle large reference numbers", () => { | ||
| const input = "[Pasted text #999 +1 lines]" | ||
| const result = extractPastedTextReferences(input) | ||
| expect(result).toEqual([999]) | ||
| }) | ||
|
|
||
| it("should not extract from similar but invalid patterns", () => { | ||
| const input = "[Pasted text #1] [Image #2] [Pasted #3 +10 lines]" | ||
| const result = extractPastedTextReferences(input) | ||
| expect(result).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe("removePastedTextReferences", () => { | ||
| it("should remove pasted text reference tokens without collapsing whitespace", () => { | ||
| const input = "Line1\n [Pasted text #1 +25 lines]\nLine3" | ||
| const result = removePastedTextReferences(input) | ||
| expect(result).toBe("Line1\n \nLine3") | ||
| }) | ||
|
|
||
| it("should remove multiple references", () => { | ||
| const input = "Hello [Pasted text #1 +10 lines] world [Pasted text #2 +20 lines] test" | ||
| const result = removePastedTextReferences(input) | ||
| expect(result).toBe("Hello world test") | ||
| }) | ||
|
|
||
| it("should handle text with no references", () => { | ||
| const input = "Hello world" | ||
| const result = removePastedTextReferences(input) | ||
| expect(result).toBe("Hello world") | ||
| }) | ||
|
|
||
| it("should preserve image references", () => { | ||
| const input = "[Image #1] [Pasted text #2 +10 lines]" | ||
| const result = removePastedTextReferences(input) | ||
| expect(result).toBe("[Image #1] ") | ||
| }) | ||
| }) | ||
|
|
||
| describe("expandPastedTextReferences", () => { | ||
| it("should expand single reference with full text", () => { | ||
| const input = "Check this: [Pasted text #1 +3 lines]" | ||
| const references = { 1: "line one\nline two\nline three" } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("Check this: line one\nline two\nline three") | ||
| }) | ||
|
|
||
| it("should expand multiple references", () => { | ||
| const input = "[Pasted text #1 +2 lines] and [Pasted text #2 +1 lines]" | ||
| const references = { | ||
| 1: "first\nsecond", | ||
| 2: "single line", | ||
| } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("first\nsecond and single line") | ||
| }) | ||
|
|
||
| it("should leave unknown references unchanged", () => { | ||
| const input = "Check this: [Pasted text #99 +10 lines]" | ||
| const references = { 1: "some text" } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("Check this: [Pasted text #99 +10 lines]") | ||
| }) | ||
|
|
||
| it("should handle empty references map", () => { | ||
| const input = "[Pasted text #1 +5 lines]" | ||
| const references = {} | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("[Pasted text #1 +5 lines]") | ||
| }) | ||
|
|
||
| it("should handle text with no references", () => { | ||
| const input = "Hello world" | ||
| const references = { 1: "unused" } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("Hello world") | ||
| }) | ||
|
|
||
| it("should handle mixed references (expand known, keep unknown)", () => { | ||
| const input = "[Pasted text #1 +2 lines] [Pasted text #2 +3 lines] [Pasted text #3 +1 lines]" | ||
| const references = { | ||
| 1: "found\ntext", | ||
| 3: "also found", | ||
| } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe("found\ntext [Pasted text #2 +3 lines] also found") | ||
| }) | ||
|
|
||
| it("should preserve surrounding text and whitespace", () => { | ||
| const input = " Before [Pasted text #1 +1 lines] After " | ||
| const references = { 1: "middle" } | ||
| const result = expandPastedTextReferences(input, references) | ||
| expect(result).toBe(" Before middle After ") | ||
| }) | ||
| }) | ||
| }) |
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,28 @@ | ||
| // Regex to match pasted text references: [Pasted text #N +X lines] | ||
| export const PASTED_TEXT_REFERENCE_REGEX = /\[Pasted text #(\d+) \+(\d+) lines\]/g | ||
|
|
||
| export function extractPastedTextReferences(text: string): number[] { | ||
| const refs: number[] = [] | ||
| let match | ||
| PASTED_TEXT_REFERENCE_REGEX.lastIndex = 0 | ||
| while ((match = PASTED_TEXT_REFERENCE_REGEX.exec(text)) !== null) { | ||
| const ref = match[1] | ||
| if (ref !== undefined) { | ||
| refs.push(parseInt(ref, 10)) | ||
| } | ||
| } | ||
| return refs | ||
| } | ||
|
|
||
| export function removePastedTextReferences(text: string): string { | ||
| return text.replace(PASTED_TEXT_REFERENCE_REGEX, "") | ||
| } | ||
|
|
||
| export function expandPastedTextReferences(text: string, pastedTextReferences: Record<number, string>): string { | ||
| return text.replace(PASTED_TEXT_REFERENCE_REGEX, (match, refNum) => { | ||
| const content = pastedTextReferences[parseInt(refNum, 10)] | ||
| if (content === undefined) return match | ||
| // Normalize tabs to spaces when expanding | ||
| return content.replace(/\t/g, " ") | ||
| }) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ok for now, but a much better approach for the future, in my opinion, would be to refactor the input field to be an array of objects, for example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking back at this, I agree. This is the better approach. I will get a follow up done.