kimi-code/docs/zh/reference/kimi-command.md
Kai 4a24ae1238
feat: import Claude Code and Codex sessions (#4342)
* feat: add standalone session import conversion package

* fix(session-import): address rollout and image review findings

- Extend a rollback cutoff only over records that share a defined turn id.
- Keep tool-call arguments verbatim instead of rewriting image-shaped values.
- Keep media from an assistant projection that its matching response lacks.
- Count unknown event and projection kinds as unrecognized source content.

* feat(session-import): render imported history as visible messages

Follow the Codex /import format: each source turn becomes an ordinary user
message and one assistant message, tool calls and results become tagged text
notes clipped to 2,000 and 4,000 characters, and thinking, source events and
injected context stay only in the full history attachment. A final user notice
records the source session, included turns, attachment paths and known
limitations.

Prefer a user-set title over later generated ones and fall back to the first
prompt line, treat agent-name rows as metadata, stop reporting missing
reasoning text, return EMPTY_SOURCE_HISTORY for sources without a prompt, and
explain why forked or restarted Codex sessions are rejected.

* feat: import Claude Code and Codex sessions

Add /import-sessions and `kimi import-sessions`, which open the same
page: scan recent Claude Code and Codex sessions, choose the last 15
days (the default) or 30 days, import the newest 100 with progress, and
summarize what was imported and skipped. Reruns skip sessions imported
before.

Imported sessions are written into the workspace of their original
folder with their title, last activity time, and synthesized turn
records, using the migration-legacy session writer, and show a
[Claude Code] or [Codex] badge in /sessions. Discovery lives in
session-import and leaves out subagent, non-interactive, and prompt-less
sessions. Sessions still being written are reported as in use, and
fallback titles skip lines without letters or digits.

* fix: list imported sessions in /sessions right away

/import-sessions writes sessions straight to disk, which the running
session index only noticed on its next periodic reconcile, so /sessions
missed them until a restart or up to a minute later. Look each imported
session up by id after the run so the index records it immediately.

* fix(session-import): store each source record once in the full history

Records without text of their own, such as titles, mode changes, and
request dumps, were kept three times in the full history attachment: in
a header list, as the step text, and again in the step's source
metadata. Keep them only as the step text with the source line in the
metadata, and do the same for unknown content blocks and unknown Codex
response items. On local sessions the attachments shrink from 346 MB to
141 MB for 240 MB of source files.

* feat(session-import): import the conversation as one framed user message

Render the imported history as a single user message: a system note
before and after it, session info, attachment locations, known
limitations, and the included turns. Every tool call keeps the first
2,000 characters of its recorded input instead of three picked fields.
The session list shows the last real prompt from the source.

* fix(session-import): skip the Claude Code API request log

Claude Code 2.1.300 and later write API request records into the
session file. Their message copies were read as conversation records,
which duplicated tool calls and results and wrote the request log into
the full history attachment.

* fix: fit session imports to the model and skip missing folders up front

Size the imported message from the default model's input limit and keep it
below the automatic compaction threshold, so continuing an imported session
does not start with a compaction or an over-limit request. Sessions whose
project folder no longer exists are left out when scanning, so they no
longer take the newest-100 slots of every run.

* fix(session-import): keep late-written parents and clean up titles

Link a Claude Code record to its parent even when the parent was written
after it, placing the parent first, instead of cutting the history there.
Titles and last prompts skip IDE context ahead of the request and image
reference lines. Codex tool search results and three recent Claude Code
metadata records are no longer reported as unrecognized content.

* fix(session-import): find the chain end and publish without hard links

Take the last record that no other record names as its parent as the end
of a Claude Code conversation, so a parent written after its child at the
end of the file no longer hides the final prompt. On filesystems without
hard links, publish attachments with an exclusive copy instead of failing
the import.

* fix: apply model overrides before sizing session imports

Resolve the default model through its overrides, as the engine does,
before taking its input limit, so an override that lowers the context
size also lowers the import budget.

* feat(session-import): keep a copy of the source session with each import

Save the source session file in the import's attachments, minus Claude
Code's API request log, together with its Claude Code folder of subagent
transcripts and saved tool output, so history from before compaction and
abandoned branches outlive the source files. The imported message says
what the full history and the copy each hold, and clipped tool notes point
to the full history.

* feat(session-import): keep more of the source session in imports

- Copy the rollouts of the subagent threads a Codex session spawned under source/subagents/.
- Show what the user did during a turn (interruptions, slash commands, messages sent while
  the agent worked, Codex shell commands) as [user_event] notes.
- Show the work recorded after a source compaction with its summary instead of dropping it.
- Title Codex sessions with the thread name from Codex's state database.
- Point omitted media and injected source context to the attachments that keep them.

* fix(session-import): show what came before a prompt at the start of its turn

- Show work and commands recorded before a turn's prompt at the start of that turn,
  including Codex work before the first prompt, instead of dropping them without a summary.
- Wait up to 3 seconds for a Codex state database lock before falling back to the
  first prompt as the title.

* fix(session-import): stop a Codex turn start from claiming earlier turns

- When a turn starts, claim only the records written before it that can open a turn
  (user input and context), so a rollback no longer drops the previous turn's
  answer and tool work in sessions that mix records with and without turn ids.
2026-10-26 13:48:16 +08:00

21 KiB
Raw Blame History

kimi 命令

kimi 是 Kimi Code CLI 的主命令,用于在终端中启动一次交互式会话。不带任何参数运行时,它会在当前工作目录下开启一个新会话;配合不同的 flag,可以续上历史会话、跳过审批、从 Plan 模式开始,或者指定自定义的 Skills 目录。

kimi [options]
kimi <subcommand> [options]

主命令选项

所有 flag 都是可选的,直接运行 kimi 即可进入交互式会话:

选项 简写 说明
--version -V 打印版本号并退出
--help -h 显示帮助信息并退出
--session [id] -S 恢复一个会话。带 ID 时直接打开指定会话;不带 ID 时进入交互式选择器
--continue -c 继续当前工作目录下最近一次的会话,无需手动指定 ID
--model <model> -m 为本次启动指定模型别名。省略时新会话使用配置文件中的 default_model
--prompt <prompt> -p 非交互执行单次 prompt,并把 Assistant 输出流式写到 stdout。该模式不会打开 TUI
--output-format <format> 设置非交互输出格式,支持 text 与 stream-json。仅可与 --prompt 一起使用,默认 text
--yolo -y 以 "Ask When Needed" 模式启动:常规修改和命令自动完成;高危操作、提问和计划仍会问你
--auto 以 "Never Ask" 模式启动:所有操作和判断自动完成,只有 Plan 模式下的计划会等你审阅
--plan 以 Plan 模式启动新会话,AI 会优先使用只读工具进行探索和规划
--skills-dir <dir> 从指定目录加载 Skills,替换自动发现的用户和项目目录。可重复传入
--agent <name> 以指定 Agent 作为 main agent 启动新会话。不能与 --session/--continue 同时使用
--agent-file <path> 从 Markdown 文件加载自定义 Agent 并为新会话选中它。不可重复传入,也不能与 --agent、--session 或 --continue 同时使用
--add-dir <dir> 为本次会话添加额外的工作目录。相对路径按当前工作目录解析。可重复传入

-r / --resume 是 --session 的隐藏别名;--yes 和 --auto-approve 是 --yolo 的隐藏别名,在帮助信息中不显示。

::: warning 注意 --yolo 会跳过普通工具调用的人工确认,包括文件写入和 Shell 命令执行,请只在受信任的工作目录下使用。Plan 模式的退出审批不会被 --yolo 跳过;Plan 模式下的 Bash 按普通放行规则处理。 :::

flag 冲突规则

以下组合会在启动时被拒绝:

  • --continue 与 --session 互斥——两者都表示"恢复历史会话"
  • --yolo 和 --auto 互斥——两种权限模式互斥
  • --prompt 不能与 --yolo、--auto 或 --plan 同时使用——非交互模式固定使用 auto 权限
  • --output-format 只能与 --prompt 一起使用

非交互模式下没人能审阅计划,所以新会话不应用 default_plan_mode。恢复已经处于 Plan 模式的会话时照常运行,会话仍在 Plan 模式中,但计划无法在非交互模式下批准,会等你下次用 kimi -r 打开会话时审阅。

恢复会话时,可以通过 --auto、--yolo 或 --plan 覆盖原会话保存的权限或计划模式。例如,kimi --continue --auto 会恢复最近会话并切换到 "Never Ask" 模式。

典型用法

直接运行开启新会话:

kimi

从上次中断的地方继续(自动找到当前目录最近的会话):

kimi --continue

从历史会话列表中挑选,或直接指定已知 ID:

kimi --session
kimi --session 01HZ...XYZ

跳过审批确认,适合已知安全的批处理任务:

kimi --yolo

让 Agent 自行处理一切,不再向用户提问:

kimi --auto

先阅读代码、产出实现计划,而不是立刻动手修改文件:

kimi --plan

自定义 Skills 目录

有两种方式指定 Skills 目录,语义不同:

  • --skills-dir <dir>(CLI flag):替换自动发现的用户和项目目录,仅对本次启动生效。可重复传入以叠加多个目录:

    kimi --skills-dir /path/to/team-skills --skills-dir ./local-skills
    
  • extra_skill_dirs(config.toml):叠加到自动发现的目录之上,长期生效,适合配置团队共享 Skills。详见 Agent Skills。

自定义 Agent

--agent 和 --agent-file 用于选择驱动新会话的 Agent,在 print 模式(kimi -p)和交互式 TUI 中均可使用:

kimi --agent reviewer
kimi -p --agent reviewer "审查这个分支上的改动"

--agent-file 以最高优先级注册单个 Agent 文件(仅本次启动)并选中它;该 flag 不可重复传入,--agent 与 --agent-file 互斥。两个 flag 都仅在新建会话时有效——都不能与 --session/--continue 组合,因为 Agent 在会话创建时绑定,恢复会话时会自动还原已绑定的 Agent。选择在会话首次绑定后即固定,之后不可切换;在 TUI 中,这些 flag 只绑定启动时的会话,之后在同一进程内新建的会话(例如通过 /new)使用默认 Agent。Agent 文件格式与发现目录详见 Agent 与 subagent。

非交互执行

在脚本或 CI 中运行单次 prompt 时,使用 -p:

kimi -p "Summarize the current repository status"

输出采用 transcript 样式:thinking 内容和 Assistant 正文都以 • 开头,换行后两个空格缩进。Assistant 正文输出到 stdout;thinking、工具进度和"恢复会话"提示输出到 stderr。-p 模式不会请求人工审批,普通工具调用按 auto 权限策略处理,静态 deny 规则仍然生效。

临时切换模型:

kimi -m kimi-code/kimi-for-coding -p "Explain the latest diff"

需要结构化读取输出时,使用 stream-json 格式——stdout 每行都是一个 JSON 对象:

kimi -p "List changed files" --output-format stream-json

stream-json 模式下,普通回复输出 Assistant 消息;模型调用工具时,先输出带 tool_calls 的 Assistant 消息,再输出对应的 Tool 消息,最后继续输出后续 Assistant 消息。thinking 内容不会写入 JSONL;工具进度和恢复会话提示仍写到 stderr。

子命令

kimi 提供以下子命令:login(非交互式登录)、acp(ACP IDE 模式)、web(前台运行本地 REST/WebSocket/web 服务并打开 web UI)、doctor(校验配置文件)、export(导出会话)、migrate(迁移旧版数据)、import-sessions(导入 Claude Code 和 Codex 会话)、upgrade(检查更新)、provider(管理供应商)。

kimi login

通过 RFC 8628 device-code 流程登录 Kimi Code OAuth,无需进入 TUI。命令会发起一次 device authorization 请求,将验证地址和用户码打印到 stderr,然后轮询直到浏览器侧完成授权。生成的 token 写入与 TUI /login 相同的本地位置,下次启动 kimi 时会自动加载。

kimi login

该子命令没有任何 flag。在轮询期间随时按 Ctrl-C 可取消登录;取消或失败时退出码为 1,成功为 0。

kimi acp

把 Kimi Code CLI 切换到 ACP(Agent Client Protocol)模式,在标准输入/输出上以 JSON-RPC 形式与 IDE 对话,让编辑器直接驱动 kimi 的会话和工具调用。通常不需要手动运行——IDE 会把它作为子进程入口启动。配置方式见在 IDE 中使用,技术细节见 kimi acp 参考。

kimi acp

kimi web

在当前终端前台运行本地 Kimi 服务 —— 同一个进程同时挂载 REST + WebSocket API 与 web UI —— 并在服务就绪后用默认浏览器打开 web UI。命令会一直挂在终端,直到收到 SIGINT / SIGTERM(如 Ctrl-C)时干净退出。

服务运行时,GET /openapi.json 会返回 REST OpenAPI 文档,GET /asyncapi.json 会返回本地 WebSocket 协议的 AsyncAPI 文档。用 API 驱动会话的完整流程见服务 API:用 API 驱动一个会话,协议细节见服务 API。

kimi web                 # 前台运行服务并打开浏览器
kimi web --no-open       # 不打开浏览器
kimi web --port 58628    # 指定绑定端口

同一 home 目录下可以同时运行多个实例:每个实例注册到 ~/.kimi-code/server/instances/,端口被占用时自动 +1 重试(58628、58629……)。

选项 说明
--port <port> 绑定端口;默认 58627;被占用时自动 +1 重试
--host [host] 绑定地址;缺省 127.0.0.1(仅本机),裸 --host 绑 0.0.0.0(所有网卡)
--allowed-host <host...> DNS 重绑定检查额外允许的 Host 头,可重复或逗号分隔
--log-level <level> 按所选级别开启服务日志;默认不输出
--debug-endpoints 挂载 /api/v1/debug/* 调试路由(默认关闭)
--dangerous-bypass-auth 关闭所有 REST 与 WebSocket 路由的 bearer token 鉴权,使 web UI 无需 token 即可连接;仅用于可信网络或自有鉴权代理之后
--web-title <title> 自定义 web UI 的浏览器标签页标题;默认为工作区目录名
--no-open 就绪后不自动打开浏览器

kimi web 默认只绑定本机 loopback 地址,并在启动横幅中打印 bearer token;web UI 通过 URL 的 #token= 片段自动完成鉴权。

::: info 提示 kimi server 命令树已废弃:任何 kimi server … 调用(含全部旧子命令)只会打印弃用提示并以退出码 1 结束,请改用 kimi web。唯一的例外是 kimi server kill,它仍然可用,仅用于停止 0.28.0 之前版本启动的服务。该提示将在 Kimi Code 下个大版本移除。 :::

::: danger 警告 --dangerous-bypass-auth 会彻底关闭鉴权。任何能访问该端口的人都能完全控制你的会话、文件系统和 shell。请仅在可信网络或自有鉴权反向代理之后使用,用完后按 Ctrl+C 停止服务。 :::

kimi server kill

已废弃——仅用于停止 0.28.0 之前的 Kimi Code 版本启动的服务。那些版本可能在后台遗留服务进程,记录在 legacy 单实例锁文件 ~/.kimi-code/server/lock 中;该命令先请求 POST /api/v1/shutdown 优雅退出,再对锁中记录的 pid 发 SIGTERM、必要时升级为 SIGKILL,并在确认进程退出后删除锁文件。kimi web 启动的服务在前台运行,直接用 Ctrl+C 停止即可。

kimi web rotate-token

生成新的持久化 bearer token(写入 ~/.kimi-code/server.token),旧 token 立即失效。token 是整个 home 目录共享的,所有运行中的实例会在下一次鉴权校验时自动换用新 token,无需重启。

kimi app

打开 Kimi Code 桌面端,在当前目录或指定目录中开始新会话:

kimi app
kimi app ~/project

需要先安装支持从 CLI 打开工作区的桌面端版本。相对路径以当前目录为基准解析。目标工作区已存在时,会打开新草稿,不恢复之前的会话。

如果系统报告无法打开应用,命令会提示运行 kimi install-desktop 打开下载页面,不会自动下载或安装桌面端。

kimi install-desktop

打印 Kimi Code 桌面端页面地址并在默认浏览器中打开,无需离开终端即可下载并安装桌面端应用。页面地址随当前区域而定:国内区域为 https://www.kimi.com/code,全球区域为 https://www.kimi.ai/code。

kimi install-desktop

该子命令没有任何选项。旧名称 kimi install-app 仍可作为隐藏别名使用。在 TUI 中也可以通过斜杠命令 /desktop(别名 /install-desktop)打开同一页面。

kimi doctor

校验 config.toml 和 tui.toml,不会启动 TUI,也不会修改任一文件。默认检查 KIMI_CODE_HOME 下的文件;未设置该环境变量时检查 ~/.kimi-code。默认路径缺失时会显示为跳过,因为内置默认值仍可生效。

kimi doctor
命令 说明
kimi doctor 校验默认 config.toml 和 tui.toml
kimi doctor config [path] 只校验 config.toml;传入 path 时使用该文件而不是默认文件
kimi doctor tui [path] 只校验 tui.toml;传入 path 时使用该文件而不是默认文件

显式传入路径时,文件必须存在。所有被检查的文件都有效或被跳过时,退出码为 0;任何指定文件缺失或配置无效时,退出码为 1。

# 检查默认配置文件
kimi doctor

# 只检查默认运行时配置
kimi doctor config

# 替换正式 TUI 配置前,先检查候选文件
kimi doctor tui ./tui.toml

kimi export

把一个会话打包成 ZIP 文件,便于分享、归档或提交问题反馈。

kimi export [sessionId] [options]
参数 / 选项 简写 说明
sessionId 要导出的会话 ID。省略时自动选择当前工作目录下最近一次的会话,并要求确认
--output <path> -o 输出 ZIP 文件路径。省略时写入当前目录下的默认文件名
--yes -y 跳过默认会话的确认提示,直接导出
--no-include-global-log 不打包全局诊断日志。默认包含

导出包含目标会话目录内的所有文件。全局诊断日志(~/.kimi-code/logs/kimi-code.log)默认包含,因为它可能含有其他会话或项目的事件;不想分享时加 --no-include-global-log。

# 导出当前工作目录最近一次会话,跳过确认
kimi export -y

# 导出指定会话到自定义路径
kimi export 01HZ...XYZ -o ./bug-report.zip

# 排除全局诊断日志
kimi export 01HZ...XYZ -o ./bug-report.zip --no-include-global-log

kimi migrate

将旧版 kimi-cli 的本地数据迁移到 kimi-code,包括历史会话和配置文件。纯交互式运行,会引导你完成全流程。

kimi migrate

完整迁移说明见从 kimi-cli 迁移。

kimi import-sessions

把最近的 Claude Code 和 Codex 会话导入为普通的 Kimi Code 会话。命令会打开一个交互式页面:选择最近 15 天(默认)或最近 30 天,然后查看进度和汇总。在 TUI 中用 /import-sessions 打开的是同一个页面。

kimi import-sessions
  • 每次最多导入最新的 100 个会话;已导入过的会话会被跳过。
  • 没有提问的会话、子代理会话,以及 codex exec 等非交互运行不会列出。原项目目录已不存在的会话、仍在写入的会话,以及经过 fork 或从之前消息重新开始的 Codex 会话会被跳过,并在汇总中说明。
  • 默认从 ~/.claude 和 ~/.codex 读取会话;设置 CLAUDE_CONFIG_DIR 或 CODEX_HOME 可改为其他位置。原始会话文件不会被修改。

导入后的会话包含哪些内容,见从 Claude Code 和 Codex 导入会话。

kimi upgrade

立即检查最新版本并展示更新提示,选择操作后退出。也可以使用别名 kimi update。

kimi upgrade [-y]

对全局 npm、pnpm、yarn、bun 安装,kimi upgrade 会展示更新选项;选择 Install update now 后运行对应的前台安装命令。对 native 安装(含 Windows),会在前台下载并校验新二进制,并在下次启动时替换生效。当前安装方式无法自动升级时,改为打印手动更新命令。传入 -y, --yes 可跳过确认提示,直接安装更新。

kimi vis

在浏览器中启动会话可视化工具,直观查看一次会话的全过程。命令会启动一个指向本地会话的进程内服务器,打印访问地址并打开浏览器,持续运行直到你按下 Ctrl-C。

kimi vis [sessionId] [options]
参数 / 选项 说明
sessionId 直接打开指定会话的可视化页面。省略时打开列出所有会话的首页
--port <number> 绑定的端口。默认自动挑选一个空闲端口
--host <host> 绑定的主机。默认 127.0.0.1
--no-open 不自动打开浏览器,仅打印访问地址
# 启动可视化工具并在浏览器中打开首页
kimi vis

# 直接打开指定会话
kimi vis 01HZ...XYZ

# 绑定固定主机和端口且不打开浏览器(例如在远程主机上)
kimi vis --host 0.0.0.0 --port 8123 --no-open

kimi provider

在 shell 中管理供应商,相当于 TUI 中 /provider 的非交互版本。适合脚本化部署、CI 初始化,以及在新机器上一行完成配置。

kimi provider <action> [options]

包含五个动作:

kimi provider add <url>

从自定义 registry(api.json)批量导入所有供应商。命令会拉取 registry,为每个条目创建 [providers.<id>] 和 [models.<alias>],并写入 source 元数据,使 TUI 下次启动时自动刷新同一 registry 地址下的供应商和模型。当 registry 条目声明了 env 字段时,命令会打印一条提示,指明声明的变量名——想用就在 config.toml 里设置 api_key_env,详见平台与模型。

参数 / 选项 说明
<url> Registry 地址
--api-key <key> 访问 registry 时携带的 Bearer token。未传时回退到环境变量 KIMI_REGISTRY_API_KEY;可选,两者都不传即可导入公开 registry
kimi provider add https://registry.example.com/v1/models/api.json --api-key YOUR_KEY

# 或通过环境变量(适合 CI / .envrc)
KIMI_REGISTRY_API_KEY=YOUR_KEY kimi provider add https://registry.example.com/v1/models/api.json

# 公开 registry:无需密钥
kimi provider add https://registry.example.com/v1/models/api.json

如果某个 provider id 已存在,会先删除再重新写入。不会自动设置默认模型,后续可用 -m 或 TUI 内的 /model 选择。

kimi provider remove <providerId>

删除指定供应商及其所有模型 alias。如果被删除的供应商正好是 default_model 所属,则同时清空 default_model。

kimi provider remove kohub

kimi provider list

按行打印每个已配置的供应商,含类型、模型数量、来源。加 --json 可输出原始的 providers 和 models 表,便于程序化处理。

kimi provider list
kimi provider list --json | jq '.providers | keys'

kimi provider catalog list [providerId]

在不修改任何配置的情况下浏览公开的 models.dev 模型目录。不传参数时列出所有供应商及协议类型和模型数量;传 providerId 时列出该供应商下所有模型的上下文窗口和能力。目录地址不可达时会使用内置目录快照。

参数 / 选项 说明
[providerId] 可选,要查看的供应商 id
--filter <substring> 按 id 或 name 大小写不敏感子串过滤
--url <url> 覆盖 catalog 地址,默认 https://models.dev/api.json
--json 以 JSON 形式输出匹配片段
kimi provider catalog list
kimi provider catalog list --filter anthropic
kimi provider catalog list anthropic

kimi provider catalog add <providerId>

按 id 从 catalog 直接导入一个已知供应商,协议类型、base URL、模型信息均由 catalog 提供,只需提供 API key。catalog 未声明协议的供应商(如 xai、openrouter 这类厂商专用 SDK)按 OpenAI 兼容协议导入,并在输出中标注 "guessed";catalog 未提供可用端点时需用 --base-url 显式指定。专有协议(如 Amazon Bedrock)无法导入。公共目录不可达时会回退到内置目录快照,离线或网络受限环境下也能导入。

参数 / 选项 说明
<providerId> catalog 中的供应商 id,如 anthropic、openai
--api-key <key> 供应商 API key。未传时回退到 KIMI_REGISTRY_API_KEY,必填
--default-model <modelId> 可选,导入后把 default_model 设为 <providerId>/<modelId>
--base-url <url> 覆盖 catalog 声明的端点;catalog 未提供端点(或仅有环境变量占位符)时必填
--url <url> 覆盖 catalog 地址,默认 https://models.dev/api.json
kimi provider catalog list anthropic          # 先看可选的模型
kimi provider catalog add anthropic --api-key sk-ant-... --default-model claude-opus-4-7

下一步

  • 斜杠命令 — 交互式 TUI 内的控制命令速查
  • 配置文件 — default_model、权限模式等启动参数的持久化配置
  • Agent Skills — --skills-dir 加载的 Skill 文件格式
  • Agent 与 subagent — 内置 subagent、自定义 Agent 文件与通过 --agent 选择 main agent