MCPcopy
hub / github.com/garrytan/gstack / handleMetaCommand

Function handleMetaCommand

browse/src/meta-commands.ts:254–1174  ·  view source on GitHub ↗
(
  command: string,
  args: string[],
  bm: BrowserManager,
  shutdown: () => Promise<void> | void,
  tokenInfo?: TokenInfo | null,
  opts?: MetaCommandOpts,
)

Source from the content-addressed store, hash-verified

252}
253
254export async function handleMetaCommand(
255 command: string,
256 args: string[],
257 bm: BrowserManager,
258 shutdown: () => Promise<void> | void,
259 tokenInfo?: TokenInfo | null,
260 opts?: MetaCommandOpts,
261): Promise<string> {
262 // Per-tab operations use the active session; global operations use bm directly
263 const session = bm.getActiveSession();
264
265 switch (command) {
266 // ─── Tabs ──────────────────────────────────────────
267 case 'tabs': {
268 const tabs = await bm.getTabListWithTitles();
269 return tabs.map(t =>
270 `${t.active ? '→ ' : ' '}[${t.id}] ${t.title || '(untitled)'} — ${t.url}`
271 ).join('\n');
272 }
273
274 case 'tab': {
275 const id = parseInt(args[0], 10);
276 if (isNaN(id)) throw new Error('Usage: browse tab <id>');
277 bm.switchTab(id);
278 return `Switched to tab ${id}`;
279 }
280
281 case 'newtab': {
282 // --json returns structured output (machine-parseable). Other flag-like
283 // tokens are treated as the url. make-pdf always passes --json.
284 let url: string | undefined;
285 let jsonMode = false;
286 for (const a of args) {
287 if (a === '--json') { jsonMode = true; }
288 else if (!url) { url = a; }
289 }
290 const id = await bm.newTab(url);
291 if (jsonMode) {
292 return JSON.stringify({ tabId: id, url: url ?? null });
293 }
294 return `Opened tab ${id}${url ? ` → ${url}` : ''}`;
295 }
296
297 case 'closetab': {
298 const id = args[0] ? parseInt(args[0], 10) : undefined;
299 await bm.closeTab(id);
300 return `Closed tab${id ? ` ${id}` : ''}`;
301 }
302
303 case 'tab-each': {
304 // Fan out a single command across every open tab. Returns a JSON
305 // object: { results: [{tabId, url, title, status, output}], total }.
306 // Restores the originally active tab when done so the user's view
307 // doesn't shift under them.
308 //
309 // Usage: $B tab-each <command> [args...]
310 // $B tab-each snapshot -i → snapshot every tab
311 // $B tab-each text → grab clean text from every tab

Callers 5

tab-each.test.tsFile · 0.90
handoff.test.tsFile · 0.90
snapshot.test.tsFile · 0.90
commands.test.tsFile · 0.90

Calls 15

canonicalizeCommandFunction · 0.90
checkScopeFunction · 0.90
validateOutputPathFunction · 0.90
guardScreenshotBufferFunction · 0.90
guardScreenshotPathFunction · 0.90
wrapUntrustedContentFunction · 0.90
validateNavigationUrlFunction · 0.90
getCleanTextFunction · 0.90
handleSnapshotFunction · 0.90
resolveConfigFunction · 0.90
mkdirSecureFunction · 0.90
writeSecureFileFunction · 0.90

Tested by

no test coverage detected