Skip to content

Commit

Permalink
Adjust file retrieval endpoint usage (BloopAI#444)
Browse files Browse the repository at this point in the history
* Adjust file retrieval endpoint

* Add `lang` field to response

* Change path
  • Loading branch information
rsdy authored May 2, 2023
1 parent 8cf9b0d commit 829e544
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion server/bleep/src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub async fn start(app: Application) -> anyhow::Result<()> {
.route("/hoverable", get(hoverable::handle))
.route("/token-info", get(intelligence::handle))
// misc
.route("/file/*ref", get(file::handle))
.route("/file", get(file::handle))
.route("/search", get(semantic::complex_search))
.route(
"/answer",
Expand Down
43 changes: 25 additions & 18 deletions server/bleep/src/webserver/file.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use axum::{
extract::{Path, Query},
Extension, Json,
};
use anyhow::Context;
use axum::{extract::Query, Extension, Json};

use crate::repo::RepoRef;

use super::prelude::*;

#[derive(Debug, serde::Deserialize, Default)]
#[derive(Debug, serde::Deserialize)]
pub(super) struct Params {
pub repo_ref: RepoRef,
pub path: PathBuf,

/// 1-indexed line number at which to start the snippet
pub line_start: Option<isize>,

Expand All @@ -19,33 +22,27 @@ pub(super) struct Params {
#[derive(serde::Serialize)]
pub(super) struct FileResponse {
contents: String,
lang: Option<String>,
}

impl super::ApiResponse for FileResponse {}

pub(super) async fn handle<'a>(
Path(path): Path<String>,
Query(params): Query<Params>,
Extension(indexes): Extension<Arc<Indexes>>,
) -> Result<Json<super::Response<'a>>, Error> {
// Strip leading slash, always present.
let Some((rr, file_path)) = path.split_once(':') else {
return Err(Error::user("invalid path, use repo_ref:path"));
};

let Ok(repo_ref) = rr.parse() else {
println!("{rr}");
return Err(Error::user("invalid repo_ref"));
};

let doc = indexes
.file
.by_path(&repo_ref, file_path)
.by_path(
&params.repo_ref,
params.path.to_str().context("invalid file path")?,
)
.await
.map_err(Error::internal)?;

Ok(json(FileResponse {
contents: split_by_lines(&doc.content, &doc.line_end_indices, &params)?.to_string(),
lang: doc.lang,
}))
}

Expand Down Expand Up @@ -93,6 +90,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: None,
line_end: None
}
Expand All @@ -106,6 +105,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(1),
line_end: None
}
Expand All @@ -119,6 +120,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(2),
line_end: None
}
Expand All @@ -132,6 +135,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(3),
line_end: Some(3),
}
Expand All @@ -145,6 +150,8 @@ cccccc
text,
&indices,
&Params {
repo_ref: "local//repo".into(),
path: "file".into(),
line_start: Some(2),
line_end: Some(3),
}
Expand Down

0 comments on commit 829e544

Please sign in to comment.