@zhin.js/adapter-milky 1.1.26 → 1.1.27
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 +146 -0
- package/LICENSE +21 -0
- package/PERMITS.md +3 -3
- package/README.md +6 -3
- package/adapters/milky/index.js +3 -0
- package/adapters/milky/index.ts +3 -0
- package/lib/endpoint-management.js +37 -0
- package/lib/milky-auth.d.ts +2 -2
- package/lib/milky-auth.js +3 -12
- package/lib/protocol.d.ts +6 -1
- package/lib/protocol.js +10 -0
- package/lib/sse-client.d.ts +6 -0
- package/lib/sse-client.js +41 -2
- package/lib/sse-endpoint.js +3 -2
- package/lib/webhook-endpoint.js +13 -17
- package/lib/ws-endpoint.js +3 -2
- package/lib/ws-types.d.ts +6 -0
- package/lib/ws-types.js +31 -1
- package/lib/wss-endpoint.d.ts +1 -1
- package/lib/wss-endpoint.js +21 -17
- package/package.json +17 -33
- package/schema.json +0 -56
- package/src/endpoint-management.ts +33 -0
- package/src/milky-auth.ts +4 -11
- package/src/protocol.ts +11 -1
- package/src/sse-client.ts +45 -2
- package/src/sse-endpoint.ts +3 -1
- package/src/webhook-endpoint.ts +10 -17
- package/src/ws-endpoint.ts +3 -1
- package/src/ws-types.ts +33 -0
- package/src/wss-endpoint.ts +21 -16
package/src/wss-endpoint.ts
CHANGED
|
@@ -11,10 +11,9 @@ import {
|
|
|
11
11
|
type EndpointManagement,
|
|
12
12
|
type EndpointSendRequest,
|
|
13
13
|
} from 'zhin.js/adapter';
|
|
14
|
-
import type
|
|
14
|
+
import { verifyProtocolAccessToken, type HttpHost, type WsConnection } from '@zhin.js/host-http';
|
|
15
15
|
import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
|
|
16
16
|
import type { CapabilityId } from 'zhin.js';
|
|
17
|
-
import { verifyMilkyAccessToken } from './milky-auth.js';
|
|
18
17
|
import { createMilkyEndpointManagement } from './endpoint-management.js';
|
|
19
18
|
import { createMilkyActionCaller, readMilkyResponse, type MilkyActionCaller, milkyClientOptions, forwardMilkyClientEvents, MilkyClient } from './client.js';
|
|
20
19
|
import {
|
|
@@ -28,6 +27,7 @@ import {
|
|
|
28
27
|
formatOutboundSegments,
|
|
29
28
|
isMentioned,
|
|
30
29
|
milkyInboundConversation,
|
|
30
|
+
milkySenderStanding,
|
|
31
31
|
parseMessageReceiveData,
|
|
32
32
|
parseMilkyMessageId,
|
|
33
33
|
senderNickname,
|
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
type MilkyIncomingMessage,
|
|
36
36
|
type MilkyWssConfig,
|
|
37
37
|
} from './protocol.js';
|
|
38
|
-
import type
|
|
38
|
+
import { socketOfConnection, type MilkyWsSocket } from './ws-types.js';
|
|
39
39
|
|
|
40
40
|
const WS_OPEN = 1;
|
|
41
41
|
|
|
@@ -99,7 +99,17 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
99
99
|
async #start(): Promise<void> {
|
|
100
100
|
if (this.#started) return;
|
|
101
101
|
this.#started = true;
|
|
102
|
-
|
|
102
|
+
if (!this.#options.config.access_token) {
|
|
103
|
+
// 未配 access_token 时只接受本机回环连接(verifyProtocolAccessToken)。
|
|
104
|
+
this.#logger.warn(formatCompact({
|
|
105
|
+
endpoint: this.#options.config.id,
|
|
106
|
+
mode: 'wss',
|
|
107
|
+
ok: false,
|
|
108
|
+
error: 'missing access_token: only loopback peers are accepted',
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
// The peer authenticates with this endpoint's own access_token, not a Console credential.
|
|
112
|
+
const handle = this.#options.http.ws(this.#options.config.path, { auth: 'protocol' });
|
|
103
113
|
this.#wsRelease = handle.onConnection((connection) => {
|
|
104
114
|
this.#connectionTask = this.#connectionTask
|
|
105
115
|
.then(() => this.#acceptConnection(connection))
|
|
@@ -177,6 +187,7 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
177
187
|
const segments = formatInboundSegments(data);
|
|
178
188
|
const audioUrl = extractInboundAudioUrl(data);
|
|
179
189
|
const nickname = senderNickname(data);
|
|
190
|
+
const platform = milkySenderStanding(data);
|
|
180
191
|
const mentioned = isMentioned(data, event.self_id);
|
|
181
192
|
void this[endpointSpec].events.message({
|
|
182
193
|
event,
|
|
@@ -185,7 +196,7 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
185
196
|
message: { conversation, id: formatInboundMessageId(data) },
|
|
186
197
|
content,
|
|
187
198
|
segments,
|
|
188
|
-
sender: { id: String(data.sender_id) },
|
|
199
|
+
sender: { id: String(data.sender_id), ...(platform ? { platform } : {}) },
|
|
189
200
|
endpointId: this.#options.config.id,
|
|
190
201
|
...(mentioned ? { mentioned: true } : {}),
|
|
191
202
|
metadata: Object.freeze({
|
|
@@ -207,11 +218,11 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
207
218
|
}
|
|
208
219
|
|
|
209
220
|
async #acceptConnection(connection: WsConnection): Promise<void> {
|
|
210
|
-
if (!
|
|
211
|
-
connection.
|
|
221
|
+
if (!verifyProtocolAccessToken(this.#options.config.access_token, connection)) {
|
|
222
|
+
connection.close(4003, 'Unauthorized');
|
|
212
223
|
return;
|
|
213
224
|
}
|
|
214
|
-
const socket = connection
|
|
225
|
+
const socket = socketOfConnection(connection);
|
|
215
226
|
await this.#connectionLifecycle.stop();
|
|
216
227
|
await this.#connectionLifecycle.start(async (lifecycleHandle) => {
|
|
217
228
|
this.#ws = socket;
|
|
@@ -229,13 +240,7 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
229
240
|
}
|
|
230
241
|
if (this.#ws === socket) this.#ws = undefined;
|
|
231
242
|
});
|
|
232
|
-
|
|
233
|
-
try {
|
|
234
|
-
if (socket.readyState === WS_OPEN) socket.ping?.();
|
|
235
|
-
} catch {
|
|
236
|
-
/* ignore */
|
|
237
|
-
}
|
|
238
|
-
}, this.#options.config.heartbeat_interval);
|
|
243
|
+
// Dead-peer detection is the Host transport's keepalive (protocol ping / pong).
|
|
239
244
|
socket.on('message', () => {
|
|
240
245
|
this.#connectionLifecycle.notifyHeartbeatAck();
|
|
241
246
|
});
|
|
@@ -251,7 +256,7 @@ export class MilkyWssEndpoint extends MilkyClient implements Endpoint {
|
|
|
251
256
|
this.#logger.debug(formatCompact({
|
|
252
257
|
endpoint: this.#options.config.id,
|
|
253
258
|
mode: 'wss',
|
|
254
|
-
peer: connection.
|
|
259
|
+
peer: connection.remoteAddress,
|
|
255
260
|
}));
|
|
256
261
|
}
|
|
257
262
|
|