@sema-agent/core
Version:
Stateless, task-oriented AI agent core
50 lines (49 loc) • 1.82 kB
JavaScript
import { asThrownError } from "../thrown-value.js";
import { parseEntryFile, serializeEntryFile } from "./frontmatter.js";
export function committedShadowOrFault(backend, id, scopes) {
try {
const content = backend.readCommittedShadow?.(id, scopes ?? undefined);
return content === undefined ? { fault: false } : { fault: false, content };
}
catch (err) {
return { fault: true, error: asThrownError(err) };
}
}
async function entryOf(face, id, scopes) {
const [entry] = await face.getByIds([id]);
if (entry === undefined || entry.id !== id)
return undefined;
return scopes === null || scopes.includes(entry.scope) ? entry : null;
}
export async function committedCopyOrFault(backend, id, scopes, face) {
let fault = false;
if (face !== undefined) {
try {
const entry = await entryOf(face, id, scopes);
if (entry !== undefined)
return entry === null ? { fault: false } : { content: serializeEntryFile(entry), fault: false };
}
catch {
fault = true;
}
}
const viaShadow = committedShadowOrFault(backend, id, scopes);
if (!viaShadow.fault && viaShadow.content !== undefined)
return { content: viaShadow.content, fault: false };
return { fault: fault || viaShadow.fault };
}
export async function committedFrontmatterOf(backend, id, face) {
if (face !== undefined) {
try {
const entry = await entryOf(face, id, null);
if (entry)
return entry.frontmatter;
}
catch {
}
}
const read = committedShadowOrFault(backend, id, null);
if (read.fault)
throw read.error;
return read.content !== undefined ? parseEntryFile(read.content).frontmatter : undefined;
}