@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
170 lines • 7.52 kB
JavaScript
/**
* Deterministic derivation of one job's widget under one spec (or none: the pending and fallback
* views). Each refresh reads every source's new text, matches the rules reading that source with
* one call, folds the matches into bounded per-field state, and builds a v1 view. No model is
* involved past the spec. Installing a new spec starts a fresh runtime.
* Ported from oh-my-pi 9df0457b31 (MIT, Copyright Can Boluk et al.).
*/
import { FieldStore, windowRate } from "./field-state.js";
import { truncateText } from "./format.js";
import { SourceLineReader } from "./line-reader.js";
import { applyMatches, compileSourceMatcher } from "./rule-match.js";
import { latestLine, MAX_STAGE_CHARS, newSourceStats, recordLines, sourceSample, } from "./source-stats.js";
import { keyedFields, pinnedKeys } from "./spec-rules.js";
import { MAX_TITLE_CHARS } from "./spec-types.js";
import { buildBlock, buildHeadline, hasData } from "./view-build.js";
/** Newest lines per source matched per refresh; older lines of a burst are counted, not matched. */
export const MAX_MATCH_LINES = 2000;
export class JobWidgetRuntime {
#matcher;
#startTime;
#now;
#redactSource;
#readers = new Map();
#stats = new Map();
#matchers = new Map();
#store;
#lastMatchAt;
#lastLineAt;
#unmatchedLines = 0;
/** Newest non-empty line no rule matched: a live widget's stage when the spec names none. */
#lastUnmatchedLine;
/** Newest CR progress segment of a line still in progress (redacted); it stands for the stage. */
#progress;
constructor(options) {
this.spec = options.spec;
this.#matcher = options.matcher;
this.#startTime = options.startTime;
this.#now = options.now ?? Date.now;
this.#redactSource = options.redactSource;
this.#store = new FieldStore(keyedFields(options.spec), pinnedKeys(options.spec));
}
/** Non-empty lines seen since the last match on a source some rule reads: the drift signal. */
get unmatchedLines() {
return this.#unmatchedLines;
}
get lastMatchAt() {
return this.#lastMatchAt;
}
/** Non-empty lines read across every source. */
get totalLines() {
let total = 0;
for (const stats of this.#stats.values())
total += stats.lines;
return total;
}
/** Non-empty lines per second across every source over the rate window. */
get linesPerSecond() {
const recent = [...this.#stats.values()].flatMap((stats) => stats.recent);
return windowRate(recent, this.#now(), this.#startTime);
}
/** Reads every source's new text; `flush` ends lines in progress. Returns whether a line arrived. */
ingest(sources, flush) {
const t = this.#now();
let changed = false;
for (const source of sources) {
let reader = this.#readers.get(source.id);
if (!reader || reader.source !== source) {
reader = new SourceLineReader(source, this.#redactSource?.());
this.#readers.set(source.id, reader);
}
const { lines, end } = reader.read(flush);
const progress = reader.progress;
if (progress !== undefined) {
// Redacted on its own: the line is not complete, so it never enters the source's stateful redactor.
const once = this.#redactSource?.();
const text = once ? [...once.push(progress), ...once.finish()].join(" ") : progress;
this.#progress = truncateText(text.trim(), MAX_STAGE_CHARS);
}
else if (lines.length > 0)
this.#progress = undefined;
let stats = this.#stats.get(source.id);
if (!stats) {
stats = newSourceStats(source.id, source.label);
this.#stats.set(source.id, stats);
}
stats.size = end;
if (lines.length === 0)
continue;
changed = true;
if (recordLines(stats, lines, t))
this.#lastLineAt = t;
this.#matchLines(source.id, lines, t);
}
return changed;
}
#sourceMatcher(id) {
const known = this.#matchers.get(id);
if (known !== undefined)
return known ?? undefined;
const entry = this.spec ? compileSourceMatcher(this.spec, this.#matcher, id) : undefined;
this.#matchers.set(id, entry ?? null);
return entry;
}
#matchLines(sourceId, lines, t) {
const entry = this.#sourceMatcher(sourceId);
if (!entry)
return;
const window = lines.length > MAX_MATCH_LINES ? lines.slice(-MAX_MATCH_LINES) : lines;
const matched = applyMatches(entry, window, t, this.#store);
const lastMatched = matched.size > 0 ? Math.max(...matched) : -1;
if (lastMatched >= 0)
this.#lastMatchAt = t;
let unmatched = 0;
for (let index = lastMatched + 1; index < window.length; index++) {
if (window[index].trim().length > 0)
unmatched += 1;
}
this.#unmatchedLines = lastMatched >= 0 ? unmatched : this.#unmatchedLines + unmatched;
for (let index = window.length - 1; index >= 0; index--) {
const line = window[index].trim();
if (matched.has(index) || line.length === 0)
continue;
this.#lastUnmatchedLine = truncateText(line, MAX_STAGE_CHARS);
break;
}
}
/** First and last lines per source, most active first, for the author and the dry run. */
samples() {
return [...this.#stats.values()]
.filter((stats) => stats.lines > 0)
.sort((a, b) => b.lines - a.lines)
.map(sourceSample);
}
derive(context) {
const now = this.#now();
const updatedAtMs = this.#lastMatchAt ?? this.#lastLineAt ?? this.#startTime;
const lineStage = context.lineStage === true;
const spec = this.spec;
if (!spec) {
const stage = context.stage ?? (lineStage ? (this.#progress ?? latestLine(this.#stats.values())) : undefined);
const title = truncateText(context.label.replace(/\s+/g, " ").trim(), MAX_TITLE_CHARS);
return {
state: context.authoring ? "pending" : "fallback",
view: { title, ...(stage ? { stage } : {}), blocks: [], lines: this.totalLines },
updatedAtMs,
};
}
const ctx = { store: this.#store, now, startTime: this.#startTime };
const headline = spec.headline ? buildHeadline(spec.headline, ctx) : undefined;
const stage = (spec.stage ? this.#specStage(spec.stage) : undefined) ??
context.stage ??
(lineStage ? (this.#progress ?? this.#lastUnmatchedLine) : undefined);
return {
state: context.settled ? "done" : "live",
view: {
title: spec.title,
...(headline ? { headline } : {}),
...(stage ? { stage } : {}),
blocks: spec.blocks.map((block) => buildBlock(block, ctx)).filter(hasData),
},
updatedAtMs,
};
}
#specStage(field) {
const series = this.#store.series(field);
const text = (series.text ?? series.line)?.trim();
return text ? truncateText(text, MAX_STAGE_CHARS) : undefined;
}
}
//# sourceMappingURL=runtime.js.map