openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.67 kB
JavaScript
import{t as redactIdentifier}from"./node-crypto-DPk8agfR.mjs";import{a as isInternalMessageChannel}from"./message-channel-514k57tV.mjs";import{r as logVerbose}from"./globals-l9I53ZKA.mjs";import{n as isRestartEnabled,t as isCommandFlagEnabled}from"./commands.flags-BdxOktqL.mjs";import{l as resolveCommandTurnContext,o as isNativeCommandTurn}from"./command-turn-context-vtXH8Cqw.mjs";import{r as formatCommandOwnerHint}from"./doctor-command-owner-D7NATa7X.mjs";
//!src/auto-reply/reply/command-gates.ts
function commandReply(e){return{shouldContinue:false,reply:typeof e===`string`?{text:e}:e}}function renderCommandJsonBlock(e,t){return`${e}\n\`\`\`json\n${JSON.stringify(t,null,2)}\n\`\`\``}function matchCommandPrefix(e,t,n={}){return e===t?``:e.startsWith(`${t} `)||n.allowColon&&e.startsWith(`${t}:`)?e.slice(t.length+1).trim():null}function defineAuthorizedTextCommand(e,t){return async(n,r)=>{if(!r)return null;const i=e.match(n.command.commandBodyNormalized,n);if(i===null)return null;const a=rejectUnauthorizedCommand(n,e.label);if(a)return a;return(typeof e.ownerOnly===`function`?e.ownerOnly(n,i):e.ownerOnly)?rejectNonOwnerCommand(n,e.label)??t(n,i):t(n,i)}}function defineGatewayControlCommand(e,t){return defineAuthorizedTextCommand({label:e,match:t=>t===e?true:null,ownerOnly:true},async n=>{if(!isRestartEnabled(n.cfg))return commandReply(`⚠️ ${e} is disabled (commands.restart=false).`);await n.opts?.turnAdoptionLifecycle?.onAdopted();return rejectNonOwnerCommand(n,e)??t(n)})}function rejectUnauthorizedCommand(e,t){if(e.command.isAuthorizedSender)try{e.command.assertAuthorizedCurrent?.();return null}catch{}logVerbose(`Ignoring ${t} from unauthorized sender: ${redactIdentifier(e.command.senderId)}`);if(isNativeCommandTurn(resolveCommandTurnContext(e.ctx)))return commandReply(`You are not authorized to use this command.`);if(isInternalMessageChannel(e.command.channel))return commandReply('You are not authorized to use this command: `commands.allowFrom` does not include this Gateway client. Add your Gateway user profile ID to `commands.allowFrom`, or move its `"*"` entries to per-channel keys so Control UI commands follow operator access.');return{shouldContinue:false}}function rejectNonOwnerCommand(e,t){if(e.command.senderIsOwner){try{e.command.assertOwnerCurrent?.()}catch{return commandReply(`Your owner authority changed; send a new request.`)}return null}logVerbose(`Ignoring ${t} from non-owner sender: ${redactIdentifier(e.command.senderId)}`);if(!e.command.isAuthorizedSender)return rejectUnauthorizedCommand(e,t);return commandReply(`You are not authorized to use this owner-only command. ${formatCommandOwnerHint({cfg:e.cfg,channel:e.command.channel,id:e.command.senderId})}`)}function requireGatewayClientScope(e,t){const n=e.ctx.GatewayClientScopes;if(!Array.isArray(n))return null;if(t.allowedScopes.some(e=>n.includes(e)))return null;logVerbose(`Ignoring ${t.label} from gateway client missing scope: ${t.allowedScopes.join(` or `)}`);return commandReply(t.missingText)}function buildDisabledCommandReply(e){const t=e.disabledVerb??`is`;const n=e.docsUrl?` Docs: ${e.docsUrl}`:``;return{text:`⚠️ ${e.label} ${t} disabled. Set commands.${e.configKey}=true to enable.${n}`}}function requireCommandFlagEnabled(e,t){if(isCommandFlagEnabled(e,t.configKey))return null;return{shouldContinue:false,reply:buildDisabledCommandReply(t)}}export{matchCommandPrefix as a,renderCommandJsonBlock as c,defineGatewayControlCommand as i,requireCommandFlagEnabled as l,commandReply as n,rejectNonOwnerCommand as o,defineAuthorizedTextCommand as r,rejectUnauthorizedCommand as s,buildDisabledCommandReply as t,requireGatewayClientScope as u};