@narumitw/pi-plan-mode 0.52.0 → 0.54.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 +143 -72
- package/dist/chunks/{chunk-H3RGWRY7.ts → chunk-U5CQ7RQX.js} +501 -294
- package/dist/chunks/chunk-U5CQ7RQX.js.map +7 -0
- package/dist/chunks/{interactive-ui-GFNX5OF4.ts → interactive-ui-W3VYAJIQ.js} +60 -24
- package/dist/chunks/interactive-ui-W3VYAJIQ.js.map +7 -0
- package/dist/index.ts +856 -270
- package/dist/index.ts.map +3 -3
- package/package.json +5 -5
- package/src/extension-runtime.ts +0 -17
- package/src/finalization-request.ts +57 -0
- package/src/fresh-implementation.ts +73 -27
- package/src/helper-tool-visibility.ts +142 -0
- package/src/implementation-retention.ts +43 -19
- package/src/message-transform.ts +130 -6
- package/src/mode-contract.ts +93 -0
- package/src/plan-launch-menu.ts +4 -4
- package/src/plan-mode.ts +586 -190
- package/src/presentation.ts +50 -17
- package/src/prompt.ts +3 -1
- package/src/required-tools.ts +0 -4
- package/src/settings-menu.ts +67 -17
- package/src/settings.ts +21 -2
- package/src/tool-policy.ts +137 -2
- package/src/tool-selection.ts +0 -23
- package/dist/chunks/chunk-H3RGWRY7.ts.map +0 -7
- package/dist/chunks/interactive-ui-GFNX5OF4.ts.map +0 -7
package/src/message-transform.ts
CHANGED
|
@@ -6,6 +6,7 @@ export const PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE = "plan-mode-implementatio
|
|
|
6
6
|
const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
|
|
7
7
|
const PLAN_IMPLEMENTATION_HANDOFF_PREFIX =
|
|
8
8
|
"Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:";
|
|
9
|
+
export const PLAN_HISTORY_IMPLEMENTATION_PROMPT = "Implement the plan.";
|
|
9
10
|
const PROPOSED_PLAN_PATTERN =
|
|
10
11
|
/^<proposed_plan>[\t ]*\r?\n([\s\S]*?)\r?\n<\/proposed_plan>[\t ]*$/gm;
|
|
11
12
|
const PROPOSED_PLAN_BLOCK_PATTERN =
|
|
@@ -22,6 +23,7 @@ export type ProposedPlanParseResult =
|
|
|
22
23
|
type SessionMessage = {
|
|
23
24
|
role?: string;
|
|
24
25
|
content?: unknown;
|
|
26
|
+
stopReason?: string;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
type TextBlock = {
|
|
@@ -69,6 +71,21 @@ export function latestAssistantText(messages: unknown) {
|
|
|
69
71
|
return "";
|
|
70
72
|
}
|
|
71
73
|
|
|
74
|
+
export function latestAssistantStopReason(messages: unknown) {
|
|
75
|
+
if (!Array.isArray(messages)) return undefined;
|
|
76
|
+
for (const entry of [...messages].reverse()) {
|
|
77
|
+
const message = (entry as { message?: SessionMessage })?.message ?? (entry as SessionMessage);
|
|
78
|
+
if (message?.role === "assistant") return message.stopReason;
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function messageTextContent(message: unknown) {
|
|
84
|
+
return messageText(
|
|
85
|
+
(message as { message?: SessionMessage })?.message ?? (message as SessionMessage),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
72
89
|
export function messageContainsLegacyPlanModeContextArtifact(message: unknown) {
|
|
73
90
|
return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
|
|
74
91
|
}
|
|
@@ -81,9 +98,18 @@ export function injectActiveImplementationContext(
|
|
|
81
98
|
messages: unknown[],
|
|
82
99
|
activeImplementation: ActiveImplementationPlan,
|
|
83
100
|
) {
|
|
101
|
+
let foundCurrentContext = false;
|
|
84
102
|
let foundCurrentHandoff = false;
|
|
103
|
+
const expectedContext = activeImplementationContextContent(activeImplementation);
|
|
85
104
|
const messagesWithoutStaleContext = messages.filter((message) => {
|
|
86
|
-
if (messageContainsPlanModeImplementationContextArtifact(message))
|
|
105
|
+
if (messageContainsPlanModeImplementationContextArtifact(message)) {
|
|
106
|
+
const candidate = unwrapSessionMessage(message);
|
|
107
|
+
if (!foundCurrentContext && candidate.content === expectedContext) {
|
|
108
|
+
foundCurrentContext = true;
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
87
113
|
if (!messageContainsPlanModeImplementationHandoff(message)) return true;
|
|
88
114
|
if (
|
|
89
115
|
!foundCurrentHandoff &&
|
|
@@ -94,14 +120,14 @@ export function injectActiveImplementationContext(
|
|
|
94
120
|
}
|
|
95
121
|
return false;
|
|
96
122
|
});
|
|
97
|
-
if (foundCurrentHandoff) return messagesWithoutStaleContext;
|
|
123
|
+
if (foundCurrentHandoff || foundCurrentContext) return messagesWithoutStaleContext;
|
|
98
124
|
|
|
99
125
|
let insertionIndex = 0;
|
|
100
126
|
while (isSummaryMessage(messagesWithoutStaleContext[insertionIndex])) insertionIndex += 1;
|
|
101
127
|
const contextMessage = {
|
|
102
128
|
role: "custom" as const,
|
|
103
129
|
customType: PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE,
|
|
104
|
-
content:
|
|
130
|
+
content: expectedContext,
|
|
105
131
|
display: false,
|
|
106
132
|
timestamp: activeImplementation.startedAt,
|
|
107
133
|
};
|
|
@@ -112,6 +138,10 @@ export function injectActiveImplementationContext(
|
|
|
112
138
|
];
|
|
113
139
|
}
|
|
114
140
|
|
|
141
|
+
function activeImplementationContextContent(activeImplementation: ActiveImplementationPlan) {
|
|
142
|
+
return `[ACTIVE IMPLEMENTATION PLAN]\n\nThe user approved the exact implementation plan below. Continue following it until the user explicitly clears or supersedes it. The exact plan is the remainder of this message:\n\n${activeImplementation.plan}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
115
145
|
export function messageContainsInactivePlanModeArtifact(message: unknown) {
|
|
116
146
|
const candidate = unwrapSessionMessage(message);
|
|
117
147
|
return (
|
|
@@ -120,6 +150,92 @@ export function messageContainsInactivePlanModeArtifact(message: unknown) {
|
|
|
120
150
|
);
|
|
121
151
|
}
|
|
122
152
|
|
|
153
|
+
interface HistoryImplementationArtifact {
|
|
154
|
+
messageIndex: number;
|
|
155
|
+
kind: "completion" | "legacy" | "presentation";
|
|
156
|
+
toolCallId?: string;
|
|
157
|
+
toolCallMessageIndex?: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// The kickoff text is intentionally generic, so only trust a plan artifact in the same
|
|
161
|
+
// uninterrupted user/summary segment and require a complete tool-call/result pair.
|
|
162
|
+
export function findHistoryImplementationArtifact(
|
|
163
|
+
messages: unknown[],
|
|
164
|
+
): HistoryImplementationArtifact | undefined {
|
|
165
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
166
|
+
const candidate = unwrapSessionMessage(messages[index]);
|
|
167
|
+
if (
|
|
168
|
+
candidate.role !== "user" ||
|
|
169
|
+
contentText(candidate.content).trim() !== PLAN_HISTORY_IMPLEMENTATION_PROMPT
|
|
170
|
+
) {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const artifact = findHistoryImplementationArtifactBeforeKickoff(messages, index);
|
|
174
|
+
if (artifact) return artifact;
|
|
175
|
+
}
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function findHistoryImplementationArtifactBeforeKickoff(
|
|
180
|
+
messages: unknown[],
|
|
181
|
+
kickoffIndex: number,
|
|
182
|
+
): HistoryImplementationArtifact | undefined {
|
|
183
|
+
for (let index = kickoffIndex - 1; index >= 0; index -= 1) {
|
|
184
|
+
const candidate = unwrapSessionMessage(messages[index]);
|
|
185
|
+
if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
|
|
186
|
+
if (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME) {
|
|
187
|
+
const toolCallId = candidate.toolCallId;
|
|
188
|
+
const toolCallMessageIndex = toolCallId
|
|
189
|
+
? findPlanModeCompletionCallMessageIndex(messages, index, toolCallId)
|
|
190
|
+
: undefined;
|
|
191
|
+
if (toolCallId && toolCallMessageIndex !== undefined) {
|
|
192
|
+
return {
|
|
193
|
+
messageIndex: index,
|
|
194
|
+
kind: "completion",
|
|
195
|
+
toolCallId,
|
|
196
|
+
toolCallMessageIndex,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE) {
|
|
202
|
+
return { messageIndex: index, kind: "presentation" };
|
|
203
|
+
}
|
|
204
|
+
if (
|
|
205
|
+
candidate.role === "assistant" &&
|
|
206
|
+
contentText(candidate.content).match(PROPOSED_PLAN_BLOCK_PATTERN)
|
|
207
|
+
) {
|
|
208
|
+
return { messageIndex: index, kind: "legacy" };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function findPlanModeCompletionCallMessageIndex(
|
|
215
|
+
messages: unknown[],
|
|
216
|
+
endIndex: number,
|
|
217
|
+
toolCallId: string,
|
|
218
|
+
) {
|
|
219
|
+
for (let index = endIndex - 1; index >= 0; index -= 1) {
|
|
220
|
+
const candidate = unwrapSessionMessage(messages[index]);
|
|
221
|
+
if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
|
|
222
|
+
if (!Array.isArray(candidate.content)) continue;
|
|
223
|
+
if (
|
|
224
|
+
candidate.content.some((block) => {
|
|
225
|
+
const toolCall = block as { type?: string; id?: string; name?: string };
|
|
226
|
+
return (
|
|
227
|
+
toolCall.type === "toolCall" &&
|
|
228
|
+
toolCall.id === toolCallId &&
|
|
229
|
+
toolCall.name === PLAN_MODE_COMPLETE_TOOL_NAME
|
|
230
|
+
);
|
|
231
|
+
})
|
|
232
|
+
) {
|
|
233
|
+
return index;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
123
239
|
export function messageContainsPlanModeImplementationHandoff(message: unknown) {
|
|
124
240
|
const candidate = unwrapSessionMessage(message);
|
|
125
241
|
return (
|
|
@@ -146,12 +262,19 @@ export function stripProposedPlanBlocksFromMessage<T>(message: T): T {
|
|
|
146
262
|
return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
|
|
147
263
|
}
|
|
148
264
|
|
|
149
|
-
export function stripPlanModeCompletionCallsFromMessage<T>(
|
|
265
|
+
export function stripPlanModeCompletionCallsFromMessage<T>(
|
|
266
|
+
message: T,
|
|
267
|
+
preservedToolCallId?: string,
|
|
268
|
+
): T {
|
|
150
269
|
return replaceAssistantContent(message, (content) => {
|
|
151
270
|
if (!Array.isArray(content)) return content;
|
|
152
271
|
const nextContent = content.filter((block) => {
|
|
153
|
-
const candidate = block as { type?: string; name?: string };
|
|
154
|
-
return !(
|
|
272
|
+
const candidate = block as { type?: string; id?: string; name?: string };
|
|
273
|
+
return !(
|
|
274
|
+
candidate.type === "toolCall" &&
|
|
275
|
+
candidate.name === PLAN_MODE_COMPLETE_TOOL_NAME &&
|
|
276
|
+
(!preservedToolCallId || candidate.id !== preservedToolCallId)
|
|
277
|
+
);
|
|
155
278
|
});
|
|
156
279
|
return nextContent.length === content.length ? content : nextContent;
|
|
157
280
|
});
|
|
@@ -185,6 +308,7 @@ function unwrapSessionMessage(message: unknown) {
|
|
|
185
308
|
role?: string;
|
|
186
309
|
customType?: string;
|
|
187
310
|
toolName?: string;
|
|
311
|
+
toolCallId?: string;
|
|
188
312
|
content?: unknown;
|
|
189
313
|
};
|
|
190
314
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { buildPlanModePrompt } from "./prompt.js";
|
|
2
|
+
|
|
3
|
+
export const MODE_CONTRACT_MESSAGE_TYPE = "plan-mode-transition";
|
|
4
|
+
export const MODE_CONTRACT_VERSION = 1;
|
|
5
|
+
export type PlanModeContract = "plan" | "normal";
|
|
6
|
+
|
|
7
|
+
const PLAN_CONTRACT_MARKER = `[PI PLAN MODE CONTRACT v${MODE_CONTRACT_VERSION}: PLAN]`;
|
|
8
|
+
const NORMAL_CONTRACT_MARKER = `[PI PLAN MODE CONTRACT v${MODE_CONTRACT_VERSION}: NORMAL]`;
|
|
9
|
+
const NORMAL_CONTRACT = `${NORMAL_CONTRACT_MARKER}
|
|
10
|
+
Plan Mode is no longer active.
|
|
11
|
+
Follow the user's current Normal-mode request and ordinary system instructions.
|
|
12
|
+
Earlier Plan-mode restrictions no longer apply, but retain the planning conversation as context.
|
|
13
|
+
The visible tool schemas are session capabilities; use only tools appropriate for the current request.`;
|
|
14
|
+
|
|
15
|
+
interface ContractMessage {
|
|
16
|
+
role?: string;
|
|
17
|
+
customType?: string;
|
|
18
|
+
content?: unknown;
|
|
19
|
+
details?: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function modeContractContent(mode: PlanModeContract) {
|
|
23
|
+
return mode === "plan" ? `${PLAN_CONTRACT_MARKER}\n${buildPlanModePrompt()}` : NORMAL_CONTRACT;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createModeContractMessage(mode: PlanModeContract, timestamp = Date.now()) {
|
|
27
|
+
return {
|
|
28
|
+
role: "custom" as const,
|
|
29
|
+
customType: MODE_CONTRACT_MESSAGE_TYPE,
|
|
30
|
+
content: modeContractContent(mode),
|
|
31
|
+
display: false,
|
|
32
|
+
details: { version: MODE_CONTRACT_VERSION, mode },
|
|
33
|
+
timestamp,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function modeContractFromMessage(message: unknown): PlanModeContract | undefined {
|
|
38
|
+
const candidate = unwrapMessage(message);
|
|
39
|
+
if (candidate.customType !== MODE_CONTRACT_MESSAGE_TYPE) return undefined;
|
|
40
|
+
if (candidate.content === modeContractContent("plan")) return "plan";
|
|
41
|
+
if (candidate.content === modeContractContent("normal")) return "normal";
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function hasModeContractArtifact(messages: readonly unknown[]) {
|
|
46
|
+
return messages.some(
|
|
47
|
+
(message) => unwrapMessage(message).customType === MODE_CONTRACT_MESSAGE_TYPE,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function latestModeContract(messages: readonly unknown[]) {
|
|
52
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
53
|
+
const mode = modeContractFromMessage(messages[index]);
|
|
54
|
+
if (mode) return { index, mode };
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function reconcileModeContract(messages: unknown[], expected: PlanModeContract) {
|
|
60
|
+
const latest = latestModeContract(messages);
|
|
61
|
+
if (latest?.mode === expected) return messages;
|
|
62
|
+
|
|
63
|
+
const latestContractIndex = findLatestContractArtifactIndex(messages);
|
|
64
|
+
const insertionIndex =
|
|
65
|
+
latestContractIndex >= 0 ? latestContractIndex + 1 : leadingSummaryBoundary(messages);
|
|
66
|
+
return [
|
|
67
|
+
...messages.slice(0, insertionIndex),
|
|
68
|
+
createModeContractMessage(expected, 0),
|
|
69
|
+
...messages.slice(insertionIndex),
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function findLatestContractArtifactIndex(messages: readonly unknown[]) {
|
|
74
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
75
|
+
if (unwrapMessage(messages[index]).customType === MODE_CONTRACT_MESSAGE_TYPE) return index;
|
|
76
|
+
}
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function leadingSummaryBoundary(messages: readonly unknown[]) {
|
|
81
|
+
let index = 0;
|
|
82
|
+
while (index < messages.length) {
|
|
83
|
+
const role = unwrapMessage(messages[index]).role;
|
|
84
|
+
if (role !== "compactionSummary" && role !== "branchSummary") break;
|
|
85
|
+
index += 1;
|
|
86
|
+
}
|
|
87
|
+
return index;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function unwrapMessage(message: unknown): ContractMessage {
|
|
91
|
+
const entry = message as { message?: unknown } | undefined;
|
|
92
|
+
return (entry?.message ?? message ?? {}) as ContractMessage;
|
|
93
|
+
}
|
package/src/plan-launch-menu.ts
CHANGED
|
@@ -44,10 +44,10 @@ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLau
|
|
|
44
44
|
}),
|
|
45
45
|
tools: () => ({
|
|
46
46
|
kind: "multiSelect",
|
|
47
|
-
title: "Choose Plan
|
|
47
|
+
title: "Choose Plan policy allowlist",
|
|
48
48
|
lines: [
|
|
49
|
-
"
|
|
50
|
-
"
|
|
49
|
+
"Policy changes apply only when you start Plan mode; first use may also reveal Plan helpers.",
|
|
50
|
+
"Only tools already active in Pi can be allowed; non-built-ins run at user risk.",
|
|
51
51
|
],
|
|
52
52
|
enableSearch: true,
|
|
53
53
|
viewportSize: 10,
|
|
@@ -64,7 +64,7 @@ export async function showPlanLaunchMenu(ctx: ExtensionContext, options: PlanLau
|
|
|
64
64
|
actions: [
|
|
65
65
|
{
|
|
66
66
|
id: "start-with-tools",
|
|
67
|
-
label: "Done — start
|
|
67
|
+
label: "Done — start with this policy",
|
|
68
68
|
action: "start-with-tools",
|
|
69
69
|
},
|
|
70
70
|
],
|