@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
79 lines • 2.81 kB
JavaScript
/**
* What rules feed: the fields each rule writes, the sources it reads, and how a field is fed
* across all rules. Ported from oh-my-pi 9df0457b31 (MIT, Copyright Can Boluk et al.).
*/
import { patternGroupNames } from "./matcher.js";
/** Whether a rule's `from` names the source `id`. */
export function ruleReadsSource(from, id) {
if (from === "*")
return true;
if (from.endsWith("*"))
return id.startsWith(from.slice(0, -1));
return from === id;
}
/** The fields a rule feeds: one per named group but `key`, then its `count`. */
export function ruleFields(rule) {
const fields = [];
for (const group of new Set(patternGroupNames(rule.match))) {
if (group !== rule.key)
fields.push({ field: group, text: group === rule.text, count: false });
}
if (rule.count !== undefined)
fields.push({ field: rule.count, text: false, count: true });
return fields;
}
/** Whether an aggregation needs numbers: `count` and `rate` read matches, not values. */
export function aggNeedsValue(agg) {
return agg !== "count" && agg !== "rate";
}
export function fieldFeeds(spec) {
const fields = new Map();
for (const rule of spec.rules) {
for (const { field, text, count } of ruleFields(rule)) {
const feed = fields.get(field) ?? { value: false, text: false, keyGroups: new Set() };
feed.value ||= !text && !count;
feed.text ||= text;
if (rule.key !== undefined)
feed.keyGroups.add(rule.key);
fields.set(field, feed);
}
}
return fields;
}
/** Fields some rule splits by key; every other field keeps a single series. */
export function keyedFields(spec) {
const keyed = new Set();
for (const rule of spec?.rules ?? []) {
if (rule.key === undefined)
continue;
for (const { field } of ruleFields(rule))
keyed.add(field);
}
return keyed;
}
/** Keys the spec's blocks and headline name, per field: a field's key cap never evicts them. */
export function pinnedKeys(spec) {
const pinned = new Map();
const pin = (ref) => {
if (ref?.key === undefined)
return;
const keys = pinned.get(ref.field) ?? new Set();
keys.add(ref.key);
pinned.set(ref.field, keys);
};
pin(spec?.headline);
for (const block of spec?.blocks ?? []) {
if (block.kind === "spark")
pin(block);
else if (block.kind === "meter") {
pin(block);
if (typeof block.total !== "number")
pin(block.total);
}
else if (block.kind === "stat")
for (const item of block.items)
pin(item);
}
return pinned;
}
//# sourceMappingURL=spec-rules.js.map