From febfa18e4a3a7b07e295b4f8aa159d8c2c56ee60 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Sun, 12 Nov 2023 14:58:15 -0800 Subject: [PATCH] refactor: move code api to tabby/serve (#771) --- .github/workflows/autofix.yml | 3 +++ Cargo.lock | 4 +--- Makefile | 4 +++- crates/tabby-common/Cargo.toml | 3 --- crates/tabby-common/src/api/mod.rs | 1 - crates/tabby-common/src/lib.rs | 1 - crates/tabby/Cargo.toml | 1 + crates/{tabby-common => tabby}/src/api/code.rs | 0 crates/tabby/src/api/mod.rs | 3 +++ crates/tabby/src/main.rs | 1 + crates/tabby/src/serve/completions.rs | 4 +++- crates/tabby/src/serve/completions/prompt.rs | 6 ++---- crates/tabby/src/serve/mod.rs | 12 ++++++------ crates/tabby/src/serve/search.rs | 3 ++- crates/tabby/src/services/code.rs | 3 ++- 15 files changed, 27 insertions(+), 22 deletions(-) delete mode 100644 crates/tabby-common/src/api/mod.rs rename crates/{tabby-common => tabby}/src/api/code.rs (100%) create mode 100644 crates/tabby/src/api/mod.rs diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml index ea401db..3489a7f 100644 --- a/.github/workflows/autofix.yml +++ b/.github/workflows/autofix.yml @@ -48,6 +48,9 @@ jobs: ~/.cargo/registry ~/.cargo/git + - name: Cargo Machete + uses: bnjbvr/cargo-machete@main + - run: bash ./ci/prepare_build_environment.sh - run: make fix diff --git a/Cargo.lock b/Cargo.lock index 5673e24..81653fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4307,6 +4307,7 @@ dependencies = [ "tabby-webserver", "tantivy", "textdistance", + "thiserror", "tokio", "tower-http 0.4.0", "tracing", @@ -4323,7 +4324,6 @@ name = "tabby-common" version = "0.6.0-dev" dependencies = [ "anyhow", - "async-trait", "chrono", "filenamify", "lazy_static", @@ -4332,9 +4332,7 @@ dependencies = [ "serde-jsonlines", "serdeconv", "tantivy", - "thiserror", "tokio", - "utoipa", "uuid 1.4.1", ] diff --git a/Makefile b/Makefile index fe3d920..442ff7a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,9 @@ else endif fix: - cargo +nightly clippy --fix --allow-dirty --allow-staged && cargo +nightly fmt + cargo machete --fix + cargo +nightly fmt + cargo +nightly clippy --fix --allow-dirty --allow-staged fix-ui: cd ee/tabby-ui && yarn format:write && yarn lint:fix diff --git a/crates/tabby-common/Cargo.toml b/crates/tabby-common/Cargo.toml index 238ef9d..ccaac95 100644 --- a/crates/tabby-common/Cargo.toml +++ b/crates/tabby-common/Cargo.toml @@ -15,9 +15,6 @@ tokio = { workspace = true, features = ["rt", "macros"] } uuid = { version = "1.4.1", features = ["v4"] } tantivy.workspace = true anyhow.workspace = true -async-trait.workspace = true -thiserror.workspace = true -utoipa = { workspace = true, features = ["axum_extras", "preserve_order"] } [features] testutils = [] diff --git a/crates/tabby-common/src/api/mod.rs b/crates/tabby-common/src/api/mod.rs deleted file mode 100644 index 9de50d4..0000000 --- a/crates/tabby-common/src/api/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod code; diff --git a/crates/tabby-common/src/lib.rs b/crates/tabby-common/src/lib.rs index cc548cc..2458fef 100644 --- a/crates/tabby-common/src/lib.rs +++ b/crates/tabby-common/src/lib.rs @@ -1,4 +1,3 @@ -pub mod api; pub mod config; pub mod events; pub mod index; diff --git a/crates/tabby/Cargo.toml b/crates/tabby/Cargo.toml index 6f47daf..1d6450a 100644 --- a/crates/tabby/Cargo.toml +++ b/crates/tabby/Cargo.toml @@ -45,6 +45,7 @@ llama-cpp-bindings = { path = "../llama-cpp-bindings" } futures.workspace = true async-trait.workspace = true tabby-webserver = { path = "../../ee/tabby-webserver" } +thiserror.workspace = true [dependencies.uuid] version = "1.3.3" diff --git a/crates/tabby-common/src/api/code.rs b/crates/tabby/src/api/code.rs similarity index 100% rename from crates/tabby-common/src/api/code.rs rename to crates/tabby/src/api/code.rs diff --git a/crates/tabby/src/api/mod.rs b/crates/tabby/src/api/mod.rs new file mode 100644 index 0000000..4c48d8b --- /dev/null +++ b/crates/tabby/src/api/mod.rs @@ -0,0 +1,3 @@ +mod code; + +pub use code::*; diff --git a/crates/tabby/src/main.rs b/crates/tabby/src/main.rs index 15202fe..11f10b2 100644 --- a/crates/tabby/src/main.rs +++ b/crates/tabby/src/main.rs @@ -1,3 +1,4 @@ +mod api; mod download; mod serve; diff --git a/crates/tabby/src/serve/completions.rs b/crates/tabby/src/serve/completions.rs index f630b6d..794b374 100644 --- a/crates/tabby/src/serve/completions.rs +++ b/crates/tabby/src/serve/completions.rs @@ -5,11 +5,13 @@ use std::sync::Arc; use axum::{extract::State, Json}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; -use tabby_common::{api::code::CodeSearch, events, languages::get_language}; +use tabby_common::{events, languages::get_language}; use tabby_inference::{TextGeneration, TextGenerationOptionsBuilder}; use tracing::{debug, instrument}; use utoipa::ToSchema; +use crate::api::CodeSearch; + #[derive(Serialize, Deserialize, ToSchema, Clone, Debug)] #[schema(example=json!({ "language": "python", diff --git a/crates/tabby/src/serve/completions/prompt.rs b/crates/tabby/src/serve/completions/prompt.rs index 742e5bf..4a0e79b 100644 --- a/crates/tabby/src/serve/completions/prompt.rs +++ b/crates/tabby/src/serve/completions/prompt.rs @@ -3,14 +3,12 @@ use std::sync::Arc; use lazy_static::lazy_static; use regex::Regex; use strfmt::strfmt; -use tabby_common::{ - api::code::{CodeSearch, CodeSearchError}, - languages::get_language, -}; +use tabby_common::languages::get_language; use textdistance::Algorithm; use tracing::warn; use super::{Segments, Snippet}; +use crate::api::{CodeSearch, CodeSearchError}; static MAX_SNIPPETS_TO_FETCH: usize = 20; static MAX_SNIPPET_CHARS_IN_PROMPT: usize = 768; diff --git a/crates/tabby/src/serve/mod.rs b/crates/tabby/src/serve/mod.rs index 3cfbff4..6e8c56b 100644 --- a/crates/tabby/src/serve/mod.rs +++ b/crates/tabby/src/serve/mod.rs @@ -15,11 +15,7 @@ use std::{ use axum::{routing, Router, Server}; use axum_tracing_opentelemetry::opentelemetry_tracing_layer; use clap::Args; -use tabby_common::{ - api::code::{Hit, HitDocument, SearchResponse}, - config::Config, - usage, -}; +use tabby_common::{config::Config, usage}; use tabby_download::download_model; use tabby_webserver::attach_webserver; use tokio::time::sleep; @@ -32,7 +28,11 @@ use self::{ engine::{create_engine, EngineInfo}, health::HealthState, }; -use crate::{fatal, services::chat::ChatService}; +use crate::{ + api::{Hit, HitDocument, SearchResponse}, + fatal, + services::chat::ChatService, +}; #[derive(OpenApi)] #[openapi( diff --git a/crates/tabby/src/serve/search.rs b/crates/tabby/src/serve/search.rs index 99edade..5bcea1a 100644 --- a/crates/tabby/src/serve/search.rs +++ b/crates/tabby/src/serve/search.rs @@ -7,10 +7,11 @@ use axum::{ }; use hyper::StatusCode; use serde::Deserialize; -use tabby_common::api::code::{CodeSearch, CodeSearchError, SearchResponse}; use tracing::{instrument, warn}; use utoipa::IntoParams; +use crate::api::{CodeSearch, CodeSearchError, SearchResponse}; + #[derive(Deserialize, IntoParams)] pub struct SearchQuery { #[param(default = "get")] diff --git a/crates/tabby/src/services/code.rs b/crates/tabby/src/services/code.rs index 7ca3188..71c1ea6 100644 --- a/crates/tabby/src/services/code.rs +++ b/crates/tabby/src/services/code.rs @@ -3,7 +3,6 @@ use std::{sync::Arc, time::Duration}; use anyhow::Result; use async_trait::async_trait; use tabby_common::{ - api::code::{CodeSearch, CodeSearchError, Hit, HitDocument, SearchResponse}, index::{self, register_tokenizers, CodeSearchSchema}, path, }; @@ -17,6 +16,8 @@ use tantivy::{ use tokio::{sync::Mutex, time::sleep}; use tracing::{debug, log::info}; +use crate::api::{CodeSearch, CodeSearchError, Hit, HitDocument, SearchResponse}; + struct CodeSearchImpl { reader: IndexReader, query_parser: QueryParser,