@@ -4,7 +4,10 @@ import { Writable } from 'stream';
4
4
5
5
import chalk from 'chalk' ;
6
6
7
- import { userMessages } from '../tools/interaction/userMessage.js' ;
7
+ import {
8
+ userMessages ,
9
+ cancelJobFlag ,
10
+ } from '../tools/interaction/userMessage.js' ;
8
11
9
12
// Custom output stream to intercept console output
10
13
class OutputInterceptor extends Writable {
@@ -69,6 +72,75 @@ export const initInteractiveInput = () => {
69
72
process . exit ( 0 ) ;
70
73
}
71
74
75
+ // Check for Ctrl+X to cancel job
76
+ if ( key . ctrl && key . name === 'x' ) {
77
+ // Pause output
78
+ interceptor . pause ( ) ;
79
+
80
+ // Create a readline interface for input
81
+ const inputRl = createInterface ( {
82
+ input : process . stdin ,
83
+ output : originalStdout ,
84
+ } ) ;
85
+
86
+ try {
87
+ // Reset cursor position and clear line
88
+ originalStdout . write ( '\r\n' ) ;
89
+ originalStdout . write (
90
+ chalk . yellow (
91
+ 'Are you sure you want to cancel the current job? (y/n):\n' ,
92
+ ) + '> ' ,
93
+ ) ;
94
+
95
+ // Get user confirmation
96
+ const confirmation = await inputRl . question ( '' ) ;
97
+
98
+ if ( confirmation . trim ( ) . toLowerCase ( ) === 'y' ) {
99
+ // Set cancel flag to true
100
+ cancelJobFlag . value = true ;
101
+
102
+ // Create a readline interface for new instructions
103
+ originalStdout . write (
104
+ chalk . green ( '\nJob cancelled. Enter new instructions:\n' ) + '> ' ,
105
+ ) ;
106
+
107
+ // Get new instructions
108
+ const newInstructions = await inputRl . question ( '' ) ;
109
+
110
+ // Add message to queue if not empty
111
+ if ( newInstructions . trim ( ) ) {
112
+ userMessages . push ( `[CANCEL JOB] ${ newInstructions } ` ) ;
113
+ originalStdout . write (
114
+ chalk . green (
115
+ '\nNew instructions sent. Resuming with new task...\n\n' ,
116
+ ) ,
117
+ ) ;
118
+ } else {
119
+ originalStdout . write (
120
+ chalk . yellow (
121
+ '\nNo new instructions provided. Job will still be cancelled...\n\n' ,
122
+ ) ,
123
+ ) ;
124
+ userMessages . push (
125
+ '[CANCEL JOB] Please stop the current task and wait for new instructions.' ,
126
+ ) ;
127
+ }
128
+ } else {
129
+ originalStdout . write (
130
+ chalk . green ( '\nCancellation aborted. Resuming output...\n\n' ) ,
131
+ ) ;
132
+ }
133
+ } catch ( error ) {
134
+ originalStdout . write ( chalk . red ( `\nError cancelling job: ${ error } \n\n` ) ) ;
135
+ } finally {
136
+ // Close input readline interface
137
+ inputRl . close ( ) ;
138
+
139
+ // Resume output
140
+ interceptor . resume ( ) ;
141
+ }
142
+ }
143
+
72
144
// Check for Ctrl+M to enter message mode
73
145
if ( key . ctrl && key . name === 'm' ) {
74
146
// Pause output
0 commit comments