use std::future::Future;
use aidaemon_core::{
Agent, CancellationToken, CoreError, Hook, HookContext, Message, ModelRequest, ModelResponse,
Provider, ProviderError, Resolution, ResponseFormat, Role, RunOutcome, RunState, RunStatus,
Usage,
};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct Typed<T> {
pub value: T,
pub response: ModelResponse,
pub usage: Usage,
pub repaired: bool,
}
#[derive(Debug, Clone)]
pub struct TypedOutcome<T> {
pub value: T,
pub outcome: RunOutcome,
pub repaired: bool,
}
#[derive(Debug, thiserror::Error)]
pub enum StructuredError {
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
Core(#[from] CoreError),
#[error("the run ended without a final answer: {:?}", .0.status)]
NotFinished(Box<RunOutcome>),
#[error("the answer is not valid {type_name}: {error}")]
Invalid {
type_name: &'static str,
error: String,
text: String,
outcome: Option<Box<RunOutcome>>,
},
}
pub fn parse_answer<T: DeserializeOwned>(text: &str) -> Result<T, String> {
let trimmed = text.trim();
let first = match serde_json::from_str::<T>(trimmed) {
Ok(value) => return Ok(value),
Err(err) => err.to_string(),
};
for candidate in [fenced(trimmed), braced(trimmed)].into_iter().flatten() {
if let Ok(value) = serde_json::from_str::<T>(candidate) {
return Ok(value);
}
}
Err(first)
}
fn fenced(text: &str) -> Option<&str> {
let start = text.find("```")?;
let rest = &text[start + 3..];
let rest = rest.strip_prefix("json").unwrap_or(rest);
let end = rest.find("```")?;
Some(rest[..end].trim())
}
fn braced(text: &str) -> Option<&str> {
let (open, close) = match (text.find('{'), text.find('[')) {
(Some(o), Some(a)) if a < o => ('[', ']'),
(Some(_), _) => ('{', '}'),
(None, Some(_)) => ('[', ']'),
(None, None) => return None,
};
let start = text.find(open)?;
let end = text.rfind(close)?;
(end > start).then(|| &text[start..=end])
}
fn format_name<T: ?Sized>() -> String {
let full = std::any::type_name::<T>();
let base = full.split('<').next().unwrap_or(full);
let short = base.rsplit("::").next().unwrap_or(base);
let name: String = short
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-')
.take(64)
.collect();
if name.is_empty() {
"answer".into()
} else {
name
}
}
fn schema_instruction(schema: &Value) -> String {
format!(
"When you give your final answer, reply with only a JSON value matching this JSON \
Schema, with no prose and no code fence:\n{schema}"
)
}
fn repair_message(error: &str) -> String {
format!(
"Your answer could not be read: {error}. Reply again with only the JSON value matching \
the schema, with no prose and no code fence."
)
}
fn add_instruction(messages: &mut Vec<Message>, instruction: &str) {
match messages.first_mut() {
Some(first) if first.role == Role::System => {
if !first.content.contains(instruction) {
if !first.content.is_empty() {
first.content.push_str("\n\n");
}
first.content.push_str(instruction);
}
}
_ => messages.insert(0, Message::system(instruction)),
}
}
pub async fn complete_typed<T>(
provider: &dyn Provider,
mut request: ModelRequest,
) -> Result<Typed<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema,
{
let schema = crate::schema::schema_for::<T>();
add_instruction(&mut request.messages, &schema_instruction(&schema));
request.options.response_format = ResponseFormat::JsonSchema {
name: format_name::<T>(),
schema,
strict: true,
};
let mut usage = Usage::default();
let first = provider.complete(&request).await?;
usage.add(first.usage);
let error = match parse_answer::<T>(&first.text) {
Ok(value) => {
return Ok(Typed {
value,
response: first,
usage,
repaired: false,
});
}
Err(error) => error,
};
request
.messages
.push(Message::assistant(first.text.clone(), Vec::new()));
request.messages.push(Message::user(repair_message(&error)));
let second = provider.complete(&request).await?;
usage.add(second.usage);
match parse_answer::<T>(&second.text) {
Ok(value) => Ok(Typed {
value,
response: second,
usage,
repaired: true,
}),
Err(error) => Err(StructuredError::Invalid {
type_name: std::any::type_name::<T>(),
error,
text: second.text,
outcome: None,
}),
}
}
struct OutputSchema {
name: String,
schema: Value,
instruction: String,
}
#[async_trait]
impl Hook for OutputSchema {
fn name(&self) -> &'static str {
"structured-output"
}
async fn before_model(&self, _ctx: &HookContext<'_>, request: &mut ModelRequest) {
add_instruction(&mut request.messages, &self.instruction);
request.options.response_format = ResponseFormat::JsonSchema {
name: self.name.clone(),
schema: self.schema.clone(),
strict: true,
};
}
}
fn typed_agent<T: JsonSchema>(agent: &Agent) -> Agent {
let schema = crate::schema::schema_for::<T>();
agent.with_hook(OutputSchema {
name: format_name::<T>(),
instruction: schema_instruction(&schema),
schema,
})
}
pub trait AgentStructuredExt {
fn run_typed<T>(
&self,
session_id: &str,
input: impl Into<Message> + Send,
) -> impl Future<Output = Result<TypedOutcome<T>, StructuredError>> + Send
where
T: DeserializeOwned + JsonSchema + Send;
fn run_typed_from<T>(
&self,
state: RunState,
cancel: CancellationToken,
) -> impl Future<Output = Result<TypedOutcome<T>, StructuredError>> + Send
where
T: DeserializeOwned + JsonSchema + Send;
fn resume_typed<T>(
&self,
state: RunState,
resolution: Resolution,
) -> impl Future<Output = Result<TypedOutcome<T>, StructuredError>> + Send
where
T: DeserializeOwned + JsonSchema + Send;
fn finish_typed<T>(
&self,
outcome: RunOutcome,
) -> impl Future<Output = Result<TypedOutcome<T>, StructuredError>> + Send
where
T: DeserializeOwned + JsonSchema + Send;
}
impl AgentStructuredExt for Agent {
async fn run_typed<T>(
&self,
session_id: &str,
input: impl Into<Message> + Send,
) -> Result<TypedOutcome<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema + Send,
{
let typed = typed_agent::<T>(self);
let outcome = typed.run(session_id, input).await?;
finish::<T>(&typed, outcome, CancellationToken::new()).await
}
async fn run_typed_from<T>(
&self,
state: RunState,
cancel: CancellationToken,
) -> Result<TypedOutcome<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema + Send,
{
let typed = typed_agent::<T>(self);
let outcome = typed.run_from(state, cancel.clone()).await?;
finish::<T>(&typed, outcome, cancel).await
}
async fn resume_typed<T>(
&self,
state: RunState,
resolution: Resolution,
) -> Result<TypedOutcome<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema + Send,
{
let typed = typed_agent::<T>(self);
let outcome = typed.resume(state, resolution).await?;
finish::<T>(&typed, outcome, CancellationToken::new()).await
}
async fn finish_typed<T>(&self, outcome: RunOutcome) -> Result<TypedOutcome<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema + Send,
{
finish::<T>(&typed_agent::<T>(self), outcome, CancellationToken::new()).await
}
}
fn final_answer(outcome: &RunOutcome) -> Option<&str> {
match outcome.status {
RunStatus::Completed | RunStatus::Finished { .. } => outcome.final_text.as_deref(),
_ => None,
}
}
async fn finish<T>(
agent: &Agent,
outcome: RunOutcome,
cancel: CancellationToken,
) -> Result<TypedOutcome<T>, StructuredError>
where
T: DeserializeOwned + JsonSchema,
{
let Some(text) = final_answer(&outcome) else {
return Err(StructuredError::NotFinished(Box::new(outcome)));
};
let error = match parse_answer::<T>(text) {
Ok(value) => {
return Ok(TypedOutcome {
value,
outcome,
repaired: false,
});
}
Err(error) => error,
};
let state = outcome.state.with_user_message(repair_message(&error));
let outcome = agent.run_from(state, cancel).await?;
let Some(text) = final_answer(&outcome) else {
return Err(StructuredError::NotFinished(Box::new(outcome)));
};
match parse_answer::<T>(text) {
Ok(value) => Ok(TypedOutcome {
value,
outcome,
repaired: true,
}),
Err(error) => Err(StructuredError::Invalid {
type_name: std::any::type_name::<T>(),
error,
text: text.to_string(),
outcome: Some(Box::new(outcome)),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Point {
x: i32,
}
#[test]
fn answers_are_read_bare_fenced_or_in_prose() {
assert_eq!(parse_answer::<Point>(r#" {"x": 1} "#), Ok(Point { x: 1 }));
assert_eq!(
parse_answer::<Point>("```json\n{\"x\": 2}\n```"),
Ok(Point { x: 2 })
);
assert_eq!(
parse_answer::<Point>("Here you go: {\"x\": 3}. Anything else?"),
Ok(Point { x: 3 })
);
let err = parse_answer::<Point>(r#"{"y": 1}"#).unwrap_err();
assert!(err.contains("missing field `x`"), "{err}");
}
#[test]
fn format_names_are_short_and_valid() {
assert_eq!(format_name::<Point>(), "Point");
assert_eq!(format_name::<Vec<Point>>(), "Vec");
}
}