pi-doc-injector 0.6.0 → 0.7.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 +50 -213
- package/audit.ts +186 -0
- package/cache.ts +23 -5
- package/commands.ts +168 -105
- package/config.ts +134 -55
- package/docs/async-subagent-bug.md +4 -196
- package/docs/publish.md +9 -7
- package/docs/test-md.md +16 -1
- package/docs/workflow-md.md +16 -1
- package/globber.ts +2 -6
- package/index.ts +237 -158
- package/injector.ts +90 -5
- package/keyword-gen.ts +157 -36
- package/keyword-llm.ts +24 -16
- package/matcher.ts +46 -19
- package/notifier.ts +36 -36
- package/package.json +10 -6
- package/picomatch.d.ts +4 -1
- package/registry.ts +92 -53
- package/types.ts +9 -1
package/injector.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Context Injector — formats matched docs into a content string suitable for
|
|
3
|
-
* injection as a `CustomMessage` (
|
|
3
|
+
* injection as a `CustomMessage` (from `before_agent_start` or `turn_end`) and
|
|
4
4
|
* sends TUI notifications.
|
|
5
5
|
*
|
|
6
6
|
* The produced content is delivered to the LLM as a `CustomMessage` rather
|
|
7
7
|
* than appended to the system prompt. This keeps the system prompt
|
|
8
8
|
* byte-identical across turns so the provider's prompt cache stays warm.
|
|
9
9
|
*/
|
|
10
|
+
import type { InjectionHistory, InjectionHistoryRecord } from "./audit";
|
|
10
11
|
import type { DocEntry } from "./types";
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -32,14 +33,95 @@ function sanitizeKeywords(keywords: string[]): string[] {
|
|
|
32
33
|
.slice(0, 20);
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Format saved injection audit records for the history command.
|
|
38
|
+
*
|
|
39
|
+
* Audit records are stored oldest-first, so the formatter walks them backward
|
|
40
|
+
* without changing the history object.
|
|
41
|
+
*/
|
|
42
|
+
export function formatInjectionHistory(
|
|
43
|
+
history: InjectionHistory,
|
|
44
|
+
limit: number | "all" = 20,
|
|
45
|
+
): string {
|
|
46
|
+
const total = history.records.length;
|
|
47
|
+
const shown =
|
|
48
|
+
limit === "all" ? total : Math.min(total, Math.max(0, Math.floor(limit)));
|
|
49
|
+
const lines = ["Saved document injection history (newest first)"];
|
|
50
|
+
lines.push(`Showing ${shown} of ${total} readable saved injection records.`);
|
|
51
|
+
|
|
52
|
+
if (total === 0) {
|
|
53
|
+
lines.push(
|
|
54
|
+
history.unavailableMessages > 0
|
|
55
|
+
? "No readable saved injection records were found on this branch."
|
|
56
|
+
: "No saved injection records were found on this branch.",
|
|
57
|
+
);
|
|
58
|
+
} else {
|
|
59
|
+
if (shown < total) {
|
|
60
|
+
lines.push("Older records are omitted by the requested limit.");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (let index = total - 1; index >= total - shown; index--) {
|
|
64
|
+
const record = history.records[index];
|
|
65
|
+
formatInjectionHistoryRecord(record, lines);
|
|
66
|
+
if (index > total - shown) lines.push("");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (history.unavailableMessages > 0) {
|
|
71
|
+
const count = history.unavailableMessages;
|
|
72
|
+
lines.push(
|
|
73
|
+
`Notice: ${count} doc-inject message${count === 1 ? "" : "s"} had legacy, missing, malformed, or unsupported audit metadata; their injection reasons and timing are unknown.`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return lines.join("\n");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function formatInjectionHistoryRecord(
|
|
81
|
+
record: InjectionHistoryRecord,
|
|
82
|
+
lines: string[],
|
|
83
|
+
): void {
|
|
84
|
+
lines.push(
|
|
85
|
+
`${new Date(record.timestamp).toISOString()} — ${record.relativePath}`,
|
|
86
|
+
);
|
|
87
|
+
lines.push(
|
|
88
|
+
` Mode: ${record.mode} (keyword source: ${record.keywordSource})`,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const matchedBySource: Record<"input" | "assistant", string[]> = {
|
|
92
|
+
input: [],
|
|
93
|
+
assistant: [],
|
|
94
|
+
};
|
|
95
|
+
for (const trigger of record.triggers) {
|
|
96
|
+
const keywords = matchedBySource[trigger.source];
|
|
97
|
+
for (const keyword of trigger.matchedKeywords) {
|
|
98
|
+
keywords.push(keyword);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const matchSources: string[] = [];
|
|
102
|
+
for (const source of ["input", "assistant"] as const) {
|
|
103
|
+
const keywords = sanitizeKeywords(matchedBySource[source]);
|
|
104
|
+
if (keywords.length > 0) {
|
|
105
|
+
matchSources.push(`${source}: ${keywords.join(", ")}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
lines.push(
|
|
109
|
+
` Matched from: ${matchSources.length > 0 ? matchSources.join("; ") : "unknown"}`,
|
|
110
|
+
);
|
|
111
|
+
lines.push(` Boundary: ${record.boundary}`);
|
|
112
|
+
if (record.turnIndex !== undefined) {
|
|
113
|
+
lines.push(` Turn index: ${record.turnIndex}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
35
117
|
/**
|
|
36
118
|
* Build the content string for a `CustomMessage` injection from matched
|
|
37
119
|
* documents. This is the payload that gets returned in
|
|
38
|
-
* `before_agent_start`'s `message.content`
|
|
120
|
+
* `before_agent_start`'s `message.content` or a `turn_end` entry draft.
|
|
39
121
|
*/
|
|
40
122
|
export function buildInjectionContent(
|
|
41
|
-
|
|
42
|
-
|
|
123
|
+
entries: DocEntry[],
|
|
124
|
+
matchedKeywords: Map<string, string[]>,
|
|
43
125
|
): string {
|
|
44
126
|
if (entries.length === 0) return "";
|
|
45
127
|
|
|
@@ -77,6 +159,9 @@ export function notifyInjection(
|
|
|
77
159
|
for (const entry of entries) {
|
|
78
160
|
const keywords = matchedKeywords.get(entry.filePath) ?? [];
|
|
79
161
|
const kwList = keywords.join(", ");
|
|
80
|
-
ui.notify(
|
|
162
|
+
ui.notify(
|
|
163
|
+
`📄 Injected: ${entry.relativePath} (matched: ${kwList})`,
|
|
164
|
+
"info",
|
|
165
|
+
);
|
|
81
166
|
}
|
|
82
167
|
}
|
package/keyword-gen.ts
CHANGED
|
@@ -13,41 +13,164 @@
|
|
|
13
13
|
|
|
14
14
|
const STOP_WORDS = new Set<string>([
|
|
15
15
|
// Articles
|
|
16
|
-
"a",
|
|
16
|
+
"a",
|
|
17
|
+
"an",
|
|
18
|
+
"the",
|
|
17
19
|
// Pronouns
|
|
18
|
-
"i",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
20
|
+
"i",
|
|
21
|
+
"you",
|
|
22
|
+
"he",
|
|
23
|
+
"she",
|
|
24
|
+
"it",
|
|
25
|
+
"we",
|
|
26
|
+
"they",
|
|
27
|
+
"me",
|
|
28
|
+
"him",
|
|
29
|
+
"her",
|
|
30
|
+
"us",
|
|
31
|
+
"them",
|
|
32
|
+
"my",
|
|
33
|
+
"your",
|
|
34
|
+
"his",
|
|
35
|
+
"its",
|
|
36
|
+
"our",
|
|
37
|
+
"their",
|
|
38
|
+
"this",
|
|
39
|
+
"that",
|
|
40
|
+
"these",
|
|
41
|
+
"those",
|
|
42
|
+
"who",
|
|
43
|
+
"whom",
|
|
44
|
+
"whose",
|
|
45
|
+
"which",
|
|
46
|
+
"what",
|
|
23
47
|
// Prepositions
|
|
24
|
-
"in",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
48
|
+
"in",
|
|
49
|
+
"on",
|
|
50
|
+
"at",
|
|
51
|
+
"by",
|
|
52
|
+
"for",
|
|
53
|
+
"with",
|
|
54
|
+
"about",
|
|
55
|
+
"to",
|
|
56
|
+
"from",
|
|
57
|
+
"of",
|
|
58
|
+
"into",
|
|
59
|
+
"onto",
|
|
60
|
+
"upon",
|
|
61
|
+
"over",
|
|
62
|
+
"under",
|
|
63
|
+
"between",
|
|
64
|
+
"among",
|
|
65
|
+
"through",
|
|
66
|
+
"during",
|
|
67
|
+
"before",
|
|
68
|
+
"after",
|
|
69
|
+
"above",
|
|
70
|
+
"below",
|
|
71
|
+
"up",
|
|
72
|
+
"down",
|
|
73
|
+
"out",
|
|
74
|
+
"off",
|
|
29
75
|
// Conjunctions
|
|
30
|
-
"and",
|
|
31
|
-
"
|
|
32
|
-
"
|
|
76
|
+
"and",
|
|
77
|
+
"but",
|
|
78
|
+
"or",
|
|
79
|
+
"nor",
|
|
80
|
+
"so",
|
|
81
|
+
"yet",
|
|
82
|
+
"for",
|
|
83
|
+
"if",
|
|
84
|
+
"then",
|
|
85
|
+
"than",
|
|
86
|
+
"as",
|
|
87
|
+
"when",
|
|
88
|
+
"while",
|
|
89
|
+
"because",
|
|
90
|
+
"since",
|
|
91
|
+
"although",
|
|
92
|
+
"though",
|
|
33
93
|
// Auxiliary/modal verbs
|
|
34
|
-
"is",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
94
|
+
"is",
|
|
95
|
+
"are",
|
|
96
|
+
"was",
|
|
97
|
+
"were",
|
|
98
|
+
"be",
|
|
99
|
+
"been",
|
|
100
|
+
"being",
|
|
101
|
+
"have",
|
|
102
|
+
"has",
|
|
103
|
+
"had",
|
|
104
|
+
"having",
|
|
105
|
+
"do",
|
|
106
|
+
"does",
|
|
107
|
+
"did",
|
|
108
|
+
"doing",
|
|
109
|
+
"will",
|
|
110
|
+
"would",
|
|
111
|
+
"shall",
|
|
112
|
+
"should",
|
|
113
|
+
"can",
|
|
114
|
+
"could",
|
|
115
|
+
"may",
|
|
116
|
+
"might",
|
|
117
|
+
"must",
|
|
39
118
|
// Common adverbs
|
|
40
|
-
"not",
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
119
|
+
"not",
|
|
120
|
+
"no",
|
|
121
|
+
"yes",
|
|
122
|
+
"just",
|
|
123
|
+
"only",
|
|
124
|
+
"also",
|
|
125
|
+
"too",
|
|
126
|
+
"very",
|
|
127
|
+
"now",
|
|
128
|
+
"then",
|
|
129
|
+
"here",
|
|
130
|
+
"there",
|
|
131
|
+
"where",
|
|
132
|
+
"how",
|
|
133
|
+
"why",
|
|
134
|
+
"all",
|
|
135
|
+
"each",
|
|
136
|
+
"every",
|
|
137
|
+
"both",
|
|
138
|
+
"few",
|
|
139
|
+
"more",
|
|
140
|
+
"most",
|
|
141
|
+
"some",
|
|
142
|
+
"any",
|
|
143
|
+
"other",
|
|
144
|
+
"another",
|
|
145
|
+
"such",
|
|
146
|
+
"much",
|
|
147
|
+
"many",
|
|
148
|
+
"little",
|
|
149
|
+
"less",
|
|
46
150
|
// Common content-less words
|
|
47
|
-
"get",
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
151
|
+
"get",
|
|
152
|
+
"set",
|
|
153
|
+
"put",
|
|
154
|
+
"use",
|
|
155
|
+
"make",
|
|
156
|
+
"see",
|
|
157
|
+
"need",
|
|
158
|
+
"one",
|
|
159
|
+
"two",
|
|
160
|
+
"three",
|
|
161
|
+
"first",
|
|
162
|
+
"second",
|
|
163
|
+
"third",
|
|
164
|
+
"using",
|
|
165
|
+
"used",
|
|
166
|
+
"into",
|
|
167
|
+
"onto",
|
|
168
|
+
"new",
|
|
169
|
+
"note",
|
|
170
|
+
"notes",
|
|
171
|
+
"example",
|
|
172
|
+
"examples",
|
|
173
|
+
"todo",
|
|
51
174
|
]);
|
|
52
175
|
|
|
53
176
|
/**
|
|
@@ -65,10 +188,7 @@ const STOP_WORDS = new Set<string>([
|
|
|
65
188
|
* @param content - The full file content
|
|
66
189
|
* @returns Up to 20 deduplicated keyword strings
|
|
67
190
|
*/
|
|
68
|
-
export function generateKeywords(
|
|
69
|
-
fileName: string,
|
|
70
|
-
content: string,
|
|
71
|
-
): string[] {
|
|
191
|
+
export function generateKeywords(fileName: string, content: string): string[] {
|
|
72
192
|
const keywords: string[] = [];
|
|
73
193
|
|
|
74
194
|
// Source 1: Filename parts
|
|
@@ -102,7 +222,7 @@ function addFromFilename(fileName: string, out: string[]): void {
|
|
|
102
222
|
const parts = nameWithoutExt.split(/[-_.\s]+/);
|
|
103
223
|
|
|
104
224
|
for (const part of parts) {
|
|
105
|
-
const cleaned = part.replace(/[
|
|
225
|
+
const cleaned = part.replace(/[^\p{L}\p{N}]/gu, "").toLowerCase();
|
|
106
226
|
if (cleaned.length >= 3 && !STOP_WORDS.has(cleaned)) {
|
|
107
227
|
out.push(cleaned);
|
|
108
228
|
}
|
|
@@ -118,7 +238,7 @@ function addFromHeadings(content: string, out: string[]): void {
|
|
|
118
238
|
// Split heading into words
|
|
119
239
|
const words = headingText.split(/\s+/);
|
|
120
240
|
for (const word of words) {
|
|
121
|
-
const cleaned = word.replace(/[
|
|
241
|
+
const cleaned = word.replace(/[^\p{L}\p{N}]/gu, "").toLowerCase();
|
|
122
242
|
if (cleaned.length >= 3 && !STOP_WORDS.has(cleaned)) {
|
|
123
243
|
out.push(cleaned);
|
|
124
244
|
}
|
|
@@ -130,7 +250,8 @@ function addFromHeadings(content: string, out: string[]): void {
|
|
|
130
250
|
function addFromCodeSymbols(content: string, out: string[]): void {
|
|
131
251
|
// Match: function name, class name, const name, interface name, type name, enum name
|
|
132
252
|
// Also: export function, export class, export const, etc.
|
|
133
|
-
const symbolRegex =
|
|
253
|
+
const symbolRegex =
|
|
254
|
+
/(?:export\s+)?(?:async\s+)?(?:function|class|const|interface|type|enum)\s+([\p{L}_][\p{L}\p{N}_]*)/gmu;
|
|
134
255
|
let match: RegExpExecArray | null;
|
|
135
256
|
while ((match = symbolRegex.exec(content)) !== null) {
|
|
136
257
|
const name = match[1];
|
package/keyword-llm.ts
CHANGED
|
@@ -23,21 +23,29 @@ export interface FileInput {
|
|
|
23
23
|
export function buildKeywordGenPrompt(files: FileInput[]): string {
|
|
24
24
|
if (files.length === 0) return "";
|
|
25
25
|
|
|
26
|
-
const fileDescriptions = files
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
const fileDescriptions = files
|
|
27
|
+
.map((f, i) => {
|
|
28
|
+
const existing =
|
|
29
|
+
f.existingKeywords.length > 0
|
|
30
|
+
? ` Existing keywords: ${f.existingKeywords.join(", ")}`
|
|
31
|
+
: "";
|
|
32
|
+
// Escape markdown special chars in path to prevent prompt injection
|
|
33
|
+
const safePath = f.path.replace(/[*`\[\]]/g, "\\$&");
|
|
34
|
+
// Escape backticks in snippet to prevent breaking code fences
|
|
35
|
+
const safeSnippet = f.snippet.replace(/```/g, "'''");
|
|
36
|
+
return `${i + 1}. **${safePath}**\n${existing}\n Snippet:\n\`\`\`\n${safeSnippet}\n\`\`\``;
|
|
37
|
+
})
|
|
38
|
+
.join("\n\n");
|
|
39
|
+
|
|
40
|
+
const expectedOutput = files
|
|
41
|
+
.map((f) => {
|
|
42
|
+
const safePath = f.path.replace(/[*`\[\]]/g, "\\$&");
|
|
43
|
+
return ` - "${safePath}": keywords array incorporating relevant existing keywords [${f.existingKeywords
|
|
44
|
+
.slice(0, 5)
|
|
45
|
+
.map((k) => `"${k}"`)
|
|
46
|
+
.join(", ")}${f.existingKeywords.length > 5 ? ", ..." : ""}]`;
|
|
47
|
+
})
|
|
48
|
+
.join("\n");
|
|
41
49
|
|
|
42
50
|
return `Generate documentation keywords for the following ${files.length} file(s). For each file, read the snippet and produce 3-10 concise, searchable keywords that someone might type when looking for this documentation.
|
|
43
51
|
|
|
@@ -54,4 +62,4 @@ After analysis, call the \`_doc_injector_keywords\` tool with a \`keywords\` arr
|
|
|
54
62
|
${expectedOutput}
|
|
55
63
|
|
|
56
64
|
Do not output any other text — just call the tool with the keywords.`;
|
|
57
|
-
}
|
|
65
|
+
}
|
package/matcher.ts
CHANGED
|
@@ -10,26 +10,33 @@ import { DEFAULT_MATCHER_OPTIONS } from "./types";
|
|
|
10
10
|
* - Content array with text/thinking blocks
|
|
11
11
|
*/
|
|
12
12
|
export function extractText(content: unknown): string {
|
|
13
|
+
return extractTextBlocks(content).join("\n");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Keep completed blocks separate so matching cannot manufacture cross-block hits. */
|
|
17
|
+
export function extractTextBlocks(content: unknown): string[] {
|
|
13
18
|
if (typeof content === "string") {
|
|
14
|
-
return content;
|
|
19
|
+
return [content];
|
|
15
20
|
}
|
|
16
21
|
if (!Array.isArray(content)) {
|
|
17
|
-
return
|
|
22
|
+
return [];
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
const parts: string[] = [];
|
|
21
26
|
for (const block of content) {
|
|
22
27
|
if (!block || typeof block !== "object") continue;
|
|
23
28
|
const b = block as Record<string, unknown>;
|
|
24
|
-
if (
|
|
29
|
+
if (b.type === "text" && typeof b.text === "string") {
|
|
25
30
|
parts.push(b.text);
|
|
31
|
+
} else if (b.type === "thinking" && typeof b.thinking === "string") {
|
|
32
|
+
parts.push(b.thinking);
|
|
26
33
|
}
|
|
27
34
|
}
|
|
28
|
-
return parts
|
|
35
|
+
return parts;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
/** Characters treated as part of a "word" for boundary detection. */
|
|
32
|
-
const WORD_CHAR = /[
|
|
39
|
+
const WORD_CHAR = /[\p{L}\p{N}_]/u;
|
|
33
40
|
|
|
34
41
|
/** Escape a string so it can be embedded literally in a RegExp. */
|
|
35
42
|
function escapeRegExp(s: string): string {
|
|
@@ -47,9 +54,12 @@ function escapeRegExp(s: string): string {
|
|
|
47
54
|
*/
|
|
48
55
|
function buildKeywordRegex(keyword: string, caseSensitive: boolean): RegExp {
|
|
49
56
|
const escaped = escapeRegExp(keyword);
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
|
|
57
|
+
const characters = Array.from(keyword);
|
|
58
|
+
const left = WORD_CHAR.test(characters[0]) ? "(?<![\\p{L}\\p{N}_])" : "";
|
|
59
|
+
const right = WORD_CHAR.test(characters[characters.length - 1])
|
|
60
|
+
? "(?![\\p{L}\\p{N}_])"
|
|
61
|
+
: "";
|
|
62
|
+
return new RegExp(`${left}${escaped}${right}`, caseSensitive ? "gu" : "giu");
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
interface CompiledEntry {
|
|
@@ -69,13 +79,24 @@ export class KeywordMatcher {
|
|
|
69
79
|
*/
|
|
70
80
|
constructor(entries: DocEntry[], options?: Partial<MatcherOptions>) {
|
|
71
81
|
this.options = { ...DEFAULT_MATCHER_OPTIONS, ...options };
|
|
72
|
-
this.compiled = entries.map((entry) =>
|
|
73
|
-
|
|
74
|
-
patterns
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
82
|
+
this.compiled = entries.map((entry) => {
|
|
83
|
+
const seen = new Set<string>();
|
|
84
|
+
const patterns = (entry.keywords ?? [])
|
|
85
|
+
.filter(
|
|
86
|
+
(kw): kw is string => typeof kw === "string" && kw.trim().length > 0,
|
|
87
|
+
)
|
|
88
|
+
.filter((kw) => {
|
|
89
|
+
const key = this.options.caseSensitive ? kw : kw.toLowerCase();
|
|
90
|
+
if (seen.has(key)) return false;
|
|
91
|
+
seen.add(key);
|
|
92
|
+
return true;
|
|
93
|
+
})
|
|
94
|
+
.map((keyword) => ({
|
|
95
|
+
keyword,
|
|
96
|
+
regex: buildKeywordRegex(keyword, this.options.caseSensitive),
|
|
97
|
+
}));
|
|
98
|
+
return { entry, patterns };
|
|
99
|
+
});
|
|
79
100
|
}
|
|
80
101
|
|
|
81
102
|
/** Match text against keyword index. Returns matching docs with hit details. */
|
|
@@ -88,8 +109,10 @@ export class KeywordMatcher {
|
|
|
88
109
|
// accumulate across chunks in the caller, so keywords that scroll past the
|
|
89
110
|
// window aren't lost.
|
|
90
111
|
const { windowSize } = this.options;
|
|
91
|
-
const
|
|
92
|
-
|
|
112
|
+
const sliced = windowSize > 0 && text.length > windowSize;
|
|
113
|
+
// Keep one preceding character so the left word boundary sees the true text.
|
|
114
|
+
const scanText = sliced ? text.slice(-windowSize - 1) : text;
|
|
115
|
+
const minStart = sliced ? 1 : 0;
|
|
93
116
|
|
|
94
117
|
const results: MatchResult[] = [];
|
|
95
118
|
|
|
@@ -99,8 +122,12 @@ export class KeywordMatcher {
|
|
|
99
122
|
|
|
100
123
|
const matchedKeywords: string[] = [];
|
|
101
124
|
for (const { keyword, regex } of patterns) {
|
|
102
|
-
|
|
125
|
+
regex.lastIndex = 0;
|
|
126
|
+
let match: RegExpExecArray | null;
|
|
127
|
+
while ((match = regex.exec(scanText)) !== null) {
|
|
128
|
+
if (match.index < minStart) continue;
|
|
103
129
|
matchedKeywords.push(keyword);
|
|
130
|
+
break;
|
|
104
131
|
}
|
|
105
132
|
}
|
|
106
133
|
|
|
@@ -115,4 +142,4 @@ export class KeywordMatcher {
|
|
|
115
142
|
|
|
116
143
|
return results;
|
|
117
144
|
}
|
|
118
|
-
}
|
|
145
|
+
}
|
package/notifier.ts
CHANGED
|
@@ -21,51 +21,51 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
21
21
|
export type NotifierLevel = "info" | "warning" | "error";
|
|
22
22
|
|
|
23
23
|
export interface Notifier {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
/** Show an informational message. */
|
|
25
|
+
info(message: string): void;
|
|
26
|
+
/** Show a warning. */
|
|
27
|
+
warn(message: string): void;
|
|
28
|
+
/** Show an error. */
|
|
29
|
+
error(message: string): void;
|
|
30
|
+
/**
|
|
31
|
+
* Bind a context. Flushes any buffered messages via `ctx.ui.notify()`
|
|
32
|
+
* in arrival order. Idempotent: re-calling replaces the context and
|
|
33
|
+
* clears the buffer (already-flushed messages are not re-sent).
|
|
34
|
+
*/
|
|
35
|
+
setContext(ctx: ExtensionContext): void;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/** Production notifier. Buffers until a context is bound. */
|
|
39
39
|
export class ExtensionNotifier implements Notifier {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
private ctx: ExtensionContext | null = null;
|
|
41
|
+
private buffer: Array<{ level: NotifierLevel; message: string }> = [];
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
43
|
+
setContext(ctx: ExtensionContext): void {
|
|
44
|
+
this.ctx = ctx;
|
|
45
|
+
const pending = this.buffer;
|
|
46
|
+
this.buffer = [];
|
|
47
|
+
for (const { level, message } of pending) {
|
|
48
|
+
ctx.ui.notify(message, level);
|
|
50
49
|
}
|
|
50
|
+
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
info(message: string): void {
|
|
53
|
+
this.emit("info", message);
|
|
54
|
+
}
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
warn(message: string): void {
|
|
57
|
+
this.emit("warning", message);
|
|
58
|
+
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
error(message: string): void {
|
|
61
|
+
this.emit("error", message);
|
|
62
|
+
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
64
|
+
private emit(level: NotifierLevel, message: string): void {
|
|
65
|
+
if (this.ctx) {
|
|
66
|
+
this.ctx.ui.notify(message, level);
|
|
67
|
+
} else {
|
|
68
|
+
this.buffer.push({ level, message });
|
|
70
69
|
}
|
|
70
|
+
}
|
|
71
71
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-doc-injector",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Inject project docs into Pi context when their filename appears in the conversation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.ts",
|
|
7
7
|
"exports": {
|
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "vitest run",
|
|
18
18
|
"test:watch": "vitest",
|
|
19
|
-
"typecheck": "tsc --noEmit"
|
|
19
|
+
"typecheck": "tsc --noEmit",
|
|
20
|
+
"format": "prettier --write .",
|
|
21
|
+
"format:check": "prettier --check .",
|
|
22
|
+
"prepare": "husky"
|
|
20
23
|
},
|
|
21
24
|
"keywords": [
|
|
22
25
|
"pi-package",
|
|
@@ -40,15 +43,16 @@
|
|
|
40
43
|
"typebox": "^1.1.38"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
|
-
"@earendil-works/pi-coding-agent": "^
|
|
46
|
+
"@earendil-works/pi-coding-agent": "^1.5.2"
|
|
44
47
|
},
|
|
45
48
|
"devDependencies": {
|
|
46
49
|
"@types/node": "^22.0.0",
|
|
50
|
+
"husky": "^9.1.7",
|
|
51
|
+
"prettier": "^3.8.4",
|
|
47
52
|
"typescript": "^5.7.0",
|
|
48
53
|
"vitest": "^3.0.0"
|
|
49
54
|
},
|
|
50
55
|
"publishConfig": {
|
|
51
56
|
"access": "public"
|
|
52
|
-
}
|
|
53
|
-
"packageManager": "npm@11.5.2"
|
|
57
|
+
}
|
|
54
58
|
}
|
package/picomatch.d.ts
CHANGED
|
@@ -5,7 +5,10 @@ declare module "picomatch" {
|
|
|
5
5
|
|
|
6
6
|
type Matcher = (input: string) => boolean;
|
|
7
7
|
|
|
8
|
-
function picomatch(
|
|
8
|
+
function picomatch(
|
|
9
|
+
patterns: string | string[],
|
|
10
|
+
options?: PicomatchOptions,
|
|
11
|
+
): Matcher;
|
|
9
12
|
|
|
10
13
|
export default picomatch;
|
|
11
14
|
}
|