@kliebhan/pi-prompt-autocomplete 0.3.0 → 0.5.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 +33 -0
  2. package/README.md +43 -8
  3. package/core.ts +433 -5
  4. package/index.ts +753 -245
  5. package/package.json +2 -2
package/CHANGELOG.md CHANGED
@@ -2,6 +2,39 @@
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.5.0] - 2026-11-17
6
+
7
+ ### Added
8
+
9
+ - RPC-only `/prompt-autocomplete complete <json>` returns a normalized draft continuation through the `prompt-autocomplete-completion` widget. Host-supplied conversation context uses the existing bounds; requests reuse model selection, cache, budget, and stats. Superseded requests are aborted and receive one null reply. Neither ghost text nor next-prompt mode needs to be enabled.
10
+
11
+ ## [0.4.0] - 2026-11-12
12
+
13
+ ### Added
14
+
15
+ - Configurable keys: `/prompt-autocomplete set key <action> <key...|default|none>` changes the keys of `accept-word`, `accept-line`, `cycle-next`, and `cycle-prev`, applies them at once, and saves them in the settings file. Keys follow Pi's key syntax and are space-separated. Keys that could never reach an action are rejected: Tab, Escape, Enter, and Backspace with their legacy aliases such as `ctrl+i`, keys Pi's editor handles itself such as `shift+enter` or `ctrl+j`, keys that type text, and keys Pi never recognizes (Escape or F1–F12 with a modifier, Clear with anything but Shift or Ctrl alone). So are keys another action already has; a key that Pi's keymap binds is accepted, with a warning once the editor is active, because Pi keeps it. `Tab` and `Escape` stay fixed.
16
+ - `/prompt-autocomplete keys` shows every action's keys, whether they are the default or saved, saved keys that are ignored, and, once the editor is active, keys Pi's keymap also binds.
17
+ - `accept-line` accepts a suggestion up to and including its first line break; a one-line suggestion is accepted whole. It has no default key.
18
+ - `--prompt-autocomplete-next-prompt`: in RPC mode, after each settled turn, one request predicts the next prompt and hands it to the host with `set_editor_text`, for hosts such as a T3 Code build that shows it as the suggested next prompt. A new turn or session aborts it, and a late result for a busy agent or another branch is dropped; model, budget, and stats apply as in the editor.
19
+
20
+ ### Changed
21
+
22
+ - Every release is now also verified against Pi `1.5.2`, next to `0.80.6`, `0.83.0`, and `1.2.0`.
23
+ - `Escape` now dismisses a suggestion for as long as you keep typing the same draft, instead of only until the next keystroke. Previously the next character brought the dismissed suggestion back, from the prefix cache or through a new provider request. The dismissal ends for good when the draft no longer extends the dismissed text (for example after `Backspace`, submitting, clearing, or recalling another prompt), when the conversation, the autocomplete model, `max-chars`, or `max-alternatives` changes, or when you press the one-shot key (`cycle-next`, `Ctrl+.` by default), accept, or cycle. An `Escape` on an empty-draft suggestion lasts only while the draft stays empty.
24
+
25
+ ### Fixed
26
+
27
+ - The word-accept and cycle keys (`Ctrl+Space`, `Ctrl+Tab`, `Ctrl+.`, `Alt+]`, `Ctrl+,`, `Alt+[`) no longer take input that Pi would use. A Pi action you bind to one of them in `keybindings.json`, including history and interrupt, and another extension's shortcut on the same key now get the key as they would without Prompt Autocomplete; previously visible ghost text took it. A pending character jump (`Ctrl+]`) is now cancelled by such a key, as in Pi, before the key's action runs. The check uses Pi's current keymap, so remaps loaded by `/reload` apply at once. Once the editor is active, `/prompt-autocomplete keys` and `status` (`keys-used-by-pi`) list keys your Pi keymap binds; other extensions' shortcuts cannot be listed. Pi's default keymap uses none of them, so nothing changes without remaps.
28
+ - Saving a setting no longer discards decisions another Pi process saved while this one was running, and no longer drops fields a newer version wrote: each save re-reads the settings file and changes only its own decision. A read error other than a missing file aborts the save instead of replacing the file. Two saves that overlap, each between its read and its write, can still lose one decision; there is no lock.
29
+
30
+ ### Security
31
+
32
+ - The settings file is replaced atomically through a new temporary file with an unpredictable name that is created exclusively, so a link planted in a shared settings directory is never written through. On POSIX systems the file is now readable by its owner only (`0600`), also when an older version created it.
33
+
34
+ ### Upgrading
35
+
36
+ - Version 0.3.0 and earlier drop the saved `keys`, like any other field they do not know, the next time they save a setting; the actions then return to their default keys.
37
+
5
38
  ## [0.3.0] - 2026-10-13
6
39
 
7
40
  ### Changed
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.3.0/prompt-autocomplete-demo.mp4)
5
+ [Watch the demo](https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.5.0/prompt-autocomplete-demo.mp4)
6
6
 
7
7
  ## Install
8
8
 
@@ -38,7 +38,8 @@ Or enable it from inside an interactive session; the decision is saved for later
38
38
  | Accept the next word or chunk | `Ctrl+Space` |
39
39
  | Next alternative / manual one-shot | `Ctrl+.` |
40
40
  | Previous alternative | `Ctrl+,` |
41
- | Dismiss the suggestion for this draft | `Escape` |
41
+ | Accept up to the end of the line | none by default; see [Keys](#keys) |
42
+ | Dismiss suggestions while you keep typing this draft | `Escape` |
42
43
  | Enable, disable, or inspect configuration | `/prompt-autocomplete on\|off\|toggle\|status` |
43
44
  | Show in-memory effectiveness and cost counters | `/prompt-autocomplete stats` |
44
45
  | Preview what a request would send, locally | `/prompt-autocomplete inspect [draft text]` |
@@ -48,10 +49,20 @@ Or enable it from inside an interactive session; the decision is saved for later
48
49
  | Cap provider requests for this session | `/prompt-autocomplete budget <n\|off>` |
49
50
  | Persist remaining runtime knobs | `/prompt-autocomplete set debounce-ms\|max-chars\|max-alternatives\|model` |
50
51
  | Inspect effective runtime knobs | `/prompt-autocomplete set` |
52
+ | Show the effective keys of every action | `/prompt-autocomplete keys` |
53
+ | Change and persist an action's keys | `/prompt-autocomplete set key <action> <key...\|default\|none>` |
51
54
  | Show or hide the debug status line | `/prompt-autocomplete debug-on\|debug-off\|debug-toggle` |
52
55
 
53
56
  Legacy fallbacks are available for terminals that forward them: `Ctrl+Tab`, `Alt+[`, and `Alt+]`.
54
57
 
58
+ ### Keys
59
+
60
+ `/prompt-autocomplete set key <action> <key...>` changes the keys of `accept-word`, `accept-line`, `cycle-next` (also the manual one-shot), or `cycle-prev`; it applies at once and is saved with the other [persistent settings](#persistent-settings). Separate several keys with spaces, since `,` is a key itself: `set key cycle-prev ctrl+, alt+[`. `default` restores an action's default keys and `none` unbinds it. `accept-line` has no default key; `set key accept-line alt+l` gives it one. Accepting a line inserts the suggestion up to and including its first line break; a one-line suggestion is accepted whole.
61
+
62
+ Keys use Pi's syntax (`ctrl+shift+x`, `alt+l`, `f5`; see Pi's `docs/keybindings.md`). Keys that cannot reach an action are rejected: `Tab`, `Escape`, `Enter`, `Backspace` and their legacy aliases such as `ctrl+i`, keys Pi's editor handles itself such as `shift+enter`, keys that type text such as `a` or `shift+a`, and keys Pi does not recognize: Escape and F1–F12 with a modifier, and Clear with anything but Shift or Ctrl alone. A key another Prompt Autocomplete action has must be freed there first. `Tab` (accept) and `Escape` (dismiss) are fixed. `/prompt-autocomplete keys` shows every action's keys, whether they are the default or saved, and saved keys that are ignored.
63
+
64
+ Pi comes first: if your Pi keymap (`keybindings.json`) binds one of these keys, or another extension registers it as a shortcut, the key does what Pi or that extension defines. Once the editor is active, `keys` and `status` (`keys-used-by-pi`) list the keys your Pi keymap binds, and `set key` warns when you choose one; before that, Pi's keymap is not available to check. Other extensions' shortcuts cannot be listed, but they still win when you press the key. A pending character jump (`Ctrl+]`) is cancelled by such a key, as in Pi, before the key's action runs. `Tab` and `Escape` keep their ghost-text meaning while a suggestion is visible.
65
+
55
66
  After `/prompt-autocomplete ` and a few letters, Pi's command menu completes the arguments, including nested forms such as `set debounce-ms` or `set model active`; `Tab` or `Enter` inserts the selection. A command that is already complete, such as `stats` or `stream on`, opens no menu, so `Enter` still submits it at once. Completion is local and installs no global autocomplete provider.
56
67
 
57
68
  Built-in slash-command and file/path completion keeps precedence over ghost suggestions.
@@ -75,8 +86,9 @@ pi \
75
86
  - Provider responses stream into the first ghost-text suggestion by default. Use `--prompt-autocomplete-stream off` or `/prompt-autocomplete stream off` to wait for complete responses instead. This changes rendering only: each suggestion request still uses the same context and token budget.
76
87
  - Changing `/prompt-autocomplete stream` cancels active autocomplete work but does **not** start a replacement request; the selected path applies to the next edit or manual one-shot.
77
88
  - Partial text advances monotonically: Latin text waits for complete word boundaries, while CJK and other no-space scripts remain grapheme-safe. Alternatives appear only after the response finishes.
78
- - `Tab` accepts all partial text currently visible. `Ctrl+Space` accepts only its next visible word/chunk. Both cancel that stream, and unlike accepting a completed suggestion, neither automatically starts another paid request.
89
+ - `Tab` accepts all partial text currently visible. `Ctrl+Space` accepts only its next visible word/chunk, and `accept-line` its visible text up to and including the first line break. Each cancels that stream, and unlike accepting a completed suggestion, none automatically starts another paid request.
79
90
  - Suggestions pause while the **main agent** is working by default, and stay paused until `agent_settled` on hosts that emit it. This is separate from streamed autocomplete responses. Use `--prompt-autocomplete-while-streaming` or `/prompt-autocomplete while-streaming on` to change that behavior.
91
+ - `Escape` dismissal lasts while the draft extends the dismissed text: further typing starts no request and does not bring the suggestion back. It ends for good when the draft stops extending it (`Backspace`, submit, clear, history), when the conversation, autocomplete model, `max-chars`, or `max-alternatives` changes, or on the one-shot key (`cycle-next`, `Ctrl+.` by default), accept, or cycling. Dismissing an empty-draft suggestion lasts only while the draft stays empty.
80
92
  - `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.
81
93
  - Use `--prompt-autocomplete-debug` or `/prompt-autocomplete debug-on` for troubleshooting.
82
94
 
@@ -98,7 +110,7 @@ Changing `max-chars`, `max-alternatives`, or `model` cancels in-flight autocompl
98
110
 
99
111
  #### Persistent settings
100
112
 
101
- `/prompt-autocomplete on`, `off`, `min-chars <n>`, and `/prompt-autocomplete set` (`debounce-ms`, `max-chars`, `max-alternatives`, `model`) 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. `/prompt-autocomplete min-chars` remains the canonical command; `set min-chars` dispatches to it. An explicit CLI flag still outranks the saved value for that invocation — for numeric knobs and the model flag, passing the registered default is indistinguishable from not passing the flag and defers to the saved value. `set model` validates syntax, registry presence, and auth before saving: a dedicated model must resolve, and `active` commits while no active model is known yet but is rejected when the known active model is unusable. `active` is an explicit sentinel for the session model, mixed-case model ids are preserved, and a cross-provider change restates the privacy notice. An enable decision is only recorded when the host actually installs the editor. The file stores nothing but these decisions; deleting it restores flag-only behaviour. `status` labels each value with its source, and success notices only claim durability when the file was actually written.
113
+ `/prompt-autocomplete on`, `off`, `min-chars <n>`, and `/prompt-autocomplete set` (`debounce-ms`, `max-chars`, `max-alternatives`, `model`, `key`) 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. `/prompt-autocomplete min-chars` remains the canonical command; `set min-chars` dispatches to it. An explicit CLI flag still outranks the saved value for that invocation — for numeric knobs and the model flag, passing the registered default is indistinguishable from not passing the flag and defers to the saved value. `set model` validates syntax, registry presence, and auth before saving: a dedicated model must resolve, and `active` commits while no active model is known yet but is rejected when the known active model is unusable. `active` is an explicit sentinel for the session model, mixed-case model ids are preserved, and a cross-provider change restates the privacy notice. An enable decision is only recorded when the host actually installs the editor. Prompt Autocomplete writes nothing but these decisions to the file, never conversation, cache, or stats, and keeps fields it does not know; deleting it restores flag-only behaviour. Each save re-reads the file and changes only the decision being saved, so decisions another Pi process saved in the meantime survive, as do fields a newer version wrote; if the file exists but cannot be read, the save is aborted rather than replacing it. On POSIX systems the file is readable by its owner only. Two saves that overlap, each between its read and its write, can still lose one decision; there is no lock. `status` labels each value with its source, and success notices only claim durability when the file was actually written.
102
114
 
103
115
  ### Usage and cost accounting
104
116
 
@@ -119,7 +131,7 @@ Budget: 4/25 (session)
119
131
  - `issued` counts provider calls actually made. Cache hits add no provider request, tokens, cost, or latency sample.
120
132
  - Cache hits distinguish exact-draft results from prefix reuse while you type through a cached suggestion.
121
133
  - `offered` counts active ghost-text suggestions handed to the editor. Streamed revisions of the same active suggestion do not inflate it; cycling to another alternative counts a new offer. A terminal that is too narrow to draw ghost text can still count an offer.
122
- - Full and word/chunk acceptance are counted separately, including acceptance of visible streamed partials.
134
+ - Full and word/chunk acceptance are counted separately, including acceptance of visible streamed partials. `accept-line` counts as word/chunk, except when it accepts a completed one-line suggestion whole.
123
135
  - `failed` includes provider errors and aborted requests. Tokens from a failed response are counted when the provider returns its terminal usage report within the bounded cancellation drain; otherwise `tok+`/`est+` marks the totals incomplete.
124
136
  - Mean provider latency measures local elapsed time from each actual provider invocation until it resolves or rejects; cache hits are excluded.
125
137
  - Token counts come from the provider.
@@ -135,6 +147,29 @@ Budget: 4/25 (session)
135
147
  - Without draft text the report covers the conversation sections only. The slash command replaced the editor text when you submitted it, so the draft a real request would carry cannot be recovered and is not invented; pass one explicitly to preview a complete request. Pi trims trailing whitespace from submitted commands.
136
148
  - `Shared policy` evaluates only the gates every editor shares — enabled state, editor ownership, the main-agent gate, model and auth, `min-chars` and slash/path detection for the given draft, and the session budget — separately for automatic requests and the manual `Ctrl+.` one-shot. Cursor position, the built-in autocomplete menu, Escape dismissal, the per-editor error cooldown, and cached responses belong to the live editor and are not evaluated.
137
149
 
150
+ ### Draft completion for RPC hosts
151
+
152
+ An RPC host can request a continuation even while the agent is busy, without enabling ghost text or next-prompt suggestions:
153
+
154
+ ```sh
155
+ pi --mode rpc --no-session --no-tools --prompt-autocomplete-model provider/model
156
+ ```
157
+
158
+ Send an RPC `prompt` with message `/prompt-autocomplete complete <json>`. The JSON is `{"id":"unique-request-id","draft":"Write","context":{"lastUser":"…","lastAssistant":"…"}}`; `context` and both of its fields are optional. Supply the host thread's latest messages when the sidecar has no session. They use the existing bounded conversation snapshot, not the sidecar's conversation.
159
+
160
+ Each request receives one fire-and-forget `ctx.ui.setWidget("prompt-autocomplete-completion", [JSON.stringify(result)])`, where `result` is `{"id":"unique-request-id","completion":" the tests"}`. The completion is text to **append**, normalized like ghost text, first alternative only and bounded by `max-chars`. It is `null` for short drafts, exhausted budgets, unusable output, superseded requests, or errors; errors also carry a short, sanitized `error` string. Malformed requests with a string `id` receive an error; invalid JSON or missing/non-string ids cannot be correlated and receive no widget.
161
+
162
+ Use a unique id per request. A new request aborts the previous one and immediately replies `null` for its id, even if the provider is slow to stop. Requests share model selection, result cache, request budget, and stats with the existing path. This command does not write editor text or cancel next-prompt requests. It is RPC-only and omitted from argument completions.
163
+
164
+ ### Next prompt for RPC hosts (T3 Code)
165
+
166
+ With `--prompt-autocomplete-next-prompt`, Pi in RPC mode predicts your next prompt after each settled turn and sends it to the host with `set_editor_text` (`ctx.ui.setEditorText`). The host decides what to do with it. Upstream T3 Code ignores the call; the T3 Code branch [`KLIEBHAN/t3code@pi-prompt-suggestions`](https://github.com/KLIEBHAN/t3code/tree/pi-prompt-suggestions) shows it as the suggested next prompt when **Suggest next prompt** is on in Settings > Labs.
167
+
168
+ - It applies only to `--mode rpc`, with or without `--prompt-autocomplete`; the TUI keeps its ghost text.
169
+ - Each settled turn makes one request with an empty draft and one alternative. The autocomplete model, `max-chars`, the session budget, and the `stats` counters apply as in the editor. A new turn or session aborts a pending request, and a result that arrives while the agent is busy again or after the conversation moved to another branch is dropped.
170
+ - In T3 Code, add the flag to Settings > Providers > Pi > Launch arguments. T3 Code starts Pi for delegated threads with the same arguments, so those turns cost a request as well, although T3 Code shows no suggestion for them.
171
+ - Hosts that ignore `set_editor_text` still pay for each request, so enable the flag only for a host that shows the suggestion.
172
+
138
173
  ## Privacy, providers, and cost
139
174
 
140
175
  Enabling Prompt Autocomplete permits additional model requests. Streaming changes only when the same response becomes visible; it does not add a second request. A request can contain:
@@ -157,10 +192,10 @@ The extension makes no autocomplete request while disabled. Automatic empty-draf
157
192
  ## Compatibility and editor ownership
158
193
 
159
194
  - Supported baseline: Pi `0.80.6`, Node.js `22.19.0` or newer.
160
- - Every release is verified against Pi `0.80.6`, `0.83.0`, and `1.2.0`. Pi 1.x draws the editor cursor through fake-cursor markers instead of inverse video; ghost text supports both forms.
195
+ - Every release is verified against Pi `0.80.6`, `0.83.0`, `1.2.0`, and `1.5.2`. Pi 1.x draws the editor cursor through fake-cursor markers instead of inverse video; ghost text supports both forms.
161
196
  - On hosts that expose Pi's model-visible session projection (Pi 1.x), autocomplete context follows it: a message that `context_edit` omitted or replaced, for example to redact it, is omitted or replaced in autocomplete requests too. If that projection fails, requests carry no conversation context rather than the raw history. Besides user and assistant message text, autocomplete sends only the newest compaction summary, so suggestions keep the summarized background after a compaction; custom messages, tool results, and the compaction's system checkpoint are not sent.
162
- - Interactive ghost text requires Pi's TUI mode. RPC, JSON, and print modes do not install a custom editor and never reach a provider.
163
- - The read-only commands `status`, `stats`, and `inspect` also work on hosts that can show notifications, such as RPC; there `status` reports `host=non-interactive` and `editor=unavailable`. Every command that changes state or touches the editor stays TUI-only. If the extension is enabled on such a host, it says once per process that no autocomplete request will be issued.
197
+ - Interactive ghost text requires Pi's TUI mode. RPC, JSON, and print modes do not install a custom editor and never reach a provider, except RPC [draft completion](#draft-completion-for-rpc-hosts) and [`--prompt-autocomplete-next-prompt`](#next-prompt-for-rpc-hosts-t3-code).
198
+ - The read-only commands `status`, `stats`, and `inspect` also work on hosts that can show notifications, such as RPC; there `status` reports `host=non-interactive` and `editor=unavailable`. Except for RPC draft completion, every command that changes state or touches the editor stays TUI-only. If the extension is enabled on such a host, it says once per process that no autocomplete request will be issued.
164
199
  - Forks of Pi's extension API whose `ExtensionContext` predates `mode`, such as [prime-agent](https://www.npmjs.com/package/prime-agent), are supported. For those hosts the extension first requires UI availability and a custom-editor slot, and then verifies that the host actually installed the editor. A front-end that accepts an editor factory without installing it, as forked RPC and daemon modes do, is detected on the first attempt and stays inactive: it is not retried, not left enabled, and can neither render ghost text nor issue a provider request.
165
200
  - On Pi 1.x, requests go through the session's model registry, which resolves each provider's stored credential itself. Models from providers that other extensions register therefore work as well, as do OAuth credentials that derive their own endpoint (such as GitHub Copilot Enterprise) and header-only or ambient credentials. Older hosts use the simple completion API below and need an API key.
166
201
  - The simple completion API is imported from the `@earendil-works/pi-ai` root specifier, which Pi maps to its compat entrypoint and such forks export directly. If a host exposes that module without `streamSimple`, the extension uses the completion path instead of failing requests.