@kliebhan/pi-prompt-autocomplete 0.1.1 → 0.2.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.
Files changed (5) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +41 -6
  3. package/core.ts +619 -26
  4. package/index.ts +460 -61
  5. package/package.json +3 -3
package/index.ts CHANGED
@@ -1,4 +1,12 @@
1
- import { completeSimple, type Api, type Model, type UserMessage } from "@earendil-works/pi-ai/compat";
1
+ import {
2
+ completeSimple,
3
+ streamSimple,
4
+ type Api,
5
+ type AssistantMessage,
6
+ type AssistantMessageEventStream,
7
+ type Model,
8
+ type UserMessage,
9
+ } from "@earendil-works/pi-ai/compat";
2
10
  import { CustomEditor, type ExtensionAPI, type ExtensionContext, type KeybindingsManager } from "@earendil-works/pi-coding-agent";
3
11
  import { matchesKey, truncateToWidth, type EditorTheme, type KeyId, type TUI, visibleWidth } from "@earendil-works/pi-tui";
4
12
  import {
@@ -6,30 +14,43 @@ import {
6
14
  buildLatestAssistantMessageContext,
7
15
  buildLatestUserMessageContext,
8
16
  buildPromptAutocompleteCacheKey,
17
+ buildPromptAutocompletePrefixContextKey,
9
18
  buildRecentConversationContext,
10
19
  cancelAllCoalescedRequests,
11
20
  computeRequestMaxTokens,
12
21
  createOwnerRefCounter,
22
+ createPromptAutocompleteUsageStats,
23
+ describeSettingSource,
13
24
  DEFAULT_DEBOUNCE_MS,
14
25
  DEFAULT_MAX_ALTERNATIVES,
15
26
  DEFAULT_MAX_SUGGESTION_CHARS,
16
27
  DEFAULT_MIN_PROMPT_CHARS,
17
28
  DEFAULT_PREFERRED_MODEL,
18
29
  DEFAULT_PROMPT_AUTOCOMPLETE_ENABLED,
30
+ DEFAULT_STREAM_RESPONSES,
19
31
  ExpiringLruCache,
20
32
  extractNextSuggestionChunk,
21
33
  formatModelLabel,
34
+ formatPromptAutocompleteStats,
35
+ formatUsageStats,
22
36
  MAX_DRAFT_CONTEXT_CHARS,
23
37
  normalizePromptSuggestions,
24
38
  parseBoundedIntFlag,
25
39
  parseModelRef,
40
+ parsePartialPromptSuggestion,
26
41
  PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
42
+ recordProviderLatency,
43
+ recordProviderUsage,
44
+ resolveOverride,
45
+ reusePromptAutocompleteSuggestions,
27
46
  SequenceOwnedSlot,
28
47
  shouldSkipPromptAutocomplete,
29
48
  truncateDraftTail,
30
49
  type CoalescedRequestEntry,
31
50
  type CoalescedRequestSubscription,
32
51
  type ModelRef,
52
+ type PromptAutocompleteRuntimeOverrides,
53
+ type PromptAutocompleteUsageStats,
33
54
  } from "./core.ts";
34
55
 
35
56
  const GHOST_TEXT_STYLE = "\x1b[2m";
@@ -41,6 +62,12 @@ const SPINNER_INTERVAL_MS = 80;
41
62
  const SPINNER_LABEL = "Generating suggestion";
42
63
  // Inline autocomplete should fail fast instead of inheriting long provider retry/timeout defaults.
43
64
  const REQUEST_TIMEOUT_MS = 8_000;
65
+ // A compliant provider emits its terminal aborted message immediately, which
66
+ // lets accounting retain any usage. These bounds settle our caller even when a
67
+ // custom provider ignores both contracts; JavaScript cannot forcibly terminate
68
+ // that provider's already-pending iterator.next().
69
+ const STREAM_ABORT_DRAIN_TIMEOUT_MS = 250;
70
+ const STREAM_HARD_TIMEOUT_GRACE_MS = 500;
44
71
  const REQUEST_MAX_RETRIES = 0;
45
72
  const REQUEST_MAX_RETRY_DELAY_MS = 2_000;
46
73
  // Brief cooldown avoids hammering providers after transient auth/network failures while keeping the UI responsive.
@@ -86,6 +113,7 @@ const EDIT_ACTIONS = [
86
113
 
87
114
  interface PromptAutocompleteConfig {
88
115
  allowWhileStreaming: boolean;
116
+ streamResponses: boolean;
89
117
  debug: boolean;
90
118
  debounceMs: number;
91
119
  minPromptChars: number;
@@ -95,6 +123,8 @@ interface PromptAutocompleteConfig {
95
123
  }
96
124
 
97
125
  interface PromptAutocompleteCacheEntry {
126
+ draft: string;
127
+ prefixContextKey: string;
98
128
  suggestions: string[];
99
129
  rawResponse?: string;
100
130
  error?: string;
@@ -104,6 +134,8 @@ interface PromptAutocompleteCacheEntry {
104
134
  interface SuggestionRefreshOptions {
105
135
  clearExisting?: boolean;
106
136
  immediate?: boolean;
137
+ /** Stable terminal-cache identity to retain across a word/chunk acceptance. */
138
+ preferredOrigin?: string;
107
139
  // Manual one-shot trigger: bypasses the streaming gate and error cooldown so the
108
140
  // user can force a single suggestion while the main agent is still working.
109
141
  manual?: boolean;
@@ -117,21 +149,28 @@ interface EditorMountState {
117
149
  }
118
150
 
119
151
  type CompleteSimpleFunction = typeof completeSimple;
152
+ type StreamSimpleFunction = typeof streamSimple;
120
153
 
121
154
  export interface PromptAutocompleteDependencies {
122
155
  completeSimple?: CompleteSimpleFunction;
156
+ streamSimple?: StreamSimpleFunction;
123
157
  now?: () => number;
124
158
  }
125
159
 
126
160
  interface PromptAutocompleteSharedState {
127
161
  enabled: boolean;
128
162
  activationId: number;
129
- streaming: boolean;
163
+ agentStreaming: boolean;
130
164
  config: PromptAutocompleteConfig;
165
+ /** Slash-command decisions that outrank flags for the lifetime of the process. */
166
+ runtimeOverrides: PromptAutocompleteRuntimeOverrides;
167
+ /** Session-scoped request accounting; reset together with the cache. */
168
+ usageStats: PromptAutocompleteUsageStats;
131
169
  currentModel?: Model<Api>;
132
170
  modelRegistry?: ExtensionContext["modelRegistry"];
133
171
  sessionManager?: ExtensionContext["sessionManager"];
134
172
  completeSimple: CompleteSimpleFunction;
173
+ streamSimple?: StreamSimpleFunction;
135
174
  now: () => number;
136
175
  debugState: string;
137
176
  editorMount?: EditorMountState;
@@ -139,7 +178,8 @@ interface PromptAutocompleteSharedState {
139
178
  lastError?: string;
140
179
  lastRawResponse?: string;
141
180
  requestCache: ExpiringLruCache<PromptAutocompleteCacheEntry>;
142
- inFlightRequests: Map<string, CoalescedRequestEntry<PromptAutocompleteCacheEntry>>;
181
+ prefixReuseCache: ExpiringLruCache<PromptAutocompleteCacheEntry>;
182
+ inFlightRequests: Map<string, CoalescedRequestEntry<PromptAutocompleteCacheEntry, string>>;
143
183
  ownsEditor?: () => boolean;
144
184
  refreshEditor?: (options?: SuggestionRefreshOptions) => void;
145
185
  cancelActiveRequest?: () => void;
@@ -150,12 +190,16 @@ interface PromptAutocompleteSharedState {
150
190
 
151
191
  interface SuggestionRequest {
152
192
  activationId: number;
193
+ leafId: string;
153
194
  draft: string;
154
195
  draftTail: string;
155
196
  model: Model<Api>;
156
197
  modelLabel: string;
157
198
  cacheKey: string;
199
+ prefixContextKey: string;
158
200
  maxAlternatives: number;
201
+ maxSuggestionChars: number;
202
+ useStreaming: boolean;
159
203
  latestAssistantContext: string;
160
204
  latestUserContext: string;
161
205
  recentContext: string;
@@ -171,6 +215,77 @@ function arraysEqual(a: string[], b: string[]): boolean {
171
215
  return a.length === b.length && a.every((value, index) => value === b[index]);
172
216
  }
173
217
 
218
+ function extractAssistantText(message: AssistantMessage): string {
219
+ return message.content
220
+ .filter((block): block is { type: "text"; text: string } => block.type === "text")
221
+ .map((block) => block.text)
222
+ .join("");
223
+ }
224
+
225
+ async function consumeAssistantStream(
226
+ stream: AssistantMessageEventStream,
227
+ signal: AbortSignal,
228
+ abortProvider: () => void,
229
+ onPartial: (message: AssistantMessage) => void,
230
+ ): Promise<AssistantMessage> {
231
+ const iterator = stream[Symbol.asyncIterator]();
232
+ let hardTimer: ReturnType<typeof setTimeout> | undefined;
233
+ let abortTimer: ReturnType<typeof setTimeout> | undefined;
234
+ let rejectAbortDrain: ((error: Error) => void) | undefined;
235
+ let terminal: AssistantMessage | undefined;
236
+
237
+ const hardDeadline = new Promise<never>((_resolve, reject) => {
238
+ hardTimer = setTimeout(() => {
239
+ // `timeoutMs` is provider-owned. Abort the actual request signal as a
240
+ // second line of defence for providers that respect cancellation but
241
+ // accidentally ignore their timeout option.
242
+ abortProvider();
243
+ reject(new Error("Provider stream exceeded the local response deadline"));
244
+ }, REQUEST_TIMEOUT_MS + STREAM_HARD_TIMEOUT_GRACE_MS);
245
+ });
246
+ const abortDeadline = new Promise<never>((_resolve, reject) => {
247
+ rejectAbortDrain = reject;
248
+ });
249
+ const beginAbortDrain = () => {
250
+ if (abortTimer) return;
251
+ abortTimer = setTimeout(
252
+ () => rejectAbortDrain?.(new Error("Provider stream did not terminate after cancellation")),
253
+ STREAM_ABORT_DRAIN_TIMEOUT_MS,
254
+ );
255
+ };
256
+ signal.addEventListener("abort", beginAbortDrain, { once: true });
257
+ if (signal.aborted) beginAbortDrain();
258
+
259
+ try {
260
+ while (!terminal) {
261
+ const next = await Promise.race([iterator.next(), hardDeadline, abortDeadline]);
262
+ if (next.done) {
263
+ throw new Error("Provider stream ended without a terminal event");
264
+ }
265
+
266
+ const event = next.value;
267
+ if (event.type === "done") {
268
+ terminal = event.message;
269
+ } else if (event.type === "error") {
270
+ terminal = event.error;
271
+ } else if (event.type === "text_delta" || event.type === "text_end") {
272
+ onPartial(event.partial);
273
+ }
274
+ }
275
+ return terminal;
276
+ } finally {
277
+ signal.removeEventListener("abort", beginAbortDrain);
278
+ if (hardTimer) clearTimeout(hardTimer);
279
+ if (abortTimer) clearTimeout(abortTimer);
280
+ if (!terminal) {
281
+ // Do not await a non-compliant iterator's return path. The provider already
282
+ // has an aborted signal; this merely gives a cooperative iterator a chance
283
+ // to release local resources while allowing our request promise to settle.
284
+ void Promise.resolve(iterator.return?.()).catch(() => undefined);
285
+ }
286
+ }
287
+ }
288
+
174
289
  function matchesAnyKey(data: string, keys: readonly KeyId[]): boolean {
175
290
  return keys.some((key) => matchesKey(data, key));
176
291
  }
@@ -197,9 +312,19 @@ function resolveSuggestionModel(shared: PromptAutocompleteSharedState): Model<Ap
197
312
  return undefined;
198
313
  }
199
314
 
315
+ function parseOnOffFlag(value: boolean | string | undefined, fallback: boolean): boolean {
316
+ if (typeof value === "boolean") return value;
317
+ if (typeof value !== "string") return fallback;
318
+ const normalized = value.trim().toLowerCase();
319
+ if (["on", "true", "1", "yes"].includes(normalized)) return true;
320
+ if (["off", "false", "0", "no"].includes(normalized)) return false;
321
+ return fallback;
322
+ }
323
+
200
324
  function parseConfig(pi: ExtensionAPI): PromptAutocompleteConfig {
201
325
  return {
202
326
  allowWhileStreaming: pi.getFlag("prompt-autocomplete-while-streaming") === true,
327
+ streamResponses: parseOnOffFlag(pi.getFlag("prompt-autocomplete-stream"), DEFAULT_STREAM_RESPONSES),
203
328
  debug: pi.getFlag("prompt-autocomplete-debug") === true,
204
329
  debounceMs: parseBoundedIntFlag(pi.getFlag("prompt-autocomplete-debounce-ms"), DEFAULT_DEBOUNCE_MS, 0, 5_000),
205
330
  minPromptChars: parseBoundedIntFlag(pi.getFlag("prompt-autocomplete-min-chars"), DEFAULT_MIN_PROMPT_CHARS, 0, 500),
@@ -219,6 +344,29 @@ function parseConfig(pi: ExtensionAPI): PromptAutocompleteConfig {
219
344
  };
220
345
  }
221
346
 
347
+ /**
348
+ * Recompute enablement and config from flags, then re-apply runtime overrides.
349
+ *
350
+ * Called on every session start. Without the override layer a new session would
351
+ * silently discard `/prompt-autocomplete on`, `while-streaming`, and `debug-*`
352
+ * decisions and revert to the flags the process happened to start with.
353
+ */
354
+ function applyEffectiveConfig(pi: ExtensionAPI, shared: PromptAutocompleteSharedState): void {
355
+ const flagEnabled = pi.getFlag("prompt-autocomplete") === true;
356
+ const flagConfig = parseConfig(pi);
357
+
358
+ shared.enabled = resolveOverride(shared.runtimeOverrides.enabled, flagEnabled);
359
+ shared.config = {
360
+ ...flagConfig,
361
+ allowWhileStreaming: resolveOverride(
362
+ shared.runtimeOverrides.allowWhileStreaming,
363
+ flagConfig.allowWhileStreaming,
364
+ ),
365
+ streamResponses: resolveOverride(shared.runtimeOverrides.streamResponses, flagConfig.streamResponses),
366
+ debug: resolveOverride(shared.runtimeOverrides.debug, flagConfig.debug),
367
+ };
368
+ }
369
+
222
370
  function truncateDebug(text: string, maxLength = 140): string {
223
371
  const normalized = text.replace(/\s+/g, " ").trim();
224
372
  if (normalized.length <= maxLength) return normalized;
@@ -249,18 +397,21 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
249
397
  : "current active model";
250
398
 
251
399
  return [
252
- `enabled=${shared.enabled ? "yes" : "no"}`,
400
+ `enabled=${shared.enabled ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.enabled)})`,
253
401
  `editor=${editorState}`,
254
402
  shared.editorBlockedReason ? `editor-blocked=${truncateDebug(shared.editorBlockedReason, 90)}` : undefined,
255
403
  `model=${formatModelLabel(resolvedModel)}`,
256
404
  `requested-model=${requestedModel}`,
257
- `while-streaming=${shared.config.allowWhileStreaming ? "yes" : "no"}`,
258
- `debug=${shared.config.debug ? "yes" : "no"}`,
405
+ `while-streaming=${shared.config.allowWhileStreaming ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.allowWhileStreaming)})`,
406
+ `stream=${shared.config.streamResponses ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.streamResponses)})`,
407
+ `request-path=${shared.config.streamResponses && shared.streamSimple ? "stream" : shared.config.streamResponses ? "complete-compat" : "complete"}`,
408
+ `debug=${shared.config.debug ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.debug)})`,
259
409
  `debounce=${shared.config.debounceMs}ms`,
260
410
  `min-chars=${shared.config.minPromptChars}`,
261
411
  `max-suggestion-chars=${shared.config.maxSuggestionChars}`,
262
412
  `max-alternatives=${shared.config.maxAlternatives}`,
263
413
  `cache-size=${shared.requestCache.size}`,
414
+ `usage=${formatUsageStats(shared.usageStats)}`,
264
415
  `state=${shared.debugState || "idle"}`,
265
416
  shared.lastError ? `error=${truncateDebug(shared.lastError, 90)}` : undefined,
266
417
  shared.lastRawResponse ? `raw=${truncateDebug(shared.lastRawResponse, 90)}` : undefined,
@@ -270,13 +421,55 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
270
421
  .join(" | ");
271
422
  }
272
423
 
424
+ function cloneCacheEntry(entry: PromptAutocompleteCacheEntry): PromptAutocompleteCacheEntry {
425
+ return { ...entry, suggestions: [...entry.suggestions] };
426
+ }
427
+
273
428
  function getCachedRequest(
274
429
  shared: PromptAutocompleteSharedState,
275
430
  cacheKey: string,
276
431
  options: { bypass?: boolean } = {},
277
432
  ): PromptAutocompleteCacheEntry | undefined {
278
433
  const entry = shared.requestCache.get(cacheKey, options);
279
- return entry ? { ...entry, suggestions: [...entry.suggestions] } : undefined;
434
+ return entry ? cloneCacheEntry(entry) : undefined;
435
+ }
436
+
437
+ function rearmPrefixReuseFromExactHit(
438
+ shared: PromptAutocompleteSharedState,
439
+ cacheKey: string,
440
+ entry: PromptAutocompleteCacheEntry,
441
+ ): void {
442
+ const expiresAt = shared.requestCache.getExpiration(cacheKey);
443
+ if (expiresAt === undefined) return;
444
+ shared.prefixReuseCache.set(entry.prefixContextKey, cloneCacheEntry(entry), { expiresAt });
445
+ }
446
+
447
+ interface PrefixReusedCacheEntry {
448
+ entry: PromptAutocompleteCacheEntry;
449
+ origins: string[];
450
+ }
451
+
452
+ function getPrefixReusedRequest(
453
+ shared: PromptAutocompleteSharedState,
454
+ request: SuggestionRequest,
455
+ options: { bypass?: boolean } = {},
456
+ ): PrefixReusedCacheEntry | undefined {
457
+ const cached = shared.prefixReuseCache.get(request.prefixContextKey, options);
458
+ if (!cached) return undefined;
459
+
460
+ const reused = reusePromptAutocompleteSuggestions(
461
+ cached.draft,
462
+ request.draft,
463
+ cached.suggestions,
464
+ request.maxSuggestionChars,
465
+ request.maxAlternatives,
466
+ );
467
+ if (!reused) return undefined;
468
+
469
+ return {
470
+ entry: { ...cloneCacheEntry(cached), draft: request.draft, suggestions: reused.suggestions },
471
+ origins: reused.origins,
472
+ };
280
473
  }
281
474
 
282
475
  function storeCachedRequest(
@@ -284,9 +477,10 @@ function storeCachedRequest(
284
477
  cacheKey: string,
285
478
  entry: PromptAutocompleteCacheEntry,
286
479
  ): PromptAutocompleteCacheEntry {
287
- const cachedEntry = { ...entry, suggestions: [...entry.suggestions] };
480
+ const cachedEntry = cloneCacheEntry(entry);
288
481
  shared.requestCache.set(cacheKey, cachedEntry);
289
- return { ...cachedEntry, suggestions: [...cachedEntry.suggestions] };
482
+ shared.prefixReuseCache.set(entry.prefixContextKey, cachedEntry);
483
+ return cloneCacheEntry(cachedEntry);
290
484
  }
291
485
 
292
486
  class PromptAutocompleteEditor extends CustomEditor {
@@ -295,7 +489,10 @@ class PromptAutocompleteEditor extends CustomEditor {
295
489
  private readonly appKeybindings: KeybindingsManager;
296
490
 
297
491
  private suggestions: string[] = [];
492
+ /** Stable terminal-cache identities parallel to the displayed suggestions. */
493
+ private suggestionOrigins: string[] = [];
298
494
  private suggestionIndex: number = 0;
495
+ private suggestionsProvisional = false;
299
496
  private debounceTimer?: ReturnType<typeof setTimeout>;
300
497
  private requestSeq = 0;
301
498
  private readonly pendingRequests = new SequenceOwnedSlot<PendingSuggestionRequest>();
@@ -463,12 +660,23 @@ class PromptAutocompleteEditor extends CustomEditor {
463
660
  return this.suggestions[this.suggestionIndex];
464
661
  }
465
662
 
466
- private setSuggestions(nextSuggestions: string[]): void {
467
- const currentActive = this.getActiveSuggestion();
663
+ private getActiveSuggestionOrigin(): string | undefined {
664
+ return this.suggestionOrigins[this.suggestionIndex];
665
+ }
666
+
667
+ private setSuggestions(
668
+ nextSuggestions: string[],
669
+ options: { provisional?: boolean; origins?: string[]; preferredOrigin?: string } = {},
670
+ ): void {
671
+ const provisional = options.provisional ?? false;
672
+ const nextOrigins = options.origins?.length === nextSuggestions.length
673
+ ? [...options.origins]
674
+ : [...nextSuggestions];
675
+ const currentOrigin = options.preferredOrigin ?? this.getActiveSuggestionOrigin();
468
676
  let nextIndex = 0;
469
677
 
470
- if (currentActive) {
471
- const preservedIndex = nextSuggestions.indexOf(currentActive);
678
+ if (currentOrigin) {
679
+ const preservedIndex = nextOrigins.indexOf(currentOrigin);
472
680
  if (preservedIndex !== -1) {
473
681
  nextIndex = preservedIndex;
474
682
  }
@@ -480,12 +688,26 @@ class PromptAutocompleteEditor extends CustomEditor {
480
688
  nextIndex = 0;
481
689
  }
482
690
 
483
- if (arraysEqual(this.suggestions, nextSuggestions) && this.suggestionIndex === nextIndex) {
691
+ if (
692
+ arraysEqual(this.suggestions, nextSuggestions)
693
+ && arraysEqual(this.suggestionOrigins, nextOrigins)
694
+ && this.suggestionIndex === nextIndex
695
+ && this.suggestionsProvisional === provisional
696
+ ) {
484
697
  return;
485
698
  }
486
699
 
700
+ // Count active ghost-text offers, not hidden alternatives or every streamed
701
+ // revision of the same candidate. Cycling records a new offer when another
702
+ // alternative becomes active.
703
+ if (nextSuggestions.length > 0 && this.suggestions.length === 0) {
704
+ this.shared.usageStats.suggestionsOffered += 1;
705
+ }
706
+
487
707
  this.suggestions = [...nextSuggestions];
708
+ this.suggestionOrigins = nextOrigins;
488
709
  this.suggestionIndex = nextIndex;
710
+ this.suggestionsProvisional = nextSuggestions.length > 0 && provisional;
489
711
 
490
712
  if (nextSuggestions.length > 0) {
491
713
  updateDebugState(
@@ -503,6 +725,7 @@ class PromptAutocompleteEditor extends CustomEditor {
503
725
  const nextIndex = (this.suggestionIndex + delta + this.suggestions.length) % this.suggestions.length;
504
726
  if (nextIndex === this.suggestionIndex) return;
505
727
  this.suggestionIndex = nextIndex;
728
+ this.shared.usageStats.suggestionsOffered += 1;
506
729
  this.dismissedKey = undefined;
507
730
  updateDebugState(this.shared, "cycled", `Now showing ${nextIndex + 1}/${this.suggestions.length}`);
508
731
  this.tui.requestRender();
@@ -579,23 +802,30 @@ class PromptAutocompleteEditor extends CustomEditor {
579
802
  const suggestion = this.getActiveSuggestion();
580
803
  if (!suggestion) return;
581
804
 
805
+ const wasProvisional = this.suggestionsProvisional;
806
+ this.shared.usageStats.fullAccepts += 1;
582
807
  this.dismissedKey = undefined;
583
- this.clearInlineSuggestion("accepted", "Accepted full suggestion");
808
+ this.clearInlineSuggestion("accepted", wasProvisional ? "Accepted streamed partial suggestion" : "Accepted full suggestion");
584
809
  super.insertTextAtCursor(suggestion);
585
- this.refreshSuggestion();
810
+ // A provisional accept is explicit cancellation of the request. Starting a
811
+ // fresh paid request immediately would turn one Tab press into two calls.
812
+ if (!wasProvisional) this.refreshSuggestion();
586
813
  }
587
814
 
588
815
  private acceptInlineSuggestionByWord(): void {
589
816
  const suggestion = this.getActiveSuggestion();
590
817
  if (!suggestion) return;
591
818
 
819
+ const origin = this.getActiveSuggestionOrigin();
592
820
  const chunk = extractNextSuggestionChunk(suggestion) ?? suggestion;
593
821
  if (!chunk) return;
594
822
 
823
+ const wasProvisional = this.suggestionsProvisional;
824
+ this.shared.usageStats.chunkAccepts += 1;
595
825
  this.dismissedKey = undefined;
596
826
  this.clearInlineSuggestion("accepted-word", `Accepted chunk: ${chunk}`);
597
827
  super.insertTextAtCursor(chunk);
598
- this.refreshSuggestion();
828
+ if (!wasProvisional) this.refreshSuggestion({ preferredOrigin: origin });
599
829
  }
600
830
 
601
831
  private shouldRenderInlineSuggestion(): boolean {
@@ -622,7 +852,7 @@ class PromptAutocompleteEditor extends CustomEditor {
622
852
  // (streaming gate, post-error cooldown, min-chars). Model/auth and slash/path
623
853
  // checks still apply because those can never produce a useful suggestion.
624
854
  if (!options.manual) {
625
- if (!this.shared.config.allowWhileStreaming && this.shared.streaming) {
855
+ if (!this.shared.config.allowWhileStreaming && this.shared.agentStreaming) {
626
856
  return "Waiting for the current agent turn to finish";
627
857
  }
628
858
  if (this.shared.now() < this.suspendedUntil) return "In temporary cooldown after the last error";
@@ -643,6 +873,7 @@ class PromptAutocompleteEditor extends CustomEditor {
643
873
  }
644
874
 
645
875
  private refreshSuggestion(options: SuggestionRefreshOptions = {}): void {
876
+ const preferredOrigin = options.preferredOrigin ?? this.getActiveSuggestionOrigin();
646
877
  if (options.clearExisting) {
647
878
  this.cancelPendingRequest();
648
879
  this.setSuggestions([]);
@@ -695,13 +926,37 @@ class PromptAutocompleteEditor extends CustomEditor {
695
926
 
696
927
  const cachedEntry = getCachedRequest(this.shared, request.cacheKey, { bypass: options.manual });
697
928
  if (cachedEntry) {
929
+ this.shared.usageStats.cacheHits += 1;
930
+ rearmPrefixReuseFromExactHit(this.shared, request.cacheKey, cachedEntry);
698
931
  this.cancelPendingRequest();
699
932
  this.shared.lastRawResponse = cachedEntry.rawResponse;
700
933
  this.shared.lastError = cachedEntry.error;
701
934
  if (cachedEntry.suggestions.length === 0) {
702
935
  updateDebugState(this.shared, "cache-hit", cachedEntry.debugState || "Cached no-suggestion result");
703
936
  }
704
- this.setSuggestions(cachedEntry.suggestions);
937
+ this.setSuggestions(cachedEntry.suggestions, {
938
+ origins: cachedEntry.suggestions,
939
+ preferredOrigin,
940
+ });
941
+ return;
942
+ }
943
+
944
+ const prefixReused = getPrefixReusedRequest(this.shared, request, { bypass: options.manual });
945
+ if (prefixReused) {
946
+ this.shared.usageStats.cacheHits += 1;
947
+ this.shared.usageStats.prefixReuseHits += 1;
948
+ this.cancelPendingRequest();
949
+ this.shared.lastRawResponse = prefixReused.entry.rawResponse;
950
+ this.shared.lastError = prefixReused.entry.error;
951
+ this.setSuggestions(prefixReused.entry.suggestions, {
952
+ origins: prefixReused.origins,
953
+ preferredOrigin,
954
+ });
955
+ updateDebugState(
956
+ this.shared,
957
+ "prefix-cache-hit",
958
+ `${prefixReused.entry.suggestions.length} suggestion(s) reused without a provider request`,
959
+ );
705
960
  return;
706
961
  }
707
962
 
@@ -751,23 +1006,28 @@ class PromptAutocompleteEditor extends CustomEditor {
751
1006
  const modelLabel = formatModelLabel(model);
752
1007
  const leafId = this.shared.sessionManager?.getLeafId?.() ?? "";
753
1008
 
1009
+ const prefixContextIdentity = {
1010
+ leafId,
1011
+ modelLabel,
1012
+ maxAlternatives: this.shared.config.maxAlternatives,
1013
+ maxSuggestionChars: this.shared.config.maxSuggestionChars,
1014
+ latestAssistantContext,
1015
+ latestUserContext,
1016
+ recentContext,
1017
+ };
1018
+
754
1019
  return {
755
1020
  activationId: this.activationId,
1021
+ leafId,
756
1022
  draft,
757
1023
  draftTail,
758
1024
  model,
759
1025
  modelLabel,
760
- cacheKey: buildPromptAutocompleteCacheKey({
761
- leafId,
762
- modelLabel,
763
- maxAlternatives: this.shared.config.maxAlternatives,
764
- maxSuggestionChars: this.shared.config.maxSuggestionChars,
765
- draft,
766
- latestAssistantContext,
767
- latestUserContext,
768
- recentContext,
769
- }),
1026
+ cacheKey: buildPromptAutocompleteCacheKey({ ...prefixContextIdentity, draft }),
1027
+ prefixContextKey: buildPromptAutocompletePrefixContextKey(prefixContextIdentity),
770
1028
  maxAlternatives: this.shared.config.maxAlternatives,
1029
+ maxSuggestionChars: this.shared.config.maxSuggestionChars,
1030
+ useStreaming: this.shared.config.streamResponses && !!this.shared.streamSimple,
771
1031
  latestAssistantContext,
772
1032
  latestUserContext,
773
1033
  recentContext,
@@ -786,7 +1046,8 @@ class PromptAutocompleteEditor extends CustomEditor {
786
1046
  this.shared.inFlightRequests,
787
1047
  request.cacheKey,
788
1048
  subscriberId,
789
- (signal) => this.fetchSuggestionUncached(request, signal),
1049
+ (signal, publish) => this.fetchSuggestionUncached(request, signal, publish),
1050
+ (suggestion) => this.showStreamedSuggestion(request, seq, spinnerOwner, suggestion),
790
1051
  );
791
1052
 
792
1053
  this.activeRequestSubscription = subscription;
@@ -831,9 +1092,28 @@ class PromptAutocompleteEditor extends CustomEditor {
831
1092
  }
832
1093
  }
833
1094
 
1095
+ private showStreamedSuggestion(
1096
+ request: SuggestionRequest,
1097
+ seq: number,
1098
+ spinnerOwner: string,
1099
+ suggestion: string,
1100
+ ): void {
1101
+ if (!this.isRequestStillCurrent(request, seq)) return;
1102
+ if (request.cacheKey === this.dismissedKey) return;
1103
+
1104
+ const current = this.getActiveSuggestion();
1105
+ // Progress is strictly monotonic. If a provider revises earlier text, keep
1106
+ // the last honest preview and let the terminal response replace it.
1107
+ if (current && (!suggestion.startsWith(current) || suggestion.length <= current.length)) return;
1108
+
1109
+ this.setSuggestions([suggestion], { provisional: true });
1110
+ this.deactivateSpinner(spinnerOwner);
1111
+ }
1112
+
834
1113
  private async fetchSuggestionUncached(
835
1114
  request: SuggestionRequest,
836
1115
  signal: AbortSignal,
1116
+ publish: (suggestion: string) => void,
837
1117
  ): Promise<PromptAutocompleteCacheEntry> {
838
1118
  if (!this.shared.modelRegistry) {
839
1119
  throw new Error("No model registry available");
@@ -892,43 +1172,81 @@ class PromptAutocompleteEditor extends CustomEditor {
892
1172
  timestamp: Date.now(),
893
1173
  };
894
1174
 
895
- const response = await this.shared.completeSimple(
896
- request.model,
897
- {
898
- systemPrompt: PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
899
- messages: [userMessage],
900
- },
901
- {
902
- apiKey: auth.apiKey,
903
- headers: auth.headers,
904
- signal,
905
- maxTokens: computeRequestMaxTokens(request.maxAlternatives, this.shared.config.maxSuggestionChars),
906
- timeoutMs: REQUEST_TIMEOUT_MS,
907
- maxRetries: REQUEST_MAX_RETRIES,
908
- maxRetryDelayMs: REQUEST_MAX_RETRY_DELAY_MS,
909
- },
910
- );
1175
+ const stats = this.shared.usageStats;
1176
+ stats.providerRequests += 1;
1177
+
1178
+ const context = {
1179
+ systemPrompt: PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
1180
+ messages: [userMessage],
1181
+ };
1182
+ const requestOptions = {
1183
+ apiKey: auth.apiKey,
1184
+ headers: auth.headers,
1185
+ signal,
1186
+ maxTokens: computeRequestMaxTokens(request.maxAlternatives, request.maxSuggestionChars),
1187
+ timeoutMs: REQUEST_TIMEOUT_MS,
1188
+ maxRetries: REQUEST_MAX_RETRIES,
1189
+ maxRetryDelayMs: REQUEST_MAX_RETRY_DELAY_MS,
1190
+ };
1191
+
1192
+ const providerStartedAt = this.shared.now();
1193
+ let response: AssistantMessage;
1194
+ try {
1195
+ if (request.useStreaming && this.shared.streamSimple) {
1196
+ const hardDeadlineController = new AbortController();
1197
+ const streamSignal = AbortSignal.any([signal, hardDeadlineController.signal]);
1198
+ const stream = this.shared.streamSimple(request.model, context, {
1199
+ ...requestOptions,
1200
+ signal: streamSignal,
1201
+ });
1202
+ let lastPublished = "";
1203
+
1204
+ // Exactly one consumer receives the terminal message. Partial text is
1205
+ // fanned out through coalescing; after UI cancellation the callback is
1206
+ // gone, but the bounded drain can still retain terminal usage.
1207
+ response = await consumeAssistantStream(stream, streamSignal, () => hardDeadlineController.abort(), (partial) => {
1208
+ // `partial` is cumulative. Appending delta plus text_end.content would
1209
+ // duplicate the completed block on providers that emit both.
1210
+ const partialText = extractAssistantText(partial);
1211
+ const suggestion = parsePartialPromptSuggestion(request.draft, partialText, request.maxSuggestionChars);
1212
+ if (!suggestion || suggestion === lastPublished) return;
1213
+ if (lastPublished && !suggestion.startsWith(lastPublished)) return;
1214
+ lastPublished = suggestion;
1215
+ publish(suggestion);
1216
+ });
1217
+ } else {
1218
+ response = await this.shared.completeSimple(request.model, context, requestOptions);
1219
+ }
1220
+ } catch (error) {
1221
+ stats.failedRequests += 1;
1222
+ throw error;
1223
+ } finally {
1224
+ recordProviderLatency(stats, this.shared.now() - providerStartedAt);
1225
+ }
1226
+
1227
+ // Tokens spent on a failed or aborted response are still spent, so usage is
1228
+ // recorded before the stop-reason check rejects the request.
1229
+ recordProviderUsage(stats, response.usage);
911
1230
 
912
1231
  const contentTypes = response.content.map((block) => block.type).join(", ") || "(none)";
913
- const text = response.content
914
- .filter((block): block is { type: "text"; text: string } => block.type === "text")
915
- .map((block) => block.text)
916
- .join("");
1232
+ const text = extractAssistantText(response);
917
1233
 
918
1234
  const responseError = response.errorMessage?.trim();
919
1235
  const rawResponse = text || responseError || `[types=${contentTypes}; stopReason=${response.stopReason}]`;
920
1236
 
921
1237
  if (response.stopReason === "error" || response.stopReason === "aborted") {
1238
+ stats.failedRequests += 1;
922
1239
  throw new Error(responseError || `Provider returned stopReason=${response.stopReason}`);
923
1240
  }
924
1241
  if (signal.aborted) {
1242
+ stats.failedRequests += 1;
925
1243
  throw new Error("Request was aborted");
926
1244
  }
927
1245
 
928
1246
  const normalized = normalizePromptSuggestions(
929
1247
  request.draft,
930
1248
  text,
931
- this.shared.config.maxSuggestionChars,
1249
+ request.maxSuggestionChars,
932
1250
  request.maxAlternatives,
933
1251
  );
934
1252
 
@@ -938,6 +1256,8 @@ class PromptAutocompleteEditor extends CustomEditor {
938
1256
  : `${normalized.length} suggestion(s) ready`;
939
1257
 
940
1258
  return storeCachedRequest(this.shared, request.cacheKey, {
1259
+ draft: request.draft,
1260
+ prefixContextKey: request.prefixContextKey,
941
1261
  suggestions: normalized,
942
1262
  rawResponse,
943
1263
  debugState,
@@ -955,7 +1275,12 @@ class PromptAutocompleteEditor extends CustomEditor {
955
1275
  if (this.isShowingAutocomplete()) return false;
956
1276
 
957
1277
  const currentModel = resolveSuggestionModel(this.shared);
958
- return formatModelLabel(currentModel) === request.modelLabel;
1278
+ if (formatModelLabel(currentModel) !== request.modelLabel) return false;
1279
+
1280
+ // Session entries are immutable and the leaf identifies the exact branch
1281
+ // from which all bounded context sections were derived. Checking it avoids
1282
+ // rebuilding/scanning and hashing that context for every streamed token.
1283
+ return (this.shared.sessionManager?.getLeafId?.() ?? "") === request.leafId;
959
1284
  }
960
1285
 
961
1286
  cancelActiveRequest(): void {
@@ -1115,14 +1440,15 @@ function unmountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedSt
1115
1440
  function resetSharedForSession(pi: ExtensionAPI, shared: PromptAutocompleteSharedState): void {
1116
1441
  shared.cancelActiveRequest?.();
1117
1442
  shared.cancelActiveRequest = undefined;
1118
- shared.enabled = pi.getFlag("prompt-autocomplete") === true;
1119
- shared.config = parseConfig(pi);
1443
+ applyEffectiveConfig(pi, shared);
1444
+ shared.usageStats = createPromptAutocompleteUsageStats();
1120
1445
  shared.editorMount = undefined;
1121
1446
  shared.editorBlockedReason = undefined;
1122
1447
  shared.ownsEditor = undefined;
1123
1448
  shared.lastError = undefined;
1124
1449
  shared.lastRawResponse = undefined;
1125
1450
  shared.requestCache.clear();
1451
+ shared.prefixReuseCache.clear();
1126
1452
  cancelAllCoalescedRequests(shared.inFlightRequests);
1127
1453
  shared.debugState = shared.enabled ? "configured" : "disabled";
1128
1454
  }
@@ -1131,7 +1457,7 @@ function bindRuntimeContext(ctx: ExtensionContext, shared: PromptAutocompleteSha
1131
1457
  shared.currentModel = ctx.model as Model<Api> | undefined;
1132
1458
  shared.modelRegistry = ctx.modelRegistry;
1133
1459
  shared.sessionManager = ctx.sessionManager;
1134
- shared.streaming = false;
1460
+ shared.agentStreaming = false;
1135
1461
  }
1136
1462
 
1137
1463
  function refreshEditorImmediately(shared: PromptAutocompleteSharedState, state: string, details: string): void {
@@ -1141,6 +1467,7 @@ function refreshEditorImmediately(shared: PromptAutocompleteSharedState, state:
1141
1467
 
1142
1468
  function setDebugDisplay(shared: PromptAutocompleteSharedState, enabled: boolean): void {
1143
1469
  shared.config.debug = enabled;
1470
+ shared.runtimeOverrides.debug = enabled;
1144
1471
  if (enabled) {
1145
1472
  updateDebugState(shared, shared.debugState || "ready");
1146
1473
  } else {
@@ -1166,25 +1493,64 @@ function setWhileStreaming(
1166
1493
  }
1167
1494
 
1168
1495
  shared.config.allowWhileStreaming = next;
1496
+ shared.runtimeOverrides.allowWhileStreaming = next;
1169
1497
  ctx.ui.notify(`Prompt autocomplete while-streaming ${next ? "enabled" : "disabled"}`, "info");
1170
1498
 
1171
1499
  if (shared.enabled) {
1172
1500
  // Only label it "waiting" when disabling actually suppresses right now (agent
1173
1501
  // streaming); otherwise the refresh below requests suggestions immediately.
1174
- const willSuppress = !next && shared.streaming;
1502
+ const willSuppress = !next && shared.agentStreaming;
1175
1503
  refreshEditorImmediately(shared, willSuppress ? "waiting" : "ready", `While-streaming ${next ? "enabled" : "disabled"}`);
1176
1504
  }
1177
1505
  }
1178
1506
 
1507
+ function setStreamResponses(
1508
+ ctx: ExtensionContext,
1509
+ shared: PromptAutocompleteSharedState,
1510
+ arg: string | undefined,
1511
+ ): void {
1512
+ let next: boolean;
1513
+ if (arg === "on") {
1514
+ next = true;
1515
+ } else if (arg === "off") {
1516
+ next = false;
1517
+ } else if (arg === undefined || arg === "toggle") {
1518
+ next = !shared.config.streamResponses;
1519
+ } else {
1520
+ ctx.ui.notify("Usage: /prompt-autocomplete stream [on|off|toggle]", "warning");
1521
+ return;
1522
+ }
1523
+
1524
+ const changed = shared.config.streamResponses !== next;
1525
+ shared.config.streamResponses = next;
1526
+ shared.runtimeOverrides.streamResponses = next;
1527
+ const path = next && shared.streamSimple ? "streaming" : next ? "completion compatibility" : "completion";
1528
+ ctx.ui.notify(
1529
+ `Prompt autocomplete response streaming ${next ? "enabled" : "disabled"} (${path} path)${changed ? "; applies to next request" : ""}`,
1530
+ "info",
1531
+ );
1532
+
1533
+ if (shared.enabled && changed) {
1534
+ // Presentation changes must not silently buy a replacement request. Cancel
1535
+ // active work and clear provisional text; the next edit or manual trigger
1536
+ // uses the selected path.
1537
+ shared.cancelActiveRequest?.();
1538
+ updateDebugState(shared, "ready", `Response streaming ${next ? "enabled" : "disabled"}; waiting for next request`);
1539
+ }
1540
+ }
1541
+
1179
1542
  function enablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
1180
1543
  shared.enabled = true;
1544
+ shared.runtimeOverrides.enabled = true;
1181
1545
  return mountEditor(ctx, shared);
1182
1546
  }
1183
1547
 
1184
1548
  function disablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): void {
1185
1549
  shared.enabled = false;
1550
+ shared.runtimeOverrides.enabled = false;
1186
1551
  unmountEditor(ctx, shared);
1187
1552
  shared.requestCache.clear();
1553
+ shared.prefixReuseCache.clear();
1188
1554
  shared.lastRawResponse = undefined;
1189
1555
  shared.lastError = undefined;
1190
1556
  }
@@ -1227,6 +1593,9 @@ function createPromptAutocompleteCommandHandlers(
1227
1593
  status: () => {
1228
1594
  ctx.ui.notify(formatStatus(shared), "info");
1229
1595
  },
1596
+ stats: () => {
1597
+ ctx.ui.notify(formatPromptAutocompleteStats(shared.usageStats), "info");
1598
+ },
1230
1599
  "debug-on": () => {
1231
1600
  setDebugDisplay(shared, true);
1232
1601
  ctx.ui.notify("Prompt autocomplete debug display enabled", "info");
@@ -1247,6 +1616,9 @@ function createPromptAutocompleteCommandHandlers(
1247
1616
  },
1248
1617
  on: () => {
1249
1618
  if (shared.enabled && shared.ownsEditor?.()) {
1619
+ // Already in the requested state, but the user still expressed intent:
1620
+ // record it so a later session does not fall back to the flag.
1621
+ shared.runtimeOverrides.enabled = true;
1250
1622
  ctx.ui.notify(`Prompt autocomplete already enabled (${formatStatus(shared)})`, "info");
1251
1623
  return;
1252
1624
  }
@@ -1257,6 +1629,7 @@ function createPromptAutocompleteCommandHandlers(
1257
1629
  },
1258
1630
  off: () => {
1259
1631
  if (!shared.enabled) {
1632
+ shared.runtimeOverrides.enabled = false;
1260
1633
  ctx.ui.notify("Prompt autocomplete is already disabled", "info");
1261
1634
  return;
1262
1635
  }
@@ -1282,6 +1655,9 @@ export function createPromptAutocompleteExtension(
1282
1655
  dependencies: PromptAutocompleteDependencies = {},
1283
1656
  ): (pi: ExtensionAPI) => void {
1284
1657
  const completeSimpleImpl = dependencies.completeSimple ?? completeSimple;
1658
+ // Existing deterministic harnesses inject only completeSimple. Never let such
1659
+ // a harness fall through to the real network-capable stream implementation.
1660
+ const streamSimpleImpl = dependencies.streamSimple ?? (dependencies.completeSimple ? undefined : streamSimple);
1285
1661
  const now = dependencies.now ?? Date.now;
1286
1662
 
1287
1663
  return function promptAutocompleteExtension(pi: ExtensionAPI): void {
@@ -1300,6 +1676,13 @@ export function createPromptAutocompleteExtension(
1300
1676
  type: "boolean",
1301
1677
  default: false,
1302
1678
  });
1679
+ pi.registerFlag("prompt-autocomplete-stream", {
1680
+ description: "Render streamed response progress as ghost text (on|off)",
1681
+ // Pi's boolean extension flags can only be switched on. A string flag is
1682
+ // required so a feature that defaults on can still be disabled at startup.
1683
+ type: "string",
1684
+ default: DEFAULT_STREAM_RESPONSES ? "on" : "off",
1685
+ });
1303
1686
  pi.registerFlag("prompt-autocomplete-debug", {
1304
1687
  description: "Show prompt autocomplete debug state below the editor",
1305
1688
  type: "boolean",
@@ -1329,12 +1712,16 @@ export function createPromptAutocompleteExtension(
1329
1712
  const shared: PromptAutocompleteSharedState = {
1330
1713
  enabled: false,
1331
1714
  activationId: 0,
1332
- streaming: false,
1715
+ agentStreaming: false,
1333
1716
  config: parseConfig(pi),
1717
+ runtimeOverrides: {},
1718
+ usageStats: createPromptAutocompleteUsageStats(),
1334
1719
  completeSimple: completeSimpleImpl,
1720
+ streamSimple: streamSimpleImpl,
1335
1721
  now,
1336
1722
  debugState: "idle",
1337
1723
  requestCache: new ExpiringLruCache(REQUEST_CACHE_TTL_MS, REQUEST_CACHE_MAX_ENTRIES, now),
1724
+ prefixReuseCache: new ExpiringLruCache(REQUEST_CACHE_TTL_MS, REQUEST_CACHE_MAX_ENTRIES, now),
1338
1725
  inFlightRequests: new Map(),
1339
1726
  };
1340
1727
 
@@ -1350,9 +1737,11 @@ export function createPromptAutocompleteExtension(
1350
1737
  });
1351
1738
 
1352
1739
  pi.on("session_shutdown", async (_event, ctx) => {
1740
+ // Teardown, not a user decision: leave runtimeOverrides untouched.
1353
1741
  shared.enabled = false;
1354
1742
  shared.cancelActiveRequest?.();
1355
1743
  shared.requestCache.clear();
1744
+ shared.prefixReuseCache.clear();
1356
1745
  cancelAllCoalescedRequests(shared.inFlightRequests);
1357
1746
  unmountEditor(ctx, shared);
1358
1747
  });
@@ -1364,20 +1753,26 @@ export function createPromptAutocompleteExtension(
1364
1753
  refreshEditorImmediately(shared, "model-changed", formatModelLabel(event.model as Model<Api>));
1365
1754
  });
1366
1755
 
1756
+ pi.on("session_tree", async (_event, ctx) => {
1757
+ shared.sessionManager = ctx.sessionManager;
1758
+ if (!shared.enabled) return;
1759
+ refreshEditorImmediately(shared, "context-changed", "Conversation branch changed");
1760
+ });
1761
+
1367
1762
  pi.on("agent_start", async () => {
1368
- shared.streaming = true;
1763
+ shared.agentStreaming = true;
1369
1764
  if (!shared.enabled || shared.config.allowWhileStreaming) return;
1370
1765
  refreshEditorImmediately(shared, "waiting", "Main agent is still working");
1371
1766
  });
1372
1767
 
1373
1768
  pi.on("agent_end", async () => {
1374
- shared.streaming = false;
1769
+ shared.agentStreaming = false;
1375
1770
  if (!shared.enabled) return;
1376
1771
  refreshEditorImmediately(shared, "ready", "Agent finished; autocomplete can request suggestions again");
1377
1772
  });
1378
1773
 
1379
1774
  pi.registerCommand("prompt-autocomplete", {
1380
- description: "Enable, disable, or inspect inline prompt autocomplete",
1775
+ description: "Enable, disable, or inspect inline prompt autocomplete and session stats",
1381
1776
  handler: async (args, ctx) => {
1382
1777
  if (ctx.mode !== "tui") {
1383
1778
  ctx.ui.notify("prompt-autocomplete requires interactive TUI mode", "warning");
@@ -1391,6 +1786,10 @@ export function createPromptAutocompleteExtension(
1391
1786
  setWhileStreaming(ctx, shared, parts[1]);
1392
1787
  return;
1393
1788
  }
1789
+ if (command === "stream") {
1790
+ setStreamResponses(ctx, shared, parts[1]);
1791
+ return;
1792
+ }
1394
1793
 
1395
1794
  const handlers = createPromptAutocompleteCommandHandlers(ctx, shared);
1396
1795
  const handler = handlers[command];
@@ -1401,7 +1800,7 @@ export function createPromptAutocompleteExtension(
1401
1800
  }
1402
1801
 
1403
1802
  ctx.ui.notify(
1404
- "Usage: /prompt-autocomplete [on|off|toggle|status|while-streaming on|off|toggle|debug-on|debug-off|debug-toggle]",
1803
+ "Usage: /prompt-autocomplete [on|off|toggle|status|stats|stream on|off|toggle|while-streaming on|off|toggle|debug-on|debug-off|debug-toggle]",
1405
1804
  "warning",
1406
1805
  );
1407
1806
  },