|
| 1 | +use futures::StreamExt; |
| 2 | +use node_bridge::{bindings::AbortSignal, prelude::*}; |
| 3 | +use serde_json::json; |
| 4 | +use wasm_bindgen::prelude::*; |
| 5 | +use wasm_bindgen_futures::future_to_promise; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + bindings::{ |
| 9 | + progress::Progress, progress_location::ProgressLocation, progress_options::ProgressOptions, |
| 10 | + }, |
| 11 | + context::get_extension_context, |
| 12 | + request::stream::{make_stream_request, StreamResponseState}, |
| 13 | +}; |
| 14 | + |
| 15 | +const STEP_MESSAGE: &str = "cursor-step"; |
| 16 | +const CREATE_MESSAGE: &str = "cursor-create"; |
| 17 | +const APPEND_MESSAGE: &str = "cursor-append"; |
| 18 | +const END_MESSAGE: &str = "cursor-end"; |
| 19 | +const FINISHED_MESSAGE: &str = "cursor-finished"; |
| 20 | + |
| 21 | +enum Task { |
| 22 | + Step(String), |
| 23 | + Create(String), |
| 24 | + Append(String), |
| 25 | +} |
| 26 | + |
| 27 | +#[wasm_bindgen(js_name = generateProject)] |
| 28 | +pub async fn generate_project(prompt: &str) -> Result<JsValue, JsValue> { |
| 29 | + let prompt = prompt.to_owned(); |
| 30 | + Ok(get_extension_context() |
| 31 | + .with_progress( |
| 32 | + ProgressOptions { |
| 33 | + location: ProgressLocation::Notification, |
| 34 | + title: Some("Generating project...".to_owned()), |
| 35 | + cancellable: true, |
| 36 | + }, |
| 37 | + closure_once!(|progress: Progress, abort_signal: AbortSignal| { |
| 38 | + future_to_promise(async move { |
| 39 | + let mut state: StreamResponseState = |
| 40 | + make_stream_request("/gen_project", &json!({ "description": prompt })) |
| 41 | + .send() |
| 42 | + .await? |
| 43 | + .into(); |
| 44 | + let mut data_stream = state.data_stream(); |
| 45 | + let mut current_task = None; |
| 46 | + while let Some(data) = data_stream.next().await { |
| 47 | + #[cfg(debug_assertions)] |
| 48 | + console::log_str(&data); |
| 49 | + |
| 50 | + // The start identifier of the task is in the form of: `identifier task`. |
| 51 | + // First, match the prefix of the identifier, |
| 52 | + // and then extract the specific task following it. |
| 53 | + if data.starts_with(STEP_MESSAGE) { |
| 54 | + let task = data[STEP_MESSAGE.len() + 1..].trim(); |
| 55 | + current_task = Some(Task::Step(task.to_owned())); |
| 56 | + } else if data.starts_with(CREATE_MESSAGE) { |
| 57 | + let task = data[CREATE_MESSAGE.len() + 1..].trim(); |
| 58 | + current_task = Some(Task::Create(task.to_owned())); |
| 59 | + } else if data.starts_with(APPEND_MESSAGE) { |
| 60 | + let task = data[APPEND_MESSAGE.len() + 1..].trim(); |
| 61 | + current_task = Some(Task::Append(task.to_owned())); |
| 62 | + } else if data.starts_with(END_MESSAGE) { |
| 63 | + current_task = None; |
| 64 | + } else if data.starts_with(FINISHED_MESSAGE) { |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + // The message sent by the report will automatically disappear after a short period of time. |
| 69 | + // In order to keep the text displayed on the dialog box, report the title every time data is returned. |
| 70 | + match ¤t_task { |
| 71 | + Some(Task::Step(title) | Task::Create(title) | Task::Append(title)) => { |
| 72 | + progress.report(&title); |
| 73 | + } |
| 74 | + _ => {} |
| 75 | + } |
| 76 | + } |
| 77 | + drop(data_stream); |
| 78 | + state.complete().await.map(|_| JsValue::null()) |
| 79 | + }) |
| 80 | + }) |
| 81 | + .into_js_value() |
| 82 | + .into(), |
| 83 | + ) |
| 84 | + .await) |
| 85 | +} |
0 commit comments