@sema-agent/core
Version:
Stateless, task-oriented AI agent core
64 lines (63 loc) • 2.82 kB
JavaScript
import { snapshotActorAssertion } from "../internal/llm.js";
import { uuidv7 } from "../internal/harness.js";
import { inlineUntrusted } from "./untrusted-text.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 mid-turn is surfaced: within the turn that is still running, often " +
"alongside the next tool result, rather than as a separate conversation turn. Address the message " +
"above as you continue this turn.";
export function frameMidTurnUserInput(projected) {
if (projected.trim().length === 0)
return projected;
return `${MID_TURN_USER_INPUT_HEAD}${projected}${MID_TURN_USER_INPUT_TAIL}`;
}
export function projectHumanInput(frame) {
if (frame.actor === undefined || frame.source === "system")
return frame.text;
if (frame.text.trim().length === 0)
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",
};
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 })) } : {}),
};
}
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 } : {}),
});
}