@im-more/dsh-onebot 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 (86) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +11 -0
  3. package/lib/index.js +2003 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/bot.d.ts +55 -0
  6. package/lib/types/bot.d.ts.map +1 -0
  7. package/lib/types/bot.js +140 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +40 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +33 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/cqcode.d.ts +29 -0
  14. package/lib/types/cqcode.d.ts.map +1 -0
  15. package/lib/types/cqcode.js +73 -0
  16. package/lib/types/cqcode.js.map +1 -0
  17. package/lib/types/decode.d.ts +106 -0
  18. package/lib/types/decode.d.ts.map +1 -0
  19. package/lib/types/decode.js +433 -0
  20. package/lib/types/decode.js.map +1 -0
  21. package/lib/types/errors.d.ts +32 -0
  22. package/lib/types/errors.d.ts.map +1 -0
  23. package/lib/types/errors.js +117 -0
  24. package/lib/types/errors.js.map +1 -0
  25. package/lib/types/identity.d.ts +18 -0
  26. package/lib/types/identity.d.ts.map +1 -0
  27. package/lib/types/identity.js +27 -0
  28. package/lib/types/identity.js.map +1 -0
  29. package/lib/types/index.d.ts +7 -0
  30. package/lib/types/index.d.ts.map +1 -0
  31. package/lib/types/index.js +6 -0
  32. package/lib/types/index.js.map +1 -0
  33. package/lib/types/modes/segment.d.ts +16 -0
  34. package/lib/types/modes/segment.d.ts.map +1 -0
  35. package/lib/types/modes/segment.js +80 -0
  36. package/lib/types/modes/segment.js.map +1 -0
  37. package/lib/types/modes/shared.d.ts +101 -0
  38. package/lib/types/modes/shared.d.ts.map +1 -0
  39. package/lib/types/modes/shared.js +208 -0
  40. package/lib/types/modes/shared.js.map +1 -0
  41. package/lib/types/request.d.ts +64 -0
  42. package/lib/types/request.d.ts.map +1 -0
  43. package/lib/types/request.js +108 -0
  44. package/lib/types/request.js.map +1 -0
  45. package/lib/types/send.d.ts +93 -0
  46. package/lib/types/send.d.ts.map +1 -0
  47. package/lib/types/send.js +161 -0
  48. package/lib/types/send.js.map +1 -0
  49. package/lib/types/transport/http.d.ts +29 -0
  50. package/lib/types/transport/http.d.ts.map +1 -0
  51. package/lib/types/transport/http.js +158 -0
  52. package/lib/types/transport/http.js.map +1 -0
  53. package/lib/types/transport/shared.d.ts +48 -0
  54. package/lib/types/transport/shared.d.ts.map +1 -0
  55. package/lib/types/transport/shared.js +75 -0
  56. package/lib/types/transport/shared.js.map +1 -0
  57. package/lib/types/transport/ws-reverse.d.ts +42 -0
  58. package/lib/types/transport/ws-reverse.d.ts.map +1 -0
  59. package/lib/types/transport/ws-reverse.js +178 -0
  60. package/lib/types/transport/ws-reverse.js.map +1 -0
  61. package/lib/types/transport/ws.d.ts +40 -0
  62. package/lib/types/transport/ws.d.ts.map +1 -0
  63. package/lib/types/transport/ws.js +233 -0
  64. package/lib/types/transport/ws.js.map +1 -0
  65. package/lib/types/types.d.ts +495 -0
  66. package/lib/types/types.d.ts.map +1 -0
  67. package/lib/types/types.js +191 -0
  68. package/lib/types/types.js.map +1 -0
  69. package/package.json +68 -3
  70. package/src/bot.ts +230 -0
  71. package/src/config.ts +66 -0
  72. package/src/cqcode.ts +85 -0
  73. package/src/decode.ts +524 -0
  74. package/src/errors.ts +134 -0
  75. package/src/identity.ts +34 -0
  76. package/src/index.ts +18 -0
  77. package/src/modes/segment.ts +109 -0
  78. package/src/modes/shared.ts +277 -0
  79. package/src/request.ts +128 -0
  80. package/src/send.ts +233 -0
  81. package/src/transport/http.ts +221 -0
  82. package/src/transport/shared.ts +98 -0
  83. package/src/transport/ws-reverse.ts +236 -0
  84. package/src/transport/ws.ts +286 -0
  85. package/src/types.ts +743 -0
  86. package/README.md +0 -3
@@ -0,0 +1,208 @@
1
+ /** Group chat id prefix; the private prefix is the upstream one. */
2
+ export const GROUP_PREFIX = 'group:';
3
+ /** Private chat id prefix, unchanged from the upstream adapter. */
4
+ export const PRIVATE_PREFIX = 'private:';
5
+ /** A text segment node. */
6
+ export function textNode(text) {
7
+ return { kind: 'segment', segment: { type: 'text', data: { text } } };
8
+ }
9
+ /** The accumulated text of a draft's last item, when it is a text segment. */
10
+ function trailingText(items) {
11
+ const last = items[items.length - 1];
12
+ if (last === undefined || last.kind !== 'segment')
13
+ return undefined;
14
+ return last.segment.type === 'text' ? last.segment : undefined;
15
+ }
16
+ /** Drop the whitespace-only ends of a draft and the blank text segments around them. */
17
+ function trimItems(input) {
18
+ const items = [...input];
19
+ const textAt = (index) => {
20
+ const item = items[index];
21
+ if (item === undefined || item.kind !== 'segment')
22
+ return undefined;
23
+ return item.segment.type === 'text' ? item.segment : undefined;
24
+ };
25
+ while (items.length > 0) {
26
+ const first = textAt(0);
27
+ if (first === undefined)
28
+ break;
29
+ const text = String(first.data.text).trimStart();
30
+ if (text !== '') {
31
+ first.data.text = text;
32
+ break;
33
+ }
34
+ items.shift();
35
+ }
36
+ while (items.length > 0) {
37
+ const last = textAt(items.length - 1);
38
+ if (last === undefined)
39
+ break;
40
+ const text = String(last.data.text).trimEnd();
41
+ if (text !== '') {
42
+ last.data.text = text;
43
+ break;
44
+ }
45
+ items.pop();
46
+ }
47
+ return items.length > 0 ? items : undefined;
48
+ }
49
+ /**
50
+ * Fold the rendered node stream into platform messages.
51
+ *
52
+ * Consecutive inline nodes share one message segment array; `audio` / `video` / `file` start a new message first, as
53
+ * the upstream encoder flushes before them. Paragraph breaks collapse into one newline so `<p>a</p><p>b</p>` becomes
54
+ * `a\nb`. With `splitMixedContent` a space is kept between text and an image, in both directions. The ends of every
55
+ * message are trimmed and an all-whitespace message is dropped.
56
+ *
57
+ * @param nodes - rendered nodes in document order.
58
+ * @param splitMixedContent - the `advanced.splitMixedContent` setting.
59
+ * @returns one draft per platform message; empty when the content renders nothing.
60
+ */
61
+ export function assembleDraft(nodes, splitMixedContent) {
62
+ const drafts = [];
63
+ let items = [];
64
+ let pendingSpace = false;
65
+ const flush = () => {
66
+ const trimmed = trimItems(items);
67
+ if (trimmed !== undefined)
68
+ drafts.push({ kind: 'message', items: trimmed });
69
+ items = [];
70
+ pendingSpace = false;
71
+ };
72
+ const pushText = (source) => {
73
+ let text = source;
74
+ if (text === '')
75
+ return;
76
+ if (pendingSpace) {
77
+ pendingSpace = false;
78
+ if (!/^\s/.test(text))
79
+ text = ` ${text}`;
80
+ }
81
+ const last = trailingText(items);
82
+ if (last === undefined) {
83
+ items.push({ kind: 'segment', segment: { type: 'text', data: { text } } });
84
+ return;
85
+ }
86
+ last.data.text = `${String(last.data.text)}${text}`;
87
+ };
88
+ const ensureNewline = () => {
89
+ pendingSpace = false;
90
+ const last = trailingText(items);
91
+ if (last === undefined) {
92
+ items.push({
93
+ kind: 'segment',
94
+ segment: { type: 'text', data: { text: '\n' } },
95
+ });
96
+ return;
97
+ }
98
+ const text = String(last.data.text);
99
+ if (!text.endsWith('\n'))
100
+ last.data.text = `${text}\n`;
101
+ };
102
+ const pushMedia = (type, ref) => {
103
+ if (splitMixedContent) {
104
+ const last = trailingText(items);
105
+ if (last !== undefined && !/\s$/.test(String(last.data.text))) {
106
+ last.data.text = `${String(last.data.text)} `;
107
+ }
108
+ pendingSpace = true;
109
+ }
110
+ items.push({ kind: 'media', type, ref });
111
+ };
112
+ for (const node of nodes) {
113
+ switch (node.kind) {
114
+ case 'newline':
115
+ ensureNewline();
116
+ break;
117
+ case 'segment':
118
+ if (node.segment.type === 'text')
119
+ pushText(String(node.segment.data.text ?? ''));
120
+ else
121
+ items.push({ kind: 'segment', segment: node.segment });
122
+ break;
123
+ case 'media':
124
+ if (node.type !== 'image')
125
+ flush();
126
+ pushMedia(node.type, node.ref);
127
+ break;
128
+ case 'file':
129
+ flush();
130
+ drafts.push({ kind: 'file', ref: node.ref, name: node.name });
131
+ break;
132
+ }
133
+ }
134
+ flush();
135
+ return drafts;
136
+ }
137
+ /** The platform key `prepare` resolved for one placeholder. */
138
+ function resolvedKey(prepared, ref) {
139
+ const resolved = prepared.resolved.get(ref);
140
+ if (resolved === undefined) {
141
+ throw new Error('onebot: the resource was not prepared before serialize');
142
+ }
143
+ return resolved.key;
144
+ }
145
+ /**
146
+ * Build the exact request data of one draft.
147
+ *
148
+ * Nothing is escaped: the array message format carries every field value literally. Only the first part answers the
149
+ * quoted message, because a split message would otherwise repeat the quote on every part.
150
+ *
151
+ * @param prepared - the draft with its resources resolved.
152
+ * @param ctx - serialize context (target chat, part index, quote).
153
+ * @returns the request data of one platform message or file upload.
154
+ */
155
+ export function serializeDraft(prepared, ctx) {
156
+ const { draft } = prepared;
157
+ const chat = ctx.target.chat;
158
+ if (draft.kind === 'file') {
159
+ return {
160
+ kind: 'file',
161
+ chat,
162
+ key: resolvedKey(prepared, draft.ref),
163
+ name: draft.name,
164
+ };
165
+ }
166
+ const replyTo = ctx.index === 0 && ctx.quote !== undefined ? ctx.quote : undefined;
167
+ const segments = replyTo === undefined ? [] : [{ type: 'reply', data: { id: replyTo } }];
168
+ for (const item of draft.items) {
169
+ if (item.kind === 'segment') {
170
+ segments.push(item.segment);
171
+ }
172
+ else {
173
+ segments.push({
174
+ type: item.type,
175
+ data: { file: resolvedKey(prepared, item.ref), cache: 0 },
176
+ });
177
+ }
178
+ }
179
+ return {
180
+ kind: 'message',
181
+ chat,
182
+ segments,
183
+ ...(replyTo === undefined ? {} : { replyTo }),
184
+ };
185
+ }
186
+ /**
187
+ * The file name of an outbound `<file>` element.
188
+ * @param name - the element's `name` attribute, when set.
189
+ * @param src - the element's source.
190
+ * @returns the declared name, else the last path segment of the source, else `file`.
191
+ */
192
+ export function fileName(name, src) {
193
+ if (name !== undefined && name !== '')
194
+ return name;
195
+ try {
196
+ const path = new URL(src).pathname;
197
+ const segment = path.slice(path.lastIndexOf('/') + 1);
198
+ if (segment !== '')
199
+ return decodeURIComponent(segment);
200
+ }
201
+ catch {
202
+ // Not a URL: the source itself is the best name available.
203
+ if (src !== '' && !src.includes('/'))
204
+ return src;
205
+ }
206
+ return 'file';
207
+ }
208
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/modes/shared.ts"],"names":[],"mappings":"AASA,oEAAoE;AACpE,MAAM,CAAC,MAAM,YAAY,GAAG,QAAQ,CAAA;AACpC,mEAAmE;AACnE,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAA;AAoDxC,2BAA2B;AAC3B,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAA;AACvE,CAAC;AAED,8EAA8E;AAC9E,SAAS,YAAY,CAAC,KAA4B;IAChD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACpC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IACnE,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAA;AAChE,CAAC;AAED,wFAAwF;AACxF,SAAS,SAAS,CAAC,KAA4B;IAC7C,MAAM,KAAK,GAAG,CAAC,GAAG,KAAK,CAAC,CAAA;IACxB,MAAM,MAAM,GAAG,CAAC,KAAa,EAA6B,EAAE;QAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAA;QACzB,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;YAAE,OAAO,SAAS,CAAA;QACnE,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MA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AClC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;QACrD,IAAI,OAAO,KAAK,EAAE;YAAE,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAA;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,2DAA2D;QAC3D,IAAI,GAAG,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC;YAAE,OAAO,GAAG,CAAA;IAClD,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
@@ -0,0 +1,64 @@
1
+ import type { Dict } from '@deepseek-ai/cosmokit';
2
+ import type { OneBotBot } from './bot.ts';
3
+ import type { Response as OneBotResponse } from './types.ts';
4
+ /** One action call sent to the implementation. */
5
+ export interface ActionFrame {
6
+ action: string;
7
+ params: Dict;
8
+ /** Answer correlation id, unique per connection. */
9
+ echo: number;
10
+ }
11
+ /**
12
+ * Action calls over one WebSocket connection.
13
+ *
14
+ * The implementation answers an action with the envelope it was sent, in `echo`; the pending table is per connection
15
+ * so a reconnect can never deliver an answer to the previous socket. A request that is not answered within
16
+ * `ws.responseTimeout` is rejected with the upstream `TimeoutError` and forgotten, and closing the connection rejects
17
+ * everything still waiting.
18
+ */
19
+ export declare class ActionRequests {
20
+ private readonly send;
21
+ private readonly timeoutMs;
22
+ private echo;
23
+ private readonly pending;
24
+ private closed;
25
+ constructor(send: (frame: ActionFrame) => void, timeoutMs: number);
26
+ /**
27
+ * Send one action and wait for its answer.
28
+ * @param action - OneBot 11 action name (`send_group_msg`, …).
29
+ * @param params - action parameters.
30
+ * @returns the answer envelope (the caller reads `retcode` / `data`).
31
+ */
32
+ request: (action: string, params: Dict) => Promise<OneBotResponse>;
33
+ /**
34
+ * Deliver one answer to its waiting call.
35
+ * @param response - the parsed frame.
36
+ * @returns whether an outstanding call was waiting for it.
37
+ */
38
+ resolve(response: OneBotResponse): boolean;
39
+ /**
40
+ * Refuse every waiting call; calls made afterwards fail immediately.
41
+ * @param error - the failure to reject with.
42
+ */
43
+ rejectAll(error: Error): void;
44
+ private settle;
45
+ }
46
+ /**
47
+ * Make a connection's `request` the bot's action entry point (`bot.internal._request`).
48
+ * @param bot - the adapter instance.
49
+ * @param request - action caller of the live connection.
50
+ */
51
+ export declare function installRequest(bot: OneBotBot, request: (action: string, params: Dict) => Promise<OneBotResponse>): void;
52
+ /**
53
+ * Make every action call of the bot fail without a connection.
54
+ * @param bot - the adapter instance.
55
+ * @param error - the failure calls report.
56
+ */
57
+ export declare function rejectRequests(bot: OneBotBot, error: Error): void;
58
+ /**
59
+ * Whether a parsed frame is one action answer.
60
+ * @param value - a parsed JSON frame from the implementation.
61
+ * @returns whether it carries an `echo`.
62
+ */
63
+ export declare function isResponse(value: unknown): value is OneBotResponse;
64
+ //# sourceMappingURL=request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,KAAK,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,YAAY,CAAA;AAG5D,kDAAkD;AAClD,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,IAAI,CAAA;IACZ,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAA;CACb;AAQD;;;;;;;GAOG;AACH,qBAAa,cAAc;IAMvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAN5B,OAAO,CAAC,IAAI,CAAI;IAChB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6B;IACrD,OAAO,CAAC,MAAM,CAAmB;gBAGd,IAAI,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,EAClC,SAAS,EAAE,MAAM;IAGpC;;;;;OAKG;IACH,OAAO,GAAI,QAAQ,MAAM,EAAE,QAAQ,IAAI,KAAG,OAAO,CAAC,cAAc,CAAC,CAgBhE;IAED;;;;OAIG;IACH,OAAO,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO;IAU1C;;;OAGG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAS7B,OAAO,CAAC,MAAM;CAOf;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,SAAS,EACd,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,KAAK,OAAO,CAAC,cAAc,CAAC,GACjE,IAAI,CAEN;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAEjE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAGlE"}
@@ -0,0 +1,108 @@
1
+ import { TimeoutError } from "./types.js";
2
+ /**
3
+ * Action calls over one WebSocket connection.
4
+ *
5
+ * The implementation answers an action with the envelope it was sent, in `echo`; the pending table is per connection
6
+ * so a reconnect can never deliver an answer to the previous socket. A request that is not answered within
7
+ * `ws.responseTimeout` is rejected with the upstream `TimeoutError` and forgotten, and closing the connection rejects
8
+ * everything still waiting.
9
+ */
10
+ export class ActionRequests {
11
+ send;
12
+ timeoutMs;
13
+ echo = 0;
14
+ pending = new Map();
15
+ closed;
16
+ constructor(send, timeoutMs) {
17
+ this.send = send;
18
+ this.timeoutMs = timeoutMs;
19
+ }
20
+ /**
21
+ * Send one action and wait for its answer.
22
+ * @param action - OneBot 11 action name (`send_group_msg`, …).
23
+ * @param params - action parameters.
24
+ * @returns the answer envelope (the caller reads `retcode` / `data`).
25
+ */
26
+ request = (action, params) => {
27
+ if (this.closed !== undefined)
28
+ return Promise.reject(this.closed);
29
+ const echo = ++this.echo;
30
+ const frame = { action, params, echo };
31
+ return new Promise((resolve, reject) => {
32
+ const timer = setTimeout(() => {
33
+ this.pending.delete(echo);
34
+ reject(new TimeoutError(params, action));
35
+ }, this.timeoutMs);
36
+ this.pending.set(echo, { resolve, reject, timer });
37
+ try {
38
+ this.send(frame);
39
+ }
40
+ catch (error) {
41
+ this.settle(echo, error);
42
+ }
43
+ });
44
+ };
45
+ /**
46
+ * Deliver one answer to its waiting call.
47
+ * @param response - the parsed frame.
48
+ * @returns whether an outstanding call was waiting for it.
49
+ */
50
+ resolve(response) {
51
+ if (typeof response.echo !== 'number')
52
+ return false;
53
+ const pending = this.pending.get(response.echo);
54
+ if (pending === undefined)
55
+ return false;
56
+ this.pending.delete(response.echo);
57
+ clearTimeout(pending.timer);
58
+ pending.resolve(response);
59
+ return true;
60
+ }
61
+ /**
62
+ * Refuse every waiting call; calls made afterwards fail immediately.
63
+ * @param error - the failure to reject with.
64
+ */
65
+ rejectAll(error) {
66
+ this.closed = error;
67
+ for (const [echo, pending] of this.pending) {
68
+ this.pending.delete(echo);
69
+ clearTimeout(pending.timer);
70
+ pending.reject(error);
71
+ }
72
+ }
73
+ settle(echo, error) {
74
+ const pending = this.pending.get(echo);
75
+ if (pending === undefined)
76
+ return;
77
+ this.pending.delete(echo);
78
+ clearTimeout(pending.timer);
79
+ pending.reject(error instanceof Error ? error : new Error(String(error)));
80
+ }
81
+ }
82
+ /**
83
+ * Make a connection's `request` the bot's action entry point (`bot.internal._request`).
84
+ * @param bot - the adapter instance.
85
+ * @param request - action caller of the live connection.
86
+ */
87
+ export function installRequest(bot, request) {
88
+ bot.internal._request = request;
89
+ }
90
+ /**
91
+ * Make every action call of the bot fail without a connection.
92
+ * @param bot - the adapter instance.
93
+ * @param error - the failure calls report.
94
+ */
95
+ export function rejectRequests(bot, error) {
96
+ bot.internal._request = () => Promise.reject(error);
97
+ }
98
+ /**
99
+ * Whether a parsed frame is one action answer.
100
+ * @param value - a parsed JSON frame from the implementation.
101
+ * @returns whether it carries an `echo`.
102
+ */
103
+ export function isResponse(value) {
104
+ if (typeof value !== 'object' || value === null)
105
+ return false;
106
+ return typeof value.echo === 'number';
107
+ }
108
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAgBzC;;;;;;;GAOG;AACH,MAAM,OAAO,cAAc;IAMN;IACA;IANX,IAAI,GAAG,CAAC,CAAA;IACC,OAAO,GAAG,IAAI,GAAG,EAAmB,CAAA;IAC7C,MAAM,CAAmB;IAEjC,YACmB,IAAkC,EAClC,SAAiB;QADjB,SAAI,GAAJ,IAAI,CAA8B;QAClC,cAAS,GAAT,SAAS,CAAQ;IACjC,CAAC;IAEJ;;;;;OAKG;IACH,OAAO,GAAG,CAAC,MAAc,EAAE,MAAY,EAA2B,EAAE;QAClE,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS;YAAE,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACjE,MAAM,IAAI,GAAG,EAAE,IAAI,CAAC,IAAI,CAAA;QACxB,MAAM,KAAK,GAAgB,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAA;QACnD,OAAO,IAAI,OAAO,CAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrD,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC5B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;gBACzB,MAAM,CAAC,IAAI,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;YAC1C,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;YAClB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,MA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AAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACzB,YAAY,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAC3E,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,GAAc,EACd,OAAkE;IAElE,GAAG,CAAC,QAAQ,CAAC,QAAQ,GAAG,OAAO,CAAA;AACjC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,GAAc,EAAE,KAAY;IACzD,GAAG,CAAC,QAAQ,CAAC,QAAQ,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IAC7D,OAAO,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ,CAAA;AAC/D,CAAC"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Outbound hooks: the send pipeline's payload, deletion, member reads, message reads and reactions.
3
+ *
4
+ * Requests go through the migrated `Internal` surface, so the action names and parameter order are the upstream ones.
5
+ * Images, records and videos travel as the source string itself; a `<file>` becomes `download_file` plus
6
+ * `upload_group_file` / `upload_private_file`, because those actions accept only a path on the implementation's host.
7
+ */
8
+ import { type ChatRef, type InboundMessage, type MembersPage, type MessageRef, type SendContext, type SentPart, type Target } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import type { OneBotBot } from './bot.ts';
10
+ import { type OneBotPayload } from './modes/shared.ts';
11
+ import type { Internal } from './types.ts';
12
+ /** Everything the send hooks need from the adapter. */
13
+ export interface OneBotSendDeps {
14
+ readonly internal: Internal;
15
+ }
16
+ /** A chat id split into the action family that addresses it. */
17
+ export interface OneBotChat {
18
+ kind: 'group' | 'private';
19
+ id: string;
20
+ }
21
+ /**
22
+ * Split a DSH chat id into the OneBot action family and the platform id.
23
+ * @param chat - the target chat.
24
+ * @returns kind and raw id.
25
+ * @throws PlatformError `invalid` for a chat id this adapter did not produce.
26
+ */
27
+ export declare function chatTarget(chat: ChatRef): OneBotChat;
28
+ /**
29
+ * Normalize one media source without fetching it.
30
+ *
31
+ * A `data:` URI becomes the `base64://` form the implementation decodes; every other source (an `http(s)` URL, a
32
+ * `base64://` payload, a path) is passed through unchanged, because the implementation fetches it itself.
33
+ *
34
+ * @param src - the element's source.
35
+ * @returns the string to put in the segment's `file` field.
36
+ */
37
+ export declare function normalizeSource(src: string): string;
38
+ /**
39
+ * Send one serialized payload.
40
+ * @param deps - the action client.
41
+ * @param payload - the mode's request data.
42
+ * @param target - the target chat.
43
+ * @param ctx - part index, quote and options.
44
+ * @returns the published part.
45
+ */
46
+ export declare function sendPayload(deps: OneBotSendDeps, payload: OneBotPayload, _target: Target, _ctx: SendContext): Promise<SentPart>;
47
+ /**
48
+ * Recall one published part.
49
+ *
50
+ * A file part carries a synthesized id that no platform message answers, so it is skipped instead of recalling an
51
+ * unrelated message.
52
+ *
53
+ * @param deps - the action client.
54
+ * @param part - the part to recall.
55
+ */
56
+ export declare function deletePart(deps: OneBotSendDeps, part: SentPart): Promise<void>;
57
+ /**
58
+ * List the members of one group.
59
+ * @param deps - the action client.
60
+ * @param chat - the group chat.
61
+ * @returns the whole member list; the standard offers no paging.
62
+ */
63
+ export declare function members(deps: OneBotSendDeps, chat: ChatRef): Promise<MembersPage>;
64
+ /**
65
+ * Read one message, expanding any merged forward it carries.
66
+ * @param deps - the action client.
67
+ * @param bot - the adapter, needed by the forward expansion.
68
+ * @param chat - the chat the caller asked about; the answer carries no chat, so it is reused verbatim.
69
+ * @param id - platform message id.
70
+ * @returns the decoded message, with the same `chat.id` the caller passed.
71
+ */
72
+ export declare function getMessage(deps: OneBotSendDeps, bot: OneBotBot, chat: ChatRef, id: string): Promise<InboundMessage>;
73
+ /**
74
+ * Add one reaction to a published message.
75
+ * @param deps - the action client.
76
+ * @param message - the message to react to.
77
+ * @param emoji - platform emoji id.
78
+ */
79
+ export declare function createReaction(deps: OneBotSendDeps, message: MessageRef, emoji: string): Promise<void>;
80
+ /**
81
+ * Remove one reaction from a published message.
82
+ * @param deps - the action client.
83
+ * @param message - the message to react to.
84
+ * @param emoji - platform emoji id.
85
+ */
86
+ export declare function deleteReaction(deps: OneBotSendDeps, message: MessageRef, emoji: string): Promise<void>;
87
+ /**
88
+ * Convert a `file:` source into the local path an upload action accepts.
89
+ * @param src - a `file:` URI.
90
+ * @returns the filesystem path.
91
+ */
92
+ export declare function localPathOf(src: string): string;
93
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/send.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAEL,KAAK,OAAO,EACZ,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,MAAM,EAEZ,MAAM,oCAAoC,CAAA;AAG3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGzC,OAAO,EAGL,KAAK,aAAa,EACnB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAE1C,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;CAC5B;AAED,gEAAgE;AAChE,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,GAAG,SAAS,CAAA;IACzB,EAAE,EAAE,MAAM,CAAA;CACX;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAWpD;AAKD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGnD;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,aAAa,EACtB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,WAAW,GAChB,OAAO,CAAC,QAAQ,CAAC,CAwCnB;AAED;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,QAAQ,GACb,OAAO,CAAC,IAAI,CAAC,CAGf;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,CAC3B,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,OAAO,GACZ,OAAO,CAAC,WAAW,CAAC,CAkBtB;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,IAAI,EAAE,cAAc,EACpB,GAAG,EAAE,SAAS,EACd,IAAI,EAAE,OAAO,EACb,EAAE,EAAE,MAAM,GACT,OAAO,CAAC,cAAc,CAAC,CAGzB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE/C"}
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Outbound hooks: the send pipeline's payload, deletion, member reads, message reads and reactions.
3
+ *
4
+ * Requests go through the migrated `Internal` surface, so the action names and parameter order are the upstream ones.
5
+ * Images, records and videos travel as the source string itself; a `<file>` becomes `download_file` plus
6
+ * `upload_group_file` / `upload_private_file`, because those actions accept only a path on the implementation's host.
7
+ */
8
+ import { PlatformError, } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import { randomUUID } from 'node:crypto';
10
+ import { fileURLToPath } from 'node:url';
11
+ import { decodeMessage, expandForward } from "./decode.js";
12
+ import { GROUP_PREFIX, PRIVATE_PREFIX, } from "./modes/shared.js";
13
+ /**
14
+ * Split a DSH chat id into the OneBot action family and the platform id.
15
+ * @param chat - the target chat.
16
+ * @returns kind and raw id.
17
+ * @throws PlatformError `invalid` for a chat id this adapter did not produce.
18
+ */
19
+ export function chatTarget(chat) {
20
+ if (chat.id.startsWith(GROUP_PREFIX)) {
21
+ return { kind: 'group', id: chat.id.slice(GROUP_PREFIX.length) };
22
+ }
23
+ if (chat.id.startsWith(PRIVATE_PREFIX)) {
24
+ return { kind: 'private', id: chat.id.slice(PRIVATE_PREFIX.length) };
25
+ }
26
+ throw new PlatformError('invalid', `onebot: chat id "${chat.id}" must start with "group:" or "private:"`);
27
+ }
28
+ /** Data URI prefix of an inline image; the implementation wants the `base64://` form instead. */
29
+ const DATA_URL = /^data:([\w/.+-]+);base64,/;
30
+ /**
31
+ * Normalize one media source without fetching it.
32
+ *
33
+ * A `data:` URI becomes the `base64://` form the implementation decodes; every other source (an `http(s)` URL, a
34
+ * `base64://` payload, a path) is passed through unchanged, because the implementation fetches it itself.
35
+ *
36
+ * @param src - the element's source.
37
+ * @returns the string to put in the segment's `file` field.
38
+ */
39
+ export function normalizeSource(src) {
40
+ const match = DATA_URL.exec(src);
41
+ return match === null ? src : `base64://${src.slice(match[0].length)}`;
42
+ }
43
+ /**
44
+ * Send one serialized payload.
45
+ * @param deps - the action client.
46
+ * @param payload - the mode's request data.
47
+ * @param target - the target chat.
48
+ * @param ctx - part index, quote and options.
49
+ * @returns the published part.
50
+ */
51
+ export async function sendPayload(deps, payload, _target, _ctx) {
52
+ if (payload.kind === 'file') {
53
+ const chat = chatTarget(payload.chat);
54
+ if (chat.kind === 'group') {
55
+ await deps.internal.uploadGroupFile(chat.id, payload.key, payload.name);
56
+ }
57
+ else {
58
+ await deps.internal.uploadPrivateFile(chat.id, payload.key, payload.name);
59
+ }
60
+ // Neither action answers with a message id, so the part carries a synthesized one the platform cannot address.
61
+ const id = `onebot-file:${randomUUID()}`;
62
+ return {
63
+ id,
64
+ kind: 'file',
65
+ time: Date.now(),
66
+ referrer: { chatId: payload.chat.id, messageId: id },
67
+ };
68
+ }
69
+ const chat = chatTarget(payload.chat);
70
+ const segments = payload.segments;
71
+ const sent = chat.kind === 'group'
72
+ ? await deps.internal.sendGroupMsg(chat.id, segments)
73
+ : await deps.internal.sendPrivateMsg(chat.id, segments);
74
+ const messageId = sent;
75
+ if (messageId === undefined) {
76
+ // The request reached the implementation, so the outcome is unknown rather than unpublished.
77
+ throw new PlatformError('invalid', 'onebot: the implementation answered a send without message_id', { published: 'unknown' });
78
+ }
79
+ const id = String(messageId);
80
+ return {
81
+ id,
82
+ kind: 'message',
83
+ time: Date.now(),
84
+ referrer: { chatId: payload.chat.id, messageId: id },
85
+ };
86
+ }
87
+ /**
88
+ * Recall one published part.
89
+ *
90
+ * A file part carries a synthesized id that no platform message answers, so it is skipped instead of recalling an
91
+ * unrelated message.
92
+ *
93
+ * @param deps - the action client.
94
+ * @param part - the part to recall.
95
+ */
96
+ export async function deletePart(deps, part) {
97
+ if (part.kind === 'file')
98
+ return;
99
+ await deps.internal.deleteMsg(part.id);
100
+ }
101
+ /**
102
+ * List the members of one group.
103
+ * @param deps - the action client.
104
+ * @param chat - the group chat.
105
+ * @returns the whole member list; the standard offers no paging.
106
+ */
107
+ export async function members(deps, chat) {
108
+ const target = chatTarget(chat);
109
+ if (target.kind !== 'group') {
110
+ throw new PlatformError('invalid', 'onebot: members() is only available in a group chat');
111
+ }
112
+ const list = await deps.internal.getGroupMemberList(target.id);
113
+ const data = list.map((member) => {
114
+ const name = member.card || member.nickname;
115
+ return {
116
+ id: String(member.user_id),
117
+ ...(name === '' ? {} : { name }),
118
+ ...(member.role === undefined ? {} : { role: member.role }),
119
+ };
120
+ });
121
+ return { data };
122
+ }
123
+ /**
124
+ * Read one message, expanding any merged forward it carries.
125
+ * @param deps - the action client.
126
+ * @param bot - the adapter, needed by the forward expansion.
127
+ * @param chat - the chat the caller asked about; the answer carries no chat, so it is reused verbatim.
128
+ * @param id - platform message id.
129
+ * @returns the decoded message, with the same `chat.id` the caller passed.
130
+ */
131
+ export async function getMessage(deps, bot, chat, id) {
132
+ const data = await deps.internal.getMsg(id);
133
+ return expandForward(bot, decodeMessage(bot, data, chat));
134
+ }
135
+ /**
136
+ * Add one reaction to a published message.
137
+ * @param deps - the action client.
138
+ * @param message - the message to react to.
139
+ * @param emoji - platform emoji id.
140
+ */
141
+ export async function createReaction(deps, message, emoji) {
142
+ await deps.internal.setMsgEmojiLike(message.id, emoji, true);
143
+ }
144
+ /**
145
+ * Remove one reaction from a published message.
146
+ * @param deps - the action client.
147
+ * @param message - the message to react to.
148
+ * @param emoji - platform emoji id.
149
+ */
150
+ export async function deleteReaction(deps, message, emoji) {
151
+ await deps.internal.setMsgEmojiLike(message.id, emoji, false);
152
+ }
153
+ /**
154
+ * Convert a `file:` source into the local path an upload action accepts.
155
+ * @param src - a `file:` URI.
156
+ * @returns the filesystem path.
157
+ */
158
+ export function localPathOf(src) {
159
+ return fileURLToPath(src);
160
+ }
161
+ //# sourceMappingURL=send.js.map