add semantic index status, for non authenticated users
This commit is contained in:
parent
a366ad02ce
commit
997f362cc2
2 changed files with 37 additions and 6 deletions
|
@ -16,6 +16,7 @@ use embedding_queue::{EmbeddingQueue, FileToEmbed};
|
|||
use futures::{future, FutureExt, StreamExt};
|
||||
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle};
|
||||
use language::{Anchor, Bias, Buffer, Language, LanguageRegistry};
|
||||
use lazy_static::lazy_static;
|
||||
use ordered_float::OrderedFloat;
|
||||
use parking_lot::Mutex;
|
||||
use parsing::{CodeContextRetriever, Span, SpanDigest, PARSEABLE_ENTIRE_FILE_TYPES};
|
||||
|
@ -24,6 +25,7 @@ use project::{search::PathMatcher, Fs, PathChange, Project, ProjectEntryId, Work
|
|||
use smol::channel;
|
||||
use std::{
|
||||
cmp::Reverse,
|
||||
env,
|
||||
future::Future,
|
||||
mem,
|
||||
ops::Range,
|
||||
|
@ -38,6 +40,10 @@ const SEMANTIC_INDEX_VERSION: usize = 11;
|
|||
const BACKGROUND_INDEXING_DELAY: Duration = Duration::from_secs(5 * 60);
|
||||
const EMBEDDING_QUEUE_FLUSH_TIMEOUT: Duration = Duration::from_millis(250);
|
||||
|
||||
lazy_static! {
|
||||
static ref OPENAI_API_KEY: Option<String> = env::var("OPENAI_API_KEY").ok();
|
||||
}
|
||||
|
||||
pub fn init(
|
||||
fs: Arc<dyn Fs>,
|
||||
http_client: Arc<dyn HttpClient>,
|
||||
|
@ -100,6 +106,7 @@ pub fn init(
|
|||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum SemanticIndexStatus {
|
||||
NotAuthenticated,
|
||||
NotIndexed,
|
||||
Indexed,
|
||||
Indexing {
|
||||
|
@ -274,7 +281,15 @@ impl SemanticIndex {
|
|||
settings::get::<SemanticIndexSettings>(cx).enabled
|
||||
}
|
||||
|
||||
pub fn has_api_key(&self) -> bool {
|
||||
OPENAI_API_KEY.as_ref().is_some()
|
||||
}
|
||||
|
||||
pub fn status(&self, project: &ModelHandle<Project>) -> SemanticIndexStatus {
|
||||
if !self.has_api_key() {
|
||||
return SemanticIndexStatus::NotAuthenticated;
|
||||
}
|
||||
|
||||
if let Some(project_state) = self.projects.get(&project.downgrade()) {
|
||||
if project_state
|
||||
.worktrees
|
||||
|
@ -694,12 +709,12 @@ impl SemanticIndex {
|
|||
let embedding_provider = self.embedding_provider.clone();
|
||||
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
index.await?;
|
||||
let query = embedding_provider
|
||||
.embed_batch(vec![query])
|
||||
.await?
|
||||
.pop()
|
||||
.ok_or_else(|| anyhow!("could not embed query"))?;
|
||||
index.await?;
|
||||
|
||||
let search_start = Instant::now();
|
||||
let modified_buffer_results = this.update(&mut cx, |this, cx| {
|
||||
|
@ -965,6 +980,10 @@ impl SemanticIndex {
|
|||
project: ModelHandle<Project>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Task<Result<()>> {
|
||||
if !self.has_api_key() {
|
||||
return Task::ready(Err(anyhow!("no open ai key present")));
|
||||
}
|
||||
|
||||
if !self.projects.contains_key(&project.downgrade()) {
|
||||
let subscription = cx.subscribe(&project, |this, project, event, cx| match event {
|
||||
project::Event::WorktreeAdded | project::Event::WorktreeRemoved(_) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue