@kliebhan/pi-prompt-autocomplete 0.1.2 → 0.2.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/CHANGELOG.md +27 -0
- package/README.md +43 -6
- package/core.ts +670 -26
- package/index.ts +577 -68
- package/package.json +2 -2
package/index.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as piAi from "@earendil-works/pi-ai";
|
|
2
|
+
import type {
|
|
3
|
+
Api,
|
|
4
|
+
AssistantMessage,
|
|
5
|
+
AssistantMessageEventStream,
|
|
6
|
+
Model,
|
|
7
|
+
UserMessage,
|
|
8
|
+
} from "@earendil-works/pi-ai";
|
|
2
9
|
import { CustomEditor, type ExtensionAPI, type ExtensionContext, type KeybindingsManager } from "@earendil-works/pi-coding-agent";
|
|
3
10
|
import { matchesKey, truncateToWidth, type EditorTheme, type KeyId, type TUI, visibleWidth } from "@earendil-works/pi-tui";
|
|
4
11
|
import {
|
|
@@ -6,30 +13,45 @@ import {
|
|
|
6
13
|
buildLatestAssistantMessageContext,
|
|
7
14
|
buildLatestUserMessageContext,
|
|
8
15
|
buildPromptAutocompleteCacheKey,
|
|
16
|
+
buildPromptAutocompletePrefixContextKey,
|
|
9
17
|
buildRecentConversationContext,
|
|
10
18
|
cancelAllCoalescedRequests,
|
|
11
19
|
computeRequestMaxTokens,
|
|
12
20
|
createOwnerRefCounter,
|
|
21
|
+
createPromptAutocompleteUsageStats,
|
|
22
|
+
describeSettingSource,
|
|
13
23
|
DEFAULT_DEBOUNCE_MS,
|
|
14
24
|
DEFAULT_MAX_ALTERNATIVES,
|
|
15
25
|
DEFAULT_MAX_SUGGESTION_CHARS,
|
|
16
26
|
DEFAULT_MIN_PROMPT_CHARS,
|
|
17
27
|
DEFAULT_PREFERRED_MODEL,
|
|
18
28
|
DEFAULT_PROMPT_AUTOCOMPLETE_ENABLED,
|
|
29
|
+
DEFAULT_STREAM_RESPONSES,
|
|
19
30
|
ExpiringLruCache,
|
|
20
31
|
extractNextSuggestionChunk,
|
|
21
32
|
formatModelLabel,
|
|
33
|
+
formatPromptAutocompleteStats,
|
|
34
|
+
formatUsageStats,
|
|
35
|
+
hostSupportsStreamedResponses,
|
|
36
|
+
isInteractiveEditorHost,
|
|
22
37
|
MAX_DRAFT_CONTEXT_CHARS,
|
|
23
38
|
normalizePromptSuggestions,
|
|
24
39
|
parseBoundedIntFlag,
|
|
25
40
|
parseModelRef,
|
|
41
|
+
parsePartialPromptSuggestion,
|
|
26
42
|
PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
|
|
43
|
+
recordProviderLatency,
|
|
44
|
+
recordProviderUsage,
|
|
45
|
+
resolveOverride,
|
|
46
|
+
reusePromptAutocompleteSuggestions,
|
|
27
47
|
SequenceOwnedSlot,
|
|
28
48
|
shouldSkipPromptAutocomplete,
|
|
29
49
|
truncateDraftTail,
|
|
30
50
|
type CoalescedRequestEntry,
|
|
31
51
|
type CoalescedRequestSubscription,
|
|
32
52
|
type ModelRef,
|
|
53
|
+
type PromptAutocompleteRuntimeOverrides,
|
|
54
|
+
type PromptAutocompleteUsageStats,
|
|
33
55
|
} from "./core.ts";
|
|
34
56
|
|
|
35
57
|
const GHOST_TEXT_STYLE = "\x1b[2m";
|
|
@@ -41,6 +63,12 @@ const SPINNER_INTERVAL_MS = 80;
|
|
|
41
63
|
const SPINNER_LABEL = "Generating suggestion";
|
|
42
64
|
// Inline autocomplete should fail fast instead of inheriting long provider retry/timeout defaults.
|
|
43
65
|
const REQUEST_TIMEOUT_MS = 8_000;
|
|
66
|
+
// A compliant provider emits its terminal aborted message immediately, which
|
|
67
|
+
// lets accounting retain any usage. These bounds settle our caller even when a
|
|
68
|
+
// custom provider ignores both contracts; JavaScript cannot forcibly terminate
|
|
69
|
+
// that provider's already-pending iterator.next().
|
|
70
|
+
const STREAM_ABORT_DRAIN_TIMEOUT_MS = 250;
|
|
71
|
+
const STREAM_HARD_TIMEOUT_GRACE_MS = 500;
|
|
44
72
|
const REQUEST_MAX_RETRIES = 0;
|
|
45
73
|
const REQUEST_MAX_RETRY_DELAY_MS = 2_000;
|
|
46
74
|
// Brief cooldown avoids hammering providers after transient auth/network failures while keeping the UI responsive.
|
|
@@ -86,6 +114,7 @@ const EDIT_ACTIONS = [
|
|
|
86
114
|
|
|
87
115
|
interface PromptAutocompleteConfig {
|
|
88
116
|
allowWhileStreaming: boolean;
|
|
117
|
+
streamResponses: boolean;
|
|
89
118
|
debug: boolean;
|
|
90
119
|
debounceMs: number;
|
|
91
120
|
minPromptChars: number;
|
|
@@ -95,6 +124,8 @@ interface PromptAutocompleteConfig {
|
|
|
95
124
|
}
|
|
96
125
|
|
|
97
126
|
interface PromptAutocompleteCacheEntry {
|
|
127
|
+
draft: string;
|
|
128
|
+
prefixContextKey: string;
|
|
98
129
|
suggestions: string[];
|
|
99
130
|
rawResponse?: string;
|
|
100
131
|
error?: string;
|
|
@@ -104,6 +135,8 @@ interface PromptAutocompleteCacheEntry {
|
|
|
104
135
|
interface SuggestionRefreshOptions {
|
|
105
136
|
clearExisting?: boolean;
|
|
106
137
|
immediate?: boolean;
|
|
138
|
+
/** Stable terminal-cache identity to retain across a word/chunk acceptance. */
|
|
139
|
+
preferredOrigin?: string;
|
|
107
140
|
// Manual one-shot trigger: bypasses the streaming gate and error cooldown so the
|
|
108
141
|
// user can force a single suggestion while the main agent is still working.
|
|
109
142
|
manual?: boolean;
|
|
@@ -116,30 +149,90 @@ interface EditorMountState {
|
|
|
116
149
|
installedFactory: EditorFactory;
|
|
117
150
|
}
|
|
118
151
|
|
|
119
|
-
type CompleteSimpleFunction = typeof completeSimple;
|
|
152
|
+
type CompleteSimpleFunction = typeof import("@earendil-works/pi-ai/compat").completeSimple;
|
|
153
|
+
type StreamSimpleFunction = typeof import("@earendil-works/pi-ai/compat").streamSimple;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Host-neutral access to the simple completion API.
|
|
157
|
+
*
|
|
158
|
+
* Pi aliases the pi-ai root specifier to its compat entrypoint for extensions,
|
|
159
|
+
* and forked hosts based on an older API revision (for example prime-agent)
|
|
160
|
+
* export the same functions from the pi-ai root module. Importing the root
|
|
161
|
+
* specifier therefore works on both, while importing `@earendil-works/pi-ai/compat`
|
|
162
|
+
* fails to resolve on hosts that never mapped that subpath.
|
|
163
|
+
*/
|
|
164
|
+
const piAiSimpleApi = piAi as unknown as {
|
|
165
|
+
completeSimple?: CompleteSimpleFunction;
|
|
166
|
+
streamSimple?: StreamSimpleFunction;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
type SimpleApiFunction = CompleteSimpleFunction | StreamSimpleFunction;
|
|
170
|
+
|
|
171
|
+
function requireSimpleApiFunction<F extends SimpleApiFunction>(
|
|
172
|
+
name: "completeSimple" | "streamSimple",
|
|
173
|
+
impl: F | undefined,
|
|
174
|
+
): F {
|
|
175
|
+
if (typeof impl !== "function") {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`prompt-autocomplete requires ${name}() from @earendil-works/pi-ai; this host does not provide it`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return impl;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Resolved on first use: plain Node test runs resolve the real pi-ai root,
|
|
184
|
+
// which only exposes these functions when the host maps them for extensions.
|
|
185
|
+
const hostCompleteSimple = ((model, context, options) =>
|
|
186
|
+
requireSimpleApiFunction("completeSimple", piAiSimpleApi.completeSimple)(
|
|
187
|
+
model,
|
|
188
|
+
context,
|
|
189
|
+
options,
|
|
190
|
+
)) as CompleteSimpleFunction;
|
|
191
|
+
|
|
192
|
+
const hostStreamSimple = ((model, context, options) =>
|
|
193
|
+
requireSimpleApiFunction("streamSimple", piAiSimpleApi.streamSimple)(
|
|
194
|
+
model,
|
|
195
|
+
context,
|
|
196
|
+
options,
|
|
197
|
+
)) as StreamSimpleFunction;
|
|
120
198
|
|
|
121
199
|
export interface PromptAutocompleteDependencies {
|
|
122
200
|
completeSimple?: CompleteSimpleFunction;
|
|
201
|
+
streamSimple?: StreamSimpleFunction;
|
|
123
202
|
now?: () => number;
|
|
124
203
|
}
|
|
125
204
|
|
|
126
205
|
interface PromptAutocompleteSharedState {
|
|
127
206
|
enabled: boolean;
|
|
128
207
|
activationId: number;
|
|
129
|
-
|
|
208
|
+
agentStreaming: boolean;
|
|
130
209
|
config: PromptAutocompleteConfig;
|
|
210
|
+
/** Slash-command decisions that outrank flags for the lifetime of the process. */
|
|
211
|
+
runtimeOverrides: PromptAutocompleteRuntimeOverrides;
|
|
212
|
+
/** Session-scoped request accounting; reset together with the cache. */
|
|
213
|
+
usageStats: PromptAutocompleteUsageStats;
|
|
131
214
|
currentModel?: Model<Api>;
|
|
132
215
|
modelRegistry?: ExtensionContext["modelRegistry"];
|
|
133
216
|
sessionManager?: ExtensionContext["sessionManager"];
|
|
134
217
|
completeSimple: CompleteSimpleFunction;
|
|
218
|
+
streamSimple?: StreamSimpleFunction;
|
|
135
219
|
now: () => number;
|
|
136
220
|
debugState: string;
|
|
137
221
|
editorMount?: EditorMountState;
|
|
138
222
|
editorBlockedReason?: string;
|
|
223
|
+
/**
|
|
224
|
+
* Set once a mount attempt proved whether this host installs custom editors.
|
|
225
|
+
*
|
|
226
|
+
* A process cannot change its front-end, so this is deliberately never reset:
|
|
227
|
+
* a host that dropped the editor factory once would drop it again. Restarting
|
|
228
|
+
* the host is the only way to re-probe.
|
|
229
|
+
*/
|
|
230
|
+
hostInstallsEditors?: boolean;
|
|
139
231
|
lastError?: string;
|
|
140
232
|
lastRawResponse?: string;
|
|
141
233
|
requestCache: ExpiringLruCache<PromptAutocompleteCacheEntry>;
|
|
142
|
-
|
|
234
|
+
prefixReuseCache: ExpiringLruCache<PromptAutocompleteCacheEntry>;
|
|
235
|
+
inFlightRequests: Map<string, CoalescedRequestEntry<PromptAutocompleteCacheEntry, string>>;
|
|
143
236
|
ownsEditor?: () => boolean;
|
|
144
237
|
refreshEditor?: (options?: SuggestionRefreshOptions) => void;
|
|
145
238
|
cancelActiveRequest?: () => void;
|
|
@@ -150,12 +243,16 @@ interface PromptAutocompleteSharedState {
|
|
|
150
243
|
|
|
151
244
|
interface SuggestionRequest {
|
|
152
245
|
activationId: number;
|
|
246
|
+
leafId: string;
|
|
153
247
|
draft: string;
|
|
154
248
|
draftTail: string;
|
|
155
249
|
model: Model<Api>;
|
|
156
250
|
modelLabel: string;
|
|
157
251
|
cacheKey: string;
|
|
252
|
+
prefixContextKey: string;
|
|
158
253
|
maxAlternatives: number;
|
|
254
|
+
maxSuggestionChars: number;
|
|
255
|
+
useStreaming: boolean;
|
|
159
256
|
latestAssistantContext: string;
|
|
160
257
|
latestUserContext: string;
|
|
161
258
|
recentContext: string;
|
|
@@ -171,6 +268,77 @@ function arraysEqual(a: string[], b: string[]): boolean {
|
|
|
171
268
|
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
172
269
|
}
|
|
173
270
|
|
|
271
|
+
function extractAssistantText(message: AssistantMessage): string {
|
|
272
|
+
return message.content
|
|
273
|
+
.filter((block): block is { type: "text"; text: string } => block.type === "text")
|
|
274
|
+
.map((block) => block.text)
|
|
275
|
+
.join("");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function consumeAssistantStream(
|
|
279
|
+
stream: AssistantMessageEventStream,
|
|
280
|
+
signal: AbortSignal,
|
|
281
|
+
abortProvider: () => void,
|
|
282
|
+
onPartial: (message: AssistantMessage) => void,
|
|
283
|
+
): Promise<AssistantMessage> {
|
|
284
|
+
const iterator = stream[Symbol.asyncIterator]();
|
|
285
|
+
let hardTimer: ReturnType<typeof setTimeout> | undefined;
|
|
286
|
+
let abortTimer: ReturnType<typeof setTimeout> | undefined;
|
|
287
|
+
let rejectAbortDrain: ((error: Error) => void) | undefined;
|
|
288
|
+
let terminal: AssistantMessage | undefined;
|
|
289
|
+
|
|
290
|
+
const hardDeadline = new Promise<never>((_resolve, reject) => {
|
|
291
|
+
hardTimer = setTimeout(() => {
|
|
292
|
+
// `timeoutMs` is provider-owned. Abort the actual request signal as a
|
|
293
|
+
// second line of defence for providers that respect cancellation but
|
|
294
|
+
// accidentally ignore their timeout option.
|
|
295
|
+
abortProvider();
|
|
296
|
+
reject(new Error("Provider stream exceeded the local response deadline"));
|
|
297
|
+
}, REQUEST_TIMEOUT_MS + STREAM_HARD_TIMEOUT_GRACE_MS);
|
|
298
|
+
});
|
|
299
|
+
const abortDeadline = new Promise<never>((_resolve, reject) => {
|
|
300
|
+
rejectAbortDrain = reject;
|
|
301
|
+
});
|
|
302
|
+
const beginAbortDrain = () => {
|
|
303
|
+
if (abortTimer) return;
|
|
304
|
+
abortTimer = setTimeout(
|
|
305
|
+
() => rejectAbortDrain?.(new Error("Provider stream did not terminate after cancellation")),
|
|
306
|
+
STREAM_ABORT_DRAIN_TIMEOUT_MS,
|
|
307
|
+
);
|
|
308
|
+
};
|
|
309
|
+
signal.addEventListener("abort", beginAbortDrain, { once: true });
|
|
310
|
+
if (signal.aborted) beginAbortDrain();
|
|
311
|
+
|
|
312
|
+
try {
|
|
313
|
+
while (!terminal) {
|
|
314
|
+
const next = await Promise.race([iterator.next(), hardDeadline, abortDeadline]);
|
|
315
|
+
if (next.done) {
|
|
316
|
+
throw new Error("Provider stream ended without a terminal event");
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const event = next.value;
|
|
320
|
+
if (event.type === "done") {
|
|
321
|
+
terminal = event.message;
|
|
322
|
+
} else if (event.type === "error") {
|
|
323
|
+
terminal = event.error;
|
|
324
|
+
} else if (event.type === "text_delta" || event.type === "text_end") {
|
|
325
|
+
onPartial(event.partial);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return terminal;
|
|
329
|
+
} finally {
|
|
330
|
+
signal.removeEventListener("abort", beginAbortDrain);
|
|
331
|
+
if (hardTimer) clearTimeout(hardTimer);
|
|
332
|
+
if (abortTimer) clearTimeout(abortTimer);
|
|
333
|
+
if (!terminal) {
|
|
334
|
+
// Do not await a non-compliant iterator's return path. The provider already
|
|
335
|
+
// has an aborted signal; this merely gives a cooperative iterator a chance
|
|
336
|
+
// to release local resources while allowing our request promise to settle.
|
|
337
|
+
void Promise.resolve(iterator.return?.()).catch(() => undefined);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
174
342
|
function matchesAnyKey(data: string, keys: readonly KeyId[]): boolean {
|
|
175
343
|
return keys.some((key) => matchesKey(data, key));
|
|
176
344
|
}
|
|
@@ -197,9 +365,19 @@ function resolveSuggestionModel(shared: PromptAutocompleteSharedState): Model<Ap
|
|
|
197
365
|
return undefined;
|
|
198
366
|
}
|
|
199
367
|
|
|
368
|
+
function parseOnOffFlag(value: boolean | string | undefined, fallback: boolean): boolean {
|
|
369
|
+
if (typeof value === "boolean") return value;
|
|
370
|
+
if (typeof value !== "string") return fallback;
|
|
371
|
+
const normalized = value.trim().toLowerCase();
|
|
372
|
+
if (["on", "true", "1", "yes"].includes(normalized)) return true;
|
|
373
|
+
if (["off", "false", "0", "no"].includes(normalized)) return false;
|
|
374
|
+
return fallback;
|
|
375
|
+
}
|
|
376
|
+
|
|
200
377
|
function parseConfig(pi: ExtensionAPI): PromptAutocompleteConfig {
|
|
201
378
|
return {
|
|
202
379
|
allowWhileStreaming: pi.getFlag("prompt-autocomplete-while-streaming") === true,
|
|
380
|
+
streamResponses: parseOnOffFlag(pi.getFlag("prompt-autocomplete-stream"), DEFAULT_STREAM_RESPONSES),
|
|
203
381
|
debug: pi.getFlag("prompt-autocomplete-debug") === true,
|
|
204
382
|
debounceMs: parseBoundedIntFlag(pi.getFlag("prompt-autocomplete-debounce-ms"), DEFAULT_DEBOUNCE_MS, 0, 5_000),
|
|
205
383
|
minPromptChars: parseBoundedIntFlag(pi.getFlag("prompt-autocomplete-min-chars"), DEFAULT_MIN_PROMPT_CHARS, 0, 500),
|
|
@@ -219,6 +397,29 @@ function parseConfig(pi: ExtensionAPI): PromptAutocompleteConfig {
|
|
|
219
397
|
};
|
|
220
398
|
}
|
|
221
399
|
|
|
400
|
+
/**
|
|
401
|
+
* Recompute enablement and config from flags, then re-apply runtime overrides.
|
|
402
|
+
*
|
|
403
|
+
* Called on every session start. Without the override layer a new session would
|
|
404
|
+
* silently discard `/prompt-autocomplete on`, `while-streaming`, and `debug-*`
|
|
405
|
+
* decisions and revert to the flags the process happened to start with.
|
|
406
|
+
*/
|
|
407
|
+
function applyEffectiveConfig(pi: ExtensionAPI, shared: PromptAutocompleteSharedState): void {
|
|
408
|
+
const flagEnabled = pi.getFlag("prompt-autocomplete") === true;
|
|
409
|
+
const flagConfig = parseConfig(pi);
|
|
410
|
+
|
|
411
|
+
shared.enabled = resolveOverride(shared.runtimeOverrides.enabled, flagEnabled);
|
|
412
|
+
shared.config = {
|
|
413
|
+
...flagConfig,
|
|
414
|
+
allowWhileStreaming: resolveOverride(
|
|
415
|
+
shared.runtimeOverrides.allowWhileStreaming,
|
|
416
|
+
flagConfig.allowWhileStreaming,
|
|
417
|
+
),
|
|
418
|
+
streamResponses: resolveOverride(shared.runtimeOverrides.streamResponses, flagConfig.streamResponses),
|
|
419
|
+
debug: resolveOverride(shared.runtimeOverrides.debug, flagConfig.debug),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
222
423
|
function truncateDebug(text: string, maxLength = 140): string {
|
|
223
424
|
const normalized = text.replace(/\s+/g, " ").trim();
|
|
224
425
|
if (normalized.length <= maxLength) return normalized;
|
|
@@ -249,18 +450,21 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
|
|
|
249
450
|
: "current active model";
|
|
250
451
|
|
|
251
452
|
return [
|
|
252
|
-
`enabled=${shared.enabled ? "yes" : "no"}`,
|
|
453
|
+
`enabled=${shared.enabled ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.enabled)})`,
|
|
253
454
|
`editor=${editorState}`,
|
|
254
455
|
shared.editorBlockedReason ? `editor-blocked=${truncateDebug(shared.editorBlockedReason, 90)}` : undefined,
|
|
255
456
|
`model=${formatModelLabel(resolvedModel)}`,
|
|
256
457
|
`requested-model=${requestedModel}`,
|
|
257
|
-
`while-streaming=${shared.config.allowWhileStreaming ? "yes" : "no"}`,
|
|
258
|
-
`
|
|
458
|
+
`while-streaming=${shared.config.allowWhileStreaming ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.allowWhileStreaming)})`,
|
|
459
|
+
`stream=${shared.config.streamResponses ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.streamResponses)})`,
|
|
460
|
+
`request-path=${shared.config.streamResponses && shared.streamSimple ? "stream" : shared.config.streamResponses ? "complete-compat" : "complete"}`,
|
|
461
|
+
`debug=${shared.config.debug ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.debug)})`,
|
|
259
462
|
`debounce=${shared.config.debounceMs}ms`,
|
|
260
463
|
`min-chars=${shared.config.minPromptChars}`,
|
|
261
464
|
`max-suggestion-chars=${shared.config.maxSuggestionChars}`,
|
|
262
465
|
`max-alternatives=${shared.config.maxAlternatives}`,
|
|
263
466
|
`cache-size=${shared.requestCache.size}`,
|
|
467
|
+
`usage=${formatUsageStats(shared.usageStats)}`,
|
|
264
468
|
`state=${shared.debugState || "idle"}`,
|
|
265
469
|
shared.lastError ? `error=${truncateDebug(shared.lastError, 90)}` : undefined,
|
|
266
470
|
shared.lastRawResponse ? `raw=${truncateDebug(shared.lastRawResponse, 90)}` : undefined,
|
|
@@ -270,13 +474,55 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
|
|
|
270
474
|
.join(" | ");
|
|
271
475
|
}
|
|
272
476
|
|
|
477
|
+
function cloneCacheEntry(entry: PromptAutocompleteCacheEntry): PromptAutocompleteCacheEntry {
|
|
478
|
+
return { ...entry, suggestions: [...entry.suggestions] };
|
|
479
|
+
}
|
|
480
|
+
|
|
273
481
|
function getCachedRequest(
|
|
274
482
|
shared: PromptAutocompleteSharedState,
|
|
275
483
|
cacheKey: string,
|
|
276
484
|
options: { bypass?: boolean } = {},
|
|
277
485
|
): PromptAutocompleteCacheEntry | undefined {
|
|
278
486
|
const entry = shared.requestCache.get(cacheKey, options);
|
|
279
|
-
return entry ?
|
|
487
|
+
return entry ? cloneCacheEntry(entry) : undefined;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function rearmPrefixReuseFromExactHit(
|
|
491
|
+
shared: PromptAutocompleteSharedState,
|
|
492
|
+
cacheKey: string,
|
|
493
|
+
entry: PromptAutocompleteCacheEntry,
|
|
494
|
+
): void {
|
|
495
|
+
const expiresAt = shared.requestCache.getExpiration(cacheKey);
|
|
496
|
+
if (expiresAt === undefined) return;
|
|
497
|
+
shared.prefixReuseCache.set(entry.prefixContextKey, cloneCacheEntry(entry), { expiresAt });
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
interface PrefixReusedCacheEntry {
|
|
501
|
+
entry: PromptAutocompleteCacheEntry;
|
|
502
|
+
origins: string[];
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function getPrefixReusedRequest(
|
|
506
|
+
shared: PromptAutocompleteSharedState,
|
|
507
|
+
request: SuggestionRequest,
|
|
508
|
+
options: { bypass?: boolean } = {},
|
|
509
|
+
): PrefixReusedCacheEntry | undefined {
|
|
510
|
+
const cached = shared.prefixReuseCache.get(request.prefixContextKey, options);
|
|
511
|
+
if (!cached) return undefined;
|
|
512
|
+
|
|
513
|
+
const reused = reusePromptAutocompleteSuggestions(
|
|
514
|
+
cached.draft,
|
|
515
|
+
request.draft,
|
|
516
|
+
cached.suggestions,
|
|
517
|
+
request.maxSuggestionChars,
|
|
518
|
+
request.maxAlternatives,
|
|
519
|
+
);
|
|
520
|
+
if (!reused) return undefined;
|
|
521
|
+
|
|
522
|
+
return {
|
|
523
|
+
entry: { ...cloneCacheEntry(cached), draft: request.draft, suggestions: reused.suggestions },
|
|
524
|
+
origins: reused.origins,
|
|
525
|
+
};
|
|
280
526
|
}
|
|
281
527
|
|
|
282
528
|
function storeCachedRequest(
|
|
@@ -284,9 +530,10 @@ function storeCachedRequest(
|
|
|
284
530
|
cacheKey: string,
|
|
285
531
|
entry: PromptAutocompleteCacheEntry,
|
|
286
532
|
): PromptAutocompleteCacheEntry {
|
|
287
|
-
const cachedEntry =
|
|
533
|
+
const cachedEntry = cloneCacheEntry(entry);
|
|
288
534
|
shared.requestCache.set(cacheKey, cachedEntry);
|
|
289
|
-
|
|
535
|
+
shared.prefixReuseCache.set(entry.prefixContextKey, cachedEntry);
|
|
536
|
+
return cloneCacheEntry(cachedEntry);
|
|
290
537
|
}
|
|
291
538
|
|
|
292
539
|
class PromptAutocompleteEditor extends CustomEditor {
|
|
@@ -295,7 +542,10 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
295
542
|
private readonly appKeybindings: KeybindingsManager;
|
|
296
543
|
|
|
297
544
|
private suggestions: string[] = [];
|
|
545
|
+
/** Stable terminal-cache identities parallel to the displayed suggestions. */
|
|
546
|
+
private suggestionOrigins: string[] = [];
|
|
298
547
|
private suggestionIndex: number = 0;
|
|
548
|
+
private suggestionsProvisional = false;
|
|
299
549
|
private debounceTimer?: ReturnType<typeof setTimeout>;
|
|
300
550
|
private requestSeq = 0;
|
|
301
551
|
private readonly pendingRequests = new SequenceOwnedSlot<PendingSuggestionRequest>();
|
|
@@ -463,12 +713,23 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
463
713
|
return this.suggestions[this.suggestionIndex];
|
|
464
714
|
}
|
|
465
715
|
|
|
466
|
-
private
|
|
467
|
-
|
|
716
|
+
private getActiveSuggestionOrigin(): string | undefined {
|
|
717
|
+
return this.suggestionOrigins[this.suggestionIndex];
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
private setSuggestions(
|
|
721
|
+
nextSuggestions: string[],
|
|
722
|
+
options: { provisional?: boolean; origins?: string[]; preferredOrigin?: string } = {},
|
|
723
|
+
): void {
|
|
724
|
+
const provisional = options.provisional ?? false;
|
|
725
|
+
const nextOrigins = options.origins?.length === nextSuggestions.length
|
|
726
|
+
? [...options.origins]
|
|
727
|
+
: [...nextSuggestions];
|
|
728
|
+
const currentOrigin = options.preferredOrigin ?? this.getActiveSuggestionOrigin();
|
|
468
729
|
let nextIndex = 0;
|
|
469
730
|
|
|
470
|
-
if (
|
|
471
|
-
const preservedIndex =
|
|
731
|
+
if (currentOrigin) {
|
|
732
|
+
const preservedIndex = nextOrigins.indexOf(currentOrigin);
|
|
472
733
|
if (preservedIndex !== -1) {
|
|
473
734
|
nextIndex = preservedIndex;
|
|
474
735
|
}
|
|
@@ -480,12 +741,26 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
480
741
|
nextIndex = 0;
|
|
481
742
|
}
|
|
482
743
|
|
|
483
|
-
if (
|
|
744
|
+
if (
|
|
745
|
+
arraysEqual(this.suggestions, nextSuggestions)
|
|
746
|
+
&& arraysEqual(this.suggestionOrigins, nextOrigins)
|
|
747
|
+
&& this.suggestionIndex === nextIndex
|
|
748
|
+
&& this.suggestionsProvisional === provisional
|
|
749
|
+
) {
|
|
484
750
|
return;
|
|
485
751
|
}
|
|
486
752
|
|
|
753
|
+
// Count active ghost-text offers, not hidden alternatives or every streamed
|
|
754
|
+
// revision of the same candidate. Cycling records a new offer when another
|
|
755
|
+
// alternative becomes active.
|
|
756
|
+
if (nextSuggestions.length > 0 && this.suggestions.length === 0) {
|
|
757
|
+
this.shared.usageStats.suggestionsOffered += 1;
|
|
758
|
+
}
|
|
759
|
+
|
|
487
760
|
this.suggestions = [...nextSuggestions];
|
|
761
|
+
this.suggestionOrigins = nextOrigins;
|
|
488
762
|
this.suggestionIndex = nextIndex;
|
|
763
|
+
this.suggestionsProvisional = nextSuggestions.length > 0 && provisional;
|
|
489
764
|
|
|
490
765
|
if (nextSuggestions.length > 0) {
|
|
491
766
|
updateDebugState(
|
|
@@ -503,6 +778,7 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
503
778
|
const nextIndex = (this.suggestionIndex + delta + this.suggestions.length) % this.suggestions.length;
|
|
504
779
|
if (nextIndex === this.suggestionIndex) return;
|
|
505
780
|
this.suggestionIndex = nextIndex;
|
|
781
|
+
this.shared.usageStats.suggestionsOffered += 1;
|
|
506
782
|
this.dismissedKey = undefined;
|
|
507
783
|
updateDebugState(this.shared, "cycled", `Now showing ${nextIndex + 1}/${this.suggestions.length}`);
|
|
508
784
|
this.tui.requestRender();
|
|
@@ -579,23 +855,30 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
579
855
|
const suggestion = this.getActiveSuggestion();
|
|
580
856
|
if (!suggestion) return;
|
|
581
857
|
|
|
858
|
+
const wasProvisional = this.suggestionsProvisional;
|
|
859
|
+
this.shared.usageStats.fullAccepts += 1;
|
|
582
860
|
this.dismissedKey = undefined;
|
|
583
|
-
this.clearInlineSuggestion("accepted", "Accepted full suggestion");
|
|
861
|
+
this.clearInlineSuggestion("accepted", wasProvisional ? "Accepted streamed partial suggestion" : "Accepted full suggestion");
|
|
584
862
|
super.insertTextAtCursor(suggestion);
|
|
585
|
-
|
|
863
|
+
// A provisional accept is explicit cancellation of the request. Starting a
|
|
864
|
+
// fresh paid request immediately would turn one Tab press into two calls.
|
|
865
|
+
if (!wasProvisional) this.refreshSuggestion();
|
|
586
866
|
}
|
|
587
867
|
|
|
588
868
|
private acceptInlineSuggestionByWord(): void {
|
|
589
869
|
const suggestion = this.getActiveSuggestion();
|
|
590
870
|
if (!suggestion) return;
|
|
591
871
|
|
|
872
|
+
const origin = this.getActiveSuggestionOrigin();
|
|
592
873
|
const chunk = extractNextSuggestionChunk(suggestion) ?? suggestion;
|
|
593
874
|
if (!chunk) return;
|
|
594
875
|
|
|
876
|
+
const wasProvisional = this.suggestionsProvisional;
|
|
877
|
+
this.shared.usageStats.chunkAccepts += 1;
|
|
595
878
|
this.dismissedKey = undefined;
|
|
596
879
|
this.clearInlineSuggestion("accepted-word", `Accepted chunk: ${chunk}`);
|
|
597
880
|
super.insertTextAtCursor(chunk);
|
|
598
|
-
this.refreshSuggestion();
|
|
881
|
+
if (!wasProvisional) this.refreshSuggestion({ preferredOrigin: origin });
|
|
599
882
|
}
|
|
600
883
|
|
|
601
884
|
private shouldRenderInlineSuggestion(): boolean {
|
|
@@ -622,7 +905,7 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
622
905
|
// (streaming gate, post-error cooldown, min-chars). Model/auth and slash/path
|
|
623
906
|
// checks still apply because those can never produce a useful suggestion.
|
|
624
907
|
if (!options.manual) {
|
|
625
|
-
if (!this.shared.config.allowWhileStreaming && this.shared.
|
|
908
|
+
if (!this.shared.config.allowWhileStreaming && this.shared.agentStreaming) {
|
|
626
909
|
return "Waiting for the current agent turn to finish";
|
|
627
910
|
}
|
|
628
911
|
if (this.shared.now() < this.suspendedUntil) return "In temporary cooldown after the last error";
|
|
@@ -643,6 +926,7 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
643
926
|
}
|
|
644
927
|
|
|
645
928
|
private refreshSuggestion(options: SuggestionRefreshOptions = {}): void {
|
|
929
|
+
const preferredOrigin = options.preferredOrigin ?? this.getActiveSuggestionOrigin();
|
|
646
930
|
if (options.clearExisting) {
|
|
647
931
|
this.cancelPendingRequest();
|
|
648
932
|
this.setSuggestions([]);
|
|
@@ -695,13 +979,37 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
695
979
|
|
|
696
980
|
const cachedEntry = getCachedRequest(this.shared, request.cacheKey, { bypass: options.manual });
|
|
697
981
|
if (cachedEntry) {
|
|
982
|
+
this.shared.usageStats.cacheHits += 1;
|
|
983
|
+
rearmPrefixReuseFromExactHit(this.shared, request.cacheKey, cachedEntry);
|
|
698
984
|
this.cancelPendingRequest();
|
|
699
985
|
this.shared.lastRawResponse = cachedEntry.rawResponse;
|
|
700
986
|
this.shared.lastError = cachedEntry.error;
|
|
701
987
|
if (cachedEntry.suggestions.length === 0) {
|
|
702
988
|
updateDebugState(this.shared, "cache-hit", cachedEntry.debugState || "Cached no-suggestion result");
|
|
703
989
|
}
|
|
704
|
-
this.setSuggestions(cachedEntry.suggestions
|
|
990
|
+
this.setSuggestions(cachedEntry.suggestions, {
|
|
991
|
+
origins: cachedEntry.suggestions,
|
|
992
|
+
preferredOrigin,
|
|
993
|
+
});
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
const prefixReused = getPrefixReusedRequest(this.shared, request, { bypass: options.manual });
|
|
998
|
+
if (prefixReused) {
|
|
999
|
+
this.shared.usageStats.cacheHits += 1;
|
|
1000
|
+
this.shared.usageStats.prefixReuseHits += 1;
|
|
1001
|
+
this.cancelPendingRequest();
|
|
1002
|
+
this.shared.lastRawResponse = prefixReused.entry.rawResponse;
|
|
1003
|
+
this.shared.lastError = prefixReused.entry.error;
|
|
1004
|
+
this.setSuggestions(prefixReused.entry.suggestions, {
|
|
1005
|
+
origins: prefixReused.origins,
|
|
1006
|
+
preferredOrigin,
|
|
1007
|
+
});
|
|
1008
|
+
updateDebugState(
|
|
1009
|
+
this.shared,
|
|
1010
|
+
"prefix-cache-hit",
|
|
1011
|
+
`${prefixReused.entry.suggestions.length} suggestion(s) reused without a provider request`,
|
|
1012
|
+
);
|
|
705
1013
|
return;
|
|
706
1014
|
}
|
|
707
1015
|
|
|
@@ -751,23 +1059,28 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
751
1059
|
const modelLabel = formatModelLabel(model);
|
|
752
1060
|
const leafId = this.shared.sessionManager?.getLeafId?.() ?? "";
|
|
753
1061
|
|
|
1062
|
+
const prefixContextIdentity = {
|
|
1063
|
+
leafId,
|
|
1064
|
+
modelLabel,
|
|
1065
|
+
maxAlternatives: this.shared.config.maxAlternatives,
|
|
1066
|
+
maxSuggestionChars: this.shared.config.maxSuggestionChars,
|
|
1067
|
+
latestAssistantContext,
|
|
1068
|
+
latestUserContext,
|
|
1069
|
+
recentContext,
|
|
1070
|
+
};
|
|
1071
|
+
|
|
754
1072
|
return {
|
|
755
1073
|
activationId: this.activationId,
|
|
1074
|
+
leafId,
|
|
756
1075
|
draft,
|
|
757
1076
|
draftTail,
|
|
758
1077
|
model,
|
|
759
1078
|
modelLabel,
|
|
760
|
-
cacheKey: buildPromptAutocompleteCacheKey({
|
|
761
|
-
|
|
762
|
-
modelLabel,
|
|
763
|
-
maxAlternatives: this.shared.config.maxAlternatives,
|
|
764
|
-
maxSuggestionChars: this.shared.config.maxSuggestionChars,
|
|
765
|
-
draft,
|
|
766
|
-
latestAssistantContext,
|
|
767
|
-
latestUserContext,
|
|
768
|
-
recentContext,
|
|
769
|
-
}),
|
|
1079
|
+
cacheKey: buildPromptAutocompleteCacheKey({ ...prefixContextIdentity, draft }),
|
|
1080
|
+
prefixContextKey: buildPromptAutocompletePrefixContextKey(prefixContextIdentity),
|
|
770
1081
|
maxAlternatives: this.shared.config.maxAlternatives,
|
|
1082
|
+
maxSuggestionChars: this.shared.config.maxSuggestionChars,
|
|
1083
|
+
useStreaming: this.shared.config.streamResponses && !!this.shared.streamSimple,
|
|
771
1084
|
latestAssistantContext,
|
|
772
1085
|
latestUserContext,
|
|
773
1086
|
recentContext,
|
|
@@ -786,7 +1099,8 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
786
1099
|
this.shared.inFlightRequests,
|
|
787
1100
|
request.cacheKey,
|
|
788
1101
|
subscriberId,
|
|
789
|
-
(signal) => this.fetchSuggestionUncached(request, signal),
|
|
1102
|
+
(signal, publish) => this.fetchSuggestionUncached(request, signal, publish),
|
|
1103
|
+
(suggestion) => this.showStreamedSuggestion(request, seq, spinnerOwner, suggestion),
|
|
790
1104
|
);
|
|
791
1105
|
|
|
792
1106
|
this.activeRequestSubscription = subscription;
|
|
@@ -831,9 +1145,28 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
831
1145
|
}
|
|
832
1146
|
}
|
|
833
1147
|
|
|
1148
|
+
private showStreamedSuggestion(
|
|
1149
|
+
request: SuggestionRequest,
|
|
1150
|
+
seq: number,
|
|
1151
|
+
spinnerOwner: string,
|
|
1152
|
+
suggestion: string,
|
|
1153
|
+
): void {
|
|
1154
|
+
if (!this.isRequestStillCurrent(request, seq)) return;
|
|
1155
|
+
if (request.cacheKey === this.dismissedKey) return;
|
|
1156
|
+
|
|
1157
|
+
const current = this.getActiveSuggestion();
|
|
1158
|
+
// Progress is strictly monotonic. If a provider revises earlier text, keep
|
|
1159
|
+
// the last honest preview and let the terminal response replace it.
|
|
1160
|
+
if (current && (!suggestion.startsWith(current) || suggestion.length <= current.length)) return;
|
|
1161
|
+
|
|
1162
|
+
this.setSuggestions([suggestion], { provisional: true });
|
|
1163
|
+
this.deactivateSpinner(spinnerOwner);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
834
1166
|
private async fetchSuggestionUncached(
|
|
835
1167
|
request: SuggestionRequest,
|
|
836
1168
|
signal: AbortSignal,
|
|
1169
|
+
publish: (suggestion: string) => void,
|
|
837
1170
|
): Promise<PromptAutocompleteCacheEntry> {
|
|
838
1171
|
if (!this.shared.modelRegistry) {
|
|
839
1172
|
throw new Error("No model registry available");
|
|
@@ -892,43 +1225,81 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
892
1225
|
timestamp: Date.now(),
|
|
893
1226
|
};
|
|
894
1227
|
|
|
895
|
-
const
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1228
|
+
const stats = this.shared.usageStats;
|
|
1229
|
+
stats.providerRequests += 1;
|
|
1230
|
+
|
|
1231
|
+
const context = {
|
|
1232
|
+
systemPrompt: PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
|
|
1233
|
+
messages: [userMessage],
|
|
1234
|
+
};
|
|
1235
|
+
const requestOptions = {
|
|
1236
|
+
apiKey: auth.apiKey,
|
|
1237
|
+
headers: auth.headers,
|
|
1238
|
+
signal,
|
|
1239
|
+
maxTokens: computeRequestMaxTokens(request.maxAlternatives, request.maxSuggestionChars),
|
|
1240
|
+
timeoutMs: REQUEST_TIMEOUT_MS,
|
|
1241
|
+
maxRetries: REQUEST_MAX_RETRIES,
|
|
1242
|
+
maxRetryDelayMs: REQUEST_MAX_RETRY_DELAY_MS,
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
const providerStartedAt = this.shared.now();
|
|
1246
|
+
let response: AssistantMessage;
|
|
1247
|
+
try {
|
|
1248
|
+
if (request.useStreaming && this.shared.streamSimple) {
|
|
1249
|
+
const hardDeadlineController = new AbortController();
|
|
1250
|
+
const streamSignal = AbortSignal.any([signal, hardDeadlineController.signal]);
|
|
1251
|
+
const stream = this.shared.streamSimple(request.model, context, {
|
|
1252
|
+
...requestOptions,
|
|
1253
|
+
signal: streamSignal,
|
|
1254
|
+
});
|
|
1255
|
+
let lastPublished = "";
|
|
1256
|
+
|
|
1257
|
+
// Exactly one consumer receives the terminal message. Partial text is
|
|
1258
|
+
// fanned out through coalescing; after UI cancellation the callback is
|
|
1259
|
+
// gone, but the bounded drain can still retain terminal usage.
|
|
1260
|
+
response = await consumeAssistantStream(stream, streamSignal, () => hardDeadlineController.abort(), (partial) => {
|
|
1261
|
+
// `partial` is cumulative. Appending delta plus text_end.content would
|
|
1262
|
+
// duplicate the completed block on providers that emit both.
|
|
1263
|
+
const partialText = extractAssistantText(partial);
|
|
1264
|
+
const suggestion = parsePartialPromptSuggestion(request.draft, partialText, request.maxSuggestionChars);
|
|
1265
|
+
if (!suggestion || suggestion === lastPublished) return;
|
|
1266
|
+
if (lastPublished && !suggestion.startsWith(lastPublished)) return;
|
|
1267
|
+
lastPublished = suggestion;
|
|
1268
|
+
publish(suggestion);
|
|
1269
|
+
});
|
|
1270
|
+
} else {
|
|
1271
|
+
response = await this.shared.completeSimple(request.model, context, requestOptions);
|
|
1272
|
+
}
|
|
1273
|
+
} catch (error) {
|
|
1274
|
+
stats.failedRequests += 1;
|
|
1275
|
+
throw error;
|
|
1276
|
+
} finally {
|
|
1277
|
+
recordProviderLatency(stats, this.shared.now() - providerStartedAt);
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
// Tokens spent on a failed or aborted response are still spent, so usage is
|
|
1281
|
+
// recorded before the stop-reason check rejects the request.
|
|
1282
|
+
recordProviderUsage(stats, response.usage);
|
|
911
1283
|
|
|
912
1284
|
const contentTypes = response.content.map((block) => block.type).join(", ") || "(none)";
|
|
913
|
-
const text = response
|
|
914
|
-
.filter((block): block is { type: "text"; text: string } => block.type === "text")
|
|
915
|
-
.map((block) => block.text)
|
|
916
|
-
.join("");
|
|
1285
|
+
const text = extractAssistantText(response);
|
|
917
1286
|
|
|
918
1287
|
const responseError = response.errorMessage?.trim();
|
|
919
1288
|
const rawResponse = text || responseError || `[types=${contentTypes}; stopReason=${response.stopReason}]`;
|
|
920
1289
|
|
|
921
1290
|
if (response.stopReason === "error" || response.stopReason === "aborted") {
|
|
1291
|
+
stats.failedRequests += 1;
|
|
922
1292
|
throw new Error(responseError || `Provider returned stopReason=${response.stopReason}`);
|
|
923
1293
|
}
|
|
924
1294
|
if (signal.aborted) {
|
|
1295
|
+
stats.failedRequests += 1;
|
|
925
1296
|
throw new Error("Request was aborted");
|
|
926
1297
|
}
|
|
927
1298
|
|
|
928
1299
|
const normalized = normalizePromptSuggestions(
|
|
929
1300
|
request.draft,
|
|
930
1301
|
text,
|
|
931
|
-
|
|
1302
|
+
request.maxSuggestionChars,
|
|
932
1303
|
request.maxAlternatives,
|
|
933
1304
|
);
|
|
934
1305
|
|
|
@@ -938,6 +1309,8 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
938
1309
|
: `${normalized.length} suggestion(s) ready`;
|
|
939
1310
|
|
|
940
1311
|
return storeCachedRequest(this.shared, request.cacheKey, {
|
|
1312
|
+
draft: request.draft,
|
|
1313
|
+
prefixContextKey: request.prefixContextKey,
|
|
941
1314
|
suggestions: normalized,
|
|
942
1315
|
rawResponse,
|
|
943
1316
|
debugState,
|
|
@@ -955,7 +1328,12 @@ class PromptAutocompleteEditor extends CustomEditor {
|
|
|
955
1328
|
if (this.isShowingAutocomplete()) return false;
|
|
956
1329
|
|
|
957
1330
|
const currentModel = resolveSuggestionModel(this.shared);
|
|
958
|
-
|
|
1331
|
+
if (formatModelLabel(currentModel) !== request.modelLabel) return false;
|
|
1332
|
+
|
|
1333
|
+
// Session entries are immutable and the leaf identifies the exact branch
|
|
1334
|
+
// from which all bounded context sections were derived. Checking it avoids
|
|
1335
|
+
// rebuilding/scanning and hashing that context for every streamed token.
|
|
1336
|
+
return (this.shared.sessionManager?.getLeafId?.() ?? "") === request.leafId;
|
|
959
1337
|
}
|
|
960
1338
|
|
|
961
1339
|
cancelActiveRequest(): void {
|
|
@@ -997,8 +1375,28 @@ function releaseEditorRuntime(shared: PromptAutocompleteSharedState): void {
|
|
|
997
1375
|
shared.ownsEditor = undefined;
|
|
998
1376
|
}
|
|
999
1377
|
|
|
1378
|
+
/**
|
|
1379
|
+
* Whether this host can hand the prompt editor to the extension.
|
|
1380
|
+
*
|
|
1381
|
+
* Hosts that report `ctx.mode` stay authoritative (only `"tui"` owns the
|
|
1382
|
+
* editor). Hosts from older forks of the extension API omit `mode` entirely, so
|
|
1383
|
+
* UI availability plus a usable editor slot decides instead.
|
|
1384
|
+
*/
|
|
1385
|
+
function hostOwnsInteractiveEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1386
|
+
// A host that already accepted an editor factory without installing it is not
|
|
1387
|
+
// interactive, no matter what its capabilities claim.
|
|
1388
|
+
if (shared.hostInstallsEditors === false) return false;
|
|
1389
|
+
const host = ctx as Partial<Pick<ExtensionContext, "mode" | "hasUI" | "ui">>;
|
|
1390
|
+
return isInteractiveEditorHost({
|
|
1391
|
+
mode: host.mode,
|
|
1392
|
+
hasUI: host.hasUI,
|
|
1393
|
+
canInstallEditor:
|
|
1394
|
+
typeof host.ui?.setEditorComponent === "function" && typeof host.ui?.getEditorComponent === "function",
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1000
1398
|
function mountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1001
|
-
if (ctx
|
|
1399
|
+
if (!hostOwnsInteractiveEditor(ctx, shared)) return false;
|
|
1002
1400
|
|
|
1003
1401
|
if (shared.editorMount) {
|
|
1004
1402
|
if (shared.ownsEditor?.()) return true;
|
|
@@ -1095,13 +1493,33 @@ function mountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedStat
|
|
|
1095
1493
|
throw error;
|
|
1096
1494
|
}
|
|
1097
1495
|
|
|
1496
|
+
// Some hosts accept the call without installing anything: forked front-ends
|
|
1497
|
+
// (for example prime-agent's RPC mode) expose a no-op editor slot while still
|
|
1498
|
+
// reporting UI availability. Verify the installation instead of trusting it.
|
|
1499
|
+
if (ctx.ui.getEditorComponent() !== installedFactory) {
|
|
1500
|
+
releaseEditorRuntime(shared);
|
|
1501
|
+
shared.editorMount = undefined;
|
|
1502
|
+
shared.hostInstallsEditors = false;
|
|
1503
|
+
// Nothing can render or request here, so the extension must not stay marked
|
|
1504
|
+
// enabled, no matter which path attempted the mount.
|
|
1505
|
+
shared.enabled = false;
|
|
1506
|
+
const reason = "This host does not install custom editors; prompt autocomplete stays inactive";
|
|
1507
|
+
const shouldNotify = shared.editorBlockedReason !== reason;
|
|
1508
|
+
shared.editorBlockedReason = reason;
|
|
1509
|
+
updateDebugState(shared, "blocked-no-editor-slot", reason);
|
|
1510
|
+
if (shouldNotify) ctx.ui.notify(reason, "warning");
|
|
1511
|
+
return false;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
shared.hostInstallsEditors = true;
|
|
1098
1515
|
updateDebugState(shared, "mounted", "Editor extension attached");
|
|
1099
1516
|
return true;
|
|
1100
1517
|
}
|
|
1101
1518
|
|
|
1102
1519
|
function unmountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): void {
|
|
1103
1520
|
const mount = shared.editorMount;
|
|
1104
|
-
const shouldRestore =
|
|
1521
|
+
const shouldRestore =
|
|
1522
|
+
hostOwnsInteractiveEditor(ctx, shared) && !!mount && ctx.ui.getEditorComponent() === mount.installedFactory;
|
|
1105
1523
|
|
|
1106
1524
|
releaseEditorRuntime(shared);
|
|
1107
1525
|
shared.editorMount = undefined;
|
|
@@ -1115,14 +1533,18 @@ function unmountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedSt
|
|
|
1115
1533
|
function resetSharedForSession(pi: ExtensionAPI, shared: PromptAutocompleteSharedState): void {
|
|
1116
1534
|
shared.cancelActiveRequest?.();
|
|
1117
1535
|
shared.cancelActiveRequest = undefined;
|
|
1118
|
-
shared
|
|
1119
|
-
|
|
1536
|
+
applyEffectiveConfig(pi, shared);
|
|
1537
|
+
// A host that never installs custom editors stays inactive for the rest of
|
|
1538
|
+
// the process, so flags must not re-enable it in a later session either.
|
|
1539
|
+
if (shared.hostInstallsEditors === false) shared.enabled = false;
|
|
1540
|
+
shared.usageStats = createPromptAutocompleteUsageStats();
|
|
1120
1541
|
shared.editorMount = undefined;
|
|
1121
1542
|
shared.editorBlockedReason = undefined;
|
|
1122
1543
|
shared.ownsEditor = undefined;
|
|
1123
1544
|
shared.lastError = undefined;
|
|
1124
1545
|
shared.lastRawResponse = undefined;
|
|
1125
1546
|
shared.requestCache.clear();
|
|
1547
|
+
shared.prefixReuseCache.clear();
|
|
1126
1548
|
cancelAllCoalescedRequests(shared.inFlightRequests);
|
|
1127
1549
|
shared.debugState = shared.enabled ? "configured" : "disabled";
|
|
1128
1550
|
}
|
|
@@ -1131,7 +1553,7 @@ function bindRuntimeContext(ctx: ExtensionContext, shared: PromptAutocompleteSha
|
|
|
1131
1553
|
shared.currentModel = ctx.model as Model<Api> | undefined;
|
|
1132
1554
|
shared.modelRegistry = ctx.modelRegistry;
|
|
1133
1555
|
shared.sessionManager = ctx.sessionManager;
|
|
1134
|
-
shared.
|
|
1556
|
+
shared.agentStreaming = false;
|
|
1135
1557
|
}
|
|
1136
1558
|
|
|
1137
1559
|
function refreshEditorImmediately(shared: PromptAutocompleteSharedState, state: string, details: string): void {
|
|
@@ -1141,6 +1563,7 @@ function refreshEditorImmediately(shared: PromptAutocompleteSharedState, state:
|
|
|
1141
1563
|
|
|
1142
1564
|
function setDebugDisplay(shared: PromptAutocompleteSharedState, enabled: boolean): void {
|
|
1143
1565
|
shared.config.debug = enabled;
|
|
1566
|
+
shared.runtimeOverrides.debug = enabled;
|
|
1144
1567
|
if (enabled) {
|
|
1145
1568
|
updateDebugState(shared, shared.debugState || "ready");
|
|
1146
1569
|
} else {
|
|
@@ -1166,25 +1589,75 @@ function setWhileStreaming(
|
|
|
1166
1589
|
}
|
|
1167
1590
|
|
|
1168
1591
|
shared.config.allowWhileStreaming = next;
|
|
1592
|
+
shared.runtimeOverrides.allowWhileStreaming = next;
|
|
1169
1593
|
ctx.ui.notify(`Prompt autocomplete while-streaming ${next ? "enabled" : "disabled"}`, "info");
|
|
1170
1594
|
|
|
1171
1595
|
if (shared.enabled) {
|
|
1172
1596
|
// Only label it "waiting" when disabling actually suppresses right now (agent
|
|
1173
1597
|
// streaming); otherwise the refresh below requests suggestions immediately.
|
|
1174
|
-
const willSuppress = !next && shared.
|
|
1598
|
+
const willSuppress = !next && shared.agentStreaming;
|
|
1175
1599
|
refreshEditorImmediately(shared, willSuppress ? "waiting" : "ready", `While-streaming ${next ? "enabled" : "disabled"}`);
|
|
1176
1600
|
}
|
|
1177
1601
|
}
|
|
1178
1602
|
|
|
1603
|
+
function setStreamResponses(
|
|
1604
|
+
ctx: ExtensionContext,
|
|
1605
|
+
shared: PromptAutocompleteSharedState,
|
|
1606
|
+
arg: string | undefined,
|
|
1607
|
+
): void {
|
|
1608
|
+
let next: boolean;
|
|
1609
|
+
if (arg === "on") {
|
|
1610
|
+
next = true;
|
|
1611
|
+
} else if (arg === "off") {
|
|
1612
|
+
next = false;
|
|
1613
|
+
} else if (arg === undefined || arg === "toggle") {
|
|
1614
|
+
next = !shared.config.streamResponses;
|
|
1615
|
+
} else {
|
|
1616
|
+
ctx.ui.notify("Usage: /prompt-autocomplete stream [on|off|toggle]", "warning");
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
const changed = shared.config.streamResponses !== next;
|
|
1621
|
+
shared.config.streamResponses = next;
|
|
1622
|
+
shared.runtimeOverrides.streamResponses = next;
|
|
1623
|
+
const path = next && shared.streamSimple ? "streaming" : next ? "completion compatibility" : "completion";
|
|
1624
|
+
ctx.ui.notify(
|
|
1625
|
+
`Prompt autocomplete response streaming ${next ? "enabled" : "disabled"} (${path} path)${changed ? "; applies to next request" : ""}`,
|
|
1626
|
+
"info",
|
|
1627
|
+
);
|
|
1628
|
+
|
|
1629
|
+
if (shared.enabled && changed) {
|
|
1630
|
+
// Presentation changes must not silently buy a replacement request. Cancel
|
|
1631
|
+
// active work and clear provisional text; the next edit or manual trigger
|
|
1632
|
+
// uses the selected path.
|
|
1633
|
+
shared.cancelActiveRequest?.();
|
|
1634
|
+
updateDebugState(shared, "ready", `Response streaming ${next ? "enabled" : "disabled"}; waiting for next request`);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1179
1638
|
function enablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1639
|
+
const previousOverride = shared.runtimeOverrides.enabled;
|
|
1180
1640
|
shared.enabled = true;
|
|
1181
|
-
|
|
1641
|
+
shared.runtimeOverrides.enabled = true;
|
|
1642
|
+
|
|
1643
|
+
if (!mountEditor(ctx, shared) && shared.hostInstallsEditors === false) {
|
|
1644
|
+
// The host accepted the editor factory without installing it. mountEditor
|
|
1645
|
+
// already cleared `enabled`; keep it cleared and drop the session override
|
|
1646
|
+
// so the refused activation is not replayed as a user decision later.
|
|
1647
|
+
shared.enabled = false;
|
|
1648
|
+
shared.runtimeOverrides.enabled = previousOverride;
|
|
1649
|
+
return false;
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
return shared.editorMount !== undefined;
|
|
1182
1653
|
}
|
|
1183
1654
|
|
|
1184
1655
|
function disablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): void {
|
|
1185
1656
|
shared.enabled = false;
|
|
1657
|
+
shared.runtimeOverrides.enabled = false;
|
|
1186
1658
|
unmountEditor(ctx, shared);
|
|
1187
1659
|
shared.requestCache.clear();
|
|
1660
|
+
shared.prefixReuseCache.clear();
|
|
1188
1661
|
shared.lastRawResponse = undefined;
|
|
1189
1662
|
shared.lastError = undefined;
|
|
1190
1663
|
}
|
|
@@ -1227,6 +1700,9 @@ function createPromptAutocompleteCommandHandlers(
|
|
|
1227
1700
|
status: () => {
|
|
1228
1701
|
ctx.ui.notify(formatStatus(shared), "info");
|
|
1229
1702
|
},
|
|
1703
|
+
stats: () => {
|
|
1704
|
+
ctx.ui.notify(formatPromptAutocompleteStats(shared.usageStats), "info");
|
|
1705
|
+
},
|
|
1230
1706
|
"debug-on": () => {
|
|
1231
1707
|
setDebugDisplay(shared, true);
|
|
1232
1708
|
ctx.ui.notify("Prompt autocomplete debug display enabled", "info");
|
|
@@ -1247,6 +1723,9 @@ function createPromptAutocompleteCommandHandlers(
|
|
|
1247
1723
|
},
|
|
1248
1724
|
on: () => {
|
|
1249
1725
|
if (shared.enabled && shared.ownsEditor?.()) {
|
|
1726
|
+
// Already in the requested state, but the user still expressed intent:
|
|
1727
|
+
// record it so a later session does not fall back to the flag.
|
|
1728
|
+
shared.runtimeOverrides.enabled = true;
|
|
1250
1729
|
ctx.ui.notify(`Prompt autocomplete already enabled (${formatStatus(shared)})`, "info");
|
|
1251
1730
|
return;
|
|
1252
1731
|
}
|
|
@@ -1257,6 +1736,7 @@ function createPromptAutocompleteCommandHandlers(
|
|
|
1257
1736
|
},
|
|
1258
1737
|
off: () => {
|
|
1259
1738
|
if (!shared.enabled) {
|
|
1739
|
+
shared.runtimeOverrides.enabled = false;
|
|
1260
1740
|
ctx.ui.notify("Prompt autocomplete is already disabled", "info");
|
|
1261
1741
|
return;
|
|
1262
1742
|
}
|
|
@@ -1281,7 +1761,13 @@ function createPromptAutocompleteCommandHandlers(
|
|
|
1281
1761
|
export function createPromptAutocompleteExtension(
|
|
1282
1762
|
dependencies: PromptAutocompleteDependencies = {},
|
|
1283
1763
|
): (pi: ExtensionAPI) => void {
|
|
1284
|
-
const completeSimpleImpl = dependencies.completeSimple ??
|
|
1764
|
+
const completeSimpleImpl = dependencies.completeSimple ?? hostCompleteSimple;
|
|
1765
|
+
const hostStreaming = hostSupportsStreamedResponses(piAiSimpleApi);
|
|
1766
|
+
// Existing deterministic harnesses inject only completeSimple. Never let such
|
|
1767
|
+
// a harness fall through to the real network-capable stream implementation.
|
|
1768
|
+
const streamSimpleImpl =
|
|
1769
|
+
dependencies.streamSimple
|
|
1770
|
+
?? (dependencies.completeSimple || !hostStreaming ? undefined : hostStreamSimple);
|
|
1285
1771
|
const now = dependencies.now ?? Date.now;
|
|
1286
1772
|
|
|
1287
1773
|
return function promptAutocompleteExtension(pi: ExtensionAPI): void {
|
|
@@ -1300,6 +1786,13 @@ export function createPromptAutocompleteExtension(
|
|
|
1300
1786
|
type: "boolean",
|
|
1301
1787
|
default: false,
|
|
1302
1788
|
});
|
|
1789
|
+
pi.registerFlag("prompt-autocomplete-stream", {
|
|
1790
|
+
description: "Render streamed response progress as ghost text (on|off)",
|
|
1791
|
+
// Pi's boolean extension flags can only be switched on. A string flag is
|
|
1792
|
+
// required so a feature that defaults on can still be disabled at startup.
|
|
1793
|
+
type: "string",
|
|
1794
|
+
default: DEFAULT_STREAM_RESPONSES ? "on" : "off",
|
|
1795
|
+
});
|
|
1303
1796
|
pi.registerFlag("prompt-autocomplete-debug", {
|
|
1304
1797
|
description: "Show prompt autocomplete debug state below the editor",
|
|
1305
1798
|
type: "boolean",
|
|
@@ -1329,12 +1822,16 @@ export function createPromptAutocompleteExtension(
|
|
|
1329
1822
|
const shared: PromptAutocompleteSharedState = {
|
|
1330
1823
|
enabled: false,
|
|
1331
1824
|
activationId: 0,
|
|
1332
|
-
|
|
1825
|
+
agentStreaming: false,
|
|
1333
1826
|
config: parseConfig(pi),
|
|
1827
|
+
runtimeOverrides: {},
|
|
1828
|
+
usageStats: createPromptAutocompleteUsageStats(),
|
|
1334
1829
|
completeSimple: completeSimpleImpl,
|
|
1830
|
+
streamSimple: streamSimpleImpl,
|
|
1335
1831
|
now,
|
|
1336
1832
|
debugState: "idle",
|
|
1337
1833
|
requestCache: new ExpiringLruCache(REQUEST_CACHE_TTL_MS, REQUEST_CACHE_MAX_ENTRIES, now),
|
|
1834
|
+
prefixReuseCache: new ExpiringLruCache(REQUEST_CACHE_TTL_MS, REQUEST_CACHE_MAX_ENTRIES, now),
|
|
1338
1835
|
inFlightRequests: new Map(),
|
|
1339
1836
|
};
|
|
1340
1837
|
|
|
@@ -1345,14 +1842,16 @@ export function createPromptAutocompleteExtension(
|
|
|
1345
1842
|
resetSharedForSession(pi, shared);
|
|
1346
1843
|
bindRuntimeContext(ctx, shared);
|
|
1347
1844
|
|
|
1348
|
-
if (ctx
|
|
1845
|
+
if (!hostOwnsInteractiveEditor(ctx, shared) || !shared.enabled) return;
|
|
1349
1846
|
mountEditor(ctx, shared);
|
|
1350
1847
|
});
|
|
1351
1848
|
|
|
1352
1849
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
1850
|
+
// Teardown, not a user decision: leave runtimeOverrides untouched.
|
|
1353
1851
|
shared.enabled = false;
|
|
1354
1852
|
shared.cancelActiveRequest?.();
|
|
1355
1853
|
shared.requestCache.clear();
|
|
1854
|
+
shared.prefixReuseCache.clear();
|
|
1356
1855
|
cancelAllCoalescedRequests(shared.inFlightRequests);
|
|
1357
1856
|
unmountEditor(ctx, shared);
|
|
1358
1857
|
});
|
|
@@ -1364,22 +1863,28 @@ export function createPromptAutocompleteExtension(
|
|
|
1364
1863
|
refreshEditorImmediately(shared, "model-changed", formatModelLabel(event.model as Model<Api>));
|
|
1365
1864
|
});
|
|
1366
1865
|
|
|
1866
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
1867
|
+
shared.sessionManager = ctx.sessionManager;
|
|
1868
|
+
if (!shared.enabled) return;
|
|
1869
|
+
refreshEditorImmediately(shared, "context-changed", "Conversation branch changed");
|
|
1870
|
+
});
|
|
1871
|
+
|
|
1367
1872
|
pi.on("agent_start", async () => {
|
|
1368
|
-
shared.
|
|
1873
|
+
shared.agentStreaming = true;
|
|
1369
1874
|
if (!shared.enabled || shared.config.allowWhileStreaming) return;
|
|
1370
1875
|
refreshEditorImmediately(shared, "waiting", "Main agent is still working");
|
|
1371
1876
|
});
|
|
1372
1877
|
|
|
1373
1878
|
pi.on("agent_end", async () => {
|
|
1374
|
-
shared.
|
|
1879
|
+
shared.agentStreaming = false;
|
|
1375
1880
|
if (!shared.enabled) return;
|
|
1376
1881
|
refreshEditorImmediately(shared, "ready", "Agent finished; autocomplete can request suggestions again");
|
|
1377
1882
|
});
|
|
1378
1883
|
|
|
1379
1884
|
pi.registerCommand("prompt-autocomplete", {
|
|
1380
|
-
description: "Enable, disable, or inspect inline prompt autocomplete",
|
|
1885
|
+
description: "Enable, disable, or inspect inline prompt autocomplete and session stats",
|
|
1381
1886
|
handler: async (args, ctx) => {
|
|
1382
|
-
if (ctx
|
|
1887
|
+
if (!hostOwnsInteractiveEditor(ctx, shared)) {
|
|
1383
1888
|
ctx.ui.notify("prompt-autocomplete requires interactive TUI mode", "warning");
|
|
1384
1889
|
return;
|
|
1385
1890
|
}
|
|
@@ -1391,6 +1896,10 @@ export function createPromptAutocompleteExtension(
|
|
|
1391
1896
|
setWhileStreaming(ctx, shared, parts[1]);
|
|
1392
1897
|
return;
|
|
1393
1898
|
}
|
|
1899
|
+
if (command === "stream") {
|
|
1900
|
+
setStreamResponses(ctx, shared, parts[1]);
|
|
1901
|
+
return;
|
|
1902
|
+
}
|
|
1394
1903
|
|
|
1395
1904
|
const handlers = createPromptAutocompleteCommandHandlers(ctx, shared);
|
|
1396
1905
|
const handler = handlers[command];
|
|
@@ -1401,7 +1910,7 @@ export function createPromptAutocompleteExtension(
|
|
|
1401
1910
|
}
|
|
1402
1911
|
|
|
1403
1912
|
ctx.ui.notify(
|
|
1404
|
-
"Usage: /prompt-autocomplete [on|off|toggle|status|while-streaming on|off|toggle|debug-on|debug-off|debug-toggle]",
|
|
1913
|
+
"Usage: /prompt-autocomplete [on|off|toggle|status|stats|stream on|off|toggle|while-streaming on|off|toggle|debug-on|debug-off|debug-toggle]",
|
|
1405
1914
|
"warning",
|
|
1406
1915
|
);
|
|
1407
1916
|
},
|