@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 2.77 kB
JavaScript
import { compilePatterns } from "./matcher.js";
import { ruleReadsSource } from "./spec-rules.js";
import { parseWidgetValue } from "./value-parse.js";
function compileRule(rule, groups) {
const captures = [];
groups.forEach((field, index) => {
if (field !== rule.key)
captures.push({ index, field, text: field === rule.text });
});
return {
key: rule.key === undefined ? -1 : groups.indexOf(rule.key),
captures,
count: rule.count,
scale: rule.scale ?? 1,
};
}
export function compileSourceMatcher(spec, matcher, sourceId) {
// Rules sharing a pattern (one line read into several fields) run it once.
const byPattern = new Map();
for (const rule of spec.rules) {
if (!ruleReadsSource(rule.from, sourceId))
continue;
const rules = byPattern.get(rule.match);
if (rules)
rules.push(rule);
else
byPattern.set(rule.match, [rule]);
}
if (byPattern.size === 0)
return undefined;
const patterns = [...byPattern.keys()];
const compiled = compilePatterns(matcher, patterns);
return {
compiled,
rules: patterns.map((pattern, index) => (byPattern.get(pattern) ?? []).map((rule) => compileRule(rule, compiled.groupNames[index]))),
};
}
/** Matches `lines` and pushes every capture; returns the indexes of the lines some rule matched. */
export function applyMatches(entry, lines, t, store) {
const matched = new Set();
for (const hit of entry.compiled.matchLines(lines)) {
matched.add(hit.line);
const line = lines[hit.line];
for (const rule of entry.rules[hit.pattern]) {
const keySpan = rule.key < 0 ? undefined : hit.spans[rule.key];
const key = keySpan ? line.slice(keySpan.start, keySpan.end).trim() : "";
for (const capture of rule.captures) {
const span = hit.spans[capture.index];
if (!span)
continue;
const captured = line.slice(span.start, span.end);
const value = capture.text ? Number.NaN : parseWidgetValue(captured) * rule.scale;
// A capture holding no number is kept as text, so a status word still reaches grids.
if (Number.isFinite(value))
store.push(capture.field, key, { value, text: undefined, line, t });
else
store.push(capture.field, key, { value: undefined, text: captured.trim(), line, t });
}
if (rule.count !== undefined)
store.push(rule.count, key, { value: undefined, text: undefined, line, t });
}
}
return matched;
}
//# sourceMappingURL=rule-match.js.map