@josephyoung/pi-file-reference 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.
- package/LICENSE +21 -0
- package/README.md +53 -0
- package/extensions/index.ts +174 -0
- package/package.json +33 -0
- package/tsconfig.json +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joseph Cheng
|
|
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,53 @@
|
|
|
1
|
+
# pi-file-reference
|
|
2
|
+
|
|
3
|
+
A [pi](https://github.com/earendil-works/pi-coding-agent) extension that resolves `@filepath` references in `AGENTS.md` and injects the referenced file contents into the system prompt.
|
|
4
|
+
|
|
5
|
+
## How it works
|
|
6
|
+
|
|
7
|
+
1. On `session_start`, the extension reads `AGENTS.md` (project-local first, then `~/.pi/agent/` as fallback)
|
|
8
|
+
2. Parses `@filepath` references from the file
|
|
9
|
+
3. Reads the referenced files and caches their contents in session state (survives reload)
|
|
10
|
+
4. On `before_agent_start`, injects the file contents into the system prompt
|
|
11
|
+
|
|
12
|
+
## @filepath syntax
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
@path/to/file.md
|
|
16
|
+
@./path/to/file.md
|
|
17
|
+
@~/path/to/file.md
|
|
18
|
+
@"path with spaces.md"
|
|
19
|
+
@/absolute/path/to/file.md
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The `@` must be at the start of a line or preceded by whitespace.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# Install from npm (once published)
|
|
28
|
+
pi install pi-file-reference
|
|
29
|
+
|
|
30
|
+
# Or clone and link locally
|
|
31
|
+
git clone https://github.com/your-org/pi-file-reference.git
|
|
32
|
+
cd pi-file-reference
|
|
33
|
+
npm install
|
|
34
|
+
pi install .
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
In your `AGENTS.md`:
|
|
40
|
+
|
|
41
|
+
```markdown
|
|
42
|
+
# Project guidelines
|
|
43
|
+
Always follow the patterns in @./docs/style-guide.md
|
|
44
|
+
|
|
45
|
+
# Architecture
|
|
46
|
+
@~/.project-arch/backend-overview.md
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The referenced files' content is automatically injected into the system prompt at the start of each agent turn.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
|
|
53
|
+
MIT
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { ExtensionAPI, CustomEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import * as os from "node:os";
|
|
5
|
+
|
|
6
|
+
const AGENTS_FILE = "AGENTS.md";
|
|
7
|
+
const STORE_KEY = "agents-at-context";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parse @filepath references from a line.
|
|
11
|
+
*
|
|
12
|
+
* Rules:
|
|
13
|
+
* - @ must be preceded by whitespace or start-of-line
|
|
14
|
+
* - @ followed by a double-quoted string: extract inside quotes
|
|
15
|
+
* - @ followed by unquoted text: extract until whitespace
|
|
16
|
+
*/
|
|
17
|
+
function parseRefs(line: string): string[] {
|
|
18
|
+
const refs: string[] = [];
|
|
19
|
+
let i = 0;
|
|
20
|
+
|
|
21
|
+
while (i < line.length) {
|
|
22
|
+
const atIdx = line.indexOf("@", i);
|
|
23
|
+
if (atIdx === -1) break;
|
|
24
|
+
|
|
25
|
+
// @ must be preceded by start-of-line or whitespace
|
|
26
|
+
if (atIdx > 0 && line[atIdx - 1] !== " " && line[atIdx - 1] !== "\t") {
|
|
27
|
+
i = atIdx + 1;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
i = atIdx + 1;
|
|
32
|
+
let ref: string;
|
|
33
|
+
|
|
34
|
+
if (line[i] === '"') {
|
|
35
|
+
// Quoted reference: @"path/to/file"
|
|
36
|
+
const close = line.indexOf('"', i + 1);
|
|
37
|
+
ref = close === -1 ? line.slice(i + 1) : line.slice(i + 1, close);
|
|
38
|
+
i = close === -1 ? line.length : close + 1;
|
|
39
|
+
} else {
|
|
40
|
+
// Unquoted reference: @path/to/file (until whitespace)
|
|
41
|
+
const start = i;
|
|
42
|
+
while (i < line.length && !/\s/.test(line[i])) i++;
|
|
43
|
+
ref = line.slice(start, i);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (ref) refs.push(ref);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return refs;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Resolve a reference path against baseDir.
|
|
54
|
+
*
|
|
55
|
+
* Handles:
|
|
56
|
+
* - Absolute paths (/foo/bar) — used as-is
|
|
57
|
+
* - Tilde paths (~/foo or ~user/foo) — expanded via os.homedir()
|
|
58
|
+
* - Relative paths — resolved against baseDir
|
|
59
|
+
*/
|
|
60
|
+
function resolveRef(ref: string, baseDir: string): string {
|
|
61
|
+
if (ref.startsWith("/")) return ref;
|
|
62
|
+
if (ref.startsWith("~")) {
|
|
63
|
+
// ~/path or ~user/path
|
|
64
|
+
const slashIdx = ref.indexOf("/");
|
|
65
|
+
if (slashIdx === -1) {
|
|
66
|
+
// Just ~ or ~user — unlikely, but handle gracefully
|
|
67
|
+
return path.join(os.homedir(), ref.slice(1));
|
|
68
|
+
}
|
|
69
|
+
const userPart = ref.slice(1, slashIdx);
|
|
70
|
+
if (!userPart) {
|
|
71
|
+
// ~/path
|
|
72
|
+
return path.join(os.homedir(), ref.slice(slashIdx + 1));
|
|
73
|
+
}
|
|
74
|
+
// ~user/ — tilde expansion for other users; falls back to homedir + user
|
|
75
|
+
return path.join(os.homedir(), userPart, ref.slice(slashIdx + 1));
|
|
76
|
+
}
|
|
77
|
+
return path.resolve(baseDir, ref);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A resolved file reference with its content. */
|
|
81
|
+
interface RefContent {
|
|
82
|
+
ref: string;
|
|
83
|
+
content: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Find and parse AGENTS.md, collect @filepath references.
|
|
88
|
+
*
|
|
89
|
+
* Search order:
|
|
90
|
+
* 1. <cwd>/AGENTS.md
|
|
91
|
+
* 2. ~/.pi/agent/AGENTS.md
|
|
92
|
+
*/
|
|
93
|
+
function loadRefs(cwd: string): RefContent[] {
|
|
94
|
+
const candidates = [
|
|
95
|
+
path.join(cwd, AGENTS_FILE),
|
|
96
|
+
path.join(os.homedir(), ".pi", "agent", AGENTS_FILE),
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
let agentsPath: string | undefined;
|
|
100
|
+
for (const candidate of candidates) {
|
|
101
|
+
if (fs.existsSync(candidate)) {
|
|
102
|
+
agentsPath = candidate;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!agentsPath) return [];
|
|
108
|
+
|
|
109
|
+
const raw = fs.readFileSync(agentsPath, "utf-8");
|
|
110
|
+
const baseDir = path.dirname(agentsPath);
|
|
111
|
+
|
|
112
|
+
// Collect all refs from all lines
|
|
113
|
+
const allRefs: string[] = [];
|
|
114
|
+
for (const line of raw.split("\n")) {
|
|
115
|
+
allRefs.push(...parseRefs(line));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Deduplicate while preserving order
|
|
119
|
+
const seen = new Set<string>();
|
|
120
|
+
const uniqueRefs = allRefs.filter((ref) => {
|
|
121
|
+
if (seen.has(ref)) return false;
|
|
122
|
+
seen.add(ref);
|
|
123
|
+
return true;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Read each referenced file
|
|
127
|
+
const results: RefContent[] = [];
|
|
128
|
+
for (const ref of uniqueRefs) {
|
|
129
|
+
const resolvedPath = resolveRef(ref, baseDir);
|
|
130
|
+
if (fs.existsSync(resolvedPath)) {
|
|
131
|
+
results.push({ ref, content: fs.readFileSync(resolvedPath, "utf-8") });
|
|
132
|
+
} else {
|
|
133
|
+
console.warn(`[agents-at-reader] @${ref} -> ${resolvedPath} not found, skipping`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return results;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export default function (pi: ExtensionAPI) {
|
|
141
|
+
pi.on("session_start", (_event, ctx) => {
|
|
142
|
+
// Check if custom entry already exists (e.g., after reload)
|
|
143
|
+
const entries = ctx.sessionManager.getEntries();
|
|
144
|
+
const existing = entries.find(
|
|
145
|
+
(e): e is CustomEntry<{ refs: RefContent[] }> =>
|
|
146
|
+
e.type === "custom" && e.customType === STORE_KEY,
|
|
147
|
+
);
|
|
148
|
+
if (existing) return;
|
|
149
|
+
|
|
150
|
+
const refs = loadRefs(ctx.cwd);
|
|
151
|
+
if (refs.length === 0) return;
|
|
152
|
+
|
|
153
|
+
pi.appendEntry(STORE_KEY, { refs });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
pi.on("before_agent_start", (event, ctx) => {
|
|
157
|
+
const entries = ctx.sessionManager.getEntries();
|
|
158
|
+
const entry = entries.find(
|
|
159
|
+
(e): e is CustomEntry<{ refs: RefContent[] }> =>
|
|
160
|
+
e.type === "custom" && e.customType === STORE_KEY,
|
|
161
|
+
);
|
|
162
|
+
if (!entry?.data?.refs?.length) return;
|
|
163
|
+
|
|
164
|
+
const injected = entry.data.refs
|
|
165
|
+
.map((r) => `// @${r.ref}\n${r.content}`)
|
|
166
|
+
.join("\n\n");
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
systemPrompt:
|
|
170
|
+
event.systemPrompt +
|
|
171
|
+
`\n\n---\n// @AGENTS.md 引用文件 — 注入以下文件内容供参考\n${injected}\n---`,
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@josephyoung/pi-file-reference",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Pi extension that resolves @filepath references in AGENTS.md and injects file content into system prompt",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"pi-package",
|
|
9
|
+
"pi",
|
|
10
|
+
"extension"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/josephyoung/pi-file-reference#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/josephyoung/pi-file-reference/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/josephyoung/pi-file-reference.git"
|
|
19
|
+
},
|
|
20
|
+
"pi": {
|
|
21
|
+
"extensions": [
|
|
22
|
+
"./extensions"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@earendil-works/pi-coding-agent": "^0.74.0",
|
|
27
|
+
"@types/node": "^25.7.0"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"registry": "https://registry.npmjs.org/"
|
|
32
|
+
}
|
|
33
|
+
}
|