-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (83 loc) · 3.7 KB
/
Copy pathindex.js
File metadata and controls
90 lines (83 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { logger } from "./logger.js";
import {
wecomChannelPlugin,
handleSubagentDeliveryTarget,
handleSubagentSpawned,
handleSubagentEnded,
} from "./wecom/channel-plugin.js";
import { createWeComMcpTool } from "./wecom/mcp-tool.js";
import { createImageStudioTool } from "./wecom/image-studio-tool.js";
import { createStageBrowserMediaTool } from "./wecom/browser-media-tool.js";
import { resolveQwenImageToolsConfig, wecomPluginConfigSchema } from "./wecom/plugin-config.js";
import { getSessionChatInfo, setOpenclawConfig, setRuntime } from "./wecom/state.js";
import { buildReplyMediaGuidance } from "./wecom/ws-monitor.js";
import { listAccountIds, resolveAccount } from "./wecom/accounts.js";
import { createCallbackHandler } from "./wecom/callback-inbound.js";
import { prepareWecomMessageToolParams } from "./wecom/outbound-sender-protocol.js";
const plugin = {
id: "wecom",
name: "Enterprise WeChat",
description: "Enterprise WeChat AI Bot channel plugin for OpenClaw",
configSchema: wecomPluginConfigSchema,
register(api) {
logger.info("Registering WeCom WS plugin");
setRuntime(api.runtime);
setOpenclawConfig(api.config);
api.registerChannel({ plugin: wecomChannelPlugin });
api.registerTool((ctx = {}) => {
const messageChannel = ctx.messageChannel ?? ctx.channelId;
const requesterUserId =
messageChannel === "wecom" ? String(ctx.requesterSenderId ?? "").trim() || undefined : undefined;
const sessionChat = getSessionChatInfo(ctx.sessionKey);
return createWeComMcpTool({
requesterUserId,
accountId: ctx.agentAccountId,
chatId: sessionChat?.chatId,
chatType: sessionChat?.chatType,
});
}, { name: "wecom_mcp" });
api.registerTool(createStageBrowserMediaTool(), { name: "stage_browser_media" });
const qwenImageToolsConfig = resolveQwenImageToolsConfig(api.pluginConfig);
if (qwenImageToolsConfig.enabled) {
api.registerTool(createImageStudioTool(qwenImageToolsConfig), { name: "image_studio" });
logger.info(
`[image_studio] Registered with provider "${qwenImageToolsConfig.provider}" using qwen(generate=${qwenImageToolsConfig.models.qwen.generate}, edit=${qwenImageToolsConfig.models.qwen.edit}) wan(generate=${qwenImageToolsConfig.models.wan.generate}, edit=${qwenImageToolsConfig.models.wan.edit})`,
);
}
// Register HTTP callback endpoints for all accounts that have callback config
for (const accountId of listAccountIds(api.config)) {
const account = resolveAccount(api.config, accountId);
if (!account?.callbackConfig) continue;
const { path: cbPath } = account.callbackConfig;
logger.info(`[CB] Registering callback endpoint for account=${accountId} path=${cbPath}`);
api.registerHttpRoute({
path: cbPath,
auth: "plugin",
match: "prefix",
handler: createCallbackHandler({ account, config: api.config, runtime: api.runtime }),
});
}
api.on("before_prompt_build", (_event, ctx) => {
if (ctx.channelId !== "wecom") {
return;
}
const guidance = buildReplyMediaGuidance(api.config, ctx.agentId);
return { appendSystemContext: guidance };
});
api.on("subagent_delivery_target", handleSubagentDeliveryTarget);
api.on("subagent_spawned", handleSubagentSpawned);
api.on("subagent_ended", handleSubagentEnded);
api.on("before_tool_call", (event, ctx) => {
if (event.toolName !== "message") {
return;
}
const params = prepareWecomMessageToolParams(event.params, ctx.agentId);
if (params === event.params) {
return;
}
return { params };
});
},
};
export default plugin;
export const register = (api) => plugin.register(api);