@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
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@im-more/dsh-line",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "LINE adapter for official dsh-im of DeepSeek Harness",
5
+ "homepage": "https://github.com/ilharp/dsh-im-more",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/ilharp/dsh-im-more.git",
9
+ "directory": "packages/line"
10
+ },
11
+ "author": {
12
+ "name": "Il Harper",
13
+ "email": "hi@ilharper.com",
14
+ "url": "https://ilharper.com"
15
+ },
16
+ "license": "MIT",
17
+ "type": "module",
18
+ "main": "lib/index.js",
19
+ "types": "lib/types/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./lib/types/index.d.ts",
23
+ "default": "./lib/index.js"
24
+ },
25
+ "./package.json": "./package.json",
26
+ "./cordis.patch.yml": "./cordis.patch.yml"
27
+ },
28
+ "files": [
29
+ "lib",
30
+ "src",
31
+ "cordis.patch.yml"
32
+ ],
33
+ "engines": {
34
+ "dsh": ">=0.2.3-alpha.3"
35
+ },
36
+ "peerDependencies": {
37
+ "@deepseek-ai/cordis": ">=4.2.0-alpha.2",
38
+ "@deepseek-ai/cordis-plugin-http": ">=1.6.1-alpha.2",
39
+ "@deepseek-ai/cordis-plugin-im-core": ">=5.0.1-alpha.2",
40
+ "@deepseek-ai/satori-element": ">=4.0.1-alpha.2",
41
+ "@deepseek-ai/schemastery": ">=3.20.0-alpha.2"
42
+ },
43
+ "devDependencies": {
44
+ "@deepseek-ai/cordis": ">=4.2.0-alpha.2",
45
+ "@deepseek-ai/cordis-plugin-http": ">=1.6.1-alpha.2",
46
+ "@deepseek-ai/cordis-plugin-im-core": ">=5.0.1-alpha.2",
47
+ "@deepseek-ai/satori-element": ">=4.0.1-alpha.2",
48
+ "@deepseek-ai/schemastery": ">=3.20.0-alpha.2",
49
+ "@types/node": "^22.20.1",
50
+ "tsdown": "^0.22.2",
51
+ "typescript": "^6.0.3",
52
+ "vitest": "^4.1.11"
53
+ },
54
+ "dsh": {
55
+ "bundle": {
56
+ "patch": "./cordis.patch.yml"
57
+ }
58
+ },
59
+ "scripts": {
60
+ "build": "tsc -p tsconfig.json && tsdown",
61
+ "typecheck": "tsc -p tsconfig.test.json",
62
+ "test": "vitest run"
63
+ }
6
64
  }
package/src/bot.ts ADDED
@@ -0,0 +1,229 @@
1
+ import type { Context } from '@deepseek-ai/cordis'
2
+ import { Adapter, PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
3
+ import type {
4
+ ChatRef,
5
+ MembersPage,
6
+ Mode,
7
+ PrepareContext,
8
+ ResolvedResource,
9
+ ResourceKind,
10
+ ResourceRef,
11
+ SendContext,
12
+ SentPart,
13
+ Target,
14
+ } from '@deepseek-ai/cordis-plugin-im-core'
15
+ import { Config, configError, publicBase, routePath } from './config.ts'
16
+ import { classifyLineError } from './errors.ts'
17
+ import { verifyIdentity } from './identity.ts'
18
+ import { serveMessageContent } from './media.ts'
19
+ import type { LineModeTypes, LinePayload } from './modes/shared.ts'
20
+ import { createTextMode } from './modes/text.ts'
21
+ import type { LineRequest } from './request.ts'
22
+ import { members, sendPayload } from './send.ts'
23
+ import { LineMediaRoute, MediaStaging } from './transport/media.ts'
24
+ import { LineWebhook } from './transport/webhook.ts'
25
+ import type { WebServerLike } from './transport/webhook.ts'
26
+ // The migrated declarations register their typed methods on `Internal` when they are imported.
27
+ import './types/index.ts'
28
+ import type { BotInfoResponse } from './types/index.ts'
29
+ import { Internal } from './types/internal.ts'
30
+
31
+ /** The single mode this adapter always registers. */
32
+ export interface LineModes {
33
+ [mode: string]: Mode<LineModeTypes>
34
+ text: Mode<LineModeTypes>
35
+ }
36
+
37
+ /** Message of any caught value, for a log line. */
38
+ function messageOf(error: unknown): string {
39
+ return error instanceof Error ? error.message : String(error)
40
+ }
41
+
42
+ /** Whether a resource source is a public HTTPS URL LINE can fetch itself. */
43
+ function isHttpsSource(src: string): boolean {
44
+ try {
45
+ return new URL(src).protocol === 'https:'
46
+ } catch {
47
+ // Not a URL at all (`data:`, `file:`, a bare path): those forms are fetched and staged instead.
48
+ return false
49
+ }
50
+ }
51
+
52
+ /**
53
+ * LINE Messaging API adapter.
54
+ *
55
+ * The channel is identified by configuration (`selfId`), confirmed with `getBotInfo` on connect. The webhook is the
56
+ * only transport, `text` is the only mode, and every send is a push: a reply token lives one minute and is single-use,
57
+ * while a turn may run for minutes. Outbound media is staged in memory and served from a public route, because the
58
+ * platform accepts media only as HTTPS URLs it fetches itself.
59
+ */
60
+ export class LineBot extends Adapter<Config, LineModes> {
61
+ /** The adapter needs the Satori service and the isolated `http` service. */
62
+ static override inject = ['satori', 'http']
63
+ /** The Loader applies this schema to a profile row's `config` before the constructor runs. */
64
+ static readonly Config = Config
65
+
66
+ readonly modes: LineModes = { text: createTextMode() }
67
+ readonly modeOrder = ['text'] as const
68
+
69
+ /** Endpoints shared by the API client and the content download. */
70
+ readonly request: LineRequest
71
+ /** Full typed Messaging API surface registered by the vendored declarations. */
72
+ override readonly internal: Internal
73
+ /** Outbound media bytes, served from the public media route. */
74
+ readonly staging: MediaStaging
75
+
76
+ private webhook: LineWebhook | undefined
77
+ private mediaRoute: LineMediaRoute | undefined
78
+
79
+ constructor(ctx: Context, config: Config) {
80
+ super(ctx, config, {
81
+ id: `line:${config.selfId}`,
82
+ platform: 'line',
83
+ adapter: 'line',
84
+ })
85
+ // `bot.id` has to be fixed at construction, so the channel is configured rather than discovered; `connect` proves
86
+ // the token belongs to it.
87
+ this.selfId = config.selfId
88
+ this.request = { ctx, config }
89
+ this.internal = new Internal(this.request)
90
+ this.staging = new MediaStaging(
91
+ publicBase(config.publicUrl),
92
+ routePath(config.media.path),
93
+ )
94
+ this.defineInternalRoute('/assets/:messageId', async ({ params }) =>
95
+ serveMessageContent(this, params.messageId),
96
+ )
97
+ }
98
+
99
+ override async connect(): Promise<void> {
100
+ const invalid = configError(this.config)
101
+ if (invalid !== undefined) {
102
+ this.ctx.logger.error('line: connect failed: %s', invalid.message)
103
+ throw invalid
104
+ }
105
+ const webServer = this.ctx.get('webServer') as WebServerLike | undefined
106
+ if (webServer === undefined) {
107
+ const error = new Error(
108
+ 'line: this adapter needs a profile-provided webServer; LINE has only the webhook transport, so add a webServer row to the profile and make publicUrl reachable from the internet',
109
+ )
110
+ this.ctx.logger.error(error.message)
111
+ throw error
112
+ }
113
+ try {
114
+ const info: BotInfoResponse = await this.internal.getBotInfo()
115
+ this.selfId = verifyIdentity(this.config.selfId, info)
116
+ this.user = { id: info.userId, name: info.displayName, isBot: true }
117
+ const webhook = new LineWebhook(this, webServer)
118
+ const mediaRoute = new LineMediaRoute(this, webServer)
119
+ webhook.start()
120
+ try {
121
+ mediaRoute.start()
122
+ } catch (error) {
123
+ webhook.stop()
124
+ throw error
125
+ }
126
+ try {
127
+ await webhook.announce()
128
+ } catch (error) {
129
+ mediaRoute.stop()
130
+ throw error
131
+ }
132
+ this.webhook = webhook
133
+ this.mediaRoute = mediaRoute
134
+ this.online()
135
+ } catch (error) {
136
+ // `Bot.start` only stores a connect failure on `bot.error`, so log it here.
137
+ this.ctx.logger.error('line: connect failed: %s', messageOf(error))
138
+ throw error
139
+ }
140
+ }
141
+
142
+ override async disconnect(): Promise<void> {
143
+ const webhook = this.webhook
144
+ const mediaRoute = this.mediaRoute
145
+ this.webhook = undefined
146
+ this.mediaRoute = undefined
147
+ webhook?.stop()
148
+ mediaRoute?.stop()
149
+ }
150
+
151
+ /**
152
+ * Turn one outbound attachment into the key a message object carries.
153
+ *
154
+ * A public HTTPS source is passed through untouched: LINE fetches it, so the bytes never enter this process. Every
155
+ * other source (`data:`, `base64:`, `satori:`, `file:`, plain HTTP) is fetched and staged on the public media route.
156
+ */
157
+ override async upload(
158
+ ref: ResourceRef,
159
+ _scope: string,
160
+ ctx: PrepareContext,
161
+ ): Promise<ResolvedResource> {
162
+ if (isHttpsSource(ref.src)) {
163
+ return {
164
+ key: ref.src,
165
+ extra: { name: ref.meta.name, mime: ref.meta.mime },
166
+ }
167
+ }
168
+ let fetched: Awaited<ReturnType<PrepareContext['fetch']>>
169
+ try {
170
+ fetched = await ctx.fetch(ref.src, ctx.signal)
171
+ } catch (error) {
172
+ // The bytes never reached a request: a source that cannot be read is a request-level failure, published: no.
173
+ if (ctx.signal.aborted) throw error
174
+ throw new PlatformError(
175
+ 'invalid',
176
+ `line: media source ${ref.src} could not be read: ${messageOf(error)}`,
177
+ { published: 'no', cause: error },
178
+ )
179
+ }
180
+ const mime = ref.meta.mime ?? fetched.mime
181
+ const name = ref.meta.name ?? fetched.name
182
+ const key = this.staging.stage({
183
+ data: fetched.data,
184
+ ...(mime === undefined ? {} : { mime }),
185
+ ...(name === undefined ? {} : { name }),
186
+ })
187
+ return {
188
+ key,
189
+ extra: {
190
+ ...(name === undefined ? {} : { name }),
191
+ ...(mime === undefined ? {} : { mime }),
192
+ size: fetched.data.byteLength,
193
+ },
194
+ }
195
+ }
196
+
197
+ override resourceScope(_target: Target, _kind: ResourceKind): string {
198
+ return this.id
199
+ }
200
+
201
+ override async sendPayload(
202
+ payload: unknown,
203
+ target: Target,
204
+ ctx: SendContext,
205
+ ): Promise<SentPart> {
206
+ return await sendPayload(
207
+ { internal: this.internal },
208
+ payload as LinePayload,
209
+ target,
210
+ ctx,
211
+ )
212
+ }
213
+
214
+ override async members(
215
+ chat: ChatRef,
216
+ cursor?: string,
217
+ limit?: number,
218
+ ): Promise<MembersPage> {
219
+ return await members({ internal: this.internal }, chat, cursor, limit)
220
+ }
221
+
222
+ override classify(error: unknown) {
223
+ return classifyLineError(error)
224
+ }
225
+ }
226
+
227
+ export { Config }
228
+
229
+ export default LineBot
package/src/config.ts ADDED
@@ -0,0 +1,113 @@
1
+ import z from '@deepseek-ai/schemastery'
2
+
3
+ /** One LINE Messaging API channel. */
4
+ export interface Config {
5
+ /** Channel access token: the `Authorization: Bearer` value of every `api.line.me` call. */
6
+ token: string
7
+ /** Channel secret: the HMAC key of `X-Line-Signature`; no API call uses it. */
8
+ secret: string
9
+ /** The bot's own user id (`U` + 32 hex), checked against `getBotInfo` on connect. */
10
+ selfId: string
11
+ /** Public base URL LINE reaches this process at; the webhook endpoint and the media URLs are built from it. */
12
+ publicUrl: string
13
+ webhook: {
14
+ /** Inbound webhook path: absolute, no trailing slash. */
15
+ path: string
16
+ }
17
+ media: {
18
+ /** Public media route prefix: absolute, no trailing slash. Must differ from `webhook.path`. */
19
+ path: string
20
+ }
21
+ }
22
+
23
+ /** Validated LINE adapter configuration. */
24
+ export const Config: z<Config> = z.object({
25
+ token: z.string().role('secret').required(),
26
+ secret: z.string().role('secret').required(),
27
+ selfId: z.string().required(),
28
+ publicUrl: z.string().role('url').required(),
29
+ webhook: z
30
+ .object({
31
+ path: z.string().default('/line'),
32
+ })
33
+ .default({}),
34
+ media: z
35
+ .object({
36
+ path: z.string().default('/line-media'),
37
+ })
38
+ .default({}),
39
+ })
40
+
41
+ /**
42
+ * Normalize a route path: absolute, no trailing slash.
43
+ *
44
+ * @param path - a configured path.
45
+ * @returns the pathname to register on the web server.
46
+ */
47
+ export function routePath(path: string): string {
48
+ const withSlash = path.startsWith('/') ? path : `/${path}`
49
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash
50
+ }
51
+
52
+ /**
53
+ * The public base URL without its trailing slashes.
54
+ *
55
+ * @param publicUrl - the configured `publicUrl`.
56
+ * @returns the base every public URL is prefixed with.
57
+ */
58
+ export function publicBase(publicUrl: string): string {
59
+ return publicUrl.replace(/\/+$/, '')
60
+ }
61
+
62
+ /**
63
+ * The configuration the adapter cannot work from, as far as it can tell locally.
64
+ *
65
+ * LINE has only the webhook transport, so a profile without a public HTTPS base can neither receive an event nor let
66
+ * LINE fetch outbound media. The check runs when the bot starts, not on the first callback.
67
+ *
68
+ * @param config - the validated configuration.
69
+ * @returns the configuration error, or undefined when the configuration is usable.
70
+ */
71
+ export function configError(config: Config): Error | undefined {
72
+ if (config.token === '') {
73
+ return new Error(
74
+ 'line: token is required (the channel access token of the Messaging API channel)',
75
+ )
76
+ }
77
+ if (config.secret === '') {
78
+ return new Error(
79
+ 'line: secret is required (the channel secret; it verifies every webhook)',
80
+ )
81
+ }
82
+ if (config.selfId === '') {
83
+ return new Error(
84
+ 'line: selfId is required (the bot\'s own user id: "U" + 32 hex, read from the console Basic settings)',
85
+ )
86
+ }
87
+ if (config.publicUrl === '') {
88
+ return new Error(
89
+ 'line: publicUrl is required (LINE only delivers webhooks, and it fetches outbound media from this base)',
90
+ )
91
+ }
92
+ let origin: URL
93
+ try {
94
+ origin = new URL(config.publicUrl)
95
+ } catch {
96
+ return new Error(
97
+ `line: publicUrl must be an absolute URL, got "${config.publicUrl}"`,
98
+ )
99
+ }
100
+ if (origin.protocol !== 'https:') {
101
+ return new Error(
102
+ `line: publicUrl must be an https URL, got "${config.publicUrl}" (LINE refuses a plain-HTTP webhook and fetches media over https)`,
103
+ )
104
+ }
105
+ const webhook = routePath(config.webhook.path)
106
+ const media = routePath(config.media.path)
107
+ if (webhook === media) {
108
+ return new Error(
109
+ `line: webhook.path and media.path must differ, both are "${webhook}"`,
110
+ )
111
+ }
112
+ return undefined
113
+ }