UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

23 lines (22 loc) • 1.41 kB
import { startTimeout } from "./clock-and-limits.js"; import { forwardSafely } from "./forward-sink.js"; export function runClockAndContent(input) { const { spec, prepared, queue, internals, rs, ident, parentToolCallId, startedToolCallIds } = input; const effectiveTimeoutMs = spec.limits?.maxWalltimeMs; const walltimeMonotonicDeadline = effectiveTimeoutMs !== undefined ? rs.telemetry.taskStartMonotonic + effectiveTimeoutMs : undefined; rs.counters.walltimeSyncBackstopFired = false; const timeout = startTimeout(prepared.harness, prepared.abortController, walltimeMonotonicDeadline !== undefined ? walltimeMonotonicDeadline - performance.now() : undefined, prepared.suspendForResource !== undefined); const pushContent = (e) => { queue.push(e); if (parentToolCallId !== undefined) forwardSafely(internals, e); }; const ownCommittedTailRef = { current: undefined }; const emitCommitted = (entryId, role, toolCallId) => { ownCommittedTailRef.current = entryId; if (role === "toolResult" && toolCallId !== undefined) startedToolCallIds.delete(toolCallId); queue.push({ type: "message_committed", entryId, role, ...(toolCallId !== undefined ? { toolCallId } : {}), ...ident() }); }; return { effectiveTimeoutMs, walltimeMonotonicDeadline, timeout, pushContent, ownCommittedTailRef, emitCommitted }; }