gm-kilo 2.0.239 → 2.0.242
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/hooks/pre-tool-use-hook.js +92 -78
- package/package.json +1 -1
|
@@ -225,15 +225,90 @@ const run = () => {
|
|
|
225
225
|
const command = (tool_input?.command || '').trim();
|
|
226
226
|
const stripFooter = (s) => s.replace(/\n\[Running tools\][\s\S]*$/, '').trimEnd();
|
|
227
227
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
228
|
+
// ─── agent-browser: CLI commands ──────────────────────────────────────────
|
|
229
|
+
const abCliMatch = command.match(/^agent-browser:\n([\s\S]+)$/);
|
|
230
|
+
if (abCliMatch) {
|
|
231
|
+
const abCode = abCliMatch[1];
|
|
232
|
+
const abNative = (() => {
|
|
233
|
+
const abDir = path.join(TOOLS_DIR, 'node_modules', 'agent-browser', 'bin');
|
|
234
|
+
const ext = IS_WIN ? '.exe' : '';
|
|
235
|
+
const archMap = { x64: 'x64', arm64: 'arm64', ia32: 'x64' };
|
|
236
|
+
const osMap = { win32: 'win32', darwin: 'darwin', linux: 'linux' };
|
|
237
|
+
const candidate = path.join(abDir, `agent-browser-${osMap[process.platform] || process.platform}-${archMap[process.arch] || process.arch}${ext}`);
|
|
238
|
+
return fs.existsSync(candidate) ? candidate : null;
|
|
239
|
+
})();
|
|
240
|
+
const abBin = abNative || (fs.existsSync(localBin('agent-browser')) ? localBin('agent-browser') : 'agent-browser');
|
|
241
|
+
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']);
|
|
242
|
+
const AB_GLOBAL_FLAGS = new Set(['--cdp','--headed','--headless','--session','--session-name','--auto-connect','--profile','--allow-file-access','--color-scheme','-p','--platform','--device']);
|
|
243
|
+
const AB_GLOBAL_FLAGS_WITH_VALUE = new Set(['--cdp','--session','--session-name','--profile','--color-scheme','-p','--platform','--device']);
|
|
244
|
+
const AB_SESSION_STATE = path.join(os.tmpdir(), 'gm-ab-sessions.json');
|
|
245
|
+
function readAbSessions() { try { return JSON.parse(fs.readFileSync(AB_SESSION_STATE, 'utf8')); } catch { return {}; } }
|
|
246
|
+
function writeAbSessions(s) { try { fs.writeFileSync(AB_SESSION_STATE, JSON.stringify(s)); } catch {} }
|
|
247
|
+
function parseAbLine(line) {
|
|
248
|
+
const tokens = line.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || [];
|
|
249
|
+
const globalArgs = [], rest = [];
|
|
250
|
+
let i = 0;
|
|
251
|
+
while (i < tokens.length) {
|
|
252
|
+
if (AB_GLOBAL_FLAGS.has(tokens[i])) {
|
|
253
|
+
globalArgs.push(tokens[i]);
|
|
254
|
+
if (AB_GLOBAL_FLAGS_WITH_VALUE.has(tokens[i]) && i + 1 < tokens.length && !tokens[i+1].startsWith('--')) globalArgs.push(tokens[++i]);
|
|
255
|
+
i++;
|
|
256
|
+
} else { rest.push(...tokens.slice(i)); break; }
|
|
257
|
+
}
|
|
258
|
+
return { globalArgs, rest };
|
|
259
|
+
}
|
|
260
|
+
const spawnAb = (bin, args, stdin) => {
|
|
261
|
+
const opts = { encoding: 'utf-8', timeout: 60000, windowsHide: true, ...(IS_WIN && { shell: true }), cwd: process.cwd(), ...(stdin !== undefined && { input: stdin }) };
|
|
262
|
+
const r = spawnSync(bin, args, opts);
|
|
263
|
+
if (!r.stdout && !r.stderr && r.error) return `[spawn error: ${r.error.message}]`;
|
|
264
|
+
const out = (r.stdout || '').trimEnd(), err = stripFooter(r.stderr || '').trimEnd();
|
|
265
|
+
return out && err ? out + '\n[stderr]\n' + err : stripFooter(out || err);
|
|
266
|
+
};
|
|
267
|
+
try {
|
|
268
|
+
const safeAb = abCode.trim();
|
|
269
|
+
const firstParsed = parseAbLine(safeAb.split('\n')[0].trim());
|
|
270
|
+
const firstWord = (firstParsed.rest[0] || '').toLowerCase();
|
|
271
|
+
const sessionName = (() => { const si = firstParsed.globalArgs.indexOf('--session'); return si >= 0 ? firstParsed.globalArgs[si+1] : 'default'; })();
|
|
272
|
+
const sessions = readAbSessions();
|
|
273
|
+
if (['open','goto','navigate'].includes(firstWord)) sessions[sessionName] = { url: firstParsed.rest[1] || '?', ts: Date.now() };
|
|
274
|
+
if (['close','quit','exit'].includes(firstWord)) delete sessions[sessionName];
|
|
275
|
+
writeAbSessions(sessions);
|
|
276
|
+
const openSessions = Object.entries(sessions);
|
|
277
|
+
let result;
|
|
278
|
+
if (AB_CMDS.has(firstWord)) {
|
|
279
|
+
const lines = safeAb.split('\n').map(l => l.trim()).filter(Boolean);
|
|
280
|
+
if (lines.length === 1) {
|
|
281
|
+
const { globalArgs, rest } = parseAbLine(lines[0]);
|
|
282
|
+
result = spawnAb(abBin, [...globalArgs, ...rest]);
|
|
283
|
+
} else {
|
|
284
|
+
const hasClose = lines.some(l => { const w = (parseAbLine(l).rest[0]||'').toLowerCase(); return ['close','quit','exit'].includes(w); });
|
|
285
|
+
const batchGlobals = firstParsed.globalArgs;
|
|
286
|
+
const results = [];
|
|
287
|
+
for (const l of lines) {
|
|
288
|
+
const { globalArgs, rest } = parseAbLine(l);
|
|
289
|
+
const mergedGlobals = [...batchGlobals.filter(f => !globalArgs.includes(f)), ...globalArgs];
|
|
290
|
+
const w = (rest[0]||'').toLowerCase();
|
|
291
|
+
if (['open','goto','navigate'].includes(w)) sessions[sessionName] = { url: rest[1]||'?', ts: Date.now() };
|
|
292
|
+
if (['close','quit','exit'].includes(w)) delete sessions[sessionName];
|
|
293
|
+
const args = AB_CMDS.has(w) ? [...mergedGlobals, ...rest] : [...mergedGlobals, 'eval', '--stdin'];
|
|
294
|
+
const stdin = AB_CMDS.has(w) ? undefined : l.trim();
|
|
295
|
+
results.push(spawnAb(abBin, args, stdin));
|
|
296
|
+
}
|
|
297
|
+
writeAbSessions(sessions);
|
|
298
|
+
result = results.filter(Boolean).join('\n');
|
|
299
|
+
if (!hasClose && openSessions.length > 0) result += `\n\n[tab] Browser session "${sessionName}" still open. Close when done:\n agent-browser:\n close`;
|
|
300
|
+
}
|
|
301
|
+
} else {
|
|
302
|
+
result = spawnAb(abBin, ['eval', '--stdin'], safeAb);
|
|
303
|
+
}
|
|
304
|
+
if (openSessions.length > 1) {
|
|
305
|
+
const stale = openSessions.filter(([n]) => n !== sessionName).map(([n,v]) => ` "${n}" → ${v.url} (${Math.round((Date.now()-v.ts)/60000)}min ago)`).join('\n');
|
|
306
|
+
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)`;
|
|
307
|
+
}
|
|
308
|
+
return allowWithNoop(`agent-browser output:\n\n${result || '(no output)'}`);
|
|
309
|
+
} catch(e) {
|
|
310
|
+
return allowWithNoop(`agent-browser error:\n\n${e.message || '(exec failed)'}`);
|
|
311
|
+
}
|
|
237
312
|
}
|
|
238
313
|
|
|
239
314
|
const execMatch = command.match(/^exec(?::(\S+))?\n([\s\S]+)$/);
|
|
@@ -241,13 +316,13 @@ const run = () => {
|
|
|
241
316
|
const rawLang = (execMatch[1] || '').toLowerCase();
|
|
242
317
|
const code = execMatch[2];
|
|
243
318
|
if (/^\s*agent-browser\s/.test(code)) {
|
|
244
|
-
return deny(`Do not call agent-browser via exec:bash. Use
|
|
319
|
+
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`);
|
|
245
320
|
}
|
|
246
321
|
const cwd = tool_input?.cwd;
|
|
247
322
|
|
|
248
323
|
// ─── Lang plugin dispatch ─────────────────────────────────────────────
|
|
249
324
|
if (rawLang) {
|
|
250
|
-
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'
|
|
325
|
+
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']);
|
|
251
326
|
if (!builtins.has(rawLang)) {
|
|
252
327
|
const plugins = loadLangPlugins(projectDir);
|
|
253
328
|
const plugin = plugins.find(p => p.exec.match.test(`exec:${rawLang}\n${code}`));
|
|
@@ -274,7 +349,7 @@ const run = () => {
|
|
|
274
349
|
return 'nodejs';
|
|
275
350
|
};
|
|
276
351
|
// Note: 'cmd' is NOT aliased to 'bash' — it has its own handler below
|
|
277
|
-
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'
|
|
352
|
+
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' };
|
|
278
353
|
const lang = aliases[rawLang] || rawLang || detectLang(code);
|
|
279
354
|
const langExts = { nodejs: 'mjs', typescript: 'ts', deno: 'ts', python: 'py', bash: 'sh', powershell: 'ps1', go: 'go', rust: 'rs', c: 'c', cpp: 'cpp', java: 'java' };
|
|
280
355
|
|
|
@@ -343,11 +418,6 @@ const run = () => {
|
|
|
343
418
|
const r = runGmExec(['type', taskId, inputData], { timeout: 15000 });
|
|
344
419
|
return allowWithNoop(`exec:type output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
345
420
|
}
|
|
346
|
-
if (lang === 'pm2list') {
|
|
347
|
-
const r = runGmExec(['pm2list'], { timeout: 15000 });
|
|
348
|
-
return allowWithNoop(`exec:pm2list output:\n\n${stripFooter((r.stdout || '') + (r.stderr || ''))}`);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
421
|
try {
|
|
352
422
|
let result;
|
|
353
423
|
if (lang === 'bash') {
|
|
@@ -368,8 +438,8 @@ const run = () => {
|
|
|
368
438
|
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); } }`;
|
|
369
439
|
result = runWithFile(lang || 'nodejs', wrapped);
|
|
370
440
|
} else if (lang === 'agent-browser') {
|
|
371
|
-
// agent-browser
|
|
372
|
-
//
|
|
441
|
+
// exec:agent-browser = JS eval in browser page context only.
|
|
442
|
+
// Browser CLI commands (open, click, snapshot, headed mode, etc.) use agent-browser: prefix.
|
|
373
443
|
const abNative = (() => {
|
|
374
444
|
const abDir = path.join(TOOLS_DIR, 'node_modules', 'agent-browser', 'bin');
|
|
375
445
|
const ext = IS_WIN ? '.exe' : '';
|
|
@@ -379,63 +449,7 @@ const run = () => {
|
|
|
379
449
|
return fs.existsSync(candidate) ? candidate : null;
|
|
380
450
|
})();
|
|
381
451
|
const abBin = abNative || (fs.existsSync(localBin('agent-browser')) ? localBin('agent-browser') : 'agent-browser');
|
|
382
|
-
|
|
383
|
-
const AB_GLOBAL_FLAGS = new Set(['--cdp','--headed','--headless','--session','--session-name','--auto-connect','--profile','--allow-file-access','--color-scheme','-p','--platform','--device']);
|
|
384
|
-
const AB_GLOBAL_FLAGS_WITH_VALUE = new Set(['--cdp','--session','--session-name','--profile','--color-scheme','-p','--platform','--device']);
|
|
385
|
-
const AB_SESSION_STATE = path.join(os.tmpdir(), 'gm-ab-sessions.json');
|
|
386
|
-
function readAbSessions() { try { return JSON.parse(fs.readFileSync(AB_SESSION_STATE, 'utf8')); } catch { return {}; } }
|
|
387
|
-
function writeAbSessions(s) { try { fs.writeFileSync(AB_SESSION_STATE, JSON.stringify(s)); } catch {} }
|
|
388
|
-
function parseAbLine(line) {
|
|
389
|
-
const tokens = line.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || [];
|
|
390
|
-
const globalArgs = [], rest = [];
|
|
391
|
-
let i = 0;
|
|
392
|
-
while (i < tokens.length) {
|
|
393
|
-
if (AB_GLOBAL_FLAGS.has(tokens[i])) {
|
|
394
|
-
globalArgs.push(tokens[i]);
|
|
395
|
-
if (AB_GLOBAL_FLAGS_WITH_VALUE.has(tokens[i]) && i + 1 < tokens.length && !tokens[i+1].startsWith('--')) {
|
|
396
|
-
globalArgs.push(tokens[++i]);
|
|
397
|
-
}
|
|
398
|
-
i++;
|
|
399
|
-
} else { rest.push(...tokens.slice(i)); break; }
|
|
400
|
-
}
|
|
401
|
-
return { globalArgs, rest };
|
|
402
|
-
}
|
|
403
|
-
const firstLineParsed = parseAbLine(safeCode.trim().split('\n')[0].trim());
|
|
404
|
-
const firstWord = (firstLineParsed.rest[0] || '').toLowerCase();
|
|
405
|
-
const sessionName = (() => { const si = firstLineParsed.globalArgs.indexOf('--session'); return si >= 0 ? firstLineParsed.globalArgs[si+1] : 'default'; })();
|
|
406
|
-
const isOpen = ['open','goto','navigate'].includes(firstWord);
|
|
407
|
-
const isClose = ['close','quit','exit'].includes(firstWord);
|
|
408
|
-
const sessions = readAbSessions();
|
|
409
|
-
if (isOpen) sessions[sessionName] = { url: (firstLineParsed.rest[1] || '?'), ts: Date.now() };
|
|
410
|
-
if (isClose) delete sessions[sessionName];
|
|
411
|
-
writeAbSessions(sessions);
|
|
412
|
-
const openSessions = Object.entries(sessions);
|
|
413
|
-
if (AB_CMDS.has(firstWord)) {
|
|
414
|
-
const lines = safeCode.split('\n').map(l => l.trim()).filter(Boolean);
|
|
415
|
-
if (lines.length === 1) {
|
|
416
|
-
const { globalArgs, rest } = parseAbLine(lines[0]);
|
|
417
|
-
result = spawnDirect(abBin, [...globalArgs, ...rest]);
|
|
418
|
-
} else {
|
|
419
|
-
const hasClose = lines.some(l => { const w = (parseAbLine(l).rest[0]||'').toLowerCase(); return ['close','quit','exit'].includes(w); });
|
|
420
|
-
const cmds = lines.map(l => {
|
|
421
|
-
const { globalArgs, rest } = parseAbLine(l);
|
|
422
|
-
const w = (rest[0]||'').toLowerCase();
|
|
423
|
-
if (['open','goto','navigate'].includes(w)) sessions[sessionName] = { url: rest[1]||'?', ts: Date.now() };
|
|
424
|
-
if (['close','quit','exit'].includes(w)) delete sessions[sessionName];
|
|
425
|
-
if (!AB_CMDS.has(w)) return [...globalArgs, 'eval', l.trim()];
|
|
426
|
-
return [...globalArgs, ...rest];
|
|
427
|
-
});
|
|
428
|
-
writeAbSessions(sessions);
|
|
429
|
-
result = spawnDirect(abBin, ['batch'], JSON.stringify(cmds));
|
|
430
|
-
if (!hasClose && openSessions.length > 0) result += `\n\n[tab] Browser session "${sessionName}" still open. Close when done:\n exec:agent-browser\n close`;
|
|
431
|
-
}
|
|
432
|
-
} else {
|
|
433
|
-
result = spawnDirect(abBin, ['eval', '--stdin'], safeCode);
|
|
434
|
-
}
|
|
435
|
-
if (openSessions.length > 1) {
|
|
436
|
-
const stale = openSessions.filter(([n]) => n !== sessionName).map(([n,v]) => ` "${n}" → ${v.url} (${Math.round((Date.now()-v.ts)/60000)}min ago)`).join('\n');
|
|
437
|
-
result = (result || '') + `\n\n[tab] ${openSessions.length - 1} other session(s) still open:\n${stale}\n Close with: exec:agent-browser\\nclose (or --session <name> close)`;
|
|
438
|
-
}
|
|
452
|
+
result = spawnDirect(abBin, ['eval', '--stdin'], safeCode);
|
|
439
453
|
} else {
|
|
440
454
|
result = runWithFile(lang, safeCode);
|
|
441
455
|
}
|
|
@@ -449,8 +463,8 @@ const run = () => {
|
|
|
449
463
|
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.`);
|
|
450
464
|
}
|
|
451
465
|
|
|
452
|
-
if (!/^exec(\s|:)/.test(command) && !/^git /.test(command) && !/(\bclaude\b)/.test(command) && !/^npm install .* \/config\/.gmweb/.test(command) && !/^bun install --cwd \/config\/.gmweb/.test(command)) {
|
|
453
|
-
return deny(`Bash is restricted to exec:<lang
|
|
466
|
+
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)) {
|
|
467
|
+
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.`);
|
|
454
468
|
}
|
|
455
469
|
}
|
|
456
470
|
|