@sema-agent/core
Version:
Stateless, task-oriented AI agent core
93 lines (92 loc) • 4.25 kB
JavaScript
import { userAttachmentOf } from "../internal/llm.js";
export const USER_ROW_ORIGINS = Object.freeze(["human_input", "attachment", "interruption_marker", "queued_note"]);
const USER_ROW_ORIGIN_SET = new Set(USER_ROW_ORIGINS);
export function isUserRowOrigin(v) {
return USER_ROW_ORIGIN_SET.has(v);
}
function describe(v) {
return typeof v === "string" ? JSON.stringify(v.length > 40 ? `${v.slice(0, 40)}…` : v) : v === null ? "null" : typeof v;
}
export function judgeInputId(inputId) {
if (typeof inputId !== "string" || inputId === "") {
throw new TypeError(`a user row's inputId must be a non-empty string (got ${describe(inputId)})`);
}
return inputId;
}
export function usableInputId(inputId) {
return typeof inputId === "string" && inputId !== "" ? inputId : undefined;
}
export function judgeUserRowSource(source) {
if (typeof source !== "object" || source === null) {
throw new TypeError(`a committed user row names its source ({ origin, inputId?, noteKey? }); got ${describe(source)}`);
}
const { origin, inputId, noteKey } = source;
if (!isUserRowOrigin(origin)) {
throw new TypeError(`a committed user row's origin must be one of ${USER_ROW_ORIGINS.join(" / ")} (got ${describe(origin)})`);
}
if (noteKey !== undefined) {
if (origin !== "queued_note")
throw new TypeError(`a ${origin} row carries no noteKey — only a queued_note row is a handed-over frame's`);
if (typeof noteKey !== "string" || noteKey === "")
throw new TypeError(`a queued_note row's noteKey must be a non-empty string (got ${describe(noteKey)})`);
}
if (inputId === undefined)
return noteKey === undefined ? { origin } : { origin, noteKey };
if (origin !== "human_input" && origin !== "attachment") {
throw new TypeError(`a ${origin} row carries no inputId — only a row of a person's input (human_input, attachment) is under a key`);
}
return { origin, inputId: judgeInputId(inputId) };
}
export function personInputRow(inputId) {
return inputId === undefined ? { origin: "human_input" } : { origin: "human_input", inputId: judgeInputId(inputId) };
}
export function stampUserRowOrigin(row) {
if (row.role !== "user" || row.origin !== undefined)
return;
const record = userAttachmentOf(row);
row.origin = record !== undefined && record.origin === undefined ? "attachment" : "queued_note";
}
export function stampLegFirstInput(row, first) {
if (row.role !== "user" || row.origin !== "human_input")
return false;
if (row.legFirstInput === undefined)
row.legFirstInput = first;
return true;
}
export function legFirstInputOf(row) {
if (typeof row !== "object" || row === null)
return undefined;
const m = row;
if (m.role !== "user" || m.origin !== "human_input")
return undefined;
return typeof m.legFirstInput === "boolean" ? m.legFirstInput : undefined;
}
export function isPersonsWordsRow(row) {
if (typeof row !== "object" || row === null || row.role !== "user" || userAttachmentOf(row) !== undefined)
return false;
const source = userRowSourceOf(row);
return source !== undefined ? source.origin === "human_input" : row.origin === undefined;
}
export function userRowSourceOf(row) {
if (typeof row !== "object" || row === null)
return undefined;
const m = row;
if (m.role !== "user" || !isUserRowOrigin(m.origin))
return undefined;
switch (m.origin) {
case "human_input": {
const key = usableInputId(m.inputId);
return key === undefined ? { origin: "human_input" } : { origin: "human_input", inputId: key };
}
case "attachment": {
const key = usableInputId(userAttachmentOf(row)?.inputId);
return key === undefined ? { origin: "attachment" } : { origin: "attachment", inputId: key };
}
case "interruption_marker":
return { origin: "interruption_marker" };
case "queued_note": {
const noteKey = m.engineNoteKey;
return typeof noteKey === "string" && noteKey !== "" ? { origin: "queued_note", noteKey } : { origin: "queued_note" };
}
}
}