use rho_protocol::{AnswerStop, TurnError, TurnErrorKind, TurnOutcome};
use rho_sdk::{RunOutcome, StopReason};
use crate::{app::session_runtime::CommandRejected, config::Config};
pub(crate) fn finished(outcome: &RunOutcome) -> TurnOutcome {
let text = outcome.text().to_owned();
let answered = |stop| TurnOutcome::Answered {
text: text.clone(),
stop,
};
match outcome.stop_reason() {
StopReason::MaxSteps => TurnOutcome::MaxSteps { text: text.clone() },
StopReason::EndTurn => answered(AnswerStop::EndTurn),
StopReason::MaxTokens => answered(AnswerStop::MaxTokens),
StopReason::Refusal => answered(AnswerStop::Refusal),
_ => answered(AnswerStop::Unknown),
}
}
pub(crate) fn turn_error(error: &anyhow::Error, model: &Config) -> TurnError {
let kind = failure_kind(error);
let message = match kind {
TurnErrorKind::Authentication => authentication_message(&model.provider, &model.auth),
TurnErrorKind::Provider => provider_message(model, &chain_message(error)),
_ => chain_message(error),
};
TurnError { kind, message }
}
fn provider_message(model: &Config, message: &str) -> String {
let provider = &model.provider;
let summarizer = crate::compaction::CompactionConfig::from(model).summarizer;
if summarizer.is_some_and(|summarizer| summarizer.provider != *provider) {
return message.to_owned();
}
match model.resolved_provider_endpoint(provider) {
Some(base) => format!("{provider} at {base}: {message}"),
None => format!("{provider}: {message}"),
}
}
pub(crate) fn diagnostic_notice(error: &anyhow::Error) -> Option<String> {
if failure_kind(error) == TurnErrorKind::Authentication {
return None;
}
let diagnostic = provider_diagnostic(error)?;
Some(format!("provider diagnostic:\n{diagnostic}"))
}
pub(crate) fn withheld_diagnostic(error: &anyhow::Error) -> Option<&str> {
(failure_kind(error) == TurnErrorKind::Authentication)
.then(|| provider_diagnostic(error))
.flatten()
}
fn provider_diagnostic(error: &anyhow::Error) -> Option<&str> {
error.chain().find_map(|cause| match cause.downcast_ref() {
Some(rho_sdk::Error::Provider(provider)) => provider.diagnostic(),
_ => None,
})
}
fn chain_message(error: &anyhow::Error) -> String {
let mut message = String::new();
for cause in error.chain() {
let text = cause.to_string();
if message.ends_with(&text) {
continue;
}
if !message.is_empty() {
message.push_str(": ");
}
message.push_str(&text);
}
message
}
fn failure_kind(error: &anyhow::Error) -> TurnErrorKind {
for cause in error.chain() {
if cause.downcast_ref::<CommandRejected>().is_some() {
return TurnErrorKind::Configuration;
}
if let Some(error) = cause.downcast_ref::<rho_sdk::Error>() {
return match error {
rho_sdk::Error::Authentication { .. } => TurnErrorKind::Authentication,
rho_sdk::Error::Provider(provider)
if provider.kind() == rho_sdk::ProviderErrorKind::Authentication =>
{
TurnErrorKind::Authentication
}
rho_sdk::Error::Provider(_) => TurnErrorKind::Provider,
rho_sdk::Error::Tool(_) => TurnErrorKind::ToolHost,
rho_sdk::Error::InvalidConfiguration { .. } => TurnErrorKind::Configuration,
_ => TurnErrorKind::Other,
};
}
if let Some(error) = cause.downcast_ref::<rho_providers::model::ModelError>() {
use rho_providers::model::ModelError;
return match error {
ModelError::MissingCredentials(_) | ModelError::Credentials(_) => {
TurnErrorKind::Authentication
}
ModelError::UnsupportedReasoning { .. } | ModelError::UnsupportedProvider(_) => {
TurnErrorKind::Configuration
}
_ => TurnErrorKind::Provider,
};
}
}
TurnErrorKind::Other
}
fn authentication_message(provider: &str, auth: &str) -> String {
let alternatives = rho_providers::provider::provider_descriptor(provider)
.into_iter()
.flat_map(|descriptor| descriptor.auth_modes())
.filter(|mode| {
mode.id != auth
&& !matches!(
mode.auth_kind,
rho_providers::provider::ProviderAuthKind::None
)
})
.map(|mode| format!("--auth {}", mode.id))
.collect::<Vec<_>>();
let mut message =
format!("authentication failed for {provider} (auth: {auth}); run /login {provider}");
if !alternatives.is_empty() {
message.push_str(" or pass ");
message.push_str(&alternatives.join(" or "));
}
message
}
#[cfg(test)]
#[path = "outcome_tests.rs"]
mod tests;