Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix chat ui (pi agent) not working (#2990)

Divanshu595cccfc786c
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index 8d01f35ec..0520eb1dd 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -690,7 +690,7 @@ fn default_max_tokens() -> i32 {
/// Returns a map of provider entries to merge into the existing models.json.
/// We merge instead of rebuilding from scratch to avoid a race condition where
/// concurrent pipes overwrite each other's providers.
-fn build_models_json(
+async fn build_models_json(
user_token: Option<&str>,
provider_config: Option<&PiProviderConfig>,
) -> serde_json::Value {
@@ -698,12 +698,13 @@ fn build_models_json(
// Always add screenpipe cloud provider
let api_key_value = user_token.unwrap_or("SCREENPIPE_API_KEY");
+ let models = screenpipe_cloud_models(SCREENPIPE_API_URL, user_token).await;
let screenpipe_provider = json!({
"baseUrl": SCREENPIPE_API_URL,
"api": "openai-completions",
"apiKey": api_key_value,
"authHeader": true,
- "models": screenpipe_cloud_models(SCREENPIPE_API_URL, user_token)
+ "models": models
});
providers_map.insert("screenpipe".to_string(), screenpipe_provider);
@@ -798,7 +799,7 @@ fn build_models_json(
}
/// Write pi's provider config (models.json + auth.json).
-fn ensure_pi_config(
+async fn ensure_pi_config(
user_token: Option<&str>,
provider_config: Option<&PiProviderConfig>,
) -> Result<(), String> {
@@ -806,7 +807,7 @@ fn ensure_pi_config(
std::fs::create_dir_all(&config_dir)
.map_err(|e| format!("Failed to create pi config dir: {}", e))?;
- let new_providers = build_models_json(user_token, provider_config);
+ let new_providers = build_models_json(user_token, provider_config).await;
// Merge into existing models.json to avoid race conditions with concurrent pipes
let models_path = config_dir.join("models.json");
@@ -1038,7 +1039,7 @@ pub async fn pi_start_inner(
ensure_web_search_extension(&project_dir, provider_config.as_ref())?;
// Ensure Pi is configured with the user's provider
- ensure_pi_config(user_token.as_deref(), provider_config.as_ref())?;
+ ensure_pi_config(user_token.as_deref(), provider_config.as_ref()).await?;
// Determine which Pi provider and model to use
let (pi_provider, pi_model) = match &provider_config {
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rs
index daea59d76..1ea88b7ca 100644
--- a/crates/screenpipe-core/src/agents/pi.rs
+++ b/crates/screenpipe-core/src/agents/pi.rs
@@ -22,8 +22,8 @@ pub const SCREENPIPE_API_URL: &str = "https://api.screenpi.pe/v1";
/// The gateway (`/v1/models`) is the single source of truth. On failure
/// (offline, timeout, gateway down) we fall back to a minimal hardcoded list
/// so the app still works without network.
-pub fn screenpipe_cloud_models(api_url: &str, token: Option<&str>) -> serde_json::Value {
- match fetch_models_from_gateway(api_url, token) {
+pub async fn screenpipe_cloud_models(api_url: &str, token: Option<&str>) -> serde_json::Value {
+ match fetch_models_from_gateway(api_url, token).await {
Some(models) => models,
None => {
warn!("failed to fetch models from gateway, using fallback list");
@@ -33,9 +33,9 @@ pub fn screenpipe_cloud_models(api_url: &str, token: Option<&str>) -> serde_json
}
/// Fetch models from the gateway and transform into Pi's format.
-fn fetch_models_from_gateway(api_url: &str, token: Option<&str>) -> Option<serde_json::Value> {
+async fn fetch_models_from_gateway(api_url: &str, token: Option<&str>) -> Option<serde_json::Value> {
let url = format!("{}/models", api_url.trim_end_matches('/'));
- let client = reqwest::blocking::Client::builder()
+ let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()
.ok()?;
@@ -45,13 +45,13 @@ fn fetch_models_from_gateway(api_url: &str, token: Option<&str>) -> Option<serde
req = req.bearer_auth(t);
}
- let resp = req.send().ok()?;
+ let resp = req.send().await.ok()?;
if !resp.status().is_success() {
warn!("gateway /v1/models returned {}", resp.status());
return None;
}
- let body: serde_json::Value = resp.json().ok()?;
+ let body: serde_json::Value = resp.json().await.ok()?;
let data = body.get("data")?.as_array()?;
let models: Vec<serde_json::Value> = data
@@ -328,7 +328,7 @@ impl PiExecutor {
/// When a pipe uses a non-screenpipe provider (e.g. ollama, openai), pass
/// the resolved `provider`, `model`, and optional `provider_url` so the
/// corresponding entry is written to `models.json`.
- pub fn ensure_pi_config(
+ pub async fn ensure_pi_config(
user_token: Option<&str>,
api_url: &str,
provider: Option<&str>,
@@ -388,12 +388,13 @@ impl PiExecutor {
.or_else(|| std::env::var("SCREENPIPE_API_KEY").ok())
.unwrap_or_else(|| "SCREENPIPE_API_KEY".to_string());
let api_key_value = api_key_value.as_str();
+ let models = screenpipe_cloud_models(api_url, user_token).await;
let screenpipe_provider = json!({
"baseUrl": api_url,
"api": "openai-completions",
"apiKey": api_key_value,
"authHeader": true,
- "models": screenpipe_cloud_models(api_url, user_token)
+ "models": models
});
if let Some(providers) = models_config
@@ -864,7 +865,7 @@ impl AgentExecutor for PiExecutor {
provider,
Some(model),
provider_url,
- )?;
+ ).await?;
// Use filtered skills if permissions are configured, unfiltered otherwise
Self::ensure_screenpipe_skill_auto(working_dir)?;
@@ -919,7 +920,7 @@ impl AgentExecutor for PiExecutor {
provider,
Some(&resolved_model),
provider_url,
- )?;
+ ).await?;
return self
.spawn_pi(
&pi_path,
@@ -960,7 +961,7 @@ impl AgentExecutor for PiExecutor {
provider,
Some(&resolved_model),
provider_url,
- )?;
+ ).await?;
// Use filtered skills if permissions are configured, unfiltered otherwise
Self::ensure_screenpipe_skill_auto(working_dir)?;
Self::ensure_web_search_extension(working_dir, Some(&resolved_provider))?;
@@ -1008,7 +1009,7 @@ impl AgentExecutor for PiExecutor {
provider,
Some(&resolved_model),
provider_url,
- )?;
+ ).await?;
return self
.spawn_pi_streaming(
&pi_path,
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index e976a798b..aedfa2ec8 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -1851,7 +1851,7 @@ impl PipeManager {
run_provider.as_deref(),
Some(&run_model),
run_provider_url.as_deref(),
- ) {
+ ).await {
warn!("failed to pre-configure pi provider: {}", e);
}
@@ -2325,7 +2325,7 @@ impl PipeManager {
run_provider.as_deref(),
Some(&run_model),
run_provider_url.as_deref(),
- ) {
+ ).await {
warn!("failed to pre-configure pi provider: {}", e);
}
@@ -3302,7 +3302,7 @@ impl PipeManager {
provider.as_deref(),
Some(&model),
provider_url.as_deref(),
- ) {
+ ).await {
warn!("scheduler: failed to pre-configure pi provider: {}", e);
}