UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

50 lines (49 loc) • 2.42 kB
import { unfitFeedbackOf, writtenFeedback } from "../approval-words.js"; import { CheckpointError } from "../checkpoint-store.js"; import { asThrownError } from "../thrown-value.js"; import { sentinelTagRefusal } from "../untrusted-text.js"; export function refuseUnfitFeedback(twin) { const unfit = unfitFeedbackOf(writtenFeedback(twin.feedback), { allowed: twin.decision === "allow", byPerson: twin.hostDecision.decidedBy === "person" }); if (unfit === undefined) return; if (unfit === "on_a_deny") { throw feedbackRefusal("resume carried `feedback` on a `deny` — feedback is the approving person's words on an `allow` (a denial's words are `reason`); refusing rather than dropping it silently, the checkpoint stays pending"); } if (unfit === "by_a_machine") { throw feedbackRefusal(`resume carried \`feedback\` on a decide made by "${twin.hostDecision.decidedBy}" — feedback is delivered to the model as the user's own words, which only a person's decide can be; refusing rather than attributing a machine's text to the user, the checkpoint stays pending`); } throw feedbackRefusal(sentinelTagRefusal("resume allow feedback")); } function feedbackRefusal(message) { return new CheckpointError("checkpoint.invalid_outcome", message, { field: "feedback" }); } export function approvalWordsOf(outcome) { return outcome.gate === "policy_ask" && outcome.decision === "allow" ? writtenFeedback(outcome.feedback) : undefined; } export function withApprovalWords(continuation, engineSegments, words) { const glueStart = continuation.length; const withGlue = `${continuation}\n\n`; engineSegments.push({ start: glueStart, end: withGlue.length }); return withGlue + words; } export function approvalWordsSeat(harness, report) { return async (words) => { try { await harness.steer(words); } catch (err) { report(new Error(`the approving person's words could not be queued for the model: ${asThrownError(err).message}`)); } }; } const APPROVAL_WORDS_FRAMES = new WeakSet(); export function owedApprovalWords(words, inputId) { if (words === undefined) return []; const frame = { text: words, trusted: false, inputId }; APPROVAL_WORDS_FRAMES.add(frame); return [frame]; } export function isApprovalWordsFrame(frame) { return APPROVAL_WORDS_FRAMES.has(frame); }