@kliebhan/pi-prompt-autocomplete 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +3 -1
- package/core.ts +51 -0
- package/index.ts +128 -18
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
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.1] - 2026-08-06
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Load the simple completion API from the `@earendil-works/pi-ai` root specifier, which Pi maps to its compat entrypoint and forked hosts export directly. Hosts that never mapped the `@earendil-works/pi-ai/compat` subpath, such as prime-agent, can now load the extension at all.
|
|
10
|
+
- Detect the interactive editor host without requiring `ExtensionContext.mode`. Hosts that report a mode keep the previous behaviour, so Pi's RPC, JSON, and print runs stay excluded unchanged. Hosts built on an older extension API are treated as candidates only when they report UI availability and expose a custom-editor slot, and the installation is then verified: a host that accepts an editor factory without installing it — such as a forked headless or daemon front-end — is detected on the first attempt, is never retried, is not left enabled, and can neither render ghost text nor issue a provider request. This restores `/prompt-autocomplete` commands and ghost text on prime-agent.
|
|
11
|
+
|
|
5
12
|
## [0.2.0] - 2026-07-31
|
|
6
13
|
|
|
7
14
|
### Added
|
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.
|
|
5
|
+
[Watch the demo](https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.1/prompt-autocomplete-demo.mp4)
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -118,6 +118,8 @@ The extension makes no autocomplete request while disabled. Automatic empty-draf
|
|
|
118
118
|
|
|
119
119
|
- Supported baseline: Pi `0.80.6`, Node.js `22.19.0` or newer.
|
|
120
120
|
- Interactive ghost text requires Pi's TUI mode. RPC, JSON, and print modes do not install a custom editor.
|
|
121
|
+
- 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.
|
|
122
|
+
- 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.
|
|
121
123
|
- Prompt Autocomplete requires exclusive ownership of Pi's custom-editor slot. It refuses to replace another custom editor and never removes a later replacement editor.
|
|
122
124
|
|
|
123
125
|
## Development
|
package/core.ts
CHANGED
|
@@ -1629,3 +1629,54 @@ export function formatModelLabel(model: Model<Api> | undefined): string {
|
|
|
1629
1629
|
if (!model) return "none";
|
|
1630
1630
|
return `${model.provider}/${model.id}`;
|
|
1631
1631
|
}
|
|
1632
|
+
|
|
1633
|
+
/**
|
|
1634
|
+
* Host capabilities needed to decide whether an interactive editor slot exists.
|
|
1635
|
+
*
|
|
1636
|
+
* Pi exposes `ExtensionContext.mode`. Forks of the Pi extension API (for
|
|
1637
|
+
* example prime-agent, which is based on an older API revision) ship
|
|
1638
|
+
* `hasUI` without `mode`.
|
|
1639
|
+
*/
|
|
1640
|
+
export interface EditorHostCapabilities {
|
|
1641
|
+
/** `ExtensionContext.mode` when the host provides it. */
|
|
1642
|
+
mode?: unknown;
|
|
1643
|
+
/** `ExtensionContext.hasUI` when the host provides it. */
|
|
1644
|
+
hasUI?: unknown;
|
|
1645
|
+
/** Whether the host exposes a usable custom-editor slot. */
|
|
1646
|
+
canInstallEditor?: unknown;
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
/**
|
|
1650
|
+
* Decide whether the host runs an interactive terminal editor.
|
|
1651
|
+
*
|
|
1652
|
+
* A host that reports `mode` is authoritative: only `"tui"` may own the editor,
|
|
1653
|
+
* so `rpc`, `json`, and `print` stay excluded exactly as before. Only a host
|
|
1654
|
+
* that omits `mode` entirely is treated as a forked API revision, and then only
|
|
1655
|
+
* when it reports UI availability *and* exposes the custom-editor slot. Any
|
|
1656
|
+
* other reported value, including `null`, fails closed.
|
|
1657
|
+
*
|
|
1658
|
+
* Capabilities alone cannot prove that a host really installs custom editors:
|
|
1659
|
+
* some forks expose a no-op editor slot in headless front-ends. The caller must
|
|
1660
|
+
* still verify the installation after mounting.
|
|
1661
|
+
*/
|
|
1662
|
+
export function isInteractiveEditorHost(host: EditorHostCapabilities): boolean {
|
|
1663
|
+
if (typeof host.mode === "string") return host.mode === "tui";
|
|
1664
|
+
if (host.mode !== undefined) return false;
|
|
1665
|
+
return host.hasUI === true && host.canInstallEditor === true;
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
/**
|
|
1669
|
+
* Whether streamed responses can be requested from the host's pi-ai module.
|
|
1670
|
+
*
|
|
1671
|
+
* A host that exposes the simple completion API without `streamSimple` must use
|
|
1672
|
+
* the completion path instead of failing every request. When the module exposes
|
|
1673
|
+
* neither function the extension is not running inside a host that maps them,
|
|
1674
|
+
* so streaming support stays unknown and is reported only once a request runs.
|
|
1675
|
+
*/
|
|
1676
|
+
export function hostSupportsStreamedResponses(api: {
|
|
1677
|
+
completeSimple?: unknown;
|
|
1678
|
+
streamSimple?: unknown;
|
|
1679
|
+
}): boolean {
|
|
1680
|
+
if (typeof api.streamSimple === "function") return true;
|
|
1681
|
+
return typeof api.completeSimple !== "function";
|
|
1682
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "@earendil-works/pi-ai/compat";
|
|
1
|
+
import * as piAi from "@earendil-works/pi-ai";
|
|
2
|
+
import type {
|
|
3
|
+
Api,
|
|
4
|
+
AssistantMessage,
|
|
5
|
+
AssistantMessageEventStream,
|
|
6
|
+
Model,
|
|
7
|
+
UserMessage,
|
|
8
|
+
} from "@earendil-works/pi-ai";
|
|
10
9
|
import { CustomEditor, type ExtensionAPI, type ExtensionContext, type KeybindingsManager } from "@earendil-works/pi-coding-agent";
|
|
11
10
|
import { matchesKey, truncateToWidth, type EditorTheme, type KeyId, type TUI, visibleWidth } from "@earendil-works/pi-tui";
|
|
12
11
|
import {
|
|
@@ -33,6 +32,8 @@ import {
|
|
|
33
32
|
formatModelLabel,
|
|
34
33
|
formatPromptAutocompleteStats,
|
|
35
34
|
formatUsageStats,
|
|
35
|
+
hostSupportsStreamedResponses,
|
|
36
|
+
isInteractiveEditorHost,
|
|
36
37
|
MAX_DRAFT_CONTEXT_CHARS,
|
|
37
38
|
normalizePromptSuggestions,
|
|
38
39
|
parseBoundedIntFlag,
|
|
@@ -148,8 +149,52 @@ interface EditorMountState {
|
|
|
148
149
|
installedFactory: EditorFactory;
|
|
149
150
|
}
|
|
150
151
|
|
|
151
|
-
type CompleteSimpleFunction = typeof completeSimple;
|
|
152
|
-
type StreamSimpleFunction = typeof streamSimple;
|
|
152
|
+
type CompleteSimpleFunction = typeof import("@earendil-works/pi-ai/compat").completeSimple;
|
|
153
|
+
type StreamSimpleFunction = typeof import("@earendil-works/pi-ai/compat").streamSimple;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Host-neutral access to the simple completion API.
|
|
157
|
+
*
|
|
158
|
+
* Pi aliases the pi-ai root specifier to its compat entrypoint for extensions,
|
|
159
|
+
* and forked hosts based on an older API revision (for example prime-agent)
|
|
160
|
+
* export the same functions from the pi-ai root module. Importing the root
|
|
161
|
+
* specifier therefore works on both, while importing `@earendil-works/pi-ai/compat`
|
|
162
|
+
* fails to resolve on hosts that never mapped that subpath.
|
|
163
|
+
*/
|
|
164
|
+
const piAiSimpleApi = piAi as unknown as {
|
|
165
|
+
completeSimple?: CompleteSimpleFunction;
|
|
166
|
+
streamSimple?: StreamSimpleFunction;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
type SimpleApiFunction = CompleteSimpleFunction | StreamSimpleFunction;
|
|
170
|
+
|
|
171
|
+
function requireSimpleApiFunction<F extends SimpleApiFunction>(
|
|
172
|
+
name: "completeSimple" | "streamSimple",
|
|
173
|
+
impl: F | undefined,
|
|
174
|
+
): F {
|
|
175
|
+
if (typeof impl !== "function") {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`prompt-autocomplete requires ${name}() from @earendil-works/pi-ai; this host does not provide it`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return impl;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Resolved on first use: plain Node test runs resolve the real pi-ai root,
|
|
184
|
+
// which only exposes these functions when the host maps them for extensions.
|
|
185
|
+
const hostCompleteSimple = ((model, context, options) =>
|
|
186
|
+
requireSimpleApiFunction("completeSimple", piAiSimpleApi.completeSimple)(
|
|
187
|
+
model,
|
|
188
|
+
context,
|
|
189
|
+
options,
|
|
190
|
+
)) as CompleteSimpleFunction;
|
|
191
|
+
|
|
192
|
+
const hostStreamSimple = ((model, context, options) =>
|
|
193
|
+
requireSimpleApiFunction("streamSimple", piAiSimpleApi.streamSimple)(
|
|
194
|
+
model,
|
|
195
|
+
context,
|
|
196
|
+
options,
|
|
197
|
+
)) as StreamSimpleFunction;
|
|
153
198
|
|
|
154
199
|
export interface PromptAutocompleteDependencies {
|
|
155
200
|
completeSimple?: CompleteSimpleFunction;
|
|
@@ -175,6 +220,14 @@ interface PromptAutocompleteSharedState {
|
|
|
175
220
|
debugState: string;
|
|
176
221
|
editorMount?: EditorMountState;
|
|
177
222
|
editorBlockedReason?: string;
|
|
223
|
+
/**
|
|
224
|
+
* Set once a mount attempt proved whether this host installs custom editors.
|
|
225
|
+
*
|
|
226
|
+
* A process cannot change its front-end, so this is deliberately never reset:
|
|
227
|
+
* a host that dropped the editor factory once would drop it again. Restarting
|
|
228
|
+
* the host is the only way to re-probe.
|
|
229
|
+
*/
|
|
230
|
+
hostInstallsEditors?: boolean;
|
|
178
231
|
lastError?: string;
|
|
179
232
|
lastRawResponse?: string;
|
|
180
233
|
requestCache: ExpiringLruCache<PromptAutocompleteCacheEntry>;
|
|
@@ -1322,8 +1375,28 @@ function releaseEditorRuntime(shared: PromptAutocompleteSharedState): void {
|
|
|
1322
1375
|
shared.ownsEditor = undefined;
|
|
1323
1376
|
}
|
|
1324
1377
|
|
|
1378
|
+
/**
|
|
1379
|
+
* Whether this host can hand the prompt editor to the extension.
|
|
1380
|
+
*
|
|
1381
|
+
* Hosts that report `ctx.mode` stay authoritative (only `"tui"` owns the
|
|
1382
|
+
* editor). Hosts from older forks of the extension API omit `mode` entirely, so
|
|
1383
|
+
* UI availability plus a usable editor slot decides instead.
|
|
1384
|
+
*/
|
|
1385
|
+
function hostOwnsInteractiveEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1386
|
+
// A host that already accepted an editor factory without installing it is not
|
|
1387
|
+
// interactive, no matter what its capabilities claim.
|
|
1388
|
+
if (shared.hostInstallsEditors === false) return false;
|
|
1389
|
+
const host = ctx as Partial<Pick<ExtensionContext, "mode" | "hasUI" | "ui">>;
|
|
1390
|
+
return isInteractiveEditorHost({
|
|
1391
|
+
mode: host.mode,
|
|
1392
|
+
hasUI: host.hasUI,
|
|
1393
|
+
canInstallEditor:
|
|
1394
|
+
typeof host.ui?.setEditorComponent === "function" && typeof host.ui?.getEditorComponent === "function",
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1325
1398
|
function mountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1326
|
-
if (ctx
|
|
1399
|
+
if (!hostOwnsInteractiveEditor(ctx, shared)) return false;
|
|
1327
1400
|
|
|
1328
1401
|
if (shared.editorMount) {
|
|
1329
1402
|
if (shared.ownsEditor?.()) return true;
|
|
@@ -1420,13 +1493,33 @@ function mountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedStat
|
|
|
1420
1493
|
throw error;
|
|
1421
1494
|
}
|
|
1422
1495
|
|
|
1496
|
+
// Some hosts accept the call without installing anything: forked front-ends
|
|
1497
|
+
// (for example prime-agent's RPC mode) expose a no-op editor slot while still
|
|
1498
|
+
// reporting UI availability. Verify the installation instead of trusting it.
|
|
1499
|
+
if (ctx.ui.getEditorComponent() !== installedFactory) {
|
|
1500
|
+
releaseEditorRuntime(shared);
|
|
1501
|
+
shared.editorMount = undefined;
|
|
1502
|
+
shared.hostInstallsEditors = false;
|
|
1503
|
+
// Nothing can render or request here, so the extension must not stay marked
|
|
1504
|
+
// enabled, no matter which path attempted the mount.
|
|
1505
|
+
shared.enabled = false;
|
|
1506
|
+
const reason = "This host does not install custom editors; prompt autocomplete stays inactive";
|
|
1507
|
+
const shouldNotify = shared.editorBlockedReason !== reason;
|
|
1508
|
+
shared.editorBlockedReason = reason;
|
|
1509
|
+
updateDebugState(shared, "blocked-no-editor-slot", reason);
|
|
1510
|
+
if (shouldNotify) ctx.ui.notify(reason, "warning");
|
|
1511
|
+
return false;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
shared.hostInstallsEditors = true;
|
|
1423
1515
|
updateDebugState(shared, "mounted", "Editor extension attached");
|
|
1424
1516
|
return true;
|
|
1425
1517
|
}
|
|
1426
1518
|
|
|
1427
1519
|
function unmountEditor(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): void {
|
|
1428
1520
|
const mount = shared.editorMount;
|
|
1429
|
-
const shouldRestore =
|
|
1521
|
+
const shouldRestore =
|
|
1522
|
+
hostOwnsInteractiveEditor(ctx, shared) && !!mount && ctx.ui.getEditorComponent() === mount.installedFactory;
|
|
1430
1523
|
|
|
1431
1524
|
releaseEditorRuntime(shared);
|
|
1432
1525
|
shared.editorMount = undefined;
|
|
@@ -1441,6 +1534,9 @@ function resetSharedForSession(pi: ExtensionAPI, shared: PromptAutocompleteShare
|
|
|
1441
1534
|
shared.cancelActiveRequest?.();
|
|
1442
1535
|
shared.cancelActiveRequest = undefined;
|
|
1443
1536
|
applyEffectiveConfig(pi, shared);
|
|
1537
|
+
// A host that never installs custom editors stays inactive for the rest of
|
|
1538
|
+
// the process, so flags must not re-enable it in a later session either.
|
|
1539
|
+
if (shared.hostInstallsEditors === false) shared.enabled = false;
|
|
1444
1540
|
shared.usageStats = createPromptAutocompleteUsageStats();
|
|
1445
1541
|
shared.editorMount = undefined;
|
|
1446
1542
|
shared.editorBlockedReason = undefined;
|
|
@@ -1540,9 +1636,20 @@ function setStreamResponses(
|
|
|
1540
1636
|
}
|
|
1541
1637
|
|
|
1542
1638
|
function enablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): boolean {
|
|
1639
|
+
const previousOverride = shared.runtimeOverrides.enabled;
|
|
1543
1640
|
shared.enabled = true;
|
|
1544
1641
|
shared.runtimeOverrides.enabled = true;
|
|
1545
|
-
|
|
1642
|
+
|
|
1643
|
+
if (!mountEditor(ctx, shared) && shared.hostInstallsEditors === false) {
|
|
1644
|
+
// The host accepted the editor factory without installing it. mountEditor
|
|
1645
|
+
// already cleared `enabled`; keep it cleared and drop the session override
|
|
1646
|
+
// so the refused activation is not replayed as a user decision later.
|
|
1647
|
+
shared.enabled = false;
|
|
1648
|
+
shared.runtimeOverrides.enabled = previousOverride;
|
|
1649
|
+
return false;
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
return shared.editorMount !== undefined;
|
|
1546
1653
|
}
|
|
1547
1654
|
|
|
1548
1655
|
function disablePromptAutocomplete(ctx: ExtensionContext, shared: PromptAutocompleteSharedState): void {
|
|
@@ -1654,10 +1761,13 @@ function createPromptAutocompleteCommandHandlers(
|
|
|
1654
1761
|
export function createPromptAutocompleteExtension(
|
|
1655
1762
|
dependencies: PromptAutocompleteDependencies = {},
|
|
1656
1763
|
): (pi: ExtensionAPI) => void {
|
|
1657
|
-
const completeSimpleImpl = dependencies.completeSimple ??
|
|
1764
|
+
const completeSimpleImpl = dependencies.completeSimple ?? hostCompleteSimple;
|
|
1765
|
+
const hostStreaming = hostSupportsStreamedResponses(piAiSimpleApi);
|
|
1658
1766
|
// Existing deterministic harnesses inject only completeSimple. Never let such
|
|
1659
1767
|
// a harness fall through to the real network-capable stream implementation.
|
|
1660
|
-
const streamSimpleImpl =
|
|
1768
|
+
const streamSimpleImpl =
|
|
1769
|
+
dependencies.streamSimple
|
|
1770
|
+
?? (dependencies.completeSimple || !hostStreaming ? undefined : hostStreamSimple);
|
|
1661
1771
|
const now = dependencies.now ?? Date.now;
|
|
1662
1772
|
|
|
1663
1773
|
return function promptAutocompleteExtension(pi: ExtensionAPI): void {
|
|
@@ -1732,7 +1842,7 @@ export function createPromptAutocompleteExtension(
|
|
|
1732
1842
|
resetSharedForSession(pi, shared);
|
|
1733
1843
|
bindRuntimeContext(ctx, shared);
|
|
1734
1844
|
|
|
1735
|
-
if (ctx
|
|
1845
|
+
if (!hostOwnsInteractiveEditor(ctx, shared) || !shared.enabled) return;
|
|
1736
1846
|
mountEditor(ctx, shared);
|
|
1737
1847
|
});
|
|
1738
1848
|
|
|
@@ -1774,7 +1884,7 @@ export function createPromptAutocompleteExtension(
|
|
|
1774
1884
|
pi.registerCommand("prompt-autocomplete", {
|
|
1775
1885
|
description: "Enable, disable, or inspect inline prompt autocomplete and session stats",
|
|
1776
1886
|
handler: async (args, ctx) => {
|
|
1777
|
-
if (ctx
|
|
1887
|
+
if (!hostOwnsInteractiveEditor(ctx, shared)) {
|
|
1778
1888
|
ctx.ui.notify("prompt-autocomplete requires interactive TUI mode", "warning");
|
|
1779
1889
|
return;
|
|
1780
1890
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kliebhan/pi-prompt-autocomplete",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
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.
|
|
29
|
+
"video": "https://github.com/KLIEBHAN/pi-extensions/releases/download/pi-prompt-autocomplete-v0.2.1/prompt-autocomplete-demo.mp4"
|
|
30
30
|
},
|
|
31
31
|
"engines": {
|
|
32
32
|
"node": ">=22.19.0"
|