UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

76 lines (75 loc) • 4.17 kB
import { boundedTail } from "../session.js"; import { readMember } from "../thrown-value.js"; import { userAttachmentOf } from "../../internal/llm.js"; import { ParentChainGuard } from "../../internal/harness.js"; import { resumeAtOutsideWindowDetail } from "../resume-at-refusal.js"; import { rewindTargetNamed } from "./rewind-target.js"; export async function atLanding(start, held, storage) { const role = start.type === "message" ? start.message.role : undefined; if (role === "assistant" || start.type === "custom_message") return start.id; let children; if (storage.getChildren !== undefined) children = storage.getChildren.bind(storage); else { const entries = await held(); if (entries.some((e) => e.id === start.id)) children = async (id) => entries.filter((e) => e.parentId === id); } if (children === undefined) return undefined; const chain = new ParentChainGuard(); chain.stepOnto(start.id); for (let leaf = start.id;;) { const next = (await children(leaf)).find((e) => e.parentId === leaf && e.type === "message" && userAttachmentOf(e.message) !== undefined); if (next === undefined) return leaf; chain.stepOnto(next.id); leaf = next.id; } } export async function refuseBranchOutsideWindow(storage, landing, target, sessionId, asker, start = target, takesBefore = true) { const leaf = landing === undefined ? start : landing; const named = rewindTargetNamed(target, asker); if (leaf !== null) await judgePath(storage, leaf, named, sessionId); if (landing === undefined) { const from = start === target ? "" : ` (a leaf move to entry "${start}")`; throw refusal(`${named}${from} keeps the frames under it, and its attachment frames cannot be read: the entries session "${sessionId}"'s handle loaded do not include entry "${start}" (the store answered it by id) and the store has no read of an entry's successors, so where the frames end is not known and the branch would continue without them — retry on a handle that holds that entry (the whole log, or its own branch)${takesBefore ? `, or rewind with resumeAtMode "before" to drop the message and send it again with its attachments` : ""}`, "attachment_frames_unreadable", takesBefore); } return leaf; } async function judgePath(storage, leafId, named, sessionId) { let path; try { path = await storage.getPathToRoot(leafId); } catch (err) { const code = readMember(err, "code"); if (code === "corrupt_session") return; if (code !== "not_found") throw err; throw outsideWindow(named, sessionId, `the store does not hold entry "${leafId}", where the branch would continue, in the part of the log it loaded`); } const top = path[0]; if (top === undefined || !top.parentId || boundedTail(path).floorEntryId !== null) return; throw outsideWindow(named, sessionId, `the store's read of that branch stops at entry "${top.id}", the floor of the part of the log it loaded, and no compaction on the branch keeps from there — the conversation before "${top.id}" would be dropped with no summary of it`); } export function indexOnBranch(branch, entryId, named, sessionId) { const at = branch.findIndex((e) => e.id === entryId); const top = branch[0]; if (at >= 0 || top === undefined || !top.parentId) return at; throw refusal(`${named} is not on the part of session "${sessionId}"'s current branch its store loaded: the store's read of that branch stops at entry "${top.id}", the floor of the part of the log it loaded, so whether the entry is on the branch below it cannot be read`, "branch_not_held"); } function outsideWindow(named, sessionId, why) { return refusal(`${named} lands on a branch session "${sessionId}"'s store does not hold whole: ${why}`, "branch_not_held"); } function refusal(message, reason, takesBefore = true) { const e = new Error(message); e.code = "resume_at.outside_window"; e.detail = resumeAtOutsideWindowDetail(reason, takesBefore); return e; }