llm-agent 0.3.0

The agent library to build LLM applications that work with any LLM providers.
Documentation
use crate::AgentError;
use llm_sdk::{boxed_stream::BoxedStream, Message, ModelResponse, Part, PartialModelResponse};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentRequest<TCtx> {
    /// The input items for this run, such as LLM messages.
    pub input: Vec<AgentItem>,
    /// The context used to resolve instructions and passed to tool executions.
    pub context: TCtx,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AgentResponse {
    /// The items generated during the run, such as new tool and assistant
    /// messages.
    pub output: Vec<AgentItem>,

    /// The last assistant output content generated by the agent.
    pub content: Vec<Part>,
}

impl AgentResponse {
    /// Extract all text content from the final content, separated by a space.
    #[must_use]
    pub fn text(&self) -> String {
        self.content
            .iter()
            .filter_map(|part| match part {
                Part::Text(part) => Some(part.text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join(" ")
    }
}

/// An input for or output entry generated by the agent run
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum AgentItem {
    /// A LLM message used in the run
    Message(Message),
    /// A model response generated in the run
    Model(ModelResponse),
    /// Tool call input and output generated during the run
    Tool(AgentItemTool),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AgentItemTool {
    /// A unique ID for the tool call
    pub tool_call_id: String,
    /// The name of the tool that was called
    pub tool_name: String,
    /// The input provided to the tool
    pub input: Value,
    /// The result content of the tool call
    pub output: Vec<Part>,
    /// Whether the tool call resulted in an error
    pub is_error: bool,
}

/// Stream events emitted by the agent during runStream call.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "event", rename_all = "kebab-case")]
pub enum AgentStreamEvent {
    Partial(PartialModelResponse),
    Item(AgentStreamItemEvent),
    Response(AgentResponse),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct AgentStreamItemEvent {
    pub index: usize,
    pub item: AgentItem,
}

pub type AgentStream = BoxedStream<'static, Result<AgentStreamEvent, AgentError>>;