@grimoire-cc/router 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/args.d.ts +18 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +45 -0
- package/dist/args.js.map +1 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -0
- package/dist/enforce.d.ts +25 -0
- package/dist/enforce.d.ts.map +1 -0
- package/dist/enforce.js +140 -0
- package/dist/enforce.js.map +1 -0
- package/dist/filtering.d.ts +20 -0
- package/dist/filtering.d.ts.map +1 -0
- package/dist/filtering.js +30 -0
- package/dist/filtering.js.map +1 -0
- package/dist/formatting.d.ts +23 -0
- package/dist/formatting.d.ts.map +1 -0
- package/dist/formatting.js +88 -0
- package/dist/formatting.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/input.d.ts +40 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +88 -0
- package/dist/input.js.map +1 -0
- package/dist/logging.d.ts +27 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +58 -0
- package/dist/logging.js.map +1 -0
- package/dist/main.d.ts +27 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +247 -0
- package/dist/main.js.map +1 -0
- package/dist/manifest.d.ts +13 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +127 -0
- package/dist/manifest.js.map +1 -0
- package/dist/matching.d.ts +47 -0
- package/dist/matching.d.ts.map +1 -0
- package/dist/matching.js +185 -0
- package/dist/matching.js.map +1 -0
- package/dist/normalize.d.ts +17 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +33 -0
- package/dist/normalize.js.map +1 -0
- package/dist/output.d.ts +13 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +19 -0
- package/dist/output.js.map +1 -0
- package/dist/scoring.d.ts +15 -0
- package/dist/scoring.d.ts.map +1 -0
- package/dist/scoring.js +106 -0
- package/dist/scoring.js.map +1 -0
- package/dist/signals.d.ts +17 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +57 -0
- package/dist/signals.js.map +1 -0
- package/dist/skill-content.d.ts +12 -0
- package/dist/skill-content.d.ts.map +1 -0
- package/dist/skill-content.js +33 -0
- package/dist/skill-content.js.map +1 -0
- package/dist/tool-formatting.d.ts +13 -0
- package/dist/tool-formatting.d.ts.map +1 -0
- package/dist/tool-formatting.js +47 -0
- package/dist/tool-formatting.js.map +1 -0
- package/dist/tool-input.d.ts +26 -0
- package/dist/tool-input.d.ts.map +1 -0
- package/dist/tool-input.js +108 -0
- package/dist/tool-input.js.map +1 -0
- package/dist/tool-output.d.ts +13 -0
- package/dist/tool-output.d.ts.map +1 -0
- package/dist/tool-output.js +20 -0
- package/dist/tool-output.js.map +1 -0
- package/dist/types.d.ts +225 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/package.json +44 -0
package/dist/matching.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stemming and fuzzy matching for keyword scoring
|
|
3
|
+
*/
|
|
4
|
+
/** Discount factor applied to fuzzy match scores */
|
|
5
|
+
export const FUZZY_DISCOUNT = 0.8;
|
|
6
|
+
const NO_MATCH = {
|
|
7
|
+
matched: false,
|
|
8
|
+
quality: 'exact',
|
|
9
|
+
matchedWord: '',
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Lightweight suffix-stripping stemmer.
|
|
13
|
+
* Words shorter than 4 characters are returned unchanged.
|
|
14
|
+
*/
|
|
15
|
+
export function stem(word) {
|
|
16
|
+
if (word.length < 4)
|
|
17
|
+
return word;
|
|
18
|
+
// -tion → -t
|
|
19
|
+
if (word.endsWith('tion'))
|
|
20
|
+
return word.slice(0, -3);
|
|
21
|
+
// -ment
|
|
22
|
+
if (word.endsWith('ment'))
|
|
23
|
+
return word.slice(0, -4);
|
|
24
|
+
// -ness
|
|
25
|
+
if (word.endsWith('ness'))
|
|
26
|
+
return word.slice(0, -4);
|
|
27
|
+
// -able
|
|
28
|
+
if (word.endsWith('able'))
|
|
29
|
+
return word.slice(0, -4);
|
|
30
|
+
// -ible
|
|
31
|
+
if (word.endsWith('ible'))
|
|
32
|
+
return word.slice(0, -4);
|
|
33
|
+
// -ing with doubled consonant: running → run
|
|
34
|
+
if (word.endsWith('ing')) {
|
|
35
|
+
const base = word.slice(0, -3);
|
|
36
|
+
if (base.length >= 2 && base[base.length - 1] === base[base.length - 2]) {
|
|
37
|
+
return base.slice(0, -1);
|
|
38
|
+
}
|
|
39
|
+
return base.length >= 2 ? base : word;
|
|
40
|
+
}
|
|
41
|
+
// -ly
|
|
42
|
+
if (word.endsWith('ly') && word.length > 4)
|
|
43
|
+
return word.slice(0, -2);
|
|
44
|
+
// -ies → -y (e.g., "queries" → "query")
|
|
45
|
+
if (word.endsWith('ies'))
|
|
46
|
+
return word.slice(0, -3) + 'y';
|
|
47
|
+
// -ied → -y (e.g., "applied" → "apply")
|
|
48
|
+
if (word.endsWith('ied'))
|
|
49
|
+
return word.slice(0, -3) + 'y';
|
|
50
|
+
// -ed with doubled consonant: stopped → stop
|
|
51
|
+
if (word.endsWith('ed')) {
|
|
52
|
+
const base = word.slice(0, -2);
|
|
53
|
+
if (base.length >= 2 && base[base.length - 1] === base[base.length - 2]) {
|
|
54
|
+
return base.slice(0, -1);
|
|
55
|
+
}
|
|
56
|
+
return base.length >= 2 ? base : word;
|
|
57
|
+
}
|
|
58
|
+
// -es after sibilants (ches, shes, ses, xes, zes) → strip -es
|
|
59
|
+
if (word.endsWith('ches') ||
|
|
60
|
+
word.endsWith('shes') ||
|
|
61
|
+
word.endsWith('ses') ||
|
|
62
|
+
word.endsWith('xes') ||
|
|
63
|
+
word.endsWith('zes')) {
|
|
64
|
+
return word.slice(0, -2);
|
|
65
|
+
}
|
|
66
|
+
// -s (but not -ss)
|
|
67
|
+
if (word.endsWith('s') && !word.endsWith('ss')) {
|
|
68
|
+
return word.slice(0, -1);
|
|
69
|
+
}
|
|
70
|
+
return word;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Levenshtein distance with early termination.
|
|
74
|
+
* Uses O(min(m,n)) space via single-row DP.
|
|
75
|
+
*
|
|
76
|
+
* @returns Edit distance, or maxDistance+1 if distance exceeds maxDistance
|
|
77
|
+
*/
|
|
78
|
+
export function levenshteinDistance(a, b, maxDistance) {
|
|
79
|
+
// Ensure a is the shorter string for O(min(m,n)) space
|
|
80
|
+
if (a.length > b.length) {
|
|
81
|
+
[a, b] = [b, a];
|
|
82
|
+
}
|
|
83
|
+
const m = a.length;
|
|
84
|
+
const n = b.length;
|
|
85
|
+
// Quick length-difference check
|
|
86
|
+
if (n - m > maxDistance)
|
|
87
|
+
return maxDistance + 1;
|
|
88
|
+
// Identical strings
|
|
89
|
+
if (a === b)
|
|
90
|
+
return 0;
|
|
91
|
+
// One empty string
|
|
92
|
+
if (m === 0)
|
|
93
|
+
return n <= maxDistance ? n : maxDistance + 1;
|
|
94
|
+
// Single-row DP
|
|
95
|
+
const row = new Array(m + 1);
|
|
96
|
+
for (let j = 0; j <= m; j++)
|
|
97
|
+
row[j] = j;
|
|
98
|
+
for (let i = 1; i <= n; i++) {
|
|
99
|
+
let prev = row[0];
|
|
100
|
+
row[0] = i;
|
|
101
|
+
let rowMin = row[0];
|
|
102
|
+
for (let j = 1; j <= m; j++) {
|
|
103
|
+
const cost = b[i - 1] === a[j - 1] ? 0 : 1;
|
|
104
|
+
const val = Math.min(row[j] + 1, // deletion
|
|
105
|
+
row[j - 1] + 1, // insertion
|
|
106
|
+
prev + cost // substitution
|
|
107
|
+
);
|
|
108
|
+
prev = row[j];
|
|
109
|
+
row[j] = val;
|
|
110
|
+
if (val < rowMin)
|
|
111
|
+
rowMin = val;
|
|
112
|
+
}
|
|
113
|
+
// Early termination: if all values in this row exceed maxDistance
|
|
114
|
+
if (rowMin > maxDistance)
|
|
115
|
+
return maxDistance + 1;
|
|
116
|
+
}
|
|
117
|
+
return row[m];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns the maximum allowed edit distance for a keyword of a given length.
|
|
121
|
+
* Short words (1-3 chars) get no fuzzy matching to avoid false positives.
|
|
122
|
+
*/
|
|
123
|
+
export function fuzzyThreshold(wordLength) {
|
|
124
|
+
if (wordLength <= 4)
|
|
125
|
+
return 0;
|
|
126
|
+
if (wordLength <= 6)
|
|
127
|
+
return 1;
|
|
128
|
+
return 2;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Pre-computes a map from stemmed form → original word for all prompt words.
|
|
132
|
+
* Used once per scoring call to avoid redundant stem() calls.
|
|
133
|
+
*/
|
|
134
|
+
export function buildStemmedWordMap(words) {
|
|
135
|
+
const map = new Map();
|
|
136
|
+
for (const word of words) {
|
|
137
|
+
const stemmed = stem(word);
|
|
138
|
+
// Only store if stem differs from original (exact match is checked separately)
|
|
139
|
+
if (stemmed !== word) {
|
|
140
|
+
map.set(stemmed, word);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return map;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Matches a single keyword against prompt words using the priority chain:
|
|
147
|
+
* exact → stem → fuzzy.
|
|
148
|
+
*
|
|
149
|
+
* @param keywordLower - The keyword in lowercase
|
|
150
|
+
* @param words - Set of normalized prompt words
|
|
151
|
+
* @param stemmedWords - Pre-computed stem → original word map
|
|
152
|
+
* @returns Match result with quality level and the matched word
|
|
153
|
+
*/
|
|
154
|
+
export function matchKeyword(keywordLower, words, stemmedWords) {
|
|
155
|
+
// 1. Exact match
|
|
156
|
+
if (words.has(keywordLower)) {
|
|
157
|
+
return { matched: true, quality: 'exact', matchedWord: keywordLower };
|
|
158
|
+
}
|
|
159
|
+
// 2. Stem match: stem the keyword and look for it in the stemmed prompt words
|
|
160
|
+
const keywordStem = stem(keywordLower);
|
|
161
|
+
// Check if any prompt word stems to the same root
|
|
162
|
+
if (stemmedWords.has(keywordStem)) {
|
|
163
|
+
return {
|
|
164
|
+
matched: true,
|
|
165
|
+
quality: 'stem',
|
|
166
|
+
matchedWord: stemmedWords.get(keywordStem),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
// Also check if the keyword stem matches any original word directly
|
|
170
|
+
if (keywordStem !== keywordLower && words.has(keywordStem)) {
|
|
171
|
+
return { matched: true, quality: 'stem', matchedWord: keywordStem };
|
|
172
|
+
}
|
|
173
|
+
// 3. Fuzzy match
|
|
174
|
+
const maxDist = fuzzyThreshold(keywordLower.length);
|
|
175
|
+
if (maxDist === 0)
|
|
176
|
+
return NO_MATCH;
|
|
177
|
+
for (const word of words) {
|
|
178
|
+
const dist = levenshteinDistance(keywordLower, word, maxDist);
|
|
179
|
+
if (dist <= maxDist) {
|
|
180
|
+
return { matched: true, quality: 'fuzzy', matchedWord: word };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return NO_MATCH;
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=matching.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matching.js","sourceRoot":"","sources":["../src/matching.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,oDAAoD;AACpD,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAalC,MAAM,QAAQ,GAAuB;IACnC,OAAO,EAAE,KAAK;IACd,OAAO,EAAE,OAAO;IAChB,WAAW,EAAE,EAAE;CAChB,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY;IAC/B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAEjC,aAAa;IACb,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,OAAO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,IAAI,IAAI,OAAO,EAAE,CAAC;YACpB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;QAChE,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt normalization functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes a user prompt for signal extraction.
|
|
6
|
+
*
|
|
7
|
+
* Algorithm (per SRS 6.2):
|
|
8
|
+
* 1. Convert to lowercase
|
|
9
|
+
* 2. Remove punctuation except dots in file extensions and slashes in paths
|
|
10
|
+
* 3. Collapse multiple spaces
|
|
11
|
+
* 4. Trim whitespace
|
|
12
|
+
*
|
|
13
|
+
* @param prompt - The raw user prompt
|
|
14
|
+
* @returns Normalized prompt string
|
|
15
|
+
*/
|
|
16
|
+
export declare function normalizePrompt(prompt: string): string;
|
|
17
|
+
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAsBtD"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt normalization functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes a user prompt for signal extraction.
|
|
6
|
+
*
|
|
7
|
+
* Algorithm (per SRS 6.2):
|
|
8
|
+
* 1. Convert to lowercase
|
|
9
|
+
* 2. Remove punctuation except dots in file extensions and slashes in paths
|
|
10
|
+
* 3. Collapse multiple spaces
|
|
11
|
+
* 4. Trim whitespace
|
|
12
|
+
*
|
|
13
|
+
* @param prompt - The raw user prompt
|
|
14
|
+
* @returns Normalized prompt string
|
|
15
|
+
*/
|
|
16
|
+
export function normalizePrompt(prompt) {
|
|
17
|
+
if (!prompt) {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
let result = prompt;
|
|
21
|
+
// Step 1: Convert to lowercase
|
|
22
|
+
result = result.toLowerCase();
|
|
23
|
+
// Step 2: Remove punctuation except dots, slashes, and alphanumeric
|
|
24
|
+
// Pattern: [^\w\s./] - matches anything that's not word char, whitespace, dot, or slash
|
|
25
|
+
// Also remove emojis and other unicode symbols (keep letters including accented ones)
|
|
26
|
+
result = result.replace(/[^\p{L}\p{N}\s./]/gu, ' ');
|
|
27
|
+
// Step 3: Collapse multiple spaces
|
|
28
|
+
result = result.replace(/\s+/g, ' ');
|
|
29
|
+
// Step 4: Trim whitespace
|
|
30
|
+
result = result.trim();
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,IAAI,MAAM,GAAG,MAAM,CAAC;IAEpB,+BAA+B;IAC/B,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IAE9B,oEAAoE;IACpE,wFAAwF;IACxF,sFAAsF;IACtF,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAEpD,mCAAmC;IACnC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAErC,0BAA0B;IAC1B,MAAM,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IAEvB,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook output building
|
|
3
|
+
*/
|
|
4
|
+
import type { HookOutput, HookEventName } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Builds the hook output structure for Claude Code.
|
|
7
|
+
*
|
|
8
|
+
* @param additionalContext - Context string to inject into the conversation
|
|
9
|
+
* @param hookEventName - The hook event type (defaults to UserPromptSubmit)
|
|
10
|
+
* @returns HookOutput ready for stdout
|
|
11
|
+
*/
|
|
12
|
+
export declare function buildHookOutput(additionalContext: string, hookEventName?: HookEventName): HookOutput;
|
|
13
|
+
//# sourceMappingURL=output.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,iBAAiB,EAAE,MAAM,EACzB,aAAa,GAAE,aAAkC,GAChD,UAAU,CAOZ"}
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook output building
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Builds the hook output structure for Claude Code.
|
|
6
|
+
*
|
|
7
|
+
* @param additionalContext - Context string to inject into the conversation
|
|
8
|
+
* @param hookEventName - The hook event type (defaults to UserPromptSubmit)
|
|
9
|
+
* @returns HookOutput ready for stdout
|
|
10
|
+
*/
|
|
11
|
+
export function buildHookOutput(additionalContext, hookEventName = 'UserPromptSubmit') {
|
|
12
|
+
return {
|
|
13
|
+
hookSpecificOutput: {
|
|
14
|
+
hookEventName,
|
|
15
|
+
additionalContext,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output.js","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,iBAAyB,EACzB,gBAA+B,kBAAkB;IAEjD,OAAO;QACL,kBAAkB,EAAE;YAClB,aAAa;YACb,iBAAiB;SAClB;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skill scoring functions
|
|
3
|
+
*/
|
|
4
|
+
import type { SkillDefinition, SkillWeights, ExtractedSignals, SkillScoreResult } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Scores a skill against extracted signals from a prompt.
|
|
7
|
+
*
|
|
8
|
+
* @param skill - The skill definition to score
|
|
9
|
+
* @param signals - Signals extracted from the normalized prompt
|
|
10
|
+
* @param normalizedPrompt - The normalized prompt (for pattern matching)
|
|
11
|
+
* @param weights - Scoring weights for each signal type
|
|
12
|
+
* @returns SkillScoreResult with score and matched signals
|
|
13
|
+
*/
|
|
14
|
+
export declare function scoreSkill(skill: SkillDefinition, signals: ExtractedSignals, normalizedPrompt: string, weights: SkillWeights): SkillScoreResult;
|
|
15
|
+
//# sourceMappingURL=scoring.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scoring.d.ts","sourceRoot":"","sources":["../src/scoring.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAEjB,MAAM,YAAY,CAAC;AAOpB;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,eAAe,EACtB,OAAO,EAAE,gBAAgB,EACzB,gBAAgB,EAAE,MAAM,EACxB,OAAO,EAAE,YAAY,GACpB,gBAAgB,CA+FlB"}
|
package/dist/scoring.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skill scoring functions
|
|
3
|
+
*/
|
|
4
|
+
import picomatch from 'picomatch';
|
|
5
|
+
import { buildStemmedWordMap, matchKeyword, FUZZY_DISCOUNT, } from './matching.js';
|
|
6
|
+
/**
|
|
7
|
+
* Scores a skill against extracted signals from a prompt.
|
|
8
|
+
*
|
|
9
|
+
* @param skill - The skill definition to score
|
|
10
|
+
* @param signals - Signals extracted from the normalized prompt
|
|
11
|
+
* @param normalizedPrompt - The normalized prompt (for pattern matching)
|
|
12
|
+
* @param weights - Scoring weights for each signal type
|
|
13
|
+
* @returns SkillScoreResult with score and matched signals
|
|
14
|
+
*/
|
|
15
|
+
export function scoreSkill(skill, signals, normalizedPrompt, weights) {
|
|
16
|
+
let score = 0;
|
|
17
|
+
const matchedSignals = [];
|
|
18
|
+
const triggers = skill.triggers;
|
|
19
|
+
// 6.4.1 Keyword matching (exact, stem, fuzzy)
|
|
20
|
+
if (triggers.keywords) {
|
|
21
|
+
const stemmedWords = buildStemmedWordMap(signals.words);
|
|
22
|
+
for (const keyword of triggers.keywords) {
|
|
23
|
+
const keywordLower = keyword.toLowerCase();
|
|
24
|
+
const result = matchKeyword(keywordLower, signals.words, stemmedWords);
|
|
25
|
+
if (result.matched) {
|
|
26
|
+
const weight = result.quality === 'fuzzy'
|
|
27
|
+
? weights.keywords * FUZZY_DISCOUNT
|
|
28
|
+
: weights.keywords;
|
|
29
|
+
score += weight;
|
|
30
|
+
matchedSignals.push({
|
|
31
|
+
type: 'keyword',
|
|
32
|
+
value: keyword,
|
|
33
|
+
matchQuality: result.quality,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 6.4.2 File extension matching
|
|
39
|
+
if (triggers.file_extensions) {
|
|
40
|
+
for (const ext of triggers.file_extensions) {
|
|
41
|
+
const extLower = ext.toLowerCase();
|
|
42
|
+
if (signals.extensions.has(extLower)) {
|
|
43
|
+
score += weights.file_extensions;
|
|
44
|
+
matchedSignals.push({
|
|
45
|
+
type: 'extension',
|
|
46
|
+
value: ext,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// 6.4.3 Pattern matching
|
|
52
|
+
if (triggers.patterns) {
|
|
53
|
+
for (const pattern of triggers.patterns) {
|
|
54
|
+
try {
|
|
55
|
+
// Bound greedy .* to max ~10 words to prevent cross-prompt matching
|
|
56
|
+
const boundedPattern = pattern.replace(/\.\*/g, '.{0,60}');
|
|
57
|
+
const regex = new RegExp(boundedPattern, 'i');
|
|
58
|
+
if (regex.test(normalizedPrompt)) {
|
|
59
|
+
score += weights.patterns;
|
|
60
|
+
matchedSignals.push({
|
|
61
|
+
type: 'pattern',
|
|
62
|
+
value: pattern,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Log warning for invalid regex, skip pattern
|
|
68
|
+
console.warn(`Invalid regex pattern: ${pattern}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// 6.4.4 File path matching (glob patterns)
|
|
73
|
+
if (triggers.file_paths) {
|
|
74
|
+
for (const pathPattern of triggers.file_paths) {
|
|
75
|
+
try {
|
|
76
|
+
const isMatch = picomatch(pathPattern, { nocase: true });
|
|
77
|
+
let matched = false;
|
|
78
|
+
for (const promptPath of signals.paths) {
|
|
79
|
+
if (isMatch(promptPath)) {
|
|
80
|
+
matched = true;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (matched) {
|
|
85
|
+
score += weights.file_paths;
|
|
86
|
+
matchedSignals.push({
|
|
87
|
+
type: 'path',
|
|
88
|
+
value: pathPattern,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
console.warn(`Invalid glob pattern: ${pathPattern}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
skill: {
|
|
99
|
+
path: skill.path,
|
|
100
|
+
name: skill.name,
|
|
101
|
+
},
|
|
102
|
+
score,
|
|
103
|
+
matchedSignals,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=scoring.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scoring.js","sourceRoot":"","sources":["../src/scoring.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,SAAS,MAAM,WAAW,CAAC;AAQlC,OAAO,EACL,mBAAmB,EACnB,YAAY,EACZ,cAAc,GACf,MAAM,eAAe,CAAC;AAEvB;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CACxB,KAAsB,EACtB,OAAyB,EACzB,gBAAwB,EACxB,OAAqB;IAErB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,cAAc,GAAoB,EAAE,CAAC;IAE3C,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;IAEhC,8CAA8C;IAC9C,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACtB,MAAM,YAAY,GAAG,mBAAmB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACxD,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;YACxC,MAAM,YAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;YAC3C,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;YACvE,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,MAAM,MAAM,GACV,MAAM,CAAC,OAAO,KAAK,OAAO;oBACxB,CAAC,CAAC,OAAO,CAAC,QAAQ,GAAG,cAAc;oBACnC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC;gBACvB,KAAK,IAAI,MAAM,CAAC;gBAChB,cAAc,CAAC,IAAI,CAAC;oBAClB,IA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A2C;IAC3C,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;QACxB,KAAK,MAAM,WAAW,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;YAC9C,IAAI,CAAC;gBACH,MAAM,OAAO,GAAG,SAAS,CAAC,WAAW,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;gBACzD,IAAI,OAAO,GAAG,KAAK,CAAC;gBACpB,KAAK,MAAM,UAAU,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;oBACvC,IAAI,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;wBACxB,OAAO,GAAG,IAAI,CAAC;wBACf,MAAM;oBACR,CAAC;gBACH,CAAC;gBACD,IAAI,OAAO,EAAE,CAAC;oBACZ,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC;oBAC5B,cAAc,CAAC,IAAI,CAAC;wBAClB,IAAI,EAAE,MAAM;wBACZ,KAAK,EAAE,WAAW;qBACnB,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,yBAAyB,WAAW,EAAE,CAAC,CAAC;YACvD,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,KAAK,EAAE;YACL,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,IAAI,EAAE,KAAK,CAAC,IAAI;SACjB;QACD,KAAK;QACL,cAAc;KACf,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal extraction functions
|
|
3
|
+
*/
|
|
4
|
+
import type { ExtractedSignals } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Extracts matchable signals from a normalized prompt.
|
|
7
|
+
*
|
|
8
|
+
* Signals include:
|
|
9
|
+
* - Words: tokens of 2+ characters
|
|
10
|
+
* - Extensions: file extensions like .pdf, .ts (1-10 chars after dot)
|
|
11
|
+
* - Paths: sequences containing forward slashes
|
|
12
|
+
*
|
|
13
|
+
* @param normalizedPrompt - A prompt that has been normalized
|
|
14
|
+
* @returns ExtractedSignals containing words, extensions, and paths
|
|
15
|
+
*/
|
|
16
|
+
export declare function extractSignals(normalizedPrompt: string): ExtractedSignals;
|
|
17
|
+
//# sourceMappingURL=signals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.d.ts","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,gBAAgB,EAAE,MAAM,GAAG,gBAAgB,CA8CzE"}
|
package/dist/signals.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signal extraction functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Extracts matchable signals from a normalized prompt.
|
|
6
|
+
*
|
|
7
|
+
* Signals include:
|
|
8
|
+
* - Words: tokens of 2+ characters
|
|
9
|
+
* - Extensions: file extensions like .pdf, .ts (1-10 chars after dot)
|
|
10
|
+
* - Paths: sequences containing forward slashes
|
|
11
|
+
*
|
|
12
|
+
* @param normalizedPrompt - A prompt that has been normalized
|
|
13
|
+
* @returns ExtractedSignals containing words, extensions, and paths
|
|
14
|
+
*/
|
|
15
|
+
export function extractSignals(normalizedPrompt) {
|
|
16
|
+
const signals = {
|
|
17
|
+
words: new Set(),
|
|
18
|
+
extensions: new Set(),
|
|
19
|
+
paths: [],
|
|
20
|
+
};
|
|
21
|
+
if (!normalizedPrompt) {
|
|
22
|
+
return signals;
|
|
23
|
+
}
|
|
24
|
+
// Extract words (minimum 2 characters)
|
|
25
|
+
// Split on whitespace, then also extract words from path segments
|
|
26
|
+
// This ensures "invoices/march.pdf" yields words: invoices, march, pdf
|
|
27
|
+
const tokens = normalizedPrompt.split(/\s+/);
|
|
28
|
+
for (const token of tokens) {
|
|
29
|
+
// Split token further on slashes and dots to get individual words
|
|
30
|
+
const subWords = token.split(/[/.]/);
|
|
31
|
+
for (const word of subWords) {
|
|
32
|
+
if (word.length >= 2) {
|
|
33
|
+
signals.words.add(word);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Extract file extensions
|
|
38
|
+
// Pattern: dot followed by 1-10 alphanumeric characters at word boundary
|
|
39
|
+
const extensionPattern = /\.[a-z0-9]{1,10}\b/g;
|
|
40
|
+
const extensionMatches = normalizedPrompt.match(extensionPattern);
|
|
41
|
+
if (extensionMatches) {
|
|
42
|
+
for (const ext of extensionMatches) {
|
|
43
|
+
signals.extensions.add(ext);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Extract file paths
|
|
47
|
+
// Pattern: sequences containing forward slash
|
|
48
|
+
const pathPattern = /[a-z0-9._-]*\/[a-z0-9._/-]*/g;
|
|
49
|
+
const pathMatches = normalizedPrompt.match(pathPattern);
|
|
50
|
+
if (pathMatches) {
|
|
51
|
+
for (const path of pathMatches) {
|
|
52
|
+
signals.paths.push(path);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return signals;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=signals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signals.js","sourceRoot":"","sources":["../src/signals.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,cAAc,CAAC,gBAAwB;IACrD,MAAM,OAAO,GAAqB;QAChC,KAAK,EAAE,IAAI,GAAG,EAAU;QACxB,UAAU,EAAE,IAAI,GAAG,EAAU;QAC7B,KAAK,EAAE,EAAE;KACV,CAAC;IAEF,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACtB,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,uCAAuC;IACvC,kEAAkE;IAClE,uEAAuE;IACvE,MAAM,MAAM,GAAG,gBAAgB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC7C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,kEAAkE;QAClE,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACrC,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;YAC5B,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;gBACrB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC1B,CAAC;QACH,CAAC;IACH,CAAC;IAED,0BAA0B;IAC1B,yEAAyE;IACzE,MAAM,gBAAgB,GAAG,qBAAqB,CAAC;IAC/C,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC;IAClE,IAAI,gBAAgB,EAAE,CAAC;QACrB,KAAK,MAAM,GAAG,IAAI,gBAAgB,EAAE,CAAC;YACnC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,qBAAqB;IACrB,8CAA8C;IAC9C,MAAM,WAAW,GAAG,8BAA8B,CAAC;IACnD,MAAM,WAAW,GAAG,gBAAgB,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IACxD,IAAI,WAAW,EAAE,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE,CAAC;YAC/B,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SKILL.md content reading utility
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Reads a SKILL.md file and returns its body content (without YAML frontmatter).
|
|
6
|
+
*
|
|
7
|
+
* @param skillPath - Relative path to the skill directory (e.g., '.claude/skills/my-skill')
|
|
8
|
+
* @param projectDir - Absolute path to the project root
|
|
9
|
+
* @returns Body content trimmed, or null if file not found, empty, or read error
|
|
10
|
+
*/
|
|
11
|
+
export declare function readSkillBody(skillPath: string, projectDir: string): string | null;
|
|
12
|
+
//# sourceMappingURL=skill-content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill-content.d.ts","sourceRoot":"","sources":["../src/skill-content.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,GACjB,MAAM,GAAG,IAAI,CAoBf"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SKILL.md content reading utility
|
|
3
|
+
*/
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
5
|
+
import { join } from 'path';
|
|
6
|
+
/**
|
|
7
|
+
* Reads a SKILL.md file and returns its body content (without YAML frontmatter).
|
|
8
|
+
*
|
|
9
|
+
* @param skillPath - Relative path to the skill directory (e.g., '.claude/skills/my-skill')
|
|
10
|
+
* @param projectDir - Absolute path to the project root
|
|
11
|
+
* @returns Body content trimmed, or null if file not found, empty, or read error
|
|
12
|
+
*/
|
|
13
|
+
export function readSkillBody(skillPath, projectDir) {
|
|
14
|
+
try {
|
|
15
|
+
const filePath = join(projectDir, skillPath, 'SKILL.md');
|
|
16
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
17
|
+
// Strip YAML frontmatter if present
|
|
18
|
+
if (content.startsWith('---\n')) {
|
|
19
|
+
const closingIndex = content.indexOf('\n---', 4);
|
|
20
|
+
if (closingIndex !== -1) {
|
|
21
|
+
const body = content.slice(closingIndex + 4).trim();
|
|
22
|
+
return body || null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// No frontmatter — return full content if non-empty
|
|
26
|
+
const trimmed = content.trim();
|
|
27
|
+
return trimmed || null;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=skill-content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill-content.js","sourceRoot":"","sources":["../src/skill-content.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAC3B,SAAiB,EACjB,UAAkB;IAElB,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;QACzD,MAAM,OAAO,GAAG,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAEhD,oCAAoC;QACpC,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAChC,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;YACjD,IAAI,YAAY,KAAK,CAAC,CAAC,EAAE,CAAC;gBACxB,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;gBACpD,OAAO,IAAI,IAAI,IAAI,CAAC;YACtB,CAAC;QACH,CAAC;QAED,oDAAoD;QACpD,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAC/B,OAAO,OAAO,IAAI,IAAI,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PreToolUse output formatting
|
|
3
|
+
*/
|
|
4
|
+
import type { SkillScoreResult, ToolName } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Formats matched skills into a concise context string for PreToolUse hooks.
|
|
7
|
+
*
|
|
8
|
+
* @param matchedSkills - Array of skill score results
|
|
9
|
+
* @param toolName - The tool being used (Edit or Write)
|
|
10
|
+
* @returns Formatted context string
|
|
11
|
+
*/
|
|
12
|
+
export declare function formatToolUseContext(matchedSkills: SkillScoreResult[], toolName: ToolName, skillContents?: Map<string, string>): string;
|
|
13
|
+
//# sourceMappingURL=tool-formatting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-formatting.d.ts","sourceRoot":"","sources":["../src/tool-formatting.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG7D;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,aAAa,EAAE,gBAAgB,EAAE,EACjC,QAAQ,EAAE,QAAQ,EAClB,aAAa,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAClC,MAAM,CA+CR"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PreToolUse output formatting
|
|
3
|
+
*/
|
|
4
|
+
import { summarizeSignals } from './formatting.js';
|
|
5
|
+
/**
|
|
6
|
+
* Formats matched skills into a concise context string for PreToolUse hooks.
|
|
7
|
+
*
|
|
8
|
+
* @param matchedSkills - Array of skill score results
|
|
9
|
+
* @param toolName - The tool being used (Edit or Write)
|
|
10
|
+
* @returns Formatted context string
|
|
11
|
+
*/
|
|
12
|
+
export function formatToolUseContext(matchedSkills, toolName, skillContents) {
|
|
13
|
+
const lines = [];
|
|
14
|
+
lines.push(`[Skill Router] Relevant skills for this ${toolName} operation:`);
|
|
15
|
+
lines.push('');
|
|
16
|
+
for (const result of matchedSkills) {
|
|
17
|
+
const skillLine = `- ${result.skill.name} (${result.skill.path}/SKILL.md)` +
|
|
18
|
+
` -- score: ${result.score.toFixed(1)}`;
|
|
19
|
+
lines.push(skillLine);
|
|
20
|
+
const signalSummary = summarizeSignals(result.matchedSignals);
|
|
21
|
+
if (signalSummary) {
|
|
22
|
+
lines.push(` Matched: ${signalSummary}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
// Check if we have any skill content to inject
|
|
26
|
+
const hasContent = skillContents && skillContents.size > 0;
|
|
27
|
+
if (hasContent) {
|
|
28
|
+
// Inject skill bodies directly (aligned with UserPromptSubmit behavior)
|
|
29
|
+
for (const result of matchedSkills) {
|
|
30
|
+
const body = skillContents.get(result.skill.path);
|
|
31
|
+
if (body) {
|
|
32
|
+
lines.push('');
|
|
33
|
+
lines.push('---');
|
|
34
|
+
lines.push(body);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
lines.push('');
|
|
38
|
+
lines.push(`Follow the skill instructions above for this ${toolName} operation.`);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// Fallback: ask Claude to read the files
|
|
42
|
+
lines.push('');
|
|
43
|
+
lines.push(`Please read the SKILL.md file(s) listed above using the Read tool before continuing with this ${toolName} operation.`);
|
|
44
|
+
}
|
|
45
|
+
return lines.join('\n');
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=tool-formatting.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-formatting.js","sourceRoot":"","sources":["../src/tool-formatting.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAEnD;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,aAAiC,EACjC,QAAkB,EAClB,aAAmC;IAEnC,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,KAAK,CAAC,IAAI,CACR,2CAA2C,QAAQ,aAAa,CACjE,CAAC;IACF,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAEf,KAAK,MAAM,MAAM,IAAI,aAAa,EAAE,CAAC;Q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