@zhin.js/adapter-napcat 3.0.1 → 4.0.1

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 (140) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/README.md +43 -96
  3. package/adapters/napcat.ts +39 -0
  4. package/agent/tools/ai_tts.ts +21 -0
  5. package/agent/tools/del_group_notice.ts +22 -0
  6. package/agent/tools/delete_essence_msg.ts +21 -0
  7. package/agent/tools/delete_friend.ts +20 -0
  8. package/agent/tools/download_file.ts +19 -0
  9. package/agent/tools/forward_single_msg.ts +32 -0
  10. package/agent/tools/get_ai_characters.ts +19 -0
  11. package/agent/tools/get_essence_list.ts +19 -0
  12. package/agent/tools/get_friend_msg_history.ts +20 -0
  13. package/agent/tools/get_group_file_url.ts +21 -0
  14. package/agent/tools/get_group_info_ex.ts +19 -0
  15. package/agent/tools/get_group_msg_history.ts +21 -0
  16. package/agent/tools/get_group_notice.ts +19 -0
  17. package/agent/tools/get_group_root_files.ts +19 -0
  18. package/agent/tools/get_group_shut_list.ts +19 -0
  19. package/agent/tools/get_mini_app_ark.ts +22 -0
  20. package/agent/tools/get_user_status.ts +18 -0
  21. package/agent/tools/group_sign.ts +20 -0
  22. package/agent/tools/mark_msg_as_read.ts +19 -0
  23. package/agent/tools/ocr_image.ts +18 -0
  24. package/agent/tools/send_forward_msg.ts +31 -0
  25. package/agent/tools/send_group_notice.ts +23 -0
  26. package/agent/tools/send_like.ts +20 -0
  27. package/agent/tools/send_poke.ts +20 -0
  28. package/agent/tools/set_avatar.ts +19 -0
  29. package/agent/tools/set_emoji_reaction.ts +20 -0
  30. package/agent/tools/set_essence_msg.ts +21 -0
  31. package/agent/tools/set_group_portrait.ts +22 -0
  32. package/agent/tools/set_online_status.ts +20 -0
  33. package/agent/tools/set_profile.ts +23 -0
  34. package/agent/tools/set_signature.ts +19 -0
  35. package/agent/tools/set_title.ts +23 -0
  36. package/agent/tools/translate.ts +18 -0
  37. package/agent/tools/upload_group_file.ts +23 -0
  38. package/lib/http-endpoint.d.ts +26 -0
  39. package/lib/http-endpoint.js +147 -0
  40. package/lib/index.d.ts +11 -21
  41. package/lib/index.js +10 -59
  42. package/lib/napcat-agent-deps.d.ts +51 -0
  43. package/lib/napcat-agent-deps.js +33 -0
  44. package/lib/napcat-inbound.d.ts +12 -5
  45. package/lib/napcat-inbound.js +28 -13
  46. package/lib/onebot-get-msg.d.ts +13 -2
  47. package/lib/onebot-get-msg.js +0 -1
  48. package/lib/protocol.d.ts +151 -0
  49. package/lib/protocol.js +217 -0
  50. package/lib/webhook.d.ts +2 -0
  51. package/lib/webhook.js +14 -0
  52. package/lib/ws-endpoint.d.ts +61 -0
  53. package/lib/ws-endpoint.js +357 -0
  54. package/lib/ws-transport.d.ts +13 -0
  55. package/lib/ws-transport.js +75 -0
  56. package/lib/ws-types.d.ts +17 -0
  57. package/lib/ws-types.js +1 -0
  58. package/lib/wss-auth.d.ts +2 -0
  59. package/lib/wss-auth.js +16 -0
  60. package/lib/wss-endpoint.d.ts +25 -0
  61. package/lib/wss-endpoint.js +157 -0
  62. package/package.json +48 -32
  63. package/plugin.ts +13 -0
  64. package/schema.json +48 -0
  65. package/src/http-endpoint.ts +197 -0
  66. package/src/index.ts +66 -82
  67. package/src/napcat-agent-deps.ts +95 -0
  68. package/src/napcat-inbound.ts +34 -10
  69. package/src/onebot-get-msg.ts +8 -2
  70. package/src/protocol.ts +373 -0
  71. package/src/webhook.ts +16 -0
  72. package/src/ws-endpoint.ts +452 -0
  73. package/src/ws-transport.ts +105 -0
  74. package/src/ws-types.ts +20 -0
  75. package/src/wss-auth.ts +17 -0
  76. package/src/wss-endpoint.ts +205 -0
  77. package/client/NapCatManagement.tsx +0 -113
  78. package/client/index.tsx +0 -11
  79. package/client/tsconfig.json +0 -7
  80. package/client/utils/api.ts +0 -30
  81. package/dist/index.js +0 -27
  82. package/lib/adapter.d.ts +0 -36
  83. package/lib/adapter.d.ts.map +0 -1
  84. package/lib/adapter.js +0 -102
  85. package/lib/adapter.js.map +0 -1
  86. package/lib/agent-prompt.d.ts +0 -3
  87. package/lib/agent-prompt.d.ts.map +0 -1
  88. package/lib/agent-prompt.js +0 -106
  89. package/lib/agent-prompt.js.map +0 -1
  90. package/lib/endpoint-base.d.ts +0 -144
  91. package/lib/endpoint-base.d.ts.map +0 -1
  92. package/lib/endpoint-base.js +0 -341
  93. package/lib/endpoint-base.js.map +0 -1
  94. package/lib/endpoint-http.d.ts +0 -17
  95. package/lib/endpoint-http.d.ts.map +0 -1
  96. package/lib/endpoint-http.js +0 -106
  97. package/lib/endpoint-http.js.map +0 -1
  98. package/lib/endpoint-ws-client.d.ts +0 -20
  99. package/lib/endpoint-ws-client.d.ts.map +0 -1
  100. package/lib/endpoint-ws-client.js +0 -139
  101. package/lib/endpoint-ws-client.js.map +0 -1
  102. package/lib/endpoint-ws-server.d.ts +0 -20
  103. package/lib/endpoint-ws-server.d.ts.map +0 -1
  104. package/lib/endpoint-ws-server.js +0 -137
  105. package/lib/endpoint-ws-server.js.map +0 -1
  106. package/lib/index.d.ts.map +0 -1
  107. package/lib/index.js.map +0 -1
  108. package/lib/napcat-inbound.d.ts.map +0 -1
  109. package/lib/napcat-inbound.js.map +0 -1
  110. package/lib/onebot-get-msg.d.ts.map +0 -1
  111. package/lib/onebot-get-msg.js.map +0 -1
  112. package/lib/routes.d.ts +0 -4
  113. package/lib/routes.d.ts.map +0 -1
  114. package/lib/routes.js +0 -61
  115. package/lib/routes.js.map +0 -1
  116. package/lib/segment-mapper.d.ts +0 -2
  117. package/lib/segment-mapper.d.ts.map +0 -1
  118. package/lib/segment-mapper.js +0 -2
  119. package/lib/segment-mapper.js.map +0 -1
  120. package/lib/tools.d.ts +0 -8
  121. package/lib/tools.d.ts.map +0 -1
  122. package/lib/tools.js +0 -605
  123. package/lib/tools.js.map +0 -1
  124. package/lib/types.d.ts +0 -275
  125. package/lib/types.d.ts.map +0 -1
  126. package/lib/types.js +0 -6
  127. package/lib/types.js.map +0 -1
  128. package/plugin.yml +0 -3
  129. package/src/adapter.ts +0 -109
  130. package/src/agent-prompt.ts +0 -116
  131. package/src/endpoint-base.ts +0 -386
  132. package/src/endpoint-http.ts +0 -95
  133. package/src/endpoint-ws-client.ts +0 -137
  134. package/src/endpoint-ws-server.ts +0 -136
  135. package/src/routes.ts +0 -61
  136. package/src/segment-mapper.ts +0 -1
  137. package/src/tools.ts +0 -632
  138. package/src/types.ts +0 -266
  139. /package/{skills/napcat → agent}/PERMITS.md +0 -0
  140. /package/{skills/napcat/SKILL.md → agent/skills/napcat.md} +0 -0
@@ -0,0 +1,18 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; user_id: number }>({
6
+ description: '获取用户在线状态。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ user_id: z.number().describe('目标 QQ 号'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['在线状态', '用户状态', 'online status'],
14
+ async execute({ endpoint_id, user_id }: { endpoint_id: string; user_id: number }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ return endpoint.ncGetUserStatus(user_id);
17
+ },
18
+ });
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; group_id: number }>({
6
+ description: '群签到/群打卡。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ group_id: z.number().describe('群号'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['签到', '打卡', 'sign', 'check in'],
14
+ scopes: ['group'],
15
+ async execute({ endpoint_id, group_id }: { endpoint_id: string; group_id: number }) {
16
+ const endpoint = getEndpoint(endpoint_id);
17
+ await endpoint.sendGroupSign(group_id);
18
+ return { success: true };
19
+ },
20
+ });
@@ -0,0 +1,19 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; message_id: number }>({
6
+ description: '标记消息为已读。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ message_id: z.number().describe('消息 ID'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['已读', 'mark read', '标记已读'],
14
+ async execute({ endpoint_id, message_id }: { endpoint_id: string; message_id: number }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ await endpoint.markMsgAsRead(message_id);
17
+ return { success: true };
18
+ },
19
+ });
@@ -0,0 +1,18 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; image: string }>({
6
+ description: '图片 OCR 文字识别。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ image: z.string().describe('图片 file 参数(收到消息中的 file 字段或 URL)'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['OCR', '文字识别', '图片识别', 'ocr'],
14
+ async execute({ endpoint_id, image }: { endpoint_id: string; image: string }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ return endpoint.ocrImage(image);
17
+ },
18
+ });
@@ -0,0 +1,31 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{
6
+ endpoint_id: string;
7
+ message_type: 'private' | 'group';
8
+ id: number;
9
+ messages: string;
10
+ }>({
11
+ description: '发送合并转发消息(群聊或私聊)。messages 为转发节点数组。',
12
+ inputSchema: z.object({
13
+ endpoint_id: z.string().describe('Endpoint 名称'),
14
+ message_type: z.enum(['private', 'group']).describe('private 或 group'),
15
+ id: z.number().describe('群号或 QQ 号'),
16
+ messages: z.string().describe('转发节点 JSON(node 数组)'),
17
+ }),
18
+ platforms: ['napcat'],
19
+ tags: ['napcat', 'qq'],
20
+ keywords: ['合并转发', 'forward', '转发消息'],
21
+ async execute({ endpoint_id, message_type, id, messages }: {
22
+ endpoint_id: string;
23
+ message_type: 'private' | 'group';
24
+ id: number;
25
+ messages: string;
26
+ }) {
27
+ const endpoint = getEndpoint(endpoint_id);
28
+ const nodes = typeof messages === 'string' ? JSON.parse(messages) : messages;
29
+ return endpoint.sendForwardMsg(message_type, id, nodes);
30
+ },
31
+ });
@@ -0,0 +1,23 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; group_id: number; content: string; image?: string }>({
6
+ description: '发送群公告。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ group_id: z.number().describe('群号'),
10
+ content: z.string().describe('公告内容'),
11
+ image: z.string().optional().describe('图片(URL 或 base64,可选)'),
12
+ }),
13
+ platforms: ['napcat'],
14
+ tags: ['napcat', 'qq'],
15
+ keywords: ['群公告', 'notice', '公告', '发公告'],
16
+ permissions: ['platform(napcat,scene_admin)'],
17
+ scopes: ['group'],
18
+ async execute({ endpoint_id, group_id, content, image }: { endpoint_id: string; group_id: number; content: string; image?: string }) {
19
+ const endpoint = getEndpoint(endpoint_id);
20
+ await endpoint.sendGroupNotice(group_id, content, image);
21
+ return { success: true };
22
+ },
23
+ });
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; user_id: number; times?: number }>({
6
+ description: '给好友点赞(每人每天最多 10 次)。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ user_id: z.number().describe('目标 QQ 号'),
10
+ times: z.number().optional().describe('点赞次数(1-10)'),
11
+ }),
12
+ platforms: ['napcat'],
13
+ tags: ['napcat', 'qq'],
14
+ keywords: ['点赞', 'like', '赞', '好友赞'],
15
+ async execute({ endpoint_id, user_id, times }: { endpoint_id: string; user_id: number; times?: number }) {
16
+ const endpoint = getEndpoint(endpoint_id);
17
+ await endpoint.sendLike(user_id, times || 1);
18
+ return { success: true, message: `已给 ${user_id} 点赞 ${times || 1} 次` };
19
+ },
20
+ });
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; user_id: number; group_id?: number }>({
6
+ description: '戳一戳(群聊或私聊)。group_id 不传时为私聊戳一戳。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ user_id: z.number().describe('目标用户 QQ 号'),
10
+ group_id: z.number().optional().describe('群号(不传则为私聊戳一戳)'),
11
+ }),
12
+ platforms: ['napcat'],
13
+ tags: ['napcat', 'qq'],
14
+ keywords: ['戳一戳', 'poke', '戳', '拍一拍'],
15
+ async execute({ endpoint_id, user_id, group_id }: { endpoint_id: string; user_id: number; group_id?: number }) {
16
+ const endpoint = getEndpoint(endpoint_id);
17
+ await endpoint.sendPoke(user_id, group_id);
18
+ return { success: true, message: `已戳 ${user_id}` };
19
+ },
20
+ });
@@ -0,0 +1,19 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; file: string }>({
6
+ description: '修改 QQ 头像。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ file: z.string().describe('图片(URL 或 base64)'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['头像', 'avatar', '设置头像', '换头像'],
14
+ async execute({ endpoint_id, file }: { endpoint_id: string; file: string }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ await endpoint.setQQAvatar(file);
17
+ return { success: true };
18
+ },
19
+ });
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; message_id: number; emoji_id: string }>({
6
+ description: '为消息添加表情回应(贴表情)。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ message_id: z.number().describe('消息 ID'),
10
+ emoji_id: z.string().describe('表情 ID'),
11
+ }),
12
+ platforms: ['napcat'],
13
+ tags: ['napcat', 'qq'],
14
+ keywords: ['表情回应', 'reaction', '贴表情', 'emoji'],
15
+ async execute({ endpoint_id, message_id, emoji_id }: { endpoint_id: string; message_id: number; emoji_id: string }) {
16
+ const endpoint = getEndpoint(endpoint_id);
17
+ await endpoint.setMsgEmojiLike(message_id, emoji_id);
18
+ return { success: true };
19
+ },
20
+ });
@@ -0,0 +1,21 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; message_id: number }>({
6
+ description: '设置群精华消息。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ message_id: z.number().describe('消息 ID'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['精华', 'essence', '设精', '加精'],
14
+ permissions: ['platform(napcat,scene_admin)'],
15
+ scopes: ['group'],
16
+ async execute({ endpoint_id, message_id }: { endpoint_id: string; message_id: number }) {
17
+ const endpoint = getEndpoint(endpoint_id);
18
+ await endpoint.setEssenceMsg(message_id);
19
+ return { success: true };
20
+ },
21
+ });
@@ -0,0 +1,22 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; group_id: number; file: string }>({
6
+ description: '设置群头像。file 为图片 URL 或 base64。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ group_id: z.number().describe('群号'),
10
+ file: z.string().describe('图片(URL 或 base64)'),
11
+ }),
12
+ platforms: ['napcat'],
13
+ tags: ['napcat', 'qq'],
14
+ keywords: ['群头像', 'group portrait', '设置群头像'],
15
+ permissions: ['platform(napcat,scene_admin)'],
16
+ scopes: ['group'],
17
+ async execute({ endpoint_id, group_id, file }: { endpoint_id: string; group_id: number; file: string }) {
18
+ const endpoint = getEndpoint(endpoint_id);
19
+ await endpoint.setGroupPortrait(group_id, file);
20
+ return { success: true };
21
+ },
22
+ });
@@ -0,0 +1,20 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; status: number; ext_status?: number }>({
6
+ description: '设置在线状态(在线、隐身、忙碌等)。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ status: z.number().describe('状态码(11=在线, 21=离开, 31=隐身, 41=忙碌, 50=请勿打扰, 60=Q我吧)'),
10
+ ext_status: z.number().optional().describe('扩展状态码'),
11
+ }),
12
+ platforms: ['napcat'],
13
+ tags: ['napcat', 'qq'],
14
+ keywords: ['在线状态', '隐身', '忙碌', 'online', 'status'],
15
+ async execute({ endpoint_id, status, ext_status }: { endpoint_id: string; status: number; ext_status?: number }) {
16
+ const endpoint = getEndpoint(endpoint_id);
17
+ await endpoint.setOnlineStatus(status, ext_status || 0);
18
+ return { success: true };
19
+ },
20
+ });
@@ -0,0 +1,23 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; nickname: string; company?: string; email?: string; college?: string; personal_note?: string }>({
6
+ description: '修改 QQ 资料(昵称等)。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ nickname: z.string().describe('昵称'),
10
+ company: z.string().optional().describe('公司(可选)'),
11
+ email: z.string().optional().describe('邮箱(可选)'),
12
+ college: z.string().optional().describe('学校(可选)'),
13
+ personal_note: z.string().optional().describe('个人说明(可选)'),
14
+ }),
15
+ platforms: ['napcat'],
16
+ tags: ['napcat', 'qq'],
17
+ keywords: ['修改资料', '设置昵称', 'profile', 'set profile'],
18
+ async execute({ endpoint_id, nickname, company, email, college, personal_note }: { endpoint_id: string; nickname: string; company?: string; email?: string; college?: string; personal_note?: string }) {
19
+ const endpoint = getEndpoint(endpoint_id);
20
+ await endpoint.setQQProfile(nickname, company, email, college, personal_note);
21
+ return { success: true };
22
+ },
23
+ });
@@ -0,0 +1,19 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; signature: string }>({
6
+ description: '设置个人签名(个性签名)。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ signature: z.string().describe('签名内容'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['签名', '个性签名', 'signature', 'longnick'],
14
+ async execute({ endpoint_id, signature }: { endpoint_id: string; signature: string }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ await endpoint.setSelfLongnick(signature);
17
+ return { success: true };
18
+ },
19
+ });
@@ -0,0 +1,23 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; group_id: number; user_id: number; title: string }>({
6
+ description: '设置群成员专属头衔。需要群主权限。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ group_id: z.number().describe('群号'),
10
+ user_id: z.number().describe('目标成员 QQ 号'),
11
+ title: z.string().describe('头衔文字'),
12
+ }),
13
+ platforms: ['napcat'],
14
+ tags: ['napcat', 'qq'],
15
+ keywords: ['头衔', 'title', '专属头衔', '设置头衔'],
16
+ permissions: ['platform(napcat,scene_owner)'],
17
+ scopes: ['group'],
18
+ async execute({ endpoint_id, group_id, user_id, title }: { endpoint_id: string; group_id: number; user_id: number; title: string }) {
19
+ const endpoint = getEndpoint(endpoint_id);
20
+ await endpoint.setTitle(group_id, user_id, title);
21
+ return { success: true, message: `已设置 ${user_id} 头衔为 "${title}"` };
22
+ },
23
+ });
@@ -0,0 +1,18 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; text: string }>({
6
+ description: '英译中翻译。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ text: z.string().describe('要翻译的英文文本'),
10
+ }),
11
+ platforms: ['napcat'],
12
+ tags: ['napcat', 'qq'],
13
+ keywords: ['翻译', 'translate', '英译中'],
14
+ async execute({ endpoint_id, text }: { endpoint_id: string; text: string }) {
15
+ const endpoint = getEndpoint(endpoint_id);
16
+ return endpoint.translateEn2Zh(text);
17
+ },
18
+ });
@@ -0,0 +1,23 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getEndpoint } from '../../src/napcat-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; group_id: number; file: string; name: string; folder?: string }>({
6
+ description: '上传文件到群。file 为本地路径或 URL。',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ group_id: z.number().describe('群号'),
10
+ file: z.string().describe('文件路径或 URL'),
11
+ name: z.string().describe('文件名'),
12
+ folder: z.string().optional().describe('目标文件夹 ID(可选)'),
13
+ }),
14
+ platforms: ['napcat'],
15
+ tags: ['napcat', 'qq'],
16
+ keywords: ['上传文件', 'upload file', '群文件'],
17
+ scopes: ['group'],
18
+ async execute({ endpoint_id, group_id, file, name, folder }: { endpoint_id: string; group_id: number; file: string; name: string; folder?: string }) {
19
+ const endpoint = getEndpoint(endpoint_id);
20
+ await endpoint.uploadGroupFile(group_id, file, name, folder);
21
+ return { success: true };
22
+ },
23
+ });
@@ -0,0 +1,26 @@
1
+ import type { EndpointInstance } from '@zhin.js/adapter';
2
+ import type { MessageGateway } from '@zhin.js/core/runtime';
3
+ import type { HttpHost } from '@zhin.js/host-http';
4
+ import type { CapabilityId } from '@zhin.js/plugin-runtime';
5
+ import { callNapCatHttpAction, type NapCatEvent, type NapCatHttpConfig } from './protocol.js';
6
+ export interface NapCatHttpEndpointOptions {
7
+ readonly id: CapabilityId;
8
+ readonly gateway: MessageGateway;
9
+ readonly http: HttpHost;
10
+ readonly config: NapCatHttpConfig;
11
+ readonly callHttpAction?: typeof callNapCatHttpAction;
12
+ }
13
+ export declare class NapCatHttpEndpoint implements EndpointInstance {
14
+ #private;
15
+ constructor(options: NapCatHttpEndpointOptions);
16
+ start(): Promise<void>;
17
+ open(): void;
18
+ close(): void;
19
+ stop(): Promise<void>;
20
+ send({ target, payload }: {
21
+ readonly target: string;
22
+ readonly payload: unknown;
23
+ }): Promise<string>;
24
+ callApi(action: string, params?: Record<string, unknown>): Promise<unknown>;
25
+ admit(ev: NapCatEvent): void;
26
+ }
@@ -0,0 +1,147 @@
1
+ import { formatCompact, getLogger } from '@zhin.js/logger';
2
+ import { registerNapcatAgentEndpoint } from './napcat-agent-deps.js';
3
+ import { InboundMessageDeduper, isNapCatBotMentioned, isSelfMessage, normalizeMessage, } from './napcat-inbound.js';
4
+ import { buildSendAction, callNapCatHttpAction, formatInboundContent, formatInboundTarget, formatOutboundSegments, isMessageEvent, senderNickname, senderUserId, } from './protocol.js';
5
+ import { readRequestBody } from './webhook.js';
6
+ import { verifyNapCatAccessToken } from './wss-auth.js';
7
+ const logger = getLogger('napcat');
8
+ export class NapCatHttpEndpoint {
9
+ #options;
10
+ #inboundDeduper = new InboundMessageDeduper();
11
+ #callHttpAction;
12
+ #routeReleases = [];
13
+ #open = false;
14
+ #started = false;
15
+ #unregisterAgent;
16
+ constructor(options) {
17
+ this.#options = options;
18
+ this.#callHttpAction = options.callHttpAction ?? callNapCatHttpAction;
19
+ }
20
+ async start() {
21
+ if (this.#started)
22
+ return;
23
+ this.#started = true;
24
+ this.#unregisterAgent = registerNapcatAgentEndpoint(this.#options.config.name, this);
25
+ this.#setupRoutes();
26
+ logger.info(formatCompact({
27
+ op: 'listen',
28
+ endpoint: this.#options.config.name,
29
+ mode: 'http',
30
+ path: this.#options.config.post_path,
31
+ }));
32
+ }
33
+ open() {
34
+ this.#open = true;
35
+ }
36
+ close() {
37
+ this.#open = false;
38
+ }
39
+ async stop() {
40
+ this.#open = false;
41
+ for (const release of this.#routeReleases.splice(0))
42
+ release();
43
+ this.#unregisterAgent?.();
44
+ this.#unregisterAgent = undefined;
45
+ this.#inboundDeduper.clear();
46
+ this.#started = false;
47
+ logger.debug(formatCompact({ op: 'disconnect', endpoint: this.#options.config.name }));
48
+ }
49
+ async send({ target, payload }) {
50
+ const message = formatOutboundSegments(payload);
51
+ const { action, params } = buildSendAction(target, message);
52
+ const resp = await this.#callHttpAction({
53
+ http_url: this.#options.config.http_url,
54
+ access_token: this.#options.config.access_token,
55
+ }, action, params);
56
+ const data = resp.data;
57
+ const messageId = data?.message_id != null ? String(data.message_id) : '';
58
+ logger.debug(formatCompact({
59
+ op: 'napcat_send',
60
+ endpoint: this.#options.config.name,
61
+ target,
62
+ messageId,
63
+ }));
64
+ return messageId;
65
+ }
66
+ callApi(action, params = {}) {
67
+ return this.#callHttpAction({
68
+ http_url: this.#options.config.http_url,
69
+ access_token: this.#options.config.access_token,
70
+ }, action, params).then((resp) => resp.data);
71
+ }
72
+ admit(ev) {
73
+ if (!this.#open || !isMessageEvent(ev))
74
+ return;
75
+ if (isSelfMessage(ev))
76
+ return;
77
+ const msgId = String(ev.message_id);
78
+ if (!this.#inboundDeduper.shouldProcess(msgId))
79
+ return;
80
+ if (Array.isArray(ev.message) || typeof ev.message === 'string') {
81
+ ev = { ...ev, message: normalizeMessage(ev.message) };
82
+ }
83
+ const target = formatInboundTarget(ev);
84
+ const nickname = senderNickname(ev);
85
+ const mentioned = isNapCatBotMentioned(ev);
86
+ void this.#options.gateway.receive({
87
+ adapter: this.#options.id,
88
+ target,
89
+ content: formatInboundContent(ev),
90
+ sender: senderUserId(ev),
91
+ id: msgId,
92
+ metadata: Object.freeze({
93
+ message_type: ev.message_type,
94
+ user_id: ev.user_id != null ? String(ev.user_id) : undefined,
95
+ group_id: ev.group_id != null ? String(ev.group_id) : undefined,
96
+ endpoint: this.#options.config.name,
97
+ time: ev.time,
98
+ self_id: ev.self_id != null ? String(ev.self_id) : undefined,
99
+ role: ev.sender?.role,
100
+ ...(nickname ? { nickname } : {}),
101
+ ...(mentioned ? { mentioned: true } : {}),
102
+ }),
103
+ }).catch((err) => {
104
+ logger.warn(formatCompact({
105
+ op: 'napcat_gateway_receive_failed',
106
+ target,
107
+ error: err instanceof Error ? err.message : String(err),
108
+ }));
109
+ });
110
+ }
111
+ #setupRoutes() {
112
+ const path = this.#options.config.post_path;
113
+ this.#routeReleases.push(this.#options.http.route('POST', path, async (request, response) => {
114
+ await this.#handlePost(request, response);
115
+ }, { summary: 'NapCat HTTP event callback', tags: ['napcat'] }));
116
+ }
117
+ async #handlePost(request, response) {
118
+ try {
119
+ if (!verifyNapCatAccessToken(this.#options.config.access_token, request)) {
120
+ response.writeHead(403, { 'Content-Type': 'application/json' });
121
+ response.end(JSON.stringify({ message: 'Unauthorized' }));
122
+ return;
123
+ }
124
+ const raw = await readRequestBody(request);
125
+ let ev;
126
+ try {
127
+ ev = JSON.parse(raw);
128
+ }
129
+ catch {
130
+ response.writeHead(400, { 'Content-Type': 'application/json' });
131
+ response.end(JSON.stringify({ message: 'Invalid JSON' }));
132
+ return;
133
+ }
134
+ if (this.#open)
135
+ this.admit(ev);
136
+ response.writeHead(200, { 'Content-Type': 'application/json' });
137
+ response.end(JSON.stringify({ status: 'ok' }));
138
+ }
139
+ catch (error) {
140
+ logger.error('NapCat HTTP webhook error:', error);
141
+ if (!response.headersSent) {
142
+ response.writeHead(500, { 'Content-Type': 'application/json' });
143
+ response.end(JSON.stringify({ message: 'Internal Server Error' }));
144
+ }
145
+ }
146
+ }
147
+ }
package/lib/index.d.ts CHANGED
@@ -1,21 +1,11 @@
1
- import type { Router } from '@zhin.js/host-router';
2
- import { PageManager } from '@zhin.js/host-api';
3
- import { NapCatAdapter } from './adapter.js';
4
- export * from './types.js';
5
- export { NapCatWsClient } from './endpoint-ws-client.js';
6
- export { NapCatWsServer } from './endpoint-ws-server.js';
7
- export { NapCatHttpEndpoint } from './endpoint-http.js';
8
- export { NapCatAdapter, type NapCatEndpoint } from './adapter.js';
9
- export { NapCatEndpointBase } from './endpoint-base.js';
10
- declare module 'zhin.js' {
11
- namespace Plugin {
12
- interface Contexts {
13
- web: PageManager;
14
- router: Router;
15
- }
16
- }
17
- interface Adapters {
18
- napcat: NapCatAdapter;
19
- }
20
- }
21
- //# sourceMappingURL=index.d.ts.map
1
+ export { buildSendAction, buildWsConnectOptions, callNapCatHttpAction, formatInboundContent, formatInboundTarget, formatOutboundSegments, getChannelId, isMessageEvent, parseSendTarget, resolveNapCatConfig, senderNickname, senderUserId, type MessageSegment, type NapCatActionRequest, type NapCatActionResponse, type NapCatAdapterConfig, type NapCatConfigBase, type NapCatEndpointConfig, type NapCatEvent, type NapCatHttpConfig, type NapCatMessageEvent, type NapCatSender, type NapCatWireSegment, type NapCatWsConfig, type NapCatWssConfig, type ParsedSendTarget, type ResolvedNapCatConfig, } from './protocol.js';
2
+ export { InboundMessageDeduper, isNapCatBotMentioned, isSelfMessage, normalizeMessage, } from './napcat-inbound.js';
3
+ export { getEndpoint, getNapcatAgentDeps, registerNapcatAgentEndpoint, setNapcatAgentDeps, type NapcatAgentDeps, type NapcatAgentEndpoint, } from './napcat-agent-deps.js';
4
+ export { parseOneBotGetMsgResponse } from './onebot-get-msg.js';
5
+ export { NapCatWsEndpoint, type NapCatWsEndpointOptions, } from './ws-endpoint.js';
6
+ export { NapCatWssEndpoint, type NapCatWssEndpointOptions, } from './wss-endpoint.js';
7
+ export { NapCatHttpEndpoint, type NapCatHttpEndpointOptions, } from './http-endpoint.js';
8
+ export type { NapCatWsSocket, NapCatWsCreateOptions } from './ws-types.js';
9
+ export { callNapCatWsAction, decodeWsPayload, handleNapCatWsMessage, rejectAllPending, startNapCatHeartbeat, } from './ws-transport.js';
10
+ export { verifyNapCatAccessToken } from './wss-auth.js';
11
+ export { readRequestBody } from './webhook.js';