openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
144 lines (143 loc) • 5.85 kB
JavaScript
import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { Ji as validateQuestionGetParams, Qi as validateQuestionWaitAnswerParams, Xi as validateQuestionRequestParams, Yi as validateQuestionListParams, Zi as validateQuestionResolveParams } from "./src-BX8OAe1J.mjs";
import { O as validateAgentRunDelegatedAuthority, c as getActiveAgentRunDelegatedAuthority, v as registerAgentRunDelegatedAuthorityClosedHandler } from "./agent-run-registry-D7vkrqFT.mjs";
import { o as getGatewayToolCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs";
import { g as registerActiveEmbeddedRunHumanInputWait } from "./run-state-jrY5s7TG.mjs";
import { t as GatewayClientRequestError } from "./request-error-C4C6eD0S.mjs";
import { t as questionShapeError } from "./question-validation-CH0jzABc.mjs";
import { n as QuestionManagerError, r as QuestionManagerErrorCodes, t as QuestionManager } from "./question-manager-B2Tq1Fax.mjs";
//#region src/infra/embedded-question-broker.ts
const EMBEDDED_SECRET_STORE_REQUEST_BLOCKER = "Secret store requests need a running Gateway; ask the operator to run `openclaw secrets store` or use the Control UI.";
let activeBroker = null;
function invalidRequest(e) {
return new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: e
});
}
/** Serves the question RPC contract for one embedded backend lifetime. */
var EmbeddedQuestionBroker = class {
constructor(e, t) {
this.onEvent = t;
this.stopped = false;
this.removeAuthorityListener = registerAgentRunDelegatedAuthorityClosedHandler(() => {
this.manager.cancelClosedAuthorities();
});
this.manager = new QuestionManager(e);
}
notify(e) {
try {
this.onEvent?.(e);
} catch {}
}
request(e, t) {
t?.throwIfAborted();
if (this.stopped) throw new Error("Embedded question broker is stopped");
if (!validateQuestionRequestParams(e)) throw invalidRequest("Invalid question.request params");
if (e.questions.some((e) => e.secretStore)) throw invalidRequest(EMBEDDED_SECRET_STORE_REQUEST_BLOCKER);
const n = questionShapeError(e.questions, {
allowPlainSecretQuestions: true,
validateUrls: false
});
if (n) throw invalidRequest(n);
const r = getGatewayToolCallerIdentity();
const i = r?.approvalAuthority ?? (r?.operationalRunInstance ? getActiveAgentRunDelegatedAuthority(r.operationalRunInstance) : void 0);
const isRequesterActive = () => {
try {
return !t?.aborted && !r?.approvalSignals?.some((e) => e.aborted) && (!r?.operationalRunInstance || i !== void 0) && (!i || validateAgentRunDelegatedAuthority(i)) && r?.approvalAuthorityCheck?.() !== false && r?.receiptAuthority?.() !== false;
} catch {
return false;
}
};
const abort = () => {
this.manager.get(a.id);
};
const a = this.manager.request({
...structuredClone(e),
...r ? {
agentId: r.agentId,
sessionKey: r.sessionKey,
...r.operationalRunInstance ? { runId: r.operationalRunInstance.runId } : {}
} : {},
timeoutMs: e.timeoutMs ?? 9e5,
isRequesterActive,
registerHumanInputWait: i ? (e) => registerActiveEmbeddedRunHumanInputWait(i, e) : void 0,
onResolved: (e) => {
t?.removeEventListener("abort", abort);
this.notify({
event: "question.resolved",
payload: e
});
}
});
t?.addEventListener("abort", abort, { once: true });
this.notify({
event: "question.requested",
payload: a
});
return {
id: a.id,
expiresAtMs: a.expiresAtMs
};
}
async waitAnswer(e, t) {
t?.throwIfAborted();
if (!validateQuestionWaitAnswerParams(e)) throw invalidRequest("Invalid question.waitAnswer params");
return await racePromiseWithAbortSignal(this.manager.waitAnswer(e.id, e.timeoutMs, e.includeResolutionId), t);
}
resolve(e) {
if (!validateQuestionResolveParams(e)) throw invalidRequest("Invalid question.resolve params");
if ("cancel" in e) return this.manager.cancel(e.id, e.resolvedBy);
if (e.secretStoreAllowedHosts !== void 0) throw invalidRequest("Secret store allowed hosts require a store-bound question.");
return this.manager.resolve(e.id, e.answers, e.resolvedBy, { resolutionId: e.resolutionId });
}
get(e) {
if (!validateQuestionGetParams(e)) throw invalidRequest("Invalid question.get params");
const t = this.manager.get(e.id);
if (!t) throw new QuestionManagerError(QuestionManagerErrorCodes.NOT_FOUND, `question '${e.id}' was not found`);
return { question: t };
}
list(e = {}) {
if (!validateQuestionListParams(e)) throw invalidRequest("Invalid question.list params");
return { questions: this.manager.list() };
}
async call(e, t, n) {
n?.signal?.throwIfAborted();
try {
switch (e) {
case "question.request": return this.request(t, n?.signal);
case "question.waitAnswer": return await this.waitAnswer(t, n?.signal);
case "question.resolve": return this.resolve(t);
case "question.get": return this.get(t);
case "question.list": return this.list(t);
default: throw invalidRequest(`Unsupported embedded question method: ${e}`);
}
} catch (e) {
if (e instanceof QuestionManagerError) throw new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: e.message,
details: { reason: e.code }
});
throw e;
}
}
stop() {
if (this.stopped) return;
this.stopped = true;
this.removeAuthorityListener();
for (const e of this.manager.list()) this.manager.cancel(e.id, "tui:embedded:stopped");
this.manager.close();
}
};
function setEmbeddedQuestionBroker(e) {
activeBroker = e;
}
function clearEmbeddedQuestionBroker(e) {
if (activeBroker === e) activeBroker = null;
}
function getEmbeddedQuestionBroker() {
return activeBroker;
}
//#endregion
export { setEmbeddedQuestionBroker as i, clearEmbeddedQuestionBroker as n, getEmbeddedQuestionBroker as r, EmbeddedQuestionBroker as t };