@sema-agent/core
Version:
Stateless, task-oriented AI agent core
52 lines (51 loc) • 2.57 kB
JavaScript
import { planReviewFactsOf } from "../plan-review-facts.js";
import { parkedApprovalCardOf } from "../pause-registry.js";
export const DEFAULT_RESOURCE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
export const USAGE_WINDOW_REAP_MARGIN_MS = 60 * 60 * 1000;
export const DEFAULT_UNATTENDED_APPROVAL_TTL_MS = 30 * 24 * 60 * 60 * 1000;
export function sanitizedTtlMs(ttlMs) {
if (ttlMs === undefined)
return undefined;
return Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : DEFAULT_RESOURCE_TTL_MS;
}
export function gatedCallIdOf(p) {
return p.pausedRef.current?.gatedCallId;
}
export function humanReviewSnapshot(ref) {
return { count: ref.count, totalWaitMs: ref.totalWaitMs, gates: [...ref.gates] };
}
export function parkContaminationMarker(refs) {
if (refs.pausedRef.current === undefined)
return undefined;
const gatedCallId = gatedCallIdOf(refs);
return { code: "gate.parked", ...(gatedCallId !== undefined ? { gatedCallId } : {}) };
}
export const HELD_CALL_TEXT = Object.freeze({
approval: "This call has not run yet: it needs approval, and the task paused here for it. If the task goes on without that approval, treat the call as not run.",
question: "This question has not been answered yet: the task paused here for a person's answer. If the task goes on without that answer, treat the question as unanswered.",
});
export function heldCallResultOf(refs, toolCallId) {
const paused = refs.pausedRef.current;
if (paused === undefined || paused.gatedCallId === undefined || paused.gatedCallId !== toolCallId)
return undefined;
const card = parkedApprovalCardOf(paused.cause.gate);
const text = card.kind === "card" && card.card === "question" ? HELD_CALL_TEXT.question : HELD_CALL_TEXT.approval;
return { content: [{ type: "text", text }], details: { code: "gate.parked", gatedCallId: toolCallId } };
}
export function publishCommittedSuspend(refs, token, gate, scope, remoteHandle, row, writes) {
const { pendingAction } = row;
const pause = {
cause: {
kind: "paused",
gate,
token,
...(row.checkpointId !== undefined ? { checkpointId: row.checkpointId } : {}),
...planReviewFactsOf(row),
...(remoteHandle !== undefined ? { restoreMode: remoteHandle.restoreMode === "park_only" ? "park_only" : "snapshot" } : {}),
},
scope,
...(pendingAction.kind === "tool_approval" ? { gatedCallId: pendingAction.toolCallId } : {}),
writes,
};
refs.pausedRef.current = pause;
}