@kodelyth/line 2026.5.39 → 2026.5.42

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 (120) hide show
  1. package/api.ts +11 -0
  2. package/channel-plugin-api.ts +1 -0
  3. package/contract-api.ts +5 -0
  4. package/dist/accounts-CD4A1FE7.js +105 -0
  5. package/dist/api.js +11 -0
  6. package/dist/basic-cards-BISytiSa.js +307 -0
  7. package/dist/card-command-dQBX3fVN.js +240 -0
  8. package/dist/channel-DV5h44-j.js +649 -0
  9. package/dist/channel-plugin-api.js +2 -0
  10. package/dist/channel.runtime-Cc-v3szZ.js +4 -0
  11. package/dist/contract-api.js +2 -0
  12. package/dist/index.js +45 -0
  13. package/dist/markdown-to-line-CC3BU6CC.js +810 -0
  14. package/dist/monitor-Ci8Hg8ay.js +1485 -0
  15. package/dist/monitor.runtime-t6-QvlDB.js +2 -0
  16. package/dist/outbound.runtime-D1CxEvcL.js +2 -0
  17. package/dist/probe-BPSs_A_8.js +30 -0
  18. package/dist/probe.runtime-7u2o9QN5.js +2 -0
  19. package/dist/reply-payload-transform-CDuBzoT4.js +855 -0
  20. package/dist/runtime-api.js +291 -0
  21. package/dist/schedule-cards-D-yZMHDE.js +359 -0
  22. package/dist/secret-contract-api.js +5 -0
  23. package/dist/setup-api.js +2 -0
  24. package/dist/setup-entry.js +11 -0
  25. package/dist/setup-surface-CHfQ6Z4i.js +282 -0
  26. package/index.ts +53 -0
  27. package/klaw.plugin.json +2 -329
  28. package/package.json +4 -4
  29. package/runtime-api.ts +179 -0
  30. package/secret-contract-api.ts +4 -0
  31. package/setup-api.ts +2 -0
  32. package/setup-entry.ts +9 -0
  33. package/src/account-helpers.ts +16 -0
  34. package/src/accounts.test.ts +288 -0
  35. package/src/accounts.ts +187 -0
  36. package/src/actions.ts +61 -0
  37. package/src/auto-reply-delivery.test.ts +253 -0
  38. package/src/auto-reply-delivery.ts +200 -0
  39. package/src/bindings.ts +65 -0
  40. package/src/bot-access.ts +30 -0
  41. package/src/bot-handlers.test.ts +1094 -0
  42. package/src/bot-handlers.ts +620 -0
  43. package/src/bot-message-context.test.ts +420 -0
  44. package/src/bot-message-context.ts +586 -0
  45. package/src/bot.ts +66 -0
  46. package/src/card-command.ts +347 -0
  47. package/src/channel-access-token.ts +14 -0
  48. package/src/channel-api.ts +17 -0
  49. package/src/channel-setup-status.contract.test.ts +70 -0
  50. package/src/channel-shared.ts +48 -0
  51. package/src/channel.logout.test.ts +145 -0
  52. package/src/channel.runtime.ts +3 -0
  53. package/src/channel.sendPayload.test.ts +659 -0
  54. package/src/channel.setup.ts +11 -0
  55. package/src/channel.status.test.ts +63 -0
  56. package/src/channel.ts +155 -0
  57. package/src/config-adapter.ts +29 -0
  58. package/src/config-schema.test.ts +53 -0
  59. package/src/config-schema.ts +81 -0
  60. package/src/download.test.ts +164 -0
  61. package/src/download.ts +34 -0
  62. package/src/flex-templates/basic-cards.ts +395 -0
  63. package/src/flex-templates/common.ts +20 -0
  64. package/src/flex-templates/media-control-cards.ts +555 -0
  65. package/src/flex-templates/message.ts +13 -0
  66. package/src/flex-templates/schedule-cards.ts +467 -0
  67. package/src/flex-templates/types.ts +22 -0
  68. package/src/flex-templates.ts +32 -0
  69. package/src/gateway.ts +129 -0
  70. package/src/group-keys.test.ts +123 -0
  71. package/src/group-keys.ts +65 -0
  72. package/src/group-policy.ts +22 -0
  73. package/src/markdown-to-line.test.ts +348 -0
  74. package/src/markdown-to-line.ts +416 -0
  75. package/src/message-cards.test.ts +204 -0
  76. package/src/monitor-durable.test.ts +57 -0
  77. package/src/monitor-durable.ts +37 -0
  78. package/src/monitor.lifecycle.test.ts +499 -0
  79. package/src/monitor.runtime.ts +1 -0
  80. package/src/monitor.ts +507 -0
  81. package/src/outbound-media.test.ts +194 -0
  82. package/src/outbound-media.ts +120 -0
  83. package/src/outbound.runtime.ts +12 -0
  84. package/src/outbound.ts +427 -0
  85. package/src/probe.contract.test.ts +9 -0
  86. package/src/probe.runtime.ts +1 -0
  87. package/src/probe.ts +34 -0
  88. package/src/quick-reply-fallback.ts +10 -0
  89. package/src/reply-chunks.test.ts +180 -0
  90. package/src/reply-chunks.ts +110 -0
  91. package/src/reply-payload-transform.test.ts +392 -0
  92. package/src/reply-payload-transform.ts +317 -0
  93. package/src/rich-menu.test.ts +315 -0
  94. package/src/rich-menu.ts +326 -0
  95. package/src/runtime.ts +32 -0
  96. package/src/send-receipt.ts +32 -0
  97. package/src/send.test.ts +453 -0
  98. package/src/send.ts +531 -0
  99. package/src/setup-core.ts +149 -0
  100. package/src/setup-runtime-api.ts +9 -0
  101. package/src/setup-surface.test.ts +481 -0
  102. package/src/setup-surface.ts +229 -0
  103. package/src/signature.test.ts +34 -0
  104. package/src/signature.ts +24 -0
  105. package/src/status.ts +37 -0
  106. package/src/template-messages.ts +333 -0
  107. package/src/types.ts +130 -0
  108. package/src/webhook-node.test.ts +598 -0
  109. package/src/webhook-node.ts +155 -0
  110. package/src/webhook-utils.ts +10 -0
  111. package/src/webhook.ts +135 -0
  112. package/tsconfig.json +16 -0
  113. package/api.js +0 -7
  114. package/channel-plugin-api.js +0 -7
  115. package/contract-api.js +0 -7
  116. package/index.js +0 -7
  117. package/runtime-api.js +0 -7
  118. package/secret-contract-api.js +0 -7
  119. package/setup-api.js +0 -7
  120. package/setup-entry.js +0 -7
@@ -0,0 +1,420 @@
1
+ import fs from "node:fs/promises";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import type { webhook } from "@line/bot-sdk";
5
+ import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
6
+ import { getSessionBindingService } from "klaw/plugin-sdk/conversation-runtime";
7
+ import { testing as sessionBindingTesting } from "klaw/plugin-sdk/conversation-runtime";
8
+ import { createTestRegistry, setActivePluginRegistry } from "klaw/plugin-sdk/plugin-test-runtime";
9
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
10
+ import { lineBindingsAdapter } from "./bindings.js";
11
+ import { buildLineMessageContext, buildLinePostbackContext } from "./bot-message-context.js";
12
+ import type { ResolvedLineAccount } from "./types.js";
13
+
14
+ type MessageEvent = webhook.MessageEvent;
15
+ type PostbackEvent = webhook.PostbackEvent;
16
+
17
+ const lineBindingsPlugin = {
18
+ id: "line",
19
+ bindings: lineBindingsAdapter,
20
+ conversationBindings: {
21
+ defaultTopLevelPlacement: "current",
22
+ supportsCurrentConversationBinding: true,
23
+ },
24
+ };
25
+
26
+ describe("buildLineMessageContext", () => {
27
+ let tmpDir: string;
28
+ let storePath: string;
29
+ let cfg: KlawConfig;
30
+ const account: ResolvedLineAccount = {
31
+ accountId: "default",
32
+ enabled: true,
33
+ channelAccessToken: "token",
34
+ channelSecret: "secret",
35
+ tokenSource: "config",
36
+ config: {},
37
+ };
38
+
39
+ const createMessageEvent = (
40
+ source: MessageEvent["source"],
41
+ overrides?: Partial<MessageEvent>,
42
+ ): MessageEvent =>
43
+ ({
44
+ type: "message",
45
+ message: { id: "1", type: "text", text: "hello" },
46
+ replyToken: "reply-token",
47
+ timestamp: Date.now(),
48
+ source,
49
+ mode: "active",
50
+ webhookEventId: "evt-1",
51
+ deliveryContext: { isRedelivery: false },
52
+ ...overrides,
53
+ }) as MessageEvent;
54
+
55
+ const createPostbackEvent = (
56
+ source: PostbackEvent["source"],
57
+ overrides?: Partial<PostbackEvent>,
58
+ ): PostbackEvent =>
59
+ ({
60
+ type: "postback",
61
+ postback: { data: "action=select" },
62
+ replyToken: "reply-token",
63
+ timestamp: Date.now(),
64
+ source,
65
+ mode: "active",
66
+ webhookEventId: "evt-2",
67
+ deliveryContext: { isRedelivery: false },
68
+ ...overrides,
69
+ }) as PostbackEvent;
70
+
71
+ beforeEach(async () => {
72
+ setActivePluginRegistry(
73
+ createTestRegistry([
74
+ {
75
+ pluginId: lineBindingsPlugin.id,
76
+ plugin: lineBindingsPlugin,
77
+ source: "test",
78
+ },
79
+ ]),
80
+ );
81
+ sessionBindingTesting.resetSessionBindingAdaptersForTests();
82
+ tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "klaw-line-context-"));
83
+ storePath = path.join(tmpDir, "sessions.json");
84
+ cfg = { session: { store: storePath } };
85
+ });
86
+
87
+ afterEach(async () => {
88
+ sessionBindingTesting.resetSessionBindingAdaptersForTests();
89
+ await fs.rm(tmpDir, {
90
+ recursive: true,
91
+ force: true,
92
+ maxRetries: 3,
93
+ retryDelay: 50,
94
+ });
95
+ });
96
+
97
+ it("routes group message replies to the group id", async () => {
98
+ const event = createMessageEvent({ type: "group", groupId: "group-1", userId: "user-1" });
99
+
100
+ const context = await buildLineMessageContext({
101
+ event,
102
+ allMedia: [],
103
+ cfg,
104
+ account,
105
+ commandAuthorized: true,
106
+ });
107
+
108
+ expect(context?.ctxPayload.OriginatingTo).toBe("line:group:group-1");
109
+ expect(context?.ctxPayload.To).toBe("line:group:group-1");
110
+ });
111
+
112
+ it("routes group postback replies to the group id", async () => {
113
+ const event = createPostbackEvent({ type: "group", groupId: "group-2", userId: "user-2" });
114
+
115
+ const context = await buildLinePostbackContext({
116
+ event,
117
+ cfg,
118
+ account,
119
+ commandAuthorized: true,
120
+ });
121
+
122
+ expect(context?.ctxPayload.OriginatingTo).toBe("line:group:group-2");
123
+ expect(context?.ctxPayload.To).toBe("line:group:group-2");
124
+ });
125
+
126
+ it("routes room postback replies to the room id", async () => {
127
+ const event = createPostbackEvent({ type: "room", roomId: "room-1", userId: "user-3" });
128
+
129
+ const context = await buildLinePostbackContext({
130
+ event,
131
+ cfg,
132
+ account,
133
+ commandAuthorized: true,
134
+ });
135
+
136
+ expect(context?.ctxPayload.OriginatingTo).toBe("line:room:room-1");
137
+ expect(context?.ctxPayload.To).toBe("line:room:room-1");
138
+ });
139
+
140
+ it("resolves prefixed-only group config through the inbound message context", async () => {
141
+ const event = createMessageEvent({ type: "group", groupId: "group-1", userId: "user-1" });
142
+
143
+ const context = await buildLineMessageContext({
144
+ event,
145
+ allMedia: [],
146
+ cfg,
147
+ account: {
148
+ ...account,
149
+ config: {
150
+ groups: {
151
+ "group:group-1": {
152
+ systemPrompt: "Use the prefixed group config",
153
+ },
154
+ },
155
+ },
156
+ },
157
+ commandAuthorized: true,
158
+ });
159
+
160
+ expect(context?.ctxPayload.GroupSystemPrompt).toBe("Use the prefixed group config");
161
+ });
162
+
163
+ it("resolves prefixed-only room config through the inbound message context", async () => {
164
+ const event = createMessageEvent({ type: "room", roomId: "room-1", userId: "user-1" });
165
+
166
+ const context = await buildLineMessageContext({
167
+ event,
168
+ allMedia: [],
169
+ cfg,
170
+ account: {
171
+ ...account,
172
+ config: {
173
+ groups: {
174
+ "room:room-1": {
175
+ systemPrompt: "Use the prefixed room config",
176
+ },
177
+ },
178
+ },
179
+ },
180
+ commandAuthorized: true,
181
+ });
182
+
183
+ expect(context?.ctxPayload.GroupSystemPrompt).toBe("Use the prefixed room config");
184
+ });
185
+
186
+ it("keeps non-text message contexts fail-closed for command auth", async () => {
187
+ const event = createMessageEvent(
188
+ { type: "user", userId: "user-audio" },
189
+ {
190
+ message: { id: "audio-1", type: "audio", duration: 1000 } as MessageEvent["message"],
191
+ },
192
+ );
193
+
194
+ const context = await buildLineMessageContext({
195
+ event,
196
+ allMedia: [],
197
+ cfg,
198
+ account,
199
+ commandAuthorized: false,
200
+ });
201
+
202
+ expect(context?.ctxPayload.CommandAuthorized).toBe(false);
203
+ });
204
+
205
+ it("sets CommandAuthorized=true when authorized", async () => {
206
+ const event = createMessageEvent({ type: "user", userId: "user-auth" });
207
+
208
+ const context = await buildLineMessageContext({
209
+ event,
210
+ allMedia: [],
211
+ cfg,
212
+ account,
213
+ commandAuthorized: true,
214
+ });
215
+
216
+ expect(context?.ctxPayload.CommandAuthorized).toBe(true);
217
+ });
218
+
219
+ it("sets CommandAuthorized=false when not authorized", async () => {
220
+ const event = createMessageEvent({ type: "user", userId: "user-noauth" });
221
+
222
+ const context = await buildLineMessageContext({
223
+ event,
224
+ allMedia: [],
225
+ cfg,
226
+ account,
227
+ commandAuthorized: false,
228
+ });
229
+
230
+ expect(context?.ctxPayload.CommandAuthorized).toBe(false);
231
+ });
232
+
233
+ it("keeps per-channel-peer direct-message last-route writes on the isolated session", async () => {
234
+ const event = createMessageEvent({ type: "user", userId: "user-1" });
235
+ const directCfg: KlawConfig = {
236
+ session: { store: storePath, dmScope: "per-channel-peer" },
237
+ };
238
+
239
+ const context = await buildLineMessageContext({
240
+ event,
241
+ allMedia: [],
242
+ cfg: directCfg,
243
+ account: {
244
+ ...account,
245
+ config: { allowFrom: ["user-1"] },
246
+ },
247
+ commandAuthorized: true,
248
+ });
249
+
250
+ expect(context?.route.sessionKey).toBe("agent:main:line:direct:user-1");
251
+ const updateLastRoute = context?.turn.record.updateLastRoute;
252
+ expect(updateLastRoute?.sessionKey).toBe(context?.route.sessionKey);
253
+ expect(updateLastRoute?.sessionKey).not.toBe("agent:main:main");
254
+ expect(updateLastRoute?.channel).toBe("line");
255
+ expect(updateLastRoute?.to).toBe("user-1");
256
+ expect(updateLastRoute?.mainDmOwnerPin).toBeUndefined();
257
+ });
258
+
259
+ it("sets CommandAuthorized on postback context", async () => {
260
+ const event = createPostbackEvent({ type: "user", userId: "user-pb" });
261
+
262
+ const context = await buildLinePostbackContext({
263
+ event,
264
+ cfg,
265
+ account,
266
+ commandAuthorized: true,
267
+ });
268
+
269
+ expect(context?.ctxPayload.CommandAuthorized).toBe(true);
270
+ });
271
+
272
+ it("group peer binding matches raw groupId without prefix (#21907)", async () => {
273
+ const groupId = "Cc7e3bece1234567890abcdef"; // pragma: allowlist secret
274
+ const bindingCfg: KlawConfig = {
275
+ session: { store: storePath },
276
+ agents: {
277
+ list: [{ id: "main" }, { id: "line-group-agent" }],
278
+ },
279
+ bindings: [
280
+ {
281
+ agentId: "line-group-agent",
282
+ match: { channel: "line", peer: { kind: "group", id: groupId } },
283
+ },
284
+ ],
285
+ };
286
+
287
+ const event = {
288
+ type: "message",
289
+ message: { id: "msg-1", type: "text", text: "hello" },
290
+ replyToken: "reply-token",
291
+ timestamp: Date.now(),
292
+ source: { type: "group", groupId, userId: "user-1" },
293
+ mode: "active",
294
+ webhookEventId: "evt-1",
295
+ deliveryContext: { isRedelivery: false },
296
+ } as MessageEvent;
297
+
298
+ const context = await buildLineMessageContext({
299
+ event,
300
+ allMedia: [],
301
+ cfg: bindingCfg,
302
+ account,
303
+ commandAuthorized: true,
304
+ });
305
+ expect(context?.route.agentId).toBe("line-group-agent");
306
+ expect(context?.route.matchedBy).toBe("binding.peer");
307
+ });
308
+
309
+ it("room peer binding matches raw roomId without prefix (#21907)", async () => {
310
+ const roomId = "Rr1234567890abcdef";
311
+ const bindingCfg: KlawConfig = {
312
+ session: { store: storePath },
313
+ agents: {
314
+ list: [{ id: "main" }, { id: "line-room-agent" }],
315
+ },
316
+ bindings: [
317
+ {
318
+ agentId: "line-room-agent",
319
+ match: { channel: "line", peer: { kind: "group", id: roomId } },
320
+ },
321
+ ],
322
+ };
323
+
324
+ const event = {
325
+ type: "message",
326
+ message: { id: "msg-2", type: "text", text: "hello" },
327
+ replyToken: "reply-token",
328
+ timestamp: Date.now(),
329
+ source: { type: "room", roomId, userId: "user-2" },
330
+ mode: "active",
331
+ webhookEventId: "evt-2",
332
+ deliveryContext: { isRedelivery: false },
333
+ } as MessageEvent;
334
+
335
+ const context = await buildLineMessageContext({
336
+ event,
337
+ allMedia: [],
338
+ cfg: bindingCfg,
339
+ account,
340
+ commandAuthorized: true,
341
+ });
342
+ expect(context?.route.agentId).toBe("line-room-agent");
343
+ expect(context?.route.matchedBy).toBe("binding.peer");
344
+ });
345
+
346
+ it("normalizes LINE ACP binding conversation ids through the plugin bindings surface", () => {
347
+ const compiled = lineBindingsAdapter.compileConfiguredBinding({
348
+ conversationId: "line:user:U1234567890abcdef1234567890abcdef",
349
+ });
350
+
351
+ expect(compiled).toEqual({
352
+ conversationId: "U1234567890abcdef1234567890abcdef",
353
+ });
354
+ expect(
355
+ lineBindingsAdapter.matchInboundConversation({
356
+ compiledBinding: compiled!,
357
+ conversationId: "U1234567890abcdef1234567890abcdef",
358
+ }),
359
+ ).toEqual({
360
+ conversationId: "U1234567890abcdef1234567890abcdef",
361
+ matchPriority: 2,
362
+ });
363
+ });
364
+
365
+ it("normalizes canonical LINE targets through the plugin bindings surface", () => {
366
+ const compiled = lineBindingsAdapter.compileConfiguredBinding({
367
+ conversationId: "line:U1234567890abcdef1234567890abcdef",
368
+ });
369
+
370
+ expect(compiled).toEqual({
371
+ conversationId: "U1234567890abcdef1234567890abcdef",
372
+ });
373
+ expect(
374
+ lineBindingsAdapter.resolveCommandConversation({
375
+ originatingTo: "line:U1234567890abcdef1234567890abcdef",
376
+ }),
377
+ ).toEqual({
378
+ conversationId: "U1234567890abcdef1234567890abcdef",
379
+ });
380
+ expect(
381
+ lineBindingsAdapter.matchInboundConversation({
382
+ compiledBinding: compiled!,
383
+ conversationId: "U1234567890abcdef1234567890abcdef",
384
+ }),
385
+ ).toEqual({
386
+ conversationId: "U1234567890abcdef1234567890abcdef",
387
+ matchPriority: 2,
388
+ });
389
+ });
390
+
391
+ it("routes LINE conversations through active ACP session bindings", async () => {
392
+ const userId = "U1234567890abcdef1234567890abcdef";
393
+ await getSessionBindingService().bind({
394
+ targetSessionKey: "agent:codex:acp:binding:line:default:test123",
395
+ targetKind: "session",
396
+ conversation: {
397
+ channel: "line",
398
+ accountId: "default",
399
+ conversationId: userId,
400
+ },
401
+ placement: "current",
402
+ metadata: {
403
+ agentId: "codex",
404
+ },
405
+ });
406
+
407
+ const event = createMessageEvent({ type: "user", userId });
408
+ const context = await buildLineMessageContext({
409
+ event,
410
+ allMedia: [],
411
+ cfg,
412
+ account,
413
+ commandAuthorized: true,
414
+ });
415
+
416
+ expect(context?.route.agentId).toBe("codex");
417
+ expect(context?.route.sessionKey).toBe("agent:codex:acp:binding:line:default:test123");
418
+ expect(context?.route.matchedBy).toBe("binding.channel");
419
+ });
420
+ });