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

refactor(pi): fetch model list from gateway instead of hardcoded list

Louis Beaumontcd4b71135573
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index d40875e9f..2c3c7e0d9 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -703,7 +703,7 @@ fn build_models_json(
"api": "openai-completions",
"apiKey": api_key_value,
"authHeader": true,
- "models": screenpipe_cloud_models()
+ "models": screenpipe_cloud_models(SCREENPIPE_API_URL, user_token)
});
providers_map.insert("screenpipe".to_string(), screenpipe_provider);
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rs
index 0f5cbf9a7..8d5ef2532 100644
--- a/crates/screenpipe-core/src/agents/pi.rs
+++ b/crates/screenpipe-core/src/agents/pi.rs
@@ -16,41 +16,87 @@ use tracing::{debug, error, info, warn};
const PI_PACKAGE: &str = "@mariozechner/[email protected]";
pub const SCREENPIPE_API_URL: &str = "https://api.screenpi.pe/v1";
-/// Returns the screenpipe cloud models array as a serde_json::Value.
-/// Shared between server-side pipe executor and desktop Pi chat so the
-/// model list stays in sync.
-/// Full model catalog matching the Cloudflare Worker gateway.
-/// Must stay in sync with packages/ai-gateway/src/handlers/models.ts.
-pub fn screenpipe_cloud_models() -> serde_json::Value {
+/// Fetch the model catalog from the Cloudflare Worker gateway and convert
+/// it into the format Pi's `models.json` expects.
+///
+/// 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) {
+ Some(models) => models,
+ None => {
+ warn!("failed to fetch models from gateway, using fallback list");
+ fallback_cloud_models()
+ }
+ }
+}
+
+/// 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> {
+ let url = format!("{}/models", api_url.trim_end_matches('/'));
+ let client = reqwest::blocking::Client::builder()
+ .timeout(std::time::Duration::from_secs(5))
+ .build()
+ .ok()?;
+
+ let mut req = client.get(&url);
+ if let Some(t) = token {
+ req = req.bearer_auth(t);
+ }
+
+ let resp = req.send().ok()?;
+ if !resp.status().is_success() {
+ warn!("gateway /v1/models returned {}", resp.status());
+ return None;
+ }
+
+ let body: serde_json::Value = resp.json().ok()?;
+ let data = body.get("data")?.as_array()?;
+
+ let models: Vec<serde_json::Value> = data
+ .iter()
+ .map(|m| {
+ let id = m.get("id").and_then(|v| v.as_str()).unwrap_or("");
+ let name = m.get("name").and_then(|v| v.as_str()).unwrap_or(id);
+ let ctx = m.get("context_window").and_then(|v| v.as_u64()).unwrap_or(128000);
+ let intelligence = m.get("intelligence").and_then(|v| v.as_str()).unwrap_or("standard");
+ let reasoning = intelligence == "highest" || intelligence == "high";
+
+ // Determine input modalities from best_for/tags
+ let best_for = m.get("best_for").and_then(|v| v.as_array());
+ let has_vision = best_for
+ .map(|arr| arr.iter().any(|v| v.as_str().map_or(false, |s| s.contains("vision"))))
+ .unwrap_or(false);
+ let input = if has_vision {
+ json!(["text", "image"])
+ } else {
+ json!(["text"])
+ };
+
+ json!({
+ "id": id,
+ "name": name,
+ "reasoning": reasoning,
+ "input": input,
+ "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},
+ "contextWindow": ctx,
+ "maxTokens": 32000,
+ })
+ })
+ .collect();
+
+ info!("fetched {} models from gateway", models.len());
+ Some(json!(models))
+}
+
+/// Minimal fallback when the gateway is unreachable.
+/// Only includes auto + a few core models so the app isn't completely broken offline.
+fn fallback_cloud_models() -> serde_json::Value {
json!([
- // ── Auto — smart routing with fallback (gateway resolves to best free model) ──
{"id": "auto", "name": "Auto (recommended)", "reasoning": true, "input": ["text", "image"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 128000, "maxTokens": 32000},
- // ── Free models (Vertex AI MaaS — GCP credits, free for users) ──
- {"id": "glm-4.7", "name": "GLM-4.7", "reasoning": true, "input": ["text"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 200000, "maxTokens": 32000},
- {"id": "glm-5", "name": "GLM-5", "reasoning": true, "input": ["text"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 128000, "maxTokens": 32000},
- {"id": "kimi-k2.5", "name": "Kimi K2.5", "reasoning": true, "input": ["text"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 128000, "maxTokens": 32000},
- {"id": "llama-4-maverick", "name": "Llama 4 Maverick (Vision)", "reasoning": true, "input": ["text", "image"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 524000, "maxTokens": 8192},
- {"id": "llama-4-scout", "name": "Llama 4 Scout (Vision)", "reasoning": false, "input": ["text", "image"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 512000, "maxTokens": 8192},
- // ── Free models (OpenRouter / Gemini) ──
- {"id": "qwen/qwen3-coder:free", "name": "Qwen3 Coder 480B", "reasoning": true, "input": ["text"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 262000, "maxTokens": 32000},
- {"id": "stepfun/step-3.5-flash:free", "name": "Step 3.5 Flash", "reasoning": false, "input": ["text"], "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 256000, "maxTokens": 32000},
- {"id": "gemini-3-flash", "name": "Gemini 3 Flash", "reasoning": false, "input": ["text", "image"], "cost": {"input": 0.10, "output": 0.40, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 1000000, "maxTokens": 65536},
- {"id": "gemini-2.5-flash", "name": "Gemini 2.5 Flash", "reasoning": false, "input": ["text", "image"], "cost": {"input": 0.075, "output": 0.30, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 1000000, "maxTokens": 65536},
- // ── Confidential inference (Tinfoil — secure enclaves) ──
- {"id": "gemma4-31b", "name": "Gemma 4 31B (Confidential)", "reasoning": false, "input": ["text"], "cost": {"input": 0.45, "output": 1.00, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 256000, "maxTokens": 32000},
- // ── Anthropic ──
- {"id": "claude-opus-4-6", "name": "Claude Opus 4.6", "reasoning": true, "input": ["text", "image"], "cost": {"input": 15, "output": 75, "cacheRead": 1.5, "cacheWrite": 18.75}, "contextWindow": 200000, "maxTokens": 32000},
- {"id": "claude-sonnet-4-5", "name": "Claude Sonnet 4.5", "reasoning": true, "input": ["text", "image"], "cost": {"input": 3, "output": 15, "cacheRead": 0.3, "cacheWrite": 3.75}, "contextWindow": 200000, "maxTokens": 64000},
{"id": "claude-haiku-4-5", "name": "Claude Haiku 4.5", "reasoning": true, "input": ["text", "image"], "cost": {"input": 0.8, "output": 4, "cacheRead": 0.08, "cacheWrite": 1}, "contextWindow": 200000, "maxTokens": 64000},
- // ── OpenRouter ──
- {"id": "qwen/qwen3.5-flash-02-23", "name": "Qwen3.5 Flash", "reasoning": false, "input": ["text"], "cost": {"input": 0.065, "output": 0.26, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 1000000, "maxTokens": 32000},
- {"id": "deepseek/deepseek-chat", "name": "DeepSeek V3.2", "reasoning": true, "input": ["text"], "cost": {"input": 0.26, "output": 0.38, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 128000, "maxTokens": 32000},
- {"id": "deepseek/deepseek-v3.2-speciale", "name": "DeepSeek V3.2 Speciale", "reasoning": true, "input": ["text"], "cost": {"input": 2.19, "output": 8.76, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 128000, "maxTokens": 32000},
- {"id": "qwen/qwen3.5-397b-a17b", "name": "Qwen3.5 397B", "reasoning": true, "input": ["text", "image"], "cost": {"input": 1.50, "output": 3.00, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 131000, "maxTokens": 32000},
- {"id": "meta-llama/llama-4-scout", "name": "Llama 4 Scout", "reasoning": false, "input": ["text"], "cost": {"input": 0.11, "output": 0.34, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 512000, "maxTokens": 32000},
- {"id": "meta-llama/llama-4-maverick", "name": "Llama 4 Maverick", "reasoning": true, "input": ["text"], "cost": {"input": 0.50, "output": 0.77, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 256000, "maxTokens": 32000},
- // ── Google ──
- {"id": "gemini-3.1-pro", "name": "Gemini 3.1 Pro", "reasoning": true, "input": ["text", "image"], "cost": {"input": 1.25, "output": 10.00, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 1000000, "maxTokens": 65536},
+ {"id": "gemini-3-flash", "name": "Gemini 3 Flash", "reasoning": false, "input": ["text", "image"], "cost": {"input": 0.10, "output": 0.40, "cacheRead": 0, "cacheWrite": 0}, "contextWindow": 1000000, "maxTokens": 65536},
])
}
@@ -349,7 +395,7 @@ impl PiExecutor {
"api": "openai-completions",
"apiKey": api_key_value,
"authHeader": true,
- "models": screenpipe_cloud_models()
+ "models": screenpipe_cloud_models(api_url, user_token)
});
if let Some(providers) = models_config
@@ -487,31 +533,18 @@ impl PiExecutor {
Ok(())
}
- /// Resolve a model name for the screenpipe provider by stripping date suffixes
- /// (e.g. "claude-haiku-4-5@20251001" → "claude-haiku-4-5") to match the cloud
- /// model list. Passthrough for non-screenpipe providers.
+ /// Resolve a model name by stripping date suffixes
+ /// (e.g. "claude-haiku-4-5@20251001" → "claude-haiku-4-5").
+ /// Passthrough for non-screenpipe providers.
fn resolve_model(requested: &str, provider: &str) -> String {
if provider != "screenpipe" {
return requested.to_string();
}
- let models = screenpipe_cloud_models();
- // Exact match → use as-is
- if models
- .as_array()
- .map(|arr| arr.iter().any(|m| m["id"].as_str() == Some(requested)))
- .unwrap_or(false)
- {
- return requested.to_string();
- }
- // Strip @date suffix and try again
+ // Strip @date suffix if present — the gateway validates the model ID
if let Some(base) = requested.split('@').next() {
- if models
- .as_array()
- .map(|arr| arr.iter().any(|m| m["id"].as_str() == Some(base)))
- .unwrap_or(false)
- {
+ if base != requested {
warn!(
- "model '{}' not in cloud list, resolved to '{}'",
+ "model '{}' has @date suffix, resolved to '{}'",
requested, base
);
return base.to_string();