openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
136 lines (135 loc) • 3.42 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
//#region extensions/anthropic/cli-user-input.ts
function createClaudeCliUserInputAuthorizer(e) {
const t = /* @__PURE__ */ new Map();
return { authorize(n) {
const r = n.toolUseId ? t.get(n.toolUseId) : void 0;
if (r) return r;
const i = runClaudeUserInput(e, n);
if (n.toolUseId) t.set(n.toolUseId, i);
return i;
} };
}
async function runClaudeUserInput(e, t) {
const n = readClaudeUserInputQuestions(t.input);
if (!n.ok) return {
behavior: "deny",
message: `OpenClaw rejected malformed Claude user questions: ${n.failure}. Correct the invalid field and retry AskUserQuestion.`
};
const r = n.questions;
const i = await e.requestUserInput({
toolName: "AskUserQuestion",
questions: r,
intro: "Claude needs input:",
...t.toolUseId ? { toolCallId: t.toolUseId } : {},
abortSignal: t.signal
});
if (i.status !== "answered") return {
behavior: "deny",
message: `${i.message} Continue with your best judgment.`
};
const a = {};
r.forEach((e) => {
a[e.question] = (i.answers[e.id] ?? []).join(", ");
});
return {
behavior: "allow",
updatedInput: {
...t.input,
answers: a
}
};
}
function readClaudeUserInputQuestions(e) {
const t = e.questions;
if (!Array.isArray(t) || t.length < 1 || t.length > 4) return {
ok: false,
failure: "questions must be an array of 1 to 4 questions"
};
const n = [];
for (const [e, r] of t.entries()) {
const t = `questions[${e}]`;
if (!isRecord(r)) return {
ok: false,
failure: `${t} must be an object`
};
const i = readBoundedText(r.question, 4096);
if (!i.ok) return {
ok: false,
failure: `${t}.question ${i.failure}`
};
const a = readBoundedText(r.header, 12);
if (!a.ok) return {
ok: false,
failure: `${t}.header ${a.failure}`
};
const o = r.options;
if (!Array.isArray(o) || o.length < 2 || o.length > 4) return {
ok: false,
failure: `${t}.options must be an array of 2 to 4 options`
};
if (typeof r.multiSelect !== "boolean") return {
ok: false,
failure: `${t}.multiSelect must be a boolean`
};
const s = [];
for (const [e, n] of o.entries()) {
const r = `${t}.options[${e}]`;
if (!isRecord(n)) return {
ok: false,
failure: `${r} must be an object`
};
const i = readBoundedText(n.label, 256);
if (!i.ok) return {
ok: false,
failure: `${r}.label ${i.failure}`
};
const a = readBoundedText(n.description, 1024, { shorten: true });
if (!a.ok) return {
ok: false,
failure: `${r}.description ${a.failure}`
};
s.push({
label: i.text,
description: a.text
});
}
n.push({
id: `question_${e + 1}`,
header: a.text,
question: i.text,
multiSelect: r.multiSelect,
isOther: true,
options: s
});
}
return {
ok: true,
questions: n
};
}
function readBoundedText(e, t, n = {}) {
if (typeof e !== "string") return {
ok: false,
failure: "must be a string"
};
if (e.length === 0) return {
ok: false,
failure: "must not be empty"
};
if (e.length > t) return n.shorten ? {
ok: true,
text: `${truncateUtf16Safe(e, t - 1)}\u2026`
} : {
ok: false,
failure: `must be at most ${t} characters`
};
return {
ok: true,
text: e
};
}
//#endregion
export { createClaudeCliUserInputAuthorizer as t };