@sema-agent/core
Version:
Stateless, task-oriented AI agent core
89 lines (88 loc) • 4.48 kB
JavaScript
let malformedNoticeSeatAnnounced = false;
export function __resetMalformedNoticeSeatAnnouncement() {
malformedNoticeSeatAnnounced = false;
}
export function deliverEngineNotice(onNotice, notice) {
if (notice.sessionId === undefined && typeof notice.detail?.["sessionId"] === "string") {
notice = { ...notice, sessionId: notice.detail["sessionId"] };
}
if (typeof onNotice === "function") {
try {
const r = onNotice(notice);
if (typeof r?.then === "function") {
r.then(undefined, () => {
});
}
}
catch {
}
return;
}
if (onNotice !== undefined) {
if (!malformedNoticeSeatAnnounced) {
malformedNoticeSeatAnnounced = true;
try {
console.warn(`The structured notice sink (RunnerDeps.onNotice) holds ${onNotice === null ? "null" : typeof onNotice} — not a ` +
`function. Notices fall back to console.warn until the wiring is fixed (omit the key, or wire a function).`);
}
catch {
}
}
}
console.warn(notice.message);
}
export function owedUserInputCounts(held, migrated, continuation) {
const owedContinuation = (continuation.rowReturned ? continuation.frames.filter((f) => f.seq === undefined).length : continuation.frames.length) - continuation.carried;
return {
steer: Math.max(0, (held?.steer ?? 0) - migrated.steer) + Math.max(0, owedContinuation),
followUp: Math.max(0, (held?.followUp ?? 0) - migrated.followUp),
};
}
export function settleOwedUserInput(account, rowReturned, ids, onNotice) {
if (SETTLED_OWED_ACCOUNTS.has(account))
return;
SETTLED_OWED_ACCOUNTS.add(account);
try {
const owed = owedUserInputCounts(account.held, account.migrated, { ...account.continuation, rowReturned });
for (const notice of undrainedUserInputNotices(owed, ids.taskId, ids.sessionId, ids.runId, ids.parent))
deliverEngineNotice(onNotice, notice);
}
catch {
}
}
const SETTLED_OWED_ACCOUNTS = new WeakSet();
export const USER_STEER_UNDRAINED_CAUSES = Object.freeze(["leg_end", "crashed"]);
export function parentLegIds(internals) {
const parentSessionId = internals?.parentSessionId;
if (typeof parentSessionId !== "string" || parentSessionId === "")
return undefined;
const parentTaskId = internals?.parentTaskId;
return { sessionId: parentSessionId, ...(typeof parentTaskId === "string" && parentTaskId !== "" ? { taskId: parentTaskId } : {}) };
}
export function undrainedUserInputNotices(counts, taskId, sessionId, runId, parent) {
const tid = taskId !== undefined ? { taskId } : {};
const sid = sessionId !== undefined ? { sessionId } : {};
const rid = runId !== undefined ? { runId } : {};
const tail = `accepted as "queued" were never consumed — the run ended first. They are NOT redelivered; re-send against a live run if still wanted.`;
const parentSession = typeof parent?.sessionId === "string" && parent.sessionId !== "" ? parent.sessionId : undefined;
const toParent = parentSession !== undefined && typeof sessionId === "string" && sessionId !== "" && parentSession !== sessionId;
const ptid = typeof parent?.taskId === "string" && parent.taskId !== "" ? { taskId: parent.taskId } : {};
const copyTail = ` (a child leg, ${sessionId})`;
const own = [];
const copies = [];
if (counts.steer > 0) {
const message = `${counts.steer} user steer(s) ${tail}`;
const count = { steer: counts.steer, cause: "leg_end" };
own.push({ code: "task.user_steer_undrained", message, detail: { ...count, ...tid, ...sid, ...rid } });
if (toParent)
copies.push({ code: "task.user_steer_undrained", message: `${message}${copyTail}`, detail: { ...count, ...ptid, sessionId: parentSession, childSessionId: sessionId } });
}
if (counts.followUp > 0) {
const message = `${counts.followUp} user follow-up(s) ${tail}`;
const count = { followUp: counts.followUp };
own.push({ code: "task.user_followup_undrained", message, detail: { ...count, ...tid, ...sid, ...rid } });
if (toParent)
copies.push({ code: "task.user_followup_undrained", message: `${message}${copyTail}`, detail: { ...count, ...ptid, sessionId: parentSession, childSessionId: sessionId } });
}
return [...own, ...copies];
}