pi-doc-injector 0.6.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 +76 -201
- package/audit.ts +186 -0
- package/cache.ts +55 -7
- 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/typescript-hardening.md +38 -0
- package/docs/workflow-md.md +16 -1
- package/globber.ts +29 -12
- package/index.ts +262 -187
- package/injector.ts +93 -5
- package/keyword-gen.ts +161 -37
- package/keyword-llm.ts +24 -16
- package/matcher.ts +57 -19
- package/notifier.ts +36 -36
- package/package.json +16 -9
- package/registry.ts +92 -53
- package/types.ts +9 -1
- package/picomatch.d.ts +0 -11
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,98 @@ 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
|
+
if (record === undefined) {
|
|
66
|
+
throw new Error("Injection history must not contain empty records");
|
|
67
|
+
}
|
|
68
|
+
formatInjectionHistoryRecord(record, lines);
|
|
69
|
+
if (index > total - shown) lines.push("");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (history.unavailableMessages > 0) {
|
|
74
|
+
const count = history.unavailableMessages;
|
|
75
|
+
lines.push(
|
|
76
|
+
`Notice: ${count} doc-inject message${count === 1 ? "" : "s"} had legacy, missing, malformed, or unsupported audit metadata; their injection reasons and timing are unknown.`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function formatInjectionHistoryRecord(
|
|
84
|
+
record: InjectionHistoryRecord,
|
|
85
|
+
lines: string[],
|
|
86
|
+
): void {
|
|
87
|
+
lines.push(
|
|
88
|
+
`${new Date(record.timestamp).toISOString()} — ${record.relativePath}`,
|
|
89
|
+
);
|
|
90
|
+
lines.push(
|
|
91
|
+
` Mode: ${record.mode} (keyword source: ${record.keywordSource})`,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const matchedBySource: Record<"input" | "assistant", string[]> = {
|
|
95
|
+
input: [],
|
|
96
|
+
assistant: [],
|
|
97
|
+
};
|
|
98
|
+
for (const trigger of record.triggers) {
|
|
99
|
+
const keywords = matchedBySource[trigger.source];
|
|
100
|
+
for (const keyword of trigger.matchedKeywords) {
|
|
101
|
+
keywords.push(keyword);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const matchSources: string[] = [];
|
|
105
|
+
for (const source of ["input", "assistant"] as const) {
|
|
106
|
+
const keywords = sanitizeKeywords(matchedBySource[source]);
|
|
107
|
+
if (keywords.length > 0) {
|
|
108
|
+
matchSources.push(`${source}: ${keywords.join(", ")}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
lines.push(
|
|
112
|
+
` Matched from: ${matchSources.length > 0 ? matchSources.join("; ") : "unknown"}`,
|
|
113
|
+
);
|
|
114
|
+
lines.push(` Boundary: ${record.boundary}`);
|
|
115
|
+
if (record.turnIndex !== undefined) {
|
|
116
|
+
lines.push(` Turn index: ${record.turnIndex}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
35
120
|
/**
|
|
36
121
|
* Build the content string for a `CustomMessage` injection from matched
|
|
37
122
|
* documents. This is the payload that gets returned in
|
|
38
|
-
* `before_agent_start`'s `message.content`
|
|
123
|
+
* `before_agent_start`'s `message.content` or a `turn_end` entry draft.
|
|
39
124
|
*/
|
|
40
125
|
export function buildInjectionContent(
|
|
41
|
-
|
|
42
|
-
|
|
126
|
+
entries: DocEntry[],
|
|
127
|
+
matchedKeywords: Map<string, string[]>,
|
|
43
128
|
): string {
|
|
44
129
|
if (entries.length === 0) return "";
|
|
45
130
|
|
|
@@ -77,6 +162,9 @@ export function notifyInjection(
|
|
|
77
162
|
for (const entry of entries) {
|
|
78
163
|
const keywords = matchedKeywords.get(entry.filePath) ?? [];
|
|
79
164
|
const kwList = keywords.join(", ");
|
|
80
|
-
ui.notify(
|
|
165
|
+
ui.notify(
|
|
166
|
+
`📄 Injected: ${entry.relativePath} (matched: ${kwList})`,
|
|
167
|
+
"info",
|
|
168
|
+
);
|
|
81
169
|
}
|
|
82
170
|
}
|
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
|
}
|
|
@@ -114,11 +234,13 @@ function addFromHeadings(content: string, out: string[]): void {
|
|
|
114
234
|
const headingRegex = /^#{1,6}\s+(.+)$/gm;
|
|
115
235
|
let match: RegExpExecArray | null;
|
|
116
236
|
while ((match = headingRegex.exec(content)) !== null) {
|
|
117
|
-
const
|
|
237
|
+
const heading = match[1];
|
|
238
|
+
if (heading === undefined) continue;
|
|
239
|
+
const headingText = heading.trim();
|
|
118
240
|
// Split heading into words
|
|
119
241
|
const words = headingText.split(/\s+/);
|
|
120
242
|
for (const word of words) {
|
|
121
|
-
const cleaned = word.replace(/[
|
|
243
|
+
const cleaned = word.replace(/[^\p{L}\p{N}]/gu, "").toLowerCase();
|
|
122
244
|
if (cleaned.length >= 3 && !STOP_WORDS.has(cleaned)) {
|
|
123
245
|
out.push(cleaned);
|
|
124
246
|
}
|
|
@@ -130,10 +252,12 @@ function addFromHeadings(content: string, out: string[]): void {
|
|
|
130
252
|
function addFromCodeSymbols(content: string, out: string[]): void {
|
|
131
253
|
// Match: function name, class name, const name, interface name, type name, enum name
|
|
132
254
|
// Also: export function, export class, export const, etc.
|
|
133
|
-
const symbolRegex =
|
|
255
|
+
const symbolRegex =
|
|
256
|
+
/(?:export\s+)?(?:async\s+)?(?:function|class|const|interface|type|enum)\s+([\p{L}_][\p{L}\p{N}_]*)/gmu;
|
|
134
257
|
let match: RegExpExecArray | null;
|
|
135
258
|
while ((match = symbolRegex.exec(content)) !== null) {
|
|
136
259
|
const name = match[1];
|
|
260
|
+
if (name === undefined) continue;
|
|
137
261
|
const cleaned = name.toLowerCase();
|
|
138
262
|
if (cleaned.length >= 3 && !STOP_WORDS.has(cleaned)) {
|
|
139
263
|
out.push(cleaned);
|
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,14 @@ 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] ?? "")
|
|
59
|
+
? "(?<![\\p{L}\\p{N}_])"
|
|
60
|
+
: "";
|
|
61
|
+
const right = WORD_CHAR.test(characters[characters.length - 1] ?? "")
|
|
62
|
+
? "(?![\\p{L}\\p{N}_])"
|
|
63
|
+
: "";
|
|
64
|
+
return new RegExp(`${left}${escaped}${right}`, caseSensitive ? "gu" : "giu");
|
|
53
65
|
}
|
|
54
66
|
|
|
55
67
|
interface CompiledEntry {
|
|
@@ -69,13 +81,24 @@ export class KeywordMatcher {
|
|
|
69
81
|
*/
|
|
70
82
|
constructor(entries: DocEntry[], options?: Partial<MatcherOptions>) {
|
|
71
83
|
this.options = { ...DEFAULT_MATCHER_OPTIONS, ...options };
|
|
72
|
-
this.compiled = entries.map((entry) =>
|
|
73
|
-
|
|
74
|
-
patterns
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
84
|
+
this.compiled = entries.map((entry) => {
|
|
85
|
+
const seen = new Set<string>();
|
|
86
|
+
const patterns = (entry.keywords ?? [])
|
|
87
|
+
.filter(
|
|
88
|
+
(kw): kw is string => typeof kw === "string" && kw.trim().length > 0,
|
|
89
|
+
)
|
|
90
|
+
.filter((kw) => {
|
|
91
|
+
const key = this.options.caseSensitive ? kw : kw.toLowerCase();
|
|
92
|
+
if (seen.has(key)) return false;
|
|
93
|
+
seen.add(key);
|
|
94
|
+
return true;
|
|
95
|
+
})
|
|
96
|
+
.map((keyword) => ({
|
|
97
|
+
keyword,
|
|
98
|
+
regex: buildKeywordRegex(keyword, this.options.caseSensitive),
|
|
99
|
+
}));
|
|
100
|
+
return { entry, patterns };
|
|
101
|
+
});
|
|
79
102
|
}
|
|
80
103
|
|
|
81
104
|
/** Match text against keyword index. Returns matching docs with hit details. */
|
|
@@ -88,8 +111,19 @@ export class KeywordMatcher {
|
|
|
88
111
|
// accumulate across chunks in the caller, so keywords that scroll past the
|
|
89
112
|
// window aren't lost.
|
|
90
113
|
const { windowSize } = this.options;
|
|
91
|
-
const
|
|
92
|
-
|
|
114
|
+
const sliced = windowSize > 0 && text.length > windowSize;
|
|
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;
|
|
93
127
|
|
|
94
128
|
const results: MatchResult[] = [];
|
|
95
129
|
|
|
@@ -99,8 +133,12 @@ export class KeywordMatcher {
|
|
|
99
133
|
|
|
100
134
|
const matchedKeywords: string[] = [];
|
|
101
135
|
for (const { keyword, regex } of patterns) {
|
|
102
|
-
|
|
136
|
+
regex.lastIndex = 0;
|
|
137
|
+
let match: RegExpExecArray | null;
|
|
138
|
+
while ((match = regex.exec(scanText)) !== null) {
|
|
139
|
+
if (match.index < minStart) continue;
|
|
103
140
|
matchedKeywords.push(keyword);
|
|
141
|
+
break;
|
|
104
142
|
}
|
|
105
143
|
}
|
|
106
144
|
|
|
@@ -115,4 +153,4 @@ export class KeywordMatcher {
|
|
|
115
153
|
|
|
116
154
|
return results;
|
|
117
155
|
}
|
|
118
|
-
}
|
|
156
|
+
}
|
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
|
}
|