@cat-kit/agent-context 1.0.7 → 1.1.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/README.md +58 -61
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/commands/done.js +2 -0
- package/dist/commands/done.js.map +1 -0
- package/dist/commands/install.js +2 -0
- package/dist/commands/install.js.map +1 -0
- package/dist/commands/printer.js +4 -0
- package/dist/commands/printer.js.map +1 -0
- package/dist/commands/status.js +2 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/sync.js +2 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/commands/validate.js +2 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/content/actions.js +177 -0
- package/dist/content/actions.js.map +1 -0
- package/dist/content/index.js +48 -0
- package/dist/content/index.js.map +1 -0
- package/dist/context/archiver.js +2 -0
- package/dist/context/archiver.js.map +1 -0
- package/dist/context/reader.js +2 -0
- package/dist/context/reader.js.map +1 -0
- package/dist/context/validator.js +2 -0
- package/dist/context/validator.js.map +1 -0
- package/dist/runner.js +3 -0
- package/dist/runner.js.map +1 -0
- package/dist/stats.html +1 -1
- package/dist/tools.js +2 -0
- package/dist/tools.js.map +1 -0
- package/package.json +2 -2
- package/src/cli.ts +33 -14
- package/src/commands/done.ts +55 -0
- package/src/commands/install.ts +56 -0
- package/src/commands/{shared.ts → printer.ts} +2 -3
- package/src/commands/status.ts +35 -0
- package/src/commands/sync.ts +40 -0
- package/src/commands/validate.ts +28 -0
- package/src/content/actions.ts +238 -0
- package/src/content/index.ts +94 -0
- package/src/context/archiver.ts +45 -0
- package/src/context/index.ts +3 -0
- package/src/context/reader.ts +77 -0
- package/src/context/validator.ts +44 -0
- package/src/runner.ts +113 -0
- package/src/tools.ts +114 -0
- package/src/types.ts +71 -0
- package/dist/adapters/tool-targets.js +0 -2
- package/dist/adapters/tool-targets.js.map +0 -1
- package/dist/commands/setup.js +0 -2
- package/dist/commands/setup.js.map +0 -1
- package/dist/commands/shared.js +0 -4
- package/dist/commands/shared.js.map +0 -1
- package/dist/commands/update.js +0 -2
- package/dist/commands/update.js.map +0 -1
- package/dist/domain/workflow-content.js +0 -2
- package/dist/domain/workflow-content.js.map +0 -1
- package/dist/domain/workflow-context.js +0 -2
- package/dist/domain/workflow-context.js.map +0 -1
- package/dist/domain/workflow-templates/done.js +0 -23
- package/dist/domain/workflow-templates/done.js.map +0 -1
- package/dist/domain/workflow-templates/implement.js +0 -35
- package/dist/domain/workflow-templates/implement.js.map +0 -1
- package/dist/domain/workflow-templates/init.js +0 -37
- package/dist/domain/workflow-templates/init.js.map +0 -1
- package/dist/domain/workflow-templates/patch.js +0 -41
- package/dist/domain/workflow-templates/patch.js.map +0 -1
- package/dist/domain/workflow-templates/plan.js +0 -58
- package/dist/domain/workflow-templates/plan.js.map +0 -1
- package/dist/domain/workflow-templates/replan.js +0 -31
- package/dist/domain/workflow-templates/replan.js.map +0 -1
- package/dist/generators/workflow.js +0 -2
- package/dist/generators/workflow.js.map +0 -1
- package/dist/runtime/execute.js +0 -2
- package/dist/runtime/execute.js.map +0 -1
- package/dist/shared/fs.js +0 -3
- package/dist/shared/fs.js.map +0 -1
- package/src/adapters/tool-targets.ts +0 -157
- package/src/commands/setup.ts +0 -64
- package/src/commands/update.ts +0 -46
- package/src/domain/types.ts +0 -50
- package/src/domain/workflow-content.ts +0 -25
- package/src/domain/workflow-context.ts +0 -48
- package/src/domain/workflow-templates/done.ts +0 -28
- package/src/domain/workflow-templates/implement.ts +0 -41
- package/src/domain/workflow-templates/index.ts +0 -7
- package/src/domain/workflow-templates/init.ts +0 -42
- package/src/domain/workflow-templates/patch.ts +0 -47
- package/src/domain/workflow-templates/plan.ts +0 -70
- package/src/domain/workflow-templates/replan.ts +0 -36
- package/src/generators/workflow.ts +0 -25
- package/src/runtime/execute.ts +0 -45
- package/src/shared/fs.ts +0 -66
- package/src/shared/paths.ts +0 -9
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
export const ACTION_NAMES = ['init', 'plan', 'replan', 'implement', 'patch', 'rush'] as const
|
|
2
|
+
type ActionName = (typeof ACTION_NAMES)[number]
|
|
3
|
+
|
|
4
|
+
export const ACTION_RENDERERS: Record<ActionName, () => string> = {
|
|
5
|
+
init: renderInit,
|
|
6
|
+
plan: renderPlan,
|
|
7
|
+
replan: renderReplan,
|
|
8
|
+
implement: renderImplement,
|
|
9
|
+
patch: renderPatch,
|
|
10
|
+
rush: renderRush
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function renderInit(): string {
|
|
14
|
+
return `# init
|
|
15
|
+
|
|
16
|
+
初始化项目的 Agent Context 上下文。根据新旧项目类型处理 \`AGENTS.md\`,新项目会自动进入计划创建流程。
|
|
17
|
+
|
|
18
|
+
可附带描述参数,用于补充项目背景、技术栈、特殊约束。
|
|
19
|
+
|
|
20
|
+
## 执行步骤
|
|
21
|
+
|
|
22
|
+
1. **判断项目类型**:检测根目录是否存在有效代码文件和目录结构。
|
|
23
|
+
- 新项目:无实质代码,或用户明确说明是新项目。
|
|
24
|
+
- 旧项目:已有代码、配置文件和目录结构。
|
|
25
|
+
- 无法判断 → 向用户提问澄清,不可假设。
|
|
26
|
+
|
|
27
|
+
2. **处理 AGENTS.md**:
|
|
28
|
+
- 新项目:
|
|
29
|
+
1. 通过提问澄清以下要点(已从描述中获取的可跳过):项目目标与核心功能、技术栈与版本、代码规范与工具链(lint/formatter/测试)、目录结构偏好。
|
|
30
|
+
2. 生成高质量 \`AGENTS.md\`(须满足下方质量标准,不满足则重新优化直至满足)。
|
|
31
|
+
3. 继续执行 plan 创建初始计划。
|
|
32
|
+
- 旧项目:
|
|
33
|
+
1. 若不存在 \`AGENTS.md\`:通过提问与代码阅读收集信息后生成。
|
|
34
|
+
2. 若已存在 \`AGENTS.md\`:按质量标准评估,不足时增补优化。
|
|
35
|
+
3. 默认不创建计划(除非用户明确要求)。
|
|
36
|
+
|
|
37
|
+
3. **输出反馈**:向用户报告项目类型判定结果、AGENTS.md 处理结果、是否需要后续计划。
|
|
38
|
+
|
|
39
|
+
## 高质量 AGENTS.md 标准
|
|
40
|
+
|
|
41
|
+
- 常用命令与约束前置,降低代理执行歧义。
|
|
42
|
+
- 明确列出目录结构(至少到第二层)。
|
|
43
|
+
- 明确标注技术栈及版本。
|
|
44
|
+
- 明确代码风格约束(命名规范、格式化工具、lint 配置)。
|
|
45
|
+
- 内容精简,无冗余段落或模板化填充。
|
|
46
|
+
- 大型单体仓库按子包拆分维护本地 \`AGENTS.md\`。
|
|
47
|
+
`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function renderPlan(): string {
|
|
51
|
+
return `# plan
|
|
52
|
+
|
|
53
|
+
创建新的执行计划,写入 \`.agent-context/plan-{number}/plan.md\`。按复杂度可拆分为多个计划,维护「单当前计划 + preparing 队列」结构。
|
|
54
|
+
|
|
55
|
+
必须附带计划描述。
|
|
56
|
+
|
|
57
|
+
## 前置检查
|
|
58
|
+
|
|
59
|
+
- 运行 \`agent-context validate\`,不通过则中止并报告错误。
|
|
60
|
+
- 描述为空 → 拒绝执行。
|
|
61
|
+
- 存在未归档的已执行当前计划 → 拒绝执行,提示先运行 \`agent-context done\` 归档。
|
|
62
|
+
- 存在多个当前计划 → 拒绝执行,提示恢复单活跃状态。
|
|
63
|
+
|
|
64
|
+
## 执行步骤
|
|
65
|
+
|
|
66
|
+
1. **需求澄清**(存在以下任一歧义时必须提问,否则跳到步骤 2):
|
|
67
|
+
- 范围边界不清:无法判定影响哪些文件或模块。
|
|
68
|
+
- 存在显著不同的技术路径需用户决策。
|
|
69
|
+
- 验收标准不明确:无法判断何时算"完成"。
|
|
70
|
+
2. 按复杂度决定单计划或多计划拆分。
|
|
71
|
+
3. 多计划拆分时:最小编号进入 \`.agent-context/\` 作为当前计划,其余进入 \`.agent-context/preparing/\`。单计划直接创建。
|
|
72
|
+
4. 每个计划创建 \`plan.md\`,遵循下方模板。
|
|
73
|
+
5. **自检**(不通过则修改后重新检查):
|
|
74
|
+
- 每个步骤可独立执行且有明确完成标准。
|
|
75
|
+
- 不存在过度拆分或拆分不足。
|
|
76
|
+
- 影响范围可预估。
|
|
77
|
+
|
|
78
|
+
## plan.md 模板
|
|
79
|
+
|
|
80
|
+
\`\`\`markdown
|
|
81
|
+
# {计划名称}
|
|
82
|
+
|
|
83
|
+
> 状态: 未执行
|
|
84
|
+
|
|
85
|
+
## 目标
|
|
86
|
+
|
|
87
|
+
{明确的目标描述}
|
|
88
|
+
|
|
89
|
+
## 内容
|
|
90
|
+
|
|
91
|
+
{详细的实施步骤}
|
|
92
|
+
|
|
93
|
+
## 影响范围
|
|
94
|
+
|
|
95
|
+
## 历史补丁
|
|
96
|
+
\`\`\`
|
|
97
|
+
|
|
98
|
+
- 状态行唯一,仅允许 \`未执行\` 或 \`已执行\`。
|
|
99
|
+
- \`## 目标\` 与 \`## 内容\` 不可为空。
|
|
100
|
+
- \`## 影响范围\` 与 \`## 历史补丁\` 创建时留空,后续由 implement 和 patch 写入。
|
|
101
|
+
`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function renderReplan(): string {
|
|
105
|
+
return `# replan
|
|
106
|
+
|
|
107
|
+
重新规划已有的未实施计划,保持「单当前计划 + preparing 队列」结构不变。
|
|
108
|
+
|
|
109
|
+
必须附带重规划描述。
|
|
110
|
+
|
|
111
|
+
## 前置检查
|
|
112
|
+
|
|
113
|
+
- 运行 \`agent-context validate\`,不通过则中止并报告错误。
|
|
114
|
+
- 描述为空 → 拒绝执行。
|
|
115
|
+
- 无未实施计划 → 拒绝执行,提示使用 plan 创建。
|
|
116
|
+
- 存在多个当前计划 → 拒绝执行,提示恢复单活跃状态。
|
|
117
|
+
- 目标计划编号不存在 → 拒绝执行,列出可选编号。
|
|
118
|
+
- 目标计划已执行 → 拒绝执行,提示使用 patch。
|
|
119
|
+
|
|
120
|
+
## 作用域
|
|
121
|
+
|
|
122
|
+
- 默认作用域:\`.agent-context/preparing/\` 中全部未实施计划。
|
|
123
|
+
- 可通过描述指定仅重规划部分计划(如"重规划 plan-3 和 plan-5")。
|
|
124
|
+
- 当前计划为 \`已执行\` → 禁止重写,仅允许重规划 \`preparing/\` 队列。
|
|
125
|
+
- 当前计划为 \`未执行\` 且用户明确要求 → 可纳入重规划范围。
|
|
126
|
+
|
|
127
|
+
## 执行步骤
|
|
128
|
+
|
|
129
|
+
1. 解析描述,确定重规划目标范围。
|
|
130
|
+
2. 读取目标计划 \`plan.md\`,理解现有意图。
|
|
131
|
+
3. 生成新的拆分方案,保持「单当前计划 + 若干 preparing 计划」结构。
|
|
132
|
+
4. 新增计划编号:全局 max(N)+1 递增分配;未改动计划保持原编号。
|
|
133
|
+
5. 更新目录结构,确保每个新计划的 \`plan.md\` 遵循标准模板。
|
|
134
|
+
`
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function renderImplement(): string {
|
|
138
|
+
return `# implement
|
|
139
|
+
|
|
140
|
+
实施当前计划 \`.agent-context/plan-{number}/plan.md\` 中的全部步骤,通过验证循环后将状态更新为「已执行」。
|
|
141
|
+
|
|
142
|
+
不接受额外描述。
|
|
143
|
+
|
|
144
|
+
## 前置检查
|
|
145
|
+
|
|
146
|
+
- 运行 \`agent-context validate\`,不通过则中止并报告错误。
|
|
147
|
+
- 带描述 → 拒绝执行。
|
|
148
|
+
- 当前计划不存在 → 拒绝执行,提示先创建计划。
|
|
149
|
+
- 当前计划状态为 \`已执行\` → 拒绝执行,提示使用 patch 修补或运行 \`agent-context done\` 归档。
|
|
150
|
+
- \`## 目标\` 或 \`## 内容\` 为空 → 拒绝执行,提示补充。
|
|
151
|
+
- 存在多个当前计划 → 拒绝执行,提示恢复单活跃状态。
|
|
152
|
+
- 仅操作当前计划,不直接操作 \`preparing/\` 中的计划。
|
|
153
|
+
- 遇到阻塞问题应向用户报告,不可静默跳过。
|
|
154
|
+
|
|
155
|
+
## 执行步骤
|
|
156
|
+
|
|
157
|
+
1. 读取当前计划 \`plan.md\`,理解 \`## 目标\` 与 \`## 内容\`。
|
|
158
|
+
2. 依据 \`## 内容\` 中的步骤逐项实施。
|
|
159
|
+
3. **验证循环**(全部通过前不可进入步骤 4):
|
|
160
|
+
a. 逐项对照 \`## 内容\` 确认每个步骤已实施。
|
|
161
|
+
b. 运行项目验证:类型检查 → lint → 测试。
|
|
162
|
+
c. 若存在失败项 → 修复后回到 a 重新验证。
|
|
163
|
+
d. 全部通过 → 进入步骤 4。
|
|
164
|
+
4. 更新 \`plan.md\` 状态行:\`> 状态: 未执行\` → \`> 状态: 已执行\`。
|
|
165
|
+
5. 更新 \`## 影响范围\`,记录所有变更文件。
|
|
166
|
+
`
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function renderPatch(): string {
|
|
170
|
+
return `# patch
|
|
171
|
+
|
|
172
|
+
基于当前已执行计划创建增量补丁,修复问题或追加变更。
|
|
173
|
+
|
|
174
|
+
必须附带补丁描述。
|
|
175
|
+
|
|
176
|
+
## 前置检查
|
|
177
|
+
|
|
178
|
+
- 运行 \`agent-context validate\`,不通过则中止并报告错误。
|
|
179
|
+
- 描述为空 → 拒绝执行。
|
|
180
|
+
- 当前计划不存在 → 拒绝执行,提示先创建计划。
|
|
181
|
+
- 当前计划状态为 \`未执行\` → 拒绝执行,提示先实施。
|
|
182
|
+
- 存在多个当前计划 → 拒绝执行,提示恢复单活跃状态。
|
|
183
|
+
- 补丁不改变计划状态,完成后保持 \`已执行\`。
|
|
184
|
+
|
|
185
|
+
## 执行步骤
|
|
186
|
+
|
|
187
|
+
1. 阅读 \`plan.md\` 与已有 \`patch-{number}.md\`,了解上下文与历史,避免重复修复。
|
|
188
|
+
2. 根据描述执行补丁所需的代码变更。
|
|
189
|
+
3. 完成必要验证(测试、类型检查等)。
|
|
190
|
+
4. 创建 \`patch-{number}.md\`(编号:扫描当前计划目录已有补丁取 max+1),遵循下方模板。
|
|
191
|
+
5. 回写 \`plan.md\`:
|
|
192
|
+
- \`## 历史补丁\`:追加 \`- patch-{number}: {补丁名称}\`,按编号去重。
|
|
193
|
+
- \`## 影响范围\`:合并本次变更路径,按路径去重。
|
|
194
|
+
|
|
195
|
+
## patch.md 模板
|
|
196
|
+
|
|
197
|
+
\`\`\`markdown
|
|
198
|
+
# {补丁名称}
|
|
199
|
+
|
|
200
|
+
## 补丁内容
|
|
201
|
+
|
|
202
|
+
{修改了什么、为什么修改}
|
|
203
|
+
|
|
204
|
+
## 影响范围
|
|
205
|
+
|
|
206
|
+
- 新增文件: \`/path/to/file\`
|
|
207
|
+
- 修改文件: \`/path/to/file\`
|
|
208
|
+
- 删除文件: \`/path/to/file\`
|
|
209
|
+
\`\`\`
|
|
210
|
+
`
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function renderRush(): string {
|
|
214
|
+
return `# rush
|
|
215
|
+
|
|
216
|
+
快速通道:创建计划并立即实施,适合范围明确、无需多轮规划的任务。
|
|
217
|
+
|
|
218
|
+
必须附带任务描述。
|
|
219
|
+
|
|
220
|
+
## 前置检查
|
|
221
|
+
|
|
222
|
+
- 运行 \`agent-context validate\`,不通过则中止并报告错误。
|
|
223
|
+
- 描述为空 → 拒绝执行。
|
|
224
|
+
- 存在未归档的已执行当前计划 → 拒绝执行,提示先运行 \`agent-context done\` 归档。
|
|
225
|
+
- 存在未实施的当前计划 → 拒绝执行,提示先 implement 或 replan。
|
|
226
|
+
|
|
227
|
+
## 执行步骤
|
|
228
|
+
|
|
229
|
+
1. 按 plan 协议创建单计划(不拆分,不进入 preparing 队列)。
|
|
230
|
+
2. 无需用户确认计划内容,直接进入 implement 流程。
|
|
231
|
+
3. 按 implement 协议执行全部步骤,包括验证循环:
|
|
232
|
+
a. 逐项对照 \`## 内容\` 确认每个步骤已实施。
|
|
233
|
+
b. 运行项目验证:类型检查 → lint → 测试。
|
|
234
|
+
c. 若存在失败项 → 修复后重新验证。
|
|
235
|
+
d. 全部通过 → 进入步骤 4。
|
|
236
|
+
4. 更新 \`plan.md\` 状态行为 \`已执行\`,更新 \`## 影响范围\`。
|
|
237
|
+
`
|
|
238
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { SkillArtifacts, ToolTarget } from '../types.js'
|
|
2
|
+
import { ACTION_NAMES, ACTION_RENDERERS } from './actions.js'
|
|
3
|
+
|
|
4
|
+
const SKILL_NAME = 'ac-workflow'
|
|
5
|
+
const SKILL_DESCRIPTION =
|
|
6
|
+
'管理 .agent-context 计划生命周期,按 init、plan、replan、implement、patch、rush、done 协议推进任务。'
|
|
7
|
+
|
|
8
|
+
export function renderSkillArtifacts(target: ToolTarget): SkillArtifacts {
|
|
9
|
+
const files: SkillArtifacts['files'] = [
|
|
10
|
+
{ relativePath: 'SKILL.md', body: renderNavigator(target) },
|
|
11
|
+
...ACTION_NAMES.map((name) => ({
|
|
12
|
+
relativePath: `actions/${name}.md`,
|
|
13
|
+
body: ACTION_RENDERERS[name]()
|
|
14
|
+
}))
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
if (target.metadataFiles.includes('openai')) {
|
|
18
|
+
files.push({ relativePath: 'agents/openai.yaml', body: renderOpenAIMetadata() })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return { files }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ── Navigator ────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
function renderNavigator(target: ToolTarget): string {
|
|
27
|
+
return `${renderFrontmatter(target)}
|
|
28
|
+
# Agent Context
|
|
29
|
+
|
|
30
|
+
管理项目中的 \`.agent-context/\` 计划生命周期。匹配用户意图后,读取对应协议文件(相对于本文件所在目录)严格执行。
|
|
31
|
+
|
|
32
|
+
## 意图匹配
|
|
33
|
+
|
|
34
|
+
| 用户意图 | 动作 | 协议文件 |
|
|
35
|
+
|----------|------|----------|
|
|
36
|
+
| 初始化项目上下文、补全 AGENTS | init | \`actions/init.md\` |
|
|
37
|
+
| 给需求出计划、拆分任务 | plan | \`actions/plan.md\` |
|
|
38
|
+
| 重做计划、调整方案 | replan | \`actions/replan.md\` |
|
|
39
|
+
| 按计划开始做、实现当前计划 | implement | \`actions/implement.md\` |
|
|
40
|
+
| 补一个小改动、在当前结果上修 | patch | \`actions/patch.md\` |
|
|
41
|
+
| 快速出计划并实施 | rush | \`actions/rush.md\` |
|
|
42
|
+
| 任务彻底完成、归档当前计划 | done | 运行 \`agent-context done\` |
|
|
43
|
+
|
|
44
|
+
## 全局约束
|
|
45
|
+
|
|
46
|
+
- 状态机两态:\`未执行\`、\`已执行\`。
|
|
47
|
+
- 任意时刻最多一个当前计划:\`.agent-context/plan-{number}\`。
|
|
48
|
+
- 多个当前计划 → 拒绝执行,提示恢复单活跃状态。
|
|
49
|
+
- 计划编号全局递增,不复用。补丁编号在单计划目录内递增,不复用。
|
|
50
|
+
|
|
51
|
+
## 目录结构
|
|
52
|
+
|
|
53
|
+
\`\`\`text
|
|
54
|
+
.agent-context/
|
|
55
|
+
├── plan-{N}/ # 当前计划(最多一个)
|
|
56
|
+
│ ├── plan.md
|
|
57
|
+
│ └── patch-{N}.md
|
|
58
|
+
├── preparing/ # 待执行计划队列
|
|
59
|
+
│ └── plan-{N}/
|
|
60
|
+
└── done/ # 已归档计划
|
|
61
|
+
└── plan-{N}-{YYYYMMDD}/
|
|
62
|
+
\`\`\`
|
|
63
|
+
|
|
64
|
+
编号规则:扫描全部 \`plan-N\` 目录取 \`max(N)+1\`。
|
|
65
|
+
`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── Frontmatter & Metadata ──────────────────────────
|
|
69
|
+
|
|
70
|
+
function renderFrontmatter(target: ToolTarget): string {
|
|
71
|
+
const lines = ['---', `name: ${SKILL_NAME}`, `description: ${SKILL_DESCRIPTION}`]
|
|
72
|
+
|
|
73
|
+
if (target.frontmatterProfile === 'claude') {
|
|
74
|
+
lines.push('argument-hint: [request]')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (target.frontmatterProfile === 'copilot') {
|
|
78
|
+
lines.push('license: MIT')
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
lines.push('---', '')
|
|
82
|
+
return `${lines.join('\n')}\n`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function renderOpenAIMetadata(): string {
|
|
86
|
+
return `interface:
|
|
87
|
+
display_name: "Agent Context"
|
|
88
|
+
short_description: "统一管理 .agent-context 计划生命周期"
|
|
89
|
+
default_prompt: "Use $ac-workflow to manage the current task through init, plan, replan, implement, patch, rush, or done."
|
|
90
|
+
|
|
91
|
+
policy:
|
|
92
|
+
allow_implicit_invocation: true
|
|
93
|
+
`
|
|
94
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { rename, mkdir } from 'node:fs/promises'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import type { ArchiveResult, ContextSnapshot } from '../types.js'
|
|
5
|
+
|
|
6
|
+
export async function archive(context: ContextSnapshot): Promise<ArchiveResult> {
|
|
7
|
+
if (!context.currentPlan) {
|
|
8
|
+
throw new Error('无当前计划')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (context.currentPlan.status !== '已执行') {
|
|
12
|
+
throw new Error('当前计划尚未执行')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const archiveName = `plan-${context.currentPlan.number}-${formatDate()}`
|
|
16
|
+
const doneDir = join(context.root, 'done')
|
|
17
|
+
const archivedTo = join(doneDir, archiveName)
|
|
18
|
+
|
|
19
|
+
await mkdir(doneDir, { recursive: true })
|
|
20
|
+
await rename(context.currentPlan.dir, archivedTo)
|
|
21
|
+
|
|
22
|
+
let promoted: number | null = null
|
|
23
|
+
const first = context.preparing[0]
|
|
24
|
+
if (first) {
|
|
25
|
+
const targetDir = join(context.root, `plan-${first.number}`)
|
|
26
|
+
await rename(first.dir, targetDir)
|
|
27
|
+
promoted = first.number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
archivedTo,
|
|
32
|
+
promoted,
|
|
33
|
+
remainingPreparing: context.preparing.length - (promoted !== null ? 1 : 0)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ── Helpers ──────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
function formatDate(): string {
|
|
40
|
+
const d = new Date()
|
|
41
|
+
const y = String(d.getFullYear())
|
|
42
|
+
const m = String(d.getMonth() + 1).padStart(2, '0')
|
|
43
|
+
const day = String(d.getDate()).padStart(2, '0')
|
|
44
|
+
return `${y}${m}${day}`
|
|
45
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { readdir, readFile } from 'node:fs/promises'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
|
|
5
|
+
import type { ContextSnapshot, PlanInfo, PlanStatus } from '../types.js'
|
|
6
|
+
|
|
7
|
+
const PLAN_DIR_RE = /^plan-(\d+)$/
|
|
8
|
+
const STATUS_RE = /^>\s*状态:\s*(未执行|已执行)\s*$/m
|
|
9
|
+
|
|
10
|
+
// ── Public API ───────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
export async function readContext(cwd: string): Promise<ContextSnapshot | null> {
|
|
13
|
+
const { snapshot } = await readRawContext(cwd)
|
|
14
|
+
return snapshot
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function readRawContext(
|
|
18
|
+
cwd: string
|
|
19
|
+
): Promise<{ snapshot: ContextSnapshot | null; currentPlanCount: number }> {
|
|
20
|
+
const root = join(cwd, '.agent-context')
|
|
21
|
+
|
|
22
|
+
if (!existsSync(root)) {
|
|
23
|
+
return { snapshot: null, currentPlanCount: 0 }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const currentPlans = await readPlanDirs(root)
|
|
27
|
+
const preparing = await readPlanDirs(join(root, 'preparing'))
|
|
28
|
+
const doneCount = await countDirs(join(root, 'done'))
|
|
29
|
+
|
|
30
|
+
const snapshot: ContextSnapshot = {
|
|
31
|
+
root,
|
|
32
|
+
currentPlan: currentPlans[0] ?? null,
|
|
33
|
+
preparing,
|
|
34
|
+
doneCount
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return { snapshot, currentPlanCount: currentPlans.length }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function readPlanStatus(planDir: string): Promise<PlanStatus> {
|
|
41
|
+
const planFile = join(planDir, 'plan.md')
|
|
42
|
+
|
|
43
|
+
if (!existsSync(planFile)) {
|
|
44
|
+
return '未执行'
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const content = await readFile(planFile, 'utf-8')
|
|
48
|
+
const match = content.match(STATUS_RE)
|
|
49
|
+
return match ? (match[1] as PlanStatus) : '未执行'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ── Helpers ──────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
async function readPlanDirs(parentDir: string): Promise<PlanInfo[]> {
|
|
55
|
+
if (!existsSync(parentDir)) return []
|
|
56
|
+
|
|
57
|
+
const entries = await readdir(parentDir, { withFileTypes: true })
|
|
58
|
+
const plans: PlanInfo[] = []
|
|
59
|
+
|
|
60
|
+
for (const entry of entries) {
|
|
61
|
+
if (!entry.isDirectory()) continue
|
|
62
|
+
const match = entry.name.match(PLAN_DIR_RE)
|
|
63
|
+
if (!match?.[1]) continue
|
|
64
|
+
const number = parseInt(match[1], 10)
|
|
65
|
+
const dir = join(parentDir, entry.name)
|
|
66
|
+
const status = await readPlanStatus(dir)
|
|
67
|
+
plans.push({ number, status, dir })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return plans.sort((a, b) => a.number - b.number)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function countDirs(dir: string): Promise<number> {
|
|
74
|
+
if (!existsSync(dir)) return 0
|
|
75
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
76
|
+
return entries.filter(e => e.isDirectory()).length
|
|
77
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import type { ContextSnapshot, ValidateResult } from '../types.js'
|
|
5
|
+
|
|
6
|
+
export function validate(
|
|
7
|
+
snapshot: ContextSnapshot | null,
|
|
8
|
+
currentPlanCount: number
|
|
9
|
+
): ValidateResult {
|
|
10
|
+
if (snapshot === null) {
|
|
11
|
+
return { valid: true, errors: [], context: null }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const errors: string[] = []
|
|
15
|
+
|
|
16
|
+
if (currentPlanCount > 1) {
|
|
17
|
+
errors.push(`存在 ${currentPlanCount} 个当前计划,应最多 1 个。`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (snapshot.currentPlan) {
|
|
21
|
+
const planMd = join(snapshot.currentPlan.dir, 'plan.md')
|
|
22
|
+
if (!existsSync(planMd)) {
|
|
23
|
+
errors.push(`当前计划 plan-${snapshot.currentPlan.number} 缺少 plan.md。`)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const allNumbers: number[] = []
|
|
28
|
+
if (snapshot.currentPlan) allNumbers.push(snapshot.currentPlan.number)
|
|
29
|
+
for (const p of snapshot.preparing) allNumbers.push(p.number)
|
|
30
|
+
|
|
31
|
+
const seen = new Set<number>()
|
|
32
|
+
const duplicates = new Set<number>()
|
|
33
|
+
for (const n of allNumbers) {
|
|
34
|
+
if (seen.has(n)) duplicates.add(n)
|
|
35
|
+
seen.add(n)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (duplicates.size > 0) {
|
|
39
|
+
const nums = [...duplicates].sort((a, b) => a - b).join(', ')
|
|
40
|
+
errors.push(`计划编号冲突: ${nums}。`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { valid: errors.length === 0, errors, context: snapshot }
|
|
44
|
+
}
|
package/src/runner.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { resolve, dirname } from 'node:path'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
4
|
+
|
|
5
|
+
import type { SkillPaths } from './tools.js'
|
|
6
|
+
import { resolveToolTargets, resolveSkillPaths } from './tools.js'
|
|
7
|
+
import { renderSkillArtifacts } from './content/index.js'
|
|
8
|
+
import type { ApplyMutationResult, FileMutation, RunOptions, RunResult, ToolId, ToolTarget } from './types.js'
|
|
9
|
+
|
|
10
|
+
export async function runInstall(options: RunOptions = {}): Promise<RunResult> {
|
|
11
|
+
return run('install', options)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function runSync(options: RunOptions = {}): Promise<RunResult> {
|
|
15
|
+
return run('sync', options)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ── Orchestration ───────────────────────────────────
|
|
19
|
+
|
|
20
|
+
async function run(mode: 'install' | 'sync', options: RunOptions): Promise<RunResult> {
|
|
21
|
+
const cwd = options.cwd ?? process.cwd()
|
|
22
|
+
const tools = dedup(options.tools)
|
|
23
|
+
const targets = resolveToolTargets(tools)
|
|
24
|
+
|
|
25
|
+
const mutations: FileMutation[] = targets.flatMap(target => buildMutations(target, cwd))
|
|
26
|
+
|
|
27
|
+
const check = options.check ?? false
|
|
28
|
+
const result = await applyMutations(mutations, check)
|
|
29
|
+
|
|
30
|
+
return { ...result, mode, check }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Mutation building ───────────────────────────────
|
|
34
|
+
|
|
35
|
+
function buildMutations(target: ToolTarget, cwd: string): FileMutation[] {
|
|
36
|
+
const artifacts = renderSkillArtifacts(target)
|
|
37
|
+
const paths = resolveSkillPaths(target, cwd)
|
|
38
|
+
|
|
39
|
+
return artifacts.files.map(file => ({
|
|
40
|
+
path: resolveArtifactPath(paths, file.relativePath),
|
|
41
|
+
body: file.body
|
|
42
|
+
}))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function resolveArtifactPath(paths: SkillPaths, relativePath: string): string {
|
|
46
|
+
if (relativePath === 'SKILL.md') {
|
|
47
|
+
return paths.skillFile
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return resolve(paths.skillDir, relativePath)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ── Mutation application ────────────────────────────
|
|
54
|
+
|
|
55
|
+
async function applyMutations(
|
|
56
|
+
mutations: FileMutation[],
|
|
57
|
+
check: boolean
|
|
58
|
+
): Promise<ApplyMutationResult> {
|
|
59
|
+
const result: ApplyMutationResult = {
|
|
60
|
+
created: [],
|
|
61
|
+
updated: [],
|
|
62
|
+
unchanged: [],
|
|
63
|
+
changed: []
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const mutation of mutations) {
|
|
67
|
+
const fileExists = existsSync(mutation.path)
|
|
68
|
+
const next = await resolveNextContent(mutation, fileExists)
|
|
69
|
+
|
|
70
|
+
if (!next.changed) {
|
|
71
|
+
result.unchanged.push(mutation.path)
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!check) {
|
|
76
|
+
await mkdir(dirname(mutation.path), { recursive: true })
|
|
77
|
+
await writeFile(mutation.path, next.content, 'utf-8')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
result.changed.push(mutation.path)
|
|
81
|
+
|
|
82
|
+
if (fileExists) {
|
|
83
|
+
result.updated.push(mutation.path)
|
|
84
|
+
} else {
|
|
85
|
+
result.created.push(mutation.path)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return result
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function resolveNextContent(
|
|
93
|
+
mutation: FileMutation,
|
|
94
|
+
fileExists: boolean
|
|
95
|
+
): Promise<{ content: string; changed: boolean }> {
|
|
96
|
+
const content = normalizeTrailingNewline(mutation.body)
|
|
97
|
+
|
|
98
|
+
if (!fileExists) {
|
|
99
|
+
return { content, changed: true }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const current = await readFile(mutation.path, 'utf-8')
|
|
103
|
+
return { content, changed: current !== content }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function normalizeTrailingNewline(content: string): string {
|
|
107
|
+
return content.endsWith('\n') ? content : `${content}\n`
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function dedup(tools?: ToolId[]): ToolId[] | undefined {
|
|
111
|
+
if (!tools || tools.length === 0) return undefined
|
|
112
|
+
return [...new Set(tools)]
|
|
113
|
+
}
|