pi-rg-fd 0.0.0-stage → 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 (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/index.js +34 -0
  4. package/package.json +39 -4
  5. package/prompt.js +126 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Li Tianyu
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 CHANGED
@@ -1,3 +1,60 @@
1
- # Temporary Holding Version
1
+ # pi-rg-fd
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Pi coding agent extension that rewrites the default system prompt's `grep` and `find` advice to `rg` and `fd` when those tools are on `PATH`.
4
+
5
+ Pi 1.4's bash tool line names `grep` and `find`, and the default guideline still names `find`, so models reach for those commands even when ripgrep and fd are installed. This package replaces those exact lines in place. The usual prompt gets shorter, and text that is not one of those Pi 1.4 lines is left as Pi wrote it.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pi install npm:pi-rg-fd
11
+ ```
12
+
13
+ For one session without saving the install: `pi -e npm:pi-rg-fd`.
14
+
15
+ Requires [Pi](https://github.com/earendil-works/pi) 1.4.x (`@earendil-works/pi-coding-agent`).
16
+
17
+ ## Replaced lines
18
+
19
+ Verified against the published `@earendil-works/pi-coding-agent@1.4.0` sources (`dist/core/tools/bash.js` and `dist/core/system-prompt.js`). These are the only default prompt lines that name the `grep` or `find` commands. The read, edit, write, and ls tool snippets do not. The optional `grep` and `find` tools are separate Pi tools (they already shell out to `rg` and `fd`); their lines are left alone.
20
+
21
+ Bash tool snippet, before:
22
+
23
+ ```text
24
+ Execute bash commands (ls, grep, find, etc.)
25
+ ```
26
+
27
+ Default guideline (bash enabled, and the grep/find/ls tools off), before:
28
+
29
+ ```text
30
+ Use bash for file operations like ls, rg, find
31
+ ```
32
+
33
+ With both `rg` and `fd` on `PATH`, those become:
34
+
35
+ ```text
36
+ Execute bash commands (ls, rg, fd, etc.)
37
+ Use bash for file operations like ls, rg, fd
38
+ ```
39
+
40
+ Each binary is swapped on its own. `fd` is checked independently of `rg`. On Windows the scan accepts `rg.exe` and `fd.exe` (and the rest of `PATHEXT`). On Debian and Ubuntu, where the package installs `fdfind`, that name is used instead of `fd`.
41
+
42
+ | On PATH | Bash tool line | Guideline |
43
+ | --- | --- | --- |
44
+ | `rg` and `fd` | `Execute bash commands (ls, rg, fd, etc.)` | `Use bash for file operations like ls, rg, fd` |
45
+ | `rg` only | `Execute bash commands (ls, rg, find, etc.)` | unchanged (it already says `rg`; `find` stays) |
46
+ | `fd` only | `Execute bash commands (ls, grep, fd, etc.)` | `Use bash for file operations like ls, rg, fd` |
47
+ | `fdfind` only | `Execute bash commands (ls, grep, fdfind, etc.)` | `Use bash for file operations like ls, rg, fdfind` |
48
+ | neither | unchanged | unchanged |
49
+
50
+ `rg` and `fd` are shorter than `grep` and `find`. `fdfind` is two characters longer than `find`, which is the Debian binary's name. If neither line is present (a custom system prompt, or a future Pi release that rewords them), the hook returns without changing the prompt.
51
+
52
+ Availability is resolved once, when the extension loads, by stating files on `PATH`. The `before_agent_start` hook is a string replace cached by the prompt text, so the same input always yields the same output.
53
+
54
+ ## Prompt text only
55
+
56
+ `pi-cli-guard` and `pi-prefer-rg` rewrite or block bash commands when the model runs them. `pi-rg-fd` edits the system prompt Pi already built. Commands the model still emits run unchanged.
57
+
58
+ ## License
59
+
60
+ MIT
package/index.js ADDED
@@ -0,0 +1,34 @@
1
+ import { detectSearchTools, rewritePrompt } from "./prompt.js";
2
+
3
+ const CACHE_LIMIT = 8;
4
+
5
+ /**
6
+ * @param {{ rg: boolean, fd: "fd" | "fdfind" | null }} [tools]
7
+ */
8
+ export function createExtension(tools = detectSearchTools()) {
9
+ /** @type {Map<string, string>} */
10
+ const cache = new Map();
11
+
12
+ return function piRgFd(pi) {
13
+ if (!tools.rg && !tools.fd) return;
14
+
15
+ pi.on("before_agent_start", (event) => {
16
+ const input = event.systemPrompt;
17
+ if (typeof input !== "string") return;
18
+
19
+ let output = cache.get(input);
20
+ if (output !== undefined) {
21
+ cache.delete(input);
22
+ cache.set(input, output);
23
+ } else {
24
+ output = rewritePrompt(input, tools);
25
+ cache.set(input, output);
26
+ if (cache.size > CACHE_LIMIT) cache.delete(cache.keys().next().value);
27
+ }
28
+
29
+ if (output !== input) return { systemPrompt: output };
30
+ });
31
+ };
32
+ }
33
+
34
+ export default createExtension();
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "pi-rg-fd",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Pi extension that rewrites the default grep/find prompt lines to rg and fd when those tools are on PATH.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "license": "MIT",
8
+ "author": "Li Tianyu",
9
+ "keywords": [
10
+ "pi-package",
11
+ "pi",
12
+ "extension",
13
+ "ripgrep",
14
+ "fd"
15
+ ],
16
+ "files": [
17
+ "index.js",
18
+ "prompt.js",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/TomXPRIME/pi-rg-fd.git"
25
+ },
26
+ "homepage": "https://github.com/TomXPRIME/pi-rg-fd",
27
+ "bugs": {
28
+ "url": "https://github.com/TomXPRIME/pi-rg-fd/issues"
29
+ },
30
+ "engines": {
31
+ "node": ">=22.19.0"
32
+ },
33
+ "scripts": {
34
+ "test": "node --test"
35
+ },
36
+ "pi": {
37
+ "extensions": [
38
+ "./index.js"
39
+ ]
40
+ }
41
+ }
package/prompt.js ADDED
@@ -0,0 +1,126 @@
1
+ import { statSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * Default bash tool line in @earendil-works/pi-coding-agent 1.4.0
6
+ * (`bashToolSystemPromptContribution.snippet`).
7
+ */
8
+ export const BASH_SNIPPET = "Execute bash commands (ls, grep, find, etc.)";
9
+
10
+ /**
11
+ * Default guideline in Pi 1.4.0 when bash is enabled and the grep, find, and
12
+ * ls tools are not (`buildRules` in `system-prompt.js`).
13
+ */
14
+ export const BASH_RULE = "Use bash for file operations like ls, rg, find";
15
+
16
+ const WINDOWS_PATHEXT = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC";
17
+
18
+ /**
19
+ * @param {Record<string, string | undefined>} env
20
+ * @param {string} name
21
+ * @param {string} platform
22
+ */
23
+ function envValue(env, name, platform) {
24
+ if (platform !== "win32") return env[name] ?? "";
25
+ const lower = name.toLowerCase();
26
+ for (const key of Object.keys(env)) {
27
+ if (key.toLowerCase() === lower) return env[key] ?? "";
28
+ }
29
+ return "";
30
+ }
31
+
32
+ /**
33
+ * @param {string} name
34
+ * @param {string} platform
35
+ * @param {string} pathext
36
+ */
37
+ function commandNames(name, platform, pathext) {
38
+ if (platform !== "win32") return [name];
39
+ const extensions = new Set([".exe", ".EXE"]);
40
+ for (const raw of pathext.split(";")) {
41
+ const ext = raw.trim();
42
+ if (ext === "" || ext === ".") {
43
+ extensions.add("");
44
+ continue;
45
+ }
46
+ const suffix = ext.startsWith(".") ? ext : `.${ext}`;
47
+ extensions.add(suffix);
48
+ extensions.add(suffix.toLowerCase());
49
+ extensions.add(suffix.toUpperCase());
50
+ }
51
+ return [...extensions].map((ext) => (ext ? name + ext : name));
52
+ }
53
+
54
+ /**
55
+ * @param {string} file
56
+ * @param {string} platform
57
+ */
58
+ function isRunnableFile(file, platform) {
59
+ try {
60
+ const stat = statSync(file);
61
+ if (!stat.isFile()) return false;
62
+ if (platform === "win32") return true;
63
+ return (stat.mode & 0o111) !== 0;
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Locate `rg` and `fd` by scanning PATH. No processes are spawned.
71
+ * `fd` is `"fd"`, `"fdfind"` (Debian/Ubuntu), or null.
72
+ *
73
+ * @param {{ env?: NodeJS.ProcessEnv, platform?: string }} [options]
74
+ * @returns {{ rg: boolean, fd: "fd" | "fdfind" | null }}
75
+ */
76
+ export function detectSearchTools(options = {}) {
77
+ const platform = options.platform ?? process.platform;
78
+ const env = options.env ?? process.env;
79
+ const dirs = envValue(env, "PATH", platform)
80
+ .split(platform === "win32" ? ";" : ":")
81
+ .filter((dir) => dir.length > 0);
82
+ const pathext = platform === "win32" ? envValue(env, "PATHEXT", platform) || WINDOWS_PATHEXT : "";
83
+
84
+ const has = (name) => {
85
+ const names = commandNames(name, platform, pathext);
86
+ for (const dir of dirs) {
87
+ for (const candidate of names) {
88
+ if (isRunnableFile(path.join(dir, candidate), platform)) return true;
89
+ }
90
+ }
91
+ return false;
92
+ };
93
+
94
+ return {
95
+ rg: has("rg"),
96
+ fd: has("fd") ? "fd" : has("fdfind") ? "fdfind" : null,
97
+ };
98
+ }
99
+
100
+ /**
101
+ * @param {string} text
102
+ * @param {string} from
103
+ * @param {string} to
104
+ */
105
+ function replaceExact(text, from, to) {
106
+ if (from === to || !text.includes(from)) return text;
107
+ return text.replaceAll(from, to);
108
+ }
109
+
110
+ /**
111
+ * Replace Pi's default grep/find lines in place. Unknown text is returned
112
+ * unchanged (same string when nothing matches).
113
+ *
114
+ * @param {string} prompt
115
+ * @param {{ rg: boolean, fd: "fd" | "fdfind" | null }} tools
116
+ */
117
+ export function rewritePrompt(prompt, tools) {
118
+ if (!tools.rg && !tools.fd) return prompt;
119
+ const contentTool = tools.rg ? "rg" : "grep";
120
+ const fileTool = tools.fd ?? "find";
121
+ let next = replaceExact(prompt, BASH_SNIPPET, `Execute bash commands (ls, ${contentTool}, ${fileTool}, etc.)`);
122
+ if (tools.fd) {
123
+ next = replaceExact(next, BASH_RULE, `Use bash for file operations like ls, rg, ${tools.fd}`);
124
+ }
125
+ return next;
126
+ }