embacle 0.14.6

LLM runner library — wraps 12 AI CLI tools as pluggable LLM providers with agent loop, guardrails, and cost tracking
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// ABOUTME: Configurable agent loop building on text-based tool simulation
// ABOUTME: Provides AgentExecutor with multi-turn tool calling, callbacks, and token accumulation
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 dravr.ai

//! # Agent Loop
//!
//! [`AgentExecutor`] provides a configurable multi-turn agent loop that:
//!
//! 1. Injects a tool catalog into the conversation
//! 2. Calls `provider.complete()` and parses tool calls from the response
//! 3. Executes tools via the provided handler
//! 4. Feeds results back and repeats until no tool calls remain or
//!    `max_turns` is reached
//!
//! Builds on [`tool_simulation`](crate::tool_simulation) types and functions.
//!
//! ## Observability
//!
//! An optional [`OnTurnCallback`] is invoked after each turn with a
//! [`TurnInfo`] snapshot, enabling logging, metrics, or UI updates.

use std::sync::Arc;

use tracing::{debug, info};

use crate::tool_simulation::{
    format_tool_results_as_text, generate_tool_catalog, inject_tool_catalog,
    parse_tool_call_blocks, strip_tool_call_blocks, FunctionCall, FunctionDeclaration,
    TextToolHandler,
};
use crate::types::{ChatMessage, ChatRequest, LlmProvider, RunnerError, TokenUsage};

/// Default maximum turns for the agent loop
const DEFAULT_MAX_TURNS: u32 = 10;

/// Absolute ceiling for `max_turns` to prevent runaway loops
const MAX_TURNS_CEILING: u32 = 50;

/// Callback invoked after each agent turn for observability
pub type OnTurnCallback = Arc<dyn Fn(&TurnInfo) + Send + Sync>;

/// Information about a single agent turn, passed to [`OnTurnCallback`]
#[derive(Debug, Clone)]
pub struct TurnInfo {
    /// Turn number (1-based)
    pub turn: u32,
    /// Tool calls made during this turn
    pub tool_calls: Vec<FunctionCall>,
    /// Text content from the LLM (tool call blocks stripped)
    pub content: String,
    /// Token usage for this turn (if reported by the provider)
    pub usage: Option<TokenUsage>,
}

/// Result of an agent execution run
#[derive(Debug, Clone)]
pub struct AgentResult {
    /// Final text content from the LLM
    pub content: String,
    /// All tool calls made across all turns
    pub tool_calls: Vec<FunctionCall>,
    /// Total number of turns executed
    pub total_turns: u32,
    /// Accumulated token usage across all turns
    pub total_usage: TokenUsage,
    /// Finish reason (e.g., "stop", "`max_turns`")
    pub finish_reason: Option<String>,
}

/// Configurable agent loop with tool calling support.
///
/// # Usage
///
/// ```rust,no_run
/// # use embacle::agent::AgentExecutor;
/// # use embacle::tool_simulation::{FunctionDeclaration, FunctionResponse, TextToolHandler};
/// # use embacle::types::{ChatMessage, LlmProvider};
/// # use serde_json::json;
/// # use std::sync::Arc;
/// # async fn example(provider: &dyn LlmProvider) -> Result<(), embacle::types::RunnerError> {
/// let declarations = vec![FunctionDeclaration {
///     name: "search".into(),
///     description: "Search the web".into(),
///     parameters: Some(json!({"type": "object", "properties": {"q": {"type": "string"}}})),
/// }];
///
/// let handler: TextToolHandler = Arc::new(|name, _args| {
///     FunctionResponse { name: name.to_owned(), response: json!({"results": []}) }
/// });
///
/// let executor = AgentExecutor::new(provider, declarations, handler);
/// let messages = vec![ChatMessage::user("Search for Rust tutorials")];
/// let result = executor.run(messages).await?;
/// println!("{}", result.content);
/// # Ok(())
/// # }
/// ```
pub struct AgentExecutor<'a> {
    provider: &'a dyn LlmProvider,
    declarations: Vec<FunctionDeclaration>,
    tool_handler: TextToolHandler,
    max_turns: u32,
    on_turn: Option<OnTurnCallback>,
}

impl<'a> AgentExecutor<'a> {
    /// Create a new agent executor with default settings (`max_turns=10`)
    pub fn new(
        provider: &'a dyn LlmProvider,
        declarations: Vec<FunctionDeclaration>,
        tool_handler: TextToolHandler,
    ) -> Self {
        Self {
            provider,
            declarations,
            tool_handler,
            max_turns: DEFAULT_MAX_TURNS,
            on_turn: None,
        }
    }

    /// Set the maximum number of turns (clamped to ceiling of 50)
    pub fn with_max_turns(mut self, max_turns: u32) -> Self {
        self.max_turns = max_turns.min(MAX_TURNS_CEILING);
        self
    }

    /// Set an observability callback invoked after each turn
    pub fn with_on_turn(mut self, callback: OnTurnCallback) -> Self {
        self.on_turn = Some(callback);
        self
    }

    /// Run the agent loop with the given initial messages.
    ///
    /// # Errors
    ///
    /// Returns [`RunnerError`] if any `provider.complete()` call fails.
    pub async fn run(
        &self,
        initial_messages: Vec<ChatMessage>,
    ) -> Result<AgentResult, RunnerError> {
        let mut messages = initial_messages;

        // Inject tool catalog into the conversation
        let catalog = generate_tool_catalog(&self.declarations);
        inject_tool_catalog(&mut messages, &catalog);

        debug!(
            tool_count = self.declarations.len(),
            max_turns = self.max_turns,
            "agent: starting loop"
        );

        let mut all_tool_calls: Vec<FunctionCall> = Vec::new();
        let mut total_usage = TokenUsage {
            prompt_tokens: 0,
            completion_tokens: 0,
            total_tokens: 0,
        };
        let mut turn: u32 = 0;

        loop {
            turn += 1;
            if turn > self.max_turns {
                info!(max_turns = self.max_turns, "agent: max turns reached");
                return Ok(AgentResult {
                    content: String::new(),
                    tool_calls: all_tool_calls,
                    total_turns: turn - 1,
                    total_usage,
                    finish_reason: Some("max_turns".to_owned()),
                });
            }

            let request = ChatRequest::new(messages.clone());
            let response = self.provider.complete(&request).await?;

            // Accumulate token usage
            if let Some(ref usage) = response.usage {
                total_usage.prompt_tokens += usage.prompt_tokens;
                total_usage.completion_tokens += usage.completion_tokens;
                total_usage.total_tokens += usage.total_tokens;
            }

            // Parse tool calls from the response
            let parsed_calls = parse_tool_call_blocks(&response.content);
            let content = strip_tool_call_blocks(&response.content);

            if parsed_calls.is_empty() {
                // No tool calls — final response
                let turn_info = TurnInfo {
                    turn,
                    tool_calls: vec![],
                    content: content.clone(),
                    usage: response.usage.clone(),
                };

                if let Some(ref callback) = self.on_turn {
                    callback(&turn_info);
                }

                debug!(turn, "agent: final response (no tool calls)");
                return Ok(AgentResult {
                    content,
                    tool_calls: all_tool_calls,
                    total_turns: turn,
                    total_usage,
                    finish_reason: response.finish_reason,
                });
            }

            info!(
                turn,
                call_count = parsed_calls.len(),
                "agent: executing tool calls"
            );

            // Execute tool calls
            let mut function_responses = Vec::with_capacity(parsed_calls.len());
            for call in &parsed_calls {
                let resp = (self.tool_handler)(&call.name, &call.args);
                function_responses.push(resp);
            }

            // Build turn info for callback
            let turn_info = TurnInfo {
                turn,
                tool_calls: parsed_calls.clone(),
                content: content.clone(),
                usage: response.usage,
            };

            if let Some(ref callback) = self.on_turn {
                callback(&turn_info);
            }

            all_tool_calls.extend(parsed_calls);

            // Append assistant response and tool results to conversation
            if !content.is_empty() {
                messages.push(ChatMessage::assistant(content));
            }

            let tool_results_text = format_tool_results_as_text(&function_responses);
            messages.push(ChatMessage::user(tool_results_text));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool_simulation::FunctionResponse;
    use crate::types::{
        ChatMessage, ChatRequest, ChatResponse, ChatStream, LlmCapabilities, LlmProvider,
        RunnerError, TokenUsage,
    };
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Mutex;

    struct TestProvider {
        responses: Mutex<Vec<Result<ChatResponse, RunnerError>>>,
        call_count: AtomicU32,
    }

    impl TestProvider {
        fn new(responses: Vec<Result<ChatResponse, RunnerError>>) -> Self {
            Self {
                responses: Mutex::new(responses),
                call_count: AtomicU32::new(0),
            }
        }
    }

    #[async_trait]
    impl LlmProvider for TestProvider {
        fn name(&self) -> &'static str {
            "test"
        }
        fn display_name(&self) -> &str {
            "Test Provider"
        }
        fn capabilities(&self) -> LlmCapabilities {
            LlmCapabilities::text_only()
        }
        fn default_model(&self) -> &'static str {
            "test-model"
        }
        fn available_models(&self) -> &[String] {
            &[]
        }
        async fn complete(&self, _request: &ChatRequest) -> Result<ChatResponse, RunnerError> {
            self.call_count.fetch_add(1, Ordering::SeqCst);
            let mut responses = self.responses.lock().expect("test lock");
            if responses.is_empty() {
                Err(RunnerError::internal("no more test responses"))
            } else {
                responses.remove(0)
            }
        }
        async fn complete_stream(&self, _request: &ChatRequest) -> Result<ChatStream, RunnerError> {
            Err(RunnerError::internal("not supported"))
        }
        async fn health_check(&self) -> Result<bool, RunnerError> {
            Ok(true)
        }
    }

    fn make_response(content: &str, usage: Option<TokenUsage>) -> ChatResponse {
        ChatResponse {
            content: content.to_owned(),
            model: "test-model".to_owned(),
            usage,
            finish_reason: Some("stop".to_owned()),
            warnings: None,
            tool_calls: None,
        }
    }

    fn noop_handler() -> TextToolHandler {
        Arc::new(|name: &str, _args: &serde_json::Value| FunctionResponse {
            name: name.to_owned(),
            response: json!({"status": "ok"}),
        })
    }

    #[tokio::test]
    async fn single_turn_no_tool_calls() {
        let provider = TestProvider::new(vec![Ok(make_response(
            "Here is a direct answer without tool calls.",
            Some(TokenUsage {
                prompt_tokens: 10,
                completion_tokens: 8,
                total_tokens: 18,
            }),
        ))]);

        let declarations = vec![FunctionDeclaration {
            name: "search".to_owned(),
            description: "Search the web".to_owned(),
            parameters: None,
        }];

        let executor = AgentExecutor::new(&provider, declarations, noop_handler());
        let messages = vec![ChatMessage::user("Hello")];
        let result = executor.run(messages).await.expect("should succeed");

        assert_eq!(
            result.content,
            "Here is a direct answer without tool calls."
        );
        assert!(result.tool_calls.is_empty());
        assert_eq!(result.total_turns, 1);
        assert_eq!(result.total_usage.prompt_tokens, 10);
        assert_eq!(result.finish_reason, Some("stop".to_owned()));
    }

    #[tokio::test]
    async fn multi_turn_with_tool_calls() {
        let provider = TestProvider::new(vec![
            // Turn 1: LLM calls a tool
            Ok(make_response(
                "Let me search for that.\n<tool_call>\n{\"name\": \"search\", \"arguments\": {\"q\": \"rust\"}}\n</tool_call>",
                Some(TokenUsage { prompt_tokens: 10, completion_tokens: 15, total_tokens: 25 }),
            )),
            // Turn 2: LLM responds with the result
            Ok(make_response(
                "Based on the search results, Rust is a systems programming language.",
                Some(TokenUsage { prompt_tokens: 30, completion_tokens: 12, total_tokens: 42 }),
            )),
        ]);

        let declarations = vec![FunctionDeclaration {
            name: "search".to_owned(),
            description: "Search the web".to_owned(),
            parameters: Some(json!({"type": "object", "properties": {"q": {"type": "string"}}})),
        }];

        let executor = AgentExecutor::new(&provider, declarations, noop_handler());
        let messages = vec![ChatMessage::user("What is Rust?")];
        let result = executor.run(messages).await.expect("should succeed");

        assert!(result.content.contains("systems programming"));
        assert_eq!(result.tool_calls.len(), 1);
        assert_eq!(result.tool_calls[0].name, "search");
        assert_eq!(result.total_turns, 2);
        assert_eq!(result.total_usage.prompt_tokens, 40);
        assert_eq!(result.total_usage.completion_tokens, 27);
    }

    #[tokio::test]
    async fn on_turn_callback_invoked() {
        let provider = TestProvider::new(vec![
            Ok(make_response(
                "<tool_call>\n{\"name\": \"ping\", \"arguments\": {}}\n</tool_call>",
                None,
            )),
            Ok(make_response("Done!", None)),
        ]);

        let declarations = vec![FunctionDeclaration {
            name: "ping".to_owned(),
            description: "Ping".to_owned(),
            parameters: None,
        }];

        let turn_log: Arc<Mutex<Vec<u32>>> = Arc::new(Mutex::new(Vec::new()));
        let turn_log_clone = Arc::clone(&turn_log);

        let callback: OnTurnCallback = Arc::new(move |info: &TurnInfo| {
            turn_log_clone.lock().expect("lock").push(info.turn);
        });

        let executor =
            AgentExecutor::new(&provider, declarations, noop_handler()).with_on_turn(callback);
        let messages = vec![ChatMessage::user("ping")];
        executor.run(messages).await.expect("should succeed");

        let logged = turn_log.lock().expect("lock").clone();
        assert_eq!(logged, vec![1, 2]);
    }

    #[tokio::test]
    async fn max_turns_exhaustion() {
        // Provider always returns tool calls — should exhaust max_turns
        let mut responses = Vec::new();
        for _ in 0..5 {
            responses.push(Ok(make_response(
                "<tool_call>\n{\"name\": \"loop\", \"arguments\": {}}\n</tool_call>",
                None,
            )));
        }
        let provider = TestProvider::new(responses);

        let declarations = vec![FunctionDeclaration {
            name: "loop".to_owned(),
            description: "Loop forever".to_owned(),
            parameters: None,
        }];

        let executor =
            AgentExecutor::new(&provider, declarations, noop_handler()).with_max_turns(3);
        let messages = vec![ChatMessage::user("go")];
        let result = executor.run(messages).await.expect("should not error");

        assert_eq!(result.finish_reason, Some("max_turns".to_owned()));
        assert_eq!(result.total_turns, 3);
        assert_eq!(result.tool_calls.len(), 3);
    }

    #[tokio::test]
    async fn token_accumulation() {
        let provider = TestProvider::new(vec![
            Ok(make_response(
                "<tool_call>\n{\"name\": \"a\", \"arguments\": {}}\n</tool_call>",
                Some(TokenUsage {
                    prompt_tokens: 10,
                    completion_tokens: 5,
                    total_tokens: 15,
                }),
            )),
            Ok(make_response(
                "final",
                Some(TokenUsage {
                    prompt_tokens: 20,
                    completion_tokens: 3,
                    total_tokens: 23,
                }),
            )),
        ]);

        let declarations = vec![FunctionDeclaration {
            name: "a".to_owned(),
            description: "Tool A".to_owned(),
            parameters: None,
        }];

        let executor = AgentExecutor::new(&provider, declarations, noop_handler());
        let messages = vec![ChatMessage::user("go")];
        let result = executor.run(messages).await.expect("should succeed");

        assert_eq!(result.total_usage.prompt_tokens, 30);
        assert_eq!(result.total_usage.completion_tokens, 8);
        assert_eq!(result.total_usage.total_tokens, 38);
    }
}