@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
184 lines • 6.41 kB
JavaScript
/**
* One session's job widgets: the jobs hosts registered, the request controller their spec requests
* share, and the refresh loop. New output schedules a refresh at most every
* {@link INGEST_MIN_INTERVAL_MS}; a 1 s unref'd ticker covers time-based work (a job turning old
* enough to author) and runs only while some job is still open. Every refresh offers the session's
* state to the publisher, which sends it only when it changed and at most once a second.
*/
import { createWidgetRedactor } from "./author-redact.js";
import { defaultLineMatcher } from "./re2-matcher.js";
import { JobWidgetJob } from "./registry-job.js";
import { JobWidgetRequestController } from "./request-controller.js";
import { JobWidgetStatePublisher } from "./state-publisher.js";
const INGEST_MIN_INTERVAL_MS = 250;
const REFRESH_INTERVAL_MS = 1000;
const INERT_HANDLE = {
notify: () => { },
update: () => { },
settle: () => { },
dispose: () => { },
};
export class JobWidgetRegistry {
#matcher;
/** Handed to every job and the publisher; it calls the current redactor, so a re-bind reaches them. */
#redact = {
text: (text) => this.#currentRedact.text(text),
source: () => this.#currentRedact.source(),
};
#currentRedact;
#controller;
#publisher;
#jobs = new Map();
#ticker;
#ingestTimer;
#lastRefreshAt = Number.NEGATIVE_INFINITY;
#disposed = false;
constructor(options) {
this.sessionId = options.sessionId;
this.#matcher = options.matcher ?? defaultLineMatcher;
this.#currentRedact = createWidgetRedactor(options.obfuscate);
this.#controller = new JobWidgetRequestController({
author: options.author,
matcher: this.#matcher,
...(options.limits === undefined ? {} : { limits: options.limits }),
...(options.obfuscate === undefined ? {} : { obfuscate: options.obfuscate }),
});
this.#publisher = new JobWidgetStatePublisher(options.publish, this.#redact.text);
}
add(input) {
if (this.#disposed)
return INERT_HANDLE;
const previous = this.#jobs.get(input.id);
if (previous)
this.#forget(previous);
const job = new JobWidgetJob(input, this.#matcher, this.#redact);
this.#jobs.set(job.id, job);
this.#refresh(job);
this.#ensureTicker();
const live = () => this.#jobs.get(job.id) === job;
return {
notify: () => {
if (live() && !job.settled)
this.#scheduleRefresh();
},
update: (brief) => {
if (!live())
return;
job.brief = { ...brief, kind: job.kind };
this.#scheduleRefresh();
},
settle: () => {
if (live())
this.#settle(job);
},
dispose: () => {
if (live())
this.#forget(job);
},
};
}
/** The session's state as clients receive it: every view redacted. */
state() {
return this.#publisher.redacted(this.snapshot());
}
snapshot() {
const jobs = [];
for (const job of this.#jobs.values()) {
const entry = job.entry();
if (entry)
jobs.push(entry);
}
return { v: 1, sessionId: this.sessionId, jobs };
}
/**
* Applies settings re-read on a re-bind (a reload): the author's start delay from the next request
* on, the session's obfuscation for every state sent, every author request and every runtime started
* after it. A reader already running keeps its own stateful source redactor, so a key block it is
* inside is never split between two redactors.
*/
configure(options) {
this.#currentRedact = createWidgetRedactor(options.obfuscate);
this.#controller.configure(options);
}
/** Sends the current state on the next offer even if unchanged, for a listener that just bound. */
republish() {
this.#publisher.reset();
this.#publish();
}
dispose() {
this.#disposed = true;
this.#stopTicker();
clearTimeout(this.#ingestTimer);
this.#ingestTimer = undefined;
this.#controller.dispose();
this.#publisher.dispose();
this.#jobs.clear();
}
#scheduleRefresh() {
if (this.#disposed || this.#ingestTimer !== undefined)
return;
const wait = Math.max(0, this.#lastRefreshAt + INGEST_MIN_INTERVAL_MS - Date.now());
this.#ingestTimer = setTimeout(() => {
this.#ingestTimer = undefined;
this.#refreshAll();
}, wait);
this.#ingestTimer.unref?.();
}
#refreshAll() {
if (this.#disposed)
return;
this.#lastRefreshAt = Date.now();
for (const job of this.#jobs.values())
if (!job.settled)
this.#refresh(job);
this.#publish();
}
#refresh(job) {
job.ingest();
if (job.settled)
return;
const pending = this.#controller.maybeRequest(job);
if (!pending)
return;
job.requestStarted();
void pending.then((outcome) => {
if (this.#disposed || this.#jobs.get(job.id) !== job)
return;
job.requestSettled(outcome);
this.#publish();
});
}
#settle(job) {
job.settled = true;
job.ingest();
this.#publish();
this.#stopTickerWhenIdle();
}
#forget(job) {
this.#jobs.delete(job.id);
this.#controller.forget(job.id);
this.#publish();
this.#stopTickerWhenIdle();
}
#publish() {
if (!this.#disposed)
this.#publisher.offer(this.snapshot());
}
#ensureTicker() {
if (this.#ticker !== undefined)
return;
this.#ticker = setInterval(() => this.#refreshAll(), REFRESH_INTERVAL_MS);
this.#ticker.unref?.();
}
#stopTickerWhenIdle() {
for (const job of this.#jobs.values())
if (!job.settled)
return;
this.#stopTicker();
}
#stopTicker() {
clearInterval(this.#ticker);
this.#ticker = undefined;
}
}
//# sourceMappingURL=registry.js.map