@wbline/pi-plan-build 0.0.0-stage → 0.0.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/CHANGELOG.md +62 -0
- package/LICENSE +21 -0
- package/README.md +91 -3
- package/package.json +52 -6
- package/src/config.ts +66 -0
- package/src/context.ts +97 -0
- package/src/core/guard.ts +168 -0
- package/src/core/prompts.ts +56 -0
- package/src/core/schema.ts +144 -0
- package/src/core/state.ts +409 -0
- package/src/core/steps.ts +94 -0
- package/src/engine.ts +170 -0
- package/src/index.ts +258 -0
- package/src/lifecycle.ts +92 -0
- package/src/tools/index.ts +16 -0
- package/src/tools/plan-exit.ts +95 -0
- package/src/tools/plan-outcome.ts +120 -0
- package/src/tools/plan-question.ts +80 -0
- package/src/tools/plan-steps.ts +153 -0
- package/src/tools/plan-task.ts +177 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** plan_complete + plan_set_outcome:结局记录。 */
|
|
2
|
+
|
|
3
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
4
|
+
import type {
|
|
5
|
+
ExtensionAPI,
|
|
6
|
+
ExtensionToolContext,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { Type } from "typebox";
|
|
9
|
+
import type { PlanOutcome } from "../core/state.ts";
|
|
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
|
+
});
|
|
36
|
+
|
|
37
|
+
export function registerPlanOutcome(
|
|
38
|
+
pi: ExtensionAPI,
|
|
39
|
+
engine: PlanEngine,
|
|
40
|
+
): void {
|
|
41
|
+
pi.registerTool({
|
|
42
|
+
name: "plan_complete",
|
|
43
|
+
label: "Plan Complete",
|
|
44
|
+
description:
|
|
45
|
+
"标记整个计划完成:仅在全部批准工作与必需检查通过后,或用户明确指示整计划完成时调用(分步执行进行中也允许,会如实收尾)。绝不虚构未执行的检查。",
|
|
46
|
+
parameters: CompleteParams,
|
|
47
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
48
|
+
if (!engine.state.plan) return fail("当前没有计划。");
|
|
49
|
+
engine.validationAsked = false;
|
|
50
|
+
const r = engine.apply(
|
|
51
|
+
{ t: "plan.complete", summary: params.summary },
|
|
52
|
+
ctx,
|
|
53
|
+
);
|
|
54
|
+
return ok(
|
|
55
|
+
`计划已标记完成。${r.note ?? ""}${params.summary ? `\n摘要:${params.summary}` : ""}`,
|
|
56
|
+
);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
pi.registerTool({
|
|
61
|
+
name: "plan_set_outcome",
|
|
62
|
+
label: "Plan Set Outcome",
|
|
63
|
+
description:
|
|
64
|
+
"记录未完成结局并保持计划开放:awaiting_validation(必需的用户验证,userAction 必填)、blocked / waiting_for_input / still_working(reason 必填)。绝不因空闲推断成功。",
|
|
65
|
+
parameters: FinishParams,
|
|
66
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
67
|
+
if (!engine.state.plan) return fail("当前没有计划。");
|
|
68
|
+
if (
|
|
69
|
+
params.outcome === "awaiting_validation" &&
|
|
70
|
+
!params.userAction?.trim()
|
|
71
|
+
) {
|
|
72
|
+
return fail(
|
|
73
|
+
"awaiting_validation 需要 userAction:用户要执行的精确验证步骤(多项检查每项一个简短要点)。",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if (params.outcome !== "awaiting_validation" && !params.reason?.trim()) {
|
|
77
|
+
return fail(`${params.outcome} 需要 reason。`);
|
|
78
|
+
}
|
|
79
|
+
let outcome: PlanOutcome;
|
|
80
|
+
switch (params.outcome) {
|
|
81
|
+
case "awaiting_validation":
|
|
82
|
+
outcome = {
|
|
83
|
+
kind: "awaiting_validation",
|
|
84
|
+
userAction: (params.userAction ?? "").trim(),
|
|
85
|
+
};
|
|
86
|
+
break;
|
|
87
|
+
case "blocked":
|
|
88
|
+
case "waiting_for_input":
|
|
89
|
+
case "still_working":
|
|
90
|
+
outcome = {
|
|
91
|
+
kind: params.outcome,
|
|
92
|
+
reason: (params.reason ?? "").trim(),
|
|
93
|
+
};
|
|
94
|
+
break;
|
|
95
|
+
default:
|
|
96
|
+
return fail(`未知 outcome:${String(params.outcome)}`);
|
|
97
|
+
}
|
|
98
|
+
const r = engine.apply({ t: "plan.finish", outcome }, ctx);
|
|
99
|
+
engine.validationAsked = false;
|
|
100
|
+
const action =
|
|
101
|
+
outcome.kind === "awaiting_validation"
|
|
102
|
+
? `\n请在总结后向用户展示必需验证步骤:${outcome.userAction}`
|
|
103
|
+
: "";
|
|
104
|
+
return ok(
|
|
105
|
+
`已记录结局:${outcome.kind}。计划保持开放。${r.note ?? ""}${action}`,
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function ok(t: string) {
|
|
112
|
+
return { content: [{ type: "text" as const, text: t }], details: undefined };
|
|
113
|
+
}
|
|
114
|
+
function fail(t: string) {
|
|
115
|
+
return {
|
|
116
|
+
content: [{ type: "text" as const, text: t }],
|
|
117
|
+
details: undefined,
|
|
118
|
+
isError: true as const,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** question:结构化提问工具(精简自官方 question.ts 模式)。 */
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ExtensionAPI,
|
|
5
|
+
ExtensionToolContext,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
|
|
9
|
+
const QuestionT = Type.Object({
|
|
10
|
+
question: Type.String({ description: "问题" }),
|
|
11
|
+
header: Type.String({ description: "短标题(≤12 字符)" }),
|
|
12
|
+
options: Type.Array(
|
|
13
|
+
Type.Object({
|
|
14
|
+
label: Type.String({ description: "选项显示文本" }),
|
|
15
|
+
description: Type.Optional(Type.String({ description: "选项说明" })),
|
|
16
|
+
}),
|
|
17
|
+
{ minItems: 2, maxItems: 4 },
|
|
18
|
+
),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const Params = Type.Object({
|
|
22
|
+
questions: Type.Array(QuestionT, { minItems: 1, maxItems: 4 }),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export function registerQuestion(pi: ExtensionAPI): void {
|
|
26
|
+
pi.registerTool({
|
|
27
|
+
name: "plan_question",
|
|
28
|
+
label: "Plan Question",
|
|
29
|
+
description:
|
|
30
|
+
"向用户提出结构化选择题,收集决策/澄清歧义。推荐选项放第一个并在 label 尾部加“(推荐)”。",
|
|
31
|
+
parameters: Params,
|
|
32
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
33
|
+
if (!ctx.hasUI) {
|
|
34
|
+
const rendered = params.questions
|
|
35
|
+
.map(
|
|
36
|
+
(q, i) =>
|
|
37
|
+
`${i + 1}. ${q.question}\n${q.options.map((o) => ` - ${o.label}${o.description ? `:${o.description}` : ""}`).join("\n")}`,
|
|
38
|
+
)
|
|
39
|
+
.join("\n");
|
|
40
|
+
return {
|
|
41
|
+
content: [
|
|
42
|
+
{
|
|
43
|
+
type: "text" as const,
|
|
44
|
+
text: `当前无交互 UI,请把问题作为文本等待用户回复:\n${rendered}`,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
details: undefined,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const answers: string[] = [];
|
|
51
|
+
for (const q of params.questions) {
|
|
52
|
+
const choice = await ctx.ui.select(
|
|
53
|
+
q.question,
|
|
54
|
+
q.options.map((o) => o.label),
|
|
55
|
+
);
|
|
56
|
+
if (choice === undefined) {
|
|
57
|
+
return {
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: "text" as const,
|
|
61
|
+
text: "用户取消了提问。请根据上下文继续,不要重复询问。",
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
details: undefined,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
answers.push(`${q.header}: ${choice}`);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
content: [
|
|
71
|
+
{
|
|
72
|
+
type: "text" as const,
|
|
73
|
+
text: `用户的选择:\n${answers.join("\n")}`,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
details: undefined,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** plan_step_control + plan_step_complete:分步执行控制(AI 完成当前步用 plan_step_complete)。 */
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
4
|
+
import type {
|
|
5
|
+
ExtensionAPI,
|
|
6
|
+
ExtensionToolContext,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { Type } from "typebox";
|
|
9
|
+
import { reviseStepInMarkdown } from "../core/steps.ts";
|
|
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
|
+
});
|
|
38
|
+
|
|
39
|
+
export function registerPlanStepTools(
|
|
40
|
+
pi: ExtensionAPI,
|
|
41
|
+
engine: PlanEngine,
|
|
42
|
+
): void {
|
|
43
|
+
pi.registerTool({
|
|
44
|
+
name: "plan_step_control",
|
|
45
|
+
label: "Plan Step Control",
|
|
46
|
+
description:
|
|
47
|
+
"分步执行控制:start 批准并开始就绪步;acknowledge 指认用户声明的已完成步(当前步无需先 start;记录既有事实,不授权新工作,AI 自己完成当前步用 plan_step_complete);skip 跳过;revise 修订未完成步文本;pause/resume 暂停恢复;cancel 取消分步执行(计划保留)。一次一个转移。",
|
|
48
|
+
parameters: ControlParams,
|
|
49
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
50
|
+
switch (params.action) {
|
|
51
|
+
case "start": {
|
|
52
|
+
const r = engine.tryApply({ t: "exec.step.start" }, ctx);
|
|
53
|
+
if ("error" in r) return err(r.error);
|
|
54
|
+
const st = r.state.execution?.steps.find(
|
|
55
|
+
(x) => x.status === "active",
|
|
56
|
+
);
|
|
57
|
+
if (!st) return err("没有进行中的步骤。");
|
|
58
|
+
const total = r.state.execution?.steps.length ?? 0;
|
|
59
|
+
return ok(
|
|
60
|
+
`第 ${st.n}/${total} 步已开始:${st.text}\n只实现本步,完成后调用 plan_step_complete。`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
case "acknowledge": {
|
|
64
|
+
const r = engine.tryApply(
|
|
65
|
+
{ t: "exec.step.ack", summary: params.summary },
|
|
66
|
+
ctx,
|
|
67
|
+
);
|
|
68
|
+
if ("error" in r) return err(r.error);
|
|
69
|
+
return ok(r.note ?? "步骤已记录完成。");
|
|
70
|
+
}
|
|
71
|
+
case "skip": {
|
|
72
|
+
const r = engine.tryApply({ t: "exec.step.skip" }, ctx);
|
|
73
|
+
if ("error" in r) return err(r.error);
|
|
74
|
+
return ok(r.note ?? "步骤已跳过。");
|
|
75
|
+
}
|
|
76
|
+
case "revise": {
|
|
77
|
+
const s = engine.state;
|
|
78
|
+
const execution = s.execution;
|
|
79
|
+
if (!execution) return err("未处于分步执行。");
|
|
80
|
+
const n =
|
|
81
|
+
params.step ?? execution.steps.find((x) => x.status === "ready")?.n;
|
|
82
|
+
if (!n || !params.text)
|
|
83
|
+
return err("revise 需要 step(步骤号)和 text(新文本)。");
|
|
84
|
+
// 先过状态机校验(不存在/已完成/进行中的步骤会被拒绝),成功后再同步计划文件
|
|
85
|
+
const r = engine.tryApply(
|
|
86
|
+
{ t: "exec.step.revise", n, text: params.text },
|
|
87
|
+
ctx,
|
|
88
|
+
);
|
|
89
|
+
if ("error" in r) return err(r.error);
|
|
90
|
+
const savedPath = r.state.plan?.path;
|
|
91
|
+
if (savedPath && fs.existsSync(savedPath)) {
|
|
92
|
+
const md = fs.readFileSync(savedPath, "utf8");
|
|
93
|
+
const revised = reviseStepInMarkdown(md, n, params.text);
|
|
94
|
+
if (revised !== null) fs.writeFileSync(savedPath, revised);
|
|
95
|
+
else
|
|
96
|
+
return ok(
|
|
97
|
+
`第 ${n} 步已修订(状态);计划文件中未找到对应步骤标记,文件未同步。`,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return ok(`第 ${n} 步已修订:${params.text}`);
|
|
101
|
+
}
|
|
102
|
+
case "pause": {
|
|
103
|
+
const r = engine.tryApply({ t: "exec.pause" }, ctx);
|
|
104
|
+
return "error" in r ? err(r.error) : ok(r.note ?? "已暂停。");
|
|
105
|
+
}
|
|
106
|
+
case "resume": {
|
|
107
|
+
const r = engine.tryApply({ t: "exec.resume" }, ctx);
|
|
108
|
+
return "error" in r ? err(r.error) : ok(r.note ?? "已恢复。");
|
|
109
|
+
}
|
|
110
|
+
case "cancel": {
|
|
111
|
+
const r = engine.tryApply({ t: "exec.cancel" }, ctx);
|
|
112
|
+
return "error" in r ? err(r.error) : ok(r.note ?? "分步执行已取消。");
|
|
113
|
+
}
|
|
114
|
+
default:
|
|
115
|
+
return err(`未知 action:${String(params.action)}`);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
pi.registerTool({
|
|
121
|
+
name: "plan_step_complete",
|
|
122
|
+
label: "Plan Step Complete",
|
|
123
|
+
description:
|
|
124
|
+
"完成当前进行中的分步(仅在本步实现与适用检查完成后调用)。摘要必填。完成立即生效,不等待用户验收;绝不开始下一步。",
|
|
125
|
+
parameters: CompleteParams,
|
|
126
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
127
|
+
const r = engine.tryApply(
|
|
128
|
+
{ t: "exec.step.complete", summary: params.summary },
|
|
129
|
+
ctx,
|
|
130
|
+
);
|
|
131
|
+
if ("error" in r) return err(r.error);
|
|
132
|
+
if (!r.state.plan) {
|
|
133
|
+
return ok(`全部步骤完成,计划已收尾。\n${r.note ?? ""}`);
|
|
134
|
+
}
|
|
135
|
+
const next = r.state.execution?.steps.find((x) => x.status === "ready");
|
|
136
|
+
if (!next) return ok(`${r.note ?? "步骤完成。"}`);
|
|
137
|
+
return ok(
|
|
138
|
+
`${r.note ?? "步骤完成。"}\n等待用户明确批准后(plan_step_control start),再开始第 ${next.n} 步:${next.text}`,
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function ok(t: string) {
|
|
145
|
+
return { content: [{ type: "text" as const, text: t }], details: undefined };
|
|
146
|
+
}
|
|
147
|
+
function err(t: string) {
|
|
148
|
+
return {
|
|
149
|
+
content: [{ type: "text" as const, text: t }],
|
|
150
|
+
details: undefined,
|
|
151
|
+
isError: true as const,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/** plan_task:任务生命周期元数据(new/title/include/exclude/abandon/list/show)。 */
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
5
|
+
import type {
|
|
6
|
+
ExtensionAPI,
|
|
7
|
+
ExtensionToolContext,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { Type } from "typebox";
|
|
10
|
+
import { statusLine } from "../core/state.ts";
|
|
11
|
+
import type { PlanEngine } from "../engine.ts";
|
|
12
|
+
import { nextPlanPath } from "../engine.ts";
|
|
13
|
+
|
|
14
|
+
const Params = Type.Object({
|
|
15
|
+
action: StringEnum([
|
|
16
|
+
"new",
|
|
17
|
+
"update",
|
|
18
|
+
"include",
|
|
19
|
+
"exclude",
|
|
20
|
+
"abandon",
|
|
21
|
+
"list",
|
|
22
|
+
"show",
|
|
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
|
+
});
|
|
39
|
+
|
|
40
|
+
export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
41
|
+
pi.registerTool({
|
|
42
|
+
name: "plan_task",
|
|
43
|
+
label: "Plan Task",
|
|
44
|
+
description:
|
|
45
|
+
"计划任务生命周期:new 建计划(Plan 模式);update 改名;include 追加用户批准的范围;exclude 记录排除话题;abandon 显式放弃(须用户指示);list/show 查看状态。转移必须先于依赖它的写入/命令单独完成。",
|
|
46
|
+
parameters: Params,
|
|
47
|
+
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
48
|
+
const s = engine.state;
|
|
49
|
+
|
|
50
|
+
if (params.action === "list") {
|
|
51
|
+
return text(
|
|
52
|
+
s.plan
|
|
53
|
+
? `当前计划:${statusLine(s)}\nseq=${s.plan.seq}`
|
|
54
|
+
: "当前没有计划。",
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (params.action === "show") {
|
|
58
|
+
if (!s.plan) return text("当前没有计划。");
|
|
59
|
+
const body = [
|
|
60
|
+
`标题:${s.plan.title}`,
|
|
61
|
+
`seq:${s.plan.seq}`,
|
|
62
|
+
`文件:${s.plan.path}${s.plan.saved ? "" : "(未写盘)"}`,
|
|
63
|
+
`模式:${s.mode}`,
|
|
64
|
+
s.plan.outcome ? `结局:${s.plan.outcome.kind}` : "结局:进行中",
|
|
65
|
+
"",
|
|
66
|
+
"## 范围",
|
|
67
|
+
s.plan.scope,
|
|
68
|
+
].join("\n");
|
|
69
|
+
return text(body);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 并发守卫:除 list/show 外都要匹配 expectedAttached
|
|
73
|
+
const expected = params.expectedAttached;
|
|
74
|
+
if (params.action === "new") {
|
|
75
|
+
if (expected !== null && expected !== undefined) {
|
|
76
|
+
return err("plan_task new 需要 expectedAttached: null。");
|
|
77
|
+
}
|
|
78
|
+
if (!params.title)
|
|
79
|
+
return err("new 需要 title(动作+对象式单一标题)。");
|
|
80
|
+
if (!params.scope) return err("new 需要 scope(详细范围)。");
|
|
81
|
+
const p = nextPlanPath(ctx.cwd, params.title);
|
|
82
|
+
const r = engine.tryApply(
|
|
83
|
+
{
|
|
84
|
+
t: "plan.create",
|
|
85
|
+
title: params.title,
|
|
86
|
+
scope: params.scope,
|
|
87
|
+
path: p,
|
|
88
|
+
},
|
|
89
|
+
ctx,
|
|
90
|
+
);
|
|
91
|
+
if ("error" in r) {
|
|
92
|
+
// 占位文件已分配但计划未建(如已有未完成计划):回收空占位,不留孤儿文件
|
|
93
|
+
try {
|
|
94
|
+
if (fs.statSync(p).size === 0) fs.unlinkSync(p);
|
|
95
|
+
} catch {
|
|
96
|
+
// 回收失败不掩盖主错误
|
|
97
|
+
}
|
|
98
|
+
return err(r.error);
|
|
99
|
+
}
|
|
100
|
+
return text(
|
|
101
|
+
`计划已创建(seq=1)。canonical 路径:${p}\n请完成只读调研后把完整计划写入该文件,然后调用 plan_exit。`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!s.plan) return err("当前没有计划。先 plan_task new。");
|
|
106
|
+
if (expected !== undefined && expected !== s.plan.seq) {
|
|
107
|
+
return err(
|
|
108
|
+
`expectedAttached=${expected} 已过期,当前 seq=${s.plan.seq}。请重读 plan_task show 后重试,不要盲目换目标。`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
switch (params.action) {
|
|
113
|
+
case "update": {
|
|
114
|
+
if (!params.title) return err("update 需要 title。");
|
|
115
|
+
const r = engine.tryApply(
|
|
116
|
+
{ t: "plan.title", title: params.title },
|
|
117
|
+
ctx,
|
|
118
|
+
);
|
|
119
|
+
return "error" in r
|
|
120
|
+
? err(r.error)
|
|
121
|
+
: text(`标题已更新:${r.state.plan?.title ?? ""}`);
|
|
122
|
+
}
|
|
123
|
+
case "include": {
|
|
124
|
+
if (!params.scope)
|
|
125
|
+
return err(
|
|
126
|
+
"include 需要 scope(用户批准的追加内容,完整合并范围)。",
|
|
127
|
+
);
|
|
128
|
+
const r = engine.tryApply(
|
|
129
|
+
{ t: "plan.scope.append", addition: params.scope },
|
|
130
|
+
ctx,
|
|
131
|
+
);
|
|
132
|
+
return "error" in r
|
|
133
|
+
? err(r.error)
|
|
134
|
+
: text(
|
|
135
|
+
`范围已追加(seq=${r.state.plan?.seq ?? 1})。请相应更新计划文件;若文件已审阅过,需重新 plan_exit。`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
case "exclude": {
|
|
139
|
+
if (!params.scope) return err("exclude 需要 scope(排除的话题)。");
|
|
140
|
+
const r = engine.tryApply(
|
|
141
|
+
{ t: "plan.exclude", topic: params.scope },
|
|
142
|
+
ctx,
|
|
143
|
+
);
|
|
144
|
+
return "error" in r
|
|
145
|
+
? err(r.error)
|
|
146
|
+
: text(`已记录排除话题:${params.scope}`);
|
|
147
|
+
}
|
|
148
|
+
case "abandon": {
|
|
149
|
+
if (!params.title)
|
|
150
|
+
return err(
|
|
151
|
+
"abandon 需要原因(放在 title 参数)。仅限用户明确指示后调用。",
|
|
152
|
+
);
|
|
153
|
+
const r = engine.tryApply(
|
|
154
|
+
{ t: "plan.abandon", reason: params.title },
|
|
155
|
+
ctx,
|
|
156
|
+
);
|
|
157
|
+
return "error" in r
|
|
158
|
+
? err(r.error)
|
|
159
|
+
: text(`计划已放弃。文件保留:${s.plan.path}`);
|
|
160
|
+
}
|
|
161
|
+
default:
|
|
162
|
+
return err("未知 action。");
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function text(t: string) {
|
|
169
|
+
return { content: [{ type: "text" as const, text: t }], details: undefined };
|
|
170
|
+
}
|
|
171
|
+
function err(t: string) {
|
|
172
|
+
return {
|
|
173
|
+
content: [{ type: "text" as const, text: t }],
|
|
174
|
+
details: undefined,
|
|
175
|
+
isError: true as const,
|
|
176
|
+
};
|
|
177
|
+
}
|