@ai2070/l0
Version:
L0: The Missing Reliability Substrate for AI
92 lines (91 loc) • 1.93 kB
JavaScript
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"
};
class StateMachine {
state = RuntimeStates.INIT;
history = [];
listeners = /* @__PURE__ */ new Set();
/**
* Transition to a new state
*/
transition(next) {
if (this.state !== next) {
this.history.push({
from: this.state,
to: next,
timestamp: Date.now()
});
this.state = next;
this.notify();
}
}
/**
* Get current state
*/
get() {
return this.state;
}
/**
* Check if current state matches any of the provided states
*/
is(...states) {
return states.includes(this.state);
}
/**
* Check if state is terminal (done or error)
*/
isTerminal() {
return this.state === RuntimeStates.COMPLETE || this.state === RuntimeStates.ERROR;
}
/**
* Reset to initial state and notify subscribers
*/
reset() {
const previousState = this.state;
this.state = RuntimeStates.INIT;
this.history = [];
if (previousState !== RuntimeStates.INIT) {
this.notify();
}
}
/**
* Get state history (for debugging)
*/
getHistory() {
return this.history;
}
/**
* Subscribe to state changes
*/
subscribe(listener) {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
notify() {
for (const listener of this.listeners) {
try {
listener(this.state);
} catch {
}
}
}
}
function createStateMachine() {
return new StateMachine();
}
export {
RuntimeStates,
StateMachine,
createStateMachine
};
//# sourceMappingURL=state-machine.js.map