-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add /undo slash command and keep replay in sync #277
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
+956
−3
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d66aa7
feat: add /undo slash command to withdraw last prompt from history
kermanx 4c373a7
feat: add /undo slash command and keep replay in sync
kermanx 421adaf
fix: only count real user prompts in undo and include skill-activatio…
kermanx 5e0675e
fix: keep undo state consistent
kermanx 9429183
fix
kermanx e296ec1
fix: align tui undo skill anchors
kermanx b79ffb0
fix
kermanx f99dca9
fix
kermanx 2ebc407
fix
kermanx fd6bd63
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,6 @@ | ||
| --- | ||
| "@moonshot-ai/agent-core": minor | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add `/undo` slash command to withdraw the last prompt from conversation history, and keep replay records in sync when a prompt is undone. | ||
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,188 @@ | ||
| import type { Component } from '@earendil-works/pi-tui'; | ||
|
|
||
| import { WelcomeComponent } from '../components/chrome/welcome'; | ||
| import { AgentGroupComponent } from '../components/messages/agent-group'; | ||
| import { AssistantMessageComponent } from '../components/messages/assistant-message'; | ||
| import { BackgroundAgentStatusComponent } from '../components/messages/background-agent-status'; | ||
| import { CronMessageComponent } from '../components/messages/cron-message'; | ||
| import { ReadGroupComponent } from '../components/messages/read-group'; | ||
| import { SkillActivationComponent } from '../components/messages/skill-activation'; | ||
| import { ThinkingComponent } from '../components/messages/thinking'; | ||
| import { ToolCallComponent } from '../components/messages/tool-call'; | ||
| import { UserMessageComponent } from '../components/messages/user-message'; | ||
| import { NO_ACTIVE_SESSION_MESSAGE } from '../constant/kimi-tui'; | ||
| import type { TranscriptEntry } from '../types'; | ||
| import { formatErrorMessage } from '../utils/event-payload'; | ||
| import { getTranscriptComponentEntry } from '../utils/transcript-component-metadata'; | ||
| import type { SlashCommandHost } from './dispatch'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Undo command | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| export async function handleUndoCommand( | ||
| host: SlashCommandHost, | ||
| args: string = '', | ||
| ): Promise<void> { | ||
| if (host.state.appState.streamingPhase !== 'idle') { | ||
| host.showError('Cannot undo while streaming — press Esc or Ctrl-C first.'); | ||
| return; | ||
| } | ||
|
|
||
| const count = parseUndoCount(args); | ||
| if (count === undefined) { | ||
| host.showError('Usage: /undo [count], where count is a positive integer.'); | ||
| return; | ||
| } | ||
|
|
||
| const session = host.session; | ||
| if (session === undefined) { | ||
| host.showError(NO_ACTIVE_SESSION_MESSAGE); | ||
| return; | ||
| } | ||
|
|
||
| const entries = host.state.transcriptEntries; | ||
| const lastUserIndex = findUndoAnchorEntryIndex(entries, count); | ||
| if (lastUserIndex === undefined) { | ||
| host.showError('Nothing to undo.'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await session.undoHistory(count); | ||
| } catch (error) { | ||
| const message = formatErrorMessage(error); | ||
| host.showError(`Failed to undo: ${message}`); | ||
| return; | ||
| } | ||
|
|
||
| const children = host.state.transcriptContainer.children; | ||
| const lastUserComponentIndex = findUndoAnchorComponentIndex(children, count); | ||
| if (lastUserComponentIndex !== undefined) { | ||
| removeUndoContextComponents(children, lastUserComponentIndex); | ||
| host.state.transcriptContainer.invalidate(); | ||
| } | ||
|
|
||
| const preservedEntries = entries.slice(lastUserIndex).filter( | ||
| (entry) => !isUndoContextEntry(entry), | ||
| ); | ||
| entries.splice(lastUserIndex, entries.length - lastUserIndex, ...preservedEntries); | ||
|
|
||
| if (entries.length === 0) { | ||
| renderWelcome(host); | ||
| } | ||
|
|
||
| host.state.ui.requestRender(); | ||
| } | ||
|
|
||
| function parseUndoCount(args: string): number | undefined { | ||
| const value = args.trim(); | ||
| if (value.length === 0) return 1; | ||
| if (!/^[1-9]\d*$/.test(value)) return undefined; | ||
| const count = Number(value); | ||
| return Number.isSafeInteger(count) ? count : undefined; | ||
| } | ||
|
|
||
| function isUndoAnchorEntry(entry: TranscriptEntry): boolean { | ||
| return ( | ||
| entry.kind === 'user' || | ||
| (entry.kind === 'skill_activation' && entry.skillTrigger === 'user-slash') | ||
| ); | ||
| } | ||
|
|
||
| function findUndoAnchorEntryIndex( | ||
| entries: readonly TranscriptEntry[], | ||
| count: number, | ||
| ): number | undefined { | ||
| let found = 0; | ||
| for (let i = entries.length - 1; i >= 0; i--) { | ||
| const entry = entries[i]; | ||
| if (entry !== undefined && isUndoAnchorEntry(entry)) { | ||
| found++; | ||
| if (found === count) return i; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function isUndoContextEntry(entry: TranscriptEntry): boolean { | ||
| switch (entry.kind) { | ||
| case 'user': | ||
| case 'assistant': | ||
| case 'tool_call': | ||
| case 'thinking': | ||
| case 'skill_activation': | ||
| case 'cron': | ||
| return true; | ||
| case 'status': | ||
|
kermanx marked this conversation as resolved.
|
||
| return entry.turnId !== undefined; | ||
| case 'welcome': | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function findUndoAnchorComponentIndex( | ||
| children: readonly Component[], | ||
| count: number, | ||
| ): number | undefined { | ||
| let found = 0; | ||
| for (let i = children.length - 1; i >= 0; i--) { | ||
| const child = children[i]; | ||
| if (child !== undefined && isUndoAnchorComponent(child)) { | ||
| found++; | ||
| if (found === count) return i; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function removeUndoContextComponents( | ||
| children: Component[], | ||
| startIndex: number, | ||
| ): void { | ||
| for (let i = children.length - 1; i >= startIndex; i--) { | ||
| const child = children[i]; | ||
| if (child !== undefined && isUndoContextComponent(child)) { | ||
| children.splice(i, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function isUndoAnchorComponent(child: Component): boolean { | ||
| return ( | ||
| child instanceof UserMessageComponent || | ||
| (child instanceof SkillActivationComponent && child.trigger === 'user-slash') | ||
| ); | ||
| } | ||
|
|
||
| function isUndoContextComponent(child: Component): boolean { | ||
| const entry = getTranscriptComponentEntry(child); | ||
| if (entry !== undefined) { | ||
| return isUndoContextEntry(entry); | ||
| } | ||
|
|
||
| return ( | ||
| child instanceof UserMessageComponent || | ||
| child instanceof AssistantMessageComponent || | ||
| child instanceof ThinkingComponent || | ||
| child instanceof ToolCallComponent || | ||
| child instanceof AgentGroupComponent || | ||
| child instanceof ReadGroupComponent || | ||
| child instanceof SkillActivationComponent || | ||
| child instanceof BackgroundAgentStatusComponent || | ||
|
kermanx marked this conversation as resolved.
|
||
| child instanceof CronMessageComponent | ||
| ); | ||
| } | ||
|
|
||
| function renderWelcome(host: SlashCommandHost): void { | ||
| if ( | ||
| host.state.transcriptContainer.children.some( | ||
| (child) => child instanceof WelcomeComponent, | ||
| ) | ||
| ) { | ||
| return; | ||
| } | ||
| host.state.transcriptContainer.addChild( | ||
| new WelcomeComponent(host.state.appState, host.state.theme.colors), | ||
| ); | ||
| } | ||
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
15 changes: 15 additions & 0 deletions
15
apps/kimi-code/src/tui/utils/transcript-component-metadata.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,15 @@ | ||
| import type { Component } from '@earendil-works/pi-tui'; | ||
|
|
||
| import type { TranscriptEntry } from '../types'; | ||
|
|
||
| const componentEntries = new WeakMap<Component, TranscriptEntry>(); | ||
|
|
||
| export function markTranscriptComponent(component: Component, entry: TranscriptEntry): void { | ||
| componentEntries.set(component, entry); | ||
| } | ||
|
|
||
| export function getTranscriptComponentEntry( | ||
| component: Component, | ||
| ): TranscriptEntry | undefined { | ||
| return componentEntries.get(component); | ||
| } |
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.
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.