@@ -35,10 +35,6 @@ if (process.env.EXPERIMENTAL_HF_MCP_SERVER) {
3535 } ) ;
3636}
3737
38- let SAMPLE_INPUT = process . env . USE_SAMPLE_INPUT
39- ? `generate a haiku about Hugging Face and save it to a file name hf.txt on my Desktop`
40- : undefined ;
41-
4238async function main ( ) {
4339 if ( ! process . env . HF_TOKEN ) {
4440 console . error ( `a valid HF_TOKEN must be present in the env` ) ;
@@ -53,14 +49,33 @@ async function main() {
5349 } ) ;
5450
5551 const rl = readline . createInterface ( { input : stdin , output : stdout } ) ;
52+ let abortController = new AbortController ( ) ;
53+ let waitingForInput = false ;
54+ async function waitForInput ( ) {
55+ waitingForInput = true ;
56+ const input = await rl . question ( "> " ) ;
57+ waitingForInput = false ;
58+ return input ;
59+ }
5660 rl . on ( "SIGINT" , async ( ) => {
57- await agent . cleanup ( ) ;
58- stdout . write ( "\n" ) ;
59- rl . close ( ) ;
61+ if ( waitingForInput ) {
62+ // close the whole process
63+ await agent . cleanup ( ) ;
64+ stdout . write ( "\n" ) ;
65+ rl . close ( ) ;
66+ } else {
67+ // otherwise, it means a request is underway
68+ abortController . abort ( ) ;
69+ abortController = new AbortController ( ) ;
70+ stdout . write ( ANSI . GRAY ) ;
71+ stdout . write ( "Ctrl+C a second time to exit" ) ;
72+ stdout . write ( ANSI . RESET ) ;
73+ }
6074 } ) ;
61- process . on ( "uncaughtException" , ( ) => {
75+ process . on ( "uncaughtException" , ( err ) => {
6276 stdout . write ( "\n" ) ;
6377 rl . close ( ) ;
78+ throw err ;
6479 } ) ;
6580
6681 await agent . loadTools ( ) ;
@@ -72,8 +87,8 @@ async function main() {
7287 stdout . write ( "\n" ) ;
7388
7489 while ( true ) {
75- const input = await rl . question ( "> " ) ;
76- for await ( const chunk of agent . run ( input ) ) {
90+ const input = await waitForInput ( ) ;
91+ for await ( const chunk of agent . run ( input , { abortSignal : abortController . signal } ) ) {
7792 if ( "choices" in chunk ) {
7893 const delta = chunk . choices [ 0 ] ?. delta ;
7994 if ( delta . content ) {
0 commit comments