@wecom/wecom-openclaw-plugin 2026.3.30 → 2026.3.31-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +251 -4
- package/dist/index.d.ts +2 -4
- package/dist/index.js +113 -0
- package/dist/src/accounts.d.ts +57 -0
- package/dist/src/accounts.js +222 -0
- package/dist/src/agent/api-client.d.ts +94 -0
- package/dist/src/agent/api-client.js +292 -0
- package/dist/src/agent/handler.d.ts +63 -0
- package/dist/src/agent/handler.js +562 -0
- package/dist/src/agent/index.d.ts +5 -0
- package/dist/src/agent/index.js +5 -0
- package/dist/src/agent/webhook.d.ts +23 -0
- package/dist/src/agent/webhook.js +226 -0
- package/dist/src/agent/xml.d.ts +21 -0
- package/dist/src/agent/xml.js +41 -0
- package/dist/src/channel.js +564 -0
- package/dist/src/chat-queue.d.ts +31 -0
- package/dist/src/chat-queue.js +45 -0
- package/dist/src/const.d.ts +46 -0
- package/dist/src/const.js +157 -0
- package/dist/src/dm-policy.js +84 -0
- package/dist/src/dynamic-agent.d.ts +37 -0
- package/dist/src/dynamic-agent.js +60 -0
- package/dist/src/dynamic-routing.d.ts +75 -0
- package/dist/src/dynamic-routing.js +124 -0
- package/dist/src/group-policy.js +126 -0
- package/dist/src/http.d.ts +26 -0
- package/dist/src/http.js +93 -0
- package/dist/src/interface.d.ts +0 -2
- package/dist/src/interface.js +4 -0
- package/dist/src/mcp/index.js +6 -0
- package/dist/src/mcp/interceptors/biz-error.js +64 -0
- package/dist/src/mcp/interceptors/index.js +66 -0
- package/dist/src/mcp/interceptors/msg-media.js +133 -0
- package/dist/src/mcp/interceptors/smartpage-create.js +132 -0
- package/dist/src/mcp/interceptors/smartpage-export.js +87 -0
- package/dist/src/mcp/interceptors/types.js +7 -0
- package/dist/src/mcp/schema.js +99 -0
- package/dist/src/mcp/tool.js +202 -0
- package/dist/src/mcp/transport.js +410 -0
- package/dist/src/media-handler.js +122 -0
- package/dist/src/media-uploader.js +333 -0
- package/dist/src/message-parser.js +129 -0
- package/dist/src/message-sender.d.ts +18 -0
- package/dist/src/message-sender.js +120 -0
- package/dist/src/monitor.js +875 -0
- package/dist/src/onboarding.js +175 -0
- package/dist/src/openclaw-compat.d.ts +47 -0
- package/dist/src/openclaw-compat.js +477 -0
- package/dist/src/reqid-store.js +83 -0
- package/dist/src/routing.d.ts +24 -0
- package/dist/src/routing.js +34 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/shared/command-auth.d.ts +23 -0
- package/dist/src/shared/command-auth.js +73 -0
- package/dist/src/shared/xml-parser.d.ts +46 -0
- package/dist/src/shared/xml-parser.js +210 -0
- package/dist/src/state-dir-resolve.d.ts +2 -0
- package/dist/src/state-dir-resolve.js +9 -0
- package/dist/src/state-manager.js +216 -0
- package/dist/src/target.d.ts +35 -0
- package/dist/src/target.js +67 -0
- package/dist/src/template-card-manager.d.ts +55 -0
- package/dist/src/template-card-manager.js +205 -0
- package/dist/src/template-card-parser.js +647 -0
- package/dist/src/timeout.js +36 -0
- package/dist/src/types/account.d.ts +29 -0
- package/dist/src/types/account.js +4 -0
- package/dist/src/types/config.d.ts +100 -0
- package/dist/src/types/config.js +7 -0
- package/dist/src/types/constants.d.ts +42 -0
- package/dist/src/types/constants.js +42 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.js +5 -0
- package/dist/src/types/message.d.ts +238 -0
- package/dist/src/types/message.js +5 -0
- package/dist/src/utils.d.ts +39 -0
- package/dist/src/utils.js +70 -0
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +24 -0
- package/dist/src/webhook/command-auth.d.ts +47 -0
- package/dist/src/webhook/command-auth.js +96 -0
- package/dist/src/webhook/gateway.d.ts +36 -0
- package/dist/src/webhook/gateway.js +227 -0
- package/dist/src/webhook/handler.d.ts +18 -0
- package/dist/src/webhook/handler.js +420 -0
- package/dist/src/webhook/helpers.d.ts +156 -0
- package/dist/src/webhook/helpers.js +833 -0
- package/dist/src/webhook/http.d.ts +26 -0
- package/dist/src/webhook/http.js +94 -0
- package/dist/src/webhook/index.d.ts +11 -0
- package/dist/src/webhook/index.js +14 -0
- package/dist/src/webhook/media.d.ts +29 -0
- package/dist/src/webhook/media.js +88 -0
- package/dist/src/webhook/monitor.d.ts +59 -0
- package/dist/src/webhook/monitor.js +1185 -0
- package/dist/src/webhook/state.d.ts +220 -0
- package/dist/src/webhook/state.js +491 -0
- package/dist/src/webhook/target.d.ts +41 -0
- package/dist/src/webhook/target.js +132 -0
- package/dist/src/webhook/types.d.ts +347 -0
- package/dist/src/webhook/types.js +33 -0
- package/package.json +15 -7
- package/dist/index.cjs.js +0 -4953
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -4926
- package/dist/index.esm.js.map +0 -1
package/dist/src/const.d.ts
CHANGED
|
@@ -70,5 +70,51 @@ export declare const EVENT_ENTER_CHECK_UPDATE = "event.enter_check_update";
|
|
|
70
70
|
export declare const CMD_ENTER_EVENT_REPLY = "ww_ai_robot_enter_event";
|
|
71
71
|
/** WSClient scene 参数:企微 OpenClaw 场景 */
|
|
72
72
|
export declare const SCENE_WECOM_OPENCLAW = 1;
|
|
73
|
+
/**
|
|
74
|
+
* WeCom 双模式常量定义
|
|
75
|
+
*/
|
|
76
|
+
/** 固定 Webhook 路径 */
|
|
77
|
+
export declare const WEBHOOK_PATHS: {
|
|
78
|
+
/** Bot 模式历史兼容路径(不再维护) */
|
|
79
|
+
readonly BOT: "/wecom";
|
|
80
|
+
/** Bot 模式历史备用兼容路径(不再维护) */
|
|
81
|
+
readonly BOT_ALT: "/wecom/bot";
|
|
82
|
+
/** Agent 模式历史兼容路径(不再维护) */
|
|
83
|
+
readonly AGENT: "/wecom/agent";
|
|
84
|
+
/** Bot 模式推荐路径前缀 */
|
|
85
|
+
readonly BOT_PLUGIN: "/plugins/wecom/bot";
|
|
86
|
+
/** Agent 模式推荐路径前缀 */
|
|
87
|
+
readonly AGENT_PLUGIN: "/plugins/wecom/agent";
|
|
88
|
+
};
|
|
89
|
+
/** 企业微信 API 端点 */
|
|
90
|
+
export declare const API_ENDPOINTS: {
|
|
91
|
+
readonly GET_TOKEN: "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
|
92
|
+
readonly SEND_MESSAGE: "https://qyapi.weixin.qq.com/cgi-bin/message/send";
|
|
93
|
+
readonly SEND_APPCHAT: "https://qyapi.weixin.qq.com/cgi-bin/appchat/send";
|
|
94
|
+
readonly UPLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/upload";
|
|
95
|
+
readonly DOWNLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/get";
|
|
96
|
+
};
|
|
97
|
+
/** 各类限制常量 */
|
|
98
|
+
export declare const LIMITS: {
|
|
99
|
+
/** 文本消息最大字节数 */
|
|
100
|
+
readonly TEXT_MAX_BYTES: 2048;
|
|
101
|
+
/** Token 刷新缓冲时间 (提前刷新) */
|
|
102
|
+
readonly TOKEN_REFRESH_BUFFER_MS: 60000;
|
|
103
|
+
/** HTTP 请求超时 */
|
|
104
|
+
readonly REQUEST_TIMEOUT_MS: 15000;
|
|
105
|
+
/** 最大请求体大小 */
|
|
106
|
+
readonly MAX_REQUEST_BODY_SIZE: number;
|
|
107
|
+
};
|
|
108
|
+
/** AES 加密常量 */
|
|
109
|
+
export declare const CRYPTO: {
|
|
110
|
+
/** PKCS#7 块大小 */
|
|
111
|
+
readonly PKCS7_BLOCK_SIZE: 32;
|
|
112
|
+
/** AES Key 长度 */
|
|
113
|
+
readonly AES_KEY_LENGTH: 32;
|
|
114
|
+
};
|
|
73
115
|
/** 合法的模板卡片 card_type 列表 */
|
|
74
116
|
export declare const VALID_CARD_TYPES: string[];
|
|
117
|
+
/** 模板卡片缓存条目 TTL(毫秒):24小时 */
|
|
118
|
+
export declare const TEMPLATE_CARD_CACHE_TTL_MS: number;
|
|
119
|
+
/** 模板卡片缓存最大条目数 */
|
|
120
|
+
export declare const TEMPLATE_CARD_CACHE_MAX_SIZE = 300;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 企业微信渠道常量定义
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 企业微信渠道 ID
|
|
6
|
+
*/
|
|
7
|
+
export const CHANNEL_ID = "wecom";
|
|
8
|
+
/**
|
|
9
|
+
* 企业微信 WebSocket 命令枚举
|
|
10
|
+
*/
|
|
11
|
+
export var WeComCommand;
|
|
12
|
+
(function (WeComCommand) {
|
|
13
|
+
/** 认证订阅 */
|
|
14
|
+
WeComCommand["SUBSCRIBE"] = "aibot_subscribe";
|
|
15
|
+
/** 心跳 */
|
|
16
|
+
WeComCommand["PING"] = "ping";
|
|
17
|
+
/** 企业微信推送消息 */
|
|
18
|
+
WeComCommand["AIBOT_CALLBACK"] = "aibot_callback";
|
|
19
|
+
/** clawdbot 响应消息 */
|
|
20
|
+
WeComCommand["AIBOT_RESPONSE"] = "aibot_response";
|
|
21
|
+
})(WeComCommand || (WeComCommand = {}));
|
|
22
|
+
// ============================================================================
|
|
23
|
+
// 超时和重试配置
|
|
24
|
+
// ============================================================================
|
|
25
|
+
/** 图片下载超时时间(毫秒) */
|
|
26
|
+
export const IMAGE_DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
27
|
+
/** 文件下载超时时间(毫秒) */
|
|
28
|
+
export const FILE_DOWNLOAD_TIMEOUT_MS = 60_000;
|
|
29
|
+
/** 消息发送超时时间(毫秒) */
|
|
30
|
+
export const REPLY_SEND_TIMEOUT_MS = 15_000;
|
|
31
|
+
/** 消息处理总超时时间(毫秒) */
|
|
32
|
+
export const MESSAGE_PROCESS_TIMEOUT_MS = 6 * 60 * 1000;
|
|
33
|
+
/** WebSocket 心跳间隔(毫秒) */
|
|
34
|
+
export const WS_HEARTBEAT_INTERVAL_MS = 30_000;
|
|
35
|
+
/** WebSocket 连接断开时的最大重连次数 */
|
|
36
|
+
export const WS_MAX_RECONNECT_ATTEMPTS = 10;
|
|
37
|
+
/** WebSocket 认证失败时的最大重试次数 */
|
|
38
|
+
export const WS_MAX_AUTH_FAILURE_ATTEMPTS = 5;
|
|
39
|
+
// ============================================================================
|
|
40
|
+
// 消息状态管理配置
|
|
41
|
+
// ============================================================================
|
|
42
|
+
/** messageStates Map 条目的最大 TTL(毫秒),防止内存泄漏 */
|
|
43
|
+
export const MESSAGE_STATE_TTL_MS = 10 * 60 * 1000;
|
|
44
|
+
/** messageStates Map 清理间隔(毫秒) */
|
|
45
|
+
export const MESSAGE_STATE_CLEANUP_INTERVAL_MS = 60_000;
|
|
46
|
+
/** messageStates Map 最大条目数 */
|
|
47
|
+
export const MESSAGE_STATE_MAX_SIZE = 500;
|
|
48
|
+
// ============================================================================
|
|
49
|
+
// 消息模板
|
|
50
|
+
// ============================================================================
|
|
51
|
+
/** "思考中"流式消息占位内容 */
|
|
52
|
+
export const THINKING_MESSAGE = "<think></think>";
|
|
53
|
+
/** 仅包含图片时的消息占位符 */
|
|
54
|
+
export const MEDIA_IMAGE_PLACEHOLDER = "<media:image>";
|
|
55
|
+
/** 仅包含文件时的消息占位符 */
|
|
56
|
+
export const MEDIA_DOCUMENT_PLACEHOLDER = "<media:document>";
|
|
57
|
+
// ============================================================================
|
|
58
|
+
// 默认值
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// MCP 配置
|
|
62
|
+
// ============================================================================
|
|
63
|
+
/** 获取 MCP 配置的 WebSocket 命令 */
|
|
64
|
+
export const MCP_GET_CONFIG_CMD = "aibot_get_mcp_config";
|
|
65
|
+
/** MCP 配置拉取超时时间(毫秒) */
|
|
66
|
+
export const MCP_CONFIG_FETCH_TIMEOUT_MS = 15_000;
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// 默认值
|
|
69
|
+
// ============================================================================
|
|
70
|
+
/** 默认媒体大小上限(MB) */
|
|
71
|
+
export const DEFAULT_MEDIA_MAX_MB = 5;
|
|
72
|
+
/** 文本分块大小上限 */
|
|
73
|
+
export const TEXT_CHUNK_LIMIT = 4000;
|
|
74
|
+
// ============================================================================
|
|
75
|
+
// 媒体上传相关常量
|
|
76
|
+
// ============================================================================
|
|
77
|
+
/** 图片大小上限(字节):10MB */
|
|
78
|
+
export const IMAGE_MAX_BYTES = 10 * 1024 * 1024;
|
|
79
|
+
/** 视频大小上限(字节):10MB */
|
|
80
|
+
export const VIDEO_MAX_BYTES = 10 * 1024 * 1024;
|
|
81
|
+
/** 语音大小上限(字节):2MB */
|
|
82
|
+
export const VOICE_MAX_BYTES = 2 * 1024 * 1024;
|
|
83
|
+
/** 文件大小上限(字节):20MB */
|
|
84
|
+
export const FILE_MAX_BYTES = 20 * 1024 * 1024;
|
|
85
|
+
/** 文件绝对上限(字节):超过此值无法发送,等于 FILE_MAX_BYTES */
|
|
86
|
+
export const ABSOLUTE_MAX_BYTES = FILE_MAX_BYTES;
|
|
87
|
+
/** 上传分片大小(字节,Base64 编码前):512KB */
|
|
88
|
+
export const UPLOAD_CHUNK_SIZE = 512 * 1024;
|
|
89
|
+
// ============================================================================
|
|
90
|
+
// 事件/命令名称常量
|
|
91
|
+
// ============================================================================
|
|
92
|
+
/** 版本检查事件名称(SDK 事件监听用) */
|
|
93
|
+
export const EVENT_ENTER_CHECK_UPDATE = "event.enter_check_update";
|
|
94
|
+
/** 版本检查事件回复命令名称 */
|
|
95
|
+
export const CMD_ENTER_EVENT_REPLY = "ww_ai_robot_enter_event";
|
|
96
|
+
// ============================================================================
|
|
97
|
+
// SDK 连接配置
|
|
98
|
+
// ============================================================================
|
|
99
|
+
/** WSClient scene 参数:企微 OpenClaw 场景 */
|
|
100
|
+
export const SCENE_WECOM_OPENCLAW = 1;
|
|
101
|
+
/**
|
|
102
|
+
* WeCom 双模式常量定义
|
|
103
|
+
*/
|
|
104
|
+
/** 固定 Webhook 路径 */
|
|
105
|
+
export const WEBHOOK_PATHS = {
|
|
106
|
+
/** Bot 模式历史兼容路径(不再维护) */
|
|
107
|
+
BOT: "/wecom",
|
|
108
|
+
/** Bot 模式历史备用兼容路径(不再维护) */
|
|
109
|
+
BOT_ALT: "/wecom/bot",
|
|
110
|
+
/** Agent 模式历史兼容路径(不再维护) */
|
|
111
|
+
AGENT: "/wecom/agent",
|
|
112
|
+
/** Bot 模式推荐路径前缀 */
|
|
113
|
+
BOT_PLUGIN: "/plugins/wecom/bot",
|
|
114
|
+
/** Agent 模式推荐路径前缀 */
|
|
115
|
+
AGENT_PLUGIN: "/plugins/wecom/agent",
|
|
116
|
+
};
|
|
117
|
+
/** 企业微信 API 端点 */
|
|
118
|
+
export const API_ENDPOINTS = {
|
|
119
|
+
GET_TOKEN: "https://qyapi.weixin.qq.com/cgi-bin/gettoken",
|
|
120
|
+
SEND_MESSAGE: "https://qyapi.weixin.qq.com/cgi-bin/message/send",
|
|
121
|
+
SEND_APPCHAT: "https://qyapi.weixin.qq.com/cgi-bin/appchat/send",
|
|
122
|
+
UPLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/upload",
|
|
123
|
+
DOWNLOAD_MEDIA: "https://qyapi.weixin.qq.com/cgi-bin/media/get",
|
|
124
|
+
};
|
|
125
|
+
/** 各类限制常量 */
|
|
126
|
+
export const LIMITS = {
|
|
127
|
+
/** 文本消息最大字节数 */
|
|
128
|
+
TEXT_MAX_BYTES: 2048,
|
|
129
|
+
/** Token 刷新缓冲时间 (提前刷新) */
|
|
130
|
+
TOKEN_REFRESH_BUFFER_MS: 60_000,
|
|
131
|
+
/** HTTP 请求超时 */
|
|
132
|
+
REQUEST_TIMEOUT_MS: 15_000,
|
|
133
|
+
/** 最大请求体大小 */
|
|
134
|
+
MAX_REQUEST_BODY_SIZE: 1024 * 1024,
|
|
135
|
+
};
|
|
136
|
+
/** AES 加密常量 */
|
|
137
|
+
export const CRYPTO = {
|
|
138
|
+
/** PKCS#7 块大小 */
|
|
139
|
+
PKCS7_BLOCK_SIZE: 32,
|
|
140
|
+
/** AES Key 长度 */
|
|
141
|
+
AES_KEY_LENGTH: 32,
|
|
142
|
+
};
|
|
143
|
+
// ============================================================================
|
|
144
|
+
// 模板卡片配置
|
|
145
|
+
// ============================================================================
|
|
146
|
+
/** 合法的模板卡片 card_type 列表 */
|
|
147
|
+
export const VALID_CARD_TYPES = [
|
|
148
|
+
"text_notice",
|
|
149
|
+
"news_notice",
|
|
150
|
+
"button_interaction",
|
|
151
|
+
"vote_interaction",
|
|
152
|
+
"multiple_interaction",
|
|
153
|
+
];
|
|
154
|
+
/** 模板卡片缓存条目 TTL(毫秒):24小时 */
|
|
155
|
+
export const TEMPLATE_CARD_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
156
|
+
/** 模板卡片缓存最大条目数 */
|
|
157
|
+
export const TEMPLATE_CARD_CACHE_MAX_SIZE = 300;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 企业微信 DM(私聊)访问控制模块
|
|
3
|
+
*
|
|
4
|
+
* 负责私聊策略检查、配对流程
|
|
5
|
+
*/
|
|
6
|
+
import { getWeComRuntime } from "./runtime.js";
|
|
7
|
+
import { CHANNEL_ID } from "./const.js";
|
|
8
|
+
import { sendWeComReply } from "./message-sender.js";
|
|
9
|
+
import { isSenderAllowed } from "./group-policy.js";
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// 公开 API
|
|
12
|
+
// ============================================================================
|
|
13
|
+
/**
|
|
14
|
+
* 检查 DM Policy 访问控制
|
|
15
|
+
* @returns 检查结果,包含是否允许继续处理
|
|
16
|
+
*/
|
|
17
|
+
export async function checkDmPolicy(params) {
|
|
18
|
+
const { senderId, isGroup, account, wsClient, frame, runtime } = params;
|
|
19
|
+
const core = getWeComRuntime();
|
|
20
|
+
// 群聊消息不检查 DM Policy
|
|
21
|
+
if (isGroup) {
|
|
22
|
+
return { allowed: true };
|
|
23
|
+
}
|
|
24
|
+
const dmPolicy = account.config.dmPolicy ?? "open";
|
|
25
|
+
const configAllowFrom = (account.config.allowFrom ?? []).map((v) => String(v));
|
|
26
|
+
// 如果 dmPolicy 是 disabled,直接拒绝
|
|
27
|
+
if (dmPolicy === "disabled") {
|
|
28
|
+
runtime.log?.(`[WeCom] Blocked DM from ${senderId} (dmPolicy=disabled)`);
|
|
29
|
+
return { allowed: false };
|
|
30
|
+
}
|
|
31
|
+
// 如果是 open 模式,允许所有人
|
|
32
|
+
if (dmPolicy === "open") {
|
|
33
|
+
return { allowed: true };
|
|
34
|
+
}
|
|
35
|
+
// OpenClaw <= 2026.2.19 signature: readAllowFromStore(channel, env?, accountId?)
|
|
36
|
+
// @ts-expect-error — 兼容旧版 SDK 的三参数签名,新版已改为单参数对象
|
|
37
|
+
const oldStoreAllowFrom = await core.channel.pairing.readAllowFromStore('wecom', undefined, account.accountId).catch(() => []);
|
|
38
|
+
// Compatibility fallback for newer OpenClaw implementations.
|
|
39
|
+
const newStoreAllowFrom = await core.channel.pairing
|
|
40
|
+
.readAllowFromStore({ channel: CHANNEL_ID, accountId: account.accountId })
|
|
41
|
+
.catch(() => []);
|
|
42
|
+
// 检查发送者是否在允许列表中
|
|
43
|
+
const storeAllowFrom = [...oldStoreAllowFrom, ...newStoreAllowFrom];
|
|
44
|
+
const effectiveAllowFrom = [...configAllowFrom, ...storeAllowFrom];
|
|
45
|
+
const senderAllowedResult = isSenderAllowed(senderId, effectiveAllowFrom);
|
|
46
|
+
if (senderAllowedResult) {
|
|
47
|
+
return { allowed: true };
|
|
48
|
+
}
|
|
49
|
+
// 处理未授权用户
|
|
50
|
+
if (dmPolicy === "pairing") {
|
|
51
|
+
const { code, created } = await core.channel.pairing.upsertPairingRequest({
|
|
52
|
+
channel: CHANNEL_ID,
|
|
53
|
+
id: senderId,
|
|
54
|
+
accountId: account.accountId,
|
|
55
|
+
meta: { name: senderId },
|
|
56
|
+
});
|
|
57
|
+
if (created) {
|
|
58
|
+
runtime.log?.(`[WeCom] Pairing request created for sender=${senderId}`);
|
|
59
|
+
try {
|
|
60
|
+
await sendWeComReply({
|
|
61
|
+
wsClient,
|
|
62
|
+
frame,
|
|
63
|
+
text: core.channel.pairing.buildPairingReply({
|
|
64
|
+
channel: CHANNEL_ID,
|
|
65
|
+
idLine: `您的企业微信用户ID: ${senderId}`,
|
|
66
|
+
code,
|
|
67
|
+
}),
|
|
68
|
+
runtime,
|
|
69
|
+
finish: true,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
runtime.error?.(`[WeCom] Failed to send pairing reply to ${senderId}: ${String(err)}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
runtime.log?.(`[WeCom] Pairing request already exists for sender=${senderId}`);
|
|
78
|
+
}
|
|
79
|
+
return { allowed: false, pairingSent: created };
|
|
80
|
+
}
|
|
81
|
+
// allowlist 模式:直接拒绝未授权用户
|
|
82
|
+
runtime.log?.(`[WeCom] Blocked unauthorized sender ${senderId} (dmPolicy=${dmPolicy})`);
|
|
83
|
+
return { allowed: false };
|
|
84
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **动态 Agent 路由模块**
|
|
3
|
+
*
|
|
4
|
+
* 为每个用户/群组自动生成独立的 Agent ID,实现会话隔离。
|
|
5
|
+
* 参考: openclaw-plugin-wecom/dynamic-agent.js
|
|
6
|
+
*/
|
|
7
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
8
|
+
export interface DynamicAgentConfig {
|
|
9
|
+
enabled: boolean;
|
|
10
|
+
dmCreateAgent: boolean;
|
|
11
|
+
groupEnabled: boolean;
|
|
12
|
+
adminUsers: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* **getDynamicAgentConfig (读取动态 Agent 配置)**
|
|
16
|
+
*
|
|
17
|
+
* 从全局配置中读取动态 Agent 配置,提供默认值。
|
|
18
|
+
*/
|
|
19
|
+
export declare function getDynamicAgentConfig(config: OpenClawConfig): DynamicAgentConfig;
|
|
20
|
+
/**
|
|
21
|
+
* **generateAgentId (生成动态 Agent ID)**
|
|
22
|
+
*
|
|
23
|
+
* 根据账号 + 聊天类型 + 对端 ID 生成确定性的 Agent ID,避免多账号串会话。
|
|
24
|
+
* 格式: wecom-{accountId}-{type}-{sanitizedPeerId}
|
|
25
|
+
*/
|
|
26
|
+
export declare function generateAgentId(chatType: "dm" | "group", peerId: string, accountId?: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* **shouldUseDynamicAgent (检查是否使用动态 Agent)**
|
|
29
|
+
*
|
|
30
|
+
* 根据配置和发送者信息判断是否应使用动态 Agent。
|
|
31
|
+
* 管理员(adminUsers)始终绕过动态路由,使用主 Agent。
|
|
32
|
+
*/
|
|
33
|
+
export declare function shouldUseDynamicAgent(params: {
|
|
34
|
+
chatType: "dm" | "group";
|
|
35
|
+
senderId: string;
|
|
36
|
+
config: OpenClawConfig;
|
|
37
|
+
}): boolean;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **动态 Agent 路由模块**
|
|
3
|
+
*
|
|
4
|
+
* 为每个用户/群组自动生成独立的 Agent ID,实现会话隔离。
|
|
5
|
+
* 参考: openclaw-plugin-wecom/dynamic-agent.js
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* **getDynamicAgentConfig (读取动态 Agent 配置)**
|
|
9
|
+
*
|
|
10
|
+
* 从全局配置中读取动态 Agent 配置,提供默认值。
|
|
11
|
+
*/
|
|
12
|
+
export function getDynamicAgentConfig(config) {
|
|
13
|
+
const dynamicAgents = config?.channels?.wecom?.dynamicAgents;
|
|
14
|
+
return {
|
|
15
|
+
enabled: dynamicAgents?.enabled ?? false,
|
|
16
|
+
dmCreateAgent: dynamicAgents?.dmCreateAgent ?? true,
|
|
17
|
+
groupEnabled: dynamicAgents?.groupEnabled ?? true,
|
|
18
|
+
adminUsers: dynamicAgents?.adminUsers ?? [],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function sanitizeDynamicIdPart(value) {
|
|
22
|
+
return String(value)
|
|
23
|
+
.trim()
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
.replace(/[^a-z0-9_-]/g, "_");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* **generateAgentId (生成动态 Agent ID)**
|
|
29
|
+
*
|
|
30
|
+
* 根据账号 + 聊天类型 + 对端 ID 生成确定性的 Agent ID,避免多账号串会话。
|
|
31
|
+
* 格式: wecom-{accountId}-{type}-{sanitizedPeerId}
|
|
32
|
+
*/
|
|
33
|
+
export function generateAgentId(chatType, peerId, accountId) {
|
|
34
|
+
const sanitizedPeer = sanitizeDynamicIdPart(peerId) || "unknown";
|
|
35
|
+
const sanitizedAccountId = sanitizeDynamicIdPart(accountId ?? "default") || "default";
|
|
36
|
+
return `wecom-${sanitizedAccountId}-${chatType}-${sanitizedPeer}`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* **shouldUseDynamicAgent (检查是否使用动态 Agent)**
|
|
40
|
+
*
|
|
41
|
+
* 根据配置和发送者信息判断是否应使用动态 Agent。
|
|
42
|
+
* 管理员(adminUsers)始终绕过动态路由,使用主 Agent。
|
|
43
|
+
*/
|
|
44
|
+
export function shouldUseDynamicAgent(params) {
|
|
45
|
+
const { chatType, senderId, config } = params;
|
|
46
|
+
const dynamicConfig = getDynamicAgentConfig(config);
|
|
47
|
+
if (!dynamicConfig.enabled) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
// 管理员绕过动态路由
|
|
51
|
+
const sender = String(senderId).trim().toLowerCase();
|
|
52
|
+
const isAdmin = dynamicConfig.adminUsers.some((admin) => admin.trim().toLowerCase() === sender);
|
|
53
|
+
if (isAdmin) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (chatType === "group") {
|
|
57
|
+
return dynamicConfig.groupEnabled;
|
|
58
|
+
}
|
|
59
|
+
return dynamicConfig.dmCreateAgent;
|
|
60
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 动态路由统一处理模块
|
|
3
|
+
* 提供统一的路由注入接口。
|
|
4
|
+
*/
|
|
5
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/core";
|
|
6
|
+
import type { PluginRuntime } from "openclaw/plugin-sdk";
|
|
7
|
+
/**
|
|
8
|
+
* 路由对象(从 core.channel.routing.resolveAgentRoute 返回)
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentRoute {
|
|
11
|
+
agentId: string;
|
|
12
|
+
sessionKey: string;
|
|
13
|
+
matchedBy: string;
|
|
14
|
+
accountId: string;
|
|
15
|
+
mainSessionKey?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 动态路由处理参数
|
|
19
|
+
*/
|
|
20
|
+
export interface DynamicRoutingParams {
|
|
21
|
+
/** 原始路由对象 */
|
|
22
|
+
route: AgentRoute;
|
|
23
|
+
/** 全局配置 */
|
|
24
|
+
config: OpenClawConfig;
|
|
25
|
+
/** 插件运行时 */
|
|
26
|
+
core: PluginRuntime;
|
|
27
|
+
/** 账号 ID */
|
|
28
|
+
accountId: string;
|
|
29
|
+
/** 会话类型 */
|
|
30
|
+
chatType: "group" | "dm";
|
|
31
|
+
/** 对话 ID(群聊为 chatId,私聊为 userId) */
|
|
32
|
+
chatId: string;
|
|
33
|
+
/** 发送者用户 ID */
|
|
34
|
+
senderId: string;
|
|
35
|
+
/** 日志输出函数(可选) */
|
|
36
|
+
log?: (msg: string) => void;
|
|
37
|
+
/** 错误日志输出函数(可选) */
|
|
38
|
+
error?: (msg: string) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 动态路由处理结果
|
|
42
|
+
*/
|
|
43
|
+
export interface DynamicRoutingResult {
|
|
44
|
+
/** 是否使用动态 Agent */
|
|
45
|
+
useDynamicAgent: boolean;
|
|
46
|
+
/** 是否被默认路由拦截 */
|
|
47
|
+
rejected: boolean;
|
|
48
|
+
/** 拒绝原因(如果 rejected=true) */
|
|
49
|
+
rejectReason?: string;
|
|
50
|
+
/** 最终的 agentId(可能被动态注入修改) */
|
|
51
|
+
finalAgentId: string;
|
|
52
|
+
/** 最终的 sessionKey(可能被动态注入修改) */
|
|
53
|
+
finalSessionKey: string;
|
|
54
|
+
/** 是否修改了路由(注入了动态 Agent) */
|
|
55
|
+
routeModified: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 统一处理动态路由注入逻辑
|
|
59
|
+
*
|
|
60
|
+
* 功能:
|
|
61
|
+
* 1. 判断是否需要使用动态 Agent
|
|
62
|
+
* 2. 根据 matchedBy 判断配置类型
|
|
63
|
+
* 3. 检查是否被默认路由拦截(fail-closed 策略)
|
|
64
|
+
* 4. 返回最终的路由信息(不修改传入的 route 对象)
|
|
65
|
+
* 5. 输出详细的调试日志
|
|
66
|
+
*
|
|
67
|
+
* **重要规则**:
|
|
68
|
+
* - matchedBy = "binding.peer" 或 "binding.peer.parent" → 跳过动态路由
|
|
69
|
+
* - matchedBy = "binding.account" 或其他 → 继续使用动态路由
|
|
70
|
+
* - matchedBy = "default" → 检查 fail-closed 策略
|
|
71
|
+
*
|
|
72
|
+
* @param params 动态路由处理参数
|
|
73
|
+
* @returns 处理结果
|
|
74
|
+
*/
|
|
75
|
+
export declare function processDynamicRouting(params: DynamicRoutingParams): DynamicRoutingResult;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 动态路由统一处理模块
|
|
3
|
+
* 提供统一的路由注入接口。
|
|
4
|
+
*/
|
|
5
|
+
import { shouldUseDynamicAgent, generateAgentId } from "./dynamic-agent.js";
|
|
6
|
+
import { shouldRejectWecomDefaultRoute } from "./routing.js";
|
|
7
|
+
/**
|
|
8
|
+
* 根据 matchedBy 和配置内容判断是否应该跳过动态路由
|
|
9
|
+
* **重要**:即使 matchedBy=binding.peer,也需要检查配置的 peer.kind 是否与实际聊天类型匹配
|
|
10
|
+
* @param params 判断参数
|
|
11
|
+
* @returns 是否跳过动态路由
|
|
12
|
+
*/
|
|
13
|
+
function shouldSkipDynamicRouting(params) {
|
|
14
|
+
const { matchedBy, config, route, chatType, log } = params;
|
|
15
|
+
// 只有精确匹配到 peer 时才可能跳过动态路由
|
|
16
|
+
if (matchedBy !== "binding.peer" && matchedBy !== "binding.peer.parent") {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const binding = config.bindings?.find(b => b.agentId === route.agentId);
|
|
20
|
+
if (!binding) {
|
|
21
|
+
log?.(`[dynamic-routing] ⚠️ 警告:matchedBy=binding.peer,但在配置中找不到 agentId=${route.agentId} 的 binding`);
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
// 检查配置的 peer.kind 是否与实际聊天类型匹配
|
|
25
|
+
const configuredKind = binding.match?.peer?.kind;
|
|
26
|
+
if (!configuredKind) {
|
|
27
|
+
// 配置中没有指定 kind,使用动态路由
|
|
28
|
+
log?.(`[dynamic-routing] ℹ️ binding 未指定`);
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const actualKind = chatType === "group" ? "group" : "direct";
|
|
32
|
+
if (configuredKind !== actualKind) {
|
|
33
|
+
// 类型不匹配,继续使用动态路由
|
|
34
|
+
log?.(`[dynamic-routing] ⚠️ binding 配置的聊天类型 (${configuredKind}) 与实际类型 (${actualKind}) 不匹配,继续使用动态路由`);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// 类型匹配,跳过动态路由
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 统一处理动态路由注入逻辑
|
|
42
|
+
*
|
|
43
|
+
* 功能:
|
|
44
|
+
* 1. 判断是否需要使用动态 Agent
|
|
45
|
+
* 2. 根据 matchedBy 判断配置类型
|
|
46
|
+
* 3. 检查是否被默认路由拦截(fail-closed 策略)
|
|
47
|
+
* 4. 返回最终的路由信息(不修改传入的 route 对象)
|
|
48
|
+
* 5. 输出详细的调试日志
|
|
49
|
+
*
|
|
50
|
+
* **重要规则**:
|
|
51
|
+
* - matchedBy = "binding.peer" 或 "binding.peer.parent" → 跳过动态路由
|
|
52
|
+
* - matchedBy = "binding.account" 或其他 → 继续使用动态路由
|
|
53
|
+
* - matchedBy = "default" → 检查 fail-closed 策略
|
|
54
|
+
*
|
|
55
|
+
* @param params 动态路由处理参数
|
|
56
|
+
* @returns 处理结果
|
|
57
|
+
*/
|
|
58
|
+
export function processDynamicRouting(params) {
|
|
59
|
+
const { route, config, accountId, chatType, chatId, senderId, log } = params;
|
|
60
|
+
log?.(`[dynamic-routing] 🔍 调试 - matchedBy=${route.matchedBy}, agentId=${route.agentId}`);
|
|
61
|
+
if (route.matchedBy !== "default") {
|
|
62
|
+
// 根据 matchedBy 和配置内容判断是否跳过动态路由
|
|
63
|
+
if (shouldSkipDynamicRouting({ matchedBy: route.matchedBy, config, route, chatType, log })) {
|
|
64
|
+
// 精确配置(binding.peer)且类型匹配,跳过动态路由
|
|
65
|
+
log?.(`[dynamic-routing] ℹ️ 检测到精确配置的 bindings (matchedBy=${route.matchedBy}),且聊天类型匹配,跳过动态路由`);
|
|
66
|
+
return {
|
|
67
|
+
useDynamicAgent: false,
|
|
68
|
+
rejected: false,
|
|
69
|
+
finalAgentId: route.agentId,
|
|
70
|
+
finalSessionKey: route.sessionKey,
|
|
71
|
+
routeModified: false,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// 其他类型的配置(binding.account、binding.channel 等),继续使用动态路由
|
|
76
|
+
log?.(`[dynamic-routing] ℹ️ 检测到非精确配置的 bindings (matchedBy=${route.matchedBy}),继续使用动态路由`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// 判断是否使用动态 Agent
|
|
80
|
+
const useDynamicAgent = shouldUseDynamicAgent({
|
|
81
|
+
chatType,
|
|
82
|
+
senderId,
|
|
83
|
+
config,
|
|
84
|
+
});
|
|
85
|
+
// 检查是否被默认路由拦截
|
|
86
|
+
const rejected = shouldRejectWecomDefaultRoute({
|
|
87
|
+
cfg: config,
|
|
88
|
+
matchedBy: route.matchedBy,
|
|
89
|
+
useDynamicAgent,
|
|
90
|
+
});
|
|
91
|
+
if (rejected) {
|
|
92
|
+
const rejectReason = `当前账号(${accountId})未绑定 OpenClaw Agent,已拒绝回退到默认主智能体。请在 bindings 中添加:{"agentId":"你的Agent","match":{"channel":"wecom","accountId":"${accountId}"}}`;
|
|
93
|
+
return {
|
|
94
|
+
useDynamicAgent,
|
|
95
|
+
rejected: true,
|
|
96
|
+
rejectReason,
|
|
97
|
+
finalAgentId: route.agentId,
|
|
98
|
+
finalSessionKey: route.sessionKey,
|
|
99
|
+
routeModified: false,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// 使用动态 Agent
|
|
103
|
+
if (useDynamicAgent) {
|
|
104
|
+
log?.(`[dynamic-routing] 原始路由信息: agentId=${route.agentId}, matchedBy=${route.matchedBy}, sessionKey=${route.sessionKey}`);
|
|
105
|
+
const targetAgentId = generateAgentId(chatType, chatId, accountId);
|
|
106
|
+
const targetSessionKey = `agent:${targetAgentId}:wecom:${accountId}:${chatType}:${chatId}`;
|
|
107
|
+
log?.(`[dynamic-routing] 🔄 路由注入: agentId=${targetAgentId}, sessionKey=${targetSessionKey}`);
|
|
108
|
+
return {
|
|
109
|
+
useDynamicAgent: true,
|
|
110
|
+
rejected: false,
|
|
111
|
+
finalAgentId: targetAgentId,
|
|
112
|
+
finalSessionKey: targetSessionKey,
|
|
113
|
+
routeModified: true,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// 不使用动态 Agent,返回原始路由
|
|
117
|
+
return {
|
|
118
|
+
useDynamicAgent: false,
|
|
119
|
+
rejected: false,
|
|
120
|
+
finalAgentId: route.agentId,
|
|
121
|
+
finalSessionKey: route.sessionKey,
|
|
122
|
+
routeModified: false,
|
|
123
|
+
};
|
|
124
|
+
}
|