/ clis / chatwise / export.js
export.js
 1  import * as fs from 'node:fs';
 2  import { cli, Strategy } from '@jackwener/opencli/registry';
 3  export const exportCommand = cli({
 4      site: 'chatwise',
 5      name: 'export',
 6      description: 'Export the current ChatWise conversation to a Markdown file',
 7      domain: 'localhost',
 8      strategy: Strategy.UI,
 9      browser: true,
10      args: [
11          { name: 'output', required: false, help: 'Output file (default: /tmp/chatwise-export.md)' },
12      ],
13      columns: ['Status', 'File', 'Messages'],
14      func: async (page, kwargs) => {
15          const outputPath = kwargs.output || '/tmp/chatwise-export.md';
16          const md = await page.evaluate(`
17        (function() {
18          const selectors = [
19            '[data-message-id]',
20            '[class*="message"]',
21            '[class*="chat-item"]',
22            '[class*="bubble"]',
23          ];
24          
25          for (const sel of selectors) {
26            const nodes = document.querySelectorAll(sel);
27            if (nodes.length > 0) {
28              return Array.from(nodes).map((n, i) => '## Message ' + (i + 1) + '\\n\\n' + (n.innerText || n.textContent).trim()).join('\\n\\n---\\n\\n');
29            }
30          }
31          
32          const main = document.querySelector('main, [role="main"], [class*="chat-container"]');
33          if (main) return main.innerText || main.textContent;
34          return document.body.innerText;
35        })()
36      `);
37          fs.writeFileSync(outputPath, '# ChatWise Conversation Export\\n\\n' + md);
38          return [
39              {
40                  Status: 'Success',
41                  File: outputPath,
42                  Messages: md.split('## Message').length - 1,
43              },
44          ];
45      },
46  });