"/loop 1m say hi, stop after 3 answers" kept firing and the model answered "stopped" every minute until /loop stop. The count
("stop after 3 answers", "three replies", "5 times"), the time ("for the next 2 hours", "stop after 30 minutes") and a stop
condition ("stop when it passes", "until the file exists") are now parsed out of the line, left out of the message the model gets,
and enforced by a new scheduler (lib/loop-runner.ts): after the last allowed answer nothing is sent again, and the person is told in
one plain sentence why it ended. The scheduler also ends the loop when an answer says it is done or stopped (a bare "Stopped."
always; "Done ..." and similar when a limit was stated; DONE as the first word when a stop condition was given in words), and after
three failed answers in a row. A busy chat waits without using up the count. Tests drive the scheduler with a scripted model and a
fake clock. Strings in 8 languages.
121 lines
5.7 KiB
TypeScript
121 lines
5.7 KiB
TypeScript
import { tr } from './i18n';
|
|
import { LOOP_MAX_FAILURES, LOOP_MAX_RUNS, judgeAnswer, loopEndNote, loopEvery, loopLimitNote, withStopRule, type LoopLimits, type LoopStop } from './loop';
|
|
|
|
/**
|
|
* The /loop scheduler. It owns the timer, the count, the time limit and the "the answer says it is done" rule, so a stop condition
|
|
* is enforced here, in the app, and never left to the model: once a loop has ended nothing is sent again.
|
|
*
|
|
* Everything outside (sending the message, reading the answer, adding a line to the chat, the clock) is passed in, so the same code
|
|
* runs in the app (store.ts) and in the tests with a scripted model and a fake clock.
|
|
*/
|
|
export interface LoopHost {
|
|
/** Send the message as an ordinary chat turn and resolve when the answer is complete. `answered` is false when nothing was sent (the chat was busy). */
|
|
send(sessionId: string, message: { text: string; shown: string }): Promise<{ answered: boolean; text: string; failed: boolean }>;
|
|
/** A line from the app itself in the chat (never sent to the model). */
|
|
note(sessionId: string, text: string): void;
|
|
/** False when the chat was deleted: the loop ends quietly. */
|
|
exists(sessionId: string): boolean;
|
|
/** True when this chat is open and nothing is being answered. */
|
|
ready(sessionId: string): boolean;
|
|
now(): number;
|
|
setTimer(fn: () => void, ms: number): unknown;
|
|
clearTimer(handle: unknown): void;
|
|
}
|
|
|
|
export interface LoopStart extends LoopLimits { everyMs: number; prompt: string }
|
|
|
|
interface LoopState extends LoopStart {
|
|
answers: number;
|
|
failures: number;
|
|
startedAt: number;
|
|
timer: unknown;
|
|
running: boolean;
|
|
}
|
|
|
|
export class LoopScheduler {
|
|
private readonly loops = new Map<string, LoopState>();
|
|
/** How long to wait before looking again when the chat is busy or not open. */
|
|
busyRetryMs = 15_000;
|
|
|
|
constructor(private readonly host: LoopHost) {}
|
|
|
|
isRunning(sessionId: string | null): boolean { return !!sessionId && this.loops.has(sessionId); }
|
|
|
|
/** What the person is told for /loop with nothing after it. */
|
|
describe(sessionId: string): string | null {
|
|
const loop = this.loops.get(sessionId);
|
|
if (!loop) return null;
|
|
const limit = loopLimitNote(loop);
|
|
return tr('A loop is running in this chat: it sends "{0}" {1}, while this chat is open. Type /loop stop to stop it.', [loop.prompt, loopEvery(loop.everyMs)]) + (limit ? ' ' + limit : '');
|
|
}
|
|
|
|
stop(sessionId: string): boolean {
|
|
const loop = this.loops.get(sessionId);
|
|
if (!loop) return false;
|
|
if (loop.timer) this.host.clearTimer(loop.timer);
|
|
this.loops.delete(sessionId);
|
|
return true;
|
|
}
|
|
|
|
/** Start a loop and send its first message now. Returns false (and says why in the chat) when the chat already has one. */
|
|
async start(sessionId: string, command: LoopStart): Promise<boolean> {
|
|
if (this.loops.has(sessionId)) { this.host.note(sessionId, tr('This chat already has a loop. Type /loop stop first, then start a new one.')); return false; }
|
|
const loop: LoopState = { ...command, answers: 0, failures: 0, startedAt: this.host.now(), timer: null, running: false };
|
|
this.loops.set(sessionId, loop);
|
|
const limit = loopLimitNote(loop);
|
|
this.host.note(sessionId, tr('Loop started: "{0}" {1}, while this chat is open. Type /loop stop to stop it. For work that must go on with Strata closed, use the Tasks screen.', [loop.prompt, loopEvery(loop.everyMs)]) + (limit ? ' ' + limit : ''));
|
|
await this.run(sessionId);
|
|
return true;
|
|
}
|
|
|
|
/** Send once, judge the answer, then either end the loop with a plain note or wait for the next time. */
|
|
private async run(sessionId: string): Promise<void> {
|
|
const loop = this.loops.get(sessionId);
|
|
if (!loop || loop.running) return;
|
|
loop.running = true;
|
|
let why: LoopStop | null = null;
|
|
try {
|
|
const result = await this.host.send(sessionId, { text: withStopRule(loop.prompt, loop.stopWhen), shown: loop.prompt });
|
|
// The person may have typed /loop stop while the answer was coming: nothing more happens then.
|
|
if (this.loops.get(sessionId) !== loop) return;
|
|
if (result.answered) {
|
|
if (result.failed) loop.failures += 1; else { loop.failures = 0; loop.answers += 1; }
|
|
why = judgeAnswer(loop, loop, result, this.host.now());
|
|
}
|
|
} catch {
|
|
if (this.loops.get(sessionId) !== loop) return;
|
|
loop.failures += 1;
|
|
if (loop.failures >= LOOP_MAX_FAILURES) why = 'failures';
|
|
} finally {
|
|
loop.running = false;
|
|
}
|
|
if (why) { this.finish(sessionId, loop, why); return; }
|
|
this.schedule(sessionId, loop.everyMs);
|
|
}
|
|
|
|
private finish(sessionId: string, loop: LoopState, why: LoopStop) {
|
|
this.stop(sessionId);
|
|
this.host.note(sessionId, loopEndNote(why, loop.answers));
|
|
}
|
|
|
|
private schedule(sessionId: string, ms: number) {
|
|
const loop = this.loops.get(sessionId);
|
|
if (!loop) return;
|
|
loop.timer = this.host.setTimer(() => void this.tick(sessionId), ms);
|
|
}
|
|
|
|
private async tick(sessionId: string): Promise<void> {
|
|
const loop = this.loops.get(sessionId);
|
|
if (!loop) return;
|
|
loop.timer = null;
|
|
if (!this.host.exists(sessionId)) { this.stop(sessionId); return; }
|
|
// The time is up, or the count is reached: never fire again, even if the last answer was not judged (a busy chat).
|
|
if (loop.maxMs !== undefined && this.host.now() - loop.startedAt >= loop.maxMs) { this.finish(sessionId, loop, 'time'); return; }
|
|
if (loop.maxRuns !== undefined && loop.answers >= loop.maxRuns) { this.finish(sessionId, loop, 'count'); return; }
|
|
if (loop.answers >= LOOP_MAX_RUNS) { this.finish(sessionId, loop, 'cap'); return; }
|
|
// Wait for a quiet moment: this chat is open and nothing is being answered.
|
|
if (!this.host.ready(sessionId)) { this.schedule(sessionId, this.busyRetryMs); return; }
|
|
await this.run(sessionId);
|
|
}
|
|
}
|