@sema-agent/core
Version:
Stateless, task-oriented AI agent core
38 lines (37 loc) • 2.23 kB
JavaScript
import { describeRejectedValue } from "./tool-search-setting.js";
export const RUN_ORIGINS = Object.freeze(["scheduled"]);
const RUN_ORIGIN_SET = new Set(RUN_ORIGINS);
export function isRunOrigin(v) {
return RUN_ORIGIN_SET.has(v);
}
const WORDS = RUN_ORIGINS.map((w) => JSON.stringify(w)).join(" | ");
function refusal(message) {
const e = new Error(message);
e.code = "config.run_origin_invalid";
return e;
}
export function screenRecordedRunOrigin(recorded) {
if (recorded === undefined || isRunOrigin(recorded))
return recorded;
throw refusal(`the checkpoint row's runOrigin is ${describeRejectedValue(recorded)}, not a run origin this engine knows (${WORDS}) — the row cannot be resumed under a word nobody can read`);
}
export function runOriginOfLeg(declared, row) {
if (row !== undefined && declared !== undefined) {
throw refusal(`a resume config declares runOrigin ${describeRejectedValue(declared)} — a resume carries no run origin in its config: omit the field. ` +
`The resumed leg reads the word of the input that opens it — a wake states it on its message (message.runOrigin) — or else the parked row's (CheckpointSummary.runOrigin)`);
}
if (declared !== undefined && !isRunOrigin(declared)) {
throw refusal(`runOrigin ${describeRejectedValue(declared)} is not a run origin (${WORDS}) — omit it when the task was not started by the deployment's scheduler; an unreadable declaration is refused, never read as "not stated".`);
}
if (row === undefined)
return declared;
const opening = row.opening === undefined ? undefined : { word: row.opening.word };
if (opening !== undefined && opening.word !== undefined && !isRunOrigin(opening.word)) {
throw refusal(`the input that opens this leg declares runOrigin ${describeRejectedValue(opening.word)}, not a run origin (${WORDS}) — an unreadable declaration is refused, never read as "not stated".`);
}
return opening !== undefined ? opening.word : row.recorded;
}
export function wakeOpening(parked, message) {
const newest = message ?? parked[parked.length - 1];
return newest === undefined ? undefined : { word: newest.runOrigin };
}