UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

28 lines (27 loc) • 1.73 kB
import { PROMPT_SCREEN_STATIONS } from "./stations.js"; import { foreignLine } from "../protocol-http-auth.js"; import { isBlankInput } from "../input-author.js"; const ARGUMENT = "text"; export function createPromptSubmitEntry(ref) { return async (args, ctx) => { const by = ctx.origin.plugin; const refused = (why) => `${by}: $.prompt.submit: refused: ${why}`; const extra = Object.keys(args).find((k) => k !== ARGUMENT); if (extra !== undefined) throw new TypeError(refused(`takes { text }; "${foreignLine(extra, 40)}" is not one`)); const text = args[ARGUMENT]; if (typeof text !== "string" || isBlankInput(text)) throw new TypeError(refused("no { text } (a non-empty string)")); const seat = ref?.seat; const handlers = ctx.frames.filter((f) => f.kind === "handler"); const waitedOn = handlers.some((f) => PROMPT_SCREEN_STATIONS.includes(f.event)) || (handlers.length > 0 && seat?.inputWindow() === "before"); if (waitedOn) throw new Error(refused("called from a handler the leg is waiting on before it can take this input; answer { text } instead, or submit from a later event (turn.start / turn.complete)")); if (seat === undefined) return Object.freeze({ drop: "no running leg takes a prompt here" }); const answer = await seat.relay("extension", text, { frames: ctx.frames, origin: ctx.origin, extension: by, priority: "later", signal: ctx.signal }); if ("entered" in answer) return Object.freeze({ text: answer.entered }); return "replied" in answer ? Object.freeze({ reply: answer.replied }) : Object.freeze({ drop: answer.drop }); }; }