phi_agent/agent/
factory.rs1use std::sync::Arc;
2
3use agent_base::{
4 AgentBuilder, AgentResult, AgentRuntime, ReasoningEffort, RunOutcome, RuntimeEvent, SafetyConfig, SessionId,
5};
6use anyhow::Result;
7
8use crate::agent::builder::base_agent_builder;
9
10#[derive(Clone)]
15pub struct PhiAgentConfig {
16 pub model: String,
18 pub enable_thinking: bool,
20 pub thinking_budget: Option<u64>,
23 pub thinking_effort: ReasoningEffort,
25 pub safety: SafetyConfig,
27 pub max_turns: Option<u32>,
30}
31
32#[derive(Clone)]
44pub struct PhiAgent {
45 runtime: AgentRuntime,
46 pub config: PhiAgentConfig,
48}
49
50impl PhiAgent {
51 pub fn builder(llm_client: Arc<dyn agent_base::LlmClient>, system_prompt: String) -> AgentBuilder {
57 base_agent_builder(llm_client).system_prompt(system_prompt)
58 }
59
60 pub fn build(builder: AgentBuilder, config: PhiAgentConfig) -> Result<Self> {
62 let runtime = builder.build()?;
63 Ok(Self { runtime, config })
64 }
65
66 pub async fn create_session(&self) -> SessionId {
68 self.runtime.create_session().await
69 }
70
71 pub async fn run_turn<F>(&self, session_id: SessionId, query: &str, on_event: F) -> AgentResult<RunOutcome>
73 where
74 F: FnMut(RuntimeEvent) -> AgentResult<()> + Send,
75 {
76 self.runtime.run_turn(session_id, query, on_event).await
77 }
78
79 pub fn cancel(&self) {
81 self.runtime.cancel();
82 }
83
84 pub fn is_cancelled(&self) -> bool {
86 self.runtime.is_cancelled()
87 }
88
89 pub async fn set_reasoning_effort(&self, effort: ReasoningEffort) {
91 self.runtime.set_reasoning_effort(effort).await;
92 }
93
94 pub fn runtime(&self) -> &AgentRuntime {
96 &self.runtime
97 }
98
99 pub async fn list_tools(&self) -> Vec<(String, String)> {
101 let tools = self.runtime.tools_mut();
102 let registry = tools.read().await;
103 let mut list: Vec<_> = registry
104 .definitions()
105 .into_iter()
106 .map(|def| {
107 let name = def["function"]["name"].as_str().unwrap_or("unknown").to_string();
108 let desc = def["function"]["description"].as_str().unwrap_or("").to_string();
109 (name, desc)
110 })
111 .collect();
112 list.sort_by(|a, b| a.0.cmp(&b.0));
113 list
114 }
115}