kimi-code/docs/zh/guides/interaction.md
liruifengv 2db5fc20ec
feat: add shell mode (!) to the CLI (#1079)
* feat: add shell mode (`!`) to the CLI

Add shell mode, letting users run shell commands directly from the prompt
with `!`. Output streams live into the transcript, supports backgrounding
(ctrl+b), cancellation (Esc / Ctrl+C), input queuing while running, and
enters the conversation context with resume support.

* feat(kimi-code): show shell mode label on editor border and add tip

Render a "! shell mode" label on the top-left of the editor border while the editor is in `!` bash mode, so the active mode is visible at a glance. Also add a rotating toolbar tip (`! to run a shell command`) to surface the feature.

* feat(kimi-code): refine shell mode queue, history, and display

- Keep `!` commands out of input history so they never resurface as bare text stripped of their `!`.

- Make `!` commands non-steerable: Ctrl-S skips them (they stay queued to run after the current task) and the steer hint is only shown when something is actually steerable.

- Render queued `!` commands with a `$` prompt and the shell-mode hue so they read as commands, not as text to send to the model.

- Echo executed shell commands with a `$` prompt instead of `!`.

* fix(kimi-code): sanitize shell output and harden rendering

Captured shell command output can contain terminal control sequences (colours, cursor moves, alternate-screen switches, OSC hyperlinks, carriage-return spinners, bells). pi-tui's Text passes strings straight to the terminal, so any unhandled sequence was executed by the terminal and fought with pi-tui's own cursor control, producing the blank-screen-plus-leftover-characters mess after running commands like pnpm dev or a nested TUI.

- Sanitize CSI (incl. private modes), OSC, single-char ESC and C0 control chars (keeping newline and tab) in both the finished/resume view (previously unsanitized) and the running tail.

- Make the sanitize, format, and ShellRunComponent render paths never-throw, and cap the live running buffer, so a misbehaving command cannot crash the TUI.

- Dispose transcript children on clear so ShellRunComponent's timer is released on /clear or session switch.

* fix(kimi-code): render shell command echo with $ instead of sparkles

The shell command echo is a 'user' transcript entry, so UserMessageComponent prefixed it with the USER_MESSAGE_BULLET (sparkles), producing 'sparkles $ command'.

Add an optional bullet override to UserMessageComponent / TranscriptEntry and set it to an empty string for the shell echo (both live and resume), so the '$ command' content sits at the leading column where the sparkles marker used to be. Normal user messages keep the sparkles bullet.

* fix(kimi-code): enter shell mode when pasting a !-prefixed command

The bash-mode trigger only handled the single ! keystroke, so a pasted !cmd was inserted as literal text in prompt mode and submitted as a normal message.

After pi-tui inserts pasted content, detect an empty-prompt buffer that now starts with !, switch to bash mode, and strip the leading ! so the buffer holds only the command, matching the typed ! path.

* fix(kimi-code): restore shell mode when recalling a queued command

recallLastQueued() dropped the queued item's mode, and the Up-arrow recall only restored the text. A queued ! command (queued while another command runs, which resets the editor to prompt mode) therefore came back as a normal prompt and was submitted as a message instead of a shell command.

Return the full QueuedMessage from recallLastQueued() and restore editor.inputMode (plus the onInputModeChange sync) from the recalled item's mode.

* feat(kimi-code): use violet as the shell mode color

Replace the claude-code-style magenta/rose shellMode token with a violet that is distinct from plan-mode blue, the user role amber, success green, error red, and the teal accent.

Custom themes that omit the token fall back to this new default via the base+overrides merge, so existing custom themes keep working unchanged.

* chore: refine the shell mode changeset

* docs: document shell mode

Add a Shell mode section to the interaction guide and list the ! and Ctrl+B shortcuts in the keyboard reference, in both English and Chinese.

* test(protocol): include shell events in volatile classification check

shell.output and shell.started were added as volatile event types for shell mode; update the snapshot test's volatile-type list and count accordingly.

* fix(agent-core): surface shell command failure reason with no output

When a ! shell command fails without producing stdout/stderr (non-zero exit with no output, timeout, spawn failure), the failure reason lived only in the tool result's output and the TUI showed '(no output)'. Fold it into stderr so the live view and replay show what went wrong.

* fix(kimi-code): decode CSI-u ! to enter shell mode

In terminals with the Kitty keyboard protocol (VSCode integrated terminal, Kitty), pressing ! arrives as a CSI-u sequence, so the raw normalized === '!' comparison never matched and shell mode could not be entered by typing !. Decode with printableChar before comparing, matching every other printable-key check in the TUI.

* fix(kimi-code): do not steer while a shell command is running

Ctrl-S steers queued input into the running turn, but a shell command is not an agent turn, so steering during streamingPhase === 'shell' would launch a turn before the command output is recorded. Keep Ctrl-S a no-op during shell runs; queued messages stay queued.

* fix(agent-core): escape bash tag delimiters in shell output

Shell command output is arbitrary text; if it contains a bash tag delimiter such as </bash-stdout>, the recorded pseudo-XML wrapper breaks and replay extracts the wrong slice. Escape the content when wrapping it in agent-core and unescape when extracting during replay, so output survives round-trip intact.

* docs: document the shellMode theme token

The shellMode color token was added to the palette but not propagated to its mirrors. Add it to the custom-theme docs token table, the theme JSON schema, and the custom-theme skill token list.

* feat(agent-core): reset background task deadline on detach

Add a resettable deadline timer to BackgroundManager and let tasks register a detach timeout; when a foreground task is moved to the background, its deadline resets to the background default counted from the detach moment.

Wire this into shell mode so ! commands run with a 3-minute foreground timeout and get 10 minutes once detached to the background, instead of staying bounded by the original 60-second foreground deadline.

* feat(agent-core): lower shell mode foreground timeout to 2 minutes
2026-06-25 21:24:53 +08:00

5.8 KiB
Raw Permalink Blame History

交互与输入

Kimi Code CLI 以交互式 TUI 运行,核心由输入框、对话视图和状态栏三部分组成。本页介绍输入方式、媒体粘贴、审批流程和模式切换。

输入框基本操作

输入框接受自由文本:Enter 发送,Shift-EnterCtrl-J 插入换行。输入框为空时按 / 浏览当前工作目录的历史输入。

退出 CLI:输入框为空时按 Ctrl-D,或空闲状态下连按 Ctrl-C 两次,或输入 /exit。流式输出期间按 Ctrl-CEsc 是中断当前轮次,不会退出程序。

粘贴图片与视频

Kimi Code CLI 支持在输入框中直接粘贴图片和视频,让 AI 结合视觉内容理解你的问题——截图报错、UI 设计图、架构图,直接粘贴进去就能讨论,无需上传或转存。

视频输入是 Kimi Code 的特色能力,支持直接粘贴视频片段让模型分析其中的内容、界面流程或代码演示。

操作方式:

  • macOS / LinuxCtrl-V
  • WindowsAlt-V

粘贴后输入框显示占位符,可像普通文本一样编辑;提交时自动替换为实际内容。纯文本剪贴板会回退到普通粘贴。媒体功能是否可用取决于当前模型的多模态能力(image_in / video_in),登录 Kimi Code 账号后默认开启。

斜杠命令

/ 开头的内容会被识别为斜杠命令。输入 / 后弹出补全菜单,随后续字符实时过滤;按 Esc 关闭菜单,匹配失败时内容会作为普通消息发送给 Agent。

已激活的 Agent Skills 会自动注册为斜杠命令:普通外部 Skill 以 /skill:<name> 调用,外部子 Skill 以 /parent.child 这样的点分命令显示,内置 Skill 直接以 /<name> 出现在斜杠命令面板中;若外部 Skill 名称与系统斜杠命令不冲突,也可以省略 skill: 前缀直接输入 /<name>

部分命令仅在 Agent 空闲时可用,流式输出或上下文压缩期间需先按 Esc 中断。/yolo/plan/help/btw 等模式切换和查询类命令则始终可用。全部命令说明见斜杠命令参考

文件引用

键入 @ 触发文件路径补全选中后在输入中插入相对路径Agent 读取时会直接加载该文件内容。文件引用在 git 和非 git 目录都可用;文件夹候选会以 / 结尾方便继续补全其下路径。如果快速搜索辅助工具仍在下载Kimi Code 会先回退到基础的文件系统扫描。隐藏路径也可补全,但 .git 会从候选中排除。

@ 引用和斜杠命令是两套不同的机制:@ 向 Agent 提供文件上下文,/ 调用内置功能或 Skill。前面有空白字符时输入 / 会按普通文本处理,不会打开斜杠命令菜单。

审批流程

Agent 调用会产生副作用的工具修改文件、执行命令等TUI 会弹出审批面板让你确认。YOLO 模式以及 Plan 模式下对计划文件的写入不触发审批。

用方向键选择选项,Enter 确认;也可以按 1/2/3 数字键直接选择。EscCtrl-CCtrl-D 等同于拒绝。

面板中通常有「Approve for this session」选项选择后本次会话内的同类调用将自动放行。如需永久规则配置文件里预置 allow / deny 规则即可。

模式切换

Plan 模式

Plan 模式下Agent 先输出行动计划,等待你确认后才动手修改文件,适合复杂或高风险任务。

  • 切换:Shift-Tab/plan
  • 清除当前计划:/plan clear(仅空闲时)

Agent 输出方案后会等待你审批——可批准执行、拒绝、或要求修改。退出 Plan 模式始终需要你确认,即使开启了 YOLO 模式也不例外。

YOLO / Auto 模式

YOLO 模式/yolo跳过几乎所有工具调用的审批确认适合已知安全的批处理任务Plan 模式的退出审批除外。

Auto 模式/auto)则更克制:工具审批自动处理,但 Agent 不会向用户提问,适合需要无人值守但又不想完全关闭审批的场景。

::: warning 注意 YOLO 模式会跳过文件写入和命令执行的确认,请只在受信任的工作目录下使用。 :::

Shell 模式

Shell 模式让你不离开对话就能运行终端命令命令输出会写入对话上下文AI 在后续轮次能够看到这些结果。

  • 进入:在空输入框中键入 !,或粘贴以 ! 开头的命令。
  • 退出:在空输入框中按 BackspaceEsc;提交命令后也会自动回到普通模式。
  • 后台运行:命令执行期间按 Ctrl+B 可将其转为后台任务。

进入 Shell 模式后,输入框左侧会显示 ! 提示符,边框变为紫色。例如,无需新开终端就能运行 !gh auth login 登录 GitHub CLI登录后 Kimi 就可以直接使用 gh

流式输出期间

Agent 思考或调用工具时,输入框仍然可用,支持以下额外操作:

  • Ctrl-S:把输入框中的内容立即注入正在运行的轮次,无需等待结束
  • Esc / Ctrl-C:中断当前轮次
  • Ctrl-O:全局切换工具输出的折叠状态

外部编辑器

Ctrl-G 把当前输入内容发给外部编辑器,保存后回填到输入框,不保存则保持原样。适合需要输入大段文本或带格式内容的场景。

编辑器优先级:/editor 配置 > $VISUAL 环境变量 > $EDITOR 环境变量。未配置时可先运行 /editor 选择默认编辑器。

下一步