Skip to main content

lha_core/
builder.rs

1use crate::manager::AgentManager;
2use crate::skills::SkillProvider;
3use crate::tools::ToolHandler;
4use crate::tools::ToolRegistry;
5use crate::tools::ToolRegistryBuilder;
6use lha_llm::BaseInstructions;
7use lha_llm::Personality;
8use lha_llm::RuntimeMetadata;
9use lha_llm::SemanticRuntime;
10use serde_json::Value;
11use std::sync::Arc;
12
13pub struct AgentDefinition {
14    pub(crate) runtime: Arc<dyn SemanticRuntime>,
15    pub(crate) base_instructions: BaseInstructions,
16    pub(crate) personality: Option<Personality>,
17    pub(crate) output_schema: Option<Value>,
18    pub(crate) tools: Arc<ToolRegistry>,
19    pub(crate) runtime_metadata: RuntimeMetadata,
20    pub(crate) skill_providers: Vec<Arc<dyn SkillProvider>>,
21}
22
23pub struct AgentBuilder {
24    runtime: Arc<dyn SemanticRuntime>,
25    base_instructions: BaseInstructions,
26    personality: Option<Personality>,
27    output_schema: Option<Value>,
28    tools: ToolRegistryBuilder,
29    skill_providers: Vec<Arc<dyn SkillProvider>>,
30}
31
32impl AgentBuilder {
33    pub fn new(runtime: Arc<dyn SemanticRuntime>) -> Self {
34        Self {
35            base_instructions: BaseInstructions::default(),
36            personality: None,
37            output_schema: None,
38            tools: ToolRegistryBuilder::new(),
39            skill_providers: Vec::new(),
40            runtime,
41        }
42    }
43
44    pub fn with_base_instructions(mut self, text: impl Into<String>) -> Self {
45        self.base_instructions = BaseInstructions { text: text.into() };
46        self
47    }
48
49    pub fn with_personality(mut self, personality: Personality) -> Self {
50        self.personality = Some(personality);
51        self
52    }
53
54    pub fn with_output_schema(mut self, schema: Value) -> Self {
55        self.output_schema = Some(schema);
56        self
57    }
58
59    pub fn register_tool(mut self, handler: Arc<dyn ToolHandler>) -> Self {
60        self.tools.register_handler(handler);
61        self
62    }
63
64    #[cfg(feature = "mcp")]
65    pub fn try_register_mcp_provider<C>(
66        mut self,
67        provider: crate::mcp::McpToolProvider<C>,
68    ) -> std::result::Result<Self, crate::mcp::McpError>
69    where
70        C: crate::mcp::McpClient + Send + Sync + 'static,
71    {
72        for handler in provider.into_tool_handlers()? {
73            self.tools.register_handler(handler);
74        }
75        Ok(self)
76    }
77
78    pub fn register_skill_provider(mut self, provider: Arc<dyn SkillProvider>) -> Self {
79        self.skill_providers.push(provider);
80        self
81    }
82
83    pub fn build(self) -> AgentManager {
84        let runtime_metadata = self.runtime.metadata();
85        let definition = AgentDefinition {
86            runtime: self.runtime,
87            base_instructions: self.base_instructions,
88            personality: self.personality,
89            output_schema: self.output_schema,
90            tools: Arc::new(self.tools.build()),
91            runtime_metadata,
92            skill_providers: self.skill_providers,
93        };
94        AgentManager::new(definition)
95    }
96}