gm-copilot-cli 2.0.246 → 2.0.248
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/copilot-profile.md +1 -1
- package/index.html +1 -1
- package/manifest.yml +1 -1
- package/package.json +1 -1
- package/tools.json +1 -1
- package/hooks/pre-tool-use-hook.js +0 -504
- package/hooks/prompt-submit-hook.js +0 -177
- package/hooks/session-end-git-hook.js +0 -190
- package/hooks/session-end-hook.js +0 -57
- package/hooks/session-start-hook.js +0 -188
package/copilot-profile.md
CHANGED
package/index.html
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<script type="module">
|
|
19
19
|
import { createElement as h, applyDiff, Fragment } from "webjsx";
|
|
20
20
|
const PLATFORM_NAME="Copilot CLI",PLATFORM_TYPE="CLI Tool",PLATFORM_TYPE_COLOR="#3b82f6";
|
|
21
|
-
const DESCRIPTION="State machine agent with hooks, skills, and automated git enforcement",VERSION="2.0.
|
|
21
|
+
const DESCRIPTION="State machine agent with hooks, skills, and automated git enforcement",VERSION="2.0.248";
|
|
22
22
|
const GITHUB_URL="https://github.com/AnEntrypoint/gm-copilot-cli",BADGE_LABEL="copilot-cli";
|
|
23
23
|
const FEATURES=[{"title":"State Machine","desc":"Immutable PLAN→EXECUTE→EMIT→VERIFY→COMPLETE phases with full mutable tracking"},{"title":"Semantic Search","desc":"Natural language codebase exploration via codesearch skill — no grep needed"},{"title":"Hooks","desc":"Pre-tool, session-start, prompt-submit, and stop hooks for full lifecycle control"},{"title":"Agents","desc":"gm, codesearch, and websearch agents pre-configured and ready to use"},{"title":"MCP Integration","desc":"Model Context Protocol server support built in"},{"title":"Auto-Recovery","desc":"Supervisor hierarchy ensures the system never crashes"}],INSTALL_STEPS=[{"desc":"Install via GitHub CLI","cmd":"gh extension install AnEntrypoint/gm-copilot-cli"},{"desc":"Restart your terminal — activates automatically"}];
|
|
24
24
|
const CURRENT_PLATFORM="gm-copilot-cli";
|
package/manifest.yml
CHANGED
package/package.json
CHANGED
package/tools.json
CHANGED
|
@@ -1,504 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const os = require('os');
|
|
6
|
-
const { execSync, spawnSync } = require('child_process');
|
|
7
|
-
|
|
8
|
-
const isGemini = process.env.GEMINI_PROJECT_DIR !== undefined;
|
|
9
|
-
const IS_WIN = process.platform === 'win32';
|
|
10
|
-
const projectDir = process.env.CLAUDE_PROJECT_DIR || process.env.GEMINI_PROJECT_DIR || process.env.OC_PROJECT_DIR || process.env.KILO_PROJECT_DIR;
|
|
11
|
-
|
|
12
|
-
// ─── Local tool management ────────────────────────────────────────────────────
|
|
13
|
-
const TOOLS_DIR = path.join(os.homedir(), '.claude', 'gm-tools');
|
|
14
|
-
const CHECK_STAMP = path.join(TOOLS_DIR, '.last-check');
|
|
15
|
-
const PKG_JSON = path.join(TOOLS_DIR, 'package.json');
|
|
16
|
-
const MANAGED_PKGS = ['agent-browser'];
|
|
17
|
-
const CHECK_INTERVAL_MS = 60 * 1000; // 60 seconds
|
|
18
|
-
|
|
19
|
-
function ensureToolsDir() {
|
|
20
|
-
try { fs.mkdirSync(TOOLS_DIR, { recursive: true }); } catch {}
|
|
21
|
-
if (!fs.existsSync(PKG_JSON)) {
|
|
22
|
-
try { fs.writeFileSync(PKG_JSON, JSON.stringify({ name: 'gm-tools', version: '1.0.0', private: true })); } catch {}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function localBin(name) {
|
|
27
|
-
const ext = IS_WIN ? '.exe' : '';
|
|
28
|
-
return path.join(TOOLS_DIR, 'node_modules', '.bin', name + ext);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function isInstalled(name) {
|
|
32
|
-
return fs.existsSync(localBin(name));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function installPkg(name) {
|
|
36
|
-
try {
|
|
37
|
-
spawnSync('bun', ['add', name + '@latest'], {
|
|
38
|
-
cwd: TOOLS_DIR, encoding: 'utf8', timeout: 120000, windowsHide: true
|
|
39
|
-
});
|
|
40
|
-
} catch {}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getInstalledVersion(name) {
|
|
44
|
-
try {
|
|
45
|
-
const p = path.join(TOOLS_DIR, 'node_modules', name, 'package.json');
|
|
46
|
-
return JSON.parse(fs.readFileSync(p, 'utf8')).version;
|
|
47
|
-
} catch { return null; }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function getLatestVersion(name) {
|
|
51
|
-
try {
|
|
52
|
-
const https = require('https');
|
|
53
|
-
return new Promise((resolve) => {
|
|
54
|
-
const req = https.get(
|
|
55
|
-
`https://registry.npmjs.org/${name}/latest`,
|
|
56
|
-
{ headers: { Accept: 'application/json' } },
|
|
57
|
-
(res) => {
|
|
58
|
-
let d = '';
|
|
59
|
-
res.on('data', c => d += c);
|
|
60
|
-
res.on('end', () => { try { resolve(JSON.parse(d).version); } catch { resolve(null); } });
|
|
61
|
-
}
|
|
62
|
-
);
|
|
63
|
-
req.setTimeout(5000, () => { req.destroy(); resolve(null); });
|
|
64
|
-
req.on('error', () => resolve(null));
|
|
65
|
-
});
|
|
66
|
-
} catch { return Promise.resolve(null); }
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function shouldCheck() {
|
|
70
|
-
try {
|
|
71
|
-
const t = parseInt(fs.readFileSync(CHECK_STAMP, 'utf8').trim(), 10);
|
|
72
|
-
return isNaN(t) || (Date.now() - t) > CHECK_INTERVAL_MS;
|
|
73
|
-
} catch { return true; }
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function stampCheck() {
|
|
77
|
-
try { fs.writeFileSync(CHECK_STAMP, String(Date.now())); } catch {}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async function ensureTools() {
|
|
81
|
-
ensureToolsDir();
|
|
82
|
-
// Install any missing packages immediately (synchronous first-run)
|
|
83
|
-
const missing = MANAGED_PKGS.filter(p => !isInstalled(p));
|
|
84
|
-
if (missing.length > 0) {
|
|
85
|
-
try {
|
|
86
|
-
spawnSync('bun', ['add', ...missing.map(p => p + '@latest')], {
|
|
87
|
-
cwd: TOOLS_DIR, encoding: 'utf8', timeout: 180000, windowsHide: true
|
|
88
|
-
});
|
|
89
|
-
} catch {}
|
|
90
|
-
}
|
|
91
|
-
// Async version check (non-blocking — fire and forget)
|
|
92
|
-
if (shouldCheck()) {
|
|
93
|
-
stampCheck();
|
|
94
|
-
(async () => {
|
|
95
|
-
for (const name of MANAGED_PKGS) {
|
|
96
|
-
const installed = getInstalledVersion(name);
|
|
97
|
-
if (!installed) { installPkg(name); continue; }
|
|
98
|
-
const latest = await getLatestVersion(name);
|
|
99
|
-
if (latest && latest !== installed) installPkg(name);
|
|
100
|
-
}
|
|
101
|
-
})().catch(() => {});
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Run tool installation (fire-and-forget, won't block hook)
|
|
106
|
-
ensureTools().catch(() => {});
|
|
107
|
-
|
|
108
|
-
// ─── Lang plugin loader ───────────────────────────────────────────────────────
|
|
109
|
-
function loadLangPlugins(projectDir) {
|
|
110
|
-
if (!projectDir) return [];
|
|
111
|
-
const langDir = path.join(projectDir, 'lang');
|
|
112
|
-
if (!fs.existsSync(langDir)) return [];
|
|
113
|
-
try {
|
|
114
|
-
return fs.readdirSync(langDir)
|
|
115
|
-
.filter(f => f.endsWith('.js') && f !== 'loader.js' && f !== 'SPEC.md')
|
|
116
|
-
.reduce((acc, f) => {
|
|
117
|
-
try {
|
|
118
|
-
const p = require(path.join(langDir, f));
|
|
119
|
-
if (p && typeof p.id === 'string' && p.exec && p.exec.match instanceof RegExp && typeof p.exec.run === 'function') acc.push(p);
|
|
120
|
-
} catch (_) {}
|
|
121
|
-
return acc;
|
|
122
|
-
}, []);
|
|
123
|
-
} catch (_) { return []; }
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Helper: run a local binary (falls back to bunx if not installed)
|
|
127
|
-
function pkgEntry(name) {
|
|
128
|
-
try {
|
|
129
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(TOOLS_DIR, 'node_modules', name, 'package.json'), 'utf8'));
|
|
130
|
-
const binVal = pkg.bin;
|
|
131
|
-
const rel = typeof binVal === 'string' ? binVal : (binVal?.[name] || Object.values(binVal || {})[0]);
|
|
132
|
-
if (rel) return path.join(TOOLS_DIR, 'node_modules', name, rel);
|
|
133
|
-
} catch {}
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function runLocal(name, args, opts = {}) {
|
|
138
|
-
if (IS_WIN) {
|
|
139
|
-
const entry = pkgEntry(name);
|
|
140
|
-
if (entry && fs.existsSync(entry)) {
|
|
141
|
-
return spawnSync('bun', [entry, ...args], { encoding: 'utf8', windowsHide: true, timeout: 65000, ...opts });
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const bin = localBin(name);
|
|
145
|
-
if (fs.existsSync(bin)) {
|
|
146
|
-
return spawnSync(bin, args, { encoding: 'utf8', windowsHide: true, timeout: 65000, ...opts });
|
|
147
|
-
}
|
|
148
|
-
return spawnSync('bun', ['x', name, ...args], { encoding: 'utf8', windowsHide: true, timeout: 65000, ...opts });
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// ─── Hook helpers ─────────────────────────────────────────────────────────────
|
|
152
|
-
const writeTools = ['Write', 'write_file'];
|
|
153
|
-
const searchTools = ['glob', 'search_file_content', 'Search', 'search'];
|
|
154
|
-
const forbiddenTools = ['find', 'Find', 'Glob', 'Grep'];
|
|
155
|
-
|
|
156
|
-
const allow = (additionalContext) => ({
|
|
157
|
-
hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'allow', ...(additionalContext && { additionalContext }) }
|
|
158
|
-
});
|
|
159
|
-
const deny = (reason) => isGemini
|
|
160
|
-
? { decision: 'deny', reason }
|
|
161
|
-
: { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: reason } };
|
|
162
|
-
|
|
163
|
-
// Write output to tmp file and redirect the Bash command to read+delete it via bun (no popup)
|
|
164
|
-
const allowWithNoop = (context) => {
|
|
165
|
-
const tmp = path.join(os.tmpdir(), `gm-out-${Date.now()}.txt`);
|
|
166
|
-
fs.writeFileSync(tmp, context, 'utf-8');
|
|
167
|
-
// Use bun -e to read and print file — windowsHide applies to child, and bun itself is hidden
|
|
168
|
-
// cmd /c type also works without popup since cmd.exe is the host shell
|
|
169
|
-
const cmd = IS_WIN
|
|
170
|
-
? `bun -e "process.stdout.write(require('fs').readFileSync(process.argv[1],'utf8'));require('fs').unlinkSync(process.argv[1])" "${tmp}"`
|
|
171
|
-
: `cat '${tmp}'; rm -f '${tmp}'`;
|
|
172
|
-
return {
|
|
173
|
-
hookSpecificOutput: {
|
|
174
|
-
hookEventName: 'PreToolUse',
|
|
175
|
-
permissionDecision: 'allow',
|
|
176
|
-
updatedInput: { command: cmd }
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
// ─── plugkit runner helper ────────────────────────────────────────────────────
|
|
182
|
-
function plugkitBin() { return path.join(TOOLS_DIR, IS_WIN ? 'plugkit.exe' : 'plugkit'); }
|
|
183
|
-
|
|
184
|
-
function runGmExec(args, opts = {}) {
|
|
185
|
-
const bin = plugkitBin();
|
|
186
|
-
if (fs.existsSync(bin)) {
|
|
187
|
-
return spawnSync(bin, args, { encoding: 'utf8', windowsHide: true, timeout: 65000, ...opts });
|
|
188
|
-
}
|
|
189
|
-
return spawnSync('plugkit', args, { encoding: 'utf8', windowsHide: true, timeout: 65000, ...opts });
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// ─── Main hook ────────────────────────────────────────────────────────────────
|
|
193
|
-
const run = () => {
|
|
194
|
-
try {
|
|
195
|
-
const input = fs.readFileSync(0, 'utf-8');
|
|
196
|
-
const data = JSON.parse(input);
|
|
197
|
-
const { tool_name, tool_input } = data;
|
|
198
|
-
|
|
199
|
-
if (!tool_name) return allow();
|
|
200
|
-
|
|
201
|
-
if (forbiddenTools.includes(tool_name)) {
|
|
202
|
-
return deny('Use the code-search skill for codebase exploration instead of Grep/Glob/find. Describe what you need in plain language — it understands intent, not just patterns.');
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (writeTools.includes(tool_name)) {
|
|
206
|
-
const file_path = tool_input?.file_path || '';
|
|
207
|
-
const ext = path.extname(file_path);
|
|
208
|
-
const inSkillsDir = file_path.includes('/skills/') || file_path.includes('\\skills\\');
|
|
209
|
-
const base = path.basename(file_path).toLowerCase();
|
|
210
|
-
if ((ext === '.md' || ext === '.txt' || base.startsWith('features_list')) &&
|
|
211
|
-
!base.startsWith('claude') && !base.startsWith('readme') && !inSkillsDir) {
|
|
212
|
-
return deny('Cannot create documentation files. Only CLAUDE.md and readme.md are maintained. For task-specific notes, use .prd. For permanent reference material, add to CLAUDE.md.');
|
|
213
|
-
}
|
|
214
|
-
if (/\.(test|spec)\.(js|ts|jsx|tsx|mjs|cjs)$/.test(base) ||
|
|
215
|
-
/^(jest|vitest|mocha|ava|jasmine|tap)\.(config|setup)/.test(base) ||
|
|
216
|
-
file_path.includes('/__tests__/') || file_path.includes('/test/') ||
|
|
217
|
-
file_path.includes('/tests/') || file_path.includes('/fixtures/') ||
|
|
218
|
-
file_path.includes('/test-data/') || file_path.includes('/__mocks__/') ||
|
|
219
|
-
/\.(snap|stub|mock|fixture)\.(js|ts|json)$/.test(base)) {
|
|
220
|
-
return deny('Test files forbidden on disk. Use Bash tool with real services for all testing.');
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (searchTools.includes(tool_name)) return allow();
|
|
225
|
-
|
|
226
|
-
if (tool_name === 'Task' && (tool_input?.subagent_type || '') === 'Explore') {
|
|
227
|
-
return deny('Use the code-search skill for codebase exploration. Describe what you need in plain language.');
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (tool_name === 'EnterPlanMode') {
|
|
231
|
-
return deny('Plan mode is disabled. Use the gm skill (PLAN→EXECUTE→EMIT→VERIFY→COMPLETE state machine) instead.');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (tool_name === 'Skill') {
|
|
235
|
-
const skill = (tool_input?.skill || '').toLowerCase().replace(/^gm:/, '');
|
|
236
|
-
if (skill === 'explore' || skill === 'search') {
|
|
237
|
-
return deny('Use the code-search skill for codebase exploration. Describe what you need in plain language — it understands intent, not just patterns.');
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (tool_name === 'Bash') {
|
|
242
|
-
const command = (tool_input?.command || '').trim();
|
|
243
|
-
const stripFooter = (s) => s.replace(/\n\[Running tools\][\s\S]*$/, '').trimEnd();
|
|
244
|
-
|
|
245
|
-
// ─── agent-browser: CLI commands ──────────────────────────────────────────
|
|
246
|
-
const abCliMatch = command.match(/^agent-browser:\n([\s\S]+)$/);
|
|
247
|
-
if (abCliMatch) {
|
|
248
|
-
const abCode = abCliMatch[1];
|
|
249
|
-
const abNative = (() => {
|
|
250
|
-
const abDir = path.join(TOOLS_DIR, 'node_modules', 'agent-browser', 'bin');
|
|
251
|
-
const ext = IS_WIN ? '.exe' : '';
|
|
252
|
-
const archMap = { x64: 'x64', arm64: 'arm64', ia32: 'x64' };
|
|
253
|
-
const osMap = { win32: 'win32', darwin: 'darwin', linux: 'linux' };
|
|
254
|
-
const candidate = path.join(abDir, `agent-browser-${osMap[process.platform] || process.platform}-${archMap[process.arch] || process.arch}${ext}`);
|
|
255
|
-
return fs.existsSync(candidate) ? candidate : null;
|
|
256
|
-
})();
|
|
257
|
-
const abBin = abNative || (fs.existsSync(localBin('agent-browser')) ? localBin('agent-browser') : 'agent-browser');
|
|
258
|
-
const AB_CMDS = new Set(['open','goto','navigate','close','quit','exit','back','forward','reload','click','dblclick','type','fill','press','check','uncheck','select','drag','upload','hover','focus','scroll','scrollintoview','wait','screenshot','pdf','snapshot','get','is','find','eval','connect','tab','frame','dialog','state','session','network','cookies','storage','set','trace','profiler','record','console','errors','highlight','inspect','diff','keyboard','mouse','install','upgrade','confirm','deny','auth','device','window']);
|
|
259
|
-
const AB_GLOBAL_FLAGS = new Set(['--cdp','--headed','--headless','--session','--session-name','--auto-connect','--profile','--allow-file-access','--color-scheme','-p','--platform','--device']);
|
|
260
|
-
const AB_GLOBAL_FLAGS_WITH_VALUE = new Set(['--cdp','--session','--session-name','--profile','--color-scheme','-p','--platform','--device']);
|
|
261
|
-
const AB_SESSION_STATE = path.join(os.tmpdir(), 'gm-ab-sessions.json');
|
|
262
|
-
function readAbSessions() { try { return JSON.parse(fs.readFileSync(AB_SESSION_STATE, 'utf8')); } catch { return {}; } }
|
|
263
|
-
function writeAbSessions(s) { try { fs.writeFileSync(AB_SESSION_STATE, JSON.stringify(s)); } catch {} }
|
|
264
|
-
function parseAbLine(line) {
|
|
265
|
-
const tokens = line.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || [];
|
|
266
|
-
const globalArgs = [], rest = [];
|
|
267
|
-
let i = 0;
|
|
268
|
-
while (i < tokens.length) {
|
|
269
|
-
if (AB_GLOBAL_FLAGS.has(tokens[i])) {
|
|
270
|
-
globalArgs.push(tokens[i]);
|
|
271
|
-
if (AB_GLOBAL_FLAGS_WITH_VALUE.has(tokens[i]) && i + 1 < tokens.length && !tokens[i+1].startsWith('--')) globalArgs.push(tokens[++i]);
|
|
272
|
-
i++;
|
|
273
|
-
} else { rest.push(...tokens.slice(i)); break; }
|
|
274
|
-
}
|
|
275
|
-
return { globalArgs, rest };
|
|
276
|
-
}
|
|
277
|
-
const spawnAb = (bin, args, stdin) => {
|
|
278
|
-
const headed = args.includes('--headed');
|
|
279
|
-
const opts = { encoding: 'utf-8', timeout: 60000, windowsHide: !headed, ...(IS_WIN && { shell: true }), cwd: process.cwd(), ...(stdin !== undefined && { input: stdin }) };
|
|
280
|
-
const r = spawnSync(bin, args, opts);
|
|
281
|
-
if (!r.stdout && !r.stderr && r.error) return `[spawn error: ${r.error.message}]`;
|
|
282
|
-
const out = (r.stdout || '').trimEnd(), err = stripFooter(r.stderr || '').trimEnd();
|
|
283
|
-
return out && err ? out + '\n[stderr]\n' + err : stripFooter(out || err);
|
|
284
|
-
};
|
|
285
|
-
try {
|
|
286
|
-
const safeAb = abCode.trim();
|
|
287
|
-
const firstParsed = parseAbLine(safeAb.split('\n')[0].trim());
|
|
288
|
-
const firstWord = (firstParsed.rest[0] || '').toLowerCase();
|
|
289
|
-
const sessionName = (() => { const si = firstParsed.globalArgs.indexOf('--session'); return si >= 0 ? firstParsed.globalArgs[si+1] : 'default'; })();
|
|
290
|
-
const sessions = readAbSessions();
|
|
291
|
-
if (['open','goto','navigate'].includes(firstWord)) sessions[sessionName] = { url: firstParsed.rest[1] || '?', ts: Date.now() };
|
|
292
|
-
if (['close','quit','exit'].includes(firstWord)) delete sessions[sessionName];
|
|
293
|
-
writeAbSessions(sessions);
|
|
294
|
-
const openSessions = Object.entries(sessions);
|
|
295
|
-
let result;
|
|
296
|
-
if (AB_CMDS.has(firstWord)) {
|
|
297
|
-
const lines = safeAb.split('\n').map(l => l.trim()).filter(Boolean);
|
|
298
|
-
if (lines.length === 1) {
|
|
299
|
-
const { globalArgs, rest } = parseAbLine(lines[0]);
|
|
300
|
-
result = spawnAb(abBin, [...globalArgs, ...rest]);
|
|
301
|
-
} else {
|
|
302
|
-
const hasClose = lines.some(l => { const w = (parseAbLine(l).rest[0]||'').toLowerCase(); return ['close','quit','exit'].includes(w); });
|
|
303
|
-
const batchGlobals = firstParsed.globalArgs;
|
|
304
|
-
const results = [];
|
|
305
|
-
for (const l of lines) {
|
|
306
|
-
const { globalArgs, rest } = parseAbLine(l);
|
|
307
|
-
const mergedGlobals = [...batchGlobals.filter(f => !globalArgs.includes(f)), ...globalArgs];
|
|
308
|
-
const w = (rest[0]||'').toLowerCase();
|
|
309
|
-
if (['open','goto','navigate'].includes(w)) sessions[sessionName] = { url: rest[1]||'?', ts: Date.now() };
|
|
310
|
-
if (['close','quit','exit'].includes(w)) delete sessions[sessionName];
|
|
311
|
-
const args = AB_CMDS.has(w) ? [...mergedGlobals, ...rest] : [...mergedGlobals, 'eval', '--stdin'];
|
|
312
|
-
const stdin = AB_CMDS.has(w) ? undefined : l.trim();
|
|
313
|
-
results.push(spawnAb(abBin, args, stdin));
|
|
314
|
-
}
|
|
315
|
-
writeAbSessions(sessions);
|
|
316
|
-
result = results.filter(Boolean).join('\n');
|
|
317
|
-
if (!hasClose && openSessions.length > 0) result += `\n\n[tab] Browser session "${sessionName}" still open. Close when done:\n agent-browser:\n close`;
|
|
318
|
-
}
|
|
319
|
-
} else {
|
|
320
|
-
result = spawnAb(abBin, ['eval', '--stdin'], safeAb);
|
|
321
|
-
}
|
|
322
|
-
if (openSessions.length > 1) {
|
|
323
|
-
const stale = openSessions.filter(([n]) => n !== sessionName).map(([n,v]) => ` "${n}" → ${v.url} (${Math.round((Date.now()-v.ts)/60000)}min ago)`).join('\n');
|
|
324
|
-
result = (result || '') + `\n\n[tab] ${openSessions.length - 1} other session(s) still open:\n${stale}\n Close with: agent-browser:\\nclose (or --session <name> close)`;
|
|
325
|
-
}
|
|
326
|
-
return allowWithNoop(`agent-browser output:\n\n${result || '(no output)'}`);
|
|
327
|
-
} catch(e) {
|
|
328
|
-
return allowWithNoop(`agent-browser error:\n\n${e.message || '(exec failed)'}`);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const execMatch = command.match(/^exec(?::(\S+))?\n([\s\S]+)$/);
|
|
333
|
-
if (execMatch) {
|
|
334
|
-
const rawLang = (execMatch[1] || '').toLowerCase();
|
|
335
|
-
const code = execMatch[2];
|
|
336
|
-
if (/^\s*agent-browser\s/.test(code)) {
|
|
337
|
-
return deny(`Do not call agent-browser via exec:bash. Use agent-browser: for CLI commands:\n\nagent-browser:\nopen http://example.com\n\nMultiple commands:\n\nagent-browser:\nopen http://localhost:3001\nwait 2000\nsnapshot -i\n\nFor headed mode:\n\nagent-browser:\n--headed open http://localhost:3001\nwait --load networkidle\nsnapshot -i\n\nFor JS eval in browser:\n\nexec:agent-browser\ndocument.title`);
|
|
338
|
-
}
|
|
339
|
-
const cwd = tool_input?.cwd;
|
|
340
|
-
|
|
341
|
-
// ─── Lang plugin dispatch ─────────────────────────────────────────────
|
|
342
|
-
if (rawLang) {
|
|
343
|
-
const builtins = new Set(['js','javascript','ts','typescript','node','nodejs','py','python','sh','bash','shell','zsh','powershell','ps1','go','rust','c','cpp','java','deno','cmd','browser','ab','agent-browser','codesearch','search','status','sleep','close','runner','type']);
|
|
344
|
-
if (!builtins.has(rawLang)) {
|
|
345
|
-
const plugins = loadLangPlugins(projectDir);
|
|
346
|
-
const plugin = plugins.find(p => p.exec.match.test(`exec:${rawLang}\n${code}`));
|
|
347
|
-
if (plugin) {
|
|
348
|
-
const runnerCode = `
|
|
349
|
-
const plugin = require(${JSON.stringify(path.join(projectDir, 'lang', plugin.id + '.js'))});
|
|
350
|
-
Promise.resolve(plugin.exec.run(${JSON.stringify(code)}, ${JSON.stringify(cwd || projectDir || process.cwd())}))
|
|
351
|
-
.then(out => process.stdout.write(String(out || '')))
|
|
352
|
-
.catch(e => { process.stderr.write(e.message || String(e)); process.exit(1); });
|
|
353
|
-
`;
|
|
354
|
-
const r = spawnSync('bun', ['-e', runnerCode], { encoding: 'utf-8', timeout: 60000, windowsHide: true });
|
|
355
|
-
const out = (r.stdout || '').trimEnd();
|
|
356
|
-
const err = (r.stderr || '').trimEnd();
|
|
357
|
-
if (r.status !== 0 || r.error) return allowWithNoop(`exec:${rawLang} error:\n\n${r.error ? r.error.message : (err || 'exec failed')}`);
|
|
358
|
-
return allowWithNoop(`exec:${rawLang} output:\n\n${out || '(no output)'}`);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
// ─────────────────────────────────────────────────────────────────────
|
|
363
|
-
const detectLang = (src) => {
|
|
364
|
-
if (/^\s*(import |from |export |const |let |var |function |class |async |await |console\.|process\.)/.test(src)) return 'nodejs';
|
|
365
|
-
if (/^\s*(import |def |print\(|class |if __name__)/.test(src)) return 'python';
|
|
366
|
-
if (/^\s*(echo |ls |cd |mkdir |rm |cat |grep |find |export |source |#!)/.test(src)) return 'bash';
|
|
367
|
-
return 'nodejs';
|
|
368
|
-
};
|
|
369
|
-
// Note: 'cmd' is NOT aliased to 'bash' — it has its own handler below
|
|
370
|
-
const aliases = { js: 'nodejs', javascript: 'nodejs', ts: 'typescript', node: 'nodejs', py: 'python', sh: 'bash', shell: 'bash', zsh: 'bash', powershell: 'powershell', ps1: 'powershell', browser: 'agent-browser', ab: 'agent-browser', codesearch: 'codesearch', search: 'search', status: 'status', sleep: 'sleep', close: 'close', runner: 'runner', type: 'type' };
|
|
371
|
-
const lang = aliases[rawLang] || rawLang || detectLang(code);
|
|
372
|
-
const langExts = { nodejs: 'mjs', typescript: 'ts', deno: 'ts', python: 'py', bash: 'sh', powershell: 'ps1', go: 'go', rust: 'rs', c: 'c', cpp: 'cpp', java: 'java' };
|
|
373
|
-
|
|
374
|
-
const spawnDirect = (bin, args, stdin) => {
|
|
375
|
-
const isAb = lang === 'agent-browser';
|
|
376
|
-
const spawnCwd = cwd || (isAb ? process.cwd() : undefined);
|
|
377
|
-
const opts = { encoding: 'utf-8', timeout: 60000, windowsHide: true, ...(isAb && IS_WIN && { shell: true }), ...(spawnCwd && { cwd: spawnCwd }), ...(stdin !== undefined && { input: stdin }) };
|
|
378
|
-
const r = spawnSync(bin, args, opts);
|
|
379
|
-
if (!r.stdout && !r.stderr && r.error) return `[spawn error: ${r.error.message}]`;
|
|
380
|
-
const out = (r.stdout || '').trimEnd(), err = stripFooter(r.stderr || '').trimEnd();
|
|
381
|
-
return out && err ? out + '\n[stderr]\n' + err : stripFooter(out || err);
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
const runWithFile = (l, src) => {
|
|
385
|
-
const tmp = path.join(os.tmpdir(), `gm-exec-${Date.now()}.${langExts[l] || l}`);
|
|
386
|
-
fs.writeFileSync(tmp, src, 'utf-8');
|
|
387
|
-
const r = runGmExec(['exec', `--lang=${l}`, `--file=${tmp}`, ...(cwd ? [`--cwd=${cwd}`] : [])], { timeout: 65000 });
|
|
388
|
-
try { fs.unlinkSync(tmp); } catch (e) {}
|
|
389
|
-
let out = stripFooter((r.stdout || '') + (r.stderr || ''));
|
|
390
|
-
const bg = out.match(/Task ID:\s*(task_\S+)/);
|
|
391
|
-
if (bg) {
|
|
392
|
-
runGmExec(['sleep', bg[1], '15'], { timeout: 25000 });
|
|
393
|
-
const sr = runGmExec(['status', bg[1]], { timeout: 15000 });
|
|
394
|
-
const statusOut = stripFooter((sr.stdout || '') + (sr.stderr || ''));
|
|
395
|
-
const stillRunning = /Status:\s*running/i.test(statusOut);
|
|
396
|
-
out = statusOut;
|
|
397
|
-
if (!stillRunning) runGmExec(['close', bg[1]], { timeout: 10000 });
|
|
398
|
-
}
|
|
399
|
-
return out;
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
const decodeB64 = (s) => {
|
|
403
|
-
const t = s.trim();
|
|
404
|
-
if (t.length < 16 || t.length % 4 !== 0 || !/^[A-Za-z0-9+/\r\n]+=*$/.test(t)) return s;
|
|
405
|
-
try { const d = Buffer.from(t, 'base64').toString('utf-8'); return /[\x00-\x08\x0b\x0e-\x1f]/.test(d) ? s : d; } catch { return s; }
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
const safeCode = decodeB64(code);
|
|
409
|
-
|
|
410
|
-
if (['codesearch', 'search'].includes(lang)) {
|
|
411
|
-
const query = safeCode.trim();
|
|
412
|
-
const r = runGmExec(['search', ...(cwd ? ['--path', cwd] : []), query], { timeout: 30000, ...(cwd && { cwd }) });
|
|
413
|
-
return allowWithNoop(`exec:${lang} output:\n\n${stripFooter((r.stdout || '') + (r.stderr || '')) || '(no results)'}`);
|
|
414
|
-
}
|
|
415
|
-
if (lang === 'status') {
|
|
416
|
-
const r = runGmExec(['status', safeCode.trim()], { timeout: 15000 });
|
|
417
|
-
return allowWithNoop(`exec:status output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
418
|
-
}
|
|
419
|
-
if (lang === 'sleep') {
|
|
420
|
-
const parts = safeCode.trim().split(/\s+/);
|
|
421
|
-
const r = runGmExec(['sleep', ...parts], { timeout: 70000 });
|
|
422
|
-
return allowWithNoop(`exec:sleep output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
423
|
-
}
|
|
424
|
-
if (lang === 'close') {
|
|
425
|
-
const r = runGmExec(['close', safeCode.trim()], { timeout: 15000 });
|
|
426
|
-
return allowWithNoop(`exec:close output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
427
|
-
}
|
|
428
|
-
if (lang === 'runner') {
|
|
429
|
-
const r = runGmExec(['runner', safeCode.trim()], { timeout: 15000 });
|
|
430
|
-
return allowWithNoop(`exec:runner output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
431
|
-
}
|
|
432
|
-
if (lang === 'type') {
|
|
433
|
-
const lines = safeCode.split(/\r?\n/);
|
|
434
|
-
const taskId = lines[0].trim();
|
|
435
|
-
const inputData = lines.slice(1).join('\n').trim();
|
|
436
|
-
const r = runGmExec(['type', taskId, inputData], { timeout: 15000 });
|
|
437
|
-
return allowWithNoop(`exec:type output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
438
|
-
}
|
|
439
|
-
try {
|
|
440
|
-
let result;
|
|
441
|
-
if (lang === 'bash') {
|
|
442
|
-
const shFile = path.join(os.tmpdir(), `gm-exec-${Date.now()}.sh`);
|
|
443
|
-
fs.writeFileSync(shFile, safeCode, 'utf-8');
|
|
444
|
-
result = spawnDirect('bash', [shFile]);
|
|
445
|
-
try { fs.unlinkSync(shFile); } catch (e) {}
|
|
446
|
-
if (!result || result.startsWith('[spawn error:')) result = runWithFile('bash', safeCode);
|
|
447
|
-
} else if (lang === 'cmd') {
|
|
448
|
-
// exec:cmd always runs cmd.exe /c — explicit Windows command prompt
|
|
449
|
-
result = spawnDirect('cmd.exe', ['/c', safeCode]);
|
|
450
|
-
if (!result || result.startsWith('[spawn error:')) result = runWithFile('cmd', safeCode);
|
|
451
|
-
return allowWithNoop(`exec:cmd output:\n\n${result || '(no output)'}`);
|
|
452
|
-
} else if (lang === 'python') {
|
|
453
|
-
result = spawnDirect('python3', ['-c', safeCode]);
|
|
454
|
-
if (!result || result.startsWith('[spawn error:')) result = spawnDirect('python', ['-c', safeCode]);
|
|
455
|
-
} else if (!lang || ['nodejs', 'typescript', 'deno'].includes(lang)) {
|
|
456
|
-
const wrapped = `const __result = await (async () => {\n${safeCode}\n})();\nif (__result !== undefined) { if (typeof __result === 'object') { console.log(JSON.stringify(__result, null, 2)); } else { console.log(__result); } }`;
|
|
457
|
-
result = runWithFile(lang || 'nodejs', wrapped);
|
|
458
|
-
} else if (lang === 'agent-browser') {
|
|
459
|
-
// exec:agent-browser = JS eval in browser page context only.
|
|
460
|
-
// Browser CLI commands (open, click, snapshot, headed mode, etc.) use agent-browser: prefix.
|
|
461
|
-
const abNative = (() => {
|
|
462
|
-
const abDir = path.join(TOOLS_DIR, 'node_modules', 'agent-browser', 'bin');
|
|
463
|
-
const ext = IS_WIN ? '.exe' : '';
|
|
464
|
-
const archMap = { x64: 'x64', arm64: 'arm64', ia32: 'x64' };
|
|
465
|
-
const osMap = { win32: 'win32', darwin: 'darwin', linux: 'linux' };
|
|
466
|
-
const candidate = path.join(abDir, `agent-browser-${osMap[process.platform] || process.platform}-${archMap[process.arch] || process.arch}${ext}`);
|
|
467
|
-
return fs.existsSync(candidate) ? candidate : null;
|
|
468
|
-
})();
|
|
469
|
-
const abBin = abNative || (fs.existsSync(localBin('agent-browser')) ? localBin('agent-browser') : 'agent-browser');
|
|
470
|
-
result = spawnDirect(abBin, ['eval', '--stdin'], safeCode);
|
|
471
|
-
} else {
|
|
472
|
-
result = runWithFile(lang, safeCode);
|
|
473
|
-
}
|
|
474
|
-
return allowWithNoop(`exec:${lang} output:\n\n${result || '(no output)'}`);
|
|
475
|
-
} catch (e) {
|
|
476
|
-
return allowWithNoop(`exec:${lang} error:\n\n${(e.stdout || '') + (e.stderr || '') || e.message || '(exec failed)'}`);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (/^bun\s+x\s+(gm-exec|rs-exec|plugkit|codebasesearch)/.test(command)) {
|
|
481
|
-
return deny(`Do not call ${command.match(/^bun\s+x\s+(\S+)/)[1]} directly. Use exec:<lang> syntax instead.\n\nExamples:\n exec:nodejs\n console.log("hello")\n\n exec:codesearch\n find all database queries\n\n exec:bash\n ls -la\n\nThe exec: prefix routes through the hook dispatcher which handles language detection, background tasks, and tool management automatically.`);
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (!/^exec(\s|:)/.test(command) && !/^agent-browser:/.test(command) && !/^git /.test(command) && !/(\bclaude\b)/.test(command) && !/^npm install .* \/config\/.gmweb/.test(command) && !/^bun install --cwd \/config\/.gmweb/.test(command)) {
|
|
485
|
-
return deny(`Bash is restricted to exec:<lang>, agent-browser:, and git.\n\nexec:<lang> syntax (lang auto-detected if omitted):\n exec:nodejs / exec:python / exec:bash / exec:typescript\n exec:go / exec:rust / exec:java / exec:c / exec:cpp\n exec:cmd ← runs cmd.exe /c on Windows\n exec:agent-browser ← JS eval in browser page context (document.title, DOM queries, etc.)\n exec ← auto-detects language\n\nexec:agent-browser — JS eval in browser page context:\n exec:agent-browser\n document.title\n\n exec:agent-browser\n JSON.stringify([...document.querySelectorAll('h1')].map(h => h.textContent))\n\nagent-browser: — browser CLI commands (open, click, snapshot, headed mode, etc.):\n agent-browser:\n open http://localhost:3001\n\n agent-browser:\n --headed open http://localhost:3001\n wait --load networkidle\n snapshot -i\n\n agent-browser:\n close\n\nTask management shortcuts (body = args):\n exec:status\n <task_id>\n\n exec:sleep\n <task_id> [seconds] [--next-output]\n\n exec:type\n <task_id>\n <input to send to stdin>\n\n exec:close\n <task_id>\n\n exec:runner\n start|stop|status\n\nCode search shortcut:\n exec:codesearch\n <natural language query>\n\nAll other Bash commands are blocked.`);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
const allowedTools = ['agent-browser', 'Skill', 'code-search', 'electron', 'TaskOutput', 'ReadMcpResourceTool', 'ListMcpResourcesTool'];
|
|
490
|
-
if (allowedTools.includes(tool_name)) return allow();
|
|
491
|
-
|
|
492
|
-
return allow();
|
|
493
|
-
} catch (error) {
|
|
494
|
-
return allow();
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
|
|
498
|
-
try {
|
|
499
|
-
const result = run();
|
|
500
|
-
console.log(JSON.stringify(result));
|
|
501
|
-
process.exit(0);
|
|
502
|
-
} catch (error) {
|
|
503
|
-
process.exit(0);
|
|
504
|
-
}
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
if (process.env.AGENTGUI_SUBPROCESS === '1') {
|
|
4
|
-
console.log(JSON.stringify({ additionalContext: '' }));
|
|
5
|
-
process.exit(0);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const fs = require('fs');
|
|
9
|
-
const path = require('path');
|
|
10
|
-
const os = require('os');
|
|
11
|
-
const { execSync, spawnSync } = require('child_process');
|
|
12
|
-
|
|
13
|
-
const IS_WIN = process.platform === 'win32';
|
|
14
|
-
const TOOLS_DIR = path.join(os.homedir(), '.claude', 'gm-tools');
|
|
15
|
-
|
|
16
|
-
function localBin(name) {
|
|
17
|
-
const ext = IS_WIN ? '.exe' : '';
|
|
18
|
-
return path.join(TOOLS_DIR, 'node_modules', '.bin', name + ext);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function runLocal(name, args, opts = {}) {
|
|
22
|
-
const bin = localBin(name);
|
|
23
|
-
if (fs.existsSync(bin)) {
|
|
24
|
-
return spawnSync(bin, args, { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
25
|
-
}
|
|
26
|
-
return spawnSync('bun', ['x', name, ...args], { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function plugkitBin() { return path.join(TOOLS_DIR, IS_WIN ? 'plugkit.exe' : 'plugkit'); }
|
|
30
|
-
|
|
31
|
-
function runPlugkit(args, opts = {}) {
|
|
32
|
-
const bin = plugkitBin();
|
|
33
|
-
if (fs.existsSync(bin)) {
|
|
34
|
-
return spawnSync(bin, args, { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
35
|
-
}
|
|
36
|
-
return spawnSync('plugkit', args, { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const projectDir = process.env.CLAUDE_PROJECT_DIR || process.env.GEMINI_PROJECT_DIR || process.env.OC_PROJECT_DIR || process.env.KILO_PROJECT_DIR;
|
|
40
|
-
|
|
41
|
-
function loadLangPlugins(dir) {
|
|
42
|
-
if (!dir) return [];
|
|
43
|
-
const langDir = path.join(dir, 'lang');
|
|
44
|
-
if (!fs.existsSync(langDir)) return [];
|
|
45
|
-
try {
|
|
46
|
-
return fs.readdirSync(langDir)
|
|
47
|
-
.filter(f => f.endsWith('.js') && f !== 'loader.js' && f !== 'SPEC.md')
|
|
48
|
-
.reduce((acc, f) => {
|
|
49
|
-
try {
|
|
50
|
-
const p = require(path.join(langDir, f));
|
|
51
|
-
if (p && typeof p.id === 'string' && p.exec && p.exec.match instanceof RegExp && typeof p.exec.run === 'function') acc.push(p);
|
|
52
|
-
} catch (_) {}
|
|
53
|
-
return acc;
|
|
54
|
-
}, []);
|
|
55
|
-
} catch (_) { return []; }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function walkFiles(dir, exts, depth) {
|
|
59
|
-
if (depth <= 0 || !fs.existsSync(dir)) return [];
|
|
60
|
-
let results = [];
|
|
61
|
-
try {
|
|
62
|
-
for (const f of fs.readdirSync(dir)) {
|
|
63
|
-
if (f.startsWith('.') || f === 'node_modules') continue;
|
|
64
|
-
const full = path.join(dir, f);
|
|
65
|
-
const stat = fs.statSync(full);
|
|
66
|
-
if (stat.isDirectory()) results = results.concat(walkFiles(full, exts, depth - 1));
|
|
67
|
-
else if (exts.some(e => f.endsWith(e))) results.push({ path: full, mtime: stat.mtimeMs });
|
|
68
|
-
}
|
|
69
|
-
} catch (_) {}
|
|
70
|
-
return results;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function getLangPluginContext(dir) {
|
|
74
|
-
const plugins = loadLangPlugins(dir);
|
|
75
|
-
if (!plugins.length) return '';
|
|
76
|
-
const parts = [];
|
|
77
|
-
for (const p of plugins) {
|
|
78
|
-
if (p.context) {
|
|
79
|
-
const ctx = typeof p.context === 'function' ? p.context() : p.context;
|
|
80
|
-
if (ctx) parts.push(String(ctx).slice(0, 2000));
|
|
81
|
-
}
|
|
82
|
-
if (p.lsp && p.extensions && p.extensions.length) {
|
|
83
|
-
try {
|
|
84
|
-
const files = walkFiles(dir, p.extensions, 4)
|
|
85
|
-
.sort((a, b) => b.mtime - a.mtime)
|
|
86
|
-
.slice(0, 3);
|
|
87
|
-
const diags = [];
|
|
88
|
-
for (const f of files) {
|
|
89
|
-
try {
|
|
90
|
-
const code = fs.readFileSync(f.path, 'utf-8');
|
|
91
|
-
const results = p.lsp.check(code, dir);
|
|
92
|
-
if (Array.isArray(results)) {
|
|
93
|
-
for (const d of results) diags.push(`${path.relative(dir, f.path)}:${d.line}:${d.col}: ${d.severity}: ${d.message}`);
|
|
94
|
-
}
|
|
95
|
-
} catch (_) {}
|
|
96
|
-
}
|
|
97
|
-
if (diags.length) parts.push(`=== ${p.id} LSP diagnostics ===\n${diags.join('\n').slice(0, 3000)}`);
|
|
98
|
-
} catch (_) {}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return parts.join('\n\n');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const ensureGitignore = () => {
|
|
105
|
-
if (!projectDir) return;
|
|
106
|
-
const gitignorePath = path.join(projectDir, '.gitignore');
|
|
107
|
-
const entry = '.gm-stop-verified';
|
|
108
|
-
try {
|
|
109
|
-
let content = fs.existsSync(gitignorePath) ? fs.readFileSync(gitignorePath, 'utf-8') : '';
|
|
110
|
-
if (!content.split('\n').some(line => line.trim() === entry)) {
|
|
111
|
-
content = (content.endsWith('\n') || content === '' ? content : content + '\n') + entry + '\n';
|
|
112
|
-
fs.writeFileSync(gitignorePath, content);
|
|
113
|
-
}
|
|
114
|
-
} catch (e) {}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const runThorns = () => {
|
|
118
|
-
if (!projectDir || !fs.existsSync(projectDir)) return '';
|
|
119
|
-
try {
|
|
120
|
-
const r = runPlugkit(['codeinsight', projectDir], { timeout: 15000 });
|
|
121
|
-
const out = ((r.stdout || '') + (r.stderr || '')).trim();
|
|
122
|
-
return out ? `=== codeinsight ===\n${out}` : '';
|
|
123
|
-
} catch (e) {
|
|
124
|
-
return '';
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const runCodeSearch = (prompt) => {
|
|
129
|
-
if (!prompt || !projectDir) return '';
|
|
130
|
-
try {
|
|
131
|
-
const r = runPlugkit(['search', '--path', projectDir, prompt], { timeout: 10000, cwd: projectDir });
|
|
132
|
-
const out = ((r.stdout || '') + (r.stderr || '')).trim();
|
|
133
|
-
return out ? `=== search ===\n${out}` : '';
|
|
134
|
-
} catch (e) {
|
|
135
|
-
return '';
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const emit = (additionalContext) => {
|
|
140
|
-
const isGemini = process.env.GEMINI_PROJECT_DIR !== undefined;
|
|
141
|
-
const isOpenCode = process.env.OC_PROJECT_DIR !== undefined;
|
|
142
|
-
const isKilo = process.env.KILO_PROJECT_DIR !== undefined;
|
|
143
|
-
if (isGemini) {
|
|
144
|
-
console.log(JSON.stringify({ systemMessage: additionalContext }, null, 2));
|
|
145
|
-
} else if (isOpenCode || isKilo) {
|
|
146
|
-
console.log(JSON.stringify({ hookSpecificOutput: { hookEventName: 'message.updated', additionalContext } }, null, 2));
|
|
147
|
-
} else {
|
|
148
|
-
console.log(JSON.stringify({ additionalContext }, null, 2));
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
try {
|
|
153
|
-
let prompt = '';
|
|
154
|
-
try {
|
|
155
|
-
const input = JSON.parse(fs.readFileSync(0, 'utf-8'));
|
|
156
|
-
prompt = input.prompt || input.message || input.userMessage || '';
|
|
157
|
-
} catch (e) {}
|
|
158
|
-
|
|
159
|
-
ensureGitignore();
|
|
160
|
-
|
|
161
|
-
const parts = [];
|
|
162
|
-
parts.push('Use the Skill tool with skill: "gm" to begin — do NOT use the Agent tool to load skills. Skills are invoked via the Skill tool only, never as agents. DO NOT use EnterPlanMode.');
|
|
163
|
-
|
|
164
|
-
const search = runCodeSearch(prompt);
|
|
165
|
-
if (search) parts.push(search);
|
|
166
|
-
|
|
167
|
-
const thorns = runThorns();
|
|
168
|
-
if (thorns) parts.push(thorns);
|
|
169
|
-
|
|
170
|
-
const langCtx = getLangPluginContext(projectDir);
|
|
171
|
-
if (langCtx) parts.push(langCtx);
|
|
172
|
-
|
|
173
|
-
emit(parts.join('\n\n'));
|
|
174
|
-
} catch (error) {
|
|
175
|
-
emit('Invoke the `gm` skill to begin. Hook error: ' + error.message);
|
|
176
|
-
process.exit(0);
|
|
177
|
-
}
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
if (process.env.AGENTGUI_SUBPROCESS === '1') {
|
|
4
|
-
console.log(JSON.stringify({ decision: 'approve' }));
|
|
5
|
-
process.exit(0);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const { execSync } = require('child_process');
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
const os = require('os');
|
|
12
|
-
const crypto = require('crypto');
|
|
13
|
-
|
|
14
|
-
const projectDir = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
15
|
-
|
|
16
|
-
const getCounterPath = () => {
|
|
17
|
-
const hash = crypto.createHash('md5').update(projectDir).digest('hex');
|
|
18
|
-
return path.join(os.tmpdir(), `gm-git-block-counter-${hash}.json`);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const readCounter = () => {
|
|
22
|
-
try {
|
|
23
|
-
const counterPath = getCounterPath();
|
|
24
|
-
if (fs.existsSync(counterPath)) {
|
|
25
|
-
const data = fs.readFileSync(counterPath, 'utf-8');
|
|
26
|
-
return JSON.parse(data);
|
|
27
|
-
}
|
|
28
|
-
} catch (e) {}
|
|
29
|
-
return { count: 0, lastGitHash: null };
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const writeCounter = (data) => {
|
|
33
|
-
try {
|
|
34
|
-
const counterPath = getCounterPath();
|
|
35
|
-
fs.writeFileSync(counterPath, JSON.stringify(data, null, 2), 'utf-8');
|
|
36
|
-
} catch (e) {}
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const getCurrentGitHash = () => {
|
|
40
|
-
try {
|
|
41
|
-
const hash = execSync('git rev-parse HEAD', {
|
|
42
|
-
cwd: projectDir,
|
|
43
|
-
stdio: 'pipe',
|
|
44
|
-
encoding: 'utf-8'
|
|
45
|
-
}).trim();
|
|
46
|
-
return hash;
|
|
47
|
-
} catch (e) {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const resetCounterIfCommitted = (currentHash) => {
|
|
53
|
-
const counter = readCounter();
|
|
54
|
-
if (counter.lastGitHash && currentHash && counter.lastGitHash !== currentHash) {
|
|
55
|
-
counter.count = 0;
|
|
56
|
-
counter.lastGitHash = currentHash;
|
|
57
|
-
writeCounter(counter);
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
return false;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const incrementCounter = (currentHash) => {
|
|
64
|
-
const counter = readCounter();
|
|
65
|
-
counter.count = (counter.count || 0) + 1;
|
|
66
|
-
counter.lastGitHash = currentHash;
|
|
67
|
-
writeCounter(counter);
|
|
68
|
-
return counter.count;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const getGitStatus = () => {
|
|
72
|
-
try {
|
|
73
|
-
execSync('git rev-parse --git-dir', {
|
|
74
|
-
cwd: projectDir,
|
|
75
|
-
stdio: 'pipe'
|
|
76
|
-
});
|
|
77
|
-
} catch (e) {
|
|
78
|
-
return { isRepo: false };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
const status = execSync('git status --porcelain', {
|
|
83
|
-
cwd: projectDir,
|
|
84
|
-
stdio: 'pipe',
|
|
85
|
-
encoding: 'utf-8'
|
|
86
|
-
}).trim();
|
|
87
|
-
|
|
88
|
-
const isDirty = status.length > 0;
|
|
89
|
-
|
|
90
|
-
let unpushedCount = 0;
|
|
91
|
-
try {
|
|
92
|
-
const unpushed = execSync('git rev-list --count @{u}..HEAD', {
|
|
93
|
-
cwd: projectDir,
|
|
94
|
-
stdio: 'pipe',
|
|
95
|
-
encoding: 'utf-8'
|
|
96
|
-
}).trim();
|
|
97
|
-
unpushedCount = parseInt(unpushed, 10) || 0;
|
|
98
|
-
} catch (e) {
|
|
99
|
-
unpushedCount = -1;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
let behindCount = 0;
|
|
103
|
-
try {
|
|
104
|
-
const behind = execSync('git rev-list --count HEAD..@{u}', {
|
|
105
|
-
cwd: projectDir,
|
|
106
|
-
stdio: 'pipe',
|
|
107
|
-
encoding: 'utf-8'
|
|
108
|
-
}).trim();
|
|
109
|
-
behindCount = parseInt(behind, 10) || 0;
|
|
110
|
-
} catch (e) {}
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
isRepo: true,
|
|
114
|
-
isDirty,
|
|
115
|
-
unpushedCount,
|
|
116
|
-
behindCount,
|
|
117
|
-
statusOutput: status
|
|
118
|
-
};
|
|
119
|
-
} catch (e) {
|
|
120
|
-
return { isRepo: true, isDirty: false, unpushedCount: 0, behindCount: 0 };
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
const run = () => {
|
|
125
|
-
const gitStatus = getGitStatus();
|
|
126
|
-
if (!gitStatus.isRepo) return { ok: true };
|
|
127
|
-
|
|
128
|
-
const currentHash = getCurrentGitHash();
|
|
129
|
-
resetCounterIfCommitted(currentHash);
|
|
130
|
-
|
|
131
|
-
const issues = [];
|
|
132
|
-
if (gitStatus.isDirty) {
|
|
133
|
-
issues.push('Uncommitted changes exist');
|
|
134
|
-
}
|
|
135
|
-
if (gitStatus.unpushedCount > 0) {
|
|
136
|
-
issues.push(`${gitStatus.unpushedCount} commit(s) not pushed`);
|
|
137
|
-
}
|
|
138
|
-
if (gitStatus.unpushedCount === -1) {
|
|
139
|
-
issues.push('Unable to verify push status - may have unpushed commits');
|
|
140
|
-
}
|
|
141
|
-
if (gitStatus.behindCount > 0) {
|
|
142
|
-
issues.push(`${gitStatus.behindCount} upstream change(s) not pulled`);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (issues.length > 0) {
|
|
146
|
-
const blockCount = incrementCounter(currentHash);
|
|
147
|
-
return {
|
|
148
|
-
ok: false,
|
|
149
|
-
reason: `${issues.join(', ')}, must push to remote`,
|
|
150
|
-
blockCount
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const counter = readCounter();
|
|
155
|
-
if (counter.count > 0) {
|
|
156
|
-
counter.count = 0;
|
|
157
|
-
writeCounter(counter);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return { ok: true };
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
const result = run();
|
|
165
|
-
if (!result.ok) {
|
|
166
|
-
if (result.blockCount === 1) {
|
|
167
|
-
console.log(JSON.stringify({
|
|
168
|
-
decision: 'block',
|
|
169
|
-
reason: `Git: ${result.reason}`
|
|
170
|
-
}, null, 2));
|
|
171
|
-
process.exit(2);
|
|
172
|
-
} else if (result.blockCount > 1) {
|
|
173
|
-
console.log(JSON.stringify({
|
|
174
|
-
decision: 'approve',
|
|
175
|
-
reason: `⚠️ Git warning (attempt #${result.blockCount}): ${result.reason} - Please commit and push your changes.`
|
|
176
|
-
}, null, 2));
|
|
177
|
-
process.exit(0);
|
|
178
|
-
}
|
|
179
|
-
} else {
|
|
180
|
-
console.log(JSON.stringify({
|
|
181
|
-
decision: 'approve'
|
|
182
|
-
}, null, 2));
|
|
183
|
-
process.exit(0);
|
|
184
|
-
}
|
|
185
|
-
} catch (e) {
|
|
186
|
-
console.log(JSON.stringify({
|
|
187
|
-
decision: 'approve'
|
|
188
|
-
}, null, 2));
|
|
189
|
-
process.exit(0);
|
|
190
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
if (process.env.AGENTGUI_SUBPROCESS === '1') {
|
|
4
|
-
console.log(JSON.stringify({ decision: 'approve' }));
|
|
5
|
-
process.exit(0);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const fs = require('fs');
|
|
9
|
-
const path = require('path');
|
|
10
|
-
|
|
11
|
-
const projectDir = process.cwd();
|
|
12
|
-
const prdFile = path.resolve(projectDir, '.prd');
|
|
13
|
-
|
|
14
|
-
let aborted = false;
|
|
15
|
-
process.on('SIGTERM', () => { aborted = true; });
|
|
16
|
-
process.on('SIGINT', () => { aborted = true; });
|
|
17
|
-
|
|
18
|
-
const run = () => {
|
|
19
|
-
if (aborted) return { ok: true };
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
if (fs.existsSync(prdFile)) {
|
|
23
|
-
const prdContent = fs.readFileSync(prdFile, 'utf-8').trim();
|
|
24
|
-
if (prdContent.length > 0) {
|
|
25
|
-
return {
|
|
26
|
-
ok: false,
|
|
27
|
-
reason: `Work items remain in ${prdFile}. Remove completed items as they finish. Delete the file when all items are done.\n\n${prdContent}`
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return { ok: true };
|
|
32
|
-
} catch (error) {
|
|
33
|
-
return { ok: true };
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
const result = run();
|
|
39
|
-
|
|
40
|
-
if (!result.ok) {
|
|
41
|
-
console.log(JSON.stringify({
|
|
42
|
-
decision: 'block',
|
|
43
|
-
reason: result.reason
|
|
44
|
-
}, null, 2));
|
|
45
|
-
process.exit(2);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
console.log(JSON.stringify({
|
|
49
|
-
decision: 'approve'
|
|
50
|
-
}, null, 2));
|
|
51
|
-
process.exit(0);
|
|
52
|
-
} catch (e) {
|
|
53
|
-
console.log(JSON.stringify({
|
|
54
|
-
decision: 'approve'
|
|
55
|
-
}, null, 2));
|
|
56
|
-
process.exit(0);
|
|
57
|
-
}
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const os = require('os');
|
|
6
|
-
const { spawnSync } = require('child_process');
|
|
7
|
-
|
|
8
|
-
const IS_WIN = process.platform === 'win32';
|
|
9
|
-
const TOOLS_DIR = path.join(os.homedir(), '.claude', 'gm-tools');
|
|
10
|
-
|
|
11
|
-
function localBin(name) {
|
|
12
|
-
const ext = IS_WIN ? '.exe' : '';
|
|
13
|
-
return path.join(TOOLS_DIR, 'node_modules', '.bin', name + ext);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function pkgEntry(name) {
|
|
17
|
-
try {
|
|
18
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(TOOLS_DIR, 'node_modules', name, 'package.json'), 'utf8'));
|
|
19
|
-
const binVal = pkg.bin;
|
|
20
|
-
const rel = typeof binVal === 'string' ? binVal : (binVal?.[name] || Object.values(binVal || {})[0]);
|
|
21
|
-
if (rel) return path.join(TOOLS_DIR, 'node_modules', name, rel);
|
|
22
|
-
} catch {}
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function runLocal(name, args, opts = {}) {
|
|
27
|
-
if (IS_WIN) {
|
|
28
|
-
const entry = pkgEntry(name);
|
|
29
|
-
if (entry && fs.existsSync(entry)) {
|
|
30
|
-
return spawnSync('bun', [entry, ...args], { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const bin = localBin(name);
|
|
34
|
-
if (fs.existsSync(bin)) {
|
|
35
|
-
return spawnSync(bin, args, { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
36
|
-
}
|
|
37
|
-
return spawnSync('bun', ['x', name, ...args], { encoding: 'utf8', windowsHide: true, timeout: 30000, ...opts });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const MANAGED_PKGS = ['agent-browser'];
|
|
41
|
-
const PKG_JSON = path.join(TOOLS_DIR, 'package.json');
|
|
42
|
-
|
|
43
|
-
const PLUGKIT_REPO = 'AnEntrypoint/rs-plugkit';
|
|
44
|
-
const archMap = { x64: 'x86_64', arm64: 'aarch64', ia32: 'x86_64' };
|
|
45
|
-
const plugkitTargets = {
|
|
46
|
-
win32: a => `plugkit-x86_64-pc-windows-msvc/plugkit.exe`,
|
|
47
|
-
darwin: a => `plugkit-x86_64-unknown-linux-gnu/plugkit`,
|
|
48
|
-
linux: a => `plugkit-x86_64-unknown-linux-gnu/plugkit`,
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
function plugkitBin() { return path.join(TOOLS_DIR, IS_WIN ? 'plugkit.exe' : 'plugkit'); }
|
|
52
|
-
|
|
53
|
-
function downloadBin(assetPath, dest) {
|
|
54
|
-
const https = require('https');
|
|
55
|
-
const url = `https://github.com/${PLUGKIT_REPO}/releases/latest/download/${assetPath}`;
|
|
56
|
-
return new Promise((resolve) => {
|
|
57
|
-
const follow = (u) => https.get(u, { headers: { 'User-Agent': 'gm' } }, res => {
|
|
58
|
-
if (res.statusCode >= 300 && res.statusCode < 400) return follow(res.headers.location);
|
|
59
|
-
const chunks = [];
|
|
60
|
-
res.on('data', c => chunks.push(c));
|
|
61
|
-
res.on('end', () => { try { fs.writeFileSync(dest, Buffer.concat(chunks)); fs.chmodSync(dest, 0o755); } catch {} resolve(); });
|
|
62
|
-
}).on('error', () => resolve());
|
|
63
|
-
follow(url);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async function ensurePlugkit() {
|
|
68
|
-
const bin = plugkitBin();
|
|
69
|
-
if (!fs.existsSync(bin)) {
|
|
70
|
-
const assetPath = plugkitTargets[process.platform]?.(archMap[process.arch] || 'x86_64') || plugkitTargets.linux('x86_64');
|
|
71
|
-
await downloadBin(assetPath, bin);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function ensureTools() {
|
|
76
|
-
try { fs.mkdirSync(TOOLS_DIR, { recursive: true }); } catch {}
|
|
77
|
-
if (!fs.existsSync(PKG_JSON)) {
|
|
78
|
-
try { fs.writeFileSync(PKG_JSON, JSON.stringify({ name: 'gm-tools', version: '1.0.0', private: true })); } catch {}
|
|
79
|
-
}
|
|
80
|
-
const missing = MANAGED_PKGS.filter(p => !fs.existsSync(localBin(p)));
|
|
81
|
-
if (missing.length > 0) {
|
|
82
|
-
try {
|
|
83
|
-
spawnSync('bun', ['add', ...missing.map(p => p + '@latest')], {
|
|
84
|
-
cwd: TOOLS_DIR, encoding: 'utf8', timeout: 180000, windowsHide: true
|
|
85
|
-
});
|
|
86
|
-
} catch {}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
ensureTools();
|
|
91
|
-
ensurePlugkit().catch(() => {});
|
|
92
|
-
|
|
93
|
-
const projectDir = process.env.CLAUDE_PROJECT_DIR || process.env.GEMINI_PROJECT_DIR || process.env.OC_PROJECT_DIR || process.env.KILO_PROJECT_DIR;
|
|
94
|
-
|
|
95
|
-
function loadLangPlugins(dir) {
|
|
96
|
-
if (!dir) return [];
|
|
97
|
-
const langDir = path.join(dir, 'lang');
|
|
98
|
-
if (!fs.existsSync(langDir)) return [];
|
|
99
|
-
try {
|
|
100
|
-
return fs.readdirSync(langDir)
|
|
101
|
-
.filter(f => f.endsWith('.js') && f !== 'loader.js' && f !== 'SPEC.md')
|
|
102
|
-
.reduce((acc, f) => {
|
|
103
|
-
try {
|
|
104
|
-
const p = require(path.join(langDir, f));
|
|
105
|
-
if (p && typeof p.id === 'string' && p.exec && p.exec.match instanceof RegExp && typeof p.exec.run === 'function') acc.push(p);
|
|
106
|
-
} catch (_) {}
|
|
107
|
-
return acc;
|
|
108
|
-
}, []);
|
|
109
|
-
} catch (_) { return []; }
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function getLangPluginContext(dir) {
|
|
113
|
-
const plugins = loadLangPlugins(dir);
|
|
114
|
-
return plugins
|
|
115
|
-
.filter(p => p.context)
|
|
116
|
-
.map(p => { const ctx = typeof p.context === 'function' ? p.context() : p.context; return ctx ? String(ctx).slice(0, 2000) : ''; })
|
|
117
|
-
.filter(Boolean)
|
|
118
|
-
.join('\n\n');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const ensureGitignore = () => {
|
|
122
|
-
if (!projectDir) return;
|
|
123
|
-
const gitignorePath = path.join(projectDir, '.gitignore');
|
|
124
|
-
const entry = '.gm-stop-verified';
|
|
125
|
-
try {
|
|
126
|
-
let content = '';
|
|
127
|
-
if (fs.existsSync(gitignorePath)) {
|
|
128
|
-
content = fs.readFileSync(gitignorePath, 'utf-8');
|
|
129
|
-
}
|
|
130
|
-
if (!content.split('\n').some(line => line.trim() === entry)) {
|
|
131
|
-
const newContent = content.endsWith('\n') || content === ''
|
|
132
|
-
? content + entry + '\n'
|
|
133
|
-
: content + '\n' + entry + '\n';
|
|
134
|
-
fs.writeFileSync(gitignorePath, newContent);
|
|
135
|
-
}
|
|
136
|
-
} catch (e) {}
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
ensureGitignore();
|
|
140
|
-
|
|
141
|
-
try {
|
|
142
|
-
let outputs = [];
|
|
143
|
-
|
|
144
|
-
outputs.push('Use the Skill tool with skill: "gm" to begin — do NOT use the Agent tool to load skills. Skills are invoked via the Skill tool only, never as agents. All code execution uses exec:<lang> via the Bash tool — never direct Bash(node ...) or Bash(npm ...) or Bash(npx ...) or Bash(plugkit ...).');
|
|
145
|
-
|
|
146
|
-
if (projectDir && fs.existsSync(projectDir)) {
|
|
147
|
-
try {
|
|
148
|
-
const bin = plugkitBin();
|
|
149
|
-
const r = fs.existsSync(bin)
|
|
150
|
-
? spawnSync(bin, ['codeinsight', projectDir], { encoding: 'utf8', windowsHide: true, timeout: 15000 })
|
|
151
|
-
: spawnSync('plugkit', ['codeinsight', projectDir], { encoding: 'utf8', windowsHide: true, timeout: 15000 });
|
|
152
|
-
const insightOutput = ((r.stdout || '') + (r.stderr || '')).trim();
|
|
153
|
-
if (insightOutput) {
|
|
154
|
-
outputs.push(`=== This is your initial insight of the repository, look at every possible aspect of this for initial opinionation and to offset the need for code exploration ===\n${insightOutput}`);
|
|
155
|
-
}
|
|
156
|
-
} catch (e) {}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const langCtx = getLangPluginContext(projectDir);
|
|
160
|
-
if (langCtx) outputs.push(langCtx);
|
|
161
|
-
|
|
162
|
-
const additionalContext = outputs.join('\n\n');
|
|
163
|
-
|
|
164
|
-
const isGemini = process.env.GEMINI_PROJECT_DIR !== undefined;
|
|
165
|
-
const isOpenCode = process.env.OC_PLUGIN_ROOT !== undefined;
|
|
166
|
-
const isKilo = process.env.KILO_PLUGIN_ROOT !== undefined;
|
|
167
|
-
|
|
168
|
-
if (isGemini) {
|
|
169
|
-
console.log(JSON.stringify({ systemMessage: additionalContext }, null, 2));
|
|
170
|
-
} else if (isOpenCode || isKilo) {
|
|
171
|
-
console.log(JSON.stringify({ hookSpecificOutput: { hookEventName: 'session.created', additionalContext } }, null, 2));
|
|
172
|
-
} else {
|
|
173
|
-
console.log(JSON.stringify({ hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext } }, null, 2));
|
|
174
|
-
}
|
|
175
|
-
} catch (error) {
|
|
176
|
-
const isGemini = process.env.GEMINI_PROJECT_DIR !== undefined;
|
|
177
|
-
const isOpenCode = process.env.OC_PLUGIN_ROOT !== undefined;
|
|
178
|
-
const isKilo = process.env.KILO_PLUGIN_ROOT !== undefined;
|
|
179
|
-
|
|
180
|
-
if (isGemini) {
|
|
181
|
-
console.log(JSON.stringify({ systemMessage: `Error executing hook: ${error.message}` }, null, 2));
|
|
182
|
-
} else if (isOpenCode || isKilo) {
|
|
183
|
-
console.log(JSON.stringify({ hookSpecificOutput: { hookEventName: 'session.created', additionalContext: `Error executing hook: ${error.message}` } }, null, 2));
|
|
184
|
-
} else {
|
|
185
|
-
console.log(JSON.stringify({ hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: `Error executing hook: ${error.message}` } }, null, 2));
|
|
186
|
-
}
|
|
187
|
-
process.exit(0);
|
|
188
|
-
}
|