UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

89 lines (88 loc) • 3.96 kB
import { describeRejectedValue } from "./tool-search-setting.js"; import { sanitizeUntrustedText } from "./untrusted-text.js"; export const INPUT_AUTHORS = Object.freeze(["operator", "parent_model", "workflow", "peer", "observer", "engine", "extension"]); const INPUT_AUTHOR_SET = new Set(INPUT_AUTHORS); export function isInputAuthor(v) { return INPUT_AUTHOR_SET.has(v); } const SAYS_NOTHING = /^[\s\p{Cc}]*$/u; export function isBlankInput(text) { return SAYS_NOTHING.test(text); } export const MAX_PENDING_STEER_CHARS = 16_000; export const STEERING_CONTROL_CHARS_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/; export function steeringTextDefect(text) { if (sanitizeUntrustedText(text) !== text) return "break_out_tag"; if (STEERING_CONTROL_CHARS_RE.test(text)) return "control_chars"; if (text.length > MAX_PENDING_STEER_CHARS) return "too_long"; return undefined; } export function steeringTextDefectClause(defect) { return defect === "break_out_tag" ? "contains a system-reminder break-out tag" : defect === "control_chars" ? "contains control characters" : `is longer than ${MAX_PENDING_STEER_CHARS} characters`; } export function readInputAuthor(raw) { return isInputAuthor(raw) ? raw : "unknown"; } const row = (screened, countsAsOperator, frame) => Object.freeze({ screened, countsAsOperator, frame }); const AUTHOR_ROWS = Object.freeze({ operator: row(true, true, "person"), parent_model: row(false, false, "verbatim"), workflow: row(false, false, "verbatim"), peer: row(false, false, "verbatim"), observer: row(false, false, "verbatim"), engine: row(false, false, "verbatim"), extension: row(true, false, "extension"), }); export function judgedAuthorOf(reading) { return reading === "unknown" ? "operator" : reading; } export function countsAsOperator(reading) { return AUTHOR_ROWS[judgedAuthorOf(reading)].countsAsOperator; } export function isScreened(reading) { return AUTHOR_ROWS[judgedAuthorOf(reading)].screened; } export function inputFrameOf(reading) { return AUTHOR_ROWS[judgedAuthorOf(reading)].frame; } export function objectiveAuthorOf(declared, onUnreadable) { if (declared === undefined) return "operator"; const reading = readInputAuthor(declared); if (reading === "unknown") { onUnreadable(new Error(`RunInternals.objectiveAuthor ${describeRejectedValue(declared)} is not an input author (${INPUT_AUTHORS.join(" / ")}) — the objective is screened as the operator's`)); } return reading; } export function recipeContinuationLeg(internals) { return { ...internals, objectiveAuthor: "engine" }; } const authoredSteers = new WeakMap(); export function bindAuthoredSteer(stream, steer) { authoredSteers.set(stream, steer); } export function authoredSteerOf(stream) { return authoredSteers.get(stream); } function countByAuthor(authors) { const counts = new Map(); for (const a of authors) counts.set(a, (counts.get(a) ?? 0) + 1); return counts; } const namedCounts = (counts) => [...counts].map(([a, n]) => `${n} from ${a}`).join(", "); export function droppedInputsDisclosure(authors) { return new Error(`the run ended with ${authors.length} accepted input(s) it never delivered that are not the operator's (${namedCounts(countByAuthor(authors))}): they are dropped — only an operator's input is carried across a pause or counted as the person's undelivered input`); } const DROPPED_INPUT_NOTICE_CODE = "task.undelivered_input_dropped"; export function droppedInputsNotice(authors, where) { const counts = countByAuthor(authors); return { code: DROPPED_INPUT_NOTICE_CODE, message: `The run ended before it read ${authors.length} accepted input(s) written by other authors (${namedCounts(counts)}): they are dropped, and no later run carries them.`, detail: { authors: Object.fromEntries(counts), ...where }, }; }