1+ // Approval delivery helpers format approval prompts and results for channel plugins.
12import type { ExecApprovalRequest } from "../infra/exec-approvals.js" ;
23import type { PluginApprovalRequest } from "../infra/plugin-approvals.js" ;
34import type { ChannelApprovalCapability } from "./channel-contract.js" ;
@@ -15,49 +16,74 @@ type ChannelApprovalCapabilitySurfaces = Pick<
1516> ;
1617
1718type ApprovalAdapterParams = {
19+ /** Full config used to inspect channel approval settings. */
1820 cfg : OpenClawConfig ;
21+ /** Optional channel account id for account-scoped approval settings. */
1922 accountId ?: string | null ;
23+ /** Actor attempting the approval action. */
2024 senderId ?: string | null ;
2125} ;
2226
2327type DeliverySuppressionParams = {
28+ /** Full config used to inspect native approval delivery settings. */
2429 cfg : OpenClawConfig ;
30+ /** Approval kind being delivered. */
2531 approvalKind : ApprovalKind ;
32+ /** Forwarding fallback target under consideration. */
2633 target : { channel : string ; accountId ?: string | null } ;
34+ /** Approval request metadata, including original turn source when available. */
2735 request : { request : { turnSourceChannel ?: string | null ; turnSourceAccountId ?: string | null } } ;
2836} ;
2937
3038type ApproverRestrictedNativeApprovalParams = {
39+ /** Channel id that owns this native approval capability. */
3140 channel : string ;
41+ /** Human-readable channel label used in denial messages. */
3242 channelLabel : string ;
43+ /** Lists configured account ids so DM-route availability can scan every account. */
3344 listAccountIds : ( cfg : OpenClawConfig ) => string [ ] ;
45+ /** Whether an account has approvers configured. */
3446 hasApprovers : ( params : ApprovalAdapterParams ) => boolean ;
47+ /** Whether a sender can approve exec approvals for this account. */
3548 isExecAuthorizedSender : ( params : ApprovalAdapterParams ) => boolean ;
49+ /** Optional plugin approval authorization hook; defaults to exec authorization. */
3650 isPluginAuthorizedSender ?: ( params : ApprovalAdapterParams ) => boolean ;
51+ /** Whether native approval delivery is enabled for an account. */
3752 isNativeDeliveryEnabled : ( params : { cfg : OpenClawConfig ; accountId ?: string | null } ) => boolean ;
53+ /** Native delivery target preference for an account. */
3854 resolveNativeDeliveryMode : ( params : {
3955 cfg : OpenClawConfig ;
4056 accountId ?: string | null ;
4157 } ) => NativeApprovalDeliveryMode ;
58+ /** Requires the approval request's original turn channel to match this channel before suppression. */
4259 requireMatchingTurnSourceChannel ?: boolean ;
60+ /** Allows suppression when the original turn source is unavailable but the native route can still handle it. */
61+ allowMissingTurnSourceChannelForSuppression ?: boolean ;
62+ /** Optional account id resolver used when deciding forwarding-fallback suppression. */
4363 resolveSuppressionAccountId ?: ( params : DeliverySuppressionParams ) => string | undefined ;
64+ /** Resolves the original channel target for native approval delivery. */
4465 resolveOriginTarget ?: ( params : {
4566 cfg : OpenClawConfig ;
4667 accountId ?: string | null ;
4768 approvalKind : ApprovalKind ;
4869 request : NativeApprovalRequest ;
4970 } ) => NativeApprovalTarget | null | Promise < NativeApprovalTarget | null > ;
71+ /** Resolves approver DM targets for native approval delivery. */
5072 resolveApproverDmTargets ?: ( params : {
5173 cfg : OpenClawConfig ;
5274 accountId ?: string | null ;
5375 approvalKind : ApprovalKind ;
5476 request : NativeApprovalRequest ;
5577 } ) => NativeApprovalTarget [ ] | Promise < NativeApprovalTarget [ ] > ;
78+ /** Whether DM-only native delivery should also notify the origin channel. */
5679 notifyOriginWhenDmOnly ?: boolean ;
80+ /** Native runtime hooks used by channel-specific delivery implementations. */
5781 nativeRuntime ?: ChannelApprovalCapability [ "nativeRuntime" ] ;
82+ /** Optional setup description helper shown when exec approvals are unavailable. */
5883 describeExecApprovalSetup ?: ChannelApprovalCapability [ "describeExecApprovalSetup" ] ;
5984} ;
6085
86+ /** Build the canonical approval capability for channels that restrict approvals to configured approvers. */
6187function buildApproverRestrictedNativeApprovalCapability (
6288 params : ApproverRestrictedNativeApprovalParams ,
6389) : ChannelApprovalCapability {
@@ -148,6 +174,9 @@ function buildApproverRestrictedNativeApprovalCapability(
148174 const turnSourceChannel = normalizeMessageChannel (
149175 input . request . request . turnSourceChannel ,
150176 ) ;
177+ if ( ! turnSourceChannel && ! params . allowMissingTurnSourceChannelForSuppression ) {
178+ return false ;
179+ }
151180 if ( turnSourceChannel && turnSourceChannel !== params . channel ) {
152181 return false ;
153182 }
@@ -157,6 +186,8 @@ function buildApproverRestrictedNativeApprovalCapability(
157186 ( resolvedAccountId === undefined
158187 ? input . target . accountId ?. trim ( )
159188 : resolvedAccountId . trim ( ) ) || undefined ;
189+ // Suppress generic forwarding only when this channel's native route can
190+ // handle the same account; otherwise the fallback is the only delivery path.
160191 return params . isNativeDeliveryEnabled ( { cfg : input . cfg , accountId } ) ;
161192 } ,
162193 } ,
@@ -188,25 +219,38 @@ function buildApproverRestrictedNativeApprovalCapability(
188219 } ) ;
189220}
190221
222+ /** Build the legacy split approval adapter shape for approver-restricted native channels. */
191223export function createApproverRestrictedNativeApprovalAdapter (
192224 params : ApproverRestrictedNativeApprovalParams ,
193225) {
194226 return splitChannelApprovalCapability ( buildApproverRestrictedNativeApprovalCapability ( params ) ) ;
195227}
196228
229+ /** Assemble a channel approval capability from its auth, delivery, render, and native surfaces. */
197230export function createChannelApprovalCapability ( params : {
231+ /** Authorizes actors attempting approval actions. */
198232 authorizeActorAction ?: ChannelApprovalCapability [ "authorizeActorAction" ] ;
233+ /** Reports whether approval actions are generally available. */
199234 getActionAvailabilityState ?: ChannelApprovalCapability [ "getActionAvailabilityState" ] ;
235+ /** Reports whether exec approvals can start from the initiating surface. */
200236 getExecInitiatingSurfaceState ?: ChannelApprovalCapability [ "getExecInitiatingSurfaceState" ] ;
237+ /** Optional command behavior override for approval replies. */
201238 resolveApproveCommandBehavior ?: ChannelApprovalCapability [ "resolveApproveCommandBehavior" ] ;
239+ /** Optional setup copy for unavailable exec approval paths. */
202240 describeExecApprovalSetup ?: ChannelApprovalCapability [ "describeExecApprovalSetup" ] ;
241+ /** Delivery fallback and DM-route helpers. */
203242 delivery ?: ChannelApprovalCapability [ "delivery" ] ;
243+ /** Native runtime hooks for channel-specific approval delivery. */
204244 nativeRuntime ?: ChannelApprovalCapability [ "nativeRuntime" ] ;
245+ /** Render hooks for pending/resolved approval payloads. */
205246 render ?: ChannelApprovalCapability [ "render" ] ;
247+ /** Native target/capability discovery hooks. */
206248 native ?: ChannelApprovalCapability [ "native" ] ;
207249 /** @deprecated Pass delivery/nativeRuntime/render/native directly. */
208250 approvals ?: Partial < ChannelApprovalCapabilitySurfaces > ;
209251} ) : ChannelApprovalCapability {
252+ // Keep the approvals alias for shipped plugin-sdk callers; registry tests track
253+ // this compatibility marker until the public deprecation window closes.
210254 const surfaces : ChannelApprovalCapabilitySurfaces = {
211255 delivery : params . delivery ?? params . approvals ?. delivery ,
212256 nativeRuntime : params . nativeRuntime ?? params . approvals ?. nativeRuntime ,
@@ -226,6 +270,7 @@ export function createChannelApprovalCapability(params: {
226270 } ;
227271}
228272
273+ /** Split the canonical approval capability into the adapter shape older channel loaders consume. */
229274export function splitChannelApprovalCapability ( capability : ChannelApprovalCapability ) : {
230275 auth : {
231276 authorizeActorAction ?: ChannelApprovalCapability [ "authorizeActorAction" ] ;
@@ -254,6 +299,7 @@ export function splitChannelApprovalCapability(capability: ChannelApprovalCapabi
254299 } ;
255300}
256301
302+ /** Build the canonical approval capability for approver-restricted native delivery channels. */
257303export function createApproverRestrictedNativeApprovalCapability (
258304 params : ApproverRestrictedNativeApprovalParams ,
259305) : ChannelApprovalCapability {
0 commit comments