Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/live-tui-mode-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Switch between regular and fullscreen TUI modes immediately from `/settings`, `/reload`, and `/reload-tui` without restarting the CLI.
71 changes: 57 additions & 14 deletions apps/kimi-code/src/tui/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
type TranscriptConfig,
type TuiConfig,
type TuiMode,
type TuiModeResolution,
} from '../config';
import type { ThemeName } from '#/tui/theme';
import { currentTheme, isBuiltInTheme, lightColors, loadCustomThemeMerged } from '#/tui/theme';
Expand All @@ -64,8 +65,13 @@ import {
type EffortSwitchRule,
} from '#/utils/effort-switch-config';
import { DEFAULT_OAUTH_PROVIDER_NAME } from '#/constant/app';
import { MAIN_AGENT_ID, NO_ACTIVE_SESSION_MESSAGE, TUI_MODE_RESTART_NOTICE } from '../constant/kimi-tui';
import {
MAIN_AGENT_ID,
NO_ACTIVE_SESSION_MESSAGE,
TUI_MODE_SWITCH_BLOCKED_NOTICE,
} from '../constant/kimi-tui';
import { DOCK_PREVIEW_ROWS } from '../constant/rendering';
import type { TuiModeSwitchResult } from '../tui-renderer';
import type { QueuedModelSwitch } from '../types';
import { formatErrorMessage } from '../utils/event-payload';
import { setMarkdownMermaidMode, type MermaidRenderMode } from '../utils/markdown-options';
Expand Down Expand Up @@ -1185,19 +1191,36 @@ export function showTuiModePicker(host: SlashCommandHost): void {
type TuiModeHost = {
readonly state: {
readonly appState: Pick<SlashCommandHost['state']['appState'], 'tuiMode'>;
readonly ui: Pick<SlashCommandHost['state']['ui'], 'mode'>;
};
setAppState(patch: Pick<SlashCommandHost['state']['appState'], 'tuiMode'>): void;
showStatus(msg: string, color?: string): void;
showNotice(msg: string): void;
switchTuiMode(mode: TuiMode): TuiModeSwitchResult;
track?: SlashCommandHost['track'];
};

export function tuiModeOverrideNotice(
resolution: TuiModeResolution,
configuredMode?: TuiMode,
source: 'settings' | 'reload' = 'settings',
): string | undefined {
if (configuredMode === undefined || resolution.mode === configuredMode) {
return undefined;
}
if (source === 'reload') {
return resolution.overriddenByEnv
? 'Unset KIMI_CODE_TUI_FULL_SCREEN for tui_mode in tui.toml to take effect.'
: 'This terminal automatically falls back to regular mode unless KIMI_CODE_TUI_FULL_SCREEN=1 is set.';
}
return resolution.overriddenByEnv
? 'Saved to tui.toml; unset KIMI_CODE_TUI_FULL_SCREEN for it to take effect.'
: 'Saved to tui.toml; this terminal automatically falls back to regular mode unless KIMI_CODE_TUI_FULL_SCREEN=1 is set.';
}

export async function applyTuiModeChoice(host: TuiModeHost, tuiMode: TuiMode): Promise<void> {
const resolved = resolveTuiMode(tuiMode);
if (tuiMode === host.state.appState.tuiMode) {
host.showStatus(`TUI mode already ${tuiMode}.`);
} else {
const configChanged = tuiMode !== host.state.appState.tuiMode;
if (configChanged) {
try {
await saveTuiConfig({
...currentTuiConfig(host as unknown as SlashCommandHost),
Expand All @@ -1210,18 +1233,38 @@ export async function applyTuiModeChoice(host: TuiModeHost, tuiMode: TuiMode): P

host.setAppState({ tuiMode });
host.track?.('tui_mode_switch', { tui_mode: tuiMode });
host.showStatus(`TUI mode set to ${tuiMode}.`, 'success');
}
if (resolved.mode !== tuiMode) {
host.showNotice(
resolved.overriddenByEnv
? 'Saved to tui.toml; unset KIMI_CODE_TUI_FULL_SCREEN for it to take effect.'
: 'Saved to tui.toml; this terminal automatically falls back to regular mode unless KIMI_CODE_TUI_FULL_SCREEN=1 is set.',
);

const reportConfigStatus = (): void => {
if (configChanged) {
host.showStatus(`TUI mode set to ${tuiMode}.`, 'success');
} else {
host.showStatus(`TUI mode already ${tuiMode}.`);
}
};

const overrideNotice = tuiModeOverrideNotice(resolved, tuiMode);
if (overrideNotice !== undefined) {
reportConfigStatus();
host.showNotice(overrideNotice);
return;
}
if (resolved.mode !== host.state.ui.mode) {
host.showNotice(TUI_MODE_RESTART_NOTICE);

const switchResult = host.switchTuiMode(resolved.mode);
switch (switchResult.status) {
case 'switched':
host.showStatus(
configChanged ? `TUI mode set to ${tuiMode}.` : `Switched TUI mode to ${tuiMode}.`,
'success',
);
return;
case 'unchanged':
reportConfigStatus();
return;
case 'blocked':
reportConfigStatus();
host.showNotice(TUI_MODE_SWITCH_BLOCKED_NOTICE);
return;
}
}

Expand Down
4 changes: 3 additions & 1 deletion apps/kimi-code/src/tui/commands/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import type { ModelMigrationController } from '../controllers/model-migration';
import type { StreamingUIController } from '../controllers/streaming-ui';
import type { TasksBrowserController } from '../controllers/tasks-browser';
import { tryHandleDanceCommand } from '../easter-eggs/dance';
import type { FollowUpDelivery } from '../config';
import type { FollowUpDelivery, TuiMode } from '../config';
import type { ResolvedTheme } from '../theme/colors';
import type { TuiModeSwitchResult } from '../tui-renderer';
import type { TUIState } from '../tui-state';
import type {
AppState,
Expand Down Expand Up @@ -164,6 +165,7 @@ export interface SlashCommandHost {

applyTheme(theme: ThemeName, resolved?: ResolvedTheme): Promise<void>;
refreshTerminalThemeTracking(): void;
switchTuiMode(mode: TuiMode): TuiModeSwitchResult;

stop(exitCode?: number): Promise<void>;
setExitOpenUrl(url: string): void;
Expand Down
27 changes: 20 additions & 7 deletions apps/kimi-code/src/tui/commands/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { TuiAltScreen } from '@moonshot-ai/pi-tui';

import { currentTheme, lightColors } from '#/tui/theme';
import { DEFAULT_MARKDOWN_CONFIG, loadTuiConfig, resolveTuiMode, type TuiConfig } from '../config';
import { TUI_MODE_RESTART_NOTICE } from '../constant/kimi-tui';
import { TUI_MODE_SWITCH_BLOCKED_NOTICE } from '../constant/kimi-tui';
import { setMarkdownMermaidMode, setMarkdownRenderLatex } from '../utils/markdown-options';
import type { SlashCommandHost } from './dispatch';
import { applyTranscriptPresentation } from './config';
import { applyTranscriptPresentation, tuiModeOverrideNotice } from './config';
import { setExperimentalFeatures } from './experimental-flags';

export async function handleReloadTuiCommand(host: SlashCommandHost): Promise<void> {
Expand Down Expand Up @@ -79,13 +79,26 @@ export async function applyReloadedTuiConfig(
markdown: config.markdown,
transcript: config.transcript,
});
applyTranscriptPresentation(host);
host.state.editor.setDisablePasteBurst(config.disablePasteBurst);
if (host.state.ui instanceof TuiAltScreen) {
host.state.ui.setCopyOnSelect(config.copyOnSelect !== false);
const applyNonSwitchedUpdates = (): void => {
applyTranscriptPresentation(host);
if (host.state.ui instanceof TuiAltScreen) {
host.state.ui.setCopyOnSelect(config.copyOnSelect !== false);
}
};
const resolution = resolveTuiMode(config.tuiMode);
const overrideNotice = tuiModeOverrideNotice(resolution, config.tuiMode, 'reload');
if (overrideNotice !== undefined) {
applyNonSwitchedUpdates();
host.showNotice(overrideNotice);
return;
}
if (resolveTuiMode(config.tuiMode).mode !== host.state.ui.mode) {
host.showNotice(TUI_MODE_RESTART_NOTICE);
const switchResult = host.switchTuiMode(resolution.mode);
if (switchResult.status !== 'switched') {
applyNonSwitchedUpdates();
}
if (switchResult.status === 'blocked') {
host.showNotice(TUI_MODE_SWITCH_BLOCKED_NOTICE);
}
}

Expand Down
21 changes: 19 additions & 2 deletions apps/kimi-code/src/tui/components/chrome/dock-section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,26 @@ function mouseTarget(component: Component, event: TuiMouseEvent): TuiMouseDispat
}

export abstract class DockSection extends VStack {
private readonly bodyScroll: ScrollView | undefined;
private currentLayout: DockLayout | undefined;
private bodyScroll: ScrollView | undefined;

constructor(readonly layout?: DockLayout) {
constructor(layout?: DockLayout) {
super();
this.mountLayout(layout);
}

get layout(): DockLayout | undefined {
return this.currentLayout;
}

setLayout(layout?: DockLayout): void {
this.mountLayout(layout);
this.invalidate();
}

private mountLayout(layout?: DockLayout): void {
this.currentLayout = layout;
this.clear();
const view = (part: DockPart): Component => {
const component: Component = {
render: (width) => this.renderPart(part, width),
Expand All @@ -63,6 +79,7 @@ export abstract class DockSection extends VStack {
return component;
};
if (layout === undefined) {
this.bodyScroll = undefined;
this.addChild(view('head'));
this.addChild(view('body'));
this.addChild(view('foot'));
Expand Down
8 changes: 7 additions & 1 deletion apps/kimi-code/src/tui/components/chrome/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export interface FooterGitBinding {
export class FooterComponent implements Component {
private state: AppState;
private readonly onRefresh: () => void;
private readonly runningFullscreen: boolean;
private runningFullscreen: boolean;
private gitBinding: ((state: AppState) => FooterGitBinding | null) | null = null;
private gitCache: GitStatusCache | null = null;
private gitCacheKey: string | null = null;
Expand Down Expand Up @@ -217,6 +217,12 @@ export class FooterComponent implements Component {
this.state = state;
}

setRunningFullscreen(runningFullscreen: boolean): void {
if (this.runningFullscreen === runningFullscreen) return;
this.runningFullscreen = runningFullscreen;
this.invalidate();
}

setGitBinding(resolve: (state: AppState) => FooterGitBinding | null): void {
this.gitBinding = resolve;
this.syncGitCache(this.state);
Expand Down
7 changes: 6 additions & 1 deletion apps/kimi-code/src/tui/components/chrome/notify-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class NotifyPanelComponent extends DockSection implements Focusable {
private ended = false;
private keptOpen = false;
private promptPending = false;
private readonly screenRows: (() => number) | undefined;
private screenRows: (() => number) | undefined;
private hitTargets: readonly HitTarget[] = [];
onReturnFocus: ((key: string | undefined) => void) | undefined;

Expand All @@ -86,6 +86,11 @@ export class NotifyPanelComponent extends DockSection implements Focusable {
this.screenRows = layout?.screenRows;
}

override setLayout(layout?: NotifyPanelLayout): void {
this.screenRows = layout?.screenRows;
super.setLayout(layout);
}

upsert(entry: NotifyEntry): void {
const ch = this.channelFor(entry);
const following =
Expand Down
3 changes: 2 additions & 1 deletion apps/kimi-code/src/tui/constant/kimi-tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const CTRL_C_HINT = 'Press Ctrl+C again to exit';
export const SHUTTING_DOWN_HINT = 'Shutting down… (running shutdown hooks)';
export const MAIN_AGENT_ID = 'main';
export const OAUTH_LOGIN_REQUIRED_STARTUP_NOTICE = 'OAuth login expired. Send /login to login.';
export const TUI_MODE_RESTART_NOTICE = 'TUI mode takes effect after restarting Kimi Code.';
export const TUI_MODE_SWITCH_BLOCKED_NOTICE =
'Close the active view and run /reload-tui to switch TUI mode, or restart Kimi Code.';
export const SESSIONLESS_STARTUP_NOTICE =
'No session yet — one will be created on your first message.';
export const TOWER_TEARDOWN_PROMPT =
Expand Down
20 changes: 15 additions & 5 deletions apps/kimi-code/src/tui/controllers/editor-keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,9 @@ export class EditorKeyboardController {
return true;
};

const { ui } = host.state;
if (ui instanceof TuiAltScreen) {
this.disposeSelectionCopy = ui.addInputListener((data) => this.handleSelectionCopyKey(ui, data));
}
this.disposeSelectionCopy = host.state.ui.addInputListener((data) =>
this.handleSelectionCopyKey(data),
);

editor.onUndo = () => {
host.track('undo');
Expand Down Expand Up @@ -418,11 +417,22 @@ export class EditorKeyboardController {
dispose(): void {
this.disposed = true;
this.disposeSelectionCopy?.();
this.disposeSelectionCopy = undefined;
this.copyKeyHeld = false;
this.clearPendingExit();
this.clearPendingUndoEsc();
}

private handleSelectionCopyKey(ui: TuiAltScreen, data: string): { consume: true } | undefined {
resetSelectionCopyState(): void {
this.copyKeyHeld = false;
}

private handleSelectionCopyKey(data: string): { consume: true } | undefined {
const { ui } = this.host.state;
if (!(ui instanceof TuiAltScreen)) {
this.copyKeyHeld = false;
return undefined;
}
const key = normalizeCapsLockedCtrl(data);
if (!SELECTION_COPY_KEYS.some((copyKey) => matchesKey(key, copyKey))) return undefined;
if (isKeyRelease(key)) {
Expand Down
Loading
Loading