This introduces semantic indexing in Zed based on chunking text from files in the developer's workspace and creating vector embeddings using an embedding model. As part of this, we've created an embeddings provider trait that allows us to work with OpenAI, a local Ollama model, or a Zed hosted embedding. The semantic index is built by breaking down text for known (programming) languages into manageable chunks that are smaller than the max token size. Each chunk is then fed to a language model to create a high dimensional vector which is then normalized to a unit vector to allow fast comparison with other vectors with a simple dot product. Alongside the vector, we store the path of the file and the range within the document where the vector was sourced from. Zed will soon grok contextual similarity across different text snippets, allowing for natural language search beyond keyword matching. This is being put together both for human-based search as well as providing results to Large Language Models to allow them to refine how they help developers. Remaining todo: * [x] Change `provider` to `model` within the zed hosted embeddings database (as its currently a combo of the provider and the model in one name) Release Notes: - N/A --------- Co-authored-by: Nathan Sobo <nathan@zed.dev> Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Conrad Irwin <conrad@zed.dev> Co-authored-by: Marshall Bowers <elliott.codes@gmail.com> Co-authored-by: Antonio <antonio@zed.dev>
188 lines
5.5 KiB
Rust
188 lines
5.5 KiB
Rust
mod buffer_tests;
|
|
mod channel_tests;
|
|
mod contributor_tests;
|
|
mod db_tests;
|
|
mod embedding_tests;
|
|
mod extension_tests;
|
|
mod feature_flag_tests;
|
|
mod message_tests;
|
|
|
|
use super::*;
|
|
use gpui::BackgroundExecutor;
|
|
use parking_lot::Mutex;
|
|
use sea_orm::ConnectionTrait;
|
|
use sqlx::migrate::MigrateDatabase;
|
|
use std::sync::{
|
|
atomic::{AtomicI32, AtomicU32, Ordering::SeqCst},
|
|
Arc,
|
|
};
|
|
|
|
pub struct TestDb {
|
|
pub db: Option<Arc<Database>>,
|
|
pub connection: Option<sqlx::AnyConnection>,
|
|
}
|
|
|
|
impl TestDb {
|
|
pub fn sqlite(background: BackgroundExecutor) -> Self {
|
|
let url = "sqlite::memory:";
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
.enable_io()
|
|
.enable_time()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let mut db = runtime.block_on(async {
|
|
let mut options = ConnectOptions::new(url);
|
|
options.max_connections(5);
|
|
let mut db = Database::new(options, Executor::Deterministic(background))
|
|
.await
|
|
.unwrap();
|
|
let sql = include_str!(concat!(
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
"/migrations.sqlite/20221109000000_test_schema.sql"
|
|
));
|
|
db.pool
|
|
.execute(sea_orm::Statement::from_string(
|
|
db.pool.get_database_backend(),
|
|
sql,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
db.initialize_notification_kinds().await.unwrap();
|
|
db
|
|
});
|
|
|
|
db.runtime = Some(runtime);
|
|
|
|
Self {
|
|
db: Some(Arc::new(db)),
|
|
connection: None,
|
|
}
|
|
}
|
|
|
|
pub fn postgres(background: BackgroundExecutor) -> Self {
|
|
static LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
let _guard = LOCK.lock();
|
|
let mut rng = StdRng::from_entropy();
|
|
let url = format!(
|
|
"postgres://postgres@localhost/zed-test-{}",
|
|
rng.gen::<u128>()
|
|
);
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
.enable_io()
|
|
.enable_time()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let mut db = runtime.block_on(async {
|
|
sqlx::Postgres::create_database(&url)
|
|
.await
|
|
.expect("failed to create test db");
|
|
let mut options = ConnectOptions::new(url);
|
|
options
|
|
.max_connections(5)
|
|
.idle_timeout(Duration::from_secs(0));
|
|
let mut db = Database::new(options, Executor::Deterministic(background))
|
|
.await
|
|
.unwrap();
|
|
let migrations_path = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations");
|
|
db.migrate(Path::new(migrations_path), false).await.unwrap();
|
|
db.initialize_notification_kinds().await.unwrap();
|
|
db
|
|
});
|
|
|
|
db.runtime = Some(runtime);
|
|
|
|
Self {
|
|
db: Some(Arc::new(db)),
|
|
connection: None,
|
|
}
|
|
}
|
|
|
|
pub fn db(&self) -> &Arc<Database> {
|
|
self.db.as_ref().unwrap()
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! test_both_dbs {
|
|
($test_name:ident, $postgres_test_name:ident, $sqlite_test_name:ident) => {
|
|
#[gpui::test]
|
|
async fn $postgres_test_name(cx: &mut gpui::TestAppContext) {
|
|
let test_db = $crate::db::TestDb::postgres(cx.executor().clone());
|
|
$test_name(test_db.db()).await;
|
|
}
|
|
|
|
#[gpui::test]
|
|
async fn $sqlite_test_name(cx: &mut gpui::TestAppContext) {
|
|
let test_db = $crate::db::TestDb::sqlite(cx.executor().clone());
|
|
$test_name(test_db.db()).await;
|
|
}
|
|
};
|
|
}
|
|
|
|
impl Drop for TestDb {
|
|
fn drop(&mut self) {
|
|
let db = self.db.take().unwrap();
|
|
if let sea_orm::DatabaseBackend::Postgres = db.pool.get_database_backend() {
|
|
db.runtime.as_ref().unwrap().block_on(async {
|
|
use util::ResultExt;
|
|
let query = "
|
|
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
|
FROM pg_stat_activity
|
|
WHERE
|
|
pg_stat_activity.datname = current_database() AND
|
|
pid <> pg_backend_pid();
|
|
";
|
|
db.pool
|
|
.execute(sea_orm::Statement::from_string(
|
|
db.pool.get_database_backend(),
|
|
query,
|
|
))
|
|
.await
|
|
.log_err();
|
|
sqlx::Postgres::drop_database(db.options.get_url())
|
|
.await
|
|
.log_err();
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
fn channel_tree(channels: &[(ChannelId, &[ChannelId], &'static str)]) -> Vec<Channel> {
|
|
channels
|
|
.iter()
|
|
.map(|(id, parent_path, name)| Channel {
|
|
id: *id,
|
|
name: name.to_string(),
|
|
visibility: ChannelVisibility::Members,
|
|
parent_path: parent_path.to_vec(),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
static GITHUB_USER_ID: AtomicI32 = AtomicI32::new(5);
|
|
|
|
async fn new_test_user(db: &Arc<Database>, email: &str) -> UserId {
|
|
db.create_user(
|
|
email,
|
|
false,
|
|
NewUserParams {
|
|
github_login: email[0..email.find('@').unwrap()].to_string(),
|
|
github_user_id: GITHUB_USER_ID.fetch_add(1, SeqCst),
|
|
},
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.user_id
|
|
}
|
|
|
|
static TEST_CONNECTION_ID: AtomicU32 = AtomicU32::new(1);
|
|
fn new_test_connection(server: ServerId) -> ConnectionId {
|
|
ConnectionId {
|
|
id: TEST_CONNECTION_ID.fetch_add(1, SeqCst),
|
|
owner_id: server.0 as u32,
|
|
}
|
|
}
|