pi-doc-injector 0.5.3 → 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 -212
- package/audit.ts +186 -0
- package/cache.ts +23 -5
- package/commands.ts +168 -105
- package/config.ts +134 -61
- 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 +253 -157
- package/injector.ts +90 -5
- package/keyword-gen.ts +157 -36
- package/keyword-llm.ts +24 -16
- package/matcher.ts +86 -25
- package/notifier.ts +37 -37
- package/package.json +13 -7
- package/picomatch.d.ts +4 -1
- package/registry.ts +95 -55
- package/types.ts +26 -2
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,53 +10,124 @@ 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;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Characters treated as part of a "word" for boundary detection. */
|
|
39
|
+
const WORD_CHAR = /[\p{L}\p{N}_]/u;
|
|
40
|
+
|
|
41
|
+
/** Escape a string so it can be embedded literally in a RegExp. */
|
|
42
|
+
function escapeRegExp(s: string): string {
|
|
43
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Compile a keyword into a regex that matches it literally with smart word
|
|
48
|
+
* boundaries.
|
|
49
|
+
*
|
|
50
|
+
* Boundaries are only asserted on an edge when the adjacent keyword character
|
|
51
|
+
* is a word character ([A-Za-z0-9_]). This gives word-boundary semantics for
|
|
52
|
+
* normal keywords ("test" won't match "latest" or "testing") while still
|
|
53
|
+
* matching symbol keywords literally ("$", "C++", "func()", "[test]", "a|b").
|
|
54
|
+
*/
|
|
55
|
+
function buildKeywordRegex(keyword: string, caseSensitive: boolean): RegExp {
|
|
56
|
+
const escaped = escapeRegExp(keyword);
|
|
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");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface CompiledEntry {
|
|
66
|
+
entry: DocEntry;
|
|
67
|
+
patterns: Array<{ keyword: string; regex: RegExp }>;
|
|
29
68
|
}
|
|
30
69
|
|
|
31
70
|
export class KeywordMatcher {
|
|
32
71
|
private options: MatcherOptions;
|
|
72
|
+
// Regexes are precompiled once per matcher so streaming re-matches (which run
|
|
73
|
+
// on every chunk) don't recompile patterns on each call.
|
|
74
|
+
private compiled: CompiledEntry[];
|
|
33
75
|
|
|
34
76
|
/**
|
|
35
77
|
* @param entries - The document entries to match against
|
|
36
78
|
* @param options - Optional matcher settings (merged with defaults)
|
|
37
79
|
*/
|
|
38
|
-
constructor(
|
|
80
|
+
constructor(entries: DocEntry[], options?: Partial<MatcherOptions>) {
|
|
39
81
|
this.options = { ...DEFAULT_MATCHER_OPTIONS, ...options };
|
|
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
|
+
});
|
|
40
100
|
}
|
|
41
101
|
|
|
42
102
|
/** Match text against keyword index. Returns matching docs with hit details. */
|
|
43
103
|
match(text: string): MatchResult[] {
|
|
44
|
-
if (!text || this.
|
|
104
|
+
if (!text || this.compiled.length === 0) return [];
|
|
105
|
+
|
|
106
|
+
// Rolling tail window: only scan the last `windowSize` chars. Keeps the
|
|
107
|
+
// per-chunk cost bounded on long streaming messages. Callers that need the
|
|
108
|
+
// full text (one-shot user input) pass windowSize 0. Matches still
|
|
109
|
+
// accumulate across chunks in the caller, so keywords that scroll past the
|
|
110
|
+
// window aren't lost.
|
|
111
|
+
const { windowSize } = this.options;
|
|
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;
|
|
45
116
|
|
|
46
117
|
const results: MatchResult[] = [];
|
|
47
118
|
|
|
48
|
-
for (const entry of this.
|
|
119
|
+
for (const { entry, patterns } of this.compiled) {
|
|
49
120
|
if (entry.injected) continue;
|
|
50
|
-
|
|
51
|
-
// Skip entries with no keywords (empty array or falsy)
|
|
52
|
-
if (!entry.keywords || entry.keywords.length === 0) continue;
|
|
121
|
+
if (patterns.length === 0) continue;
|
|
53
122
|
|
|
54
123
|
const matchedKeywords: string[] = [];
|
|
55
|
-
for (const keyword of
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
124
|
+
for (const { keyword, regex } of patterns) {
|
|
125
|
+
regex.lastIndex = 0;
|
|
126
|
+
let match: RegExpExecArray | null;
|
|
127
|
+
while ((match = regex.exec(scanText)) !== null) {
|
|
128
|
+
if (match.index < minStart) continue;
|
|
59
129
|
matchedKeywords.push(keyword);
|
|
130
|
+
break;
|
|
60
131
|
}
|
|
61
132
|
}
|
|
62
133
|
|
|
@@ -71,14 +142,4 @@ export class KeywordMatcher {
|
|
|
71
142
|
|
|
72
143
|
return results;
|
|
73
144
|
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Check if a single keyword matches the given text.
|
|
77
|
-
* Uses simple substring inclusion (case-insensitive by default).
|
|
78
|
-
*/
|
|
79
|
-
private keywordMatches(text: string, keyword: string): boolean {
|
|
80
|
-
const search = this.options.caseSensitive ? text : text.toLowerCase();
|
|
81
|
-
const kw = this.options.caseSensitive ? keyword : keyword.toLowerCase();
|
|
82
|
-
return search.includes(kw);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
145
|
+
}
|
package/notifier.ts
CHANGED
|
@@ -16,56 +16,56 @@
|
|
|
16
16
|
* satisfying the `Notifier` interface (or a `vi.fn()` spy) — no real
|
|
17
17
|
* extension context is needed.
|
|
18
18
|
*/
|
|
19
|
-
import type { ExtensionContext } from "@
|
|
19
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
20
20
|
|
|
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
|
}
|