UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

42 lines (41 loc) • 1.75 kB
import { deliverEngineNotice } from "../engine-notice.js"; import { createSafeNotifier, observeThenableRejection, safeErrorMessage } from "../safe-notify.js"; const FORWARD_SINK_SITE = "runtask.onForwardEvent"; const ORPHAN_SCOPES = new WeakMap(); function forwardSinkFailedNotice(message) { return { code: "config.forward_sink_failed", message: `The subagent display sink (RunInternals.onForwardEvent) failed and the frame was dropped: ${message}. ` + `The run is unaffected — forwarding is a render channel — and further failures of this run are counted, not repeated.`, detail: { site: FORWARD_SINK_SITE, cause: message }, }; } export function createForwardSinkScope(onNotice) { return { notifier: createSafeNotifier({ onError: (f) => deliverEngineNotice(onNotice, forwardSinkFailedNotice(safeErrorMessage(f.error))), }), }; } function scopeOf(holder) { if (holder.forwardScope !== undefined) return holder.forwardScope; const existing = ORPHAN_SCOPES.get(holder); if (existing !== undefined) return existing; const scope = createForwardSinkScope(undefined); ORPHAN_SCOPES.set(holder, scope); return scope; } export function forwardSafely(holder, event) { if (holder?.onForwardEvent === undefined) return; const { notifier } = scopeOf(holder); notifier.notify(() => observeThenableRejection(holder.onForwardEvent?.(event), notifier, FORWARD_SINK_SITE), FORWARD_SINK_SITE); } export function forwardSinkFailures(holder) { if (holder === undefined) return 0; const scope = holder.forwardScope ?? ORPHAN_SCOPES.get(holder); return scope?.notifier.failuresAt(FORWARD_SINK_SITE) ?? 0; }