@kliebhan/pi-prompt-autocomplete 0.2.2 → 0.2.4

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 CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  All notable changes to this package are documented here. This project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4
4
 
5
+ ## [0.2.4] - 2026-08-06
6
+
7
+ ### Added
8
+
9
+ - `/prompt-autocomplete on` and `off` now persist across processes. The decision is saved to `$XDG_CONFIG_HOME/pi-prompt-autocomplete/settings.json` (`~/.config` fallback, `PI_PROMPT_AUTOCOMPLETE_SETTINGS` override) and enables autocomplete in later sessions without the CLI flag. An explicit `--prompt-autocomplete` flag still outranks a saved `off` for that invocation, a decision is only recorded when the host actually installs the editor, a failed save warns without losing the in-process decision, and a malformed file degrades to flag-only behaviour. `status` attributes the setting as `(flag)`, `(saved)`, or `(session)`.
10
+
11
+ ## [0.2.3] - 2026-08-06
12
+
13
+ ### Fixed
14
+
15
+ - Inline suggestions now render on hosts that keep an editor background surface alive. The renderer located the fake cursor by Pi's exact `\x1b[7m \x1b[0m` byte sequence, but forks such as prime-agent close the inverse-video cursor with `\x1b[27m` so the background survives the cursor cell. The lookup missed, so suggestions were generated and counted as showing yet never drawn. The cursor lookup now accepts both reset variants and rebuilds the line with the host's own reset.
16
+
5
17
  ## [0.2.2] - 2026-08-06
6
18
 
7
19
  ### Security
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Inline AI completions for the [Pi coding agent](https://github.com/earendil-works/pi-mono), rendered as unobtrusive ghost text in the prompt editor.
4
4
 
5
- [Watch the demo](https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.2/prompt-autocomplete-demo.mp4)
5
+ [Watch the demo](https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.4/prompt-autocomplete-demo.mp4)
6
6
 
7
7
  ## Install
8
8
 
@@ -70,7 +70,11 @@ pi \
70
70
  - `Ctrl+.` with no active suggestion is an explicit one-shot request and may bypass the main-agent-streaming, cooldown, and minimum-length gates. Model, authentication, slash-command, and path safety checks still apply.
71
71
  - Use `--prompt-autocomplete-debug` or `/prompt-autocomplete debug-on` for troubleshooting.
72
72
 
73
- Slash-command toggles (`on`, `off`, `stream`, `while-streaming`, `debug-*`) outrank the CLI flags for the rest of the process, including in sessions started later. `status` labels each toggle with its source, `(flag)` or `(session)`. Settings you never toggled keep following their flag.
73
+ Slash-command toggles (`on`, `off`, `stream`, `while-streaming`, `debug-*`) outrank the CLI flags for the rest of the process, including in sessions started later. `status` labels each toggle with its source, `(flag)`, `(saved)`, or `(session)`. Settings you never toggled keep following their flag.
74
+
75
+ #### Persistent enablement
76
+
77
+ `/prompt-autocomplete on` and `off` are durable: the decision is saved to `$XDG_CONFIG_HOME/pi-prompt-autocomplete/settings.json` (falling back to `~/.config`; override the location with `PI_PROMPT_AUTOCOMPLETE_SETTINGS`) and applies to later processes without any CLI flag. An explicit `--prompt-autocomplete` flag still outranks a saved `off` for that invocation, and a saved decision is only recorded when the host actually installs the editor. The file stores nothing but the boolean decision; deleting it restores flag-only behaviour.
74
78
 
75
79
  ### Usage and cost accounting
76
80
 
package/core.ts CHANGED
@@ -610,7 +610,7 @@ export interface PromptAutocompleteRuntimeOverrides {
610
610
  debug?: boolean;
611
611
  }
612
612
 
613
- export type PromptAutocompleteSettingSource = "flag" | "session";
613
+ export type PromptAutocompleteSettingSource = "flag" | "saved" | "session";
614
614
 
615
615
  export function resolveOverride<T>(override: T | undefined, flagValue: T): T {
616
616
  return override ?? flagValue;
@@ -620,6 +620,67 @@ export function describeSettingSource(override: unknown): PromptAutocompleteSett
620
620
  return override === undefined ? "flag" : "session";
621
621
  }
622
622
 
623
+ /**
624
+ * Settings persisted across processes.
625
+ *
626
+ * Only explicit `/prompt-autocomplete on|off` decisions are stored. An absent
627
+ * field defers to the CLI flag, so an empty or missing file behaves exactly
628
+ * like no persisted decision.
629
+ */
630
+ export interface PromptAutocompletePersistedSettings {
631
+ enabled?: boolean;
632
+ }
633
+
634
+ /**
635
+ * Parse persisted settings text defensively.
636
+ *
637
+ * The file is user-editable, so anything malformed — invalid JSON, a non-object
638
+ * payload, or a non-boolean `enabled` — degrades to "no persisted decision"
639
+ * instead of failing extension activation.
640
+ */
641
+ export function parsePromptAutocompletePersistedSettings(
642
+ text: string | undefined,
643
+ ): PromptAutocompletePersistedSettings {
644
+ if (!text) return {};
645
+ let parsed: unknown;
646
+ try {
647
+ parsed = JSON.parse(text);
648
+ } catch {
649
+ return {};
650
+ }
651
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return {};
652
+ const enabled = (parsed as Record<string, unknown>).enabled;
653
+ return typeof enabled === "boolean" ? { enabled } : {};
654
+ }
655
+
656
+ export function serializePromptAutocompletePersistedSettings(
657
+ settings: PromptAutocompletePersistedSettings,
658
+ ): string {
659
+ const payload: PromptAutocompletePersistedSettings = {};
660
+ if (typeof settings.enabled === "boolean") payload.enabled = settings.enabled;
661
+ return `${JSON.stringify(payload, null, 2)}\n`;
662
+ }
663
+
664
+ /**
665
+ * Resolve the effective enabled state and its attribution.
666
+ *
667
+ * Precedence: an in-session slash-command decision outranks everything; an
668
+ * explicit CLI flag outranks the persisted decision because it is scoped to
669
+ * this invocation; the persisted decision outranks the built-in default.
670
+ * pi's boolean extension flags can only be switched on, so `flagEnabled` is
671
+ * true exactly when the flag was passed explicitly.
672
+ */
673
+ export function resolvePersistedEnabled(
674
+ override: boolean | undefined,
675
+ flagEnabled: boolean,
676
+ saved: boolean | undefined,
677
+ ): { enabled: boolean; source: PromptAutocompleteSettingSource } {
678
+ if (override !== undefined) return { enabled: override, source: "session" };
679
+ if (flagEnabled) return { enabled: true, source: "flag" };
680
+ if (saved !== undefined) return { enabled: saved, source: "saved" };
681
+ return { enabled: false, source: "flag" };
682
+ }
683
+
623
684
  interface SuggestionPayload {
624
685
  completions?: unknown;
625
686
  suggestions?: unknown;
package/index.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, isAbsolute, join } from "node:path";
1
4
  import * as piAi from "@earendil-works/pi-ai";
2
5
  import type {
3
6
  Api,
@@ -40,6 +43,9 @@ import {
40
43
  parseBoundedIntFlag,
41
44
  parsePartialPromptSuggestion,
42
45
  parsePromptAutocompleteModelSelection,
46
+ parsePromptAutocompletePersistedSettings,
47
+ resolvePersistedEnabled,
48
+ serializePromptAutocompletePersistedSettings,
43
49
  PROMPT_AUTOCOMPLETE_SYSTEM_PROMPT,
44
50
  recordProviderLatency,
45
51
  recordProviderUsage,
@@ -52,6 +58,7 @@ import {
52
58
  type CoalescedRequestEntry,
53
59
  type CoalescedRequestSubscription,
54
60
  type PromptAutocompleteModelSelection,
61
+ type PromptAutocompletePersistedSettings,
55
62
  type PromptAutocompleteRuntimeOverrides,
56
63
  type PromptAutocompleteUsageStats,
57
64
  } from "./core.ts";
@@ -59,7 +66,13 @@ import {
59
66
  const GHOST_TEXT_STYLE = "\x1b[2m";
60
67
  const GHOST_INDICATOR_STYLE = "\x1b[90m";
61
68
  const RESET = "\x1b[0m";
62
- const CURSOR_TOKEN = "\x1b[7m \x1b[0m";
69
+ // Hosts draw the end-of-line fake cursor as an inverse-video space. Pi closes
70
+ // it with a full SGR reset (\x1b[0m); forks that keep an editor background
71
+ // surface alive, such as prime-agent, close it with \x1b[27m so the background
72
+ // survives the cursor cell. Accept both, otherwise the suggestion lookup never
73
+ // finds the cursor and inline suggestions silently never render.
74
+ const CURSOR_INVERSE_SPACE = "\x1b[7m ";
75
+ const CURSOR_TOKEN_PATTERN = /\x1b\[7m \x1b\[(0|27)m/;
63
76
  const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] as const;
64
77
  const SPINNER_INTERVAL_MS = 80;
65
78
  const SPINNER_LABEL = "Generating suggestion";
@@ -204,6 +217,59 @@ export interface PromptAutocompleteDependencies {
204
217
  completeSimple?: CompleteSimpleFunction;
205
218
  streamSimple?: StreamSimpleFunction;
206
219
  now?: () => number;
220
+ /** Store for decisions that survive the process; defaults to a JSON settings file. */
221
+ settingsStore?: PromptAutocompleteSettingsStore;
222
+ }
223
+
224
+ /**
225
+ * Persistence boundary for cross-process decisions.
226
+ *
227
+ * `load` runs once per activation and must not throw for a missing or
228
+ * malformed store; `save` may throw and the caller reports the failure.
229
+ */
230
+ export interface PromptAutocompleteSettingsStore {
231
+ load(): PromptAutocompletePersistedSettings;
232
+ save(settings: PromptAutocompletePersistedSettings): void;
233
+ }
234
+
235
+ /**
236
+ * Resolve the persisted settings file path.
237
+ *
238
+ * `PI_PROMPT_AUTOCOMPLETE_SETTINGS` overrides the location outright; otherwise
239
+ * the file lives under `$XDG_CONFIG_HOME/pi-prompt-autocomplete/settings.json`,
240
+ * falling back to `~/.config` when XDG is unset or not absolute as the spec
241
+ * requires.
242
+ */
243
+ export function resolvePromptAutocompleteSettingsPath(
244
+ env: Record<string, string | undefined> = process.env,
245
+ homeDir: string = homedir(),
246
+ ): string {
247
+ const explicit = env.PI_PROMPT_AUTOCOMPLETE_SETTINGS;
248
+ if (explicit) return explicit;
249
+ const xdgConfigHome = env.XDG_CONFIG_HOME;
250
+ const configHome = xdgConfigHome && isAbsolute(xdgConfigHome) ? xdgConfigHome : join(homeDir, ".config");
251
+ return join(configHome, "pi-prompt-autocomplete", "settings.json");
252
+ }
253
+
254
+ function createFileSettingsStore(path: string): PromptAutocompleteSettingsStore {
255
+ return {
256
+ load(): PromptAutocompletePersistedSettings {
257
+ let text: string;
258
+ try {
259
+ text = readFileSync(path, "utf8");
260
+ } catch {
261
+ return {};
262
+ }
263
+ return parsePromptAutocompletePersistedSettings(text);
264
+ },
265
+ save(settings: PromptAutocompletePersistedSettings): void {
266
+ // Atomic replace so a crash mid-write cannot corrupt the settings file.
267
+ const tempPath = `${path}.tmp`;
268
+ mkdirSync(dirname(path), { recursive: true });
269
+ writeFileSync(tempPath, serializePromptAutocompletePersistedSettings(settings), "utf8");
270
+ renameSync(tempPath, path);
271
+ },
272
+ };
207
273
  }
208
274
 
209
275
  interface PromptAutocompleteSharedState {
@@ -213,6 +279,11 @@ interface PromptAutocompleteSharedState {
213
279
  config: PromptAutocompleteConfig;
214
280
  /** Slash-command decisions that outrank flags for the lifetime of the process. */
215
281
  runtimeOverrides: PromptAutocompleteRuntimeOverrides;
282
+ /** Decisions persisted across processes; loaded once per activation. */
283
+ persistedSettings: PromptAutocompletePersistedSettings;
284
+ settingsStore?: PromptAutocompleteSettingsStore;
285
+ /** Last enabled-flag value seen by applyEffectiveConfig, for status attribution. */
286
+ enabledFlagValue: boolean;
216
287
  /** Session-scoped request accounting; reset together with the cache. */
217
288
  usageStats: PromptAutocompleteUsageStats;
218
289
  currentModel?: Model<Api>;
@@ -475,7 +546,12 @@ function applyEffectiveConfig(pi: ExtensionAPI, shared: PromptAutocompleteShared
475
546
  const flagEnabled = pi.getFlag("prompt-autocomplete") === true;
476
547
  const flagConfig = parseConfig(pi);
477
548
 
478
- shared.enabled = resolveOverride(shared.runtimeOverrides.enabled, flagEnabled);
549
+ shared.enabledFlagValue = flagEnabled;
550
+ shared.enabled = resolvePersistedEnabled(
551
+ shared.runtimeOverrides.enabled,
552
+ flagEnabled,
553
+ shared.persistedSettings.enabled,
554
+ ).enabled;
479
555
  shared.config = {
480
556
  ...flagConfig,
481
557
  allowWhileStreaming: resolveOverride(
@@ -529,7 +605,13 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
529
605
  const requestedModel = describePromptAutocompleteModelSelection(shared.config.modelSelection);
530
606
 
531
607
  return [
532
- `enabled=${shared.enabled ? "yes" : "no"}(${describeSettingSource(shared.runtimeOverrides.enabled)})`,
608
+ `enabled=${shared.enabled ? "yes" : "no"}(${
609
+ resolvePersistedEnabled(
610
+ shared.runtimeOverrides.enabled,
611
+ shared.enabledFlagValue,
612
+ shared.persistedSettings.enabled,
613
+ ).source
614
+ })`,
533
615
  `editor=${editorState}`,
534
616
  shared.editorBlockedReason ? `editor-blocked=${truncateDebug(shared.editorBlockedReason, 90)}` : undefined,
535
617
  `model=${truncateDebug(formatModelLabel(resolvedModel), 90)}`,
@@ -718,15 +800,26 @@ class PromptAutocompleteEditor extends CustomEditor {
718
800
  return lines;
719
801
  }
720
802
 
721
- const cursorLineIndex = lines.findIndex((line) => line.includes(CURSOR_TOKEN));
722
- if (cursorLineIndex === -1) return lines;
803
+ let cursorLineIndex = -1;
804
+ let cursorMatch: RegExpMatchArray | undefined;
805
+ for (let index = 0; index < lines.length; index++) {
806
+ if (!lines[index]!.includes(CURSOR_INVERSE_SPACE)) continue;
807
+ const match = lines[index]!.match(CURSOR_TOKEN_PATTERN);
808
+ if (match?.index !== undefined) {
809
+ cursorLineIndex = index;
810
+ cursorMatch = match;
811
+ break;
812
+ }
813
+ }
814
+ if (cursorLineIndex === -1 || !cursorMatch || cursorMatch.index === undefined) return lines;
723
815
 
724
816
  const cursorLine = lines[cursorLineIndex]!;
725
- const cursorIndex = cursorLine.indexOf(CURSOR_TOKEN);
726
- if (cursorIndex === -1) return lines;
817
+ const cursorIndex = cursorMatch.index;
818
+ const cursorToken = cursorMatch[0];
819
+ const cursorReset = cursorMatch[1] === "27" ? "\x1b[27m" : "\x1b[0m";
727
820
 
728
821
  const before = cursorLine.slice(0, cursorIndex);
729
- const after = cursorLine.slice(cursorIndex + CURSOR_TOKEN.length);
822
+ const after = cursorLine.slice(cursorIndex + cursorToken.length);
730
823
  const availableWidth = visibleWidth(after);
731
824
  if (availableWidth <= 0) return lines;
732
825
 
@@ -735,7 +828,7 @@ class PromptAutocompleteEditor extends CustomEditor {
735
828
  const firstPreviewChar = previewChars[0] ?? "";
736
829
  const absorbLeadingWhitespaceIntoCursor = /^[ \t]$/.test(firstPreviewChar);
737
830
 
738
- const cursorCell = absorbLeadingWhitespaceIntoCursor ? `\x1b[7m${firstPreviewChar}\x1b[0m` : CURSOR_TOKEN;
831
+ const cursorCell = absorbLeadingWhitespaceIntoCursor ? `\x1b[7m${firstPreviewChar}${cursorReset}` : cursorToken;
739
832
  const renderedPreviewText = absorbLeadingWhitespaceIntoCursor ? previewChars.slice(1).join("") : previewText;
740
833
 
741
834
  const indicator = this.suggestions.length > 1 ? ` ‹${this.suggestionIndex + 1}/${this.suggestions.length}›` : "";
@@ -1724,6 +1817,31 @@ function setStreamResponses(
1724
1817
  }
1725
1818
  }
1726
1819
 
1820
+ /**
1821
+ * Record an explicit enable/disable decision so later processes honour it.
1822
+ *
1823
+ * The in-memory copy is updated even when the store write fails, so the
1824
+ * current process stays consistent with what the user asked for.
1825
+ */
1826
+ function persistEnabledDecision(
1827
+ ctx: ExtensionContext,
1828
+ shared: PromptAutocompleteSharedState,
1829
+ enabled: boolean,
1830
+ ): void {
1831
+ shared.persistedSettings = { ...shared.persistedSettings, enabled };
1832
+ const store = shared.settingsStore;
1833
+ if (!store) return;
1834
+ try {
1835
+ store.save(shared.persistedSettings);
1836
+ } catch (error) {
1837
+ const reason = error instanceof Error ? error.message : String(error);
1838
+ ctx.ui.notify(
1839
+ `Prompt autocomplete could not save the enabled setting: ${truncateDebug(reason, 90)}`,
1840
+ "warning",
1841
+ );
1842
+ }
1843
+ }
1844
+
1727
1845
  function enablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
1728
1846
  const previousOverride = shared.runtimeOverrides.enabled;
1729
1847
  shared.enabled = true;
@@ -1829,34 +1947,40 @@ function createPromptAutocompleteCommandHandlers(
1829
1947
  on: () => {
1830
1948
  if (shared.enabled && shared.ownsEditor?.()) {
1831
1949
  // Already in the requested state, but the user still expressed intent:
1832
- // record it so a later session does not fall back to the flag.
1950
+ // record it so later sessions and processes do not fall back to the flag.
1833
1951
  shared.runtimeOverrides.enabled = true;
1952
+ persistEnabledDecision(ctx, shared, true);
1834
1953
  ctx.ui.notify(`Prompt autocomplete already enabled (${formatStatus(shared)})`, "info");
1835
1954
  return;
1836
1955
  }
1837
1956
 
1838
1957
  if (enablePromptAutocomplete(ctx, shared)) {
1958
+ persistEnabledDecision(ctx, shared, true);
1839
1959
  notifyPromptAutocompleteEnabled(ctx, shared, { includeModel: true });
1840
1960
  }
1841
1961
  },
1842
1962
  off: () => {
1843
1963
  if (!shared.enabled) {
1844
1964
  shared.runtimeOverrides.enabled = false;
1965
+ persistEnabledDecision(ctx, shared, false);
1845
1966
  ctx.ui.notify("Prompt autocomplete is already disabled", "info");
1846
1967
  return;
1847
1968
  }
1848
1969
 
1849
1970
  disablePromptAutocomplete(ctx, shared);
1971
+ persistEnabledDecision(ctx, shared, false);
1850
1972
  ctx.ui.notify("Prompt autocomplete disabled", "info");
1851
1973
  },
1852
1974
  toggle: () => {
1853
1975
  if (shared.enabled) {
1854
1976
  disablePromptAutocomplete(ctx, shared);
1977
+ persistEnabledDecision(ctx, shared, false);
1855
1978
  ctx.ui.notify("Prompt autocomplete disabled", "info");
1856
1979
  return;
1857
1980
  }
1858
1981
 
1859
1982
  if (enablePromptAutocomplete(ctx, shared)) {
1983
+ persistEnabledDecision(ctx, shared, true);
1860
1984
  notifyPromptAutocompleteEnabled(ctx, shared, { includeModel: false });
1861
1985
  }
1862
1986
  },
@@ -1876,6 +2000,15 @@ export function createPromptAutocompleteExtension(
1876
2000
  const now = dependencies.now ?? Date.now;
1877
2001
 
1878
2002
  return function promptAutocompleteExtension(pi: ExtensionAPI): void {
2003
+ const settingsStore = dependencies.settingsStore ?? createFileSettingsStore(resolvePromptAutocompleteSettingsPath());
2004
+ let persistedSettings: PromptAutocompletePersistedSettings;
2005
+ try {
2006
+ persistedSettings = settingsStore.load();
2007
+ } catch {
2008
+ // A store that violates the load contract must not block activation.
2009
+ persistedSettings = {};
2010
+ }
2011
+
1879
2012
  pi.registerFlag("prompt-autocomplete", {
1880
2013
  description: "Enable inline AI prompt autocomplete in the editor",
1881
2014
  type: "boolean",
@@ -1930,6 +2063,9 @@ export function createPromptAutocompleteExtension(
1930
2063
  agentStreaming: false,
1931
2064
  config: parseConfig(pi),
1932
2065
  runtimeOverrides: {},
2066
+ persistedSettings,
2067
+ settingsStore,
2068
+ enabledFlagValue: false,
1933
2069
  usageStats: createPromptAutocompleteUsageStats(),
1934
2070
  completeSimple: completeSimpleImpl,
1935
2071
  streamSimple: streamSimpleImpl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kliebhan/pi-prompt-autocomplete",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Privacy-conscious inline AI prompt completion for the Pi coding agent",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -26,7 +26,7 @@
26
26
  "extensions": [
27
27
  "./index.ts"
28
28
  ],
29
- "video": "https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.2/prompt-autocomplete-demo.mp4"
29
+ "video": "https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.4/prompt-autocomplete-demo.mp4"
30
30
  },
31
31
  "engines": {
32
32
  "node": ">=22.19.0"