|
| 1 | +import * as readline from 'readline'; |
| 2 | +import { createInterface } from 'readline/promises'; |
| 3 | +import { Writable } from 'stream'; |
| 4 | + |
| 5 | +import chalk from 'chalk'; |
| 6 | + |
| 7 | +import { userMessages } from '../tools/interaction/userMessage.js'; |
| 8 | + |
| 9 | +// Custom output stream to intercept console output |
| 10 | +class OutputInterceptor extends Writable { |
| 11 | + private originalStdout: NodeJS.WriteStream; |
| 12 | + private paused: boolean = false; |
| 13 | + |
| 14 | + constructor(originalStdout: NodeJS.WriteStream) { |
| 15 | + super(); |
| 16 | + this.originalStdout = originalStdout; |
| 17 | + } |
| 18 | + |
| 19 | + pause() { |
| 20 | + this.paused = true; |
| 21 | + } |
| 22 | + |
| 23 | + resume() { |
| 24 | + this.paused = false; |
| 25 | + } |
| 26 | + |
| 27 | + _write( |
| 28 | + chunk: Buffer | string, |
| 29 | + encoding: BufferEncoding, |
| 30 | + callback: (error?: Error | null) => void, |
| 31 | + ): void { |
| 32 | + if (!this.paused) { |
| 33 | + this.originalStdout.write(chunk, encoding); |
| 34 | + } |
| 35 | + callback(); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Initialize interactive input mode |
| 40 | +export const initInteractiveInput = () => { |
| 41 | + // Save original stdout |
| 42 | + const originalStdout = process.stdout; |
| 43 | + |
| 44 | + // Create interceptor |
| 45 | + const interceptor = new OutputInterceptor(originalStdout); |
| 46 | + |
| 47 | + // We no longer try to replace process.stdout as it's not allowed in newer Node.js versions |
| 48 | + // Instead, we'll just use the interceptor for readline |
| 49 | + |
| 50 | + // Create readline interface for listening to key presses |
| 51 | + const rl = readline.createInterface({ |
| 52 | + input: process.stdin, |
| 53 | + output: interceptor, |
| 54 | + terminal: true, |
| 55 | + }); |
| 56 | + |
| 57 | + // Close the interface to avoid keeping the process alive |
| 58 | + rl.close(); |
| 59 | + |
| 60 | + // Listen for keypress events |
| 61 | + readline.emitKeypressEvents(process.stdin); |
| 62 | + if (process.stdin.isTTY) { |
| 63 | + process.stdin.setRawMode(true); |
| 64 | + } |
| 65 | + |
| 66 | + process.stdin.on('keypress', async (str, key) => { |
| 67 | + // Check for Ctrl+C to exit |
| 68 | + if (key.ctrl && key.name === 'c') { |
| 69 | + process.exit(0); |
| 70 | + } |
| 71 | + |
| 72 | + // Check for Ctrl+M to enter message mode |
| 73 | + if (key.ctrl && key.name === 'm') { |
| 74 | + // Pause output |
| 75 | + interceptor.pause(); |
| 76 | + |
| 77 | + // Create a readline interface for input |
| 78 | + const inputRl = createInterface({ |
| 79 | + input: process.stdin, |
| 80 | + output: originalStdout, |
| 81 | + }); |
| 82 | + |
| 83 | + try { |
| 84 | + // Reset cursor position and clear line |
| 85 | + originalStdout.write('\r\n'); |
| 86 | + originalStdout.write( |
| 87 | + chalk.green( |
| 88 | + 'Enter correction or additional context (Ctrl+C to cancel):\n', |
| 89 | + ) + '> ', |
| 90 | + ); |
| 91 | + |
| 92 | + // Get user input |
| 93 | + const userInput = await inputRl.question(''); |
| 94 | + |
| 95 | + // Add message to queue if not empty |
| 96 | + if (userInput.trim()) { |
| 97 | + userMessages.push(userInput); |
| 98 | + originalStdout.write( |
| 99 | + chalk.green('\nMessage sent to agent. Resuming output...\n\n'), |
| 100 | + ); |
| 101 | + } else { |
| 102 | + originalStdout.write( |
| 103 | + chalk.yellow('\nEmpty message not sent. Resuming output...\n\n'), |
| 104 | + ); |
| 105 | + } |
| 106 | + } catch (error) { |
| 107 | + originalStdout.write( |
| 108 | + chalk.red(`\nError sending message: ${error}\n\n`), |
| 109 | + ); |
| 110 | + } finally { |
| 111 | + // Close input readline interface |
| 112 | + inputRl.close(); |
| 113 | + |
| 114 | + // Resume output |
| 115 | + interceptor.resume(); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + // Return a cleanup function |
| 121 | + return () => { |
| 122 | + // We no longer need to restore process.stdout |
| 123 | + |
| 124 | + // Disable raw mode |
| 125 | + if (process.stdin.isTTY) { |
| 126 | + process.stdin.setRawMode(false); |
| 127 | + } |
| 128 | + }; |
| 129 | +}; |
0 commit comments