@zhin.js/adapter-line 0.1.1 → 1.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.
- package/CHANGELOG.md +596 -0
- package/README.md +54 -22
- package/adapters/line.js +31 -0
- package/adapters/line.ts +36 -0
- package/agent/tools/get_group_members.ts +16 -0
- package/agent/tools/get_profile.ts +16 -0
- package/commands/endpoint/add/[id].js +3 -0
- package/commands/endpoint/add/[id].ts +3 -0
- package/commands/endpoint/list.js +3 -0
- package/commands/endpoint/list.ts +3 -0
- package/commands/endpoint/remove/[id].js +3 -0
- package/commands/endpoint/remove/[id].ts +3 -0
- package/lib/client.d.ts +33 -0
- package/lib/client.js +54 -0
- package/lib/endpoint.d.ts +41 -37
- package/lib/endpoint.js +149 -500
- package/lib/index.d.ts +4 -15
- package/lib/index.js +4 -83
- package/lib/line-endpoint-commands.d.ts +1 -0
- package/lib/line-endpoint-commands.js +17 -0
- package/lib/line-runtime-state.d.ts +1 -0
- package/lib/line-runtime-state.js +6 -0
- package/lib/protocol.d.ts +158 -0
- package/lib/protocol.js +265 -0
- package/lib/side-event-dispatch.d.ts +4 -0
- package/lib/side-event-dispatch.js +42 -0
- package/lib/webhook.d.ts +13 -0
- package/lib/webhook.js +50 -0
- package/package.json +61 -21
- package/plugin.js +14 -0
- package/schema.json +86 -0
- package/src/client.ts +87 -0
- package/src/endpoint.ts +197 -551
- package/src/index.ts +51 -100
- package/src/line-endpoint-commands.ts +18 -0
- package/src/line-runtime-state.ts +7 -0
- package/src/protocol.ts +442 -0
- package/src/side-event-dispatch.ts +54 -0
- package/src/webhook.ts +79 -0
- package/lib/adapter.d.ts +0 -15
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -20
- package/lib/adapter.js.map +0 -1
- package/lib/endpoint.d.ts.map +0 -1
- package/lib/endpoint.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -112
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -5
- package/lib/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -29
- package/src/types.ts +0 -130
- /package/{skills/line → agent}/PERMITS.md +0 -0
- /package/{skills/line/SKILL.md → agent/skills/line.md} +0 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/adapter-line",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Zhin.js adapter for
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Zhin.js LINE Messaging API adapter for Plugin Runtime (HTTP webhook)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
@@ -31,34 +31,53 @@
|
|
|
31
31
|
"type": "git",
|
|
32
32
|
"directory": "plugins/adapters/line"
|
|
33
33
|
},
|
|
34
|
-
"
|
|
35
|
-
"@
|
|
36
|
-
"
|
|
37
|
-
"@zhin.js/
|
|
38
|
-
"@zhin.js/host-
|
|
39
|
-
"@zhin.js/
|
|
40
|
-
"@zhin.js/logger": "
|
|
41
|
-
"zhin.js": "2.0.1"
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@zhin.js/adapter": "1.1.12",
|
|
36
|
+
"@zhin.js/core": "1.1.35",
|
|
37
|
+
"@zhin.js/feature-kit": "1.1.0",
|
|
38
|
+
"@zhin.js/host-http": "1.1.0",
|
|
39
|
+
"@zhin.js/im-contract": "1.1.0",
|
|
40
|
+
"@zhin.js/logger": "1.1.0"
|
|
42
41
|
},
|
|
43
42
|
"peerDependencies": {
|
|
44
|
-
"
|
|
45
|
-
"@zhin.js/
|
|
46
|
-
"@zhin.js/
|
|
47
|
-
"zhin.js": "
|
|
43
|
+
"zod": "^4.0.0",
|
|
44
|
+
"@zhin.js/adapter": "^1.1.12",
|
|
45
|
+
"@zhin.js/agent": "^1.1.23",
|
|
46
|
+
"@zhin.js/command": "^1.1.0",
|
|
47
|
+
"@zhin.js/core": "^1.1.35",
|
|
48
|
+
"@zhin.js/host-http": "^1.1.0",
|
|
49
|
+
"zhin.js": "^1.1.0"
|
|
48
50
|
},
|
|
49
51
|
"peerDependenciesMeta": {
|
|
50
|
-
"@zhin.js/
|
|
52
|
+
"@zhin.js/agent": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"@zhin.js/command": {
|
|
56
|
+
"optional": true
|
|
57
|
+
},
|
|
58
|
+
"zhin.js": {
|
|
51
59
|
"optional": true
|
|
52
60
|
},
|
|
53
|
-
"
|
|
61
|
+
"zod": {
|
|
54
62
|
"optional": true
|
|
55
63
|
}
|
|
56
64
|
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@types/node": "^26.1.2",
|
|
67
|
+
"typescript": "^6.0.3",
|
|
68
|
+
"vitest": "^4.1.10",
|
|
69
|
+
"zod": "^4.4.3",
|
|
70
|
+
"@zhin.js/agent": "1.1.23",
|
|
71
|
+
"zhin.js": "1.1.0"
|
|
72
|
+
},
|
|
57
73
|
"files": [
|
|
74
|
+
"adapters",
|
|
75
|
+
"commands",
|
|
76
|
+
"plugin.js",
|
|
77
|
+
"schema.json",
|
|
58
78
|
"src",
|
|
59
79
|
"lib",
|
|
60
|
-
"
|
|
61
|
-
"plugin.yml",
|
|
80
|
+
"agent",
|
|
62
81
|
"README.md",
|
|
63
82
|
"CHANGELOG.md"
|
|
64
83
|
],
|
|
@@ -66,9 +85,30 @@
|
|
|
66
85
|
"access": "public",
|
|
67
86
|
"registry": "https://registry.npmjs.org"
|
|
68
87
|
},
|
|
88
|
+
"engines": {
|
|
89
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
90
|
+
},
|
|
91
|
+
"zhin": {
|
|
92
|
+
"protocol": 1,
|
|
93
|
+
"type": "plugin",
|
|
94
|
+
"entry": "./plugin.js",
|
|
95
|
+
"engine": "^1.0.0",
|
|
96
|
+
"runtime": "trusted",
|
|
97
|
+
"features": [
|
|
98
|
+
{
|
|
99
|
+
"package": "@zhin.js/adapter",
|
|
100
|
+
"api": "^1.0.0"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"package": "@zhin.js/command",
|
|
104
|
+
"api": "^1.0.0"
|
|
105
|
+
}
|
|
106
|
+
],
|
|
107
|
+
"plugins": []
|
|
108
|
+
},
|
|
69
109
|
"scripts": {
|
|
70
|
-
"build": "
|
|
71
|
-
"clean": "rimraf lib
|
|
72
|
-
"
|
|
110
|
+
"build": "tsc",
|
|
111
|
+
"clean": "rimraf lib",
|
|
112
|
+
"test": "NODE_OPTIONS=--experimental-strip-types vitest run --root ../../.. plugins/adapters/line/tests"
|
|
73
113
|
}
|
|
74
114
|
}
|
package/plugin.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
+
import { createEndpointRuntimeState } from 'zhin.js/adapter';
|
|
3
|
+
import { definePlugin } from 'zhin.js';
|
|
4
|
+
import { lineRuntimeStateToken } from "./lib/line-runtime-state.js";
|
|
5
|
+
export default definePlugin({
|
|
6
|
+
name: 'line',
|
|
7
|
+
metadata: {
|
|
8
|
+
displayName: 'LINE Messaging API Adapter',
|
|
9
|
+
},
|
|
10
|
+
setup(context) {
|
|
11
|
+
// 运行中 endpoint 注册表(line.endpoint list 的"运行中"数据源)
|
|
12
|
+
context.resources.provide(lineRuntimeStateToken, createEndpointRuntimeState());
|
|
13
|
+
},
|
|
14
|
+
});
|
package/schema.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"type": "object",
|
|
4
|
+
"additionalProperties": false,
|
|
5
|
+
"properties": {
|
|
6
|
+
"webhookPath": {
|
|
7
|
+
"type": "string",
|
|
8
|
+
"default": "/line/webhook"
|
|
9
|
+
},
|
|
10
|
+
"apiBaseUrl": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"default": "https://api.line.me"
|
|
13
|
+
},
|
|
14
|
+
"master": {
|
|
15
|
+
"type": [
|
|
16
|
+
"string",
|
|
17
|
+
"number"
|
|
18
|
+
],
|
|
19
|
+
"description": "框架 master(LINE user id;AI/工具权限、endpoint 管理)。endpoints[i].master 可逐项覆盖"
|
|
20
|
+
},
|
|
21
|
+
"trusted": {
|
|
22
|
+
"type": "array",
|
|
23
|
+
"items": {
|
|
24
|
+
"type": [
|
|
25
|
+
"string",
|
|
26
|
+
"number"
|
|
27
|
+
],
|
|
28
|
+
"description": "Trusted LINE user id"
|
|
29
|
+
},
|
|
30
|
+
"description": "框架 trusted 用户列表(弱于 master)。endpoints[i].trusted 可逐项追加"
|
|
31
|
+
},
|
|
32
|
+
"endpoints": {
|
|
33
|
+
"type": "array",
|
|
34
|
+
"description": "多账号:一个插件实例挂多个 endpoint。每项与顶层字段同构(id 必填,其余覆盖顶层)",
|
|
35
|
+
"items": {
|
|
36
|
+
"type": "object",
|
|
37
|
+
"additionalProperties": true,
|
|
38
|
+
"properties": {
|
|
39
|
+
"master": {
|
|
40
|
+
"type": [
|
|
41
|
+
"string",
|
|
42
|
+
"number"
|
|
43
|
+
],
|
|
44
|
+
"description": "本 endpoint 的框架 master(LINE user id);覆盖顶层 master"
|
|
45
|
+
},
|
|
46
|
+
"trusted": {
|
|
47
|
+
"type": "array",
|
|
48
|
+
"items": {
|
|
49
|
+
"type": [
|
|
50
|
+
"string",
|
|
51
|
+
"number"
|
|
52
|
+
],
|
|
53
|
+
"description": "Trusted LINE user id"
|
|
54
|
+
},
|
|
55
|
+
"description": "本 endpoint 的 trusted 列表"
|
|
56
|
+
},
|
|
57
|
+
"channelSecret": {
|
|
58
|
+
"type": "string",
|
|
59
|
+
"description": "LINE channel secret"
|
|
60
|
+
},
|
|
61
|
+
"channelAccessToken": {
|
|
62
|
+
"type": "string",
|
|
63
|
+
"description": "LINE channel access token"
|
|
64
|
+
},
|
|
65
|
+
"id": {
|
|
66
|
+
"type": "string",
|
|
67
|
+
"description": "LINE bot name"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"required": [
|
|
71
|
+
"id",
|
|
72
|
+
"channelSecret",
|
|
73
|
+
"channelAccessToken"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"commandPrefix": {
|
|
78
|
+
"type": "string",
|
|
79
|
+
"default": "",
|
|
80
|
+
"description": "命令前缀(默认 '' 无前缀,任意文本按命令匹配;如 '/' 要求 / 开头)。endpoints[i] 可逐项覆盖"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"required": [
|
|
84
|
+
"endpoints"
|
|
85
|
+
]
|
|
86
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { LineFetch } from './endpoint.js';
|
|
2
|
+
import { defineEndpointClient } from 'zhin.js/adapter';
|
|
3
|
+
import type { LineEvent, ResolvedLineConfig } from './protocol.js';
|
|
4
|
+
|
|
5
|
+
export interface LineGroupMember {
|
|
6
|
+
readonly user_id: string;
|
|
7
|
+
readonly nickname: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Direct LINE Messaging API client; it has no Endpoint lifecycle methods. */
|
|
11
|
+
export class LineClient {
|
|
12
|
+
constructor(
|
|
13
|
+
readonly config: ResolvedLineConfig,
|
|
14
|
+
readonly fetch: LineFetch,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
async request<T = unknown>(path: string, init: {
|
|
18
|
+
readonly method?: string;
|
|
19
|
+
readonly body?: string;
|
|
20
|
+
readonly signal?: AbortSignal;
|
|
21
|
+
} = {}): Promise<T> {
|
|
22
|
+
const response = await this.fetch(`${this.config.apiBaseUrl}${path}`, {
|
|
23
|
+
method: init.method ?? 'GET',
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `Bearer ${this.config.channelAccessToken}`,
|
|
26
|
+
...(init.body === undefined ? {} : { 'Content-Type': 'application/json' }),
|
|
27
|
+
},
|
|
28
|
+
...(init.body === undefined ? {} : { body: init.body }),
|
|
29
|
+
signal: init.signal ?? AbortSignal.timeout(30_000),
|
|
30
|
+
});
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
const text = await response.text();
|
|
33
|
+
throw new Error(`LINE API error ${response.status}: ${text}`);
|
|
34
|
+
}
|
|
35
|
+
return await response.json() as T;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getProfile(userId: string): Promise<unknown> {
|
|
39
|
+
return this.request(`/v2/profile/${encodeURIComponent(userId)}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
getGroupMemberIds(groupId: string, start?: string): Promise<{
|
|
43
|
+
readonly memberIds?: string[];
|
|
44
|
+
readonly next?: string;
|
|
45
|
+
}> {
|
|
46
|
+
const kind = groupId.startsWith('R') ? 'room' : 'group';
|
|
47
|
+
const query = start ? `?start=${encodeURIComponent(start)}` : '';
|
|
48
|
+
return this.request(
|
|
49
|
+
`/v2/bot/${kind}/${encodeURIComponent(groupId)}/members/ids${query}`,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async getGroupMembers(groupId: string): Promise<LineGroupMember[]> {
|
|
54
|
+
const kind = groupId.startsWith('R') ? 'room' : 'group';
|
|
55
|
+
const ids: string[] = [];
|
|
56
|
+
let next: string | undefined;
|
|
57
|
+
do {
|
|
58
|
+
const page = await this.getGroupMemberIds(groupId, next);
|
|
59
|
+
ids.push(...(page.memberIds ?? []).filter((id) => typeof id === 'string' && id.length > 0));
|
|
60
|
+
next = page.next || undefined;
|
|
61
|
+
} while (next);
|
|
62
|
+
|
|
63
|
+
return Promise.all(ids.map(async (userId) => {
|
|
64
|
+
try {
|
|
65
|
+
const profile = await this.request<{ displayName?: string }>(
|
|
66
|
+
`/v2/bot/${kind}/${encodeURIComponent(groupId)}/member/${encodeURIComponent(userId)}`,
|
|
67
|
+
);
|
|
68
|
+
return { user_id: userId, nickname: String(profile.displayName ?? userId) };
|
|
69
|
+
} catch {
|
|
70
|
+
return { user_id: userId, nickname: userId };
|
|
71
|
+
}
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type LineClientEventMap = Record<string, LineEvent>;
|
|
77
|
+
|
|
78
|
+
declare module '@zhin.js/feature-kit' {
|
|
79
|
+
interface AdapterClientRegistry {
|
|
80
|
+
readonly line: {
|
|
81
|
+
readonly client: LineClient;
|
|
82
|
+
readonly events: LineClientEventMap;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export const lineClient = defineEndpointClient<LineClient, LineClientEventMap>('line');
|