@zhin.js/adapter-github 1.0.0 → 1.0.1
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 +7 -7
- package/lib/adapter.d.ts +10 -9
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +43 -42
- package/lib/adapter.js.map +1 -1
- package/lib/{bot.d.ts → endpoint.d.ts} +7 -7
- package/lib/endpoint.d.ts.map +1 -0
- package/lib/{bot.js → endpoint.js} +8 -8
- package/lib/endpoint.js.map +1 -0
- package/lib/gh-client.d.ts.map +1 -1
- package/lib/gh-client.js +10 -6
- package/lib/gh-client.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +44 -44
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +3 -3
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +1 -1
- package/lib/types.js.map +1 -1
- package/package.json +5 -4
- package/skills/github/SKILL.md +4 -4
- package/src/adapter.ts +46 -44
- package/src/{bot.ts → endpoint.ts} +10 -10
- package/src/gh-client.ts +16 -12
- package/src/index.ts +47 -47
- package/src/types.ts +4 -4
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js.map +0 -1
package/src/adapter.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { formatCompact, Adapter, Message, Plugin } from 'zhin.js';
|
|
5
5
|
import crypto from 'node:crypto';
|
|
6
|
-
import {
|
|
6
|
+
import { GitHubEndpoint } from './endpoint.js';
|
|
7
7
|
import type { Router } from '@zhin.js/host-router';
|
|
8
|
-
import type {
|
|
8
|
+
import type { GitHubEndpointConfig, EventType, GenericWebhookPayload, Subscription } from './types.js';
|
|
9
9
|
import type { GhClient } from './gh-client.js';
|
|
10
10
|
import type { IssueCommentPayload, PRReviewCommentPayload, PRReviewPayload } from './types.js';
|
|
11
11
|
|
|
@@ -69,7 +69,9 @@ function formatNotification(event: string, p: GenericWebhookPayload): string {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
export class GitHubAdapter extends Adapter<
|
|
72
|
+
export class GitHubAdapter extends Adapter<GitHubEndpoint> {
|
|
73
|
+
static override readonly capabilities = ['inbound', 'outbound'] as const;
|
|
74
|
+
|
|
73
75
|
/** 轮询定时器 */
|
|
74
76
|
private _pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
75
77
|
/** 每个 repo 的 ETag 缓存 */
|
|
@@ -83,8 +85,8 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
83
85
|
super(plugin, 'github', []);
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
|
|
87
|
-
return new
|
|
88
|
+
createEndpoint(config: GitHubEndpointConfig): GitHubEndpoint {
|
|
89
|
+
return new GitHubEndpoint(this, config);
|
|
88
90
|
}
|
|
89
91
|
|
|
90
92
|
async start(): Promise<void> {
|
|
@@ -96,10 +98,10 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
96
98
|
await super.stop();
|
|
97
99
|
}
|
|
98
100
|
|
|
99
|
-
/** 获取第一个可用
|
|
101
|
+
/** 获取第一个可用 Endpoint 的 GhClient (工具用) */
|
|
100
102
|
getAPI(): GhClient | null {
|
|
101
|
-
const
|
|
102
|
-
return
|
|
103
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
104
|
+
return endpoint?.gh || null;
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
/** 获取指定用户绑定的 GhClient;未绑定则返回 null */
|
|
@@ -114,7 +116,7 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
114
116
|
return base.withToken(record.access_token);
|
|
115
117
|
}
|
|
116
118
|
|
|
117
|
-
/** 获取用户 API,若未绑定则降级为
|
|
119
|
+
/** 获取用户 API,若未绑定则降级为 Endpoint 默认的 API */
|
|
118
120
|
async getUserOrDefaultAPI(platform?: string, platformUid?: string): Promise<GhClient | null> {
|
|
119
121
|
if (platform && platformUid) {
|
|
120
122
|
const userApi = await this.getUserAPI(platform, platformUid);
|
|
@@ -123,34 +125,34 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
123
125
|
return this.getAPI();
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
/** 获取第一个
|
|
128
|
+
/** 获取第一个 Endpoint 的 client_id(App 认证时从 /app 自动获取) */
|
|
127
129
|
getClientId(): string | null {
|
|
128
|
-
const
|
|
129
|
-
return
|
|
130
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
131
|
+
return endpoint?.gh.clientId || null;
|
|
130
132
|
}
|
|
131
133
|
|
|
132
|
-
/** 获取第一个
|
|
134
|
+
/** 获取第一个 Endpoint 的 host 配置 */
|
|
133
135
|
getHost(): string | undefined {
|
|
134
|
-
const
|
|
135
|
-
return
|
|
136
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
137
|
+
return endpoint?.$config.host;
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
/** 获取 App slug(用于生成安装链接) */
|
|
139
141
|
getAppSlug(): string | null {
|
|
140
|
-
const
|
|
141
|
-
return
|
|
142
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
143
|
+
return endpoint?.gh.appSlug || null;
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
/** 获取所有已发现的安装信息 */
|
|
145
147
|
getInstallations() {
|
|
146
|
-
const
|
|
147
|
-
return
|
|
148
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
149
|
+
return endpoint?.gh.installations || [];
|
|
148
150
|
}
|
|
149
151
|
|
|
150
|
-
/** 第一个
|
|
152
|
+
/** 第一个 Endpoint 是否配置了 Webhook */
|
|
151
153
|
get hasWebhookConfig(): boolean {
|
|
152
|
-
const
|
|
153
|
-
return !!
|
|
154
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
155
|
+
return !!endpoint?.$config.webhook_secret;
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
/** Webhook 是否已激活 */
|
|
@@ -162,13 +164,13 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
162
164
|
|
|
163
165
|
/** 在 router 上挂载 Webhook 路由(生产环境推荐) */
|
|
164
166
|
setupWebhook(router: Router): void {
|
|
165
|
-
const
|
|
166
|
-
if (!
|
|
167
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
168
|
+
if (!endpoint?.$config.webhook_secret) {
|
|
167
169
|
this.plugin.logger.warn(formatCompact( { op: 'webhook', ok: false, error: 'missing webhook_secret' }));
|
|
168
170
|
return;
|
|
169
171
|
}
|
|
170
|
-
const secret =
|
|
171
|
-
const path =
|
|
172
|
+
const secret = endpoint.$config.webhook_secret;
|
|
173
|
+
const path = endpoint.$config.webhook_path || '/github/webhook';
|
|
172
174
|
|
|
173
175
|
router.post(path, async (ctx) => {
|
|
174
176
|
const signature = ctx.get('x-hub-signature-256') as string;
|
|
@@ -207,7 +209,7 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
207
209
|
|
|
208
210
|
/** 处理 Webhook 推送的事件 */
|
|
209
211
|
async handleWebhookPayload(event: string, payload: any): Promise<void> {
|
|
210
|
-
const
|
|
212
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
211
213
|
const repo = payload.repository?.full_name;
|
|
212
214
|
|
|
213
215
|
this.plugin.logger.debug(`Webhook: ${event}${payload.action ? `.${payload.action}` : ''} ${repo || ''}`);
|
|
@@ -222,26 +224,26 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
222
224
|
}
|
|
223
225
|
|
|
224
226
|
// 处理消息类事件(Issue/PR 评论)
|
|
225
|
-
if (
|
|
226
|
-
const message =
|
|
227
|
-
const botUser =
|
|
227
|
+
if (endpoint && event === 'issue_comment' && payload.action === 'created' && payload.comment) {
|
|
228
|
+
const message = endpoint.$formatMessage(payload as IssueCommentPayload);
|
|
229
|
+
const botUser = endpoint.gh.authenticatedUser;
|
|
228
230
|
if (!(botUser && message.$sender.id === botUser)) {
|
|
229
231
|
this.emit('message.receive', message);
|
|
230
232
|
}
|
|
231
233
|
}
|
|
232
234
|
|
|
233
|
-
if (
|
|
234
|
-
const message =
|
|
235
|
-
const botUser =
|
|
235
|
+
if (endpoint && event === 'pull_request_review_comment' && payload.action === 'created' && payload.comment) {
|
|
236
|
+
const message = endpoint.formatPRReviewComment(payload as PRReviewCommentPayload);
|
|
237
|
+
const botUser = endpoint.gh.authenticatedUser;
|
|
236
238
|
if (!(botUser && message.$sender.id === botUser)) {
|
|
237
239
|
this.emit('message.receive', message);
|
|
238
240
|
}
|
|
239
241
|
}
|
|
240
242
|
|
|
241
|
-
if (
|
|
242
|
-
const message =
|
|
243
|
+
if (endpoint && event === 'pull_request_review' && payload.action === 'submitted') {
|
|
244
|
+
const message = endpoint.formatPRReview(payload as PRReviewPayload);
|
|
243
245
|
if (message) {
|
|
244
|
-
const botUser =
|
|
246
|
+
const botUser = endpoint.gh.authenticatedUser;
|
|
245
247
|
if (!(botUser && message.$sender.id === botUser)) {
|
|
246
248
|
this.emit('message.receive', message);
|
|
247
249
|
}
|
|
@@ -269,8 +271,8 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
269
271
|
/** 启动事件轮询 */
|
|
270
272
|
startPolling(): void {
|
|
271
273
|
if (this._pollTimer) return;
|
|
272
|
-
const
|
|
273
|
-
const interval = (
|
|
274
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
275
|
+
const interval = (endpoint?.$config.poll_interval || 60) * 1000;
|
|
274
276
|
this.plugin.logger.debug(formatCompact( { op: 'poll', interval_s: interval / 1000 }));
|
|
275
277
|
|
|
276
278
|
// 立即执行一次
|
|
@@ -339,7 +341,7 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
339
341
|
|
|
340
342
|
this.plugin.logger.debug(`${repo}: ${newEvents.length} 条新事件`);
|
|
341
343
|
|
|
342
|
-
const
|
|
344
|
+
const endpoint = this.endpoints.values().next().value as GitHubEndpoint | undefined;
|
|
343
345
|
|
|
344
346
|
// 按时间正序处理(API 返回倒序)
|
|
345
347
|
for (const ev of newEvents.reverse()) {
|
|
@@ -366,9 +368,9 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
366
368
|
}
|
|
367
369
|
|
|
368
370
|
// 处理消息类事件(Issue/PR 评论)
|
|
369
|
-
if (
|
|
370
|
-
const message =
|
|
371
|
-
const botUser =
|
|
371
|
+
if (endpoint && eventName === 'issue_comment' && ev.payload?.action === 'created' && ev.payload?.comment) {
|
|
372
|
+
const message = endpoint.$formatMessage(ev.payload as IssueCommentPayload);
|
|
373
|
+
const botUser = endpoint.gh.authenticatedUser;
|
|
372
374
|
if (!(botUser && message.$sender.id === botUser)) {
|
|
373
375
|
this.emit('message.receive', message);
|
|
374
376
|
}
|
|
@@ -436,8 +438,8 @@ export class GitHubAdapter extends Adapter<GitHubBot> {
|
|
|
436
438
|
this.plugin.logger.warn(formatCompact( { op: 'notify', ok: false, adapter: s.adapter, error: 'no sendMessage' }));
|
|
437
439
|
continue;
|
|
438
440
|
}
|
|
439
|
-
this.plugin.logger.debug(formatCompact( { op: 'notify', event: eventType, adapter: s.adapter,
|
|
440
|
-
await targetAdapter.sendMessage({ context: s.adapter,
|
|
441
|
+
this.plugin.logger.debug(formatCompact( { op: 'notify', event: eventType, adapter: s.adapter, endpoint: s.endpoint, target: s.target_id }));
|
|
442
|
+
await targetAdapter.sendMessage({ context: s.adapter, endpoint: s.endpoint, id: s.target_id, type: s.target_type, content: text });
|
|
441
443
|
} catch (e) {
|
|
442
444
|
this.plugin.logger.error(`通知推送失败 → ${s.adapter}:${s.target_id}`, e);
|
|
443
445
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* GitHub
|
|
2
|
+
* GitHub Endpoint 实现(基于 gh CLI)
|
|
3
3
|
*/
|
|
4
|
-
import { formatCompact,
|
|
4
|
+
import { formatCompact, Endpoint, Message, segment, SendContent, SendOptions, type MessageSegment } from 'zhin.js';
|
|
5
5
|
import type {
|
|
6
|
-
|
|
6
|
+
GitHubEndpointConfig,
|
|
7
7
|
IssueCommentPayload,
|
|
8
8
|
PRReviewCommentPayload,
|
|
9
9
|
PRReviewPayload,
|
|
@@ -40,7 +40,7 @@ export function toMarkdown(content: SendContent): string {
|
|
|
40
40
|
}).join('');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export class
|
|
43
|
+
export class GitHubEndpoint implements Endpoint<GitHubEndpointConfig, IssueCommentPayload> {
|
|
44
44
|
$connected = false;
|
|
45
45
|
gh: GhClient;
|
|
46
46
|
|
|
@@ -50,7 +50,7 @@ export class GitHubBot implements Bot<GitHubBotConfig, IssueCommentPayload> {
|
|
|
50
50
|
return this.adapter.plugin.logger;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
constructor(public adapter: GitHubAdapter, public $config:
|
|
53
|
+
constructor(public adapter: GitHubAdapter, public $config: GitHubEndpointConfig) {
|
|
54
54
|
const { host, app_id, private_key } = $config;
|
|
55
55
|
const appAuth = app_id && private_key
|
|
56
56
|
? { appId: app_id, privateKey: private_key }
|
|
@@ -62,12 +62,12 @@ export class GitHubBot implements Bot<GitHubBotConfig, IssueCommentPayload> {
|
|
|
62
62
|
const result = await this.gh.verifyAuth();
|
|
63
63
|
if (!result.ok) throw new Error(`GitHub 认证失败: ${result.message}`);
|
|
64
64
|
this.$connected = true;
|
|
65
|
-
this.logger.info(formatCompact({
|
|
65
|
+
this.logger.info(formatCompact({ endpoint: this.$id }));
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
async $disconnect(): Promise<void> {
|
|
69
69
|
this.$connected = false;
|
|
70
|
-
this.logger.debug(formatCompact({
|
|
70
|
+
this.logger.debug(formatCompact({ endpoint: this.$id, disconnect: true }));
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
$formatMessage(payload: IssueCommentPayload): Message<IssueCommentPayload> {
|
|
@@ -80,7 +80,7 @@ export class GitHubBot implements Bot<GitHubBotConfig, IssueCommentPayload> {
|
|
|
80
80
|
const result = Message.from(payload, {
|
|
81
81
|
$id: payload.comment.id.toString(),
|
|
82
82
|
$adapter: 'github',
|
|
83
|
-
$
|
|
83
|
+
$endpoint: this.$config.name,
|
|
84
84
|
$sender: { id: payload.sender.login, name: payload.sender.login },
|
|
85
85
|
$channel: { id: channelId, type: 'group' },
|
|
86
86
|
$content: parseMarkdown(payload.comment.body),
|
|
@@ -110,7 +110,7 @@ export class GitHubBot implements Bot<GitHubBotConfig, IssueCommentPayload> {
|
|
|
110
110
|
return Message.from(payload, {
|
|
111
111
|
$id: payload.comment.id.toString(),
|
|
112
112
|
$adapter: 'github',
|
|
113
|
-
$
|
|
113
|
+
$endpoint: this.$config.name,
|
|
114
114
|
$sender: { id: payload.sender.login, name: payload.sender.login },
|
|
115
115
|
$channel: { id: channelId, type: 'group' },
|
|
116
116
|
$content: parseMarkdown(body),
|
|
@@ -140,7 +140,7 @@ export class GitHubBot implements Bot<GitHubBotConfig, IssueCommentPayload> {
|
|
|
140
140
|
return Message.from(payload, {
|
|
141
141
|
$id: payload.review.id.toString(),
|
|
142
142
|
$adapter: 'github',
|
|
143
|
-
$
|
|
143
|
+
$endpoint: this.$config.name,
|
|
144
144
|
$sender: { id: payload.sender.login, name: payload.sender.login },
|
|
145
145
|
$channel: { id: channelId, type: 'group' },
|
|
146
146
|
$content: parseMarkdown(body),
|
package/src/gh-client.ts
CHANGED
|
@@ -162,12 +162,13 @@ export class GhClient {
|
|
|
162
162
|
let data: any;
|
|
163
163
|
try { data = raw ? JSON.parse(raw) : null; } catch { data = raw; }
|
|
164
164
|
return { ok: true, status: 200, data };
|
|
165
|
-
} catch (err:
|
|
165
|
+
} catch (err: unknown) {
|
|
166
166
|
let data: any;
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
const errObj = err instanceof Error ? err : new Error(String(err));
|
|
168
|
+
try { data = (err as any).stdout ? JSON.parse((err as any).stdout) : { message: errObj.message }; } catch {
|
|
169
|
+
data = { message: (err as any).stderr?.trim() || errObj.message };
|
|
169
170
|
}
|
|
170
|
-
return { ok: false, status: err.exitCode || 0, data };
|
|
171
|
+
return { ok: false, status: (err as any).exitCode || 0, data };
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
|
|
@@ -205,8 +206,8 @@ export class GhClient {
|
|
|
205
206
|
}
|
|
206
207
|
const errBody = await appRes.text();
|
|
207
208
|
return { ok: false, user: '', message: `App Token 验证失败 (${appRes.status}): ${errBody}` };
|
|
208
|
-
} catch (e:
|
|
209
|
-
return { ok: false, user: '', message: e.message || 'App 认证失败' };
|
|
209
|
+
} catch (e: unknown) {
|
|
210
|
+
return { ok: false, user: '', message: (e instanceof Error ? e.message : String(e)) || 'App 认证失败' };
|
|
210
211
|
}
|
|
211
212
|
}
|
|
212
213
|
// Token 模式(用户绑定)或 gh CLI 默认模式
|
|
@@ -225,8 +226,8 @@ export class GhClient {
|
|
|
225
226
|
const login = raw.trim();
|
|
226
227
|
this._user = login;
|
|
227
228
|
return { ok: true, user: login, message: `gh CLI: ${login}` };
|
|
228
|
-
} catch (e:
|
|
229
|
-
return { ok: false, user: '', message: e.message || 'gh 认证检查失败' };
|
|
229
|
+
} catch (e: unknown) {
|
|
230
|
+
return { ok: false, user: '', message: (e instanceof Error ? e.message : String(e)) || 'gh 认证检查失败' };
|
|
230
231
|
}
|
|
231
232
|
}
|
|
232
233
|
|
|
@@ -469,9 +470,10 @@ export class GhClient {
|
|
|
469
470
|
let events: any[];
|
|
470
471
|
try { events = JSON.parse(bodyPart); } catch { events = []; }
|
|
471
472
|
return { events: Array.isArray(events) ? events : [], etag: newEtag };
|
|
472
|
-
} catch (err:
|
|
473
|
+
} catch (err: unknown) {
|
|
473
474
|
// 304 Not Modified — gh 会以非 0 退出码返回
|
|
474
|
-
|
|
475
|
+
const ghErr = err instanceof Error ? (err as Error & { stderr?: string; exitCode?: number }) : null;
|
|
476
|
+
if (ghErr?.stderr?.includes('304') || (ghErr as { exitCode?: number } | null)?.exitCode === 1) {
|
|
475
477
|
return { events: [], etag: etag || null };
|
|
476
478
|
}
|
|
477
479
|
throw err;
|
|
@@ -506,9 +508,11 @@ export class GhClient {
|
|
|
506
508
|
body: JSON.stringify({ client_id: clientId }),
|
|
507
509
|
signal: AbortSignal.timeout(15_000),
|
|
508
510
|
});
|
|
509
|
-
} catch (e:
|
|
511
|
+
} catch (e: unknown) {
|
|
512
|
+
const errMsg = e instanceof Error ? e.message : String(e);
|
|
513
|
+
const errCause = e instanceof Error ? (e as any).cause?.code : undefined;
|
|
510
514
|
throw new Error(
|
|
511
|
-
`Device Flow 请求失败: 无法连接 ${baseUrl} (${
|
|
515
|
+
`Device Flow 请求失败: 无法连接 ${baseUrl} (${errCause || errMsg})。` +
|
|
512
516
|
`Device Flow 需要访问 github.com(非 api.github.com),请检查网络/代理设置。`,
|
|
513
517
|
);
|
|
514
518
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* GitHub 适配器入口:类型扩展、模型、导出、注册
|
|
3
3
|
*/
|
|
4
|
-
import { formatCompact, registerAgentPromptContributor, type Context, type
|
|
4
|
+
import { formatCompact, registerAgentPromptContributor, type Context, type Message, type Plugin, type Tool, type ToolFeature, unregisterAgentPromptContributor, usePlugin } from 'zhin.js';
|
|
5
5
|
import { createGithubAgentPromptContributor } from './agent-prompt.js';
|
|
6
6
|
import { GitHubAdapter } from './adapter.js';
|
|
7
7
|
import { GhClient } from './gh-client.js';
|
|
@@ -25,7 +25,7 @@ declare module 'zhin.js' {
|
|
|
25
25
|
target_id: string;
|
|
26
26
|
target_type: 'private' | 'group' | 'channel';
|
|
27
27
|
adapter: string;
|
|
28
|
-
|
|
28
|
+
endpoint: string;
|
|
29
29
|
};
|
|
30
30
|
github_events: {
|
|
31
31
|
id: number;
|
|
@@ -38,7 +38,7 @@ declare module 'zhin.js' {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export * from './types.js';
|
|
41
|
-
export {
|
|
41
|
+
export { GitHubEndpoint, parseMarkdown, toMarkdown } from './endpoint.js';
|
|
42
42
|
export { GitHubAdapter } from './adapter.js';
|
|
43
43
|
export { GhClient } from './gh-client.js';
|
|
44
44
|
|
|
@@ -54,7 +54,7 @@ defineModel('github_subscriptions', {
|
|
|
54
54
|
target_id: { type: 'text', nullable: false },
|
|
55
55
|
target_type: { type: 'text', nullable: false },
|
|
56
56
|
adapter: { type: 'text', nullable: false },
|
|
57
|
-
|
|
57
|
+
endpoint: { type: 'text', nullable: false },
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
defineModel('github_events', {
|
|
@@ -118,7 +118,7 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
118
118
|
// --- Star ---
|
|
119
119
|
{
|
|
120
120
|
name: 'github_star',
|
|
121
|
-
description: 'Star 或取消 Star 一个 GitHub 仓库(使用你绑定的 GitHub 账号,未绑定则用
|
|
121
|
+
description: 'Star 或取消 Star 一个 GitHub 仓库(使用你绑定的 GitHub 账号,未绑定则用 Endpoint 默认账号)',
|
|
122
122
|
parameters: {
|
|
123
123
|
type: 'object' as const,
|
|
124
124
|
properties: {
|
|
@@ -128,8 +128,8 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
128
128
|
required: ['action', 'repo'],
|
|
129
129
|
},
|
|
130
130
|
tags: ['github'],
|
|
131
|
-
execute: async (args: Record<string, any>,
|
|
132
|
-
const gh = await adapter.getUserOrDefaultAPI(
|
|
131
|
+
execute: async (args: Record<string, any>, commMessage?: Message<any>) => {
|
|
132
|
+
const gh = await adapter.getUserOrDefaultAPI(commMessage?.$adapter, commMessage?.$sender.id);
|
|
133
133
|
if (!gh) return '❌ 没有可用的 GitHub bot';
|
|
134
134
|
const { action, repo } = args;
|
|
135
135
|
switch (action) {
|
|
@@ -158,19 +158,19 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
158
158
|
properties: {},
|
|
159
159
|
},
|
|
160
160
|
tags: ['github'],
|
|
161
|
-
execute: async (_args: Record<string, any>,
|
|
162
|
-
if (!
|
|
161
|
+
execute: async (_args: Record<string, any>, commMessage?: Message<any>) => {
|
|
162
|
+
if (!commMessage?.$adapter || !commMessage?.$sender?.id) {
|
|
163
163
|
return '❌ 无法获取当前用户信息';
|
|
164
164
|
}
|
|
165
165
|
const clientId = adapter.getClientId();
|
|
166
|
-
if (!clientId) return '❌
|
|
166
|
+
if (!clientId) return '❌ Endpoint 未配置 GitHub App 或 App 无 client_id,无法进行账号绑定';
|
|
167
167
|
|
|
168
168
|
const db = plugin.root?.inject('database') as any;
|
|
169
169
|
const model = db?.models?.get('github_oauth_users');
|
|
170
170
|
if (!model) return '❌ 数据库未就绪';
|
|
171
171
|
|
|
172
172
|
// 检查是否已绑定
|
|
173
|
-
const [existing] = await model.select().where({ platform:
|
|
173
|
+
const [existing] = await model.select().where({ platform: commMessage.$adapter, platform_uid: commMessage.$sender.id });
|
|
174
174
|
if (existing) {
|
|
175
175
|
return `⚠️ 你已绑定 GitHub 账号: ${existing.github_login}\n如需重新绑定,请先执行 github_unbind`;
|
|
176
176
|
}
|
|
@@ -197,7 +197,7 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
197
197
|
tokenPromise.then(async (tokenData) => {
|
|
198
198
|
if (!tokenData) {
|
|
199
199
|
// 授权超时或被拒绝 — 由于 execute 已经返回了,这里只能通过日志记录
|
|
200
|
-
logger.warn(formatCompact( { op: 'device_flow', ok: false, platform:
|
|
200
|
+
logger.warn(formatCompact( { op: 'device_flow', ok: false, platform: commMessage.$adapter, sender: commMessage.$sender.id }));
|
|
201
201
|
return;
|
|
202
202
|
}
|
|
203
203
|
|
|
@@ -208,25 +208,25 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
208
208
|
|
|
209
209
|
await model.insert({
|
|
210
210
|
id: Date.now(),
|
|
211
|
-
platform:
|
|
212
|
-
platform_uid:
|
|
211
|
+
platform: commMessage.$adapter,
|
|
212
|
+
platform_uid: commMessage.$sender.id,
|
|
213
213
|
github_login: login,
|
|
214
214
|
access_token: tokenData.access_token,
|
|
215
215
|
created_at: Date.now(),
|
|
216
216
|
});
|
|
217
|
-
logger.info(formatCompact( { op: 'bind', platform:
|
|
217
|
+
logger.info(formatCompact( { op: 'bind', platform: commMessage.$adapter, sender: commMessage.$sender.id, login }));
|
|
218
218
|
|
|
219
219
|
// 尝试回复绑定成功消息
|
|
220
|
-
if (
|
|
221
|
-
await
|
|
220
|
+
if (commMessage?.$reply) {
|
|
221
|
+
await commMessage.$reply(`✅ GitHub 账号绑定成功!\n👤 ${login}`);
|
|
222
222
|
}
|
|
223
223
|
}).catch(err => {
|
|
224
224
|
logger.error('GitHub Device Flow 错误:', err);
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
return replyMsg;
|
|
228
|
-
} catch (e:
|
|
229
|
-
return `❌ Device Flow 启动失败: ${e.message}`;
|
|
228
|
+
} catch (e: unknown) {
|
|
229
|
+
return `❌ Device Flow 启动失败: ${e instanceof Error ? e.message : String(e)}`;
|
|
230
230
|
}
|
|
231
231
|
},
|
|
232
232
|
},
|
|
@@ -239,15 +239,15 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
239
239
|
properties: {},
|
|
240
240
|
},
|
|
241
241
|
tags: ['github'],
|
|
242
|
-
execute: async (_args: Record<string, any>,
|
|
243
|
-
if (!
|
|
242
|
+
execute: async (_args: Record<string, any>, commMessage?: Message<any>) => {
|
|
243
|
+
if (!commMessage?.$adapter || !commMessage?.$sender?.id) {
|
|
244
244
|
return '❌ 无法获取当前用户信息';
|
|
245
245
|
}
|
|
246
246
|
const db = plugin.root?.inject('database') as any;
|
|
247
247
|
const model = db?.models?.get('github_oauth_users');
|
|
248
248
|
if (!model) return '❌ 数据库未就绪';
|
|
249
249
|
|
|
250
|
-
const [existing] = await model.select().where({ platform:
|
|
250
|
+
const [existing] = await model.select().where({ platform: commMessage.$adapter, platform_uid: commMessage.$sender.id });
|
|
251
251
|
if (!existing) return '📭 你尚未绑定 GitHub 账号';
|
|
252
252
|
|
|
253
253
|
await model.delete().where({ id: existing.id });
|
|
@@ -263,15 +263,15 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
263
263
|
properties: {},
|
|
264
264
|
},
|
|
265
265
|
tags: ['github'],
|
|
266
|
-
execute: async (_args: Record<string, any>,
|
|
267
|
-
if (!
|
|
266
|
+
execute: async (_args: Record<string, any>, commMessage?: Message<any>) => {
|
|
267
|
+
if (!commMessage?.$adapter || !commMessage?.$sender?.id) {
|
|
268
268
|
return '❌ 无法获取当前用户信息';
|
|
269
269
|
}
|
|
270
270
|
const db = plugin.root?.inject('database') as any;
|
|
271
271
|
const model = db?.models?.get('github_oauth_users');
|
|
272
272
|
if (!model) return '❌ 数据库未就绪';
|
|
273
273
|
|
|
274
|
-
const [existing] = await model.select().where({ platform:
|
|
274
|
+
const [existing] = await model.select().where({ platform: commMessage.$adapter, platform_uid: commMessage.$sender.id });
|
|
275
275
|
if (!existing) return '📭 你尚未绑定 GitHub 账号\n🔗 使用 github_bind 绑定你的账号';
|
|
276
276
|
|
|
277
277
|
// 验证 token 是否仍然有效
|
|
@@ -286,7 +286,7 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
286
286
|
// --- Install App ---
|
|
287
287
|
{
|
|
288
288
|
name: 'github_install',
|
|
289
|
-
description: '获取安装 GitHub App 的链接 — 安装后
|
|
289
|
+
description: '获取安装 GitHub App 的链接 — 安装后 Endpoint 可以访问你的仓库,你也可以使用更多功能',
|
|
290
290
|
parameters: {
|
|
291
291
|
type: 'object' as const,
|
|
292
292
|
properties: {},
|
|
@@ -294,7 +294,7 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
294
294
|
tags: ['github'],
|
|
295
295
|
execute: async () => {
|
|
296
296
|
const slug = adapter.getAppSlug();
|
|
297
|
-
if (!slug) return '❌
|
|
297
|
+
if (!slug) return '❌ Endpoint 未配置 GitHub App';
|
|
298
298
|
const host = adapter.getHost() || 'github.com';
|
|
299
299
|
const installations = adapter.getInstallations();
|
|
300
300
|
let msg = `🔗 请点击以下链接安装 GitHub App 到你的仓库:\n https://${host}/apps/${slug}/installations/new`;
|
|
@@ -321,8 +321,8 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
321
321
|
},
|
|
322
322
|
platforms: ['github'],
|
|
323
323
|
tags: ['github'],
|
|
324
|
-
execute: async (args: Record<string, any>,
|
|
325
|
-
if (!
|
|
324
|
+
execute: async (args: Record<string, any>, commMessage?: Message<any>) => {
|
|
325
|
+
if (!commMessage?.$adapter || !commMessage?.$sender.id || !commMessage?.$channel?.id || !commMessage?.$endpoint) {
|
|
326
326
|
return '❌ 无法获取当前聊天通道信息';
|
|
327
327
|
}
|
|
328
328
|
const db = plugin.root?.inject('database') as any;
|
|
@@ -337,12 +337,12 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
337
337
|
|
|
338
338
|
const [existing] = await model.select().where({
|
|
339
339
|
repo: args.repo,
|
|
340
|
-
target_id:
|
|
341
|
-
adapter:
|
|
342
|
-
|
|
340
|
+
target_id: commMessage.$channel?.id,
|
|
341
|
+
adapter: commMessage.$adapter,
|
|
342
|
+
endpoint: commMessage.$endpoint,
|
|
343
343
|
});
|
|
344
344
|
if (existing) {
|
|
345
|
-
await model.update({ events, target_type:
|
|
345
|
+
await model.update({ events, target_type: commMessage.$channel?.type || 'private' }).where({ id: existing.id });
|
|
346
346
|
return `✅ 已更新订阅 ${args.repo}\n📡 事件: ${events.join(', ')}`;
|
|
347
347
|
}
|
|
348
348
|
|
|
@@ -350,10 +350,10 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
350
350
|
id: Date.now(),
|
|
351
351
|
repo: args.repo,
|
|
352
352
|
events,
|
|
353
|
-
target_id:
|
|
354
|
-
target_type:
|
|
355
|
-
adapter:
|
|
356
|
-
|
|
353
|
+
target_id: commMessage.$channel?.id,
|
|
354
|
+
target_type: commMessage.$channel?.type || 'private',
|
|
355
|
+
adapter: commMessage.$adapter,
|
|
356
|
+
endpoint: commMessage.$endpoint,
|
|
357
357
|
});
|
|
358
358
|
return `✅ 已订阅 ${args.repo}\n📡 事件: ${events.join(', ')}\n📌 通知将推送到当前通道`;
|
|
359
359
|
},
|
|
@@ -371,8 +371,8 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
371
371
|
},
|
|
372
372
|
platforms: ['github'],
|
|
373
373
|
tags: ['github'],
|
|
374
|
-
execute: async (args: Record<string, any>,
|
|
375
|
-
if (!
|
|
374
|
+
execute: async (args: Record<string, any>, commMessage?: Message<any>) => {
|
|
375
|
+
if (!commMessage?.$adapter || !commMessage?.$channel?.id || !commMessage?.$endpoint) {
|
|
376
376
|
return '❌ 无法获取当前聊天通道信息';
|
|
377
377
|
}
|
|
378
378
|
const db = plugin.root?.inject('database') as any;
|
|
@@ -381,9 +381,9 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
381
381
|
|
|
382
382
|
const [existing] = await model.select().where({
|
|
383
383
|
repo: args.repo,
|
|
384
|
-
target_id:
|
|
385
|
-
adapter:
|
|
386
|
-
|
|
384
|
+
target_id: commMessage.$channel?.id,
|
|
385
|
+
adapter: commMessage.$adapter,
|
|
386
|
+
endpoint: commMessage.$endpoint,
|
|
387
387
|
});
|
|
388
388
|
if (!existing) return `📭 当前通道未订阅 ${args.repo}`;
|
|
389
389
|
|
|
@@ -401,8 +401,8 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
401
401
|
},
|
|
402
402
|
platforms: ['github'],
|
|
403
403
|
tags: ['github'],
|
|
404
|
-
execute: async (_args: Record<string, any>,
|
|
405
|
-
if (!
|
|
404
|
+
execute: async (_args: Record<string, any>, commMessage?: Message<any>) => {
|
|
405
|
+
if (!commMessage?.$adapter || !commMessage?.$channel?.id || !commMessage?.$endpoint) {
|
|
406
406
|
return '❌ 无法获取当前聊天通道信息';
|
|
407
407
|
}
|
|
408
408
|
const db = plugin.root?.inject('database') as any;
|
|
@@ -410,9 +410,9 @@ useContext('tool', 'github', (toolService: ToolFeature, adapter: GitHubAdapter)
|
|
|
410
410
|
if (!model) return '❌ 数据库未就绪';
|
|
411
411
|
|
|
412
412
|
const subs = await model.select().where({
|
|
413
|
-
target_id:
|
|
414
|
-
adapter:
|
|
415
|
-
|
|
413
|
+
target_id: commMessage.$channel?.id,
|
|
414
|
+
adapter: commMessage.$adapter,
|
|
415
|
+
endpoint: commMessage.$endpoint,
|
|
416
416
|
});
|
|
417
417
|
if (!subs?.length) return '📭 当前通道没有任何 GitHub 订阅';
|
|
418
418
|
|
package/src/types.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// ──
|
|
1
|
+
// ── Endpoint 配置 ─────────────────────────────────────────────────────────
|
|
2
2
|
// 基于 gh CLI 认证:需要系统已安装并认证 gh CLI
|
|
3
3
|
// 认证方式:gh auth login
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface GitHubEndpointConfig {
|
|
6
6
|
context: 'github';
|
|
7
|
-
/**
|
|
7
|
+
/** Endpoint 标识名称 */
|
|
8
8
|
name: string;
|
|
9
9
|
/** GitHub Enterprise 主机名(默认 github.com) */
|
|
10
10
|
host?: string;
|
|
@@ -135,7 +135,7 @@ export interface Subscription {
|
|
|
135
135
|
target_id: string;
|
|
136
136
|
target_type: 'private' | 'group' | 'channel';
|
|
137
137
|
adapter: string;
|
|
138
|
-
|
|
138
|
+
endpoint: string;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
// ── Tool Action Types ────────────────────────────────────────────────
|
package/lib/bot.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bot.d.ts","sourceRoot":"","sources":["../src/bot.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAiB,GAAG,EAAE,OAAO,EAAW,WAAW,EAAE,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAC;AAC9G,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EACnB,sBAAsB,EACtB,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,wBAAgB,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,EAAE,CAY1D;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,MAAM,CAYvD;AAED,qBAAa,SAAU,YAAW,GAAG,CAAC,eAAe,EAAE,mBAAmB,CAAC;IAUtD,OAAO,EAAE,aAAa;IAAS,OAAO,EAAE,eAAe;IAT1E,UAAU,UAAS;IACnB,EAAE,EAAE,QAAQ,CAAC;IAEb,IAAI,GAAG,WAAgC;IAEvC,IAAI,MAAM,6BAET;gBAEkB,OAAO,EAAE,aAAa,EAAS,OAAO,EAAE,eAAe;IAQpE,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAOzB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAKlC,cAAc,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IA2B1E,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,CAAC;IA2BvF,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,GAAG,IAAI;IA8BnE,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAcnD,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGhD"}
|