/ clis / codex / send.js
send.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  import { SelectorError } from '@jackwener/opencli/errors';
 3  export const sendCommand = cli({
 4      site: 'codex',
 5      name: 'send',
 6      description: 'Send text/commands to the Codex AI composer',
 7      domain: 'localhost',
 8      strategy: Strategy.UI,
 9      browser: true,
10      args: [{ name: 'text', required: true, positional: true, help: 'Text, command (e.g. /review), or skill (e.g. $imagegen)' }],
11      columns: ['Status', 'InjectedText'],
12      func: async (page, kwargs) => {
13          const textToInsert = kwargs.text;
14          const injected = await page.evaluate(`
15        (function(text) {
16          let composer = document.querySelector('textarea, [contenteditable="true"]');
17          
18          const editables = Array.from(document.querySelectorAll('[contenteditable="true"]'));
19          if (editables.length > 0) {
20             composer = editables[editables.length - 1];
21          }
22  
23          if (!composer) return false;
24  
25          composer.focus();
26          document.execCommand('insertText', false, text);
27          return true;
28        })(${JSON.stringify(textToInsert)})
29      `);
30          if (!injected)
31              throw new SelectorError('Codex Composer input element');
32          // Wait for the UI to register the input
33          await page.wait(0.5);
34          // Simulate Enter key to submit
35          await page.pressKey('Enter');
36          return [
37              {
38                  Status: 'Success',
39                  InjectedText: textToInsert,
40              },
41          ];
42      },
43  });