-
Notifications
You must be signed in to change notification settings - Fork 907
feat(tui): pin the last user message at the top of the viewport #2491
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
Closed
lucastononro
wants to merge
3
commits into
MoonshotAI:main
from
lucastononro:feat/pin-last-user-message
+503
−2
Closed
Changes from all commits
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
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 @@ | ||
| --- | ||
| "@moonshot-ai/pi-tui": patch | ||
| --- | ||
|
|
||
| Add read-only accessors for the rendered buffer's viewport top and content height so components can read the engine's scroll state. |
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 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Pin your last sent message at the top of the terminal viewport once it scrolls off, so the prompt behind the current work stays visible. Opt out with `pin_last_user_message = false` in `tui.toml`. |
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
127 changes: 127 additions & 0 deletions
127
apps/kimi-code/src/tui/components/messages/pinned-user-message.ts
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,127 @@ | ||
| /** | ||
| * Renders the user's most recently sent message as a band pinned to the top | ||
| * of the viewport (mounted as a non-capturing, top-anchored overlay), so the | ||
| * prompt that kicked off the current work stays visible while the transcript | ||
| * scrolls underneath — same idea as Claude Code's pinned prompt. | ||
| * | ||
| * The component snapshots the message text (the transcript window may trim | ||
| * the original UserMessageComponent) and stays invisible until the original | ||
| * message has scrolled above the viewport, so it never duplicates content | ||
| * that is still on screen. Visibility lags one frame because it is derived | ||
| * from the engine's last-completed frame (`TUI.getViewportTop`), which is | ||
| * imperceptible in practice. | ||
| */ | ||
|
|
||
| import { Text, truncateToWidth, visibleWidth, type Component, type TUI } from '@moonshot-ai/pi-tui'; | ||
|
|
||
| import { USER_MESSAGE_BULLET } from '#/tui/constant/symbols'; | ||
| import { currentTheme } from '#/tui/theme'; | ||
|
|
||
| /** Maximum band height in rows; longer messages are ellipsized. */ | ||
| const MAX_LINES = 3; | ||
|
|
||
| const EMPTY_LINES: string[] = []; | ||
|
|
||
| export class PinnedUserMessageComponent implements Component { | ||
| private readonly tui: TUI; | ||
| private readonly isEnabled: () => boolean; | ||
| private text = ''; | ||
| /** Root-buffer line index one past the message's last line, measured from | ||
| * the transcript container's rendered height right after the append. The | ||
| * pin shows once the viewport top passes it. */ | ||
| private anchorLine = 0; | ||
| private generation = 0; | ||
|
|
||
| private wrapCache: { width: number; lines: string[] } | undefined; | ||
| private bandCache: | ||
| | { width: number; generation: number; visible: boolean; lines: string[] } | ||
| | undefined; | ||
|
|
||
| constructor(tui: TUI, isEnabled: () => boolean) { | ||
| this.tui = tui; | ||
| this.isEnabled = isEnabled; | ||
| } | ||
|
|
||
| /** Snapshot a freshly sent user message. `anchorLine` should be the | ||
| * transcript container's rendered height right after the append (root-buffer | ||
| * line index one past the message's last line). */ | ||
| setMessage(text: string, anchorLine: number): void { | ||
| this.text = text; | ||
| this.anchorLine = anchorLine; | ||
| this.generation += 1; | ||
| this.wrapCache = undefined; | ||
| this.bandCache = undefined; | ||
| } | ||
|
|
||
| /** Hide the pin (session clear / new session). */ | ||
| clear(): void { | ||
| if (this.text.length === 0) return; | ||
| this.text = ''; | ||
| this.generation += 1; | ||
| this.wrapCache = undefined; | ||
| this.bandCache = undefined; | ||
| } | ||
|
|
||
| invalidate(): void { | ||
| // Theme change: re-dye from the current palette on the next render. | ||
| this.bandCache = undefined; | ||
| } | ||
|
|
||
| /** Plain-text wrap of the message at `contentWidth`, cached per width. */ | ||
| private wrapLines(contentWidth: number): string[] { | ||
| if (this.wrapCache !== undefined && this.wrapCache.width === contentWidth) { | ||
| return this.wrapCache.lines; | ||
| } | ||
| const lines = new Text(this.text, 0, 0).render(contentWidth); | ||
| this.wrapCache = { width: contentWidth, lines }; | ||
| return lines; | ||
| } | ||
|
|
||
| render(width: number): string[] { | ||
| const safeWidth = Math.max(0, width); | ||
| if (safeWidth <= 0 || this.text.length === 0 || !this.isEnabled()) return EMPTY_LINES; | ||
|
|
||
| const bullet = USER_MESSAGE_BULLET; | ||
| const bulletWidth = visibleWidth(bullet); | ||
| const contentWidth = Math.max(1, safeWidth - bulletWidth); | ||
|
|
||
| // anchorLine sits one past the message's last buffer line, so the pin | ||
| // appears exactly when the original entry has fully scrolled off. | ||
| const visible = this.tui.getViewportTop() >= this.anchorLine; | ||
|
|
||
| if ( | ||
| this.bandCache !== undefined && | ||
| this.bandCache.width === safeWidth && | ||
| this.bandCache.generation === this.generation && | ||
| this.bandCache.visible === visible | ||
| ) { | ||
| return this.bandCache.lines; | ||
| } | ||
|
|
||
| const wrapped = this.wrapLines(contentWidth); | ||
|
|
||
| let lines: string[] = EMPTY_LINES; | ||
| if (visible) { | ||
| let capped = wrapped; | ||
| if (wrapped.length > MAX_LINES) { | ||
| capped = wrapped.slice(0, MAX_LINES); | ||
| // Ellipsize the last visible row, guaranteeing the marker fits. | ||
| const last = truncateToWidth(capped[MAX_LINES - 1]!, Math.max(1, contentWidth - 2), ''); | ||
| capped[MAX_LINES - 1] = `${last} …`; | ||
| } | ||
| // Subtle styling: kimi-blue text on the terminal's own background (the | ||
| // overlay compositor blanks the rows underneath), separated from the | ||
| // scrolling transcript by a thin muted rule. | ||
| lines = capped.map((line, index) => { | ||
| const prefix = index === 0 ? bullet : ' '.repeat(bulletWidth); | ||
| const styled = currentTheme.fg('primary', prefix + line); | ||
| const pad = Math.max(0, safeWidth - visibleWidth(styled)); | ||
| return styled + ' '.repeat(pad); | ||
| }); | ||
| lines.push(currentTheme.dimFg('textMuted', '─'.repeat(safeWidth))); | ||
| } | ||
|
|
||
| this.bandCache = { width: safeWidth, generation: this.generation, visible, lines }; | ||
| return lines; | ||
| } | ||
| } |
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
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
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.