openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.24 kB
JavaScript
import{n as normalizeAccountId}from"./account-id-3yqrEsRn.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import{t as isDeliverableMessageChannel}from"./message-channel-normalize-hDAO6_JQ.mjs";import"./message-channel-514k57tV.mjs";import{r as resolveChannelAccountEntry}from"./account-lookup-DXbrh7Os.mjs";import{i as validateTargetProviderPrefix,t as resolveTargetPrefixedChannel}from"./channel-target-prefix-CDxsZqXF.mjs";import{n as assertCanonicalCronDeliveryMode}from"./delivery-codec-rJfKfjDT.mjs";import{n as listConfiguredMessageChannels}from"./channel-selection-DA2EgjK_.mjs";import{r as resolveFailureAlert}from"./failure-alerts-CBth-ws-.mjs";
//!src/cron/delivery-channel-validation.ts
function hasExplicitChannelConfigEntry(e){const t=e.channels;if(!t||typeof t!==`object`||Array.isArray(t))return false;return Object.entries(t).some(([e,t])=>{if(e===`defaults`||e===`modelByChannel`)return false;return Boolean(t&&typeof t===`object`&&!Array.isArray(t)&&Object.keys(t).length>0)})}async function assertConfiguredAnnounceChannel(e){if(e.channel===`last`)return;const t=normalizeMessageChannel(e.channel);if(!t&&e.field===`delivery.channel`)return;const n=(await listConfiguredMessageChannels(e.cfg)).toSorted();if(!t){if(n.length<=1)return;throw new Error(`${e.field} is required when multiple channels are configured: ${n.join(`, `)}`)}if(n.length===0){if(!hasExplicitChannelConfigEntry(e.cfg)){if(!isDeliverableMessageChannel(t))throw new Error(`${e.field} is not a known channel: ${t}`);return}throw new Error(`${e.field} is not configured: ${t}`)}if(!n.includes(t))throw new Error(`${e.field} must be one of: ${n.join(`, `)}`)}function assertEnabledAnnounceAccount(e){if(!e.accountId||!e.channel||e.channel===`last`)return;const t=normalizeMessageChannel(e.channel)??e.channel;const n=e.cfg.channels?.[t];if(!n||typeof n!==`object`||Array.isArray(n))return;const r=n.accounts;if(resolveChannelAccountEntry(r,e.accountId,t,normalizeAccountId)?.enabled!==false)return;throw new Error(`${e.field}: account "${e.accountId}" is disabled for channel ${t}`)}function resolveAnnounceValidationChannel(e){return e.channel&&e.channel!==`last`?e.channel:resolveTargetPrefixedChannel(e.to)??e.channel}function assertCompatibleAnnounceTarget(e){if(!e.channel||e.channel===`last`)return;const t=validateTargetProviderPrefix({channel:e.channel,to:e.to});if(t)throw new Error(`${e.field}: ${t.message}`)}async function assertValidCronAnnounceDelivery(e){assertCanonicalCronDeliveryMode(e.delivery);if(e.delivery?.mode===`announce`){assertCompatibleAnnounceTarget({channel:e.delivery.channel,to:e.delivery.to,field:`delivery.channel`});await assertConfiguredAnnounceChannel({cfg:e.cfg,channel:resolveAnnounceValidationChannel(e.delivery),field:`delivery.channel`});assertEnabledAnnounceAccount({cfg:e.cfg,channel:resolveAnnounceValidationChannel(e.delivery),accountId:e.delivery.accountId,field:`delivery.accountId`})}const t=e.delivery?.failureDestination;if(t&&(t.mode??`announce`)===`announce`){if(t.channel===void 0&&t.to===void 0&&t.accountId===void 0&&t.mode===void 0)return;assertCompatibleAnnounceTarget({channel:t.channel,to:t.to,field:`delivery.failureDestination.channel`});await assertConfiguredAnnounceChannel({cfg:e.cfg,channel:resolveAnnounceValidationChannel(t),field:`delivery.failureDestination.channel`})}}async function assertValidCronFailureAlert(e){const t=resolveFailureAlert({deps:{cronConfig:e.cfg.cron,defaultAgentId:e.defaultAgentId}},{agentId:e.owner?.agentId,sessionKey:e.owner?.sessionKey,sessionTarget:e.owner?.sessionTarget,createdActor:e.owner?.createdActor,delivery:e.delivery,failureAlert:e.failureAlert});if(!t||t.mode===`webhook`)return;assertCompatibleAnnounceTarget({channel:t.channel,to:t.to,field:`failureAlert.channel`});await assertConfiguredAnnounceChannel({cfg:e.cfg,channel:t.channel,field:`failureAlert.channel`})}async function assertValidCronCreateDelivery(e,t,n){await assertValidCronAnnounceDelivery({cfg:e,delivery:t.delivery});await assertValidCronFailureAlert({cfg:e,failureAlert:t.failureAlert,delivery:t.delivery,owner:t,defaultAgentId:n})}export{assertValidCronCreateDelivery as n,assertValidCronFailureAlert as r,assertValidCronAnnounceDelivery as t};