UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

107 lines • 3.86 kB
import { createHash } from "node:crypto"; import picomatch from "picomatch"; const PICOMATCH_OPTIONS = { bash: true, dot: true }; const MAX_COMPILED_PATTERN_SET_CACHE_ENTRIES = 256; const compiledPatternSets = new Map(); export function matchRule(input) { if (input.isSingleFile) { return { matched: true, reason: "single-file" }; } if (input.frontmatter.alwaysApply === true) { return { matched: true, reason: "alwaysApply" }; } const patterns = normalizeGlobs(input.frontmatter); if (patterns.length === 0) { return noMatch(); } const pathBases = [ normalizePath(input.pathBases.projectRelative), input.pathBases.scopeRelative ? normalizePath(input.pathBases.scopeRelative) : undefined, normalizePath(input.pathBases.basename), ].filter((pathBase) => pathBase !== undefined); const { positiveMatchers, negativeMatchers } = compiledPatternSetFor(patterns); for (const { pattern, isMatch } of positiveMatchers) { for (const pathBase of pathBases) { if (!isMatch(pathBase)) { continue; } if (isExcluded(pathBase, negativeMatchers)) { return noMatch(); } return { matched: true, reason: { kind: "glob", pattern } }; } } return noMatch(); } export function normalizeGlobs(frontmatter) { const patterns = [ ...normalizePatternList(frontmatter.globs), ...normalizePatternList(frontmatter.paths), ...normalizePatternList(frontmatter.applyTo), ]; return [...new Set(patterns.map(normalizePath))]; } export function hashContent(body) { return createHash("sha256").update(body).digest("hex"); } export function resetMatcherCache() { compiledPatternSets.clear(); } export function getMatcherCacheStats() { let compiledPatterns = 0; for (const patternSet of compiledPatternSets.values()) { compiledPatterns += patternSet.positiveMatchers.length + patternSet.negativeMatchers.length; } return { entries: compiledPatternSets.size, compiledPatterns }; } function compiledPatternSetFor(patterns) { const cacheKey = patterns.join("\0"); const cached = compiledPatternSets.get(cacheKey); if (cached !== undefined) { compiledPatternSets.delete(cacheKey); compiledPatternSets.set(cacheKey, cached); return cached; } const positiveMatchers = []; const negativeMatchers = []; for (const pattern of patterns) { if (pattern.startsWith("!")) { negativeMatchers.push(picomatch(pattern.slice(1), PICOMATCH_OPTIONS)); continue; } positiveMatchers.push({ pattern, isMatch: picomatch(pattern, PICOMATCH_OPTIONS) }); } const compiledPatternSet = { positiveMatchers, negativeMatchers }; setCompiledPatternSet(cacheKey, compiledPatternSet); return compiledPatternSet; } function setCompiledPatternSet(cacheKey, compiledPatternSet) { if (compiledPatternSets.size >= MAX_COMPILED_PATTERN_SET_CACHE_ENTRIES) { const oldestCacheKey = compiledPatternSets.keys().next().value; if (oldestCacheKey !== undefined) { compiledPatternSets.delete(oldestCacheKey); } } compiledPatternSets.set(cacheKey, compiledPatternSet); } function normalizePatternList(patterns) { if (patterns === undefined) { return []; } return Array.isArray(patterns) ? patterns : [patterns]; } function normalizePath(path) { return path.replaceAll("\\", "/"); } function isExcluded(pathBase, negativeMatchers) { for (const isMatch of negativeMatchers) { if (isMatch(pathBase)) { return true; } } return false; } function noMatch() { return { matched: false, reason: { kind: "no-match" } }; } //# sourceMappingURL=matcher.js.map