/ clis / doubao-app / ask.js
ask.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  import { SEL, injectTextScript, clickSendScript, pollResponseScript } from './utils.js';
 3  export const askCommand = cli({
 4      site: 'doubao-app',
 5      name: 'ask',
 6      description: 'Send a message to Doubao desktop app and wait for the AI response',
 7      domain: 'doubao-app',
 8      strategy: Strategy.UI,
 9      browser: true,
10      args: [
11          { name: 'text', required: true, positional: true, help: 'Prompt to send' },
12          { name: 'timeout', type: 'int', default: 30, help: 'Max seconds to wait for response' },
13      ],
14      columns: ['Role', 'Text'],
15      func: async (page, kwargs) => {
16          const text = kwargs.text;
17          const timeout = kwargs.timeout || 30;
18          // Count existing messages before sending
19          const beforeCount = await page.evaluate(`document.querySelectorAll('${SEL.MESSAGE}').length`);
20          // Inject text + send
21          const injected = await page.evaluate(injectTextScript(text));
22          if (!injected?.ok)
23              throw new Error('Could not find chat input.');
24          await page.wait(0.5);
25          const clicked = await page.evaluate(clickSendScript());
26          if (!clicked)
27              await page.pressKey('Enter');
28          // Poll for response
29          const pollInterval = 1;
30          const maxPolls = Math.ceil(timeout / pollInterval);
31          let response = '';
32          for (let i = 0; i < maxPolls; i++) {
33              await page.wait(pollInterval);
34              const result = await page.evaluate(pollResponseScript(beforeCount));
35              if (!result)
36                  continue;
37              if (result.phase === 'done' && result.text) {
38                  response = result.text;
39                  break;
40              }
41          }
42          if (!response) {
43              return [
44                  { Role: 'User', Text: text },
45                  { Role: 'System', Text: `No response received within ${timeout}s.` },
46              ];
47          }
48          return [
49              { Role: 'User', Text: text },
50              { Role: 'Assistant', Text: response },
51          ];
52      },
53  });