@im-more/dsh-line 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 (97) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +12 -0
  3. package/lib/index.js +1701 -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 +169 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +47 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +77 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/decode.d.ts +60 -0
  14. package/lib/types/decode.d.ts.map +1 -0
  15. package/lib/types/decode.js +321 -0
  16. package/lib/types/decode.js.map +1 -0
  17. package/lib/types/errors.d.ts +47 -0
  18. package/lib/types/errors.d.ts.map +1 -0
  19. package/lib/types/errors.js +112 -0
  20. package/lib/types/errors.js.map +1 -0
  21. package/lib/types/identity.d.ts +17 -0
  22. package/lib/types/identity.d.ts.map +1 -0
  23. package/lib/types/identity.js +24 -0
  24. package/lib/types/identity.js.map +1 -0
  25. package/lib/types/index.d.ts +17 -0
  26. package/lib/types/index.d.ts.map +1 -0
  27. package/lib/types/index.js +16 -0
  28. package/lib/types/index.js.map +1 -0
  29. package/lib/types/media.d.ts +19 -0
  30. package/lib/types/media.d.ts.map +1 -0
  31. package/lib/types/media.js +55 -0
  32. package/lib/types/media.js.map +1 -0
  33. package/lib/types/modes/shared.d.ts +126 -0
  34. package/lib/types/modes/shared.d.ts.map +1 -0
  35. package/lib/types/modes/shared.js +316 -0
  36. package/lib/types/modes/shared.js.map +1 -0
  37. package/lib/types/modes/text.d.ts +13 -0
  38. package/lib/types/modes/text.d.ts.map +1 -0
  39. package/lib/types/modes/text.js +143 -0
  40. package/lib/types/modes/text.js.map +1 -0
  41. package/lib/types/request.d.ts +49 -0
  42. package/lib/types/request.d.ts.map +1 -0
  43. package/lib/types/request.js +162 -0
  44. package/lib/types/request.js.map +1 -0
  45. package/lib/types/send.d.ts +43 -0
  46. package/lib/types/send.d.ts.map +1 -0
  47. package/lib/types/send.js +74 -0
  48. package/lib/types/send.js.map +1 -0
  49. package/lib/types/signature.d.ts +19 -0
  50. package/lib/types/signature.d.ts.map +1 -0
  51. package/lib/types/signature.js +30 -0
  52. package/lib/types/signature.js.map +1 -0
  53. package/lib/types/transport/media.d.ts +62 -0
  54. package/lib/types/transport/media.d.ts.map +1 -0
  55. package/lib/types/transport/media.js +104 -0
  56. package/lib/types/transport/media.js.map +1 -0
  57. package/lib/types/transport/webhook.d.ts +62 -0
  58. package/lib/types/transport/webhook.d.ts.map +1 -0
  59. package/lib/types/transport/webhook.js +208 -0
  60. package/lib/types/transport/webhook.js.map +1 -0
  61. package/lib/types/types/definition.d.ts +1006 -0
  62. package/lib/types/types/definition.d.ts.map +1 -0
  63. package/lib/types/types/definition.js +2 -0
  64. package/lib/types/types/definition.js.map +1 -0
  65. package/lib/types/types/index.d.ts +542 -0
  66. package/lib/types/types/index.d.ts.map +1 -0
  67. package/lib/types/types/index.js +4 -0
  68. package/lib/types/types/index.js.map +1 -0
  69. package/lib/types/types/internal.d.ts +8 -0
  70. package/lib/types/types/internal.d.ts.map +1 -0
  71. package/lib/types/types/internal.js +234 -0
  72. package/lib/types/types/internal.js.map +1 -0
  73. package/lib/types/types/request.d.ts +341 -0
  74. package/lib/types/types/request.d.ts.map +1 -0
  75. package/lib/types/types/request.js +2 -0
  76. package/lib/types/types/request.js.map +1 -0
  77. package/package.json +61 -3
  78. package/src/bot.ts +229 -0
  79. package/src/config.ts +113 -0
  80. package/src/decode.ts +418 -0
  81. package/src/errors.ts +138 -0
  82. package/src/identity.ts +28 -0
  83. package/src/index.ts +60 -0
  84. package/src/media.ts +66 -0
  85. package/src/modes/shared.ts +408 -0
  86. package/src/modes/text.ts +177 -0
  87. package/src/request.ts +215 -0
  88. package/src/send.ts +104 -0
  89. package/src/signature.ts +32 -0
  90. package/src/transport/media.ts +117 -0
  91. package/src/transport/webhook.ts +266 -0
  92. package/src/types/.eslintrc.yml +2 -0
  93. package/src/types/definition.ts +1008 -0
  94. package/src/types/index.ts +593 -0
  95. package/src/types/internal.ts +233 -0
  96. package/src/types/request.ts +341 -0
  97. package/README.md +0 -3
@@ -0,0 +1,77 @@
1
+ import z from '@deepseek-ai/schemastery';
2
+ /** Validated LINE adapter configuration. */
3
+ export const Config = z.object({
4
+ token: z.string().role('secret').required(),
5
+ secret: z.string().role('secret').required(),
6
+ selfId: z.string().required(),
7
+ publicUrl: z.string().role('url').required(),
8
+ webhook: z
9
+ .object({
10
+ path: z.string().default('/line'),
11
+ })
12
+ .default({}),
13
+ media: z
14
+ .object({
15
+ path: z.string().default('/line-media'),
16
+ })
17
+ .default({}),
18
+ });
19
+ /**
20
+ * Normalize a route path: absolute, no trailing slash.
21
+ *
22
+ * @param path - a configured path.
23
+ * @returns the pathname to register on the web server.
24
+ */
25
+ export function routePath(path) {
26
+ const withSlash = path.startsWith('/') ? path : `/${path}`;
27
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash;
28
+ }
29
+ /**
30
+ * The public base URL without its trailing slashes.
31
+ *
32
+ * @param publicUrl - the configured `publicUrl`.
33
+ * @returns the base every public URL is prefixed with.
34
+ */
35
+ export function publicBase(publicUrl) {
36
+ return publicUrl.replace(/\/+$/, '');
37
+ }
38
+ /**
39
+ * The configuration the adapter cannot work from, as far as it can tell locally.
40
+ *
41
+ * LINE has only the webhook transport, so a profile without a public HTTPS base can neither receive an event nor let
42
+ * LINE fetch outbound media. The check runs when the bot starts, not on the first callback.
43
+ *
44
+ * @param config - the validated configuration.
45
+ * @returns the configuration error, or undefined when the configuration is usable.
46
+ */
47
+ export function configError(config) {
48
+ if (config.token === '') {
49
+ return new Error('line: token is required (the channel access token of the Messaging API channel)');
50
+ }
51
+ if (config.secret === '') {
52
+ return new Error('line: secret is required (the channel secret; it verifies every webhook)');
53
+ }
54
+ if (config.selfId === '') {
55
+ return new Error('line: selfId is required (the bot\'s own user id: "U" + 32 hex, read from the console Basic settings)');
56
+ }
57
+ if (config.publicUrl === '') {
58
+ return new Error('line: publicUrl is required (LINE only delivers webhooks, and it fetches outbound media from this base)');
59
+ }
60
+ let origin;
61
+ try {
62
+ origin = new URL(config.publicUrl);
63
+ }
64
+ catch {
65
+ return new Error(`line: publicUrl must be an absolute URL, got "${config.publicUrl}"`);
66
+ }
67
+ if (origin.protocol !== 'https:') {
68
+ return new Error(`line: publicUrl must be an https URL, got "${config.publicUrl}" (LINE refuses a plain-HTTP webhook and fetches media over https)`);
69
+ }
70
+ const webhook = routePath(config.webhook.path);
71
+ const media = routePath(config.media.path);
72
+ if (webhook === media) {
73
+ return new Error(`line: webhook.path and media.path must differ, both are "${webhook}"`);
74
+ }
75
+ return undefined;
76
+ }
77
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAsBxC,4CAA4C;AAC5C,MAAM,CAAC,MAAM,MAAM,GAAc,CAAC,CAAC,MAAM,CAAC;IACxC,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC3C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC5C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC7B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE;IAC5C,OAAO,EAAE,CAAC;SACP,MAAM,CAAC;QACN,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,OAA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H,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,KAAK,CACd,iDAAiD,MAAM,CAAC,SAAS,GAAG,CACrE,CAAA;IACH,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACjC,OAAO,IAAI,KAAK,CACd,8CAA8C,MAAM,CAAC,SAAS,oEAAoE,CACnI,CAAA;IACH,CAAC;IACD,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9C,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC1C,IAAI,OAAO,KAAK,KAAK,EAAE,CAAC;QACtB,OAAO,IAAI,KAAK,CACd,4DAA4D,OAAO,GAAG,CACvE,CAAA;IACH,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Inbound decoding: one LINE webhook event becomes an intake `Session` (`message`), a button interaction, or an
3
+ * unsent-message notification.
4
+ *
5
+ * LINE gives the content away exactly once — there is no API that reads a message back — so the webhook payload, its
6
+ * UTF-16 indices included, is the only source of text, mentions, quotes and media.
7
+ */
8
+ import type { ChatRef, Session } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import type { LineBot } from './bot.ts';
10
+ import type { EventSource, WebhookEvent } from './types/index.ts';
11
+ /** Adapter-private context carried through `Target.referrer`. */
12
+ export interface LineReferrer {
13
+ /** Platform id of the message this context belongs to; absent on a postback. */
14
+ messageId?: string;
15
+ /** Token that quotes this inbound message, when the platform offers one (text, sticker, image, video). */
16
+ quoteToken?: string;
17
+ /** One-use, one-minute reply token of the event; the adapter itself always pushes. */
18
+ replyToken?: string;
19
+ timestamp: number;
20
+ mode: 'active' | 'standby';
21
+ }
22
+ /** How a chat id names its platform session: the `chat.id` prefix. */
23
+ export type LineChatKind = 'user' | 'group' | 'room';
24
+ /** A parsed `chat.id`: the platform conversation kind and its id. */
25
+ export interface LineChatAddress {
26
+ kind: LineChatKind;
27
+ id: string;
28
+ }
29
+ /** The `chat.id` of a source: `user:`, `group:` or `room:` plus the platform id. */
30
+ export declare function chatIdOf(source: EventSource): string;
31
+ /** The `ChatRef` of a source; a room is a group in the DSH vocabulary. */
32
+ export declare function chatRefOf(source: EventSource): ChatRef;
33
+ /**
34
+ * Split a `chat.id` back into the platform conversation.
35
+ *
36
+ * @param chatId - a `chat.id` this adapter wrote, or one a profile configured by hand.
37
+ * @returns the conversation kind and its platform id.
38
+ * @throws PlatformError `invalid` when the id carries no known prefix.
39
+ */
40
+ export declare function parseChatId(chatId: string): LineChatAddress;
41
+ /**
42
+ * The `pushMessage` recipient of a `chat.id`.
43
+ *
44
+ * @param chatId - a `chat.id` this adapter wrote.
45
+ * @returns the `to` value of a push request.
46
+ * @throws PlatformError `invalid` when the id carries no known prefix.
47
+ */
48
+ export declare function chatTargetOf(chatId: string): string;
49
+ /**
50
+ * Decode one webhook event.
51
+ *
52
+ * Events the DSH contract has no consumer for (follows, joins, member changes, beacons, edits, deliveries …) are
53
+ * dropped with a debug line: the model has no `friend-*`/`guild-*` event to receive them.
54
+ *
55
+ * @param bot - the receiving adapter.
56
+ * @param event - one event of the webhook body.
57
+ * @returns the session to publish, or undefined for an ignored event.
58
+ */
59
+ export declare function decodeEvent(bot: LineBot, event: WebhookEvent): Session | undefined;
60
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAEV,OAAO,EAEP,OAAO,EAER,MAAM,oCAAoC,CAAA;AAG3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AACvC,OAAO,KAAK,EAGV,WAAW,EAIX,YAAY,EACb,MAAM,kBAAkB,CAAA;AAEzB,iEAAiE;AACjE,MAAM,WAAW,YAAY;IAC3B,gFAAgF;IAChF,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,0GAA0G;IAC1G,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,sFAAsF;IACtF,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,QAAQ,GAAG,SAAS,CAAA;CAC3B;AAED,sEAAsE;AACtE,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAA;AAEpD,qEAAqE;AACrE,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,YAAY,CAAA;IAClB,EAAE,EAAE,MAAM,CAAA;CACX;AAED,oFAAoF;AACpF,wBAAgB,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CASpD;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAGtD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,CAW3D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEnD;AA0QD;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,YAAY,GAClB,OAAO,GAAG,SAAS,CAyCrB"}
@@ -0,0 +1,321 @@
1
+ /**
2
+ * Inbound decoding: one LINE webhook event becomes an intake `Session` (`message`), a button interaction, or an
3
+ * unsent-message notification.
4
+ *
5
+ * LINE gives the content away exactly once — there is no API that reads a message back — so the webhook payload, its
6
+ * UTF-16 indices included, is the only source of text, mentions, quotes and media.
7
+ */
8
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import { h, normalize } from '@deepseek-ai/satori-element';
10
+ /** The `chat.id` of a source: `user:`, `group:` or `room:` plus the platform id. */
11
+ export function chatIdOf(source) {
12
+ switch (source.type) {
13
+ case 'user':
14
+ return `user:${source.userId}`;
15
+ case 'group':
16
+ return `group:${source.groupId}`;
17
+ case 'room':
18
+ return `room:${source.roomId}`;
19
+ }
20
+ }
21
+ /** The `ChatRef` of a source; a room is a group in the DSH vocabulary. */
22
+ export function chatRefOf(source) {
23
+ const id = chatIdOf(source);
24
+ return { id, type: source.type === 'user' ? 'direct' : 'group' };
25
+ }
26
+ /**
27
+ * Split a `chat.id` back into the platform conversation.
28
+ *
29
+ * @param chatId - a `chat.id` this adapter wrote, or one a profile configured by hand.
30
+ * @returns the conversation kind and its platform id.
31
+ * @throws PlatformError `invalid` when the id carries no known prefix.
32
+ */
33
+ export function parseChatId(chatId) {
34
+ const index = chatId.indexOf(':');
35
+ const kind = index === -1 ? '' : chatId.slice(0, index);
36
+ if (kind !== 'user' && kind !== 'group' && kind !== 'room') {
37
+ throw new PlatformError('invalid', `line: chat id "${chatId}" carries no user:/group:/room: prefix`, { published: 'no' });
38
+ }
39
+ return { kind, id: chatId.slice(index + 1) };
40
+ }
41
+ /**
42
+ * The `pushMessage` recipient of a `chat.id`.
43
+ *
44
+ * @param chatId - a `chat.id` this adapter wrote.
45
+ * @returns the `to` value of a push request.
46
+ * @throws PlatformError `invalid` when the id carries no known prefix.
47
+ */
48
+ export function chatTargetOf(chatId) {
49
+ return parseChatId(chatId).id;
50
+ }
51
+ /** The sender of a source; LINE omits the user id of an unconverted user, which collapses to `anonymous`. */
52
+ function senderOf(source) {
53
+ const id = source.type === 'user' ? source.userId : (source.userId ?? 'anonymous');
54
+ return { id };
55
+ }
56
+ /** A mention's visible name, without the leading `@` the payload carries. */
57
+ function mentionName(slice) {
58
+ return slice.startsWith('@') ? slice.slice(1) : slice;
59
+ }
60
+ /** One UTF-16 slice of a text message; LINE indices count UTF-16 code units. */
61
+ function sliceOf(text, index, length) {
62
+ return text.slice(index, index + length);
63
+ }
64
+ /** The element nodes of one text message, slicing the body at its emoji and mention spans. */
65
+ function textNodes(message) {
66
+ const text = message.text;
67
+ const spans = [
68
+ ...(message.emojis ?? []).map((emoji) => ({
69
+ kind: 'emoji',
70
+ index: emoji.index,
71
+ length: emoji.length,
72
+ emoji,
73
+ })),
74
+ ...(message.mention?.mentionees ?? []).map((mention) => ({
75
+ kind: 'mention',
76
+ index: mention.index,
77
+ length: mention.length,
78
+ mention,
79
+ })),
80
+ ]
81
+ .filter((span) => span.index >= 0 &&
82
+ span.length > 0 &&
83
+ span.index + span.length <= text.length)
84
+ .sort((a, b) => a.index - b.index);
85
+ const nodes = [];
86
+ let cursor = 0;
87
+ for (const span of spans) {
88
+ // A span the payload reports inside another one cannot be rendered without repeating text.
89
+ if (span.index < cursor)
90
+ continue;
91
+ if (span.index > cursor)
92
+ nodes.push(text.slice(cursor, span.index));
93
+ if (span.kind === 'emoji') {
94
+ nodes.push(h('emoji', { id: `e:${span.emoji.productId}:${span.emoji.emojiId}` }));
95
+ }
96
+ else if (span.mention.type === 'all') {
97
+ nodes.push(h('at', { type: 'all' }));
98
+ }
99
+ else if (span.mention.userId === undefined) {
100
+ // An `at[type=user]` needs the id it addresses; a mention of a user who did not agree to the terms of use names
101
+ // only the visible text, which stays readable as plain text.
102
+ nodes.push(sliceOf(text, span.index, span.length));
103
+ }
104
+ else {
105
+ nodes.push(h('at', {
106
+ type: 'user',
107
+ id: span.mention.userId,
108
+ name: mentionName(sliceOf(text, span.index, span.length)),
109
+ }));
110
+ }
111
+ cursor = span.index + span.length;
112
+ }
113
+ if (cursor < text.length)
114
+ nodes.push(text.slice(cursor));
115
+ return nodes;
116
+ }
117
+ /** The source of an inbound media element: LINE's own content API, or the URL a third party provided. */
118
+ function contentSource(bot, messageId, provider) {
119
+ if (provider?.type === 'external')
120
+ return provider.originalContentUrl;
121
+ return bot.getInternalUrl(`/assets/${encodeURIComponent(messageId)}`);
122
+ }
123
+ /** The elements of one message of the seven platform types. */
124
+ function messageNodes(bot, message) {
125
+ switch (message.type) {
126
+ case 'text':
127
+ return textNodes(message);
128
+ case 'image':
129
+ return [
130
+ h('img', {
131
+ src: contentSource(bot, message.id, message.contentProvider),
132
+ }),
133
+ ];
134
+ case 'video':
135
+ return [
136
+ h('video', {
137
+ src: contentSource(bot, message.id, message.contentProvider),
138
+ ...(message.duration === undefined
139
+ ? {}
140
+ : { duration: message.duration }),
141
+ }),
142
+ ];
143
+ case 'audio':
144
+ return [
145
+ h('audio', {
146
+ src: contentSource(bot, message.id, message.contentProvider),
147
+ duration: message.duration,
148
+ }),
149
+ ];
150
+ case 'file': {
151
+ const size = Number(message.fileSize);
152
+ return [
153
+ h('file', {
154
+ src: bot.getInternalUrl(`/assets/${encodeURIComponent(message.id)}`),
155
+ name: message.fileName,
156
+ ...(Number.isFinite(size) ? { size } : {}),
157
+ }),
158
+ ];
159
+ }
160
+ case 'location': {
161
+ const lines = [
162
+ message.title,
163
+ message.address,
164
+ `${message.latitude}, ${message.longitude}`,
165
+ ].filter((line) => line !== '');
166
+ return [h('p', lines.join('\n'))];
167
+ }
168
+ case 'sticker':
169
+ return [
170
+ h('emoji', {
171
+ id: `s:${message.packageId}:${message.stickerId}`,
172
+ }),
173
+ ];
174
+ default:
175
+ // A message type this snapshot does not know: the caller keeps the payload in a `<raw>` element.
176
+ return [];
177
+ }
178
+ }
179
+ /** Mention flags and mentioned users of one message. */
180
+ function mentionsOf(bot, message) {
181
+ if (message.type !== 'text')
182
+ return { self: false, all: false, users: [] };
183
+ const text = message.text;
184
+ const mentionees = message.mention?.mentionees ?? [];
185
+ const users = [];
186
+ for (const mention of mentionees) {
187
+ if (mention.type !== 'user' || mention.userId === undefined)
188
+ continue;
189
+ users.push({
190
+ id: mention.userId,
191
+ name: mentionName(sliceOf(text, mention.index, mention.length)),
192
+ });
193
+ }
194
+ const all = mentionees.some((mention) => mention.type === 'all');
195
+ // `isSelf` is the platform's own answer to "was the bot mentioned"; an older payload without it falls back to the id.
196
+ const declaresSelf = mentionees.some((mention) => mention.isSelf !== undefined);
197
+ const self = mentionees.some((mention) => mention.isSelf === true) ||
198
+ (!declaresSelf &&
199
+ mentionees.some((mention) => mention.type === 'user' && mention.userId === bot.selfId));
200
+ return { self, all, users };
201
+ }
202
+ /** The quote token of a message, which only the four quotable types carry. */
203
+ function quoteTokenOf(message) {
204
+ switch (message.type) {
205
+ case 'text':
206
+ case 'image':
207
+ case 'video':
208
+ case 'sticker':
209
+ return message.quoteToken;
210
+ default:
211
+ return undefined;
212
+ }
213
+ }
214
+ /** The quote an inbound message answers, when the platform names it. */
215
+ function quoteOf(message) {
216
+ return message.type === 'text' && message.quotedMessageId !== undefined
217
+ ? { id: message.quotedMessageId }
218
+ : undefined;
219
+ }
220
+ /** Decode one message event into the normalized inbound message. */
221
+ function decodeMessage(bot, event) {
222
+ const source = event.source;
223
+ const message = event.message;
224
+ const referrer = {
225
+ messageId: message.id,
226
+ timestamp: event.timestamp,
227
+ mode: event.mode,
228
+ ...(quoteTokenOf(message) === undefined
229
+ ? {}
230
+ : { quoteToken: quoteTokenOf(message) }),
231
+ ...(event.replyToken === undefined ? {} : { replyToken: event.replyToken }),
232
+ };
233
+ const nodes = messageNodes(bot, message);
234
+ const content = normalize(nodes.length > 0
235
+ ? nodes
236
+ : [h('raw', { platform: 'line', type: 'message', data: message })]);
237
+ const quote = quoteOf(message);
238
+ return {
239
+ id: message.id,
240
+ chat: chatRefOf(source),
241
+ sender: senderOf(source),
242
+ content,
243
+ ...(quote === undefined ? {} : { quote }),
244
+ mentions: mentionsOf(bot, message),
245
+ time: event.timestamp,
246
+ referrer,
247
+ raw: event,
248
+ };
249
+ }
250
+ /** Decode the button context of a postback, which names no platform message of its own. */
251
+ function decodePostback(bot, event) {
252
+ const chat = chatRefOf(event.source);
253
+ const referrer = {
254
+ timestamp: event.timestamp,
255
+ mode: event.mode,
256
+ replyToken: event.replyToken,
257
+ };
258
+ const button = {
259
+ id: event.postback.data,
260
+ data: event.postback.data,
261
+ referrer,
262
+ time: event.timestamp,
263
+ bot,
264
+ user: senderOf(event.source),
265
+ chat,
266
+ };
267
+ return bot.session({
268
+ type: 'interaction/button',
269
+ id: event.webhookEventId,
270
+ timestamp: event.timestamp,
271
+ button,
272
+ raw: event,
273
+ });
274
+ }
275
+ /**
276
+ * Decode one webhook event.
277
+ *
278
+ * Events the DSH contract has no consumer for (follows, joins, member changes, beacons, edits, deliveries …) are
279
+ * dropped with a debug line: the model has no `friend-*`/`guild-*` event to receive them.
280
+ *
281
+ * @param bot - the receiving adapter.
282
+ * @param event - one event of the webhook body.
283
+ * @returns the session to publish, or undefined for an ignored event.
284
+ */
285
+ export function decodeEvent(bot, event) {
286
+ if (event.mode === 'standby') {
287
+ // The channel is handled by a human agent; the platform explicitly says not to send from a standby event.
288
+ bot.ctx.logger.debug('line: skipping standby event %s', event.webhookEventId);
289
+ return undefined;
290
+ }
291
+ if (event.deliveryContext?.isRedelivery === true) {
292
+ // A redelivery is handled like any other event: skipping it would lose the event whose first delivery failed.
293
+ bot.ctx.logger.debug('line: handling redelivered event %s', event.webhookEventId);
294
+ }
295
+ switch (event.type) {
296
+ case 'message': {
297
+ const message = decodeMessage(bot, event);
298
+ return bot.session({
299
+ type: 'message',
300
+ id: message.id,
301
+ timestamp: message.time,
302
+ message,
303
+ raw: event,
304
+ });
305
+ }
306
+ case 'unsend':
307
+ return bot.session({
308
+ type: 'message-deleted',
309
+ id: event.webhookEventId,
310
+ timestamp: event.timestamp,
311
+ deleted: { id: event.unsend.messageId, chat: chatRefOf(event.source) },
312
+ raw: event,
313
+ });
314
+ case 'postback':
315
+ return decodePostback(bot, event);
316
+ default:
317
+ bot.ctx.logger.debug('line: ignoring a %s event', event.type);
318
+ return undefined;
319
+ }
320
+ }
321
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;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AS,EAAE,OAAO,CAAC,IAAI;gBACvB,OAAO;gBACP,GAAG,EAAE,KAAK;aACX,CAAC,CAAA;QACJ,CAAC;QACD,KAAK,QAAQ;YACX,OAAO,GAAG,CAAC,OAAO,CAAC;gBACjB,IAAI,EAAE,iBAAiB;gBACvB,EAAE,EAAE,KAAK,CAAC,cAAc;gBACxB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,OAAO,EAAE,EAAE,EAAE,EAAE,KAAK,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE;gBACtE,GAAG,EAAE,KAAK;aACX,CAAC,CAAA;QACJ,KAAK,UAAU;YACb,OAAO,cAAc,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACnC;YACE,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,IAAI,CAAC,CAAA;YAC7D,OAAO,SAAS,CAAA;IACpB,CAAC;AACH,CAAC"}
@@ -0,0 +1,47 @@
1
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
2
+ /** One field-level detail of a LINE business error (`details[].property`). */
3
+ export interface LineErrorDetail {
4
+ property?: string;
5
+ message?: string;
6
+ }
7
+ /** Fields a failed LINE API call carries for classification and logs. */
8
+ export interface LineApiErrorInit {
9
+ /** HTTP status, absent when the request never got an answer. */
10
+ status?: number;
11
+ /** Text of the `message` / `error_description` field, whichever the answer carries. */
12
+ platformMessage?: string;
13
+ /** The `error` field of a token or request-format refusal. */
14
+ error?: string;
15
+ details?: readonly LineErrorDetail[];
16
+ /** Start of the answer body, for a refusal that carried no JSON. */
17
+ bodyText?: string;
18
+ /** The answer body was not JSON, so the status alone does not explain the outcome. */
19
+ nonJson?: boolean;
20
+ cause?: unknown;
21
+ }
22
+ /**
23
+ * One failed LINE Messaging API call.
24
+ *
25
+ * LINE answers a refusal in one of two JSON envelopes — `{ message, details[] }` for a business refusal and
26
+ * `{ error, error_description }` for a token or request-format refusal — or with a page no proxy should produce.
27
+ */
28
+ export declare class LineApiError extends Error {
29
+ readonly status: number | undefined;
30
+ readonly platformMessage: string | undefined;
31
+ readonly error: string | undefined;
32
+ readonly details: readonly LineErrorDetail[];
33
+ readonly bodyText: string | undefined;
34
+ readonly nonJson: boolean;
35
+ constructor(message: string, init?: LineApiErrorInit);
36
+ }
37
+ /**
38
+ * Map any failure onto the core platform taxonomy.
39
+ *
40
+ * A LINE refusal is a request-level answer: the platform rejects the whole request or sends all of it, so every 4xx
41
+ * means nothing was published. Transport failures, answers whose body is not JSON and 5xx leave the outcome unknown.
42
+ *
43
+ * @param error - the caught failure.
44
+ * @returns the classified platform error.
45
+ */
46
+ export declare function classifyLineError(error: unknown): PlatformError;
47
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAElE,8EAA8E;AAC9E,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,yEAAyE;AACzE,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,uFAAuF;IACvF,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,8DAA8D;IAC9D,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,SAAS,eAAe,EAAE,CAAA;IACpC,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,sFAAsF;IACtF,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;;;;GAKG;AACH,qBAAa,YAAa,SAAQ,KAAK;IACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5C,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IAClC,QAAQ,CAAC,OAAO,EAAE,SAAS,eAAe,EAAE,CAAA;IAC5C,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;IACrC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;gBAEb,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,gBAAqB;CAUzD;AAUD;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAsE/D"}
@@ -0,0 +1,112 @@
1
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
2
+ /**
3
+ * One failed LINE Messaging API call.
4
+ *
5
+ * LINE answers a refusal in one of two JSON envelopes — `{ message, details[] }` for a business refusal and
6
+ * `{ error, error_description }` for a token or request-format refusal — or with a page no proxy should produce.
7
+ */
8
+ export class LineApiError extends Error {
9
+ status;
10
+ platformMessage;
11
+ error;
12
+ details;
13
+ bodyText;
14
+ nonJson;
15
+ constructor(message, init = {}) {
16
+ super(message, init.cause === undefined ? undefined : { cause: init.cause });
17
+ this.name = 'LineApiError';
18
+ this.status = init.status;
19
+ this.platformMessage = init.platformMessage;
20
+ this.error = init.error;
21
+ this.details = init.details ?? [];
22
+ this.bodyText = init.bodyText;
23
+ this.nonJson = init.nonJson ?? false;
24
+ }
25
+ }
26
+ /** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
27
+ function isAbortError(error) {
28
+ return ((error instanceof Error || error instanceof DOMException) &&
29
+ error.name === 'AbortError');
30
+ }
31
+ /**
32
+ * Map any failure onto the core platform taxonomy.
33
+ *
34
+ * A LINE refusal is a request-level answer: the platform rejects the whole request or sends all of it, so every 4xx
35
+ * means nothing was published. Transport failures, answers whose body is not JSON and 5xx leave the outcome unknown.
36
+ *
37
+ * @param error - the caught failure.
38
+ * @returns the classified platform error.
39
+ */
40
+ export function classifyLineError(error) {
41
+ if (error instanceof PlatformError)
42
+ return error;
43
+ if (error instanceof LineApiError) {
44
+ const status = error.status;
45
+ const init = {
46
+ ...(status === undefined ? {} : { code: status }),
47
+ cause: error,
48
+ };
49
+ // The answer body was not the platform's JSON envelope (a proxy page, a cut-off answer): the status alone cannot
50
+ // say whether the request was rejected or half-processed.
51
+ if (status === undefined || error.nonJson) {
52
+ return new PlatformError('retryable', error.message, {
53
+ ...init,
54
+ published: 'unknown',
55
+ });
56
+ }
57
+ if (status === 429) {
58
+ return new PlatformError('rate-limit', error.message, {
59
+ ...init,
60
+ published: 'no',
61
+ });
62
+ }
63
+ if (status === 401 || status === 403) {
64
+ return new PlatformError('permission', error.message, {
65
+ ...init,
66
+ published: 'no',
67
+ });
68
+ }
69
+ if (status === 404 || status === 410) {
70
+ return new PlatformError('not-found', error.message, {
71
+ ...init,
72
+ published: 'no',
73
+ });
74
+ }
75
+ if (status === 409) {
76
+ // Only a request carrying `X-Line-Retry-Key` answers 409, and it means the key was already accepted: the
77
+ // message may have been sent.
78
+ return new PlatformError('fatal', error.message, {
79
+ ...init,
80
+ published: 'unknown',
81
+ });
82
+ }
83
+ if (status === 413) {
84
+ return new PlatformError('limit', error.message, {
85
+ ...init,
86
+ published: 'no',
87
+ });
88
+ }
89
+ if (status >= 500) {
90
+ return new PlatformError('retryable', error.message, {
91
+ ...init,
92
+ published: 'unknown',
93
+ });
94
+ }
95
+ return new PlatformError('invalid', error.message, {
96
+ ...init,
97
+ published: 'no',
98
+ });
99
+ }
100
+ if (isAbortError(error)) {
101
+ return new PlatformError('fatal', error.message, {
102
+ published: 'unknown',
103
+ cause: error,
104
+ });
105
+ }
106
+ const message = error instanceof Error ? error.message : String(error);
107
+ return new PlatformError('retryable', `line: request failed: ${message}`, {
108
+ published: 'unknown',
109
+ cause: error,
110
+ });
111
+ }
112
+ //# sourceMappingURL=errors.js.map
@@ -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