use std::time::Duration;
use gpui_kit::{App, AppContext as _, Global, Task};
use subspace_agent_tools::ToolEnvironment;
use subspace_product::ProductContext;
use subspace_search::{Policy, Service};
use crate::product::ProductFiles;
use crate::registry::{BoardRegistration, RegistrationSets};
const FIRST_SWEEP_DELAY: Duration = Duration::from_secs(600);
pub type RerankerFactory = fn(std::path::PathBuf) -> std::sync::Arc<dyn subspace_search::Reranker>;
pub(crate) fn initialize(
environment: &mut ToolEnvironment,
context: &ProductContext,
boards: &[BoardRegistration],
files: &ProductFiles,
reranker: Option<RerankerFactory>,
) -> Result<(), String> {
let allowed: Vec<String> = boards
.iter()
.filter(|board| board.searchable)
.map(|board| board.id.to_owned())
.collect();
if allowed.is_empty() {
return Ok(());
}
let root = files
.state
.parent()
.ok_or("Cannot locate private search storage.")?
.join("search");
let service = Service::open(
root.clone(),
context.product.binary_name.as_str().to_owned(),
allowed,
)?;
offer_defaults(&service, boards);
if let Some(create) = reranker {
service.set_reranker(create(root.join("models")));
}
environment.search = Some(service);
Ok(())
}
pub(crate) fn initialize_desktop(
environment: &mut ToolEnvironment,
context: &ProductContext,
boards: &RegistrationSets,
files: &ProductFiles,
reranker: Option<RerankerFactory>,
) -> Result<(), String> {
initialize(environment, context, &boards.included, files, reranker)?;
set_active(environment.search.as_ref(), &boards.active)
}
pub(crate) fn set_active(
service: Option<&Service>,
boards: &[BoardRegistration],
) -> Result<(), String> {
if let Some(service) = service {
service.set_active_boards(
boards
.iter()
.filter(|board| board.searchable)
.map(|board| board.id.to_owned())
.collect(),
)?;
}
Ok(())
}
fn offer_defaults(service: &Service, boards: &[BoardRegistration]) {
let stored = Policy {
enabled: true,
..Policy::default()
};
for board in boards
.iter()
.filter(|board| board.searchable && board.settings.store_by_default)
{
let _ = service.offer_default(board.id, stored.clone());
}
}
struct Maintenance {
_task: Task<()>,
_provisioning: Task<()>,
}
impl Global for Maintenance {}
pub(crate) fn start(cx: &mut App) {
let Some(service) = subspace_dashboard::full_text_service(cx) else {
return;
};
let model_service = service.clone();
let provisioning = cx.background_executor().spawn(async move {
provision_missing_model(&model_service);
});
let task = cx.spawn(async move |cx| {
loop {
cx.background_executor().timer(FIRST_SWEEP_DELAY).await;
let service = service.clone();
let _ = cx.background_spawn(async move { service.sweep() }).await;
}
});
cx.set_global(Maintenance {
_task: task,
_provisioning: provisioning,
});
}
fn provision_missing_model(service: &Service) {
if service
.reranker_status()
.is_some_and(|status| !status.installed)
&& let Err(error) = service.install_reranker()
{
subspace_core::log::Logger::new("search").record(
subspace_core::log::Level::Warn,
error,
&[],
);
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use gpui_kit::{App, Window};
use subspace_agent_tools::{AgentTool, ToolEnvironment, test_support};
use subspace_core::config::Config;
use subspace_dashboard::DashboardHandle;
use subspace_dashboard::settings::BoardSettings;
use subspace_product::{OsKind, PlatformEnv, ProductContext, fixture};
use super::{FIRST_SWEEP_DELAY, initialize, provision_missing_model};
use crate::product::ProductFiles;
use crate::registry::BoardRegistration;
fn context(root: &Path) -> ProductContext {
let known = fixture::products().unwrap();
let env = PlatformEnv {
os: OsKind::Macos,
home: root.to_owned(),
xdg_config_home: None,
xdg_state_home: None,
app_data: None,
local_app_data: None,
};
ProductContext::new(known[0].clone(), env)
}
fn no_gui(_: &mut Window, _: &mut App) -> DashboardHandle {
panic!("Search initialization must not construct a board")
}
fn no_tools(_: &ToolEnvironment) -> Vec<std::sync::Arc<dyn AgentTool>> {
panic!("Search initialization must not fetch board tools")
}
fn board() -> BoardRegistration {
BoardRegistration {
id: "irc",
title: "Channels".into(),
description: "IRC networks, channels and private messages".into(),
icon: None,
help: "",
help_images: &[],
searchable: true,
create: std::sync::Arc::new(no_gui),
tools: no_tools,
settings: BoardSettings::default(),
settings_editor: None,
fields: None,
start_background: |_| panic!("Headless initialization must not start monitoring"),
stop_background: |_| panic!("Headless initialization must not stop monitoring"),
retain_when_disabled: false,
configuration: None,
default_order: usize::MAX,
artwork: subspace_dashboard::BoardArtwork::default(),
connection: Default::default(),
contracts: Default::default(),
}
}
#[test]
fn headless_search_uses_the_product_allowlist_without_enabling_data_collection() {
let root = tempfile::tempdir().unwrap();
let context = context(root.path());
let files = ProductFiles::for_context(&context);
let mut environment = test_support::environment(Config::default());
initialize(&mut environment, &context, &[board()], &files, None).unwrap();
let service = environment.search.unwrap();
assert_eq!(service.app_id(), context.product.binary_name.as_str());
assert!(!service.policy("irc").unwrap().enabled);
assert!(service.policy("github").is_err());
assert_eq!(service.sources().unwrap()[0].records, 0);
assert!(
service
.reranker_status()
.is_none_or(|status| !status.installed)
);
assert_eq!(FIRST_SWEEP_DELAY.as_secs(), 600);
}
#[test]
fn a_board_stored_by_default_starts_on_until_the_person_chooses() {
let root = tempfile::tempdir().unwrap();
let context = context(root.path());
let files = ProductFiles::for_context(&context);
let mut stored = board();
stored.settings.store_by_default = true;
let mut environment = test_support::environment(Config::default());
initialize(&mut environment, &context, &[stored.clone()], &files, None).unwrap();
let service = environment.search.unwrap();
assert!(service.policy("irc").unwrap().enabled);
service
.set_policy("irc", subspace_search::Policy::default())
.unwrap();
drop(service);
let mut environment = test_support::environment(Config::default());
initialize(&mut environment, &context, &[stored], &files, None).unwrap();
assert!(!environment.search.unwrap().policy("irc").unwrap().enabled);
}
#[test]
fn desktop_setup_installs_the_model_once_when_it_is_missing() {
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use subspace_search::{Hit, Reranker, RerankerStatus, Service};
#[derive(Default)]
struct Model(AtomicUsize);
impl Reranker for Model {
fn rerank(&self, _: &str, _: &[Hit]) -> Result<Vec<Hit>, String> {
panic!("Setup must not perform inference")
}
fn status(&self) -> RerankerStatus {
RerankerStatus {
installed: self.0.load(Ordering::SeqCst) != 0,
name: "Fixture model".into(),
download_bytes: 10,
message: String::new(),
}
}
fn install(&self) -> Result<(), String> {
self.0.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
let root = tempfile::tempdir().unwrap();
let service = Service::open(root.path().into(), "test-app".into(), vec![]).unwrap();
let model = Arc::new(Model::default());
service.set_reranker(model.clone());
assert_eq!(model.0.load(Ordering::SeqCst), 0);
provision_missing_model(&service);
provision_missing_model(&service);
assert_eq!(model.0.load(Ordering::SeqCst), 1);
}
#[test]
fn nonparticipating_products_do_not_create_search_storage() {
let root = tempfile::tempdir().unwrap();
let context = context(root.path());
let files = ProductFiles::for_context(&context);
let mut environment = test_support::environment(Config::default());
initialize(
&mut environment,
&context,
&[],
&files,
Some(|_| panic!("A nonparticipating product must not construct a model")),
)
.unwrap();
assert!(environment.search.is_none());
assert!(!root.path().join("Library").exists());
}
}