JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(tui): Fixed an issue with the keybindings feature
kdgmpwf2591c9c21b1
diff --git a/docs/en/SlashCommands.md b/docs/en/SlashCommands.mdindex a11e234a8a..e0f9a871cb 100644--- a/docs/en/SlashCommands.md+++ b/docs/en/SlashCommands.md@@ -639,6 +639,10 @@ Alias: `/keybind`.- Built-in defaults are merged with user JSON per **context**; set a key to `null` to unbind a default.- Key ids must match pi-tui `matchesKey` format (`ctrl`/`shift`/`alt` + main key); chords are not supported.+- **Unsupported keys**:+ - **`win` / `cmd` / `super` / `meta`**: These modifiers require the Kitty keyboard protocol, which is not available in standard terminals (Windows CMD, VS Code integrated terminal, etc.). Bindings using these will never fire.+ - **`ctrl+shift+letter`**: On legacy terminals like Windows CMD, `ctrl+shift+l` and `ctrl+l` produce the same byte — the terminal cannot distinguish them. This combination is not recommended. Use Windows Terminal, WezTerm, or another VT-mode capable terminal if you need these combos.+ - **Chords** (space-separated multi-key sequences, e.g. `"ctrl+x ctrl+k"`): Not supported in the current version.- **Non-rebindable**: `ctrl+c`, `ctrl+d`, `ctrl+m` (reserved keys).- Select-list navigation, config editor text input, Resume preview/rename sub-states, etc. remain hardcoded.diff --git "a/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md" "b/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"index 38d47b0959..b7e86f60a8 100644--- "a/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"+++ "b/docs/zh/TUI\344\275\277\347\224\250\346\214\207\345\215\227.md"@@ -545,6 +545,11 @@键名格式需符合 pi-tui 的 `matchesKey` 约定:小写修饰键 `ctrl` / `shift` / `alt`,特殊键如 `pageUp`、`escape`、`return` 等;不支持 chord(空格分隔的多段按键)。+**不支持的按键**:+- **`win` / `cmd` / `super` / `meta`**:这些修饰键依赖 Kitty 键盘协议,普通终端(Windows CMD、VS Code 内置终端等)无法发送,快捷键不会生效。+- **`ctrl+shift+字母`**:在 Windows CMD 等传统终端中,`ctrl+shift+l` 与 `ctrl+l` 发送的字节码相同,终端层面无法区分,不建议使用。如需使用此类组合建议换用 Windows Terminal、WezTerm 等支持 VT 模式的终端。+- **chord 组合键**(空格分隔,如 `"ctrl+x ctrl+k"`):当前版本不支持。+#### 可配置的 Context 与 Action| Context | 生效场景 | 主要默认键 |diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/keybindings.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/keybindings.tsindex 5b5c584dea..1c1424ab63 100644--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/keybindings.ts+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/keybindings.ts@@ -78,16 +78,26 @@ function listBindings(ctx: CommandContext): void {function resetBindings(ctx: CommandContext): void {const path = getKeybindingsPath();+ let deleted = false;if (existsSync(path)) {try {rmSync(path);+ deleted = true;} catch (err) {ctx.addItem(addError(ctx.sessionId, `删除 keybindings.json 失败:${(err as Error).message}`));return;}}applyAndReport(ctx);- ctx.addItem(addInfo(ctx.sessionId, "已重置为默认快捷键", "k"));+ ctx.addItem(+ addInfo(+ ctx.sessionId,+ deleted+ ? `已删除 ${path},恢复为默认快捷键`+ : "已是默认快捷键,无需重置",+ "k",+ ),+ );}export function createKeybindingsCommand(): SlashCommand {diff --git a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.tsindex 111dc3d80d..296bf815d1 100644--- a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts+++ b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/resolver.ts@@ -1,7 +1,7 @@import { type KeyId, matchesKey } from "@mariozechner/pi-tui";import type { KeybindingAction, KeybindingContextName } from "./actions.js";-import { loadKeybindings } from "./store.js";+import { loadKeybindings, startKeybindingsWatcher } from "./store.js";import type { KeybindingWarning, ResolvedBindings } from "./types.js";// Initialized at module load from defaults + user file. /keybindings triggers@@ -15,6 +15,10 @@ export function reloadResolver(): KeybindingWarning[] {return result.warnings;}+// Start watching keybindings.json for external changes (edit / delete / create).+// Automatically reloads to defaults when the file is deleted at runtime.+startKeybindingsWatcher(() => reloadResolver());+/*** Resolve a terminal input string to an action within a context.* Returns null if no binding in this context matches.diff --git a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.tsindex 229746c5d6..e56d9b038b 100644--- a/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts+++ b/jiuwenswarm/channels/tui/frontend/src/core/keybindings/store.ts@@ -1,6 +1,7 @@import { existsSync, readFileSync } from "node:fs";+import { watch } from "node:fs";import { homedir } from "node:os";-import { join } from "node:path";+import { dirname, join } from "node:path";import {isKeybindingAction,@@ -236,3 +237,47 @@ export function loadKeybindings(): LoadResult {return { resolved, warnings, userFileLoaded: true };}++// ---------------------------------------------------------------------------+// File watcher — detect external changes (edit / delete / create) to+// keybindings.json at runtime and reload immediately, mirroring Claude Code.+// ---------------------------------------------------------------------------++let _watcher: ReturnType<typeof watch> | null = null;++/**+ * Start watching the keybindings.json file for external changes.+ *+ * Calls `onReload` whenever the file is created, changed, or deleted so the+ * caller can reload the resolver and reflect the change immediately.+ *+ * Uses a directory watch + basename filter because `fs.watch` on individual+ * files is unreliable on some platforms (especially Windows).+ */+export function startKeybindingsWatcher(onReload: () => void): void {+ if (_watcher) return;++ const filePath = KEYBINDINGS_FILE;+ const dirPath = dirname(filePath);+ const fileName = join(filePath).split(/[/\\]/).pop() ?? "keybindings.json";++ try {+ _watcher = watch(dirPath, { persistent: false }, (_eventType, filename) => {+ // Ignore unrelated files in the same directory.+ if (filename !== fileName) return;+ onReload();+ });+ } catch {+ // Directory doesn't exist yet — nothing to watch.+ }+}++/**+ * Stop the file watcher. Idempotent.+ */+export function stopKeybindingsWatcher(): void {+ if (_watcher) {+ _watcher.close();+ _watcher = null;+ }+}diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.tsindex 9f2af538a5..e5f62a0486 100644--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts@@ -1973,64 +1973,71 @@ export class AppScreen implements Component, Focusable {return;}- const handled = handleAppScreenKeyInput(data, {- interruptTask: () => this.interruptTask(),- exitApp: () => this.exit(),- toggleTodos: () => {- this.todosCollapsed = !this.todosCollapsed;- this.tui.requestRender();- },- toggleTeamPanel: () => {- this.showTeamPanel = !this.showTeamPanel;- if (!this.showTeamPanel) {- this.viewedTeamMemberId = null;- }- this.tui.requestRender();- },- toggleTranscript: () => {- const snapshot = this.state.getSnapshot();- this.state.setTranscriptMode(- snapshot.transcriptMode === "detailed" ? "compact" : "detailed",- );- },- redraw: () => {- this.tui.invalidate();- this.tui.requestRender(true);- this.transientNotice = "Screen redrawn";- if (this.transientNoticeTimer) {- clearTimeout(this.transientNoticeTimer);- }- this.transientNoticeTimer = setTimeout(() => {- this.transientNotice = null;- this.transientNoticeTimer = null;+ // Global ctrl+l/t/g/o only apply on the main screen — defer while an overlay+ // or the team panel is active so context-specific bindings (e.g. ResumeList)+ // can use the same physical keys.+ const skipGlobalMainScreenKeys = hasOverlay || this.showTeamPanel;+ let handled = false;+ if (!skipGlobalMainScreenKeys) {+ handled = handleAppScreenKeyInput(data, {+ interruptTask: () => this.interruptTask(),+ exitApp: () => this.exit(),+ toggleTodos: () => {+ this.todosCollapsed = !this.todosCollapsed;this.tui.requestRender();- }, 1200);- this.tui.requestRender();- },- clearInput: () => {- this.editor.setText("");- this.tui.requestRender();- },- isIdle: () => {- return !snapshot.isProcessing && !snapshot.pendingQuestion && !snapshot.cancellableWork;- },- hasServerTask: () => this.state.hasServerTask(),- requestLocalInterrupt: () => {- return this.state.requestLocalInterrupt();- },- showCtrlCExitHint: () => {- if (this.transientNoticeTimer) {- clearTimeout(this.transientNoticeTimer);- }- this.transientNotice = "Press Ctrl+C again to exit";- this.transientNoticeTimer = setTimeout(() => {- this.transientNotice = null;- this.transientNoticeTimer = null;+ },+ toggleTeamPanel: () => {+ this.showTeamPanel = !this.showTeamPanel;+ if (!this.showTeamPanel) {+ this.viewedTeamMemberId = null;+ }this.tui.requestRender();- }, 3000);- this.tui.requestRender();- },- });+ },+ toggleTranscript: () => {+ const snapshot = this.state.getSnapshot();+ this.state.setTranscriptMode(+ snapshot.transcriptMode === "detailed" ? "compact" : "detailed",+ );+ },+ redraw: () => {+ this.tui.invalidate();+ this.tui.requestRender(true);+ this.transientNotice = "Screen redrawn";+ if (this.transientNoticeTimer) {+ clearTimeout(this.transientNoticeTimer);+ }+ this.transientNoticeTimer = setTimeout(() => {+ this.transientNotice = null;+ this.transientNoticeTimer = null;+ this.tui.requestRender();+ }, 1200);+ this.tui.requestRender();+ },+ clearInput: () => {+ this.editor.setText("");+ this.tui.requestRender();+ },+ isIdle: () => {+ return !snapshot.isProcessing && !snapshot.pendingQuestion && !snapshot.cancellableWork;+ },+ hasServerTask: () => this.state.hasServerTask(),+ requestLocalInterrupt: () => {+ return this.state.requestLocalInterrupt();+ },+ showCtrlCExitHint: () => {+ if (this.transientNoticeTimer) {+ clearTimeout(this.transientNoticeTimer);+ }+ this.transientNotice = "Press Ctrl+C again to exit";+ this.transientNoticeTimer = setTimeout(() => {+ this.transientNotice = null;+ this.transientNoticeTimer = null;+ this.tui.requestRender();+ }, 3000);+ this.tui.requestRender();+ },+ });+ }if (handled) {return;}