diff --git a/server/bleep/src/agent.rs b/server/bleep/src/agent.rs index 4efdfab625..06fdcdac6d 100644 --- a/server/bleep/src/agent.rs +++ b/server/bleep/src/agent.rs @@ -405,7 +405,7 @@ impl Agent { query: &str, ) -> impl Iterator + 'a { let langs = self.last_exchange().query.langs.iter().map(Deref::deref); - let user_id = "1"; + let user_id = self.user.username().expect("didn't have user ID"); let (repos, branches): (Vec<_>, Vec<_>) = sqlx::query! { "SELECT pr.repo_ref, pr.branch @@ -441,12 +441,23 @@ impl Agent { fn store(&mut self) -> impl Future { let sql = Arc::clone(&self.app.sql); - let user_id = "1".to_string(); + let user_id = self + .user + .username() + .context("didn't have user ID") + .map(str::to_owned); let conversation = self.conversation.clone(); async move { - conversation.store(&sql, &user_id).await.expect("failed to store conversation"); + let result = match user_id { + Ok(user_id) => conversation.store(&sql, &user_id).await, + Err(e) => Err(e), + }; + + if let Err(e) = result { + error!("failed to store conversation: {e}"); + } } } } diff --git a/server/bleep/src/background.rs b/server/bleep/src/background.rs index 662e1aaac2..f8838a8d0b 100644 --- a/server/bleep/src/background.rs +++ b/server/bleep/src/background.rs @@ -89,7 +89,7 @@ impl BackgroundExecutor { let tokio_ref = tokio_ref.clone(); let thread_priority = thread_priority::ThreadPriority::Max; - thread::Builder::new() + std::thread::Builder::new() .name("index-worker".to_owned()) .spawn_with_priority(thread_priority, move |_| { let _tokio = tokio_ref.enter(); diff --git a/server/bleep/src/cache.rs b/server/bleep/src/cache.rs index a1f5478edc..ac6511d120 100644 --- a/server/bleep/src/cache.rs +++ b/server/bleep/src/cache.rs @@ -548,7 +548,7 @@ impl<'a> ChunkCache<'a> { let branches_hash = blake3::hash(payload.branches.join("\n").as_ref()).to_string(); match self.cache.entry(id) { - Entry::Occupied(mut existing) => { + scc::hash_map::Entry::Occupied(mut existing) => { let key = existing.key(); trace!(?key, "found; not upserting new"); if existing.get().value != branches_hash { @@ -560,7 +560,7 @@ impl<'a> ChunkCache<'a> { } *existing.get_mut() = branches_hash.into(); } - Entry::Vacant(vacant) => { + scc::hash_map::Entry::Vacant(vacant) => { let key = vacant.key(); trace!(?key, "inserting new"); self.new_sql diff --git a/server/bleep/src/commits.rs b/server/bleep/src/commits.rs index d9222e9b04..27b5d9e279 100644 --- a/server/bleep/src/commits.rs +++ b/server/bleep/src/commits.rs @@ -437,7 +437,7 @@ Assistant: initialisation"#, pub async fn generate_tutorial_questions( db: crate::db::SqlDb, - llm_gateway: Result, + llm_gateway: Result, repo_pool: RepositoryPool, reporef: RepoRef, ) -> Result<()> { diff --git a/server/bleep/src/indexes.rs b/server/bleep/src/indexes.rs index bd2bb04738..2d9648cd69 100644 --- a/server/bleep/src/indexes.rs +++ b/server/bleep/src/indexes.rs @@ -118,11 +118,11 @@ impl Indexes { // we don't support old schemas, and tantivy will hard // error if we try to open a db with a different schema. if config.index_path("repo").as_ref().exists() { - fs::remove_dir_all(config.index_path("repo"))?; + std::fs::remove_dir_all(config.index_path("repo"))?; debug!("removed index repo dir") } if config.index_path("content").as_ref().exists() { - fs::remove_dir_all(config.index_path("content"))?; + std::fs::remove_dir_all(config.index_path("content"))?; debug!("removed index content dir") } Ok(()) diff --git a/server/bleep/src/indexes/schema.rs b/server/bleep/src/indexes/schema.rs index f2f7d025f5..f7f0cd961a 100644 --- a/server/bleep/src/indexes/schema.rs +++ b/server/bleep/src/indexes/schema.rs @@ -69,7 +69,7 @@ pub struct File { impl File { pub fn new() -> Self { - let mut builder = SchemaBuilder::new(); + let mut builder = tantivy::schema::SchemaBuilder::new(); let trigram = TextOptions::default().set_stored().set_indexing_options( TextFieldIndexing::default() .set_tokenizer("default") diff --git a/server/bleep/src/query/parser.rs b/server/bleep/src/query/parser.rs index 8be2b28eb0..4f82efc00a 100644 --- a/server/bleep/src/query/parser.rs +++ b/server/bleep/src/query/parser.rs @@ -356,7 +356,7 @@ impl<'a> Literal<'a> { /// Force this literal into the `Regex` variant. fn make_regex(&mut self) { - *self = match mem::take(self) { + *self = match std::mem::take(self) { Self::Plain(s) | Self::Regex(s) => Self::Regex(s), } } diff --git a/server/bleep/src/repo.rs b/server/bleep/src/repo.rs index 9bdc2e0ea5..2868a816fe 100644 --- a/server/bleep/src/repo.rs +++ b/server/bleep/src/repo.rs @@ -153,7 +153,7 @@ impl FromStr for RepoRef { } impl Display for RepoRef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self.backend() { Backend::Github => write!(f, "github.com/{}", self.name()), Backend::Local => write!(f, "local/{}", self.name()), diff --git a/server/bleep/src/webserver.rs b/server/bleep/src/webserver.rs index f6f898f921..1a324ff999 100644 --- a/server/bleep/src/webserver.rs +++ b/server/bleep/src/webserver.rs @@ -254,7 +254,7 @@ impl Error { self } - fn internal(message: S) -> Self { + fn internal(message: S) -> Self { Error { status: StatusCode::INTERNAL_SERVER_ERROR, body: EndpointError { @@ -264,7 +264,7 @@ impl Error { } } - fn user(message: S) -> Self { + fn user(message: S) -> Self { Error { status: StatusCode::BAD_REQUEST, body: EndpointError { @@ -274,7 +274,7 @@ impl Error { } } - fn not_found(message: S) -> Self { + fn not_found(message: S) -> Self { Error { status: StatusCode::NOT_FOUND, body: EndpointError { @@ -360,3 +360,7 @@ async fn health(State(app): State) { // subsystem checks at this stage app.semantic.health_check().await.unwrap() } + +fn no_user_id() -> Error { + Error::user("didn't have user ID") +} diff --git a/server/bleep/src/webserver/answer.rs b/server/bleep/src/webserver/answer.rs index 451c60ed9e..527406b10d 100644 --- a/server/bleep/src/webserver/answer.rs +++ b/server/bleep/src/webserver/answer.rs @@ -73,7 +73,7 @@ pub(super) async fn answer( info!(?params.q, "handling /answer query"); let query_id = uuid::Uuid::new_v4(); - let user_id = "1"; + let user_id = user.username().ok_or_else(super::no_user_id)?; let mut conversation = match params.conversation_id { Some(conversation_id) => { @@ -173,7 +173,7 @@ impl AgentExecutor { async fn try_execute(&mut self) -> super::Result>> { QueryLog::new(&self.app.sql).insert(&self.params.q).await?; - let username = "1"; + let username = self.user.username().ok_or_else(super::no_user_id)?; let repo_refs = sqlx::query! { "SELECT repo_ref diff --git a/server/bleep/src/webserver/conversation.rs b/server/bleep/src/webserver/conversation.rs index e160a1e7a7..47111d4c61 100644 --- a/server/bleep/src/webserver/conversation.rs +++ b/server/bleep/src/webserver/conversation.rs @@ -206,7 +206,7 @@ pub(in crate::webserver) async fn get( Path((project_id, conversation_id)): Path<(i64, i64)>, State(app): State, ) -> webserver::Result> { - let user_id = "1"; + let user_id = user.username().ok_or_else(super::no_user_id)?; let mut conversation = Conversation::load(&app.sql, user_id, project_id, conversation_id).await?; diff --git a/server/bleep/src/webserver/project.rs b/server/bleep/src/webserver/project.rs index 0294372201..68dc06292b 100644 --- a/server/bleep/src/webserver/project.rs +++ b/server/bleep/src/webserver/project.rs @@ -25,7 +25,7 @@ pub async fn list( app: Extension, user: Extension, ) -> webserver::Result>> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let projects = sqlx::query! { "SELECT p.id, p.name, ( @@ -91,7 +91,7 @@ pub async fn create( user: Extension, Json(params): Json, ) -> webserver::Result { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let project_id = sqlx::query! { "INSERT INTO projects (user_id, name) VALUES (?, ?) RETURNING id", @@ -118,7 +118,7 @@ pub async fn get( user: Extension, Path(id): Path, ) -> webserver::Result> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let row = sqlx::query! { "SELECT name, ( @@ -172,7 +172,7 @@ pub async fn update( Path(id): Path, Json(update): Json, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); sqlx::query! { "UPDATE projects SET name = ? WHERE id = ? AND user_id = ? RETURNING id", @@ -191,7 +191,7 @@ pub async fn delete( user: Extension, Path(id): Path, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?; sqlx::query! { "DELETE FROM projects WHERE id = ? AND user_id = ? RETURNING id", diff --git a/server/bleep/src/webserver/project/doc.rs b/server/bleep/src/webserver/project/doc.rs index c04f9bfc60..31c18936d1 100644 --- a/server/bleep/src/webserver/project/doc.rs +++ b/server/bleep/src/webserver/project/doc.rs @@ -22,7 +22,10 @@ pub async fn list( user: Extension, Path(project_id): Path, ) -> webserver::Result>> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); let docs = sqlx::query_as! { Doc, @@ -54,7 +57,10 @@ pub async fn add( Path(project_id): Path, Json(params): Json, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); sqlx::query! { "SELECT id FROM projects WHERE id = ? AND user_id = ?", @@ -81,7 +87,10 @@ pub async fn delete( user: Extension, Path((project_id, doc_id)): Path<(i64, i64)>, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); sqlx::query! { "DELETE FROM project_docs diff --git a/server/bleep/src/webserver/project/repo.rs b/server/bleep/src/webserver/project/repo.rs index 881312f8c0..9e88b97f7f 100644 --- a/server/bleep/src/webserver/project/repo.rs +++ b/server/bleep/src/webserver/project/repo.rs @@ -21,7 +21,10 @@ pub async fn list( user: Extension, Path(project_id): Path, ) -> webserver::Result>> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); let list = sqlx::query! { "SELECT repo_ref, branch @@ -65,7 +68,10 @@ pub async fn add( Path(project_id): Path, Json(params): Json, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); sqlx::query! { "SELECT id FROM projects WHERE id = ? AND user_id = ?", @@ -102,7 +108,10 @@ pub async fn delete( Path(project_id): Path, Query(Delete { repo_ref }): Query, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); sqlx::query! { "DELETE FROM project_repos @@ -135,7 +144,10 @@ pub async fn put( Path(project_id): Path, Json(params): Json, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user + .username() + .ok_or_else(webserver::no_user_id)? + .to_string(); sqlx::query! { "SELECT id FROM projects WHERE id = ? AND user_id = ?", diff --git a/server/bleep/src/webserver/query.rs b/server/bleep/src/webserver/query.rs index dec4b8ab8a..498e6a80d2 100644 --- a/server/bleep/src/webserver/query.rs +++ b/server/bleep/src/webserver/query.rs @@ -16,5 +16,5 @@ pub(super) async fn handle( .query(&app) .await .map(json) - .map_err(Error::from) + .map_err(super::Error::from) } diff --git a/server/bleep/src/webserver/repos.rs b/server/bleep/src/webserver/repos.rs index bc29fbb992..0e4929f2ce 100644 --- a/server/bleep/src/webserver/repos.rs +++ b/server/bleep/src/webserver/repos.rs @@ -168,7 +168,7 @@ impl Repo { last_update: origin.pushed_at.unwrap(), last_index: None, most_common_lang: None, - branch_filter: BranchFilterConfig::Select(vec![]), + branch_filter: crate::repo::BranchFilterConfig::Select(vec![]), file_filter: Default::default(), branches: vec![], } diff --git a/server/bleep/src/webserver/search.rs b/server/bleep/src/webserver/search.rs index a8d5eb4f2b..1c88cd8e15 100644 --- a/server/bleep/src/webserver/search.rs +++ b/server/bleep/src/webserver/search.rs @@ -20,7 +20,7 @@ pub(super) async fn semantic_code( Ok(q) => semantic::execute::execute(semantic, q, args) .await .map(json) - .map_err(Error::from), + .map_err(super::Error::from), Err(err) => { error!(?err, "Couldn't parse query"); Err(Error::new(ErrorKind::UpstreamService, "error")) diff --git a/server/bleep/src/webserver/studio.rs b/server/bleep/src/webserver/studio.rs index d7abad8acc..d74e56c926 100644 --- a/server/bleep/src/webserver/studio.rs +++ b/server/bleep/src/webserver/studio.rs @@ -167,7 +167,7 @@ pub async fn get( Path((project_id, studio_id)): Path<(i64, i64)>, Query(params): Query, ) -> webserver::Result> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let snapshot_id = match params.snapshot_id { Some(id) => id, @@ -220,7 +220,7 @@ pub async fn patch( Path((project_id, studio_id)): Path<(i64, i64)>, Json(patch): Json, ) -> webserver::Result> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let mut transaction = app.sql.begin().await?; @@ -330,7 +330,7 @@ pub async fn delete( user: Extension, Path((project_id, studio_id)): Path<(i64, i64)>, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); sqlx::query!( "DELETE FROM studios @@ -365,7 +365,7 @@ pub async fn list( user: Extension, Path(project_id): Path, ) -> webserver::Result>> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let studios = sqlx::query!( "SELECT @@ -707,7 +707,7 @@ pub async fn generate( user: Extension, Path((_project_id, studio_id)): Path<(i64, i64)>, ) -> webserver::Result> + Send>>>> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let snapshot_id = latest_snapshot_id(studio_id, &*app.sql, &user_id).await?; @@ -935,7 +935,7 @@ pub async fn diff( user: Extension, Path((_project_id, studio_id)): Path<(i64, i64)>, ) -> webserver::Result> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let snapshot_id = latest_snapshot_id(studio_id, &*app.sql, &user_id).await?; @@ -991,7 +991,7 @@ pub async fn diff( let response = llm_gateway.chat(&messages, None).await?; let diff_chunks = diff::extract(&response)?.collect::>(); - let valid_chunks = stream::iter(diff_chunks) + let valid_chunks = futures::stream::iter(diff_chunks) .map(|mut chunk| { let app = (*app).clone(); let llm_context = llm_context.clone(); @@ -1250,7 +1250,7 @@ pub async fn diff_apply( Path((_project_id, studio_id)): Path<(i64, i64)>, diff: String, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let snapshot_id = latest_snapshot_id(studio_id, &*app.sql, &user_id).await?; @@ -1331,7 +1331,7 @@ pub async fn diff_apply( // Force a re-sync. for repo in dirty_repos { - let _ = webserver::repos::sync( + let _ = crate::webserver::repos::sync( Query(webserver::repos::RepoParams { repo, shallow: false, @@ -1352,7 +1352,7 @@ async fn populate_studio_name( user: Extension, studio_id: i64, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let snapshot_id = latest_snapshot_id(studio_id, &*app.sql, &user_id).await?; let needs_name = sqlx::query! { @@ -1421,7 +1421,7 @@ pub async fn import( ) -> webserver::Result { let mut transaction = app.sql.begin().await?; - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); let thread_id = params.thread_id.to_string(); @@ -1641,7 +1641,7 @@ pub async fn list_snapshots( user: Extension, Path((project_id, studio_id)): Path<(i64, i64)>, ) -> webserver::Result>> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); sqlx::query! { "SELECT ss.id as 'id!', ss.modified_at, ss.context, ss.doc_context, ss.messages @@ -1688,7 +1688,7 @@ pub async fn delete_snapshot( user: Extension, Path((project_id, studio_id, snapshot_id)): Path<(i64, i64, i64)>, ) -> webserver::Result<()> { - let user_id = "1".to_string(); + let user_id = user.username().ok_or_else(super::no_user_id)?.to_string(); sqlx::query! { "DELETE FROM studio_snapshots diff --git a/server/bleep/src/webserver/template.rs b/server/bleep/src/webserver/template.rs index aa9d0a28aa..302fb43551 100644 --- a/server/bleep/src/webserver/template.rs +++ b/server/bleep/src/webserver/template.rs @@ -16,7 +16,10 @@ pub async fn create( user: Extension, params: Json, ) -> webserver::Result { - let user_id = "1"; + let user_id = user + .username() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); let id = sqlx::query!( "INSERT INTO templates (name, content, user_id) VALUES (?, ?, ?)", @@ -44,7 +47,10 @@ pub async fn list( app: Extension, user: Extension, ) -> webserver::Result>> { - let user_id = "1"; + let user_id = user + .username() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); let templates = sqlx::query_as!( Template, @@ -64,7 +70,10 @@ pub async fn get( user: Extension, Path(id): Path, ) -> webserver::Result> { - let user_id = "1"; + let user_id = user + .username() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); let template = sqlx::query_as!( Template, @@ -93,7 +102,10 @@ pub async fn patch( Path(mut id): Path, Json(patch): Json, ) -> webserver::Result { - let user_id = "1"; + let user_id = user + .username() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); let mut transaction = app.sql.begin().await?; @@ -153,7 +165,10 @@ pub async fn delete( user: Extension, Path(id): Path, ) -> webserver::Result<()> { - let user_id = "1"; + let user_id = user + .username() + .ok_or_else(|| super::Error::user("didn't have user ID"))? + .to_string(); sqlx::query!( "DELETE FROM templates WHERE id = ? AND user_id = ? RETURNING id",