@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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +12 -0
  3. package/lib/index.js +2380 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/bot.d.ts +53 -0
  6. package/lib/types/bot.d.ts.map +1 -0
  7. package/lib/types/bot.js +138 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +46 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +52 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/decode.d.ts +88 -0
  14. package/lib/types/decode.d.ts.map +1 -0
  15. package/lib/types/decode.js +564 -0
  16. package/lib/types/decode.js.map +1 -0
  17. package/lib/types/errors.d.ts +46 -0
  18. package/lib/types/errors.d.ts.map +1 -0
  19. package/lib/types/errors.js +159 -0
  20. package/lib/types/errors.js.map +1 -0
  21. package/lib/types/identity.d.ts +17 -0
  22. package/lib/types/identity.d.ts.map +1 -0
  23. package/lib/types/identity.js +24 -0
  24. package/lib/types/identity.js.map +1 -0
  25. package/lib/types/index.d.ts +15 -0
  26. package/lib/types/index.d.ts.map +1 -0
  27. package/lib/types/index.js +15 -0
  28. package/lib/types/index.js.map +1 -0
  29. package/lib/types/kmarkdown.d.ts +76 -0
  30. package/lib/types/kmarkdown.d.ts.map +1 -0
  31. package/lib/types/kmarkdown.js +201 -0
  32. package/lib/types/kmarkdown.js.map +1 -0
  33. package/lib/types/modes/kmarkdown.d.ts +17 -0
  34. package/lib/types/modes/kmarkdown.d.ts.map +1 -0
  35. package/lib/types/modes/kmarkdown.js +166 -0
  36. package/lib/types/modes/kmarkdown.js.map +1 -0
  37. package/lib/types/modes/shared.d.ts +192 -0
  38. package/lib/types/modes/shared.d.ts.map +1 -0
  39. package/lib/types/modes/shared.js +371 -0
  40. package/lib/types/modes/shared.js.map +1 -0
  41. package/lib/types/request.d.ts +53 -0
  42. package/lib/types/request.d.ts.map +1 -0
  43. package/lib/types/request.js +147 -0
  44. package/lib/types/request.js.map +1 -0
  45. package/lib/types/send.d.ts +57 -0
  46. package/lib/types/send.d.ts.map +1 -0
  47. package/lib/types/send.js +141 -0
  48. package/lib/types/send.js.map +1 -0
  49. package/lib/types/types.d.ts +807 -0
  50. package/lib/types/types.d.ts.map +1 -0
  51. package/lib/types/types.js +168 -0
  52. package/lib/types/types.js.map +1 -0
  53. package/lib/types/webhook.d.ts +35 -0
  54. package/lib/types/webhook.d.ts.map +1 -0
  55. package/lib/types/webhook.js +132 -0
  56. package/lib/types/webhook.js.map +1 -0
  57. package/lib/types/ws.d.ts +54 -0
  58. package/lib/types/ws.d.ts.map +1 -0
  59. package/lib/types/ws.js +358 -0
  60. package/lib/types/ws.js.map +1 -0
  61. package/package.json +61 -3
  62. package/src/bot.ts +217 -0
  63. package/src/config.ts +84 -0
  64. package/src/decode.ts +660 -0
  65. package/src/errors.ts +188 -0
  66. package/src/identity.ts +32 -0
  67. package/src/index.ts +80 -0
  68. package/src/kmarkdown.ts +246 -0
  69. package/src/modes/kmarkdown.ts +204 -0
  70. package/src/modes/shared.ts +498 -0
  71. package/src/request.ts +196 -0
  72. package/src/send.ts +216 -0
  73. package/src/types.ts +764 -0
  74. package/src/webhook.ts +170 -0
  75. package/src/ws.ts +431 -0
  76. package/README.md +0 -3
@@ -0,0 +1,46 @@
1
+ import z from '@deepseek-ai/schemastery';
2
+ /** How the adapter receives KOOK events: the Gateway WebSocket (outbound) or a Webhook (inbound HTTP). */
3
+ export type KookProtocol = 'ws' | 'http';
4
+ /** One KOOK bot. */
5
+ export interface Config {
6
+ /** Bot token (`Authorization: Bot <token>`). */
7
+ token: string;
8
+ /** Bot user id: `bot.id = kook:<selfId>`, checked against `/user/me` on connect. */
9
+ selfId: string;
10
+ protocol: KookProtocol;
11
+ /** Inbound route of `protocol: 'http'`: absolute, no trailing slash. */
12
+ path: string;
13
+ /** Webhook source check (`verify_token`); required by `protocol: 'http'`. */
14
+ verifyToken: string;
15
+ /** API root; `/gateway/index`, `/message/create`, `/asset/create` and the rest hang off it. */
16
+ apiBaseUrl: string;
17
+ ws: {
18
+ /** Fast-retry budget of the Gateway reconnect loop; it never gives up. */
19
+ retryTimes: number;
20
+ /** Delay between dials while the fast-retry budget lasts. */
21
+ retryInterval: number;
22
+ /** Delay between dials once the fast-retry budget is spent. */
23
+ retryLazy: number;
24
+ };
25
+ }
26
+ /** Validated KOOK adapter configuration, also exported as `KookConfig`. */
27
+ export type KookConfig = Config;
28
+ /** Validated KOOK adapter configuration. */
29
+ export declare const Config: z<Config>;
30
+ /**
31
+ * The configuration the selected protocol cannot work from.
32
+ *
33
+ * The schema already refuses a missing token or a non-numeric `selfId`; what it cannot express is which credential a
34
+ * protocol needs, so that check runs when the plugin loads and names the corrective action.
35
+ *
36
+ * @param config - the validated configuration.
37
+ * @returns the configuration error, or undefined when the selection is complete.
38
+ */
39
+ export declare function configError(config: Config): Error | undefined;
40
+ /**
41
+ * Normalize an inbound route path: absolute, no trailing slash.
42
+ * @param path - the configured `path`.
43
+ * @returns the pathname to register.
44
+ */
45
+ export declare function routePath(path: string): string;
46
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAExC,0GAA0G;AAC1G,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,MAAM,CAAA;AAExC,oBAAoB;AACpB,MAAM,WAAW,MAAM;IACrB,gDAAgD;IAChD,KAAK,EAAE,MAAM,CAAA;IACb,oFAAoF;IACpF,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,YAAY,CAAA;IACtB,wEAAwE;IACxE,IAAI,EAAE,MAAM,CAAA;IACZ,6EAA6E;IAC7E,WAAW,EAAE,MAAM,CAAA;IACnB,+FAA+F;IAC/F,UAAU,EAAE,MAAM,CAAA;IAClB,EAAE,EAAE;QACF,0EAA0E;QAC1E,UAAU,EAAE,MAAM,CAAA;QAClB,6DAA6D;QAC7D,aAAa,EAAE,MAAM,CAAA;QACrB,+DAA+D;QAC/D,SAAS,EAAE,MAAM,CAAA;KAClB,CAAA;CACF;AAED,2EAA2E;AAC3E,MAAM,MAAM,UAAU,GAAG,MAAM,CAAA;AAE/B,4CAA4C;AAC5C,eAAO,MAAM,MAAM,EAAE,CAAC,CAAC,MAAM,CAc3B,CAAA;AAEF;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAgB7D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG9C"}
@@ -0,0 +1,52 @@
1
+ import z from '@deepseek-ai/schemastery';
2
+ /** Validated KOOK adapter configuration. */
3
+ export const Config = z.object({
4
+ token: z.string().role('secret').required(),
5
+ selfId: z.string().pattern(/^\d+$/).required(),
6
+ protocol: z.union(['ws', 'http']).required(),
7
+ path: z.string().default('/kook'),
8
+ verifyToken: z.string().role('secret').default(''),
9
+ apiBaseUrl: z.string().role('url').default('https://www.kookapp.cn/api/v3'),
10
+ ws: z
11
+ .object({
12
+ retryTimes: z.natural().default(6),
13
+ retryInterval: z.natural().role('ms').default(5_000),
14
+ retryLazy: z.natural().role('ms').default(60_000),
15
+ })
16
+ .default({}),
17
+ });
18
+ /**
19
+ * The configuration the selected protocol cannot work from.
20
+ *
21
+ * The schema already refuses a missing token or a non-numeric `selfId`; what it cannot express is which credential a
22
+ * protocol needs, so that check runs when the plugin loads and names the corrective action.
23
+ *
24
+ * @param config - the validated configuration.
25
+ * @returns the configuration error, or undefined when the selection is complete.
26
+ */
27
+ export function configError(config) {
28
+ let url;
29
+ try {
30
+ url = new URL(config.apiBaseUrl);
31
+ }
32
+ catch {
33
+ return new Error('kook: apiBaseUrl must be an absolute http(s) URL');
34
+ }
35
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
36
+ return new Error('kook: apiBaseUrl must be an absolute http(s) URL');
37
+ }
38
+ if (config.protocol === 'http' && config.verifyToken === '') {
39
+ return new Error('kook: protocol "http" needs verifyToken (the verify_token KOOK issues for the webhook)');
40
+ }
41
+ return undefined;
42
+ }
43
+ /**
44
+ * Normalize an inbound route path: absolute, no trailing slash.
45
+ * @param path - the configured `path`.
46
+ * @returns the pathname to register.
47
+ */
48
+ export function routePath(path) {
49
+ const withSlash = path.startsWith('/') ? path : `/${path}`;
50
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash;
51
+ }
52
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,0BAA0B,CAAA;AA+BxC,4CAA4C;AAC5C,MAAM,CAAC,MAAM,MAAM,GAAc,CAAC,CAAC,MAAM,CAAC;IACxC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC3C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE;IAC9C,QAAQ,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE;IAC5C,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC;IACjC,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC;IAClD,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,+BAA+B,CAAC;IA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MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AACzE,CAAC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Inbound decoding: one received KOOK payload becomes an intake `Session`, and one message object becomes an
3
+ * `InboundMessage`.
4
+ *
5
+ * The event kinds the vocabulary can carry are chat messages (types 1/2/3/4/8/9/10), button clicks, deletions and
6
+ * reactions; everything else is dropped with a debug line. A card is decoded module by module because KOOK now sends
7
+ * file messages as cards, so skipping a card would lose the file; an unknown module keeps a text placeholder instead
8
+ * of the whole card.
9
+ */
10
+ import type { ChatRef, Element, InboundMessage, Session, UserRef } from '@deepseek-ai/cordis-plugin-im-core';
11
+ import type { KookBot } from './bot.ts';
12
+ import { Type } from './types.ts';
13
+ import type { Author, Data, Message, MessageExtra, MessageMeta, User } from './types.ts';
14
+ /** Adapter-private context carried through `Target.referrer` and `Session.referrer`. */
15
+ export interface KookReferrer {
16
+ /** Channel id (group) or chat code (direct). */
17
+ chatId: string;
18
+ /** The platform's `channel_type` of the event. */
19
+ channelType: 'GROUP' | 'PERSON' | 'BROADCAST';
20
+ /** The message the event is about. */
21
+ messageId: string;
22
+ guildId?: string;
23
+ }
24
+ /**
25
+ * The chat one message event belongs to.
26
+ *
27
+ * A group message names its channel in `target_id`; a private one names the conversation (`chat_code`) in
28
+ * `extra.code`, which is the id `/user-chat/create-msg` addresses. That field is the adapter's riskiest assumption on
29
+ * the wire, so it is decoded exactly as the upstream adapter does.
30
+ */
31
+ export declare function chatOfMessage(data: Data, extra: MessageExtra, bot: KookBot): ChatRef;
32
+ /** A user reference: the id a mention addresses, the name people see, and whether the account is a bot. */
33
+ export declare function userRefOf(user: User | Author | undefined): UserRef;
34
+ /** The referrer of one message, handed back as `Target.referrer` so a reply can name the message it answers. */
35
+ export declare function referrerOf(chat: ChatRef, messageId: string, guildId?: string): KookReferrer;
36
+ /**
37
+ * Decode the body of one message by its platform type.
38
+ * @param base - the `type`/`content` pair of the message (an event or a read row).
39
+ * @param meta - the metadata carrying attachments, mention names and the quote.
40
+ * @returns the content nodes, in platform order.
41
+ */
42
+ export declare function decodeContent(base: {
43
+ type: Type | number;
44
+ content: string;
45
+ }, meta: MessageMeta | undefined): Element[];
46
+ /**
47
+ * Parse the mentions of a plain-text message: `@name#id`, `@全体成员`, `@在线成员`, `@role:<id>;` and
48
+ * `#channel:<id>;`.
49
+ *
50
+ * The upstream pattern had no `g` flag, so only the first mention of a message was decoded; every occurrence is
51
+ * decoded here so the model sees the whole mention list.
52
+ *
53
+ * @param content - `d.content` of a type-1 message.
54
+ * @returns the decoded content nodes.
55
+ */
56
+ export declare function parseTextMentions(content: string): Element[];
57
+ /**
58
+ * Decode a card message body (`type: 10`).
59
+ *
60
+ * @param content - the card JSON array, serialized as a string.
61
+ * @returns the decoded content nodes; the raw text when the body is not JSON.
62
+ */
63
+ export declare function decodeCard(content: string): Element[];
64
+ /**
65
+ * Decode one event message (`d.type !== 255`).
66
+ * @param bot - the receiving adapter.
67
+ * @param data - the event payload.
68
+ * @returns the decoded inbound message.
69
+ */
70
+ export declare function decodeInboundMessage(bot: KookBot, data: Data): InboundMessage;
71
+ /**
72
+ * Decode one message read by id (`/message/view`, `/user-chat/view`).
73
+ *
74
+ * @param bot - the adapter that read the message.
75
+ * @param message - the message row.
76
+ * @param chat - the chat the caller asked about; the read answer does not name its type, so the request stands.
77
+ * @returns the decoded inbound message.
78
+ */
79
+ export declare function decodeMessage(bot: KookBot, message: Message, chat: ChatRef): InboundMessage;
80
+ /**
81
+ * Decode one received payload.
82
+ *
83
+ * @param bot - the receiving adapter, whose `selfId` marks the bot's own mentions.
84
+ * @param data - the payload's `d` field.
85
+ * @returns the session to publish, or undefined for an event this adapter does not carry.
86
+ */
87
+ export declare function decodeEvent(bot: KookBot, data: Data): Session | undefined;
88
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA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