UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

91 lines (90 loc) • 4.47 kB
import { snapshotActorAssertion } from "../internal/llm.js"; import { uuidv7 } from "../internal/harness.js"; import { isBlankInput } from "./input-author.js"; import { inlineUntrusted } from "./untrusted-text.js"; import { formatHookFeedback } from "./reminder-mint.js"; import { attrEscape, EXTERNAL_SOURCE_MAX } from "./task-notification.js"; const MID_TURN_USER_INPUT_HEAD = "The user sent a new message while you were working:\n"; const MID_TURN_USER_INPUT_TAIL = "\n\nThis is how a message sent while you were working is surfaced: next to the next tool result when you " + "were mid-task, or as the next turn when your last reply was already complete. Address the message above, " + "continuing what you were doing where that still applies."; export function frameMidTurnUserInput(projected) { if (isBlankInput(projected)) return projected; return `${MID_TURN_USER_INPUT_HEAD}${projected}${MID_TURN_USER_INPUT_TAIL}`; } const EXTENSION_INPUT_TAIL = "\n\nThis is how a prompt an extension submits is surfaced: between turns, starting this turn in the user's place. Address " + "the message above."; export function frameExtensionInput(name, projected) { if (isBlankInput(projected)) return projected; return `${name !== undefined ? `The extension "${name}"` : "An extension"} sent a message:\n${projected}${EXTENSION_INPUT_TAIL}`; } export function personSteerFrame(text, actor, trusted, reminderMark, source) { const projected = projectHumanInput({ text, actor, source }); if (trusted) return formatHookFeedback(projected, reminderMark); return source === "system" ? projected : frameMidTurnUserInput(projected); } export function projectHumanInput(frame) { if (frame.actor === undefined || frame.source === "system") return frame.text; if (isBlankInput(frame.text)) return frame.text; const label = attrEscape(inlineUntrusted(frame.actor.id, EXTERNAL_SOURCE_MAX)); const mark = frame.actor.hostAsserted ? "" : " (unverified)"; return `[from "${label}"${mark}]\n${frame.text}`; } const HANDLED_HUMAN_INPUT_KEYS = { type: "minted-here", inputId: "minted-here", sessionSeq: "forwarded", carrier: "forwarded", source: "forwarded", issuer: "forwarded", actor: "forwarded", delivery: "forwarded", principal: "forwarded", entryId: "forwarded", attachments: "forwarded", runOrigin: "forwarded", rewrittenBy: "forwarded", author: "forwarded", extension: "forwarded", }; export function buildHumanInputEvent(input) { void HANDLED_HUMAN_INPUT_KEYS; return { type: "human_input", inputId: input.inputId ?? uuidv7(), sessionSeq: input.sessionSeq, carrier: input.carrier, source: input.source, delivery: input.delivery, ...(input.issuer !== undefined ? { issuer: input.issuer } : {}), ...(input.actor !== undefined ? { actor: snapshotActorAssertion(input.actor) } : {}), ...(input.principal !== undefined ? { principal: input.principal } : {}), ...(input.entryId !== undefined ? { entryId: input.entryId } : {}), ...(input.attachments !== undefined && input.attachments.length > 0 ? { attachments: input.attachments.map((a) => ({ ...a })) } : {}), ...(input.runOrigin !== undefined ? { runOrigin: input.runOrigin } : {}), ...(input.rewrittenBy !== undefined ? { rewrittenBy: Object.freeze([...input.rewrittenBy]) } : {}), ...(input.author !== undefined ? { author: input.author } : {}), ...(input.extension !== undefined ? { extension: input.extension } : {}), }; } export function buildAppliedHumanInputEvent(input) { return buildHumanInputEvent({ carrier: input.carrier, source: input.source, delivery: "applied", sessionSeq: input.sessionSeq, inputId: input.inputId, ...(input.actor !== undefined ? { actor: input.actor } : {}), ...(input.actor?.issuer !== undefined ? { issuer: input.actor.issuer } : {}), ...(input.principal !== undefined ? { principal: input.principal } : {}), ...(input.runOrigin !== undefined ? { runOrigin: input.runOrigin } : {}), ...(input.rewrittenBy !== undefined ? { rewrittenBy: input.rewrittenBy } : {}), ...(input.author !== undefined ? { author: input.author } : {}), ...(input.extension !== undefined ? { extension: input.extension } : {}), }); }