openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines (4 loc) • 4.59 kB
JavaScript
import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{Au as QuestionRequestQuestionSchema,Du as QuestionOptionSchema}from"./src-BDZMFmKa.mjs";import{n as ToolInputError}from"./tool-input-error-BCfbFipG.mjs";import"./common-DIgvX97L.mjs";import{t as questionShapeError}from"./question-validation-BU4-Vkv5.mjs";import{Value}from"typebox/value";import{Type}from"typebox";
//!src/agents/tools/ask-user-tool-normalization.ts
const DEFAULT_ASK_USER_TIMEOUT_SECONDS=900;const REQUIRED_ACTION_READY=`Ready to recheck`;const REQUIRED_ACTION_STOP=`Stop this task`;const QUESTION_RPC_GRACE_MS=1e4;const MIN_ASK_USER_TIMEOUT_SECONDS=30;const MAX_ASK_USER_TIMEOUT_SECONDS=3600;function normalizeAskUserParams(e){if(!Value.Check(AskUserToolSchema,e))throw new ToolInputError(`ask_user arguments do not match the model-facing question contract`);const t=[];const n=e.requiredAction;if(n){if(e.questions)throw new ToolInputError(`ask_user accepts questions or requiredAction, not both`);if(!n.reason.trim()||!n.instructions.trim())throw new ToolInputError(`requiredAction needs a specific blocker and supported manual step`);t.push({questionId:`required_action`,header:`Manual step`,question:[n.reason.trim(),n.instructions.trim(),...n.url?[n.url]:[]].join(`
`),...n.url?{url:n.url}:{},options:[{label:REQUIRED_ACTION_READY},{label:REQUIRED_ACTION_STOP}],isOther:false})}else{if(!e.questions)throw new ToolInputError(`ask_user requires questions or requiredAction`);t.push(...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 r=questionShapeError(t,{allowPlainSecretQuestions:false,validateUrls:true});if(r)throw new ToolInputError(r);return{questions:t,timeoutSeconds:normalizeQuestionTimeoutSeconds(e.timeoutSeconds),...n?{requiredAction:true}:{}}}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.Optional(Type.Array(AskUserQuestionSchema,{minItems:1,maxItems:3})),requiredAction:Type.Optional(Type.Object({reason:Type.String({minLength:1,maxLength:500,description:`Observed external blocker, not an assumed policy denial or inactive Goal status.`}),instructions:Type.String({minLength:1,maxLength:2e3,description:`External human-only step that can finish while this run waits. Never Goal Resume, an idle-session control, or approval of already authorized work.`}),url:Type.Optional({...QuestionRequestQuestionSchema.properties.url,description:`Optional verified HTTP(S) destination. Opening it is not completion or authorization.`})},{additionalProperties:false})),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});export{normalizeQuestionTimeoutSeconds as a,normalizeAskUserParams as i,QUESTION_RPC_GRACE_MS as n,resolveQuestionTimeoutMs as o,REQUIRED_ACTION_READY as r,AskUserToolSchema as t};