@@ -24,7 +24,11 @@ class OutputInterceptor extends Writable {
24
24
this . paused = false ;
25
25
}
26
26
27
- _write ( chunk : Buffer | string , encoding : BufferEncoding , callback : ( error ?: Error | null ) => void ) : void {
27
+ _write (
28
+ chunk : Buffer | string ,
29
+ encoding : BufferEncoding ,
30
+ callback : ( error ?: Error | null ) => void ,
31
+ ) : void {
28
32
if ( ! this . paused ) {
29
33
this . originalStdout . write ( chunk , encoding ) ;
30
34
}
@@ -36,83 +40,92 @@ class OutputInterceptor extends Writable {
36
40
export const initInteractiveInput = ( ) => {
37
41
// Save original stdout
38
42
const originalStdout = process . stdout ;
39
-
43
+
40
44
// Create interceptor
41
45
const interceptor = new OutputInterceptor ( originalStdout ) ;
42
-
46
+
43
47
// Replace stdout with our interceptor
44
48
// @ts -expect-error - This is a hack to replace stdout
45
49
process . stdout = interceptor ;
46
-
50
+
47
51
// Create readline interface for listening to key presses
48
52
const rl = readline . createInterface ( {
49
53
input : process . stdin ,
50
54
output : interceptor ,
51
55
terminal : true ,
52
56
} ) ;
53
-
57
+
54
58
// Close the interface to avoid keeping the process alive
55
59
rl . close ( ) ;
56
-
60
+
57
61
// Listen for keypress events
58
62
readline . emitKeypressEvents ( process . stdin ) ;
59
63
if ( process . stdin . isTTY ) {
60
64
process . stdin . setRawMode ( true ) ;
61
65
}
62
-
66
+
63
67
process . stdin . on ( 'keypress' , async ( str , key ) => {
64
68
// Check for Ctrl+C to exit
65
69
if ( key . ctrl && key . name === 'c' ) {
66
70
process . exit ( 0 ) ;
67
71
}
68
-
72
+
69
73
// Check for Ctrl+M to enter message mode
70
74
if ( key . ctrl && key . name === 'm' ) {
71
75
// Pause output
72
76
interceptor . pause ( ) ;
73
-
77
+
74
78
// Create a readline interface for input
75
79
const inputRl = createInterface ( {
76
80
input : process . stdin ,
77
81
output : originalStdout ,
78
82
} ) ;
79
-
83
+
80
84
try {
81
85
// Reset cursor position and clear line
82
86
originalStdout . write ( '\r\n' ) ;
83
- originalStdout . write ( chalk . green ( 'Enter correction or additional context (Ctrl+C to cancel):\n' ) + '> ' ) ;
84
-
87
+ originalStdout . write (
88
+ chalk . green (
89
+ 'Enter correction or additional context (Ctrl+C to cancel):\n' ,
90
+ ) + '> ' ,
91
+ ) ;
92
+
85
93
// Get user input
86
94
const userInput = await inputRl . question ( '' ) ;
87
-
95
+
88
96
// Add message to queue if not empty
89
97
if ( userInput . trim ( ) ) {
90
98
userMessages . push ( userInput ) ;
91
- originalStdout . write ( chalk . green ( '\nMessage sent to agent. Resuming output...\n\n' ) ) ;
99
+ originalStdout . write (
100
+ chalk . green ( '\nMessage sent to agent. Resuming output...\n\n' ) ,
101
+ ) ;
92
102
} else {
93
- originalStdout . write ( chalk . yellow ( '\nEmpty message not sent. Resuming output...\n\n' ) ) ;
103
+ originalStdout . write (
104
+ chalk . yellow ( '\nEmpty message not sent. Resuming output...\n\n' ) ,
105
+ ) ;
94
106
}
95
107
} catch ( error ) {
96
- originalStdout . write ( chalk . red ( `\nError sending message: ${ error } \n\n` ) ) ;
108
+ originalStdout . write (
109
+ chalk . red ( `\nError sending message: ${ error } \n\n` ) ,
110
+ ) ;
97
111
} finally {
98
112
// Close input readline interface
99
113
inputRl . close ( ) ;
100
-
114
+
101
115
// Resume output
102
116
interceptor . resume ( ) ;
103
117
}
104
118
}
105
119
} ) ;
106
-
120
+
107
121
// Return a cleanup function
108
122
return ( ) => {
109
123
// Restore original stdout
110
- // @ts -expect-error - This is a hack to restore stdout
111
124
process . stdout = originalStdout ;
112
-
125
+
113
126
// Disable raw mode
114
127
if ( process . stdin . isTTY ) {
115
128
process . stdin . setRawMode ( false ) ;
116
129
}
117
130
} ;
118
- } ;
131
+ } ;
0 commit comments