@zhin.js/adapter-lark 1.0.97 → 1.1.0

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +637 -3
  2. package/README.md +70 -83
  3. package/adapters/lark.js +36 -0
  4. package/adapters/lark.ts +41 -0
  5. package/agent/PERMITS.md +22 -0
  6. package/agent/tools/add_members.ts +20 -0
  7. package/agent/tools/create_chat.ts +18 -0
  8. package/agent/tools/dissolve_chat.ts +16 -0
  9. package/agent/tools/get_user.ts +16 -0
  10. package/agent/tools/remove_managers.ts +20 -0
  11. package/agent/tools/set_managers.ts +20 -0
  12. package/agent/tools/update_chat.ts +21 -0
  13. package/agent/tools/upload_file.ts +23 -0
  14. package/commands/endpoint/add/[id].js +3 -0
  15. package/commands/endpoint/add/[id].ts +3 -0
  16. package/commands/endpoint/list.js +3 -0
  17. package/commands/endpoint/list.ts +3 -0
  18. package/commands/endpoint/remove/[id].js +3 -0
  19. package/commands/endpoint/remove/[id].ts +3 -0
  20. package/lib/client.d.ts +12 -0
  21. package/lib/client.js +2 -0
  22. package/lib/endpoint.d.ts +100 -0
  23. package/lib/endpoint.js +497 -0
  24. package/lib/index.d.ts +6 -15
  25. package/lib/index.js +6 -209
  26. package/lib/lark-endpoint-commands.d.ts +1 -0
  27. package/lib/lark-endpoint-commands.js +19 -0
  28. package/lib/lark-runtime-state.d.ts +1 -0
  29. package/lib/lark-runtime-state.js +6 -0
  30. package/lib/media-upload.d.ts +15 -0
  31. package/lib/media-upload.js +58 -0
  32. package/lib/platform-permit.d.ts +14 -0
  33. package/lib/platform-permit.js +35 -0
  34. package/lib/protocol.d.ts +123 -0
  35. package/lib/protocol.js +273 -0
  36. package/lib/webhook.d.ts +14 -0
  37. package/lib/webhook.js +73 -0
  38. package/package.json +63 -20
  39. package/plugin.js +19 -0
  40. package/schema.json +97 -0
  41. package/src/client.ts +16 -0
  42. package/src/endpoint.ts +590 -0
  43. package/src/index.ts +51 -227
  44. package/src/lark-endpoint-commands.ts +20 -0
  45. package/src/lark-runtime-state.ts +7 -0
  46. package/src/media-upload.ts +69 -0
  47. package/src/platform-permit.ts +50 -0
  48. package/src/protocol.ts +402 -0
  49. package/src/webhook.ts +110 -0
  50. package/lib/adapter.d.ts +0 -17
  51. package/lib/adapter.d.ts.map +0 -1
  52. package/lib/adapter.js +0 -45
  53. package/lib/adapter.js.map +0 -1
  54. package/lib/bot.d.ts +0 -82
  55. package/lib/bot.d.ts.map +0 -1
  56. package/lib/bot.js +0 -638
  57. package/lib/bot.js.map +0 -1
  58. package/lib/index.d.ts.map +0 -1
  59. package/lib/index.js.map +0 -1
  60. package/lib/types.d.ts +0 -63
  61. package/lib/types.d.ts.map +0 -1
  62. package/lib/types.js +0 -2
  63. package/lib/types.js.map +0 -1
  64. package/plugin.yml +0 -3
  65. package/src/adapter.ts +0 -55
  66. package/src/bot.ts +0 -712
  67. package/src/types.ts +0 -58
  68. /package/{skills/lark/SKILL.md → agent/skills/lark.md} +0 -0
package/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  # @zhin.js/adapter-lark
2
2
 
3
- Zhin.js 飞书 / Lark 适配器,支持飞书机器人的消息收发和群管理。
3
+ Zhin.js 飞书 / Lark 适配器(Plugin Runtime),通过 Runtime Host HTTP Webhook 收发消息。
4
4
 
5
- ## 功能特性
5
+ ## 功能
6
6
 
7
- - 支持飞书和 Lark 国际版
8
- - Webhook 事件接收(HTTP 回调)
9
- - Bearer Token 自动刷新
10
- - 消息加密和签名验证(可选)
11
- - URL 验证自动处理
12
- - 群管理 AI 工具(通过包内 `skills/lark/SKILL.md` 与 `addTool` 暴露给 AI)
7
+ - Webhook 事件接收(`httpHostToken` POST + 可选 verificationToken / encryptKey 签名)
8
+ - URL 验证挑战(`url_verification`)
9
+ - Tenant Access Token 自动刷新
10
+ - 支持飞书与 Lark 国际版 API 基址
11
+ - canonical `markdown` 段编码为 `interactive` 卡片中的 `lark_md`
12
+ - 约定式 `defineAdapter` / `definePlugin`(无需 `usePlugin`)
13
13
 
14
14
  ## 安装
15
15
 
@@ -17,99 +17,86 @@ Zhin.js 飞书 / Lark 适配器,支持飞书机器人的消息收发和群管
17
17
  pnpm add @zhin.js/adapter-lark
18
18
  ```
19
19
 
20
- ## 依赖
20
+ ## Plugin Runtime
21
21
 
22
- - `@zhin.js/host-router` — HTTP 服务(提供 Webhook 路由)
22
+ - `@zhin.js/adapter` — 约定式 `adapters/lark.ts`(`defineAdapter`)
23
+ - `@zhin.js/core` — `Endpoint.emit(...)` 入站、`outboundMessageToken` 出站
24
+ - `@zhin.js/host-http` — `httpHostToken` 注册 Webhook 路由(**非** legacy host-router/Koa)
25
+ - `zhin.js` — `plugin.ts`(`definePlugin`)
26
+ - 配置经插件 `schema.json` 落到 `plugins.<instanceKey>`
23
27
 
24
- ## 配置
28
+ 入站:`gateway.receive({ conversation, message: { conversation, id }, content: text, sender, metadata })`
29
+ 出站:`send({ conversation, payload })` → `im/v1/messages`
25
30
 
26
- ```yaml
27
- # zhin.config.yml
28
- bots:
29
- - context: lark
30
- name: my-lark-bot
31
- appId: cli_xxxxxxxxxxxx
32
- appSecret: xxxxxxxxxxxxxxxxxxxxxxxx
33
- webhookPath: /lark/webhook
34
- # 可选配置
35
- # encryptKey: your-encrypt-key
36
- # verificationToken: your-token
37
- # isFeishu: true # 使用飞书 API(默认)
38
- # apiBaseUrl: https://open.feishu.cn/open-apis # 自定义 API 地址
39
-
40
- plugins:
41
- - "@zhin.js/adapter-lark"
42
- - "@zhin.js/host-router"
43
- ```
44
-
45
- 或使用 TypeScript 配置:
46
-
47
- ```typescript
48
- import { defineConfig } from 'zhin.js'
49
-
50
- export default defineConfig({
51
- bots: [
52
- {
53
- context: 'lark',
54
- name: 'my-lark-bot',
55
- appId: process.env.LARK_APP_ID!,
56
- appSecret: process.env.LARK_APP_SECRET!,
57
- webhookPath: '/lark/webhook',
58
- }
59
- ],
60
- plugins: ['@zhin.js/adapter-lark', '@zhin.js/host-router']
61
- })
62
- ```
31
+ 入站 `metadata.mentioned`:**未接线**。消息事件的 `mentions[]` 元素含 `id.open_id`,但本适配器拿不到 bot 自身的 open_id——配置(`appId` / `appSecret` / `name` 等)不含 bot open_id,代码也未调用 `bot/v3/info` 获取应用信息,故无可靠判据比对 mentions。
63
32
 
64
- ## 使用示例
33
+ ## 前置条件
65
34
 
66
- ### 注册命令
35
+ 1. 在 [飞书开放平台](https://open.feishu.cn/)(或 Lark)创建企业自建应用
36
+ 2. 获取 **App ID**、**App Secret**
37
+ 3. 启用机器人能力并配置事件订阅 URL:`https://your-domain/lark/webhook`
38
+ 4. Runtime Host(`http`)须已 listen,Webhook 才可达
67
39
 
68
- ```typescript
69
- import { usePlugin, MessageCommand } from 'zhin.js'
40
+ 必填字段(`endpoints[i]`):`name`、`appId`、`appSecret`。
70
41
 
71
- const { addCommand } = usePlugin()
42
+ ## 最小配置
72
43
 
73
- addCommand(
74
- new MessageCommand('hello')
75
- .desc('飞书问候')
76
- .action((message) => {
77
- return `你好,${message.$sender.name}!`
78
- })
79
- )
44
+ ```yaml
45
+ # zhin.config.yml(Plugin Runtime)
46
+ plugins:
47
+ lark:
48
+ webhookPath: /lark/webhook # 可选,默认 /lark/webhook
49
+ isFeishu: true # 可选,默认 true
50
+ # apiBaseUrl: https://open.feishu.cn/open-apis
51
+ endpoints:
52
+ - name: my-lark-bot
53
+ appId: ${LARK_APP_ID}
54
+ appSecret: ${LARK_APP_SECRET}
55
+ # encryptKey: ${LARK_ENCRYPT_KEY} # 可选
56
+ # verificationToken: ${LARK_VERIFY_TOKEN} # 可选
80
57
  ```
81
58
 
82
- ### 消息处理
59
+ 根插件 `zhin.plugins`(或项目图)需引用 `@zhin.js/adapter-lark`(`instanceKey: lark`)。
83
60
 
84
- ```typescript
85
- import { usePlugin } from 'zhin.js'
61
+ ## 环境变量
86
62
 
87
- const { addMiddleware } = usePlugin()
63
+ | 变量 | 说明 |
64
+ |------|------|
65
+ | `LARK_APP_ID` | 应用 App ID |
66
+ | `LARK_APP_SECRET` | 应用 App Secret |
67
+ | `LARK_BOT_NAME` | 默认 endpoint 名称(可选) |
88
68
 
89
- addMiddleware(async (message, next) => {
90
- if (message.$adapter === 'lark') {
91
- console.log('收到飞书消息:', message.$content)
92
- }
93
- await next()
94
- })
95
- ```
69
+ ## 消息类型映射
96
70
 
97
- ## AI 工具(Skill)
71
+ | 飞书类型 | 入站 content(文本摘要) | 出站 wire |
72
+ |----------|--------------------------|-----------|
73
+ | text | 原文 | text |
74
+ | markdown | 原文 | interactive(`lark_md`) |
75
+ | image | `[image]` | image(需 `file_key`) |
76
+ | file | `[file: name]` | file |
77
+ | audio / video / sticker | `[audio]` / `[video]` / `[sticker]` | — |
78
+ | card | — | interactive |
98
79
 
99
- 适配器内置群管理 Skill,自动注册以下工具供 AI 调用:
80
+ ## Agent 工具
100
81
 
101
- - 群聊管理(成员管理、群信息修改等)
82
+ `agent/` 目录保留(get_user、群聊、管理员、上传文件等)。工具声明 `adapter: 'lark'` 后,通过惰性的 `context.$client` 自动取得当前操作的 `LarkClient`;无需把 Endpoint id 暴露给模型。
102
83
 
103
- > 工具使用飞书的 `open_id` 和 `chat_id` 格式标识用户和群聊。
84
+ ## 平台权限(platform permit)
104
85
 
105
- ## 飞书开发者配置
86
+ `plugin.ts` 在 generation setup 注册 `src/platform-permit.ts` checker,并在 dispose 注销;CapabilityIngress 与 ToolSystem 统一经 Core `canAccessTool()` 消费工具权限。
106
87
 
107
- 1. 登录 [飞书开放平台](https://open.feishu.cn/)
108
- 2. 创建应用并获取 `App ID` 和 `App Secret`
109
- 3. 配置事件回调 URL:`http://your-server:8086/api/lark/webhook`
110
- 4. 订阅 `im.message.receive_v1` 事件
111
- 5. 发布应用
88
+ ## 测试
89
+
90
+ ```bash
91
+ pnpm --filter @zhin.js/adapter-lark build
92
+ pnpm --filter @zhin.js/adapter-lark test
93
+ ```
112
94
 
113
- ## 许可证
95
+ ## 故障排查
114
96
 
115
- MIT License
97
+ | 现象 | 排查 |
98
+ | --- | --- |
99
+ | URL 验证失败 | 检查公网 HTTPS、`webhookPath`、verification token 与 encrypt key |
100
+ | Tenant Token 获取失败 | 检查 `appId`、`appSecret` 与应用版本是否已发布 |
101
+ | 群里 @机器人不触发 AI | 当前无法可靠判定 bot open_id;使用显式 AI 前缀 |
102
+ | 能收不能发 | 检查机器人消息权限、可见范围与应用是否已加入群聊 |
@@ -0,0 +1,36 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ /**
3
+ * Convention entry: discover `adapters/lark.ts` → defineAdapter.
4
+ */
5
+ import { defineAdapter } from 'zhin.js/adapter';
6
+ import { httpHostToken } from '@zhin.js/host-http';
7
+ import { LarkEndpoint } from "../lib/endpoint.js";
8
+ import { resolveLarkConfig, } from "../lib/protocol.js";
9
+ import { larkRuntimeStateToken } from "../lib/lark-runtime-state.js";
10
+ export { LarkEndpoint } from "../lib/endpoint.js";
11
+ export default defineAdapter({
12
+ capabilities: ['inbound', 'outbound'],
13
+ operations: ['recall'],
14
+ // image 段 canonical MediaRef 经 /im/v1/images 物化为 image_key
15
+ // (base64 解码 / path 读盘 / url 下载后上传,kind=file 平台引用直通);
16
+ // canonical Markdown 由 endpoint codec 转为 lark_md 消息卡片;
17
+ // 卡片交互未接入出站通道,交互段降级纯文本。
18
+ segments: {
19
+ outboundMedia: ['url', 'upload'],
20
+ interactive: 'text',
21
+ markdown: 'native',
22
+ },
23
+ create(context) {
24
+ const config = resolveLarkConfig(context.config);
25
+ // 注册到插件运行时状态(lark.endpoint list 的"运行中"数据源)
26
+ context.use(larkRuntimeStateToken).endpoints.set(config.id, {
27
+ id: config.id,
28
+ mode: 'webhook',
29
+ });
30
+ return new LarkEndpoint({
31
+ id: context.id,
32
+ http: context.use(httpHostToken),
33
+ config,
34
+ });
35
+ },
36
+ });
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Convention entry: discover `adapters/lark.ts` → defineAdapter.
3
+ */
4
+ import { defineAdapter } from 'zhin.js/adapter';
5
+ import { httpHostToken } from '@zhin.js/host-http';
6
+ import { LarkEndpoint } from '../src/endpoint.js';
7
+ import {
8
+ resolveLarkConfig,
9
+ type LarkAdapterConfig,
10
+ } from '../src/protocol.js';
11
+ import { larkRuntimeStateToken } from '../src/lark-runtime-state.js';
12
+
13
+ export { LarkEndpoint } from '../src/endpoint.js';
14
+ export type { LarkEndpointOptions, LarkFetch } from '../src/endpoint.js';
15
+
16
+ export default defineAdapter<LarkAdapterConfig>({
17
+ capabilities: ['inbound', 'outbound'],
18
+ operations: ['recall'],
19
+ // image 段 canonical MediaRef 经 /im/v1/images 物化为 image_key
20
+ // (base64 解码 / path 读盘 / url 下载后上传,kind=file 平台引用直通);
21
+ // canonical Markdown 由 endpoint codec 转为 lark_md 消息卡片;
22
+ // 卡片交互未接入出站通道,交互段降级纯文本。
23
+ segments: {
24
+ outboundMedia: ['url', 'upload'],
25
+ interactive: 'text',
26
+ markdown: 'native',
27
+ },
28
+ create(context) {
29
+ const config = resolveLarkConfig(context.config);
30
+ // 注册到插件运行时状态(lark.endpoint list 的"运行中"数据源)
31
+ context.use(larkRuntimeStateToken).endpoints.set(config.id, {
32
+ id: config.id,
33
+ mode: 'webhook',
34
+ });
35
+ return new LarkEndpoint({
36
+ id: context.id,
37
+ http: context.use(httpHostToken),
38
+ config,
39
+ });
40
+ },
41
+ });
@@ -0,0 +1,22 @@
1
+ # 飞书 Lark Platform Permits
2
+
3
+ ## 入站字段
4
+
5
+ 群聊消息经 `getChatInfo`(TTL 60s)比对 `owner_id` / `user_manager_id_list`:
6
+
7
+ | 字段 | 取值 |
8
+ |------|------|
9
+ | `$sender.role` | `owner` · `admin` · `member` |
10
+ | `$sender.permissions` | `chat_owner` · `chat_admin` · `manage_managers` |
11
+
12
+ ## Permit 词汇表
13
+
14
+ | `platform(lark,…)` | 含义 |
15
+ |--------------------|------|
16
+ | `chat_owner` | 群主 |
17
+ | `chat_admin` | 群管理员 |
18
+ | `manage_managers` | 可设/撤管理员 |
19
+
20
+ ## 工厂映射
21
+
22
+ `scene_admin` → `chat_admin` · `scene_owner` → `chat_owner`
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+
5
+ export default defineAgentTool<{ chat_id: string; user_ids: string }>({
6
+ description: '添加飞书群成员',
7
+ inputSchema: z.object({
8
+ chat_id: z.string().describe('群聊 ID'),
9
+ user_ids: z.string().describe('用户 open_id 列表,逗号分隔'),
10
+ }),
11
+ adapter: 'lark',
12
+ tags: ['lark'],
13
+ permissions: [platformPermit('chat_admin')],
14
+ async execute({ chat_id, user_ids }: { chat_id: string; user_ids: string }, context) {
15
+ const endpoint = context.$client;
16
+ const success = await endpoint.addChatMembers(chat_id, user_ids.split(','));
17
+ return { success, message: success ? '成员添加成功' : '添加失败' };
18
+ },
19
+ });
20
+
@@ -0,0 +1,18 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ export default defineAgentTool<{ name: string; members: string; owner?: string }>({
4
+ description: '创建飞书群聊',
5
+ inputSchema: z.object({
6
+ name: z.string().describe('群名'),
7
+ members: z.string().describe('成员 open_id 列表,逗号分隔'),
8
+ owner: z.string().optional().describe('群主 open_id(可选)'),
9
+ }),
10
+ adapter: 'lark',
11
+ tags: ['lark'],
12
+ async execute({ name, members, owner }: { name: string; members: string; owner?: string }, context) {
13
+ const endpoint = context.$client;
14
+ const chatId = await endpoint.createChat(name, members.split(','), owner);
15
+ return { success: !!chatId, chat_id: chatId, message: chatId ? `群聊创建成功: ${chatId}` : '创建失败' };
16
+ },
17
+ });
18
+
@@ -0,0 +1,16 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ export default defineAgentTool<{ chat_id: string }>({
4
+ description: '解散飞书群聊(需要群主权限)',
5
+ inputSchema: z.object({
6
+ chat_id: z.string().describe('群聊 ID'),
7
+ }),
8
+ adapter: 'lark',
9
+ tags: ['lark'],
10
+ async execute({ chat_id }: { chat_id: string }, context) {
11
+ const endpoint = context.$client;
12
+ const success = await endpoint.dissolveChat(chat_id);
13
+ return { success, message: success ? '群聊已解散' : '解散失败' };
14
+ },
15
+ });
16
+
@@ -0,0 +1,16 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+
4
+ export default defineAgentTool<{ user_id: string }>({
5
+ description: '获取飞书用户信息',
6
+ inputSchema: z.object({
7
+ user_id: z.string().describe('用户 ID (open_id)'),
8
+ }),
9
+ adapter: 'lark',
10
+ tags: ['lark'],
11
+ async execute({ user_id }: { user_id: string }, context) {
12
+ const endpoint = context.$client;
13
+ return await endpoint.getUserInfo(user_id);
14
+ },
15
+ });
16
+
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+
5
+ export default defineAgentTool<{ chat_id: string; user_ids: string }>({
6
+ description: '移除飞书群管理员',
7
+ inputSchema: z.object({
8
+ chat_id: z.string().describe('群聊 ID'),
9
+ user_ids: z.string().describe('用户 open_id 列表,逗号分隔'),
10
+ }),
11
+ adapter: 'lark',
12
+ tags: ['lark'],
13
+ permissions: [platformPermit('manage_managers')],
14
+ async execute({ chat_id, user_ids }: { chat_id: string; user_ids: string }, context) {
15
+ const endpoint = context.$client;
16
+ const success = await endpoint.removeChatManagers(chat_id, user_ids.split(','));
17
+ return { success, message: success ? '管理员移除成功' : '移除失败' };
18
+ },
19
+ });
20
+
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+
5
+ export default defineAgentTool<{ chat_id: string; user_ids: string }>({
6
+ description: '设置飞书群管理员',
7
+ inputSchema: z.object({
8
+ chat_id: z.string().describe('群聊 ID'),
9
+ user_ids: z.string().describe('用户 open_id 列表,逗号分隔'),
10
+ }),
11
+ adapter: 'lark',
12
+ tags: ['lark'],
13
+ permissions: [platformPermit('manage_managers')],
14
+ async execute({ chat_id, user_ids }: { chat_id: string; user_ids: string }, context) {
15
+ const endpoint = context.$client;
16
+ const success = await endpoint.setChatManagers(chat_id, user_ids.split(','));
17
+ return { success, message: success ? '管理员设置成功' : '设置失败' };
18
+ },
19
+ });
20
+
@@ -0,0 +1,21 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+
5
+ export default defineAgentTool<{ chat_id: string; name?: string; description?: string }>({
6
+ description: '更新飞书群聊信息(群名、描述)',
7
+ inputSchema: z.object({
8
+ chat_id: z.string().describe('群聊 ID'),
9
+ name: z.string().optional().describe('新群名(可选)'),
10
+ description: z.string().optional().describe('新描述(可选)'),
11
+ }),
12
+ adapter: 'lark',
13
+ tags: ['lark'],
14
+ permissions: [platformPermit('chat_admin')],
15
+ async execute({ chat_id, name, description }: { chat_id: string; name?: string; description?: string }, context) {
16
+ const endpoint = context.$client;
17
+ const success = await endpoint.updateChatInfo(chat_id, { name, description });
18
+ return { success, message: success ? '群信息更新成功' : '更新失败' };
19
+ },
20
+ });
21
+
@@ -0,0 +1,23 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+
4
+ export default defineAgentTool<{
5
+ file_path: string;
6
+ file_type: 'image' | 'file' | 'video' | 'audio';
7
+ }>({
8
+ description: '上传文件到飞书(image/file/video/audio)',
9
+ inputSchema: z.object({
10
+ file_path: z.string().describe('本地文件路径'),
11
+ file_type: z.enum(['image', 'file', 'video', 'audio']).default('file').describe('飞书文件类型'),
12
+ }),
13
+ adapter: 'lark',
14
+ tags: ['lark'],
15
+ async execute({ file_path, file_type }: {
16
+ file_path: string;
17
+ file_type: 'image' | 'file' | 'video' | 'audio';
18
+ }, context) {
19
+ const endpoint = context.$client;
20
+ const result = await endpoint.uploadFile(file_path, file_type);
21
+ return { success: true, file_key: result, message: `文件已上传,file_key: ${result}` };
22
+ },
23
+ });
@@ -0,0 +1,3 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { larkEndpointCommands } from "../../../lib/lark-endpoint-commands.js";
3
+ export default larkEndpointCommands.add;
@@ -0,0 +1,3 @@
1
+ import { larkEndpointCommands } from '../../../src/lark-endpoint-commands.js';
2
+
3
+ export default larkEndpointCommands.add;
@@ -0,0 +1,3 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { larkEndpointCommands } from "../../lib/lark-endpoint-commands.js";
3
+ export default larkEndpointCommands.list;
@@ -0,0 +1,3 @@
1
+ import { larkEndpointCommands } from '../../src/lark-endpoint-commands.js';
2
+
3
+ export default larkEndpointCommands.list;
@@ -0,0 +1,3 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { larkEndpointCommands } from "../../../lib/lark-endpoint-commands.js";
3
+ export default larkEndpointCommands.remove;
@@ -0,0 +1,3 @@
1
+ import { larkEndpointCommands } from '../../../src/lark-endpoint-commands.js';
2
+
3
+ export default larkEndpointCommands.remove;
@@ -0,0 +1,12 @@
1
+ import type { LarkClient } from './endpoint.js';
2
+ import type { LarkEventBody } from './protocol.js';
3
+ export type LarkClientEventMap = Record<string, LarkEventBody>;
4
+ declare module '@zhin.js/feature-kit' {
5
+ interface AdapterClientRegistry {
6
+ readonly lark: {
7
+ readonly client: LarkClient;
8
+ readonly events: LarkClientEventMap;
9
+ };
10
+ }
11
+ }
12
+ export declare const larkClient: import("@zhin.js/adapter").EndpointClientToken<LarkClient, LarkClientEventMap>;
package/lib/client.js ADDED
@@ -0,0 +1,2 @@
1
+ import { defineEndpointClient } from 'zhin.js/adapter';
2
+ export const larkClient = defineEndpointClient('lark');
@@ -0,0 +1,100 @@
1
+ import { Endpoint } from 'zhin.js/adapter';
2
+ import { type EndpointControl, type EndpointManagement, type EndpointSendRequest } from 'zhin.js/adapter';
3
+ import type { HttpHost } from '@zhin.js/host-http';
4
+ import type { CapabilityId } from 'zhin.js';
5
+ import { resolveMediaBinary } from './media-upload.js';
6
+ import { type LarkMessage, type ResolvedLarkConfig } from './protocol.js';
7
+ export type LarkFetch = (url: string, init?: {
8
+ readonly method?: string;
9
+ readonly headers?: Record<string, string>;
10
+ readonly body?: string | FormData;
11
+ readonly signal?: AbortSignal;
12
+ }) => Promise<{
13
+ readonly ok: boolean;
14
+ readonly status: number;
15
+ text(): Promise<string>;
16
+ json(): Promise<unknown>;
17
+ }>;
18
+ export interface LarkEndpointOptions {
19
+ readonly id: CapabilityId;
20
+ readonly http: HttpHost;
21
+ readonly config: ResolvedLarkConfig;
22
+ readonly fetch?: LarkFetch;
23
+ }
24
+ export interface LarkClientApi {
25
+ getUserInfo(userId: string, userIdType?: 'open_id' | 'user_id' | 'union_id'): Promise<unknown>;
26
+ getChatInfo(chatId: string): Promise<unknown>;
27
+ uploadFile(filePath: string, fileType?: 'image' | 'file' | 'video' | 'audio'): Promise<string | null>;
28
+ createChat(name: string, userIds: string[], ownerId?: string): Promise<string | null>;
29
+ updateChatInfo(chatId: string, options: {
30
+ name?: string;
31
+ description?: string;
32
+ }): Promise<boolean>;
33
+ addChatMembers(chatId: string, userIds: string[]): Promise<boolean>;
34
+ removeChatMembers(chatId: string, userIds: string[]): Promise<boolean>;
35
+ getChatMembers(chatId: string): Promise<unknown[]>;
36
+ dissolveChat(chatId: string): Promise<boolean>;
37
+ setChatManagers(chatId: string, userIds: string[]): Promise<boolean>;
38
+ removeChatManagers(chatId: string, userIds: string[]): Promise<boolean>;
39
+ }
40
+ /** Feishu/Lark OpenAPI client surface carried by every Endpoint event. */
41
+ export declare class LarkClient implements LarkClientApi {
42
+ readonly api: LarkClientApi;
43
+ constructor(api: LarkClientApi);
44
+ getUserInfo: (id: string, type?: "open_id" | "user_id" | "union_id") => Promise<unknown>;
45
+ getChatInfo: (id: string) => Promise<unknown>;
46
+ uploadFile: (path: string, type?: "image" | "file" | "video" | "audio") => Promise<string | null>;
47
+ createChat: (name: string, users: string[], owner?: string) => Promise<string | null>;
48
+ updateChatInfo: (id: string, options: {
49
+ name?: string;
50
+ description?: string;
51
+ }) => Promise<boolean>;
52
+ addChatMembers: (id: string, users: string[]) => Promise<boolean>;
53
+ removeChatMembers: (id: string, users: string[]) => Promise<boolean>;
54
+ getChatMembers: (id: string) => Promise<unknown[]>;
55
+ dissolveChat: (id: string) => Promise<boolean>;
56
+ setChatManagers: (id: string, users: string[]) => Promise<boolean>;
57
+ removeChatManagers: (id: string, users: string[]) => Promise<boolean>;
58
+ }
59
+ export declare class LarkEndpoint extends Endpoint<LarkClient> {
60
+ #private;
61
+ readonly client: LarkClient;
62
+ /**
63
+ * Console 社交面语义端口。Lark OpenAPI 仅接读取类列表能力:
64
+ * - listGroups:GET /im/v1/chats(bot 所在群列表,分页归一为 {group_id: chat_id, name})
65
+ * - listGroupMembers:GET /im/v1/chats/:chat_id/members(open_id 分页,返回平台原生成员形状)
66
+ * 不接的项:listFriends(Lark 无好友概念,contact API 为组织通讯录而非社交关系)、
67
+ * listChannels(无频道/guild 概念)、写操作(审批/踢人/禁言等本轮不在范围内)。
68
+ */
69
+ readonly management: EndpointManagement;
70
+ readonly control: EndpointControl;
71
+ constructor(options: LarkEndpointOptions);
72
+ /** Used by webhook handler. */
73
+ get isOpen(): boolean;
74
+ get config(): ResolvedLarkConfig;
75
+ start(): Promise<void>;
76
+ open(): void;
77
+ close(): void;
78
+ stop(): Promise<void>;
79
+ send({ conversation, payload }: EndpointSendRequest): Promise<string>;
80
+ recallMessage(messageId: string): Promise<void>;
81
+ /** POST /im/v1/images(image_type=message),返回 image_key;失败返回 null。 */
82
+ uploadImage(media: Parameters<typeof resolveMediaBinary>[0]): Promise<string | null>;
83
+ /** Test / internal: admit a parsed message when open (non-webhook path). */
84
+ admit(msg: LarkMessage): void;
85
+ admitPlatform(name: string, event: unknown): void;
86
+ getUserInfo(userId: string, userIdType?: 'open_id' | 'user_id' | 'union_id'): Promise<unknown>;
87
+ getChatInfo(chatId: string): Promise<unknown>;
88
+ uploadFile(filePath: string, fileType?: 'image' | 'file' | 'video' | 'audio'): Promise<string | null>;
89
+ createChat(name: string, userIds: string[], ownerId?: string): Promise<string | null>;
90
+ updateChatInfo(chatId: string, options: {
91
+ name?: string;
92
+ description?: string;
93
+ }): Promise<boolean>;
94
+ addChatMembers(chatId: string, userIds: string[]): Promise<boolean>;
95
+ removeChatMembers(chatId: string, userIds: string[]): Promise<boolean>;
96
+ getChatMembers(chatId: string): Promise<unknown[]>;
97
+ dissolveChat(chatId: string): Promise<boolean>;
98
+ setChatManagers(chatId: string, userIds: string[]): Promise<boolean>;
99
+ removeChatManagers(chatId: string, userIds: string[]): Promise<boolean>;
100
+ }