Communitygithub.com

MoonshotAI/write-tui

用於撰寫或修改 kimi-code 終端機介面(位於 apps/kimi-code/src/tui)時使用,涵蓋元件、對話框/選擇器、斜線指令、主題、串流渲染以及 KimiTUI 控制器。說明架構、新功能放置位置、測試位置、主題系統機制以及對話框互動與視覺規範(DESIGN.md)。

write-tui 是什麼?

這個技能(MoonshotAI/write-tui)是專門為開發與維護 kimi-code 終端機使用者介面(Terminal UI)的開發者設計的。它提供完整的架構指引,說明 KimiTUI 協調器如何將狀態、版面、連線與對話框整合在一起,並將業務邏輯委派給各個專屬控制器(例如串流渲染、連線重播、按鍵處理等)。開發者可以依照功能類型(斜線指令、對話框、串流事件等)找到正確的實作位置,了解測試檔案應放在哪裡,以及掌握集中式的主題管理機制(色彩記號來源為 ColorPalette)。此外,本技能也強調必須遵循 DESIGN.md 中的互動與視覺規範,並在提交前執行自我檢查清單。支援平台為 Claude Code、Cursor、Codex 等以 SKILL.md 為基礎的 AI 代理工具。

相容平台~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 — 串流渲染:助理 delta、思考過程、工具呼叫/結果、壓縮、子代理、背景代理、對話記錄彙總。
    • 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 標記主題、終端機背景偵測。色彩的唯一真相來源。
  • 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 — 標記/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 命名顏色、對比度要求、禁止在模組頂層快取 styled 函式、在發明顏色前先新增 ColorPalette 記號)是標準規範並由守護程式強制執行——它們位於 apps/kimi-code/AGENTS.md 中。本技能僅涵蓋機制。

提交前

  • 對您修改的檔案執行 lint / format / test。
  • 對於任何對話框/選擇器/輸入/切換列表,請執行 DESIGN.md 末尾的自我檢查清單。
  • 對於可列印按鍵比較(CI 守護程式),請使用 printableChar();對於顏色(CI 守護程式),請使用 chalk.hex(colors.<token>)。

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.

相關技能