UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

9 lines • 6.37 kB
import{l as resolveAgentIdFromSessionKey}from"./session-key-BPgKfU2m.mjs";import{l as resolveSessionStorePathCore}from"./paths-CcxgiC3g.mjs";import{_ as resolveDefaultAgentId}from"./agent-scope-config-CG9CFpGX.mjs";import{n as parseConfigPath,r as setConfigValueAtPath,t as getConfigValueAtPath}from"./config-paths-BNzEq3Gi.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{t as REDACTED_SENTINEL}from"./redact-sentinel-BwYTpV-q.mjs";import{Z as captureSessionTarget}from"./session-controller.state-BIDbpLPn.mjs";import{t as appendAssistantMessageToSessionTranscript}from"./transcript-D-i65_T9.mjs";import{r as logVerbose}from"./globals-l9I53ZKA.mjs";import{c as resolveActiveSessionRunId,i as isSessionRunActiveForKey}from"./session-controller.queries-CBzzV0sx.mjs";import{o as parseSlashCommandOrNull}from"./commands-registry.data-IaSrmBM9.mjs";import{n as normalizeCommandBody}from"./commands-registry-normalize-yxTxMnHJ.mjs";import{t as parseConfigValue}from"./config-value-DxtqKUFI.mjs";import{r as waitForSessionRunIdle}from"./session-controller.wait-BGB6lYw_.mjs"; //!src/auto-reply/reply/commands-setunset.ts function parseSlashCommandWithSetUnset(e){const t=parseSlashCommandOrNull(e.raw,e.slash);if(!t)return null;const error=e=>({action:`error`,message:e});const{action:n,args:r}=t;if(n===`unset`)return r?{action:`unset`,path:r}:error(`Usage: ${e.slash} unset path`);if(n===`set`){const t=r.indexOf(`=`);const n=t>0?r.slice(0,t).trim():``;if(!n)return error(`Usage: ${e.slash} set path=value`);const i=parseConfigValue(r.slice(t+1));return i.error?error(i.error):{action:`set`,path:n,value:i.value}}return e.onKnownAction(n,r)||error(e.usageMessage)} //!src/auto-reply/reply/config-commands.ts function parseConfigCommand(e){return parseSlashCommandWithSetUnset({raw:e,slash:`/config`,usageMessage:`Usage: /config show|set|unset`,onKnownAction:(e,t)=>{if(e===`show`||e===`get`)return{action:`show`,path:t||void 0}}})} //!src/auto-reply/reply/debug-commands.ts function parseDebugCommand(e){return parseSlashCommandWithSetUnset({raw:e,slash:`/debug`,usageMessage:`Usage: /debug show|set|unset|reset`,onKnownAction:e=>e===`show`||e===`reset`?{action:e}:void 0})} //!src/config/sessions/command-transcript.ts function scopeCommandTranscriptId(e,t){return e?JSON.stringify([t.channelId??``,t.accountId??``,t.conversationId??``,e]):void 0}async function redactConfigCommandInput(e){const t=parseSlashCommandOrNull(e,`/config`)?`/config`:`/debug`;if(parseSlashCommandOrNull(e,t)?.action!==`set`)return e;const n=t===`/config`?parseConfigCommand(e):parseDebugCommand(e);if(n?.action!==`set`)return`${t} set ${REDACTED_SENTINEL}`;const r=parseConfigPath(n.path);if(!r.ok)return`${t} set ${REDACTED_SENTINEL}`;const[{redactConfigObject:i},{loadGatewayConfigRedactionHints:a}]=await Promise.all([import(`./redact-snapshot-1tjvwMbf.mjs`),import(`./runtime-schema-CYWMVqyi.mjs`)]);const o={};setConfigValueAtPath(o,r.path,n.value);const s=i(o,a());return`${t} set ${n.path}=${JSON.stringify(getConfigValueAtPath(s,r.path))}`}function redactCommandTranscriptText(e,t){const n=t.replace(/\b((?:device|pairing|verification|login|setup)\s+code|code)\s*:\s*(?:<code>)?[^\s<]+(?:<\/code>)?/gi,`$1: [login code redacted]`);return(/^\/pair(?:@\S+)?(?:\s|$)/i.test(e)?n.replace(/\beyJ[A-Za-z0-9_-]{32,}={0,2}\b/g,`[pairing code redacted]`):n).replace(/https?:\/\/[^\s<>]+/gi,t=>/^\/login(?:@\S+)?(?:\s|$)/i.test(e)||/[?&](?:[\w-]*token|state|code|user_code|device_code|secret|key)=/i.test(t)?`[login URL redacted]`:t)}function normalizeCommandTranscriptText(e){return normalizeCommandBody(e,{targetedCommandMode:`pre-identity`,preserveArguments:true}).trim()}function isEphemeralCommandTranscript(e){return/^\/(?:btw|side)(?:@\S+)?(?:\s|$)/i.test(e)}async function prepareCommandTranscriptInput(e){const t=normalizeCommandTranscriptText(e);if(!t||isEphemeralCommandTranscript(t))return;return redactCommandTranscriptText(t,await redactConfigCommandInput(t))}function recordCommittedCommandExchange(e){return recordCommandExchange(e)}const MAX_DEFERRED_COMMAND_EXCHANGE_WAITS=8;function resolveExchangeTarget(e){const t=e.sessionKey.trim();try{const n=e.agentId?.trim()||resolveAgentIdFromSessionKey(t,e.config?resolveDefaultAgentId(e.config):void 0);const r=e.storePath??resolveSessionStorePathCore(e.config?.session?.store,{agentId:n});return captureSessionTarget({storeScope:r,sessionKey:t,agentId:n})}catch{return}}function shouldDeferUntilActiveTurnSettles(e,t){if(e.contextFreeCommand===true||e.expectedWriterRunId)return false;const n=e.sessionKey.trim();try{if(t)return resolveActiveSessionRunId(n,t,{executionStartedOnly:true})!==void 0}catch{}return isSessionRunActiveForKey(n)}async function recordDeliveredCommandExchangeNow(e){try{return await recordCommandExchange(e)}catch{logVerbose(`Command transcript recording failed after delivery`);return{ok:false,reason:`transcript recording failed`}}}async function recordAfterActiveTurnSettles(e,t){for(let n=0;n<MAX_DEFERRED_COMMAND_EXCHANGE_WAITS;n+=1){await waitForSessionRunIdle(e.sessionKey,void 0,t?{target:t}:void 0);if(!shouldDeferUntilActiveTurnSettles(e,t))return await recordDeliveredCommandExchangeNow(e)}logVerbose(`Command transcript recording skipped: session stayed busy after delivery`);return{ok:false,reason:`session stayed busy`}}async function recordDeliveredCommandExchange(e){const t=resolveExchangeTarget(e);if(shouldDeferUntilActiveTurnSettles(e,t)){recordAfterActiveTurnSettles(e,t).catch(()=>{logVerbose(`Command transcript recording failed after delivery`)});return{ok:false,reason:`deferred until the active turn settles`}}return await recordDeliveredCommandExchangeNow(e)}async function recordCommandExchange(e){const t=normalizeCommandTranscriptText(e.commandText);if(isEphemeralCommandTranscript(t))return{ok:false,reason:`ephemeral command`};return appendAssistantMessageToSessionTranscript({...e,text:redactCommandTranscriptText(t,e.replyText),...e.commandInputCommitted?{}:{command:{text:redactCommandTranscriptText(t,await redactConfigCommandInput(t)),idempotencyKey:`command-input:${e.commandId}`}},idempotencyKey:`command-reply:${e.commandId}:${e.replyId}`})}export{parseDebugCommand as a,scopeCommandTranscriptId as i,recordCommittedCommandExchange as n,parseConfigCommand as o,recordDeliveredCommandExchange as r,parseSlashCommandWithSetUnset as s,prepareCommandTranscriptInput as t};