@wbline/pi-plan-build 0.0.5 → 0.0.6
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/CHANGELOG.md +25 -0
- package/README.md +15 -13
- package/package.json +1 -1
- package/src/context.ts +10 -1
- package/src/core/archive.ts +20 -3
- package/src/core/guard.ts +146 -58
- package/src/core/prompts.ts +4 -4
- package/src/engine.ts +1 -1
- package/src/lifecycle.ts +24 -16
- package/src/tools/descriptions.ts +118 -0
- package/src/tools/index.ts +1 -1
- package/src/tools/plan-exit.ts +23 -9
- package/src/tools/plan-outcome.ts +21 -29
- package/src/tools/plan-question.ts +21 -17
- package/src/tools/plan-steps.ts +35 -43
- package/src/tools/plan-task.ts +88 -54
package/src/tools/plan-task.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** plan_task:任务生命周期元数据(new/title/include/exclude/abandon/list/show)。 */
|
|
1
|
+
/** plan_task:任务生命周期元数据(new/revise/title/include/exclude/abandon/list/show)。 */
|
|
2
2
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
@@ -8,60 +8,52 @@ import type {
|
|
|
8
8
|
ExtensionToolContext,
|
|
9
9
|
} from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { Type } from "typebox";
|
|
11
|
-
import { listPlanFiles } from "../core/archive.ts";
|
|
11
|
+
import { listPlanFiles, markSuperseded } from "../core/archive.ts";
|
|
12
12
|
import { statusLine } from "../core/state.ts";
|
|
13
13
|
import type { PlanEngine } from "../engine.ts";
|
|
14
14
|
import { nextPlanPath } from "../engine.ts";
|
|
15
|
-
|
|
16
|
-
const Params = Type.Object({
|
|
17
|
-
action: StringEnum([
|
|
18
|
-
"new",
|
|
19
|
-
"update",
|
|
20
|
-
"include",
|
|
21
|
-
"exclude",
|
|
22
|
-
"abandon",
|
|
23
|
-
"list",
|
|
24
|
-
"show",
|
|
25
|
-
]),
|
|
26
|
-
/** new: 标题(动作+对象);update: 新标题;abandon: 原因。 */
|
|
27
|
-
title: Type.Optional(
|
|
28
|
-
Type.String({ description: "new/update 的标题,或 abandon 的原因" }),
|
|
29
|
-
),
|
|
30
|
-
/** include: 追加范围全文;exclude: 排除话题。 */
|
|
31
|
-
scope: Type.Optional(
|
|
32
|
-
Type.String({ description: "include 的完整合并范围,或 exclude 的话题" }),
|
|
33
|
-
),
|
|
34
|
-
/** 乐观并发:必须传最近一次调用返回的 seq。 */
|
|
35
|
-
expectedAttached: Type.Optional(
|
|
36
|
-
Type.Union([Type.Integer(), Type.Null()], {
|
|
37
|
-
description: "当前计划 seq;new 传 null",
|
|
38
|
-
}),
|
|
39
|
-
),
|
|
40
|
-
/** new 专用:恢复既有计划文件,以其内容为计划正文继续。 */
|
|
41
|
-
fromFile: Type.Optional(
|
|
42
|
-
Type.String({
|
|
43
|
-
description:
|
|
44
|
-
"new 专用:恢复既有计划文件(绝对或 cwd 相对路径),以其内容为计划正文继续(换会话后接着干);传了则 title 可省(默认取文件一级标题),scope 不需要。",
|
|
45
|
-
}),
|
|
46
|
-
),
|
|
47
|
-
/** 仅当用户明确要求规划时传 true,跳过「创建计划 / 直接执行」入口询问。 */
|
|
48
|
-
planningRequested: Type.Optional(
|
|
49
|
-
Type.Boolean({
|
|
50
|
-
description:
|
|
51
|
-
'仅当用户明确要求规划(如"帮我规划""出个方案")时传 true,跳过入口询问;未传时工具会先弹窗让用户选择创建计划还是直接执行。',
|
|
52
|
-
}),
|
|
53
|
-
),
|
|
54
|
-
});
|
|
15
|
+
import { toolTexts } from "./descriptions.ts";
|
|
55
16
|
|
|
56
17
|
const PLAN_FIRST = "创建计划,审阅后再实现";
|
|
57
18
|
const DIRECT_EXECUTE = "直接执行";
|
|
58
19
|
|
|
20
|
+
/** plans 目录内判定(Windows 大小写不敏感)。 */
|
|
21
|
+
function sameDir(a: string, b: string): boolean {
|
|
22
|
+
return process.platform === "win32"
|
|
23
|
+
? a.toLowerCase() === b.toLowerCase()
|
|
24
|
+
: a === b;
|
|
25
|
+
}
|
|
26
|
+
|
|
59
27
|
export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
28
|
+
const T = toolTexts(engine.config.lang);
|
|
29
|
+
const Params = Type.Object({
|
|
30
|
+
action: StringEnum([
|
|
31
|
+
"new",
|
|
32
|
+
"revise",
|
|
33
|
+
"update",
|
|
34
|
+
"include",
|
|
35
|
+
"exclude",
|
|
36
|
+
"abandon",
|
|
37
|
+
"list",
|
|
38
|
+
"show",
|
|
39
|
+
]),
|
|
40
|
+
title: Type.Optional(Type.String({ description: T.taskTitle })),
|
|
41
|
+
scope: Type.Optional(Type.String({ description: T.taskScope })),
|
|
42
|
+
expectedAttached: Type.Optional(
|
|
43
|
+
Type.Union([Type.Integer(), Type.Null()], {
|
|
44
|
+
description: T.taskExpected,
|
|
45
|
+
}),
|
|
46
|
+
),
|
|
47
|
+
fromFile: Type.Optional(Type.String({ description: T.taskFromFile })),
|
|
48
|
+
planningRequested: Type.Optional(
|
|
49
|
+
Type.Boolean({ description: T.taskPlanningRequested }),
|
|
50
|
+
),
|
|
51
|
+
});
|
|
52
|
+
|
|
60
53
|
pi.registerTool({
|
|
61
54
|
name: "plan_task",
|
|
62
55
|
label: "plan task",
|
|
63
|
-
description:
|
|
64
|
-
"计划任务生命周期:new 建计划(plan 模式;build 模式调用会弹窗确认切换);用户未明确要求规划时 new 会先弹窗让用户选「创建计划 / 直接执行」,选直接执行则自动切 build 并放弃建计划;new 支持 fromFile 恢复既有计划文件;update 改名;include 追加用户批准的范围;exclude 记录排除话题;abandon 显式放弃(须用户指示);list 查看状态并附 plans 目录历史计划(含归档结局);show 查看详情。转移必须先于依赖它的写入/命令单独完成。",
|
|
56
|
+
description: T.planTask,
|
|
65
57
|
parameters: Params,
|
|
66
58
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
67
59
|
const s = engine.state;
|
|
@@ -122,7 +114,9 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
122
114
|
const sw = engine.tryApply({ t: "mode.set", mode: "plan" }, ctx);
|
|
123
115
|
if ("error" in sw) return err(sw.error);
|
|
124
116
|
}
|
|
125
|
-
// fromFile
|
|
117
|
+
// fromFile 恢复:既有计划文件继续(换会话后接着干)。
|
|
118
|
+
// plans 目录内的文件原地接管(不产生重复副本);外部文件复制到 canonical
|
|
119
|
+
// 路径并在原文件文末标记「已接管」,避免两份同内容文件分不清哪份有效。
|
|
126
120
|
if (params.fromFile) {
|
|
127
121
|
const src = path.isAbsolute(params.fromFile)
|
|
128
122
|
? params.fromFile
|
|
@@ -138,8 +132,10 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
138
132
|
params.title?.trim() ||
|
|
139
133
|
/^#\s+(.+)$/m.exec(content)?.[1]?.trim() ||
|
|
140
134
|
"恢复的计划";
|
|
141
|
-
const
|
|
142
|
-
|
|
135
|
+
const plansDir = path.join(ctx.cwd, ".pi", "agent", "plans");
|
|
136
|
+
const inPlace = sameDir(path.dirname(src), plansDir);
|
|
137
|
+
const p = inPlace ? src : nextPlanPath(ctx.cwd, title);
|
|
138
|
+
if (!inPlace) fs.writeFileSync(p, content);
|
|
143
139
|
const r = engine.tryApply(
|
|
144
140
|
{
|
|
145
141
|
t: "plan.create",
|
|
@@ -151,16 +147,24 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
151
147
|
);
|
|
152
148
|
if ("error" in r) {
|
|
153
149
|
// 占位文件已分配但计划未建:回收空占位,不留孤儿文件
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
150
|
+
if (!inPlace) {
|
|
151
|
+
try {
|
|
152
|
+
if (fs.statSync(p).size === 0) fs.unlinkSync(p);
|
|
153
|
+
} catch {
|
|
154
|
+
// 回收失败不掩盖主错误
|
|
155
|
+
}
|
|
158
156
|
}
|
|
159
157
|
return err(r.error);
|
|
160
158
|
}
|
|
161
159
|
engine.apply({ t: "plan.saved" }, ctx);
|
|
160
|
+
if (inPlace) {
|
|
161
|
+
return text(
|
|
162
|
+
`计划已在原文件恢复(seq=1)。canonical 路径:${p}\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
const marked = markSuperseded(src, p);
|
|
162
166
|
return text(
|
|
163
|
-
`计划已从 ${src} 恢复(seq=1)。canonical 路径:${p}\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
167
|
+
`计划已从 ${src} 恢复(seq=1)。canonical 路径:${p}${marked ? "\n原文件文末已标记「已接管」,以新路径为准。" : ""}\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
164
168
|
);
|
|
165
169
|
}
|
|
166
170
|
if (!params.title)
|
|
@@ -219,11 +223,41 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
219
223
|
if (!s.plan) return err("当前没有计划。先 plan_task new。");
|
|
220
224
|
if (expected !== s.plan.seq) {
|
|
221
225
|
return err(
|
|
222
|
-
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq}
|
|
226
|
+
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq} 不匹配(计划已被其他转移更新)。无副作用动作可改用 seq=${s.plan.seq} 直接重试;include 等范围类动作请先 plan_task show 核对最新范围再重试。`,
|
|
223
227
|
);
|
|
224
228
|
}
|
|
225
229
|
|
|
226
230
|
switch (params.action) {
|
|
231
|
+
case "revise": {
|
|
232
|
+
// build 中途修订计划的专用通道:确认门切回 plan(对称于 new 的 build 确认门)
|
|
233
|
+
if (s.mode === "plan") {
|
|
234
|
+
return text(
|
|
235
|
+
`已处于 plan 模式(seq=${s.plan.seq}):直接修订计划文件,完成后调用 plan_exit 重新提请审批。`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (s.execution) {
|
|
239
|
+
return err(
|
|
240
|
+
"分步执行进行中:先 plan_step_control cancel(计划保留为未完成)再 revise 修订计划;或完成/标记完成后整计划收尾。",
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (ctx.hasUI) {
|
|
244
|
+
const okSwitch = await ctx.ui.confirm(
|
|
245
|
+
"修订计划需要 plan 模式",
|
|
246
|
+
"切换到 plan 模式(只读)修订计划文件?修订后需再次 plan_exit 提请审批。",
|
|
247
|
+
);
|
|
248
|
+
if (!okSwitch) {
|
|
249
|
+
return err(
|
|
250
|
+
"用户拒绝切换:计划未修订。可建议用户手动 /plan 后再修订。",
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const sw = engine.tryApply({ t: "mode.set", mode: "plan" }, ctx);
|
|
255
|
+
if ("error" in sw) return err(sw.error);
|
|
256
|
+
const note = ctx.hasUI ? "" : "(无交互 UI:确认门已跳过)";
|
|
257
|
+
return text(
|
|
258
|
+
`已切回 plan 模式(seq=${s.plan.seq})${note}。修订计划文件后调用 plan_exit 重新提请审批。`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
227
261
|
case "update": {
|
|
228
262
|
if (!params.title) return err("update 需要 title。");
|
|
229
263
|
const r = engine.tryApply(
|
|
@@ -246,7 +280,7 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
246
280
|
return "error" in r
|
|
247
281
|
? err(r.error)
|
|
248
282
|
: text(
|
|
249
|
-
`范围已追加(seq=${r.state.plan?.seq ?? 1})。请相应更新计划文件;若文件已审阅过,需重新 plan_exit。${r.state.mode === "build" ? "(build
|
|
283
|
+
`范围已追加(seq=${r.state.plan?.seq ?? 1})。请相应更新计划文件;若文件已审阅过,需重新 plan_exit。${r.state.mode === "build" ? "(build 模式下计划文件受保护:调用 plan_task revise 切回 plan 再修订,改完 plan_exit。)" : ""}`,
|
|
250
284
|
);
|
|
251
285
|
}
|
|
252
286
|
case "exclude": {
|