UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

106 lines (105 loc) • 4.79 kB
import { FileError, err, ok } from "../../internal/harness.js"; import { thrownText } from "../../core/thrown-value.js"; import { envErrorDetail } from "./env-error-detail.js"; export async function openBoundAt(env, key, signal) { try { const open = env.openBound; if (open === undefined) return { kind: "unbindable" }; const opened = await open.call(env, key, signal); if (opened.ok === true) { if (typeof opened.value !== "object" || opened.value === null) throw new TypeError("it answered ok without an object"); return { kind: "bound", object: opened.value }; } if (opened.ok !== false || typeof opened.error?.code !== "string") throw new TypeError("it answered no result"); return opened.error.code === "not_supported" ? { kind: "unbindable" } : { kind: "refused", error: opened.error }; } catch (thrown) { return { kind: "refused", error: new FileError("unknown", `openBound gave no answer: ${envErrorDetail(thrownText(thrown))}`, key) }; } } export function sameKeyAgain(judge, key) { return async (signal) => { const again = await judge(signal).catch(() => undefined); return again?.ok === true && again.key === key; }; } export async function readWithin(object, max, signal) { const before = await object.size(); if (!before.ok) return { kind: "failed", error: before.error }; if (before.value > max) return { kind: "over", size: before.value, on: "size" }; const read = await object.readBytes(max, signal); if (read.ok) return { kind: "bytes", bytes: read.value }; if (read.error.code !== "invalid") return { kind: "failed", error: read.error }; const after = await object.size(); return after.ok && after.value > max ? { kind: "over", size: after.value, on: "read" } : { kind: "failed", error: read.error }; } export async function readObjectAt(env, key, rejudge, signal) { return objectOfOpening(env, key, rejudge, await openBoundAt(env, key, signal), signal); } export async function objectOfOpening(env, key, rejudge, opening, signal) { if (opening.kind === "bound") return { ok: true, value: opening.object }; if (opening.kind === "refused") return { ok: false, error: opening.error, step: "open" }; const simulated = await simulatedObjectAt(env, key, rejudge, signal); return simulated.ok ? simulated : { ok: false, error: simulated.error, step: "look" }; } export async function simulatedObjectAt(env, key, rejudge, signal) { const changed = () => new FileError("precondition_failed", "The file changed while it was read by name (a link or another object took its place), so it was not read through", key); const judged = async (s) => { const stillTheKey = await rejudge(s); if (s?.aborted === true) return err(new FileError("aborted", "aborted", key)); if (!stillTheKey) return err(changed()); const look = await env.fileInfo(key, s); if (!look.ok) return look; if (look.value.kind === "symlink") return err(new FileError("precondition_failed", "A link stands on the path now, so it is not the object that was checked", key)); return ok({ kind: look.value.kind, size: look.value.size }); }; const first = await judged(signal); if (!first.ok) return first; const kind = first.value.kind; return ok({ kind, size: async () => { const now = await judged(signal); if (!now.ok) return now; return now.value.kind === kind ? ok(now.value.size) : err(changed()); }, readBytes: async (max, readSignal) => { if (kind !== "file") return err(new FileError("is_directory", "The object is a directory", key)); const read = await env.readBinaryFile(key, readSignal, max + 1); if (!read.ok) return read; if (read.value.byteLength > max) return err(new FileError("invalid", `The file is over the ${max}-byte read cap (${read.value.byteLength} bytes or more); it was not read`, key)); const after = await judged(readSignal); if (!after.ok) return after; return after.value.kind === "file" ? read : err(changed()); }, byName: { key, check: async (s) => { const now = await judged(s); if (!now.ok) return now; return now.value.kind === kind ? ok(undefined) : err(changed()); }, }, close: async () => { }, }); }