@nextclaw/channel-extension-weixin 0.1.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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +12 -0
- package/dist/services/weixin-api.service.d.ts +125 -0
- package/dist/services/weixin-api.service.js +182 -0
- package/dist/services/weixin-auth-capability.service.d.ts +20 -0
- package/dist/services/weixin-auth-capability.service.js +33 -0
- package/dist/services/weixin-channel-adapter.service.d.ts +53 -0
- package/dist/services/weixin-channel-adapter.service.js +303 -0
- package/dist/services/weixin-extension-runtime.service.d.ts +19 -0
- package/dist/services/weixin-extension-runtime.service.js +65 -0
- package/dist/services/weixin-login.service.d.ts +55 -0
- package/dist/services/weixin-login.service.js +203 -0
- package/dist/services/weixin-media-part-reader.service.js +192 -0
- package/dist/services/weixin-reply-chat.service.js +141 -0
- package/dist/services/weixin-typing-controller.service.js +109 -0
- package/dist/stores/weixin-account.store.d.ts +19 -0
- package/dist/stores/weixin-account.store.js +56 -0
- package/dist/types/weixin-extension.types.d.ts +43 -0
- package/dist/utils/weixin-config.utils.d.ts +69 -0
- package/dist/utils/weixin-config.utils.js +107 -0
- package/dist/utils/weixin-inbound-media.utils.js +215 -0
- package/dist/utils/weixin-media.utils.js +168 -0
- package/dist/utils/weixin-session-route.utils.js +38 -0
- package/nextclaw.extension.json +58 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NextClaw contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { WeixinAuthPollResult, WeixinAuthStartResult, WeixinLoginParams, WeixinLoginService } from "./services/weixin-login.service.js";
|
|
2
|
+
import { WeixinAuthCapability } from "./services/weixin-auth-capability.service.js";
|
|
3
|
+
import { WeixinAccountConfig, WeixinChannelConfig, WeixinInboundMessage } from "./types/weixin-extension.types.js";
|
|
4
|
+
import { WeixinChannelAdapter } from "./services/weixin-channel-adapter.service.js";
|
|
5
|
+
import { WeixinExtensionRuntime } from "./services/weixin-extension-runtime.service.js";
|
|
6
|
+
import { DEFAULT_WEIXIN_BASE_URL, WEIXIN_CHANNEL_CONFIG_SCHEMA, WEIXIN_CHANNEL_CONFIG_UI_HINTS, WEIXIN_CHANNEL_ID, WEIXIN_EXTENSION_ID } from "./utils/weixin-config.utils.js";
|
|
7
|
+
export { DEFAULT_WEIXIN_BASE_URL, WEIXIN_CHANNEL_CONFIG_SCHEMA, WEIXIN_CHANNEL_CONFIG_UI_HINTS, WEIXIN_CHANNEL_ID, WEIXIN_EXTENSION_ID, type WeixinAccountConfig, WeixinAuthCapability, type WeixinAuthPollResult, type WeixinAuthStartResult, WeixinChannelAdapter, type WeixinChannelConfig, WeixinExtensionRuntime, type WeixinInboundMessage, type WeixinLoginParams, WeixinLoginService };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DEFAULT_WEIXIN_BASE_URL, WEIXIN_CHANNEL_CONFIG_SCHEMA, WEIXIN_CHANNEL_CONFIG_UI_HINTS, WEIXIN_CHANNEL_ID, WEIXIN_EXTENSION_ID } from "./utils/weixin-config.utils.js";
|
|
2
|
+
import { WeixinLoginService } from "./services/weixin-login.service.js";
|
|
3
|
+
import { WeixinAuthCapability } from "./services/weixin-auth-capability.service.js";
|
|
4
|
+
import { WeixinChannelAdapter } from "./services/weixin-channel-adapter.service.js";
|
|
5
|
+
import { WeixinExtensionRuntime } from "./services/weixin-extension-runtime.service.js";
|
|
6
|
+
export { DEFAULT_WEIXIN_BASE_URL, WEIXIN_CHANNEL_CONFIG_SCHEMA, WEIXIN_CHANNEL_CONFIG_UI_HINTS, WEIXIN_CHANNEL_ID, WEIXIN_EXTENSION_ID, WeixinAuthCapability, WeixinChannelAdapter, WeixinExtensionRuntime, WeixinLoginService };
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { WeixinAuthCapability } from "./services/weixin-auth-capability.service.js";
|
|
2
|
+
import { WeixinChannelAdapter } from "./services/weixin-channel-adapter.service.js";
|
|
3
|
+
import { WeixinExtensionRuntime } from "./services/weixin-extension-runtime.service.js";
|
|
4
|
+
import { NextClawExtension } from "@nextclaw/extension-sdk";
|
|
5
|
+
//#region src/main.ts
|
|
6
|
+
const extension = new NextClawExtension();
|
|
7
|
+
const weixin = extension.channels.use("weixin");
|
|
8
|
+
const runtime = new WeixinExtensionRuntime(weixin, new WeixinChannelAdapter());
|
|
9
|
+
extension.capabilities.provide("channel.auth", new WeixinAuthCapability({ channel: weixin }));
|
|
10
|
+
await runtime.start();
|
|
11
|
+
//#endregion
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
//#region src/services/weixin-api.service.d.ts
|
|
2
|
+
type WeixinMessageItem = {
|
|
3
|
+
type?: number;
|
|
4
|
+
text_item?: {
|
|
5
|
+
text?: string;
|
|
6
|
+
};
|
|
7
|
+
image_item?: {
|
|
8
|
+
media?: WeixinCdnMedia;
|
|
9
|
+
thumb_media?: WeixinCdnMedia;
|
|
10
|
+
aeskey?: string;
|
|
11
|
+
mid_size?: number;
|
|
12
|
+
thumb_size?: number;
|
|
13
|
+
thumb_height?: number;
|
|
14
|
+
thumb_width?: number;
|
|
15
|
+
hd_size?: number;
|
|
16
|
+
};
|
|
17
|
+
voice_item?: {
|
|
18
|
+
text?: string;
|
|
19
|
+
};
|
|
20
|
+
file_item?: {
|
|
21
|
+
file_name?: string;
|
|
22
|
+
media?: WeixinCdnMedia;
|
|
23
|
+
len?: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
type WeixinCdnMedia = {
|
|
27
|
+
encrypt_query_param?: string;
|
|
28
|
+
aes_key?: string;
|
|
29
|
+
encrypt_type?: number;
|
|
30
|
+
full_url?: string;
|
|
31
|
+
};
|
|
32
|
+
type WeixinMessage = {
|
|
33
|
+
message_id?: number;
|
|
34
|
+
from_user_id?: string;
|
|
35
|
+
to_user_id?: string;
|
|
36
|
+
message_type?: number;
|
|
37
|
+
item_list?: WeixinMessageItem[];
|
|
38
|
+
context_token?: string;
|
|
39
|
+
};
|
|
40
|
+
type WeixinGetUpdatesResponse = {
|
|
41
|
+
ret?: number;
|
|
42
|
+
errcode?: number;
|
|
43
|
+
errmsg?: string;
|
|
44
|
+
msgs?: WeixinMessage[];
|
|
45
|
+
get_updates_buf?: string;
|
|
46
|
+
};
|
|
47
|
+
type WeixinConfigResponse = {
|
|
48
|
+
ret?: number;
|
|
49
|
+
errcode?: number;
|
|
50
|
+
errmsg?: string;
|
|
51
|
+
typing_ticket?: string;
|
|
52
|
+
};
|
|
53
|
+
type WeixinQrCodeResponse = {
|
|
54
|
+
qrcode?: string;
|
|
55
|
+
qrcode_img_content?: string;
|
|
56
|
+
};
|
|
57
|
+
type WeixinQrStatusResponse = {
|
|
58
|
+
status?: string;
|
|
59
|
+
bot_token?: string;
|
|
60
|
+
ilink_bot_id?: string;
|
|
61
|
+
baseurl?: string;
|
|
62
|
+
ilink_user_id?: string;
|
|
63
|
+
};
|
|
64
|
+
type WeixinSendTypingResponse = {
|
|
65
|
+
ret?: number;
|
|
66
|
+
errcode?: number;
|
|
67
|
+
errmsg?: string;
|
|
68
|
+
};
|
|
69
|
+
type WeixinApiClient = {
|
|
70
|
+
fetchUpdates: (params: {
|
|
71
|
+
baseUrl: string;
|
|
72
|
+
token: string;
|
|
73
|
+
cursor?: string;
|
|
74
|
+
timeoutMs: number;
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
}) => Promise<WeixinGetUpdatesResponse>;
|
|
77
|
+
fetchConfig: (params: {
|
|
78
|
+
baseUrl: string;
|
|
79
|
+
token: string;
|
|
80
|
+
ilinkUserId: string;
|
|
81
|
+
contextToken: string;
|
|
82
|
+
signal?: AbortSignal;
|
|
83
|
+
}) => Promise<WeixinConfigResponse>;
|
|
84
|
+
fetchQrCode: (params: {
|
|
85
|
+
baseUrl: string;
|
|
86
|
+
botType?: string;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
}) => Promise<WeixinQrCodeResponse>;
|
|
89
|
+
fetchQrStatus: (params: {
|
|
90
|
+
baseUrl: string;
|
|
91
|
+
qrcode: string;
|
|
92
|
+
timeoutMs?: number;
|
|
93
|
+
signal?: AbortSignal;
|
|
94
|
+
}) => Promise<WeixinQrStatusResponse>;
|
|
95
|
+
sendTyping: (params: {
|
|
96
|
+
baseUrl: string;
|
|
97
|
+
token: string;
|
|
98
|
+
toUserId: string;
|
|
99
|
+
typingTicket: string;
|
|
100
|
+
status: 1 | 2;
|
|
101
|
+
signal?: AbortSignal;
|
|
102
|
+
}) => Promise<WeixinSendTypingResponse>;
|
|
103
|
+
sendTextMessage: (params: {
|
|
104
|
+
baseUrl: string;
|
|
105
|
+
token: string;
|
|
106
|
+
toUserId: string;
|
|
107
|
+
text: string;
|
|
108
|
+
contextToken?: string;
|
|
109
|
+
signal?: AbortSignal;
|
|
110
|
+
}) => Promise<{
|
|
111
|
+
messageId: string;
|
|
112
|
+
}>;
|
|
113
|
+
sendMessageItem: (params: {
|
|
114
|
+
baseUrl: string;
|
|
115
|
+
token: string;
|
|
116
|
+
toUserId: string;
|
|
117
|
+
item: WeixinMessageItem;
|
|
118
|
+
contextToken?: string;
|
|
119
|
+
signal?: AbortSignal;
|
|
120
|
+
}) => Promise<{
|
|
121
|
+
messageId: string;
|
|
122
|
+
}>;
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
export { WeixinApiClient };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { randomInt, randomUUID } from "node:crypto";
|
|
2
|
+
//#region src/services/weixin-api.service.ts
|
|
3
|
+
const WEIXIN_API_TIMEOUT_MS = 15e3;
|
|
4
|
+
const WEIXIN_MESSAGE_STATE_FINISH = 2;
|
|
5
|
+
const WEIXIN_CHANNEL_VERSION = "nextclaw-weixin/0.1.0";
|
|
6
|
+
function normalizeWeixinBaseUrl(baseUrl) {
|
|
7
|
+
return baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
8
|
+
}
|
|
9
|
+
function buildUinHeader() {
|
|
10
|
+
return Buffer.from(String(randomInt(4294967295)), "utf8").toString("base64");
|
|
11
|
+
}
|
|
12
|
+
function buildWeixinBaseInfo() {
|
|
13
|
+
return { channel_version: WEIXIN_CHANNEL_VERSION };
|
|
14
|
+
}
|
|
15
|
+
function stripSimpleMarkdown(text) {
|
|
16
|
+
return text.replace(/```[^\n]*\n?([\s\S]*?)```/g, "$1").replace(/!\[[^\]]*]\([^)]*\)/g, "").replace(/\[([^\]]+)]\([^)]*\)/g, "$1").replace(/[*_~`>#-]+/g, "").trim();
|
|
17
|
+
}
|
|
18
|
+
function assertSuccess(response, action) {
|
|
19
|
+
const failedRet = typeof response.ret === "number" && response.ret !== 0;
|
|
20
|
+
const failedErrcode = typeof response.errcode === "number" && response.errcode !== 0;
|
|
21
|
+
if (!failedRet && !failedErrcode) return;
|
|
22
|
+
const detail = [
|
|
23
|
+
response.ret === void 0 ? null : `ret=${response.ret}`,
|
|
24
|
+
response.errcode === void 0 ? null : `errcode=${response.errcode}`,
|
|
25
|
+
response.errmsg?.trim() ? `errmsg=${response.errmsg.trim()}` : null
|
|
26
|
+
].filter(Boolean).join(", ");
|
|
27
|
+
throw new Error(detail ? `weixin ${action} failed: ${detail}` : `weixin ${action} failed`);
|
|
28
|
+
}
|
|
29
|
+
async function fetchWeixinJson(params) {
|
|
30
|
+
const { body, method, signal, timeoutMs, token, url } = params;
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const timeout = setTimeout(() => controller.abort(), Math.max(1e3, timeoutMs));
|
|
33
|
+
const abort = () => controller.abort();
|
|
34
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
35
|
+
try {
|
|
36
|
+
const response = await fetch(url, {
|
|
37
|
+
method: method ?? "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
AuthorizationType: "ilink_bot_token",
|
|
41
|
+
"X-WECHAT-UIN": buildUinHeader(),
|
|
42
|
+
...token ? { Authorization: `Bearer ${token}` } : {},
|
|
43
|
+
...params.headers ?? {}
|
|
44
|
+
},
|
|
45
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
46
|
+
signal: controller.signal
|
|
47
|
+
});
|
|
48
|
+
const text = await response.text();
|
|
49
|
+
if (!response.ok) throw new Error(`weixin api ${response.status}: ${text}`);
|
|
50
|
+
return JSON.parse(text);
|
|
51
|
+
} finally {
|
|
52
|
+
clearTimeout(timeout);
|
|
53
|
+
signal?.removeEventListener("abort", abort);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
var HttpWeixinApiClient = class {
|
|
57
|
+
fetchUpdates = async (params) => {
|
|
58
|
+
const { baseUrl, cursor, signal, timeoutMs, token } = params;
|
|
59
|
+
try {
|
|
60
|
+
const response = await fetchWeixinJson({
|
|
61
|
+
url: new URL("ilink/bot/getupdates", normalizeWeixinBaseUrl(baseUrl)).toString(),
|
|
62
|
+
token,
|
|
63
|
+
timeoutMs,
|
|
64
|
+
signal,
|
|
65
|
+
body: {
|
|
66
|
+
get_updates_buf: cursor ?? "",
|
|
67
|
+
base_info: buildWeixinBaseInfo()
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
assertSuccess(response, "getupdates");
|
|
71
|
+
return response;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (error instanceof Error && error.name === "AbortError") return {
|
|
74
|
+
ret: 0,
|
|
75
|
+
msgs: [],
|
|
76
|
+
get_updates_buf: cursor
|
|
77
|
+
};
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
fetchConfig = async (params) => {
|
|
82
|
+
const { baseUrl, contextToken, ilinkUserId, signal, token } = params;
|
|
83
|
+
const response = await fetchWeixinJson({
|
|
84
|
+
url: new URL("ilink/bot/getconfig", normalizeWeixinBaseUrl(baseUrl)).toString(),
|
|
85
|
+
token,
|
|
86
|
+
timeoutMs: WEIXIN_API_TIMEOUT_MS,
|
|
87
|
+
signal,
|
|
88
|
+
body: {
|
|
89
|
+
ilink_user_id: ilinkUserId,
|
|
90
|
+
context_token: contextToken,
|
|
91
|
+
base_info: buildWeixinBaseInfo()
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
assertSuccess(response, "getconfig");
|
|
95
|
+
return response;
|
|
96
|
+
};
|
|
97
|
+
sendTyping = async (params) => {
|
|
98
|
+
const { baseUrl, signal, status, toUserId, token, typingTicket } = params;
|
|
99
|
+
const response = await fetchWeixinJson({
|
|
100
|
+
url: new URL("ilink/bot/sendtyping", normalizeWeixinBaseUrl(baseUrl)).toString(),
|
|
101
|
+
token,
|
|
102
|
+
timeoutMs: WEIXIN_API_TIMEOUT_MS,
|
|
103
|
+
signal,
|
|
104
|
+
body: {
|
|
105
|
+
ilink_user_id: toUserId,
|
|
106
|
+
typing_ticket: typingTicket,
|
|
107
|
+
status,
|
|
108
|
+
base_info: buildWeixinBaseInfo()
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
assertSuccess(response, "sendtyping");
|
|
112
|
+
return response;
|
|
113
|
+
};
|
|
114
|
+
fetchQrCode = async (params) => {
|
|
115
|
+
const { baseUrl, botType, signal } = params;
|
|
116
|
+
return await fetchWeixinJson({
|
|
117
|
+
url: new URL(`ilink/bot/get_bot_qrcode?bot_type=${encodeURIComponent(botType ?? "3")}`, normalizeWeixinBaseUrl(baseUrl)).toString(),
|
|
118
|
+
method: "GET",
|
|
119
|
+
timeoutMs: WEIXIN_API_TIMEOUT_MS,
|
|
120
|
+
signal
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
fetchQrStatus = async (params) => {
|
|
124
|
+
const { baseUrl, qrcode, signal, timeoutMs } = params;
|
|
125
|
+
const url = new URL(`ilink/bot/get_qrcode_status?qrcode=${encodeURIComponent(qrcode)}`, normalizeWeixinBaseUrl(baseUrl));
|
|
126
|
+
try {
|
|
127
|
+
return await fetchWeixinJson({
|
|
128
|
+
url: url.toString(),
|
|
129
|
+
method: "GET",
|
|
130
|
+
timeoutMs: timeoutMs ?? 15e3,
|
|
131
|
+
headers: { "iLink-App-ClientVersion": "1" },
|
|
132
|
+
signal
|
|
133
|
+
});
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof Error && error.name === "AbortError") return { status: "wait" };
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
sendMessageItem = async (params) => {
|
|
140
|
+
const { baseUrl, contextToken, item, signal, token, toUserId } = params;
|
|
141
|
+
const clientMessageId = randomUUID();
|
|
142
|
+
const response = await fetchWeixinJson({
|
|
143
|
+
url: new URL("ilink/bot/sendmessage", normalizeWeixinBaseUrl(baseUrl)).toString(),
|
|
144
|
+
token,
|
|
145
|
+
timeoutMs: WEIXIN_API_TIMEOUT_MS,
|
|
146
|
+
signal,
|
|
147
|
+
body: {
|
|
148
|
+
msg: {
|
|
149
|
+
from_user_id: "",
|
|
150
|
+
to_user_id: toUserId,
|
|
151
|
+
client_id: clientMessageId,
|
|
152
|
+
message_type: 2,
|
|
153
|
+
message_state: WEIXIN_MESSAGE_STATE_FINISH,
|
|
154
|
+
item_list: [item],
|
|
155
|
+
context_token: contextToken ?? ""
|
|
156
|
+
},
|
|
157
|
+
base_info: buildWeixinBaseInfo()
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
assertSuccess(response, "sendmessage");
|
|
161
|
+
return { messageId: typeof response.message_id === "string" ? response.message_id : typeof response.message_id === "number" ? String(response.message_id) : clientMessageId };
|
|
162
|
+
};
|
|
163
|
+
sendTextMessage = async (params) => {
|
|
164
|
+
const { baseUrl, contextToken, signal, text, token, toUserId } = params;
|
|
165
|
+
return await this.sendMessageItem({
|
|
166
|
+
baseUrl,
|
|
167
|
+
token,
|
|
168
|
+
toUserId,
|
|
169
|
+
contextToken,
|
|
170
|
+
signal,
|
|
171
|
+
item: {
|
|
172
|
+
type: 1,
|
|
173
|
+
text_item: { text: stripSimpleMarkdown(text) }
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
async function sendWeixinMessageItem(params) {
|
|
179
|
+
return await new HttpWeixinApiClient().sendMessageItem(params);
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
export { HttpWeixinApiClient, WEIXIN_API_TIMEOUT_MS, buildWeixinBaseInfo, fetchWeixinJson, normalizeWeixinBaseUrl, sendWeixinMessageItem };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { WeixinLoginService } from "./weixin-login.service.js";
|
|
2
|
+
import { ExtensionCapabilityPayload, ExtensionChannel } from "@nextclaw/extension-sdk";
|
|
3
|
+
|
|
4
|
+
//#region src/services/weixin-auth-capability.service.d.ts
|
|
5
|
+
type WeixinAuthCapabilityDeps = {
|
|
6
|
+
channel: ExtensionChannel;
|
|
7
|
+
loginService?: Pick<WeixinLoginService, "start" | "poll" | "login">;
|
|
8
|
+
};
|
|
9
|
+
declare class WeixinAuthCapability {
|
|
10
|
+
private readonly channel;
|
|
11
|
+
private readonly loginService;
|
|
12
|
+
constructor(deps: WeixinAuthCapabilityDeps);
|
|
13
|
+
readonly start: (request: ExtensionCapabilityPayload) => Promise<unknown>;
|
|
14
|
+
readonly poll: (request: ExtensionCapabilityPayload) => Promise<unknown>;
|
|
15
|
+
readonly login: (request: ExtensionCapabilityPayload) => Promise<unknown>;
|
|
16
|
+
private readonly readCurrentConfig;
|
|
17
|
+
private readonly readString;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { WeixinAuthCapability };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { WeixinLoginService } from "./weixin-login.service.js";
|
|
2
|
+
//#region src/services/weixin-auth-capability.service.ts
|
|
3
|
+
var WeixinAuthCapability = class {
|
|
4
|
+
channel;
|
|
5
|
+
loginService;
|
|
6
|
+
constructor(deps) {
|
|
7
|
+
this.channel = deps.channel;
|
|
8
|
+
this.loginService = deps.loginService ?? new WeixinLoginService();
|
|
9
|
+
}
|
|
10
|
+
start = async (request) => await this.loginService.start({
|
|
11
|
+
pluginConfig: await this.readCurrentConfig(),
|
|
12
|
+
requestedAccountId: this.readString(request.accountId),
|
|
13
|
+
baseUrl: this.readString(request.baseUrl)
|
|
14
|
+
});
|
|
15
|
+
poll = async (request) => {
|
|
16
|
+
const sessionId = this.readString(request.sessionId);
|
|
17
|
+
if (!sessionId) throw new Error("sessionId is required");
|
|
18
|
+
return await this.loginService.poll({ sessionId });
|
|
19
|
+
};
|
|
20
|
+
login = async (request) => await this.loginService.login({
|
|
21
|
+
pluginConfig: await this.readCurrentConfig(),
|
|
22
|
+
requestedAccountId: this.readString(request.accountId),
|
|
23
|
+
baseUrl: this.readString(request.baseUrl),
|
|
24
|
+
verbose: request.verbose === true
|
|
25
|
+
});
|
|
26
|
+
readCurrentConfig = async () => {
|
|
27
|
+
const config = await this.channel.config.get();
|
|
28
|
+
return config && typeof config === "object" && !Array.isArray(config) ? config : {};
|
|
29
|
+
};
|
|
30
|
+
readString = (value) => typeof value === "string" && value.trim() ? value.trim() : null;
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
export { WeixinAuthCapability };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { WeixinAccountStore } from "../stores/weixin-account.store.js";
|
|
2
|
+
import { WeixinApiClient } from "./weixin-api.service.js";
|
|
3
|
+
import { WeixinChannelConfig, WeixinInboundMessage, WeixinRuntimeAccount } from "../types/weixin-extension.types.js";
|
|
4
|
+
import { NcpEndpointEvent } from "@nextclaw/ncp";
|
|
5
|
+
|
|
6
|
+
//#region src/services/weixin-channel-adapter.service.d.ts
|
|
7
|
+
type WeixinAdapterDeps = {
|
|
8
|
+
api?: WeixinApiClient;
|
|
9
|
+
store?: WeixinAccountStore;
|
|
10
|
+
sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
11
|
+
logger?: Pick<typeof console, "warn">;
|
|
12
|
+
};
|
|
13
|
+
declare class WeixinChannelAdapter {
|
|
14
|
+
private messageHandler;
|
|
15
|
+
private readonly api;
|
|
16
|
+
private readonly store;
|
|
17
|
+
private readonly sleep;
|
|
18
|
+
private readonly logger;
|
|
19
|
+
private readonly replyConsumer;
|
|
20
|
+
private readonly typingController;
|
|
21
|
+
private readonly contextTokens;
|
|
22
|
+
private readonly accountControllers;
|
|
23
|
+
private readonly pollTasks;
|
|
24
|
+
private readonly replySessions;
|
|
25
|
+
private readonly canonicalRoutes;
|
|
26
|
+
private running;
|
|
27
|
+
private config;
|
|
28
|
+
constructor(deps?: WeixinAdapterDeps);
|
|
29
|
+
readonly configure: (config: WeixinChannelConfig) => Promise<void>;
|
|
30
|
+
readonly start: () => Promise<void>;
|
|
31
|
+
readonly stop: () => Promise<void>;
|
|
32
|
+
readonly onMessage: (handler: (message: WeixinInboundMessage) => void | Promise<void>) => (() => void);
|
|
33
|
+
readonly sendNcpEvent: (event: NcpEndpointEvent) => Promise<void>;
|
|
34
|
+
readonly emitMessageForTest: (message: WeixinInboundMessage) => Promise<void>;
|
|
35
|
+
readonly sendText: (params: {
|
|
36
|
+
account: WeixinRuntimeAccount;
|
|
37
|
+
conversationId: string;
|
|
38
|
+
text: string;
|
|
39
|
+
contextToken?: string;
|
|
40
|
+
}) => Promise<void>;
|
|
41
|
+
private readonly resolveReplySession;
|
|
42
|
+
private readonly listAvailableAccountIds;
|
|
43
|
+
private readonly resolveCanonicalRoute;
|
|
44
|
+
private readonly resolveConfiguredCanonicalRoute;
|
|
45
|
+
private readonly resolveRuntimeAccount;
|
|
46
|
+
private readonly resolveSelectedAccountId;
|
|
47
|
+
private readonly resolveSendAccount;
|
|
48
|
+
private readonly resolveContextToken;
|
|
49
|
+
private readonly runPollingLoop;
|
|
50
|
+
private readonly handleInboundMessage;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { WeixinChannelAdapter };
|