@42ailab/42plugin 0.1.0-beta.1 → 0.1.5

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/README.md CHANGED
@@ -1,101 +1,244 @@
1
1
  # 42plugin CLI
2
2
 
3
- 活水插件,支持搜索、安装、管理来自 42plugin 平台的能力、插件与插件列表。
3
+ 活水插件命令行工具,支持搜索、安装、管理来自 42plugin 平台的插件。
4
+
5
+ **扁平化架构**:每个 plugin 就是一个 capability (1:1),简化数据模型。
4
6
 
5
7
  ## 前提条件
6
- - 需要安装 [Bun](https://bun.sh)(用于运行与构建)。
7
- - Node.js/npm 非必需;依赖通过 `bun install` 处理。
8
8
 
9
- ## 安装依赖
9
+ - 需要安装 [Bun](https://bun.sh)(用于运行与构建)
10
+ - Node.js/npm 非必需,依赖通过 `bun install` 处理
11
+
12
+ ## 快速开始
13
+
10
14
  ```bash
15
+ # 安装依赖
11
16
  bun install
17
+
18
+ # 开发运行
19
+ bun run dev -- <command> [options]
20
+
21
+ # 或直接运行
22
+ bun run src/index.ts <command> [options]
23
+
24
+ # 构建跨平台可执行文件
25
+ bun run build
26
+ # 输出位于 dist/ 下
27
+ ```
28
+
29
+ ## 技术栈
30
+
31
+ | 技术 | 版本 | 说明 |
32
+ |------|------|------|
33
+ | TypeScript | 5.7.0 | 类型安全 |
34
+ | Bun | 1.0+ | 运行时和包管理 |
35
+ | Commander.js | 13.0.0 | CLI 框架 |
36
+ | @libsql/client | 0.14.0 | SQLite 数据库 |
37
+ | better-auth | 1.4.6 | 认证客户端 |
38
+ | chalk | 5.4.1 | 终端颜色 |
39
+ | ora | 8.1.1 | 加载动画 |
40
+
41
+ ## 目录结构
42
+
43
+ ```
44
+ 42plugin-cli/
45
+ ├── src/
46
+ │ ├── index.ts # 入口文件
47
+ │ ├── cli.ts # 命令定义
48
+ │ ├── config.ts # 配置管理
49
+ │ ├── commands/ # CLI 子命令
50
+ │ │ ├── auth.ts # 登录/授权/登出
51
+ │ │ ├── search.ts # 搜索
52
+ │ │ ├── install.ts # 安装
53
+ │ │ ├── list.ts # 查看已安装
54
+ │ │ ├── uninstall.ts # 卸载
55
+ │ │ ├── version.ts # 版本
56
+ │ │ └── publish.ts # 发布(开发中)
57
+ │ ├── services/ # 业务逻辑
58
+ │ │ ├── api.ts # API 客户端
59
+ │ │ ├── auth.ts # 凭证管理
60
+ │ │ ├── download.ts # 下载处理
61
+ │ │ ├── cache.ts # 缓存管理
62
+ │ │ ├── link.ts # 符号链接
63
+ │ │ └── project.ts # 项目注册
64
+ │ ├── db/
65
+ │ │ └── client.ts # 数据库客户端
66
+ │ ├── types/ # 类型定义
67
+ │ └── utils/ # 工具函数
68
+ ├── tests/ # 测试用例
69
+ └── scripts/ # 构建脚本
70
+ ```
71
+
72
+ ## CLI 命令
73
+
74
+ ### 认证
75
+
76
+ 使用 Better Auth Device Authorization Flow (RFC 8628):
77
+
78
+ ```bash
79
+ 42plugin auth # 浏览器授权登录
80
+ 42plugin auth --status # 查看登录状态
81
+ 42plugin auth --logout # 登出并清除本地凭证
12
82
  ```
13
83
 
14
- ## 本地开发与运行
15
- - 直接运行入口(便于调试):
16
- ```bash
17
- bun run src/index.ts <command> [options]
18
- ```
19
- - 或使用脚本(等同于上方):
20
- ```bash
21
- bun run dev -- <command> [options]
22
- ```
23
- - 构建跨平台可执行文件:
24
- ```bash
25
- bun run build
26
- # 输出位于 dist/ 下,如 dist/42plugin-darwin-arm64、dist/42plugin-linux-x64 等
27
- ```
28
- 构建后二进制即可直接执行,例如 `./dist/42plugin-darwin-arm64 search claude`。
29
-
30
- ## CLI 使用
31
- ### 登录
32
- - 浏览器授权登录:`42plugin auth`
33
- - 查看状态:`42plugin auth --status`
34
- - 登出并清理凭证:`42plugin auth --logout`
35
-
36
- ### 搜索插件/能力/列表
84
+ ### 搜索
85
+
37
86
  ```bash
38
- 42plugin search <关键词> [--type skill|agent|command|hook|list] [--limit 50] [--json]
87
+ 42plugin search <关键词> [--type skill|agent|command|hook|kit] [--limit 20] [--json]
39
88
  ```
40
89
 
41
90
  ### 安装
42
- 支持的安装目标格式:
43
- - 能力:`owner/repo:plugin:type:name`
44
- - 插件(带插件名):`owner/repo:plugin`
45
- - 简化插件:`owner/plugin`
46
- - 列表:`owner/list/<slug>`
47
91
 
48
- 常用示例:
92
+ 安装格式:
93
+
94
+ | 格式 | 说明 |
95
+ |------|------|
96
+ | `author/name` | 安装插件 |
97
+ | `author/kit/slug` | 安装套包 |
98
+
49
99
  ```bash
50
- 42plugin install user/repo:cool-plugin
51
- 42plugin install user/repo:cool-plugin:command:deploy
52
- 42plugin install user/list/tools # 安装列表(默认只装必选项)
53
- 42plugin install user/list/tools --optional # 包含列表中的可选项
100
+ # 安装插件
101
+ 42plugin install alice/smart-reviewer
102
+
103
+ # 安装套包
104
+ 42plugin install user/kit/tools # 仅安装必选项
105
+ 42plugin install user/kit/tools --optional # 包含可选项
106
+
107
+ # 安装某个插件所属的整个套包
108
+ 42plugin install alice/smart-reviewer -k
54
109
  ```
55
110
 
56
111
  选项:
57
- - `-g, --global`:仅下载到缓存,不链接到当前项目。
58
- - `--force`:忽略缓存,强制重新下载。
59
- - `--no-cache`:跳过缓存命中检查。
60
- - `--optional`:安装列表时包含可选能力(默认只安装必选)。
112
+ - `-g, --global`:仅下载到缓存,不链接到当前项目
113
+ - `-k, --kit`:安装该插件所属的整个套包
114
+ - `--optional`:安装套包时包含可选项
115
+ - `--force`:强制重新下载
116
+ - `--no-cache`:跳过缓存命中检查
117
+ - `--from <source>`:指定来源套件
61
118
 
62
- 安装会将包解压到本地缓存,再在当前工作目录创建链接到具体安装路径(例如 `.claude/` 下),同时在本地数据库记录。请在项目根目录执行。
119
+ ### 管理
63
120
 
64
- ### 查看已安装
65
121
  ```bash
66
- 42plugin list [--type <type>] [--json]
122
+ 42plugin list # 查看已安装
123
+ 42plugin list --type skill --json
124
+ 42plugin uninstall <full_name> # 卸载
125
+ 42plugin uninstall <full_name> --purge
126
+ 42plugin version # 版本
67
127
  ```
68
128
 
69
- ### 卸载
129
+ ### 发布(开发中)
130
+
70
131
  ```bash
71
- 42plugin uninstall <full_name> [--purge]
132
+ 42plugin publish [path] # 发布插件
133
+ 42plugin publish --dry-run # 仅检查,不实际发布
72
134
  ```
73
- `--purge` 会同时删除缓存内容。
74
135
 
75
- ### 查看版本
136
+ 发布前置条件:
137
+ - 用户必须登录
138
+ - 用户必须验证手机号
139
+ - 用户必须设置 username
140
+
141
+ ## 数据存储
142
+
143
+ | 类型 | 路径 |
144
+ |------|------|
145
+ | 数据库 | `~/.42plugin/local.db`(Windows: `%APPDATA%/42plugin/local.db`) |
146
+ | 缓存 | `~/.42plugin/cache` |
147
+ | 登录凭证 | `~/.42plugin/secrets.json`(权限 600) |
148
+ | 项目链接 | `.claude/`(项目目录下) |
149
+
150
+ ## 本地数据库表(SQLite)
151
+
152
+ | 表名 | 说明 |
153
+ |------|------|
154
+ | `projects` | 本地项目注册(id, path, name, registered_at, last_used_at)|
155
+ | `plugin_cache` | 插件下载缓存(full_name, type, version, cache_path, checksum)|
156
+ | `project_plugins` | 项目已安装的插件(project_id, full_name, type, version, link_path, source)|
157
+ | `config` | 配置项(key-value)|
158
+
159
+ 注意:这些表仅在 SQLite 模式下创建,PostgreSQL 模式会跳过以避免污染云端数据库。
160
+
161
+ ## 环境变量
162
+
76
163
  ```bash
77
- 42plugin version
164
+ # API 配置
165
+ API_BASE_URL=https://42plugin.com/api
166
+ CDN_BASE=https://cdn.42plugin.com
167
+
168
+ # 调试
169
+ DEBUG=true
170
+
171
+ # 数据库(可选,使用 PostgreSQL 替代默认 SQLite)
172
+ CLI_DB_DRIVER=postgres
173
+ CLI_DATABASE_URL=postgresql://user@localhost:5432/42plugin_local
78
174
  ```
79
175
 
80
- ## 数据存储位置
81
- - 配置与数据库:`~/.42plugin/local.db`(Windows 为 `%APPDATA%/42plugin/local.db`)
82
- - 缓存:`~/.42plugin/cache`
83
- - 登录凭证:`~/.42plugin/secrets.json`(权限 600)
176
+ ## 认证流程
177
+
178
+ ```
179
+ CLI API Browser
180
+ │ │ │
181
+ │─────POST /api/auth/device/code─────>│ │
182
+ │<────{device_code, user_code}────────│ │
183
+ │ │ │
184
+ │ 显示 user_code 和 verification_uri │ │
185
+ │───────────────────────────────────────────────────────────>│
186
+ │ │ 用户输入 user_code 并授权 │
187
+ │ │<──────────────────────────────────│
188
+ │ │ │
189
+ │─────POST /api/auth/device/token──>│ │
190
+ │ (轮询,5s 间隔) │ │
191
+ │<────{access_token, user}─────────│ │
192
+ │ │ │
193
+ │ 保存到 secrets.json │ │
194
+ ```
195
+
196
+ ## 安装流程
197
+
198
+ ```
199
+ 1. 解析安装目标格式(utils/target.ts)
200
+ - author/name → TargetType.Plugin
201
+ - author/kit/slug → TargetType.Kit
202
+
203
+ 2. 从 API 获取下载信息
204
+ - GET /v1/plugins/{author}/{name}/download
205
+ - GET /v1/kits/{username}/kit/{slug}/download
206
+
207
+ 3. 检查本地缓存(plugin_cache 表)
208
+
209
+ 4. 从 CDN 下载 tarball
210
+
211
+ 5. 验证 SHA256 校验和
212
+
213
+ 6. 解压到缓存目录 (~/.42plugin/cache)
214
+
215
+ 7. 创建符号链接到项目 .claude/ 目录
216
+
217
+ 8. 记录到本地数据库(project_plugins 表)
218
+ ```
219
+
220
+ ## API 端点
221
+
222
+ ### 认证 & 用户
223
+
224
+ | 端点 | 方法 | 说明 |
225
+ |------|------|------|
226
+ | `/api/auth/device/code` | POST | 请求 device code |
227
+ | `/api/auth/device/token` | POST | 轮询 token |
228
+ | `/api/auth/get-session` | GET | 获取用户会话 |
229
+ | `/api/user/installed` | POST | 同步安装记录 |
230
+ | `/api/user/installed/{author}/{name}` | DELETE | 同步卸载记录 |
231
+
232
+ ### 业务 API
233
+
234
+ | 端点 | 方法 | 说明 |
235
+ |------|------|------|
236
+ | `/v1/plugins/{author}/{name}/download` | GET | 获取插件下载信息 |
237
+ | `/v1/kits/{username}/kit/{slug}/download` | GET | 获取套包下载信息 |
238
+ | `/v1/search?q=` | GET | 搜索 |
239
+
240
+ ## 开发命令
84
241
 
85
- ## 环境变量
86
- - `API_BASE`:替换默认的 API 地址(默认 `https://api.42plugin.com`)。
87
- - `CDN_BASE`:替换默认的 CDN 地址(默认 `https://cdn.42plugin.com`)。
88
- - `DEBUG=true`:输出调试信息与原始错误。
89
- - `CLI_DB_DRIVER=postgres`:让 CLI 使用 Postgres 而非默认的本地 SQLite(`~/.42plugin/local.db`)。配合 `CLI_DATABASE_URL` 或 `LOCAL_DATABASE_URL` 指定连接串,例如 `postgresql://postgres:password@localhost:5432/42plugin_cli_dev`。
90
-
91
- ## 项目结构速览
92
- - `src/cli.ts`:命令定义与统一入口。
93
- - `src/commands/*`:各子命令实现(auth、install、search、list、uninstall、version)。
94
- - `src/services/*`:API、缓存、下载、项目记录、链接创建等核心逻辑。
95
- - `src/db/client.ts`:本地 SQLite 初始化与连接(基于 @libsql/client)。
96
- - `tests/`:命令与服务的测试用例。
97
-
98
- ## 常用开发命令
99
242
  ```bash
100
243
  bun test # 运行测试
101
244
  bun run lint # ESLint
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.1.0-beta.1",
3
+ "version": "0.1.5",
4
4
  "description": "活水插件",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -12,12 +12,12 @@
12
12
  "README.md"
13
13
  ],
14
14
  "scripts": {
15
- "dev": "bun run --env-file=../.env --env-file=.env src/index.ts",
16
- "build": "bun run --env-file=../.env --env-file=.env scripts/build.ts",
17
- "test": "bun run --env-file=../.env --env-file=.env test",
18
- "lint": "eslint --env-file=../.env --env-file=.env src/",
19
- "format": "prettier --write --env-file=../.env --env-file=.env src/",
20
- "typecheck": "tsc --noEmit --env-file=../.env --env-file=.env"
15
+ "dev": "bun run --env-file=../../.env --env-file=.env src/index.ts",
16
+ "build": "bun run --env-file=../../.env --env-file=.env scripts/build.ts",
17
+ "test": "bun run --env-file=../../.env --env-file=.env test",
18
+ "lint": "eslint --env-file=../../.env --env-file=.env src/",
19
+ "format": "prettier --write --env-file=../../.env --env-file=.env src/",
20
+ "typecheck": "tsc --noEmit --env-file=../../.env --env-file=.env"
21
21
  },
22
22
  "keywords": [
23
23
  "claude",
@@ -41,9 +41,13 @@
41
41
  "node": ">=18.0.0"
42
42
  },
43
43
  "dependencies": {
44
+ "@inquirer/prompts": "^8.1.0",
44
45
  "@libsql/client": "^0.14.0",
46
+ "better-auth": "^1.4.6",
45
47
  "chalk": "^5.4.1",
48
+ "cli-progress": "^3.12.0",
46
49
  "commander": "^13.0.0",
50
+ "gray-matter": "^4.0.3",
47
51
  "nanoid": "^5.0.9",
48
52
  "open": "^10.1.0",
49
53
  "ora": "^8.1.1",
@@ -52,6 +56,7 @@
52
56
  },
53
57
  "devDependencies": {
54
58
  "@types/bun": "latest",
59
+ "@types/cli-progress": "^3.11.6",
55
60
  "@types/tar": "^6.1.13",
56
61
  "typescript": "^5.7.0"
57
62
  }