@im-more/dsh-matrix 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 +14 -0
  3. package/lib/index.js +2401 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/as.d.ts +33 -0
  6. package/lib/types/as.d.ts.map +1 -0
  7. package/lib/types/as.js +237 -0
  8. package/lib/types/as.js.map +1 -0
  9. package/lib/types/bot.d.ts +109 -0
  10. package/lib/types/bot.d.ts.map +1 -0
  11. package/lib/types/bot.js +234 -0
  12. package/lib/types/bot.js.map +1 -0
  13. package/lib/types/config.d.ts +59 -0
  14. package/lib/types/config.d.ts.map +1 -0
  15. package/lib/types/config.js +67 -0
  16. package/lib/types/config.js.map +1 -0
  17. package/lib/types/decode.d.ts +56 -0
  18. package/lib/types/decode.d.ts.map +1 -0
  19. package/lib/types/decode.js +298 -0
  20. package/lib/types/decode.js.map +1 -0
  21. package/lib/types/errors.d.ts +45 -0
  22. package/lib/types/errors.d.ts.map +1 -0
  23. package/lib/types/errors.js +120 -0
  24. package/lib/types/errors.js.map +1 -0
  25. package/lib/types/html.d.ts +44 -0
  26. package/lib/types/html.d.ts.map +1 -0
  27. package/lib/types/html.js +286 -0
  28. package/lib/types/html.js.map +1 -0
  29. package/lib/types/identity.d.ts +20 -0
  30. package/lib/types/identity.d.ts.map +1 -0
  31. package/lib/types/identity.js +136 -0
  32. package/lib/types/identity.js.map +1 -0
  33. package/lib/types/index.d.ts +5 -0
  34. package/lib/types/index.d.ts.map +1 -0
  35. package/lib/types/index.js +5 -0
  36. package/lib/types/index.js.map +1 -0
  37. package/lib/types/media.d.ts +54 -0
  38. package/lib/types/media.d.ts.map +1 -0
  39. package/lib/types/media.js +94 -0
  40. package/lib/types/media.js.map +1 -0
  41. package/lib/types/mentions.d.ts +58 -0
  42. package/lib/types/mentions.d.ts.map +1 -0
  43. package/lib/types/mentions.js +94 -0
  44. package/lib/types/mentions.js.map +1 -0
  45. package/lib/types/modes/html.d.ts +13 -0
  46. package/lib/types/modes/html.d.ts.map +1 -0
  47. package/lib/types/modes/html.js +242 -0
  48. package/lib/types/modes/html.js.map +1 -0
  49. package/lib/types/modes/shared.d.ts +164 -0
  50. package/lib/types/modes/shared.d.ts.map +1 -0
  51. package/lib/types/modes/shared.js +312 -0
  52. package/lib/types/modes/shared.js.map +1 -0
  53. package/lib/types/request.d.ts +82 -0
  54. package/lib/types/request.d.ts.map +1 -0
  55. package/lib/types/request.js +206 -0
  56. package/lib/types/request.js.map +1 -0
  57. package/lib/types/rooms.d.ts +83 -0
  58. package/lib/types/rooms.d.ts.map +1 -0
  59. package/lib/types/rooms.js +177 -0
  60. package/lib/types/rooms.js.map +1 -0
  61. package/lib/types/send.d.ts +76 -0
  62. package/lib/types/send.d.ts.map +1 -0
  63. package/lib/types/send.js +172 -0
  64. package/lib/types/send.js.map +1 -0
  65. package/lib/types/types.d.ts +418 -0
  66. package/lib/types/types.d.ts.map +1 -0
  67. package/lib/types/types.js +134 -0
  68. package/lib/types/types.js.map +1 -0
  69. package/package.json +65 -3
  70. package/src/as.ts +293 -0
  71. package/src/bot.ts +338 -0
  72. package/src/config.ts +100 -0
  73. package/src/decode.ts +392 -0
  74. package/src/errors.ts +149 -0
  75. package/src/html.ts +325 -0
  76. package/src/identity.ts +165 -0
  77. package/src/index.ts +14 -0
  78. package/src/media.ts +132 -0
  79. package/src/mentions.ts +134 -0
  80. package/src/modes/html.ts +346 -0
  81. package/src/modes/shared.ts +485 -0
  82. package/src/request.ts +295 -0
  83. package/src/rooms.ts +232 -0
  84. package/src/send.ts +275 -0
  85. package/src/types.ts +633 -0
  86. package/README.md +0 -3
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The single `html` mode's node, draft and payload types, HTML escaping, media promotion, reply/thread relations and
3
+ * payload assembly.
4
+ *
5
+ * A Matrix message is either one `m.text` event with `body` + `formatted_body`, or one media event per media element.
6
+ * Every handler therefore returns an HTML fragment and its plain-text projection from the same traversal, so the two
7
+ * sides cannot drift; `assemble` flushes a text draft around each media element.
8
+ */
9
+ import type { ModeTypes, Prepared, RenderContext, ResourceRef, SerializeContext } from '@deepseek-ai/cordis-plugin-im-core';
10
+ import type { Element } from '@deepseek-ai/satori-element';
11
+ import { createMentionSet } from '../mentions.ts';
12
+ import type { MentionNode, MatrixMentionWire } from '../mentions.ts';
13
+ /** Media elements Matrix publishes as their own event. */
14
+ export type MatrixMediaTag = 'img' | 'audio' | 'video' | 'file';
15
+ /** One inline run: its HTML and plain-text projections, and the mentions it contributes. */
16
+ export interface MatrixHtmlNode {
17
+ type: 'html';
18
+ html: string;
19
+ text: string;
20
+ mentions?: MentionNode[];
21
+ }
22
+ /** One media element, to be published after the text run it stood in. */
23
+ export interface MatrixMediaNode {
24
+ type: 'media';
25
+ tag: MatrixMediaTag;
26
+ ref: ResourceRef;
27
+ name?: string;
28
+ width?: number;
29
+ height?: number;
30
+ duration?: number;
31
+ }
32
+ /** One node a mode handler produces. */
33
+ export type MatrixNode = MatrixHtmlNode | MatrixMediaNode;
34
+ /** One text event before resource resolution. */
35
+ export interface MatrixTextDraft {
36
+ kind: 'text';
37
+ html: string;
38
+ text: string;
39
+ mentions: MatrixMentionWire;
40
+ }
41
+ /** One media event before resource resolution. */
42
+ export interface MatrixMediaDraft {
43
+ kind: 'media';
44
+ tag: MatrixMediaTag;
45
+ ref: ResourceRef;
46
+ name?: string;
47
+ width?: number;
48
+ height?: number;
49
+ duration?: number;
50
+ mentions: MatrixMentionWire;
51
+ }
52
+ /** One platform event before resource resolution. */
53
+ export type MatrixDraft = MatrixTextDraft | MatrixMediaDraft;
54
+ /** `info` of one media event. */
55
+ export interface MatrixMediaInfo {
56
+ w?: number;
57
+ h?: number;
58
+ mimetype?: string;
59
+ size?: number;
60
+ duration?: number;
61
+ }
62
+ /** The thread relation one payload carries. */
63
+ export interface MatrixThread {
64
+ root: string;
65
+ }
66
+ /** The `m.relates_to` object of one payload. */
67
+ export interface MatrixRelation {
68
+ rel_type?: 'm.thread';
69
+ event_id?: string;
70
+ 'm.in_reply_to'?: {
71
+ event_id: string;
72
+ };
73
+ is_falling_back?: boolean;
74
+ }
75
+ /** One `m.text` request body. */
76
+ export interface MatrixTextPayload {
77
+ kind: 'text';
78
+ roomId: string;
79
+ body: string;
80
+ html?: string;
81
+ mentions: MatrixMentionWire;
82
+ relates?: MatrixRelation;
83
+ thread?: MatrixThread;
84
+ }
85
+ /** One media request body (`m.image` / `m.file` / `m.audio` / `m.video`). */
86
+ export interface MatrixMediaPayload {
87
+ kind: 'media';
88
+ roomId: string;
89
+ msgtype: 'm.image' | 'm.file' | 'm.audio' | 'm.video';
90
+ url: string;
91
+ body: string;
92
+ info?: MatrixMediaInfo;
93
+ mentions: MatrixMentionWire;
94
+ relates?: MatrixRelation;
95
+ thread?: MatrixThread;
96
+ }
97
+ /** Exact request data of one platform event. */
98
+ export type MatrixPayload = MatrixTextPayload | MatrixMediaPayload;
99
+ /** Mode types of the `html` mode. */
100
+ export interface MatrixModeTypes extends ModeTypes {
101
+ node: MatrixNode;
102
+ draft: MatrixDraft;
103
+ payload: MatrixPayload;
104
+ }
105
+ /** Escape character data of `formatted_body`. */
106
+ export declare function escapeHtml(text: string): string;
107
+ /** Escape one HTML attribute value (double-quoted). */
108
+ export declare function escapeAttribute(text: string): string;
109
+ /** One inline node. */
110
+ export declare function htmlNode(html: string, text: string, mentions?: MentionNode[]): MatrixHtmlNode;
111
+ /** The mutable mention collection one inline run accumulates. */
112
+ export type MatrixMentionSet = ReturnType<typeof createMentionSet>;
113
+ /** One mention node per collected user and one for the room mention. */
114
+ export declare function mentionNodes(set: MatrixMentionSet): MentionNode[];
115
+ /** The inline HTML and text of rendered children, plus the media that cannot ride inside the run. */
116
+ export interface InlineRun {
117
+ html: string;
118
+ text: string;
119
+ mentions: MatrixMentionSet;
120
+ rest: MatrixNode[];
121
+ }
122
+ /**
123
+ * Split rendered children into one inline run and the media nodes that follow it.
124
+ * @param nodes - rendered children.
125
+ * @returns the concatenated HTML/text, the collected mentions and the media nodes, in source order.
126
+ */
127
+ export declare function inlineRun(nodes: readonly MatrixNode[]): InlineRun;
128
+ /**
129
+ * Wrap the inline text of rendered children in one HTML tag; media of the subtree follows the wrapper.
130
+ * @param tag - the HTML tag name.
131
+ * @param nodes - rendered children.
132
+ * @param textSuffix - plain-text separator this block adds after its inline text.
133
+ * @returns the wrapper (when it has text) and the media nodes.
134
+ */
135
+ export declare function inlineWrapper(tag: string, nodes: readonly MatrixNode[], textSuffix?: string): MatrixNode[];
136
+ /**
137
+ * Turn one media element into its node.
138
+ *
139
+ * Matrix clients render inline `mxc:` images inconsistently and a media message is the standard presentation, so every
140
+ * media element becomes its own event. Media standing in phrasing content is reported as `moved`: nothing is lost,
141
+ * only repositioned.
142
+ *
143
+ * @param el - one of `img`/`audio`/`video`/`file`.
144
+ * @param ctx - render context of the mode.
145
+ * @returns the media node, to be published after the text run it was found in.
146
+ */
147
+ export declare function mediaNode(el: Element<MatrixMediaTag>, ctx: RenderContext<MatrixModeTypes>): MatrixNode;
148
+ /** The Matrix `msgtype` of one media tag. */
149
+ export declare function msgtypeOf(tag: MatrixMediaTag): 'm.image' | 'm.file' | 'm.audio' | 'm.video';
150
+ /**
151
+ * Fold a node stream into platform events: a text run around each media node.
152
+ * @param nodes - rendered nodes, in source order.
153
+ * @returns the drafts to publish, at least one for any content a handler produced.
154
+ */
155
+ export declare function assembleDrafts(nodes: readonly MatrixNode[]): MatrixDraft[];
156
+ /**
157
+ * Build the request payload of one draft.
158
+ *
159
+ * @param prepared - one assembled draft with its resolved media.
160
+ * @param ctx - serialize context (target, part index, quote).
161
+ * @returns the exact request data `sendPayload` posts.
162
+ */
163
+ export declare function serializeDraft(prepared: Prepared<MatrixDraft>, ctx: SerializeContext): MatrixPayload;
164
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.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3B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,UAAU,EAAE,EAC5B,UAAU,SAAK,GACd,UAAU,EAAE,CAcd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CACvB,EAAE,EAAE,OAAO,CAAC,cAAc,CAAC,EAC3B,GAAG,EAAE,aAAa,CAAC,eAAe,CAAC,GAClC,UAAU,CAiEZ;AAED,6CAA6C;AAC7C,wBAAgB,SAAS,CACvB,GAAG,EAAE,cAAc,GAClB,SAAS,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,CAW9C;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,UAAU,EAAE,GAAG,WAAW,EAAE,CAwC1E;AAwED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,QAAQ,CAAC,WAAW,CAAC,EAC/B,GAAG,EAAE,gBAAgB,GACpB,aAAa,CAsCf"}
@@ -0,0 +1,312 @@
1
+ /**
2
+ * The single `html` mode's node, draft and payload types, HTML escaping, media promotion, reply/thread relations and
3
+ * payload assembly.
4
+ *
5
+ * A Matrix message is either one `m.text` event with `body` + `formatted_body`, or one media event per media element.
6
+ * Every handler therefore returns an HTML fragment and its plain-text projection from the same traversal, so the two
7
+ * sides cannot drift; `assemble` flushes a text draft around each media element.
8
+ */
9
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
10
+ import { isPhrasingContainer } from '@deepseek-ai/satori-element';
11
+ import { addMention, createMentionSet, mentionWire } from "../mentions.js";
12
+ /** Escape character data of `formatted_body`. */
13
+ export function escapeHtml(text) {
14
+ return text
15
+ .replaceAll('&', '&amp;')
16
+ .replaceAll('<', '&lt;')
17
+ .replaceAll('>', '&gt;');
18
+ }
19
+ /** Escape one HTML attribute value (double-quoted). */
20
+ export function escapeAttribute(text) {
21
+ return escapeHtml(text).replaceAll('"', '&quot;');
22
+ }
23
+ /** One inline node. */
24
+ export function htmlNode(html, text, mentions) {
25
+ return {
26
+ type: 'html',
27
+ html,
28
+ text,
29
+ ...(mentions === undefined || mentions.length === 0 ? {} : { mentions }),
30
+ };
31
+ }
32
+ /** One mention node per collected user and one for the room mention. */
33
+ export function mentionNodes(set) {
34
+ return [
35
+ ...set.users.map((user) => ({ user })),
36
+ ...(set.room ? [{ room: true }] : []),
37
+ ];
38
+ }
39
+ /**
40
+ * Split rendered children into one inline run and the media nodes that follow it.
41
+ * @param nodes - rendered children.
42
+ * @returns the concatenated HTML/text, the collected mentions and the media nodes, in source order.
43
+ */
44
+ export function inlineRun(nodes) {
45
+ let html = '';
46
+ let text = '';
47
+ const mentions = createMentionSet();
48
+ const rest = [];
49
+ for (const node of nodes) {
50
+ if (node.type === 'media') {
51
+ rest.push(node);
52
+ continue;
53
+ }
54
+ html += node.html;
55
+ text += node.text;
56
+ for (const mention of node.mentions ?? [])
57
+ addMention(mentions, mention);
58
+ }
59
+ return { html, text, mentions, rest };
60
+ }
61
+ /**
62
+ * Wrap the inline text of rendered children in one HTML tag; media of the subtree follows the wrapper.
63
+ * @param tag - the HTML tag name.
64
+ * @param nodes - rendered children.
65
+ * @param textSuffix - plain-text separator this block adds after its inline text.
66
+ * @returns the wrapper (when it has text) and the media nodes.
67
+ */
68
+ export function inlineWrapper(tag, nodes, textSuffix = '') {
69
+ const { html, text, mentions, rest } = inlineRun(nodes);
70
+ const out = [];
71
+ if (html !== '') {
72
+ out.push(htmlNode(`<${tag}>${html}</${tag}>`, text + textSuffix, mentionNodes(mentions)));
73
+ }
74
+ out.push(...rest);
75
+ return out;
76
+ }
77
+ /**
78
+ * Turn one media element into its node.
79
+ *
80
+ * Matrix clients render inline `mxc:` images inconsistently and a media message is the standard presentation, so every
81
+ * media element becomes its own event. Media standing in phrasing content is reported as `moved`: nothing is lost,
82
+ * only repositioned.
83
+ *
84
+ * @param el - one of `img`/`audio`/`video`/`file`.
85
+ * @param ctx - render context of the mode.
86
+ * @returns the media node, to be published after the text run it was found in.
87
+ */
88
+ export function mediaNode(el, ctx) {
89
+ const parent = ctx.ancestors[ctx.ancestors.length - 1];
90
+ if (parent !== undefined && isPhrasingContainer(parent.type)) {
91
+ ctx.report.moved.push({
92
+ tag: el.type,
93
+ reason: 'matrix-media-split',
94
+ path: ctx.stableId(el),
95
+ });
96
+ }
97
+ switch (el.type) {
98
+ case 'img':
99
+ return {
100
+ type: 'media',
101
+ tag: 'img',
102
+ ref: ctx.resource('image', el.attrs.src, {
103
+ ...(el.attrs.alt === undefined ? {} : { name: el.attrs.alt }),
104
+ ...(el.attrs.width === undefined ? {} : { width: el.attrs.width }),
105
+ ...(el.attrs.height === undefined ? {} : { height: el.attrs.height }),
106
+ }),
107
+ ...(el.attrs.alt === undefined ? {} : { name: el.attrs.alt }),
108
+ ...(el.attrs.width === undefined ? {} : { width: el.attrs.width }),
109
+ ...(el.attrs.height === undefined ? {} : { height: el.attrs.height }),
110
+ };
111
+ case 'audio':
112
+ return {
113
+ type: 'media',
114
+ tag: 'audio',
115
+ ref: ctx.resource('audio', el.attrs.src, {
116
+ ...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
117
+ ...(el.attrs.duration === undefined
118
+ ? {}
119
+ : { duration: el.attrs.duration }),
120
+ }),
121
+ ...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
122
+ ...(el.attrs.duration === undefined
123
+ ? {}
124
+ : { duration: el.attrs.duration }),
125
+ };
126
+ case 'video':
127
+ return {
128
+ type: 'media',
129
+ tag: 'video',
130
+ ref: ctx.resource('video', el.attrs.src, {
131
+ ...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
132
+ ...(el.attrs.duration === undefined
133
+ ? {}
134
+ : { duration: el.attrs.duration }),
135
+ }),
136
+ ...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
137
+ ...(el.attrs.duration === undefined
138
+ ? {}
139
+ : { duration: el.attrs.duration }),
140
+ };
141
+ case 'file':
142
+ return {
143
+ type: 'media',
144
+ tag: 'file',
145
+ ref: ctx.resource('file', el.attrs.src, el.attrs.name === undefined ? {} : { name: el.attrs.name }),
146
+ ...(el.attrs.name === undefined ? {} : { name: el.attrs.name }),
147
+ };
148
+ }
149
+ }
150
+ /** The Matrix `msgtype` of one media tag. */
151
+ export function msgtypeOf(tag) {
152
+ switch (tag) {
153
+ case 'img':
154
+ return 'm.image';
155
+ case 'audio':
156
+ return 'm.audio';
157
+ case 'video':
158
+ return 'm.video';
159
+ case 'file':
160
+ return 'm.file';
161
+ }
162
+ }
163
+ /**
164
+ * Fold a node stream into platform events: a text run around each media node.
165
+ * @param nodes - rendered nodes, in source order.
166
+ * @returns the drafts to publish, at least one for any content a handler produced.
167
+ */
168
+ export function assembleDrafts(nodes) {
169
+ const drafts = [];
170
+ let html = '';
171
+ let text = '';
172
+ let mentions = createMentionSet();
173
+ const flush = () => {
174
+ if (html.trim() === '' && text.trim() === '') {
175
+ html = '';
176
+ text = '';
177
+ mentions = createMentionSet();
178
+ return;
179
+ }
180
+ drafts.push({ kind: 'text', html, text, mentions: mentionWire(mentions) });
181
+ html = '';
182
+ text = '';
183
+ mentions = createMentionSet();
184
+ };
185
+ for (const node of nodes) {
186
+ if (node.type === 'media') {
187
+ flush();
188
+ drafts.push({
189
+ kind: 'media',
190
+ tag: node.tag,
191
+ ref: node.ref,
192
+ ...(node.name === undefined ? {} : { name: node.name }),
193
+ ...(node.width === undefined ? {} : { width: node.width }),
194
+ ...(node.height === undefined ? {} : { height: node.height }),
195
+ ...(node.duration === undefined ? {} : { duration: node.duration }),
196
+ mentions: {},
197
+ });
198
+ continue;
199
+ }
200
+ html += node.html;
201
+ text += node.text;
202
+ for (const mention of node.mentions ?? [])
203
+ addMention(mentions, mention);
204
+ }
205
+ flush();
206
+ return drafts;
207
+ }
208
+ /** The adapter-private reply context of a publication, when the target carries one. */
209
+ function referrerOf(target) {
210
+ const referrer = target.referrer;
211
+ if (typeof referrer !== 'object' || referrer === null)
212
+ return undefined;
213
+ return referrer;
214
+ }
215
+ /** The thread root a target names: an explicit id, else the referrer's root for `thread: true`. */
216
+ function threadRootOf(target) {
217
+ if (typeof target.thread === 'string')
218
+ return target.thread;
219
+ if (target.thread !== true)
220
+ return undefined;
221
+ return referrerOf(target)?.threadRoot;
222
+ }
223
+ /**
224
+ * Build the `m.relates_to` object of one event.
225
+ *
226
+ * A reply names only the quoted event id: the fallback body needs the quoted text, which this adapter does not fetch.
227
+ * A thread carries its root, and — because the specification requires an unthreaded-client fallback — an
228
+ * `m.in_reply_to` naming the latest known event (`is_falling_back: true`).
229
+ */
230
+ function relationOf(ctx) {
231
+ const referrer = referrerOf(ctx.target);
232
+ const threadRoot = threadRootOf(ctx.target);
233
+ const quote = ctx.index === 0 ? ctx.quote : undefined;
234
+ if (threadRoot === undefined) {
235
+ return quote === undefined
236
+ ? undefined
237
+ : { 'm.in_reply_to': { event_id: quote } };
238
+ }
239
+ const reply = quote ?? referrer?.eventId;
240
+ return {
241
+ rel_type: 'm.thread',
242
+ event_id: threadRoot,
243
+ ...(reply === undefined
244
+ ? {}
245
+ : { is_falling_back: true, 'm.in_reply_to': { event_id: reply } }),
246
+ };
247
+ }
248
+ /** One media key resolved by `prepare`. */
249
+ function resolvedKey(prepared, ref) {
250
+ const resolved = prepared.resolved.get(ref);
251
+ if (resolved === undefined) {
252
+ throw new PlatformError('invalid', 'matrix: media was not prepared before serialize', { published: 'no' });
253
+ }
254
+ return resolved.key;
255
+ }
256
+ /** The extra fields `upload` recorded for one prepared media key. */
257
+ function extraOf(prepared, ref) {
258
+ const resolved = prepared.resolved.get(ref);
259
+ if (resolved === undefined)
260
+ return {};
261
+ const extra = resolved.extra;
262
+ if (typeof extra !== 'object' || extra === null)
263
+ return {};
264
+ return extra;
265
+ }
266
+ /**
267
+ * Build the request payload of one draft.
268
+ *
269
+ * @param prepared - one assembled draft with its resolved media.
270
+ * @param ctx - serialize context (target, part index, quote).
271
+ * @returns the exact request data `sendPayload` posts.
272
+ */
273
+ export function serializeDraft(prepared, ctx) {
274
+ const draft = prepared.draft;
275
+ const relates = relationOf(ctx);
276
+ const thread = threadRootOf(ctx.target);
277
+ const common = {
278
+ roomId: ctx.target.chat.id,
279
+ mentions: draft.mentions,
280
+ ...(relates === undefined ? {} : { relates }),
281
+ ...(thread === undefined ? {} : { thread: { root: thread } }),
282
+ };
283
+ if (draft.kind === 'text') {
284
+ const body = draft.text.trim();
285
+ const html = draft.html.trim();
286
+ return {
287
+ kind: 'text',
288
+ ...common,
289
+ // Matrix requires a plain `body`; the media path is the only draft that may carry no text.
290
+ body: body === '' ? ' ' : body,
291
+ ...(html === '' ? {} : { html }),
292
+ };
293
+ }
294
+ const extra = extraOf(prepared, draft.ref);
295
+ const body = draft.name ?? extra.name ?? 'file';
296
+ const info = {
297
+ ...(draft.width === undefined ? {} : { w: draft.width }),
298
+ ...(draft.height === undefined ? {} : { h: draft.height }),
299
+ ...(draft.duration === undefined ? {} : { duration: draft.duration }),
300
+ ...(extra.mime === undefined ? {} : { mimetype: extra.mime }),
301
+ ...(extra.size === undefined ? {} : { size: extra.size }),
302
+ };
303
+ return {
304
+ kind: 'media',
305
+ ...common,
306
+ msgtype: msgtypeOf(draft.tag),
307
+ url: resolvedKey(prepared, draft.ref),
308
+ body,
309
+ ...(Object.keys(info).length === 0 ? {} : { info }),
310
+ };
311
+ }
312
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/modes/shared.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAQlE,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAGjE,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AA2G1E,iDAAiD;AACjD,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,IAAI;SACR,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC;SACxB,UAAU,CAAC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AI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;KACpD,CAAA;AACH,CAAC"}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Matrix Client-Server transport: one HTTP path for every endpoint the adapter calls, the standard error envelope
3
+ * (`errcode` / `error`), rate-limit fields, upload bytes and authenticated media download.
4
+ *
5
+ * `Internal` (the migrated upstream client) keeps its upstream method bodies and calls this class through
6
+ * `bot.http`; the adapter's own send/media paths call it directly, so both cannot diverge on base URL or auth.
7
+ */
8
+ import type { Context } from '@deepseek-ai/cordis';
9
+ /** Optional inputs of one Matrix request. */
10
+ export interface MatrixRequestOptions {
11
+ /** Query parameters appended to the path. */
12
+ params?: Record<string, string | number | boolean>;
13
+ /** Extra headers (an upload's `content-type`). */
14
+ headers?: Record<string, string>;
15
+ signal?: AbortSignal;
16
+ }
17
+ /**
18
+ * The Client-Server client of one bot.
19
+ *
20
+ * Requests are relative to the homeserver root with `/_matrix` already appended. The access token is set after
21
+ * `register`/`login`; `register`/`login` themselves go through `ctx.http` with the application service token.
22
+ */
23
+ export declare class MatrixClient {
24
+ readonly ctx: Context;
25
+ readonly endpoint: string;
26
+ /** The user access token every Client-Server call carries; empty until `connect` logged in. */
27
+ token: string | undefined;
28
+ /** @param ctx - context whose `http` service performs requests. @param endpoint - homeserver root plus `/_matrix`. */
29
+ constructor(ctx: Context, endpoint: string);
30
+ /** Set the user access token minted by `m.login.application_service`. */
31
+ setToken(token: string): void;
32
+ /** The absolute URL of one endpoint path. */
33
+ url(path: string): string;
34
+ /**
35
+ * Call one GET endpoint.
36
+ * @param path - endpoint below the Matrix root.
37
+ * @param options - query parameters, headers and cancellation.
38
+ * @returns the parsed JSON answer.
39
+ */
40
+ get<T = any>(path: string, options?: MatrixRequestOptions): Promise<T>;
41
+ /**
42
+ * Call one PUT endpoint.
43
+ * @param path - endpoint below the Matrix root.
44
+ * @param data - JSON body (`t.rn` events, profile writes).
45
+ * @param options - query parameters, headers and cancellation.
46
+ * @returns the parsed JSON answer.
47
+ */
48
+ put<T = any>(path: string, data?: unknown, options?: MatrixRequestOptions): Promise<T>;
49
+ /**
50
+ * Call one POST endpoint.
51
+ * @param path - endpoint below the Matrix root.
52
+ * @param data - JSON body or raw bytes (upload).
53
+ * @param options - query parameters, headers and cancellation.
54
+ * @returns the parsed JSON answer.
55
+ */
56
+ post<T = any>(path: string, data?: unknown, options?: MatrixRequestOptions): Promise<T>;
57
+ /**
58
+ * Upload bytes to the media repository.
59
+ * @param name - file name sent as the `filename` query parameter.
60
+ * @param data - the bytes.
61
+ * @param mimetype - `content-type` of the bytes, when known.
62
+ * @param signal - cancellation.
63
+ * @returns the upload answer (`content_uri` on success).
64
+ */
65
+ upload(name: string, data: Uint8Array, mimetype: string | undefined, signal?: AbortSignal): Promise<{
66
+ content_uri?: string;
67
+ }>;
68
+ /**
69
+ * Download one media item through the authenticated endpoint (v1.11's recommended path).
70
+ * @param serverName - the server name of the `mxc://` URI.
71
+ * @param mediaId - the media id of the `mxc://` URI.
72
+ * @param fileName - optional file name path segment.
73
+ * @param signal - cancellation.
74
+ * @returns the platform's answer, body included.
75
+ */
76
+ download(serverName: string, mediaId: string, fileName?: string, signal?: AbortSignal): Promise<Response>;
77
+ /** Perform one request and return the raw answer; transport failures become `MatrixApiError`s. */
78
+ private send;
79
+ /** Decode one answer: JSON object on success, `MatrixApiError` for every refusal. */
80
+ private decode;
81
+ }
82
+ //# sourceMappingURL=request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAIlD,6CAA6C;AAC7C,MAAM,WAAW,oBAAoB;IACnC,6CAA6C;IAC7C,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,G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