@ai2070/l0
Version:
L0: The Missing Reliability Substrate for AI
67 lines • 1.73 kB
JavaScript
export const RuntimeStates = {
INIT: "init",
WAITING_FOR_TOKEN: "waiting_for_token",
STREAMING: "streaming",
TOOL_CALL_DETECTED: "tool_call_detected",
CONTINUATION_MATCHING: "continuation_matching",
CHECKPOINT_VERIFYING: "checkpoint_verifying",
RETRYING: "retrying",
FALLBACK: "fallback",
FINALIZING: "finalizing",
COMPLETE: "complete",
ERROR: "error",
};
export class StateMachine {
state = RuntimeStates.INIT;
history = [];
listeners = new Set();
transition(next) {
if (this.state !== next) {
this.history.push({
from: this.state,
to: next,
timestamp: Date.now(),
});
this.state = next;
this.notify();
}
}
get() {
return this.state;
}
is(...states) {
return states.includes(this.state);
}
isTerminal() {
return (this.state === RuntimeStates.COMPLETE ||
this.state === RuntimeStates.ERROR);
}
reset() {
const previousState = this.state;
this.state = RuntimeStates.INIT;
this.history = [];
if (previousState !== RuntimeStates.INIT) {
this.notify();
}
}
getHistory() {
return this.history;
}
subscribe(listener) {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
notify() {
for (const listener of this.listeners) {
try {
listener(this.state);
}
catch {
}
}
}
}
export function createStateMachine() {
return new StateMachine();
}
//# sourceMappingURL=state-machine.js.map