model.js
1 import { cli, Strategy } from '@jackwener/opencli/registry'; 2 export const modelCommand = cli({ 3 site: 'codex', 4 name: 'model', 5 description: 'Get or switch the currently active AI model in Codex Desktop', 6 domain: 'localhost', 7 strategy: Strategy.UI, 8 browser: true, 9 args: [ 10 { name: 'model-name', required: false, positional: true, help: 'The ID of the model to switch to (e.g. gpt-4)' } 11 ], 12 columns: ['Status', 'Model'], 13 func: async (page, kwargs) => { 14 const desiredModel = kwargs['model-name']; 15 if (!desiredModel) { 16 // Just read the current model. We traverse iframes/webviews if needed. 17 const currentModel = await page.evaluate(` 18 (function() { 19 // Look for any typical model switcher selectors in the DOM 20 let m = document.querySelector('[title*="Model"], [aria-label*="Model"], .model-selector, [class*="ModelPicker"]'); 21 22 if (!m && document.querySelector('webview, iframe')) { 23 // Not directly in main DOM, might be in a webview, but Playwright evaluate doesn't cross origin boundaries easily without frames[]. 24 return 'Unknown (Likely inside a WebView, please focus the Chat tab)'; 25 } 26 return m ? (m.textContent || m.getAttribute('title') || m.getAttribute('aria-label')).trim() : 'Unknown or Not Found'; 27 })() 28 `); 29 return [ 30 { 31 Status: 'Active', 32 Model: currentModel, 33 }, 34 ]; 35 } 36 else { 37 // Try to switch model (click dropdown, type/select model) 38 const success = await page.evaluate(` 39 (function(targetModel) { 40 const dropdown = document.querySelector('[title*="Model"], [aria-label*="Model"], .model-selector, [class*="ModelPicker"]'); 41 if (!dropdown) return 'Dropdown not found'; 42 43 dropdown.click(); 44 return 'Dropdown clicked. Generic interaction initiated.'; 45 })(${JSON.stringify(desiredModel)}) 46 `); 47 return [ 48 { 49 Status: success, 50 Model: desiredModel, 51 }, 52 ]; 53 } 54 }, 55 });