@zhin.js/adapter-kook 5.0.1 → 5.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.
Files changed (97) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +58 -451
  3. package/adapters/kook.ts +46 -0
  4. package/agent/tools/blacklist.ts +33 -0
  5. package/agent/tools/create_role.ts +25 -0
  6. package/agent/tools/delete_role.ts +21 -0
  7. package/agent/tools/grant_role.ts +22 -0
  8. package/agent/tools/list_roles.ts +27 -0
  9. package/agent/tools/revoke_role.ts +22 -0
  10. package/lib/endpoint.d.ts +80 -151
  11. package/lib/endpoint.js +260 -736
  12. package/lib/index.d.ts +6 -18
  13. package/lib/index.js +6 -350
  14. package/lib/kook-agent-deps.d.ts +29 -0
  15. package/lib/kook-agent-deps.js +30 -0
  16. package/lib/platform-permit.d.ts +12 -3
  17. package/lib/platform-permit.js +5 -3
  18. package/lib/protocol.d.ts +147 -0
  19. package/lib/protocol.js +279 -0
  20. package/lib/webhook.d.ts +15 -0
  21. package/lib/webhook.js +76 -0
  22. package/lib/ws.d.ts +42 -0
  23. package/lib/ws.js +63 -0
  24. package/package.json +47 -33
  25. package/plugin.ts +13 -0
  26. package/schema.json +55 -0
  27. package/src/endpoint.ts +287 -866
  28. package/src/index.ts +55 -331
  29. package/src/kook-agent-deps.ts +55 -0
  30. package/src/platform-permit.ts +13 -4
  31. package/src/protocol.ts +429 -0
  32. package/src/webhook.ts +116 -0
  33. package/src/ws.ts +120 -0
  34. package/client/Dashboard.tsx +0 -255
  35. package/client/index.tsx +0 -11
  36. package/client/tsconfig.json +0 -7
  37. package/client/utils/api.ts +0 -30
  38. package/dist/index.js +0 -31
  39. package/lib/adapter.d.ts +0 -30
  40. package/lib/adapter.d.ts.map +0 -1
  41. package/lib/adapter.js +0 -67
  42. package/lib/adapter.js.map +0 -1
  43. package/lib/endpoint.d.ts.map +0 -1
  44. package/lib/endpoint.js.map +0 -1
  45. package/lib/index.d.ts.map +0 -1
  46. package/lib/index.js.map +0 -1
  47. package/lib/kook-asset-upload.d.ts +0 -12
  48. package/lib/kook-asset-upload.d.ts.map +0 -1
  49. package/lib/kook-asset-upload.js +0 -46
  50. package/lib/kook-asset-upload.js.map +0 -1
  51. package/lib/kook-inbound.d.ts +0 -6
  52. package/lib/kook-inbound.d.ts.map +0 -1
  53. package/lib/kook-inbound.js +0 -22
  54. package/lib/kook-inbound.js.map +0 -1
  55. package/lib/kook-msg-route.d.ts +0 -34
  56. package/lib/kook-msg-route.d.ts.map +0 -1
  57. package/lib/kook-msg-route.js +0 -106
  58. package/lib/kook-msg-route.js.map +0 -1
  59. package/lib/kook-side-events.d.ts +0 -46
  60. package/lib/kook-side-events.d.ts.map +0 -1
  61. package/lib/kook-side-events.js +0 -189
  62. package/lib/kook-side-events.js.map +0 -1
  63. package/lib/outbound-keyboard.d.ts +0 -13
  64. package/lib/outbound-keyboard.d.ts.map +0 -1
  65. package/lib/outbound-keyboard.js +0 -63
  66. package/lib/outbound-keyboard.js.map +0 -1
  67. package/lib/outbound-media.d.ts +0 -10
  68. package/lib/outbound-media.d.ts.map +0 -1
  69. package/lib/outbound-media.js +0 -42
  70. package/lib/outbound-media.js.map +0 -1
  71. package/lib/outbound-sendable.d.ts +0 -12
  72. package/lib/outbound-sendable.d.ts.map +0 -1
  73. package/lib/outbound-sendable.js +0 -64
  74. package/lib/outbound-sendable.js.map +0 -1
  75. package/lib/platform-permit.d.ts.map +0 -1
  76. package/lib/platform-permit.js.map +0 -1
  77. package/lib/segment-mapper.d.ts +0 -2
  78. package/lib/segment-mapper.d.ts.map +0 -1
  79. package/lib/segment-mapper.js +0 -2
  80. package/lib/segment-mapper.js.map +0 -1
  81. package/lib/types.d.ts +0 -54
  82. package/lib/types.d.ts.map +0 -1
  83. package/lib/types.js +0 -8
  84. package/lib/types.js.map +0 -1
  85. package/plugin.yml +0 -3
  86. package/src/adapter.ts +0 -76
  87. package/src/kook-asset-upload.ts +0 -55
  88. package/src/kook-inbound.ts +0 -22
  89. package/src/kook-msg-route.ts +0 -122
  90. package/src/kook-side-events.ts +0 -250
  91. package/src/outbound-keyboard.ts +0 -78
  92. package/src/outbound-media.ts +0 -67
  93. package/src/outbound-sendable.ts +0 -73
  94. package/src/segment-mapper.ts +0 -1
  95. package/src/types.ts +0 -59
  96. /package/{skills/kook → agent}/PERMITS.md +0 -0
  97. /package/{skills/kook/SKILL.md → agent/skills/kook.md} +0 -0
@@ -0,0 +1,33 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
5
+
6
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string; action: 'add' | 'remove'; user_id: string; remark?: string }>({
7
+ description: 'KOOK 服务器黑名单管理:添加/移除',
8
+ inputSchema: z.object({
9
+ endpoint_id: z.string().describe('Endpoint 名称'),
10
+ guild_id: z.string().describe('服务器 ID'),
11
+ action: z.enum(['add', 'remove']).describe('add|remove'),
12
+ user_id: z.string().describe('用户 ID'),
13
+ remark: z.string().optional().describe('备注(add 可选)'),
14
+ }),
15
+ platforms: ['kook'],
16
+ tags: ['kook'],
17
+ permissions: [platformPermit('guild_admin')],
18
+ async execute({ endpoint_id, guild_id, action, user_id, remark }: { endpoint_id: string; guild_id: string; action: 'add' | 'remove'; user_id: string; remark?: string }) {
19
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
20
+ switch (action) {
21
+ case 'add': {
22
+ const success = await endpoint.addToBlacklist(guild_id, user_id, remark);
23
+ return { success, message: success ? `已将 ${user_id} 加入黑名单` : '操作失败' };
24
+ }
25
+ case 'remove': {
26
+ const success = await endpoint.removeFromBlacklist(guild_id, user_id);
27
+ return { success, message: success ? `已将 ${user_id} 从黑名单移除` : '操作失败' };
28
+ }
29
+ default:
30
+ return { success: false, message: `未知操作: ${action}` };
31
+ }
32
+ },
33
+ });
@@ -0,0 +1,25 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
5
+
6
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string; name: string }>({
7
+ description: '在 KOOK 服务器中创建新角色',
8
+ inputSchema: z.object({
9
+ endpoint_id: z.string().describe('Endpoint 名称'),
10
+ guild_id: z.string().describe('服务器 ID'),
11
+ name: z.string().describe('角色名称'),
12
+ }),
13
+ platforms: ['kook'],
14
+ tags: ['kook'],
15
+ permissions: [platformPermit('guild_owner')],
16
+ async execute({ endpoint_id, guild_id, name }: { endpoint_id: string; guild_id: string; name: string }) {
17
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
18
+ const role = await endpoint.createRole(guild_id, name);
19
+ return {
20
+ success: true,
21
+ message: `已创建角色 "${name}"`,
22
+ role: { id: role.role_id, name: role.name },
23
+ };
24
+ },
25
+ });
@@ -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
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
5
+
6
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string; role_id: string }>({
7
+ description: '删除 KOOK 服务器中的角色',
8
+ inputSchema: z.object({
9
+ endpoint_id: z.string().describe('Endpoint 名称'),
10
+ guild_id: z.string().describe('服务器 ID'),
11
+ role_id: z.string().describe('角色 ID'),
12
+ }),
13
+ platforms: ['kook'],
14
+ tags: ['kook'],
15
+ permissions: [platformPermit('guild_owner')],
16
+ async execute({ endpoint_id, guild_id, role_id }: { endpoint_id: string; guild_id: string; role_id: string }) {
17
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
18
+ const success = await endpoint.deleteRole(guild_id, role_id);
19
+ return { success, message: success ? `已删除角色 ${role_id}` : '删除角色失败' };
20
+ },
21
+ });
@@ -0,0 +1,22 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
5
+
6
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string; user_id: string; role_id: string }>({
7
+ description: '给用户授予 KOOK 服务器角色',
8
+ inputSchema: z.object({
9
+ endpoint_id: z.string().describe('Endpoint 名称'),
10
+ guild_id: z.string().describe('服务器 ID'),
11
+ user_id: z.string().describe('用户 ID'),
12
+ role_id: z.string().describe('角色 ID'),
13
+ }),
14
+ platforms: ['kook'],
15
+ tags: ['kook'],
16
+ permissions: [platformPermit('manage_roles')],
17
+ async execute({ endpoint_id, guild_id, user_id, role_id }: { endpoint_id: string; guild_id: string; user_id: string; role_id: string }) {
18
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
19
+ const success = await endpoint.grantRole(guild_id, user_id, role_id);
20
+ return { success, message: success ? `已授予用户 ${user_id} 角色 ${role_id}` : '授予角色失败' };
21
+ },
22
+ });
@@ -0,0 +1,27 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
4
+
5
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string }>({
6
+ description: '获取 KOOK 服务器的角色列表',
7
+ inputSchema: z.object({
8
+ endpoint_id: z.string().describe('Endpoint 名称'),
9
+ guild_id: z.string().describe('服务器 ID'),
10
+ }),
11
+ platforms: ['kook'],
12
+ tags: ['kook'],
13
+ async execute({ endpoint_id, guild_id }: { endpoint_id: string; guild_id: string }) {
14
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
15
+ const roles = await endpoint.getRoleList(guild_id);
16
+ return {
17
+ roles: roles.map((r) => ({
18
+ id: String(r.role_id),
19
+ name: r.name,
20
+ color: r.color,
21
+ position: r.position,
22
+ permissions: r.permissions,
23
+ })),
24
+ count: roles.length,
25
+ };
26
+ },
27
+ });
@@ -0,0 +1,22 @@
1
+ import { defineAgentTool } from '@zhin.js/agent/tools';
2
+ import { z } from 'zod';
3
+ import { platformPermit } from '../../src/platform-permit.js';
4
+ import { getKookAgentDeps } from '../../src/kook-agent-deps.js';
5
+
6
+ export default defineAgentTool<{ endpoint_id: string; guild_id: string; user_id: string; role_id: string }>({
7
+ description: '撤销用户的 KOOK 服务器角色',
8
+ inputSchema: z.object({
9
+ endpoint_id: z.string().describe('Endpoint 名称'),
10
+ guild_id: z.string().describe('服务器 ID'),
11
+ user_id: z.string().describe('用户 ID'),
12
+ role_id: z.string().describe('角色 ID'),
13
+ }),
14
+ platforms: ['kook'],
15
+ tags: ['kook'],
16
+ permissions: [platformPermit('manage_roles')],
17
+ async execute({ endpoint_id, guild_id, user_id, role_id }: { endpoint_id: string; guild_id: string; user_id: string; role_id: string }) {
18
+ const endpoint = getKookAgentDeps().getEndpoint(endpoint_id);
19
+ const success = await endpoint.revokeRole(guild_id, user_id, role_id);
20
+ return { success, message: success ? `已撤销用户 ${user_id} 的角色 ${role_id}` : '撤销角色失败' };
21
+ },
22
+ });
package/lib/endpoint.d.ts CHANGED
@@ -1,155 +1,84 @@
1
- /**
2
- * KOOK Endpoint 实现
3
- */
4
- import { Client, User, Guild } from "kook-client";
5
- import { Endpoint, Message, SendOptions } from 'zhin.js';
6
- import type { KookEndpointConfig, KookRawMessage } from "./types.js";
7
- import type { KookAdapter } from "./adapter.js";
8
- import { type KookMsgRoute } from "./kook-msg-route.js";
9
- export declare class KookEndpoint extends Client implements Endpoint<KookEndpointConfig, KookRawMessage> {
10
- $config: KookEndpointConfig;
11
- $connected: boolean;
12
- /** KOOK 平台 user_id,用于 @ 触发匹配(resolveEndpointAtIds) */
13
- $platformUserId?: string;
14
- adapter: KookAdapter;
15
- private readonly inboundDeduper;
16
- private readonly onGatewayEvent;
17
- /** kook-client 构造函数注入的 process 监听器,disconnect 时需移除 */
18
- private readonly _kookProcessListeners;
19
- get $id(): string;
20
- get pluginLogger(): import("zhin.js").Logger;
21
- constructor(adapter: KookAdapter, $config: KookEndpointConfig);
22
- /**
23
- * 在 kook-client Receiver transform 之前拦截原始 gateway 事件(SDK 未实现 notice transform)
24
- */
25
- private hookGatewayReceiver;
26
- private handleGatewayEvent;
27
- private emitSideEvent;
28
- /**
29
- * 设置事件监听器
30
- */
31
- private setupEventListeners;
32
- /**
33
- * KOOK 消息转换为标准消息格式
34
- */
35
- $formatMessage(msg: KookRawMessage): Message<KookRawMessage>;
36
- /**
37
- * 获取发送者的详细权限信息
38
- */
39
- private getSenderInfo;
40
- /**
41
- * 踢出用户
42
- * @param guildId 服务器ID
43
- * @param userId 用户ID
44
- */
45
- kickUser(guildId: string, userId: string): Promise<boolean>;
46
- /**
47
- * 将用户加入黑名单
48
- * @param guildId 服务器ID
49
- * @param userId 用户ID
50
- * @param remark 备注
51
- * @param delMsgDays 删除消息天数(0-7)
52
- */
53
- addToBlacklist(guildId: string, userId: string, remark?: string, delMsgDays?: number): Promise<boolean>;
54
- /**
55
- * 将用户从黑名单移除
56
- * @param guildId 服务器ID
57
- * @param userId 用户ID
58
- */
59
- removeFromBlacklist(guildId: string, userId: string): Promise<boolean>;
60
- /**
61
- * 给用户授予角色
62
- * @param guildId 服务器ID
63
- * @param userId 用户ID
64
- * @param roleId 角色ID
65
- */
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 { type KookInboundMessage, type ResolvedKookWebhookConfig, type ResolvedKookWebsocketConfig } from './protocol.js';
6
+ import { type CreateKookClient } from './ws.js';
7
+ export interface KookEndpointOptions {
8
+ readonly id: CapabilityId;
9
+ readonly gateway: MessageGateway;
10
+ readonly config: ResolvedKookWebsocketConfig;
11
+ readonly createClient?: CreateKookClient;
12
+ }
13
+ export declare class KookWebsocketEndpoint implements EndpointInstance {
14
+ #private;
15
+ constructor(options: KookEndpointOptions);
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
+ /** Test / internal: admit a message when open. */
25
+ admit(msg: KookInboundMessage): void;
26
+ getRoleList(guildId: string): Promise<{
27
+ role_id: string | number;
28
+ name: string;
29
+ color?: number;
30
+ position?: number;
31
+ permissions?: unknown;
32
+ }[]>;
33
+ createRole(guildId: string, name: string): Promise<{
34
+ role_id: string | number;
35
+ name: string;
36
+ }>;
37
+ deleteRole(guildId: string, roleId: string): Promise<boolean>;
66
38
  grantRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
67
- /**
68
- * 撤销用户角色
69
- * @param guildId 服务器ID
70
- * @param userId 用户ID
71
- * @param roleId 角色ID
72
- */
73
39
  revokeRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
74
- /**
75
- * 设置用户昵称
76
- * @param guildId 服务器ID
77
- * @param userId 用户ID
78
- * @param nickname 新昵称
79
- */
80
- setNickname(guildId: string, userId: string, nickname: string): Promise<boolean>;
81
- /**
82
- * 获取服务器角色列表
83
- * @param guildId 服务器ID
84
- */
85
- getRoleList(guildId: string): Promise<Guild.Role[]>;
86
- /**
87
- * 创建角色
88
- * @param guildId 服务器ID
89
- * @param name 角色名称
90
- */
91
- createRole(guildId: string, name: string): Promise<Guild.Role>;
92
- /**
93
- * 删除角色
94
- * @param guildId 服务器ID
95
- * @param roleId 角色ID
96
- */
97
- deleteRole(guildId: string, roleId: string): Promise<boolean>;
98
- /**
99
- * 获取服务器成员列表
100
- * @param guildId 服务器ID
101
- * @param channelId 可选的频道ID
102
- */
103
- getGuildMembers(guildId: string, channelId?: string): Promise<User.Info[]>;
104
- private buildAtElement;
105
- private resolveAtUserId;
106
- private parseMarkdown;
107
- /**
108
- * 将 kook-client 的 MessageSegment[] 转换为 Zhin 的 MessageElement[]
109
- */
110
- private parseMessageContent;
111
- /**
112
- * 检查文本是否包含 KMarkdown 特殊语法
113
- */
114
- private hasKMarkdownSyntax;
115
- /**
116
- * 连接到 KOOK
117
- */
118
- $connect(): Promise<void>;
119
- /**
120
- * 断开连接
121
- */
122
- $disconnect(): Promise<void>;
123
- /**
124
- * 上传媒体(覆盖 kook-client:其 FormData.append 不接受 Node Buffer)
125
- */
126
- uploadMedia(data: string | Buffer): Promise<string>;
127
- /**
128
- * 发送消息
129
- */
130
- $sendMessage(options: SendOptions): Promise<string>;
131
- /**
132
- * 撤回消息。支持 `kook:channel:msgId` 出站 ref,或入站 plain msgId + route/sceneType 提示。
133
- */
134
- $recallMessage(messageIdOrRef: string, hint?: {
135
- route?: KookMsgRoute;
136
- sceneType?: 'private' | 'group' | 'channel';
137
- }): Promise<void>;
138
- private deleteKookMsg;
139
- /**
140
- * 为消息添加表情回应(TypingIndicator reaction 模式)
141
- * @returns reactionId(含 channel/direct 路由),供 $removeReaction 使用
142
- */
143
- $addReaction(messageId: string, emoji: string, hint?: {
144
- sceneType?: 'private' | 'group' | 'channel';
40
+ addToBlacklist(guildId: string, userId: string, remark?: string): Promise<boolean>;
41
+ removeFromBlacklist(guildId: string, userId: string): Promise<boolean>;
42
+ }
43
+ export interface KookWebhookEndpointOptions {
44
+ readonly id: CapabilityId;
45
+ readonly gateway: MessageGateway;
46
+ readonly http: HttpHost;
47
+ readonly config: ResolvedKookWebhookConfig;
48
+ readonly createClient?: CreateKookClient;
49
+ }
50
+ export declare class KookWebhookEndpoint implements EndpointInstance {
51
+ #private;
52
+ constructor(options: KookWebhookEndpointOptions);
53
+ /** Used by webhook handler. */
54
+ get isOpen(): boolean;
55
+ get config(): ResolvedKookWebhookConfig;
56
+ get selfId(): string | undefined;
57
+ start(): Promise<void>;
58
+ open(): void;
59
+ close(): void;
60
+ stop(): Promise<void>;
61
+ send({ target, payload }: {
62
+ readonly target: string;
63
+ readonly payload: unknown;
145
64
  }): Promise<string>;
146
- /** 移除本 Endpoint 在消息上的表情回应 */
147
- $removeReaction(messageId: string, reactionId: string): Promise<void>;
148
- private mutateMsgReaction;
149
- /**
150
- * 将消息段转换为 KOOK KMarkdown 格式
151
- * 支持:文本、图片、@提及、表情、引用等
152
- */
153
- private convertToKookFormat;
65
+ /** Test / internal: admit a message when open. */
66
+ admit(msg: KookInboundMessage): void;
67
+ checkAndRememberSn(sn: number): boolean;
68
+ getRoleList(guildId: string): Promise<{
69
+ role_id: string | number;
70
+ name: string;
71
+ color?: number;
72
+ position?: number;
73
+ permissions?: unknown;
74
+ }[]>;
75
+ createRole(guildId: string, name: string): Promise<{
76
+ role_id: string | number;
77
+ name: string;
78
+ }>;
79
+ deleteRole(guildId: string, roleId: string): Promise<boolean>;
80
+ grantRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
81
+ revokeRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
82
+ addToBlacklist(guildId: string, userId: string, remark?: string): Promise<boolean>;
83
+ removeFromBlacklist(guildId: string, userId: string): Promise<boolean>;
154
84
  }
155
- //# sourceMappingURL=endpoint.d.ts.map