@narumitw/pi-plan-mode 0.47.0 → 0.48.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/README.md +101 -50
- package/package.json +1 -1
- package/src/active-implementation-menu.ts +11 -9
- package/src/command.ts +6 -1
- package/src/fresh-implementation.ts +213 -0
- package/src/implementation-retention.ts +122 -0
- package/src/message-transform.ts +1 -1
- package/src/plan-action-controller.ts +93 -0
- package/src/plan-action-menus.ts +73 -36
- package/src/plan-export-controller.ts +38 -0
- package/src/plan-export-screen.ts +19 -0
- package/src/plan-export.ts +31 -5
- package/src/plan-launch-menu.ts +122 -0
- package/src/plan-mode.ts +221 -236
- package/src/saved-plan-menu.ts +39 -14
- package/src/settings-menu.ts +346 -0
- package/src/settings.ts +275 -44
- package/src/state.ts +13 -2
- package/src/tool-policy.ts +14 -47
- package/src/tool-selection.ts +65 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
injectActiveImplementationContext,
|
|
3
|
+
isEmptyAssistantMessage,
|
|
4
|
+
messageContainsExactPlanModeImplementationHandoff,
|
|
5
|
+
messageContainsInactivePlanModeArtifact,
|
|
6
|
+
messageContainsLegacyPlanModeContextArtifact,
|
|
7
|
+
messageContainsPlanModeImplementationContextArtifact,
|
|
8
|
+
messageContainsPlanModeImplementationHandoff,
|
|
9
|
+
stripPlanModeCompletionCallsFromMessage,
|
|
10
|
+
stripProposedPlanBlocksFromMessage,
|
|
11
|
+
} from "./message-transform.js";
|
|
12
|
+
import type { ImplementationPlanRetention } from "./settings.js";
|
|
13
|
+
import type { ActiveImplementationPlan, PlanModeState } from "./state.js";
|
|
14
|
+
|
|
15
|
+
export function retentionLabel(retention: ImplementationPlanRetention) {
|
|
16
|
+
return {
|
|
17
|
+
keep: "Keep plan active",
|
|
18
|
+
"clear-on-start": "Use plan for handoff only",
|
|
19
|
+
"clear-after-first-run": "Clear after first implementation run",
|
|
20
|
+
}[retention];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function implementationRetentionPreview(retention: ImplementationPlanRetention) {
|
|
24
|
+
return {
|
|
25
|
+
keep: "After Implement: Keep plan active until /plan exit.",
|
|
26
|
+
"clear-on-start":
|
|
27
|
+
"After Implement: Use the plan for the implementation handoff only, then clear it.",
|
|
28
|
+
"clear-after-first-run": "After Implement: Clear after the first implementation run settles.",
|
|
29
|
+
}[retention];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ImplementationContextResult {
|
|
33
|
+
messages: unknown[];
|
|
34
|
+
clearActiveImplementationId?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ImplementationRetentionCoordinator {
|
|
38
|
+
restore(activeImplementation: ActiveImplementationPlan | undefined): void;
|
|
39
|
+
transformContext(messages: unknown[], state: PlanModeState): ImplementationContextResult;
|
|
40
|
+
implementationSettled(
|
|
41
|
+
activeImplementation: ActiveImplementationPlan | undefined,
|
|
42
|
+
): string | undefined;
|
|
43
|
+
reset(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createImplementationRetentionCoordinator(): ImplementationRetentionCoordinator {
|
|
47
|
+
let implementationWithDeliveredContext: string | undefined;
|
|
48
|
+
let restoredImplementationAwaitingContext: string | undefined;
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
restore(activeImplementation) {
|
|
52
|
+
restoredImplementationAwaitingContext =
|
|
53
|
+
activeImplementation && activeImplementation.retention !== "keep"
|
|
54
|
+
? activeImplementation.id
|
|
55
|
+
: undefined;
|
|
56
|
+
},
|
|
57
|
+
transformContext(messages, state) {
|
|
58
|
+
const messagesWithoutPlanContext = messages.filter(
|
|
59
|
+
(message) =>
|
|
60
|
+
!messageContainsLegacyPlanModeContextArtifact(message) &&
|
|
61
|
+
!messageContainsPlanModeImplementationContextArtifact(message),
|
|
62
|
+
);
|
|
63
|
+
if (state.enabled) {
|
|
64
|
+
return {
|
|
65
|
+
messages: messagesWithoutPlanContext.filter(
|
|
66
|
+
(message) => !messageContainsPlanModeImplementationHandoff(message),
|
|
67
|
+
),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const activeImplementation = state.activeImplementation;
|
|
72
|
+
const inactiveMessages = activeImplementation
|
|
73
|
+
? messagesWithoutPlanContext
|
|
74
|
+
: messagesWithoutPlanContext.filter(
|
|
75
|
+
(message) => !messageContainsPlanModeImplementationHandoff(message),
|
|
76
|
+
);
|
|
77
|
+
const filteredMessages = inactiveMessages
|
|
78
|
+
.filter((message) => !messageContainsInactivePlanModeArtifact(message))
|
|
79
|
+
.map(stripProposedPlanBlocksFromMessage)
|
|
80
|
+
.map(stripPlanModeCompletionCallsFromMessage)
|
|
81
|
+
.filter((message) => !isEmptyAssistantMessage(message));
|
|
82
|
+
if (!activeImplementation) return { messages: filteredMessages };
|
|
83
|
+
|
|
84
|
+
const contextualMessages = injectActiveImplementationContext(
|
|
85
|
+
filteredMessages,
|
|
86
|
+
activeImplementation,
|
|
87
|
+
);
|
|
88
|
+
// A busy /plan implement queues its handoff behind an older run. Do not arm cleanup
|
|
89
|
+
// until that exact handoff reaches context; a restored session has no older run to drain.
|
|
90
|
+
const deliveredCurrentHandoff =
|
|
91
|
+
restoredImplementationAwaitingContext === activeImplementation.id ||
|
|
92
|
+
filteredMessages.some((message) =>
|
|
93
|
+
messageContainsExactPlanModeImplementationHandoff(message, activeImplementation.plan),
|
|
94
|
+
);
|
|
95
|
+
if (!deliveredCurrentHandoff) return { messages: contextualMessages };
|
|
96
|
+
restoredImplementationAwaitingContext = undefined;
|
|
97
|
+
|
|
98
|
+
if (activeImplementation.retention === "clear-after-first-run") {
|
|
99
|
+
implementationWithDeliveredContext = activeImplementation.id;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
messages: contextualMessages,
|
|
103
|
+
clearActiveImplementationId:
|
|
104
|
+
activeImplementation.retention === "clear-on-start" ? activeImplementation.id : undefined,
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
implementationSettled(activeImplementation) {
|
|
108
|
+
if (
|
|
109
|
+
activeImplementation?.retention !== "clear-after-first-run" ||
|
|
110
|
+
implementationWithDeliveredContext !== activeImplementation.id
|
|
111
|
+
) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
implementationWithDeliveredContext = undefined;
|
|
115
|
+
return activeImplementation.id;
|
|
116
|
+
},
|
|
117
|
+
reset() {
|
|
118
|
+
implementationWithDeliveredContext = undefined;
|
|
119
|
+
restoredImplementationAwaitingContext = undefined;
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
package/src/message-transform.ts
CHANGED
|
@@ -128,7 +128,7 @@ export function messageContainsPlanModeImplementationHandoff(message: unknown) {
|
|
|
128
128
|
);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
function messageContainsExactPlanModeImplementationHandoff(message: unknown, plan: string) {
|
|
131
|
+
export function messageContainsExactPlanModeImplementationHandoff(message: unknown, plan: string) {
|
|
132
132
|
const candidate = unwrapSessionMessage(message);
|
|
133
133
|
if (candidate.role !== "user") return false;
|
|
134
134
|
return (
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { showPlanModeMenu, showReadyPlanMenu } from "./plan-action-menus.js";
|
|
3
|
+
import type { PlanExportDestination } from "./plan-export.js";
|
|
4
|
+
import { showSavedPlanMenu } from "./saved-plan-menu.js";
|
|
5
|
+
import type { PlanModeState } from "./state.js";
|
|
6
|
+
|
|
7
|
+
interface MenuLifecycle {
|
|
8
|
+
signal: AbortSignal;
|
|
9
|
+
isCurrent(): boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface PlanActionControllerOptions {
|
|
13
|
+
getState(): PlanModeState;
|
|
14
|
+
captureLifecycle(): MenuLifecycle;
|
|
15
|
+
statusText(): string;
|
|
16
|
+
implementationOutcome(): string;
|
|
17
|
+
getExportDestination(ctx: ExtensionContext): PlanExportDestination;
|
|
18
|
+
show(ctx: ExtensionContext): void;
|
|
19
|
+
finalize(ctx: ExtensionContext): void;
|
|
20
|
+
implementHere(ctx: ExtensionContext): void | Promise<void>;
|
|
21
|
+
implementFresh(ctx: ExtensionContext, isCurrent: () => boolean): void | Promise<void>;
|
|
22
|
+
exportPlan(
|
|
23
|
+
ctx: ExtensionContext,
|
|
24
|
+
path: string,
|
|
25
|
+
signal: AbortSignal,
|
|
26
|
+
isCurrent: () => boolean,
|
|
27
|
+
): Promise<boolean>;
|
|
28
|
+
settings(ctx: ExtensionContext, signal: AbortSignal, isCurrent: () => boolean): Promise<boolean>;
|
|
29
|
+
save(ctx: ExtensionContext): void;
|
|
30
|
+
stay(ctx: ExtensionContext): void;
|
|
31
|
+
exitReady(ctx: ExtensionContext): void;
|
|
32
|
+
clearSaved(ctx: ExtensionContext): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createPlanActionController(options: PlanActionControllerOptions) {
|
|
36
|
+
const freshAction = (ctx: ExtensionContext, lifecycle: MenuLifecycle, signal: AbortSignal) =>
|
|
37
|
+
options.implementFresh(ctx, () => lifecycle.isCurrent() && !signal.aborted);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
async showSaved(ctx: ExtensionContext) {
|
|
41
|
+
const lifecycle = options.captureLifecycle();
|
|
42
|
+
await showSavedPlanMenu(ctx, {
|
|
43
|
+
statusText: options.statusText(),
|
|
44
|
+
implementationOutcome: options.implementationOutcome,
|
|
45
|
+
getExportDestination: () => options.getExportDestination(ctx),
|
|
46
|
+
signal: lifecycle.signal,
|
|
47
|
+
isCurrent: lifecycle.isCurrent,
|
|
48
|
+
show: () => options.show(ctx),
|
|
49
|
+
implementHere: () => options.implementHere(ctx),
|
|
50
|
+
implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
|
|
51
|
+
exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
|
|
52
|
+
settings: (signal) => options.settings(ctx, signal, lifecycle.isCurrent),
|
|
53
|
+
clear: () => options.clearSaved(ctx),
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
async showCurrent(ctx: ExtensionContext) {
|
|
57
|
+
if (!ctx.hasUI) {
|
|
58
|
+
ctx.ui.notify(options.statusText(), "info");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const lifecycle = options.captureLifecycle();
|
|
62
|
+
await showPlanModeMenu(ctx, {
|
|
63
|
+
statusText: options.statusText(),
|
|
64
|
+
hasReadyPlan: options.getState().latestPlan !== undefined,
|
|
65
|
+
implementationOutcome: options.implementationOutcome,
|
|
66
|
+
getExportDestination: () => options.getExportDestination(ctx),
|
|
67
|
+
...lifecycle,
|
|
68
|
+
show: () => options.show(ctx),
|
|
69
|
+
finalize: () => options.finalize(ctx),
|
|
70
|
+
implementHere: () => options.implementHere(ctx),
|
|
71
|
+
implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
|
|
72
|
+
exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
|
|
73
|
+
save: () => options.save(ctx),
|
|
74
|
+
stay: () => options.stay(ctx),
|
|
75
|
+
exit: () => options.exitReady(ctx),
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
async showReady(ctx: ExtensionContext) {
|
|
79
|
+
const lifecycle = options.captureLifecycle();
|
|
80
|
+
await showReadyPlanMenu(ctx, {
|
|
81
|
+
...lifecycle,
|
|
82
|
+
implementationOutcome: options.implementationOutcome,
|
|
83
|
+
getExportDestination: () => options.getExportDestination(ctx),
|
|
84
|
+
implementHere: () => options.implementHere(ctx),
|
|
85
|
+
implementFresh: (signal) => freshAction(ctx, lifecycle, signal),
|
|
86
|
+
exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
|
|
87
|
+
save: () => options.save(ctx),
|
|
88
|
+
stay: () => undefined,
|
|
89
|
+
exit: () => options.exitReady(ctx),
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
package/src/plan-action-menus.ts
CHANGED
|
@@ -1,60 +1,84 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
|
|
3
|
+
import { type PlanExportDestinationProvider, planExportInputScreen } from "./plan-export-screen.js";
|
|
3
4
|
|
|
4
5
|
interface MenuLifecycle {
|
|
5
6
|
signal: AbortSignal;
|
|
6
7
|
isCurrent(): boolean;
|
|
7
8
|
}
|
|
8
9
|
|
|
10
|
+
const IMPLEMENTATION_CONTEXT_LINES = [
|
|
11
|
+
"Implement here keeps this planning conversation.",
|
|
12
|
+
"Start fresh transfers only the approved plan to a new session.",
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
9
15
|
interface PlanMenuOptions extends MenuLifecycle {
|
|
10
16
|
statusText: string;
|
|
11
17
|
hasReadyPlan: boolean;
|
|
18
|
+
implementationOutcome(): string;
|
|
19
|
+
getExportDestination: PlanExportDestinationProvider;
|
|
12
20
|
show(): void;
|
|
13
21
|
finalize(): void;
|
|
14
|
-
|
|
22
|
+
implementHere(): void | Promise<void>;
|
|
23
|
+
implementFresh(signal: AbortSignal): void | Promise<void>;
|
|
15
24
|
exportPlan(path: string, signal: AbortSignal): Promise<boolean>;
|
|
16
25
|
save(): void;
|
|
17
|
-
tools(): Promise<void>;
|
|
18
26
|
stay(): void;
|
|
19
27
|
exit(): void;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuOptions) {
|
|
23
31
|
type Screen = "main" | "export";
|
|
24
|
-
type Action =
|
|
32
|
+
type Action =
|
|
33
|
+
| "show"
|
|
34
|
+
| "finalize"
|
|
35
|
+
| "implement-here"
|
|
36
|
+
| "implement-fresh"
|
|
37
|
+
| "export"
|
|
38
|
+
| "save"
|
|
39
|
+
| "stay"
|
|
40
|
+
| "exit";
|
|
25
41
|
const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
|
|
26
42
|
start: "main",
|
|
27
43
|
screens: {
|
|
28
44
|
main: () => ({
|
|
29
45
|
kind: "actions",
|
|
30
46
|
title: "Plan mode",
|
|
31
|
-
lines: [
|
|
47
|
+
lines: [
|
|
48
|
+
options.statusText,
|
|
49
|
+
...(options.hasReadyPlan
|
|
50
|
+
? [...IMPLEMENTATION_CONTEXT_LINES, options.implementationOutcome()]
|
|
51
|
+
: []),
|
|
52
|
+
],
|
|
32
53
|
items: options.hasReadyPlan
|
|
33
54
|
? [
|
|
34
55
|
{ id: "show", label: "Show latest proposed plan", action: "show" },
|
|
35
|
-
{
|
|
56
|
+
{
|
|
57
|
+
id: "implement-here",
|
|
58
|
+
label: "Implement here",
|
|
59
|
+
description: "Continue in this session with the planning conversation.",
|
|
60
|
+
action: "implement-here",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: "implement-fresh",
|
|
64
|
+
label: "Start fresh and implement",
|
|
65
|
+
description: "Open a new linked session; transfer only the approved plan.",
|
|
66
|
+
action: "implement-fresh",
|
|
67
|
+
busyLabel: "Starting fresh implementation session…",
|
|
68
|
+
},
|
|
36
69
|
{ id: "export", label: "Export plan…", to: "export" },
|
|
37
70
|
{ id: "save", label: "Save for later", action: "save" },
|
|
38
|
-
{ id: "tools", label: "Configure Plan-mode tools", action: "tools" },
|
|
39
71
|
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
40
|
-
{ id: "exit", label: "
|
|
72
|
+
{ id: "exit", label: "Discard plan and exit", action: "exit" },
|
|
41
73
|
]
|
|
42
74
|
: [
|
|
43
75
|
{ id: "finalize", label: "Request final plan", action: "finalize" },
|
|
44
|
-
{ id: "tools", label: "Configure Plan-mode tools", action: "tools" },
|
|
45
76
|
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
46
77
|
{ id: "exit", label: "Exit Plan mode", action: "exit" },
|
|
47
78
|
],
|
|
48
79
|
hint: "close",
|
|
49
80
|
}),
|
|
50
|
-
export: () => (
|
|
51
|
-
kind: "input",
|
|
52
|
-
title: "Export plan",
|
|
53
|
-
lines: ["Existing paths are never overwritten."],
|
|
54
|
-
placeholder: "PLAN.md",
|
|
55
|
-
action: "export",
|
|
56
|
-
hint: "back",
|
|
57
|
-
}),
|
|
81
|
+
export: () => planExportInputScreen(options.getExportDestination),
|
|
58
82
|
},
|
|
59
83
|
actions: {
|
|
60
84
|
show: async () => {
|
|
@@ -65,8 +89,12 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
|
|
|
65
89
|
options.finalize();
|
|
66
90
|
return { kind: "close" };
|
|
67
91
|
},
|
|
68
|
-
implement: async () => {
|
|
69
|
-
await options.
|
|
92
|
+
"implement-here": async () => {
|
|
93
|
+
await options.implementHere();
|
|
94
|
+
return { kind: "close" };
|
|
95
|
+
},
|
|
96
|
+
"implement-fresh": async ({ signal }) => {
|
|
97
|
+
await options.implementFresh(signal);
|
|
70
98
|
return { kind: "close" };
|
|
71
99
|
},
|
|
72
100
|
export: async ({ value, signal }) =>
|
|
@@ -75,10 +103,6 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
|
|
|
75
103
|
options.save();
|
|
76
104
|
return { kind: "close" };
|
|
77
105
|
},
|
|
78
|
-
tools: async () => {
|
|
79
|
-
await options.tools();
|
|
80
|
-
return { kind: "stay" };
|
|
81
|
-
},
|
|
82
106
|
stay: async () => {
|
|
83
107
|
options.stay();
|
|
84
108
|
return { kind: "close" };
|
|
@@ -97,7 +121,10 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
|
|
|
97
121
|
}
|
|
98
122
|
|
|
99
123
|
interface ReadyPlanMenuOptions extends MenuLifecycle {
|
|
100
|
-
|
|
124
|
+
implementationOutcome(): string;
|
|
125
|
+
getExportDestination: PlanExportDestinationProvider;
|
|
126
|
+
implementHere(): void | Promise<void>;
|
|
127
|
+
implementFresh(signal: AbortSignal): void | Promise<void>;
|
|
101
128
|
exportPlan(path: string, signal: AbortSignal): Promise<boolean>;
|
|
102
129
|
save(): void;
|
|
103
130
|
stay(): void;
|
|
@@ -106,34 +133,44 @@ interface ReadyPlanMenuOptions extends MenuLifecycle {
|
|
|
106
133
|
|
|
107
134
|
export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPlanMenuOptions) {
|
|
108
135
|
type Screen = "ready" | "export";
|
|
109
|
-
type Action = "implement" | "export" | "save" | "stay" | "exit";
|
|
136
|
+
type Action = "implement-here" | "implement-fresh" | "export" | "save" | "stay" | "exit";
|
|
110
137
|
const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
|
|
111
138
|
start: "ready",
|
|
112
139
|
screens: {
|
|
113
140
|
ready: () => ({
|
|
114
141
|
kind: "actions",
|
|
115
142
|
title: "Proposed plan ready. What next?",
|
|
143
|
+
lines: [...IMPLEMENTATION_CONTEXT_LINES, options.implementationOutcome()],
|
|
116
144
|
items: [
|
|
117
|
-
{
|
|
145
|
+
{
|
|
146
|
+
id: "implement-here",
|
|
147
|
+
label: "Implement here",
|
|
148
|
+
description: "Continue in this session with the planning conversation.",
|
|
149
|
+
action: "implement-here",
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
id: "implement-fresh",
|
|
153
|
+
label: "Start fresh and implement",
|
|
154
|
+
description: "Open a new linked session; transfer only the approved plan.",
|
|
155
|
+
action: "implement-fresh",
|
|
156
|
+
busyLabel: "Starting fresh implementation session…",
|
|
157
|
+
},
|
|
118
158
|
{ id: "export", label: "Export plan…", to: "export" },
|
|
119
159
|
{ id: "save", label: "Save for later", action: "save" },
|
|
120
160
|
{ id: "stay", label: "Stay in Plan mode", action: "stay" },
|
|
121
|
-
{ id: "exit", label: "
|
|
161
|
+
{ id: "exit", label: "Discard plan and exit", action: "exit" },
|
|
122
162
|
],
|
|
123
163
|
hint: "close",
|
|
124
164
|
}),
|
|
125
|
-
export: () => (
|
|
126
|
-
kind: "input",
|
|
127
|
-
title: "Export plan",
|
|
128
|
-
lines: ["Existing paths are never overwritten."],
|
|
129
|
-
placeholder: "PLAN.md",
|
|
130
|
-
action: "export",
|
|
131
|
-
hint: "back",
|
|
132
|
-
}),
|
|
165
|
+
export: () => planExportInputScreen(options.getExportDestination),
|
|
133
166
|
},
|
|
134
167
|
actions: {
|
|
135
|
-
implement: async () => {
|
|
136
|
-
await options.
|
|
168
|
+
"implement-here": async () => {
|
|
169
|
+
await options.implementHere();
|
|
170
|
+
return { kind: "close" };
|
|
171
|
+
},
|
|
172
|
+
"implement-fresh": async ({ signal }) => {
|
|
173
|
+
await options.implementFresh(signal);
|
|
137
174
|
return { kind: "close" };
|
|
138
175
|
},
|
|
139
176
|
export: async ({ value, signal }) =>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { exportStoredPlan, planExportDestination } from "./plan-export.js";
|
|
3
|
+
import { configuredPlanExportPath, type PlanModeSettings } from "./settings.js";
|
|
4
|
+
import type { PlanModeState } from "./state.js";
|
|
5
|
+
|
|
6
|
+
interface PlanExportControllerOptions {
|
|
7
|
+
getState(): PlanModeState;
|
|
8
|
+
getSettings(): PlanModeSettings;
|
|
9
|
+
finishReady(ctx: ExtensionContext): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createPlanExportController(options: PlanExportControllerOptions) {
|
|
13
|
+
return {
|
|
14
|
+
export(
|
|
15
|
+
path: string | undefined,
|
|
16
|
+
ctx: ExtensionContext,
|
|
17
|
+
signal: AbortSignal,
|
|
18
|
+
isCurrent: () => boolean,
|
|
19
|
+
) {
|
|
20
|
+
const state = options.getState();
|
|
21
|
+
return exportStoredPlan(
|
|
22
|
+
state,
|
|
23
|
+
path,
|
|
24
|
+
ctx,
|
|
25
|
+
{
|
|
26
|
+
signal,
|
|
27
|
+
isCurrent,
|
|
28
|
+
getState: options.getState,
|
|
29
|
+
finishReady: () => options.finishReady(ctx),
|
|
30
|
+
},
|
|
31
|
+
configuredPlanExportPath(options.getSettings()),
|
|
32
|
+
);
|
|
33
|
+
},
|
|
34
|
+
getDestination(ctx: ExtensionContext) {
|
|
35
|
+
return planExportDestination(configuredPlanExportPath(options.getSettings()), ctx.cwd);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PlanExportDestination } from "./plan-export.js";
|
|
2
|
+
|
|
3
|
+
export type PlanExportDestinationProvider = () => PlanExportDestination;
|
|
4
|
+
|
|
5
|
+
export function planExportInputScreen(getDestination: PlanExportDestinationProvider) {
|
|
6
|
+
const destination = getDestination();
|
|
7
|
+
return {
|
|
8
|
+
kind: "input" as const,
|
|
9
|
+
title: "Export plan",
|
|
10
|
+
lines: [
|
|
11
|
+
"Existing paths are never overwritten.",
|
|
12
|
+
`Default: ${destination.configuredPath}`,
|
|
13
|
+
`Resolves to: ${destination.resolvedPath}`,
|
|
14
|
+
],
|
|
15
|
+
placeholder: destination.configuredPath,
|
|
16
|
+
action: "export" as const,
|
|
17
|
+
hint: "back" as const,
|
|
18
|
+
};
|
|
19
|
+
}
|
package/src/plan-export.ts
CHANGED
|
@@ -2,14 +2,20 @@ import { mkdir, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import { dirname, resolve } from "node:path";
|
|
3
3
|
import { stripVTControlCharacters } from "node:util";
|
|
4
4
|
import { type ExtensionContext, withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { DEFAULT_PLAN_EXPORT_PATH } from "./settings.js";
|
|
5
6
|
import type { PlanModeState } from "./state.js";
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
+
export { DEFAULT_PLAN_EXPORT_PATH };
|
|
8
9
|
|
|
9
10
|
export interface PlanExportResult {
|
|
10
11
|
path: string;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
export interface PlanExportDestination {
|
|
15
|
+
configuredPath: string;
|
|
16
|
+
resolvedPath: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
export interface PlanExportLifecycle {
|
|
14
20
|
signal: AbortSignal;
|
|
15
21
|
isCurrent(): boolean;
|
|
@@ -22,6 +28,7 @@ export async function exportStoredPlan(
|
|
|
22
28
|
requestedPath: string | undefined,
|
|
23
29
|
ctx: ExtensionContext,
|
|
24
30
|
lifecycle?: PlanExportLifecycle,
|
|
31
|
+
defaultPath = DEFAULT_PLAN_EXPORT_PATH,
|
|
25
32
|
) {
|
|
26
33
|
const plan =
|
|
27
34
|
(state.enabled ? state.latestPlan : undefined)?.trim() ??
|
|
@@ -41,7 +48,14 @@ export async function exportStoredPlan(
|
|
|
41
48
|
(lifecycle.isCurrent() && (!lifecycle.getState || lifecycle.getState() === state));
|
|
42
49
|
let result: PlanExportResult;
|
|
43
50
|
try {
|
|
44
|
-
result = await exportPlanToFile(
|
|
51
|
+
result = await exportPlanToFile(
|
|
52
|
+
plan,
|
|
53
|
+
requestedPath,
|
|
54
|
+
ctx.cwd,
|
|
55
|
+
lifecycle?.signal,
|
|
56
|
+
isCurrent,
|
|
57
|
+
defaultPath,
|
|
58
|
+
);
|
|
45
59
|
} catch (error: unknown) {
|
|
46
60
|
if (lifecycle?.signal.aborted || !isCurrent()) return false;
|
|
47
61
|
if (!ctx.hasUI) throw error;
|
|
@@ -65,8 +79,9 @@ export async function exportPlanToFile(
|
|
|
65
79
|
cwd: string,
|
|
66
80
|
signal?: AbortSignal,
|
|
67
81
|
isCurrent: () => boolean = () => true,
|
|
82
|
+
defaultPath = DEFAULT_PLAN_EXPORT_PATH,
|
|
68
83
|
): Promise<PlanExportResult> {
|
|
69
|
-
const path = resolvePlanExportPath(requestedPath, cwd);
|
|
84
|
+
const path = resolvePlanExportPath(requestedPath, cwd, defaultPath);
|
|
70
85
|
await withFileMutationQueue(path, async () => {
|
|
71
86
|
throwIfCancelled(signal, isCurrent);
|
|
72
87
|
await mkdir(dirname(path), { recursive: true });
|
|
@@ -85,8 +100,19 @@ export async function exportPlanToFile(
|
|
|
85
100
|
return { path };
|
|
86
101
|
}
|
|
87
102
|
|
|
88
|
-
export function
|
|
89
|
-
|
|
103
|
+
export function planExportDestination(defaultPath: string, cwd: string): PlanExportDestination {
|
|
104
|
+
return {
|
|
105
|
+
configuredPath: safeNotification(defaultPath),
|
|
106
|
+
resolvedPath: safeNotification(resolvePlanExportPath(undefined, cwd, defaultPath)),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function resolvePlanExportPath(
|
|
111
|
+
requestedPath: string | undefined,
|
|
112
|
+
cwd: string,
|
|
113
|
+
defaultPath = DEFAULT_PLAN_EXPORT_PATH,
|
|
114
|
+
) {
|
|
115
|
+
const rawPath = requestedPath?.trim() || defaultPath;
|
|
90
116
|
const normalizedPath = rawPath.startsWith("@") ? rawPath.slice(1) : rawPath;
|
|
91
117
|
if (!normalizedPath.trim()) throw new Error("Plan export path must not be empty.");
|
|
92
118
|
if (normalizedPath.includes("\0")) {
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
|
|
3
|
+
|
|
4
|
+
export interface PlanLaunchTool {
|
|
5
|
+
name: string;
|
|
6
|
+
description: string;
|
|
7
|
+
searchText: string;
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
disabledReason?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface PlanLaunchMenuOptions {
|
|
13
|
+
statusText: string;
|
|
14
|
+
toolSummary(selectedNames: ReadonlySet<string>): string;
|
|
15
|
+
getSelectedNames(): ReadonlySet<string>;
|
|
16
|
+
tools: readonly PlanLaunchTool[];
|
|
17
|
+
signal: AbortSignal;
|
|
18
|
+
isCurrent(): boolean;
|
|
19
|
+
initialScreen?: "main" | "tools";
|
|
20
|
+
start(signal: AbortSignal): void;
|
|
21
|
+
startWithTools(toolNames: string[], signal: AbortSignal): void;
|
|
22
|
+
settings(signal: AbortSignal): Promise<boolean>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLaunchMenuOptions) {
|
|
26
|
+
type Screen = "main" | "tools" | "help";
|
|
27
|
+
type Action = "start" | "toggle-tool" | "start-with-tools" | "settings";
|
|
28
|
+
const selectedNames = new Set(options.getSelectedNames());
|
|
29
|
+
let draftChanged = false;
|
|
30
|
+
const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
|
|
31
|
+
start: options.initialScreen ?? "main",
|
|
32
|
+
screens: {
|
|
33
|
+
main: () => ({
|
|
34
|
+
kind: "actions",
|
|
35
|
+
title: "Plan mode",
|
|
36
|
+
lines: [options.statusText, options.toolSummary(selectedNames)],
|
|
37
|
+
items: [
|
|
38
|
+
{ id: "start", label: "Start Plan mode", action: "start" },
|
|
39
|
+
{ id: "tools", label: "Choose tools, then start…", to: "tools" },
|
|
40
|
+
{ id: "settings", label: "Settings", action: "settings" },
|
|
41
|
+
{ id: "help", label: "How Plan mode works", to: "help" },
|
|
42
|
+
],
|
|
43
|
+
hint: "close",
|
|
44
|
+
}),
|
|
45
|
+
tools: () => ({
|
|
46
|
+
kind: "multiSelect",
|
|
47
|
+
title: "Choose Plan-mode tools",
|
|
48
|
+
lines: [
|
|
49
|
+
"Changes apply only when you start Plan mode.",
|
|
50
|
+
"Non-built-in tools run at user risk.",
|
|
51
|
+
],
|
|
52
|
+
enableSearch: true,
|
|
53
|
+
viewportSize: 10,
|
|
54
|
+
items: options.tools.map((tool) => ({
|
|
55
|
+
id: tool.name,
|
|
56
|
+
label: tool.name,
|
|
57
|
+
description: tool.description,
|
|
58
|
+
searchText: tool.searchText,
|
|
59
|
+
selected: selectedNames.has(tool.name),
|
|
60
|
+
disabled: tool.disabled,
|
|
61
|
+
disabledReason: tool.disabledReason,
|
|
62
|
+
})),
|
|
63
|
+
action: "toggle-tool",
|
|
64
|
+
actions: [
|
|
65
|
+
{
|
|
66
|
+
id: "start-with-tools",
|
|
67
|
+
label: "Done — start Plan mode",
|
|
68
|
+
action: "start-with-tools",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
hint: "back",
|
|
72
|
+
}),
|
|
73
|
+
help: () => ({
|
|
74
|
+
kind: "detail",
|
|
75
|
+
title: "How Plan mode works",
|
|
76
|
+
lines: [
|
|
77
|
+
"Plan mode uses read-only exploration to understand the project before implementation.",
|
|
78
|
+
"The agent can ask important decision questions, then returns a complete implementation-ready plan.",
|
|
79
|
+
"File mutation stays blocked until you explicitly choose to implement the completed plan.",
|
|
80
|
+
],
|
|
81
|
+
hint: "back",
|
|
82
|
+
}),
|
|
83
|
+
},
|
|
84
|
+
actions: {
|
|
85
|
+
start: async ({ signal }) => {
|
|
86
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
|
|
87
|
+
options.start(signal);
|
|
88
|
+
return { kind: "close" };
|
|
89
|
+
},
|
|
90
|
+
"toggle-tool": async ({ itemId, selected, signal }) => {
|
|
91
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
|
|
92
|
+
const tool = options.tools.find((candidate) => candidate.name === itemId);
|
|
93
|
+
if (!tool || tool.disabled) return { kind: "rejected" };
|
|
94
|
+
if (selected) selectedNames.add(tool.name);
|
|
95
|
+
else selectedNames.delete(tool.name);
|
|
96
|
+
draftChanged = true;
|
|
97
|
+
return { kind: "stay" };
|
|
98
|
+
},
|
|
99
|
+
"start-with-tools": async ({ signal }) => {
|
|
100
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
|
|
101
|
+
options.startWithTools(Array.from(selectedNames), signal);
|
|
102
|
+
return { kind: "close" };
|
|
103
|
+
},
|
|
104
|
+
settings: async ({ signal }) => {
|
|
105
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
|
|
106
|
+
const close = await options.settings(signal);
|
|
107
|
+
if (signal.aborted || !options.isCurrent()) return { kind: "rejected" };
|
|
108
|
+
if (close) return { kind: "close" };
|
|
109
|
+
if (!draftChanged) {
|
|
110
|
+
selectedNames.clear();
|
|
111
|
+
for (const name of options.getSelectedNames()) selectedNames.add(name);
|
|
112
|
+
}
|
|
113
|
+
return { kind: "stay" };
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
await runMenu(ctx, menu, {
|
|
118
|
+
getState: () => undefined,
|
|
119
|
+
signal: options.signal,
|
|
120
|
+
isCurrent: options.isCurrent,
|
|
121
|
+
});
|
|
122
|
+
}
|