UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

96 lines (95 loc) • 4.87 kB
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_INPUT_UNDRAINED_CAUSES = Object.freeze(["leg_end", "crashed"]); const STEER_UNDRAINED = "task.user_steer_undrained"; const FOLLOWUP_UNDRAINED = "task.user_followup_undrained"; export const USER_INPUT_UNDRAINED_FAMILIES = Object.freeze({ steer: Object.freeze({ code: STEER_UNDRAINED, countKey: "steer" }), followUp: Object.freeze({ code: FOLLOWUP_UNDRAINED, countKey: "followUp" }), }); 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 = []; const cause = "leg_end"; if (counts.steer > 0) { const message = `${counts.steer} user steer(s) ${tail}`; const count = { [USER_INPUT_UNDRAINED_FAMILIES.steer.countKey]: counts.steer, cause }; own.push({ code: STEER_UNDRAINED, message, detail: { ...count, ...tid, ...sid, ...rid } }); if (toParent) copies.push({ code: 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 = { [USER_INPUT_UNDRAINED_FAMILIES.followUp.countKey]: counts.followUp, cause }; own.push({ code: FOLLOWUP_UNDRAINED, message, detail: { ...count, ...tid, ...sid, ...rid } }); if (toParent) copies.push({ code: FOLLOWUP_UNDRAINED, message: `${message}${copyTail}`, detail: { ...count, ...ptid, sessionId: parentSession, childSessionId: sessionId } }); } return [...own, ...copies]; }