Skip to main content

machi_agent/
instance.rs

1//! Built agent instance.
2
3use std::sync::Arc;
4
5use machi_tools::ToolRegistry;
6
7use crate::definition::AgentDefinition;
8
9/// Session-bound agent: definition + resolved prompt + tools.
10#[derive(Clone)]
11pub struct Agent {
12    definition: AgentDefinition,
13    system_prompt: String,
14    tools: Arc<ToolRegistry>,
15}
16
17impl std::fmt::Debug for Agent {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        f.debug_struct("Agent")
20            .field("name", &self.definition.name)
21            .field("model", &self.definition.model)
22            .field("system_prompt_len", &self.system_prompt.len())
23            .field("tools", &self.tools.len())
24            .finish()
25    }
26}
27
28impl Agent {
29    /// Construct directly (prefer [`crate::AgentBuilder`]).
30    #[must_use]
31    pub fn new(
32        definition: AgentDefinition,
33        system_prompt: String,
34        tools: Arc<ToolRegistry>,
35    ) -> Self {
36        Self {
37            definition,
38            system_prompt,
39            tools,
40        }
41    }
42
43    /// Agent name.
44    #[must_use]
45    pub fn name(&self) -> &str {
46        &self.definition.name
47    }
48
49    /// Model id.
50    #[must_use]
51    pub fn model(&self) -> &str {
52        &self.definition.model
53    }
54
55    /// Resolved system prompt.
56    #[must_use]
57    pub fn system_prompt(&self) -> &str {
58        &self.system_prompt
59    }
60
61    /// Definition.
62    #[must_use]
63    pub const fn definition(&self) -> &AgentDefinition {
64        &self.definition
65    }
66
67    /// Tool registry.
68    #[must_use]
69    pub fn tools(&self) -> &Arc<ToolRegistry> {
70        &self.tools
71    }
72
73    /// Max steps default.
74    #[must_use]
75    pub const fn max_steps(&self) -> usize {
76        self.definition.max_steps
77    }
78}