@kkelly-offical/kkcode 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +445 -0
- package/package.json +46 -0
- package/src/agent/agent.mjs +170 -0
- package/src/agent/custom-agent-loader.mjs +158 -0
- package/src/agent/generator.mjs +115 -0
- package/src/agent/prompt/architect.txt +36 -0
- package/src/agent/prompt/build-fixer.txt +71 -0
- package/src/agent/prompt/build.txt +101 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +29 -0
- package/src/agent/prompt/guide.txt +40 -0
- package/src/agent/prompt/longagent.txt +178 -0
- package/src/agent/prompt/plan.txt +50 -0
- package/src/agent/prompt/researcher.txt +23 -0
- package/src/agent/prompt/reviewer.txt +44 -0
- package/src/agent/prompt/security-reviewer.txt +62 -0
- package/src/agent/prompt/tdd-guide.txt +84 -0
- package/src/agent/prompt/title.txt +8 -0
- package/src/command/custom-commands.mjs +57 -0
- package/src/commands/agent.mjs +71 -0
- package/src/commands/audit.mjs +77 -0
- package/src/commands/background.mjs +86 -0
- package/src/commands/chat.mjs +114 -0
- package/src/commands/command.mjs +41 -0
- package/src/commands/config.mjs +44 -0
- package/src/commands/doctor.mjs +148 -0
- package/src/commands/hook.mjs +29 -0
- package/src/commands/init.mjs +141 -0
- package/src/commands/longagent.mjs +100 -0
- package/src/commands/mcp.mjs +89 -0
- package/src/commands/permission.mjs +36 -0
- package/src/commands/prompt.mjs +42 -0
- package/src/commands/review.mjs +266 -0
- package/src/commands/rule.mjs +34 -0
- package/src/commands/session.mjs +235 -0
- package/src/commands/theme.mjs +98 -0
- package/src/commands/usage.mjs +91 -0
- package/src/config/defaults.mjs +195 -0
- package/src/config/import-config.mjs +76 -0
- package/src/config/load-config.mjs +76 -0
- package/src/config/schema.mjs +509 -0
- package/src/context.mjs +40 -0
- package/src/core/constants.mjs +46 -0
- package/src/core/errors.mjs +57 -0
- package/src/core/events.mjs +29 -0
- package/src/core/types.mjs +57 -0
- package/src/github/api.mjs +78 -0
- package/src/github/auth.mjs +286 -0
- package/src/github/flow.mjs +298 -0
- package/src/github/workspace.mjs +212 -0
- package/src/index.mjs +82 -0
- package/src/knowledge/api-design.txt +9 -0
- package/src/knowledge/cpp.txt +10 -0
- package/src/knowledge/docker.txt +10 -0
- package/src/knowledge/dotnet.txt +9 -0
- package/src/knowledge/electron.txt +10 -0
- package/src/knowledge/flutter.txt +10 -0
- package/src/knowledge/go.txt +9 -0
- package/src/knowledge/graphql.txt +10 -0
- package/src/knowledge/java.txt +9 -0
- package/src/knowledge/kotlin.txt +10 -0
- package/src/knowledge/loader.mjs +125 -0
- package/src/knowledge/next.txt +8 -0
- package/src/knowledge/node.txt +8 -0
- package/src/knowledge/nuxt.txt +9 -0
- package/src/knowledge/php.txt +10 -0
- package/src/knowledge/python.txt +10 -0
- package/src/knowledge/react-native.txt +10 -0
- package/src/knowledge/react.txt +9 -0
- package/src/knowledge/ruby.txt +11 -0
- package/src/knowledge/rust.txt +9 -0
- package/src/knowledge/svelte.txt +9 -0
- package/src/knowledge/swift.txt +10 -0
- package/src/knowledge/tailwind.txt +10 -0
- package/src/knowledge/testing.txt +8 -0
- package/src/knowledge/typescript.txt +8 -0
- package/src/knowledge/vue.txt +9 -0
- package/src/mcp/client-http.mjs +157 -0
- package/src/mcp/client-sse.mjs +286 -0
- package/src/mcp/client-stdio.mjs +451 -0
- package/src/mcp/registry.mjs +394 -0
- package/src/mcp/stdio-framing.mjs +127 -0
- package/src/orchestration/background-manager.mjs +358 -0
- package/src/orchestration/background-worker.mjs +245 -0
- package/src/orchestration/longagent-manager.mjs +116 -0
- package/src/orchestration/stage-scheduler.mjs +489 -0
- package/src/orchestration/subagent-router.mjs +62 -0
- package/src/orchestration/task-scheduler.mjs +74 -0
- package/src/permission/engine.mjs +92 -0
- package/src/permission/exec-policy.mjs +372 -0
- package/src/permission/prompt.mjs +39 -0
- package/src/permission/rules.mjs +120 -0
- package/src/permission/workspace-trust.mjs +44 -0
- package/src/plugin/builtin-hooks/console-warn.mjs +41 -0
- package/src/plugin/builtin-hooks/extract-patterns.mjs +75 -0
- package/src/plugin/builtin-hooks/post-edit-format.mjs +57 -0
- package/src/plugin/builtin-hooks/post-edit-typecheck.mjs +61 -0
- package/src/plugin/builtin-hooks/strategic-compaction.mjs +38 -0
- package/src/plugin/hook-bus.mjs +154 -0
- package/src/provider/anthropic.mjs +389 -0
- package/src/provider/ollama.mjs +236 -0
- package/src/provider/openai-compatible.mjs +1 -0
- package/src/provider/openai.mjs +339 -0
- package/src/provider/retry-policy.mjs +68 -0
- package/src/provider/router.mjs +228 -0
- package/src/provider/sse.mjs +91 -0
- package/src/repl.mjs +2929 -0
- package/src/review/diff-parser.mjs +36 -0
- package/src/review/rejection-queue.mjs +62 -0
- package/src/review/review-store.mjs +21 -0
- package/src/review/risk-score.mjs +61 -0
- package/src/rules/load-rules.mjs +64 -0
- package/src/runtime.mjs +1 -0
- package/src/session/checkpoint.mjs +239 -0
- package/src/session/compaction.mjs +276 -0
- package/src/session/engine.mjs +225 -0
- package/src/session/instinct-manager.mjs +172 -0
- package/src/session/instruction-loader.mjs +25 -0
- package/src/session/longagent-plan.mjs +329 -0
- package/src/session/longagent-scaffold.mjs +100 -0
- package/src/session/longagent.mjs +1462 -0
- package/src/session/loop.mjs +905 -0
- package/src/session/memory-loader.mjs +75 -0
- package/src/session/project-context.mjs +367 -0
- package/src/session/prompt/anthropic.txt +151 -0
- package/src/session/prompt/beast.txt +37 -0
- package/src/session/prompt/max-steps.txt +6 -0
- package/src/session/prompt/plan.txt +9 -0
- package/src/session/prompt/qwen.txt +46 -0
- package/src/session/prompt-loader.mjs +18 -0
- package/src/session/recovery.mjs +52 -0
- package/src/session/store.mjs +503 -0
- package/src/session/system-prompt.mjs +260 -0
- package/src/session/task-validator.mjs +266 -0
- package/src/session/usability-gates.mjs +379 -0
- package/src/skill/builtin/backend-patterns.mjs +123 -0
- package/src/skill/builtin/commit.mjs +64 -0
- package/src/skill/builtin/debug.mjs +45 -0
- package/src/skill/builtin/frontend-patterns.mjs +120 -0
- package/src/skill/builtin/frontend.mjs +188 -0
- package/src/skill/builtin/init.mjs +220 -0
- package/src/skill/builtin/review.mjs +49 -0
- package/src/skill/builtin/security-checklist.mjs +80 -0
- package/src/skill/builtin/tdd.mjs +54 -0
- package/src/skill/generator.mjs +113 -0
- package/src/skill/registry.mjs +336 -0
- package/src/storage/audit-store.mjs +83 -0
- package/src/storage/event-log.mjs +82 -0
- package/src/storage/ghost-commit-store.mjs +235 -0
- package/src/storage/json-store.mjs +53 -0
- package/src/storage/paths.mjs +148 -0
- package/src/theme/color.mjs +64 -0
- package/src/theme/default-theme.mjs +29 -0
- package/src/theme/load-theme.mjs +71 -0
- package/src/theme/markdown.mjs +135 -0
- package/src/theme/schema.mjs +45 -0
- package/src/theme/status-bar.mjs +158 -0
- package/src/tool/audit-wrapper.mjs +38 -0
- package/src/tool/edit-transaction.mjs +126 -0
- package/src/tool/executor.mjs +109 -0
- package/src/tool/file-lock-manager.mjs +85 -0
- package/src/tool/git-auto.mjs +545 -0
- package/src/tool/git-full-auto.mjs +478 -0
- package/src/tool/image-util.mjs +276 -0
- package/src/tool/prompt/background_cancel.txt +1 -0
- package/src/tool/prompt/background_output.txt +1 -0
- package/src/tool/prompt/bash.txt +71 -0
- package/src/tool/prompt/codesearch.txt +18 -0
- package/src/tool/prompt/edit.txt +27 -0
- package/src/tool/prompt/enter_plan.txt +74 -0
- package/src/tool/prompt/exit_plan.txt +62 -0
- package/src/tool/prompt/glob.txt +33 -0
- package/src/tool/prompt/grep.txt +43 -0
- package/src/tool/prompt/list.txt +8 -0
- package/src/tool/prompt/multiedit.txt +20 -0
- package/src/tool/prompt/notebookedit.txt +21 -0
- package/src/tool/prompt/patch.txt +24 -0
- package/src/tool/prompt/question.txt +44 -0
- package/src/tool/prompt/read.txt +40 -0
- package/src/tool/prompt/task.txt +83 -0
- package/src/tool/prompt/todowrite.txt +117 -0
- package/src/tool/prompt/webfetch.txt +38 -0
- package/src/tool/prompt/websearch.txt +43 -0
- package/src/tool/prompt/write.txt +38 -0
- package/src/tool/prompt-loader.mjs +18 -0
- package/src/tool/question-prompt.mjs +86 -0
- package/src/tool/registry.mjs +1309 -0
- package/src/tool/task-tool.mjs +28 -0
- package/src/ui/activity-renderer.mjs +410 -0
- package/src/ui/repl-dashboard.mjs +357 -0
- package/src/usage/pricing.mjs +121 -0
- package/src/usage/usage-meter.mjs +113 -0
- package/src/util/git.mjs +496 -0
- package/src/util/template.mjs +10 -0
- package/src/util/yaml.mjs +100 -0
package/README.md
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
# kkcode
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
面向团队协作的终端 AI Coding Agent:兼顾 **可执行**(工具链 + 子任务)、**可治理**(权限 / 审计 / 预算)、**可长跑**(LongAgent 阶段并行编排)。
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 目录
|
|
12
|
+
|
|
13
|
+
- [kkcode](#kkcode)
|
|
14
|
+
- [目录](#目录)
|
|
15
|
+
- [快速开始](#快速开始)
|
|
16
|
+
- [环境要求](#环境要求)
|
|
17
|
+
- [安装与运行](#安装与运行)
|
|
18
|
+
- [初始化项目配置](#初始化项目配置)
|
|
19
|
+
- [模式系统](#模式系统)
|
|
20
|
+
- [LongAgent 编排](#longagent-编排)
|
|
21
|
+
- [主流程](#主流程)
|
|
22
|
+
- [并行与一致性](#并行与一致性)
|
|
23
|
+
- [主动规划](#主动规划)
|
|
24
|
+
- [权限系统](#权限系统)
|
|
25
|
+
- [内置工具](#内置工具)
|
|
26
|
+
- [子智能体](#子智能体)
|
|
27
|
+
- [Auto Memory](#auto-memory)
|
|
28
|
+
- [MCP 接入](#mcp-接入)
|
|
29
|
+
- [GitHub 集成](#github-集成)
|
|
30
|
+
- [登录 GitHub](#登录-github)
|
|
31
|
+
- [登出 GitHub](#登出-github)
|
|
32
|
+
- [工作流程](#工作流程)
|
|
33
|
+
- [完成工作后推送](#完成工作后推送)
|
|
34
|
+
- [扩展机制](#扩展机制)
|
|
35
|
+
- [会话与审计](#会话与审计)
|
|
36
|
+
- [TUI 交互](#tui-交互)
|
|
37
|
+
- [状态栏](#状态栏)
|
|
38
|
+
- [快捷键](#快捷键)
|
|
39
|
+
- [常用 Slash 命令](#常用-slash-命令)
|
|
40
|
+
- [项目结构](#项目结构)
|
|
41
|
+
- [配置示例](#配置示例)
|
|
42
|
+
- [常见问题](#常见问题)
|
|
43
|
+
- [致谢](#致谢)
|
|
44
|
+
- [License](#license)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 快速开始
|
|
49
|
+
|
|
50
|
+
### 环境要求
|
|
51
|
+
|
|
52
|
+
- Node.js `>=22`
|
|
53
|
+
- npm / pnpm
|
|
54
|
+
- 建议终端:Windows Terminal、iTerm2 或现代 Linux terminal
|
|
55
|
+
|
|
56
|
+
### 安装与运行
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
cd kkcode
|
|
60
|
+
npm install
|
|
61
|
+
npm run start
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
全局链接(开发常用):
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npm link
|
|
68
|
+
kkcode
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 初始化项目配置
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
kkcode init -y
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
配置文件按优先级自动查找:
|
|
78
|
+
|
|
79
|
+
- 用户级:`~/.kkcode/config.yaml`
|
|
80
|
+
- 项目级:`./kkcode.config.yaml` 或 `./.kkcode/config.yaml`
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 模式系统
|
|
85
|
+
|
|
86
|
+
| 模式 | 目标 | 工具权限 | 典型场景 |
|
|
87
|
+
|---|---|---|---|
|
|
88
|
+
| `ask` | 问答 / 解释 | 只读 | 理解代码、解释报错 |
|
|
89
|
+
| `plan` | 方案拆解 | 只读 | 先出执行计划 |
|
|
90
|
+
| `agent` | 单轮执行 | 全工具 | 快速改代码 + 运行命令 |
|
|
91
|
+
| `longagent` | 长程编排 | 全工具 + 调度 | 跨文件 / 多阶段任务 |
|
|
92
|
+
|
|
93
|
+
TUI 中按 `Tab` 一键轮换模式。
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## LongAgent 编排
|
|
98
|
+
|
|
99
|
+
LongAgent 是 kkcode 的核心差异能力,支持"意图识别 + 阶段并行 + 门禁闭环"。
|
|
100
|
+
|
|
101
|
+
### 主流程
|
|
102
|
+
|
|
103
|
+
1. **意图识别** — 非编码目标直接阻断,避免空转
|
|
104
|
+
2. **Intake** — 多轮澄清并生成摘要
|
|
105
|
+
3. **Plan Frozen** — 冻结阶段计划(StagePlan)
|
|
106
|
+
4. **Stage Barrier** — 同阶段任务并发执行,全部终态后推进
|
|
107
|
+
5. **Recovery** — 失败任务重试,优先续写 remaining files
|
|
108
|
+
6. **Usability Gates** — build / test / review / health / budget 全量校验
|
|
109
|
+
7. **完成判定** — 门禁通过 + 完成标记
|
|
110
|
+
|
|
111
|
+
### 并行与一致性
|
|
112
|
+
|
|
113
|
+
- 同阶段并发由 `max_concurrency` 控制
|
|
114
|
+
- 子任务由后台 worker 承载,主会话等待 barrier
|
|
115
|
+
- 写入工具支持 file lock,避免并发冲突
|
|
116
|
+
- TUI 实时显示阶段状态、进度条与文件变动摘要
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## 主动规划
|
|
121
|
+
|
|
122
|
+
Agent 在执行过程中可**主动进入规划模式**,无需用户手动切换。
|
|
123
|
+
|
|
124
|
+
1. Agent 遇到复杂任务时调用 `enter_plan`
|
|
125
|
+
2. 在规划模式下分析代码、设计方案(理解 → 设计 → 审查 → 最终计划)
|
|
126
|
+
3. 调用 `exit_plan` 提交计划,TUI 弹出审批面板
|
|
127
|
+
4. 用户 **Approve** 或 **Reject**(可附反馈)
|
|
128
|
+
5. 批准后按计划执行,驳回则修订
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 权限系统
|
|
133
|
+
|
|
134
|
+
采用 **策略 + 交互审批 + 会话缓存授权** 组合模型。
|
|
135
|
+
|
|
136
|
+
- `permission.default_policy`: `ask | allow | deny`
|
|
137
|
+
- `permission.rules[]`: 按工具 / 模式 / 命令前缀匹配细粒度规则
|
|
138
|
+
- 审批一次后同会话内同类操作自动放行
|
|
139
|
+
|
|
140
|
+
TUI 审批面板:`1` allow once / `2` allow session / `3` deny / `Esc` deny
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## 内置工具
|
|
145
|
+
|
|
146
|
+
| 工具 | 说明 |
|
|
147
|
+
|------|------|
|
|
148
|
+
| `read` | 读取文件内容(支持 offset/limit 分页) |
|
|
149
|
+
| `write` | 原子写文件(支持 overwrite / append / insert 模式) |
|
|
150
|
+
| `edit` | 事务性字符串替换 + 自动回滚 |
|
|
151
|
+
| `patch` | 按行号范围替换文件内容 |
|
|
152
|
+
| `glob` | 按模式搜索文件 |
|
|
153
|
+
| `grep` | 按正则搜索文件内容(支持行号、分页) |
|
|
154
|
+
| `bash` | 执行 shell 命令 |
|
|
155
|
+
| `task` | 委派子智能体执行子任务 |
|
|
156
|
+
| `todowrite` | 结构化任务管理 |
|
|
157
|
+
| `question` | 向用户提问 |
|
|
158
|
+
| `enter_plan` | 主动进入规划模式 |
|
|
159
|
+
| `exit_plan` | 提交计划等待审批 |
|
|
160
|
+
| `webfetch` | 抓取网页内容 |
|
|
161
|
+
| `websearch` | Web 搜索 |
|
|
162
|
+
| `codesearch` | 代码搜索引擎 |
|
|
163
|
+
|
|
164
|
+
写入安全特性:原子写、事务回滚、外部修改检测、读前编辑约束、file lock 串行化。
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## 子智能体
|
|
169
|
+
|
|
170
|
+
通过 `task` 工具委派专项子智能体:
|
|
171
|
+
|
|
172
|
+
| 类型 | 说明 | 权限 |
|
|
173
|
+
|------|------|------|
|
|
174
|
+
| `build` | 通用构建执行 | 全工具 |
|
|
175
|
+
| `explore` | 快速代码探索 | 只读 |
|
|
176
|
+
| `reviewer` | 代码审查(bug / 安全 / 质量) | 只读 |
|
|
177
|
+
| `researcher` | 深度研究 + Web 搜索 | 只读 + 网络 |
|
|
178
|
+
|
|
179
|
+
支持通过 YAML/MJS 自定义子智能体,或 `/create-agent` 自动生成。
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## Auto Memory
|
|
184
|
+
|
|
185
|
+
为每个项目维护独立持久记忆,跨会话保存项目知识。
|
|
186
|
+
|
|
187
|
+
- 存储位置:`~/.kkcode/projects/<项目名>_<hash>/memory/MEMORY.md`
|
|
188
|
+
- 每次会话启动时自动注入系统提示词(限 200 行)
|
|
189
|
+
- Agent 可直接读写记忆文件
|
|
190
|
+
- 状态栏显示 `MEM` 徽章表示已加载
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## MCP 接入
|
|
195
|
+
|
|
196
|
+
支持三种传输协议:
|
|
197
|
+
|
|
198
|
+
| 传输 | 适用场景 | 关键字段 |
|
|
199
|
+
|------|----------|----------|
|
|
200
|
+
| `stdio` | 本地子进程 | `command`, `args` |
|
|
201
|
+
| `sse` | 远程 Streamable HTTP | `url`, `headers` |
|
|
202
|
+
| `http` | REST 风格 | `url`, `headers` |
|
|
203
|
+
|
|
204
|
+
自动发现:合并 `.mcp.json`、`.kkcode/mcp.json`、`~/.kkcode/mcp.json` 中的配置。
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
kkcode mcp test # 健康状态
|
|
208
|
+
kkcode mcp tools # 可用工具列表
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## GitHub 集成
|
|
214
|
+
|
|
215
|
+
通过 GitHub Device Flow 实现安全的仓库访问,无需手动配置 Token。
|
|
216
|
+
|
|
217
|
+
### 登录 GitHub
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
kkcode --github
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
执行后:
|
|
224
|
+
1. **自动打开浏览器** — 弹窗授权页面
|
|
225
|
+
2. **自动复制验证码** — 剪贴板已填充设备码,直接粘贴即可
|
|
226
|
+
3. **自动轮询授权状态** — 无需手动操作,等待完成
|
|
227
|
+
4. **增强网络容错** — 支持不稳定网络环境,自动重试
|
|
228
|
+
|
|
229
|
+
### 登出 GitHub
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
kkcode --github logout
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
清除本地存储的 GitHub Token。
|
|
236
|
+
|
|
237
|
+
### 工作流程
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
kkcode --github # 启动 GitHub 登录流程
|
|
241
|
+
# 选择仓库 → 选择分支 → 选择本地/云端操作模式
|
|
242
|
+
# 进入 REPL 开始编码
|
|
243
|
+
# 退出 REPL 后自动询问是否推送更改到云端
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**本地/云端操作选项:**
|
|
247
|
+
|
|
248
|
+
| 选项 | 说明 |
|
|
249
|
+
|------|------|
|
|
250
|
+
| **使用本地仓库** | 直接使用本地已有代码,不拉取更新 |
|
|
251
|
+
| **同步云端代码** | 执行 `git pull` 拉取最新代码 |
|
|
252
|
+
| **强制重新克隆** | 删除本地仓库,重新完整克隆 |
|
|
253
|
+
|
|
254
|
+
### 完成工作后推送
|
|
255
|
+
|
|
256
|
+
退出 REPL 时自动询问是否推送更改:
|
|
257
|
+
|
|
258
|
+
```
|
|
259
|
+
🚀 检测到本地更改,是否推送至云端?(y/N)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## 扩展机制
|
|
265
|
+
|
|
266
|
+
| 扩展类型 | 目录 | 说明 |
|
|
267
|
+
|----------|------|------|
|
|
268
|
+
| 命令模板 | `.kkcode/commands/` | Markdown 模板,支持变量替换 |
|
|
269
|
+
| 技能 | `.kkcode/skills/` | 可编程技能,`/create-skill` 生成 |
|
|
270
|
+
| 子智能体 | `.kkcode/agents/` | YAML/MJS 定义,`/create-agent` 生成 |
|
|
271
|
+
| 自定义工具 | `.kkcode/tools/` | .mjs 自动加载,`/reload` 热更新 |
|
|
272
|
+
| 插件/Hook | `.kkcode/plugins/` | Hook 事件脚本 |
|
|
273
|
+
| 规则 | `.kkcode/rules/` | 项目级提示词规则 |
|
|
274
|
+
| 指令文件 | `KKCODE.md` | 项目级指令,自动注入提示词 |
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## 会话与审计
|
|
279
|
+
|
|
280
|
+
- 会话存储:`~/.kkcode/sessions/`
|
|
281
|
+
- 审计日志:`~/.kkcode/audit-log.json`
|
|
282
|
+
- 事件日志:`~/.kkcode/events.log`
|
|
283
|
+
- 三层用量追踪:turn / session / global
|
|
284
|
+
- 预算策略:`warn | block`
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
kkcode doctor --json # 完整诊断
|
|
288
|
+
kkcode session list # 会话列表
|
|
289
|
+
kkcode session fsck # 一致性检查
|
|
290
|
+
kkcode session gc # 过期清理
|
|
291
|
+
kkcode usage show # 用量统计
|
|
292
|
+
kkcode longagent status # LongAgent 状态
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## TUI 交互
|
|
298
|
+
|
|
299
|
+
### 状态栏
|
|
300
|
+
|
|
301
|
+
底部实时显示:`MODE` `MODEL` `TOKENS` `COST` `CONTEXT` `MEM` `PERMISSION` `LONG`
|
|
302
|
+
|
|
303
|
+
- **CONTEXT** 85%+ 红色告警
|
|
304
|
+
- **COST** 含缓存节省额(如 `↓$0.03`)
|
|
305
|
+
|
|
306
|
+
### 快捷键
|
|
307
|
+
|
|
308
|
+
| 按键 | 功能 |
|
|
309
|
+
|------|------|
|
|
310
|
+
| `Enter` | 发送 |
|
|
311
|
+
| `Ctrl+J` | 换行 |
|
|
312
|
+
| `Tab` | 模式轮换 |
|
|
313
|
+
| `/paste` | 粘贴剪贴板图片 |
|
|
314
|
+
| `Ctrl+Up/Down` | 滚动日志区 |
|
|
315
|
+
| `Ctrl+Home/End` | 跳转日志首尾 |
|
|
316
|
+
| `Up/Down` | 输入历史 |
|
|
317
|
+
| `Esc` | 清空输入 |
|
|
318
|
+
| `Ctrl+L` | 清空活动区 |
|
|
319
|
+
| `Ctrl+C` | 退出 |
|
|
320
|
+
|
|
321
|
+
支持 `@图片路径` 或 `@图片URL` 引用多模态输入。
|
|
322
|
+
|
|
323
|
+
### 常用 Slash 命令
|
|
324
|
+
|
|
325
|
+
`/help` `/status` `/history` `/new` `/resume` `/mode` `/provider` `/model` `/permission` `/paste` `/commands` `/reload` `/clear` `/exit`
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## 项目结构
|
|
330
|
+
|
|
331
|
+
```
|
|
332
|
+
kkcode/
|
|
333
|
+
├── src/
|
|
334
|
+
│ ├── index.mjs # CLI 入口
|
|
335
|
+
│ ├── repl.mjs # TUI 主循环(输入/渲染/键盘处理)
|
|
336
|
+
│ ├── runtime.mjs # 运行时初始化
|
|
337
|
+
│ ├── context.mjs # 全局上下文构建
|
|
338
|
+
│ ├── core/ # 核心类型、常量、事件总线
|
|
339
|
+
│ ├── config/ # 配置加载、Schema 校验、默认值
|
|
340
|
+
│ ├── session/ # 会话引擎、消息循环、系统提示词
|
|
341
|
+
│ │ ├── loop.mjs # Agent 主循环(流式处理/自动续写)
|
|
342
|
+
│ │ ├── engine.mjs # 会话引擎
|
|
343
|
+
│ │ ├── longagent.mjs # LongAgent 编排调度
|
|
344
|
+
│ │ ├── longagent-plan.mjs # 阶段计划生成与冻结
|
|
345
|
+
│ │ ├── compaction.mjs # 上下文压缩
|
|
346
|
+
│ │ ├── instinct-manager.mjs # Instinct 自动学习
|
|
347
|
+
│ │ └── prompt/ # 各 Provider 系统提示词模板
|
|
348
|
+
│ ├── tool/ # 工具注册、执行、事务管理
|
|
349
|
+
│ │ ├── registry.mjs # 内置工具定义(read/write/edit/patch/...)
|
|
350
|
+
│ │ ├── edit-transaction.mjs # 编辑事务与原子写入
|
|
351
|
+
│ │ ├── task-tool.mjs # task 子任务委派
|
|
352
|
+
│ │ ├── image-util.mjs # 图片处理与剪贴板读取
|
|
353
|
+
│ │ └── prompt/ # 工具使用提示词
|
|
354
|
+
│ ├── agent/ # 子智能体(explore/reviewer/researcher)
|
|
355
|
+
│ ├── provider/ # 多 Provider 适配(Anthropic/OpenAI/Ollama/...)
|
|
356
|
+
│ ├── permission/ # 权限引擎与工作区信任
|
|
357
|
+
│ ├── mcp/ # MCP 客户端(stdio/sse/http)
|
|
358
|
+
│ ├── skill/ # 技能注册与生成
|
|
359
|
+
│ ├── command/ # 自定义命令加载
|
|
360
|
+
│ ├── commands/ # CLI 子命令(doctor/init/theme/...)
|
|
361
|
+
│ ├── orchestration/ # 后台任务管理与并行 worker
|
|
362
|
+
│ ├── plugin/ # Hook 事件总线
|
|
363
|
+
│ ├── ui/ # Dashboard 渲染、活动日志
|
|
364
|
+
│ ├── theme/ # 主题系统、状态栏、颜色
|
|
365
|
+
│ ├── storage/ # 会话/任务持久化
|
|
366
|
+
│ ├── usage/ # Token 计量与预算控制
|
|
367
|
+
│ ├── review/ # Diff 审查工作流
|
|
368
|
+
│ ├── rules/ # 规则文件加载
|
|
369
|
+
│ ├── knowledge/ # 知识库
|
|
370
|
+
│ └── util/ # 通用工具函数(git/markdown/...)
|
|
371
|
+
├── templates/ # 模板文件(主题/定价/Hook 示例)
|
|
372
|
+
├── test/ # 单元测试与 E2E 测试
|
|
373
|
+
├── kkcode.config.yaml # 项目默认配置
|
|
374
|
+
├── package.json # Node.js 包定义
|
|
375
|
+
├── LICENSE # GPL-3.0
|
|
376
|
+
└── SECURITY.md # 安全政策
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
381
|
+
## 配置示例
|
|
382
|
+
|
|
383
|
+
```yaml
|
|
384
|
+
provider:
|
|
385
|
+
default: anthropic
|
|
386
|
+
anthropic:
|
|
387
|
+
type: anthropic
|
|
388
|
+
base_url: https://api.anthropic.com/v1
|
|
389
|
+
api_key_env: ANTHROPIC_API_KEY
|
|
390
|
+
default_model: claude-sonnet-4-20250514
|
|
391
|
+
thinking:
|
|
392
|
+
type: enabled
|
|
393
|
+
budget_tokens: 10000
|
|
394
|
+
|
|
395
|
+
agent:
|
|
396
|
+
default_mode: agent
|
|
397
|
+
max_steps: 8
|
|
398
|
+
longagent:
|
|
399
|
+
parallel:
|
|
400
|
+
enabled: true
|
|
401
|
+
max_concurrency: 3
|
|
402
|
+
|
|
403
|
+
permission:
|
|
404
|
+
default_policy: ask
|
|
405
|
+
|
|
406
|
+
usage:
|
|
407
|
+
budget:
|
|
408
|
+
strategy: warn
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
完整配置参考见 [NOTICE.md](NOTICE.md)。
|
|
412
|
+
|
|
413
|
+
---
|
|
414
|
+
|
|
415
|
+
## 常见问题
|
|
416
|
+
|
|
417
|
+
**Q: LongAgent 为什么拒绝"你好"?**
|
|
418
|
+
LongAgent 是执行型编排器,非编码目标会被意图识别拦截。
|
|
419
|
+
|
|
420
|
+
**Q: enter_plan 和 /plan 有什么区别?**
|
|
421
|
+
`/plan` 是用户手动切换模式。`enter_plan` 是 agent 执行中主动进入规划,审批通过后继续执行。
|
|
422
|
+
|
|
423
|
+
**Q: Auto Memory 会无限增长吗?**
|
|
424
|
+
注入系统提示时限制 200 行。Agent 会保持精简并删除过时条目。
|
|
425
|
+
|
|
426
|
+
**Q: 大文件创建被截断怎么办?**
|
|
427
|
+
使用 `write(mode="append")` 分段追加创建,或用 `patch` 按行号范围修改。
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
|
|
431
|
+
## 致谢
|
|
432
|
+
|
|
433
|
+
kkcode 的设计受到以下项目的启发:
|
|
434
|
+
|
|
435
|
+
- **[Claude Code](https://github.com/anthropics/claude-code)** — Anthropic 官方 AI Coding CLI。工具体系、子智能体架构、提示词工程等核心设计以此为标杆。
|
|
436
|
+
- **[OpenCode](https://github.com/nicepkg/opencode)** — 开源终端 AI Coding 助手。多 Provider 支持、主题系统等借鉴了其设计。
|
|
437
|
+
- **[Everything Claude Code](https://github.com/affaan-m/everything-claude-code)** — Instinct 自动学习、Hook Recipes、TDD 工作流等能力受此启发。
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
## License
|
|
442
|
+
|
|
443
|
+
本项目基于 [GNU General Public License v3.0](LICENSE) 开源。
|
|
444
|
+
|
|
445
|
+
Copyright (C) 2026 kkcode team
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kkelly-offical/kkcode",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Team-first terminal AI coding agent CLI with LongAgent orchestration, GitHub integration and themed UX.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"packageManager": "pnpm@10.5.2",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"author": "kkelly-offical",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/kkelly-offical/kkcode.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/kkelly-offical/kkcode",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/kkelly-offical/kkcode/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"ai",
|
|
19
|
+
"coding-agent",
|
|
20
|
+
"cli",
|
|
21
|
+
"terminal",
|
|
22
|
+
"github",
|
|
23
|
+
"longagent",
|
|
24
|
+
"code-review",
|
|
25
|
+
"mcp"
|
|
26
|
+
],
|
|
27
|
+
"bin": {
|
|
28
|
+
"kkcode": "./src/index.mjs"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src/",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=22"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"start": "node ./src/index.mjs",
|
|
39
|
+
"test": "node --test",
|
|
40
|
+
"test:e2e": "node --test test/e2e/"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"commander": "^13.1.0",
|
|
44
|
+
"yaml": "^2.8.1"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { fileURLToPath } from "node:url"
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
6
|
+
|
|
7
|
+
const registry = new Map()
|
|
8
|
+
|
|
9
|
+
function promptPath(name) {
|
|
10
|
+
return path.join(__dirname, "prompt", `${name}.txt`)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function loadPrompt(name) {
|
|
14
|
+
try {
|
|
15
|
+
return (await readFile(promptPath(name), "utf8")).trim()
|
|
16
|
+
} catch {
|
|
17
|
+
return ""
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function defineAgent(spec) {
|
|
22
|
+
const agent = {
|
|
23
|
+
name: spec.name,
|
|
24
|
+
description: spec.description || "",
|
|
25
|
+
mode: spec.mode || "primary",
|
|
26
|
+
permission: spec.permission || "default",
|
|
27
|
+
tools: spec.tools || null,
|
|
28
|
+
model: spec.model || null,
|
|
29
|
+
temperature: spec.temperature ?? null,
|
|
30
|
+
maxTurns: spec.maxTurns || null,
|
|
31
|
+
hidden: spec.hidden || false,
|
|
32
|
+
promptFile: spec.promptFile || spec.name,
|
|
33
|
+
_promptCache: spec._promptCache ?? null,
|
|
34
|
+
_customAgent: spec._customAgent || false,
|
|
35
|
+
_scope: spec._scope || null,
|
|
36
|
+
_source: spec._source || null
|
|
37
|
+
}
|
|
38
|
+
registry.set(agent.name, agent)
|
|
39
|
+
return agent
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function getAgentPrompt(name) {
|
|
43
|
+
const agent = registry.get(name)
|
|
44
|
+
if (!agent) return ""
|
|
45
|
+
if (agent._promptCache !== null) return agent._promptCache
|
|
46
|
+
agent._promptCache = await loadPrompt(agent.promptFile)
|
|
47
|
+
return agent._promptCache
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getAgent(name) {
|
|
51
|
+
return registry.get(name) || null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function listAgents({ includeHidden = false } = {}) {
|
|
55
|
+
const agents = [...registry.values()]
|
|
56
|
+
return includeHidden ? agents : agents.filter((a) => !a.hidden)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function resolveAgentForMode(mode) {
|
|
60
|
+
if (registry.has(mode)) return registry.get(mode)
|
|
61
|
+
const modeMap = { ask: "build", plan: "plan", agent: "build", longagent: "longagent" }
|
|
62
|
+
const mapped = modeMap[mode]
|
|
63
|
+
return mapped ? registry.get(mapped) || null : null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
defineAgent({
|
|
67
|
+
name: "build",
|
|
68
|
+
description: "Default agent with full tool access for code development",
|
|
69
|
+
mode: "primary",
|
|
70
|
+
permission: "full",
|
|
71
|
+
tools: null
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
defineAgent({
|
|
75
|
+
name: "plan",
|
|
76
|
+
description: "Read-only analysis agent, no file editing allowed",
|
|
77
|
+
mode: "primary",
|
|
78
|
+
permission: "readonly",
|
|
79
|
+
tools: ["read", "glob", "grep", "list", "bash"]
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
defineAgent({
|
|
83
|
+
name: "explore",
|
|
84
|
+
description: "Fast file search subagent for codebase exploration",
|
|
85
|
+
mode: "subagent",
|
|
86
|
+
permission: "readonly",
|
|
87
|
+
tools: ["read", "glob", "grep", "list", "bash"]
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
defineAgent({
|
|
91
|
+
name: "longagent",
|
|
92
|
+
description: "Persistent iterative execution agent for complex multi-step tasks",
|
|
93
|
+
mode: "primary",
|
|
94
|
+
permission: "full",
|
|
95
|
+
tools: null
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
defineAgent({
|
|
99
|
+
name: "reviewer",
|
|
100
|
+
description: "Code review specialist for analyzing code quality, bugs, and security issues",
|
|
101
|
+
mode: "subagent",
|
|
102
|
+
permission: "readonly",
|
|
103
|
+
tools: ["read", "glob", "grep", "list", "bash"]
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
defineAgent({
|
|
107
|
+
name: "researcher",
|
|
108
|
+
description: "Deep codebase research and web-augmented exploration agent",
|
|
109
|
+
mode: "subagent",
|
|
110
|
+
permission: "readonly",
|
|
111
|
+
tools: ["read", "glob", "grep", "list", "bash", "websearch", "codesearch", "webfetch"]
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
defineAgent({
|
|
115
|
+
name: "architect",
|
|
116
|
+
description: "Feature architecture designer. Analyzes codebase patterns, designs implementation blueprints with specific files, component designs, data flows.",
|
|
117
|
+
mode: "subagent",
|
|
118
|
+
permission: "readonly",
|
|
119
|
+
tools: ["read", "glob", "grep", "list", "bash"]
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
defineAgent({
|
|
123
|
+
name: "guide",
|
|
124
|
+
description: "kkcode self-help guide. Answers questions about kkcode features, tools, configuration, modes, skills, hooks, MCP servers, and usage patterns by searching the kkcode source code.",
|
|
125
|
+
mode: "subagent",
|
|
126
|
+
permission: "readonly",
|
|
127
|
+
tools: ["read", "glob", "grep", "list", "webfetch", "websearch"]
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
defineAgent({
|
|
131
|
+
name: "security-reviewer",
|
|
132
|
+
description: "Security audit specialist. Performs OWASP Top 10 checks, hardcoded secret scans, dependency audits, and authentication/authorization reviews.",
|
|
133
|
+
mode: "subagent",
|
|
134
|
+
permission: "readonly",
|
|
135
|
+
tools: ["read", "glob", "grep", "list", "bash"]
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
defineAgent({
|
|
139
|
+
name: "tdd-guide",
|
|
140
|
+
description: "TDD specialist. Guides and executes test-driven development: scaffold interfaces, write failing tests (RED), implement minimum code (GREEN), refactor (IMPROVE). Targets 80%+ coverage.",
|
|
141
|
+
mode: "subagent",
|
|
142
|
+
permission: "full",
|
|
143
|
+
tools: ["read", "write", "edit", "bash", "glob", "grep", "list"]
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
defineAgent({
|
|
147
|
+
name: "build-fixer",
|
|
148
|
+
description: "Build error diagnosis and repair. Analyzes build failures, identifies root causes, applies fixes, and verifies the build succeeds. Supports TypeScript, Python, Go, Rust, Java.",
|
|
149
|
+
mode: "subagent",
|
|
150
|
+
permission: "full",
|
|
151
|
+
tools: ["read", "write", "edit", "bash", "glob", "grep", "list"]
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
defineAgent({
|
|
155
|
+
name: "compaction",
|
|
156
|
+
description: "Conversation summarizer for context compression",
|
|
157
|
+
mode: "subagent",
|
|
158
|
+
permission: "none",
|
|
159
|
+
tools: [],
|
|
160
|
+
hidden: true
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
defineAgent({
|
|
164
|
+
name: "title",
|
|
165
|
+
description: "Session title generator",
|
|
166
|
+
mode: "subagent",
|
|
167
|
+
permission: "none",
|
|
168
|
+
tools: [],
|
|
169
|
+
hidden: true
|
|
170
|
+
})
|