use std::{future::IntoFuture, marker::PhantomData};
use schemars::{JsonSchema, schema_for};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use rig_core::structured_output;
use rig_core::wasm_compat::{WasmBoxedFuture, WasmCompatSend};
use tracing_futures::Instrument;
use super::{Agent, runner::AgentRunner};
use crate::{
completion::{Message, StructuredOutputError, Usage},
run::response::{CompletionCall, PromptResponse},
run::{OutputMode, spec::UnhandledInvalidToolCall},
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypedPromptResponse<T> {
pub output: T,
pub usage: Usage,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub completion_calls: Vec<CompletionCall>,
#[serde(default)]
pub messages: Vec<Message>,
}
impl<T> TypedPromptResponse<T> {
pub fn completion_calls(&self) -> &[CompletionCall] {
&self.completion_calls
}
pub fn requests(&self) -> usize {
self.completion_calls.len()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OutputSource {
Native,
OutputTool,
}
#[derive(Debug)]
pub struct TypedOutput<T> {
source: OutputSource,
retries: usize,
_t: PhantomData<fn() -> T>,
}
pub type TypedRun<T> = AgentRunner<TypedOutput<T>>;
impl<T> TypedRun<T>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend,
{
pub(crate) fn native(agent: &Agent, prompt: impl Into<Message>) -> Self {
let mut runner = AgentRunner::from_agent(agent, prompt);
runner.config.spec_mut().output_schema = Some(schema_for!(T));
runner.config.spec_mut().output_mode = OutputMode::Native;
Self::from_runner(runner, OutputSource::Native)
}
pub(crate) fn output_tool(runner: AgentRunner) -> Self {
Self::from_runner(
runner.unhandled_invalid_tool_call(UnhandledInvalidToolCall::Ignore),
OutputSource::OutputTool,
)
}
fn from_runner(runner: AgentRunner, source: OutputSource) -> Self {
let output = TypedOutput {
source,
retries: 0,
_t: PhantomData,
};
runner.replace_output(output).0
}
pub fn retries(mut self, retries: usize) -> Self {
self.output.retries = retries;
self
}
async fn send(
self,
ambient: tracing::Span,
) -> Result<TypedPromptResponse<T>, StructuredOutputError> {
let (
runner,
TypedOutput {
source, retries, ..
},
) = self.replace_output(());
let mut usage = Usage::default();
let mut last_error = None;
for attempt in 0..=retries {
if retries > 0 {
tracing::debug!(
"Attempting to extract structured output. Retries left: {}",
retries - attempt
);
}
let (result, attempt_usage) =
runner.clone().run_with_error_usage(ambient.clone()).await;
if attempt_usage != Usage::default() {
usage += attempt_usage;
}
let outcome = match result {
Ok(response) => {
recover_output(&response, source).map(|output| TypedPromptResponse {
output,
usage,
completion_calls: response.completion_calls,
messages: response.messages,
})
}
Err(err) => Err(StructuredOutputError::Prompt(err)),
};
match outcome {
Ok(response) => return Ok(response),
Err(err) => {
if attempt < retries {
tracing::warn!(
"Attempt {attempt} to extract structured output failed: {err:?}. Retrying..."
);
}
last_error = Some(err);
}
}
}
Err(last_error.unwrap_or(StructuredOutputError::EmptyResponse))
}
}
fn recover_output<T: DeserializeOwned>(
response: &PromptResponse,
source: OutputSource,
) -> Result<T, StructuredOutputError> {
let output = response.output();
match source {
OutputSource::Native => {
if output.is_empty() {
return Err(StructuredOutputError::EmptyResponse);
}
structured_output::json_in_text(&output)
.map_err(|error| deserialization_error(&output, error))
}
OutputSource::OutputTool => {
let submissions = response.output_tool_calls();
if submissions == 0
&& let Ok(value) = serde_json::from_str::<T>(output.trim())
{
return Ok(value);
}
if submissions == 0 {
tracing::warn!(
"The submit tool was not called. If this happens more than once, please ensure the model you are using is powerful enough to reliably call tools."
);
return Err(StructuredOutputError::EmptyResponse);
}
if submissions > 1 {
tracing::warn!(
"Multiple submit calls detected, using the first one. Providers / agents should only ensure one submit call."
);
}
serde_json::from_str(&output).map_err(|error| deserialization_error(&output, error))
}
}
}
fn deserialization_error(output: &str, error: serde_json::Error) -> StructuredOutputError {
StructuredOutputError::Deserialization {
output: output.to_string(),
error,
}
}
impl<T> IntoFuture for TypedRun<T>
where
T: JsonSchema + DeserializeOwned + WasmCompatSend + 'static,
{
type Output = Result<TypedPromptResponse<T>, StructuredOutputError>;
type IntoFuture = WasmBoxedFuture<'static, Self::Output>;
fn into_future(self) -> Self::IntoFuture {
let ambient = tracing::Span::current();
let run_under = ambient.clone();
Box::pin(self.send(run_under).instrument(ambient))
}
}