Communitygithub.com

终端UI开发技能 (apps/kimi-code/tui)

用于编写或修改kimi-code终端用户界面(位于apps/kimi-code/src/tui)——涵盖组件、对话框/选择器、斜杠命令、主题、流式渲染以及KimiTUI控制器。涉及架构说明、新功能放置位置、测试放置规则、主题系统机制以及对话框交互/视觉规范(DESIGN.md)。

kimi-code/tui) 是什么?

该技能专为开发kimi-code的终端用户界面(位于apps/kimi-code/src/tui)而设计。它提供了详尽的架构指南,包括KimiTUI协调器、控制器、组件、主题系统等核心模块的职责划分。通过该技能,开发者可以明确各类新功能(如CLI参数、斜杠命令、弹窗、流式渲染等)的正确放置位置,并遵循严格的测试放置规则(测试文件应紧邻对应源代码目录)。主题系统是技能的重点之一,它详细说明了颜色令牌管理、应用/切换流程以及必须保持同步的文档和模式。此外,技能还强调在提交代码前必须运行lint/format/test,并遵循DESIGN.md中的自检清单,确保对话框、选择器、输入框等元素的交互和视觉符合规范。该技能适用于基于Node.js的终端应用开发,尤其是使用pi-tui、chalk等库的CLI工具。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/MoonshotAI/kimi-code/tree/main/.agents/skills/write-tui

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

编写TUI(apps/kimi-code)

终端用户界面位于 apps/kimi-code/src/tui。在编写TUI代码前,请先阅读 apps/kimi-code/AGENTS.md,了解始终有效的项目地图、模块边界和硬约束(可打印键解码、禁止chalk命名颜色等)。本技能是操作指南:架构说明、功能路由、测试放置、主题机制和对话框规范。

对于任何列表对话框、选择器、输入框或状态/切换列表,交互和视觉规则均为规范性要求——请参见此文件夹中的 DESIGN.md,并在提交前遵循其自检清单。

架构

KimiTUI 是一个协调器,负责将状态、布局、会话和对话框连接在一起,并将复杂逻辑委托给控制器。

  • src/tui/kimi-tui.ts — KimiTUI 协调器。持有 state,负责启动/关闭顺序、布局/编辑器连接、用户输入入口、发送/排队、会话生命周期以及斜杠命令处理调度。它不应积累事件路由或渲染逻辑——这些应放在控制器中。
  • src/tui/tui-state.ts — TUIState、createTUIState、createInitialAppState。全局UI状态形状。在添加新全局字段前,需判断它是否真正属于此处,还是应作为局部组件状态。
  • src/tui/controllers/ — 可独立测试的责任模块。每个控制器负责一个切片:
    • session-event-handler.ts — 路由SDK会话事件(handleEvent 调度 + 每个事件的 handleXxx)。具体事件处理在此处,而不是在 KimiTUI 中。
    • streaming-ui.ts — 流式渲染:助手增量、思考、工具调用/结果、压缩、子代理、后台代理、转录聚合。
    • session-replay.ts — 恢复/重播编排;通过相同的实时渲染钩子驱动重播记录。无状态重播解析/限制/投影辅助函数位于 src/tui/utils/message-replay.ts。
    • tasks-browser.ts — 任务浏览器控制器。
    • editor-keyboard.ts — 编辑器键盘处理、退出快捷键、外部编辑器、剪贴板图片。
    • auth-flow.ts — 登录/认证编排(refreshConfigAfterLogin 等)。
  • src/tui/commands/ — 斜杠命令声明、解析、排序和动态技能命令生成。仅包含解析和类型;执行由 KimiTUI 的斜杠命令处理部分调度,复杂执行流入 utils 或特定组件。
  • src/tui/components/ — 按UI类型划分的pi-tui组件:chrome/(页脚、待办事项、欢迎、加载器、设备码)、dialogs/(选择器、批准/问题面板、替换编辑器的设置弹窗)、editor/(输入框 + 提及提供器)、media/(图片、差异、代码高亮)、messages/(转录块 + 工具渲染器)、panes/(活动、队列)。
  • src/tui/reverse-rpc/ — 将SDK批准/问题回调适配为UI面板数据,并将用户的选择转换回SDK响应。
  • src/tui/theme/ — 主题、颜色令牌、样式辅助函数、pi-tui markdown主题、终端背景检测。颜色的唯一真实来源。
  • src/tui/utils/ — TUI专用工具(需要 TUIState 或组件)。应用范围内、与UI无关的辅助函数位于 src/utils/。

当控制器或 KimiTUI 的某个部分不断增长时,应将纯函数、状态投影和展示组件拆分到匹配的目录中,而不是扩展现有文件。

新功能的放置位置

功能类型决定其归属位置:

  • CLI参数 → src/cli/commands.ts / src/cli/options.ts,通过 src/cli/run-shell.ts 传入TUI。CLI从不直接操作会话。
  • CLI子命令 → src/cli/sub/,仅非交互模式;通过 @moonshot-ai/kimi-code-sdk 访问核心。
  • 斜杠命令 → 在 src/tui/commands/ 下声明/解析/类型化;在 KimiTUI 的斜杠命令处理部分添加执行入口;将复杂逻辑下沉到 utils 或特定组件。
  • 技能派生命令 → 接入 buildSkillSlashCommands / 技能命令映射;不要硬编码单个技能。
  • 转录消息类型 → 在 src/tui/types.ts 中定义形状,添加/扩展 components/messages/ 组件,在转录构建器中注册渲染器。
  • 工具结果展示 → 扩展 components/messages/tool-renderers/registry.ts 和渲染器;不要在 ToolCallComponent 内部堆积分支。
  • 弹窗/选择器 → components/dialogs/,通过 mountEditorReplacement 挂载;遵循 DESIGN.md。如果由SDK回调触发,检查 reverse-rpc/ 是否需要适配器/控制器/处理器。
  • SDK事件处理 → 在 session-event-handler.ts 的 handleEvent 中添加调度,然后是匹配的 handleXxx。
  • 流式渲染 → controllers/streaming-ui.ts。
  • 会话开始/恢复行为 → KimiTUI 的会话管理部分;重播行为 → controllers/session-replay.ts,重用实时渲染路径。
  • 状态栏/活动/队列 → chrome/footer、panes/activity、panes/queue 以及匹配的 updateXxx。
  • 配置选项 → 在 src/tui/config.ts 中读/写 + 模式,然后是设置UI;通过 saveTuiConfig 持久化(组件从不自行写入配置文件)。
  • 常量 → 共享的CLI/TUI非复制常量位于 src/constant/;TUI专用的非复制常量位于 src/tui/constant/。组件局部的复制、选项标签、帮助文本、对话框标题/页脚应保持在组件旁边——不要将复制集中到全局模块中。
  • 通用能力 → 无TUI状态依赖 → src/utils/;依赖TUI状态或组件 → src/tui/utils/。

测试放置

  • 组件行为测试放置在该组件的现有测试旁边(test/tui/components/...)。
  • 命令解析测试 → test/tui/commands/。
  • reverse-rpc测试 → test/tui/reverse-rpc/。
  • 纯工具函数测试 → 放在相应工具测试旁边。
  • 不要为了放一个小功能而创建通用的 some-feature.test.ts;应扩展最近的现有测试文件。

主题系统机制

主题在 src/tui/theme/ 下集中管理:

  • colors.ts — 语义令牌:ColorPalette、darkColors、lightColors。
  • styles.ts — 基于 ColorPalette 的通用chalk辅助函数。
  • pi-tui-theme.ts — markdown/pi-tui主题配置。
  • terminal-background.ts — 自动分辨率使用的终端背景检测。
  • bundle.ts — 将 colors、styles、markdownTheme 打包成 KimiTUIThemeBundle。
  • index.ts / detect.ts — 主题类型和自动/深色/浅色分辨率。

保持颜色令牌集同步。 colors.ts 中的 ColorPalette 是颜色令牌的唯一真实来源。当你添加、重命名或删除一个令牌时,请在同一个更改中更新其镜像:自定义主题JSON模式(apps/kimi-code/src/tui/theme/theme-schema.json)、自定义主题文档中的令牌表(docs/en/customization/themes.md 和 docs/zh/customization/themes.md),以及内置 custom-theme 技能中的令牌表(packages/agent-core/src/skill/builtin/custom-theme.md)。

应用/切换流程:

  • UI入口:ThemeSelectorComponent → handleThemeCommand → applyThemeChoice。
  • 实际应用步骤是 KimiTUI.applyTheme:它更新 state.theme、state.appState.theme,并通知组件刷新其调色板。
  • 通过 saveTuiConfig 持久化选择——组件不得自行写入配置文件。

硬性颜色规则(禁止chalk命名颜色、对比度要求、禁止模块顶级的缓存样式函数、在发明颜色前先添加 ColorPalette 令牌)是规范性要求,并通过守卫强制执行——它们位于 apps/kimi-code/AGENTS.md 中。本技能仅涵盖机制。

提交前

  • 对你更改的文件运行 lint / format / test。
  • 对于任何对话框/选择器/输入框/切换列表,请检查 DESIGN.md 末尾的自检清单。
  • 对可打印键比较使用 printableChar()(CI守卫),对颜色使用 chalk.hex(colors.<token>)(CI守卫)。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

MoonshotAI/agent-core-dev

Use when developing in packages/agent-core-v2 (the DI × Scope agent engine) — adding or modifying a domain Service, choosing a LifecycleScope, wiring DI dependencies, splitting a domain across scopes, owning or migrating a config section, gating behavior behind an experimental flag, raising coded errors, working on the permission system, writing DI/Scope tests, porting business logic from agent-core (v1) to v2, triaging a main-branch commit against v2, or exposing a v2 domain over server-v2 while keeping the /api/v1 wire contract compatible with released clients. Self-contained guide organized by development stage (orient → design → implement → test → verify) plus align workflows for v1→v2 migration, main-branch commit triage, and server-v2 wire exposure; each file carries the rules, examples, and red lines for its step.

MoonshotAI/agent-core-review

Use ONLY for code review and test write/review guidance in `packages/agent-core-v2` (the DI × Scope agent engine). Does NOT apply to the legacy `packages/agent-core` or to any other package — for those, do not load this skill. Groups the review and testing lenses used for agent-core-v2 — `slop` (single-level-of-abstraction / layered error-handling review, invoked only on explicit request) and `test` (contract-driven per-test rules for both authoring and reviewing tests). Apply the sub-skill that matches the task; do not apply `slop` unprompted.

MoonshotAI/consolidate

Apply an approved sub-skill grouping by moving user-specified skills into a parent bundle, with timestamped backups of every modified directory.

MoonshotAI/gen-changesets

Use when generating changesets in the kimi-code repository, including package bump selection, internal package and CLI bundle handling, bump levels, major confirmation, and English changelog wording.

MoonshotAI/gen-docs

Update Kimi Code CLI user documentation after meaningful code changes that affect product behavior or user experience.

MoonshotAI/kimi-datasource

Universal data-source assistant. Use this skill when the user wants external structured data such as stocks, financial reports, technical indicators, A-share/HK/US markets, global macroeconomics, Chinese enterprise registry information, arXiv papers, Google Scholar results, or Chinese laws/regulations and judicial cases. This plugin exposes tools via MCP server `plugin-kimi-datasource_data`; call them in the flow `mcp__plugin-kimi-datasource_data__get_data_source_desc` → `mcp__plugin-kimi-datasource_data__call_data_source_tool`.

MoonshotAI/pre-changelog

Use before merging a kimi-code release PR to preview the user-facing CLI changelog in Chinese. Reads the changelog that changesets pre-generated in the release PR, then reuses sync-changelog's strip / classify / translate logic to render a Chinese preview. Writes no files.

MoonshotAI/review

Analyze the available skill set and recommend candidate groups that could be consolidated into sub-skill bundles. Read-only — proposes a plan, does not move files.

MoonshotAI/slop

Invoke only when the user explicitly asks to review code through the "single level of abstraction / layered error handling" lens — a function does only its own layer's business logic while errors are handled above or below. The agent reports detections, raw-count measurements, and move directions. Apply only when the user explicitly requests this lens.

MoonshotAI/sub-skill

Discover and reorganize the skill inventory into hierarchical sub-skill bundles. Use when the user asks to review, group, or consolidate skills into a parent bundle.

MoonshotAI/sync-changelog

Use after a release succeeds, when maintainers need to sync apps/kimi-code/CHANGELOG.md into docs/en/release-notes/changelog.md and docs/zh/release-notes/changelog.md, then open a PR on a dedicated branch.

MoonshotAI/test

Use when writing or reviewing tests, or when asked how to write a good single test. Encodes the per-test rules behind the "test the contract / responsibility, not the implementation" principle — name and structure one behavior per `it`, drive through the public surface, stub only true external boundaries, control time and config via documented knobs, and keep tests clear, isolated, and refactor-resilient. The same rules drive both authoring (write mode) and auditing existing tests (review mode).

MoonshotAI/translate-docs

Translate and sync bilingual user documentation between docs/zh/ and docs/en/ following the source-of-truth rules in docs/AGENTS.md.

相关技能