@sema-agent/core
Version:
Stateless, task-oriented AI agent core
64 lines (63 loc) • 3.77 kB
JavaScript
import { runWithBrainTelemetry, runWithReasoningWireFacts, runWithStatusSink } from "../../brain/status-sink.js";
import { createSafeNotifier, observeThenableRejection } from "../safe-notify.js";
import { emitTrace } from "../trace.js";
import { inlineUntrusted } from "../untrusted-text.js";
export function runBrainSinks(input) {
const { queue, internals, rs, ident, parentToolCallId, observeReasoningWireFacts } = input;
const subagentName = parentToolCallId !== undefined && internals?.agentName !== undefined ? inlineUntrusted(internals.agentName.slice(0, 320), 80) : undefined;
const statusSinkNotifier = createSafeNotifier({
onError: (f) => console.warn(`[sema-core] ${f.site}: run-internals status sink threw (contained; further failures counted, not re-disclosed): ${f.error.message}`),
});
const statusEmit = (s) => {
const frame = {
type: "status",
phase: s.phase,
...(s.detail !== undefined ? { detail: s.detail } : {}),
...(s.retryInSec !== undefined ? { retryInSec: s.retryInSec } : {}),
...(s.retryInMs !== undefined ? { retryInMs: s.retryInMs } : {}),
...(s.retryAtMs !== undefined ? { retryAtMs: s.retryAtMs } : {}),
...(s.elapsedMs !== undefined ? { elapsedMs: s.elapsedMs } : {}),
...(s.timeoutMs !== undefined ? { timeoutMs: s.timeoutMs } : {}),
...(s.attempt !== undefined ? { attempt: s.attempt } : {}),
...(s.maxRetries !== undefined ? { maxRetries: s.maxRetries } : {}),
...(s.errClass !== undefined ? { errClass: s.errClass } : {}),
...(s.errorStatus !== undefined ? { errorStatus: s.errorStatus } : {}),
...ident(),
};
Object.freeze(frame);
const accepted = queue.push(frame);
if (accepted && internals?.onStatusEvent !== undefined) {
statusSinkNotifier.notify(() => observeThenableRejection(internals.onStatusEvent?.(frame), statusSinkNotifier, "runtask.onStatusEvent"), "runtask.onStatusEvent");
}
};
const telemetryEmit = (t) => {
emitTrace(rs.telemetry.tracer, () => t.kind === "failover"
? {
kind: "brain.failover",
version: 1,
taskId: rs.telemetry.taskId,
servedIndex: t.servedIndex,
total: t.total,
...(t.errorCode !== undefined ? { errorCode: t.errorCode } : {}),
ts: Date.now(),
}
: t.kind === "breaker"
? { kind: "breaker.transition", version: 1, taskId: rs.telemetry.taskId, key: t.key, phase: t.phase, failures: t.failures, ts: Date.now() }
: t.kind === "retry"
? {
kind: "brain.retry",
version: 1,
taskId: rs.telemetry.taskId,
attempt: t.attempt,
phase: t.phase,
...(t.errClass !== undefined ? { errClass: t.errClass } : {}),
...(t.nextDelayMs !== undefined ? { nextDelayMs: t.nextDelayMs } : {}),
ts: Date.now(),
}
: t.kind === "echo_dropped"
? { kind: "brain.echo_dropped", version: 1, taskId: rs.telemetry.taskId, field: t.field, ts: Date.now() }
: { kind: "vision.placeholder", version: 1, taskId: rs.telemetry.taskId, count: t.count, ts: Date.now() });
};
const withBrainSinks = (fn) => runWithStatusSink(statusEmit, () => runWithBrainTelemetry(telemetryEmit, () => runWithReasoningWireFacts(observeReasoningWireFacts, fn)));
return { subagentName, withBrainSinks };
}