pi-autosuggestions 0.4.0 → 0.6.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.
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +14 -0
- package/README.md +38 -7
- package/extensions/autosuggestions.ts +407 -19
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{".":"0.
|
|
1
|
+
{".":"0.6.0"}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.6.0](https://github.com/0xPlayerOne/pi-autosuggestions/compare/pi-autosuggestions-v0.5.0...pi-autosuggestions-v0.6.0) (2026-09-05)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* cross-session history, candidate cycling, theme-aware cursor, new dynamic sources ([71fe236](https://github.com/0xPlayerOne/pi-autosuggestions/commit/71fe236ac272d54432da96aa35e53a919102f657))
|
|
9
|
+
|
|
10
|
+
## [0.5.0](https://github.com/0xPlayerOne/pi-autosuggestions/compare/pi-autosuggestions-v0.4.0...pi-autosuggestions-v0.5.0) (2026-09-04)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* dynamic completions and expanded command tables ([4e99186](https://github.com/0xPlayerOne/pi-autosuggestions/commit/4e991868049694c1f973bc8ab2069af433c8bf4c))
|
|
16
|
+
|
|
3
17
|
## [0.4.0](https://github.com/0xPlayerOne/pi-autosuggestions/compare/pi-autosuggestions-v0.3.3...pi-autosuggestions-v0.4.0) (2026-09-04)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -23,6 +23,12 @@ pi install git:github.com/0xPlayerOne/pi-autosuggestions@pi-autosuggestions-v0.2
|
|
|
23
23
|
pi install npm:pi-autosuggestions
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
## Configuration
|
|
27
|
+
|
|
28
|
+
`/autosuggest` — toggle cross-session history for ghost suggestions (stored
|
|
29
|
+
in `~/.pi/agent/pi-autosuggestions.json`; applies from the next session).
|
|
30
|
+
Default: off (history is session-scoped).
|
|
31
|
+
|
|
26
32
|
## Scope
|
|
27
33
|
|
|
28
34
|
| Layer | Where it works |
|
|
@@ -32,11 +38,19 @@ pi install npm:pi-autosuggestions
|
|
|
32
38
|
| Command-name completion | **Bash mode only**, first word after `!` |
|
|
33
39
|
| Beam cursor | **Everywhere** |
|
|
34
40
|
|
|
35
|
-
History is **session-scoped**: the ghost
|
|
36
|
-
submitted
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
History is **session-scoped by default**: the ghost suggests prompts you
|
|
42
|
+
submitted in the current session. Opt into **cross-session history** with
|
|
43
|
+
`/autosuggest` — the extension then seeds suggestions from your previous
|
|
44
|
+
pi sessions in the same working directory (read from pi's own session files;
|
|
45
|
+
nothing leaves your machine). If you mostly run `!` commands, the ghost will
|
|
46
|
+
mostly appear in bash mode — it isn't restricted there, it just has nothing
|
|
47
|
+
to suggest elsewhere.
|
|
48
|
+
|
|
49
|
+
**Candidate cycling**: with multiple history matches, `Alt+Down` / `Alt+Up`
|
|
50
|
+
rotate the ghost through the alternatives (most recent first).
|
|
51
|
+
|
|
52
|
+
The cursor colors follow the **pi theme** (accent while "on", dim while
|
|
53
|
+
"off"), so they stay visible on light and dark themes.
|
|
40
54
|
|
|
41
55
|
## Features
|
|
42
56
|
|
|
@@ -64,8 +78,25 @@ subcommand table instead of the filesystem:
|
|
|
64
78
|
| `!git zz` | no subcommand match → falls back to path completion |
|
|
65
79
|
|
|
66
80
|
Covered commands: `git`, `npm`, `pnpm`, `yarn`, `bun`, `docker`, `kubectl`,
|
|
67
|
-
`cargo`, `brew`, `gh
|
|
68
|
-
|
|
81
|
+
`cargo`, `brew`, `gh`, `go`, `uv`, `pip`, `poetry`, `terraform`, `helm`,
|
|
82
|
+
`wrangler`, `vercel`, `mise`, `asdf`, `systemctl`, `apt`, `pacman`.
|
|
83
|
+
Deeper arguments (flags, file args) still complete from history or the
|
|
84
|
+
filesystem.
|
|
85
|
+
|
|
86
|
+
### Dynamic completions (bash mode)
|
|
87
|
+
|
|
88
|
+
Some completions are **live** — the extension reads your project instead of a
|
|
89
|
+
static table (results cached for 10s):
|
|
90
|
+
|
|
91
|
+
| Input | Source |
|
|
92
|
+
|-------|--------|
|
|
93
|
+
| `!git checkout f` / `!git switch` / `!git merge` / `!git rebase` | branch names from `git branch` |
|
|
94
|
+
| `!npm run d` / `!pnpm run` / `!bun d` (implicit for yarn/pnpm/bun) | `scripts` from the nearest `package.json` |
|
|
95
|
+
| `!make d` | targets parsed from the nearest `Makefile` |
|
|
96
|
+
| `!docker compose u` | compose subcommands |
|
|
97
|
+
| `!docker start n` / `logs` / `exec` … | running container names |
|
|
98
|
+
| `!kubectl get p` | resource types |
|
|
99
|
+
| `!ssh ` / `!scp ` | hosts from `~/.ssh/config` |
|
|
69
100
|
|
|
70
101
|
### Command completion (bash mode)
|
|
71
102
|
|
|
@@ -20,7 +20,9 @@ import {
|
|
|
20
20
|
} from "@earendil-works/pi-tui";
|
|
21
21
|
|
|
22
22
|
/** Runtime access to Editor internals that are private in the type declarations. */
|
|
23
|
-
import { readdirSync, statSync } from "node:fs";
|
|
23
|
+
import { existsSync, readFileSync, realpathSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
24
|
+
import { dirname } from "node:path";
|
|
25
|
+
import { homedir } from "node:os";
|
|
24
26
|
import type { AutocompleteItem } from "@earendil-works/pi-tui";
|
|
25
27
|
|
|
26
28
|
interface EditorInternals {
|
|
@@ -34,6 +36,32 @@ interface EditorInternals {
|
|
|
34
36
|
|
|
35
37
|
const DIM = "\x1b[2m";
|
|
36
38
|
const RESET = "\x1b[0m";
|
|
39
|
+
/** Set at extension load; used for dynamic completion exec calls. */
|
|
40
|
+
let execApi: ExtensionAPI | undefined;
|
|
41
|
+
/** Active pi theme (set at session start) for theme-aware cursor colors. */
|
|
42
|
+
let themeRef: { fg(name: string, text: string): string } | undefined;
|
|
43
|
+
/** Persisted extension settings (~/.pi/agent/pi-autosuggestions.json). */
|
|
44
|
+
interface AutosuggestConfig {
|
|
45
|
+
crossSessionHistory?: boolean;
|
|
46
|
+
}
|
|
47
|
+
let config: AutosuggestConfig = {};
|
|
48
|
+
const configPath = `${homedir()}/.pi/agent/pi-autosuggestions.json`;
|
|
49
|
+
|
|
50
|
+
function loadConfig(): void {
|
|
51
|
+
try {
|
|
52
|
+
config = JSON.parse(readFileSync(configPath, "utf8"));
|
|
53
|
+
} catch {
|
|
54
|
+
config = {};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function saveConfig(): void {
|
|
59
|
+
try {
|
|
60
|
+
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
61
|
+
} catch {
|
|
62
|
+
// read-only home — settings stay in-memory
|
|
63
|
+
}
|
|
64
|
+
}
|
|
37
65
|
/** Beam cursor: thin vertical bar, bold so it stands out. One cell wide. */
|
|
38
66
|
const BEAM = "\x1b[1m▏\x1b[0m";
|
|
39
67
|
/** Matches the reverse-video block the base renderer emits for the cursor. */
|
|
@@ -103,8 +131,89 @@ const SUBCOMMANDS: Record<string, string[]> = {
|
|
|
103
131
|
"extension", "gist", "gpg-key", "issue", "label", "org", "pr", "project", "release",
|
|
104
132
|
"repo", "run", "search", "secret", "ssh-key", "status", "variable", "workflow",
|
|
105
133
|
],
|
|
134
|
+
go: [
|
|
135
|
+
"bug", "build", "clean", "doc", "env", "fix", "fmt", "generate", "get", "help",
|
|
136
|
+
"install", "list", "mod", "run", "test", "tool", "version", "vet", "work",
|
|
137
|
+
],
|
|
138
|
+
uv: [
|
|
139
|
+
"add", "build", "cache", "export", "help", "init", "lock", "pip", "publish", "python",
|
|
140
|
+
"remove", "run", "self", "sync", "tool", "tree", "venv", "version",
|
|
141
|
+
],
|
|
142
|
+
pip: [
|
|
143
|
+
"cache", "check", "config", "debug", "download", "freeze", "hash", "index", "inspect",
|
|
144
|
+
"install", "list", "show", "uninstall", "wheel",
|
|
145
|
+
],
|
|
146
|
+
poetry: [
|
|
147
|
+
"add", "build", "cache", "check", "config", "env", "export", "init", "install",
|
|
148
|
+
"list", "lock", "new", "publish", "remove", "run", "search", "self", "shell",
|
|
149
|
+
"show", "source", "update", "version",
|
|
150
|
+
],
|
|
151
|
+
terraform: [
|
|
152
|
+
"apply", "console", "destroy", "fmt", "get", "graph", "import", "init", "output",
|
|
153
|
+
"plan", "providers", "refresh", "show", "state", "taint", "test", "untaint",
|
|
154
|
+
"validate", "version", "workspace",
|
|
155
|
+
],
|
|
156
|
+
helm: [
|
|
157
|
+
"completion", "create", "dependency", "env", "get", "help", "history", "install",
|
|
158
|
+
"lint", "list", "package", "plugin", "pull", "push", "registry", "repo", "rollback",
|
|
159
|
+
"search", "show", "status", "template", "test", "uninstall", "upgrade", "verify",
|
|
160
|
+
],
|
|
161
|
+
wrangler: [
|
|
162
|
+
"ai", "d1", "delete", "deploy", "dev", "dispatch", "init", "kv", "login", "logout",
|
|
163
|
+
"logs", "r2", "rollback", "secret", "subdomain", "tail", "triggers", "types",
|
|
164
|
+
"versions", "whoami",
|
|
165
|
+
],
|
|
166
|
+
vercel: [
|
|
167
|
+
"alias", "build", "deploy", "dev", "dns", "domains", "env", "init", "inspect",
|
|
168
|
+
"link", "login", "logout", "logs", "ls", "projects", "pull", "remove", "switch",
|
|
169
|
+
"teams", "telemetry", "whoami",
|
|
170
|
+
],
|
|
171
|
+
mise: [
|
|
172
|
+
"activate", "current", "deactivate", "doctor", "exec", "global", "install", "link",
|
|
173
|
+
"list", "local", "ls", "outdated", "prune", "reshim", "run", "settings", "tasks",
|
|
174
|
+
"uninstall", "upgrade", "use", "which",
|
|
175
|
+
],
|
|
176
|
+
asdf: [
|
|
177
|
+
"current", "exec", "global", "info", "install", "latest", "list", "list-all",
|
|
178
|
+
"local", "plugin", "reshim", "shell", "uninstall", "update", "version", "where",
|
|
179
|
+
"which",
|
|
180
|
+
],
|
|
181
|
+
systemctl: [
|
|
182
|
+
"cat", "disable", "enable", "is-active", "is-enabled", "isolate", "kill", "list",
|
|
183
|
+
"list-dependencies", "list-timers", "list-units", "mask", "reload", "reload-or-restart",
|
|
184
|
+
"restart", "set-default", "show", "start", "status", "stop", "unmask",
|
|
185
|
+
],
|
|
186
|
+
apt: [
|
|
187
|
+
"autoclean", "autopurge", "autoremove", "build-dep", "cache", "check", "clean",
|
|
188
|
+
"download", "edit-sources", "full-upgrade", "help", "install", "list", "purge",
|
|
189
|
+
"reinstall", "remove", "satisfy", "search", "show", "source", "update", "upgrade",
|
|
190
|
+
],
|
|
191
|
+
pacman: [
|
|
192
|
+
"-Q", "-R", "-S", "-T", "-U", "-F", "clean", "deps", "files", "help", "query",
|
|
193
|
+
"remove", "sync", "test", "upgrade", "version",
|
|
194
|
+
],
|
|
106
195
|
};
|
|
107
196
|
|
|
197
|
+
const COMPOSE_SUBCOMMANDS = [
|
|
198
|
+
"build", "config", "create", "down", "events", "exec", "images", "kill", "logs", "ls",
|
|
199
|
+
"pause", "port", "ps", "pull", "push", "restart", "rm", "run", "start", "stop", "top",
|
|
200
|
+
"unpause", "up", "version", "wait", "watch",
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
// (SUBCOMMANDS gains more tables below — see SUBCOMMANDS_EXTRA merge point.)
|
|
204
|
+
|
|
205
|
+
const DOCKER_CONTAINER_VERBS = [
|
|
206
|
+
"start", "stop", "restart", "kill", "rm", "logs", "exec", "inspect", "stats",
|
|
207
|
+
"top", "pause", "unpause", "commit",
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
const KUBECTL_RESOURCES = [
|
|
211
|
+
"pods", "po", "deployments", "deploy", "services", "svc", "ingresses", "ing",
|
|
212
|
+
"nodes", "no", "namespaces", "ns", "configmaps", "cm", "secrets", "persistentvolumes",
|
|
213
|
+
"pv", "persistentvolumeclaims", "pvc", "events", "ev", "replicasets", "rs",
|
|
214
|
+
"statefulsets", "sts", "daemonsets", "ds", "jobs", "cronjobs", "hpa", "endpoints", "ep",
|
|
215
|
+
];
|
|
216
|
+
|
|
108
217
|
const BLINK_INTERVAL_MS = 500;
|
|
109
218
|
/** Cursor stays solid for this long after each keystroke, then resumes blinking. */
|
|
110
219
|
const SOLID_AFTER_INPUT_MS = 450;
|
|
@@ -120,6 +229,10 @@ class BashInlineEditor extends CustomEditor {
|
|
|
120
229
|
private ghostAbort?: AbortController;
|
|
121
230
|
/** Submitted prompts, oldest first — the zsh-autosuggestions history strategy. */
|
|
122
231
|
private promptHistory: string[] = [];
|
|
232
|
+
private historyLoaded = false;
|
|
233
|
+
/** History candidates for the current ghost (most recent first). */
|
|
234
|
+
private ghostCandidates: string[] = [];
|
|
235
|
+
private ghostCandidateIndex = 0;
|
|
123
236
|
/** Set by Escape; keeps the ghost dismissed until the next text edit. */
|
|
124
237
|
private ghostSuppressed = false;
|
|
125
238
|
|
|
@@ -207,6 +320,14 @@ class BashInlineEditor extends CustomEditor {
|
|
|
207
320
|
this.acceptGhostWord();
|
|
208
321
|
return;
|
|
209
322
|
}
|
|
323
|
+
if (matchesKey(data, "alt+down")) {
|
|
324
|
+
this.cycleGhostCandidate(1);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (matchesKey(data, "alt+up")) {
|
|
328
|
+
this.cycleGhostCandidate(-1);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
210
331
|
}
|
|
211
332
|
super.handleInput(data);
|
|
212
333
|
if (matchesKey(data, "escape")) {
|
|
@@ -314,6 +435,15 @@ class BashInlineEditor extends CustomEditor {
|
|
|
314
435
|
return st.cursorCol === (st.lines[st.cursorLine] ?? "").length;
|
|
315
436
|
}
|
|
316
437
|
|
|
438
|
+
/** Theme-aware styles; fall back to white/dim when no theme is set. */
|
|
439
|
+
private bright(text: string): string {
|
|
440
|
+
return themeRef ? themeRef.fg("accent", text) : `${CURSOR_COLOR}${text}${RESET}`;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private dimmed(text: string): string {
|
|
444
|
+
return themeRef ? themeRef.fg("dim", text) : `${DIM}${text}${RESET}`;
|
|
445
|
+
}
|
|
446
|
+
|
|
317
447
|
/** Suppress the dropdown in bash mode unless the popup is already open. */
|
|
318
448
|
|
|
319
449
|
/**
|
|
@@ -377,6 +507,152 @@ class BashInlineEditor extends CustomEditor {
|
|
|
377
507
|
.slice(0, 100);
|
|
378
508
|
}
|
|
379
509
|
|
|
510
|
+
private dynCache = new Map<string, { at: number; data: string[] }>();
|
|
511
|
+
|
|
512
|
+
private cached(key: string, ttl: number, fetch: () => Promise<string[]>): Promise<string[]> {
|
|
513
|
+
const hit = this.dynCache.get(key);
|
|
514
|
+
if (hit && Date.now() - hit.at < ttl) {
|
|
515
|
+
return hit.data;
|
|
516
|
+
}
|
|
517
|
+
return fetch()
|
|
518
|
+
.then((data) => {
|
|
519
|
+
this.dynCache.set(key, { at: Date.now(), data });
|
|
520
|
+
return data;
|
|
521
|
+
})
|
|
522
|
+
.catch(() => []);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private async runCommand(cmd: string, args: string[], timeout = 4000): Promise<string[]> {
|
|
526
|
+
if (!execApi) {
|
|
527
|
+
return [];
|
|
528
|
+
}
|
|
529
|
+
const result = await execApi.exec(cmd, args, { cwd: process.cwd(), timeout });
|
|
530
|
+
if (result.code !== 0) {
|
|
531
|
+
return [];
|
|
532
|
+
}
|
|
533
|
+
return result.stdout.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
private findUp(name: string): string | null {
|
|
537
|
+
let dir = process.cwd();
|
|
538
|
+
for (;;) {
|
|
539
|
+
const candidate = `${dir}/${name}`;
|
|
540
|
+
if (existsSync(candidate)) {
|
|
541
|
+
return candidate;
|
|
542
|
+
}
|
|
543
|
+
const parent = dirname(dir);
|
|
544
|
+
if (parent === dir) {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
dir = parent;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
private gitBranches(): Promise<string[]> {
|
|
552
|
+
return this.cached("git:branches", 10_000, async () =>
|
|
553
|
+
(await this.runCommand("git", ["branch", "--list"]))
|
|
554
|
+
.map((l) => l.replace(/^\*?\s*/, ""))
|
|
555
|
+
.filter((l) => l && !l.includes(" ")));
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
private npmScripts(): Promise<string[]> {
|
|
559
|
+
return this.cached("npm:scripts", 10_000, async () => {
|
|
560
|
+
const pkg = this.findUp("package.json");
|
|
561
|
+
if (!pkg) {
|
|
562
|
+
return [];
|
|
563
|
+
}
|
|
564
|
+
const parsed: { scripts?: Record<string, string> } = JSON.parse(readFileSync(pkg, "utf8"));
|
|
565
|
+
return Object.keys(parsed.scripts ?? {});
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
private makeTargets(): Promise<string[]> {
|
|
570
|
+
return this.cached("make:targets", 10_000, async () => {
|
|
571
|
+
const file = this.findUp("Makefile") ?? this.findUp("makefile");
|
|
572
|
+
if (!file) {
|
|
573
|
+
return [];
|
|
574
|
+
}
|
|
575
|
+
const targets = new Set<string>();
|
|
576
|
+
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
577
|
+
const m = /^([a-zA-Z0-9][^\s=$#]*(?:\s+[a-zA-Z0-9][^\s=$#]*)?):(?!=)/.exec(line);
|
|
578
|
+
if (m) {
|
|
579
|
+
for (const t of m[1]!.trim().split(/\s+/)) {
|
|
580
|
+
targets.add(t);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return [...targets];
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** Live sources: git branches, package scripts, make targets, compose. */
|
|
589
|
+
private sshHosts(): Promise<string[]> {
|
|
590
|
+
return this.cached("ssh:hosts", 60_000, async () => {
|
|
591
|
+
const cfg = `${homedir()}/.ssh/config`;
|
|
592
|
+
if (!existsSync(cfg)) {
|
|
593
|
+
return [];
|
|
594
|
+
}
|
|
595
|
+
const hosts: string[] = [];
|
|
596
|
+
for (const lineText of readFileSync(cfg, "utf8").split("\n")) {
|
|
597
|
+
const m = /^\s*[Hh]ost\s+(.+)$/.exec(lineText);
|
|
598
|
+
if (m) {
|
|
599
|
+
for (const host of m[1]!.trim().split(/\s+/)) {
|
|
600
|
+
if (!host.includes("*") && !hosts.includes(host)) {
|
|
601
|
+
hosts.push(host);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return hosts;
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
private dockerContainers(): Promise<string[]> {
|
|
611
|
+
return this.cached("docker:containers", 10_000, async () =>
|
|
612
|
+
await this.runCommand("docker", ["ps", "--format", "{{.Names}}"]));
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
private async dynamicCompletions(
|
|
616
|
+
cmd: string,
|
|
617
|
+
args: string[],
|
|
618
|
+
token: string,
|
|
619
|
+
): Promise<{ items: string[]; description: string } | null> {
|
|
620
|
+
let items: string[] | null = null;
|
|
621
|
+
let description = "";
|
|
622
|
+
if (cmd === "git" && args.length === 1 && ["checkout", "switch", "merge", "rebase"].includes(args[0]!)) {
|
|
623
|
+
items = (await this.gitBranches()).filter((b) => b.startsWith(token));
|
|
624
|
+
description = "branch";
|
|
625
|
+
} else if (cmd === "git" && args[0] === "branch" && args.length === 1) {
|
|
626
|
+
items = (await this.gitBranches()).filter((b) => b.startsWith(token));
|
|
627
|
+
description = "branch";
|
|
628
|
+
} else if (
|
|
629
|
+
["npm", "pnpm", "yarn", "bun"].includes(cmd) &&
|
|
630
|
+
((args[0] === "run" && args.length === 1) || (cmd !== "npm" && args.length === 0))
|
|
631
|
+
) {
|
|
632
|
+
items = (await this.npmScripts()).filter((sc) => sc.startsWith(token));
|
|
633
|
+
description = "script";
|
|
634
|
+
} else if ((cmd === "make" || cmd === "gmake") && args.length === 0) {
|
|
635
|
+
items = (await this.makeTargets()).filter((t) => t.startsWith(token));
|
|
636
|
+
description = "make target";
|
|
637
|
+
} else if (cmd === "docker" && args[0] === "compose" && args.length === 1) {
|
|
638
|
+
items = COMPOSE_SUBCOMMANDS.filter((sc) => sc.startsWith(token));
|
|
639
|
+
description = "compose subcommand";
|
|
640
|
+
} else if (cmd === "docker" && args.length === 1 && DOCKER_CONTAINER_VERBS.includes(args[0]!)) {
|
|
641
|
+
items = (await this.dockerContainers()).filter((c) => c.startsWith(token));
|
|
642
|
+
description = "container";
|
|
643
|
+
} else if (cmd === "kubectl" && args[0] === "get" && args.length === 1) {
|
|
644
|
+
items = KUBECTL_RESOURCES.filter((r) => r.startsWith(token));
|
|
645
|
+
description = "resource";
|
|
646
|
+
} else if (["ssh", "scp"].includes(cmd) && args.length === 0) {
|
|
647
|
+
items = (await this.sshHosts()).filter((h) => h.startsWith(token));
|
|
648
|
+
description = "host";
|
|
649
|
+
}
|
|
650
|
+
if (!items || items.length === 0) {
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
return { items: items.slice(0, 50), description };
|
|
654
|
+
}
|
|
655
|
+
|
|
380
656
|
private createBashProvider(current: AutocompleteProvider): AutocompleteProvider {
|
|
381
657
|
const self = this;
|
|
382
658
|
return {
|
|
@@ -392,21 +668,32 @@ class BashInlineEditor extends CustomEditor {
|
|
|
392
668
|
const cmd = arg[1]!;
|
|
393
669
|
const token = arg[2] ?? "";
|
|
394
670
|
const subs = SUBCOMMANDS[cmd];
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
671
|
+
const staticMatches = (subs ?? []).filter((sc) => sc.startsWith(token)).slice(0, 50);
|
|
672
|
+
const dynamic = await self.dynamicCompletions(cmd, [], token);
|
|
673
|
+
const dynamicItems = dynamic?.items.filter((d) => !staticMatches.includes(d)) ?? [];
|
|
674
|
+
if (staticMatches.length > 0 || dynamicItems.length > 0) {
|
|
675
|
+
return {
|
|
676
|
+
items: [
|
|
677
|
+
...staticMatches.map((sc) => ({ value: sc, label: sc, description: `${cmd} subcommand` })),
|
|
678
|
+
...dynamicItems.map((sc) => ({ value: sc, label: sc, description: dynamic!.description })),
|
|
679
|
+
],
|
|
680
|
+
prefix: token,
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
// Nothing matched: fall through to paths.
|
|
684
|
+
}
|
|
685
|
+
// Second-argument dynamic completion (branches, scripts, compose…).
|
|
686
|
+
const words = before.replace(/^[\t ]*!+/, "").split(/[ \t]+/);
|
|
687
|
+
if (words.length >= 2) {
|
|
688
|
+
const cmd = words[0]!;
|
|
689
|
+
const token = words[words.length - 1]!;
|
|
690
|
+
const args = words.slice(1, -1).filter(Boolean);
|
|
691
|
+
const dyn = await self.dynamicCompletions(cmd, args, token);
|
|
692
|
+
if (dyn && dyn.items.length > 0) {
|
|
693
|
+
return {
|
|
694
|
+
items: dyn.items.map((sc) => ({ value: sc, label: sc, description: dyn.description })),
|
|
695
|
+
prefix: token,
|
|
696
|
+
};
|
|
410
697
|
}
|
|
411
698
|
}
|
|
412
699
|
let queryLines = lines;
|
|
@@ -489,22 +776,103 @@ class BashInlineEditor extends CustomEditor {
|
|
|
489
776
|
|
|
490
777
|
/** Most recent history entry extending the current line (history strategy). */
|
|
491
778
|
private historyGhost(): Ghost | null {
|
|
779
|
+
this.loadCrossSessionHistory();
|
|
492
780
|
const st = (this as unknown as EditorInternals).state;
|
|
493
781
|
const line = st.lines[st.cursorLine] ?? "";
|
|
494
782
|
const typed = line.slice(0, st.cursorCol);
|
|
495
783
|
if (!typed.trim()) {
|
|
496
784
|
return null;
|
|
497
785
|
}
|
|
786
|
+
const matches: string[] = [];
|
|
498
787
|
for (let i = this.promptHistory.length - 1; i >= 0; i--) {
|
|
499
788
|
const entry = this.promptHistory[i] ?? "";
|
|
500
789
|
// Suggest only the entry's first line: ghost text renders on a
|
|
501
790
|
// single editor row (full multi-line entries are inserted on accept).
|
|
502
791
|
const firstLine = entry.split("\n", 1)[0] ?? "";
|
|
503
|
-
if (firstLine.length > typed.length && firstLine.startsWith(typed)) {
|
|
504
|
-
|
|
792
|
+
if (firstLine.length > typed.length && firstLine.startsWith(typed) && !matches.includes(firstLine)) {
|
|
793
|
+
matches.push(firstLine);
|
|
505
794
|
}
|
|
506
795
|
}
|
|
507
|
-
|
|
796
|
+
this.ghostCandidates = matches;
|
|
797
|
+
this.ghostCandidateIndex = 0;
|
|
798
|
+
const first = matches[0];
|
|
799
|
+
return first ? { lineIndex: st.cursorLine, typed, suggestion: first, source: "history" } : null;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* Seed the history strategy with prompts from previous sessions in this
|
|
804
|
+
* working directory. Opt-in via /autosuggest; runs once per editor.
|
|
805
|
+
*/
|
|
806
|
+
private loadCrossSessionHistory(): void {
|
|
807
|
+
if (this.historyLoaded) {
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
this.historyLoaded = true;
|
|
811
|
+
if (!config.crossSessionHistory) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
try {
|
|
815
|
+
const sessionsDir = `${homedir()}/.pi/agent/sessions`;
|
|
816
|
+
const encoded = (p: string) => `--${p.split("/").filter(Boolean).join("-")}--`;
|
|
817
|
+
let dir: string | undefined;
|
|
818
|
+
for (const candidate of [process.cwd(), realpathSync(process.cwd())]) {
|
|
819
|
+
const path13 = `${sessionsDir}/${encoded(candidate)}`;
|
|
820
|
+
if (existsSync(path13)) {
|
|
821
|
+
dir = path13;
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
if (!dir) {
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
const files = readdirSync(dir)
|
|
829
|
+
.filter((f) => f.endsWith(".jsonl"))
|
|
830
|
+
.sort()
|
|
831
|
+
.slice(-5);
|
|
832
|
+
const prompts: string[] = [];
|
|
833
|
+
for (const file of files) {
|
|
834
|
+
for (const lineText of readFileSync(`${dir}/${file}`, "utf8").split("\n")) {
|
|
835
|
+
try {
|
|
836
|
+
const entry = JSON.parse(lineText) as {
|
|
837
|
+
type?: string;
|
|
838
|
+
message?: { role?: string; content?: string | { type?: string; text?: string }[] };
|
|
839
|
+
};
|
|
840
|
+
if (entry.type !== "message" || entry.message?.role !== "user") {
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
const content = entry.message.content;
|
|
844
|
+
const text =
|
|
845
|
+
typeof content === "string"
|
|
846
|
+
? content
|
|
847
|
+
: (content ?? [])
|
|
848
|
+
.filter((b) => b.type === "text")
|
|
849
|
+
.map((b) => b.text ?? "")
|
|
850
|
+
.join("");
|
|
851
|
+
const firstLine = text.split("\n", 1)[0] ?? "";
|
|
852
|
+
if (firstLine.trim()) {
|
|
853
|
+
prompts.push(firstLine);
|
|
854
|
+
}
|
|
855
|
+
} catch {
|
|
856
|
+
// partial line or unsupported entry
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
// Cross-session entries come before anything typed this session.
|
|
861
|
+
this.promptHistory = [...prompts.reverse(), ...this.promptHistory];
|
|
862
|
+
} catch (e) {
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/** Rotate to the previous/next history candidate (Alt+Up / Alt+Down). */
|
|
867
|
+
private cycleGhostCandidate(dir: 1 | -1): void {
|
|
868
|
+
const ghost = this.activeGhost();
|
|
869
|
+
if (!ghost || ghost.source !== "history" || this.ghostCandidates.length < 2) {
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
this.ghostCandidateIndex =
|
|
873
|
+
(this.ghostCandidateIndex + dir + this.ghostCandidates.length) % this.ghostCandidates.length;
|
|
874
|
+
this.ghost = { ...ghost, suggestion: this.ghostCandidates[this.ghostCandidateIndex]! };
|
|
875
|
+
(this as unknown as EditorInternals).tui.requestRender();
|
|
508
876
|
}
|
|
509
877
|
|
|
510
878
|
/** Path ghost from provider suggestions (completion strategy, bash mode). */
|
|
@@ -626,9 +994,29 @@ class BashInlineEditor extends CustomEditor {
|
|
|
626
994
|
}
|
|
627
995
|
|
|
628
996
|
export default function (pi: ExtensionAPI): void {
|
|
997
|
+
execApi = pi;
|
|
998
|
+
loadConfig();
|
|
629
999
|
pi.on("session_start", (_event, ctx) => {
|
|
1000
|
+
themeRef = ctx.ui.theme;
|
|
630
1001
|
ctx.ui.setEditorComponent((tui, theme, keybindings) => new BashInlineEditor(tui, theme, keybindings));
|
|
631
1002
|
});
|
|
1003
|
+
pi.registerCommand("autosuggest", {
|
|
1004
|
+
description: "Toggle cross-session history for ghost suggestions",
|
|
1005
|
+
handler: async (args, ctx) => {
|
|
1006
|
+
const arg = (args ?? "").trim().toLowerCase();
|
|
1007
|
+
if (arg === "on" || arg === "off") {
|
|
1008
|
+
config.crossSessionHistory = arg === "on";
|
|
1009
|
+
} else {
|
|
1010
|
+
config.crossSessionHistory = !config.crossSessionHistory;
|
|
1011
|
+
}
|
|
1012
|
+
saveConfig();
|
|
1013
|
+
const state = config.crossSessionHistory ? "ON" : "OFF";
|
|
1014
|
+
ctx.ui.notify(
|
|
1015
|
+
`pi-autosuggestions: cross-session history ${state}${config.crossSessionHistory ? " (applies next session)" : ""}`,
|
|
1016
|
+
"info",
|
|
1017
|
+
);
|
|
1018
|
+
},
|
|
1019
|
+
});
|
|
632
1020
|
// Restore the terminal's own cursor color and shape when pi exits so the
|
|
633
1021
|
// shell afterwards isn't left with our forced white bar.
|
|
634
1022
|
process.on("exit", () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-autosuggestions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "zsh-autosuggestions-style ghost completions for the pi coding agent — history-based inline suggestions, path completion in bash mode, blinking beam cursor",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|