/ clis / cursor / model.js
model.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  export const modelCommand = cli({
 3      site: 'cursor',
 4      name: 'model',
 5      description: 'Get or switch the currently active AI model in Cursor',
 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. claude-3.5-sonnet)' }
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
17              const currentModel = await page.evaluate(`
18          (function() {
19            const m = document.querySelector('.composer-unified-dropdown-model span, [class*="unifiedmodeldropdown"] span');
20            return m ? m.textContent.trim() : 'Unknown or Not Found';
21          })()
22        `);
23              return [
24                  {
25                      Status: 'Active',
26                      Model: currentModel,
27                  },
28              ];
29          }
30          else {
31              // Try to switch model (click dropdown, type/select model)
32              const success = await page.evaluate(`
33          (function(targetModel) {
34            const dropdown = document.querySelector('.composer-unified-dropdown-model, [class*="unifiedmodeldropdown"]');
35            if (!dropdown) return 'Dropdown not found';
36            
37            dropdown.click();
38            // After clicking, the DOM usually spawns a popup list.
39            // Because it's hard to predict exactly how the list renders, 
40            // a simple scriptable approach is just to click it, and hope we can select it via UI.
41            // In many React apps, clicking it opens a menu, and clicking the item works.
42            return 'Dropdown opened. Automated switching is not fully generic. Please implement precise list navigation depending on DOM.';
43          })(${JSON.stringify(desiredModel)})
44        `);
45              return [
46                  {
47                      Status: success,
48                      Model: desiredModel,
49                  },
50              ];
51          }
52      },
53  });