openai_agents_rust/agent/
configured.rs

1use crate::agent::runner::Runner;
2use crate::agent::traits::{Agent, AgentContext};
3use crate::agent::types::AgentConfig;
4use crate::error::AgentError;
5use crate::model::Model;
6use async_trait::async_trait;
7
8/// A configurable agent that calls the model and optionally executes tools.
9pub struct ConfiguredAgent<M: Model + 'static> {
10    pub config: AgentConfig,
11    pub model: M,
12}
13
14impl<M: Model + 'static> ConfiguredAgent<M> {
15    pub fn new(name: impl Into<String>, model: M) -> Self {
16        Self {
17            config: AgentConfig::new(name),
18            model,
19        }
20    }
21}
22
23#[async_trait]
24impl<M: Model + 'static> Agent for ConfiguredAgent<M> {
25    async fn run(&self, ctx: &AgentContext) -> Result<(), AgentError> {
26        let default_instructions = format!("Agent: {}", self.config.name);
27        let instructions = self
28            .config
29            .instructions
30            .as_deref()
31            .or(Some(default_instructions.as_str()));
32        let behavior = self.config.tool_use_behavior.clone();
33        let res =
34            Runner::run_agent_with_model(&self.model, ctx, instructions, "say hello", behavior)
35                .await?;
36        tracing::info!(
37            "ConfiguredAgent({}) -> {}",
38            self.config.name,
39            res.text.unwrap_or_default()
40        );
41        Ok(())
42    }
43}