-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathgit.ts
More file actions
197 lines (184 loc) · 6.28 KB
/
git.ts
File metadata and controls
197 lines (184 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { resolve } from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { AgentManifest, loadAgentManifest, agentDirExists } from '../utils/loader.js';
import { resolveRepo, ResolveRepoOptions } from '../utils/git-cache.js';
import { exportToSystemPrompt } from '../adapters/system-prompt.js';
import { runWithClaude } from './claude.js';
import { runWithOpenAI } from './openai.js';
import { runWithCrewAI } from './crewai.js';
import { runWithOpenClaw } from './openclaw.js';
import { runWithNanobot } from './nanobot.js';
import { runWithLyzr } from './lyzr.js';
import { runWithGitHub } from './github.js';
import { runWithOpenCode } from './opencode.js';
import { error, info, success, label, heading, divider, warn } from '../utils/format.js';
export interface GitRunOptions {
repo: string;
branch?: string;
refresh?: boolean;
noCache?: boolean;
adapter?: string;
prompt?: string;
workspace?: string;
}
/**
* Clone a git repo containing a gitagent agent and run it with the
* adapter specified in agent.yaml (or overridden via --adapter).
*
* This is the "one-command" runner:
* gitagent run -a git -r https://github.com/user/agent -p "Hello"
*
* It resolves the repo, reads agent.yaml to detect the best adapter,
* then delegates to the appropriate runner.
*/
export async function runWithGit(
repoUrl: string,
options: GitRunOptions,
): Promise<void> {
heading('Git Runner');
label('Repository', repoUrl);
label('Branch', options.branch ?? 'main');
// Clone / resolve from cache
let agentDir: string;
let cleanup: (() => void) | undefined;
try {
const result = resolveRepo(repoUrl, {
branch: options.branch,
refresh: options.refresh,
noCache: options.noCache,
});
agentDir = result.dir;
cleanup = result.cleanup;
success(`Resolved to ${agentDir}`);
} catch (e) {
error(`Failed to clone repository: ${(e as Error).message}`);
process.exit(1);
}
// Validate
if (!agentDirExists(agentDir)) {
error(`No agent.yaml found in ${agentDir}`);
if (cleanup) cleanup();
process.exit(1);
}
let manifest: AgentManifest;
try {
manifest = loadAgentManifest(agentDir);
} catch (e) {
error(`Failed to load manifest: ${(e as Error).message}`);
if (cleanup) cleanup();
process.exit(1);
}
// Detect adapter: explicit override > .gitagent_adapter file > model-based detection > claude
const adapter = options.adapter ?? detectAdapter(agentDir, manifest);
divider();
heading(`Running: ${manifest.name} v${manifest.version}`);
label('Description', manifest.description);
if (manifest.model?.preferred) {
label('Model', manifest.model.preferred);
}
label('Adapter', adapter);
if (options.workspace) {
label('Workspace', resolve(options.workspace));
}
divider();
try {
switch (adapter) {
case 'claude':
runWithClaude(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'openai':
runWithOpenAI(agentDir, manifest, { workspace: options.workspace });
break;
case 'crewai':
runWithCrewAI(agentDir, manifest, { workspace: options.workspace });
break;
case 'openclaw':
runWithOpenClaw(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'nanobot':
runWithNanobot(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'opencode':
runWithOpenCode(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'lyzr':
await runWithLyzr(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'github':
await runWithGitHub(agentDir, manifest, { prompt: options.prompt, workspace: options.workspace });
break;
case 'prompt':
console.log(exportToSystemPrompt(agentDir));
break;
default:
error(`Unknown adapter: ${adapter}`);
process.exit(1);
}
} catch (e) {
error(`Run failed: ${(e as Error).message}`);
process.exit(1);
} finally {
if (cleanup) cleanup();
}
}
/**
* Auto-detect the best adapter from the agent definition.
*
* Priority:
* 1. .gitagent_adapter file in the repo (explicit preference)
* 2. Model name hints (claude-* → claude, gpt-* → openai)
* 3. Presence of framework-specific files (CLAUDE.md, .cursorrules, etc.)
* 4. Default: claude
*/
function detectAdapter(agentDir: string, manifest: AgentManifest): string {
// 1. Explicit adapter file
const adapterFile = join(agentDir, '.gitagent_adapter');
if (existsSync(adapterFile)) {
const val = readFileSync(adapterFile, 'utf-8').trim().toLowerCase();
if (val) {
info(`Detected adapter from .gitagent_adapter: ${val}`);
return val;
}
}
// 2. Model name hints
const model = manifest.model?.preferred;
if (model) {
if (model.startsWith('claude')) {
info('Auto-detected adapter: claude (from model preference)');
return 'claude';
}
if (model.startsWith('gpt') || model.startsWith('o1') || model.startsWith('o3')) {
info('Auto-detected adapter: openai (from model preference)');
return 'openai';
}
}
// 3. Framework-specific file hints
if (existsSync(join(agentDir, 'CLAUDE.md')) || existsSync(join(agentDir, '.claude'))) {
info('Auto-detected adapter: claude (from CLAUDE.md/.claude)');
return 'claude';
}
if (existsSync(join(agentDir, '.cursorrules'))) {
info('Auto-detected adapter: openai (from .cursorrules)');
return 'openai';
}
if (existsSync(join(agentDir, 'crew.yaml')) || existsSync(join(agentDir, 'crewai.yaml'))) {
info('Auto-detected adapter: crewai (from crew.yaml)');
return 'crewai';
}
if (existsSync(join(agentDir, '.lyzr_agent_id'))) {
info('Auto-detected adapter: lyzr (from .lyzr_agent_id)');
return 'lyzr';
}
if (existsSync(join(agentDir, '.github_models'))) {
info('Auto-detected adapter: github (from .github_models)');
return 'github';
}
if (existsSync(join(agentDir, 'opencode.json'))) {
info('Auto-detected adapter: opencode (from opencode.json)');
return 'opencode';
}
// 4. Default
info('Using default adapter: claude');
return 'claude';
}