/ clis / chatgpt-app / read.js
read.js
 1  import { execSync } from 'node:child_process';
 2  import { cli, Strategy } from '@jackwener/opencli/registry';
 3  import { CommandExecutionError, ConfigError, getErrorMessage } from '@jackwener/opencli/errors';
 4  import { getVisibleChatMessages } from './ax.js';
 5  export const readCommand = cli({
 6      site: 'chatgpt-app',
 7      name: 'read',
 8      description: 'Read the last visible message from the focused ChatGPT Desktop window',
 9      domain: 'localhost',
10      strategy: Strategy.PUBLIC,
11      browser: false,
12      args: [],
13      columns: ['Role', 'Text'],
14      func: async (page) => {
15          if (process.platform !== 'darwin') {
16              throw new ConfigError('ChatGPT Desktop integration requires macOS (osascript is not available on this platform)');
17          }
18          try {
19              execSync("osascript -e 'tell application \"ChatGPT\" to activate'");
20              execSync("osascript -e 'delay 0.3'");
21              const messages = getVisibleChatMessages();
22              if (!messages.length) {
23                  return [{ Role: 'System', Text: 'No visible chat messages were found in the current ChatGPT window.' }];
24              }
25              return [{ Role: 'Assistant', Text: messages[messages.length - 1] }];
26          }
27          catch (err) {
28              throw new CommandExecutionError("Failed to read from ChatGPT: " + getErrorMessage(err));
29          }
30      },
31  });