hanzo-agent 1.1.21

Agent framework with tool calling capabilities for Hanzo AI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Agent execution runner

use crate::agent::Agent;
use crate::context::RunContext;
use crate::errors::{AgentError, Result};
use crate::result::RunResult;
use crate::types::{InputItem, ModelResponse, ModelSettings, RunItem, Usage};
use serde_json::{json, Value};
use tracing::{debug, info, warn};

/// Default maximum turns for agent execution
pub const DEFAULT_MAX_TURNS: usize = 10;

/// Configuration for agent run
#[derive(Debug, Clone)]
pub struct RunConfig {
    /// Maximum number of turns (LLM invocations)
    pub max_turns: usize,

    /// The API base URL for the LLM provider
    pub api_base: String,

    /// API key for authentication
    pub api_key: Option<String>,

    /// Global model settings override
    pub model_settings: Option<ModelSettings>,

    /// Whether to include tool calls in the context
    pub include_tool_calls: bool,
}

impl RunConfig {
    /// Create a new run config with defaults
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the API base URL
    pub fn with_api_base(mut self, url: impl Into<String>) -> Self {
        self.api_base = url.into();
        self
    }

    /// Set the API key
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Set maximum turns
    pub fn with_max_turns(mut self, turns: usize) -> Self {
        self.max_turns = turns;
        self
    }
}

impl Default for RunConfig {
    fn default() -> Self {
        Self {
            max_turns: DEFAULT_MAX_TURNS,
            api_base: std::env::var("OPENAI_API_BASE")
                .unwrap_or_else(|_| "https://api.openai.com/v1".to_string()),
            api_key: std::env::var("OPENAI_API_KEY").ok(),
            model_settings: None,
            include_tool_calls: true,
        }
    }
}

/// Runner executes the agent loop
pub struct Runner;

impl Runner {
    /// Run the agent with the given input
    ///
    /// This executes the agent loop:
    /// 1. Send input to LLM
    /// 2. If tool calls are returned, execute them
    /// 3. If a final output is returned, complete
    /// 4. If handoff occurs, switch to new agent
    /// 5. Repeat until max_turns or final output
    pub async fn run(agent: &Agent, input: String, config: &RunConfig) -> Result<RunResult> {
        let mut ctx = RunContext::new();
        let current_agent = agent;
        let mut turn = 0;
        let original_input = vec![InputItem::user_message(input)];
        let mut generated_items: Vec<RunItem> = Vec::new();
        let mut model_responses: Vec<ModelResponse> = Vec::new();

        info!("Starting agent run: {}", agent.name);

        loop {
            turn += 1;
            if turn > config.max_turns {
                warn!("Max turns ({}) exceeded", config.max_turns);
                return Err(AgentError::MaxTurnsExceeded(config.max_turns));
            }

            debug!("Turn {}: Running agent {}", turn, current_agent.name);

            // Build the messages for this turn
            let mut messages = original_input.clone();
            messages.extend(generated_items.iter().map(|item| item.to_input_item()));

            // Add system prompt if present
            let mut system_messages = Vec::new();
            if let Some(prompt) = current_agent.system_prompt() {
                system_messages.push(InputItem::system_message(prompt));
            }

            // Get response from LLM
            let response =
                Self::call_llm(current_agent, &system_messages, &messages, config, &mut ctx)
                    .await?;

            model_responses.push(response.clone());
            ctx.add_usage(&response.usage);

            // Process the response
            let (next_step, new_items) =
                Self::process_response(current_agent, response, &mut ctx, config).await?;

            generated_items.extend(new_items);

            match next_step {
                NextStep::FinalOutput(output) => {
                    info!("Agent completed with output");
                    return Ok(RunResult::new(
                        original_input,
                        generated_items,
                        model_responses,
                        output,
                        ctx.usage().clone(),
                    ));
                }
                NextStep::RunAgain => {
                    debug!("Continuing agent loop (tools executed)");
                    continue;
                }
                NextStep::Handoff(_new_agent) => {
                    // TODO: Implement handoff logic
                    warn!("Handoff not yet implemented");
                    return Err(AgentError::Configuration(
                        "Handoff not yet implemented".to_string(),
                    ));
                }
            }
        }
    }

    /// Call the LLM with the current messages
    async fn call_llm(
        agent: &Agent,
        system_messages: &[InputItem],
        messages: &[InputItem],
        config: &RunConfig,
        _ctx: &mut RunContext,
    ) -> Result<ModelResponse> {
        let client = reqwest::Client::new();

        // Build the request body
        let mut all_messages = Vec::new();
        all_messages.extend(Self::items_to_openai_messages(system_messages));
        all_messages.extend(Self::items_to_openai_messages(messages));

        let mut body = json!({
            "model": agent.model,
            "messages": all_messages,
        });

        // Add tools if present
        if !agent.tools.is_empty() {
            let tools: Vec<Value> = agent
                .tools
                .iter()
                .map(|t| {
                    json!({
                        "type": "function",
                        "function": {
                            "name": t.name(),
                            "description": t.description(),
                            "parameters": t.json_schema(),
                        }
                    })
                })
                .collect();
            body["tools"] = json!(tools);
        }

        // Add model settings
        let settings = config
            .model_settings
            .as_ref()
            .unwrap_or(&agent.model_settings);
        if let Some(temp) = settings.temperature {
            body["temperature"] = json!(temp);
        }
        if let Some(top_p) = settings.top_p {
            body["top_p"] = json!(top_p);
        }
        if let Some(max_tokens) = settings.max_tokens {
            body["max_tokens"] = json!(max_tokens);
        }

        debug!("Calling LLM: {}", agent.model);

        // Make the request
        let api_key = config
            .api_key
            .as_ref()
            .ok_or_else(|| AgentError::Configuration("API key not set".to_string()))?;

        let response = client
            .post(format!("{}/chat/completions", config.api_base))
            .header("Authorization", format!("Bearer {}", api_key))
            .header("Content-Type", "application/json")
            .json(&body)
            .send()
            .await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            return Err(AgentError::ModelError(format!(
                "LLM API error {}: {}",
                status, error_text
            )));
        }

        let response_json: Value = response.json().await?;
        debug!("LLM response: {:?}", response_json);

        Self::parse_llm_response(response_json)
    }

    /// Parse the LLM response into a ModelResponse
    fn parse_llm_response(response: Value) -> Result<ModelResponse> {
        let choice = response["choices"]
            .get(0)
            .ok_or_else(|| AgentError::ModelBehavior("No choices in response".to_string()))?;

        let message = &choice["message"];
        let mut output = Vec::new();

        // Check for text content
        if let Some(content) = message["content"].as_str() {
            if !content.is_empty() {
                output.push(RunItem::Message {
                    role: "assistant".to_string(),
                    content: content.to_string(),
                });
            }
        }

        // Check for tool calls
        if let Some(tool_calls) = message["tool_calls"].as_array() {
            for call in tool_calls {
                let id = call["id"]
                    .as_str()
                    .ok_or_else(|| AgentError::ModelBehavior("Missing tool call id".to_string()))?;
                let function = &call["function"];
                let name = function["name"]
                    .as_str()
                    .ok_or_else(|| AgentError::ModelBehavior("Missing tool name".to_string()))?;
                let args = function["arguments"].as_str().ok_or_else(|| {
                    AgentError::ModelBehavior("Missing tool arguments".to_string())
                })?;

                output.push(RunItem::ToolCall {
                    id: id.to_string(),
                    name: name.to_string(),
                    arguments: args.to_string(),
                });
            }
        }

        // Parse usage
        let usage = if let Some(u) = response["usage"].as_object() {
            Usage {
                requests: 1,
                input_tokens: u["prompt_tokens"].as_u64().unwrap_or(0) as usize,
                output_tokens: u["completion_tokens"].as_u64().unwrap_or(0) as usize,
                total_tokens: u["total_tokens"].as_u64().unwrap_or(0) as usize,
            }
        } else {
            Usage::default()
        };

        Ok(ModelResponse {
            output,
            usage,
            id: response["id"].as_str().map(|s| s.to_string()),
        })
    }

    /// Process the model response
    async fn process_response(
        agent: &Agent,
        response: ModelResponse,
        ctx: &mut RunContext,
        _config: &RunConfig,
    ) -> Result<(NextStep, Vec<RunItem>)> {
        let mut new_items = Vec::new();

        // Check if there are tool calls to execute
        let tool_calls: Vec<_> = response
            .output
            .iter()
            .filter_map(|item| {
                if let RunItem::ToolCall {
                    id,
                    name,
                    arguments,
                } = item
                {
                    Some((id.clone(), name.clone(), arguments.clone()))
                } else {
                    None
                }
            })
            .collect();

        if !tool_calls.is_empty() {
            debug!("Executing {} tool calls", tool_calls.len());

            // Add the tool calls to new items
            for (id, name, args) in &tool_calls {
                new_items.push(RunItem::ToolCall {
                    id: id.clone(),
                    name: name.clone(),
                    arguments: args.clone(),
                });
            }

            // Execute each tool
            for (id, name, args) in tool_calls {
                let tool = agent
                    .tools
                    .iter()
                    .find(|t| t.name() == name)
                    .ok_or_else(|| AgentError::ToolError {
                        tool_name: name.clone(),
                        message: "Tool not found".to_string(),
                    })?;

                debug!("Invoking tool: {}", name);
                let result = tool
                    .invoke(ctx, &args)
                    .await
                    .map_err(|e| AgentError::ToolError {
                        tool_name: name.clone(),
                        message: e.to_string(),
                    })?;

                new_items.push(RunItem::ToolResult {
                    tool_call_id: id,
                    content: result,
                });
            }

            return Ok((NextStep::RunAgain, new_items));
        }

        // Check for final output (text message)
        for item in &response.output {
            if let RunItem::Message { content, .. } = item {
                new_items.push(item.clone());
                return Ok((NextStep::FinalOutput(content.clone()), new_items));
            }
        }

        // No tool calls and no text - this is an error
        Err(AgentError::ModelBehavior(
            "Model produced no tool calls or text output".to_string(),
        ))
    }

    /// Convert InputItems to OpenAI message format
    fn items_to_openai_messages(items: &[InputItem]) -> Vec<Value> {
        items
            .iter()
            .map(|item| match item {
                InputItem::Message { role, content } => {
                    json!({
                        "role": role,
                        "content": content,
                    })
                }
                InputItem::ToolResult {
                    tool_call_id,
                    content,
                } => {
                    json!({
                        "role": "tool",
                        "tool_call_id": tool_call_id,
                        "content": content,
                    })
                }
            })
            .collect()
    }
}

/// Next step in the agent loop
#[derive(Debug)]
enum NextStep {
    /// Agent produced final output
    FinalOutput(String),

    /// Run the agent again (after tool execution)
    RunAgain,

    /// Handoff to another agent (planned for multi-agent workflows)
    #[allow(dead_code)]
    Handoff(Agent),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_run_config_builder() {
        let config = RunConfig::new()
            .with_max_turns(5)
            .with_api_base("https://example.com");

        assert_eq!(config.max_turns, 5);
        assert_eq!(config.api_base, "https://example.com");
    }
}