@kkelly-offical/kkcode 0.1.7 → 0.2.1
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 -674
- package/README.md +452 -387
- package/package.json +50 -46
- package/src/agent/agent.mjs +228 -220
- package/src/agent/custom-agent-loader.mjs +6 -3
- package/src/agent/generator.mjs +2 -2
- package/src/agent/prompt/assistant.txt +12 -0
- package/src/agent/prompt/bug-hunter.txt +89 -89
- package/src/agent/prompt/frontend-designer.txt +58 -58
- package/src/agent/prompt/guide.txt +1 -1
- package/src/agent/prompt/longagent-blueprint-agent.txt +83 -83
- package/src/agent/prompt/longagent-coding-agent.txt +37 -37
- package/src/agent/prompt/longagent-debugging-agent.txt +46 -46
- package/src/agent/prompt/longagent-preview-agent.txt +63 -63
- package/src/command/custom-commands.mjs +2 -2
- package/src/commands/agent.mjs +1 -1
- package/src/commands/background.mjs +145 -4
- package/src/commands/chat.mjs +117 -76
- package/src/commands/config.mjs +148 -1
- package/src/commands/doctor.mjs +30 -6
- package/src/commands/init.mjs +32 -6
- package/src/commands/longagent.mjs +117 -0
- package/src/commands/mcp.mjs +275 -43
- package/src/commands/permission.mjs +1 -1
- package/src/commands/session.mjs +195 -140
- package/src/commands/skill.mjs +63 -0
- package/src/commands/theme.mjs +1 -1
- package/src/config/defaults.mjs +280 -260
- package/src/config/import-config.mjs +1 -1
- package/src/config/load-config.mjs +61 -4
- package/src/config/schema.mjs +591 -574
- package/src/context.mjs +4 -1
- package/src/core/constants.mjs +97 -91
- package/src/core/types.mjs +1 -1
- package/src/github/api.mjs +78 -78
- package/src/github/auth.mjs +294 -286
- package/src/github/flow.mjs +298 -298
- package/src/github/workspace.mjs +225 -212
- package/src/index.mjs +84 -82
- package/src/knowledge/frontend-aesthetics.txt +38 -38
- package/src/mcp/client-http.mjs +139 -141
- package/src/mcp/client-sse.mjs +297 -288
- package/src/mcp/client-stdio.mjs +534 -533
- package/src/mcp/constants.mjs +2 -2
- package/src/mcp/registry.mjs +498 -479
- package/src/mcp/stdio-framing.mjs +135 -133
- package/src/mcp/tool-result.mjs +24 -24
- package/src/observability/edit-diagnostics.mjs +449 -0
- package/src/observability/index.mjs +42 -42
- package/src/observability/metrics.mjs +165 -137
- package/src/observability/tracer.mjs +137 -137
- package/src/onboarding.mjs +209 -0
- package/src/orchestration/background-manager.mjs +567 -372
- package/src/orchestration/background-worker.mjs +419 -305
- package/src/orchestration/interruption-reason.mjs +21 -0
- package/src/orchestration/longagent-manager.mjs +197 -171
- package/src/orchestration/stage-scheduler.mjs +733 -728
- package/src/orchestration/subagent-router.mjs +7 -1
- package/src/orchestration/task-scheduler.mjs +219 -7
- package/src/permission/engine.mjs +1 -1
- package/src/permission/exec-policy.mjs +370 -370
- package/src/permission/file-edit-policy.mjs +108 -0
- package/src/permission/prompt.mjs +1 -1
- package/src/permission/rules.mjs +116 -7
- package/src/plugin/builtin-hooks/post-edit-format.mjs +2 -1
- package/src/plugin/builtin-hooks/post-edit-typecheck.mjs +104 -40
- package/src/plugin/hook-bus.mjs +19 -5
- package/src/plugin/manifest-loader.mjs +222 -0
- package/src/provider/anthropic.mjs +396 -390
- package/src/provider/ollama.mjs +7 -1
- package/src/provider/openai.mjs +382 -340
- package/src/provider/retry-policy.mjs +74 -68
- package/src/provider/router.mjs +242 -241
- package/src/provider/sse.mjs +104 -104
- package/src/provider/wizard.mjs +556 -0
- package/src/repl/capability-facade.mjs +30 -0
- package/src/repl/command-surface.mjs +23 -0
- package/src/repl/controller-entry.mjs +40 -0
- package/src/repl/core-shell.mjs +208 -0
- package/src/repl/dialog-router.mjs +87 -0
- package/src/repl/input-engine.mjs +76 -0
- package/src/repl/keymap.mjs +7 -0
- package/src/repl/operator-surface.mjs +15 -0
- package/src/repl/permission-flow.mjs +49 -0
- package/src/repl/runtime-facade.mjs +36 -0
- package/src/repl/slash-router.mjs +62 -0
- package/src/repl/state-store.mjs +29 -0
- package/src/repl/turn-controller.mjs +58 -0
- package/src/repl/verification.mjs +23 -0
- package/src/repl.mjs +3368 -2981
- package/src/rules/load-rules.mjs +3 -3
- package/src/runtime.mjs +1 -1
- package/src/session/agent-transaction.mjs +86 -0
- package/src/session/checkpoint.mjs +302 -302
- package/src/session/compaction.mjs +298 -298
- package/src/session/engine.mjs +417 -232
- package/src/session/longagent-4stage.mjs +467 -460
- package/src/session/longagent-hybrid.mjs +1344 -1097
- package/src/session/longagent-plan.mjs +376 -365
- package/src/session/longagent-project-memory.mjs +53 -53
- package/src/session/longagent-scaffold.mjs +291 -291
- package/src/session/longagent-task-bus.mjs +138 -54
- package/src/session/longagent-utils.mjs +828 -472
- package/src/session/longagent.mjs +911 -900
- package/src/session/loop.mjs +1005 -930
- package/src/session/prompt/agent.txt +25 -25
- package/src/session/prompt/anthropic.txt +150 -150
- package/src/session/prompt/beast.txt +1 -1
- package/src/session/prompt/plan.txt +31 -31
- package/src/session/prompt/qwen.txt +46 -46
- package/src/session/recovery.mjs +21 -0
- package/src/session/rollback.mjs +196 -195
- package/src/session/routing-observability.mjs +72 -0
- package/src/session/runtime-state.mjs +47 -0
- package/src/session/store.mjs +523 -519
- package/src/session/system-prompt.mjs +308 -273
- package/src/session/task-validator.mjs +267 -267
- package/src/session/usability-gates.mjs +2 -2
- package/src/skill/builtin/commit.mjs +64 -64
- package/src/skill/builtin/design.mjs +76 -76
- package/src/skill/generator.mjs +18 -2
- package/src/skill/registry.mjs +642 -390
- package/src/storage/audit-store.mjs +18 -11
- package/src/storage/event-log.mjs +7 -1
- package/src/storage/ghost-commit-store.mjs +243 -245
- package/src/storage/paths.mjs +13 -0
- package/src/theme/default-theme.mjs +1 -1
- package/src/theme/markdown.mjs +4 -0
- package/src/theme/schema.mjs +1 -1
- package/src/theme/status-bar.mjs +162 -158
- package/src/tool/audit-wrapper.mjs +18 -2
- package/src/tool/edit-transaction.mjs +23 -0
- package/src/tool/executor.mjs +26 -1
- package/src/tool/file-read-state.mjs +65 -0
- package/src/tool/git-auto.mjs +526 -526
- package/src/tool/git-full-auto.mjs +487 -478
- package/src/tool/mutation-guard.mjs +54 -0
- package/src/tool/prompt/edit.txt +3 -3
- package/src/tool/prompt/multiedit.txt +1 -0
- package/src/tool/prompt/notebookedit.txt +2 -1
- package/src/tool/prompt/patch.txt +25 -24
- package/src/tool/prompt/read.txt +3 -3
- package/src/tool/prompt/sysinfo.txt +29 -0
- package/src/tool/prompt/task.txt +66 -4
- package/src/tool/prompt/write.txt +2 -2
- package/src/tool/question-prompt.mjs +99 -93
- package/src/tool/registry.mjs +1701 -1343
- package/src/tool/task-tool.mjs +14 -6
- package/src/ui/activity-renderer.mjs +667 -664
- package/src/ui/repl-background-panel.mjs +7 -0
- package/src/ui/repl-capability-panel.mjs +9 -0
- package/src/ui/repl-dashboard.mjs +54 -4
- package/src/ui/repl-help.mjs +110 -0
- package/src/ui/repl-operator-panel.mjs +12 -0
- package/src/ui/repl-route-feedback.mjs +35 -0
- package/src/ui/repl-status-view.mjs +76 -0
- package/src/ui/repl-task-panel.mjs +5 -0
- package/src/ui/repl-transcript-panel.mjs +56 -0
- package/src/ui/repl-turn-summary.mjs +135 -0
- package/src/usage/pricing.mjs +122 -121
- package/src/usage/usage-meter.mjs +1 -0
- package/src/util/git.mjs +562 -519
- package/src/util/template.mjs +6 -1
|
@@ -23,19 +23,26 @@ export async function readAuditStore() {
|
|
|
23
23
|
return readJson(auditStorePath(), defaults())
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
let writeLock = Promise.resolve()
|
|
27
|
+
|
|
26
28
|
export async function appendAuditEntry(entry) {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
store.entries
|
|
29
|
+
const run = async () => {
|
|
30
|
+
const store = await readAuditStore()
|
|
31
|
+
store.entries.push({
|
|
32
|
+
id: `aud_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
33
|
+
createdAt: Date.now(),
|
|
34
|
+
...entry
|
|
35
|
+
})
|
|
36
|
+
if (store.entries.length > state.maxEntries) {
|
|
37
|
+
store.entries = store.entries.slice(-state.maxEntries)
|
|
38
|
+
}
|
|
39
|
+
store.updatedAt = Date.now()
|
|
40
|
+
await writeJson(auditStorePath(), store)
|
|
41
|
+
return store.entries[store.entries.length - 1]
|
|
35
42
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
43
|
+
const result = writeLock.then(run, run)
|
|
44
|
+
writeLock = result.then(() => undefined, () => undefined)
|
|
45
|
+
return result
|
|
39
46
|
}
|
|
40
47
|
|
|
41
48
|
export async function listAuditEntries(options = {}) {
|
|
@@ -43,11 +43,17 @@ async function cleanupOldLogs() {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
let lastCleanupAt = 0
|
|
47
|
+
const CLEANUP_INTERVAL_MS = 60 * 60 * 1000 // 1 hour
|
|
48
|
+
|
|
46
49
|
export async function appendEventLog(event) {
|
|
47
50
|
await ensureUserRoot()
|
|
48
51
|
await maybeRotate()
|
|
49
52
|
await appendFile(eventLogPath(), JSON.stringify(event) + "\n", "utf8")
|
|
50
|
-
|
|
53
|
+
if (Date.now() - lastCleanupAt > CLEANUP_INTERVAL_MS) {
|
|
54
|
+
lastCleanupAt = Date.now()
|
|
55
|
+
await cleanupOldLogs()
|
|
56
|
+
}
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
export async function eventLogStats() {
|
|
@@ -1,245 +1,243 @@
|
|
|
1
|
-
import path from "node:path"
|
|
2
|
-
import { mkdir, readdir, readFile, writeFile, unlink } from "node:fs/promises"
|
|
3
|
-
import { userRootDir } from "./paths.mjs"
|
|
4
|
-
|
|
5
|
-
const GHOST_COMMIT_DIR = "ghost-commits"
|
|
6
|
-
const MAX_GHOST_COMMITS_PER_REPO = 50 // 每个仓库最多保留的幽灵提交数
|
|
7
|
-
const GHOST_COMMIT_TTL_MS = 7 * 24 * 60 * 60 * 1000 // 7天过期
|
|
8
|
-
|
|
9
|
-
// 防止并发 cleanup 竞态:per-repo 锁
|
|
10
|
-
const cleanupLocks = new Map()
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Ghost Commit 存储管理
|
|
14
|
-
*
|
|
15
|
-
* 提供幽灵提交的持久化存储、查询、清理等功能。
|
|
16
|
-
* 存储位置: ~/.kkcode/ghost-commits/
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/** 获取幽灵提交存储目录 */
|
|
20
|
-
export function getGhostCommitDir() {
|
|
21
|
-
return path.join(userRootDir(), GHOST_COMMIT_DIR)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** 获取仓库的存储目录(基于 repoPath 的哈希) */
|
|
25
|
-
function getRepoDir(repoPath) {
|
|
26
|
-
// 使用简单的哈希来避免路径中的特殊字符问题
|
|
27
|
-
const hash = Buffer.from(repoPath).toString("base64url")
|
|
28
|
-
return path.join(getGhostCommitDir(), hash)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** 确保目录存在 */
|
|
32
|
-
async function ensureDir(dir) {
|
|
33
|
-
await mkdir(dir, { recursive: true })
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 保存幽灵提交元数据
|
|
38
|
-
*
|
|
39
|
-
* @param {Object} ghostCommit - 幽灵提交信息
|
|
40
|
-
* @param {string} ghostCommit.id - 唯一ID
|
|
41
|
-
* @param {string} ghostCommit.commitHash - Git commit hash
|
|
42
|
-
* @param {string} ghostCommit.repoPath - 仓库路径
|
|
43
|
-
* @param {string} ghostCommit.parentHash - 父提交hash
|
|
44
|
-
* @param {string} ghostCommit.message - 提交信息
|
|
45
|
-
* @param {number} ghostCommit.createdAt - 创建时间戳
|
|
46
|
-
* @param {string[]} ghostCommit.files - 包含的文件列表
|
|
47
|
-
*/
|
|
48
|
-
export async function saveGhostCommit(ghostCommit) {
|
|
49
|
-
const repoDir = getRepoDir(ghostCommit.repoPath)
|
|
50
|
-
await ensureDir(repoDir)
|
|
51
|
-
|
|
52
|
-
const filePath = path.join(repoDir, `${ghostCommit.id}.json`)
|
|
53
|
-
const data = {
|
|
54
|
-
...ghostCommit,
|
|
55
|
-
savedAt: Date.now()
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
await writeFile(filePath, JSON.stringify(data, null, 2), "utf8")
|
|
59
|
-
|
|
60
|
-
// 清理旧的幽灵提交
|
|
61
|
-
await cleanupOldGhostCommits(ghostCommit.repoPath)
|
|
62
|
-
|
|
63
|
-
return { ok: true, path: filePath }
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 加载幽灵提交元数据
|
|
68
|
-
*
|
|
69
|
-
* @param {string} repoPath - 仓库路径
|
|
70
|
-
* @param {string} ghostCommitId - 幽灵提交ID
|
|
71
|
-
* @returns {Promise<Object|null>}
|
|
72
|
-
*/
|
|
73
|
-
export async function loadGhostCommit(repoPath, ghostCommitId) {
|
|
74
|
-
const repoDir = getRepoDir(repoPath)
|
|
75
|
-
const filePath = path.join(repoDir, `${ghostCommitId}.json`)
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
const content = await readFile(filePath, "utf8")
|
|
79
|
-
return JSON.parse(content)
|
|
80
|
-
} catch {
|
|
81
|
-
return null
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 列出仓库的所有幽灵提交
|
|
87
|
-
*
|
|
88
|
-
* @param {string} repoPath - 仓库路径
|
|
89
|
-
* @param {Object} options - 选项
|
|
90
|
-
* @param {boolean} [options.includeExpired=false] - 是否包含已过期的
|
|
91
|
-
* @returns {Promise<Array<Object>>}
|
|
92
|
-
*/
|
|
93
|
-
export async function listGhostCommits(repoPath, options = {}) {
|
|
94
|
-
const { includeExpired = false } = options
|
|
95
|
-
const repoDir = getRepoDir(repoPath)
|
|
96
|
-
|
|
97
|
-
try {
|
|
98
|
-
const entries = await readdir(repoDir, { withFileTypes: true })
|
|
99
|
-
const commits = []
|
|
100
|
-
|
|
101
|
-
for (const entry of entries) {
|
|
102
|
-
if (!entry.isFile() || !entry.name.endsWith(".json")) continue
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
const content = await readFile(path.join(repoDir, entry.name), "utf8")
|
|
106
|
-
const commit = JSON.parse(content)
|
|
107
|
-
|
|
108
|
-
// 检查是否过期
|
|
109
|
-
const isExpired = Date.now() - commit.createdAt > GHOST_COMMIT_TTL_MS
|
|
110
|
-
if (!includeExpired && isExpired) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* @
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
*
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return { total: commits.length, expired }
|
|
245
|
-
}
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import { mkdir, readdir, readFile, writeFile, unlink } from "node:fs/promises"
|
|
3
|
+
import { userRootDir } from "./paths.mjs"
|
|
4
|
+
|
|
5
|
+
const GHOST_COMMIT_DIR = "ghost-commits"
|
|
6
|
+
const MAX_GHOST_COMMITS_PER_REPO = 50 // 每个仓库最多保留的幽灵提交数
|
|
7
|
+
const GHOST_COMMIT_TTL_MS = 7 * 24 * 60 * 60 * 1000 // 7天过期
|
|
8
|
+
|
|
9
|
+
// 防止并发 cleanup 竞态:per-repo 锁
|
|
10
|
+
const cleanupLocks = new Map()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Ghost Commit 存储管理
|
|
14
|
+
*
|
|
15
|
+
* 提供幽灵提交的持久化存储、查询、清理等功能。
|
|
16
|
+
* 存储位置: ~/.kkcode/ghost-commits/
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** 获取幽灵提交存储目录 */
|
|
20
|
+
export function getGhostCommitDir() {
|
|
21
|
+
return path.join(userRootDir(), GHOST_COMMIT_DIR)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 获取仓库的存储目录(基于 repoPath 的哈希) */
|
|
25
|
+
function getRepoDir(repoPath) {
|
|
26
|
+
// 使用简单的哈希来避免路径中的特殊字符问题
|
|
27
|
+
const hash = Buffer.from(repoPath).toString("base64url")
|
|
28
|
+
return path.join(getGhostCommitDir(), hash)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 确保目录存在 */
|
|
32
|
+
async function ensureDir(dir) {
|
|
33
|
+
await mkdir(dir, { recursive: true })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 保存幽灵提交元数据
|
|
38
|
+
*
|
|
39
|
+
* @param {Object} ghostCommit - 幽灵提交信息
|
|
40
|
+
* @param {string} ghostCommit.id - 唯一ID
|
|
41
|
+
* @param {string} ghostCommit.commitHash - Git commit hash
|
|
42
|
+
* @param {string} ghostCommit.repoPath - 仓库路径
|
|
43
|
+
* @param {string} ghostCommit.parentHash - 父提交hash
|
|
44
|
+
* @param {string} ghostCommit.message - 提交信息
|
|
45
|
+
* @param {number} ghostCommit.createdAt - 创建时间戳
|
|
46
|
+
* @param {string[]} ghostCommit.files - 包含的文件列表
|
|
47
|
+
*/
|
|
48
|
+
export async function saveGhostCommit(ghostCommit) {
|
|
49
|
+
const repoDir = getRepoDir(ghostCommit.repoPath)
|
|
50
|
+
await ensureDir(repoDir)
|
|
51
|
+
|
|
52
|
+
const filePath = path.join(repoDir, `${ghostCommit.id}.json`)
|
|
53
|
+
const data = {
|
|
54
|
+
...ghostCommit,
|
|
55
|
+
savedAt: Date.now()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
await writeFile(filePath, JSON.stringify(data, null, 2), "utf8")
|
|
59
|
+
|
|
60
|
+
// 清理旧的幽灵提交
|
|
61
|
+
await cleanupOldGhostCommits(ghostCommit.repoPath)
|
|
62
|
+
|
|
63
|
+
return { ok: true, path: filePath }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 加载幽灵提交元数据
|
|
68
|
+
*
|
|
69
|
+
* @param {string} repoPath - 仓库路径
|
|
70
|
+
* @param {string} ghostCommitId - 幽灵提交ID
|
|
71
|
+
* @returns {Promise<Object|null>}
|
|
72
|
+
*/
|
|
73
|
+
export async function loadGhostCommit(repoPath, ghostCommitId) {
|
|
74
|
+
const repoDir = getRepoDir(repoPath)
|
|
75
|
+
const filePath = path.join(repoDir, `${ghostCommitId}.json`)
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const content = await readFile(filePath, "utf8")
|
|
79
|
+
return JSON.parse(content)
|
|
80
|
+
} catch {
|
|
81
|
+
return null
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 列出仓库的所有幽灵提交
|
|
87
|
+
*
|
|
88
|
+
* @param {string} repoPath - 仓库路径
|
|
89
|
+
* @param {Object} options - 选项
|
|
90
|
+
* @param {boolean} [options.includeExpired=false] - 是否包含已过期的
|
|
91
|
+
* @returns {Promise<Array<Object>>}
|
|
92
|
+
*/
|
|
93
|
+
export async function listGhostCommits(repoPath, options = {}) {
|
|
94
|
+
const { includeExpired = false } = options
|
|
95
|
+
const repoDir = getRepoDir(repoPath)
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const entries = await readdir(repoDir, { withFileTypes: true })
|
|
99
|
+
const commits = []
|
|
100
|
+
|
|
101
|
+
for (const entry of entries) {
|
|
102
|
+
if (!entry.isFile() || !entry.name.endsWith(".json")) continue
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
const content = await readFile(path.join(repoDir, entry.name), "utf8")
|
|
106
|
+
const commit = JSON.parse(content)
|
|
107
|
+
|
|
108
|
+
// 检查是否过期
|
|
109
|
+
const isExpired = Date.now() - commit.createdAt > GHOST_COMMIT_TTL_MS
|
|
110
|
+
if (!includeExpired && isExpired) {
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
commits.push({
|
|
115
|
+
...commit,
|
|
116
|
+
isExpired
|
|
117
|
+
})
|
|
118
|
+
} catch {
|
|
119
|
+
// 跳过无效文件
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 按创建时间降序排列
|
|
124
|
+
return commits.sort((a, b) => b.createdAt - a.createdAt)
|
|
125
|
+
} catch {
|
|
126
|
+
return []
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 删除幽灵提交
|
|
132
|
+
*
|
|
133
|
+
* @param {string} repoPath - 仓库路径
|
|
134
|
+
* @param {string} ghostCommitId - 幽灵提交ID
|
|
135
|
+
* @returns {Promise<boolean>}
|
|
136
|
+
*/
|
|
137
|
+
export async function deleteGhostCommit(repoPath, ghostCommitId) {
|
|
138
|
+
const repoDir = getRepoDir(repoPath)
|
|
139
|
+
const filePath = path.join(repoDir, `${ghostCommitId}.json`)
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
await unlink(filePath)
|
|
143
|
+
return true
|
|
144
|
+
} catch {
|
|
145
|
+
return false
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 清理旧的幽灵提交
|
|
151
|
+
* 保留最新的 MAX_GHOST_COMMITS_PER_REPO 个
|
|
152
|
+
*
|
|
153
|
+
* @param {string} repoPath - 仓库路径
|
|
154
|
+
*/
|
|
155
|
+
export async function cleanupOldGhostCommits(repoPath) {
|
|
156
|
+
// 同一 repo 的 cleanup 串行化,防止并发竞态
|
|
157
|
+
if (cleanupLocks.get(repoPath)) return
|
|
158
|
+
cleanupLocks.set(repoPath, true)
|
|
159
|
+
try {
|
|
160
|
+
const commits = await listGhostCommits(repoPath, { includeExpired: true })
|
|
161
|
+
|
|
162
|
+
if (commits.length <= MAX_GHOST_COMMITS_PER_REPO) {
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 删除多余的旧提交
|
|
167
|
+
const toDelete = commits.slice(MAX_GHOST_COMMITS_PER_REPO)
|
|
168
|
+
for (const commit of toDelete) {
|
|
169
|
+
await deleteGhostCommit(repoPath, commit.id)
|
|
170
|
+
}
|
|
171
|
+
} finally {
|
|
172
|
+
cleanupLocks.delete(repoPath)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 清理所有过期的幽灵提交
|
|
178
|
+
*
|
|
179
|
+
* @returns {Promise<{deleted: number}>}
|
|
180
|
+
*/
|
|
181
|
+
export async function cleanupAllExpired() {
|
|
182
|
+
const baseDir = getGhostCommitDir()
|
|
183
|
+
let deleted = 0
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const repoDirs = await readdir(baseDir, { withFileTypes: true })
|
|
187
|
+
|
|
188
|
+
for (const dir of repoDirs) {
|
|
189
|
+
if (!dir.isDirectory()) continue
|
|
190
|
+
|
|
191
|
+
const repoPath = path.join(baseDir, dir.name)
|
|
192
|
+
try {
|
|
193
|
+
const files = await readdir(repoPath)
|
|
194
|
+
|
|
195
|
+
for (const file of files) {
|
|
196
|
+
if (!file.endsWith(".json")) continue
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
const content = await readFile(path.join(repoPath, file), "utf8")
|
|
200
|
+
const commit = JSON.parse(content)
|
|
201
|
+
|
|
202
|
+
const isExpired = Date.now() - commit.createdAt > GHOST_COMMIT_TTL_MS
|
|
203
|
+
if (isExpired) {
|
|
204
|
+
await unlink(path.join(repoPath, file))
|
|
205
|
+
deleted++
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
// 跳过无效文件
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
} catch {
|
|
212
|
+
// 跳过无法读取的目录
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
} catch {
|
|
216
|
+
// 目录可能不存在
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return { deleted }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 获取最新的幽灵提交
|
|
224
|
+
*
|
|
225
|
+
* @param {string} repoPath - 仓库路径
|
|
226
|
+
* @returns {Promise<Object|null>}
|
|
227
|
+
*/
|
|
228
|
+
export async function getLatestGhostCommit(repoPath) {
|
|
229
|
+
const commits = await listGhostCommits(repoPath)
|
|
230
|
+
return commits[0] || null
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 统计幽灵提交数量
|
|
235
|
+
*
|
|
236
|
+
* @param {string} repoPath - 仓库路径
|
|
237
|
+
* @returns {Promise<{total: number, expired: number}>}
|
|
238
|
+
*/
|
|
239
|
+
export async function countGhostCommits(repoPath) {
|
|
240
|
+
const commits = await listGhostCommits(repoPath, { includeExpired: true })
|
|
241
|
+
const expired = commits.filter(c => c.isExpired).length
|
|
242
|
+
return { total: commits.length, expired }
|
|
243
|
+
}
|
package/src/storage/paths.mjs
CHANGED
|
@@ -34,6 +34,14 @@ export function projectConfigCandidates(cwd = process.cwd()) {
|
|
|
34
34
|
]
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
export function envFileCandidates(cwd = process.cwd()) {
|
|
38
|
+
return [
|
|
39
|
+
path.join(cwd, ".env"),
|
|
40
|
+
path.join(projectRootDir(cwd), ".env"),
|
|
41
|
+
path.join(userRootDir(), ".env")
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
export function usageStorePath() {
|
|
38
46
|
return path.join(userRootDir(), "usage.json")
|
|
39
47
|
}
|
|
@@ -146,3 +154,8 @@ export function githubReposDir() {
|
|
|
146
154
|
export async function ensureGithubReposDir() {
|
|
147
155
|
await mkdir(githubReposDir(), { recursive: true })
|
|
148
156
|
}
|
|
157
|
+
|
|
158
|
+
// User onboarding profile — stores tech stack, style preferences, etc.
|
|
159
|
+
export function profilePath() {
|
|
160
|
+
return path.join(userRootDir(), "profile.yaml")
|
|
161
|
+
}
|
package/src/theme/markdown.mjs
CHANGED
|
@@ -38,6 +38,10 @@ function renderLine(line) {
|
|
|
38
38
|
|
|
39
39
|
function renderInline(text) {
|
|
40
40
|
return text
|
|
41
|
+
// 先处理组合格式:**`code`** → 粗体+青色
|
|
42
|
+
.replace(/\*\*`([^`]+)`\*\*/g, (_, c) => paint(c, COLORS.code, { bold: true }))
|
|
43
|
+
.replace(/__`([^`]+)`__/g, (_, c) => paint(c, COLORS.code, { bold: true }))
|
|
44
|
+
// 再处理单独格式
|
|
41
45
|
.replace(/`([^`]+)`/g, (_, code) => paint(code, COLORS.code))
|
|
42
46
|
.replace(/\*\*([^*]+)\*\*/g, (_, b) => paint(b, null, { bold: true }))
|
|
43
47
|
.replace(/__([^_]+)__/g, (_, b) => paint(b, null, { bold: true }))
|
package/src/theme/schema.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const REQUIRED_GROUPS = ["base", "semantic", "modes", "components"]
|
|
2
|
-
const MODE_KEYS = ["
|
|
2
|
+
const MODE_KEYS = ["assistant", "plan", "agent", "longagent"]
|
|
3
3
|
const HEX_RE = /^#([A-Fa-f0-9]{6})$/
|
|
4
4
|
|
|
5
5
|
function validateColor(value, path, errors) {
|