@elizaos/plugin-line 2.0.0-alpha.3

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.
package/build.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { execSync } from "node:child_process";
2
+ import { rmSync } from "node:fs";
3
+ import { join } from "node:path";
4
+
5
+ const distDir = join(import.meta.dirname, "dist");
6
+
7
+ // Clean
8
+ rmSync(distDir, { recursive: true, force: true });
9
+
10
+ // Build
11
+ execSync("npx tsc -p tsconfig.json", {
12
+ cwd: import.meta.dirname,
13
+ stdio: "inherit",
14
+ });
15
+
16
+ console.log("Build complete: plugin-line");
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * LINE Plugin for ElizaOS
3
+ *
4
+ * Provides LINE Messaging API integration for ElizaOS agents,
5
+ * supporting text, flex messages, locations, and more.
6
+ */
7
+ import { logger } from "@elizaos/core";
8
+ import { sendFlexMessage, sendLocation, sendMessage } from "./actions/index.js";
9
+ import { chatContextProvider, userContextProvider } from "./providers/index.js";
10
+ import { LineService } from "./service.js";
11
+ // Re-export types and service
12
+ export * from "./types.js";
13
+ export { LineService };
14
+ export { sendMessage, sendFlexMessage, sendLocation };
15
+ export { chatContextProvider, userContextProvider };
16
+ // Account management exports
17
+ export { DEFAULT_ACCOUNT_ID, isLineMentionRequired, isLineUserAllowed, isMultiAccountEnabled, listEnabledLineAccounts, listLineAccountIds, normalizeAccountId, resolveDefaultLineAccountId, resolveLineAccount, resolveLineGroupConfig, resolveLineSecret, resolveLineToken, } from "./accounts.js";
18
+ // Messaging utilities exports
19
+ export { buildLineDeepLink, chunkLineText, extractCodeBlocks, extractLinks, extractMarkdownTables, formatCodeBlockAsText, formatLineUser, formatTableAsText, getChatId, getChatType, hasMarkdownContent, isGroupChat, LINE_MAX_REPLY_MESSAGES, LINE_TEXT_CHUNK_LIMIT, markdownToLineChunks, processLineMessage, resolveLineSystemLocation, stripMarkdown, truncateText, } from "./messaging.js";
20
+ /**
21
+ * LINE plugin for ElizaOS agents.
22
+ */
23
+ const linePlugin = {
24
+ name: "line",
25
+ description: "LINE Messaging API plugin for ElizaOS agents",
26
+ services: [LineService],
27
+ actions: [sendMessage, sendFlexMessage, sendLocation],
28
+ providers: [chatContextProvider, userContextProvider],
29
+ tests: [],
30
+ init: async (config, _runtime) => {
31
+ logger.info("Initializing LINE plugin...");
32
+ const hasAccessToken = Boolean(config.LINE_CHANNEL_ACCESS_TOKEN || process.env.LINE_CHANNEL_ACCESS_TOKEN);
33
+ const hasSecret = Boolean(config.LINE_CHANNEL_SECRET || process.env.LINE_CHANNEL_SECRET);
34
+ logger.info("LINE plugin configuration:");
35
+ logger.info(` - Access token configured: ${hasAccessToken ? "Yes" : "No"}`);
36
+ logger.info(` - Channel secret configured: ${hasSecret ? "Yes" : "No"}`);
37
+ logger.info(` - DM policy: ${config.LINE_DM_POLICY || process.env.LINE_DM_POLICY || "pairing"}`);
38
+ logger.info(` - Group policy: ${config.LINE_GROUP_POLICY || process.env.LINE_GROUP_POLICY || "allowlist"}`);
39
+ if (!hasAccessToken) {
40
+ logger.warn("LINE channel access token not configured. Set LINE_CHANNEL_ACCESS_TOKEN.");
41
+ }
42
+ if (!hasSecret) {
43
+ logger.warn("LINE channel secret not configured. Set LINE_CHANNEL_SECRET.");
44
+ }
45
+ logger.info("LINE plugin initialized");
46
+ },
47
+ };
48
+ export default linePlugin;
49
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@elizaos/plugin-line",
3
+ "version": "2.0.0-alpha.3",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "bun run build.ts",
9
+ "test": "vitest run",
10
+ "lint": "biome check --write --unsafe src"
11
+ },
12
+ "dependencies": {
13
+ "@elizaos/core": "2.0.0-alpha.3",
14
+ "@line/bot-sdk": "^10.5.0",
15
+ "zod": "^4.3.6"
16
+ },
17
+ "devDependencies": {
18
+ "@types/node": "^20.0.0",
19
+ "typescript": "^5.6.0",
20
+ "vitest": "^2.0.0"
21
+ },
22
+ "milaidy": {
23
+ "platforms": [
24
+ "node"
25
+ ],
26
+ "runtime": "node",
27
+ "platformDetails": {
28
+ "node": "Node.js via main entry point"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ }
34
+ }
@@ -0,0 +1,462 @@
1
+ import type { IAgentRuntime } from "@elizaos/core";
2
+
3
+ /**
4
+ * Default account identifier used when no specific account is configured
5
+ */
6
+ export const DEFAULT_ACCOUNT_ID = "default";
7
+
8
+ /**
9
+ * Token source indicator
10
+ */
11
+ export type LineTokenSource = "config" | "env" | "character" | "none";
12
+
13
+ /**
14
+ * Group-specific configuration
15
+ */
16
+ export interface LineGroupConfig {
17
+ /** If false, ignore messages from this group */
18
+ enabled?: boolean;
19
+ /** Allowlist for users in this group */
20
+ allowFrom?: Array<string | number>;
21
+ /** Require bot mention to respond */
22
+ requireMention?: boolean;
23
+ /** Custom system prompt for this group */
24
+ systemPrompt?: string;
25
+ /** Skills enabled for this group */
26
+ skills?: string[];
27
+ }
28
+
29
+ /**
30
+ * Configuration for a single LINE account
31
+ */
32
+ export interface LineAccountConfig {
33
+ /** Optional display name for this account */
34
+ name?: string;
35
+ /** If false, do not start this LINE account */
36
+ enabled?: boolean;
37
+ /** Channel access token */
38
+ channelAccessToken?: string;
39
+ /** Channel secret */
40
+ channelSecret?: string;
41
+ /** Path to file containing channel access token */
42
+ tokenFile?: string;
43
+ /** Path to file containing channel secret */
44
+ secretFile?: string;
45
+ /** Allowlist for DM senders */
46
+ allowFrom?: Array<string | number>;
47
+ /** Allowlist for groups */
48
+ groupAllowFrom?: Array<string | number>;
49
+ /** DM access policy */
50
+ dmPolicy?: "open" | "allowlist" | "pairing" | "disabled";
51
+ /** Group message access policy */
52
+ groupPolicy?: "open" | "allowlist" | "disabled";
53
+ /** Max media size in MB */
54
+ mediaMaxMb?: number;
55
+ /** Custom webhook path */
56
+ webhookPath?: string;
57
+ /** Group-specific configurations */
58
+ groups?: Record<string, LineGroupConfig>;
59
+ }
60
+
61
+ /**
62
+ * Multi-account LINE configuration structure
63
+ */
64
+ export interface LineMultiAccountConfig {
65
+ /** Default/base configuration applied to all accounts */
66
+ enabled?: boolean;
67
+ channelAccessToken?: string;
68
+ channelSecret?: string;
69
+ tokenFile?: string;
70
+ secretFile?: string;
71
+ dmPolicy?: "open" | "allowlist" | "pairing" | "disabled";
72
+ groupPolicy?: "open" | "allowlist" | "disabled";
73
+ mediaMaxMb?: number;
74
+ webhookPath?: string;
75
+ /** Per-account configuration overrides */
76
+ accounts?: Record<string, LineAccountConfig>;
77
+ /** Group configurations at base level */
78
+ groups?: Record<string, LineGroupConfig>;
79
+ }
80
+
81
+ /**
82
+ * Token resolution result
83
+ */
84
+ export interface LineTokenResolution {
85
+ token: string;
86
+ source: LineTokenSource;
87
+ }
88
+
89
+ /**
90
+ * Resolved LINE account with all configuration merged
91
+ */
92
+ export interface ResolvedLineAccount {
93
+ accountId: string;
94
+ enabled: boolean;
95
+ name?: string;
96
+ channelAccessToken: string;
97
+ channelSecret: string;
98
+ tokenSource: LineTokenSource;
99
+ configured: boolean;
100
+ config: LineAccountConfig;
101
+ }
102
+
103
+ /**
104
+ * Normalizes an account ID, returning the default if not provided
105
+ */
106
+ export function normalizeAccountId(accountId?: string | null): string {
107
+ if (!accountId || typeof accountId !== "string") {
108
+ return DEFAULT_ACCOUNT_ID;
109
+ }
110
+ const trimmed = accountId.trim().toLowerCase();
111
+ if (!trimmed || trimmed === "default") {
112
+ return DEFAULT_ACCOUNT_ID;
113
+ }
114
+ return trimmed;
115
+ }
116
+
117
+ /**
118
+ * Gets the multi-account configuration from runtime settings
119
+ */
120
+ export function getMultiAccountConfig(
121
+ runtime: IAgentRuntime,
122
+ ): LineMultiAccountConfig {
123
+ const characterLine = runtime.character?.settings?.line as
124
+ | LineMultiAccountConfig
125
+ | undefined;
126
+
127
+ return {
128
+ enabled: characterLine?.enabled,
129
+ channelAccessToken: characterLine?.channelAccessToken,
130
+ channelSecret: characterLine?.channelSecret,
131
+ tokenFile: characterLine?.tokenFile,
132
+ secretFile: characterLine?.secretFile,
133
+ dmPolicy: characterLine?.dmPolicy,
134
+ groupPolicy: characterLine?.groupPolicy,
135
+ mediaMaxMb: characterLine?.mediaMaxMb,
136
+ webhookPath: characterLine?.webhookPath,
137
+ accounts: characterLine?.accounts,
138
+ groups: characterLine?.groups,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Lists all configured account IDs
144
+ */
145
+ export function listLineAccountIds(runtime: IAgentRuntime): string[] {
146
+ const config = getMultiAccountConfig(runtime);
147
+ const accounts = config.accounts;
148
+ const ids = new Set<string>();
149
+
150
+ // Add default account if configured at base level
151
+ const envToken = runtime.getSetting("LINE_CHANNEL_ACCESS_TOKEN") as
152
+ | string
153
+ | undefined;
154
+ if (
155
+ config.channelAccessToken?.trim() ||
156
+ config.tokenFile ||
157
+ envToken?.trim()
158
+ ) {
159
+ ids.add(DEFAULT_ACCOUNT_ID);
160
+ }
161
+
162
+ // Add named accounts
163
+ if (accounts && typeof accounts === "object") {
164
+ for (const id of Object.keys(accounts)) {
165
+ if (id) {
166
+ ids.add(id);
167
+ }
168
+ }
169
+ }
170
+
171
+ const result = Array.from(ids);
172
+ if (result.length === 0) {
173
+ return [DEFAULT_ACCOUNT_ID];
174
+ }
175
+
176
+ return result.slice().sort((a: string, b: string) => a.localeCompare(b));
177
+ }
178
+
179
+ /**
180
+ * Resolves the default account ID to use
181
+ */
182
+ export function resolveDefaultLineAccountId(runtime: IAgentRuntime): string {
183
+ const ids = listLineAccountIds(runtime);
184
+ if (ids.includes(DEFAULT_ACCOUNT_ID)) {
185
+ return DEFAULT_ACCOUNT_ID;
186
+ }
187
+ return ids[0] ?? DEFAULT_ACCOUNT_ID;
188
+ }
189
+
190
+ /**
191
+ * Gets the account-specific configuration
192
+ */
193
+ function getAccountConfig(
194
+ runtime: IAgentRuntime,
195
+ accountId: string,
196
+ ): LineAccountConfig | undefined {
197
+ const config = getMultiAccountConfig(runtime);
198
+ const accounts = config.accounts;
199
+
200
+ if (!accounts || typeof accounts !== "object") {
201
+ return undefined;
202
+ }
203
+
204
+ return accounts[accountId];
205
+ }
206
+
207
+ /**
208
+ * Resolves the channel access token for a LINE account
209
+ */
210
+ export function resolveLineToken(
211
+ runtime: IAgentRuntime,
212
+ accountId: string,
213
+ ): LineTokenResolution {
214
+ const multiConfig = getMultiAccountConfig(runtime);
215
+ const accountConfig = getAccountConfig(runtime, accountId);
216
+
217
+ // Check account-level config first
218
+ if (accountConfig?.channelAccessToken?.trim()) {
219
+ return { token: accountConfig.channelAccessToken.trim(), source: "config" };
220
+ }
221
+
222
+ // For default account, check base config
223
+ if (accountId === DEFAULT_ACCOUNT_ID) {
224
+ if (multiConfig.channelAccessToken?.trim()) {
225
+ return { token: multiConfig.channelAccessToken.trim(), source: "config" };
226
+ }
227
+
228
+ // Check environment/runtime settings
229
+ const envToken = runtime.getSetting("LINE_CHANNEL_ACCESS_TOKEN") as
230
+ | string
231
+ | undefined;
232
+ if (envToken?.trim()) {
233
+ return { token: envToken.trim(), source: "env" };
234
+ }
235
+ }
236
+
237
+ return { token: "", source: "none" };
238
+ }
239
+
240
+ /**
241
+ * Resolves the channel secret for a LINE account
242
+ */
243
+ export function resolveLineSecret(
244
+ runtime: IAgentRuntime,
245
+ accountId: string,
246
+ ): string {
247
+ const multiConfig = getMultiAccountConfig(runtime);
248
+ const accountConfig = getAccountConfig(runtime, accountId);
249
+
250
+ // Check account-level config first
251
+ if (accountConfig?.channelSecret?.trim()) {
252
+ return accountConfig.channelSecret.trim();
253
+ }
254
+
255
+ // For default account, check base config and env
256
+ if (accountId === DEFAULT_ACCOUNT_ID) {
257
+ if (multiConfig.channelSecret?.trim()) {
258
+ return multiConfig.channelSecret.trim();
259
+ }
260
+
261
+ const envSecret = runtime.getSetting("LINE_CHANNEL_SECRET") as
262
+ | string
263
+ | undefined;
264
+ if (envSecret?.trim()) {
265
+ return envSecret.trim();
266
+ }
267
+ }
268
+
269
+ return "";
270
+ }
271
+
272
+ /**
273
+ * Merges base configuration with account-specific overrides
274
+ */
275
+ /**
276
+ * Removes undefined values from an object to prevent them from overwriting during spread
277
+ */
278
+ function filterDefined<T extends object>(obj: T): Partial<T> {
279
+ return Object.fromEntries(
280
+ Object.entries(obj).filter(([, v]) => v !== undefined),
281
+ ) as Partial<T>;
282
+ }
283
+
284
+ function mergeLineAccountConfig(
285
+ runtime: IAgentRuntime,
286
+ accountId: string,
287
+ ): LineAccountConfig {
288
+ const multiConfig = getMultiAccountConfig(runtime);
289
+ const { accounts: _ignored, ...baseConfig } = multiConfig;
290
+ const accountConfig = getAccountConfig(runtime, accountId) ?? {};
291
+
292
+ // Get environment/runtime settings for the base config
293
+ const envDmPolicy = runtime.getSetting("LINE_DM_POLICY") as
294
+ | string
295
+ | undefined;
296
+ const envGroupPolicy = runtime.getSetting("LINE_GROUP_POLICY") as
297
+ | string
298
+ | undefined;
299
+
300
+ const envConfig: LineAccountConfig = {
301
+ dmPolicy: envDmPolicy as LineAccountConfig["dmPolicy"] | undefined,
302
+ groupPolicy: envGroupPolicy as LineAccountConfig["groupPolicy"] | undefined,
303
+ };
304
+
305
+ // Merge order: env defaults < base config < account config
306
+ // Filter undefined values to prevent them from overwriting defined values
307
+ return {
308
+ ...filterDefined(envConfig),
309
+ ...filterDefined(baseConfig),
310
+ ...filterDefined(accountConfig),
311
+ };
312
+ }
313
+
314
+ /**
315
+ * Resolves a complete LINE account configuration
316
+ */
317
+ export function resolveLineAccount(
318
+ runtime: IAgentRuntime,
319
+ accountId?: string | null,
320
+ ): ResolvedLineAccount {
321
+ const normalizedAccountId = normalizeAccountId(accountId);
322
+ const multiConfig = getMultiAccountConfig(runtime);
323
+
324
+ const baseEnabled = multiConfig.enabled !== false;
325
+ const merged = mergeLineAccountConfig(runtime, normalizedAccountId);
326
+ const accountEnabled = merged.enabled !== false;
327
+ const enabled = baseEnabled && accountEnabled;
328
+
329
+ const { token, source: tokenSource } = resolveLineToken(
330
+ runtime,
331
+ normalizedAccountId,
332
+ );
333
+ const secret = resolveLineSecret(runtime, normalizedAccountId);
334
+
335
+ // Determine if this account is actually configured
336
+ const configured = Boolean(token || secret);
337
+
338
+ return {
339
+ accountId: normalizedAccountId,
340
+ enabled,
341
+ name: merged.name?.trim() || undefined,
342
+ channelAccessToken: token,
343
+ channelSecret: secret,
344
+ tokenSource,
345
+ configured,
346
+ config: merged,
347
+ };
348
+ }
349
+
350
+ /**
351
+ * Lists all enabled LINE accounts
352
+ */
353
+ export function listEnabledLineAccounts(
354
+ runtime: IAgentRuntime,
355
+ ): ResolvedLineAccount[] {
356
+ return listLineAccountIds(runtime)
357
+ .map((accountId) => resolveLineAccount(runtime, accountId))
358
+ .filter((account) => account.enabled && account.configured);
359
+ }
360
+
361
+ /**
362
+ * Checks if multi-account mode is enabled
363
+ */
364
+ export function isMultiAccountEnabled(runtime: IAgentRuntime): boolean {
365
+ const accounts = listEnabledLineAccounts(runtime);
366
+ return accounts.length > 1;
367
+ }
368
+
369
+ /**
370
+ * Resolves group configuration for a specific group
371
+ */
372
+ export function resolveLineGroupConfig(
373
+ runtime: IAgentRuntime,
374
+ accountId: string,
375
+ groupId: string,
376
+ ): LineGroupConfig | undefined {
377
+ const multiConfig = getMultiAccountConfig(runtime);
378
+ const accountConfig = getAccountConfig(runtime, accountId);
379
+
380
+ // Check account-level groups first
381
+ const accountGroup = accountConfig?.groups?.[groupId];
382
+ if (accountGroup) {
383
+ return accountGroup;
384
+ }
385
+
386
+ // Fall back to base-level groups
387
+ return multiConfig.groups?.[groupId];
388
+ }
389
+
390
+ /**
391
+ * Checks if a user is allowed based on policy and allowlist
392
+ */
393
+ export function isLineUserAllowed(params: {
394
+ userId: string;
395
+ accountConfig: LineAccountConfig;
396
+ isGroup: boolean;
397
+ groupId?: string;
398
+ groupConfig?: LineGroupConfig;
399
+ }): boolean {
400
+ const { userId, accountConfig, isGroup, groupConfig } = params;
401
+
402
+ if (isGroup) {
403
+ const policy = accountConfig.groupPolicy ?? "allowlist";
404
+ if (policy === "disabled") {
405
+ return false;
406
+ }
407
+
408
+ if (policy === "open") {
409
+ return true;
410
+ }
411
+
412
+ // Check group-specific allowlist first
413
+ if (groupConfig?.allowFrom?.length) {
414
+ return groupConfig.allowFrom.some(
415
+ (allowed) => String(allowed) === userId,
416
+ );
417
+ }
418
+
419
+ // Check account-level group allowlist
420
+ if (accountConfig.groupAllowFrom?.length) {
421
+ return accountConfig.groupAllowFrom.some(
422
+ (allowed) => String(allowed) === userId,
423
+ );
424
+ }
425
+
426
+ return policy !== "allowlist";
427
+ }
428
+
429
+ // DM handling
430
+ const policy = accountConfig.dmPolicy ?? "pairing";
431
+ if (policy === "disabled") {
432
+ return false;
433
+ }
434
+
435
+ if (policy === "open") {
436
+ return true;
437
+ }
438
+
439
+ if (policy === "pairing") {
440
+ return true;
441
+ }
442
+
443
+ // Allowlist policy
444
+ if (accountConfig.allowFrom?.length) {
445
+ return accountConfig.allowFrom.some(
446
+ (allowed) => String(allowed) === userId,
447
+ );
448
+ }
449
+
450
+ return false;
451
+ }
452
+
453
+ /**
454
+ * Checks if mention is required in a group
455
+ */
456
+ export function isLineMentionRequired(params: {
457
+ accountConfig: LineAccountConfig;
458
+ groupConfig?: LineGroupConfig;
459
+ }): boolean {
460
+ const { groupConfig } = params;
461
+ return groupConfig?.requireMention ?? false;
462
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * LINE plugin actions.
3
+ */
4
+
5
+ export { sendFlexMessage } from "./sendFlexMessage.js";
6
+ export { sendLocation } from "./sendLocation.js";
7
+ export { sendMessage } from "./sendMessage.js";