@zhin.js/adapter-github 6.0.0 → 6.0.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.
- package/CHANGELOG.md +34 -0
- package/README.md +18 -2
- package/adapters/github.js +0 -7
- package/adapters/github.ts +0 -7
- package/agent/prompt-sections/platform.ts +16 -0
- package/agent/tools/bind.ts +4 -3
- package/agent/tools/create_pr.ts +3 -2
- package/agent/tools/install.ts +4 -3
- package/agent/tools/patch_file.ts +3 -2
- package/agent/tools/prepare_workspace.ts +3 -2
- package/agent/tools/push_branch.ts +3 -2
- package/agent/tools/star.ts +3 -2
- package/agent/tools/subscribe.ts +3 -3
- package/agent/tools/subscriptions.ts +4 -4
- package/agent/tools/unbind.ts +4 -3
- package/agent/tools/unsubscribe.ts +3 -3
- package/agent/tools/whoami.ts +4 -3
- package/lib/client.d.ts +36 -0
- package/lib/client.js +47 -0
- package/lib/endpoint.d.ts +10 -24
- package/lib/endpoint.js +35 -65
- package/lib/github-bot-handlers.d.ts +5 -4
- package/lib/github-bot-handlers.js +12 -13
- package/lib/github-endpoint-commands.d.ts +1 -1
- package/lib/github-tool-handlers.d.ts +9 -8
- package/lib/github-tool-handlers.js +26 -33
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/protocol.d.ts +1 -1
- package/lib/protocol.js +1 -1
- package/lib/webhook.d.ts +1 -0
- package/lib/webhook.js +1 -0
- package/package.json +19 -13
- package/plugin.js +0 -17
- package/src/client.ts +65 -0
- package/src/endpoint.ts +36 -75
- package/src/github-bot-handlers.ts +13 -9
- package/src/github-tool-handlers.ts +27 -32
- package/src/index.ts +1 -9
- package/src/protocol.ts +1 -1
- package/src/webhook.ts +5 -0
- package/lib/agent-prompt.d.ts +0 -2
- package/lib/agent-prompt.js +0 -84
- package/lib/github-agent-deps.d.ts +0 -47
- package/lib/github-agent-deps.js +0 -43
- package/src/agent-prompt.ts +0 -101
- package/src/github-agent-deps.ts +0 -82
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent tool deps for github.
|
|
3
|
-
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
-
*/
|
|
5
|
-
import type { GhClient } from './gh-client.js';
|
|
6
|
-
import type { ResolvedGithubConfig } from './protocol.js';
|
|
7
|
-
import type { WorkspaceManager } from './workspace-manager.js';
|
|
8
|
-
export interface GithubAgentEndpoint {
|
|
9
|
-
readonly name: string;
|
|
10
|
-
readonly gh: GhClient;
|
|
11
|
-
readonly config: ResolvedGithubConfig;
|
|
12
|
-
getAPI(): GhClient;
|
|
13
|
-
getUserOrDefaultAPI(platform?: string, platformUid?: string): Promise<GhClient | null>;
|
|
14
|
-
getClientId(): string | null;
|
|
15
|
-
getHost(): string | undefined;
|
|
16
|
-
getAppSlug(): string | null;
|
|
17
|
-
getInstallations(): Array<{
|
|
18
|
-
id: number;
|
|
19
|
-
account: {
|
|
20
|
-
login: string;
|
|
21
|
-
type: string;
|
|
22
|
-
};
|
|
23
|
-
target_type: string;
|
|
24
|
-
}>;
|
|
25
|
-
getWorkspaceManager(): WorkspaceManager;
|
|
26
|
-
/** DatabaseHost wired at endpoint creation (optional). */
|
|
27
|
-
getDatabase?(): unknown;
|
|
28
|
-
}
|
|
29
|
-
export interface GithubAgentDeps {
|
|
30
|
-
getEndpoint: (endpointKey?: string) => GithubAgentEndpoint;
|
|
31
|
-
/** Alias kept for existing agent handlers that call getAdapter(). */
|
|
32
|
-
getAdapter: () => GithubAgentEndpoint;
|
|
33
|
-
getWorkspaceManager: () => WorkspaceManager;
|
|
34
|
-
getDatabase?: () => {
|
|
35
|
-
models?: Map<string, unknown>;
|
|
36
|
-
} | null | undefined;
|
|
37
|
-
logger?: {
|
|
38
|
-
debug: (...args: unknown[]) => void;
|
|
39
|
-
warn: (...args: unknown[]) => void;
|
|
40
|
-
error: (...args: unknown[]) => void;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
export declare function registerGithubAgentEndpoint(endpointKey: string, endpoint: GithubAgentEndpoint): () => void;
|
|
44
|
-
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
45
|
-
export declare function setGithubAgentDeps(deps: GithubAgentDeps | null): void;
|
|
46
|
-
export declare function getGithubAgentDeps(): GithubAgentDeps;
|
|
47
|
-
export declare function getAdapter(): GithubAgentEndpoint;
|
package/lib/github-agent-deps.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent tool deps for github.
|
|
3
|
-
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
-
*/
|
|
5
|
-
const endpoints = new Map();
|
|
6
|
-
let override = null;
|
|
7
|
-
export function registerGithubAgentEndpoint(endpointKey, endpoint) {
|
|
8
|
-
endpoints.set(endpointKey, endpoint);
|
|
9
|
-
return () => {
|
|
10
|
-
if (endpoints.get(endpointKey) === endpoint) {
|
|
11
|
-
endpoints.delete(endpointKey);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
16
|
-
export function setGithubAgentDeps(deps) {
|
|
17
|
-
override = deps;
|
|
18
|
-
}
|
|
19
|
-
function lookup(endpointKey) {
|
|
20
|
-
if (endpointKey) {
|
|
21
|
-
const registered = endpoints.get(endpointKey);
|
|
22
|
-
if (!registered)
|
|
23
|
-
throw new Error(`Endpoint ${endpointKey} 不存在`);
|
|
24
|
-
return registered;
|
|
25
|
-
}
|
|
26
|
-
const first = endpoints.values().next().value;
|
|
27
|
-
if (!first)
|
|
28
|
-
throw new Error('github agent deps not initialized');
|
|
29
|
-
return first;
|
|
30
|
-
}
|
|
31
|
-
export function getGithubAgentDeps() {
|
|
32
|
-
if (override)
|
|
33
|
-
return override;
|
|
34
|
-
return {
|
|
35
|
-
getEndpoint: lookup,
|
|
36
|
-
getAdapter: () => lookup(),
|
|
37
|
-
getWorkspaceManager: () => lookup().getWorkspaceManager(),
|
|
38
|
-
getDatabase: () => lookup().getDatabase?.(),
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
export function getAdapter() {
|
|
42
|
-
return getGithubAgentDeps().getAdapter();
|
|
43
|
-
}
|
package/src/agent-prompt.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AgentPromptBuildContext,
|
|
3
|
-
AgentPromptContributor,
|
|
4
|
-
AgentPromptSection,
|
|
5
|
-
DeferredToolCatalogItem,
|
|
6
|
-
} from 'zhin.js';
|
|
7
|
-
import { filterTools, type AgentTool } from 'zhin.js/ai';
|
|
8
|
-
|
|
9
|
-
function selectGithubDeferredTools(
|
|
10
|
-
query: string,
|
|
11
|
-
goal: string,
|
|
12
|
-
deferredCatalog: DeferredToolCatalogItem[],
|
|
13
|
-
maxTools: number,
|
|
14
|
-
): DeferredToolCatalogItem[] {
|
|
15
|
-
const pool = deferredCatalog.filter(
|
|
16
|
-
t => !t.name.startsWith('mcp_filesystem') && !t.name.startsWith('mcp_icqq_'),
|
|
17
|
-
);
|
|
18
|
-
const pinned: DeferredToolCatalogItem[] = [];
|
|
19
|
-
const bash = pool.find(t => t.name === 'bash');
|
|
20
|
-
if (bash) pinned.push(bash);
|
|
21
|
-
|
|
22
|
-
const preferNames = [
|
|
23
|
-
...pool.filter(t => t.name.startsWith('github_')).map(t => t.name),
|
|
24
|
-
...pool.filter(t => t.name.startsWith('mcp_github_')).map(t => t.name),
|
|
25
|
-
];
|
|
26
|
-
for (const name of preferNames) {
|
|
27
|
-
if (pinned.length >= maxTools) break;
|
|
28
|
-
const t = pool.find(x => x.name === name);
|
|
29
|
-
if (t && !pinned.some(p => p.name === name)) pinned.push(t);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const extra = filterTools(`${query} ${goal}`, pool as AgentTool[], { maxTools, minScore: 0.08 })
|
|
33
|
-
.map((t) => ({ name: t.name, description: t.description }));
|
|
34
|
-
|
|
35
|
-
const merged: DeferredToolCatalogItem[] = [...pinned];
|
|
36
|
-
for (const t of extra) {
|
|
37
|
-
if (merged.length >= maxTools) break;
|
|
38
|
-
if (!merged.some(p => p.name === t.name)) merged.push(t);
|
|
39
|
-
}
|
|
40
|
-
return merged.slice(0, maxTools);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function isGithubDelegatedTask(query: string, goal: string): boolean {
|
|
44
|
-
const text = `${query} ${goal}`;
|
|
45
|
-
return /github|gh_|mcp_github|\bissue\b|pull\s*request|\bpr\b/i.test(text);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const ORCHESTRATOR_GITHUB = [
|
|
49
|
-
'On GitHub: use run_deferred_task with tool_query "github_".',
|
|
50
|
-
'Discuss issues/PRs in chat context; do not call github_* tools on this orchestrator.',
|
|
51
|
-
'Bot write operations use github_* tools (Installation Token), not mcp_github_*.',
|
|
52
|
-
].join('\n');
|
|
53
|
-
|
|
54
|
-
const WORKER_GITHUB = [
|
|
55
|
-
'Use github_prepare_workspace before multi-file edits in a repo.',
|
|
56
|
-
'Small single-file change: github_patch_file (Contents API).',
|
|
57
|
-
'Multi-file / tests: workspace + bash, then github_push_branch (requires approval) and github_create_pr for Issues.',
|
|
58
|
-
'Issue thread: new branch + new PR. PR thread: push to existing PR head branch.',
|
|
59
|
-
'Do NOT use mcp_github_* for writes — PAT acts as human, not Bot.',
|
|
60
|
-
'Summarize outcomes (PR link, branch) for the orchestrator.',
|
|
61
|
-
].map(line => `- ${line}`).join('\n');
|
|
62
|
-
|
|
63
|
-
export function createGithubAgentPromptContributor(): AgentPromptContributor {
|
|
64
|
-
return {
|
|
65
|
-
platform: 'github',
|
|
66
|
-
|
|
67
|
-
async buildSections(ctx: AgentPromptBuildContext): Promise<AgentPromptSection[] | null> {
|
|
68
|
-
if (ctx.slot === 'orchestrator') {
|
|
69
|
-
return [{
|
|
70
|
-
id: 'platform.github.orchestrator',
|
|
71
|
-
title: '## GitHub',
|
|
72
|
-
body: ORCHESTRATOR_GITHUB,
|
|
73
|
-
priority: 50,
|
|
74
|
-
}];
|
|
75
|
-
}
|
|
76
|
-
if (ctx.slot === 'deferred_worker') {
|
|
77
|
-
const query = ctx.deferred?.toolQuery ?? ctx.deferred?.goal ?? '';
|
|
78
|
-
const goal = ctx.deferred?.goal ?? '';
|
|
79
|
-
if (!isGithubDelegatedTask(query, goal)) return null;
|
|
80
|
-
return [{
|
|
81
|
-
id: 'platform.github.deferred_worker',
|
|
82
|
-
title: '## GitHub(本任务)',
|
|
83
|
-
body: WORKER_GITHUB,
|
|
84
|
-
priority: 50,
|
|
85
|
-
}];
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
matchesDeferredTask(ctx: AgentPromptBuildContext): boolean {
|
|
91
|
-
const query = ctx.deferred?.toolQuery ?? ctx.deferred?.goal ?? ctx.userMessagePreview ?? '';
|
|
92
|
-
const goal = ctx.deferred?.goal ?? ctx.userMessagePreview ?? '';
|
|
93
|
-
return isGithubDelegatedTask(query, goal);
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
selectDeferredTools(query, goal, catalog, maxTools) {
|
|
97
|
-
if (!isGithubDelegatedTask(query, goal)) return null;
|
|
98
|
-
return selectGithubDeferredTools(query, goal, catalog, maxTools);
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
}
|
package/src/github-agent-deps.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent tool deps for github.
|
|
3
|
-
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { GhClient } from './gh-client.js';
|
|
7
|
-
import type { ResolvedGithubConfig } from './protocol.js';
|
|
8
|
-
import type { WorkspaceManager } from './workspace-manager.js';
|
|
9
|
-
|
|
10
|
-
export interface GithubAgentEndpoint {
|
|
11
|
-
readonly name: string;
|
|
12
|
-
readonly gh: GhClient;
|
|
13
|
-
readonly config: ResolvedGithubConfig;
|
|
14
|
-
getAPI(): GhClient;
|
|
15
|
-
getUserOrDefaultAPI(platform?: string, platformUid?: string): Promise<GhClient | null>;
|
|
16
|
-
getClientId(): string | null;
|
|
17
|
-
getHost(): string | undefined;
|
|
18
|
-
getAppSlug(): string | null;
|
|
19
|
-
getInstallations(): Array<{ id: number; account: { login: string; type: string }; target_type: string }>;
|
|
20
|
-
getWorkspaceManager(): WorkspaceManager;
|
|
21
|
-
/** DatabaseHost wired at endpoint creation (optional). */
|
|
22
|
-
getDatabase?(): unknown;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface GithubAgentDeps {
|
|
26
|
-
getEndpoint: (endpointKey?: string) => GithubAgentEndpoint;
|
|
27
|
-
/** Alias kept for existing agent handlers that call getAdapter(). */
|
|
28
|
-
getAdapter: () => GithubAgentEndpoint;
|
|
29
|
-
getWorkspaceManager: () => WorkspaceManager;
|
|
30
|
-
getDatabase?: () => { models?: Map<string, unknown> } | null | undefined;
|
|
31
|
-
logger?: {
|
|
32
|
-
debug: (...args: unknown[]) => void;
|
|
33
|
-
warn: (...args: unknown[]) => void;
|
|
34
|
-
error: (...args: unknown[]) => void;
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const endpoints = new Map<string, GithubAgentEndpoint>();
|
|
39
|
-
let override: GithubAgentDeps | null = null;
|
|
40
|
-
|
|
41
|
-
export function registerGithubAgentEndpoint(
|
|
42
|
-
endpointKey: string,
|
|
43
|
-
endpoint: GithubAgentEndpoint,
|
|
44
|
-
): () => void {
|
|
45
|
-
endpoints.set(endpointKey, endpoint);
|
|
46
|
-
return () => {
|
|
47
|
-
if (endpoints.get(endpointKey) === endpoint) {
|
|
48
|
-
endpoints.delete(endpointKey);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
54
|
-
export function setGithubAgentDeps(deps: GithubAgentDeps | null): void {
|
|
55
|
-
override = deps;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function lookup(endpointKey?: string): GithubAgentEndpoint {
|
|
59
|
-
if (endpointKey) {
|
|
60
|
-
const registered = endpoints.get(endpointKey);
|
|
61
|
-
if (!registered) throw new Error(`Endpoint ${endpointKey} 不存在`);
|
|
62
|
-
return registered;
|
|
63
|
-
}
|
|
64
|
-
const first = endpoints.values().next().value;
|
|
65
|
-
if (!first) throw new Error('github agent deps not initialized');
|
|
66
|
-
return first;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function getGithubAgentDeps(): GithubAgentDeps {
|
|
70
|
-
if (override) return override;
|
|
71
|
-
return {
|
|
72
|
-
getEndpoint: lookup,
|
|
73
|
-
getAdapter: () => lookup(),
|
|
74
|
-
getWorkspaceManager: () => lookup().getWorkspaceManager(),
|
|
75
|
-
getDatabase: () => lookup().getDatabase?.() as
|
|
76
|
-
{ models?: Map<string, unknown> } | null | undefined,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function getAdapter(): GithubAgentEndpoint {
|
|
81
|
-
return getGithubAgentDeps().getAdapter();
|
|
82
|
-
}
|