Skip to content

Commit

Permalink
handle invalid cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Jan 14, 2024
1 parent 98880ab commit 5b24d27
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/http/routers/v1/code_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::sync::RwLock as StdRwLock;

use axum::Extension;
use axum::response::Result;
use futures_util::future::ok;
use hyper::{Body, Response, StatusCode};
use tracing::info;
use ropey::Rope;
use tracing::{error, info};

use crate::call_validation::CodeCompletionPost;
use crate::caps;
Expand Down Expand Up @@ -35,10 +37,29 @@ async fn _lookup_code_completion_scratchpad(
Ok((model_name, sname.clone(), patch.clone(), n_ctx))
}

fn validate_post(code_completion_post: CodeCompletionPost) -> Result<(), ScratchError> {
let pos = code_completion_post.inputs.cursor.clone();
let Some(source) = code_completion_post.inputs.sources.get(&code_completion_post.inputs.cursor.file) else {
return Err(ScratchError::new(StatusCode::BAD_REQUEST, "invalid post".to_string()))
};
let text = Rope::from_str(&*source);
let line_number = pos.line as usize;
if line_number >= text.len_lines() {
return Err(ScratchError::new(StatusCode::BAD_REQUEST, "invalid post".to_string()))
}
let line = text.line(line_number);
let col = pos.character as usize;
if col >= line.len_chars() {
return Err(ScratchError::new(StatusCode::BAD_REQUEST, "invalid post".to_string()))
}
Ok(())
}

pub async fn handle_v1_code_completion(
global_context: SharedGlobalContext,
code_completion_post: &mut CodeCompletionPost,
) -> Result<Response<Body>, ScratchError> {
validate_post(code_completion_post.clone())?;
let caps = crate::global_context::try_load_caps_quickly_if_not_present(global_context.clone()).await?;
let (model_name, scratchpad_name, scratchpad_patch, n_ctx) = _lookup_code_completion_scratchpad(
caps.clone(),
Expand Down

0 comments on commit 5b24d27

Please sign in to comment.