@zhin.js/adapter-milky 6.0.3 → 6.0.5

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.
@@ -9,7 +9,7 @@ import type {
9
9
  } from '@zhin.js/adapter';
10
10
  import type { MessageGateway } from '@zhin.js/core/runtime';
11
11
  import type { HttpHost, WsConnection } from '@zhin.js/host-http';
12
- import { formatCompact, getLogger } from '@zhin.js/logger';
12
+ import { formatCompact, getAdapterLogger } from '@zhin.js/logger';
13
13
  import type { CapabilityId } from '@zhin.js/plugin-runtime';
14
14
  import { verifyMilkyAccessToken } from './milky-auth.js';
15
15
  import { createMilkyEndpointManagement } from './endpoint-management.js';
@@ -34,7 +34,6 @@ import {
34
34
  } from './protocol.js';
35
35
  import type { MilkyWsSocket } from './ws-types.js';
36
36
 
37
- const logger = getLogger('milky');
38
37
  const WS_OPEN = 1;
39
38
 
40
39
  export interface MilkyWssEndpointOptions {
@@ -46,6 +45,8 @@ export interface MilkyWssEndpointOptions {
46
45
  }
47
46
 
48
47
  export class MilkyWssEndpoint implements EndpointInstance {
48
+ readonly #logger!: ReturnType<typeof getAdapterLogger>;
49
+
49
50
  readonly #options: MilkyWssEndpointOptions;
50
51
  readonly #callApi: typeof callApi;
51
52
  readonly management: EndpointManagement = createMilkyEndpointManagement(this);
@@ -57,6 +58,7 @@ export class MilkyWssEndpoint implements EndpointInstance {
57
58
  #unregisterAgent?: () => void;
58
59
 
59
60
  constructor(options: MilkyWssEndpointOptions) {
61
+ this.#logger = getAdapterLogger('milky', options.config.id);
60
62
  this.#options = options;
61
63
  this.#callApi = options.callApi ?? callApi;
62
64
  }
@@ -64,14 +66,14 @@ export class MilkyWssEndpoint implements EndpointInstance {
64
66
  async start(): Promise<void> {
65
67
  if (this.#started) return;
66
68
  this.#started = true;
67
- this.#unregisterAgent = registerMilkyAgentEndpoint(this.#options.config.name, this);
69
+ this.#unregisterAgent = registerMilkyAgentEndpoint(this.#options.config.id, this);
68
70
  const handle = this.#options.http.ws(this.#options.config.path);
69
71
  this.#wsRelease = handle.onConnection((connection) => {
70
72
  this.#acceptConnection(connection);
71
73
  });
72
- logger.info(formatCompact({
74
+ this.#logger.info(formatCompact({
73
75
  op: 'listen',
74
- endpoint: this.#options.config.name,
76
+ endpoint: this.#options.config.id,
75
77
  mode: 'wss',
76
78
  path: this.#options.config.path,
77
79
  }));
@@ -111,9 +113,9 @@ export class MilkyWssEndpoint implements EndpointInstance {
111
113
  const { action, params } = buildSendAction(conversation, message);
112
114
  const data = await this.callApi(action, params) as { message_seq?: number } | undefined;
113
115
  const messageId = formatOutboundMessageId(conversation, data?.message_seq);
114
- logger.debug(formatCompact({
116
+ this.#logger.debug(formatCompact({
115
117
  op: 'milky_send',
116
- endpoint: this.#options.config.name,
118
+ endpoint: this.#options.config.id,
117
119
  target: `${conversation.kind}:${conversation.id}`,
118
120
  messageId,
119
121
  }));
@@ -229,21 +231,20 @@ export class MilkyWssEndpoint implements EndpointInstance {
229
231
  message: { conversation, id: formatInboundMessageId(data) },
230
232
  content,
231
233
  segments,
232
- sender: String(data.sender_id),
234
+ sender: { id: String(data.sender_id) },
235
+ endpointId: this.#options.config.id,
236
+ ...(mentioned ? { mentioned: true } : {}),
233
237
  metadata: Object.freeze({
234
238
  message_scene: data.message_scene,
235
239
  peer_id: String(data.peer_id),
236
240
  sender_id: String(data.sender_id),
237
241
  message_seq: data.message_seq,
238
- endpoint: this.#options.config.name,
239
242
  time: data.time ?? event.time,
240
243
  self_id: event.self_id != null ? String(event.self_id) : undefined,
241
244
  ...(nickname ? { nickname } : {}),
242
- ...(mentioned ? { mentioned: true } : {}),
243
- ...(audioUrl ? { audio_url: audioUrl } : {}),
244
245
  }),
245
246
  }).catch((err) => {
246
- logger.warn(formatCompact({
247
+ this.#logger.warn(formatCompact({
247
248
  op: 'milky_gateway_receive_failed',
248
249
  target,
249
250
  error: err instanceof Error ? err.message : String(err),
@@ -272,8 +273,8 @@ export class MilkyWssEndpoint implements EndpointInstance {
272
273
  socket.on('close', () => {
273
274
  if (this.#ws === socket) this.#ws = undefined;
274
275
  });
275
- logger.debug(formatCompact({
276
- endpoint: this.#options.config.name,
276
+ this.#logger.debug(formatCompact({
277
+ endpoint: this.#options.config.id,
277
278
  mode: 'wss',
278
279
  peer: connection.request.socket.remoteAddress,
279
280
  }));
@@ -291,9 +292,9 @@ export class MilkyWssEndpoint implements EndpointInstance {
291
292
  const event = JSON.parse(raw) as MilkyEvent;
292
293
  this.admit(event);
293
294
  } catch (error) {
294
- logger.warn(formatCompact({
295
+ this.#logger.warn(formatCompact({
295
296
  op: 'milky_parse_failed',
296
- endpoint: this.#options.config.name,
297
+ endpoint: this.#options.config.id,
297
298
  error: error instanceof Error ? error.message : String(error),
298
299
  }));
299
300
  }
File without changes
File without changes
File without changes
File without changes