@narumitw/pi-plan-mode 0.57.1 → 0.58.1
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 +68 -4
- package/dist/chunks/{chunk-57OBPS7P.js → chunk-HF7ZEYIC.js} +169 -164
- package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
- package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UK7IGPOE.js} +312 -32
- package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
- package/dist/index.ts +670 -212
- package/dist/index.ts.map +4 -4
- package/docs/settings.md +26 -2
- package/package.json +50 -51
- package/src/active-implementation-menu.ts +57 -60
- package/src/command.ts +22 -21
- package/src/completion-tool.ts +53 -56
- package/src/extension-runtime.ts +5 -5
- package/src/finalization-request.ts +40 -44
- package/src/fresh-handoff-coordinator.ts +52 -0
- package/src/fresh-implementation.ts +312 -205
- package/src/implementation-models.ts +43 -0
- package/src/implementation-retention.ts +109 -123
- package/src/message-transform.ts +218 -252
- package/src/mode-contract.ts +46 -49
- package/src/plan-action-controller.ts +139 -89
- package/src/plan-action-menus.ts +414 -177
- package/src/plan-export-controller.ts +23 -28
- package/src/plan-export-screen.ts +13 -13
- package/src/plan-export.ts +85 -102
- package/src/plan-launch-menu.ts +119 -119
- package/src/plan-mode.ts +2027 -1675
- package/src/presentation.ts +84 -108
- package/src/prompt.ts +1 -1
- package/src/question-tool.ts +239 -267
- package/src/required-tools.ts +15 -19
- package/src/saved-plan-menu.ts +82 -84
- package/src/saved-plan-preflight.ts +29 -33
- package/src/settings-menu.ts +489 -377
- package/src/settings.ts +403 -382
- package/src/state.ts +177 -146
- package/src/tool-policy.ts +634 -699
- package/src/tool-selection.ts +39 -44
- package/src/workflow-mutex.ts +66 -66
- package/dist/chunks/chunk-57OBPS7P.js.map +0 -7
- package/dist/chunks/interactive-ui-N2ZKBVNE.js.map +0 -7
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface DeferredFreshHandoffDependencies {
|
|
2
|
+
schedule(callback: () => void): unknown;
|
|
3
|
+
cancel(handle: unknown): void;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface DeferredFreshHandoffCoordinator {
|
|
7
|
+
schedule(run: (isCurrent: () => boolean) => Promise<void>, onError: (error: unknown) => void): void;
|
|
8
|
+
cancel(): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const defaultDependencies: DeferredFreshHandoffDependencies = {
|
|
12
|
+
schedule: (callback) => setTimeout(callback, 0),
|
|
13
|
+
cancel: (handle) => clearTimeout(handle as ReturnType<typeof setTimeout>),
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function createDeferredFreshHandoffCoordinator(
|
|
17
|
+
dependencies: DeferredFreshHandoffDependencies = defaultDependencies,
|
|
18
|
+
): DeferredFreshHandoffCoordinator {
|
|
19
|
+
let generation = 0;
|
|
20
|
+
let pending: { generation: number; handle: unknown } | undefined;
|
|
21
|
+
|
|
22
|
+
const cancel = () => {
|
|
23
|
+
generation += 1;
|
|
24
|
+
if (!pending) return;
|
|
25
|
+
dependencies.cancel(pending.handle);
|
|
26
|
+
pending = undefined;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
schedule(run, onError) {
|
|
31
|
+
cancel();
|
|
32
|
+
const taskGeneration = generation;
|
|
33
|
+
const handle = dependencies.schedule(() => {
|
|
34
|
+
if (pending?.generation !== taskGeneration || generation !== taskGeneration) return;
|
|
35
|
+
pending = undefined;
|
|
36
|
+
const isCurrent = () => generation === taskGeneration;
|
|
37
|
+
void Promise.resolve()
|
|
38
|
+
.then(() => run(isCurrent))
|
|
39
|
+
.catch((error: unknown) => {
|
|
40
|
+
if (!isCurrent()) return;
|
|
41
|
+
try {
|
|
42
|
+
onError(error);
|
|
43
|
+
} catch {
|
|
44
|
+
// Detached error reporting must not create another unhandled rejection.
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
pending = { generation: taskGeneration, handle };
|
|
49
|
+
},
|
|
50
|
+
cancel,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -1,259 +1,366 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { stripVTControlCharacters } from "node:util";
|
|
2
3
|
import type { ExtensionCommandContext, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
4
|
import { PLAN_HISTORY_IMPLEMENTATION_PROMPT } from "./message-transform.js";
|
|
4
5
|
import { createModeContractMessage } from "./mode-contract.js";
|
|
5
6
|
import type { ImplementationPlanRetention } from "./settings.js";
|
|
6
|
-
import
|
|
7
|
+
import {
|
|
8
|
+
type ImplementationRuntimeSelection,
|
|
9
|
+
isPendingImplementationModelIdentifier,
|
|
10
|
+
MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
|
|
11
|
+
type PendingImplementationRuntime,
|
|
12
|
+
type PlanCompletionSource,
|
|
13
|
+
type PlanModeState,
|
|
14
|
+
restorePlanModeState,
|
|
15
|
+
} from "./state.js";
|
|
7
16
|
|
|
8
17
|
type NewSessionOptions = Exclude<Parameters<ExtensionCommandContext["newSession"]>[0], undefined>;
|
|
9
18
|
type ReplacementContext = Parameters<NonNullable<NewSessionOptions["withSession"]>>[0];
|
|
10
19
|
|
|
11
20
|
export interface FreshImplementationRequest {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
plan: string;
|
|
22
|
+
source: PlanCompletionSource;
|
|
23
|
+
retention: ImplementationPlanRetention;
|
|
24
|
+
stateEntryType: string;
|
|
25
|
+
runtime?: ImplementationRuntimeSelection;
|
|
26
|
+
isCurrent(): boolean;
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
interface FreshImplementationFromStateOptions {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
getState(): PlanModeState;
|
|
31
|
+
menuIsCurrent(): boolean;
|
|
32
|
+
retention: ImplementationPlanRetention;
|
|
33
|
+
stateEntryType: string;
|
|
34
|
+
runtime?: ImplementationRuntimeSelection;
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
export type FreshImplementationResult =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
| { kind: "started" }
|
|
39
|
+
| { kind: "cancelled" }
|
|
40
|
+
| { kind: "partial" }
|
|
41
|
+
| { kind: "rejected" }
|
|
42
|
+
| { kind: "stale" };
|
|
32
43
|
|
|
33
44
|
export function formatImplementationHandoff(plan: string) {
|
|
34
|
-
|
|
45
|
+
return `Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:\n\n${plan}`;
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
export function formatHistoryImplementationPrompt() {
|
|
38
|
-
|
|
49
|
+
return PLAN_HISTORY_IMPLEMENTATION_PROMPT;
|
|
39
50
|
}
|
|
40
51
|
|
|
41
52
|
export function formatTransferredPlanPrompt(plan: string, fresh: boolean) {
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
const destination = fresh ? " in a fresh context" : "";
|
|
54
|
+
return `A previous agent produced the plan below to accomplish the user's task. Implement the plan${destination}. Treat the plan as the source of user intent, re-read files as needed, and carry the work through implementation and verification.\n\n${plan}`;
|
|
44
55
|
}
|
|
45
56
|
|
|
46
57
|
export async function startFreshImplementationFromState(
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
ctx: ExtensionContext,
|
|
59
|
+
options: FreshImplementationFromStateOptions,
|
|
49
60
|
) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
61
|
+
if (!isCommandContext(ctx)) {
|
|
62
|
+
ctx.ui.notify(
|
|
63
|
+
"Fresh implementation requires the interactive /plan command. Reopen /plan and try again.",
|
|
64
|
+
"warning",
|
|
65
|
+
);
|
|
66
|
+
return { kind: "rejected" } as const;
|
|
67
|
+
}
|
|
68
|
+
const initialState = options.getState();
|
|
69
|
+
const savedPlan = initialState.enabled ? undefined : initialState.savedPlan;
|
|
70
|
+
const plan = (initialState.enabled ? initialState.latestPlan : savedPlan?.plan)?.trim();
|
|
71
|
+
const source = initialState.enabled ? initialState.latestPlanSource : savedPlan?.source;
|
|
72
|
+
if (!plan || !source) {
|
|
73
|
+
ctx.ui.notify("No completed plan is available to implement.", "warning");
|
|
74
|
+
return { kind: "rejected" } as const;
|
|
75
|
+
}
|
|
76
|
+
const wasEnabled = initialState.enabled;
|
|
77
|
+
const isCurrent = () => {
|
|
78
|
+
const current = options.getState();
|
|
79
|
+
return (
|
|
80
|
+
options.menuIsCurrent() &&
|
|
81
|
+
current.enabled === wasEnabled &&
|
|
82
|
+
(wasEnabled
|
|
83
|
+
? current.latestPlan === plan && current.latestPlanSource === source
|
|
84
|
+
: current.savedPlan === savedPlan)
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
return startFreshImplementationSession(ctx, {
|
|
88
|
+
plan,
|
|
89
|
+
source,
|
|
90
|
+
retention: options.retention,
|
|
91
|
+
stateEntryType: options.stateEntryType,
|
|
92
|
+
runtime: options.runtime,
|
|
93
|
+
isCurrent,
|
|
94
|
+
});
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
export async function startFreshImplementationSession(
|
|
86
|
-
|
|
87
|
-
|
|
98
|
+
ctx: ExtensionCommandContext,
|
|
99
|
+
request: FreshImplementationRequest,
|
|
88
100
|
): Promise<FreshImplementationResult> {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
101
|
+
if (ctx.mode === "print" || ctx.mode === "json") {
|
|
102
|
+
throw new Error("Fresh plan implementation is unavailable in print/JSON mode. Use TUI or RPC.");
|
|
103
|
+
}
|
|
92
104
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
105
|
+
await ctx.waitForIdle();
|
|
106
|
+
if (!request.isCurrent()) return { kind: "stale" };
|
|
107
|
+
if (!(await preflightModel(ctx, request))) return { kind: "rejected" };
|
|
108
|
+
if (!request.isCurrent()) return { kind: "stale" };
|
|
97
109
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
110
|
+
const usesConversationHistory = request.retention === "clear-on-start";
|
|
111
|
+
const pendingImplementationRuntime = pendingRuntimeIntent(request.runtime);
|
|
112
|
+
const activeImplementation = usesConversationHistory
|
|
113
|
+
? undefined
|
|
114
|
+
: {
|
|
115
|
+
id: randomUUID(),
|
|
116
|
+
plan: request.plan,
|
|
117
|
+
source: request.source,
|
|
118
|
+
startedAt: Date.now(),
|
|
119
|
+
retention: request.retention,
|
|
120
|
+
};
|
|
121
|
+
const destinationState: PlanModeState | undefined =
|
|
122
|
+
activeImplementation || pendingImplementationRuntime
|
|
123
|
+
? {
|
|
124
|
+
enabled: false,
|
|
125
|
+
awaitingAction: false,
|
|
126
|
+
...(activeImplementation ? { activeImplementation } : {}),
|
|
127
|
+
...(pendingImplementationRuntime ? { pendingImplementationRuntime } : {}),
|
|
128
|
+
}
|
|
129
|
+
: undefined;
|
|
130
|
+
const handoff = usesConversationHistory
|
|
131
|
+
? formatTransferredPlanPrompt(request.plan, true)
|
|
132
|
+
: formatImplementationHandoff(request.plan);
|
|
133
|
+
const parentSession = ctx.sessionManager.getSessionFile();
|
|
134
|
+
let setupError: string | undefined;
|
|
135
|
+
let kickoffError: string | undefined;
|
|
118
136
|
|
|
119
|
-
|
|
137
|
+
if (ctx.mode === "rpc") safeNotify(ctx, "Starting fresh implementation session…", "info");
|
|
120
138
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
139
|
+
let result: Awaited<ReturnType<ExtensionCommandContext["newSession"]>>;
|
|
140
|
+
try {
|
|
141
|
+
result = await ctx.newSession({
|
|
142
|
+
...(parentSession ? { parentSession } : {}),
|
|
143
|
+
setup: async (sessionManager) => {
|
|
144
|
+
try {
|
|
145
|
+
const contract = createModeContractMessage("normal");
|
|
146
|
+
sessionManager.appendCustomMessageEntry(
|
|
147
|
+
contract.customType,
|
|
148
|
+
contract.content,
|
|
149
|
+
contract.display,
|
|
150
|
+
contract.details,
|
|
151
|
+
);
|
|
152
|
+
if (destinationState) {
|
|
153
|
+
sessionManager.appendCustomEntry(request.stateEntryType, destinationState);
|
|
154
|
+
}
|
|
155
|
+
} catch (error: unknown) {
|
|
156
|
+
setupError = safeErrorDetail(error);
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
withSession: async (replacementCtx) => {
|
|
160
|
+
if (setupError) {
|
|
161
|
+
recoverSetupFailure(replacementCtx, handoff, setupError);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const reportKickoffFailure = (detail: string) => {
|
|
165
|
+
kickoffError = detail;
|
|
166
|
+
const recoveredInEditor = usesConversationHistory && safeSetEditorText(replacementCtx, handoff);
|
|
167
|
+
safeNotify(
|
|
168
|
+
replacementCtx,
|
|
169
|
+
usesConversationHistory
|
|
170
|
+
? recoveredInEditor
|
|
171
|
+
? `Fresh session created, but implementation did not start: ${detail}. The implementation request is in the editor; submit it or resume the parent planning session.`
|
|
172
|
+
: `Fresh session created, but implementation did not start: ${detail}. The implementation request could not be restored to the editor; resume the parent planning session.`
|
|
173
|
+
: `Fresh session created, but implementation did not start: ${detail}. Send a message to continue, use /plan exit to clear the active plan, or resume the parent planning session.`,
|
|
174
|
+
"error",
|
|
175
|
+
);
|
|
176
|
+
};
|
|
177
|
+
try {
|
|
178
|
+
await replacementCtx.sendUserMessage(handoff);
|
|
179
|
+
} catch (error: unknown) {
|
|
180
|
+
reportKickoffFailure(safeErrorDetail(error));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (pendingImplementationRuntime) {
|
|
184
|
+
const runtimeStatus = destinationRuntimeConsumptionStatus(replacementCtx, request.stateEntryType);
|
|
185
|
+
if (runtimeStatus !== "consumed") {
|
|
186
|
+
reportKickoffFailure(
|
|
187
|
+
runtimeStatus === "pending"
|
|
188
|
+
? "fresh implementation settings could not be consumed"
|
|
189
|
+
: "fresh implementation settings could not be verified as consumed",
|
|
190
|
+
);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
safeNotify(
|
|
195
|
+
replacementCtx,
|
|
196
|
+
"Fresh implementation session started. Only the approved plan was transferred.",
|
|
197
|
+
"info",
|
|
198
|
+
);
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
} catch (error: unknown) {
|
|
202
|
+
safeNotify(
|
|
203
|
+
ctx,
|
|
204
|
+
`Unable to start a fresh implementation session: ${safeErrorDetail(error)}. The source plan remains available; retry or resume the planning session.`,
|
|
205
|
+
"error",
|
|
206
|
+
);
|
|
207
|
+
return { kind: "rejected" };
|
|
208
|
+
}
|
|
178
209
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
210
|
+
if (result.cancelled) {
|
|
211
|
+
safeNotify(ctx, "Fresh implementation cancelled. The plan remains available.", "info");
|
|
212
|
+
return { kind: "cancelled" };
|
|
213
|
+
}
|
|
214
|
+
return setupError || kickoffError ? { kind: "partial" } : { kind: "started" };
|
|
184
215
|
}
|
|
185
216
|
|
|
186
|
-
async function preflightModel(ctx: ExtensionCommandContext,
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
217
|
+
async function preflightModel(ctx: ExtensionCommandContext, request: FreshImplementationRequest) {
|
|
218
|
+
const requestedModel = request.runtime?.model;
|
|
219
|
+
let model = ctx.model;
|
|
220
|
+
if (requestedModel) {
|
|
221
|
+
if (
|
|
222
|
+
!isPendingImplementationModelIdentifier(requestedModel.provider) ||
|
|
223
|
+
!isPendingImplementationModelIdentifier(requestedModel.modelId)
|
|
224
|
+
) {
|
|
225
|
+
notifyCurrent(
|
|
226
|
+
ctx,
|
|
227
|
+
request,
|
|
228
|
+
`Unable to implement the plan: implementation model provider and ID must each contain 1-${MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH} characters. Choose another model and retry.`,
|
|
229
|
+
"warning",
|
|
230
|
+
);
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
const isCurrentModel = ctx.model?.provider === requestedModel.provider && ctx.model.id === requestedModel.modelId;
|
|
234
|
+
if (!isCurrentModel) {
|
|
235
|
+
try {
|
|
236
|
+
model = ctx.modelRegistry.find(requestedModel.provider, requestedModel.modelId);
|
|
237
|
+
} catch (error: unknown) {
|
|
238
|
+
notifyCurrent(ctx, request, `Unable to implement the plan: ${safeErrorDetail(error)}`, "error");
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (!model) {
|
|
243
|
+
notifyCurrent(
|
|
244
|
+
ctx,
|
|
245
|
+
request,
|
|
246
|
+
`Unable to implement the plan: implementation model ${safeModelReference(requestedModel)} is no longer available. Choose another model and retry.`,
|
|
247
|
+
"warning",
|
|
248
|
+
);
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (!model) {
|
|
253
|
+
notifyCurrent(ctx, request, "Unable to implement the plan: no model is selected.", "warning");
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
let auth: Awaited<ReturnType<ExtensionCommandContext["modelRegistry"]["getApiKeyAndHeaders"]>>;
|
|
257
|
+
try {
|
|
258
|
+
auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
259
|
+
} catch (error: unknown) {
|
|
260
|
+
notifyCurrent(ctx, request, `Unable to implement the plan: ${safeErrorDetail(error)}`, "error");
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
if (!request.isCurrent()) return false;
|
|
264
|
+
if (!auth.ok) {
|
|
265
|
+
notifyCurrent(
|
|
266
|
+
ctx,
|
|
267
|
+
request,
|
|
268
|
+
requestedModel
|
|
269
|
+
? `Unable to implement the plan with ${safeModelReference(model)}: ${safeErrorDetail(auth.error)}. Choose another model or configure authentication, then retry.`
|
|
270
|
+
: `Unable to implement the plan: ${safeErrorDetail(auth.error)}`,
|
|
271
|
+
"warning",
|
|
272
|
+
);
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function notifyCurrent(
|
|
279
|
+
ctx: Pick<ExtensionContext, "ui">,
|
|
280
|
+
request: FreshImplementationRequest,
|
|
281
|
+
message: string,
|
|
282
|
+
level: "info" | "warning" | "error",
|
|
283
|
+
) {
|
|
284
|
+
if (request.isCurrent()) safeNotify(ctx, message, level);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function pendingRuntimeIntent(
|
|
288
|
+
selection: ImplementationRuntimeSelection | undefined,
|
|
289
|
+
): PendingImplementationRuntime | undefined {
|
|
290
|
+
if (!selection?.model && !selection?.thinkingLevel) return undefined;
|
|
291
|
+
return {
|
|
292
|
+
version: 1,
|
|
293
|
+
...(selection.model ? { model: { provider: selection.model.provider, modelId: selection.model.modelId } } : {}),
|
|
294
|
+
...(selection.thinkingLevel ? { thinkingLevel: selection.thinkingLevel } : {}),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function safeModelReference(model: { provider: string; id?: string; modelId?: string }) {
|
|
299
|
+
return safeErrorDetail(`${model.provider}/${model.modelId ?? model.id ?? "unknown"}`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function destinationRuntimeConsumptionStatus(
|
|
303
|
+
ctx: ReplacementContext,
|
|
304
|
+
stateEntryType: string,
|
|
305
|
+
): "consumed" | "pending" | "unavailable" {
|
|
306
|
+
try {
|
|
307
|
+
return restorePlanModeState(ctx.sessionManager.getBranch(), stateEntryType).pendingImplementationRuntime
|
|
308
|
+
? "pending"
|
|
309
|
+
: "consumed";
|
|
310
|
+
} catch {
|
|
311
|
+
return "unavailable";
|
|
312
|
+
}
|
|
207
313
|
}
|
|
208
314
|
|
|
209
315
|
function recoverSetupFailure(ctx: ReplacementContext, handoff: string, setupError: string) {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
316
|
+
const recoveredInEditor = safeSetEditorText(ctx, handoff);
|
|
317
|
+
safeNotify(
|
|
318
|
+
ctx,
|
|
319
|
+
recoveredInEditor
|
|
320
|
+
? `Fresh session created, but the active plan could not be saved: ${setupError}. The implementation request is in the editor; submit it to continue or resume the parent planning session.`
|
|
321
|
+
: `Fresh session created, but the active plan could not be saved: ${setupError}. The implementation request could not be restored to the editor; resume the parent planning session.`,
|
|
322
|
+
"error",
|
|
323
|
+
);
|
|
218
324
|
}
|
|
219
325
|
|
|
220
326
|
function safeSetEditorText(ctx: Pick<ExtensionContext, "ui">, text: string) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
327
|
+
try {
|
|
328
|
+
ctx.ui.setEditorText(text);
|
|
329
|
+
return true;
|
|
330
|
+
} catch {
|
|
331
|
+
// A replacement context can become stale while Pi reports a partial handoff.
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
228
334
|
}
|
|
229
335
|
|
|
230
|
-
function safeNotify(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
ctx.ui.notify(message, level);
|
|
237
|
-
} catch {
|
|
238
|
-
// A source or replacement context can become stale during session replacement.
|
|
239
|
-
}
|
|
336
|
+
function safeNotify(ctx: Pick<ExtensionContext, "ui">, message: string, level: "info" | "warning" | "error") {
|
|
337
|
+
try {
|
|
338
|
+
ctx.ui.notify(message, level);
|
|
339
|
+
} catch {
|
|
340
|
+
// A source or replacement context can become stale during session replacement.
|
|
341
|
+
}
|
|
240
342
|
}
|
|
241
343
|
|
|
242
344
|
function isCommandContext(ctx: ExtensionContext): ctx is ExtensionCommandContext {
|
|
243
|
-
|
|
345
|
+
return typeof (ctx as Partial<ExtensionCommandContext>).newSession === "function";
|
|
244
346
|
}
|
|
245
347
|
|
|
246
348
|
function safeErrorDetail(error: unknown) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
349
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
350
|
+
const normalized =
|
|
351
|
+
[...stripVTControlCharacters(detail)]
|
|
352
|
+
.map((character) => {
|
|
353
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
354
|
+
return codePoint <= 0x1f ||
|
|
355
|
+
(codePoint >= 0x7f && codePoint <= 0x9f) ||
|
|
356
|
+
(codePoint >= 0x202a && codePoint <= 0x202e) ||
|
|
357
|
+
(codePoint >= 0x2066 && codePoint <= 0x2069)
|
|
358
|
+
? " "
|
|
359
|
+
: character;
|
|
360
|
+
})
|
|
361
|
+
.join("")
|
|
362
|
+
.replace(/\s+/gu, " ")
|
|
363
|
+
.trim() || "unknown error";
|
|
364
|
+
const characters = [...normalized];
|
|
365
|
+
return characters.length > 500 ? `${characters.slice(0, 499).join("")}…` : normalized;
|
|
259
366
|
}
|