@sema-agent/core
Version:
Stateless, task-oriented AI agent core
33 lines (32 loc) • 2 kB
JavaScript
import { thrownFailureOf } from "../core/runner/terminal-account.js";
import { terminalProjection } from "../core/runner/terminal-projection.js";
import { isPlainRecord } from "../core/mods/kernel/receive.js";
import { isEngineCodeShape, readMember, thrownMarksOf } from "../core/thrown-value.js";
import { egressText, egressThrownText, egressValue } from "../core/untrusted-egress.js";
export const childTerminalFace = (terminal) => {
const face = terminalProjection(terminal);
return typeof face.errorMessage === "string" ? { ...face, errorMessage: egressText("same-principal", face.errorMessage) } : face;
};
export const childThrownText = (err) => egressThrownText("same-principal", err);
export function childFailureOf(err) {
const { code, detail } = thrownFailureOf(err, "");
const errorKind = thrownMarksOf(err)?.errorKind;
return { ...(code !== undefined ? { code } : {}), ...(typeof errorKind === "string" ? { errorKind } : {}), ...(detail !== undefined ? { detail } : {}) };
}
export const rejectedChildOf = (err) => ({ text: childThrownText(err), failure: childFailureOf(err) });
export const childRowCode = (f) => f.code ?? f.errorKind;
const engineCodeOf = (f) => (f.code !== undefined && isEngineCodeShape(f.code) ? f.code : undefined);
export const childAnswerCode = (f) => engineCodeOf(f) ?? f.errorKind;
export const childRejection = (err) => {
const snapshot = egressValue("same-principal", err);
if (snapshot === null || typeof snapshot !== "object")
return snapshot;
const code = engineCodeOf(childFailureOf(snapshot));
const details = readMember(snapshot, "details");
if (!isPlainRecord(details) && code === undefined)
return snapshot;
const bag = isPlainRecord(details) ? details : {};
const { code: _stated, ...rest } = bag;
Object.defineProperty(snapshot, "details", { value: code !== undefined ? { ...rest, code } : rest, enumerable: true, writable: true, configurable: true });
return snapshot;
};