@sema-agent/core
Version:
Stateless, task-oriented AI agent core
33 lines (32 loc) • 1.88 kB
JavaScript
import { types } from "node:util";
import { isPlainRecord, receive } from "../core/mods/kernel/receive.js";
import { isTaskArtifactRef } from "../core/tool-result-store.js";
const RECORD_ARTIFACT_RECEIPT = Object.freeze({ accessors: "refuse", depth: 1, others: "refuse", isProxy: (v) => types.isProxy(v) });
const RECORD_ARTIFACT_LINE = {
stored: (ref) => `Full record (the result and every agent's row): read it with ReadToolResult (ref "${ref}") before assuming an empty or unexpected result.`,
unconfirmed: () => "The full record (the result and every agent's row) could not be stored (the write failed or went unconfirmed), so no ref can read it back — it is not readable in this session.",
no_store: () => "The full record (the result and every agent's row) is not stored behind a ref (no offload store) — it is not readable in this session.",
};
const RECORD_UNREADABLE_LINE = "This run's record could not be read.";
function isRecordArtifactKind(word) {
return Object.hasOwn(RECORD_ARTIFACT_LINE, word);
}
function decodeRecordArtifact(raw) {
const received = receive(raw, RECORD_ARTIFACT_RECEIPT);
if (!received.ok || !isPlainRecord(received.value))
return undefined;
const { kind, ref } = received.value;
if (typeof kind !== "string" || !isRecordArtifactKind(kind))
return undefined;
if (kind === "stored")
return typeof ref === "string" && isTaskArtifactRef(ref) ? { kind, ref } : undefined;
return ref === undefined ? { kind } : undefined;
}
export function workflowRecordPickupOf(raw) {
const artifact = decodeRecordArtifact(raw);
if (artifact === undefined)
return { line: RECORD_UNREADABLE_LINE };
if (artifact.kind === "stored")
return { ref: artifact.ref, line: RECORD_ARTIFACT_LINE.stored(artifact.ref) };
return { line: RECORD_ARTIFACT_LINE[artifact.kind]() };
}