Skip to content

Commit 43c54af

Browse files
authored
fix(plugins): bind tool delivery to current turn (#127098)
Keep plugin-originated text and media bound to the active host-selected route and revoke authority at turn closure. Fence Gateway-owned channels until a server-verifiable authority and media-policy contract exists. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 8dd21b0 commit 43c54af

29 files changed

Lines changed: 974 additions & 78 deletions

‎docs/plugins/building-plugins.md‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,17 @@ loads the owning plugin runtime.
244244

245245
Tool factories receive trusted runtime context, including `deliveryContext`,
246246
`nativeChannelId` for the active platform conversation when available, and
247-
`requesterSenderId`.
247+
`requesterSenderId`. A factory can use
248+
`toolContext.delivery?.send({ text, mediaUrl })` to send text or media to the
249+
current conversation. The property is unavailable outside an active channel
250+
turn or when the channel uses Gateway-owned delivery. OpenClaw binds the route,
251+
account, thread, and media access policy; the capability expires when the turn
252+
ends.
248253

249254
```typescript
250255
register(api) {
251256
api.registerTool(
252-
{
257+
(toolContext) => ({
253258
name: "workflow_tool",
254259
description: "Run a workflow",
255260
parameters: Type.Object({ pipeline: Type.String() }),
@@ -258,13 +263,16 @@ register(api) {
258263
{ additionalProperties: false },
259264
),
260265
async execute(_id, params) {
266+
await toolContext.delivery?.send({
267+
text: `Workflow started: ${params.pipeline}`,
268+
});
261269
return {
262270
content: [{ type: "text", text: params.pipeline }],
263271
details: { pipeline: params.pipeline },
264272
};
265273
},
266-
},
267-
{ optional: true },
274+
}),
275+
{ name: "workflow_tool", optional: true },
268276
);
269277
}
270278
```

‎docs/plugins/tool-plugins.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ tool({
155155
});
156156
```
157157

158+
Factories can use `toolContext.delivery?.send({ text, mediaUrl })` for outbound
159+
messages in the active conversation. The host chooses the destination,
160+
account, thread, and local-media policy; plugins cannot retarget this helper,
161+
and retained copies stop working after the turn closes. The helper is unavailable
162+
for channels whose delivery is owned by a Gateway transport.
163+
158164
Factories still declare a fixed tool name up front. Use `definePluginEntry`
159165
directly when the plugin computes tool names dynamically or combines tools
160166
with hooks, services, providers, or commands.

‎src/agents/agent-tools.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,15 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
700700
: resolveOpenClawPluginToolsForOptions({
701701
options: {
702702
agentSessionKey: options?.sessionKey,
703+
runId: options?.runId,
703704
agentChannel: resolveGatewayMessageChannel(
704705
options?.messageChannel ?? options?.messageProvider,
705706
),
706707
agentAccountId: options?.agentAccountId,
707708
agentTo: options?.messageTo,
708709
agentThreadId: options?.messageThreadId,
709710
nativeChannelId: options?.nativeChannelId,
711+
messageActionTurnCapability: options?.messageActionTurnCapability,
710712
agentDir: options?.agentDir,
711713
preparedModelRuntime: options?.preparedModelRuntime,
712714
workspaceDir: workspaceRoot,

‎src/agents/openclaw-plugin-tools.ts‎

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
* auth profiles, and the current runtime config snapshot.
66
*/
77
import 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";
1019
import { resolvePluginTools } from "../plugins/tools.js";
20+
import type { OpenClawPluginToolContext } from "../plugins/types.js";
21+
import { createLazyRuntimeModule } from "../shared/lazy-runtime.js";
1122
import { resolveApiKeyForProfile, resolveAuthProfileOrder } from "./auth-profiles.js";
1223
import type { AuthProfileStore } from "./auth-profiles/types.js";
1324
import {
@@ -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. */
50192
export 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

Comments
 (0)