openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 4.71 kB
JavaScript
import { f as prepareQuestionInputAuthority, m as readQuestionDispatchCapability } from "./host-private-capabilities-DDumj0c_.mjs";
import { t as isEmbeddedMode } from "./embedded-mode-CncGOzNj.mjs";
//#region src/agents/harness/gateway-question-dispatch.ts
var QuestionDispatchRefusedError = class extends Error {
constructor(...e) {
super(...e);
this.name = "QuestionDispatchRefusedError";
}
};
function refuseQuestionDispatch(e) {
throw new QuestionDispatchRefusedError(e instanceof Error ? e.message : "question dispatch authority refused", { cause: e });
}
async function prepareQuestionDispatchAuthority(e) {
try {
const t = await prepareQuestionInputAuthority(e);
await t.prepareCurrent?.();
return t;
} catch (e) {
return refuseQuestionDispatch(e);
}
}
/** No input was submitted; the inherited name preserves legacy runtime refusal propagation. */
var QuestionDispatchUnsupportedError = class extends QuestionDispatchRefusedError {};
/** A core input was refused after question and source preparation. */
var PreparedQuestionAnswerRefusedError = class extends Error {
constructor(e) {
super(e instanceof Error ? e.message : "Prepared question input is no longer current", { cause: e });
this.name = "PreparedQuestionAnswerRefusedError";
}
};
function buildAgentQuestionRequestQuestions(e) {
return e.map(({ id: e, defaultAnswers: t, ...n }) => ({
...n,
questionId: e,
options: [...n.options ?? []],
...t ? { defaultAnswers: [...t] } : {}
}));
}
function buildAgentQuestionAnswers(e) {
return { answers: Object.fromEntries(Object.entries(e.answers).map(([e, t]) => [e, t.answers])) };
}
/** A failed transport cannot release possibly committed input for another route. */
var QuestionAnswerUnconfirmedError = class extends Error {
constructor(e) {
super("The question answer may have been accepted, but confirmation was lost. It was not sent again; check the conversation before retrying.", { cause: e });
this.name = "QuestionAnswerUnconfirmedError";
}
};
/** The effect starts inside the reader's synchronous consumer, before its authority expires. */
async function withQuestionDispatchAuthority(e, t) {
const n = readQuestionDispatchCapability(e?.assertCurrent);
const r = n?.callerRead;
if (!r) {
(n?.assertCompatibilityCurrent ?? e?.assertCurrent)?.();
return t();
}
const { withSessionEntriesFromStoresInWorker: i } = await import("./session-entry-read-runtime-24no1ay9.mjs");
const consumePrepared = (n) => {
r.assertPrepared(n);
e?.assertCurrent();
return { value: t() };
};
return (await i(r.reads, consumePrepared, { ordered: true })).value;
}
function resolveAgentQuestionGatewayCall(e) {
if (e && typeof e !== "function" && e.version !== 2) throw new Error("unsupported question dispatcher version");
let t = null;
return async (...n) => {
const [r, i, a, o] = n;
if (e && o?.dispatchAuthority?.kind === "run") {
const e = o.dispatchAuthority;
(readQuestionDispatchCapability(e.assertCurrent)?.assertCompatibilityCurrent ?? e.assertCurrent)();
}
if (typeof e === "function") {
if (o?.dispatchAuthority?.kind === "source-bound") {
o.dispatchAuthority.assertCurrent();
throw new QuestionDispatchUnsupportedError("source-bound question input requires the default or a version 2 dispatcher");
}
return n.length === 4 && (!o?.dispatchAuthority || o.signal) ? e(r, i, a, o?.signal ? { signal: o.signal } : void 0) : e(r, i, a);
}
if (e) return e.call({
method: r,
options: i,
params: a,
signal: o?.signal,
authority: o?.dispatchAuthority?.kind === "source-bound" ? {
kind: "source-bound",
assertCurrent: readQuestionDispatchCapability(o.dispatchAuthority.assertCurrent)?.assertCompatibilityCurrent ?? o.dispatchAuthority.assertCurrent,
assertCurrentAsync: o.dispatchAuthority.prepareCurrent
} : { kind: "unscoped" }
});
if (!t && isEmbeddedMode()) {
const { getEmbeddedQuestionBroker: e } = await import("./embedded-question-broker-DFFZ6EyE.mjs");
t = e();
}
if (t) {
await o?.dispatchAuthority?.prepareCurrent?.();
o?.signal?.throwIfAborted();
return withQuestionDispatchAuthority(o?.dispatchAuthority, () => t.call(r, a, o));
}
const { callGatewayTool: s } = await import("./gateway-question-dispatch.runtime.js");
return s(r, i, a, o);
};
}
//#endregion
export { buildAgentQuestionAnswers as a, refuseQuestionDispatch as c, QuestionDispatchUnsupportedError as i, resolveAgentQuestionGatewayCall as l, QuestionAnswerUnconfirmedError as n, buildAgentQuestionRequestQuestions as o, QuestionDispatchRefusedError as r, prepareQuestionDispatchAuthority as s, PreparedQuestionAnswerRefusedError as t, withQuestionDispatchAuthority as u };