Skip to content

Commit 0b849a3

Browse files
authored
refactor(plugins): single-source interactive dispatch and its auth downgrade via plugin SDK (#120062)
1 parent d64f5b6 commit 0b849a3

10 files changed

Lines changed: 377 additions & 371 deletions

File tree

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ ea56ea0455c62f292e1c7b3e56cac6eb3fea00e548f9ab2f4a69e0b41b0a2d96 module/memory-
9999
8d8c0c4ebfc6e0c6125df3ae64ec66c10cd797a325de5ea7869fcd84068ab290 module/persistent-dedupe
100100
987648ebe317cc4d6c0505a52d344b12ce9c3a8261a5bd969cee3423c0c53529 module/plugin-config-runtime
101101
d7dd3c82a4b1df9e4144b078caf0e6ab95e7b11c5046b53a38f01143a7fa0eec module/plugin-entry
102-
8bc85c9f7df434c87cd7d35cc33080e2577a1893353f92bff8a0cc63d2c4eb0e module/plugin-runtime
102+
c0894007b842d68f1dcf0ba6e9a61486d06ef820e78e194939b70a5a4b28e9cb module/plugin-runtime
103103
de508df6d9cfa9d21c4524989dd8bd142fb60cb4ae980193e0907767d76e6022 module/provider-auth
104104
71bebeac51e701cd7c8e63d22754b9bcbd82b024aced303aa055d229781129d7 module/provider-catalog-runtime
105105
56151035047a69e6163d5578023d00f51a2413b777f3784af88e06261c039345 module/proxy-capture
Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Discord plugin module implements interactive dispatch behavior.
22
import type { ChannelStructuredComponents } from "openclaw/plugin-sdk/channel-contract";
33
import {
4-
createInteractiveConversationBindingHelpers,
5-
dispatchPluginInteractiveHandler,
4+
createChannelInteractiveDispatcher,
65
type PluginConversationBinding,
76
type PluginConversationBindingRequestParams,
87
type PluginConversationBindingRequestResult,
@@ -52,61 +51,24 @@ export type DiscordInteractiveHandlerRegistration = PluginInteractiveRegistratio
5251
"discord"
5352
>;
5453

55-
type DiscordInteractiveDispatchContext = Omit<
56-
DiscordInteractiveHandlerContext,
57-
| "interaction"
58-
| "respond"
59-
| "channel"
60-
| "requestConversationBinding"
61-
| "detachConversationBinding"
62-
| "getCurrentConversationBinding"
63-
> & {
64-
interaction: Omit<
65-
DiscordInteractiveHandlerContext["interaction"],
66-
"data" | "namespace" | "payload"
67-
>;
68-
};
54+
const dispatchDiscordInteractive = createChannelInteractiveDispatcher<
55+
"discord",
56+
"interaction",
57+
DiscordInteractiveHandlerContext
58+
>({
59+
channel: "discord",
60+
interactiveKey: "interaction",
61+
});
6962

7063
export async function dispatchDiscordPluginInteractiveHandler(params: {
7164
data: string;
7265
interactionId: string;
73-
ctx: DiscordInteractiveDispatchContext;
66+
ctx: Parameters<typeof dispatchDiscordInteractive>[0]["ctx"];
7467
respond: DiscordInteractiveHandlerContext["respond"];
7568
onMatched?: () => Promise<void> | void;
7669
}) {
77-
return await dispatchPluginInteractiveHandler<DiscordInteractiveHandlerRegistration>({
78-
channel: "discord",
79-
data: params.data,
70+
return await dispatchDiscordInteractive({
71+
...params,
8072
dedupeId: params.interactionId,
81-
onMatched: params.onMatched,
82-
invoke: ({ registration, namespace, payload }) =>
83-
registration.handler({
84-
...params.ctx,
85-
channel: "discord",
86-
interaction: {
87-
...params.ctx.interaction,
88-
data: params.data,
89-
namespace,
90-
payload,
91-
},
92-
respond: params.respond,
93-
...createInteractiveConversationBindingHelpers({
94-
// Untrusted callbacks may reach their plugin, but never inherit conversation-binding authority.
95-
registration:
96-
params.ctx.auth?.isAuthorizedSender &&
97-
params.ctx.senderId?.trim() &&
98-
params.ctx.accountId?.trim() &&
99-
params.ctx.conversationId?.trim()
100-
? registration
101-
: { ...registration, pluginRoot: undefined },
102-
senderId: params.ctx.senderId,
103-
conversation: {
104-
channel: "discord",
105-
accountId: params.ctx.accountId,
106-
conversationId: params.ctx.conversationId,
107-
parentConversationId: params.ctx.parentConversationId,
108-
},
109-
}),
110-
}),
11173
});
11274
}

‎extensions/slack/src/interactive-dispatch.ts‎

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Slack plugin module implements interactive dispatch behavior.
22
import {
3-
createInteractiveConversationBindingHelpers,
4-
dispatchPluginInteractiveHandler,
3+
createChannelInteractiveDispatcher,
54
type PluginConversationBinding,
65
type PluginConversationBindingRequestParams,
76
type PluginConversationBindingRequestResult,
@@ -86,25 +85,21 @@ export type SlackInteractiveHandlerRegistration = PluginInteractiveRegistration<
8685
SlackInteractiveHandlerResult
8786
>;
8887

89-
type SlackInteractiveDispatchContext = Omit<
88+
const dispatchSlackInteractive = createChannelInteractiveDispatcher<
89+
"slack",
90+
"interaction",
9091
SlackInteractiveHandlerContext,
91-
| "interaction"
92-
| "respond"
93-
| "channel"
94-
| "requestConversationBinding"
95-
| "detachConversationBinding"
96-
| "getCurrentConversationBinding"
97-
> & {
98-
interaction:
99-
| Omit<SlackBlockInteractivePayload, "data" | "namespace" | "payload">
100-
| Omit<SlackModalInteractivePayload, "data" | "namespace" | "payload">;
101-
};
92+
SlackInteractiveHandlerResult
93+
>({
94+
channel: "slack",
95+
interactiveKey: "interaction",
96+
});
10297

10398
export async function dispatchSlackPluginInteractiveHandler(params: {
10499
data: string;
105100
interactionId: string;
106101
channelType?: "im" | "mpim" | "channel" | "group";
107-
ctx: SlackInteractiveDispatchContext;
102+
ctx: Parameters<typeof dispatchSlackInteractive>[0]["ctx"];
108103
respond: SlackInteractiveHandlerContext["respond"];
109104
onMatched?: () => Promise<void> | void;
110105
}) {
@@ -117,39 +112,17 @@ export async function dispatchSlackPluginInteractiveHandler(params: {
117112
: params.ctx.conversationId.trim();
118113
const threadId = params.ctx.threadId?.trim() || undefined;
119114

120-
return await dispatchPluginInteractiveHandler<SlackInteractiveHandlerRegistration>({
121-
channel: "slack",
122-
data: params.data,
115+
return await dispatchSlackInteractive({
116+
...params,
123117
dedupeId: params.interactionId,
124-
onMatched: params.onMatched,
125-
invoke: ({ registration, namespace, payload }) =>
126-
registration.handler({
127-
...params.ctx,
128-
channel: "slack",
129-
interaction: {
130-
...params.ctx.interaction,
131-
data: params.data,
132-
namespace,
133-
payload,
134-
},
135-
respond: params.respond,
136-
...createInteractiveConversationBindingHelpers({
137-
// The shared helpers fail closed without owner authority; never expose it to unauthenticated actions.
138-
registration:
139-
params.ctx.auth.isAuthorizedSender && baseConversationId
140-
? registration
141-
: { ...registration, pluginRoot: undefined },
142-
senderId: params.ctx.senderId,
143-
conversation: {
144-
channel: "slack",
145-
accountId: params.ctx.accountId,
146-
conversationId: threadId ?? baseConversationId,
147-
parentConversationId: threadId
148-
? (params.ctx.parentConversationId ?? baseConversationId)
149-
: params.ctx.parentConversationId,
150-
threadId,
151-
},
152-
}),
153-
}),
118+
conversation: {
119+
channel: "slack",
120+
accountId: params.ctx.accountId,
121+
conversationId: threadId ?? baseConversationId,
122+
parentConversationId: threadId
123+
? (params.ctx.parentConversationId ?? baseConversationId)
124+
: params.ctx.parentConversationId,
125+
threadId,
126+
},
154127
});
155128
}

‎extensions/slack/src/monitor/events/interactions.test.ts‎

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,75 @@ vi.mock("openclaw/plugin-sdk/question-gateway-runtime", () => ({
9898
},
9999
}));
100100

101-
vi.mock("openclaw/plugin-sdk/plugin-runtime", () => ({
102-
dispatchPluginInteractiveHandler: (arg: unknown) => dispatchPluginInteractiveHandlerMock(arg),
103-
createInteractiveConversationBindingHelpers: (params: {
104-
registration: { pluginRoot?: string };
105-
conversation: Record<string, unknown>;
106-
}) => createInteractiveConversationBindingHelpersMock(params),
107-
}));
101+
vi.mock("openclaw/plugin-sdk/plugin-runtime", async (importOriginal) => {
102+
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/plugin-runtime")>();
103+
return {
104+
...actual,
105+
createChannelInteractiveDispatcher: (config: {
106+
channel: string;
107+
interactiveKey: PropertyKey;
108+
dispatchInteractiveKey?: PropertyKey;
109+
}) => {
110+
return (params: {
111+
data: string;
112+
dedupeId: string;
113+
ctx: Record<PropertyKey, unknown> & {
114+
accountId: string;
115+
conversationId: string;
116+
senderId?: string;
117+
auth: { isAuthorizedSender: boolean };
118+
};
119+
respond: unknown;
120+
conversation?: Record<string, unknown>;
121+
onMatched?: () => Promise<void> | void;
122+
afterInvoke?: (result: unknown) => Promise<void> | void;
123+
}) =>
124+
dispatchPluginInteractiveHandlerMock({
125+
channel: config.channel,
126+
data: params.data,
127+
dedupeId: params.dedupeId,
128+
onMatched: params.onMatched,
129+
afterInvoke: params.afterInvoke,
130+
invoke: ({
131+
registration,
132+
namespace,
133+
payload,
134+
}: {
135+
registration: { pluginRoot?: string; handler: (ctx: unknown) => unknown };
136+
namespace: string;
137+
payload: string;
138+
}) => {
139+
const dispatchKey = config.dispatchInteractiveKey ?? config.interactiveKey;
140+
const handlerContext = { ...params.ctx };
141+
const interactiveContext = handlerContext[dispatchKey];
142+
delete handlerContext[dispatchKey];
143+
const hasBindingAuthority =
144+
params.ctx.auth.isAuthorizedSender &&
145+
params.ctx.senderId?.trim() &&
146+
params.ctx.accountId.trim() &&
147+
params.ctx.conversationId.trim();
148+
return registration.handler({
149+
...handlerContext,
150+
channel: config.channel,
151+
[config.interactiveKey]: {
152+
...(interactiveContext as object),
153+
data: params.data,
154+
namespace,
155+
payload,
156+
},
157+
respond: params.respond,
158+
...createInteractiveConversationBindingHelpersMock({
159+
registration: hasBindingAuthority
160+
? registration
161+
: { ...registration, pluginRoot: undefined },
162+
conversation: params.conversation ?? {},
163+
}),
164+
});
165+
},
166+
});
167+
},
168+
};
169+
});
108170

109171
vi.mock("../conversation.runtime.js", () => {
110172
const parsePluginBindingApprovalCustomId = (value: string) => {
Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Telegram plugin module implements interactive dispatch behavior.
22
import {
3-
createInteractiveConversationBindingHelpers,
4-
dispatchPluginInteractiveHandler,
3+
createChannelInteractiveDispatcher,
54
type PluginConversationBinding,
65
type PluginConversationBindingRequestParams,
76
type PluginConversationBindingRequestResult,
@@ -63,78 +62,28 @@ export type TelegramInteractiveHandlerRegistration = PluginInteractiveRegistrati
6362
TelegramInteractiveHandlerResult
6463
>;
6564

66-
type TelegramInteractiveDispatchContext = Omit<
65+
const dispatchTelegramInteractive = createChannelInteractiveDispatcher<
66+
"telegram",
67+
"callback",
6768
TelegramInteractiveHandlerContext,
68-
| "callback"
69-
| "respond"
70-
| "channel"
71-
| "requestConversationBinding"
72-
| "detachConversationBinding"
73-
| "getCurrentConversationBinding"
74-
> & {
75-
callbackMessage: {
76-
messageId: number;
77-
chatId: string;
78-
messageText?: string;
79-
};
80-
};
69+
TelegramInteractiveHandlerResult,
70+
"callbackMessage"
71+
>({
72+
channel: "telegram",
73+
interactiveKey: "callback",
74+
dispatchInteractiveKey: "callbackMessage",
75+
});
8176

8277
export async function dispatchTelegramPluginInteractiveHandler(params: {
8378
data: string;
8479
callbackId: string;
85-
ctx: TelegramInteractiveDispatchContext;
86-
respond: {
87-
reply: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise<void>;
88-
editMessage: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise<void>;
89-
editButtons: (params: { buttons: TelegramInteractiveButtons }) => Promise<void>;
90-
clearButtons: () => Promise<void>;
91-
deleteMessage: () => Promise<void>;
92-
};
80+
ctx: Parameters<typeof dispatchTelegramInteractive>[0]["ctx"];
81+
respond: TelegramInteractiveHandlerContext["respond"];
9382
onMatched?: () => Promise<void> | void;
9483
afterInvoke?: (result: TelegramInteractiveHandlerResult) => Promise<void> | void;
9584
}) {
96-
return await dispatchPluginInteractiveHandler<
97-
TelegramInteractiveHandlerRegistration,
98-
TelegramInteractiveHandlerResult
99-
>({
100-
channel: "telegram",
101-
data: params.data,
85+
return await dispatchTelegramInteractive({
86+
...params,
10287
dedupeId: params.callbackId,
103-
onMatched: params.onMatched,
104-
afterInvoke: params.afterInvoke,
105-
invoke: ({ registration, namespace, payload }) => {
106-
const { callbackMessage, ...handlerContext } = params.ctx;
107-
return registration.handler({
108-
...handlerContext,
109-
channel: "telegram",
110-
callback: {
111-
data: params.data,
112-
namespace,
113-
payload,
114-
messageId: callbackMessage.messageId,
115-
chatId: callbackMessage.chatId,
116-
messageText: callbackMessage.messageText,
117-
},
118-
respond: params.respond,
119-
...createInteractiveConversationBindingHelpers({
120-
// Untrusted callbacks may reach their plugin, but never inherit conversation-binding authority.
121-
registration:
122-
handlerContext.auth?.isAuthorizedSender &&
123-
handlerContext.senderId?.trim() &&
124-
handlerContext.accountId?.trim() &&
125-
handlerContext.conversationId?.trim()
126-
? registration
127-
: { ...registration, pluginRoot: undefined },
128-
senderId: handlerContext.senderId,
129-
conversation: {
130-
channel: "telegram",
131-
accountId: handlerContext.accountId,
132-
conversationId: handlerContext.conversationId,
133-
parentConversationId: handlerContext.parentConversationId,
134-
threadId: handlerContext.threadId,
135-
},
136-
}),
137-
});
138-
},
13988
});
14089
}

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
221221
// +1: bounded external-content sanitizer for plugin-owned untrusted projections.
222222
// +1: auth-profile preservation decision for native model pickers.
223223
// +2: shared channel question-reaction store and preflight-audio factories.
224-
4833,
224+
// +1: shared channel interactive dispatcher with canonical binding authorization.
225+
4834,
225226
env,
226227
),
227228
publicFunctionExports: readPluginSdkSurfaceBudgetEnv(
@@ -270,7 +271,8 @@ export function readPluginSdkSurfaceBudgets(env = process.env) {
270271
// +1: bounded external-content sanitizer for plugin-owned untrusted projections.
271272
// +1: auth-profile preservation decision for native model pickers.
272273
// +2: shared channel question-reaction store and preflight-audio factories.
273-
2910,
274+
// +1: shared channel interactive dispatcher with canonical binding authorization.
275+
2911,
274276
env,
275277
),
276278
publicDeprecatedExports: readPluginSdkSurfaceBudgetEnv(

0 commit comments

Comments
 (0)