@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,59 @@
1
+ /**
2
+ * The Application Service configuration: the registration credentials, the virtual user, the inbound route and the
3
+ * media request defaults. There is no transport switch — the homeserver's transaction push is the only intake.
4
+ *
5
+ * `endpoint` defaults to `https://<host>`; trailing slashes are removed so the client can append `/_matrix` once.
6
+ */
7
+ import z from '@deepseek-ai/schemastery';
8
+ /** One Matrix Application Service bot. */
9
+ export interface Config {
10
+ /** Homeserver root (`https://<host>` unless the homeserver lives elsewhere); `/_matrix` is appended. */
11
+ endpoint: string;
12
+ /** Homeserver server name; the virtual user is `@<id>:<host>`. */
13
+ host: string;
14
+ /** Localpart of the AS virtual user; it must fall inside the registration's users namespace. */
15
+ id: string;
16
+ /** The registration's `as_token` (`role('secret')`). */
17
+ asToken: string;
18
+ /** The registration's `hs_token`, used to verify inbound transactions (`role('secret')`). */
19
+ hsToken: string;
20
+ /** Prefix of the inbound Application Service route; the registration's `url` points at it. */
21
+ path: string;
22
+ /** Display name written to the AS user when set. */
23
+ name?: string | null;
24
+ /** Avatar URL downloaded and written to the AS user when set. */
25
+ avatar?: string | null;
26
+ /** Join a room the bot is invited to (an AS virtual user has no second login entry). */
27
+ autoJoin: boolean;
28
+ }
29
+ /** Validated Matrix adapter configuration; `endpoint` is always resolved. */
30
+ export declare const Config: z<{
31
+ endpoint?: string | null;
32
+ host?: string | null;
33
+ id?: string | null;
34
+ asToken?: string | null;
35
+ hsToken?: string | null;
36
+ path?: string | null;
37
+ name?: string | null;
38
+ avatar?: string | null;
39
+ autoJoin?: boolean | null;
40
+ } & import("@deepseek-ai/cordis-plugin-im-core").Dict, Config, "plain">;
41
+ /** Validated Matrix adapter configuration, also exported as `MatrixConfig`. */
42
+ export type MatrixConfig = Config;
43
+ /**
44
+ * The endpoint the adapter refuses to start with.
45
+ *
46
+ * The schema marks `endpoint` as a URL for profile renderers, but only this check makes a malformed root fail before
47
+ * the first request instead of on every call.
48
+ *
49
+ * @param config - the validated configuration.
50
+ * @returns the configuration error, or undefined when the endpoint is usable.
51
+ */
52
+ export declare function configError(config: Config): Error | undefined;
53
+ /**
54
+ * Normalize an inbound route path: absolute, no trailing slash.
55
+ * @param path - the configured `path`.
56
+ * @returns the pathname to register.
57
+ */
58
+ export declare function routePath(path: string): string;
59
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAUxC,0CAA0C;AAC1C,MAAM,WAAW,MAAM;IACrB,wGAAwG;IACxG,QAAQ,EAAE,MAAM,CAAA;IAChB,kEAAkE;IAClE,IAAI,EAAE,MAAM,CAAA;IACZ,gGAAgG;IAChG,EAAE,EAAE,MAAM,CAAA;IACV,wDAAwD;IACxD,OAAO,EAAE,MAAM,CAAA;IACf,6FAA6F;IAC7F,OAAO,EAAE,MAAM,CAAA;IACf,8FAA8F;IAC9F,IAAI,EAAE,MAAM,CAAA;IACZ,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,wFAAwF;IACxF,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED,6EAA6E;AAC7E,eAAO,MAAM,MAAM;;;;;;;;;;uEAwBlB,CAAA;AAED,+EAA+E;AAC/E,MAAM,MAAM,YAAY,GAAG,MAAM,CAAA;AAEjC;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAW7D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG9C"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The Application Service configuration: the registration credentials, the virtual user, the inbound route and the
3
+ * media request defaults. There is no transport switch — the homeserver's transaction push is the only intake.
4
+ *
5
+ * `endpoint` defaults to `https://<host>`; trailing slashes are removed so the client can append `/_matrix` once.
6
+ */
7
+ import z from '@deepseek-ai/schemastery';
8
+ /** A schema-required field, narrowed from the transform's nullable view of an object schema. */
9
+ function required(value, field) {
10
+ if (value === undefined || value === null || value === '') {
11
+ throw new Error(`matrix: ${field} is required`);
12
+ }
13
+ return value;
14
+ }
15
+ /** Validated Matrix adapter configuration; `endpoint` is always resolved. */
16
+ export const Config = z.transform(z.object({
17
+ endpoint: z.string().role('url'),
18
+ host: z.string().required(),
19
+ id: z.string().required(),
20
+ asToken: z.string().role('secret').required(),
21
+ hsToken: z.string().role('secret').required(),
22
+ path: z.string().default('/matrix'),
23
+ name: z.string(),
24
+ avatar: z.string(),
25
+ autoJoin: z.boolean().default(true),
26
+ }), (value) => ({
27
+ ...value,
28
+ endpoint: (value.endpoint ?? `https://${required(value.host, 'host')}`).replace(/\/+$/, ''),
29
+ host: required(value.host, 'host'),
30
+ id: required(value.id, 'id'),
31
+ asToken: required(value.asToken, 'asToken'),
32
+ hsToken: required(value.hsToken, 'hsToken'),
33
+ path: value.path ?? '/matrix',
34
+ autoJoin: value.autoJoin ?? true,
35
+ }));
36
+ /**
37
+ * The endpoint the adapter refuses to start with.
38
+ *
39
+ * The schema marks `endpoint` as a URL for profile renderers, but only this check makes a malformed root fail before
40
+ * the first request instead of on every call.
41
+ *
42
+ * @param config - the validated configuration.
43
+ * @returns the configuration error, or undefined when the endpoint is usable.
44
+ */
45
+ export function configError(config) {
46
+ let url;
47
+ try {
48
+ url = new URL(config.endpoint);
49
+ }
50
+ catch {
51
+ return new Error('matrix: endpoint must be an absolute http(s) URL');
52
+ }
53
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
54
+ return new Error('matrix: endpoint must be an absolute http(s) URL');
55
+ }
56
+ return undefined;
57
+ }
58
+ /**
59
+ * Normalize an inbound route path: absolute, no trailing slash.
60
+ * @param path - the configured `path`.
61
+ * @returns the pathname to register.
62
+ */
63
+ export function routePath(path) {
64
+ const withSlash = path.startsWith('/') ? path : `/${path}`;
65
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash;
66
+ }
67
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version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,CAAA;AACzE,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Inbound decoding: one Application Service event becomes one `Session`.
3
+ *
4
+ * `m.room.message` becomes a message (text/emote/notice through `formatted_body` or the plain body, media through the
5
+ * authenticated `satori:` route); `m.reaction` becomes `reaction-added`; `m.room.redaction` becomes
6
+ * `message-deleted` (room v11 moved the target into `content.redacts`). Membership and room-name events only update
7
+ * the room cache, and an invitation of the bot itself is joined when `autoJoin` is on. Everything else is dropped;
8
+ * `m.room.encrypted` is the visible consequence of the no-E2EE decision.
9
+ */
10
+ import type { ChatRef, InboundMessage, Session, SessionEvent } from '@deepseek-ai/cordis-plugin-im-core';
11
+ import type { RoomCache } from './rooms.ts';
12
+ import type { ClientEvent } from './types.ts';
13
+ /** Adapter-private context carried through `Target.referrer`. */
14
+ export interface MatrixReferrer {
15
+ roomId: string;
16
+ eventId: string;
17
+ /** The sender of the event this referrer belongs to. */
18
+ sender: string;
19
+ /** The thread root this event was posted in, when it was threaded. */
20
+ threadRoot?: string;
21
+ }
22
+ /** The part of the adapter inbound decoding uses. */
23
+ export interface MatrixDecoderHost {
24
+ readonly selfId: string;
25
+ readonly rooms: RoomCache;
26
+ readonly autoJoin: boolean;
27
+ getInternalUrl(path: string, init?: Record<string, string>): string;
28
+ session(event: Partial<SessionEvent>): Session;
29
+ /** Debug line for an event the adapter drops. */
30
+ log(message: string): void;
31
+ /** Warning line for an unreadable room. */
32
+ warn(message: string): void;
33
+ /** Join a room the bot was invited to (no room-state refresh; the self-join event triggers one). */
34
+ joinRoom(roomId: string): Promise<void>;
35
+ /** Re-run the one-shot full-state sync and apply it. */
36
+ refreshRooms(): Promise<void>;
37
+ }
38
+ /** The message types this adapter decodes; every other `msgtype` is dropped whole. */
39
+ export declare const MESSAGE_TYPES: ReadonlySet<string>;
40
+ /**
41
+ * Decode one `m.room.message` event.
42
+ *
43
+ * @param host - the receiving adapter.
44
+ * @param event - one message event.
45
+ * @param chat - the chat to report, when the caller already knows it (`getMessage` uses the requested one).
46
+ * @returns the decoded message, whose content is never empty.
47
+ */
48
+ export declare function decodeMessage(host: MatrixDecoderHost, event: ClientEvent, chat?: ChatRef): InboundMessage;
49
+ /** Decode one received event.
50
+ *
51
+ * @param host - the receiving adapter.
52
+ * @param event - one event of an Application Service transaction.
53
+ * @returns the session to publish, or undefined for an event this adapter does not carry.
54
+ */
55
+ export declare function decodeEvent(host: MatrixDecoderHost, event: ClientEvent): Promise<Session | undefined>;
56
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EACV,OAAO,EAEP,cAAc,EACd,OAAO,EACP,YAAY,EAEb,MAAM,oCAAoC,CAAA;AAI3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,KAAK,EACV,WAAW,EAQZ,MAAM,YAAY,CAAA;AAEnB,iEAAiE;AACjE,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAA;IACd,sEAAsE;IACtE,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,qDAAqD;AACrD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;IAC1B,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAA;IACnE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,OAAO,CAAA;IAC9C,iDAAiD;IACjD,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,2CAA2C;IAC3C,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IAC3B,oGAAoG;IACpG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACvC,wDAAwD;IACxD,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CAC9B;AAKD,sFAAsF;AACtF,eAAO,MAAM,aAAa,EAAE,WAAW,CAAC,MAAM,CAQ5C,CAAA;AA4GF;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,iBAAiB,EACvB,KAAK,EAAE,WAAW,EAClB,IAAI,CAAC,EAAE,OAAO,GACb,cAAc,CAkDhB;AAED;;;;;GAKG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,iBAAiB,EACvB,KAAK,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAyI9B"}
@@ -0,0 +1,298 @@
1
+ /**
2
+ * Inbound decoding: one Application Service event becomes one `Session`.
3
+ *
4
+ * `m.room.message` becomes a message (text/emote/notice through `formatted_body` or the plain body, media through the
5
+ * authenticated `satori:` route); `m.reaction` becomes `reaction-added`; `m.room.redaction` becomes
6
+ * `message-deleted` (room v11 moved the target into `content.redacts`). Membership and room-name events only update
7
+ * the room cache, and an invitation of the bot itself is joined when `autoJoin` is on. Everything else is dropped;
8
+ * `m.room.encrypted` is the visible consequence of the no-E2EE decision.
9
+ */
10
+ import { h } from '@deepseek-ai/cordis-plugin-im-core';
11
+ import { htmlToElements, stripPlainFallback } from "./html.js";
12
+ import { internalMediaSource } from "./media.js";
13
+ import { decodeMentions } from "./mentions.js";
14
+ /** Rooms whose first `m.room.encrypted` event has already been logged. */
15
+ const encryptedLogged = new WeakMap();
16
+ /** The message types this adapter decodes; every other `msgtype` is dropped whole. */
17
+ export const MESSAGE_TYPES = new Set([
18
+ 'm.text',
19
+ 'm.emote',
20
+ 'm.notice',
21
+ 'm.image',
22
+ 'm.file',
23
+ 'm.audio',
24
+ 'm.video',
25
+ ]);
26
+ /** The chat of one room, from the cache. */
27
+ function chatOf(host, roomId) {
28
+ const name = host.rooms.nameOf(roomId);
29
+ return {
30
+ id: roomId,
31
+ type: host.rooms.isDirect(roomId, host.selfId) ? 'direct' : 'group',
32
+ ...(name === undefined ? {} : { name }),
33
+ };
34
+ }
35
+ /** The `satori:` source of one `mxc://` URI, or the raw address when it is not one. */
36
+ function mediaSource(host, uri) {
37
+ if (uri === undefined || uri === '')
38
+ return undefined;
39
+ return internalMediaSource(host, uri) ?? uri;
40
+ }
41
+ /** The sender of one event, with the display name the room cache knows. */
42
+ function senderOf(host, event) {
43
+ const name = host.rooms.displayNameOf(event.room_id, event.sender);
44
+ return {
45
+ id: event.sender,
46
+ ...(name === undefined ? {} : { name }),
47
+ isBot: false,
48
+ };
49
+ }
50
+ /** The elements of one `m.room.message` content, or undefined for a msgtype this adapter drops. */
51
+ function messageElements(host, event) {
52
+ const content = event.content;
53
+ switch (content.msgtype) {
54
+ case 'm.text':
55
+ case 'm.emote':
56
+ case 'm.notice': {
57
+ const formatted = content;
58
+ if (formatted.format === 'org.matrix.custom.html' &&
59
+ typeof formatted.formatted_body === 'string' &&
60
+ formatted.formatted_body !== '') {
61
+ return htmlToElements(formatted.formatted_body, {
62
+ mediaSource: (mxc) => mediaSource(host, mxc) ?? mxc,
63
+ });
64
+ }
65
+ return [h.text(stripPlainFallback(content.body ?? ''))];
66
+ }
67
+ case 'm.image': {
68
+ const image = content;
69
+ const src = mediaSource(host, image.url);
70
+ if (src === undefined)
71
+ return undefined;
72
+ return [
73
+ h('img', {
74
+ src,
75
+ ...(image.body === '' ? {} : { alt: image.body }),
76
+ ...(image.info?.w === undefined ? {} : { width: image.info.w }),
77
+ ...(image.info?.h === undefined ? {} : { height: image.info.h }),
78
+ }),
79
+ ];
80
+ }
81
+ case 'm.file': {
82
+ const file = content;
83
+ const src = mediaSource(host, file.url);
84
+ if (src === undefined)
85
+ return undefined;
86
+ const name = file.filename ?? file.body;
87
+ return [h('file', { src, ...(name === '' ? {} : { name }) })];
88
+ }
89
+ case 'm.audio': {
90
+ const audio = content;
91
+ const src = mediaSource(host, audio.url);
92
+ if (src === undefined)
93
+ return undefined;
94
+ const name = audio.filename ?? audio.body;
95
+ return [
96
+ h('audio', {
97
+ src,
98
+ ...(name === '' ? {} : { name }),
99
+ ...(audio.info?.duration === undefined
100
+ ? {}
101
+ : { duration: audio.info.duration }),
102
+ }),
103
+ ];
104
+ }
105
+ case 'm.video': {
106
+ const video = content;
107
+ const src = mediaSource(host, video.url);
108
+ if (src === undefined)
109
+ return undefined;
110
+ const name = video.body;
111
+ return [
112
+ h('video', {
113
+ src,
114
+ ...(name === '' ? {} : { name }),
115
+ ...(video.info?.duration === undefined
116
+ ? {}
117
+ : { duration: video.info.duration }),
118
+ }),
119
+ ];
120
+ }
121
+ default:
122
+ return undefined;
123
+ }
124
+ }
125
+ /**
126
+ * Decode one `m.room.message` event.
127
+ *
128
+ * @param host - the receiving adapter.
129
+ * @param event - one message event.
130
+ * @param chat - the chat to report, when the caller already knows it (`getMessage` uses the requested one).
131
+ * @returns the decoded message, whose content is never empty.
132
+ */
133
+ export function decodeMessage(host, event, chat) {
134
+ const content = event.content;
135
+ const elements = messageElements(host, event);
136
+ if (elements === undefined) {
137
+ host.log(`matrix: ignored msgtype "${content.msgtype ?? 'unknown'}"`);
138
+ }
139
+ // A body of "" or a formatted body that only contained a fallback decodes to nothing usable.
140
+ const usable = (elements ?? []).filter((element) => element.type !== 'text' || element.attrs.content !== '');
141
+ const children = usable.length > 0
142
+ ? usable
143
+ : [
144
+ h('raw', { platform: 'matrix', type: 'm.room.message', data: event.content }, h.text('')),
145
+ ];
146
+ const message = h('message', {}, ...children);
147
+ const relates = content['m.relates_to'];
148
+ const threadRoot = relates?.rel_type === 'm.thread' ? relates.event_id : undefined;
149
+ const quoteId = relates?.['m.in_reply_to']?.event_id;
150
+ const referrer = {
151
+ roomId: event.room_id,
152
+ eventId: event.event_id,
153
+ sender: event.sender,
154
+ ...(threadRoot === undefined ? {} : { threadRoot }),
155
+ };
156
+ return {
157
+ id: event.event_id,
158
+ chat: chat ?? chatOf(host, event.room_id),
159
+ ...(threadRoot === undefined
160
+ ? {}
161
+ : { thread: { id: threadRoot, root: threadRoot } }),
162
+ sender: senderOf(host, event),
163
+ content: message,
164
+ ...(quoteId === undefined ? {} : { quote: { id: quoteId } }),
165
+ mentions: decodeMentions(content, content.body, message.children, host.selfId),
166
+ time: event.origin_server_ts,
167
+ referrer,
168
+ raw: event,
169
+ };
170
+ }
171
+ /** Decode one received event.
172
+ *
173
+ * @param host - the receiving adapter.
174
+ * @param event - one event of an Application Service transaction.
175
+ * @returns the session to publish, or undefined for an event this adapter does not carry.
176
+ */
177
+ export async function decodeEvent(host, event) {
178
+ // The homeserver echoes the bot's own events; the host also skips them, but not worth a `receive` round-trip.
179
+ if (event.sender === host.selfId)
180
+ return undefined;
181
+ const timestamp = event.origin_server_ts;
182
+ switch (event.type) {
183
+ case 'm.room.message': {
184
+ const content = event.content;
185
+ if (content['m.new_content'] !== undefined) {
186
+ host.log('matrix: ignored an edited message (m.new_content)');
187
+ return undefined;
188
+ }
189
+ if (!MESSAGE_TYPES.has(content.msgtype)) {
190
+ host.log(`matrix: ignored msgtype "${content.msgtype ?? 'unknown'}"`);
191
+ return undefined;
192
+ }
193
+ const message = decodeMessage(host, event);
194
+ return host.session({
195
+ type: 'message',
196
+ id: event.event_id,
197
+ timestamp,
198
+ message,
199
+ raw: event,
200
+ });
201
+ }
202
+ case 'm.reaction': {
203
+ const relation = event.content['m.relates_to'];
204
+ if (relation?.rel_type !== 'm.annotation') {
205
+ host.log('matrix: ignored a reaction without an m.annotation relation');
206
+ return undefined;
207
+ }
208
+ if (relation.event_id === undefined || relation.key === undefined) {
209
+ host.log('matrix: ignored a reaction without an event id or key');
210
+ return undefined;
211
+ }
212
+ return host.session({
213
+ type: 'reaction-added',
214
+ id: event.event_id,
215
+ timestamp,
216
+ reaction: {
217
+ message: { id: relation.event_id, chat: chatOf(host, event.room_id) },
218
+ emoji: relation.key,
219
+ user: { id: event.sender },
220
+ },
221
+ raw: event,
222
+ });
223
+ }
224
+ case 'm.room.redaction': {
225
+ // Room version 11 moved the target into `content.redacts`; older rooms keep it on the event.
226
+ const content = event.content;
227
+ const target = event.redacts ?? content.redacts;
228
+ if (target === undefined)
229
+ return undefined;
230
+ return host.session({
231
+ type: 'message-deleted',
232
+ id: event.event_id,
233
+ timestamp,
234
+ deleted: { id: target, chat: chatOf(host, event.room_id) },
235
+ raw: event,
236
+ });
237
+ }
238
+ case 'm.room.member': {
239
+ const member = event.content;
240
+ const stateKey = event.state_key;
241
+ if (stateKey === undefined)
242
+ return undefined;
243
+ host.rooms.applyMember(event.room_id, stateKey, member.membership, member.displayname);
244
+ if (stateKey !== host.selfId)
245
+ return undefined;
246
+ if (member.membership === 'invite') {
247
+ if (!host.autoJoin) {
248
+ host.log(`matrix: invited to ${event.room_id}; autoJoin is off, so it is not joined`);
249
+ return undefined;
250
+ }
251
+ try {
252
+ await host.joinRoom(event.room_id);
253
+ }
254
+ catch (error) {
255
+ host.warn(`matrix: could not join ${event.room_id}: ${error instanceof Error ? error.message : String(error)}`);
256
+ }
257
+ return undefined;
258
+ }
259
+ if (member.membership === 'join') {
260
+ // A new room's name/members/summary are not in the push; refresh the cache once.
261
+ try {
262
+ await host.refreshRooms();
263
+ }
264
+ catch (error) {
265
+ host.warn(`matrix: could not refresh rooms after joining ${event.room_id}: ${error instanceof Error ? error.message : String(error)}`);
266
+ }
267
+ }
268
+ return undefined;
269
+ }
270
+ case 'm.room.name': {
271
+ const content = event.content;
272
+ if (typeof content.name === 'string')
273
+ host.rooms.setName(event.room_id, content.name);
274
+ return undefined;
275
+ }
276
+ case 'm.room.encryption':
277
+ if (host.rooms.markEncrypted(event.room_id)) {
278
+ host.warn(`matrix: room ${event.room_id} enabled end-to-end encryption; this adapter cannot decrypt its messages`);
279
+ }
280
+ return undefined;
281
+ case 'm.room.encrypted': {
282
+ let logged = encryptedLogged.get(host.rooms);
283
+ if (logged === undefined) {
284
+ logged = new Set();
285
+ encryptedLogged.set(host.rooms, logged);
286
+ }
287
+ if (!logged.has(event.room_id)) {
288
+ logged.add(event.room_id);
289
+ host.log(`matrix: dropped an encrypted message in ${event.room_id} (E2EE is not supported)`);
290
+ }
291
+ return undefined;
292
+ }
293
+ default:
294
+ host.log(`matrix: ignored ${event.type} event`);
295
+ return undefined;
296
+ }
297
+ }
298
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,oCAAoC,CAAA;AAStD,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAwC9C,0EAA0E;AAC1E,MAAM,eAAe,GAAG,IAAI,OAAO,EAA0B,CAAA;AAE7D,sFAAsF;AACtF,MAAM,CAAC,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC;IACxD,QAAQ;IACR,SAAS;IACT,UAAU;IACV,SAAS;IACT,QAAQ;IACR,SAAS;IACT,SAAS;CACV,CAAC,CAAA;AAEF,4CAA4C;AAC5C,SAAS,MAAM,CAAC,IAAuB,EAAE,MAAc;IACrD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IACtC,OAAO;QACL,EAAE,EAAE,MAAM;QACV,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OA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AAG,KAAK,CAAC,OAAsB,CAAA;YAC5C,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ;gBAClC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;YACjD,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,KAAK,mBAAmB;YACtB,IAAI,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC5C,IAAI,CAAC,IAAI,CACP,gBAAgB,KAAK,CAAC,OAAO,0EAA0E,CACxG,CAAA;YACH,CAAC;YACD,OAAO,SAAS,CAAA;QAClB,KAAK,kBAAkB,CAAC,CAAC,CAAC;YACxB,IAAI,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAC5C,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACzB,MAAM,GAAG,IAAI,GAAG,EAAE,CAAA;gBAClB,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;YACzC,CAAC;YACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC/B,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;gBACzB,IAAI,CAAC,GAAG,CACN,2CAA2C,KAAK,CAAC,OAAO,0BAA0B,CACnF,CAAA;YACH,CAAC;YACD,OAAO,SAAS,CAAA;QAClB,CAAC;QACD;YACE,IAAI,CAAC,GAAG,CAAC,mBAAmB,KAAK,CAAC,IAAI,QAAQ,CAAC,CAAA;YAC/C,OAAO,SAAS,CAAA;IACpB,CAAC;AACH,CAAC"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Matrix failures and their mapping onto the core platform taxonomy.
3
+ *
4
+ * Matrix answers with one JSON envelope (`errcode`, `error`) and asks clients to decide from `errcode`, falling back
5
+ * to the HTTP status only for `M_UNKNOWN`. A refused write is atomic, so every 4xx means nothing was published;
6
+ * transport failures and 5xx answers leave the outcome unknown. The token-failure codes also mark the bot offline at
7
+ * the transport layer, because the AS-issued user token has no refresh path.
8
+ */
9
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
10
+ /** Fields one failed Matrix call carries for classification. */
11
+ export interface MatrixApiErrorInit {
12
+ /** HTTP method of the failed call, when the failure came from one. */
13
+ method?: string;
14
+ /** Endpoint path of the failed call. */
15
+ path?: string;
16
+ /** HTTP status; absent for a transport failure. */
17
+ status?: number;
18
+ /** The envelope's `errcode`. */
19
+ errcode?: string;
20
+ /** The envelope's human-readable `error`. */
21
+ error?: string;
22
+ /** Delay asked for by the answer (`retry_after_ms` or `Retry-After`). */
23
+ retryAfterMs?: number;
24
+ cause?: unknown;
25
+ }
26
+ /** One failed Matrix API call, with the fields `classify` reads. */
27
+ export declare class MatrixApiError extends Error {
28
+ readonly method: string | undefined;
29
+ readonly path: string | undefined;
30
+ readonly status: number | undefined;
31
+ readonly errcode: string | undefined;
32
+ readonly error: string | undefined;
33
+ readonly retryAfterMs: number | undefined;
34
+ constructor(message: string, init?: MatrixApiErrorInit);
35
+ }
36
+ /** Whether a failure means the user token is gone; the bot has to be re-logged in from scratch. */
37
+ export declare function isTokenFailure(error: unknown): boolean;
38
+ /**
39
+ * Map any failure onto the core platform taxonomy.
40
+ *
41
+ * @param error - the caught failure.
42
+ * @returns the classified platform error, with `retryAfterMs` when the platform named a delay.
43
+ */
44
+ export declare function classifyMatrixError(error: unknown): PlatformError;
45
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAGlE,gEAAgE;AAChE,MAAM,WAAW,kBAAkB;IACjC,sEAAsE;IACtE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,mDAAmD;IACnD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,gCAAgC;IAChC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,oEAAoE;AACpE,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IAClC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAA;gBAE7B,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,kBAAuB;CAU3D;AAED,mGAAmG;AACnG,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAUtD;AA+CD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAkCjE"}