@wbline/pi-plan-build 0.0.4 → 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 +41 -0
- package/README.md +24 -21
- package/package.json +1 -1
- package/src/context.ts +13 -2
- package/src/core/archive.ts +87 -0
- package/src/core/guard.ts +146 -58
- package/src/core/prompts.ts +13 -8
- package/src/core/schema.ts +2 -1
- package/src/core/state.ts +28 -23
- package/src/engine.ts +46 -1
- package/src/lifecycle.ts +49 -17
- package/src/tools/descriptions.ts +118 -0
- package/src/tools/index.ts +1 -1
- package/src/tools/plan-exit.ts +27 -13
- package/src/tools/plan-outcome.ts +25 -30
- package/src/tools/plan-question.ts +21 -17
- package/src/tools/plan-steps.ts +50 -35
- package/src/tools/plan-task.ts +182 -32
|
@@ -8,41 +8,34 @@ import type {
|
|
|
8
8
|
import { Type } from "typebox";
|
|
9
9
|
import type { PlanOutcome } from "../core/state.ts";
|
|
10
10
|
import type { PlanEngine } from "../engine.ts";
|
|
11
|
-
|
|
12
|
-
const CompleteParams = Type.Object({
|
|
13
|
-
summary: Type.Optional(
|
|
14
|
-
Type.String({ description: "事实性完成摘要(可选),随历史记录保留" }),
|
|
15
|
-
),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const FinishParams = Type.Object({
|
|
19
|
-
outcome: StringEnum([
|
|
20
|
-
"awaiting_validation",
|
|
21
|
-
"blocked",
|
|
22
|
-
"waiting_for_input",
|
|
23
|
-
"still_working",
|
|
24
|
-
]),
|
|
25
|
-
userAction: Type.Optional(
|
|
26
|
-
Type.String({
|
|
27
|
-
description: "awaiting_validation 必填:用户要执行的精确验证步骤",
|
|
28
|
-
}),
|
|
29
|
-
),
|
|
30
|
-
reason: Type.Optional(
|
|
31
|
-
Type.String({
|
|
32
|
-
description: "blocked/waiting_for_input/still_working 必填:原因",
|
|
33
|
-
}),
|
|
34
|
-
),
|
|
35
|
-
});
|
|
11
|
+
import { toolTexts } from "./descriptions.ts";
|
|
36
12
|
|
|
37
13
|
export function registerPlanOutcome(
|
|
38
14
|
pi: ExtensionAPI,
|
|
39
15
|
engine: PlanEngine,
|
|
40
16
|
): void {
|
|
17
|
+
const T = toolTexts(engine.config.lang);
|
|
18
|
+
const CompleteParams = Type.Object({
|
|
19
|
+
summary: Type.Optional(Type.String({ description: T.completeSummary })),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const FinishParams = Type.Object({
|
|
23
|
+
outcome: StringEnum([
|
|
24
|
+
"awaiting_validation",
|
|
25
|
+
"blocked",
|
|
26
|
+
"waiting_for_input",
|
|
27
|
+
"still_working",
|
|
28
|
+
]),
|
|
29
|
+
userAction: Type.Optional(
|
|
30
|
+
Type.String({ description: T.outcomeUserAction }),
|
|
31
|
+
),
|
|
32
|
+
reason: Type.Optional(Type.String({ description: T.outcomeReason })),
|
|
33
|
+
});
|
|
34
|
+
|
|
41
35
|
pi.registerTool({
|
|
42
36
|
name: "plan_complete",
|
|
43
37
|
label: "plan complete",
|
|
44
|
-
description:
|
|
45
|
-
"标记整个计划完成:仅在全部批准工作与必需检查通过后,或用户明确指示整计划完成时调用(分步执行进行中也允许,会如实收尾)。绝不虚构未执行的检查。",
|
|
38
|
+
description: T.planComplete,
|
|
46
39
|
parameters: CompleteParams,
|
|
47
40
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
48
41
|
if (!engine.state.plan) return fail("当前没有计划。");
|
|
@@ -51,8 +44,11 @@ export function registerPlanOutcome(
|
|
|
51
44
|
{ t: "plan.complete", summary: params.summary },
|
|
52
45
|
ctx,
|
|
53
46
|
);
|
|
47
|
+
const arch = engine.lastArchive?.ok
|
|
48
|
+
? `\n状态块已写入计划文件:${engine.lastArchive.path}`
|
|
49
|
+
: "";
|
|
54
50
|
return ok(
|
|
55
|
-
`计划已标记完成。${r.note ?? ""}${params.summary ? `\n摘要:${params.summary}` : ""}`,
|
|
51
|
+
`计划已标记完成。${r.note ?? ""}${params.summary ? `\n摘要:${params.summary}` : ""}${arch}`,
|
|
56
52
|
);
|
|
57
53
|
},
|
|
58
54
|
});
|
|
@@ -60,8 +56,7 @@ export function registerPlanOutcome(
|
|
|
60
56
|
pi.registerTool({
|
|
61
57
|
name: "plan_set_outcome",
|
|
62
58
|
label: "plan set outcome",
|
|
63
|
-
description:
|
|
64
|
-
"记录未完成结局并保持计划开放:awaiting_validation(必需的用户验证,userAction 必填)、blocked / waiting_for_input / still_working(reason 必填)。绝不因空闲推断成功。",
|
|
59
|
+
description: T.planOutcome,
|
|
65
60
|
parameters: FinishParams,
|
|
66
61
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
67
62
|
if (!engine.state.plan) return fail("当前没有计划。");
|
|
@@ -5,29 +5,33 @@ import type {
|
|
|
5
5
|
ExtensionToolContext,
|
|
6
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { Type } from "typebox";
|
|
8
|
+
import type { Lang } from "../core/prompts.ts";
|
|
9
|
+
import { toolTexts } from "./descriptions.ts";
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Type.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
12
|
+
const T = toolTexts(lang);
|
|
13
|
+
const QuestionT = Type.Object({
|
|
14
|
+
question: Type.String({ description: T.questionPrompt }),
|
|
15
|
+
header: Type.String({ description: T.questionHeader }),
|
|
16
|
+
options: Type.Array(
|
|
17
|
+
Type.Object({
|
|
18
|
+
label: Type.String({ description: T.questionLabel }),
|
|
19
|
+
description: Type.Optional(
|
|
20
|
+
Type.String({ description: T.questionDescription }),
|
|
21
|
+
),
|
|
22
|
+
}),
|
|
23
|
+
{ minItems: 2, maxItems: 4 },
|
|
24
|
+
),
|
|
25
|
+
});
|
|
20
26
|
|
|
21
|
-
const Params = Type.Object({
|
|
22
|
-
|
|
23
|
-
});
|
|
27
|
+
const Params = Type.Object({
|
|
28
|
+
questions: Type.Array(QuestionT, { minItems: 1, maxItems: 4 }),
|
|
29
|
+
});
|
|
24
30
|
|
|
25
|
-
export function registerQuestion(pi: ExtensionAPI): void {
|
|
26
31
|
pi.registerTool({
|
|
27
32
|
name: "plan_question",
|
|
28
33
|
label: "plan question",
|
|
29
|
-
description:
|
|
30
|
-
"向用户提出结构化选择题,收集决策/澄清歧义。推荐选项放第一个并在 label 尾部加“(推荐)”。",
|
|
34
|
+
description: T.question,
|
|
31
35
|
parameters: Params,
|
|
32
36
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
33
37
|
if (!ctx.hasUI) {
|
package/src/tools/plan-steps.ts
CHANGED
|
@@ -8,48 +8,54 @@ import type {
|
|
|
8
8
|
import { Type } from "typebox";
|
|
9
9
|
import { reviseStepInMarkdown } from "../core/steps.ts";
|
|
10
10
|
import type { PlanEngine } from "../engine.ts";
|
|
11
|
-
|
|
12
|
-
const ControlParams = Type.Object({
|
|
13
|
-
action: StringEnum([
|
|
14
|
-
"start",
|
|
15
|
-
"acknowledge",
|
|
16
|
-
"skip",
|
|
17
|
-
"revise",
|
|
18
|
-
"pause",
|
|
19
|
-
"resume",
|
|
20
|
-
"cancel",
|
|
21
|
-
]),
|
|
22
|
-
/** acknowledge: 步骤摘要;revise: 新文本;cancel 不需要参数。 */
|
|
23
|
-
summary: Type.Optional(
|
|
24
|
-
Type.String({ description: "acknowledge 的完成摘要" }),
|
|
25
|
-
),
|
|
26
|
-
step: Type.Optional(
|
|
27
|
-
Type.Integer({
|
|
28
|
-
minimum: 1,
|
|
29
|
-
description: "revise 的步骤号(默认当前就绪步)",
|
|
30
|
-
}),
|
|
31
|
-
),
|
|
32
|
-
text: Type.Optional(Type.String({ description: "revise 的新步骤文本" })),
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
const CompleteParams = Type.Object({
|
|
36
|
-
summary: Type.String({ description: "本步实现与验证的简明摘要" }),
|
|
37
|
-
});
|
|
11
|
+
import { toolTexts } from "./descriptions.ts";
|
|
38
12
|
|
|
39
13
|
export function registerPlanStepTools(
|
|
40
14
|
pi: ExtensionAPI,
|
|
41
15
|
engine: PlanEngine,
|
|
42
16
|
): void {
|
|
17
|
+
const T = toolTexts(engine.config.lang);
|
|
18
|
+
const ControlParams = Type.Object({
|
|
19
|
+
action: StringEnum([
|
|
20
|
+
"start",
|
|
21
|
+
"acknowledge",
|
|
22
|
+
"skip",
|
|
23
|
+
"revise",
|
|
24
|
+
"pause",
|
|
25
|
+
"resume",
|
|
26
|
+
"cancel",
|
|
27
|
+
]),
|
|
28
|
+
summary: Type.Optional(Type.String({ description: T.stepAckSummary })),
|
|
29
|
+
advance: Type.Optional(
|
|
30
|
+
Type.Union([Type.Literal("one"), Type.Literal("all")], {
|
|
31
|
+
description: T.stepAdvance,
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
step: Type.Optional(
|
|
35
|
+
Type.Integer({ description: T.stepReviseStep, minimum: 1 }),
|
|
36
|
+
),
|
|
37
|
+
text: Type.Optional(Type.String({ description: T.stepReviseText })),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const CompleteParams = Type.Object({
|
|
41
|
+
summary: Type.String({ description: T.stepSummary }),
|
|
42
|
+
});
|
|
43
|
+
|
|
43
44
|
pi.registerTool({
|
|
44
45
|
name: "plan_step_control",
|
|
45
46
|
label: "plan step control",
|
|
46
|
-
description:
|
|
47
|
-
"分步执行控制:start 批准并开始就绪步;acknowledge 指认用户声明的已完成步(当前步无需先 start;记录既有事实,不授权新工作,AI 自己完成当前步用 plan_step_complete);skip 跳过;revise 修订未完成步文本;pause/resume 暂停恢复;cancel 取消分步执行(计划保留)。一次一个转移。",
|
|
47
|
+
description: T.stepControl,
|
|
48
48
|
parameters: ControlParams,
|
|
49
49
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
50
50
|
switch (params.action) {
|
|
51
51
|
case "start": {
|
|
52
|
-
const r = engine.tryApply(
|
|
52
|
+
const r = engine.tryApply(
|
|
53
|
+
{
|
|
54
|
+
t: "exec.step.start",
|
|
55
|
+
advance: params.advance === "all" ? "all" : undefined,
|
|
56
|
+
},
|
|
57
|
+
ctx,
|
|
58
|
+
);
|
|
53
59
|
if ("error" in r) return err(r.error);
|
|
54
60
|
const st = r.state.execution?.steps.find(
|
|
55
61
|
(x) => x.status === "active",
|
|
@@ -57,7 +63,9 @@ export function registerPlanStepTools(
|
|
|
57
63
|
if (!st) return err("没有进行中的步骤。");
|
|
58
64
|
const total = r.state.execution?.steps.length ?? 0;
|
|
59
65
|
return ok(
|
|
60
|
-
|
|
66
|
+
r.state.execution?.auto
|
|
67
|
+
? `第 ${st.n}/${total} 步已开始(连跑授权):${st.text}\n完成后调用 plan_step_complete,下一步将自动开始,无需等用户批准;遇阻用 plan_set_outcome 记录并停。`
|
|
68
|
+
: `第 ${st.n}/${total} 步已开始:${st.text}\n只实现本步,完成后调用 plan_step_complete。`,
|
|
61
69
|
);
|
|
62
70
|
}
|
|
63
71
|
case "acknowledge": {
|
|
@@ -66,7 +74,7 @@ export function registerPlanStepTools(
|
|
|
66
74
|
ctx,
|
|
67
75
|
);
|
|
68
76
|
if ("error" in r) return err(r.error);
|
|
69
|
-
return ok(r.note ?? "步骤已记录完成。");
|
|
77
|
+
return ok((r.note ?? "步骤已记录完成。") + archiveSuffix());
|
|
70
78
|
}
|
|
71
79
|
case "skip": {
|
|
72
80
|
const r = engine.tryApply({ t: "exec.step.skip" }, ctx);
|
|
@@ -120,8 +128,7 @@ export function registerPlanStepTools(
|
|
|
120
128
|
pi.registerTool({
|
|
121
129
|
name: "plan_step_complete",
|
|
122
130
|
label: "plan step complete",
|
|
123
|
-
description:
|
|
124
|
-
"完成当前进行中的分步(仅在本步实现与适用检查完成后调用)。摘要必填。完成立即生效,不等待用户验收;绝不开始下一步。",
|
|
131
|
+
description: T.stepComplete,
|
|
125
132
|
parameters: CompleteParams,
|
|
126
133
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
127
134
|
const r = engine.tryApply(
|
|
@@ -130,7 +137,9 @@ export function registerPlanStepTools(
|
|
|
130
137
|
);
|
|
131
138
|
if ("error" in r) return err(r.error);
|
|
132
139
|
if (!r.state.plan) {
|
|
133
|
-
return ok(
|
|
140
|
+
return ok(
|
|
141
|
+
`${r.note ?? "全部步骤完成,计划已收尾。"}${archiveSuffix()}`,
|
|
142
|
+
);
|
|
134
143
|
}
|
|
135
144
|
const next = r.state.execution?.steps.find((x) => x.status === "ready");
|
|
136
145
|
if (!next) return ok(`${r.note ?? "步骤完成。"}`);
|
|
@@ -139,6 +148,12 @@ export function registerPlanStepTools(
|
|
|
139
148
|
);
|
|
140
149
|
},
|
|
141
150
|
});
|
|
151
|
+
|
|
152
|
+
/** 终态归档透出:仅当本次转移收尾了计划(lastArchive 每次 apply 重置)。 */
|
|
153
|
+
function archiveSuffix(): string {
|
|
154
|
+
const a = engine.lastArchive;
|
|
155
|
+
return a?.ok ? `\n状态块已写入计划文件:${a.path}` : "";
|
|
156
|
+
}
|
|
142
157
|
}
|
|
143
158
|
|
|
144
159
|
function ok(t: string) {
|
package/src/tools/plan-task.ts
CHANGED
|
@@ -1,58 +1,80 @@
|
|
|
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
|
+
import path from "node:path";
|
|
4
5
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
5
6
|
import type {
|
|
6
7
|
ExtensionAPI,
|
|
7
8
|
ExtensionToolContext,
|
|
8
9
|
} from "@earendil-works/pi-coding-agent";
|
|
9
10
|
import { Type } from "typebox";
|
|
11
|
+
import { listPlanFiles, markSuperseded } from "../core/archive.ts";
|
|
10
12
|
import { statusLine } from "../core/state.ts";
|
|
11
13
|
import type { PlanEngine } from "../engine.ts";
|
|
12
14
|
import { nextPlanPath } from "../engine.ts";
|
|
15
|
+
import { toolTexts } from "./descriptions.ts";
|
|
13
16
|
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
]),
|
|
24
|
-
/** new: 标题(动作+对象);update: 新标题;abandon: 原因。 */
|
|
25
|
-
title: Type.Optional(
|
|
26
|
-
Type.String({ description: "new/update 的标题,或 abandon 的原因" }),
|
|
27
|
-
),
|
|
28
|
-
/** include: 追加范围全文;exclude: 排除话题。 */
|
|
29
|
-
scope: Type.Optional(
|
|
30
|
-
Type.String({ description: "include 的完整合并范围,或 exclude 的话题" }),
|
|
31
|
-
),
|
|
32
|
-
/** 乐观并发:必须传最近一次调用返回的 seq。 */
|
|
33
|
-
expectedAttached: Type.Optional(
|
|
34
|
-
Type.Union([Type.Integer(), Type.Null()], {
|
|
35
|
-
description: "当前计划 seq;new 传 null",
|
|
36
|
-
}),
|
|
37
|
-
),
|
|
38
|
-
});
|
|
17
|
+
const PLAN_FIRST = "创建计划,审阅后再实现";
|
|
18
|
+
const DIRECT_EXECUTE = "直接执行";
|
|
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
|
+
}
|
|
39
26
|
|
|
40
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
|
+
|
|
41
53
|
pi.registerTool({
|
|
42
54
|
name: "plan_task",
|
|
43
55
|
label: "plan task",
|
|
44
|
-
description:
|
|
45
|
-
"计划任务生命周期:new 建计划(plan 模式);update 改名;include 追加用户批准的范围;exclude 记录排除话题;abandon 显式放弃(须用户指示);list/show 查看状态。转移必须先于依赖它的写入/命令单独完成。",
|
|
56
|
+
description: T.planTask,
|
|
46
57
|
parameters: Params,
|
|
47
58
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
48
59
|
const s = engine.state;
|
|
49
60
|
|
|
50
61
|
if (params.action === "list") {
|
|
51
|
-
|
|
62
|
+
const lines = [
|
|
52
63
|
s.plan
|
|
53
64
|
? `当前计划:${statusLine(s)}\nseq=${s.plan.seq}`
|
|
54
65
|
: "当前没有计划。",
|
|
66
|
+
];
|
|
67
|
+
const hist = listPlanFiles(ctx.cwd).filter(
|
|
68
|
+
(h) => h.path !== s.plan?.path,
|
|
55
69
|
);
|
|
70
|
+
if (hist.length > 0) {
|
|
71
|
+
lines.push("", "历史计划(.pi/agent/plans/,按修改时间倒序):");
|
|
72
|
+
for (const h of hist) {
|
|
73
|
+
const tag = h.status ? `${h.status}(${h.date})` : "未收尾";
|
|
74
|
+
lines.push(` ${tag} · ${h.title} — ${h.path}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return text(lines.join("\n"));
|
|
56
78
|
}
|
|
57
79
|
if (params.action === "show") {
|
|
58
80
|
if (!s.plan) return text("当前没有计划。");
|
|
@@ -75,9 +97,105 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
75
97
|
if (expected !== null) {
|
|
76
98
|
return err("plan_task new 需要 expectedAttached: null。");
|
|
77
99
|
}
|
|
100
|
+
// build 模式建计划 → 确认门:确认即自动切 plan(对称于 plan_exit 出口审批)
|
|
101
|
+
if (s.mode === "build" && !s.plan && ctx.hasUI) {
|
|
102
|
+
const label = params.fromFile
|
|
103
|
+
? "恢复既有计划文件"
|
|
104
|
+
: `创建「${params.title ?? "(未命名)"}」`;
|
|
105
|
+
const okSwitch = await ctx.ui.confirm(
|
|
106
|
+
"创建计划需要 plan 模式",
|
|
107
|
+
`切换到 plan 模式(只读规划)并${label}?`,
|
|
108
|
+
);
|
|
109
|
+
if (!okSwitch) {
|
|
110
|
+
return err(
|
|
111
|
+
"用户拒绝切换:计划未创建。任务可直接完成就直接实现;需要规划时请用户 /plan 切换。",
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const sw = engine.tryApply({ t: "mode.set", mode: "plan" }, ctx);
|
|
115
|
+
if ("error" in sw) return err(sw.error);
|
|
116
|
+
}
|
|
117
|
+
// fromFile 恢复:既有计划文件继续(换会话后接着干)。
|
|
118
|
+
// plans 目录内的文件原地接管(不产生重复副本);外部文件复制到 canonical
|
|
119
|
+
// 路径并在原文件文末标记「已接管」,避免两份同内容文件分不清哪份有效。
|
|
120
|
+
if (params.fromFile) {
|
|
121
|
+
const src = path.isAbsolute(params.fromFile)
|
|
122
|
+
? params.fromFile
|
|
123
|
+
: path.join(ctx.cwd, params.fromFile);
|
|
124
|
+
let content: string;
|
|
125
|
+
try {
|
|
126
|
+
content = fs.readFileSync(src, "utf8");
|
|
127
|
+
} catch {
|
|
128
|
+
return err(`计划文件读取失败:${src}`);
|
|
129
|
+
}
|
|
130
|
+
if (!content.trim()) return err(`计划文件为空:${src}`);
|
|
131
|
+
const title =
|
|
132
|
+
params.title?.trim() ||
|
|
133
|
+
/^#\s+(.+)$/m.exec(content)?.[1]?.trim() ||
|
|
134
|
+
"恢复的计划";
|
|
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);
|
|
139
|
+
const r = engine.tryApply(
|
|
140
|
+
{
|
|
141
|
+
t: "plan.create",
|
|
142
|
+
title,
|
|
143
|
+
scope: content,
|
|
144
|
+
path: p,
|
|
145
|
+
},
|
|
146
|
+
ctx,
|
|
147
|
+
);
|
|
148
|
+
if ("error" in r) {
|
|
149
|
+
// 占位文件已分配但计划未建:回收空占位,不留孤儿文件
|
|
150
|
+
if (!inPlace) {
|
|
151
|
+
try {
|
|
152
|
+
if (fs.statSync(p).size === 0) fs.unlinkSync(p);
|
|
153
|
+
} catch {
|
|
154
|
+
// 回收失败不掩盖主错误
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return err(r.error);
|
|
158
|
+
}
|
|
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);
|
|
166
|
+
return text(
|
|
167
|
+
`计划已从 ${src} 恢复(seq=1)。canonical 路径:${p}${marked ? "\n原文件文末已标记「已接管」,以新路径为准。" : ""}\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
78
170
|
if (!params.title)
|
|
79
171
|
return err("new 需要 title(动作+对象式单一标题)。");
|
|
80
172
|
if (!params.scope) return err("new 需要 scope(详细范围)。");
|
|
173
|
+
// 入口询问(镜像 plan_exit 出口审批):plan 模式且无计划时,用户未
|
|
174
|
+
// 明确要求规划就先让用户决定「创建计划 / 直接执行」,直接执行则切 build 不建计划
|
|
175
|
+
if (
|
|
176
|
+
s.mode === "plan" &&
|
|
177
|
+
!s.plan &&
|
|
178
|
+
params.planningRequested !== true &&
|
|
179
|
+
ctx.hasUI
|
|
180
|
+
) {
|
|
181
|
+
const choice = await ctx.ui.select(
|
|
182
|
+
`请选择新任务的处理方式:${params.title}`,
|
|
183
|
+
[PLAN_FIRST, DIRECT_EXECUTE],
|
|
184
|
+
);
|
|
185
|
+
if (choice === DIRECT_EXECUTE) {
|
|
186
|
+
const sw = engine.tryApply({ t: "mode.set", mode: "build" }, ctx);
|
|
187
|
+
if ("error" in sw) return err(sw.error);
|
|
188
|
+
return text(
|
|
189
|
+
"用户选择直接执行:已切换到 build 模式,计划未创建。请直接实现该任务(普通 build 模式工作流);不要再次调用 plan_task new。",
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
if (choice === undefined) {
|
|
193
|
+
return text(
|
|
194
|
+
"用户取消了选择:计划未创建、模式未变。请等待用户进一步指示,不要自行开始规划或实现。",
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
// 选了「创建计划」:继续创建计划
|
|
198
|
+
}
|
|
81
199
|
const p = nextPlanPath(ctx.cwd, params.title);
|
|
82
200
|
const r = engine.tryApply(
|
|
83
201
|
{
|
|
@@ -105,11 +223,41 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
105
223
|
if (!s.plan) return err("当前没有计划。先 plan_task new。");
|
|
106
224
|
if (expected !== s.plan.seq) {
|
|
107
225
|
return err(
|
|
108
|
-
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq}
|
|
226
|
+
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq} 不匹配(计划已被其他转移更新)。无副作用动作可改用 seq=${s.plan.seq} 直接重试;include 等范围类动作请先 plan_task show 核对最新范围再重试。`,
|
|
109
227
|
);
|
|
110
228
|
}
|
|
111
229
|
|
|
112
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
|
+
}
|
|
113
261
|
case "update": {
|
|
114
262
|
if (!params.title) return err("update 需要 title。");
|
|
115
263
|
const r = engine.tryApply(
|
|
@@ -132,7 +280,7 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
132
280
|
return "error" in r
|
|
133
281
|
? err(r.error)
|
|
134
282
|
: text(
|
|
135
|
-
`范围已追加(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。)" : ""}`,
|
|
136
284
|
);
|
|
137
285
|
}
|
|
138
286
|
case "exclude": {
|
|
@@ -156,7 +304,9 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
156
304
|
);
|
|
157
305
|
return "error" in r
|
|
158
306
|
? err(r.error)
|
|
159
|
-
: text(
|
|
307
|
+
: text(
|
|
308
|
+
`计划已放弃。文件保留:${s.plan.path}${engine.lastArchive?.ok ? "(文末已写入状态块)" : ""}`,
|
|
309
|
+
);
|
|
160
310
|
}
|
|
161
311
|
default:
|
|
162
312
|
return err("未知 action。");
|