UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

40 lines (39 loc) • 2.38 kB
import { startTimeout } from "./clock-and-limits.js"; import { forwardSafely } from "./forward-sink.js"; import { judgeUserRowSource } from "../user-row-origin.js"; export function runClockAndContent(input) { const { spec, prepared, queue, internals, rs, ident, parentToolCallId, startedToolCallIds, offerLegFrame } = 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 (e.type === "tool_start" || e.type === "tool_end") internals?.onSubtreeActivity?.(); if (parentToolCallId !== undefined) forwardSafely(internals, e); }; const ownCommittedTailRef = { current: undefined }; const emitCommitted = (entryId, role, toolCallId, userRow) => { ownCommittedTailRef.current = entryId; if (role === "user" && userRow === undefined) throw new TypeError("a committed user row names its source ({ origin, inputId? })"); if (role !== "user" && userRow !== undefined) throw new TypeError(`a ${role} row has no user-row source`); const source = userRow === undefined ? undefined : judgeUserRowSource(userRow); if (source?.origin === "human_input") prepared.runTimeReminders.elapsedClock.committed(source.inputId); if (role === "toolResult" && toolCallId !== undefined) startedToolCallIds.delete(toolCallId); offerLegFrame({ type: "message_committed", entryId, role, ...(toolCallId !== undefined ? { toolCallId } : {}), ...(source !== undefined ? { origin: source.origin, ...(source.inputId !== undefined ? { inputId: source.inputId } : {}), ...(source.noteKey !== undefined ? { noteKey: source.noteKey } : {}) } : {}), ...ident(), }); }; return { effectiveTimeoutMs, walltimeMonotonicDeadline, timeout, pushContent, ownCommittedTailRef, emitCommitted }; }