@im-more/dsh-kook 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/cordis.patch.yml +12 -0
- package/lib/index.js +2380 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +53 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +138 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +46 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +52 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +88 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +564 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +46 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +159 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +17 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +24 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +15 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +15 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/kmarkdown.d.ts +76 -0
- package/lib/types/kmarkdown.d.ts.map +1 -0
- package/lib/types/kmarkdown.js +201 -0
- package/lib/types/kmarkdown.js.map +1 -0
- package/lib/types/modes/kmarkdown.d.ts +17 -0
- package/lib/types/modes/kmarkdown.d.ts.map +1 -0
- package/lib/types/modes/kmarkdown.js +166 -0
- package/lib/types/modes/kmarkdown.js.map +1 -0
- package/lib/types/modes/shared.d.ts +192 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +371 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +53 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +147 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +57 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +141 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/types.d.ts +807 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +168 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/webhook.d.ts +35 -0
- package/lib/types/webhook.d.ts.map +1 -0
- package/lib/types/webhook.js +132 -0
- package/lib/types/webhook.js.map +1 -0
- package/lib/types/ws.d.ts +54 -0
- package/lib/types/ws.d.ts.map +1 -0
- package/lib/types/ws.js +358 -0
- package/lib/types/ws.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +217 -0
- package/src/config.ts +84 -0
- package/src/decode.ts +660 -0
- package/src/errors.ts +188 -0
- package/src/identity.ts +32 -0
- package/src/index.ts +80 -0
- package/src/kmarkdown.ts +246 -0
- package/src/modes/kmarkdown.ts +204 -0
- package/src/modes/shared.ts +498 -0
- package/src/request.ts +196 -0
- package/src/send.ts +216 -0
- package/src/types.ts +764 -0
- package/src/webhook.ts +170 -0
- package/src/ws.ts +431 -0
- package/README.md +0 -3
package/lib/types/ws.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway WebSocket transport (`protocol: 'ws'`).
|
|
3
|
+
*
|
|
4
|
+
* The adapter fetches `/gateway/index?compress=0` before **every** dial (KOOK hands out a fresh gateway URL), dials it
|
|
5
|
+
* with the `http` service and keeps dialing forever: `ws.retryTimes` is a fast-retry budget, not a give-up count.
|
|
6
|
+
* Resume is not implemented — the platform would replay the offline window only for a client that keeps its session,
|
|
7
|
+
* and the upstream adapter never did — so a dropped connection loses the events it missed and the bot says so in its
|
|
8
|
+
* logs.
|
|
9
|
+
*
|
|
10
|
+
* The `sn` heartbeat confirmation is the platform's ACK: the `sn` a PING carries is the last message `bot.receive`
|
|
11
|
+
* accepted, so a receive that fails leaves the event unconfirmed and the platform retransmits it on the next heartbeat.
|
|
12
|
+
*/
|
|
13
|
+
import { Status } from '@deepseek-ai/cordis-plugin-im-core';
|
|
14
|
+
import { decodeEvent } from "./decode.js";
|
|
15
|
+
import { isPermanentHelloCode, isResumeCode } from "./errors.js";
|
|
16
|
+
import { invoke } from "./request.js";
|
|
17
|
+
import { Signal } from "./types.js";
|
|
18
|
+
/** The platform requires HELLO within 6 s of the connection opening. */
|
|
19
|
+
const HELLO_TIMEOUT = 6_000;
|
|
20
|
+
/** The platform requires PONG within 6 s of a PING. */
|
|
21
|
+
const PONG_TIMEOUT = 6_000;
|
|
22
|
+
/** Heartbeat interval, 30 s plus a random ±5 s as the platform asks. */
|
|
23
|
+
const HEARTBEAT_BASE = 30_000;
|
|
24
|
+
const HEARTBEAT_JITTER = 5_000;
|
|
25
|
+
/** Waitable sleep that resolves early on abort. */
|
|
26
|
+
function sleep(ms, signal) {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
if (signal.aborted) {
|
|
29
|
+
resolve();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const timer = setTimeout(() => {
|
|
33
|
+
signal.removeEventListener('abort', onAbort);
|
|
34
|
+
resolve();
|
|
35
|
+
}, ms);
|
|
36
|
+
const onAbort = () => {
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
resolve();
|
|
39
|
+
};
|
|
40
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Text of one WebSocket frame. */
|
|
44
|
+
function frameText(data) {
|
|
45
|
+
return typeof data === 'string' ? data : String(data);
|
|
46
|
+
}
|
|
47
|
+
/** One JSON field of one frame, when it is an object. */
|
|
48
|
+
function recordOf(value) {
|
|
49
|
+
return typeof value === 'object' && value !== null
|
|
50
|
+
? value
|
|
51
|
+
: undefined;
|
|
52
|
+
}
|
|
53
|
+
/** One number field of one frame. */
|
|
54
|
+
function numberOf(value) {
|
|
55
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
56
|
+
}
|
|
57
|
+
/** The platform refused the connection for a reason only a corrected configuration can fix. */
|
|
58
|
+
class KookGatewayFatalError extends Error {
|
|
59
|
+
constructor(message) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.name = 'KookGatewayFatalError';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The Gateway client of one bot.
|
|
66
|
+
*
|
|
67
|
+
* `start()` returns after starting the reconnect loop, never after the first HELLO: `Bot.start` awaits `connect()`,
|
|
68
|
+
* and holding plugin initialization open until KOOK is reachable would block the whole profile.
|
|
69
|
+
*/
|
|
70
|
+
export class KookWsTransport {
|
|
71
|
+
bot;
|
|
72
|
+
controller;
|
|
73
|
+
running;
|
|
74
|
+
socket;
|
|
75
|
+
heartbeatTimer;
|
|
76
|
+
pongTimer;
|
|
77
|
+
constructor(bot) {
|
|
78
|
+
this.bot = bot;
|
|
79
|
+
}
|
|
80
|
+
/** Start the reconnect loop. */
|
|
81
|
+
async start() {
|
|
82
|
+
this.controller = new AbortController();
|
|
83
|
+
this.running = this.loop(this.controller.signal);
|
|
84
|
+
}
|
|
85
|
+
/** Stop the loop and wait for it to unwind. */
|
|
86
|
+
async stop() {
|
|
87
|
+
this.controller?.abort();
|
|
88
|
+
this.controller = undefined;
|
|
89
|
+
this.closeSocket();
|
|
90
|
+
await this.running?.catch(() => undefined);
|
|
91
|
+
this.running = undefined;
|
|
92
|
+
}
|
|
93
|
+
/** Delay before the next dial: the fast interval while the failure budget lasts, the lazy one after it. */
|
|
94
|
+
intervalOf(failures) {
|
|
95
|
+
const { ws } = this.bot.config;
|
|
96
|
+
return failures < ws.retryTimes ? ws.retryInterval : ws.retryLazy;
|
|
97
|
+
}
|
|
98
|
+
async loop(signal) {
|
|
99
|
+
let failures = 0;
|
|
100
|
+
while (!signal.aborted) {
|
|
101
|
+
try {
|
|
102
|
+
const socket = await this.open(signal);
|
|
103
|
+
const greeted = await this.accept(socket, signal);
|
|
104
|
+
if (signal.aborted)
|
|
105
|
+
return;
|
|
106
|
+
// A connection that greeted once proves the token works, so the next dial goes out at the fast interval.
|
|
107
|
+
failures = greeted ? 0 : failures + 1;
|
|
108
|
+
this.bot.ctx.logger.warn('kook: the gateway connection closed; reconnecting');
|
|
109
|
+
this.bot.status = Status.RECONNECT;
|
|
110
|
+
await sleep(this.intervalOf(failures), signal);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (signal.aborted)
|
|
114
|
+
return;
|
|
115
|
+
if (error instanceof KookGatewayFatalError) {
|
|
116
|
+
// A rejected token cannot be fixed by reconnecting: report it and stop until the profile is edited.
|
|
117
|
+
this.bot.ctx.logger.error(error.message);
|
|
118
|
+
this.bot.offline(error);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
failures += 1;
|
|
122
|
+
this.bot.ctx.logger.warn('kook: %s; reconnecting', messageOf(error));
|
|
123
|
+
this.bot.status = Status.RECONNECT;
|
|
124
|
+
await sleep(this.intervalOf(failures), signal);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Fetch a fresh gateway URL and dial it, resolving once the socket opens. */
|
|
129
|
+
async open(signal) {
|
|
130
|
+
const gateway = await invoke(this.bot.request, 'GET', '/gateway/index', { compress: 0 }, signal);
|
|
131
|
+
const url = gateway?.url;
|
|
132
|
+
if (url === undefined || url === '') {
|
|
133
|
+
throw new Error('kook: /gateway/index answered without a url');
|
|
134
|
+
}
|
|
135
|
+
return await this.dial(url, signal);
|
|
136
|
+
}
|
|
137
|
+
/** Dial one gateway URL and resolve once the socket is open. */
|
|
138
|
+
dial(url, signal) {
|
|
139
|
+
const socket = this.bot.ctx.http.ws(url, {
|
|
140
|
+
headers: { authorization: `Bot ${this.bot.config.token}` },
|
|
141
|
+
});
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
const cleanup = () => {
|
|
144
|
+
socket.removeEventListener('open', onOpen);
|
|
145
|
+
socket.removeEventListener('error', onError);
|
|
146
|
+
socket.removeEventListener('close', onClose);
|
|
147
|
+
signal.removeEventListener('abort', onAbort);
|
|
148
|
+
};
|
|
149
|
+
const onOpen = () => {
|
|
150
|
+
cleanup();
|
|
151
|
+
resolve(socket);
|
|
152
|
+
};
|
|
153
|
+
const onError = (event) => {
|
|
154
|
+
cleanup();
|
|
155
|
+
reject(new Error(`kook: WebSocket error${event.message === undefined ? '' : `: ${event.message}`}`));
|
|
156
|
+
};
|
|
157
|
+
const onClose = (event) => {
|
|
158
|
+
cleanup();
|
|
159
|
+
reject(new Error(`kook: WebSocket closed before opening (code ${event.code})`));
|
|
160
|
+
};
|
|
161
|
+
const onAbort = () => {
|
|
162
|
+
cleanup();
|
|
163
|
+
reject(signal.reason ?? new Error('kook: adapter stopped'));
|
|
164
|
+
};
|
|
165
|
+
if (signal.aborted) {
|
|
166
|
+
onAbort();
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
socket.addEventListener('open', onOpen);
|
|
170
|
+
socket.addEventListener('error', onError);
|
|
171
|
+
socket.addEventListener('close', onClose);
|
|
172
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Serve one open connection until it closes.
|
|
177
|
+
* @param socket - the open socket.
|
|
178
|
+
* @param signal - cancellation of the loop.
|
|
179
|
+
* @returns whether HELLO arrived, i.e. whether the connection ever worked.
|
|
180
|
+
*/
|
|
181
|
+
async accept(socket, signal) {
|
|
182
|
+
this.socket = socket;
|
|
183
|
+
let greeted = false;
|
|
184
|
+
let sn = 0;
|
|
185
|
+
// Events are received one at a time so `sn` only advances past a message whose `bot.receive` resolved.
|
|
186
|
+
let queue = Promise.resolve();
|
|
187
|
+
// HELLO settles through one of: the frame, the connection closing, the deadline, or the loop stopping.
|
|
188
|
+
let helloCode;
|
|
189
|
+
let helloDone = false;
|
|
190
|
+
let settleHello;
|
|
191
|
+
const hello = new Promise((resolve) => {
|
|
192
|
+
settleHello = resolve;
|
|
193
|
+
});
|
|
194
|
+
const settle = (code) => {
|
|
195
|
+
if (helloDone)
|
|
196
|
+
return;
|
|
197
|
+
helloDone = true;
|
|
198
|
+
helloCode = code;
|
|
199
|
+
settleHello?.();
|
|
200
|
+
};
|
|
201
|
+
const closed = new Promise((resolve) => {
|
|
202
|
+
socket.addEventListener('close', () => {
|
|
203
|
+
settle(undefined);
|
|
204
|
+
resolve();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
socket.addEventListener('message', (event) => {
|
|
208
|
+
let parsed;
|
|
209
|
+
try {
|
|
210
|
+
parsed = JSON.parse(frameText(event.data));
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
this.bot.ctx.logger.warn('kook: cannot parse a gateway frame: %s', messageOf(error));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
switch (parsed.s) {
|
|
217
|
+
case Signal.event: {
|
|
218
|
+
queue = queue.then(async () => {
|
|
219
|
+
try {
|
|
220
|
+
const session = decodeEvent(this.bot, parsed.d);
|
|
221
|
+
if (session !== undefined)
|
|
222
|
+
await this.bot.receive(session);
|
|
223
|
+
const frameSn = numberOf(parsed.sn);
|
|
224
|
+
if (frameSn !== undefined)
|
|
225
|
+
sn = Math.max(sn, frameSn);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
// The heartbeat's `sn` still points at the previous message, so the platform retransmits this one.
|
|
229
|
+
this.bot.ctx.logger.error('kook: intake failed: %s', messageOf(error));
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
case Signal.hello: {
|
|
235
|
+
settle(numberOf(recordOf(parsed.d)?.code) ?? 0);
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
case Signal.pong: {
|
|
239
|
+
this.clearPong();
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case Signal.resume: {
|
|
243
|
+
// The migrated enum names s=5 `resume`; the platform sends s=5 to demand a reconnect (and s=4 is only ever
|
|
244
|
+
// sent by a client that resumes, which this adapter does not).
|
|
245
|
+
const code = numberOf(recordOf(parsed.d)?.code);
|
|
246
|
+
this.bot.ctx.logger.info('kook: the platform asked for a reconnect%s', code === undefined ? '' : ` (code ${code})`);
|
|
247
|
+
if (isResumeCode(code)) {
|
|
248
|
+
this.bot.ctx.logger.warn('kook: the platform refused the session (%s); resume is not implemented, so the offline window is lost', code);
|
|
249
|
+
}
|
|
250
|
+
this.closeSocket();
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
default:
|
|
254
|
+
this.bot.ctx.logger.debug('kook: ignored a gateway frame with s=%s', String(parsed.s));
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
const deadline = setTimeout(() => {
|
|
258
|
+
settle(Number.NaN);
|
|
259
|
+
}, HELLO_TIMEOUT);
|
|
260
|
+
const onAbort = () => {
|
|
261
|
+
settle(Number.NaN);
|
|
262
|
+
this.closeSocket();
|
|
263
|
+
};
|
|
264
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
265
|
+
try {
|
|
266
|
+
await hello;
|
|
267
|
+
clearTimeout(deadline);
|
|
268
|
+
if (signal.aborted)
|
|
269
|
+
return false;
|
|
270
|
+
if (helloCode === undefined) {
|
|
271
|
+
// The connection closed before HELLO: a transport failure, not a configuration one.
|
|
272
|
+
throw new Error('kook: the gateway connection closed before HELLO');
|
|
273
|
+
}
|
|
274
|
+
if (Number.isNaN(helloCode)) {
|
|
275
|
+
this.closeSocket();
|
|
276
|
+
throw new Error(`kook: HELLO did not arrive within ${HELLO_TIMEOUT / 1_000} s`);
|
|
277
|
+
}
|
|
278
|
+
if (helloCode !== 0) {
|
|
279
|
+
this.closeSocket();
|
|
280
|
+
// Only 40100–40103 are configuration refusals; an undocumented code is retried like any other transport failure.
|
|
281
|
+
if (isPermanentHelloCode(helloCode)) {
|
|
282
|
+
throw new KookGatewayFatalError(`kook: the gateway refused the connection (code ${helloCode}); fix token/selfId and restart`);
|
|
283
|
+
}
|
|
284
|
+
throw new Error(`kook: the gateway HELLO failed with code ${helloCode}`);
|
|
285
|
+
}
|
|
286
|
+
greeted = true;
|
|
287
|
+
this.bot.online();
|
|
288
|
+
this.scheduleHeartbeat(socket, () => sn, signal);
|
|
289
|
+
await closed;
|
|
290
|
+
// Let the receives already queued finish before a new connection starts dispatching.
|
|
291
|
+
await queue;
|
|
292
|
+
return greeted;
|
|
293
|
+
}
|
|
294
|
+
finally {
|
|
295
|
+
clearTimeout(deadline);
|
|
296
|
+
signal.removeEventListener('abort', onAbort);
|
|
297
|
+
this.stopHeartbeat();
|
|
298
|
+
if (this.socket === socket)
|
|
299
|
+
this.socket = undefined;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** One heartbeat at 30 s ± 5 s, then another; a missing PONG within 6 s closes the socket for the reconnect loop. */
|
|
303
|
+
scheduleHeartbeat(socket, sn, signal) {
|
|
304
|
+
this.stopHeartbeat();
|
|
305
|
+
this.queueHeartbeat(socket, sn, signal);
|
|
306
|
+
}
|
|
307
|
+
/** Arm the next heartbeat without dropping the PONG deadline of the one already sent. */
|
|
308
|
+
queueHeartbeat(socket, sn, signal) {
|
|
309
|
+
const delay = HEARTBEAT_BASE + Math.round((Math.random() * 2 - 1) * HEARTBEAT_JITTER);
|
|
310
|
+
this.heartbeatTimer = setTimeout(() => {
|
|
311
|
+
if (signal.aborted || this.socket !== socket)
|
|
312
|
+
return;
|
|
313
|
+
try {
|
|
314
|
+
socket.send(JSON.stringify({ s: Signal.ping, sn: sn() }));
|
|
315
|
+
}
|
|
316
|
+
catch (error) {
|
|
317
|
+
this.bot.ctx.logger.warn('kook: could not send a heartbeat: %s', messageOf(error));
|
|
318
|
+
this.closeSocket();
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
this.pongTimer = setTimeout(() => {
|
|
322
|
+
if (this.socket !== socket)
|
|
323
|
+
return;
|
|
324
|
+
this.bot.ctx.logger.warn('kook: the gateway did not answer the heartbeat within %s s; reconnecting', PONG_TIMEOUT / 1_000);
|
|
325
|
+
this.closeSocket();
|
|
326
|
+
}, PONG_TIMEOUT);
|
|
327
|
+
this.queueHeartbeat(socket, sn, signal);
|
|
328
|
+
}, delay);
|
|
329
|
+
}
|
|
330
|
+
clearPong() {
|
|
331
|
+
if (this.pongTimer !== undefined)
|
|
332
|
+
clearTimeout(this.pongTimer);
|
|
333
|
+
this.pongTimer = undefined;
|
|
334
|
+
}
|
|
335
|
+
stopHeartbeat() {
|
|
336
|
+
if (this.heartbeatTimer !== undefined)
|
|
337
|
+
clearTimeout(this.heartbeatTimer);
|
|
338
|
+
this.heartbeatTimer = undefined;
|
|
339
|
+
this.clearPong();
|
|
340
|
+
}
|
|
341
|
+
closeSocket() {
|
|
342
|
+
const socket = this.socket;
|
|
343
|
+
this.socket = undefined;
|
|
344
|
+
this.stopHeartbeat();
|
|
345
|
+
try {
|
|
346
|
+
socket?.close(1000, 'adapter stopped');
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
// The socket is already closed or closing; nothing left to release.
|
|
350
|
+
error;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** Message of any caught value, for a log line. */
|
|
355
|
+
function messageOf(error) {
|
|
356
|
+
return error instanceof Error ? error.message : String(error);
|
|
357
|
+
}
|
|
358
|
+
//# sourceMappingURL=ws.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws.js","sourceRoot":"","sources":["../../src/ws.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oCAAoC,CAAA;AAG3D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,E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,CAAC;IAEO,aAAa;QACnB,IAAI,IAAI,CAAC,cAAc,KAAK,SAAS;YAAE,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAA;QACxE,IAAI,CAAC,cAAc,GAAG,SAAS,CAAA;QAC/B,IAAI,CAAC,SAAS,EAAE,CAAA;IAClB,CAAC;IAEO,WAAW;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAA;QAC1B,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;QACvB,IAAI,CAAC,aAAa,EAAE,CAAA;QACpB,IAAI,CAAC;YACH,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAA;QACxC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,oEAAoE;YACpE,KAAK,CAAA;QACP,CAAC;IACH,CAAC;CACF;AAED,mDAAmD;AACnD,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@im-more/dsh-kook",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "KOOK adapter for official dsh-im of DeepSeek Harness",
|
|
5
|
+
"homepage": "https://github.com/ilharp/dsh-im-more",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/ilharp/dsh-im-more.git",
|
|
9
|
+
"directory": "packages/kook"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Il Harper",
|
|
13
|
+
"email": "hi@ilharper.com",
|
|
14
|
+
"url": "https://ilharper.com"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "lib/index.js",
|
|
19
|
+
"types": "lib/types/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./lib/types/index.d.ts",
|
|
23
|
+
"default": "./lib/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
"./cordis.patch.yml": "./cordis.patch.yml"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"lib",
|
|
30
|
+
"src",
|
|
31
|
+
"cordis.patch.yml"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"dsh": ">=0.2.3-alpha.3"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@deepseek-ai/cordis": ">=4.2.0-alpha.2",
|
|
38
|
+
"@deepseek-ai/cordis-plugin-http": ">=1.6.1-alpha.2",
|
|
39
|
+
"@deepseek-ai/cordis-plugin-im-core": ">=5.0.1-alpha.2",
|
|
40
|
+
"@deepseek-ai/satori-element": ">=4.0.1-alpha.2",
|
|
41
|
+
"@deepseek-ai/schemastery": ">=3.20.0-alpha.2"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@deepseek-ai/cordis": ">=4.2.0-alpha.2",
|
|
45
|
+
"@deepseek-ai/cordis-plugin-http": ">=1.6.1-alpha.2",
|
|
46
|
+
"@deepseek-ai/cordis-plugin-im-core": ">=5.0.1-alpha.2",
|
|
47
|
+
"@deepseek-ai/satori-element": ">=4.0.1-alpha.2",
|
|
48
|
+
"@deepseek-ai/schemastery": ">=3.20.0-alpha.2",
|
|
49
|
+
"@types/node": "^22.20.1",
|
|
50
|
+
"tsdown": "^0.22.2",
|
|
51
|
+
"typescript": "^6.0.3",
|
|
52
|
+
"vitest": "^4.1.11"
|
|
53
|
+
},
|
|
54
|
+
"dsh": {
|
|
55
|
+
"bundle": {
|
|
56
|
+
"patch": "./cordis.patch.yml"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsc -p tsconfig.json && tsdown",
|
|
61
|
+
"typecheck": "tsc -p tsconfig.test.json",
|
|
62
|
+
"test": "vitest run"
|
|
63
|
+
}
|
|
6
64
|
}
|
package/src/bot.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
2
|
+
import { Adapter, PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
3
|
+
import type {
|
|
4
|
+
ChatRef,
|
|
5
|
+
InboundMessage,
|
|
6
|
+
MembersPage,
|
|
7
|
+
Mode,
|
|
8
|
+
PrepareContext,
|
|
9
|
+
ResolvedResource,
|
|
10
|
+
ResourceKind,
|
|
11
|
+
ResourceRef,
|
|
12
|
+
SendContext,
|
|
13
|
+
SentPart,
|
|
14
|
+
Target,
|
|
15
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
16
|
+
import { Config, configError } from './config.ts'
|
|
17
|
+
import { classifyKookError } from './errors.ts'
|
|
18
|
+
import { verifyIdentity } from './identity.ts'
|
|
19
|
+
import { createKmarkdownMode } from './modes/kmarkdown.ts'
|
|
20
|
+
import type { KookModeTypes } from './modes/shared.ts'
|
|
21
|
+
import { createKookRequest, invoke } from './request.ts'
|
|
22
|
+
import type { KookRequest } from './request.ts'
|
|
23
|
+
import { deletePart, getMessage, members, sendPayload } from './send.ts'
|
|
24
|
+
import { Internal } from './types.ts'
|
|
25
|
+
import type { Self } from './types.ts'
|
|
26
|
+
import { KookWebhook } from './webhook.ts'
|
|
27
|
+
import { KookWsTransport } from './ws.ts'
|
|
28
|
+
|
|
29
|
+
/** The single mode this adapter always registers. */
|
|
30
|
+
export interface KookModes {
|
|
31
|
+
[mode: string]: Mode<KookModeTypes>
|
|
32
|
+
kmarkdown: Mode<KookModeTypes>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A transport `connect` starts and `disconnect` stops. */
|
|
36
|
+
interface KookTransport {
|
|
37
|
+
start(): Promise<void>
|
|
38
|
+
stop(): Promise<void>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** MIME type by file extension for the multipart part name of an upload. */
|
|
42
|
+
const EXTENSIONS: Record<string, string> = {
|
|
43
|
+
'image/png': '.png',
|
|
44
|
+
'image/jpeg': '.jpg',
|
|
45
|
+
'image/gif': '.gif',
|
|
46
|
+
'image/webp': '.webp',
|
|
47
|
+
'video/mp4': '.mp4',
|
|
48
|
+
'video/quicktime': '.mov',
|
|
49
|
+
'audio/mpeg': '.mp3',
|
|
50
|
+
'audio/ogg': '.ogg',
|
|
51
|
+
'audio/wav': '.wav',
|
|
52
|
+
'application/pdf': '.pdf',
|
|
53
|
+
'text/plain': '.txt',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Message of any caught value, for a log line. */
|
|
57
|
+
function messageOf(error: unknown): string {
|
|
58
|
+
return error instanceof Error ? error.message : String(error)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The file extension a MIME type names, when it has one worth adding to the upload name. */
|
|
62
|
+
function extensionOf(mime: string | undefined): string {
|
|
63
|
+
return mime === undefined ? '' : (EXTENSIONS[mime] ?? '')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* KOOK adapter.
|
|
68
|
+
*
|
|
69
|
+
* Identity is the configured `selfId`, checked against `/user/me` on connect; the Gateway WebSocket and the Webhook are
|
|
70
|
+
* the two transports; `kmarkdown` is the only mode, because a KOOK message is either KMarkdown text or a card and the
|
|
71
|
+
* mode chooses between them per message. Outbound media goes through `/asset/create` because card images must be
|
|
72
|
+
* uploaded by this bot.
|
|
73
|
+
*/
|
|
74
|
+
export class KookBot extends Adapter<Config, KookModes> {
|
|
75
|
+
/** The adapter needs the Satori service and the isolated `http` service. */
|
|
76
|
+
static override inject = ['satori', 'http']
|
|
77
|
+
/** The Loader applies this schema to a profile row's `config` before the constructor runs. */
|
|
78
|
+
static readonly Config = Config
|
|
79
|
+
|
|
80
|
+
readonly modes: KookModes = { kmarkdown: createKmarkdownMode() }
|
|
81
|
+
readonly modeOrder = ['kmarkdown'] as const
|
|
82
|
+
|
|
83
|
+
/** Endpoints shared by every API call of this bot. */
|
|
84
|
+
readonly request: KookRequest
|
|
85
|
+
/** Full typed KOOK API surface registered by the migrated declarations. */
|
|
86
|
+
override readonly internal: Internal
|
|
87
|
+
|
|
88
|
+
private transport: KookTransport | undefined
|
|
89
|
+
|
|
90
|
+
constructor(ctx: Context, config: Config) {
|
|
91
|
+
// A profile the selected protocol cannot work from fails at load, not at the first dial or callback.
|
|
92
|
+
const invalid = configError(config)
|
|
93
|
+
if (invalid !== undefined) throw invalid
|
|
94
|
+
super(ctx, config, {
|
|
95
|
+
id: `kook:${config.selfId}`,
|
|
96
|
+
platform: 'kook',
|
|
97
|
+
adapter: 'kook',
|
|
98
|
+
})
|
|
99
|
+
this.selfId = config.selfId
|
|
100
|
+
this.request = createKookRequest(ctx, config.apiBaseUrl, config.token)
|
|
101
|
+
this.internal = new Internal(this.request)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
override async connect(): Promise<void> {
|
|
105
|
+
try {
|
|
106
|
+
const user: Self = await verifyIdentity(this.request, this.config.selfId)
|
|
107
|
+
this.user = {
|
|
108
|
+
id: this.selfId,
|
|
109
|
+
name:
|
|
110
|
+
user.username === undefined || user.username === ''
|
|
111
|
+
? this.selfId
|
|
112
|
+
: user.username,
|
|
113
|
+
...(user.avatar === undefined || user.avatar === ''
|
|
114
|
+
? {}
|
|
115
|
+
: { avatar: user.avatar }),
|
|
116
|
+
isBot: true,
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
// `Bot.start` only stores a connect failure on `bot.error`, so log it here.
|
|
120
|
+
this.ctx.logger.error('kook: connect failed: %s', messageOf(error))
|
|
121
|
+
throw error
|
|
122
|
+
}
|
|
123
|
+
this.transport =
|
|
124
|
+
this.config.protocol === 'http'
|
|
125
|
+
? new KookWebhook(this)
|
|
126
|
+
: new KookWsTransport(this)
|
|
127
|
+
await this.transport.start()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
override async disconnect(): Promise<void> {
|
|
131
|
+
const transport = this.transport
|
|
132
|
+
this.transport = undefined
|
|
133
|
+
await transport?.stop()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Upload one outbound attachment to `/asset/create`.
|
|
138
|
+
*
|
|
139
|
+
* The platform requires card images to be uploaded by the bot itself ("图片必须由你的机器人上传"), so every media
|
|
140
|
+
* element is uploaded and the returned URL is what the card carries; an external link is never used.
|
|
141
|
+
*/
|
|
142
|
+
override async upload(
|
|
143
|
+
ref: ResourceRef,
|
|
144
|
+
_scope: string,
|
|
145
|
+
ctx: PrepareContext,
|
|
146
|
+
): Promise<ResolvedResource> {
|
|
147
|
+
const fetched = await ctx.fetch(ref.src, ctx.signal)
|
|
148
|
+
const mime = ref.meta.mime ?? fetched.mime ?? 'application/octet-stream'
|
|
149
|
+
const name =
|
|
150
|
+
ref.meta.name ?? fetched.name ?? `kook-${ref.kind}${extensionOf(mime)}`
|
|
151
|
+
const form = new FormData()
|
|
152
|
+
form.append('file', new Blob([fetched.data], { type: mime }), name)
|
|
153
|
+
const asset = await invoke<{ url?: string }>(
|
|
154
|
+
this.request,
|
|
155
|
+
'POST',
|
|
156
|
+
'/asset/create',
|
|
157
|
+
form,
|
|
158
|
+
)
|
|
159
|
+
const url = asset?.url
|
|
160
|
+
if (url === undefined || url === '') {
|
|
161
|
+
throw new PlatformError(
|
|
162
|
+
'invalid',
|
|
163
|
+
'kook: /asset/create answered without a url',
|
|
164
|
+
{ published: 'no' },
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
return { key: url }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
override resourceScope(_target: Target, _kind: ResourceKind): string {
|
|
171
|
+
// An uploaded asset belongs to the bot; the same URL is valid in every channel it posts to.
|
|
172
|
+
return this.id
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
override async sendPayload(
|
|
176
|
+
payload: unknown,
|
|
177
|
+
target: Target,
|
|
178
|
+
ctx: SendContext,
|
|
179
|
+
): Promise<SentPart> {
|
|
180
|
+
return await sendPayload(
|
|
181
|
+
this.request,
|
|
182
|
+
payload as Parameters<typeof sendPayload>[1],
|
|
183
|
+
target,
|
|
184
|
+
ctx,
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
override async deletePart(
|
|
189
|
+
part: SentPart,
|
|
190
|
+
handle: Parameters<typeof deletePart>[2],
|
|
191
|
+
): Promise<void> {
|
|
192
|
+
await deletePart(this.request, part, handle)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
override async members(
|
|
196
|
+
chat: ChatRef,
|
|
197
|
+
cursor?: string,
|
|
198
|
+
limit?: number,
|
|
199
|
+
): Promise<MembersPage> {
|
|
200
|
+
return await members(this.request, chat, cursor, limit)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
override async getMessage(
|
|
204
|
+
chat: ChatRef,
|
|
205
|
+
id: string,
|
|
206
|
+
): Promise<InboundMessage> {
|
|
207
|
+
return await getMessage(this.request, this, chat, id)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
override classify(error: unknown) {
|
|
211
|
+
return classifyKookError(error)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export { Config }
|
|
216
|
+
|
|
217
|
+
export default KookBot
|