@wbline/pi-plan-build 0.0.9 → 0.1.0
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 +171 -148
- package/LICENSE +21 -21
- package/README.md +119 -116
- package/index.ts +6 -6
- package/package.json +53 -53
- package/src/context.ts +13 -2
- package/src/core/archive.ts +10 -17
- package/src/core/guard.ts +48 -4
- package/src/core/limits.ts +31 -0
- package/src/core/schema.ts +10 -0
- package/src/core/state.ts +46 -5
- package/src/core/steps.ts +50 -31
- package/src/engine.ts +57 -13
- package/src/index.ts +66 -62
- package/src/lifecycle.ts +41 -6
- package/src/tools/descriptions.ts +6 -6
- package/src/tools/index.ts +1 -1
- package/src/tools/plan-exit.ts +28 -5
- package/src/tools/plan-outcome.ts +37 -1
- package/src/tools/plan-question.ts +48 -10
- package/src/tools/plan-steps.ts +36 -4
- package/src/tools/plan-task.ts +93 -13
- package/src/ui.ts +80 -0
package/src/tools/index.ts
CHANGED
|
@@ -12,5 +12,5 @@ export function registerAllTools(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
12
12
|
registerPlanExit(pi, engine);
|
|
13
13
|
registerPlanStepTools(pi, engine);
|
|
14
14
|
registerPlanOutcome(pi, engine);
|
|
15
|
-
registerQuestion(pi, engine.config.lang);
|
|
15
|
+
registerQuestion(pi, () => engine.config.lang);
|
|
16
16
|
}
|
package/src/tools/plan-exit.ts
CHANGED
|
@@ -5,8 +5,10 @@ import type {
|
|
|
5
5
|
ExtensionToolContext,
|
|
6
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { Type } from "typebox";
|
|
8
|
+
import { LIMITS } from "../core/limits.ts";
|
|
8
9
|
import { parseSteps } from "../core/steps.ts";
|
|
9
10
|
import type { PlanEngine } from "../engine.ts";
|
|
11
|
+
import { selectWithDescriptions } from "../ui.ts";
|
|
10
12
|
import { toolTexts } from "./descriptions.ts";
|
|
11
13
|
|
|
12
14
|
const Params = Type.Object({});
|
|
@@ -33,6 +35,11 @@ export function registerPlanExit(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
33
35
|
`计划文件尚未写盘:${plan.path}。请先用文件写入工具把完整计划写入该文件,成功后调用 plan_exit。`,
|
|
34
36
|
);
|
|
35
37
|
}
|
|
38
|
+
const st = fs.statSync(plan.path);
|
|
39
|
+
if (st.size > LIMITS.planFileBytes)
|
|
40
|
+
return fail(
|
|
41
|
+
`计划文件过大(${Math.round(st.size / 1024)}KB,上限 ${Math.round(LIMITS.planFileBytes / 1024)}KB):计划应精炼可执行,请拆分或精简后重试。`,
|
|
42
|
+
);
|
|
36
43
|
const markdown = fs.readFileSync(plan.path, "utf8");
|
|
37
44
|
if (!markdown.trim()) {
|
|
38
45
|
// nextPlanPath 分配即建占位空文件,存在性检查恒真——必须校验内容非空
|
|
@@ -55,11 +62,27 @@ export function registerPlanExit(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
55
62
|
if (headless) {
|
|
56
63
|
choice = HERE;
|
|
57
64
|
} else {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
const idx = await selectWithDescriptions(
|
|
66
|
+
ctx,
|
|
67
|
+
`请选择计划的实现方式:${plan.title}`,
|
|
68
|
+
[
|
|
69
|
+
{
|
|
70
|
+
label: HERE,
|
|
71
|
+
description:
|
|
72
|
+
"切到 build 模式,在已批准范围内一次做完;按计划 ## 验证 小节执行检查,完成后 plan_complete 收尾",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
label: STEPWISE,
|
|
76
|
+
description:
|
|
77
|
+
"切到 build 模式并按计划步骤逐个实现;每步等你批准后才开工(「剩下的都做完」可授权连跑)",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
label: STAY,
|
|
81
|
+
description: "不进入实现:计划保持开放,继续讨论或由你另行安排",
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
);
|
|
85
|
+
choice = idx === undefined ? undefined : [HERE, STEPWISE, STAY][idx];
|
|
63
86
|
if (choice === undefined) {
|
|
64
87
|
// 用户关闭/取消审批 ≠ 主动选择「留在 plan」:交回用户,让模型询问意图
|
|
65
88
|
return ok(
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ExtensionToolContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { Type } from "typebox";
|
|
9
|
+
import { LIMITS, overLimit } from "../core/limits.ts";
|
|
9
10
|
import type { PlanOutcome } from "../core/state.ts";
|
|
10
11
|
import type { PlanEngine } from "../engine.ts";
|
|
11
12
|
import { toolTexts } from "./descriptions.ts";
|
|
@@ -38,7 +39,34 @@ export function registerPlanOutcome(
|
|
|
38
39
|
description: T.planComplete,
|
|
39
40
|
parameters: CompleteParams,
|
|
40
41
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
41
|
-
if (
|
|
42
|
+
if (params.summary !== undefined) {
|
|
43
|
+
const over = overLimit("完成摘要", params.summary, LIMITS.stepText);
|
|
44
|
+
if (over) return fail(over);
|
|
45
|
+
}
|
|
46
|
+
const s = engine.state;
|
|
47
|
+
if (!s.plan) return fail("当前没有计划。");
|
|
48
|
+
// 出口确认门(与 plan_exit 入口审批对称):收尾是单向动作,有 UI 时用户拍板
|
|
49
|
+
if (ctx.hasUI) {
|
|
50
|
+
const pendingSteps = s.execution
|
|
51
|
+
? s.execution.steps.filter(
|
|
52
|
+
(x) => x.status !== "completed" && x.status !== "skipped",
|
|
53
|
+
)
|
|
54
|
+
: [];
|
|
55
|
+
const pendingNote = pendingSteps.length
|
|
56
|
+
? `\n注意:分步执行进行中,第 ${pendingSteps.map((x) => x.n).join("、")} 步尚未完成,确认后将如实收尾(未完成步骤不再继续)。`
|
|
57
|
+
: "";
|
|
58
|
+
const outcomeNote = s.plan.outcome
|
|
59
|
+
? `\n当前结局:${s.plan.outcome.kind}。`
|
|
60
|
+
: "";
|
|
61
|
+
const okDone = await ctx.ui.confirm(
|
|
62
|
+
"标记计划完成",
|
|
63
|
+
`确认「${s.plan.title}」的全部批准工作与必需检查已通过?${outcomeNote}${pendingNote}`,
|
|
64
|
+
);
|
|
65
|
+
if (!okDone)
|
|
66
|
+
return fail(
|
|
67
|
+
"用户未确认完成:计划保持开放。继续未竟事项,或用 plan_set_outcome 记录未完成结局。",
|
|
68
|
+
);
|
|
69
|
+
}
|
|
42
70
|
engine.validationAsked = false;
|
|
43
71
|
const r = engine.apply(
|
|
44
72
|
{ t: "plan.complete", summary: params.summary },
|
|
@@ -71,6 +99,14 @@ export function registerPlanOutcome(
|
|
|
71
99
|
if (params.outcome !== "awaiting_validation" && !params.reason?.trim()) {
|
|
72
100
|
return fail(`${params.outcome} 需要 reason。`);
|
|
73
101
|
}
|
|
102
|
+
const actionOver = params.userAction
|
|
103
|
+
? overLimit("验证步骤", params.userAction, LIMITS.reason)
|
|
104
|
+
: undefined;
|
|
105
|
+
if (actionOver) return fail(actionOver);
|
|
106
|
+
const reasonOver = params.reason
|
|
107
|
+
? overLimit("原因", params.reason, LIMITS.reason)
|
|
108
|
+
: undefined;
|
|
109
|
+
if (reasonOver) return fail(reasonOver);
|
|
74
110
|
let outcome: PlanOutcome;
|
|
75
111
|
switch (params.outcome) {
|
|
76
112
|
case "awaiting_validation":
|
|
@@ -5,15 +5,17 @@ import type {
|
|
|
5
5
|
ExtensionToolContext,
|
|
6
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
7
|
import { Type } from "typebox";
|
|
8
|
+
import { LIMITS, overLimit } from "../core/limits.ts";
|
|
8
9
|
import type { Lang } from "../core/prompts.ts";
|
|
10
|
+
import { type SelectOption, selectWithDescriptions } from "../ui.ts";
|
|
9
11
|
import { toolTexts } from "./descriptions.ts";
|
|
10
12
|
|
|
11
13
|
/** 上游模型偶发在流式工具参数里夹带控制符(CR 会被 IDE 渲染成方块、零宽字符不可见),UI 文本一律清洗。 */
|
|
12
14
|
const stripControlNoise = (s: string): string =>
|
|
13
15
|
s.replace(/[\r\u200b-\u200d\ufeff]/g, "");
|
|
14
16
|
|
|
15
|
-
export function registerQuestion(pi: ExtensionAPI,
|
|
16
|
-
const T = toolTexts(
|
|
17
|
+
export function registerQuestion(pi: ExtensionAPI, getLang: () => Lang): void {
|
|
18
|
+
const T = toolTexts(getLang());
|
|
17
19
|
const QuestionT = Type.Object({
|
|
18
20
|
question: Type.String({ description: T.questionPrompt }),
|
|
19
21
|
header: Type.String({ description: T.questionHeader }),
|
|
@@ -21,7 +23,9 @@ export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
|
21
23
|
Type.Object({
|
|
22
24
|
label: Type.String({ description: T.questionLabel }),
|
|
23
25
|
description: Type.Optional(
|
|
24
|
-
Type.String({
|
|
26
|
+
Type.String({
|
|
27
|
+
description: T.questionDescription,
|
|
28
|
+
}),
|
|
25
29
|
),
|
|
26
30
|
}),
|
|
27
31
|
{ minItems: 2, maxItems: 4 },
|
|
@@ -49,6 +53,21 @@ export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
|
49
53
|
: {}),
|
|
50
54
|
})),
|
|
51
55
|
}));
|
|
56
|
+
// UI 文本限长:弹窗渲染与回传结果都不该被超长文本撑爆
|
|
57
|
+
for (const q of questions) {
|
|
58
|
+
const qOver = overLimit("问题", q.question, 4 * 1024);
|
|
59
|
+
if (qOver) return fail(qOver);
|
|
60
|
+
const hOver = overLimit("标题", q.header, LIMITS.title);
|
|
61
|
+
if (hOver) return fail(hOver);
|
|
62
|
+
for (const o of q.options) {
|
|
63
|
+
const lOver = overLimit("选项", o.label, LIMITS.excludeTopic);
|
|
64
|
+
if (lOver) return fail(lOver);
|
|
65
|
+
if (o.description) {
|
|
66
|
+
const dOver = overLimit("选项说明", o.description, LIMITS.reason);
|
|
67
|
+
if (dOver) return fail(dOver);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
52
71
|
if (!ctx.hasUI) {
|
|
53
72
|
const rendered = questions
|
|
54
73
|
.map(
|
|
@@ -68,14 +87,25 @@ export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
|
68
87
|
}
|
|
69
88
|
const answers: string[] = [];
|
|
70
89
|
for (const q of questions) {
|
|
71
|
-
//
|
|
90
|
+
// 自由输入入口等运行时文案随 config.lang 动态读取(菜单切语言立即生效;
|
|
91
|
+
// 工具描述字符串由 pi 在注册时固定,重启后生效)
|
|
92
|
+
const T = toolTexts(getLang());
|
|
72
93
|
const custom = T.questionCustom;
|
|
73
|
-
|
|
74
|
-
|
|
94
|
+
// description 参数真正渲染到弹窗(此前只在无 UI 文本回退里使用)
|
|
95
|
+
let opts: SelectOption[] = q.options.map((o) => ({
|
|
96
|
+
label: o.label,
|
|
97
|
+
...(o.description ? { description: o.description } : {}),
|
|
98
|
+
}));
|
|
99
|
+
if (!opts.some((o) => o.label === custom))
|
|
100
|
+
opts = [
|
|
101
|
+
...opts,
|
|
102
|
+
{ label: custom, description: T.questionCustomPrompt },
|
|
103
|
+
];
|
|
104
|
+
const customIdx = opts.findIndex((o) => o.label === custom);
|
|
75
105
|
// 输入为空或取消 → 回到选项列表(镜像官方示例:编辑器 Esc 返回选项)
|
|
76
106
|
while (true) {
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
107
|
+
const idx = await selectWithDescriptions(ctx, q.question, opts);
|
|
108
|
+
if (idx === undefined) {
|
|
79
109
|
return {
|
|
80
110
|
content: [
|
|
81
111
|
{
|
|
@@ -86,8 +116,8 @@ export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
|
86
116
|
details: undefined,
|
|
87
117
|
};
|
|
88
118
|
}
|
|
89
|
-
if (
|
|
90
|
-
answers.push(`${q.header}: ${
|
|
119
|
+
if (idx !== customIdx) {
|
|
120
|
+
answers.push(`${q.header}: ${opts[idx]?.label ?? ""}`);
|
|
91
121
|
break;
|
|
92
122
|
}
|
|
93
123
|
const customAnswer = await ctx.ui.input(
|
|
@@ -112,3 +142,11 @@ export function registerQuestion(pi: ExtensionAPI, lang: Lang): void {
|
|
|
112
142
|
},
|
|
113
143
|
});
|
|
114
144
|
}
|
|
145
|
+
|
|
146
|
+
function fail(t: string) {
|
|
147
|
+
return {
|
|
148
|
+
content: [{ type: "text" as const, text: t }],
|
|
149
|
+
details: undefined,
|
|
150
|
+
isError: true as const,
|
|
151
|
+
};
|
|
152
|
+
}
|
package/src/tools/plan-steps.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
ExtensionToolContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { Type } from "typebox";
|
|
9
|
+
import { LIMITS, overLimit } from "../core/limits.ts";
|
|
9
10
|
import { reviseStepInMarkdown } from "../core/steps.ts";
|
|
10
11
|
import type { PlanEngine } from "../engine.ts";
|
|
11
12
|
import { toolTexts } from "./descriptions.ts";
|
|
@@ -69,6 +70,10 @@ export function registerPlanStepTools(
|
|
|
69
70
|
);
|
|
70
71
|
}
|
|
71
72
|
case "acknowledge": {
|
|
73
|
+
if (params.summary !== undefined) {
|
|
74
|
+
const over = overLimit("完成摘要", params.summary, LIMITS.stepText);
|
|
75
|
+
if (over) return err(over);
|
|
76
|
+
}
|
|
72
77
|
const r = engine.tryApply(
|
|
73
78
|
{ t: "exec.step.ack", summary: params.summary },
|
|
74
79
|
ctx,
|
|
@@ -88,8 +93,15 @@ export function registerPlanStepTools(
|
|
|
88
93
|
const n =
|
|
89
94
|
params.step ?? execution.steps.find((x) => x.status === "ready")?.n;
|
|
90
95
|
if (!n || !params.text)
|
|
91
|
-
return
|
|
92
|
-
|
|
96
|
+
return execution.steps.find((x) => x.status === "active")
|
|
97
|
+
? err(
|
|
98
|
+
"当前步骤进行中不能修订:等 plan_step_complete 后,或明确指定 step 参数修订其他未完成步骤。",
|
|
99
|
+
)
|
|
100
|
+
: err("revise 需要 step(步骤号)和 text(新文本)。");
|
|
101
|
+
const textOver = overLimit("步骤文本", params.text, LIMITS.stepText);
|
|
102
|
+
if (textOver) return err(textOver);
|
|
103
|
+
// 先试算文件修订(此时不落盘),状态机校验通过且文件能定位步骤后才提交,
|
|
104
|
+
// 避免状态已改而文件写失败的半途状态
|
|
93
105
|
const r = engine.tryApply(
|
|
94
106
|
{ t: "exec.step.revise", n, text: params.text },
|
|
95
107
|
ctx,
|
|
@@ -99,11 +111,17 @@ export function registerPlanStepTools(
|
|
|
99
111
|
if (savedPath && fs.existsSync(savedPath)) {
|
|
100
112
|
const md = fs.readFileSync(savedPath, "utf8");
|
|
101
113
|
const revised = reviseStepInMarkdown(md, n, params.text);
|
|
102
|
-
if (revised
|
|
103
|
-
else
|
|
114
|
+
if (revised === null)
|
|
104
115
|
return ok(
|
|
105
116
|
`第 ${n} 步已修订(状态);计划文件中未找到对应步骤标记,文件未同步。`,
|
|
106
117
|
);
|
|
118
|
+
try {
|
|
119
|
+
fs.writeFileSync(savedPath, revised);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
return ok(
|
|
122
|
+
`第 ${n} 步已修订(状态);计划文件同步失败:${e instanceof Error ? e.message : String(e)}。请手动把第 ${n} 步改为「${params.text}」。`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
107
125
|
}
|
|
108
126
|
return ok(`第 ${n} 步已修订:${params.text}`);
|
|
109
127
|
}
|
|
@@ -131,6 +149,12 @@ export function registerPlanStepTools(
|
|
|
131
149
|
description: T.stepComplete,
|
|
132
150
|
parameters: CompleteParams,
|
|
133
151
|
async execute(_id, params, _signal, _onUpdate, ctx: ExtensionToolContext) {
|
|
152
|
+
const summaryOver = overLimit(
|
|
153
|
+
"完成摘要",
|
|
154
|
+
params.summary,
|
|
155
|
+
LIMITS.stepText,
|
|
156
|
+
);
|
|
157
|
+
if (summaryOver) return err(summaryOver);
|
|
134
158
|
const r = engine.tryApply(
|
|
135
159
|
{ t: "exec.step.complete", summary: params.summary },
|
|
136
160
|
ctx,
|
|
@@ -141,6 +165,14 @@ export function registerPlanStepTools(
|
|
|
141
165
|
`${r.note ?? "全部步骤完成,计划已收尾。"}${archiveSuffix()}`,
|
|
142
166
|
);
|
|
143
167
|
}
|
|
168
|
+
// 连跑授权下下一步直接 active:返回文本要给出"继续"指引(状态栏/context 之外的第二信源)
|
|
169
|
+
const activeNext = r.state.execution?.steps.find(
|
|
170
|
+
(x) => x.status === "active",
|
|
171
|
+
);
|
|
172
|
+
if (activeNext)
|
|
173
|
+
return ok(
|
|
174
|
+
`${r.note ?? "步骤完成。"}\n连跑授权中:直接开始第 ${activeNext.n} 步的实现(无需等用户批准)——${activeNext.text}\n遇只有用户能解决的阻塞用 plan_set_outcome 记录并停。`,
|
|
175
|
+
);
|
|
144
176
|
const next = r.state.execution?.steps.find((x) => x.status === "ready");
|
|
145
177
|
if (!next) return ok(`${r.note ?? "步骤完成。"}`);
|
|
146
178
|
return ok(
|
package/src/tools/plan-task.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** plan_task:任务生命周期元数据(new/revise/title/include/exclude/abandon/list/show)。 */
|
|
2
2
|
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
4
5
|
import path from "node:path";
|
|
5
6
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
6
7
|
import type {
|
|
@@ -8,10 +9,13 @@ import type {
|
|
|
8
9
|
ExtensionToolContext,
|
|
9
10
|
} from "@earendil-works/pi-coding-agent";
|
|
10
11
|
import { Type } from "typebox";
|
|
11
|
-
import { listPlanFiles
|
|
12
|
+
import { listPlanFiles } from "../core/archive.ts";
|
|
13
|
+
import { realpathLike } from "../core/guard.ts";
|
|
14
|
+
import { LIMITS, overLimit } from "../core/limits.ts";
|
|
12
15
|
import { statusLine } from "../core/state.ts";
|
|
13
16
|
import type { PlanEngine } from "../engine.ts";
|
|
14
17
|
import { nextPlanPath } from "../engine.ts";
|
|
18
|
+
import { selectWithDescriptions } from "../ui.ts";
|
|
15
19
|
import { toolTexts } from "./descriptions.ts";
|
|
16
20
|
|
|
17
21
|
const PLAN_FIRST = "创建计划,审阅后再实现";
|
|
@@ -87,6 +91,9 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
87
91
|
"",
|
|
88
92
|
"## 范围",
|
|
89
93
|
s.plan.scope,
|
|
94
|
+
...(s.plan.excludes?.length
|
|
95
|
+
? ["", "## 已排除话题", ...s.plan.excludes.map((x) => `- ${x}`)]
|
|
96
|
+
: []),
|
|
90
97
|
].join("\n");
|
|
91
98
|
return text(body);
|
|
92
99
|
}
|
|
@@ -118,11 +125,20 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
118
125
|
// plans 目录内的文件原地接管(不产生重复副本);外部文件复制到 canonical
|
|
119
126
|
// 路径并在原文件文末标记「已接管」,避免两份同内容文件分不清哪份有效。
|
|
120
127
|
if (params.fromFile) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
128
|
+
// 归一化顺序与文件守卫一致:先剥 @ / \\?\ / ~ 前缀,再判绝对路径
|
|
129
|
+
let src = params.fromFile.trim();
|
|
130
|
+
if (src.startsWith("@")) src = src.slice(1).trim();
|
|
131
|
+
if (src.startsWith("\\\\?\\")) src = src.slice(4);
|
|
132
|
+
if (src === "~" || src.startsWith("~/"))
|
|
133
|
+
src = path.join(os.homedir(), src.slice(1));
|
|
134
|
+
if (!path.isAbsolute(src)) src = path.join(ctx.cwd, src);
|
|
124
135
|
let content: string;
|
|
125
136
|
try {
|
|
137
|
+
const st = fs.statSync(src);
|
|
138
|
+
if (st.size > LIMITS.planFileBytes)
|
|
139
|
+
return err(
|
|
140
|
+
`计划文件过大(${Math.round(st.size / 1024)}KB,上限 ${Math.round(LIMITS.planFileBytes / 1024)}KB):不恢复异常文件。`,
|
|
141
|
+
);
|
|
126
142
|
content = fs.readFileSync(src, "utf8");
|
|
127
143
|
} catch {
|
|
128
144
|
return err(`计划文件读取失败:${src}`);
|
|
@@ -133,9 +149,28 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
133
149
|
/^#\s+(.+)$/m.exec(content)?.[1]?.trim() ||
|
|
134
150
|
"恢复的计划";
|
|
135
151
|
const plansDir = path.join(ctx.cwd, ".pi", "agent", "plans");
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
152
|
+
// symlink 解析后再比较,链接出来的 plans 目录不误判
|
|
153
|
+
const inPlace = sameDir(
|
|
154
|
+
realpathLike(path.dirname(src)),
|
|
155
|
+
realpathLike(plansDir),
|
|
156
|
+
);
|
|
157
|
+
let p: string;
|
|
158
|
+
try {
|
|
159
|
+
p = inPlace ? src : nextPlanPath(ctx.cwd, title);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
162
|
+
}
|
|
163
|
+
if (!inPlace) {
|
|
164
|
+
fs.writeFileSync(p, content);
|
|
165
|
+
// 来源注记写在 canonical 文件内(append-only),原文件不再被
|
|
166
|
+
// 扩展写入——fromFile 的读取路径不构成对任意文件的写通道
|
|
167
|
+
const d = new Date();
|
|
168
|
+
const pd = (n: number) => String(n).padStart(2, "0");
|
|
169
|
+
fs.appendFileSync(
|
|
170
|
+
p,
|
|
171
|
+
`\n---\n\n> 来源:自 \`${src}\` 复制(${d.getFullYear()}-${pd(d.getMonth() + 1)}-${pd(d.getDate())})。原文件保留未改动。`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
139
174
|
const r = engine.tryApply(
|
|
140
175
|
{
|
|
141
176
|
t: "plan.create",
|
|
@@ -162,14 +197,17 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
162
197
|
`计划已在原文件恢复(seq=1)。canonical 路径:${p}\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
163
198
|
);
|
|
164
199
|
}
|
|
165
|
-
const marked = markSuperseded(src, p);
|
|
166
200
|
return text(
|
|
167
|
-
`计划已从 ${src} 恢复(seq=1)。canonical 路径:${p}
|
|
201
|
+
`计划已从 ${src} 恢复(seq=1)。canonical 路径:${p}\n原文件保留未改动;canonical 文件文末已注明来源,以新路径为准。\n内容已就绪:可直接调用 plan_exit 提请审批;如需修订先改该文件(plan 模式允许写计划文件)再 plan_exit。`,
|
|
168
202
|
);
|
|
169
203
|
}
|
|
170
204
|
if (!params.title)
|
|
171
205
|
return err("new 需要 title(动作+对象式单一标题)。");
|
|
172
206
|
if (!params.scope) return err("new 需要 scope(详细范围)。");
|
|
207
|
+
const titleOver = overLimit("标题", params.title, LIMITS.title);
|
|
208
|
+
if (titleOver) return err(titleOver);
|
|
209
|
+
const scopeOver = overLimit("范围", params.scope, LIMITS.scope);
|
|
210
|
+
if (scopeOver) return err(scopeOver);
|
|
173
211
|
// 入口询问(镜像 plan_exit 出口审批):plan 模式且无计划时,用户未
|
|
174
212
|
// 明确要求规划就先让用户决定「创建计划 / 直接执行」,直接执行则切 build 不建计划
|
|
175
213
|
if (
|
|
@@ -178,10 +216,24 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
178
216
|
params.planningRequested !== true &&
|
|
179
217
|
ctx.hasUI
|
|
180
218
|
) {
|
|
181
|
-
const
|
|
219
|
+
const idx = await selectWithDescriptions(
|
|
220
|
+
ctx,
|
|
182
221
|
`请选择新任务的处理方式:${params.title}`,
|
|
183
|
-
[
|
|
222
|
+
[
|
|
223
|
+
{
|
|
224
|
+
label: PLAN_FIRST,
|
|
225
|
+
description:
|
|
226
|
+
"切到 plan 模式创建计划;agent 写完计划文件后经 plan_exit 审批,你批准了才开工",
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
label: DIRECT_EXECUTE,
|
|
230
|
+
description:
|
|
231
|
+
"不建计划:切到 build 模式,agent 直接实现(适合小改动或方向明确的任务)",
|
|
232
|
+
},
|
|
233
|
+
],
|
|
184
234
|
);
|
|
235
|
+
const choice =
|
|
236
|
+
idx === undefined ? undefined : [PLAN_FIRST, DIRECT_EXECUTE][idx];
|
|
185
237
|
if (choice === DIRECT_EXECUTE) {
|
|
186
238
|
const sw = engine.tryApply({ t: "mode.set", mode: "build" }, ctx);
|
|
187
239
|
if ("error" in sw) return err(sw.error);
|
|
@@ -196,7 +248,12 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
196
248
|
}
|
|
197
249
|
// 选了「创建计划」:继续创建计划
|
|
198
250
|
}
|
|
199
|
-
|
|
251
|
+
let p: string;
|
|
252
|
+
try {
|
|
253
|
+
p = nextPlanPath(ctx.cwd, params.title);
|
|
254
|
+
} catch (e) {
|
|
255
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
256
|
+
}
|
|
200
257
|
const r = engine.tryApply(
|
|
201
258
|
{
|
|
202
259
|
t: "plan.create",
|
|
@@ -223,7 +280,7 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
223
280
|
if (!s.plan) return err("当前没有计划。先 plan_task new。");
|
|
224
281
|
if (expected !== s.plan.seq) {
|
|
225
282
|
return err(
|
|
226
|
-
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq}
|
|
283
|
+
`expectedAttached=${expected ?? "(未传)"} 与当前 seq=${s.plan.seq} 不匹配(计划已被其他转移更新)。请先 plan_task show 核对最新状态,再用 seq=${s.plan.seq} 重试;不要凭旧印象直接重试——这些动作都有副作用(如 include 会重复追加范围)。`,
|
|
227
284
|
);
|
|
228
285
|
}
|
|
229
286
|
|
|
@@ -260,6 +317,8 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
260
317
|
}
|
|
261
318
|
case "update": {
|
|
262
319
|
if (!params.title) return err("update 需要 title。");
|
|
320
|
+
const titleOver = overLimit("标题", params.title, LIMITS.title);
|
|
321
|
+
if (titleOver) return err(titleOver);
|
|
263
322
|
const r = engine.tryApply(
|
|
264
323
|
{ t: "plan.title", title: params.title },
|
|
265
324
|
ctx,
|
|
@@ -273,6 +332,8 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
273
332
|
return err(
|
|
274
333
|
"include 需要 scope(用户批准的追加内容,完整合并范围)。",
|
|
275
334
|
);
|
|
335
|
+
const scopeOver = overLimit("追加范围", params.scope, LIMITS.scope);
|
|
336
|
+
if (scopeOver) return err(scopeOver);
|
|
276
337
|
const r = engine.tryApply(
|
|
277
338
|
{ t: "plan.scope.append", addition: params.scope },
|
|
278
339
|
ctx,
|
|
@@ -285,6 +346,12 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
285
346
|
}
|
|
286
347
|
case "exclude": {
|
|
287
348
|
if (!params.scope) return err("exclude 需要 scope(排除的话题)。");
|
|
349
|
+
const topicOver = overLimit(
|
|
350
|
+
"排除话题",
|
|
351
|
+
params.scope,
|
|
352
|
+
LIMITS.excludeTopic,
|
|
353
|
+
);
|
|
354
|
+
if (topicOver) return err(topicOver);
|
|
288
355
|
const r = engine.tryApply(
|
|
289
356
|
{ t: "plan.exclude", topic: params.scope },
|
|
290
357
|
ctx,
|
|
@@ -298,6 +365,19 @@ export function registerPlanTask(pi: ExtensionAPI, engine: PlanEngine): void {
|
|
|
298
365
|
return err(
|
|
299
366
|
"abandon 需要原因(放在 title 参数)。仅限用户明确指示后调用。",
|
|
300
367
|
);
|
|
368
|
+
// 放弃是单向动作:有 UI 时用户拍板(与 plan_complete 出口确认门对称)
|
|
369
|
+
if (ctx.hasUI) {
|
|
370
|
+
const okAbandon = await ctx.ui.confirm(
|
|
371
|
+
"放弃计划",
|
|
372
|
+
`确认放弃「${s.plan.title}」?原因:${params.title}\n计划文件保留在原处,分步执行进度将丢弃。`,
|
|
373
|
+
);
|
|
374
|
+
if (!okAbandon)
|
|
375
|
+
return err(
|
|
376
|
+
"用户未确认放弃:计划保持开放。继续当前计划或询问用户下一步。",
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
const reasonOver = overLimit("放弃原因", params.title, LIMITS.reason);
|
|
380
|
+
if (reasonOver) return err(reasonOver);
|
|
301
381
|
const r = engine.tryApply(
|
|
302
382
|
{ t: "plan.abandon", reason: params.title },
|
|
303
383
|
ctx,
|
package/src/ui.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 带描述的选择弹窗:选项 label 下渲染 dim 小字说明,用户不用猜选项含义。
|
|
3
|
+
*
|
|
4
|
+
* TUI:ui.custom 渲染描述列表(label 高亮 + 描述小字)。
|
|
5
|
+
* RPC/ACP(JetBrains 桥接、print/json):ui.select 只接受 string[],把描述
|
|
6
|
+
* 内联进 label("选项 —— 说明"),返回值按位置映射回索引。
|
|
7
|
+
*/
|
|
8
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { matchesKey, truncateToWidth } from "@earendil-works/pi-tui";
|
|
10
|
+
|
|
11
|
+
export interface SelectOption {
|
|
12
|
+
label: string;
|
|
13
|
+
/** 选项下方的说明小字;省略则只显示 label。 */
|
|
14
|
+
description?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class DescribedSelectComponent {
|
|
18
|
+
private selected = 0;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly title: string,
|
|
22
|
+
private readonly options: readonly SelectOption[],
|
|
23
|
+
private readonly theme: Theme,
|
|
24
|
+
private readonly done: (index: number | undefined) => void,
|
|
25
|
+
) {}
|
|
26
|
+
|
|
27
|
+
handleInput(data: string): void {
|
|
28
|
+
if (matchesKey(data, "escape")) this.done(undefined);
|
|
29
|
+
else if (matchesKey(data, "up"))
|
|
30
|
+
this.selected =
|
|
31
|
+
(this.selected - 1 + this.options.length) % this.options.length;
|
|
32
|
+
else if (matchesKey(data, "down"))
|
|
33
|
+
this.selected = (this.selected + 1) % this.options.length;
|
|
34
|
+
else if (matchesKey(data, "return")) this.done(this.selected);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
render(width: number): string[] {
|
|
38
|
+
const th = this.theme;
|
|
39
|
+
const lines: string[] = ["", ` ${th.fg("accent", this.title)}`, ""];
|
|
40
|
+
this.options.forEach((opt, i) => {
|
|
41
|
+
const active = i === this.selected;
|
|
42
|
+
const label = active ? th.fg("accent", opt.label) : opt.label;
|
|
43
|
+
lines.push(
|
|
44
|
+
truncateToWidth(
|
|
45
|
+
` ${active ? th.fg("accent", "❯ ") : " "}${label}`,
|
|
46
|
+
width,
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
if (opt.description)
|
|
50
|
+
lines.push(
|
|
51
|
+
truncateToWidth(` ${th.fg("dim", opt.description)}`, width),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
lines.push("", ` ${th.fg("dim", "↑↓ 选择 · Enter 确认 · Esc 取消")}`, "");
|
|
55
|
+
return lines;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
invalidate() {}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 返回所选选项的索引;undefined = 用户取消(Esc/关闭)。 */
|
|
62
|
+
export async function selectWithDescriptions(
|
|
63
|
+
ctx: ExtensionContext,
|
|
64
|
+
title: string,
|
|
65
|
+
options: readonly SelectOption[],
|
|
66
|
+
): Promise<number | undefined> {
|
|
67
|
+
if (ctx.mode === "tui") {
|
|
68
|
+
return ctx.ui.custom<number | undefined>(
|
|
69
|
+
(_tui, theme, _kb, done) =>
|
|
70
|
+
new DescribedSelectComponent(title, options, theme, done),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
const labels = options.map((o) =>
|
|
74
|
+
o.description ? `${o.label} —— ${o.description}` : o.label,
|
|
75
|
+
);
|
|
76
|
+
const choice = await ctx.ui.select(title, labels);
|
|
77
|
+
if (choice === undefined) return undefined;
|
|
78
|
+
const idx = labels.indexOf(choice);
|
|
79
|
+
return idx >= 0 ? idx : undefined;
|
|
80
|
+
}
|