@zhin.js/adapter-github 0.1.59 → 0.1.61

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 (2) hide show
  1. package/README.md +28 -44
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -44,9 +44,7 @@ pnpm add @zhin.js/adapter-github
44
44
  - Issue comment、Pull request、Pull request review、Pull request review comment
45
45
  - Push、Star、Fork(用于通知)
46
46
  6. 创建后记录 **App ID**,点击 **Generate a private key** 下载 `.pem` 文件
47
- 7. 如需 OAuth 用户绑定功能,记录 **Client ID** 和 **Client Secret**(在 App 设置页的 General 中)
48
- 8. 在 App 设置页配置 **Callback URL**: `https://your-domain/pub/github/oauth/callback`
49
- 9. 安装 App 到目标仓库/组织
47
+ 7. 安装 App 到目标仓库/组织
50
48
 
51
49
  ## 配置
52
50
 
@@ -57,25 +55,21 @@ bots:
57
55
  name: my-github-bot
58
56
  app_id: 123456
59
57
  private_key: ./data/github-app.pem # PEM 文件路径或直接粘贴内容
60
- # installation_id: 78901234 # 可选,不填则自动获取第一个
61
- # webhook_secret: your-secret # 可选,Webhook 签名验证
62
- # client_id: Iv1.xxxxxxxxxx # 可选,OAuth 用户绑定
63
- # client_secret: xxxxxxxxxxxx # 可选,配合 client_id 使用
64
- # public_url: https://bot.example.com # 可选,OAuth 回调和绑定链接的公开地址
58
+ webhook_secret: your-secret # 可选;配置后启用 Webhook
59
+ webhook_path: /pub/github/webhook # 可选;默认 /github/webhook,/pub/* 可绕过 Bearer
65
60
 
66
61
  plugins:
62
+ - database
67
63
  - "@zhin.js/adapter-github"
68
- - "@zhin.js/host-router" # 可选,提供 Webhook + OAuth 能力
64
+ - "@zhin.js/host-router" # Webhook 需要
69
65
  ```
70
66
 
71
- `.env` 文件:
72
67
  ```env
73
68
  GITHUB_APP_ID=123456
74
69
  GITHUB_WEBHOOK_SECRET=your-secret
75
- GITHUB_CLIENT_ID=Iv1.xxxxxxxxxx
76
- GITHUB_CLIENT_SECRET=xxxxxxxxxxxx
77
70
  # MCP server-github(适配器自动 addMcp,与 App 认证独立)
78
71
  GITHUB_PERSONAL_ACCESS_TOKEN=ghp_xxxxxxxxxxxx
72
+ # 或 ai.githubMcp.token / ai.mcpServers
79
73
  ```
80
74
 
81
75
  或在 `zhin.config.yml` 中设置 `ai.githubMcp.token`(优先于环境变量)。
@@ -150,9 +144,9 @@ Headers:
150
144
  - `X-GitHub-Event`: 事件类型
151
145
  - `X-Hub-Signature-256`: 签名(配置了 secret 时)
152
146
 
153
- ## OAuth 用户绑定
147
+ ## OAuth 用户绑定(Device Flow)
154
148
 
155
- 支持聊天平台用户绑定自己的 GitHub 账号。绑定后,star/fork 等个人操作将以用户自己的身份执行。
149
+ 用户通过工具 **`github_bind`** 绑定 GitHub 账号([OAuth Device Flow](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#device-flow)),**不是**浏览器 OAuth 回调页面。
156
150
 
157
151
  ### 认证策略
158
152
 
@@ -162,31 +156,20 @@ Headers:
162
156
  | 评论(bot 发言) | App JWT | 以机器人身份发言 |
163
157
  | star / unstar | User OAuth | 个人行为 |
164
158
  | fork | User OAuth | 个人行为 |
165
- | 创建 issue / PR | 视场景选择 | 已绑定用用户 token,未绑定用 App |
166
159
 
167
160
  ### 绑定流程
168
161
 
169
- 1. 用户在聊天中发送"绑定 GitHub 账号"或触发 `github.bind` 工具
170
- 2. Bot 返回一个带有 `state` OAuth 授权链接
171
- 3. 用户点击链接 浏览器跳转到 GitHub 授权页面
172
- 4. 用户授权后 GitHub 回调到 `/pub/github/oauth/callback`
173
- 5. 服务端交换 code → access_token,获取用户信息,存入数据库
174
- 6. 用户看到"绑定成功"页面
162
+ 1. 用户在聊天中触发 `github_bind`(或说「绑定 GitHub」)
163
+ 2. Bot 返回 **verification URI** **user code**(GitHub Device Flow)
164
+ 3. 用户在浏览器打开链接并输入 code 完成授权
165
+ 4. Token 写入 `github_oauth_users` 表;可用 `github_whoami` / `github_unbind` 管理
175
166
 
176
- ### OAuth 端点
177
-
178
- | 路由 | 说明 |
179
- |------|------|
180
- | `GET /pub/github/oauth?state=xxx` | 发起 GitHub 授权重定向 |
181
- | `GET /pub/github/oauth/callback?code=xxx&state=xxx` | GitHub 回调,完成绑定 |
182
-
183
- > OAuth 路由注册在 `/github/` 而非 `/api/` 下,因此不受 API Token 认证限制,用户浏览器可直接访问。
167
+ **Client ID**:从 GitHub App 的 `/app` API 自动获取(`GhClient.getClientId()`),**无需**在 bot 配置里写 `client_id` / `client_secret`。
184
168
 
185
169
  ### 前置条件
186
170
 
187
- - 需要配置 `client_id` 和 `client_secret`
188
- - 需要 `@zhin.js/host-router` 插件(提供 HTTP 路由)
189
- - GitHub App 设置页中的 Callback URL 须指向 `https://your-domain/pub/github/oauth/callback`
171
+ - 需要 **`database`** 插件(`github_oauth_users` 模型)
172
+ - Device Flow 在 IM 内完成,**不依赖** `/pub/github/oauth` HTTP 路由
190
173
 
191
174
  ## 数据库表
192
175
 
@@ -216,29 +199,30 @@ Headers:
216
199
  | 字段 | 类型 | 说明 |
217
200
  |------|------|------|
218
201
  | id | integer | 主键 |
219
- | platform | text | 聊天平台 (icqq / kook / discord) |
220
- | platform_uid | text | 聊天平台用户 ID |
202
+ | platform | text | 聊天平台 |
203
+ | platform_uid | text | 平台用户 ID |
221
204
  | github_login | text | GitHub 用户名 |
222
- | github_id | integer | GitHub 用户 ID |
223
- | access_token | text | OAuth access_token |
224
- | scope | text | 授权范围 |
225
- | created_at | date | 创建时间 |
226
- | updated_at | date | 更新时间 |
205
+ | access_token | text | Device Flow 获得的 token |
206
+ | created_at | integer | 创建时间戳 |
227
207
 
228
208
  ## 架构说明
229
209
 
230
210
  ```
231
211
  adapter-github/
232
212
  ├── src/
233
- │ ├── index.ts # GitHubBot + GitHubAdapter + 工具注册 + Webhook
234
- │ ├── api.ts # GitHub REST API 客户端 (JWT 认证)
235
- └── types.ts # 类型定义 (BotConfig, Webhook Payloads, Channel ID)
213
+ │ ├── index.ts # 插件入口:provide、useContext、模型表
214
+ │ ├── adapter.ts # GitHubAdapter
215
+ ├── bot.ts # GitHubBot(Issue/PR 频道)
216
+ │ ├── gh-client.ts # GitHub REST(App JWT / Installation Token)
217
+ │ ├── register-github-mcp.ts # orchestrator.addMcp(server-github)
218
+ │ ├── agent-prompt.ts # 平台 prompt / deferred 工具筛选
219
+ │ └── types.ts # BotConfig、Webhook payload、Channel ID
236
220
  ├── package.json
237
221
  └── README.md
238
222
  ```
239
223
 
240
- - **无 `@zhin.js/host-router` 时**:适配器正常运行,可使用所有 API 工具(PR/Issue/Repo 管理),但无法接收 Webhook 推送和 OAuth 绑定
241
- - **有 `@zhin.js/host-router` 时**:通过 `useContext('router')` 自动注册 Webhook 路由 + OAuth 路由,获得事件通知、聊天消息接收和用户绑定能力
224
+ - **无 `@zhin.js/host-router` 时**:REST 工具与轮询仍可用,但无法注册 Webhook
225
+ - **有 `@zhin.js/host-router` 时**:注册 Webhook;MCP 通过 `orchestrator.addMcp`(需 PAT `ai.githubMcp.token`)
242
226
 
243
227
  ## 许可证
244
228
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/adapter-github",
3
- "version": "0.1.59",
3
+ "version": "0.1.61",
4
4
  "description": "Zhin.js adapter for GitHub (gh CLI) — treat issues/PRs as chat channels, full repo management, webhook notifications",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -46,11 +46,11 @@
46
46
  "license": "MIT",
47
47
  "devDependencies": {
48
48
  "typescript": "^6.0.3",
49
- "zhin.js": "1.0.88"
49
+ "zhin.js": "1.0.90"
50
50
  },
51
51
  "peerDependencies": {
52
- "zhin.js": "1.0.88",
53
- "@zhin.js/host-router": "0.0.1"
52
+ "@zhin.js/host-router": "0.0.3",
53
+ "zhin.js": "1.0.90"
54
54
  },
55
55
  "peerDependenciesMeta": {
56
56
  "@zhin.js/host-router": {