openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
79 lines (78 loc) • 3.72 kB
JavaScript
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { _u as QuestionRequestQuestionSchema, mu as QuestionOptionSchema } from "./src-BX8OAe1J.mjs";
import { n as ToolInputError } from "./tool-input-error-B1hgavSw.mjs";
import "./common-njQNtpnW.mjs";
import { t as questionShapeError } from "./question-validation-CH0jzABc.mjs";
import { Value } from "typebox/value";
import { Type } from "typebox";
//#region src/agents/tools/ask-user-tool-normalization.ts
const DEFAULT_ASK_USER_TIMEOUT_SECONDS = 900;
const QUESTION_RPC_GRACE_MS = 1e4;
const MIN_ASK_USER_TIMEOUT_SECONDS = 30;
const MAX_ASK_USER_TIMEOUT_SECONDS = 3600;
/** Validates and canonicalizes model-authored ask_user arguments. */
function normalizeAskUserParams(e) {
if (!Value.Check(AskUserToolSchema, e)) throw new ToolInputError("ask_user arguments do not match the model-facing question contract");
const t = e.questions.map((e) => ({
questionId: e.id.trim(),
header: truncateUtf16Safe(e.header.trim(), 12),
question: e.question.trim(),
options: e.options.map((e) => ({
label: e.label.trim(),
...e.description?.trim() ? { description: e.description.trim() } : {}
})),
...e.multiSelect === true ? { multiSelect: true } : {},
isOther: true
}));
if (!t.every((e) => Value.Check(QuestionRequestQuestionSchema, e))) throw new ToolInputError("ask_user questions do not match the required question format");
if (t.some(({ header: e, options: t }) => !e || t.some(({ label: e }) => e.length > 64))) throw new ToolInputError("ask_user questions exceed the model-facing display contract");
const n = questionShapeError(t, {
allowPlainSecretQuestions: false,
validateUrls: true
});
if (n) throw new ToolInputError(n);
return {
questions: t,
timeoutSeconds: normalizeQuestionTimeoutSeconds(e.timeoutSeconds)
};
}
/** Shared human-question wait contract, including credential entry and harness watchdogs. */
function normalizeQuestionTimeoutSeconds(e) {
if (e !== void 0 && (typeof e !== "number" || !Number.isFinite(e) || !Number.isInteger(e))) throw new ToolInputError("timeoutSeconds must be an integer");
return Math.min(MAX_ASK_USER_TIMEOUT_SECONDS, Math.max(MIN_ASK_USER_TIMEOUT_SECONDS, e ?? 900));
}
function resolveQuestionTimeoutMs(e) {
return normalizeQuestionTimeoutSeconds(e) * 1e3 + QUESTION_RPC_GRACE_MS;
}
const AskUserQuestionSchema = Type.Object({
id: {
...QuestionRequestQuestionSchema.properties.questionId,
description: "Unique snake_case answer key."
},
header: Type.String({
minLength: 1,
description: "Short chip label; longer input is truncated to 12 characters."
}),
question: {
...QuestionRequestQuestionSchema.properties.question,
description: "Single-sentence question only. Put all selectable choices in options."
},
options: Type.Array(QuestionOptionSchema, {
minItems: 2,
maxItems: 4,
description: "Every selectable choice. Put the recommended choice first; do not repeat choices only in the question text."
}),
multiSelect: Type.Optional({
...QuestionRequestQuestionSchema.properties.multiSelect,
description: "True only when the user may choose several options at once."
})
}, { additionalProperties: false });
const AskUserToolSchema = Type.Object({
questions: Type.Array(AskUserQuestionSchema, {
minItems: 1,
maxItems: 3
}),
timeoutSeconds: Type.Optional(Type.Integer({ description: "Maximum human wait in seconds; default 900, clamped 30-3600. Earlier run cancellation or overall run timeout still applies." }))
}, { additionalProperties: false });
//#endregion
export { resolveQuestionTimeoutMs as a, normalizeQuestionTimeoutSeconds as i, QUESTION_RPC_GRACE_MS as n, normalizeAskUserParams as r, AskUserToolSchema as t };