@sema-agent/core
Version:
Stateless, task-oriented AI agent core
70 lines (69 loc) • 3.63 kB
JavaScript
import { callerText } from "./caller-text.js";
import { optionKeys, receiveMembers } from "./own-property-receipt.js";
import { describeRejectedValue } from "./tool-search-setting.js";
const HOST_INSTANCE_KEYS = optionKeys()({ instanceId: 0, startedAt: 0 });
const HOST_INSTANCE_ID_MAX_CHARS = 256;
function hostRefusal(message, field) {
const e = new Error(message);
e.code = "config.invalid_argument";
e.detail = Object.freeze({ field });
return e;
}
export function receiveHostInstance(raw, subject = "RunnerDeps.host") {
if (raw === undefined)
return undefined;
if (typeof raw !== "object" || raw === null) {
throw hostRefusal(`${subject} must be a plain object { instanceId, startedAt }, got ${raw === null ? "null" : typeof raw}`, subject);
}
let copy;
try {
copy = receiveMembers(raw, { subject, unit: "member", moment: "when host identity is declared" }, HOST_INSTANCE_KEYS, (msg, [stray]) => hostRefusal(msg, typeof stray === "string" ? `${subject}.${stray}` : subject));
}
catch (err) {
if (err instanceof Error && err.detail === undefined) {
const { member } = err;
const field = typeof member === "string" ? `${subject}.${member}` : subject;
err.detail = Object.freeze({ field });
}
throw err;
}
const idField = `${subject}.instanceId`;
const id = copy.instanceId;
if (typeof id !== "string") {
throw hostRefusal(`${idField} must be a non-empty string, got ${describeRejectedValue(id)}`, idField);
}
if (id.length === 0 || id.trim().length === 0) {
throw hostRefusal(`${idField} must be a non-empty visible string (got empty or whitespace-only)`, idField);
}
if (id.trim() !== id) {
throw hostRefusal(`${idField} must not carry leading or trailing whitespace (${callerText(id)})`, idField);
}
if (/[\u0000-\u001f\u007f-\u009f\u{2028}\u{2029}]/u.test(id)) {
throw hostRefusal(`${idField} must be a single-line visible string without control characters (${callerText(id)})`, idField);
}
if (/(?:^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?:$|[^\uDC00-\uDFFF])/.test(id)) {
throw hostRefusal(`${idField} must be well-formed Unicode`, idField);
}
if (id.length > HOST_INSTANCE_ID_MAX_CHARS) {
throw hostRefusal(`${idField} exceeds ${HOST_INSTANCE_ID_MAX_CHARS} UTF-16 code units (got ${id.length})`, idField);
}
const startedAtField = `${subject}.startedAt`;
const startedAt = copy.startedAt;
if (typeof startedAt !== "number" || !Number.isSafeInteger(startedAt) || startedAt < 0) {
throw hostRefusal(`${startedAtField} must be a non-negative safe integer epoch-ms timestamp, got ${describeRejectedValue(startedAt)}`, startedAtField);
}
return Object.freeze({ instanceId: id, startedAt });
}
export function readLegInstanceFact(raw) {
if (typeof raw !== "object" || raw === null) {
throw new Error(`the legInstance wiring fact must be { runId, startedAt }, got ${raw === null ? "null" : typeof raw}`);
}
const { runId, startedAt } = raw;
if (typeof runId !== "string" || runId.length === 0) {
throw new Error(`the legInstance wiring fact requires a non-empty string runId, got ${describeRejectedValue(runId)}`);
}
if (typeof startedAt !== "number" || !Number.isSafeInteger(startedAt) || startedAt < 0) {
throw new Error(`the legInstance wiring fact requires a non-negative safe integer epoch-ms startedAt, got ${describeRejectedValue(startedAt)}`);
}
return Object.freeze({ runId, startedAt });
}