@kliebhan/pi-prompt-autocomplete 0.2.3 → 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,12 @@
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
+
5
11
  ## [0.2.3] - 2026-08-06
6
12
 
7
13
  ### Fixed
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.3/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";
@@ -210,6 +217,59 @@ export interface PromptAutocompleteDependencies {
210
217
  completeSimple?: CompleteSimpleFunction;
211
218
  streamSimple?: StreamSimpleFunction;
212
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
+ };
213
273
  }
214
274
 
215
275
  interface PromptAutocompleteSharedState {
@@ -219,6 +279,11 @@ interface PromptAutocompleteSharedState {
219
279
  config: PromptAutocompleteConfig;
220
280
  /** Slash-command decisions that outrank flags for the lifetime of the process. */
221
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;
222
287
  /** Session-scoped request accounting; reset together with the cache. */
223
288
  usageStats: PromptAutocompleteUsageStats;
224
289
  currentModel?: Model<Api>;
@@ -481,7 +546,12 @@ function applyEffectiveConfig(pi: ExtensionAPI, shared: PromptAutocompleteShared
481
546
  const flagEnabled = pi.getFlag("prompt-autocomplete") === true;
482
547
  const flagConfig = parseConfig(pi);
483
548
 
484
- 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;
485
555
  shared.config = {
486
556
  ...flagConfig,
487
557
  allowWhileStreaming: resolveOverride(
@@ -535,7 +605,13 @@ function formatStatus(shared: PromptAutocompleteSharedState): string {
535
605
  const requestedModel = describePromptAutocompleteModelSelection(shared.config.modelSelection);
536
606
 
537
607
  return [
538
- `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
+ })`,
539
615
  `editor=${editorState}`,
540
616
  shared.editorBlockedReason ? `editor-blocked=${truncateDebug(shared.editorBlockedReason, 90)}` : undefined,
541
617
  `model=${truncateDebug(formatModelLabel(resolvedModel), 90)}`,
@@ -1741,6 +1817,31 @@ function setStreamResponses(
1741
1817
  }
1742
1818
  }
1743
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
+
1744
1845
  function enablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
1745
1846
  const previousOverride = shared.runtimeOverrides.enabled;
1746
1847
  shared.enabled = true;
@@ -1846,34 +1947,40 @@ function createPromptAutocompleteCommandHandlers(
1846
1947
  on: () => {
1847
1948
  if (shared.enabled && shared.ownsEditor?.()) {
1848
1949
  // Already in the requested state, but the user still expressed intent:
1849
- // 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.
1850
1951
  shared.runtimeOverrides.enabled = true;
1952
+ persistEnabledDecision(ctx, shared, true);
1851
1953
  ctx.ui.notify(`Prompt autocomplete already enabled (${formatStatus(shared)})`, "info");
1852
1954
  return;
1853
1955
  }
1854
1956
 
1855
1957
  if (enablePromptAutocomplete(ctx, shared)) {
1958
+ persistEnabledDecision(ctx, shared, true);
1856
1959
  notifyPromptAutocompleteEnabled(ctx, shared, { includeModel: true });
1857
1960
  }
1858
1961
  },
1859
1962
  off: () => {
1860
1963
  if (!shared.enabled) {
1861
1964
  shared.runtimeOverrides.enabled = false;
1965
+ persistEnabledDecision(ctx, shared, false);
1862
1966
  ctx.ui.notify("Prompt autocomplete is already disabled", "info");
1863
1967
  return;
1864
1968
  }
1865
1969
 
1866
1970
  disablePromptAutocomplete(ctx, shared);
1971
+ persistEnabledDecision(ctx, shared, false);
1867
1972
  ctx.ui.notify("Prompt autocomplete disabled", "info");
1868
1973
  },
1869
1974
  toggle: () => {
1870
1975
  if (shared.enabled) {
1871
1976
  disablePromptAutocomplete(ctx, shared);
1977
+ persistEnabledDecision(ctx, shared, false);
1872
1978
  ctx.ui.notify("Prompt autocomplete disabled", "info");
1873
1979
  return;
1874
1980
  }
1875
1981
 
1876
1982
  if (enablePromptAutocomplete(ctx, shared)) {
1983
+ persistEnabledDecision(ctx, shared, true);
1877
1984
  notifyPromptAutocompleteEnabled(ctx, shared, { includeModel: false });
1878
1985
  }
1879
1986
  },
@@ -1893,6 +2000,15 @@ export function createPromptAutocompleteExtension(
1893
2000
  const now = dependencies.now ?? Date.now;
1894
2001
 
1895
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
+
1896
2012
  pi.registerFlag("prompt-autocomplete", {
1897
2013
  description: "Enable inline AI prompt autocomplete in the editor",
1898
2014
  type: "boolean",
@@ -1947,6 +2063,9 @@ export function createPromptAutocompleteExtension(
1947
2063
  agentStreaming: false,
1948
2064
  config: parseConfig(pi),
1949
2065
  runtimeOverrides: {},
2066
+ persistedSettings,
2067
+ settingsStore,
2068
+ enabledFlagValue: false,
1950
2069
  usageStats: createPromptAutocompleteUsageStats(),
1951
2070
  completeSimple: completeSimpleImpl,
1952
2071
  streamSimple: streamSimpleImpl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kliebhan/pi-prompt-autocomplete",
3
- "version": "0.2.3",
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.3/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"