@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.
@@ -11,10 +11,9 @@ import {
11
11
  type EndpointManagement,
12
12
  type EndpointSendRequest,
13
13
  } from 'zhin.js/adapter';
14
- import type { HttpHost, WsConnection } from '@zhin.js/host-http';
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 { MilkyWsSocket } from './ws-types.js';
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
- const handle = this.#options.http.ws(this.#options.config.path);
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 (!verifyMilkyAccessToken(this.#options.config.access_token, connection.request)) {
211
- connection.socket.close(4003, 'Unauthorized');
221
+ if (!verifyProtocolAccessToken(this.#options.config.access_token, connection)) {
222
+ connection.close(4003, 'Unauthorized');
212
223
  return;
213
224
  }
214
- const socket = connection.socket as unknown as MilkyWsSocket;
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
- this.#connectionLifecycle.startHeartbeat(() => {
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.request.socket.remoteAddress,
259
+ peer: connection.remoteAddress,
255
260
  }));
256
261
  }
257
262