@zhin.js/adapter-kook 5.0.2 → 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.
- package/CHANGELOG.md +41 -0
- package/README.md +58 -452
- package/adapters/kook.ts +46 -0
- package/agent/tools/blacklist.ts +2 -2
- package/agent/tools/create_role.ts +2 -2
- package/agent/tools/delete_role.ts +2 -2
- package/agent/tools/grant_role.ts +2 -2
- package/agent/tools/list_roles.ts +2 -2
- package/agent/tools/revoke_role.ts +2 -2
- package/lib/endpoint.d.ts +84 -0
- package/lib/endpoint.js +299 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/kook-agent-deps.d.ts +29 -0
- package/lib/kook-agent-deps.js +30 -0
- package/lib/platform-permit.d.ts +23 -0
- package/lib/{src/platform-permit.js → platform-permit.js} +2 -3
- package/lib/protocol.d.ts +147 -0
- package/lib/protocol.js +279 -0
- package/lib/webhook.d.ts +15 -0
- package/lib/webhook.js +76 -0
- package/lib/ws.d.ts +42 -0
- package/lib/ws.js +63 -0
- package/package.json +44 -40
- package/plugin.ts +13 -0
- package/schema.json +55 -0
- package/src/endpoint.ts +287 -879
- package/src/index.ts +55 -177
- package/src/kook-agent-deps.ts +44 -11
- package/src/platform-permit.ts +13 -3
- package/src/protocol.ts +429 -0
- package/src/webhook.ts +116 -0
- package/src/ws.ts +120 -0
- package/client/Dashboard.tsx +0 -255
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -30
- package/dist/index.js +0 -31
- package/lib/agent/tools/blacklist.js +0 -33
- package/lib/agent/tools/blacklist.js.map +0 -1
- package/lib/agent/tools/create_role.js +0 -25
- package/lib/agent/tools/create_role.js.map +0 -1
- package/lib/agent/tools/delete_role.js +0 -21
- package/lib/agent/tools/delete_role.js.map +0 -1
- package/lib/agent/tools/grant_role.js +0 -22
- package/lib/agent/tools/grant_role.js.map +0 -1
- package/lib/agent/tools/list_roles.js +0 -27
- package/lib/agent/tools/list_roles.js.map +0 -1
- package/lib/agent/tools/revoke_role.js +0 -22
- package/lib/agent/tools/revoke_role.js.map +0 -1
- package/lib/src/adapter.js +0 -67
- package/lib/src/adapter.js.map +0 -1
- package/lib/src/endpoint.js +0 -796
- package/lib/src/endpoint.js.map +0 -1
- package/lib/src/index.js +0 -197
- package/lib/src/index.js.map +0 -1
- package/lib/src/kook-agent-deps.js +0 -10
- package/lib/src/kook-agent-deps.js.map +0 -1
- package/lib/src/kook-asset-upload.js +0 -46
- package/lib/src/kook-asset-upload.js.map +0 -1
- package/lib/src/kook-inbound.js +0 -22
- package/lib/src/kook-inbound.js.map +0 -1
- package/lib/src/kook-msg-route.js +0 -106
- package/lib/src/kook-msg-route.js.map +0 -1
- package/lib/src/kook-side-events.js +0 -166
- package/lib/src/kook-side-events.js.map +0 -1
- package/lib/src/outbound-keyboard.js +0 -66
- package/lib/src/outbound-keyboard.js.map +0 -1
- package/lib/src/outbound-media.js +0 -45
- package/lib/src/outbound-media.js.map +0 -1
- package/lib/src/outbound-sendable.js +0 -64
- package/lib/src/outbound-sendable.js.map +0 -1
- package/lib/src/platform-permit.js.map +0 -1
- package/lib/src/segment-mapper.js +0 -2
- package/lib/src/segment-mapper.js.map +0 -1
- package/lib/src/types.js +0 -8
- package/lib/src/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -75
- package/src/kook-asset-upload.ts +0 -55
- package/src/kook-inbound.ts +0 -22
- package/src/kook-msg-route.ts +0 -122
- package/src/kook-side-events.ts +0 -230
- package/src/outbound-keyboard.ts +0 -77
- package/src/outbound-media.ts +0 -60
- package/src/outbound-sendable.ts +0 -73
- package/src/segment-mapper.ts +0 -1
- package/src/types.ts +0 -58
package/src/index.ts
CHANGED
|
@@ -1,177 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
export {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const sceneTools = createSceneManagementTools(
|
|
57
|
-
kook as unknown as ISceneManagement,
|
|
58
|
-
'kook',
|
|
59
|
-
{ permitResolver: kookGroupPermitResolver, registerChecker: false },
|
|
60
|
-
);
|
|
61
|
-
disposers.push(...sceneTools.map(t => toolService.addTool(t, plugin.name)));
|
|
62
|
-
|
|
63
|
-
return () => disposers.forEach(d => d());
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// ── Web 控制台 ─────────────────────────────────────────────────────────
|
|
67
|
-
useContext("web", (pageManager) => {
|
|
68
|
-
pageManager.addEntry({
|
|
69
|
-
id: "kook",
|
|
70
|
-
development: path.resolve(import.meta.dirname, "../client/index.tsx"),
|
|
71
|
-
production: path.resolve(import.meta.dirname, "../dist/index.js"),
|
|
72
|
-
meta: { name: "KOOK" },
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
useContext("router", "kook", (router: any, kook: KookAdapter) => {
|
|
77
|
-
router.get("/api/kook/endpoints", async (ctx: any) => {
|
|
78
|
-
try {
|
|
79
|
-
const endpoints = Array.from(kook.endpoints.values());
|
|
80
|
-
const result = endpoints.map((endpoint: any) => {
|
|
81
|
-
try {
|
|
82
|
-
return {
|
|
83
|
-
name: endpoint.$config.name,
|
|
84
|
-
connected: endpoint.$connected || false,
|
|
85
|
-
guildCount: endpoint.guilds?.size || 0,
|
|
86
|
-
status: endpoint.$connected ? "online" : "offline",
|
|
87
|
-
};
|
|
88
|
-
} catch {
|
|
89
|
-
return { name: endpoint.$config.name, connected: false, guildCount: 0, status: "error" };
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
ctx.body = { success: true, data: result };
|
|
93
|
-
} catch {
|
|
94
|
-
ctx.status = 500;
|
|
95
|
-
ctx.body = { success: false, error: "获取 Endpoint 数据失败" };
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// Endpoint 连接/断开
|
|
100
|
-
router.post("/api/kook/endpoints/:name/connect", async (ctx: any) => {
|
|
101
|
-
try {
|
|
102
|
-
const endpoint = kook.endpoints.get(ctx.params.name);
|
|
103
|
-
if (!endpoint) { ctx.status = 404; ctx.body = { success: false, error: "Endpoint 不存在" }; return; }
|
|
104
|
-
if (endpoint.$connected) { ctx.body = { success: true, message: "已经在线" }; return; }
|
|
105
|
-
await endpoint.$connect();
|
|
106
|
-
ctx.body = { success: true, message: "连接成功" };
|
|
107
|
-
} catch (e: unknown) {
|
|
108
|
-
ctx.status = 500;
|
|
109
|
-
ctx.body = { success: false, error: e instanceof Error ? e.message : "连接失败" };
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
router.post("/api/kook/endpoints/:name/disconnect", async (ctx: any) => {
|
|
114
|
-
try {
|
|
115
|
-
const endpoint = kook.endpoints.get(ctx.params.name);
|
|
116
|
-
if (!endpoint) { ctx.status = 404; ctx.body = { success: false, error: "Endpoint 不存在" }; return; }
|
|
117
|
-
if (!endpoint.$connected) { ctx.body = { success: true, message: "已经离线" }; return; }
|
|
118
|
-
await endpoint.$disconnect();
|
|
119
|
-
ctx.body = { success: true, message: "已断开" };
|
|
120
|
-
} catch (e: unknown) {
|
|
121
|
-
ctx.status = 500;
|
|
122
|
-
ctx.body = { success: false, error: e instanceof Error ? e.message : "断开失败" };
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// 角色列表
|
|
127
|
-
router.get("/api/kook/endpoints/:name/guilds/:guildId/roles", async (ctx: any) => {
|
|
128
|
-
try {
|
|
129
|
-
const endpoint: any = kook.endpoints.get(ctx.params.name);
|
|
130
|
-
if (!endpoint) { ctx.status = 404; ctx.body = { success: false, error: "Endpoint 不存在" }; return; }
|
|
131
|
-
if (!endpoint.$connected) { ctx.status = 400; ctx.body = { success: false, error: "Endpoint 未连接" }; return; }
|
|
132
|
-
const roles = await endpoint.getRoleList(ctx.params.guildId);
|
|
133
|
-
ctx.body = {
|
|
134
|
-
success: true,
|
|
135
|
-
data: roles.map((r: any) => ({
|
|
136
|
-
id: r.role_id,
|
|
137
|
-
name: r.name,
|
|
138
|
-
color: r.color,
|
|
139
|
-
position: r.position,
|
|
140
|
-
})),
|
|
141
|
-
};
|
|
142
|
-
} catch (e: unknown) {
|
|
143
|
-
ctx.status = 500;
|
|
144
|
-
ctx.body = { success: false, error: e instanceof Error ? e.message : "获取角色列表失败" };
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// 创建角色
|
|
149
|
-
router.post("/api/kook/endpoints/:name/guilds/:guildId/roles", async (ctx: any) => {
|
|
150
|
-
try {
|
|
151
|
-
const endpoint: any = kook.endpoints.get(ctx.params.name);
|
|
152
|
-
if (!endpoint) { ctx.status = 404; ctx.body = { success: false, error: "Endpoint 不存在" }; return; }
|
|
153
|
-
if (!endpoint.$connected) { ctx.status = 400; ctx.body = { success: false, error: "Endpoint 未连接" }; return; }
|
|
154
|
-
const { name } = ctx.request.body || {};
|
|
155
|
-
if (!name) { ctx.status = 400; ctx.body = { success: false, error: "缺少角色名称" }; return; }
|
|
156
|
-
const role = await endpoint.createRole(ctx.params.guildId, name);
|
|
157
|
-
ctx.body = { success: true, data: { id: role.role_id, name: role.name } };
|
|
158
|
-
} catch (e: unknown) {
|
|
159
|
-
ctx.status = 500;
|
|
160
|
-
ctx.body = { success: false, error: e instanceof Error ? e.message : "创建角色失败" };
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// 删除角色
|
|
165
|
-
router.delete("/api/kook/endpoints/:name/guilds/:guildId/roles/:roleId", async (ctx: any) => {
|
|
166
|
-
try {
|
|
167
|
-
const endpoint: any = kook.endpoints.get(ctx.params.name);
|
|
168
|
-
if (!endpoint) { ctx.status = 404; ctx.body = { success: false, error: "Endpoint 不存在" }; return; }
|
|
169
|
-
if (!endpoint.$connected) { ctx.status = 400; ctx.body = { success: false, error: "Endpoint 未连接" }; return; }
|
|
170
|
-
const success = await endpoint.deleteRole(ctx.params.guildId, ctx.params.roleId);
|
|
171
|
-
ctx.body = { success, message: success ? "角色已删除" : "删除失败" };
|
|
172
|
-
} catch (e: unknown) {
|
|
173
|
-
ctx.status = 500;
|
|
174
|
-
ctx.body = { success: false, error: e instanceof Error ? e.message : "删除角色失败" };
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
});
|
|
1
|
+
export {
|
|
2
|
+
formatInboundContent,
|
|
3
|
+
formatInboundTarget,
|
|
4
|
+
formatOutboundKmarkdown,
|
|
5
|
+
parseSendTarget,
|
|
6
|
+
resolveKookConfig,
|
|
7
|
+
senderDisplayName,
|
|
8
|
+
KookPermission,
|
|
9
|
+
type KookAdapterConfig,
|
|
10
|
+
type KookInboundMessage,
|
|
11
|
+
type KookWireSegment,
|
|
12
|
+
type LogLevel,
|
|
13
|
+
type ParsedSendTarget,
|
|
14
|
+
type ResolvedKookConfig,
|
|
15
|
+
type ResolvedKookWebhookConfig,
|
|
16
|
+
type ResolvedKookWebsocketConfig,
|
|
17
|
+
} from './protocol.js';
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
KookWebhookEndpoint,
|
|
21
|
+
KookWebsocketEndpoint,
|
|
22
|
+
type KookEndpointOptions,
|
|
23
|
+
type KookWebhookEndpointOptions,
|
|
24
|
+
} from './endpoint.js';
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
registerKookWebhookRoutes,
|
|
28
|
+
handleKookWebhookRequest,
|
|
29
|
+
type KookWebhookHandler,
|
|
30
|
+
} from './webhook.js';
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
defaultCreateClient,
|
|
34
|
+
defaultCreateWebhookClient,
|
|
35
|
+
normalizeKookMessage,
|
|
36
|
+
type CreateKookClient,
|
|
37
|
+
type KookClientTransport,
|
|
38
|
+
} from './ws.js';
|
|
39
|
+
|
|
40
|
+
export {
|
|
41
|
+
getKookAgentDeps,
|
|
42
|
+
registerKookAgentEndpoint,
|
|
43
|
+
setKookAgentDeps,
|
|
44
|
+
type KookAgentDeps,
|
|
45
|
+
type KookAgentEndpoint,
|
|
46
|
+
} from './kook-agent-deps.js';
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
checkKookPlatformPermit,
|
|
50
|
+
kookGroupPermitResolver,
|
|
51
|
+
normalizeKookSenderForPermit,
|
|
52
|
+
platformPermit,
|
|
53
|
+
registerKookPlatformPermitChecker,
|
|
54
|
+
type KookSenderInfo,
|
|
55
|
+
} from './platform-permit.js';
|
package/src/kook-agent-deps.ts
CHANGED
|
@@ -1,22 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Agent tool deps for kook.
|
|
3
|
+
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
|
|
6
|
+
export interface KookAgentEndpoint {
|
|
7
|
+
getRoleList(guildId: string): Promise<Array<{
|
|
8
|
+
role_id: string | number;
|
|
9
|
+
name: string;
|
|
10
|
+
color?: number;
|
|
11
|
+
position?: number;
|
|
12
|
+
permissions?: unknown;
|
|
13
|
+
}>>;
|
|
14
|
+
createRole(guildId: string, name: string): Promise<{ role_id: string | number; name: string }>;
|
|
15
|
+
deleteRole(guildId: string, roleId: string): Promise<boolean>;
|
|
16
|
+
grantRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
17
|
+
revokeRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
18
|
+
addToBlacklist(guildId: string, userId: string, remark?: string): Promise<boolean>;
|
|
19
|
+
removeFromBlacklist(guildId: string, userId: string): Promise<boolean>;
|
|
20
|
+
}
|
|
7
21
|
|
|
8
22
|
export interface KookAgentDeps {
|
|
9
|
-
getEndpoint: (endpointId: string) =>
|
|
10
|
-
getAdapter: () => KookAdapter;
|
|
23
|
+
getEndpoint: (endpointId: string) => KookAgentEndpoint;
|
|
11
24
|
}
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
const endpoints = new Map<string, KookAgentEndpoint>();
|
|
27
|
+
let override: KookAgentDeps | null = null;
|
|
28
|
+
|
|
29
|
+
export function registerKookAgentEndpoint(
|
|
30
|
+
endpointId: string,
|
|
31
|
+
endpoint: KookAgentEndpoint,
|
|
32
|
+
): () => void {
|
|
33
|
+
endpoints.set(endpointId, endpoint);
|
|
34
|
+
return () => {
|
|
35
|
+
if (endpoints.get(endpointId) === endpoint) {
|
|
36
|
+
endpoints.delete(endpointId);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
14
40
|
|
|
15
|
-
|
|
16
|
-
|
|
41
|
+
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
42
|
+
export function setKookAgentDeps(deps: KookAgentDeps | null): void {
|
|
43
|
+
override = deps;
|
|
17
44
|
}
|
|
18
45
|
|
|
19
46
|
export function getKookAgentDeps(): KookAgentDeps {
|
|
20
|
-
if (
|
|
21
|
-
return
|
|
47
|
+
if (override) return override;
|
|
48
|
+
return {
|
|
49
|
+
getEndpoint(endpointId: string): KookAgentEndpoint {
|
|
50
|
+
const registered = endpoints.get(endpointId);
|
|
51
|
+
if (!registered) throw new Error(`Endpoint ${endpointId} 不存在`);
|
|
52
|
+
return registered;
|
|
53
|
+
},
|
|
54
|
+
};
|
|
22
55
|
}
|
package/src/platform-permit.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* KOOK platform permit — guild / channel 差异化门禁
|
|
3
3
|
*/
|
|
4
|
-
import { registerPlatformPermitChecker, type Message } from 'zhin.js';
|
|
5
|
-
|
|
6
|
-
import { KookPermission, type KookSenderInfo } from './types.js';
|
|
4
|
+
import { registerPlatformPermitChecker, type Message } from '@zhin.js/core';
|
|
5
|
+
import { KookPermission } from './protocol.js';
|
|
7
6
|
|
|
8
7
|
const ADAPTER = 'kook';
|
|
9
8
|
|
|
@@ -20,6 +19,17 @@ export function kookGroupPermitResolver(logicalPerm: string): string {
|
|
|
20
19
|
return platformPermit(FACTORY_PERM_MAP[logicalPerm] ?? logicalPerm);
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
export interface KookSenderInfo {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
permission?: KookPermission;
|
|
26
|
+
roles?: number[];
|
|
27
|
+
isGuildOwner?: boolean;
|
|
28
|
+
isAdmin?: boolean;
|
|
29
|
+
role?: string;
|
|
30
|
+
permissions?: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
23
33
|
export function normalizeKookSenderForPermit(
|
|
24
34
|
info: KookSenderInfo,
|
|
25
35
|
isChannel: boolean,
|