# 《Kimi Code CLI 详解》第7-8章
## 第七章:用户界面与交互层
### 多 UI 架构的设计理念
Kimi Code CLI 支持多种用户界面模式:
| 场景 | UI 模式 | 原因 |
|------|---------|------|
| 日常开发 | Shell | 快速、高效、沉浸式 |
| CI/CD | Print | 非交互式、可脚本化 |
| IDE 集成 | ACP | 标准化协议、跨编辑器 |
| 远程访问 | Web | 跨平台、无需安装 |
### Wire 协议:UI 与核心的契约
```
┌─────────────┐ Wire 协议 ┌─────────────┐
│ UI 层 │ ◄─────────────────► │ 核心层 │
│ (Shell/ │ │ (KimiSoul) │
│ Print/ACP)│ │ │
└─────────────┘ └─────────────┘
```
消息类型:
- **控制事件**:`TurnBegin`, `StepBegin`, `TurnEnd`
- **内容事件**:`TextPart`, `ToolCall`, `ToolResult`
- **请求**:`ApprovalRequest`, `ToolCallRequest`
### Shell UI:沉浸式终端体验
实时渲染 LLM 输出和工具执行结果:
```python
async def _execute(self, user_input: str):
async for msg in self._soul.run(user_input, cancel_event):
await self._render(msg)
async def _render(self, msg: WireMessage):
match msg:
case TextPart(text=text):
self._console.print(text, end="")
case ToolCall():
self._show_tool_call(msg)
case ApprovalRequest():
await self._handle_approval(msg)
```
### ACP:IDE 集成的桥梁
ACP(Agent Client Protocol)是开放的协议标准:
```json
// Zed 配置
{
"agent_servers": {
"Kimi Code CLI": {
"command": "kimi",
"args": ["acp"]
}
}
}
```
---
## 第八章:配置系统与扩展机制
### 配置的分层模型
```
优先级(高 → 低)
命令行参数 → 环境变量 → 用户配置 → 项目配置 → 默认值
```
### Agent 规范系统
支持继承机制:
```yaml
agent:
extend: default # 继承 default agent
name: MyCustomAgent
system_prompt_path: ./prompt.txt # 覆盖系统提示词
tools:
- kimi_cli.tools.shell:Shell
subagents:
coder:
path: ./agents/coder.yaml
description: A coding agent
```
### Skill 系统:轻量级扩展
Skill 是包含 `SKILL.md` 的目录:
```markdown
---
name: gen-changelog
description: Generate changelog from git commits
type: standard
---
# Generate Changelog
1. Get commits since last tag
2. Categorize by type (feat, fix, docs)
3. Format according to Keep a Changelog
```
自动注册为斜杠命令:`/skill:gen-changelog`
### MCP 扩展:开放工具生态
```bash
# 添加 MCP 服务器
kimi mcp add --transport stdio my-server -- npx my-mcp-server
kimi mcp add --transport http my-server https://api.example.com/mcp
# 查看已添加的服务器
kimi mcp list
```
### 自定义工具开发
```python
class MyCustomTool(CallableTool):
def __init__(self, config: Config):
super().__init__(
name="my_tool",
description="Does something useful",
parameters={...}
)
async def __call__(self, param1: str) -> ToolReturnValue:
try:
result = await self._process(param1)
return ToolOk(output=result)
except Exception as e:
return ToolError(message=str(e))
```
---
### 讨论话题
1. Wire 协议的设计可以应用到哪些其他场景?
2. 如果你要设计一个新的 UI(如语音界面),需要考虑哪些因素?
3. 配置系统的分层模型有什么优缺点?
*本系列共10章,第9-10章(开发实践与未来展望)即将发布。完整内容已保存至本地 `book/` 目录。*
登录后可参与表态
讨论回复
0 条回复还没有人回复,快来发表你的看法吧!