Monorepo for Aesthetic.Computer aesthetic.computer
at main 179 lines 5.3 kB view raw
1#!/usr/bin/env node 2// cdp-cli.mjs - Command-line tools for managing CDP connections 3 4import CDP, { createCDP, withCDP } from './cdp.mjs'; 5 6const commands = { 7 async list() { 8 const cdp = new CDP({ verbose: true }); 9 const targets = await cdp.listTargets(); 10 11 console.log('\n📋 Available CDP Targets:\n'); 12 13 const acPages = targets.filter(t => 14 t.url && (t.url.includes('localhost:8888') || t.url.includes('aesthetic.computer')) 15 ); 16 17 if (acPages.length > 0) { 18 console.log('🎨 Aesthetic Computer Pages:'); 19 acPages.forEach(t => { 20 console.log(` ${t.type.padEnd(8)} ${t.url}`); 21 console.log(` ID: ${t.id}`); 22 }); 23 console.log(''); 24 } 25 26 const vscodePages = targets.filter(t => 27 t.url && t.url.includes('vscode') && !acPages.includes(t) 28 ); 29 30 if (vscodePages.length > 0) { 31 console.log(`📝 VS Code Pages (${vscodePages.length} total):`); 32 vscodePages.slice(0, 3).forEach(t => { 33 const title = t.title?.substring(0, 60) || 'Untitled'; 34 console.log(` ${t.type.padEnd(8)} ${title}`); 35 }); 36 if (vscodePages.length > 3) { 37 console.log(` ... and ${vscodePages.length - 3} more`); 38 } 39 console.log(''); 40 } 41 42 console.log(`Total targets: ${targets.length}`); 43 }, 44 45 async find(urlPattern) { 46 if (!urlPattern) { 47 console.error('Usage: cdp-cli find <url-pattern>'); 48 process.exit(1); 49 } 50 51 const cdp = new CDP({ verbose: true }); 52 const target = await cdp.findPage(urlPattern); 53 54 if (target) { 55 console.log('\n✅ Found target:'); 56 console.log(` URL: ${target.url}`); 57 console.log(` ID: ${target.id}`); 58 console.log(` Type: ${target.type}`); 59 console.log(` WS: ${target.webSocketDebuggerUrl}`); 60 } else { 61 console.log(`\n❌ No target found matching: ${urlPattern}`); 62 const pages = await cdp.findAestheticPages(); 63 if (pages.length > 0) { 64 console.log('\nAvailable aesthetic.computer pages:'); 65 pages.forEach(p => console.log(` - ${p.url}`)); 66 } 67 process.exit(1); 68 } 69 }, 70 71 async connect(urlPattern) { 72 const targetUrl = urlPattern || 'https://localhost:8888/prompt'; 73 74 await withCDP(async (cdp) => { 75 const info = await cdp.getPageInfo(); 76 console.log('\n✅ Connected to page:'); 77 console.log(` URL: ${info.url}`); 78 console.log(` Title: ${info.title}`); 79 console.log(` Ready: ${info.readyState}`); 80 console.log(` Has Bios: ${info.hasACBios ? '✓' : '✗'}`); 81 }, { targetUrl, verbose: true }); 82 }, 83 84 async eval(expression) { 85 if (!expression) { 86 console.error('Usage: cdp-cli eval <expression>'); 87 process.exit(1); 88 } 89 90 await withCDP(async (cdp) => { 91 const result = await cdp.eval(expression); 92 console.log(result); 93 }, { verbose: false }); 94 }, 95 96 async cache() { 97 const cdp = new CDP({ verbose: true }); 98 const target = await cdp.findPage('https://localhost:8888'); 99 100 if (target) { 101 await cdp.cacheTarget(target); 102 console.log('✅ Cached target for fast reconnection'); 103 } else { 104 console.log('❌ No aesthetic.computer page found to cache'); 105 process.exit(1); 106 } 107 }, 108 109 async test() { 110 console.log('🧪 Testing CDP connection...\n'); 111 112 await withCDP(async (cdp) => { 113 // Test basic eval 114 const result = await cdp.eval('2 + 2'); 115 console.log(`✓ Basic eval: 2 + 2 = ${result}`); 116 117 // Test page info 118 const info = await cdp.getPageInfo(); 119 console.log(`✓ Page info: ${info.title}`); 120 121 // Test performance API access 122 const perfTest = await cdp.eval(` 123 performance.getEntriesByType('resource').length 124 `); 125 console.log(`✓ Performance API: ${perfTest} resource entries`); 126 127 // Test bios access (if available) 128 if (info.hasACBios) { 129 const biosTest = await cdp.eval('typeof window.bios'); 130 console.log(`✓ AC Bios: ${biosTest}`); 131 } 132 133 console.log('\n✅ All tests passed!'); 134 }, { verbose: true }); 135 }, 136 137 help() { 138 console.log(` 139CDP CLI - Chrome DevTools Protocol utilities for Aesthetic Computer 140 141Usage: node cdp-cli.mjs <command> [args] 142 143Commands: 144 list List all available CDP targets 145 find <pattern> Find a specific target by URL pattern 146 connect [url] Test connection to a target (default: prompt) 147 eval <expression> Evaluate JavaScript in the page 148 cache Cache current aesthetic.computer page for fast access 149 test Run connection tests 150 help Show this help message 151 152Examples: 153 node cdp-cli.mjs list 154 node cdp-cli.mjs find prompt 155 node cdp-cli.mjs connect 156 node cdp-cli.mjs eval "document.title" 157 node cdp-cli.mjs cache 158 node cdp-cli.mjs test 159`); 160 } 161}; 162 163// Run command 164const [,, command, ...args] = process.argv; 165 166if (!command || !commands[command]) { 167 commands.help(); 168 process.exit(command ? 1 : 0); 169} 170 171try { 172 await commands[command](...args); 173} catch (err) { 174 console.error(`\n❌ Error: ${err.message}`); 175 if (err.stack && process.env.DEBUG) { 176 console.error(err.stack); 177 } 178 process.exit(1); 179}