prompty 2.0.0-alpha.11

Prompty is an asset class and format for LLM prompts
Documentation
// Code generated by AgentSchema emitter; DO NOT EDIT.

#![allow(
    unused_imports,
    dead_code,
    non_camel_case_types,
    unused_variables,
    clippy::all
)]

use super::super::context::{LoadContext, SaveContext};

use super::compaction_config::CompactionConfig;

/// Configuration for the agent loop's turn() function. Controls iteration limits, retry policy, context management, and execution behavior. Runtimes accept these as either a TurnOptions object or individual keyword/named parameters — the TypeSpec model defines the canonical field set.
#[derive(Debug, Clone, Default)]
pub struct TurnOptions {
    /// Maximum number of tool-call iterations before the loop terminates
    pub max_iterations: Option<i32>,
    /// Maximum number of LLM call retries on transient failure within a single iteration
    pub max_llm_retries: Option<i32>,
    /// Character budget for the context window. When set, the loop trims older messages to stay within budget before each LLM call. System messages are never dropped.
    pub context_budget: Option<i32>,
    /// Whether to execute multiple tool calls concurrently when the LLM returns several in one response
    pub parallel_tool_calls: Option<bool>,
    /// When true, return the raw LLM response without processor post-processing
    pub raw: Option<bool>,
    /// Turn number for trace labeling. Useful when the caller maintains an outer conversation loop.
    pub turn: Option<i32>,
    /// Context compaction configuration. Controls how messages are summarized when the context window exceeds the budget.
    pub compaction: Option<CompactionConfig>,
}

impl TurnOptions {
    /// Create a new TurnOptions with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Load TurnOptions from a JSON string.
    pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
        let value: serde_json::Value = serde_json::from_str(json)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load TurnOptions from a YAML string.
    pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
        let value: serde_json::Value = serde_yaml::from_str(yaml)?;
        Ok(Self::load_from_value(&value, ctx))
    }

    /// Load TurnOptions from a `serde_json::Value`.
    ///
    /// Calls `ctx.process_input` before field extraction.
    pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
        let value = ctx.process_input(value.clone());
        Self {
            max_iterations: value
                .get("maxIterations")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            max_llm_retries: value
                .get("maxLlmRetries")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            context_budget: value
                .get("contextBudget")
                .and_then(|v| v.as_i64())
                .map(|v| v as i32),
            parallel_tool_calls: value.get("parallelToolCalls").and_then(|v| v.as_bool()),
            raw: value.get("raw").and_then(|v| v.as_bool()),
            turn: value.get("turn").and_then(|v| v.as_i64()).map(|v| v as i32),
            compaction: value
                .get("compaction")
                .filter(|v| v.is_object() || v.is_array() || v.is_string())
                .map(|v| CompactionConfig::load_from_value(v, ctx)),
        }
    }

    /// Serialize TurnOptions to a `serde_json::Value`.
    ///
    /// Calls `ctx.process_dict` after serialization.
    pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
        let mut result = serde_json::Map::new();
        // Write base fields
        if let Some(val) = self.max_iterations {
            result.insert(
                "maxIterations".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(val) = self.max_llm_retries {
            result.insert(
                "maxLlmRetries".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(val) = self.context_budget {
            result.insert(
                "contextBudget".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(val) = self.parallel_tool_calls {
            result.insert(
                "parallelToolCalls".to_string(),
                serde_json::Value::Bool(val),
            );
        }
        if let Some(val) = self.raw {
            result.insert("raw".to_string(), serde_json::Value::Bool(val));
        }
        if let Some(val) = self.turn {
            result.insert(
                "turn".to_string(),
                serde_json::Value::Number(serde_json::Number::from(val)),
            );
        }
        if let Some(ref val) = self.compaction {
            let nested = val.to_value(ctx);
            if !nested.is_null() {
                result.insert("compaction".to_string(), nested);
            }
        }
        ctx.process_dict(serde_json::Value::Object(result))
    }

    /// Serialize TurnOptions to a JSON string.
    pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.to_value(ctx))
    }

    /// Serialize TurnOptions to a YAML string.
    pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(&self.to_value(ctx))
    }
}