55 * auth profiles, and the current runtime config snapshot.
66 */
77import type { OpenClawConfig } from "../config/types.openclaw.js" ;
8- import { getActivePluginRegistry } from "../plugins/runtime.js" ;
9- import { getPluginRuntimeGatewayRequestScope } from "../plugins/runtime/gateway-request-scope.js" ;
8+ import {
9+ resolveMessageActionTurnCapability ,
10+ selectMessageActionRequesterIdentity ,
11+ } from "../gateway/message-action-turn-capability.js" ;
12+ import { resolveAgentScopedOutboundMediaAccess } from "../media/read-capability.js" ;
13+ import { getActivePluginRegistry , getActivePluginRegistryVersion } from "../plugins/runtime.js" ;
14+ import {
15+ getPluginRuntimeGatewayRequestScope ,
16+ withPluginRuntimeRegistryScope ,
17+ } from "../plugins/runtime/gateway-request-scope.js" ;
18+ import type { OpenClawPluginToolDelivery } from "../plugins/tool-types.js" ;
1019import { resolvePluginTools } from "../plugins/tools.js" ;
20+ import type { OpenClawPluginToolContext } from "../plugins/types.js" ;
21+ import { createLazyRuntimeModule } from "../shared/lazy-runtime.js" ;
1122import { resolveApiKeyForProfile , resolveAuthProfileOrder } from "./auth-profiles.js" ;
1223import type { AuthProfileStore } from "./auth-profiles/types.js" ;
1324import {
@@ -46,6 +57,137 @@ type ResolveOpenClawPluginToolsOptions = OpenClawPluginToolOptions & {
4657 authProfileStore ?: AuthProfileStore ;
4758} ;
4859
60+ const loadMessageActionRunner = createLazyRuntimeModule (
61+ ( ) => import ( "../infra/outbound/message-action-runner.js" ) ,
62+ ) ;
63+
64+ function createPluginToolDelivery ( params : {
65+ options : ResolveOpenClawPluginToolsOptions | undefined ;
66+ context : OpenClawPluginToolContext ;
67+ bindingConfig : OpenClawConfig | undefined ;
68+ resolveConfig : ( ) => OpenClawConfig | undefined ;
69+ } ) : OpenClawPluginToolDelivery | undefined {
70+ const deliveryContext = params . context . deliveryContext ;
71+ const agentId = params . context . agentId ;
72+ const sessionKey = params . context . sessionKey ;
73+ const sessionId = params . context . sessionId ;
74+ const senderIsOwner = params . context . senderIsOwner ;
75+ const conversationReadOrigin = params . context . conversationReadOrigin ;
76+ const runId = params . options ?. runId ;
77+ const token = params . options ?. messageActionTurnCapability ;
78+ const activeRegistry = getActivePluginRegistry ( ) ;
79+ const activeRegistryVersion = getActivePluginRegistryVersion ( ) ;
80+ if (
81+ ! deliveryContext ?. channel ||
82+ ! deliveryContext . to ||
83+ ! agentId ||
84+ ! sessionKey ||
85+ ! runId ||
86+ ! token ||
87+ ! activeRegistry
88+ ) {
89+ return undefined ;
90+ }
91+ const channelPlugin = activeRegistry . channels . find (
92+ ( entry ) => entry . plugin . id === deliveryContext . channel ,
93+ ) ?. plugin ;
94+ // The turn capability and sender-scoped media reader are process-local.
95+ // Gateway-owned delivery needs a server-verifiable authority contract first.
96+ if ( channelPlugin ?. outbound ?. deliveryMode === "gateway" ) {
97+ return undefined ;
98+ }
99+ const route = {
100+ channel : deliveryContext . channel ,
101+ to : deliveryContext . to ,
102+ accountId : deliveryContext . accountId ,
103+ threadId : deliveryContext . threadId ,
104+ } ;
105+
106+ const resolveAuthorization = ( ) => {
107+ if (
108+ getActivePluginRegistry ( ) !== activeRegistry ||
109+ getActivePluginRegistryVersion ( ) !== activeRegistryVersion
110+ ) {
111+ throw new Error ( "plugin delivery capability is no longer active" ) ;
112+ }
113+ const authorization = resolveMessageActionTurnCapability ( {
114+ token,
115+ agentId,
116+ runId,
117+ sessionKey,
118+ sessionId,
119+ } ) ;
120+ if ( ! authorization ) {
121+ throw new Error ( "plugin delivery capability is no longer active" ) ;
122+ }
123+ return authorization ;
124+ } ;
125+ const bindingAuthorization = resolveAuthorization ( ) ;
126+ const bindingConfig = params . bindingConfig ;
127+ if ( ! bindingConfig ) {
128+ return undefined ;
129+ }
130+ const mediaAccess = resolveAgentScopedOutboundMediaAccess ( {
131+ cfg : bindingConfig ,
132+ agentId,
133+ workspaceDir : params . context . workspaceDir ,
134+ sessionKey,
135+ accountId : bindingAuthorization . requesterAccountId ?? route . accountId ,
136+ requesterSenderId : bindingAuthorization . requesterSenderId ,
137+ requesterSenderName : bindingAuthorization . requesterSenderName ,
138+ requesterSenderUsername : bindingAuthorization . requesterSenderUsername ,
139+ requesterSenderE164 : bindingAuthorization . requesterSenderE164 ,
140+ } ) ;
141+
142+ return {
143+ send : async ( { text, mediaUrl } ) => {
144+ resolveAuthorization ( ) ;
145+ const { runMessageAction } = await loadMessageActionRunner ( ) ;
146+ const authorization = resolveAuthorization ( ) ;
147+ const cfg = params . resolveConfig ( ) ;
148+ if ( ! cfg ) {
149+ throw new Error ( "plugin delivery requires an active runtime config" ) ;
150+ }
151+ await withPluginRuntimeRegistryScope ( activeRegistry , ( ) =>
152+ runMessageAction ( {
153+ cfg,
154+ action : "send" ,
155+ params : {
156+ channel : route . channel ,
157+ target : route . to ,
158+ ...( route . accountId ? { accountId : route . accountId } : { } ) ,
159+ ...( route . threadId != null ? { threadId : route . threadId } : { } ) ,
160+ ...( text !== undefined ? { message : text } : { } ) ,
161+ ...( mediaUrl !== undefined ? { mediaUrl } : { } ) ,
162+ } ,
163+ defaultAccountId : route . accountId ,
164+ ...selectMessageActionRequesterIdentity ( authorization ) ,
165+ messageActionAuthorization : {
166+ requesterAccountId : authorization . requesterAccountId ,
167+ requesterSenderId : authorization . requesterSenderId ,
168+ toolContext : authorization . toolContext ,
169+ } ,
170+ senderIsOwner,
171+ conversationReadOrigin,
172+ toolContext : authorization . toolContext ,
173+ sessionKey,
174+ sessionId,
175+ runId,
176+ agentId,
177+ mediaAccess,
178+ onPlatformSendDispatch : async ( ) => {
179+ resolveAuthorization ( ) ;
180+ } ,
181+ forceCoreDelivery : true ,
182+ skipQueue : true ,
183+ dryRun : false ,
184+ } ) ,
185+ ) ;
186+ resolveAuthorization ( ) ;
187+ } ,
188+ } ;
189+ }
190+
49191/** Resolves plugin tools for an agent run and applies delivery-context defaults. */
50192export function resolveOpenClawPluginToolsForOptions ( params : {
51193 options ?: ResolveOpenClawPluginToolsOptions ;
@@ -69,6 +211,12 @@ export function resolveOpenClawPluginToolsForOptions(params: {
69211 } ) ;
70212 const authProfileStore = params . options ?. authProfileStore ;
71213 const availabilityConfig = resolveCurrentRuntimeConfig ( ) ;
214+ const delivery = createPluginToolDelivery ( {
215+ options : params . options ,
216+ context : pluginToolInputs . context ,
217+ bindingConfig : availabilityConfig ,
218+ resolveConfig : resolveCurrentRuntimeConfig ,
219+ } ) ;
72220 const availabilityRuntimeLookup = authProfileStore
73221 ? createRuntimeProviderAuthLookup ( {
74222 cfg : availabilityConfig ,
@@ -146,6 +294,7 @@ export function resolveOpenClawPluginToolsForOptions(params: {
146294 ...pluginToolInputs ,
147295 context : {
148296 ...pluginToolInputs . context ,
297+ ...( delivery ? { delivery } : { } ) ,
149298 ...( hasAuthForProvider ? { hasAuthForProvider } : { } ) ,
150299 ...( resolveApiKeyForProvider ? { resolveApiKeyForProvider } : { } ) ,
151300 } ,
0 commit comments