enact-core 0.0.2

Core agent runtime for Enact - Graph-Native AI agents
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! LLM Callable - LLM-powered execution with tool loop
//!
//! This is an "agentic" callable - it runs an LLM with tools in a loop
//! until the LLM produces a final response.

use super::Callable;
use crate::kernel::cost::TokenUsage;
use crate::providers::{
    ChatMessage, ChatRequest, ChatTool, ChatToolFunction, ContentPart, MessageToolCall,
    ModelProvider, ToolChoice,
};
use crate::routing::{ModelRouter, RoutingDecision, RoutingPolicy};
use crate::streaming::{EventEmitter, StreamEvent};
use crate::tool::{DynTool, Tool};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use tokio::time::{interval, Duration};

/// Tool call from the LLM
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    pub id: String,
    pub name: String,
    pub arguments: Value,
}

/// Multimodal input format for encoding images in string input
///
/// When sending images to an LLM callable, encode the input as:
/// ```json
/// {
///   "__multimodal__": true,
///   "text": "Describe this image",
///   "images": [
///     {"data": "<base64>", "mime_type": "image/jpeg"}
///   ]
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultimodalInput {
    #[serde(rename = "__multimodal__")]
    pub multimodal_marker: bool,
    /// The text portion of the message
    pub text: String,
    /// Base64-encoded images with mime types
    #[serde(default)]
    pub images: Vec<MultimodalImage>,
}

/// An image in multimodal input
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultimodalImage {
    /// Base64-encoded image data
    pub data: String,
    /// MIME type (e.g., "image/jpeg", "image/png")
    pub mime_type: String,
}

impl MultimodalInput {
    /// Create a new multimodal input with text and images
    pub fn new(text: impl Into<String>, images: Vec<(Vec<u8>, String)>) -> Self {
        use base64::Engine;
        Self {
            multimodal_marker: true,
            text: text.into(),
            images: images
                .into_iter()
                .map(|(data, mime_type)| MultimodalImage {
                    data: base64::engine::general_purpose::STANDARD.encode(&data),
                    mime_type,
                })
                .collect(),
        }
    }

    /// Encode to JSON string for passing through the runner
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap_or_else(|_| self.text.clone())
    }

    /// Try to parse multimodal input from a string
    /// Returns None if not multimodal format
    pub fn parse(input: &str) -> Option<Self> {
        if !input.trim_start().starts_with(r#"{"__multimodal__":"#) {
            return None;
        }
        serde_json::from_str(input).ok()
    }
}

/// Tool schema for LLM
#[derive(Debug, Clone, Serialize)]
pub struct ToolSchema {
    #[serde(rename = "type")]
    pub tool_type: String,
    pub function: FunctionSchema,
}

#[derive(Debug, Clone, Serialize)]
pub struct FunctionSchema {
    pub name: String,
    pub description: String,
    pub parameters: Value,
}

impl ToolSchema {
    pub fn from_tool(tool: &dyn Tool) -> Self {
        Self {
            tool_type: "function".to_string(),
            function: FunctionSchema {
                name: tool.name().to_string(),
                description: tool.description().to_string(),
                parameters: tool.parameters_schema(),
            },
        }
    }
}

/// LLM-powered callable with tool execution
///
/// This is the "agentic" callable - it runs the LLM in a loop,
/// executing tools as requested until a final response is produced.
///
/// Note: The loop is controlled by `max_iterations` to prevent runaway.
pub struct LlmCallable {
    name: String,
    description: Option<String>,
    system_prompt: String,
    provider: Arc<dyn ModelProvider>,
    requested_model: Option<String>,
    routing_policy: RoutingPolicy,
    tools: Vec<DynTool>,
    max_iterations: usize,
    /// Optional event emitter for streaming tool events
    emitter: Option<Arc<EventEmitter>>,
    /// Token usage from the last run (accumulated across tool-loop iterations)
    last_usage: Mutex<Option<TokenUsage>>,
}

impl LlmCallable {
    /// Create with a custom provider
    pub fn with_provider(
        name: impl Into<String>,
        system_prompt: impl Into<String>,
        provider: Arc<dyn ModelProvider>,
    ) -> Self {
        Self {
            name: name.into(),
            description: None,
            system_prompt: system_prompt.into(),
            provider,
            requested_model: None,
            routing_policy: RoutingPolicy::default(),
            tools: Vec::new(),
            max_iterations: 10,
            emitter: None,
            last_usage: Mutex::new(None),
        }
    }

    /// Set the event emitter for streaming tool events
    pub fn with_emitter(mut self, emitter: Arc<EventEmitter>) -> Self {
        self.emitter = Some(emitter);
        self
    }

    /// Pin a logical model id for this callable.
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.requested_model = Some(model.into());
        self
    }

    /// Override routing policy for this callable.
    pub fn with_routing_policy(mut self, policy: RoutingPolicy) -> Self {
        self.routing_policy = policy;
        self
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add a tool to the callable
    pub fn add_tool(mut self, tool: impl Tool + 'static) -> Self {
        self.tools.push(Arc::new(tool));
        self
    }

    /// Add multiple tools
    pub fn add_tools(mut self, tools: Vec<DynTool>) -> Self {
        self.tools.extend(tools);
        self
    }

    /// Set max iterations for tool loop
    pub fn max_iterations(mut self, max: usize) -> Self {
        self.max_iterations = max;
        self
    }

    /// Execute a tool by name
    async fn execute_tool(&self, name: &str, args: Value) -> anyhow::Result<Value> {
        let tool = self
            .tools
            .iter()
            .find(|t| t.name() == name)
            .ok_or_else(|| anyhow::anyhow!("Tool '{}' not found", name))?;

        tool.execute(args).await
    }

    /// Build ChatTool list for request (OpenAI shape)
    fn build_chat_tools(&self) -> Vec<ChatTool> {
        self.tools
            .iter()
            .map(|t| ChatTool {
                tool_type: "function".to_string(),
                function: ChatToolFunction {
                    name: t.name().to_string(),
                    description: t.description().to_string(),
                    parameters: t.parameters_schema(),
                },
            })
            .collect()
    }

    /// Map native message tool_calls to internal ToolCall (parse arguments JSON)
    fn message_tool_calls_to_internal(&self, tool_calls: &[MessageToolCall]) -> Vec<ToolCall> {
        tool_calls
            .iter()
            .map(|tc| {
                let arguments = serde_json::from_str(&tc.function.arguments).unwrap_or(Value::Null);
                ToolCall {
                    id: tc.id.clone(),
                    name: tc.function.name.clone(),
                    arguments,
                }
            })
            .collect()
    }

    fn resolve_routing(&self) -> RoutingDecision {
        ModelRouter::resolve(
            self.requested_model.as_deref(),
            self.provider.as_ref(),
            &self.routing_policy,
        )
    }
}

#[async_trait]
impl Callable for LlmCallable {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    async fn run_streaming(
        &self,
        input: &str,
        event_tx: mpsc::Sender<StreamEvent>,
    ) -> anyhow::Result<String> {
        let emitter = self.emitter.clone();
        let tx = event_tx.clone();
        let poll_handle = if emitter.is_some() {
            Some(tokio::spawn(async move {
                let emitter = match &emitter {
                    Some(e) => e,
                    None => return,
                };
                let mut interval = interval(Duration::from_millis(50));
                loop {
                    interval.tick().await;
                    let events = emitter.drain();
                    for ev in events {
                        if tx.send(ev).await.is_err() {
                            return;
                        }
                    }
                }
            }))
        } else {
            None
        };

        let result = self.run(input).await;

        if let Some(ref e) = self.emitter {
            for ev in e.drain() {
                let _ = event_tx.send(ev).await;
            }
        }
        drop(event_tx);
        if let Some(h) = poll_handle {
            let _ = h.await;
        }

        result
    }

    async fn run(&self, input: &str) -> anyhow::Result<String> {
        *self.last_usage.lock().expect("last_usage mutex") = None;

        if !self.tools.is_empty() && !self.provider.capabilities().supports_tools {
            anyhow::bail!(
                "Callable has {} tool(s) but provider does not support native tools (supports_tools is false)",
                self.tools.len()
            );
        }

        let routing = self.resolve_routing();
        tracing::info!(
            callable = %self.name,
            logical_model = %routing.logical_model,
            concrete_model = %routing.concrete_model,
            profile = ?routing.profile,
            confidence = routing.confidence,
            used_default_router = routing.used_default_router,
            rationale = %routing.rationale,
            "Model routing decision resolved"
        );

        // Check if input is multimodal (contains images)
        let user_message = if let Some(multimodal) = MultimodalInput::parse(input) {
            tracing::debug!(
                image_count = multimodal.images.len(),
                text_len = multimodal.text.len(),
                "Processing multimodal input with images"
            );

            if !self.provider.capabilities().supports_vision {
                tracing::warn!(
                    "Provider does not support vision, falling back to text-only. \
                     Images will be ignored. Consider using a vision-capable model."
                );
                ChatMessage::user(&multimodal.text)
            } else {
                // Build multimodal message with images
                use base64::Engine;
                let mut parts = vec![ContentPart::text(&multimodal.text)];
                for img in &multimodal.images {
                    // Decode and re-encode to ensure valid base64
                    if let Ok(data) = base64::engine::general_purpose::STANDARD.decode(&img.data) {
                        parts.push(ContentPart::image_base64(
                            base64::engine::general_purpose::STANDARD.encode(&data),
                            &img.mime_type,
                        ));
                    } else {
                        tracing::warn!(mime_type = %img.mime_type, "Failed to decode image base64 data");
                    }
                }

                ChatMessage {
                    role: "user".to_string(),
                    content: None,
                    multimodal_content: Some(parts),
                    tool_calls: None,
                    tool_call_id: None,
                }
            }
        } else {
            ChatMessage::user(input)
        };

        let mut messages = vec![ChatMessage::system(&self.system_prompt), user_message];

        let (tools, tool_choice) = if self.tools.is_empty() {
            (None, None)
        } else {
            (
                Some(self.build_chat_tools()),
                Some(ToolChoice::String("auto".to_string())),
            )
        };

        let mut accumulated_usage: Option<TokenUsage> = None;

        for iteration in 0..self.max_iterations {
            tracing::debug!(iteration, "Callable iteration");

            let request = ChatRequest {
                messages: messages.clone(),
                max_tokens: Some(4096),
                temperature: Some(0.7),
                tools: tools.clone(),
                tool_choice: tool_choice.clone(),
            };

            let response = self.provider.chat(request).await?;

            if let Some(ref u) = response.usage {
                accumulated_usage = Some(match accumulated_usage {
                    None => TokenUsage::new(u.prompt_tokens, u.completion_tokens),
                    Some(a) => TokenUsage::new(
                        a.prompt_tokens + u.prompt_tokens,
                        a.completion_tokens + u.completion_tokens,
                    ),
                });
            }

            let choice = response
                .choices
                .first()
                .ok_or_else(|| anyhow::anyhow!("Empty choices in chat response"))?;
            let msg = &choice.message;

            let native_tool_calls = msg.tool_calls.as_deref().unwrap_or(&[]);
            if native_tool_calls.is_empty() {
                let content = msg.content.clone().unwrap_or_default();
                *self.last_usage.lock().expect("last_usage mutex") = accumulated_usage;
                return Ok(content);
            }

            let calls = self.message_tool_calls_to_internal(native_tool_calls);
            messages.push(ChatMessage::assistant_with_tool_calls(
                msg.content.clone(),
                native_tool_calls.to_vec(),
            ));

            for call in &calls {
                tracing::debug!(tool = %call.name, "Executing tool");

                if let Some(ref emitter) = self.emitter {
                    emitter.emit(StreamEvent::ToolInputAvailable {
                        tool_call_id: call.id.clone(),
                        tool_name: call.name.clone(),
                        input: call.arguments.clone(),
                    });
                }

                let tool_start = std::time::Instant::now();
                let result = self
                    .execute_tool(&call.name, call.arguments.clone())
                    .await?;
                let tool_duration_ms = tool_start.elapsed().as_millis() as u64;

                if let Some(ref emitter) = self.emitter {
                    emitter.emit(StreamEvent::ToolOutputAvailable {
                        tool_call_id: call.id.clone(),
                        output: serde_json::json!({
                            "result": result.clone(),
                            "duration_ms": tool_duration_ms,
                        }),
                    });
                }

                let result_str = serde_json::to_string(&result)?;
                messages.push(ChatMessage::tool_result(&call.id, &result_str));
            }
        }

        *self.last_usage.lock().expect("last_usage mutex") = accumulated_usage;
        anyhow::bail!("Max iterations ({}) reached", self.max_iterations)
    }

    fn last_usage(&self) -> Option<crate::kernel::LlmTokenUsage> {
        self.last_usage.lock().expect("last_usage mutex").clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::{ChatChoice, ChatResponse, MessageToolCall, MessageToolCallFunction};
    use crate::tool::Tool;
    use async_trait::async_trait;

    struct MockProviderNoTools;
    #[async_trait]
    impl ModelProvider for MockProviderNoTools {
        fn name(&self) -> &str {
            "mock-no-tools"
        }
        fn capabilities(&self) -> crate::providers::ModelCapabilities {
            crate::providers::ModelCapabilities {
                supports_tools: false,
                ..Default::default()
            }
        }
        async fn chat(&self, _request: ChatRequest) -> anyhow::Result<ChatResponse> {
            Ok(ChatResponse {
                id: "id".to_string(),
                choices: vec![ChatChoice {
                    index: 0,
                    message: ChatMessage::assistant("ok"),
                    finish_reason: Some("stop".to_string()),
                }],
                usage: None,
            })
        }
    }

    struct EchoTool;
    #[async_trait]
    impl Tool for EchoTool {
        fn name(&self) -> &str {
            "echo"
        }
        fn description(&self) -> &str {
            "Echoes input"
        }
        async fn execute(&self, args: Value) -> anyhow::Result<Value> {
            Ok(args.get("x").cloned().unwrap_or(Value::Null))
        }
    }

    #[tokio::test]
    async fn run_errors_when_tools_registered_but_provider_does_not_support_tools() {
        let provider = Arc::new(MockProviderNoTools);
        let callable =
            LlmCallable::with_provider("test", "You are helpful", provider).add_tool(EchoTool);

        let err = callable.run("hello").await.unwrap_err();
        assert!(
            err.to_string().contains("does not support native tools"),
            "expected error about supports_tools, got: {}",
            err
        );
    }

    /// Mock provider that returns tool_calls on first call, then final content when request includes tool results.
    struct MockProviderWithToolCalls {
        call_count: std::sync::atomic::AtomicUsize,
    }
    #[async_trait]
    impl ModelProvider for MockProviderWithToolCalls {
        fn name(&self) -> &str {
            "mock-with-tools"
        }
        fn capabilities(&self) -> crate::providers::ModelCapabilities {
            crate::providers::ModelCapabilities {
                supports_tools: true,
                ..Default::default()
            }
        }
        async fn chat(&self, request: ChatRequest) -> anyhow::Result<ChatResponse> {
            let n = self
                .call_count
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            let has_tool_result = request.messages.iter().any(|m| m.role == "tool");
            if !has_tool_result && n == 0 {
                return Ok(ChatResponse {
                    id: "id".to_string(),
                    choices: vec![ChatChoice {
                        index: 0,
                        message: ChatMessage::assistant_with_tool_calls(
                            None,
                            vec![MessageToolCall {
                                id: "call-1".to_string(),
                                call_type: "function".to_string(),
                                function: MessageToolCallFunction {
                                    name: "echo".to_string(),
                                    arguments: r#"{"x": "world"}"#.to_string(),
                                },
                            }],
                        ),
                        finish_reason: Some("tool_calls".to_string()),
                    }],
                    usage: None,
                });
            }
            Ok(ChatResponse {
                id: "id".to_string(),
                choices: vec![ChatChoice {
                    index: 0,
                    message: ChatMessage::assistant("Final: world"),
                    finish_reason: Some("stop".to_string()),
                }],
                usage: None,
            })
        }
    }

    #[tokio::test]
    async fn run_uses_native_tool_calls_and_returns_final_content() {
        let provider = Arc::new(MockProviderWithToolCalls {
            call_count: std::sync::atomic::AtomicUsize::new(0),
        });
        let callable = LlmCallable::with_provider("test", "You are helpful", provider)
            .add_tool(EchoTool)
            .max_iterations(5);

        let out = callable.run("hello").await.unwrap();
        assert_eq!(out, "Final: world");
    }
}