@zhin.js/adapter-github 1.0.1 → 1.1.2

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 (113) hide show
  1. package/CHANGELOG.md +1212 -0
  2. package/README.md +73 -195
  3. package/adapters/github/index.js +51 -0
  4. package/adapters/github/index.ts +59 -0
  5. package/agents/github/agent.json +20 -0
  6. package/agents/github/boundaries.md +3 -0
  7. package/agents/github/conventions.md +3 -0
  8. package/agents/github/skills/github-account/SKILL.md +20 -0
  9. package/agents/github/skills/github-account/handlers.js +116 -0
  10. package/agents/github/skills/github-account/handlers.ts +136 -0
  11. package/agents/github/skills/github-account/tools/bind/index.js +13 -0
  12. package/agents/github/skills/github-account/tools/bind/index.ts +13 -0
  13. package/agents/github/skills/github-account/tools/install/index.js +13 -0
  14. package/agents/github/skills/github-account/tools/install/index.ts +13 -0
  15. package/agents/github/skills/github-account/tools/unbind/index.js +13 -0
  16. package/agents/github/skills/github-account/tools/unbind/index.ts +13 -0
  17. package/agents/github/skills/github-account/tools/whoami/index.js +13 -0
  18. package/agents/github/skills/github-account/tools/whoami/index.ts +13 -0
  19. package/agents/github/skills/github-cli/SKILL.md +47 -0
  20. package/agents/github/skills/github-repository/SKILL.md +21 -0
  21. package/agents/github/skills/github-repository/handlers.js +77 -0
  22. package/agents/github/skills/github-repository/handlers.ts +113 -0
  23. package/agents/github/skills/github-repository/tools/create_pr/index.js +20 -0
  24. package/agents/github/skills/github-repository/tools/create_pr/index.ts +20 -0
  25. package/agents/github/skills/github-repository/tools/patch_file/index.js +19 -0
  26. package/agents/github/skills/github-repository/tools/patch_file/index.ts +19 -0
  27. package/agents/github/skills/github-repository/tools/prepare_workspace/index.js +15 -0
  28. package/agents/github/skills/github-repository/tools/prepare_workspace/index.ts +15 -0
  29. package/agents/github/skills/github-repository/tools/push_branch/index.js +18 -0
  30. package/agents/github/skills/github-repository/tools/push_branch/index.ts +18 -0
  31. package/agents/github/skills/github-subscriptions/SKILL.md +20 -0
  32. package/agents/github/skills/github-subscriptions/handlers.js +101 -0
  33. package/agents/github/skills/github-subscriptions/handlers.ts +119 -0
  34. package/agents/github/skills/github-subscriptions/tools/star/index.js +16 -0
  35. package/agents/github/skills/github-subscriptions/tools/star/index.ts +16 -0
  36. package/agents/github/skills/github-subscriptions/tools/subscribe/index.js +16 -0
  37. package/agents/github/skills/github-subscriptions/tools/subscribe/index.ts +16 -0
  38. package/agents/github/skills/github-subscriptions/tools/subscriptions/index.js +13 -0
  39. package/agents/github/skills/github-subscriptions/tools/subscriptions/index.ts +13 -0
  40. package/agents/github/skills/github-subscriptions/tools/unsubscribe/index.js +15 -0
  41. package/agents/github/skills/github-subscriptions/tools/unsubscribe/index.ts +15 -0
  42. package/agents/github/system.md +3 -0
  43. package/commands/github/endpoint/add/[id]/index.js +3 -0
  44. package/commands/github/endpoint/add/[id]/index.ts +3 -0
  45. package/commands/github/endpoint/definition.js +23 -0
  46. package/commands/github/endpoint/definition.ts +23 -0
  47. package/commands/github/endpoint/list/index.js +3 -0
  48. package/commands/github/endpoint/list/index.ts +3 -0
  49. package/commands/github/endpoint/remove/[id]/index.js +3 -0
  50. package/commands/github/endpoint/remove/[id]/index.ts +3 -0
  51. package/lib/client.d.ts +36 -0
  52. package/lib/client.js +44 -0
  53. package/lib/endpoint.d.ts +32 -22
  54. package/lib/endpoint.js +125 -145
  55. package/lib/gh-client.d.ts +73 -1
  56. package/lib/gh-client.js +99 -1
  57. package/lib/github-channel-context.d.ts +16 -0
  58. package/lib/github-channel-context.js +31 -0
  59. package/lib/github-runtime-state.d.ts +1 -0
  60. package/lib/github-runtime-state.js +6 -0
  61. package/lib/index.d.ts +7 -32
  62. package/lib/index.js +7 -385
  63. package/lib/oauth-users.d.ts +33 -0
  64. package/lib/oauth-users.js +38 -0
  65. package/lib/protocol.d.ts +72 -0
  66. package/lib/protocol.js +281 -0
  67. package/lib/types.d.ts +0 -18
  68. package/lib/types.js +2 -4
  69. package/lib/webhook.d.ts +14 -0
  70. package/lib/webhook.js +88 -0
  71. package/lib/workspace-manager.d.ts +21 -0
  72. package/lib/workspace-manager.js +154 -0
  73. package/package.json +93 -23
  74. package/plugin.js +38 -0
  75. package/prompt-sections/platform/index.js +16 -0
  76. package/prompt-sections/platform/index.ts +16 -0
  77. package/schema.json +137 -0
  78. package/src/client.ts +62 -0
  79. package/src/endpoint.ts +145 -150
  80. package/src/gh-client.ts +131 -0
  81. package/src/github-channel-context.ts +46 -0
  82. package/src/github-runtime-state.ts +7 -0
  83. package/src/index.ts +48 -431
  84. package/src/oauth-users.ts +47 -0
  85. package/src/protocol.ts +372 -0
  86. package/src/types.ts +0 -22
  87. package/src/webhook.ts +130 -0
  88. package/src/workspace-manager.ts +168 -0
  89. package/lib/adapter.d.ts +0 -64
  90. package/lib/adapter.d.ts.map +0 -1
  91. package/lib/adapter.js +0 -416
  92. package/lib/adapter.js.map +0 -1
  93. package/lib/agent-prompt.d.ts +0 -3
  94. package/lib/agent-prompt.d.ts.map +0 -1
  95. package/lib/agent-prompt.js +0 -82
  96. package/lib/agent-prompt.js.map +0 -1
  97. package/lib/endpoint.d.ts.map +0 -1
  98. package/lib/endpoint.js.map +0 -1
  99. package/lib/gh-client.d.ts.map +0 -1
  100. package/lib/gh-client.js.map +0 -1
  101. package/lib/index.d.ts.map +0 -1
  102. package/lib/index.js.map +0 -1
  103. package/lib/register-github-mcp.d.ts +0 -6
  104. package/lib/register-github-mcp.d.ts.map +0 -1
  105. package/lib/register-github-mcp.js +0 -35
  106. package/lib/register-github-mcp.js.map +0 -1
  107. package/lib/types.d.ts.map +0 -1
  108. package/lib/types.js.map +0 -1
  109. package/plugin.yml +0 -3
  110. package/skills/github/SKILL.md +0 -266
  111. package/src/adapter.ts +0 -448
  112. package/src/agent-prompt.ts +0 -99
  113. package/src/register-github-mcp.ts +0 -60
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zhin.js/adapter-github",
3
- "version": "1.0.1",
4
- "description": "Zhin.js adapter for GitHub (gh CLI) — treat issues/PRs as chat channels, full repo management, webhook notifications",
3
+ "version": "1.1.2",
4
+ "description": "Zhin.js GitHub adapter for Plugin Runtime (App auth + webhook)",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
@@ -18,15 +18,6 @@
18
18
  },
19
19
  "./package.json": "./package.json"
20
20
  },
21
- "files": [
22
- "src",
23
- "lib",
24
- "client",
25
- "dist",
26
- "skills",
27
- "plugin.yml",
28
- "README.md"
29
- ],
30
21
  "keywords": [
31
22
  "zhin",
32
23
  "zhin.js",
@@ -44,31 +35,110 @@
44
35
  "url": "https://github.com/lc-cn"
45
36
  },
46
37
  "license": "MIT",
47
- "devDependencies": {
48
- "typescript": "^6.0.3",
49
- "@zhin.js/host-router": "1.0.1",
50
- "zhin.js": "2.0.1"
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/zhinjs/zhin.git",
41
+ "directory": "plugins/adapters/github"
42
+ },
43
+ "dependencies": {
44
+ "@zhin.js/adapter": "1.1.14",
45
+ "@zhin.js/agent-feature": "1.1.1",
46
+ "@zhin.js/core": "1.1.37",
47
+ "@zhin.js/feature-kit": "1.1.1",
48
+ "@zhin.js/host-http": "1.1.1",
49
+ "@zhin.js/im-contract": "1.1.1",
50
+ "@zhin.js/logger": "1.1.1",
51
+ "@zhin.js/skill": "1.1.1",
52
+ "@zhin.js/tool": "1.1.1"
51
53
  },
52
54
  "peerDependencies": {
53
- "@zhin.js/host-router": "1.0.1",
54
- "zhin.js": "2.0.1"
55
+ "zod": "^4.0.0",
56
+ "@zhin.js/adapter": "^1.1.14",
57
+ "@zhin.js/agent": "^1.1.26",
58
+ "@zhin.js/command": "^1.1.1",
59
+ "@zhin.js/core": "^1.1.37",
60
+ "@zhin.js/host-http": "^1.1.1",
61
+ "@zhin.js/prompt-section": "^1.1.1",
62
+ "zhin.js": "^1.1.2"
55
63
  },
56
64
  "peerDependenciesMeta": {
57
- "@zhin.js/host-router": {
65
+ "@zhin.js/agent": {
66
+ "optional": true
67
+ },
68
+ "@zhin.js/command": {
69
+ "optional": true
70
+ },
71
+ "@zhin.js/prompt-section": {
72
+ "optional": true
73
+ },
74
+ "zhin.js": {
75
+ "optional": true
76
+ },
77
+ "zod": {
58
78
  "optional": true
59
79
  }
60
80
  },
61
- "repository": {
62
- "type": "git",
63
- "url": "git+https://github.com/zhinjs/zhin.git",
64
- "directory": "plugins/adapters/github"
81
+ "devDependencies": {
82
+ "@types/node": "^26.1.2",
83
+ "typescript": "^6.0.3",
84
+ "vitest": "^4.1.10",
85
+ "zod": "^4.4.3",
86
+ "@zhin.js/agent": "1.1.26",
87
+ "@zhin.js/prompt-section": "1.1.1",
88
+ "zhin.js": "1.1.2"
65
89
  },
90
+ "files": [
91
+ "adapters",
92
+ "commands",
93
+ "plugin.js",
94
+ "schema.json",
95
+ "src",
96
+ "lib",
97
+ "agents",
98
+ "README.md",
99
+ "CHANGELOG.md",
100
+ "prompt-sections"
101
+ ],
66
102
  "publishConfig": {
67
103
  "access": "public",
68
104
  "registry": "https://registry.npmjs.org"
69
105
  },
106
+ "engines": {
107
+ "node": "^20.19.0 || >=22.12.0"
108
+ },
109
+ "zhin": {
110
+ "protocol": 1,
111
+ "type": "plugin",
112
+ "entry": "./plugin.js",
113
+ "engine": "^1.0.0",
114
+ "runtime": "trusted",
115
+ "features": [
116
+ {
117
+ "package": "@zhin.js/adapter",
118
+ "api": "^1.0.0"
119
+ },
120
+ {
121
+ "package": "@zhin.js/command",
122
+ "api": "^1.0.0"
123
+ },
124
+ {
125
+ "package": "@zhin.js/tool",
126
+ "api": "^1.0.0"
127
+ },
128
+ {
129
+ "package": "@zhin.js/skill",
130
+ "api": "^1.0.0"
131
+ },
132
+ {
133
+ "package": "@zhin.js/agent-feature",
134
+ "api": "^1.0.0"
135
+ }
136
+ ],
137
+ "plugins": []
138
+ },
70
139
  "scripts": {
71
140
  "build": "tsc",
72
- "clean": "rimraf lib"
141
+ "clean": "rimraf lib",
142
+ "test": "NODE_OPTIONS=--experimental-strip-types vitest run --root ../../.. plugins/adapters/github/tests"
73
143
  }
74
144
  }
package/plugin.js ADDED
@@ -0,0 +1,38 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { createEndpointRuntimeState } from 'zhin.js/adapter';
3
+ import { definePlugin, databaseHostToken } from 'zhin.js';
4
+ import { githubRuntimeStateToken } from "./lib/github-runtime-state.js";
5
+ import { defineGithubOauthUsersTable } from "./lib/oauth-users.js";
6
+ /**
7
+ * github_subscriptions — repo event subscriptions per chat channel
8
+ * (used by github_subscriptions agent tool; schema is registered through DatabaseHost).
9
+ */
10
+ const GITHUB_SUBSCRIPTIONS_SCHEMA = {
11
+ id: { type: 'integer', primary: true },
12
+ repo: { type: 'text', nullable: false },
13
+ events: { type: 'json', default: [] },
14
+ target_id: { type: 'text', nullable: false },
15
+ target_type: { type: 'text', nullable: false },
16
+ adapter: { type: 'text', nullable: false },
17
+ endpoint: { type: 'text', nullable: false },
18
+ };
19
+ /**
20
+ * Plugin Runtime GitHub adapter.
21
+ * - Endpoint: `adapters/github/index.ts`
22
+ * - OAuth user tokens: define `github_oauth_users` when DatabaseHost is present
23
+ */
24
+ export default definePlugin({
25
+ name: 'github',
26
+ metadata: {
27
+ displayName: 'GitHub Adapter',
28
+ },
29
+ setup(context) {
30
+ // 运行中 endpoint 注册表(github endpoint list 的"运行中"数据源)
31
+ context.resources.provide(githubRuntimeStateToken, createEndpointRuntimeState());
32
+ if (context.resources.has(databaseHostToken)) {
33
+ const host = context.resources.use(databaseHostToken);
34
+ defineGithubOauthUsersTable(host);
35
+ host.define('github_subscriptions', { ...GITHUB_SUBSCRIPTIONS_SCHEMA });
36
+ }
37
+ },
38
+ });
@@ -0,0 +1,16 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { defineAgentPromptSection } from '@zhin.js/prompt-section';
3
+ export default defineAgentPromptSection({
4
+ title: 'GitHub',
5
+ content: [
6
+ 'On GitHub, discuss Issues and Pull Requests in their current conversation context.',
7
+ 'Use github_* tools for Bot write operations; do not use mcp_github_* writes that act as a human PAT.',
8
+ 'Use a workspace and branch for multi-file changes, then report the resulting Pull Request or branch.',
9
+ ].join('\n'),
10
+ layer: 'tools',
11
+ order: 70,
12
+ retention: 'preferred',
13
+ maxChars: 1200,
14
+ profiles: ['interactive'],
15
+ platforms: ['github'],
16
+ });
@@ -0,0 +1,16 @@
1
+ import { defineAgentPromptSection } from '@zhin.js/prompt-section';
2
+
3
+ export default defineAgentPromptSection({
4
+ title: 'GitHub',
5
+ content: [
6
+ 'On GitHub, discuss Issues and Pull Requests in their current conversation context.',
7
+ 'Use github_* tools for Bot write operations; do not use mcp_github_* writes that act as a human PAT.',
8
+ 'Use a workspace and branch for multi-file changes, then report the resulting Pull Request or branch.',
9
+ ].join('\n'),
10
+ layer: 'tools',
11
+ order: 70,
12
+ retention: 'preferred',
13
+ maxChars: 1200,
14
+ profiles: ['interactive'],
15
+ platforms: ['github'],
16
+ });
package/schema.json ADDED
@@ -0,0 +1,137 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "type": "object",
4
+ "additionalProperties": false,
5
+ "properties": {
6
+ "host": {
7
+ "type": "string",
8
+ "description": "GitHub Enterprise hostname (default github.com)"
9
+ },
10
+ "webhook_path": {
11
+ "type": "string",
12
+ "default": "/github/webhook"
13
+ },
14
+ "auto_reply_repos": {
15
+ "type": "array",
16
+ "items": {
17
+ "type": "string"
18
+ },
19
+ "description": "Repos whose Issue/PR comments auto-trigger AI without @bot"
20
+ },
21
+ "bot_login": {
22
+ "type": "string",
23
+ "description": "Override App bot login (default {slug}[bot])"
24
+ },
25
+ "workspace_root": {
26
+ "type": "string",
27
+ "description": "Managed git workspace root"
28
+ },
29
+ "master": {
30
+ "type": [
31
+ "string",
32
+ "number"
33
+ ],
34
+ "description": "框架 master(GitHub user login or id;AI/工具权限、endpoint 管理)。endpoints[i].master 可逐项覆盖"
35
+ },
36
+ "trusted": {
37
+ "type": "array",
38
+ "items": {
39
+ "type": [
40
+ "string",
41
+ "number"
42
+ ],
43
+ "description": "Trusted GitHub user login or id"
44
+ },
45
+ "description": "框架 trusted 用户列表(弱于 master)。endpoints[i].trusted 可逐项追加"
46
+ },
47
+ "endpoints": {
48
+ "type": "array",
49
+ "minItems": 1,
50
+ "description": "多账号:一个插件实例挂多个 endpoint",
51
+ "items": {
52
+ "type": "object",
53
+ "additionalProperties": false,
54
+ "properties": {
55
+ "master": {
56
+ "type": [
57
+ "string",
58
+ "number"
59
+ ],
60
+ "description": "本 endpoint 的框架 master(GitHub user login or id);覆盖顶层 master"
61
+ },
62
+ "trusted": {
63
+ "type": "array",
64
+ "items": {
65
+ "type": [
66
+ "string",
67
+ "number"
68
+ ],
69
+ "description": "Trusted GitHub user login or id"
70
+ },
71
+ "description": "本 endpoint 的 trusted 列表"
72
+ },
73
+ "app_id": {
74
+ "type": [
75
+ "string",
76
+ "number"
77
+ ],
78
+ "description": "GitHub App ID"
79
+ },
80
+ "private_key": {
81
+ "type": "string",
82
+ "description": "GitHub App private key (PEM content or file path)"
83
+ },
84
+ "webhook_secret": {
85
+ "type": "string",
86
+ "description": "Webhook HMAC secret; enables httpHostToken POST route"
87
+ },
88
+ "id": {
89
+ "type": "string",
90
+ "minLength": 1,
91
+ "description": "GitHub App bot name"
92
+ },
93
+ "host": {
94
+ "type": "string",
95
+ "description": "GitHub Enterprise hostname"
96
+ },
97
+ "webhook_path": {
98
+ "type": "string",
99
+ "description": "Webhook route path"
100
+ },
101
+ "auto_reply_repos": {
102
+ "type": "array",
103
+ "items": {
104
+ "type": "string"
105
+ },
106
+ "description": "Repos whose comments auto-trigger AI"
107
+ },
108
+ "bot_login": {
109
+ "type": "string",
110
+ "description": "Override App bot login"
111
+ },
112
+ "workspace_root": {
113
+ "type": "string",
114
+ "description": "Managed git workspace root"
115
+ },
116
+ "commandPrefix": {
117
+ "type": "string",
118
+ "description": "本 endpoint 的命令前缀"
119
+ }
120
+ },
121
+ "required": [
122
+ "id",
123
+ "app_id",
124
+ "private_key"
125
+ ]
126
+ }
127
+ },
128
+ "commandPrefix": {
129
+ "type": "string",
130
+ "default": "",
131
+ "description": "命令前缀(默认 '' 无前缀,任意文本按命令匹配;如 '/' 要求 / 开头)。endpoints[i] 可逐项覆盖"
132
+ }
133
+ },
134
+ "required": [
135
+ "endpoints"
136
+ ]
137
+ }
package/src/client.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { PluginDatabaseHost } from 'zhin.js';
2
+ import { defineEndpointClient } from 'zhin.js/adapter';
3
+ import { GhClient } from './gh-client.js';
4
+ import { lookupGithubOauthAccessToken } from './oauth-users.js';
5
+ import type { ResolvedGithubConfig } from './protocol.js';
6
+ import { WorkspaceManager } from './workspace-manager.js';
7
+
8
+ /** GitHub SDK surface exposed to plugin handlers and Agent tools. */
9
+ export class GithubClient {
10
+ #workspaceManager?: WorkspaceManager;
11
+
12
+ constructor(
13
+ readonly name: string,
14
+ readonly api: GhClient,
15
+ readonly config: ResolvedGithubConfig,
16
+ readonly database?: PluginDatabaseHost,
17
+ ) {}
18
+
19
+ async getUserOrDefaultApi(platform?: string, platformUid?: string): Promise<GhClient> {
20
+ if (platform && platformUid) {
21
+ const token = await lookupGithubOauthAccessToken(this.database, platform, platformUid);
22
+ if (token) return this.api.withToken(token);
23
+ }
24
+ return this.api;
25
+ }
26
+
27
+ get clientId(): string | null {
28
+ return this.api.clientId || null;
29
+ }
30
+
31
+ get host(): string | undefined {
32
+ return this.config.host;
33
+ }
34
+
35
+ get appSlug(): string | null {
36
+ return this.api.appSlug || null;
37
+ }
38
+
39
+ get installations() {
40
+ return this.api.installations || [];
41
+ }
42
+
43
+ get workspaceManager(): WorkspaceManager {
44
+ if (!this.#workspaceManager) {
45
+ this.#workspaceManager = new WorkspaceManager(this.api, this.config.workspaceRoot);
46
+ }
47
+ return this.#workspaceManager;
48
+ }
49
+ }
50
+
51
+ export type GithubClientEventMap = Record<string, unknown>;
52
+
53
+ declare module '@zhin.js/feature-kit' {
54
+ interface AdapterClientRegistry {
55
+ readonly github: {
56
+ readonly client: GithubClient;
57
+ readonly events: GithubClientEventMap;
58
+ };
59
+ }
60
+ }
61
+
62
+ export const githubClient = defineEndpointClient<GithubClient, GithubClientEventMap>('github');