import { Editor, type KeyId, matchesKey, parseKittySequence } from "@oh-my-pi/pi-tui";
import type { AppKeybinding } from "../../config/keybindings";
import { highlightMagicKeywords } from "../magic-keywords";
type ConfigurableEditorAction = Extract<
AppKeybinding,
| "app.interrupt"
| "app.clear"
| "app.exit"
| "app.suspend"
| "app.thinking.cycle"
| "app.model.cycleForward"
| "app.model.cycleBackward"
| "app.model.select"
| "app.model.selectTemporary"
| "app.tools.expand"
| "app.thinking.toggle"
| "app.editor.external"
| "app.history.search"
| "app.message.dequeue"
| "app.clipboard.pasteImage"
| "app.clipboard.copyPrompt"
>;
const DEFAULT_ACTION_KEYS: Record<ConfigurableEditorAction, KeyId[]> = {
"app.interrupt": ["escape"],
"app.clear": ["ctrl+c"],
"app.exit": ["ctrl+d"],
"app.suspend": ["ctrl+z"],
"app.thinking.cycle": ["shift+tab"],
"app.model.cycleForward": ["ctrl+p"],
"app.model.cycleBackward": ["shift+ctrl+p"],
"app.model.select": ["ctrl+l"],
"app.model.selectTemporary": ["alt+p"],
"app.tools.expand": ["ctrl+o"],
"app.thinking.toggle": ["ctrl+t"],
"app.editor.external": ["ctrl+g"],
"app.history.search": ["ctrl+r"],
"app.message.dequeue": ["alt+up"],
"app.clipboard.pasteImage": ["ctrl+v"],
"app.clipboard.copyPrompt": ["alt+shift+c"],
};
* Custom editor that handles configurable app-level shortcuts for coding-agent.
*/
export class CustomEditor extends Editor {
* them, skipping any occurrence inside code spans, fenced blocks, or XML sections. */
decorateText = (text: string): string => highlightMagicKeywords(text);
onEscape?: () => void;
shouldBypassAutocompleteOnEscape?: () => boolean;
onClear?: () => void;
onExit?: () => void;
onCycleThinkingLevel?: () => void;
onCycleModelForward?: () => void;
onCycleModelBackward?: () => void;
onSelectModel?: () => void;
onExpandTools?: () => void;
onToggleThinking?: () => void;
onExternalEditor?: () => void;
onHistorySearch?: () => void;
onSuspend?: () => void;
onSelectModelTemporary?: () => void;
onCopyPrompt?: () => void;
onPasteImage?: () => Promise<boolean>;
onDequeue?: () => void;
onCapsLock?: () => void;
#customKeyHandlers = new Map<KeyId, () => void>();
#actionKeys = new Map<ConfigurableEditorAction, KeyId[]>(
Object.entries(DEFAULT_ACTION_KEYS).map(([action, keys]) => [action as ConfigurableEditorAction, [...keys]]),
);
setActionKeys(action: ConfigurableEditorAction, keys: KeyId[]): void {
this.#actionKeys.set(action, [...keys]);
}
#matchesAction(data: string, action: ConfigurableEditorAction): boolean {
const keys = this.#actionKeys.get(action);
if (!keys) return false;
for (const key of keys) {
if (matchesKey(data, key)) return true;
}
return false;
}
* Register a custom key handler. Extensions use this for shortcuts.
*/
setCustomKeyHandler(key: KeyId, handler: () => void): void {
this.#customKeyHandlers.set(key, handler);
}
* Remove a custom key handler.
*/
removeCustomKeyHandler(key: KeyId): void {
this.#customKeyHandlers.delete(key);
}
* Clear all custom key handlers.
*/
clearCustomKeyHandlers(): void {
this.#customKeyHandlers.clear();
}
handleInput(data: string): void {
const parsed = parseKittySequence(data);
if (parsed && (parsed.modifier & 64) !== 0 && this.onCapsLock) {
this.onCapsLock();
return;
}
if (this.#matchesAction(data, "app.clipboard.pasteImage") && this.onPasteImage) {
void this.onPasteImage();
return;
}
if (this.#matchesAction(data, "app.editor.external") && this.onExternalEditor) {
this.onExternalEditor();
return;
}
if (this.#matchesAction(data, "app.model.selectTemporary") && this.onSelectModelTemporary) {
this.onSelectModelTemporary();
return;
}
if (this.#matchesAction(data, "app.suspend") && this.onSuspend) {
this.onSuspend();
return;
}
if (this.#matchesAction(data, "app.thinking.toggle") && this.onToggleThinking) {
this.onToggleThinking();
return;
}
if (this.#matchesAction(data, "app.model.select") && this.onSelectModel) {
this.onSelectModel();
return;
}
if (this.#matchesAction(data, "app.history.search") && this.onHistorySearch) {
this.onHistorySearch();
return;
}
if (this.#matchesAction(data, "app.tools.expand") && this.onExpandTools) {
this.onExpandTools();
return;
}
if (this.#matchesAction(data, "app.model.cycleBackward") && this.onCycleModelBackward) {
this.onCycleModelBackward();
return;
}
if (this.#matchesAction(data, "app.model.cycleForward") && this.onCycleModelForward) {
this.onCycleModelForward();
return;
}
if (this.#matchesAction(data, "app.thinking.cycle") && this.onCycleThinkingLevel) {
this.onCycleThinkingLevel();
return;
}
if (this.#matchesAction(data, "app.interrupt") && this.onEscape) {
if (!this.isShowingAutocomplete() || this.shouldBypassAutocompleteOnEscape?.()) {
this.onEscape();
return;
}
}
if (this.#matchesAction(data, "app.clear") && this.onClear) {
this.onClear();
return;
}
if (this.#matchesAction(data, "app.exit")) {
this.onExit?.();
return;
}
if (this.#matchesAction(data, "app.message.dequeue") && this.onDequeue) {
this.onDequeue();
return;
}
if (this.#matchesAction(data, "app.clipboard.copyPrompt") && this.onCopyPrompt) {
this.onCopyPrompt();
return;
}
for (const [keyId, handler] of this.#customKeyHandlers) {
if (matchesKey(data, keyId)) {
handler();
return;
}
}
super.handleInput(data);
}
}