Skip to content

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

feat(inference/failure_copy): add localized failure handling

Steven Enamakel7c8be0be89f8
diff --git a/crates/openhuman-core/src/inference/failure_copy/localized.rs b/crates/openhuman-core/src/inference/failure_copy/localized.rs
new file mode 100644
index 0000000000..72dbf60f7b
--- /dev/null
+++ b/crates/openhuman-core/src/inference/failure_copy/localized.rs
@@ -0,0 +1,78 @@
+//! Rendering of classified native-channel failures in the configured UI locale.
+
+use serde_json::Value;
+
+const QUEUED_FAILURE_PREFIX: &str = "queued turn was not run: ";
+
+pub(crate) fn supported_channel_locale(locale: Option<&str>) -> Option<&'static str> {
+ let locale = locale?.trim();
+ chat_error_catalog::SUPPORTED_LOCALES
+ .iter()
+ .copied()
+ .find(|supported| *supported == locale)
+}
+
+/// Render known chat-error keys from the shared locale catalog. Unknown keys
+/// retain the English message supplied by the host classifier.
+pub(crate) fn render_channel_failure(
+ locale: &str,
+ fallback_message: &str,
+ copy_key: Option<&str>,
+ copy_params: Option<&Value>,
+) -> String {
+ let queued = fallback_message
+ .strip_prefix(QUEUED_FAILURE_PREFIX)
+ .is_some();
+ let locale = supported_channel_locale(Some(locale)).unwrap_or("en");
+ let Some(key) = copy_key.filter(|key| key.starts_with("chat_error.")) else {
+ return fallback_message.to_owned();
+ };
+ let Some(base) = chat_error_catalog::translation(locale, key) else {
+ return fallback_message.to_owned();
+ };
+
+ let mut message = base.to_owned();
+ if let Some(seconds) = copy_params
+ .and_then(|params| params.get("retry_after_secs"))
+ .and_then(Value::as_u64)
+ {
+ let retry_key = match seconds {
+ 0 => "chat_error.retryHint.immediately",
+ 1 => "chat_error.retryHint.oneSecond",
+ 2..=89 => "chat_error.retryHint.seconds",
+ _ => {
+ let minutes = seconds / 60 + u64::from(seconds % 60 != 0);
+ if minutes == 1 {
+ "chat_error.retryHint.aboutMinute"
+ } else {
+ "chat_error.retryHint.aboutMinutes"
+ }
+ }
+ };
+ if let Some(retry) = chat_error_catalog::translation(locale, retry_key) {
+ let rendered = if retry_key.ends_with("seconds") {
+ retry.replace("{n}", &seconds.to_string())
+ } else if retry_key.ends_with("aboutMinutes") {
+ let minutes = seconds / 60 + u64::from(seconds % 60 != 0);
+ retry.replace("{n}", &minutes.to_string())
+ } else {
+ retry.to_owned()
+ };
+ message.push(' ');
+ message.push_str(&rendered);
+ }
+ }
+
+ if let Some(detail) = copy_params
+ .and_then(|params| params.get("detail"))
+ .and_then(Value::as_str)
+ .filter(|detail| !detail.is_empty())
+ {
+ message.push_str("\n\n> ");
+ message.push_str(detail);
+ }
+ if queued {
+ message.insert_str(0, QUEUED_FAILURE_PREFIX);
+ }
+ message
+}
diff --git a/crates/openhuman-core/src/inference/failure_copy/localized_tests.rs b/crates/openhuman-core/src/inference/failure_copy/localized_tests.rs
new file mode 100644
index 0000000000..2720d8e212
--- /dev/null
+++ b/crates/openhuman-core/src/inference/failure_copy/localized_tests.rs
@@ -0,0 +1,55 @@
+use super::*;
+
+#[test]
+fn native_failure_copy_uses_supported_locale_and_interpolates_retry_delay() {
+ let rendered = render_channel_failure(
+ "zh-CN",
+ "Your AI provider is rate-limiting requests. Retry in 12 seconds.",
+ Some("chat_error.rate_limited"),
+ Some(&serde_json::json!({ "retry_after_secs": 12 })),
+ );
+
+ assert!(rendered.contains("你的 AI 提供商"));
+ assert!(rendered.contains("12"));
+ assert!(!rendered.contains("Your AI provider"));
+}
+
+#[test]
+fn native_failure_copy_preserves_queued_prefix_and_sanitized_detail() {
+ let rendered = render_channel_failure(
+ "fr",
+ "queued turn was not run: authentication failed\n\n> sanitized provider detail",
+ Some("chat_error.auth_error"),
+ Some(&serde_json::json!({ "detail": "sanitized provider detail" })),
+ );
+
+ assert!(rendered.starts_with("queued turn was not run: "));
+ assert!(rendered.contains("problème d’authentification"));
+ assert!(rendered.ends_with("> sanitized provider detail"));
+ assert!(!rendered.contains("authentication failed"));
+}
+
+#[test]
+fn unknown_locale_key_and_parameter_types_fall_back_safely() {
+ assert_eq!(supported_channel_locale(None), None);
+ assert_eq!(supported_channel_locale(Some(" xx ")), None);
+ assert_eq!(supported_channel_locale(Some(" zh-CN ")), Some("zh-CN"));
+
+ let english = render_channel_failure(
+ "xx",
+ "safe English fallback",
+ Some("chat_error.rate_limited"),
+ Some(&serde_json::json!({ "retry_after_secs": "not-a-number", "detail": 4 })),
+ );
+ assert_eq!(english, chat_error_catalog::translation("en", "chat_error.rate_limited").unwrap());
+ assert!(!english.contains("not-a-number"));
+ assert!(!english.contains('4'));
+
+ let unknown = render_channel_failure(
+ "fr",
+ "safe English fallback",
+ Some("chat_error.from_a_newer_core"),
+ None,
+ );
+ assert_eq!(unknown, "safe English fallback");
+}
diff --git a/crates/openhuman-core/src/inference/failure_copy/mod.rs b/crates/openhuman-core/src/inference/failure_copy/mod.rs
index 14975ab319..d155963847 100644
--- a/crates/openhuman-core/src/inference/failure_copy/mod.rs
+++ b/crates/openhuman-core/src/inference/failure_copy/mod.rs
@@ -11,6 +11,8 @@
//! `tinyinference`; deciding *which class an error is* stays in the callers.
mod halt;
+mod chat_error_catalog;
+mod localized;
mod table;
#[cfg(test)]
@@ -19,4 +21,9 @@ pub(crate) use halt::{
recoverable_identical_halt_summary, recoverable_no_progress_halt_summary,
terminal_inference_failure_kind, terminal_inference_halt_summary, user_actionable_escalation,
};
+pub(crate) use localized::{render_channel_failure, supported_channel_locale};
pub(crate) use table::{failure_copy, FailureClass};
+
+#[cfg(test)]
+#[path = "localized_tests.rs"]
+mod localized_tests;