-
Notifications
You must be signed in to change notification settings - Fork 413
feat(ui): add keyboard prompt navigation #348
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
Open
thatdudealso
wants to merge
3
commits into
superagent-ai:main
Choose a base branch
from
thatdudealso:fm/grok-prompt-nav-p1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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,54 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| deriveUserPromptIndex, | ||
| findNearestPrompt, | ||
| getPromptNavigationDirection, | ||
| type PromptAnchor, | ||
| } from "./prompt-navigation"; | ||
|
|
||
| describe("deriveUserPromptIndex", () => { | ||
| it("returns the positions of user chat entries from the existing entry list", () => { | ||
| expect( | ||
| deriveUserPromptIndex([{ type: "assistant" }, { type: "user" }, { type: "tool_result" }, { type: "user" }]), | ||
| ).toEqual([1, 3]); | ||
| }); | ||
|
|
||
| it("also recognizes transcript messages by role", () => { | ||
| expect(deriveUserPromptIndex([{ role: "system" }, { role: "user" }, { role: "assistant" }])).toEqual([1]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("findNearestPrompt", () => { | ||
| const anchors: PromptAnchor[] = [ | ||
| { messageIndex: 1, offset: 10 }, | ||
| { messageIndex: 4, offset: 80 }, | ||
| { messageIndex: 7, offset: 160 }, | ||
| ]; | ||
|
|
||
| it("finds the nearest previous prompt relative to the current scroll", () => { | ||
| expect(findNearestPrompt(anchors, 120, "previous")).toEqual(anchors[1]); | ||
| }); | ||
|
|
||
| it("finds the nearest next prompt relative to the current scroll", () => { | ||
| expect(findNearestPrompt(anchors, 80, "next")).toEqual(anchors[2]); | ||
| }); | ||
|
|
||
| it("stops at both boundaries without wrapping", () => { | ||
| expect(findNearestPrompt(anchors, 0, "previous")).toBeNull(); | ||
| expect(findNearestPrompt(anchors, 160, "next")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getPromptNavigationDirection", () => { | ||
| it("maps the unambiguous control-letter events to navigation actions", () => { | ||
| expect(getPromptNavigationDirection({ name: "p", ctrl: true, sequence: "\u0010" })).toBe("previous"); | ||
| expect(getPromptNavigationDirection({ name: "n", ctrl: true, sequence: "\u000e" })).toBe("next"); | ||
| }); | ||
|
|
||
| it("does not treat terminal control-bracket bytes or arrow events as navigation", () => { | ||
| expect(getPromptNavigationDirection({ name: "escape", sequence: "\u001b" })).toBeNull(); | ||
| expect(getPromptNavigationDirection({ name: "\u001d", sequence: "\u001d" })).toBeNull(); | ||
| expect(getPromptNavigationDirection({ name: "up", ctrl: true })).toBeNull(); | ||
| expect(getPromptNavigationDirection({ name: "down", ctrl: true })).toBeNull(); | ||
| }); | ||
| }); |
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,66 @@ | ||
| export type PromptSourceEntry = { | ||
| role?: string; | ||
| type?: string; | ||
| }; | ||
|
|
||
| export type PromptNavigationDirection = "previous" | "next"; | ||
|
|
||
| export type PromptNavigationKey = { | ||
| name?: string; | ||
| sequence?: string; | ||
| ctrl?: boolean; | ||
| shift?: boolean; | ||
| option?: boolean; | ||
| meta?: boolean; | ||
| super?: boolean; | ||
| }; | ||
|
|
||
| export interface PromptAnchor { | ||
| messageIndex: number; | ||
| offset: number; | ||
| } | ||
|
|
||
| /** | ||
| * Map terminal-safe control-letter events to navigation actions. Control | ||
| * letters have unambiguous single-byte encodings and cannot be consumed as | ||
| * ordinary arrow movement by a focused textarea or scrollbox. | ||
| */ | ||
| export function getPromptNavigationDirection(key: PromptNavigationKey): PromptNavigationDirection | null { | ||
| if (key.ctrl && !key.shift && !key.option && !key.meta && !key.super) { | ||
| if (key.name === "p") return "previous"; | ||
| if (key.name === "n") return "next"; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Chat entries are derived from transcript messages. Keep prompt navigation | ||
| * derived from those entries so it cannot drift into a second message index. | ||
| */ | ||
| export function deriveUserPromptIndex(entries: readonly PromptSourceEntry[]): number[] { | ||
| return entries.flatMap((entry, index) => (entry.role === "user" || entry.type === "user" ? [index] : [])); | ||
| } | ||
|
|
||
| /** | ||
| * Find the closest prompt strictly before or after the current scroll top. | ||
| * Strict comparisons make a prompt already at the top count as the current | ||
| * prompt, so navigation never wraps at either boundary. | ||
| */ | ||
| export function findNearestPrompt( | ||
| anchors: readonly PromptAnchor[], | ||
| currentScrollTop: number, | ||
| direction: PromptNavigationDirection, | ||
| ): PromptAnchor | null { | ||
| if (direction === "previous") { | ||
| for (let index = anchors.length - 1; index >= 0; index -= 1) { | ||
| const anchor = anchors[index]; | ||
| if (anchor && anchor.offset < currentScrollTop) return anchor; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| for (const anchor of anchors) { | ||
| if (anchor.offset > currentScrollTop) return anchor; | ||
| } | ||
| return null; | ||
| } |
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.