Skip to content

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

fix(web_errors): rename budget module and update error code references

Steven Enamakelb1cc96d705e8
diff --git a/crates/openhuman-core/src/web_chat/web_errors/backend_error_code.rs b/crates/openhuman-core/src/web_chat/web_errors/backend_error_code.rs
index 447819d0c5..efdb27402f 100644
--- a/crates/openhuman-core/src/web_chat/web_errors/backend_error_code.rs
+++ b/crates/openhuman-core/src/web_chat/web_errors/backend_error_code.rs
@@ -3,7 +3,6 @@
//! [`classify_inference_error`](super::classify::classify_inference_error).
use super::classify::ClassifiedError;
-use super::response_predicates::malformed_history_user_message;
use super::retry::retry_after_hint;
use tinyinference_llm::failure::{is_malformed_tool_history_text, parse_retry_after_secs};
@@ -49,71 +48,41 @@ pub(super) fn classify_by_backend_error_code(
provider,
);
+ use crate::inference::failure_copy::{failure_copy, FailureClass as C};
+ use super::classify::{classified, classified_plain};
+
let classified = match code {
BackendErrorCode::RateLimited => {
let retry_secs = parse_retry_after_secs(err);
ClassifiedError {
- error_type: "rate_limited",
- message: format!(
- "Your AI provider is rate-limiting requests. You can retry in this thread.{}",
- retry_after_hint(retry_secs)
- ),
- source: "provider",
- retryable: true,
retry_after_ms: retry_secs.map(|s| s.saturating_mul(1000)),
- provider,
- fallback_available,
+ ..classified(
+ C::ManagedRateLimited,
+ format!(
+ "{}{}",
+ failure_copy(C::ManagedRateLimited).copy,
+ retry_after_hint(retry_secs)
+ ),
+ provider,
+ fallback_available,
+ )
}
}
- BackendErrorCode::UserInsufficientCredits => ClassifiedError {
- error_type: "budget_exhausted",
- message: "You're out of credits. Top up, or switch to 'Use Your Own Models' \
- in Settings."
- .to_string(),
- source: "openhuman_billing",
- retryable: false,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- },
+ BackendErrorCode::UserInsufficientCredits => {
+ classified_plain(C::ManagedBudgetExhausted, provider, None)
+ }
// Operator fault (our key/account/quota/5xx) OR operator registry /
// routing misconfig — NOT user-actionable. Both route to the same
// calm "we've been notified" copy; the backend already paged. We
// deliberately DROP the "check your API key" (F4) and "pick a
// different model" (F6) copy the BYO substring arms would emit.
BackendErrorCode::UpstreamUnavailable | BackendErrorCode::ModelUnavailable => {
- ClassifiedError {
- error_type: "provider_error",
- message: "The AI service is temporarily unavailable — we've been notified. \
- Please try again shortly."
- .to_string(),
- source: "provider",
- retryable: true,
- retry_after_ms: None,
- provider,
- fallback_available,
- }
+ classified_plain(C::ManagedUnavailable, provider, fallback_available)
+ }
+ BackendErrorCode::PayloadTooLarge => classified_plain(C::PayloadTooLarge, provider, None),
+ BackendErrorCode::ContextLengthExceeded => {
+ classified_plain(C::ContextOverflow, provider, None)
}
- BackendErrorCode::PayloadTooLarge => ClassifiedError {
- error_type: "payload_too_large",
- message: "Your message or attachment is too large for this model. Shorten it \
- or remove the attachment — or start a new thread."
- .to_string(),
- source: "config",
- retryable: false,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- },
- BackendErrorCode::ContextLengthExceeded => ClassifiedError {
- error_type: "context_overflow",
- message: "The conversation is too long. Please start a new chat.".to_string(),
- source: "config",
- retryable: false,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- },
BackendErrorCode::BadRequest => {
// Same code, three shapes. FIRST: a tool-ordering rejection
// (`validateToolMessageOrdering` — an orphaned `role:'tool'` message
@@ -123,15 +92,7 @@ pub(super) fn classify_by_backend_error_code(
// is built, so the next turn cold-boots clean — tell the user
// exactly that (and mark retryable, because resending now works).
if is_malformed_tool_history_text(&err.to_lowercase()) {
- ClassifiedError {
- error_type: "provider_request_rejected",
- message: malformed_history_user_message().to_string(),
- source: "provider",
- retryable: true,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- }
+ classified_plain(C::MalformedHistory, provider, None)
// Else two shapes (B8/F8): a backend-flagged *malformed*
// payload is a client bug (the request was built wrong — it pages
// Sentry at the FE layer, gated elsewhere), while a plain
@@ -139,42 +100,16 @@ pub(super) fn classify_by_backend_error_code(
// fix. The copy differs: don't tell the user to abandon the thread
// for a one-off malformation (only this turn failed).
} else if body_flags_malformed(err) {
- ClassifiedError {
- error_type: "provider_request_rejected",
- message: "Something went wrong with this message. Try rephrasing it — \
- or start a new thread if it keeps happening."
- .to_string(),
- source: "provider",
- retryable: false,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- }
+ classified_plain(C::ManagedMalformedRequest, provider, None)
} else {
- ClassifiedError {
- error_type: "provider_request_rejected",
- message: "The request was rejected — usually a model or parameter \
- mismatch. Try a different model in Connections → API keys → LLM."
- .to_string(),
- source: "provider",
- retryable: false,
- retry_after_ms: None,
- provider,
- fallback_available: None,
- }
+ classified_plain(C::ManagedRequestRejected, provider, None)
}
}
- BackendErrorCode::InternalError => ClassifiedError {
- error_type: "inference",
- // Backend already paged its own 500; the FE must not double-report
- // (gated in the Sentry classifier) and the user just retries.
- message: "Something went wrong — we've been notified. Please try again.".to_string(),
- source: "provider",
- retryable: true,
- retry_after_ms: None,
- provider,
- fallback_available,
- },
+ // Backend already paged its own 500; the FE must not double-report
+ // (gated in the Sentry classifier) and the user just retries.
+ BackendErrorCode::InternalError => {
+ classified_plain(C::ManagedInternal, provider, fallback_available)
+ }
};
Some(classified)
diff --git a/crates/openhuman-core/src/web_chat/web_errors/budget.rs b/crates/openhuman-core/src/web_chat/web_errors/budget.rs
index c8a745bfb6..8bab4a48d0 100644
--- a/crates/openhuman-core/src/web_chat/web_errors/budget.rs
+++ b/crates/openhuman-core/src/web_chat/web_errors/budget.rs
@@ -1,56 +1,30 @@
-//! User-facing copy and detection for OpenHuman's own budget/quota limits —
-//! inference credits and the SecurityPolicy per-hour action-budget cap.
+//! Detection and user-facing accessors for OpenHuman's own budget/quota limits —
+//! inference credits and the SecurityPolicy per-hour action-budget cap. The
+//! copy lives in the host copy table; the phrase matching in
+//! `tinyinference_providers::billing`.
-use once_cell::sync::Lazy;
-use regex::Regex;
-
-static BUDGET_ERROR_NORMALIZE_RE: Lazy<Regex> =
- Lazy::new(|| Regex::new(r"[-_\s]+").expect("budget normalize regex"));
-static BUDGET_ERROR_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
- vec![
- Regex::new(r"budget.*exceed").expect("budget exceeded regex"),
- Regex::new(r"top up").expect("top up regex"),
- Regex::new(r"add.*credits").expect("add credits regex"),
- Regex::new(r"out of credits").expect("out of credits regex"),
- Regex::new(r"no remaining credits").expect("no remaining credits regex"),
- ]
-});
+use crate::inference::failure_copy::{failure_copy, FailureClass};
+use tinyinference_providers::{is_budget_message, BudgetMatch};
+/// Whether an error string signals an exhausted inference budget, read with the
+/// chat surface's [`BudgetMatch::Managed`] strictness: the managed no-credits
+/// 400 ("Insufficient budget" / "Insufficient balance") plus the looser
+/// top-up / out-of-credits wording, so the user gets the actionable budget copy
+/// instead of the generic apology (#3088).
pub(crate) fn is_inference_budget_exceeded_error(message: &str) -> bool {
- let normalized = BUDGET_ERROR_NORMALIZE_RE
- .replace_all(&message.trim().to_ascii_lowercase(), " ")
- .into_owned();
- if BUDGET_ERROR_PATTERNS
- .iter()
- .any(|pattern| pattern.is_match(&normalized))
- {
- return true;
- }
- // Align with the canonical OpenHuman-backend budget detector
- // (`billing_error::is_budget_exhausted_message`) so the managed
- // no-credits response — a 400 carrying "Insufficient budget" /
- // "Insufficient balance" — surfaces the actionable budget message
- // below instead of the generic "Something went wrong" apology
- // (issue #3088). Without this, an Ollama user with zero credits and
- // routing still on Managed sees an opaque "provider error" and has no
- // way to self-diagnose that they must top up or switch routing.
- tinyinference_providers::is_budget_exhausted_message(message)
+ is_budget_message(message, BudgetMatch::Managed)
}
pub(crate) fn inference_budget_exceeded_user_message() -> &'static str {
- // Keep the literal "top up" / "credits" tokens (asserted by
- // `budget_exceeded_copy_mentions_top_up`) and add the self-diagnosis
- // path for issue #3088: a user who enabled a local model but left
- // routing on Managed needs to know they can switch to their own model
- // rather than being stuck. We guide, never auto-switch — the user's
- // routing choice in Settings is respected.
- "You're out of credits, so I can't run the managed (cloud) model right now. \
- You can top up your credits or pick a plan to continue — or, if you've enabled a \
- local model like Ollama, switch routing to \"Use Your Own Models\" in Connections → API keys → LLM."
+ // Keeps the literal "top up" / "credits" tokens (asserted by
+ // `budget_exceeded_copy_mentions_top_up`) and the self-diagnosis path for
+ // #3088. We guide, never auto-switch — the user's routing choice in
+ // Settings is respected.
+ failure_copy(FailureClass::BudgetExhausted).copy
}
pub(crate) fn generic_inference_error_user_message() -> &'static str {
- "Something went wrong. Please try again."
+ failure_copy(FailureClass::Inference).copy
}
/// Detect the SecurityPolicy global hourly action-budget signal