@firstpick/pi-extension-bang-command-autocomplete 0.1.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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/index.ts +213 -0
  4. package/package.json +25 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # pi-extension-bang-command-autocomplete
2
+
3
+ Autocomplete for !<command> in Pi, with optional shell-history indexing.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pi install npm:@firstpick/pi-extension-bang-command-autocomplete
9
+ ```
10
+
11
+ For local testing:
12
+
13
+ ```bash
14
+ pi install /absolute/path/to/pi-extension-bang-command-autocomplete
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ - `PI_BANG_AUTOCOMPLETE_INCLUDE_HISTORY`
20
+
21
+ ## Commands
22
+
23
+ - `/bang-refresh`
24
+ - `/bang-status`
25
+
26
+ ## Tools
27
+
28
+ - none
29
+
30
+ ## Publish
31
+
32
+ Preferred (Bun):
33
+
34
+ ```bash
35
+ bun publish --access public
36
+ ```
37
+
38
+ Alternative (npm):
39
+
40
+ ```bash
41
+ npm publish --access public
42
+ ```
package/index.ts ADDED
@@ -0,0 +1,213 @@
1
+ import * as fs from "node:fs";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
5
+
6
+ type CommandSource = "common" | "history";
7
+
8
+ function envFlag(name: string, fallback: boolean): boolean {
9
+ const raw = process.env[name]?.trim().toLowerCase();
10
+ if (!raw) return fallback;
11
+ return raw === "1" || raw === "true" || raw === "yes" || raw === "on";
12
+ }
13
+
14
+ const COMMON_COMMANDS = [
15
+ "ls",
16
+ "la",
17
+ "ll",
18
+ "cd",
19
+ "pwd",
20
+ "cat",
21
+ "less",
22
+ "bat",
23
+ "rg",
24
+ "fd",
25
+ "find",
26
+ "grep",
27
+ "sed",
28
+ "awk",
29
+ "jq",
30
+ "git",
31
+ "gh",
32
+ "g",
33
+ "pnpm",
34
+ "bun",
35
+ "npm",
36
+ "node",
37
+ "python",
38
+ "python3",
39
+ "uv",
40
+ "cargo",
41
+ "rustc",
42
+ "make",
43
+ "just",
44
+ "docker",
45
+ "docker-compose",
46
+ "systemctl",
47
+ "journalctl",
48
+ "pacman",
49
+ "yay",
50
+ "curl",
51
+ "wget",
52
+ "ssh",
53
+ "scp",
54
+ "rsync",
55
+ "tmux",
56
+ "htop",
57
+ "btop",
58
+ ] as const;
59
+
60
+ function extractExecutable(commandLine: string): string | undefined {
61
+ const trimmed = commandLine.trim();
62
+ if (!trimmed || trimmed.startsWith("#")) return undefined;
63
+
64
+ const tokens = trimmed.split(/\s+/).filter(Boolean);
65
+ if (tokens.length === 0) return undefined;
66
+
67
+ let executable = tokens[0] ?? "";
68
+ if (executable === "sudo") executable = tokens[1] ?? "";
69
+ if (executable.startsWith("!")) executable = executable.slice(1);
70
+
71
+ if (!executable) return undefined;
72
+ return executable;
73
+ }
74
+
75
+ function readFishHistoryExecutables(): string[] {
76
+ const historyPath = path.join(os.homedir(), ".local", "share", "fish", "fish_history");
77
+ if (!fs.existsSync(historyPath)) return [];
78
+
79
+ const content = fs.readFileSync(historyPath, "utf8");
80
+ const lines = content.split(/\r?\n/);
81
+ const commands: string[] = [];
82
+
83
+ for (const line of lines) {
84
+ const match = line.match(/^\s*-\s*cmd:\s*(.*)$/);
85
+ if (!match) continue;
86
+
87
+ const executable = extractExecutable(match[1] ?? "");
88
+ if (executable) commands.push(executable);
89
+ }
90
+
91
+ return commands;
92
+ }
93
+
94
+ function readBashHistoryExecutables(): string[] {
95
+ const historyPath = path.join(os.homedir(), ".bash_history");
96
+ if (!fs.existsSync(historyPath)) return [];
97
+
98
+ const content = fs.readFileSync(historyPath, "utf8");
99
+ return content
100
+ .split(/\r?\n/)
101
+ .map((line) => extractExecutable(line))
102
+ .filter((v): v is string => Boolean(v));
103
+ }
104
+
105
+ function buildCommandIndex(includeHistory: boolean): Array<{ command: string; source: CommandSource }> {
106
+ const merged = new Map<string, CommandSource>();
107
+
108
+ for (const command of COMMON_COMMANDS) {
109
+ merged.set(command, "common");
110
+ }
111
+
112
+ if (includeHistory) {
113
+ const historyExecutables = [...readFishHistoryExecutables(), ...readBashHistoryExecutables()];
114
+ for (let i = historyExecutables.length - 1; i >= 0; i--) {
115
+ const command = historyExecutables[i];
116
+ if (!command) continue;
117
+
118
+ // History wins over common list and keeps most-recent-first order.
119
+ if (!merged.has(command) || merged.get(command) === "common") {
120
+ merged.set(command, "history");
121
+ }
122
+ }
123
+ }
124
+
125
+ return Array.from(merged.entries()).map(([command, source]) => ({ command, source }));
126
+ }
127
+
128
+ function rankCommands(commands: Array<{ command: string; source: CommandSource }>, query: string) {
129
+ const q = query.toLowerCase();
130
+
131
+ const startsWith = commands.filter((c) => c.command.toLowerCase().startsWith(q));
132
+ const includes = commands.filter(
133
+ (c) => !c.command.toLowerCase().startsWith(q) && c.command.toLowerCase().includes(q),
134
+ );
135
+
136
+ return [...startsWith, ...includes].slice(0, 24);
137
+ }
138
+
139
+ export default function bangCommandAutocomplete(pi: ExtensionAPI) {
140
+ const includeHistory = envFlag("PI_BANG_AUTOCOMPLETE_INCLUDE_HISTORY", false);
141
+ let commandIndex = buildCommandIndex(includeHistory);
142
+
143
+ const refreshIndex = () => {
144
+ commandIndex = buildCommandIndex(includeHistory);
145
+ };
146
+
147
+ pi.on("session_start", (_event, ctx) => {
148
+ refreshIndex();
149
+
150
+ ctx.ui.addAutocompleteProvider((current) => ({
151
+ async getSuggestions(lines, cursorLine, cursorCol, options) {
152
+ const line = lines[cursorLine] ?? "";
153
+ const beforeCursor = line.slice(0, cursorCol);
154
+
155
+ // Trigger on `!<command>` in the current token.
156
+ const match = beforeCursor.match(/(?:^|[ \t])!([^\s!]*)$/);
157
+ if (!match) {
158
+ return current.getSuggestions(lines, cursorLine, cursorCol, options);
159
+ }
160
+
161
+ const partial = match[1] ?? "";
162
+ const ranked = rankCommands(commandIndex, partial);
163
+
164
+ return {
165
+ prefix: `!${partial}`,
166
+ items: ranked.map((entry) => ({
167
+ value: `!${entry.command}`,
168
+ label: `!${entry.command}`,
169
+ description: entry.source === "history" ? "shell history" : "common command",
170
+ })),
171
+ };
172
+ },
173
+
174
+ applyCompletion(lines, cursorLine, cursorCol, item, prefix) {
175
+ return current.applyCompletion(lines, cursorLine, cursorCol, item, prefix);
176
+ },
177
+
178
+ shouldTriggerFileCompletion(lines, cursorLine, cursorCol) {
179
+ const line = lines[cursorLine] ?? "";
180
+ const beforeCursor = line.slice(0, cursorCol);
181
+
182
+ // Allow Tab-forced autocomplete for bang commands (editor reserves auto-popups
183
+ // for /, @, # contexts by default).
184
+ if (beforeCursor.match(/(?:^|[ \t])![^\s!]*$/)) {
185
+ return true;
186
+ }
187
+
188
+ return current.shouldTriggerFileCompletion?.(lines, cursorLine, cursorCol) ?? true;
189
+ },
190
+ }));
191
+ });
192
+
193
+ pi.registerCommand("bang-refresh", {
194
+ description: "Refresh !command autocomplete index",
195
+ handler: async (_args, ctx) => {
196
+ refreshIndex();
197
+ ctx.ui.notify(
198
+ `Bang autocomplete refreshed (${commandIndex.length} commands, history ${includeHistory ? "enabled" : "disabled"})`,
199
+ "info",
200
+ );
201
+ },
202
+ });
203
+
204
+ pi.registerCommand("bang-status", {
205
+ description: "Show !command autocomplete configuration",
206
+ handler: async (_args, ctx) => {
207
+ ctx.ui.notify(
208
+ `Bang autocomplete: ${commandIndex.length} commands · history ${includeHistory ? "enabled" : "disabled"} (${includeHistory ? "PI_BANG_AUTOCOMPLETE_INCLUDE_HISTORY=1" : "set PI_BANG_AUTOCOMPLETE_INCLUDE_HISTORY=1 to enable"})`,
209
+ "info",
210
+ );
211
+ },
212
+ });
213
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@firstpick/pi-extension-bang-command-autocomplete",
3
+ "version": "0.1.0",
4
+ "description": "Autocomplete for !<command> in Pi, with optional shell-history indexing.",
5
+ "license": "MIT",
6
+ "keywords": [
7
+ "pi-package",
8
+ "pi",
9
+ "pi-coding-agent",
10
+ "extension"
11
+ ],
12
+ "pi": {
13
+ "extensions": [
14
+ "./index.ts"
15
+ ]
16
+ },
17
+ "peerDependencies": {
18
+ "@mariozechner/pi-coding-agent": "*"
19
+ },
20
+ "files": [
21
+ "index.ts",
22
+ "README.md",
23
+ "LICENSE"
24
+ ]
25
+ }