@zhin.js/adapter-lark 6.0.2 → 6.0.3

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # @zhin.js/adapter-lark
2
2
 
3
+ ## 6.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [afc0e66]
8
+ - Updated dependencies [2e41ad5]
9
+ - Updated dependencies [9f57124]
10
+ - @zhin.js/core@1.5.2
11
+ - @zhin.js/adapter@1.1.5
12
+ - @zhin.js/im-contract@1.0.1
13
+ - @zhin.js/plugin-runtime@1.1.3
14
+ - @zhin.js/command@1.0.7
15
+ - @zhin.js/host-http@1.0.6
16
+ - @zhin.js/agent@1.1.3
17
+ - zhin.js@6.0.2
18
+
3
19
  ## 6.0.2
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -24,8 +24,8 @@ pnpm add @zhin.js/adapter-lark
24
24
  - `@zhin.js/plugin-runtime` — `plugin.ts`(`definePlugin`)
25
25
  - 配置经插件 `schema.json` 落到 `plugins.<instanceKey>`
26
26
 
27
- 入站:`gateway.receive({ adapter, target: chat_id, content: text, sender, metadata })`
28
- 出站:`send({ target, payload })` → `im/v1/messages`
27
+ 入站:`gateway.receive({ conversation, message: { conversation, id }, content: text, sender, metadata })`
28
+ 出站:`send({ conversation, payload })` → `im/v1/messages`
29
29
 
30
30
  入站 `metadata.mentioned`:**未接线**。消息事件的 `mentions[]` 元素含 `id.open_id`,但本适配器拿不到 bot 自身的 open_id——配置(`appId` / `appSecret` / `name` 等)不含 bot open_id,代码也未调用 `bot/v3/info` 获取应用信息,故无可靠判据比对 mentions。
31
31
 
package/lib/endpoint.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { EndpointInstance, EndpointManagement } from '@zhin.js/adapter';
1
+ import type { EndpointInstance, EndpointManagement, EndpointSendRequest } from '@zhin.js/adapter';
2
2
  import type { MessageGateway } from '@zhin.js/core/runtime';
3
3
  import type { HttpHost } from '@zhin.js/host-http';
4
4
  import type { CapabilityId } from '@zhin.js/plugin-runtime';
@@ -40,10 +40,7 @@ export declare class LarkEndpoint implements EndpointInstance {
40
40
  open(): void;
41
41
  close(): void;
42
42
  stop(): Promise<void>;
43
- send({ target, payload }: {
44
- readonly target: string;
45
- readonly payload: unknown;
46
- }): Promise<string>;
43
+ send({ conversation, payload }: EndpointSendRequest): Promise<string>;
47
44
  recallMessage(messageId: string): Promise<void>;
48
45
  /** POST /im/v1/images(image_type=message),返回 image_key;失败返回 null。 */
49
46
  uploadImage(media: Parameters<typeof resolveMediaBinary>[0]): Promise<string | null>;
package/lib/endpoint.js CHANGED
@@ -6,7 +6,7 @@ import { basename } from 'node:path';
6
6
  import { formatCompact, getLogger } from '@zhin.js/logger';
7
7
  import { registerLarkAgentEndpoint } from './lark-agent-deps.js';
8
8
  import { buildImageUploadForm, readOutboundImageMedia, resolveMediaBinary, } from './media-upload.js';
9
- import { formatInboundContent, formatOutboundBody, generateMessageId, resolveChatType, resolveSender, resolveTarget, } from './protocol.js';
9
+ import { formatInboundContent, formatOutboundBody, generateMessageId, larkInboundConversation, resolveChatType, resolveSender, } from './protocol.js';
10
10
  import { registerLarkWebhookRoutes } from './webhook.js';
11
11
  const logger = getLogger('lark');
12
12
  /** 出站 HTTP 调用统一 30s 超时。 */
@@ -99,14 +99,14 @@ export class LarkEndpoint {
99
99
  this.#started = false;
100
100
  logger.debug(formatCompact({ op: 'disconnect', endpoint: this.#options.config.name }));
101
101
  }
102
- async send({ target, payload }) {
102
+ async send({ conversation, payload }) {
103
103
  const materialized = await this.#materializeOutboundMedia(payload);
104
104
  const content = formatOutboundBody(materialized);
105
105
  const data = await this.#request('/im/v1/messages', {
106
106
  method: 'POST',
107
107
  params: { receive_id_type: 'chat_id' },
108
108
  body: {
109
- receive_id: target,
109
+ receive_id: conversation.id,
110
110
  msg_type: content.msg_type,
111
111
  content: content.content,
112
112
  },
@@ -118,7 +118,7 @@ export class LarkEndpoint {
118
118
  logger.debug(formatCompact({
119
119
  op: 'send',
120
120
  endpoint: this.#options.config.name,
121
- to: target,
121
+ to: conversation.id,
122
122
  id: messageId,
123
123
  }));
124
124
  return messageId;
@@ -185,13 +185,12 @@ export class LarkEndpoint {
185
185
  admit(msg) {
186
186
  if (!this.#open)
187
187
  return;
188
- const target = resolveTarget(msg);
188
+ const conversation = larkInboundConversation(String(this.#options.id), msg);
189
189
  void this.#options.gateway.receive({
190
- adapter: this.#options.id,
191
- target,
190
+ conversation,
191
+ message: { conversation, id: generateMessageId(msg) },
192
192
  content: formatInboundContent(msg),
193
193
  sender: resolveSender(msg),
194
- id: generateMessageId(msg),
195
194
  metadata: Object.freeze({
196
195
  messageType: msg.message_type,
197
196
  chatType: resolveChatType(msg.chat_id, msg.chat_type),
@@ -200,7 +199,7 @@ export class LarkEndpoint {
200
199
  }).catch((err) => {
201
200
  logger.warn(formatCompact({
202
201
  op: 'lark_gateway_receive_failed',
203
- target,
202
+ conversation: conversation.id,
204
203
  error: err instanceof Error ? err.message : String(err),
205
204
  }));
206
205
  });
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { formatInboundContent, formatOutboundBody, generateMessageId, headerValue, normalizeWebhookPath, readTextBody, resolveChatType, resolveLarkConfig, resolveSender, resolveTarget, verifySignature, type AccessToken, type LarkAdapterConfig, type LarkApiResponse, type LarkEventBody, type LarkMessage, type LarkSendBody, type LarkWireSegment, type ResolvedLarkConfig, } from './protocol.js';
1
+ export { formatInboundContent, formatOutboundBody, generateMessageId, headerValue, larkInboundConversation, normalizeWebhookPath, readTextBody, resolveChatType, resolveLarkConfig, resolveSender, verifySignature, type AccessToken, type LarkAdapterConfig, type LarkApiResponse, type LarkEventBody, type LarkMessage, type LarkSendBody, type LarkWireSegment, type ResolvedLarkConfig, } from './protocol.js';
2
2
  export { getLarkAgentDeps, registerLarkAgentEndpoint, setLarkAgentDeps, type LarkAgentDeps, type LarkAgentEndpoint, } from './lark-agent-deps.js';
3
3
  export { checkLarkPlatformPermit, larkGroupPermitResolver, normalizeLarkSenderForPermit, platformPermit, registerLarkPlatformPermitChecker, } from './platform-permit.js';
4
4
  export { LarkEndpoint, type LarkEndpointOptions, type LarkFetch, } from './endpoint.js';
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { formatInboundContent, formatOutboundBody, generateMessageId, headerValue, normalizeWebhookPath, readTextBody, resolveChatType, resolveLarkConfig, resolveSender, resolveTarget, verifySignature, } from './protocol.js';
1
+ export { formatInboundContent, formatOutboundBody, generateMessageId, headerValue, larkInboundConversation, normalizeWebhookPath, readTextBody, resolveChatType, resolveLarkConfig, resolveSender, verifySignature, } from './protocol.js';
2
2
  export { getLarkAgentDeps, registerLarkAgentEndpoint, setLarkAgentDeps, } from './lark-agent-deps.js';
3
3
  export { checkLarkPlatformPermit, larkGroupPermitResolver, normalizeLarkSenderForPermit, platformPermit, registerLarkPlatformPermitChecker, } from './platform-permit.js';
4
4
  export { LarkEndpoint, } from './endpoint.js';
package/lib/protocol.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  * Canonicalization is owned by gateway/core before endpoint.send.
4
4
  */
5
5
  import type { IncomingMessage } from 'node:http';
6
+ import type { ConversationRef } from '@zhin.js/im-contract';
6
7
  /** Plugin Runtime owner config (`plugins.<instanceKey>` / schema.json). */
7
8
  export interface LarkAdapterConfig {
8
9
  readonly name?: string;
@@ -96,7 +97,12 @@ export interface LarkSendBody {
96
97
  export declare function resolveLarkConfig(config?: LarkAdapterConfig): ResolvedLarkConfig;
97
98
  export declare function normalizeWebhookPath(path: string): string;
98
99
  export declare function resolveChatType(chatId?: string, chatType?: string): 'group' | 'private';
99
- export declare function resolveTarget(msg: LarkMessage): string;
100
+ /**
101
+ * 入站归一化 → ConversationRef:`chat_type` 为 'group' 的会话映射为 group,
102
+ * p2p 单聊映射为 private;id 一律为平台 chat_id。Lark 无 guild/频道容器,
103
+ * 不产生 `parent`。
104
+ */
105
+ export declare function larkInboundConversation(endpointId: string, msg: LarkMessage): ConversationRef;
100
106
  export declare function resolveSender(msg: LarkMessage): string;
101
107
  export declare function generateMessageId(msg: LarkMessage): string;
102
108
  /** Build inbound text for MessageGateway.receive. */
package/lib/protocol.js CHANGED
@@ -49,8 +49,17 @@ export function resolveChatType(chatId, chatType) {
49
49
  return 'private';
50
50
  return chatId?.startsWith('oc_') ? 'group' : 'private';
51
51
  }
52
- export function resolveTarget(msg) {
53
- return msg.chat_id || 'unknown';
52
+ /**
53
+ * 入站归一化 → ConversationRef:`chat_type` 为 'group' 的会话映射为 group,
54
+ * p2p 单聊映射为 private;id 一律为平台 chat_id。Lark 无 guild/频道容器,
55
+ * 不产生 `parent`。
56
+ */
57
+ export function larkInboundConversation(endpointId, msg) {
58
+ return {
59
+ endpoint: { id: endpointId, adapter: endpointId.split('\0')[0] ?? endpointId },
60
+ kind: resolveChatType(msg.chat_id, msg.chat_type),
61
+ id: msg.chat_id || 'unknown',
62
+ };
54
63
  }
55
64
  export function resolveSender(msg) {
56
65
  return msg.sender?.sender_id?.open_id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/adapter-lark",
3
- "version": "6.0.2",
3
+ "version": "6.0.3",
4
4
  "description": "Zhin.js Lark/Feishu (飞书) adapter for Plugin Runtime (HTTP webhook)",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -34,21 +34,22 @@
34
34
  "directory": "plugins/adapters/lark"
35
35
  },
36
36
  "dependencies": {
37
- "@zhin.js/adapter": "1.1.4",
38
- "@zhin.js/command": "1.0.6",
39
- "@zhin.js/core": "1.5.1",
40
- "@zhin.js/host-http": "1.0.5",
37
+ "@zhin.js/adapter": "1.1.5",
38
+ "@zhin.js/command": "1.0.7",
39
+ "@zhin.js/core": "1.5.2",
40
+ "@zhin.js/host-http": "1.0.6",
41
+ "@zhin.js/im-contract": "1.0.1",
41
42
  "@zhin.js/logger": "1.0.75",
42
- "@zhin.js/plugin-runtime": "1.1.2"
43
+ "@zhin.js/plugin-runtime": "1.1.3"
43
44
  },
44
45
  "peerDependencies": {
45
46
  "zod": "^4.0.0",
46
- "@zhin.js/adapter": "1.1.4",
47
- "@zhin.js/agent": "1.1.2",
48
- "@zhin.js/core": "1.5.1",
49
- "@zhin.js/host-http": "1.0.5",
50
- "@zhin.js/plugin-runtime": "1.1.2",
51
- "zhin.js": "6.0.1"
47
+ "@zhin.js/adapter": "1.1.5",
48
+ "@zhin.js/agent": "1.1.3",
49
+ "@zhin.js/core": "1.5.2",
50
+ "@zhin.js/host-http": "1.0.6",
51
+ "@zhin.js/plugin-runtime": "1.1.3",
52
+ "zhin.js": "6.0.2"
52
53
  },
53
54
  "peerDependenciesMeta": {
54
55
  "zhin.js": {
@@ -66,8 +67,8 @@
66
67
  "typescript": "^6.0.3",
67
68
  "vitest": "^4.1.10",
68
69
  "zod": "^4.4.3",
69
- "@zhin.js/agent": "1.1.2",
70
- "zhin.js": "6.0.1"
70
+ "@zhin.js/agent": "1.1.3",
71
+ "zhin.js": "6.0.2"
71
72
  },
72
73
  "files": [
73
74
  "adapters",
package/src/endpoint.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { readFile } from 'node:fs/promises';
5
5
  import { basename } from 'node:path';
6
- import type { EndpointGroup, EndpointInstance, EndpointManagement } from '@zhin.js/adapter';
6
+ import type { EndpointGroup, EndpointInstance, EndpointManagement, EndpointSendRequest } from '@zhin.js/adapter';
7
7
  import type { MessageGateway } from '@zhin.js/core/runtime';
8
8
  import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
9
9
  import { formatCompact, getLogger } from '@zhin.js/logger';
@@ -18,9 +18,9 @@ import {
18
18
  formatInboundContent,
19
19
  formatOutboundBody,
20
20
  generateMessageId,
21
+ larkInboundConversation,
21
22
  resolveChatType,
22
23
  resolveSender,
23
- resolveTarget,
24
24
  type AccessToken,
25
25
  type LarkApiResponse,
26
26
  type LarkMessage,
@@ -151,14 +151,14 @@ export class LarkEndpoint implements EndpointInstance {
151
151
  logger.debug(formatCompact({ op: 'disconnect', endpoint: this.#options.config.name }));
152
152
  }
153
153
 
154
- async send({ target, payload }: { readonly target: string; readonly payload: unknown }): Promise<string> {
154
+ async send({ conversation, payload }: EndpointSendRequest): Promise<string> {
155
155
  const materialized = await this.#materializeOutboundMedia(payload);
156
156
  const content = formatOutboundBody(materialized);
157
157
  const data = await this.#request('/im/v1/messages', {
158
158
  method: 'POST',
159
159
  params: { receive_id_type: 'chat_id' },
160
160
  body: {
161
- receive_id: target,
161
+ receive_id: conversation.id,
162
162
  msg_type: content.msg_type,
163
163
  content: content.content,
164
164
  },
@@ -170,7 +170,7 @@ export class LarkEndpoint implements EndpointInstance {
170
170
  logger.debug(formatCompact({
171
171
  op: 'send',
172
172
  endpoint: this.#options.config.name,
173
- to: target,
173
+ to: conversation.id,
174
174
  id: messageId,
175
175
  }));
176
176
  return messageId;
@@ -234,13 +234,12 @@ export class LarkEndpoint implements EndpointInstance {
234
234
  /** Test / internal: admit a parsed message when open (non-webhook path). */
235
235
  admit(msg: LarkMessage): void {
236
236
  if (!this.#open) return;
237
- const target = resolveTarget(msg);
237
+ const conversation = larkInboundConversation(String(this.#options.id), msg);
238
238
  void this.#options.gateway.receive({
239
- adapter: this.#options.id,
240
- target,
239
+ conversation,
240
+ message: { conversation, id: generateMessageId(msg) },
241
241
  content: formatInboundContent(msg),
242
242
  sender: resolveSender(msg),
243
- id: generateMessageId(msg),
244
243
  metadata: Object.freeze({
245
244
  messageType: msg.message_type,
246
245
  chatType: resolveChatType(msg.chat_id, msg.chat_type),
@@ -249,7 +248,7 @@ export class LarkEndpoint implements EndpointInstance {
249
248
  }).catch((err) => {
250
249
  logger.warn(formatCompact({
251
250
  op: 'lark_gateway_receive_failed',
252
- target,
251
+ conversation: conversation.id,
253
252
  error: err instanceof Error ? err.message : String(err),
254
253
  }));
255
254
  });
package/src/index.ts CHANGED
@@ -3,12 +3,12 @@ export {
3
3
  formatOutboundBody,
4
4
  generateMessageId,
5
5
  headerValue,
6
+ larkInboundConversation,
6
7
  normalizeWebhookPath,
7
8
  readTextBody,
8
9
  resolveChatType,
9
10
  resolveLarkConfig,
10
11
  resolveSender,
11
- resolveTarget,
12
12
  verifySignature,
13
13
  type AccessToken,
14
14
  type LarkAdapterConfig,
package/src/protocol.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  import { createHash, timingSafeEqual } from 'node:crypto';
7
7
  import type { IncomingMessage } from 'node:http';
8
8
  import { isMediaRef } from '@zhin.js/core';
9
+ import type { ConversationRef } from '@zhin.js/im-contract';
9
10
  import { formatCompact, getLogger } from '@zhin.js/logger';
10
11
 
11
12
  const logger = getLogger('lark');
@@ -158,8 +159,17 @@ export function resolveChatType(chatId?: string, chatType?: string): 'group' | '
158
159
  return chatId?.startsWith('oc_') ? 'group' : 'private';
159
160
  }
160
161
 
161
- export function resolveTarget(msg: LarkMessage): string {
162
- return msg.chat_id || 'unknown';
162
+ /**
163
+ * 入站归一化 → ConversationRef:`chat_type` 为 'group' 的会话映射为 group,
164
+ * p2p 单聊映射为 private;id 一律为平台 chat_id。Lark 无 guild/频道容器,
165
+ * 不产生 `parent`。
166
+ */
167
+ export function larkInboundConversation(endpointId: string, msg: LarkMessage): ConversationRef {
168
+ return {
169
+ endpoint: { id: endpointId, adapter: endpointId.split('\0')[0] ?? endpointId },
170
+ kind: resolveChatType(msg.chat_id, msg.chat_type),
171
+ id: msg.chat_id || 'unknown',
172
+ };
163
173
  }
164
174
 
165
175
  export function resolveSender(msg: LarkMessage): string {