openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
11 lines (8 loc) • 8.25 kB
JavaScript
import{c as normalizeOptionalLowercaseString,l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import"./src-0ci69Prg.mjs";import{t as expectDefined}from"./expect-DkJCbmLj.mjs";import{a as asOptionalRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as isWellFormedApprovalId}from"./approval-id-Bjlr6lXj.mjs";import{o as resolveExecApprovalAllowedDecisions}from"./exec-approvals-policy-DUqwCsoC.mjs";import{t as formatFencedCodeBlock}from"./markdown-code-rYi19S0p.mjs";import"./exec-approvals-BLANskJu.mjs";import{n as summarizeApprovalScope}from"./approval-scope-BgbNHTF5.mjs";import{o as supportsNativeExecApprovalClient,r as listNativeExecApprovalClientLabels,t as describeNativeExecApprovalClientSetup}from"./exec-approval-surface-D6pya3Sh.mjs";import{t as formatHumanList}from"./human-list-C2QGgk5P.mjs";import{t as formatApprovalDisplayPath}from"./approval-display-paths-D_A02cT9.mjs";
//!src/infra/exec-approval-reply.ts
function buildGenericNativeExecApprovalFallbackText(e){const t=formatHumanList(listNativeExecApprovalClientLabels(e));let n="Print the web app URL with `openclaw dashboard --no-open`, open it in a browser, then use the approval inbox.";if(e?.host===`node`){const t=normalizeOptionalString(e.nodeId)??`<id|name|ip>`;n+=` Inspect the node's effective exec policy with \`openclaw approvals get --node ${t}\`.`}return t?`Approve it from the Web UI, or enable a native chat approval client such as ${t}. ${n} If those accounts already know your owner ID via allowFrom or owner config, OpenClaw can often infer approvers automatically.`:`Approve it from the Web UI. ${n}`}function resolveAllowedDecisions(e){return e.allowedDecisions??resolveExecApprovalAllowedDecisions({ask:e.ask})}function buildApprovalActionDescriptors(e,t){return[{decision:`allow-once`,label:`Allow Once`,style:`success`},{decision:`allow-always`,label:`Allow Always`,style:`primary`},{decision:`deny`,label:`Deny`,style:`danger`}].filter(e=>t.includes(e.decision)).map(t=>Object.assign(t,{command:`/approve ${e} ${t.decision}`}))}function buildExecApprovalActionDescriptors(e){const t=e.approvalCommandId.trim();return t?buildApprovalActionDescriptors(t,resolveAllowedDecisions(e)):[]}function buildTypedApprovalActionDescriptors(e){const t=e.approvalCommandId;if(!isWellFormedApprovalId(t))return[];return buildApprovalActionDescriptors(t,resolveAllowedDecisions(e)).map(n=>Object.assign(n,{action:{type:`approval`,approvalId:t,approvalKind:e.approvalKind,decision:n.decision}}))}function buildApprovalPresentationFromActionDescriptors(e){const t=e.map(e=>{const t=e.action??{type:`command`,command:e.command};return{label:e.label,action:t,...e.action?{}:{value:e.command},style:e.style}});return t.length>0?{blocks:[{type:`buttons`,buttons:t}]}:void 0}function buildApprovalButtonPresentation(e){return buildApprovalPresentationFromActionDescriptors(buildExecApprovalActionDescriptors({approvalCommandId:e.approvalId,ask:e.ask,allowedDecisions:e.allowedDecisions}))}function buildTypedApprovalPresentation(e){return buildApprovalPresentationFromActionDescriptors(buildTypedApprovalActionDescriptors({approvalCommandId:e.approvalId,approvalKind:e.approvalKind,ask:e.ask,allowedDecisions:e.allowedDecisions}))}function buildExecApprovalPresentation(e){return buildApprovalPresentationFromActionDescriptors(buildExecApprovalActionDescriptors(e))}function getExecApprovalApproverDmNoticeText(){return`Approval required. I sent approval DMs to the approvers for this account.`}function parseExecApprovalCommandText(e){const t=e.trim().match(/^\/?approve(?:@[^\s]+)?\s+([A-Za-z0-9][A-Za-z0-9._:-]*)\s+(allow-once|allow-always|always|deny)\b/i);if(!t)return null;const n=normalizeOptionalLowercaseString(t[2])??``;return{approvalId:expectDefined(t[1],`exec approval reply regex capture 1`),decision:n===`always`?`allow-always`:n}}function formatExecApprovalExpiresIn(e,t){const n=Math.max(0,Math.round((e-t)/1e3));if(n<60)return`${n}s`;const r=Math.floor(n/3600);const i=Math.floor(n%3600/60);const a=n%60;const o=[];if(r>0)o.push(`${r}h`);if(i>0)o.push(`${i}m`);if(r===0&&i<5&&a>0)o.push(`${a}s`);return o.join(` `)}function getExecApprovalReplyMetadata(e){const t=asOptionalRecord(asOptionalRecord(e.channelData)?.execApproval);if(!t)return null;const n=normalizeOptionalString(t.approvalId)??``;const r=normalizeOptionalString(t.approvalSlug)??``;if(!n||!r)return null;const i=t.approvalKind===`plugin`||t.approvalKind===`system-agent`?t.approvalKind:`exec`;const a=Array.isArray(t.allowedDecisions)?t.allowedDecisions.filter(e=>e===`allow-once`||e===`allow-always`||e===`deny`):void 0;return{approvalId:n,approvalSlug:r,approvalKind:i,agentId:normalizeOptionalString(t.agentId),allowedDecisions:a,sessionKey:normalizeOptionalString(t.sessionKey)}}function buildExecApprovalPendingReplyPayload(e){const t=e.approvalCommandId?.trim()||e.approvalSlug;const n=resolveAllowedDecisions(e);const r=buildExecApprovalActionDescriptors({approvalCommandId:t,allowedDecisions:n});const i=r[0]??null;const a=r.slice(1);const o=[];const s=e.warningText?.trim();if(s)o.push(s);o.push(`Approval required.`);if(i){o.push(`Run:`);o.push(formatFencedCodeBlock(i.command,`txt`))}o.push(`Pending command:`);o.push(formatFencedCodeBlock(e.command,`sh`));if(a.length>0)o.push(`Other options:`,formatFencedCodeBlock(a.map(e=>e.command).join(`
`),`txt`));if(!n.includes(`allow-always`))o.push(`Allow Always is unavailable for this command.`);const c=[];c.push(`Host: ${e.host}`);if(e.nodeId)c.push(`Node: ${e.nodeId}`);if(e.cwd)c.push(`CWD: ${formatApprovalDisplayPath(e.cwd)}`);if(e.scope)c.push(`Scope: ${summarizeApprovalScope(e.scope)}`);if(typeof e.expiresAtMs===`number`&&Number.isFinite(e.expiresAtMs))c.push(`Expires in: ${formatExecApprovalExpiresIn(e.expiresAtMs,e.nowMs??Date.now())}`);c.push(`Full id: \`${e.approvalId}\``);o.push(c.join(`
`));return{text:o.join(`
`),presentation:buildApprovalButtonPresentation({approvalId:e.approvalId,allowedDecisions:n}),channelData:{execApproval:{approvalId:e.approvalId,approvalSlug:e.approvalSlug,approvalKind:`exec`,agentId:normalizeOptionalString(e.agentId),allowedDecisions:n,sessionKey:normalizeOptionalString(e.sessionKey)}}}}function buildTypedExecApprovalPendingReplyPayload(e){return{...buildExecApprovalPendingReplyPayload(e),presentation:buildTypedApprovalPresentation({approvalId:e.approvalId,approvalKind:`exec`,allowedDecisions:resolveAllowedDecisions(e)})}}function buildExecApprovalUnavailableReplyPayload(e){const t=[];const n={execApprovalUnavailable:{reason:e.reason}};const r=e.warningText?.trim();if(r)t.push(r);if(e.sentApproverDms){t.push(getExecApprovalApproverDmNoticeText());return{text:t.join(`
`),channelData:n}}const fallbackText=t=>buildGenericNativeExecApprovalFallbackText({...t,host:e.host,nodeId:e.nodeId});if(e.reason===`initiating-platform-disabled`){t.push(`Exec approval is required, but native chat exec approvals are not configured on ${e.channelLabel??`this platform`}.`);const n=normalizeOptionalLowercaseString(e.channel);const r=n&&e.channelLabel&&supportsNativeExecApprovalClient(n)?describeNativeExecApprovalClientSetup({channel:n,channelLabel:e.channelLabel,accountId:e.accountId}):null;if(r)t.push(r);else t.push(fallbackText())}else if(e.reason===`initiating-platform-unsupported`){t.push(`Exec approval is required, but ${e.channelLabel??`this platform`} does not support chat exec approvals.`);t.push(fallbackText({excludeChannel:e.channel}))}else{t.push(`Exec approval is required, but no interactive approval client is currently available.`);t.push(`${fallbackText()} Then retry the command. You can usually leave execApprovals.approvers unset when owner config already identifies the approvers.`)}return{text:t.join(`
`),channelData:n}}export{buildExecApprovalPresentation as a,buildTypedApprovalPresentation as c,getExecApprovalApproverDmNoticeText as d,getExecApprovalReplyMetadata as f,buildExecApprovalPendingReplyPayload as i,buildTypedExecApprovalPendingReplyPayload as l,buildApprovalPresentationFromActionDescriptors as n,buildExecApprovalUnavailableReplyPayload as o,parseExecApprovalCommandText as p,buildExecApprovalActionDescriptors as r,buildTypedApprovalActionDescriptors as s,buildApprovalButtonPresentation as t,formatExecApprovalExpiresIn as u};