Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(embeddings): split monolithic rpc module into submodules

Steven Enamakela500ea11e121
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc.rs b/crates/openhuman-core/src/inference/embeddings/rpc.rs
index 3fa98459bc..a112cc3946 100644
--- a/crates/openhuman-core/src/inference/embeddings/rpc.rs
+++ b/crates/openhuman-core/src/inference/embeddings/rpc.rs
@@ -3,5 +3,86 @@
#[cfg(test)]
#[path = "rpc_tests.rs"]
mod tests;
-include!("rpc_part_01.rs");
-include!("rpc_part_02.rs");
+
+mod api_keys;
+mod embed;
+mod probe;
+mod served_models;
+mod settings;
+
+pub use api_keys::{clear_api_key, set_api_key};
+pub use embed::{embed, test_connection};
+pub use settings::{get_settings, update_settings};
+
+#[cfg(test)]
+use probe::{classify_embed_probe, EmbedProbe};
+#[cfg(test)]
+use served_models::{
+ check_requested_model_served, fetch_served_model_ids, normalize_embed_model_id,
+ reject_model_not_served,
+};
+#[cfg(test)]
+use settings::remember_active_custom_profile;
+
+use crate::config::Config;
+use crate::security::credentials::AuthService;
+
+use super::factory::create_embedding_provider_with_config;
+
+const LOG_PREFIX: &str = "[embeddings::rpc]";
+
+pub fn provider_from_config(config: &Config) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {
+ build_embedder(
+ config,
+ &config.memory.embedding_provider,
+ &config.memory.embedding_model,
+ config.memory.embedding_dimensions,
+ )
+}
+
+/// Construct an embedding provider for an explicit `(provider_name, model,
+/// dims)` triple, resolving the stored API key + inline `custom:<url>` endpoint
+/// the same way [`embed`] / [`test_connection`] do. Single construction seam so
+/// the save-time probe in [`update_settings`] and the live embed path can't
+/// drift on slug-normalization / credential-lookup rules.
+fn build_embedder(
+ config: &Config,
+ provider_name: &str,
+ model: &str,
+ dims: usize,
+) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {
+ let api_key = resolve_api_key(config, provider_name);
+ let custom_endpoint = provider_name.strip_prefix("custom:").map(|s| s.to_string());
+ let provider_slug = if provider_name.starts_with("custom:") {
+ "custom"
+ } else {
+ provider_name
+ };
+ create_embedding_provider_with_config(
+ config,
+ provider_slug,
+ model,
+ dims,
+ &api_key,
+ custom_endpoint.as_deref(),
+ )
+}
+
+/// Normalized result of the setup-time test embed in [`update_settings`].
+/// Collapses the `Result<Result<_, _>, Elapsed>` timeout shape into one enum so
+/// the verification policy can be expressed (and unit-tested) as a pure
+/// function over it.
+
+pub(crate) fn resolve_api_key(config: &Config, provider_name: &str) -> String {
+ let slug = if provider_name.starts_with("custom:") {
+ "custom"
+ } else {
+ provider_name
+ };
+ let cred_provider = format!("embeddings:{slug}");
+ let auth = AuthService::from_config(config);
+ auth.get_provider_bearer_token(&cred_provider, None)
+ .ok()
+ .flatten()
+ .unwrap_or_default()
+}
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc/api_keys.rs b/crates/openhuman-core/src/inference/embeddings/rpc/api_keys.rs
new file mode 100644
index 0000000000..907b642b24
--- /dev/null
+++ b/crates/openhuman-core/src/inference/embeddings/rpc/api_keys.rs
@@ -0,0 +1,86 @@
+//! Storing and clearing a provider's embedding API key.
+
+use std::collections::HashMap;
+
+use crate::config::Config;
+use crate::rpc::RpcOutcome;
+use crate::security::credentials::AuthService;
+
+use super::LOG_PREFIX;
+
+pub async fn set_api_key(
+ config: &Config,
+ provider_slug: &str,
+ api_key: &str,
+) -> Result<RpcOutcome<serde_json::Value>, String> {
+ if provider_slug.is_empty() {
+ return Err("provider slug is required".into());
+ }
+ if api_key.trim().is_empty() {
+ return Err("api_key cannot be empty".into());
+ }
+
+ let cred_provider = format!("embeddings:{provider_slug}");
+ let auth = AuthService::from_config(config);
+ auth.store_provider_token(&cred_provider, "default", api_key, HashMap::new(), true)
+ .map_err(|e| format!("failed to store embedding API key: {e}"))?;
+
+ // #5324: supplying a BYO key does NOT change the embedding signature, so
+ // `ensure_reembed_backfill` has nothing to enqueue — but it is precisely
+ // the action that unblocks jobs parked on `budget_exhausted` /
+ // `auth_missing`. Requeue them here or they stay dead until the user
+ // separately discovers the "Retry failed" button. A store failure is
+ // surfaced (not reported as `0`) so the key-stored response can't imply the
+ // parked queue was recovered when it wasn't.
+ let requeue_result = crate::memory::ops::maintenance::retry_failed(config).await;
+ let requeued_count = *requeue_result.as_ref().unwrap_or(&0);
+ let requeue_error = requeue_result.as_ref().err().cloned();
+ let requeued_note = match &requeue_error {
+ None => requeued_count.to_string(),
+ Some(e) => format!("error ({e})"),
+ };
+
+ tracing::info!(
+ provider = provider_slug,
+ requeued = requeued_count,
+ requeue_error = requeue_error.as_deref().unwrap_or(""),
+ "{LOG_PREFIX} set_api_key stored"
+ );
+
+ Ok(RpcOutcome::new(
+ serde_json::json!({ "stored": true, "provider": provider_slug, "requeued_failed_jobs": requeued_count, "requeue_error": requeue_error }),
+ vec![format!(
+ "embedding API key stored for {provider_slug} (requeued_failed={requeued_note})"
+ )],
+ ))
+}
+
+/// Removes the API key for a specific embedding provider.
+pub async fn clear_api_key(
+ config: &Config,
+ provider_slug: &str,
+) -> Result<RpcOutcome<serde_json::Value>, String> {
+ if provider_slug.is_empty() {
+ return Err("provider slug is required".into());
+ }
+
+ let cred_provider = format!("embeddings:{provider_slug}");
+ let auth = AuthService::from_config(config);
+ let removed = auth
+ .remove_profile(&cred_provider, "default")
+ .map_err(|e| format!("failed to clear embedding API key: {e}"))?;
+
+ tracing::info!(
+ provider = provider_slug,
+ removed,
+ "{LOG_PREFIX} clear_api_key"
+ );
+
+ Ok(RpcOutcome::new(
+ serde_json::json!({ "cleared": removed, "provider": provider_slug }),
+ vec![format!("embedding API key cleared for {provider_slug}")],
+ ))
+}
+
+/// Generates embeddings for the given input texts using the currently
+/// configured provider.
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc/embed.rs b/crates/openhuman-core/src/inference/embeddings/rpc/embed.rs
new file mode 100644
index 0000000000..5e85f7e806
--- /dev/null
+++ b/crates/openhuman-core/src/inference/embeddings/rpc/embed.rs
@@ -0,0 +1,151 @@
+//! Live embedding through the configured provider, and the connectivity test.
+
+use crate::config::Config;
+use crate::rpc::RpcOutcome;
+
+use super::probe::probe_custom_embeddings;
+use super::{resolve_api_key, LOG_PREFIX};
+use crate::inference::embeddings::factory::create_embedding_provider_with_config;
+
+pub async fn embed(
+ config: &Config,
+ inputs: &[String],
+) -> Result<RpcOutcome<serde_json::Value>, String> {
+ let provider_name = &config.memory.embedding_provider;
+ let model = &config.memory.embedding_model;
+ let dims = config.memory.embedding_dimensions;
+
+ let api_key = resolve_api_key(config, provider_name);
+
+ let custom_endpoint = if provider_name.starts_with("custom:") {
+ provider_name
+ .strip_prefix("custom:")
+ .map(|s: &str| s.to_string())
+ } else {
+ None
+ };
+
+ let provider_slug = if provider_name.starts_with("custom:") {
+ "custom"
+ } else {
+ provider_name.as_str()
+ };
+
+ let embedder = create_embedding_provider_with_config(
+ config,
+ provider_slug,
+ model,
+ dims,
+ &api_key,
+ custom_endpoint.as_deref(),
+ )
+ .map_err(|e| e.to_string())?;
+
+ let refs: Vec<&str> = inputs.iter().map(|s| s.as_str()).collect();
+ let vectors = embedder.embed(&refs).await.map_err(|e| e.to_string())?;
+
+ let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);
+
+ tracing::debug!(
+ provider = provider_slug,
+ model,
+ input_count = inputs.len(),
+ vector_count = vectors.len(),
+ dims = actual_dims,
+ "{LOG_PREFIX} embed completed"
+ );
+
+ let payload = serde_json::json!({
+ "vectors": vectors,
+ "dimensions": actual_dims,
+ "count": vectors.len(),
+ "provider": provider_slug,
+ "model": model,
+ });
+
+ Ok(RpcOutcome::new(payload, vec!["embedding completed".into()]))
+}
+
+/// Tests connectivity to the configured (or specified) embedding provider.
+pub async fn test_connection(
+ config: &Config,
+ provider_slug: Option<&str>,
+ model: Option<&str>,
+ dims: Option<usize>,
+) -> Result<RpcOutcome<serde_json::Value>, String> {
+ let slug = provider_slug.unwrap_or(&config.memory.embedding_provider);
+ let model = model.unwrap_or(&config.memory.embedding_model);
+ let dims = dims.unwrap_or(config.memory.embedding_dimensions);
+
+ let api_key = resolve_api_key(config, slug);
+
+ let custom_endpoint = if slug.starts_with("custom:") {
+ slug.strip_prefix("custom:").map(|s| s.to_string())
+ } else {
+ None
+ };
+
+ let provider_tag = if slug.starts_with("custom:") {
+ "custom"
+ } else {
+ slug
+ };
+
+ tracing::debug!(
+ provider = provider_tag,
+ model,
+ dims,
+ "{LOG_PREFIX} test_connection starting"
+ );
+
+ let result = if let Some(endpoint) = custom_endpoint.as_deref() {
+ probe_custom_embeddings(endpoint, &api_key, model).await
+ } else {
+ let embedder = create_embedding_provider_with_config(
+ config,
+ provider_tag,
+ model,
+ dims,
+ &api_key,
+ None,
+ )
+ .map_err(|e| e.to_string())?;
+ embedder
+ .embed(&["connection test"])
+ .await
+ .map_err(|e| e.to_string())
+ };
+
+ match result {
+ Ok(vectors) => {
+ let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);
+ let payload = serde_json::json!({
+ "success": true,
+ "provider": provider_tag,
+ "model": model,
+ "requested_dimensions": dims,
+ "actual_dimensions": actual_dims,
+ });
+ Ok(RpcOutcome::new(
+ payload,
+ vec!["connection test passed".into()],
+ ))
+ }
+ Err(e) => {
+ let payload = serde_json::json!({
+ "success": false,
+ "provider": provider_tag,
+ "model": model,
+ "error": e.to_string(),
+ });
+ Ok(RpcOutcome::new(
+ payload,
+ vec![format!("connection test failed: {e}")],
+ ))
+ }
+ }
+}
+
+/// Build an embedding provider from the live config — the same construction
+/// [`embed`] uses, exposed so other domains (e.g. `codegraph`) can obtain a
+/// provider for `signature()` + direct embedding without a JSON-RPC round-trip.
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc_part_02.rs b/crates/openhuman-core/src/inference/embeddings/rpc/probe.rs
similarity index 61%
rename from crates/openhuman-core/src/inference/embeddings/rpc_part_02.rs
rename to crates/openhuman-core/src/inference/embeddings/rpc/probe.rs
index 675ff308d6..3755b1223a 100644
--- a/crates/openhuman-core/src/inference/embeddings/rpc_part_02.rs
+++ b/crates/openhuman-core/src/inference/embeddings/rpc/probe.rs
@@ -1,129 +1,81 @@
+//! The setup-time embed probe: sending one width-agnostic request to a custom
+//! endpoint and classifying what came back into an accept-or-reject verdict.
-/// Tests connectivity to the configured (or specified) embedding provider.
-pub async fn test_connection(
- config: &Config,
- provider_slug: Option<&str>,
- model: Option<&str>,
- dims: Option<usize>,
-) -> Result<RpcOutcome<serde_json::Value>, String> {
- let slug = provider_slug.unwrap_or(&config.memory.embedding_provider);
- let model = model.unwrap_or(&config.memory.embedding_model);
- let dims = dims.unwrap_or(config.memory.embedding_dimensions);
+use crate::rpc::RpcOutcome;
- let api_key = resolve_api_key(config, slug);
+use crate::inference::embeddings::factory::model_supports_dimensions;
- let custom_endpoint = if slug.starts_with("custom:") {
- slug.strip_prefix("custom:").map(|s| s.to_string())
- } else {
- None
- };
-
- let provider_tag = if slug.starts_with("custom:") {
- "custom"
- } else {
- slug
- };
-
- tracing::debug!(
- provider = provider_tag,
- model,
- dims,
- "{LOG_PREFIX} test_connection starting"
- );
-
- let result = if let Some(endpoint) = custom_endpoint.as_deref() {
- probe_custom_embeddings(endpoint, &api_key, model).await
+/// Send one OpenAI-compatible embedding request without requesting or
+/// validating a vector width. This is intentionally separate from the live
+/// provider: a setup probe must discover a custom endpoint's native width,
+/// while a live provider must enforce the width persisted after that probe.
+pub(super) async fn probe_custom_embeddings(
+ endpoint: &str,
+ api_key: &str,
+ model: &str,
+) -> Result<Vec<Vec<f32>>, String> {
+ let base = endpoint.trim_end_matches('/');
+ let url = if base.ends_with("/embeddings") {
+ base.to_string()
+ } else if base.ends_with("/v1") {
+ format!("{base}/embeddings")
} else {
- let embedder = create_embedding_provider_with_config(
- config,
- provider_tag,
- model,
- dims,
- &api_key,
- None,
- )
- .map_err(|e| e.to_string())?;
- embedder
- .embed(&["connection test"])
- .await
- .map_err(|e| e.to_string())
+ format!("{base}/v1/embeddings")
};
-
- match result {
- Ok(vectors) => {
- let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);
- let payload = serde_json::json!({
- "success": true,
- "provider": provider_tag,
- "model": model,
- "requested_dimensions": dims,
- "actual_dimensions": actual_dims,
- });
- Ok(RpcOutcome::new(
- payload,
- vec!["connection test passed".into()],
- ))
- }
- Err(e) => {
- let payload = serde_json::json!({
- "success": false,
- "provider": provider_tag,
- "model": model,
- "error": e.to_string(),
- });
- Ok(RpcOutcome::new(
- payload,
- vec![format!("connection test failed: {e}")],
- ))
- }
+ let mut request = reqwest::Client::new()
+ .post(&url)
+ .json(&serde_json::json!({ "model": model, "input": ["connection test"] }));
+ if !api_key.trim().is_empty() {
+ request = request.bearer_auth(api_key.trim());
}
+ let response = request
+ .send()
+ .await
+ .map_err(|e| format!("custom embeddings request to {url} failed: {e}"))?;
+ let status = response.status();
+ let body = response
+ .text()
+ .await
+ .map_err(|e| format!("custom embeddings response read failed: {e}"))?;
+ if !status.is_success() {
+ return Err(format!("custom embeddings returned HTTP {status}: {body}"));
+ }
+ let data = serde_json::from_str::<serde_json::Value>(&body)
+ .map_err(|e| format!("custom embeddings response was not JSON: {e}"))?
+ .get("data")
+ .and_then(serde_json::Value::as_array)
+ .cloned()
+ .ok_or_else(|| "custom embeddings response missing data array".to_string())?;
+ data.into_iter()
+ .map(|item| {
+ item.get("embedding")
+ .and_then(serde_json::Value::as_array)
+ .ok_or_else(|| "custom embeddings response missing embedding array".to_string())?
+ .iter()
+ .map(|value| value.as_f64().map(|value| value as f32).ok_or_else(|| "custom embeddings response contains a non-numeric vector".to_string()))
+ .collect()
+ })
+ .collect()
}
-/// Build an embedding provider from the live config — the same construction
-/// [`embed`] uses, exposed so other domains (e.g. `codegraph`) can obtain a
-/// provider for `signature()` + direct embedding without a JSON-RPC round-trip.
-pub fn provider_from_config(config: &Config) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {
- build_embedder(
- config,
- &config.memory.embedding_provider,
- &config.memory.embedding_model,
- config.memory.embedding_dimensions,
- )
-}
-
-/// Construct an embedding provider for an explicit `(provider_name, model,
-/// dims)` triple, resolving the stored API key + inline `custom:<url>` endpoint
-/// the same way [`embed`] / [`test_connection`] do. Single construction seam so
-/// the save-time probe in [`update_settings`] and the live embed path can't
-/// drift on slug-normalization / credential-lookup rules.
-fn build_embedder(
- config: &Config,
- provider_name: &str,
- model: &str,
- dims: usize,
-) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {
- let api_key = resolve_api_key(config, provider_name);
- let custom_endpoint = provider_name.strip_prefix("custom:").map(|s| s.to_string());
- let provider_slug = if provider_name.starts_with("custom:") {
- "custom"
+/// Dimension to persist after a successful Custom verification probe.
+///
+/// For a `text-embedding-3-*` model the endpoint honoured the requested size,
+/// so keep the user's `configured` value (Matryoshka). For every other model we
+/// probed dimension-agnostically, so adopt the endpoint's actual returned
+/// length (`actual`) — the user can't be expected to know it, and storing the
+/// real size is what lets the live embed path's length guard pass afterwards.
+/// Falls back to `configured` if the probe somehow reported a zero-length
+/// vector (defensive — `classify_embed_probe` already rejects empty vectors).
+pub(super) fn final_probe_dims(model: &str, configured: usize, actual: usize) -> usize {
+ if model_supports_dimensions(model) || actual == 0 {
+ configured
} else {
- provider_name
- };
- create_embedding_provider_with_config(
- config,
- provider_slug,
- model,
- dims,
- &api_key,
- custom_endpoint.as_deref(),
- )
+ actual
+ }
}
-/// Normalized result of the setup-time test embed in [`update_settings`].
-/// Collapses the `Result<Result<_, _>, Elapsed>` timeout shape into one enum so
-/// the verification policy can be expressed (and unit-tested) as a pure
-/// function over it.
-enum EmbedProbe {
+pub(super) enum EmbedProbe {
/// The endpoint returned vectors (may still be empty/zero-dim — checked).
Returned(Vec<Vec<f32>>),
/// The embed call returned an error; the string is the provider detail.
@@ -142,7 +94,7 @@ enum EmbedProbe {
/// classify-and-suppress the resulting embed flood in code — residual floods
/// (e.g. the user unloads the model after a good save) are handled Sentry-side.
/// The known shapes only get a friendlier remediation message.
-fn classify_embed_probe(outcome: EmbedProbe) -> Option<RpcOutcome<serde_json::Value>> {
+pub(super) fn classify_embed_probe(outcome: EmbedProbe) -> Option<RpcOutcome<serde_json::Value>> {
let reject = |error: &str, message: &str, summary: &str, detail: Option<&str>| {
let mut body = serde_json::json!({ "error": error, "message": message });
if let Some(d) = detail {
@@ -364,107 +316,3 @@ fn is_embedding_endpoint_unreachable(lower: &str) -> bool {
/// GET `{endpoint}/models` (OpenAI-compatible) and return the served model ids.
/// Time-boxed and best-effort — any failure returns `Err` and the caller falls
/// back to the live test-embed probe (issue #3761).
-async fn fetch_served_model_ids(endpoint: &str, api_key: &str) -> Result<Vec<String>, String> {
- #[derive(serde::Deserialize)]
- struct ModelEntry {
- id: String,
- }
- #[derive(serde::Deserialize)]
- struct ModelsResponse {
- #[serde(default)]
- data: Vec<ModelEntry>,
- }
-
- let url = format!("{}/models", endpoint.trim_end_matches('/'));
- let client = reqwest::Client::new();
- let mut req = client.get(&url).timeout(std::time::Duration::from_secs(5));
- if !api_key.trim().is_empty() {
- req = req.bearer_auth(api_key.trim());
- }
- let resp = req
- .send()
- .await
- .map_err(|e| format!("models request failed: {e}"))?;
- if !resp.status().is_success() {
- return Err(format!("models request returned status {}", resp.status()));
- }
- let parsed: ModelsResponse = resp
- .json()
- .await
- .map_err(|e| format!("models parse failed: {e}"))?;
- Ok(parsed.data.into_iter().map(|m| m.id).collect())
-}
-
-/// Normalize an embedding model id for tolerant *suggestion* matching:
-/// lowercase, drop a leading `text-embedding-`, drop a trailing `:tag`. Used
-/// only to suggest the right served name — never to silently rewrite the id.
-fn normalize_embed_model_id(name: &str) -> String {
- let lower = name.trim().to_ascii_lowercase();
- let stripped = lower.strip_prefix("text-embedding-").unwrap_or(&lower);
- stripped.split(':').next().unwrap_or(stripped).to_string()
-}
-
-/// Decide whether the requested model is acceptable given the endpoint's served
-/// list. Returns `Some(reject)` only when the endpoint reports a non-empty list
-/// that does NOT contain the requested id — i.e. we have positive evidence the
-/// model isn't loaded. An empty/unknown list returns `None` (defer to the live
-/// test-embed probe) so we never block on a server that doesn't expose
-/// `/models` (issue #3761).
-fn check_requested_model_served(
- requested: &str,
- served: &[String],
-) -> Option<RpcOutcome<serde_json::Value>> {
- if served.is_empty() || served.iter().any(|m| m == requested) {
- return None;
- }
- Some(reject_model_not_served(requested, served))
-}
-
-/// Build the "model not served" rejection: names what the endpoint actually
-/// serves and, when a normalized match exists, suggests the exact name to pick
-/// (e.g. `bge-m3` → `text-embedding-bge-m3`). Reuses the
-/// `EMBEDDINGS_NO_MODEL_LOADED` error code so the existing Embeddings setup
-/// dialog surfaces `message` and keeps the config unsaved (issue #3761).
-fn reject_model_not_served(requested: &str, served: &[String]) -> RpcOutcome<serde_json::Value> {
- let want = normalize_embed_model_id(requested);
- let suggestion = served
- .iter()
- .find(|m| normalize_embed_model_id(m) == want)
- .cloned();
- let served_list = served.join(", ");
- let message = match suggestion.as_deref() {
- Some(s) => format!(
- "`{requested}` isn't loaded on this embeddings server — but the same model appears to be served as `{s}`. Select `{s}` (the exact name your server reports), then save again. Available models: {served_list}."
- ),
- None => format!(
- "`{requested}` isn't loaded on this embeddings server. Select one of the loaded models (the exact name your server reports), then save again. Available models: {served_list}."
- ),
- };
- let mut body = serde_json::json!({
- "error": "EMBEDDINGS_NO_MODEL_LOADED",
- "message": message,
- "requested_model": requested,
- "available_models": served,
- });
- if let Some(s) = suggestion {
- body["suggested_model"] = serde_json::Value::String(s);
- }
- RpcOutcome::new(
- body,
- vec!["embedding model not served by endpoint — not saved".to_string()],
- )
-}
-
-pub(crate) fn resolve_api_key(config: &Config, provider_name: &str) -> String {
- let slug = if provider_name.starts_with("custom:") {
- "custom"
- } else {
- provider_name
- };
- let cred_provider = format!("embeddings:{slug}");
- let auth = AuthService::from_config(config);
- auth.get_provider_bearer_token(&cred_provider, None)
- .ok()
- .flatten()
- .unwrap_or_default()
-}
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc/served_models.rs b/crates/openhuman-core/src/inference/embeddings/rpc/served_models.rs
new file mode 100644
index 0000000000..8b83bce1ea
--- /dev/null
+++ b/crates/openhuman-core/src/inference/embeddings/rpc/served_models.rs
@@ -0,0 +1,101 @@
+//! Checking a requested embedding model against the ids an OpenAI-compatible
+//! endpoint reports on `/models`.
+
+use crate::rpc::RpcOutcome;
+
+pub(super) async fn fetch_served_model_ids(
+ endpoint: &str,
+ api_key: &str,
+) -> Result<Vec<String>, String> {
+ #[derive(serde::Deserialize)]
+ struct ModelEntry {
+ id: String,
+ }
+ #[derive(serde::Deserialize)]
+ struct ModelsResponse {
+ #[serde(default)]
+ data: Vec<ModelEntry>,
+ }
+
+ let url = format!("{}/models", endpoint.trim_end_matches('/'));
+ let client = reqwest::Client::new();
+ let mut req = client.get(&url).timeout(std::time::Duration::from_secs(5));
+ if !api_key.trim().is_empty() {
+ req = req.bearer_auth(api_key.trim());
+ }
+ let resp = req
+ .send()
+ .await
+ .map_err(|e| format!("models request failed: {e}"))?;
+ if !resp.status().is_success() {
+ return Err(format!("models request returned status {}", resp.status()));
+ }
+ let parsed: ModelsResponse = resp
+ .json()
+ .await
+ .map_err(|e| format!("models parse failed: {e}"))?;
+ Ok(parsed.data.into_iter().map(|m| m.id).collect())
+}
+
+/// Normalize an embedding model id for tolerant *suggestion* matching:
+/// lowercase, drop a leading `text-embedding-`, drop a trailing `:tag`. Used
+/// only to suggest the right served name — never to silently rewrite the id.
+pub(super) fn normalize_embed_model_id(name: &str) -> String {
+ let lower = name.trim().to_ascii_lowercase();
+ let stripped = lower.strip_prefix("text-embedding-").unwrap_or(&lower);
+ stripped.split(':').next().unwrap_or(stripped).to_string()
+}
+
+/// Decide whether the requested model is acceptable given the endpoint's served
+/// list. Returns `Some(reject)` only when the endpoint reports a non-empty list
+/// that does NOT contain the requested id — i.e. we have positive evidence the
+/// model isn't loaded. An empty/unknown list returns `None` (defer to the live
+/// test-embed probe) so we never block on a server that doesn't expose
+/// `/models` (issue #3761).
+pub(super) fn check_requested_model_served(
+ requested: &str,
+ served: &[String],
+) -> Option<RpcOutcome<serde_json::Value>> {
+ if served.is_empty() || served.iter().any(|m| m == requested) {
+ return None;
+ }
+ Some(reject_model_not_served(requested, served))
+}
+
+/// Build the "model not served" rejection: names what the endpoint actually
+/// serves and, when a normalized match exists, suggests the exact name to pick
+/// (e.g. `bge-m3` → `text-embedding-bge-m3`). Reuses the
+/// `EMBEDDINGS_NO_MODEL_LOADED` error code so the existing Embeddings setup
+/// dialog surfaces `message` and keeps the config unsaved (issue #3761).
+pub(super) fn reject_model_not_served(
+ requested: &str,
+ served: &[String],
+) -> RpcOutcome<serde_json::Value> {
+ let want = normalize_embed_model_id(requested);
+ let suggestion = served
+ .iter()
+ .find(|m| normalize_embed_model_id(m) == want)
+ .cloned();
+ let served_list = served.join(", ");
+ let message = match suggestion.as_deref() {
+ Some(s) => format!(
+ "`{requested}` isn't loaded on this embeddings server — but the same model appears to be served as `{s}`. Select `{s}` (the exact name your server reports), then save again. Available models: {served_list}."
+ ),
+ None => format!(
+ "`{requested}` isn't loaded on this embeddings server. Select one of the loaded models (the exact name your server reports), then save again. Available models: {served_list}."
+ ),
+ };
+ let mut body = serde_json::json!({
+ "error": "EMBEDDINGS_NO_MODEL_LOADED",
+ "message": message,
+ "requested_model": requested,
+ "available_models": served,
+ });
+ if let Some(s) = suggestion {
+ body["suggested_model"] = serde_json::Value::String(s);
+ }
+ RpcOutcome::new(
+ body,
+ vec!["embedding model not served by endpoint — not saved".to_string()],
+ )
+}
diff --git a/crates/openhuman-core/src/inference/embeddings/rpc_part_01.rs b/crates/openhuman-core/src/inference/embeddings/rpc/settings.rs
similarity index 56%
rename from crates/openhuman-core/src/inference/embeddings/rpc_part_01.rs
rename to crates/openhuman-core/src/inference/embeddings/rpc/settings.rs
index e86a6cf141..6f06e9d27b 100644
--- a/crates/openhuman-core/src/inference/embeddings/rpc_part_01.rs
+++ b/crates/openhuman-core/src/inference/embeddings/rpc/settings.rs
@@ -1,13 +1,14 @@
-use std::collections::HashMap;
+//! Embedding settings: reading the current configuration and applying an
+//! update, including the save-time verification of a custom endpoint.
use crate::config::Config;
-use crate::security::credentials::AuthService;
use crate::rpc::RpcOutcome;
+use crate::security::credentials::AuthService;
-use super::catalog;
-use super::factory::{create_embedding_provider_with_config, model_supports_dimensions};
-
-const LOG_PREFIX: &str = "[embeddings::rpc]";
+use super::probe::{classify_embed_probe, final_probe_dims, probe_custom_embeddings, EmbedProbe};
+use super::served_models::{check_requested_model_served, fetch_served_model_ids};
+use super::{resolve_api_key, LOG_PREFIX};
+use crate::inference::embeddings::catalog;
/// Slug naming the embedder ingestion will actually use, resolved host-side
/// from the `Config` fields the resolution ladder reads.
@@ -75,79 +76,7 @@ fn effective_embedder_slug_from_config(config: &Config) -> &'static str {
"unconfigured"
}
-/// Send one OpenAI-compatible embedding request without requesting or
-/// validating a vector width. This is intentionally separate from the live
-/// provider: a setup probe must discover a custom endpoint's native width,
-/// while a live provider must enforce the width persisted after that probe.
-async fn probe_custom_embeddings(
- endpoint: &str,
- api_key: &str,
- model: &str,
-) -> Result<Vec<Vec<f32>>, String> {
- let base = endpoint.trim_end_matches('/');
- let url = if base.ends_with("/embeddings") {
- base.to_string()
- } else if base.ends_with("/v1") {
- format!("{base}/embeddings")
- } else {
- format!("{base}/v1/embeddings")
- };
- let mut request = reqwest::Client::new()
- .post(&url)
- .json(&serde_json::json!({ "model": model, "input": ["connection test"] }));
- if !api_key.trim().is_empty() {
- request = request.bearer_auth(api_key.trim());
- }
- let response = request
- .send()
- .await
- .map_err(|e| format!("custom embeddings request to {url} failed: {e}"))?;
- let status = response.status();
- let body = response
- .text()
- .await
- .map_err(|e| format!("custom embeddings response read failed: {e}"))?;
- if !status.is_success() {
- return Err(format!("custom embeddings returned HTTP {status}: {body}"));
- }
- let data = serde_json::from_str::<serde_json::Value>(&body)
- .map_err(|e| format!("custom embeddings response was not JSON: {e}"))?
- .get("data")
- .and_then(serde_json::Value::as_array)
- .cloned()
- .ok_or_else(|| "custom embeddings response missing data array".to_string())?;
- data.into_iter()
- .map(|item| {
- item.get("embedding")
- .and_then(serde_json::Value::as_array)
- .ok_or_else(|| "custom embeddings response missing embedding array".to_string())?
- .iter()
- .map(|value| value.as_f64().map(|value| value as f32).ok_or_else(|| "custom embeddings response contains a non-numeric vector".to_string()))
- .collect()
- })
- .collect()
-}
-
-/// Dimension to persist after a successful Custom verification probe.
-///
-/// For a `text-embedding-3-*` model the endpoint honoured the requested size,
-/// so keep the user's `configured` value (Matryoshka). For every other model we
-/// probed dimension-agnostically, so adopt the endpoint's actual returned
-/// length (`actual`) — the user can't be expected to know it, and storing the
-/// real size is what lets the live embed path's length guard pass afterwards.
-/// Falls back to `configured` if the probe somehow reported a zero-length
-/// vector (defensive — `classify_embed_probe` already rejects empty vectors).
-fn final_probe_dims(model: &str, configured: usize, actual: usize) -> usize {
- if model_supports_dimensions(model) || actual == 0 {
- configured
- } else {
- actual
- }
-}
-
-fn active_custom_profile(
- config: &Config,
-) -> Option<crate::config::schema::CustomEmbeddingsConfig> {
+fn active_custom_profile(config: &Config) -> Option<crate::config::schema::CustomEmbeddingsConfig> {
let endpoint = config
.memory
.embedding_provider
@@ -163,7 +92,7 @@ fn active_custom_profile(
})
}
-fn remember_active_custom_profile(config: &mut Config) {
+pub(super) fn remember_active_custom_profile(config: &mut Config) {
if let Some(profile) = active_custom_profile(config) {
config.custom_embeddings = Some(profile);
}
@@ -335,115 +264,115 @@ pub async fn update_settings(
)
.await;
{
- // Time-box the probe so a black-hole host can't hang the RPC.
- tracing::debug!(
- provider = effective_provider.as_str(),
- "{LOG_PREFIX} update_settings verifying embeddings endpoint with a test embed"
- );
- // Normalize the timeout/result into one shape, then apply the
- // pure verification policy (`classify_embed_probe`, unit-tested).
- let outcome = match probe {
- Ok(Ok(vectors)) => EmbedProbe::Returned(vectors),
- Ok(Err(e)) => EmbedProbe::Failed(e.to_string()),
- Err(_elapsed) => EmbedProbe::TimedOut,
- };
- // Peek the actual vector length before the policy consumes the
- // outcome — on a pass this is the endpoint's real dimension.
- let probe_actual_dims = match &outcome {
- EmbedProbe::Returned(vectors) => vectors.first().map(|v| v.len()).unwrap_or(0),
- _ => 0,
- };
- if let Some(reject) = classify_embed_probe(outcome) {
- // Log the classified error code (never the raw detail — it can
- // carry endpoint response bodies) so support can distinguish
- // auth vs wrong-model vs unreachable failures (issue #5017).
- let reject_code = reject
- .value
- .get("error")
- .and_then(|v| v.as_str())
- .unwrap_or("EMBEDDINGS_VERIFICATION_FAILED");
- tracing::warn!(
+ // Time-box the probe so a black-hole host can't hang the RPC.
+ tracing::debug!(
+ provider = effective_provider.as_str(),
+ "{LOG_PREFIX} update_settings verifying embeddings endpoint with a test embed"
+ );
+ // Normalize the timeout/result into one shape, then apply the
+ // pure verification policy (`classify_embed_probe`, unit-tested).
+ let outcome = match probe {
+ Ok(Ok(vectors)) => EmbedProbe::Returned(vectors),
+ Ok(Err(e)) => EmbedProbe::Failed(e.to_string()),
+ Err(_elapsed) => EmbedProbe::TimedOut,
+ };
+ // Peek the actual vector length before the policy consumes the
+ // outcome — on a pass this is the endpoint's real dimension.
+ let probe_actual_dims = match &outcome {
+ EmbedProbe::Returned(vectors) => vectors.first().map(|v| v.len()).unwrap_or(0),
+ _ => 0,
+ };
+ if let Some(reject) = classify_embed_probe(outcome) {
+ // Log the classified error code (never the raw detail — it can
+ // carry endpoint response bodies) so support can distinguish
+ // auth vs wrong-model vs unreachable failures (issue #5017).
+ let reject_code = reject
+ .value
+ .get("error")
+ .and_then(|v| v.as_str())
+ .unwrap_or("EMBEDDINGS_VERIFICATION_FAILED");
+ tracing::warn!(
provider = effective_provider.as_str(),
reject_code,
"{LOG_PREFIX} update_settings rejected — embeddings endpoint failed verification"
);
- // Right-feedback (issue #3761): the probe failed. If the
- // endpoint lists its served models and the requested id
- // isn't among them, the cause is almost certainly a name
- // mismatch (e.g. the user entered `bge-m3` but LM Studio
- // serves `text-embedding-bge-m3`). Replace the generic
- // failure with an actionable message naming the available
- // models and the suggested match. Best-effort and only on
- // the failure path, so a passing config is never blocked by
- // an endpoint that doesn't expose `/models`. Derive the
- // endpoint from the payload OR the already-stored
- // `custom:<url>` provider, so a model-only update to an
- // existing custom endpoint still gets the guidance.
- let listed_endpoint = custom_endpoint
- .as_deref()
- .or_else(|| effective_provider.strip_prefix("custom:"));
- if let Some(ep) = listed_endpoint {
- let api_key = resolve_api_key(&config, "custom");
- tracing::debug!(
+ // Right-feedback (issue #3761): the probe failed. If the
+ // endpoint lists its served models and the requested id
+ // isn't among them, the cause is almost certainly a name
+ // mismatch (e.g. the user entered `bge-m3` but LM Studio
+ // serves `text-embedding-bge-m3`). Replace the generic
+ // failure with an actionable message naming the available
+ // models and the suggested match. Best-effort and only on
+ // the failure path, so a passing config is never blocked by
+ // an endpoint that doesn't expose `/models`. Derive the
+ // endpoint from the payload OR the already-stored
+ // `custom:<url>` provider, so a model-only update to an
+ // existing custom endpoint still gets the guidance.
+ let listed_endpoint = custom_endpoint
+ .as_deref()
+ .or_else(|| effective_provider.strip_prefix("custom:"));
+ if let Some(ep) = listed_endpoint {
+ let api_key = resolve_api_key(&config, "custom");
+ tracing::debug!(
provider = effective_provider.as_str(),
requested = new_model.as_str(),
"{LOG_PREFIX} update_settings: probing endpoint /models for served-id guidance"
);
- match fetch_served_model_ids(ep, &api_key).await {
- Ok(served) => match check_requested_model_served(&new_model, &served) {
- Some(better) => {
- tracing::warn!(
+ match fetch_served_model_ids(ep, &api_key).await {
+ Ok(served) => match check_requested_model_served(&new_model, &served) {
+ Some(better) => {
+ tracing::warn!(
provider = effective_provider.as_str(),
requested = new_model.as_str(),
served = served.len(),
"{LOG_PREFIX} update_settings: model not in served list — returning name-mismatch guidance"
);
- return Ok(better);
- }
- None => {
- tracing::debug!(
+ return Ok(better);
+ }
+ None => {
+ tracing::debug!(
provider = effective_provider.as_str(),
served = served.len(),
"{LOG_PREFIX} update_settings: requested model is served (or list empty) — keeping generic verification error"
);
- }
- },
- Err(e) => {
- tracing::debug!(
- provider = effective_provider.as_str(),
- error = %e,
- "{LOG_PREFIX} update_settings: /models lookup failed — keeping generic verification error"
- );
}
+ },
+ Err(e) => {
+ tracing::debug!(
+ provider = effective_provider.as_str(),
+ error = %e,
+ "{LOG_PREFIX} update_settings: /models lookup failed — keeping generic verification error"
+ );
}
}
- return Ok(reject);
}
- // Passed. Adopt the endpoint's real vector length for every model
- // we probed dimension-agnostically — the user can't be expected to
- // know it, and storing the actual size is what keeps the live embed
- // path's length guard from rejecting future embeds (issue #4056).
- // `text-embedding-3-*` keeps the requested size (server honoured it).
- let detected_dims = final_probe_dims(&new_model, new_dims, probe_actual_dims);
- if detected_dims != new_dims {
- tracing::info!(
+ return Ok(reject);
+ }
+ // Passed. Adopt the endpoint's real vector length for every model
+ // we probed dimension-agnostically — the user can't be expected to
+ // know it, and storing the actual size is what keeps the live embed
+ // path's length guard from rejecting future embeds (issue #4056).
+ // `text-embedding-3-*` keeps the requested size (server honoured it).
+ let detected_dims = final_probe_dims(&new_model, new_dims, probe_actual_dims);
+ if detected_dims != new_dims {
+ tracing::info!(
provider = effective_provider.as_str(),
model = new_model.as_str(),
requested = new_dims,
detected = detected_dims,
"{LOG_PREFIX} update_settings auto-detected custom embedding dimension from probe"
);
- new_dims = detected_dims;
- new_sig = format_embedding_signature(&new_provider, &new_model, new_dims);
- dims_changed = new_dims != old_dims;
- sig_changed = new_sig != old_sig;
- }
- tracing::debug!(
- provider = effective_provider.as_str(),
- new_dims,
- "{LOG_PREFIX} update_settings test embed passed — accepting config"
- );
+ new_dims = detected_dims;
+ new_sig = format_embedding_signature(&new_provider, &new_model, new_dims);
+ dims_changed = new_dims != old_dims;
+ sig_changed = new_sig != old_sig;
}
+ tracing::debug!(
+ provider = effective_provider.as_str(),
+ new_dims,
+ "{LOG_PREFIX} update_settings test embed passed — accepting config"
+ );
+ }
}
// Only require a wipe when dimensions actually change — switching
@@ -500,23 +429,18 @@ pub async fn update_settings(
if let Some(ep) = &custom_endpoint {
if new_provider == "custom" || new_provider.starts_with("custom:") {
config.memory.embedding_provider = format!("custom:{ep}");
- config.custom_embeddings =
- Some(crate::config::schema::CustomEmbeddingsConfig {
- endpoint: ep.clone(),
- model: new_model.clone(),
- dimensions: new_dims,
- });
+ config.custom_embeddings = Some(crate::config::schema::CustomEmbeddingsConfig {
+ endpoint: ep.clone(),
+ model: new_model.clone(),
+ dimensions: new_dims,
+ });
}
}
config.save().await.map_err(|e| e.to_string())?;
if sig_changed {
- crate::memory::ops::maintenance::reembed_best_effort(
- &config,
- "embedding settings",
- )
- .await;
+ crate::memory::ops::maintenance::reembed_best_effort(&config, "embedding settings").await;
}
// #5324: this is the exact screen the "embedding budget reached" alert
@@ -574,139 +498,3 @@ pub async fn update_settings(
)],
))
}
-
-/// Stores an API key for a specific embedding provider.
-pub async fn set_api_key(
- config: &Config,
- provider_slug: &str,
- api_key: &str,
-) -> Result<RpcOutcome<serde_json::Value>, String> {
- if provider_slug.is_empty() {
- return Err("provider slug is required".into());
- }
- if api_key.trim().is_empty() {
- return Err("api_key cannot be empty".into());
- }
-
- let cred_provider = format!("embeddings:{provider_slug}");
- let auth = AuthService::from_config(config);
- auth.store_provider_token(&cred_provider, "default", api_key, HashMap::new(), true)
- .map_err(|e| format!("failed to store embedding API key: {e}"))?;
-
- // #5324: supplying a BYO key does NOT change the embedding signature, so
- // `ensure_reembed_backfill` has nothing to enqueue — but it is precisely
- // the action that unblocks jobs parked on `budget_exhausted` /
- // `auth_missing`. Requeue them here or they stay dead until the user
- // separately discovers the "Retry failed" button. A store failure is
- // surfaced (not reported as `0`) so the key-stored response can't imply the
- // parked queue was recovered when it wasn't.
- let requeue_result = crate::memory::ops::maintenance::retry_failed(config).await;
- let requeued_count = *requeue_result.as_ref().unwrap_or(&0);
- let requeue_error = requeue_result.as_ref().err().cloned();
- let requeued_note = match &requeue_error {
- None => requeued_count.to_string(),
- Some(e) => format!("error ({e})"),
- };
-
- tracing::info!(
- provider = provider_slug,
- requeued = requeued_count,
- requeue_error = requeue_error.as_deref().unwrap_or(""),
- "{LOG_PREFIX} set_api_key stored"
- );
-
- Ok(RpcOutcome::new(
- serde_json::json!({ "stored": true, "provider": provider_slug, "requeued_failed_jobs": requeued_count, "requeue_error": requeue_error }),
- vec![format!(
- "embedding API key stored for {provider_slug} (requeued_failed={requeued_note})"
- )],
- ))
-}
-
-/// Removes the API key for a specific embedding provider.
-pub async fn clear_api_key(
- config: &Config,
- provider_slug: &str,
-) -> Result<RpcOutcome<serde_json::Value>, String> {
- if provider_slug.is_empty() {
- return Err("provider slug is required".into());
- }
-
- let cred_provider = format!("embeddings:{provider_slug}");
- let auth = AuthService::from_config(config);
- let removed = auth
- .remove_profile(&cred_provider, "default")
- .map_err(|e| format!("failed to clear embedding API key: {e}"))?;
-
- tracing::info!(
- provider = provider_slug,
- removed,
- "{LOG_PREFIX} clear_api_key"
- );
-
- Ok(RpcOutcome::new(
- serde_json::json!({ "cleared": removed, "provider": provider_slug }),
- vec![format!("embedding API key cleared for {provider_slug}")],
- ))
-}
-
-/// Generates embeddings for the given input texts using the currently
-/// configured provider.
-pub async fn embed(
- config: &Config,
- inputs: &[String],
-) -> Result<RpcOutcome<serde_json::Value>, String> {
- let provider_name = &config.memory.embedding_provider;
- let model = &config.memory.embedding_model;
- let dims = config.memory.embedding_dimensions;
-
- let api_key = resolve_api_key(config, provider_name);
-
- let custom_endpoint = if provider_name.starts_with("custom:") {
- provider_name
- .strip_prefix("custom:")
- .map(|s: &str| s.to_string())
- } else {
- None
- };
-
- let provider_slug = if provider_name.starts_with("custom:") {
- "custom"
- } else {
- provider_name.as_str()
- };
-
- let embedder = create_embedding_provider_with_config(
- config,
- provider_slug,
- model,
- dims,
- &api_key,
- custom_endpoint.as_deref(),
- )
- .map_err(|e| e.to_string())?;
-
- let refs: Vec<&str> = inputs.iter().map(|s| s.as_str()).collect();
- let vectors = embedder.embed(&refs).await.map_err(|e| e.to_string())?;
-
- let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);
-
- tracing::debug!(
- provider = provider_slug,
- model,
- input_count = inputs.len(),
- vector_count = vectors.len(),
- dims = actual_dims,
- "{LOG_PREFIX} embed completed"
- );
-
- let payload = serde_json::json!({
- "vectors": vectors,
- "dimensions": actual_dims,
- "count": vectors.len(),
- "provider": provider_slug,
- "model": model,
- });
-
- Ok(RpcOutcome::new(payload, vec!["embedding completed".into()]))
-}