assistant: Allow /docs to perform JIT indexing when run (#14768)

This PR updates the `/docs` slash command with the ability to
just-in-time index a package when there are not yet any results in the
index.

When running a `/docs` slash command, we fist check to see if there are
any results in the index that would match the search.

If there are, we go ahead and return them, as we do today.

However, if there are not yet any results we kick off an indexing task
as part of the command execution to fetch the results.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-18 17:01:48 -04:00 committed by GitHub
parent 7c63f26aa9
commit ad3055076d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 84 additions and 3 deletions

View file

@ -112,6 +112,16 @@ impl IndexedDocsStore {
.await
}
/// Returns whether any entries exist with the given prefix.
pub async fn any_with_prefix(&self, prefix: String) -> Result<bool> {
self.database_future
.clone()
.await
.map_err(|err| anyhow!(err))?
.any_with_prefix(prefix)
.await
}
pub fn index(
self: Arc<Self>,
package: PackageName,
@ -288,6 +298,20 @@ impl IndexedDocsDatabase {
})
}
/// Returns whether any entries exist with the given prefix.
pub fn any_with_prefix(&self, prefix: String) -> Task<Result<bool>> {
let env = self.env.clone();
let entries = self.entries;
self.executor.spawn(async move {
let txn = env.read_txn()?;
let any = entries
.iter(&txn)?
.any(|entry| entry.map_or(false, |(key, _value)| key.starts_with(&prefix)));
Ok(any)
})
}
pub fn insert(&self, key: String, docs: String) -> Task<Result<()>> {
let env = self.env.clone();
let entries = self.entries;