1+ mod handler;
2+
13use futures:: StreamExt ;
24use node_bridge:: { bindings:: AbortSignal , prelude:: * } ;
35use serde_json:: json;
@@ -12,6 +14,8 @@ use crate::{
1214 request:: stream:: { make_stream_request, StreamResponseState } ,
1315} ;
1416
17+ use self :: handler:: ProjectHandler ;
18+
1519const STEP_MESSAGE : & str = "cursor-step" ;
1620const CREATE_MESSAGE : & str = "cursor-create" ;
1721const APPEND_MESSAGE : & str = "cursor-append" ;
@@ -35,7 +39,7 @@ impl Task {
3539}
3640
3741#[ wasm_bindgen( js_name = generateProject) ]
38- pub async fn generate_project ( prompt : & str ) -> Result < JsValue , JsValue > {
42+ pub async fn generate_project ( prompt : & str , handler : ProjectHandler ) -> Result < JsValue , JsValue > {
3943 let prompt = prompt. to_owned ( ) ;
4044 Ok ( get_extension_context ( )
4145 . with_progress (
@@ -53,6 +57,7 @@ pub async fn generate_project(prompt: &str) -> Result<JsValue, JsValue> {
5357 . into( ) ;
5458 let mut data_stream = state. data_stream( ) ;
5559 let mut current_task = None ;
60+ let mut file_writer = None ;
5661 while let Some ( data) = data_stream. next( ) . await {
5762 #[ cfg( debug_assertions) ]
5863 console:: log_str( & data) ;
@@ -65,30 +70,39 @@ pub async fn generate_project(prompt: &str) -> Result<JsValue, JsValue> {
6570 current_task = Some ( Task :: Step ( task. to_owned( ) ) ) ;
6671 } else if data. starts_with( CREATE_MESSAGE ) {
6772 let task = data[ CREATE_MESSAGE . len( ) + 1 ..] . trim( ) ;
68- current_task = Some ( Task :: Create ( task. to_owned( ) ) ) ;
73+ current_task = Some ( Task :: Create ( format!( "Creating {}" , task) ) ) ;
74+
75+ // The title of the "create" message is a file path,
76+ // which requires creating a file based on the path.
77+ handler. create_file_recursive( task) . await ;
6978 } else if data. starts_with( APPEND_MESSAGE ) {
7079 let task = data[ APPEND_MESSAGE . len( ) + 1 ..] . trim( ) ;
71- current_task = Some ( Task :: Append ( task. to_owned( ) ) ) ;
80+ current_task =
81+ Some ( Task :: Append ( format!( "Appending contents to {}" , task) ) ) ;
82+
83+ file_writer = handler. make_file_writer( task) ;
7284 } else if data. starts_with( END_MESSAGE ) {
7385 current_task = None ;
86+ file_writer. as_ref( ) . map( |w| w. end( ) ) ;
87+ file_writer = None ;
7488 } else if data. starts_with( FINISHED_MESSAGE ) {
89+ file_writer. as_ref( ) . map( |w| w. end( ) ) ;
7590 break ;
91+ } else {
92+ match & current_task {
93+ Some ( Task :: Append ( _) ) => {
94+ if let Some ( writer) = file_writer. as_ref( ) {
95+ writer. write( & data) ;
96+ }
97+ }
98+ _ => { }
99+ }
76100 }
77101
78102 // The message sent by the report will automatically disappear after a short period of time.
79103 // In order to keep the text displayed on the dialog box, report the title every time data is returned.
80104 if current_task. is_some( ) {
81105 progress. report( current_task. as_ref( ) . unwrap( ) . title( ) ) ;
82- continue ;
83- }
84- match & current_task {
85- Some ( Task :: Create ( _) ) => {
86- todo!( )
87- }
88- Some ( Task :: Append ( _) ) => {
89- todo!( )
90- }
91- _ => { }
92106 }
93107 }
94108 drop( data_stream) ;
0 commit comments