UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

93 lines (92 loc) • 5.98 kB
import { createAskUserQuestionTool, isLiveQuestionFace } from "../ask-question.js"; import { boundInputHashOf } from "../canonical-json.js"; import { durableParkOptInDeclared } from "../tool-policy.js"; import { CONTENT_ASK_BINDING_CAP } from "./content-ask-bindings.js"; import { RosterBuilder } from "../tool-roster.js"; import { questionSeatFact } from "../tool-mount-facts.js"; export { CONTENT_ASK_BINDING_CAP } from "./content-ask-bindings.js"; export function prepareQuestionFace(input) { const { frozenOnQuestion, spec, askSource, deps, checkpointStore, runtimeCaps, approvalParkNotResumable, resolvedInteractionPosture, resume, sessionId, roster, inheritedUnavailableAsks } = input; const tools = roster.tools; const onQuestion = frozenOnQuestion; const liveQuestionFace = isLiveQuestionFace(onQuestion) ? onQuestion : undefined; const contentAskBindings = new Map(); const questionParkStoreWired = checkpointStore !== undefined; const questionDurableMandateBinding = runtimeCaps?.forceDurableGate === true && questionParkStoreWired; const contentAskRoutable = (toolCallId) => liveQuestionFace !== undefined && mountedQuestionTool !== undefined && tools.includes(mountedQuestionTool) && !questionDurableMandateBinding && !inheritedUnavailableAsks().has(toolCallId); const lateStrandedAnswers = []; const discloseStrandedAnswers = (records, why) => { if (records.length === 0) return; try { deps.onError?.(new Error(`AskUserQuestion: ${records.length} question(s) were answered by a person but no call ` + `collected the answer (${records.map((r) => `${r.toolCallId} [delivery ${r.deliveryId}]`).join(", ")}) — ${why}. The answer(s) were NOT ` + `delivered to the model and are gone with this leg; re-ask if the decision is still needed.`), { phase: "degraded", sessionId, classification: "unconsumed-human-answer" }); } catch { } }; const strandLateAnswer = (record) => { lateStrandedAnswers.push(record); discloseStrandedAnswers([record], "the call's wait had ended (the run's stop, or a halt or steer-now of the turn) before the answer arrived"); }; const settleContentAskBindings = () => { const stranded = []; for (const [callId, bound] of contentAskBindings) { if (bound.kind === "answered") stranded.push({ deliveryId: bound.deliveryId, toolCallId: callId }); } contentAskBindings.clear(); discloseStrandedAnswers(stranded, "the leg ended first (abort, batch teardown, or a loop failure)"); const byDelivery = new Map(); for (const r of [...stranded, ...lateStrandedAnswers]) byDelivery.set(r.deliveryId, r); return [...byDelivery.values()]; }; const durableQuestionFace = questionParkStoreWired && durableParkOptInDeclared({ forceDurableGate: runtimeCaps?.forceDurableGate === true, durableApprovalDeclared: spec.durableApproval !== undefined, approvalParkNotResumable }); const mountedQuestionFace = liveQuestionFace !== undefined ? async (req, signal) => { const bound = contentAskBindings.get(req.toolCallId); if (bound !== undefined) { if (bound.questionsHash === boundInputHashOf(req.questions)) { contentAskBindings.delete(req.toolCallId); if (bound.kind === "answered") return bound.answer; throw bound.error; } contentAskBindings.delete(req.toolCallId); } return liveQuestionFace(req, signal); } : onQuestion; roster.mounts.know({ "seat.question": questionSeatFact({ face: onQuestion !== undefined, parkStore: questionParkStoreWired, forceDurableGate: runtimeCaps?.forceDurableGate === true, durableApprovalDeclared: spec.durableApproval !== undefined, approvalParkNotResumable }) }); const questionToolMounted = roster.mounts.judge("ask-user-question"); let mountedQuestionTool; if (roster.mounts.judge("ask-user-question")) roster.mount((mountedQuestionTool = createAskUserQuestionTool(mountedQuestionFace, askSource, { ...(resume?.redeemedContentAskCallId !== undefined ? { redeemedApprovalCallId: resume.redeemedContentAskCallId, ...(resume.redeemedContentAskQuestionsHash !== undefined ? { redeemedApprovalQuestionsHash: resume.redeemedContentAskQuestionsHash } : {}), } : {}), ...(resolvedInteractionPosture !== undefined ? { posture: resolvedInteractionPosture } : {}), ...(spec.interactiveQuestionFallback === true ? { interactiveFallback: true } : {}), onStrandedAnswer: strandLateAnswer, onSyntheticContinuation: ({ questionId, reason }) => { deps.onError?.(new Error(`AskUserQuestion ${questionId}: no human answer was obtainable (` + (reason === "seam_absent" ? "no onQuestion seam is wired" : reason === "declined_unavailable" ? "the wired question channel reported nobody was reachable" : "the wired question channel failed") + `) — the model was instructed to self-answer and the run CONTINUES (warning, not a failure).`), { phase: "degraded", sessionId, classification: "no-human-autoanswered" }); }, })), { source: "builtin", mountedBy: "optional" }); return { checkpointStore, liveQuestionFace, contentAskBindings, contentAskRoutable, strandLateAnswer, settleContentAskBindings, durableQuestionFace, mountedQuestionFace, questionToolMounted, mountedQuestionTool }; }