@zhin.js/adapter-onebot12 5.0.1 → 5.0.4
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/CHANGELOG.md +55 -0
- package/README.md +3 -3
- package/adapters/onebot12.js +2 -2
- package/adapters/onebot12.ts +2 -2
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/protocol.d.ts +13 -12
- package/lib/protocol.js +41 -43
- package/lib/webhook.d.ts +2 -5
- package/lib/webhook.js +18 -17
- package/lib/ws-endpoint.d.ts +2 -5
- package/lib/ws-endpoint.js +28 -26
- package/lib/wss-endpoint.d.ts +2 -5
- package/lib/wss-endpoint.js +25 -24
- package/package.json +13 -12
- package/schema.json +41 -5
- package/src/index.ts +1 -3
- package/src/protocol.ts +43 -51
- package/src/webhook.ts +19 -18
- package/src/ws-endpoint.ts +30 -27
- package/src/wss-endpoint.ts +27 -26
- /package/commands/endpoint/add/{[name:string].js → [id].js} +0 -0
- /package/commands/endpoint/add/{[name:string].ts → [id].ts} +0 -0
- /package/commands/endpoint/remove/{[name:string].js → [id].js} +0 -0
- /package/commands/endpoint/remove/{[name:string].ts → [id].ts} +0 -0
package/src/ws-endpoint.ts
CHANGED
|
@@ -9,19 +9,20 @@ import {
|
|
|
9
9
|
type EndpointInstance,
|
|
10
10
|
type EndpointLifecycle,
|
|
11
11
|
type EndpointManagement,
|
|
12
|
+
type EndpointSendRequest,
|
|
12
13
|
} from '@zhin.js/adapter';
|
|
13
14
|
import type { MessageGateway } from '@zhin.js/core/runtime';
|
|
14
|
-
import { formatCompact,
|
|
15
|
+
import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
|
|
15
16
|
import type { CapabilityId } from '@zhin.js/plugin-runtime';
|
|
16
17
|
import { createOneBot12EndpointManagement } from './endpoint-management.js';
|
|
17
18
|
import {
|
|
18
19
|
buildSendMessageParams,
|
|
19
20
|
buildWsConnectOptions,
|
|
20
21
|
formatInboundContent,
|
|
21
|
-
formatInboundTarget,
|
|
22
22
|
formatOutboundSegments,
|
|
23
23
|
isBotMentioned,
|
|
24
24
|
isMessageEvent,
|
|
25
|
+
onebot12InboundConversation,
|
|
25
26
|
senderNickname,
|
|
26
27
|
senderUserId,
|
|
27
28
|
uploadOneBot12MediaSegments,
|
|
@@ -36,8 +37,6 @@ import {
|
|
|
36
37
|
WS_OPEN,
|
|
37
38
|
} from './ws-types.js';
|
|
38
39
|
|
|
39
|
-
const logger = getLogger('onebot12');
|
|
40
|
-
|
|
41
40
|
export interface OneBot12WsEndpointOptions {
|
|
42
41
|
readonly id: CapabilityId;
|
|
43
42
|
readonly gateway: MessageGateway;
|
|
@@ -49,6 +48,8 @@ export interface OneBot12WsEndpointOptions {
|
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
export class OneBot12WsEndpoint implements EndpointInstance {
|
|
51
|
+
readonly #logger!: ReturnType<typeof getAdapterLogger>;
|
|
52
|
+
|
|
52
53
|
readonly #options: OneBot12WsEndpointOptions;
|
|
53
54
|
readonly management: EndpointManagement = createOneBot12EndpointManagement(this);
|
|
54
55
|
readonly #lifecycle: EndpointLifecycle;
|
|
@@ -62,10 +63,11 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
62
63
|
#open = false;
|
|
63
64
|
|
|
64
65
|
constructor(options: OneBot12WsEndpointOptions) {
|
|
66
|
+
this.#logger = getAdapterLogger('onebot12', options.config.id);
|
|
65
67
|
this.#options = options;
|
|
66
68
|
const { config } = options;
|
|
67
69
|
this.#lifecycle = createEndpointLifecycle({
|
|
68
|
-
name: config.
|
|
70
|
+
name: config.id,
|
|
69
71
|
reconnect: {
|
|
70
72
|
initialIntervalMs: config.reconnect_interval,
|
|
71
73
|
// 固定间隔(multiplier 1、无抖动),对齐旧 reconnect_interval 语义
|
|
@@ -115,26 +117,27 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
115
117
|
this.#ws = undefined;
|
|
116
118
|
}
|
|
117
119
|
|
|
118
|
-
async send({
|
|
120
|
+
async send({ conversation, payload }: EndpointSendRequest): Promise<string> {
|
|
119
121
|
const materialized = await uploadOneBot12MediaSegments(
|
|
120
122
|
payload,
|
|
121
123
|
(action, params) => this.callApi(action, params),
|
|
122
124
|
(error) => {
|
|
123
|
-
logger.warn(formatCompact({
|
|
125
|
+
this.#logger.warn(formatCompact({
|
|
124
126
|
op: 'onebot12_upload_failed',
|
|
125
|
-
endpoint: this.#options.config.
|
|
127
|
+
endpoint: this.#options.config.id,
|
|
126
128
|
error: error instanceof Error ? error.message : String(error),
|
|
127
129
|
}));
|
|
128
130
|
},
|
|
129
131
|
);
|
|
130
132
|
const message = formatOutboundSegments(materialized);
|
|
131
|
-
const params = buildSendMessageParams(
|
|
133
|
+
const params = buildSendMessageParams(conversation, message);
|
|
132
134
|
const data = await this.#callAction('send_message', params) as { message_id?: string } | undefined;
|
|
133
135
|
const messageId = data?.message_id ?? '';
|
|
134
|
-
logger.debug(formatCompact({
|
|
136
|
+
this.#logger.debug(formatCompact({
|
|
135
137
|
op: 'onebot12_send',
|
|
136
|
-
endpoint: this.#options.config.
|
|
137
|
-
|
|
138
|
+
endpoint: this.#options.config.id,
|
|
139
|
+
kind: conversation.kind,
|
|
140
|
+
conversationId: conversation.id,
|
|
138
141
|
messageId,
|
|
139
142
|
}));
|
|
140
143
|
return messageId;
|
|
@@ -153,31 +156,31 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
153
156
|
/** Test / internal: admit a parsed event when the endpoint is open. */
|
|
154
157
|
admit(ev: OneBot12Event): void {
|
|
155
158
|
if (!this.#open || !isMessageEvent(ev)) return;
|
|
156
|
-
const
|
|
159
|
+
const conversation = onebot12InboundConversation(String(this.#options.id), ev);
|
|
157
160
|
const content = formatInboundContent(ev);
|
|
158
161
|
const nickname = senderNickname(ev);
|
|
159
162
|
const mentioned = isBotMentioned(ev);
|
|
160
163
|
void this.#options.gateway.receive({
|
|
161
|
-
|
|
162
|
-
|
|
164
|
+
conversation,
|
|
165
|
+
message: { conversation, id: ev.message_id },
|
|
163
166
|
content,
|
|
164
|
-
sender: senderUserId(ev),
|
|
165
|
-
|
|
167
|
+
sender: { id: senderUserId(ev), name: senderNickname(ev) },
|
|
168
|
+
endpointId: this.#options.config.id,
|
|
169
|
+
...(mentioned ? { mentioned: true } : {}),
|
|
166
170
|
metadata: Object.freeze({
|
|
167
171
|
detail_type: ev.detail_type,
|
|
168
172
|
user_id: ev.user_id,
|
|
169
173
|
group_id: ev.group_id,
|
|
170
174
|
channel_id: ev.channel_id,
|
|
171
175
|
guild_id: ev.guild_id,
|
|
172
|
-
endpoint: this.#options.config.name,
|
|
173
176
|
time: ev.time,
|
|
174
177
|
...(nickname ? { nickname } : {}),
|
|
175
|
-
...(mentioned ? { mentioned: true } : {}),
|
|
176
178
|
}),
|
|
177
179
|
}).catch((err) => {
|
|
178
|
-
logger.warn(formatCompact({
|
|
180
|
+
this.#logger.warn(formatCompact({
|
|
179
181
|
op: 'onebot12_gateway_receive_failed',
|
|
180
|
-
|
|
182
|
+
kind: conversation.kind,
|
|
183
|
+
conversationId: conversation.id,
|
|
181
184
|
error: err instanceof Error ? err.message : String(err),
|
|
182
185
|
}));
|
|
183
186
|
});
|
|
@@ -204,8 +207,8 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
204
207
|
ws.on('open', () => {
|
|
205
208
|
if (settled) return;
|
|
206
209
|
settled = true;
|
|
207
|
-
logger.debug(formatCompact({
|
|
208
|
-
endpoint: this.#options.config.
|
|
210
|
+
this.#logger.debug(formatCompact({
|
|
211
|
+
endpoint: this.#options.config.id,
|
|
209
212
|
mode: 'ws',
|
|
210
213
|
url: safeUrl,
|
|
211
214
|
}));
|
|
@@ -237,9 +240,9 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
237
240
|
|
|
238
241
|
ws.on('error', (err) => {
|
|
239
242
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
240
|
-
logger.warn(formatCompact({
|
|
243
|
+
this.#logger.warn(formatCompact({
|
|
241
244
|
op: 'ws_error',
|
|
242
|
-
endpoint: this.#options.config.
|
|
245
|
+
endpoint: this.#options.config.id,
|
|
243
246
|
ok: false,
|
|
244
247
|
error: error.message,
|
|
245
248
|
}));
|
|
@@ -274,9 +277,9 @@ export class OneBot12WsEndpoint implements EndpointInstance {
|
|
|
274
277
|
}
|
|
275
278
|
this.admit(msg as OneBot12Event);
|
|
276
279
|
} catch (error) {
|
|
277
|
-
logger.warn(formatCompact({
|
|
280
|
+
this.#logger.warn(formatCompact({
|
|
278
281
|
op: 'onebot12_parse_failed',
|
|
279
|
-
endpoint: this.#options.config.
|
|
282
|
+
endpoint: this.#options.config.id,
|
|
280
283
|
error: error instanceof Error ? error.message : String(error),
|
|
281
284
|
}));
|
|
282
285
|
}
|
package/src/wss-endpoint.ts
CHANGED
|
@@ -2,19 +2,19 @@
|
|
|
2
2
|
* OneBot12 reverse WSS endpoint — accepts inbound WebSocket from OneBot implementation.
|
|
3
3
|
*/
|
|
4
4
|
import { clearInterval } from 'node:timers';
|
|
5
|
-
import type { EndpointInstance, EndpointManagement } from '@zhin.js/adapter';
|
|
5
|
+
import type { EndpointInstance, EndpointManagement, EndpointSendRequest } from '@zhin.js/adapter';
|
|
6
6
|
import type { MessageGateway } from '@zhin.js/core/runtime';
|
|
7
7
|
import type { HttpHost, WsConnection } from '@zhin.js/host-http';
|
|
8
|
-
import { formatCompact,
|
|
8
|
+
import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
|
|
9
9
|
import type { CapabilityId } from '@zhin.js/plugin-runtime';
|
|
10
10
|
import { createOneBot12EndpointManagement } from './endpoint-management.js';
|
|
11
11
|
import {
|
|
12
12
|
buildSendMessageParams,
|
|
13
13
|
formatInboundContent,
|
|
14
|
-
formatInboundTarget,
|
|
15
14
|
formatOutboundSegments,
|
|
16
15
|
isBotMentioned,
|
|
17
16
|
isMessageEvent,
|
|
17
|
+
onebot12InboundConversation,
|
|
18
18
|
senderNickname,
|
|
19
19
|
senderUserId,
|
|
20
20
|
uploadOneBot12MediaSegments,
|
|
@@ -26,8 +26,6 @@ import {
|
|
|
26
26
|
import { verifyOneBotAccessToken } from './wss-auth.js';
|
|
27
27
|
import { type OneBot12WsSocket, WS_OPEN } from './ws-types.js';
|
|
28
28
|
|
|
29
|
-
const logger = getLogger('onebot12');
|
|
30
|
-
|
|
31
29
|
export interface OneBot12WssEndpointOptions {
|
|
32
30
|
readonly id: CapabilityId;
|
|
33
31
|
readonly gateway: MessageGateway;
|
|
@@ -36,6 +34,8 @@ export interface OneBot12WssEndpointOptions {
|
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
export class OneBot12WssEndpoint implements EndpointInstance {
|
|
37
|
+
readonly #logger!: ReturnType<typeof getAdapterLogger>;
|
|
38
|
+
|
|
39
39
|
readonly #options: OneBot12WssEndpointOptions;
|
|
40
40
|
readonly management: EndpointManagement = createOneBot12EndpointManagement(this);
|
|
41
41
|
#ws?: OneBot12WsSocket;
|
|
@@ -51,6 +51,7 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
51
51
|
#started = false;
|
|
52
52
|
|
|
53
53
|
constructor(options: OneBot12WssEndpointOptions) {
|
|
54
|
+
this.#logger = getAdapterLogger('onebot12', options.config.id);
|
|
54
55
|
this.#options = options;
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -59,8 +60,8 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
59
60
|
this.#started = true;
|
|
60
61
|
if (!this.#options.config.access_token) {
|
|
61
62
|
// wss 模式未配 access_token 时任何连接都会被放行(verifyOneBotAccessToken 直接 return true)
|
|
62
|
-
logger.warn(formatCompact({
|
|
63
|
-
endpoint: this.#options.config.
|
|
63
|
+
this.#logger.warn(formatCompact({
|
|
64
|
+
endpoint: this.#options.config.id,
|
|
64
65
|
mode: 'wss',
|
|
65
66
|
ok: false,
|
|
66
67
|
error: 'missing access_token',
|
|
@@ -70,9 +71,9 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
70
71
|
this.#wsRelease = handle.onConnection((connection) => {
|
|
71
72
|
this.#acceptConnection(connection);
|
|
72
73
|
});
|
|
73
|
-
logger.info(formatCompact({
|
|
74
|
+
this.#logger.info(formatCompact({
|
|
74
75
|
op: 'listen',
|
|
75
|
-
endpoint: this.#options.config.
|
|
76
|
+
endpoint: this.#options.config.id,
|
|
76
77
|
mode: 'wss',
|
|
77
78
|
path: this.#options.config.path,
|
|
78
79
|
}));
|
|
@@ -110,20 +111,20 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
110
111
|
this.#started = false;
|
|
111
112
|
}
|
|
112
113
|
|
|
113
|
-
async send({
|
|
114
|
+
async send({ conversation, payload }: EndpointSendRequest): Promise<string> {
|
|
114
115
|
const materialized = await uploadOneBot12MediaSegments(
|
|
115
116
|
payload,
|
|
116
117
|
(action, params) => this.callApi(action, params),
|
|
117
118
|
(error) => {
|
|
118
|
-
logger.warn(formatCompact({
|
|
119
|
+
this.#logger.warn(formatCompact({
|
|
119
120
|
op: 'onebot12_upload_failed',
|
|
120
|
-
endpoint: this.#options.config.
|
|
121
|
+
endpoint: this.#options.config.id,
|
|
121
122
|
error: error instanceof Error ? error.message : String(error),
|
|
122
123
|
}));
|
|
123
124
|
},
|
|
124
125
|
);
|
|
125
126
|
const message = formatOutboundSegments(materialized);
|
|
126
|
-
const params = buildSendMessageParams(
|
|
127
|
+
const params = buildSendMessageParams(conversation, message);
|
|
127
128
|
const data = await this.#callAction('send_message', params) as { message_id?: string } | undefined;
|
|
128
129
|
return data?.message_id ?? '';
|
|
129
130
|
}
|
|
@@ -140,30 +141,30 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
140
141
|
|
|
141
142
|
admit(ev: OneBot12Event): void {
|
|
142
143
|
if (!this.#open || !isMessageEvent(ev)) return;
|
|
143
|
-
const
|
|
144
|
+
const conversation = onebot12InboundConversation(String(this.#options.id), ev);
|
|
144
145
|
const nickname = senderNickname(ev);
|
|
145
146
|
const mentioned = isBotMentioned(ev);
|
|
146
147
|
void this.#options.gateway.receive({
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
conversation,
|
|
149
|
+
message: { conversation, id: ev.message_id },
|
|
149
150
|
content: formatInboundContent(ev),
|
|
150
|
-
sender: senderUserId(ev),
|
|
151
|
-
|
|
151
|
+
sender: { id: senderUserId(ev), name: senderNickname(ev) },
|
|
152
|
+
endpointId: this.#options.config.id,
|
|
153
|
+
...(mentioned ? { mentioned: true } : {}),
|
|
152
154
|
metadata: Object.freeze({
|
|
153
155
|
detail_type: ev.detail_type,
|
|
154
156
|
user_id: ev.user_id,
|
|
155
157
|
group_id: ev.group_id,
|
|
156
158
|
channel_id: ev.channel_id,
|
|
157
159
|
guild_id: ev.guild_id,
|
|
158
|
-
endpoint: this.#options.config.name,
|
|
159
160
|
time: ev.time,
|
|
160
161
|
...(nickname ? { nickname } : {}),
|
|
161
|
-
...(mentioned ? { mentioned: true } : {}),
|
|
162
162
|
}),
|
|
163
163
|
}).catch((err) => {
|
|
164
|
-
logger.warn(formatCompact({
|
|
164
|
+
this.#logger.warn(formatCompact({
|
|
165
165
|
op: 'onebot12_gateway_receive_failed',
|
|
166
|
-
|
|
166
|
+
kind: conversation.kind,
|
|
167
|
+
conversationId: conversation.id,
|
|
167
168
|
error: err instanceof Error ? err.message : String(err),
|
|
168
169
|
}));
|
|
169
170
|
});
|
|
@@ -196,8 +197,8 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
196
197
|
}
|
|
197
198
|
}
|
|
198
199
|
});
|
|
199
|
-
logger.debug(formatCompact({
|
|
200
|
-
endpoint: this.#options.config.
|
|
200
|
+
this.#logger.debug(formatCompact({
|
|
201
|
+
endpoint: this.#options.config.id,
|
|
201
202
|
mode: 'wss',
|
|
202
203
|
peer: connection.request.socket.remoteAddress,
|
|
203
204
|
}));
|
|
@@ -226,9 +227,9 @@ export class OneBot12WssEndpoint implements EndpointInstance {
|
|
|
226
227
|
}
|
|
227
228
|
this.admit(msg as OneBot12Event);
|
|
228
229
|
} catch (error) {
|
|
229
|
-
logger.warn(formatCompact({
|
|
230
|
+
this.#logger.warn(formatCompact({
|
|
230
231
|
op: 'onebot12_parse_failed',
|
|
231
|
-
endpoint: this.#options.config.
|
|
232
|
+
endpoint: this.#options.config.id,
|
|
232
233
|
error: error instanceof Error ? error.message : String(error),
|
|
233
234
|
}));
|
|
234
235
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|