use rig_core::completion::{FinishReason, ResponseIdentity, Usage};
use rig_core::error::ProviderError;
use rig_core::message::{AssistantContent, Message};
use rig_core::transcript::assistant_text_from_choice;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompletionCall {
pub call_index: usize,
pub usage: Usage,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub response_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_request_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub finish_reason: Option<FinishReason>,
pub raw: serde_json::Value,
}
impl CompletionCall {
pub fn new(call_index: usize, usage: Usage, raw: serde_json::Value) -> Self {
Self {
call_index,
usage,
response_id: None,
provider_request_id: None,
finish_reason: None,
raw,
}
}
pub fn with_identity(mut self, identity: ResponseIdentity) -> Self {
self.response_id = identity.response_id;
self.provider_request_id = identity.provider_request_id;
self
}
pub fn with_finish_reason(mut self, finish_reason: Option<FinishReason>) -> Self {
self.finish_reason = finish_reason;
self
}
pub fn identity(&self) -> ResponseIdentity {
ResponseIdentity {
response_id: self.response_id.clone(),
provider_request_id: self.provider_request_id.clone(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptResponse {
pub usage: Usage,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub completion_calls: Vec<CompletionCall>,
#[serde(default)]
pub messages: Vec<Message>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub memory_append: Option<MemoryAppend>,
pub content: Vec<AssistantContent>,
#[serde(skip)]
output_tool_calls: usize,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum MemoryAppend {
Acknowledged,
Failed {
report: rig_core::error::ErrorReport,
},
}
impl MemoryAppend {
pub fn is_acknowledged(&self) -> bool {
matches!(self, Self::Acknowledged)
}
pub fn failure(&self) -> Option<&rig_core::error::ErrorReport> {
match self {
Self::Acknowledged => None,
Self::Failed { report } => Some(report),
}
}
}
impl std::fmt::Display for PromptResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.output().fmt(f)
}
}
impl PromptResponse {
pub fn new(output: impl Into<String>, usage: Usage) -> Self {
Self::from_content(vec![AssistantContent::text(output)], usage)
}
pub(crate) fn from_content(content: Vec<AssistantContent>, usage: Usage) -> Self {
Self {
content,
usage,
completion_calls: Vec::new(),
messages: Vec::new(),
memory_append: None,
output_tool_calls: 0,
}
}
pub fn empty() -> Self {
Self::new(String::new(), Usage::default())
}
pub fn with_messages(mut self, messages: Vec<Message>) -> Self {
self.messages = messages;
self
}
pub fn with_completion_calls(mut self, completion_calls: Vec<CompletionCall>) -> Self {
self.completion_calls = completion_calls;
self
}
pub fn with_memory_append(mut self, memory_append: Option<MemoryAppend>) -> Self {
self.memory_append = memory_append;
self
}
pub(crate) fn with_output_tool_calls(mut self, count: usize) -> Self {
self.output_tool_calls = count;
self
}
pub fn output_tool_calls(&self) -> usize {
self.output_tool_calls
}
pub fn output(&self) -> String {
assistant_text_from_choice(&self.content)
}
pub fn usage(&self) -> Usage {
self.usage
}
pub fn messages(&self) -> &[Message] {
&self.messages
}
pub fn memory_append(&self) -> Option<&MemoryAppend> {
self.memory_append.as_ref()
}
pub fn content(&self) -> &[AssistantContent] {
&self.content
}
pub fn completion_calls(&self) -> &[CompletionCall] {
&self.completion_calls
}
pub fn requests(&self) -> usize {
self.completion_calls.len()
}
}
pub(crate) fn finalize_output_tool_choice(
choice: &[AssistantContent],
output: &str,
) -> Option<Vec<AssistantContent>> {
let finalized_via_output_tool = choice
.iter()
.any(|item| matches!(item, AssistantContent::ToolCall(_)));
if !finalized_via_output_tool {
return None;
}
let mut items: Vec<AssistantContent> = choice
.iter()
.filter(|item| {
!matches!(
item,
AssistantContent::ToolCall(_) | AssistantContent::Text(_)
)
})
.cloned()
.collect();
items.push(AssistantContent::text(output.to_string()));
Some(items)
}
use thiserror::Error;
use rig_core::memory::MemoryError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PromptError {
#[error(transparent)]
Provider(ProviderError),
#[error(transparent)]
Report(#[from] rig_core::error::ErrorReport),
#[error(transparent)]
Memory(#[from] MemoryError),
#[error("reached the max turns limit of {max_turns}")]
MaxTurns {
max_turns: usize,
chat_history: Vec<Message>,
prompt: Message,
},
#[error("the run was cancelled: {reason}")]
Cancelled {
chat_history: Vec<Message>,
reason: String,
},
#[error(
"model attempted to call unknown or disallowed tool `{tool_name}`. Available tools: {available_tools:?}. Allowed tools for this turn: {allowed_tools:?}"
)]
UnknownToolCall {
tool_name: String,
available_tools: Vec<String>,
allowed_tools: Vec<String>,
chat_history: Vec<Message>,
},
}
impl From<ProviderError> for PromptError {
fn from(error: ProviderError) -> Self {
match error {
ProviderError::Relayed(report) => Self::Report(*report),
error => Self::Provider(error),
}
}
}
macro_rules! forward_provider_response_helpers {
($err:ident, $variant:ident, $inner:literal $(, report = $report:ident)?) => {
impl $err {
#[doc = concat!("Returns the provider response body exposed by a wrapped ", $inner, ".")]
pub fn provider_response_body(&self) -> Option<&str> {
match self {
Self::$variant(error) => error.provider_response_body(),
$(Self::$report(report) => report.provider_response_body(),)?
_ => None,
}
}
#[doc = concat!("Parses the provider response body of a wrapped ", $inner, " as JSON when present.")]
pub fn provider_response_json(
&self,
) -> Result<Option<serde_json::Value>, serde_json::Error> {
match self {
Self::$variant(error) => error.provider_response_json(),
$(Self::$report(report) => report.provider_response_json(),)?
_ => Ok(None),
}
}
#[doc = concat!("Returns the provider transport request id exposed by a wrapped ", $inner, ", or carried by a wire report.")]
pub fn provider_request_id(&self) -> Option<&str> {
match self {
Self::$variant(error) => error.provider_request_id(),
$(Self::$report(report) => report.request_id.as_deref(),)?
_ => None,
}
}
#[doc = concat!("Returns the HTTP status exposed by a wrapped ", $inner, ", or carried by a wire report.")]
pub fn provider_response_status(&self) -> Option<http::StatusCode> {
match self {
Self::$variant(error) => error.provider_response_status(),
$(Self::$report(report) => report
.http_status
.and_then(|status| http::StatusCode::from_u16(status).ok()),)?
_ => None,
}
}
#[doc = concat!("Returns the response headers exposed by a wrapped ", $inner, " or report.")]
pub fn provider_response_headers(&self) -> Option<&http::HeaderMap> {
match self {
Self::$variant(error) => error.provider_response_headers(),
$(Self::$report(report) => report.provider_response_headers(),)?
_ => None,
}
}
}
};
}
pub(crate) use forward_provider_response_helpers;
forward_provider_response_helpers!(PromptError, Provider, "completion error", report = Report);
impl PromptError {
pub fn cancelled(
chat_history: impl IntoIterator<Item = Message>,
reason: impl Into<String>,
) -> Self {
Self::Cancelled {
chat_history: chat_history.into_iter().collect(),
reason: reason.into(),
}
}
}