-
Notifications
You must be signed in to change notification settings - Fork 165
feat: add btw side-channel command #338
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
+1,839
−60
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c115889
feat: add btw side-channel command
kermanx 461ce0c
feat: support multi-turn btw side conversations
kermanx d02b48c
fix: restore main agent after btw prompt
kermanx c70d3e8
fix: satisfy btw lint checks
kermanx 59586a6
fix: limit btw config sync
kermanx 7d05c46
feat: refine btw panel interactions
kermanx fb8fc3d
feat: improve btw panel scrolling
kermanx 571a57d
fix: clarify btw side-agent reminder
kermanx b920cbb
fix
kermanx e4ec506
fix
kermanx 1af3ec0
fix
kermanx 9bf9987
Merge remote-tracking branch 'origin/main' into xtr/btw-side-agent
kermanx 8e4d550
fix
kermanx da56cde
fix
kermanx 7553ac1
fix
kermanx 5f1c0f4
fix
kermanx e77d343
fix
kermanx d855647
fix
kermanx eaf9e8d
Merge remote-tracking branch 'origin/main' into xtr/btw-side-agent
kermanx 59250e6
fix
kermanx 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,7 @@ | ||
| --- | ||
| "@moonshot-ai/agent-core": minor | ||
| "@moonshot-ai/kimi-code-sdk": minor | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add `/btw` for side-channel conversations without steering the active main turn. |
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,25 @@ | ||
| import { LLM_NOT_SET_MESSAGE } from '../constant/kimi-tui'; | ||
| import { formatErrorMessage } from '../utils/event-payload'; | ||
| import type { SlashCommandHost } from './dispatch'; | ||
|
|
||
| export async function handleBtwCommand(host: SlashCommandHost, args: string): Promise<void> { | ||
| const prompt = args.trim(); | ||
| if (prompt.length === 0) { | ||
| host.showError('Usage: /btw <question>'); | ||
| return; | ||
| } | ||
|
|
||
| const session = host.session; | ||
| if (host.state.appState.model.trim().length === 0 || session === undefined) { | ||
| host.showError(LLM_NOT_SET_MESSAGE); | ||
| return; | ||
| } | ||
| host.btwPanelController.closeOrCancel(); | ||
|
|
||
| try { | ||
| const agentId = await session.startBtw(); | ||
| host.btwPanelController.open(agentId, prompt); | ||
| } catch (error) { | ||
| host.showError(`Failed to start /btw: ${formatErrorMessage(error)}`); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| import type { Component, MarkdownTheme } from '@earendil-works/pi-tui'; | ||
| import { | ||
| Markdown, | ||
| Text, | ||
| truncateToWidth, | ||
| visibleWidth, | ||
| } from '@earendil-works/pi-tui'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| import { THINKING_PREVIEW_LINES } from '../../constant/rendering'; | ||
| import type { ColorPalette } from '../../theme/colors'; | ||
|
|
||
| type BtwPanelPhase = 'running' | 'done' | 'failed'; | ||
|
|
||
| const MIN_COLLAPSED_PANEL_LINES = 3; | ||
|
|
||
| interface BtwTurn { | ||
| readonly prompt: string; | ||
| answer: string; | ||
| thinking: string; | ||
| error?: string | undefined; | ||
| phase: BtwPanelPhase; | ||
| } | ||
|
|
||
| interface BtwBodyRender { | ||
| readonly lines: string[]; | ||
| readonly truncated: boolean; | ||
| } | ||
|
|
||
| export interface BtwPanelOptions { | ||
| readonly colors: ColorPalette; | ||
| readonly markdownTheme: MarkdownTheme; | ||
| readonly canUseScrollKeys: () => boolean; | ||
| readonly onPrompt: (prompt: string) => void; | ||
| readonly terminalRows: () => number; | ||
| } | ||
|
|
||
| export class BtwPanelComponent implements Component { | ||
| private readonly turns: BtwTurn[] = []; | ||
| private readonly transientNotices: string[] = []; | ||
| private minBodyLines = 0; | ||
| private followTail = true; | ||
| private scrollTop = 0; | ||
| private maxScrollTop = 0; | ||
|
|
||
| constructor(private readonly options: BtwPanelOptions) {} | ||
|
|
||
| submit(prompt: string): void { | ||
| const normalized = prompt.trim(); | ||
| if (normalized.length === 0 || this.isRunning()) return; | ||
| this.followTail = true; | ||
| this.scrollTop = 0; | ||
| this.transientNotices.length = 0; | ||
| this.turns.push({ | ||
| prompt: normalized, | ||
| answer: '', | ||
| thinking: '', | ||
| phase: 'running', | ||
| }); | ||
| this.options.onPrompt(normalized); | ||
| } | ||
|
|
||
| addTransientNotice(message: string): void { | ||
| this.transientNotices.push(message); | ||
| this.followTail = true; | ||
| } | ||
|
|
||
| appendAnswer(delta: string): void { | ||
| const turn = this.currentTurn(); | ||
| if (turn === undefined) return; | ||
| turn.answer += delta; | ||
| } | ||
|
|
||
| appendThinking(delta: string): void { | ||
| const turn = this.currentTurn(); | ||
| if (turn === undefined) return; | ||
| turn.thinking += delta; | ||
| } | ||
|
|
||
| markDone(resultSummary?: string | undefined): void { | ||
| const turn = this.currentTurn(); | ||
| if (turn === undefined) return; | ||
| if (turn.answer.trim().length === 0 && resultSummary !== undefined) { | ||
| turn.answer = resultSummary; | ||
| } | ||
| this.transientNotices.length = 0; | ||
| turn.phase = 'done'; | ||
| } | ||
|
|
||
| markFailed(error: string): void { | ||
| const turn = this.currentTurn(); | ||
| if (turn === undefined || turn.phase !== 'running') { | ||
| this.turns.push({ | ||
| prompt: '', | ||
| answer: '', | ||
| thinking: '', | ||
| error, | ||
| phase: 'failed', | ||
| }); | ||
| this.transientNotices.length = 0; | ||
| return; | ||
| } | ||
| turn.error = error; | ||
| this.transientNotices.length = 0; | ||
| turn.phase = 'failed'; | ||
| } | ||
|
|
||
| invalidate(): void {} | ||
|
|
||
| render(width: number): string[] { | ||
| const safeWidth = Math.max(4, width); | ||
| const contentWidth = Math.max(1, safeWidth - 4); | ||
| const body = this.renderBody(contentWidth); | ||
| const lines = [this.renderTopBorder(safeWidth, body.truncated)]; | ||
| for (const line of body.lines) { | ||
| lines.push(this.renderBodyLine(line, safeWidth)); | ||
| } | ||
| return lines; | ||
| } | ||
|
|
||
| private renderTopBorder(width: number, truncated: boolean): string { | ||
| const paint = (s: string): string => chalk.hex(this.options.colors.border)(s); | ||
| const hint = truncated && this.options.canUseScrollKeys() | ||
| ? 'Esc close · ↑↓ scroll ' | ||
| : 'Esc close '; | ||
| const title = | ||
| chalk.hex(this.options.colors.accent).bold(' BTW ') + | ||
| paint('─ ') + | ||
| chalk.hex(this.options.colors.textMuted)(hint); | ||
| const innerWidth = Math.max(1, width - 2); | ||
| const clippedTitle = | ||
| visibleWidth(title) > innerWidth ? truncateToWidth(title, innerWidth, '') : title; | ||
| const dashCount = Math.max(0, innerWidth - visibleWidth(clippedTitle)); | ||
| return paint('╭') + clippedTitle + paint('─'.repeat(dashCount)) + paint('╮'); | ||
| } | ||
|
|
||
| private renderBody(width: number): BtwBodyRender { | ||
| const lines: string[] = []; | ||
| for (const [index, turn] of this.turns.entries()) { | ||
| if (index > 0) lines.push(''); | ||
| lines.push(...this.renderTurn(turn, width)); | ||
| } | ||
| if (this.turns.length === 0) { | ||
| lines.push(chalk.hex(this.options.colors.textDim)('Ready for a side question...')); | ||
| } | ||
| lines.push(...this.renderTransientNotices(width)); | ||
| return this.fitBodyLines(lines); | ||
| } | ||
|
|
||
| private renderTransientNotices(width: number): string[] { | ||
| const lines: string[] = []; | ||
| for (const notice of this.transientNotices) { | ||
| lines.push(...new Text(chalk.hex(this.options.colors.textDim)(notice), 0, 0).render(width)); | ||
| } | ||
| return lines; | ||
| } | ||
|
|
||
| private fitBodyLines(lines: string[]): BtwBodyRender { | ||
| const bodyLimit = this.collapsedBodyLimit(); | ||
| const targetUncapped = Math.max(this.minBodyLines, lines.length); | ||
| const target = | ||
| bodyLimit === undefined ? targetUncapped : Math.min(bodyLimit, targetUncapped); | ||
| this.minBodyLines = Math.max(this.minBodyLines, target); | ||
|
|
||
| if (lines.length > target) { | ||
| this.maxScrollTop = lines.length - target; | ||
| if (this.followTail) { | ||
| this.scrollTop = this.maxScrollTop; | ||
| } else { | ||
| this.scrollTop = Math.min(this.scrollTop, this.maxScrollTop); | ||
| } | ||
| const start = this.scrollTop; | ||
| return { lines: lines.slice(start, start + target), truncated: true }; | ||
| } | ||
|
|
||
| this.followTail = true; | ||
| this.scrollTop = 0; | ||
| this.maxScrollTop = 0; | ||
| const padded = [...lines]; | ||
| while (padded.length < target) { | ||
| padded.push(''); | ||
| } | ||
| return { lines: padded, truncated: false }; | ||
| } | ||
|
|
||
| private collapsedBodyLimit(): number | undefined { | ||
| const terminalRows = this.options.terminalRows(); | ||
| if (!Number.isFinite(terminalRows) || terminalRows <= 0) return undefined; | ||
| const maxPanelLines = Math.max(MIN_COLLAPSED_PANEL_LINES, Math.floor(terminalRows / 2)); | ||
| return Math.max(1, maxPanelLines - 1); | ||
| } | ||
|
|
||
| private renderTurn(turn: BtwTurn, width: number): string[] { | ||
| const prompt = chalk.hex(this.options.colors.accent)(`Q: ${turn.prompt}`); | ||
| const lines = [...new Text(prompt, 0, 0).render(width)]; | ||
| const answer = turn.answer.trim(); | ||
| const thinking = turn.thinking.trim(); | ||
| if (answer.length > 0) { | ||
| lines.push(...new Markdown(answer, 0, 0, this.options.markdownTheme).render(width)); | ||
| } else if (thinking.length > 0) { | ||
| const thinkingLines = new Text(chalk.hex(this.options.colors.textDim)(thinking), 0, 0).render( | ||
| width, | ||
| ); | ||
| const visibleThinking = | ||
| thinkingLines.length > THINKING_PREVIEW_LINES | ||
| ? thinkingLines.slice(thinkingLines.length - THINKING_PREVIEW_LINES) | ||
| : thinkingLines; | ||
| lines.push(...visibleThinking); | ||
| } else if (turn.error === undefined) { | ||
| lines.push(chalk.hex(this.options.colors.textDim)('Waiting for answer...')); | ||
| } | ||
| if (turn.error !== undefined) { | ||
| const error = chalk.hex(this.options.colors.error)(turn.error); | ||
| lines.push(...new Text(error, 0, 0).render(width)); | ||
| } | ||
| return lines; | ||
| } | ||
|
|
||
| private renderBodyLine(line: string, width: number): string { | ||
| const paint = (s: string): string => chalk.hex(this.options.colors.border)(s); | ||
| const contentWidth = Math.max(1, width - 4); | ||
| const clipped = | ||
| visibleWidth(line) > contentWidth ? truncateToWidth(line, contentWidth, '…') : line; | ||
| const padding = Math.max(0, contentWidth - visibleWidth(clipped)); | ||
| return paint('│') + ' ' + clipped + ' '.repeat(padding) + ' ' + paint('│'); | ||
| } | ||
|
|
||
| private currentTurn(): BtwTurn | undefined { | ||
| return this.turns.at(-1); | ||
| } | ||
|
|
||
| isRunning(): boolean { | ||
| return this.currentTurn()?.phase === 'running'; | ||
| } | ||
|
|
||
| scroll(direction: 'up' | 'down'): boolean { | ||
| if (this.maxScrollTop <= 0) return false; | ||
| const current = this.followTail ? this.maxScrollTop : this.scrollTop; | ||
| const next = | ||
| direction === 'up' | ||
| ? Math.max(0, current - 1) | ||
| : Math.min(this.maxScrollTop, current + 1); | ||
| this.scrollTop = next; | ||
| this.followTail = next === this.maxScrollTop; | ||
| return true; | ||
| } | ||
| } |
Oops, something went wrong.
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.