Skip to content

Commit

Permalink
WIP: pass filePath and workspaceDir from JS to Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
ktiays committed Mar 22, 2023
1 parent 089a42d commit 688a752
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/cursor-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1"
futures = "0.3"
chrono = "0.4"
chrono = "0.4"
js-sys = "0.3"
36 changes: 18 additions & 18 deletions crates/cursor-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const IGENERATE_INPUT: &'static str = r#"
interface IGenerateInput {
get prompt(): string;
get documentText(): string;
get filePath(): string;
get workspaceDirectory(): string | null;
get selectionRange(): ISelectionRange;
get resultStream(): IResultStream;
}
Expand Down Expand Up @@ -66,6 +68,12 @@ extern "C" {
#[wasm_bindgen(method, getter, structural, js_name = "documentText")]
pub fn document_text(this: &GenerateInput) -> String;

#[wasm_bindgen(method, getter, structural, js_name = "filePath")]
pub fn file_path(this: &GenerateInput) -> String;

#[wasm_bindgen(method, getter, structural, js_name = "workspaceDirectory")]
pub fn workspace_directory(this: &GenerateInput) -> Option<String>;

#[wasm_bindgen(method, getter, structural, js_name = "selectionRange")]
pub fn selection_range(this: &GenerateInput) -> SelectionRange;

Expand Down Expand Up @@ -93,7 +101,14 @@ fn split_code_into_blocks(code: &str) -> Vec<String> {

#[wasm_bindgen(js_name = generateCode)]
pub async fn generate_code(input: &GenerateInput) -> Result<(), JsValue> {
let file_path = "file_path";
let file_path = input.file_path();
let file_dir = file_path
.split("/")
.take(file_path.split("/").count() - 1)
.collect::<Vec<&str>>()
.join("/");
node_bridge::bindings::console::log_str(&format!("file_dir: {}", file_dir));
let workspace_directory = input.workspace_directory();
let selection = input.selection_range();
let document_text_utf16: Vec<u16> = input.document_text().encode_utf16().collect();

Expand All @@ -118,23 +133,15 @@ pub async fn generate_code(input: &GenerateInput) -> Result<(), JsValue> {

let user_request = UserRequest::new(
prompt,
".".to_owned(),
file_dir,
file_path.to_owned(),
input.document_text(),
split_code_into_blocks(&preceding_code),
split_code_into_blocks(&following_code),
selection_text,
vec![],
vec![],
message_type,
0,
);
let mut request_body = RequestBody::new(
user_request,
vec![],
"copilot".to_owned(),
Some(".".to_owned()),
);
let mut request_body = RequestBody::new(user_request, vec![], workspace_directory);

let result_stream = input.result_stream();

Expand All @@ -157,19 +164,12 @@ pub async fn generate_code(input: &GenerateInput) -> Result<(), JsValue> {
if conversation_id.is_none() {
conversation_id = Some(node_bridge::bindings::uuid::uuid_v4());
}
let timestamp = chrono::Utc::now().timestamp_millis();
let bot_message = BotMessage::new(
"bot".to_owned(),
timestamp,
conversation_id.clone().unwrap(),
message_type,
previous_message.clone(),
last_token.clone(),
false,
file_path.to_owned(),
true,
0,
true,
);
request_body.bot_messages = vec![bot_message];
}
Expand Down
34 changes: 14 additions & 20 deletions crates/cursor-core/src/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ impl RequestBody {
pub fn new(
user_request: UserRequest,
bot_messages: Vec<BotMessage>,
context_type: String,
root_path: Option<String>,
) -> Self {
Self {
user_request,
bot_messages,
user_messages: vec![],
context_type,
context_type: "copilot".to_owned(),
root_path,
}
}
}

fn random() -> i32 {
js_sys::Math::floor(js_sys::Math::random() * 1000.0) as i32
}

#[derive(Debug, Serialize, Clone)]
pub struct UserRequest {
pub message: String,
Expand Down Expand Up @@ -89,10 +92,7 @@ impl UserRequest {
preceding_code: Vec<String>,
suffix_code: Vec<String>,
current_selection: Option<String>,
copilot_code_blocks: Vec<String>,
custom_code_blocks: Vec<String>,
message_type: MessageType,
max_original_line: i32,
) -> Self {
Self {
message,
Expand All @@ -102,11 +102,11 @@ impl UserRequest {
preceding_code,
suffix_code,
current_selection,
copilot_code_blocks,
custom_code_blocks,
copilot_code_blocks: vec![],
custom_code_blocks: vec![],
code_block_identifiers: vec![],
message_type,
max_original_line,
max_original_line: random(),
}
}
}
Expand Down Expand Up @@ -149,30 +149,24 @@ pub struct BotMessage {

impl BotMessage {
pub fn new(
sender: String,
send_at: i64,
conversation_id: String,
message_type: MessageType,
message: String,
last_token: String,
finished: bool,
current_file: String,
interrupted: bool,
max_original_line: i32,
hit_token_limit: bool,
) -> Self {
Self {
sender,
send_at,
sender: "bot".to_owned(),
send_at: chrono::Utc::now().timestamp_millis(),
conversation_id,
message_type,
message,
last_token,
finished,
finished: false,
current_file,
interrupted,
max_original_line,
hit_token_limit,
interrupted: true,
max_original_line: random(),
hit_token_limit: true,
}
}
}
26 changes: 25 additions & 1 deletion src/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ import {
class GenerateInput implements IGenerateInput {
private _prompt: string;
private _documentText: string;
private _filePath: string;
private _workspaceDirectory: string | null;
private _selectionRange: ISelectionRange;
private _resultStream: IResultStream;

constructor(
prompt: string,
documentText: string,
filePath: string,
workspaceDirectory: string | null,
selectionRange: ISelectionRange,
resultStream: IResultStream
) {
this._prompt = prompt;
this._documentText = documentText;
this._filePath = filePath;
this._workspaceDirectory = workspaceDirectory;
this._selectionRange = selectionRange;
this._resultStream = resultStream;
}
Expand All @@ -34,6 +40,14 @@ class GenerateInput implements IGenerateInput {
return this._documentText;
}

get filePath(): string {
return this._filePath;
}

get workspaceDirectory(): string | null {
return this._workspaceDirectory;
}

get selectionRange(): ISelectionRange {
return this._selectionRange;
}
Expand Down Expand Up @@ -68,6 +82,9 @@ export async function generateCode(
resultStream: ResultStream<String>
): Promise<void> {
const document = editor.document;
const filePath = document.uri.fsPath;
const workspaceDirectory =
vscode.workspace.workspaceFolders?.[0].uri.fsPath ?? null;
const selection = editor.selection;
const text = document.getText();
const selectionStartOffset = document.offsetAt(selection.start);
Expand All @@ -78,6 +95,13 @@ export async function generateCode(
);

await rustGenerateCode(
new GenerateInput(prompt, text, selectionRange, resultStream)
new GenerateInput(
prompt,
text,
filePath,
workspaceDirectory,
selectionRange,
resultStream
)
);
}

0 comments on commit 688a752

Please sign in to comment.