@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
package/src/config.ts ADDED
@@ -0,0 +1,84 @@
1
+ import z from '@deepseek-ai/schemastery'
2
+
3
+ /** How the adapter receives KOOK events: the Gateway WebSocket (outbound) or a Webhook (inbound HTTP). */
4
+ export type KookProtocol = 'ws' | 'http'
5
+
6
+ /** One KOOK bot. */
7
+ export interface Config {
8
+ /** Bot token (`Authorization: Bot <token>`). */
9
+ token: string
10
+ /** Bot user id: `bot.id = kook:<selfId>`, checked against `/user/me` on connect. */
11
+ selfId: string
12
+ protocol: KookProtocol
13
+ /** Inbound route of `protocol: 'http'`: absolute, no trailing slash. */
14
+ path: string
15
+ /** Webhook source check (`verify_token`); required by `protocol: 'http'`. */
16
+ verifyToken: string
17
+ /** API root; `/gateway/index`, `/message/create`, `/asset/create` and the rest hang off it. */
18
+ apiBaseUrl: string
19
+ ws: {
20
+ /** Fast-retry budget of the Gateway reconnect loop; it never gives up. */
21
+ retryTimes: number
22
+ /** Delay between dials while the fast-retry budget lasts. */
23
+ retryInterval: number
24
+ /** Delay between dials once the fast-retry budget is spent. */
25
+ retryLazy: number
26
+ }
27
+ }
28
+
29
+ /** Validated KOOK adapter configuration, also exported as `KookConfig`. */
30
+ export type KookConfig = Config
31
+
32
+ /** Validated KOOK adapter configuration. */
33
+ export const Config: z<Config> = z.object({
34
+ token: z.string().role('secret').required(),
35
+ selfId: z.string().pattern(/^\d+$/).required(),
36
+ protocol: z.union(['ws', 'http']).required(),
37
+ path: z.string().default('/kook'),
38
+ verifyToken: z.string().role('secret').default(''),
39
+ apiBaseUrl: z.string().role('url').default('https://www.kookapp.cn/api/v3'),
40
+ ws: z
41
+ .object({
42
+ retryTimes: z.natural().default(6),
43
+ retryInterval: z.natural().role('ms').default(5_000),
44
+ retryLazy: z.natural().role('ms').default(60_000),
45
+ })
46
+ .default({}),
47
+ })
48
+
49
+ /**
50
+ * The configuration the selected protocol cannot work from.
51
+ *
52
+ * The schema already refuses a missing token or a non-numeric `selfId`; what it cannot express is which credential a
53
+ * protocol needs, so that check runs when the plugin loads and names the corrective action.
54
+ *
55
+ * @param config - the validated configuration.
56
+ * @returns the configuration error, or undefined when the selection is complete.
57
+ */
58
+ export function configError(config: Config): Error | undefined {
59
+ let url: URL
60
+ try {
61
+ url = new URL(config.apiBaseUrl)
62
+ } catch {
63
+ return new Error('kook: apiBaseUrl must be an absolute http(s) URL')
64
+ }
65
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
66
+ return new Error('kook: apiBaseUrl must be an absolute http(s) URL')
67
+ }
68
+ if (config.protocol === 'http' && config.verifyToken === '') {
69
+ return new Error(
70
+ 'kook: protocol "http" needs verifyToken (the verify_token KOOK issues for the webhook)',
71
+ )
72
+ }
73
+ return undefined
74
+ }
75
+
76
+ /**
77
+ * Normalize an inbound route path: absolute, no trailing slash.
78
+ * @param path - the configured `path`.
79
+ * @returns the pathname to register.
80
+ */
81
+ export function routePath(path: string): string {
82
+ const withSlash = path.startsWith('/') ? path : `/${path}`
83
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash
84
+ }