openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
154 lines (153 loc) • 5.21 kB
JavaScript
import { a as isStructuredInputRecord, i as compileStructuredInputUrl, n as compileStructuredInputForm, o as snapshotStructuredInput } from "./structured-input-DSYgb17h.mjs";
import { t as runStructuredInput } from "./structured-input-execution-BMyxgore.mjs";
import { createHash } from "node:crypto";
//#region src/auto-reply/reply/acp-elicitation-handler.ts
const MAX_CORRELATION_TEXT = 128;
/** Adapts ACP wire scope and codex-acp metadata into the shared structured-input compiler. */
function parseAcpElicitationRequest(e) {
const t = snapshotStructuredInput(e);
if (!isStructuredInputRecord(t)) return unsupported("OpenClaw declined a malformed or over-limit ACP input request.");
const n = readScope(t);
if (typeof n === "string") return unsupported(n);
const r = readAcpElicitationString(t, "mode");
if (r === "url") return {
correlation: n,
input: compileStructuredInputUrl({
url: readValue(t, "url"),
elicitationId: readValue(t, "elicitationId"),
message: readValue(t, "message"),
fallbackMessage: "ACP provided a URL",
protocolName: "ACP"
})
};
if (r !== "form") return unsupported(`OpenClaw does not support ACP elicitation mode ${JSON.stringify(r ?? "unknown")}.`);
return {
correlation: n,
input: compileStructuredInputForm({
schema: readValue(t, "requestedSchema"),
message: readAcpElicitationString(t, "message"),
fallbackMessage: "ACP needs input",
options: {
protocolName: "ACP",
allowEmptyForm: true,
minimumChoiceCount: 2,
booleanLabels: ["True", "False"],
metadata: {
secretPath: [
"_meta",
"codex",
"isSecret"
],
otherAnswerPath: [
"_meta",
"codex",
"isOtherAnswer"
],
otherQuestionIdPath: [
"_meta",
"codex",
"questionId"
]
}
}
})
};
}
function readScope(e) {
const t = readValue(e, "sessionId");
const n = readValue(e, "requestId");
const r = t !== void 0;
if (r === (n !== void 0)) return "OpenClaw declined an ACP input request with an invalid or ambiguous scope.";
if (r) {
const n = readCorrelationText(t);
if (!n) return "OpenClaw declined an ACP input request with an invalid session id.";
const r = readValue(e, "toolCallId");
const i = r === void 0 || r === null ? r : readCorrelationText(r);
if (r !== void 0 && r !== null && !i) return "OpenClaw declined an ACP input request with an invalid tool-call id.";
return {
sessionId: n,
...r === void 0 ? {} : { toolCallId: i ?? null }
};
}
if (n === null || typeof n === "number") return { requestId: n };
const i = readCorrelationText(n);
return i ? { requestId: i } : "OpenClaw declined an ACP input request with an invalid request scope.";
}
function readCorrelationText(e) {
if (typeof e !== "string" || e.length === 0 || e.length > MAX_CORRELATION_TEXT) return;
for (const t of e) {
const e = t.codePointAt(0) ?? 0;
if (e <= 31 || e >= 127 && e <= 159) return;
}
return e;
}
function readValue(e, t) {
return Object.hasOwn(e, t) ? e[t] : void 0;
}
function readAcpElicitationString(e, t) {
const n = readValue(e, t);
return typeof n === "string" ? n : void 0;
}
function unsupported(e) {
return { input: {
kind: "unsupported",
message: e
} };
}
const DEFAULT_ELICITATION_TIMEOUT_MS = 9e5;
const MAX_REQUEST_ID_TEXT = 128;
function questionId(e) {
return `acp_${createHash("sha256").update(JSON.stringify(e)).digest("hex").slice(0, 24)}_${e.batch}`;
}
function cancellation(e) {
return {
action: "cancel",
_meta: { message: e }
};
}
function isContextRequestIdValid(e) {
return e === null || typeof e === "number" && Number.isFinite(e) || typeof e === "string" && e.length > 0 && e.length <= MAX_REQUEST_ID_TEXT;
}
/** Creates the turn-owned ACP form/URL bridge used by channel delivery. */
function createAcpElicitationHandler(e) {
const t = { onBlockReply: async (t) => {
await e.delivery.deliver("block", t);
} };
return async (n, r) => {
if (!isContextRequestIdValid(r.requestId) || r.signal.aborted || !e.isActive()) return cancellation("ACP input request is no longer active.");
const i = parseAcpElicitationRequest(n);
const a = await runStructuredInput({
input: i.input,
sessionKey: e.targetSessionKey,
agentId: e.agentId,
runId: e.runId,
timeoutMs: DEFAULT_ELICITATION_TIMEOUT_MS,
delivery: t,
signal: r.signal,
isActive: e.isActive,
questionId: (t) => questionId({
contextRequestId: r.requestId,
correlation: i.correlation,
sourceSessionKey: e.sourceSessionKey,
targetSessionKey: e.targetSessionKey,
outerRequestId: e.outerRequestId,
batch: t
}),
promptOptions: {
unsupportedIntro: "ACP input request could not be shown:",
urlIntro: "ACP needs confirmation:"
}
});
if (a.status === "answered") return i.input.kind === "ready" && i.input.plan.kind === "url" ? { action: "accept" } : {
action: "accept",
content: a.content
};
if (a.status === "declined" || a.status === "unsupported") return {
action: "decline",
...a.message ? { _meta: { message: a.message } } : {}
};
return cancellation(a.message ?? "ACP input request was cancelled.");
};
}
//#endregion
export { createAcpElicitationHandler };