1pub mod agent;
19pub mod config;
20#[allow(unused_imports)]
21pub use config::*;
22
23pub(crate) mod flow;
24pub(crate) mod functions;
25pub(crate) mod live;
26pub(crate) mod mode;
27pub(crate) mod node_wrapper;
28pub(crate) mod output_schema_binding;
29pub use node_wrapper::run_llm_agent_as_node;
30pub(crate) mod processors;
31mod templating;
32pub use templating::inject_session_state;
33pub(crate) mod state;
34pub(crate) mod transfer;
35
36use std::sync::Arc;
37
38use gadk_agent::{Agent, Tool};
39
40pub fn agent_tools(agent: &dyn Agent) -> Option<Vec<Arc<dyn Tool>>> {
44 state::reveal(agent).map(|s| s.tools())
45}
46
47pub fn llm_agent_config(agent: &dyn Agent) -> Option<&LlmAgentConfig> {
52 state::reveal(agent).map(|s| &s.cfg)
53}
54
55#[derive(Debug, Clone, Default, PartialEq, Eq)]
57pub struct AgentInstructions {
58 pub instruction: String,
61 pub global_instruction: String,
63}
64
65pub fn agent_instructions(agent: &dyn Agent) -> Option<AgentInstructions> {
69 state::reveal(agent).map(|s| AgentInstructions {
70 instruction: s.instruction(),
71 global_instruction: s.cfg.global_instruction.clone(),
72 })
73}
74
75#[cfg(test)]
76#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
77mod tests {
78 use gadk_agent::AgentConfig;
79
80 use super::*;
81 use crate::agents::sequential_agent::{self, SequentialAgentConfig};
82 use crate::tool::exit_loop;
83
84 #[test]
85 fn agent_tools_returns_an_llm_agents_tools_and_none_otherwise() {
86 let llm = agent::new(LlmAgentConfig {
87 name: "llm".into(),
88 tools: vec![exit_loop()],
89 ..Default::default()
90 })
91 .unwrap();
92 let names: Vec<String> = agent_tools(&*llm)
93 .unwrap()
94 .iter()
95 .map(|t| t.name().to_owned())
96 .collect();
97 assert_eq!(names, ["exit_loop"]);
98
99 let seq = sequential_agent::new(SequentialAgentConfig {
100 agent: AgentConfig {
101 name: "seq".into(),
102 sub_agents: vec![llm],
103 ..Default::default()
104 },
105 })
106 .unwrap();
107 assert!(agent_tools(&*seq).is_none());
108 }
109
110 #[test]
111 fn llm_agent_config_returns_an_llm_agents_config_and_none_otherwise() {
112 let llm = agent::new(LlmAgentConfig {
113 name: "llm".into(),
114 description: "Answers.".into(),
115 instruction: "You answer questions.".into(),
116 ..Default::default()
117 })
118 .unwrap();
119 let cfg = llm_agent_config(&*llm).unwrap();
120 assert_eq!(
121 (
122 cfg.name.as_str(),
123 cfg.description.as_str(),
124 cfg.instruction.as_str()
125 ),
126 ("llm", "Answers.", "You answer questions.")
127 );
128
129 let seq = sequential_agent::new(SequentialAgentConfig {
130 agent: AgentConfig {
131 name: "seq".into(),
132 sub_agents: vec![llm],
133 ..Default::default()
134 },
135 })
136 .unwrap();
137 assert!(llm_agent_config(&*seq).is_none());
138 }
139
140 #[test]
141 fn agent_instructions_returns_an_llm_agents_instructions_and_none_otherwise() {
142 let llm = agent::new(LlmAgentConfig {
143 name: "llm".into(),
144 instruction: "You answer questions.".into(),
145 global_instruction: "You are helpful.".into(),
146 ..Default::default()
147 })
148 .unwrap();
149 assert_eq!(
150 agent_instructions(&*llm),
151 Some(AgentInstructions {
152 instruction: "You answer questions.".into(),
153 global_instruction: "You are helpful.".into(),
154 })
155 );
156
157 let seq = sequential_agent::new(SequentialAgentConfig {
158 agent: AgentConfig {
159 name: "seq".into(),
160 sub_agents: vec![llm],
161 ..Default::default()
162 },
163 })
164 .unwrap();
165 assert!(agent_instructions(&*seq).is_none());
166 }
167
168 #[test]
169 fn agent_instructions_reads_the_configured_text_not_the_providers() {
170 let provider: InstructionProvider =
171 Arc::new(|_| Box::pin(async { Ok("dynamic".to_owned()) }));
172 let llm = agent::new(LlmAgentConfig {
173 name: "llm".into(),
174 instruction_provider: Some(provider.clone()),
175 global_instruction_provider: Some(provider),
176 ..Default::default()
177 })
178 .unwrap();
179 assert_eq!(
180 agent_instructions(&*llm),
181 Some(AgentInstructions::default())
182 );
183 }
184
185 #[test]
186 fn agent_instructions_includes_the_appended_instruction_suffix() {
187 let llm = agent::new(LlmAgentConfig {
188 name: "llm".into(),
189 instruction: "Base.".into(),
190 ..Default::default()
191 })
192 .unwrap();
193 state::reveal(&*llm)
194 .unwrap()
195 .instruction_suffix
196 .write()
197 .push_str(" Suffix.");
198 assert_eq!(
199 agent_instructions(&*llm).unwrap().instruction,
200 "Base. Suffix."
201 );
202 }
203}