pi-doc-injector 0.7.0 → 0.8.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/README.md +38 -0
- package/cache.ts +32 -2
- package/commands.ts +3 -3
- package/docs/typescript-hardening.md +38 -0
- package/globber.ts +29 -8
- package/index.ts +34 -38
- package/injector.ts +3 -0
- package/keyword-gen.ts +4 -1
- package/matcher.ts +16 -5
- package/package.json +9 -6
- package/picomatch.d.ts +0 -14
package/README.md
CHANGED
|
@@ -8,6 +8,15 @@ A [Pi](https://pi.dev) extension that adds relevant project docs to the conversa
|
|
|
8
8
|
pi install npm:pi-doc-injector
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
Or install directly from GitHub:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pi install git:github.com/lmn451/pi-docs@master
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Replace `master` with a branch, tag, or commit to select another revision.
|
|
18
|
+
If switching from npm, run `pi remove npm:pi-doc-injector` first to avoid loading both copies.
|
|
19
|
+
|
|
11
20
|
Requires Pi 1.5.2 or newer within the 1.x series.
|
|
12
21
|
|
|
13
22
|
## Simple mode (default)
|
|
@@ -45,6 +54,10 @@ Optional `.pi/doc-injector.json` in your project:
|
|
|
45
54
|
|
|
46
55
|
Set `"mode": "keywords"` to use frontmatter keywords, heuristic keyword generation, or optional LLM keyword generation. It also supports nested docs and `.txt` files. The existing `matchThreshold` (default: 2), `streamWindowSize` (default: 500), `recursive`, `autoKeywords`, `llmKeywords`, and `llmBatchSize` settings apply to this mode. Keyword-mode assistant matches still accumulate across streaming updates and wait for the next agent run; they do not request same-run continuation. Neither mode aborts an active response. `streamWindowSize` and `matchThreshold` do not affect simple matching.
|
|
47
56
|
|
|
57
|
+
Keyword mode lazily compiles and reuses one matcher per assistant message. Registry, keyword, injection-state, and configuration changes invalidate that matcher; accumulated keyword hits remain until the message ends.
|
|
58
|
+
|
|
59
|
+
Cache writes merge under Pi's per-file mutation queue for heuristic and LLM updates. This coordinates writers only within the Pi process; it is not a cross-process lock or an atomic filesystem transaction.
|
|
60
|
+
|
|
48
61
|
Example doc:
|
|
49
62
|
|
|
50
63
|
```md
|
|
@@ -80,6 +93,23 @@ The history is newest-first and has one record per document in each saved inject
|
|
|
80
93
|
|
|
81
94
|
Reason metadata is versioned and stored in `details` on the existing Pi custom messages; it does not duplicate document bodies or write a separate `debug.log`. The history reflects saved injection messages, not an acknowledgement that the model received or read the documents. It is read from native current-branch session history on each command, so it survives extension reload; in-memory injection marks are separate and are rebuilt on reload. Older injection messages without usable reasons metadata are reported unavailable by the command rather than assigned guessed reasons.
|
|
82
95
|
|
|
96
|
+
## Documentation authoring skill
|
|
97
|
+
|
|
98
|
+
The repository includes `skills/doc-authoring/SKILL.md` for creating and
|
|
99
|
+
validating docs folders. It defaults to top-level filename matching, supports
|
|
100
|
+
opt-in keyword mode, and checks file eligibility, sizes, links, and runtime
|
|
101
|
+
registration without resetting injection state automatically.
|
|
102
|
+
|
|
103
|
+
To install it globally from a repository checkout:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
mkdir -p ~/.pi/agent/skills/doc-authoring
|
|
107
|
+
cp skills/doc-authoring/SKILL.md ~/.pi/agent/skills/doc-authoring/SKILL.md
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Run `/reload` in an active Pi session, then use
|
|
111
|
+
`/skill:doc-authoring create or validate this project's docs folder`.
|
|
112
|
+
|
|
83
113
|
## Development
|
|
84
114
|
|
|
85
115
|
```bash
|
|
@@ -88,6 +118,14 @@ npm run typecheck
|
|
|
88
118
|
npm run format:check
|
|
89
119
|
```
|
|
90
120
|
|
|
121
|
+
The runtime glob filter uses `minimatch`. Pi supplies the required
|
|
122
|
+
`@earendil-works/pi-coding-agent` and `typebox` peer APIs; local development
|
|
123
|
+
installs them as dev dependencies, and tests run with Vitest 4.1.12. Its current
|
|
124
|
+
Vite 8.3.8 toolchain resolves vulnerable `picomatch@4.0.7`
|
|
125
|
+
(GHSA-fc5q-39xc-vjpf); npm audit also flags the related `tinyglobby`, `fdir`,
|
|
126
|
+
Vite, and Vitest development-graph nodes. This is dev-only, not the runtime glob
|
|
127
|
+
engine; no patched picomatch release is available.
|
|
128
|
+
|
|
91
129
|
Lifecycle tests cover full input and long completed thinking/output, block boundaries and partial words, persisted same-run injection, duplicate suppression, abort/error outcomes, the continuation limit, context guards, and keyword-mode streaming regressions.
|
|
92
130
|
|
|
93
131
|
Injection marks live in memory. Reloading the extension rebuilds this state; it does not reconstruct marks from saved or compacted history.
|
package/cache.ts
CHANGED
|
@@ -6,9 +6,14 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Invalid files (wrong version, bad JSON, ENOENT) result in an empty cache.
|
|
8
8
|
*/
|
|
9
|
+
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
9
10
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
10
|
-
import { dirname, join } from "node:path";
|
|
11
|
-
import
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
import {
|
|
13
|
+
LLM_CACHE_SENTINEL,
|
|
14
|
+
type CacheEntry,
|
|
15
|
+
type KeywordCache,
|
|
16
|
+
} from "./types";
|
|
12
17
|
import type { Notifier } from "./notifier";
|
|
13
18
|
|
|
14
19
|
const CACHE_FILENAME = ".pi/doc-injector-cache.json";
|
|
@@ -71,6 +76,31 @@ export async function saveCache(
|
|
|
71
76
|
await writeFile(cachePath, JSON.stringify(cache, null, 2), "utf-8");
|
|
72
77
|
}
|
|
73
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Merge dirty keyword entries into the latest on-disk cache and save it under
|
|
81
|
+
* Pi's per-file mutation queue.
|
|
82
|
+
*/
|
|
83
|
+
export async function mergeAndSaveCache(
|
|
84
|
+
cwd: string,
|
|
85
|
+
dirtyEntries: Record<string, CacheEntry>,
|
|
86
|
+
notifier: Notifier,
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
await withFileMutationQueue(resolve(cwd, CACHE_FILENAME), async () => {
|
|
89
|
+
const cache = await loadCache(cwd, notifier);
|
|
90
|
+
for (const [path, entry] of Object.entries(dirtyEntries)) {
|
|
91
|
+
const current = cache.files[path];
|
|
92
|
+
if (
|
|
93
|
+
current?.mtimeMs === LLM_CACHE_SENTINEL &&
|
|
94
|
+
entry.mtimeMs !== LLM_CACHE_SENTINEL
|
|
95
|
+
) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
cache.files[path] = entry;
|
|
99
|
+
}
|
|
100
|
+
await saveCache(cwd, cache);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
74
104
|
/** Check that a parsed value matches the KeywordCache shape. */
|
|
75
105
|
function isValidCache(value: unknown): value is KeywordCache {
|
|
76
106
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
package/commands.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { DocInjectorConfig } from "./types";
|
|
|
13
13
|
/** Dependencies injected into the command registrar. */
|
|
14
14
|
export interface CommandDeps {
|
|
15
15
|
getRegistry: () => DocRegistry | null;
|
|
16
|
+
resetInjectionState: () => void;
|
|
16
17
|
getEnabled: () => boolean;
|
|
17
18
|
setEnabled: (v: boolean) => void;
|
|
18
19
|
reloadRegistry: () => Promise<number>;
|
|
@@ -81,7 +82,7 @@ export function registerCommands(pi: ExtensionAPI, deps: CommandDeps): void {
|
|
|
81
82
|
} else if (a === "reset") {
|
|
82
83
|
const reg = deps.getRegistry();
|
|
83
84
|
if (reg) {
|
|
84
|
-
|
|
85
|
+
deps.resetInjectionState();
|
|
85
86
|
ctx.ui.notify("📄 Injection state reset", "info");
|
|
86
87
|
} else {
|
|
87
88
|
ctx.ui.notify("📄 No registry loaded", "warning");
|
|
@@ -162,10 +163,9 @@ export function registerCommands(pi: ExtensionAPI, deps: CommandDeps): void {
|
|
|
162
163
|
|
|
163
164
|
const targetPath = args.trim();
|
|
164
165
|
|
|
165
|
-
//
|
|
166
|
+
// Heuristic keywords remain eligible for upgrade after cache-backed reloads.
|
|
166
167
|
let candidates = reg.getEntries().filter((e) => {
|
|
167
168
|
if (e.keywordSource === "frontmatter") return false;
|
|
168
|
-
if (e.keywordSource === "cache") return false;
|
|
169
169
|
if (e.keywordSource === "llm") return false; // already LLM-generated
|
|
170
170
|
return true;
|
|
171
171
|
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# TypeScript hardening
|
|
2
|
+
|
|
3
|
+
`npm run typecheck` runs the existing whole-project check and a stricter,
|
|
4
|
+
dependency-light core check. `npm run typecheck:core` runs only the latter.
|
|
5
|
+
The core covers shared types, matching, local and LLM keyword prompt generation,
|
|
6
|
+
and injection formatting. It enables `noUncheckedIndexedAccess`,
|
|
7
|
+
`exactOptionalPropertyTypes`, and dependency declaration checking, with no emit.
|
|
8
|
+
It does not claim stricter coverage of Pi SDK integration or filesystem modules.
|
|
9
|
+
|
|
10
|
+
Negative contracts in `typechecks/core.test-d.ts` verify that unchecked array
|
|
11
|
+
access and explicit undefined option overrides are rejected. These contracts
|
|
12
|
+
are deliberately outside the normal test compilation. Runtime tests in
|
|
13
|
+
`test/typescript-config.test.ts` protect the scope, flags and mandatory script
|
|
14
|
+
chain. Existing matching tests cover Unicode boundaries and rolling windows.
|
|
15
|
+
|
|
16
|
+
## Provenance
|
|
17
|
+
|
|
18
|
+
The method was recovered locally from `/Users/applesucks/dev/pi-agents`, whose
|
|
19
|
+
package is `pi-subagentura`. The relevant work is from November 14, 2026:
|
|
20
|
+
|
|
21
|
+
- `efaa6eb15630a72ebe0fc3a48003c6a247dd57b6`, 16:05:16 +0200:
|
|
22
|
+
Harden extension execution and TypeScript boundaries (#235).
|
|
23
|
+
- `ffd2e5c445eb57e0371d0acda5ecfe9edcd711b7`, 14:03:14 +0200:
|
|
24
|
+
Incremental strict core typechecks, negative contracts and mandatory script
|
|
25
|
+
wiring.
|
|
26
|
+
- `74282f7e32ead66f430849dabaf0505d56b6f248`, 14:01:33 +0200:
|
|
27
|
+
Worker JavaScript and RPC checks. Those worker-specific changes are not needed
|
|
28
|
+
here because this extension has no JavaScript worker.
|
|
29
|
+
|
|
30
|
+
Pi session history under `~/.pi/agent/sessions` was also inspected. The concrete
|
|
31
|
+
implementation evidence is the local Git history and its checked-in compiler,
|
|
32
|
+
contract and test artifacts. The inspected hardening precedent does not define
|
|
33
|
+
ISO, so this work does not assign a meaning to that acronym.
|
|
34
|
+
|
|
35
|
+
A reusable global Pi skill is installed at
|
|
36
|
+
`~/.pi/agent/skills/typescript-hardening/SKILL.md`, using the existing global
|
|
37
|
+
skill directory and YAML frontmatter conventions. Its human-readable name is
|
|
38
|
+
TypeScript hardening and its skill name is `typescript-hardening`.
|
package/globber.ts
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Glob filter for include/exclude pattern matching.
|
|
3
|
-
*
|
|
3
|
+
* Patterns are compiled once into Minimatch's non-recursive matchers.
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
5
|
+
import { Minimatch } from "minimatch";
|
|
6
6
|
import type { GlobFilter } from "./types";
|
|
7
7
|
|
|
8
|
+
// Preserve literal # and leading-! behavior while keeping extglobs enabled.
|
|
9
|
+
const globOptions = { dot: true, nocomment: true, nonegate: false };
|
|
10
|
+
|
|
11
|
+
function compilePatterns(patterns: string[]): Minimatch[] {
|
|
12
|
+
const matchers: Minimatch[] = [];
|
|
13
|
+
for (const pattern of patterns) {
|
|
14
|
+
matchers.push(new Minimatch(pattern, globOptions));
|
|
15
|
+
}
|
|
16
|
+
return matchers;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function matchesAny(
|
|
20
|
+
matchers: readonly Minimatch[],
|
|
21
|
+
relativePath: string,
|
|
22
|
+
): boolean {
|
|
23
|
+
for (const matcher of matchers) {
|
|
24
|
+
if (matcher.match(relativePath)) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
8
31
|
/**
|
|
9
32
|
* Create a glob filter from include and exclude patterns.
|
|
10
33
|
*
|
|
@@ -22,20 +45,18 @@ export function createGlobFilter(
|
|
|
22
45
|
include: string[],
|
|
23
46
|
exclude: string[],
|
|
24
47
|
): GlobFilter {
|
|
25
|
-
const
|
|
26
|
-
include.length > 0 ? picomatch(include, { dot: true }) : null;
|
|
48
|
+
const includeMatchers = include.length > 0 ? compilePatterns(include) : null;
|
|
27
49
|
|
|
28
|
-
const
|
|
29
|
-
exclude.length > 0 ? picomatch(exclude, { dot: true }) : null;
|
|
50
|
+
const excludeMatchers = exclude.length > 0 ? compilePatterns(exclude) : null;
|
|
30
51
|
|
|
31
52
|
return {
|
|
32
53
|
match(relativePath: string): boolean {
|
|
33
54
|
// If include patterns are specified, path must match at least one
|
|
34
|
-
if (
|
|
55
|
+
if (includeMatchers && !matchesAny(includeMatchers, relativePath)) {
|
|
35
56
|
return false;
|
|
36
57
|
}
|
|
37
58
|
// Path must not match any exclude pattern
|
|
38
|
-
if (
|
|
59
|
+
if (excludeMatchers && matchesAny(excludeMatchers, relativePath)) {
|
|
39
60
|
return false;
|
|
40
61
|
}
|
|
41
62
|
return true;
|
package/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
} from "@earendil-works/pi-coding-agent";
|
|
15
15
|
import { Type } from "typebox";
|
|
16
16
|
import { resolve } from "node:path";
|
|
17
|
-
import { loadCache,
|
|
17
|
+
import { loadCache, mergeAndSaveCache } from "./cache";
|
|
18
18
|
import { loadConfig } from "./config";
|
|
19
19
|
import {
|
|
20
20
|
type InjectionAuditDetails,
|
|
@@ -87,37 +87,24 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
87
87
|
let llmBatchesCompleted = 0;
|
|
88
88
|
let llmTotalFiles = 0;
|
|
89
89
|
let cache: KeywordCache = { version: 1, files: {} };
|
|
90
|
+
// Reuse compiled patterns across chunks; discard them when the registry changes.
|
|
91
|
+
let streamMatcher: KeywordMatcher | null = null;
|
|
90
92
|
|
|
91
93
|
// ---- Helpers ----
|
|
92
94
|
const getRegistry = () => registry;
|
|
95
|
+
const invalidateStreamMatcher = () => {
|
|
96
|
+
streamMatcher = null;
|
|
97
|
+
};
|
|
98
|
+
const resetInjectionState = () => {
|
|
99
|
+
registry?.markAllNotInjected();
|
|
100
|
+
invalidateStreamMatcher();
|
|
101
|
+
};
|
|
93
102
|
const getEnabled = () => enabled;
|
|
94
103
|
const setEnabled = (v: boolean) => {
|
|
95
104
|
enabled = v;
|
|
96
105
|
};
|
|
97
106
|
const getConfig = () => config;
|
|
98
107
|
|
|
99
|
-
const safeSaveCache = async (
|
|
100
|
-
cwd: string,
|
|
101
|
-
dirtyEntries: Record<string, CacheEntry>,
|
|
102
|
-
) => {
|
|
103
|
-
// MAJOR-2 fix: before saveCache, re-read cache from disk to merge
|
|
104
|
-
// LLM-written entries that may have landed during the scan.
|
|
105
|
-
const freshCache = await loadCache(cwd, notifier);
|
|
106
|
-
const mergedCache: KeywordCache = { version: 1, files: {} };
|
|
107
|
-
|
|
108
|
-
// Start with fresh (disk) entries — includes any LLM writes during scan
|
|
109
|
-
for (const [key, entry] of Object.entries(freshCache.files)) {
|
|
110
|
-
mergedCache.files[key] = entry;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Overlay dirty entries from this scan (scan results take precedence)
|
|
114
|
-
for (const [key, entry] of Object.entries(dirtyEntries)) {
|
|
115
|
-
mergedCache.files[key] = entry;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
await saveCache(cwd, mergedCache);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
108
|
const initRegistry = async (cwd: string) => {
|
|
122
109
|
config = await loadConfig(cwd, notifier);
|
|
123
110
|
const docsPath = resolve(cwd, config.docsPath);
|
|
@@ -126,10 +113,11 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
126
113
|
? await loadCache(cwd, notifier)
|
|
127
114
|
: { version: 1, files: {} };
|
|
128
115
|
registry = await DocRegistry.create(docsPath, config, cache, notifier);
|
|
116
|
+
invalidateStreamMatcher();
|
|
129
117
|
|
|
130
118
|
const dirty = registry.getDirtyCache();
|
|
131
119
|
if (Object.keys(dirty).length > 0) {
|
|
132
|
-
await
|
|
120
|
+
await mergeAndSaveCache(cwd, dirty, notifier);
|
|
133
121
|
}
|
|
134
122
|
};
|
|
135
123
|
|
|
@@ -155,8 +143,6 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
155
143
|
};
|
|
156
144
|
|
|
157
145
|
// P5.4a — Inline tool registration (BLOCKER-2 fix).
|
|
158
|
-
// Registered inside the factory for closure access to cache, cwd, saveCache,
|
|
159
|
-
// and llmBatchesCompleted. Uses real mtime from stat().
|
|
160
146
|
pi.registerTool({
|
|
161
147
|
name: "_doc_injector_keywords",
|
|
162
148
|
label: "Doc Injector Keywords",
|
|
@@ -177,6 +163,7 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
177
163
|
}>;
|
|
178
164
|
const { stat } = await import("node:fs/promises");
|
|
179
165
|
let saved = 0;
|
|
166
|
+
const dirtyEntries: Record<string, CacheEntry> = {};
|
|
180
167
|
for (const item of generated) {
|
|
181
168
|
// Only documents in the registry may receive generated keywords.
|
|
182
169
|
const entry = registry
|
|
@@ -185,13 +172,16 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
185
172
|
if (config.mode !== "keywords" || !entry) continue;
|
|
186
173
|
const fileStat = await stat(entry.filePath).catch(() => null);
|
|
187
174
|
if (!fileStat?.isFile()) continue;
|
|
188
|
-
|
|
175
|
+
dirtyEntries[item.path] = {
|
|
189
176
|
mtimeMs: LLM_CACHE_SENTINEL,
|
|
190
177
|
keywords: item.keywords.map((k) => k.toLowerCase()).slice(0, 20),
|
|
191
178
|
};
|
|
192
179
|
saved++;
|
|
193
180
|
}
|
|
194
|
-
if (saved > 0)
|
|
181
|
+
if (saved > 0) {
|
|
182
|
+
await mergeAndSaveCache(ctx.cwd, dirtyEntries, notifier);
|
|
183
|
+
invalidateStreamMatcher();
|
|
184
|
+
}
|
|
195
185
|
llmBatchesCompleted++;
|
|
196
186
|
llmTotalFiles += saved;
|
|
197
187
|
return {
|
|
@@ -215,6 +205,7 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
215
205
|
streamHits.clear();
|
|
216
206
|
continuedForDocs = false;
|
|
217
207
|
continuationLimitNotified = false;
|
|
208
|
+
invalidateStreamMatcher();
|
|
218
209
|
|
|
219
210
|
// Bind the notifier to the live context FIRST so any warnings emitted
|
|
220
211
|
// during initRegistry below go directly to the TUI instead of being
|
|
@@ -245,10 +236,11 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
245
236
|
}
|
|
246
237
|
|
|
247
238
|
await registry.rebuild();
|
|
239
|
+
invalidateStreamMatcher();
|
|
248
240
|
|
|
249
241
|
const dirty = registry.getDirtyCache();
|
|
250
242
|
if (Object.keys(dirty).length > 0) {
|
|
251
|
-
await
|
|
243
|
+
await mergeAndSaveCache(effectiveCwd, dirty, notifier);
|
|
252
244
|
}
|
|
253
245
|
|
|
254
246
|
const count = registry.getEntries().length;
|
|
@@ -303,16 +295,17 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
303
295
|
const textBuffer = extractText(content);
|
|
304
296
|
if (!textBuffer) return;
|
|
305
297
|
|
|
306
|
-
// Discovery
|
|
307
|
-
//
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
298
|
+
// Discovery uses threshold 1 per chunk; streamHits applies the configured
|
|
299
|
+
// threshold across the whole message. Reuse compiled patterns between chunks.
|
|
300
|
+
if (!streamMatcher) {
|
|
301
|
+
streamMatcher = buildMatcher({
|
|
302
|
+
matchThreshold: 1,
|
|
303
|
+
windowSize: config.streamWindowSize,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
if (!streamMatcher) return;
|
|
314
307
|
|
|
315
|
-
const results =
|
|
308
|
+
const results = streamMatcher.match(textBuffer);
|
|
316
309
|
|
|
317
310
|
for (const result of results) {
|
|
318
311
|
const filePath = result.entry.filePath;
|
|
@@ -332,6 +325,7 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
332
325
|
// Hits belong to one assistant message. Clear them after any message ends.
|
|
333
326
|
pi.on("message_end", async () => {
|
|
334
327
|
streamHits.clear();
|
|
328
|
+
invalidateStreamMatcher();
|
|
335
329
|
});
|
|
336
330
|
|
|
337
331
|
const prepareInjection = (
|
|
@@ -413,6 +407,7 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
413
407
|
// Pi exposes no post-commit callback for returned messages/drafts. Mark
|
|
414
408
|
// only after preparation succeeds, immediately before proposing them.
|
|
415
409
|
registry.markInjected(matchedEntries.map((e) => e.filePath));
|
|
410
|
+
invalidateStreamMatcher();
|
|
416
411
|
clearPendingMatches();
|
|
417
412
|
return { content, details };
|
|
418
413
|
};
|
|
@@ -508,6 +503,7 @@ export default async function docInjectorExtension(pi: ExtensionAPI) {
|
|
|
508
503
|
// ---- Commands ----
|
|
509
504
|
registerCommands(pi, {
|
|
510
505
|
getRegistry,
|
|
506
|
+
resetInjectionState,
|
|
511
507
|
getEnabled,
|
|
512
508
|
setEnabled,
|
|
513
509
|
reloadRegistry,
|
package/injector.ts
CHANGED
|
@@ -62,6 +62,9 @@ export function formatInjectionHistory(
|
|
|
62
62
|
|
|
63
63
|
for (let index = total - 1; index >= total - shown; index--) {
|
|
64
64
|
const record = history.records[index];
|
|
65
|
+
if (record === undefined) {
|
|
66
|
+
throw new Error("Injection history must not contain empty records");
|
|
67
|
+
}
|
|
65
68
|
formatInjectionHistoryRecord(record, lines);
|
|
66
69
|
if (index > total - shown) lines.push("");
|
|
67
70
|
}
|
package/keyword-gen.ts
CHANGED
|
@@ -234,7 +234,9 @@ function addFromHeadings(content: string, out: string[]): void {
|
|
|
234
234
|
const headingRegex = /^#{1,6}\s+(.+)$/gm;
|
|
235
235
|
let match: RegExpExecArray | null;
|
|
236
236
|
while ((match = headingRegex.exec(content)) !== null) {
|
|
237
|
-
const
|
|
237
|
+
const heading = match[1];
|
|
238
|
+
if (heading === undefined) continue;
|
|
239
|
+
const headingText = heading.trim();
|
|
238
240
|
// Split heading into words
|
|
239
241
|
const words = headingText.split(/\s+/);
|
|
240
242
|
for (const word of words) {
|
|
@@ -255,6 +257,7 @@ function addFromCodeSymbols(content: string, out: string[]): void {
|
|
|
255
257
|
let match: RegExpExecArray | null;
|
|
256
258
|
while ((match = symbolRegex.exec(content)) !== null) {
|
|
257
259
|
const name = match[1];
|
|
260
|
+
if (name === undefined) continue;
|
|
258
261
|
const cleaned = name.toLowerCase();
|
|
259
262
|
if (cleaned.length >= 3 && !STOP_WORDS.has(cleaned)) {
|
|
260
263
|
out.push(cleaned);
|
package/matcher.ts
CHANGED
|
@@ -55,8 +55,10 @@ function escapeRegExp(s: string): string {
|
|
|
55
55
|
function buildKeywordRegex(keyword: string, caseSensitive: boolean): RegExp {
|
|
56
56
|
const escaped = escapeRegExp(keyword);
|
|
57
57
|
const characters = Array.from(keyword);
|
|
58
|
-
const left = WORD_CHAR.test(characters[0]
|
|
59
|
-
|
|
58
|
+
const left = WORD_CHAR.test(characters[0] ?? "")
|
|
59
|
+
? "(?<![\\p{L}\\p{N}_])"
|
|
60
|
+
: "";
|
|
61
|
+
const right = WORD_CHAR.test(characters[characters.length - 1] ?? "")
|
|
60
62
|
? "(?![\\p{L}\\p{N}_])"
|
|
61
63
|
: "";
|
|
62
64
|
return new RegExp(`${left}${escaped}${right}`, caseSensitive ? "gu" : "giu");
|
|
@@ -110,9 +112,18 @@ export class KeywordMatcher {
|
|
|
110
112
|
// window aren't lost.
|
|
111
113
|
const { windowSize } = this.options;
|
|
112
114
|
const sliced = windowSize > 0 && text.length > windowSize;
|
|
113
|
-
//
|
|
114
|
-
const
|
|
115
|
-
|
|
115
|
+
// Preserve the complete preceding code point, including surrogate pairs.
|
|
116
|
+
const windowStart = sliced ? text.length - windowSize : 0;
|
|
117
|
+
let scanStart = Math.max(0, windowStart - 1);
|
|
118
|
+
if (
|
|
119
|
+
scanStart > 0 &&
|
|
120
|
+
/[\uDC00-\uDFFF]/.test(text.charAt(scanStart)) &&
|
|
121
|
+
/[\uD800-\uDBFF]/.test(text.charAt(scanStart - 1))
|
|
122
|
+
) {
|
|
123
|
+
scanStart--;
|
|
124
|
+
}
|
|
125
|
+
const scanText = text.slice(scanStart);
|
|
126
|
+
const minStart = windowStart - scanStart;
|
|
116
127
|
|
|
117
128
|
const results: MatchResult[] = [];
|
|
118
129
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-doc-injector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Inject project docs into Pi context when their filename appears in the conversation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.ts",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "vitest run",
|
|
18
18
|
"test:watch": "vitest",
|
|
19
|
-
"typecheck": "tsc --noEmit",
|
|
19
|
+
"typecheck": "tsc --noEmit && npm run typecheck:core",
|
|
20
|
+
"typecheck:core": "tsc -p tsconfig.core.json",
|
|
20
21
|
"format": "prettier --write .",
|
|
21
22
|
"format:check": "prettier --check .",
|
|
22
23
|
"prepare": "husky"
|
|
@@ -39,18 +40,20 @@
|
|
|
39
40
|
]
|
|
40
41
|
},
|
|
41
42
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
"typebox": "^1.1.38"
|
|
43
|
+
"minimatch": "^11.0.9"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@earendil-works/pi-coding-agent": "
|
|
46
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
47
|
+
"typebox": "*"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": "^1.5.2",
|
|
49
51
|
"@types/node": "^22.0.0",
|
|
50
52
|
"husky": "^9.1.7",
|
|
51
53
|
"prettier": "^3.8.4",
|
|
54
|
+
"typebox": "^1.3.27",
|
|
52
55
|
"typescript": "^5.7.0",
|
|
53
|
-
"vitest": "^
|
|
56
|
+
"vitest": "^4.1.12"
|
|
54
57
|
},
|
|
55
58
|
"publishConfig": {
|
|
56
59
|
"access": "public"
|
package/picomatch.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
declare module "picomatch" {
|
|
2
|
-
interface PicomatchOptions {
|
|
3
|
-
dot?: boolean;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
type Matcher = (input: string) => boolean;
|
|
7
|
-
|
|
8
|
-
function picomatch(
|
|
9
|
-
patterns: string | string[],
|
|
10
|
-
options?: PicomatchOptions,
|
|
11
|
-
): Matcher;
|
|
12
|
-
|
|
13
|
-
export default picomatch;
|
|
14
|
-
}
|