orra 0.0.2

Context-aware agent session management for any application
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
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::sync::Arc;

use async_trait::async_trait;

use crate::message::Message;
use crate::provider::{
    CompletionRequest, CompletionResponse, FinishReason, Provider, Usage,
};
use crate::tool::{Tool, ToolDefinition, ToolError, ToolRegistry};

// ---------------------------------------------------------------------------
// Sub-agent configuration
// ---------------------------------------------------------------------------

/// Configuration for spawned sub-agents.
#[derive(Debug, Clone)]
pub struct SubAgentConfig {
    /// Maximum number of LLM turns the sub-agent can take before stopping.
    pub max_turns: usize,

    /// Default system prompt to use if the caller doesn't specify one.
    pub default_system_prompt: Option<String>,

    /// Max tokens per completion call.
    pub max_tokens: Option<u32>,

    /// Temperature for completions.
    pub temperature: Option<f32>,
}

impl Default for SubAgentConfig {
    fn default() -> Self {
        Self {
            max_turns: 5,
            default_system_prompt: None,
            max_tokens: None,
            temperature: None,
        }
    }
}

// ---------------------------------------------------------------------------
// Sub-agent runner
// ---------------------------------------------------------------------------

/// Minimal agent loop that runs a task to completion without session persistence.
/// Used internally by the spawn_agent tool.
pub struct SubAgentRunner {
    provider: Arc<dyn Provider>,
    tools: Arc<ToolRegistry>,
    config: SubAgentConfig,
}

impl SubAgentRunner {
    pub fn new(
        provider: Arc<dyn Provider>,
        tools: Arc<ToolRegistry>,
        config: SubAgentConfig,
    ) -> Self {
        Self {
            provider,
            tools,
            config,
        }
    }

    /// Run a sub-agent with the given task. Returns the final assistant message.
    pub async fn run(
        &self,
        task: &str,
        system_prompt: Option<&str>,
    ) -> Result<SubAgentResult, SubAgentError> {
        let sys_prompt = system_prompt
            .or(self.config.default_system_prompt.as_deref())
            .unwrap_or("You are a helpful sub-agent. Complete the given task concisely.");

        let mut messages = vec![
            Message::system(sys_prompt),
            Message::user(task),
        ];

        let mut total_usage = Usage::default();
        let tool_defs = self.tools.definitions();

        for turn in 0..self.config.max_turns {
            let request = CompletionRequest {
                messages: messages.clone(),
                tools: tool_defs.clone(),
                max_tokens: self.config.max_tokens,
                temperature: self.config.temperature,
                model: None,
            };

            let response = self
                .provider
                .complete(request)
                .await
                .map_err(|e| SubAgentError::Provider(e.to_string()))?;

            total_usage.input_tokens += response.usage.input_tokens;
            total_usage.output_tokens += response.usage.output_tokens;

            messages.push(response.message.clone());

            // If the model wants to use tools, execute them and continue the loop
            if response.finish_reason == FinishReason::ToolUse
                && !response.message.tool_calls.is_empty()
            {
                let mut results = Vec::new();

                for call in &response.message.tool_calls {
                    let result = match self.tools.get(&call.name) {
                        Some(tool) => match tool.execute(call.arguments.clone()).await {
                            Ok(output) => crate::message::ToolResult {
                                call_id: call.id.clone(),
                                content: output,
                                is_error: false,
                            },
                            Err(e) => crate::message::ToolResult {
                                call_id: call.id.clone(),
                                content: format!("Error: {}", e),
                                is_error: true,
                            },
                        },
                        None => crate::message::ToolResult {
                            call_id: call.id.clone(),
                            content: format!("Unknown tool: {}", call.name),
                            is_error: true,
                        },
                    };
                    results.push(result);
                }

                messages.push(Message::tool_result(results));
                continue;
            }

            // No tool use — we have a final answer
            return Ok(SubAgentResult {
                content: response.message.content.clone(),
                turns_used: turn + 1,
                usage: total_usage,
            });
        }

        // Exceeded max turns — return whatever we have
        let last_content = messages
            .iter()
            .rev()
            .find(|m| m.role == crate::message::Role::Assistant)
            .map(|m| m.content.clone())
            .unwrap_or_default();

        Ok(SubAgentResult {
            content: last_content,
            turns_used: self.config.max_turns,
            usage: total_usage,
        })
    }
}

// ---------------------------------------------------------------------------
// Results and errors
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct SubAgentResult {
    pub content: String,
    pub turns_used: usize,
    pub usage: Usage,
}

#[derive(Debug, thiserror::Error)]
pub enum SubAgentError {
    #[error("provider error: {0}")]
    Provider(String),

    #[error("tool error: {0}")]
    Tool(String),
}

// ---------------------------------------------------------------------------
// SpawnAgent tool
// ---------------------------------------------------------------------------

/// Tool that lets the main agent delegate a subtask to a temporary sub-agent.
/// The sub-agent gets its own conversation context and can use a subset of
/// available tools. This is useful for breaking complex tasks into
/// independent pieces that can be handled in isolation.
pub struct SpawnAgentTool {
    runner: Arc<SubAgentRunner>,
}

impl SpawnAgentTool {
    pub fn new(runner: Arc<SubAgentRunner>) -> Self {
        Self { runner }
    }
}

#[async_trait]
impl Tool for SpawnAgentTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "spawn_agent".into(),
            description: "Delegate a task to a sub-agent that runs independently with its own \
                          conversation context. The sub-agent can use tools and will return its \
                          final response. Use this for tasks that benefit from focused, \
                          independent reasoning."
                .into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "task": {
                        "type": "string",
                        "description": "The task for the sub-agent to complete"
                    },
                    "system_prompt": {
                        "type": "string",
                        "description": "Optional system prompt to shape the sub-agent's behavior"
                    }
                },
                "required": ["task"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let task = input
            .get("task")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'task'".into()))?;

        let system_prompt = input.get("system_prompt").and_then(|v| v.as_str());

        let result = self
            .runner
            .run(task, system_prompt)
            .await
            .map_err(|e| ToolError::ExecutionFailed(e.to_string()))?;

        Ok(format!(
            "[Sub-agent completed in {} turn(s), {} tokens used]\n\n{}",
            result.turns_used,
            result.usage.total_tokens(),
            result.content,
        ))
    }
}

// ---------------------------------------------------------------------------
// DelegateToAgent tool — route a task to a named agent's runtime
// ---------------------------------------------------------------------------

/// Tool that lets an agent delegate a task to another named agent.
/// Unlike `SpawnAgentTool`, this uses an existing agent's full runtime
/// (with its own system prompt, model, and tools) rather than creating
/// an ephemeral sub-agent.
pub struct DelegateToAgentTool {
    /// Map of lowercase agent names to their runtimes.
    runtimes: Arc<tokio::sync::RwLock<std::collections::HashMap<String, Arc<crate::runtime::Runtime<crate::context::CharEstimator>>>>>,
    /// Name of the agent that owns this tool (to prevent self-delegation).
    self_name: String,
}

impl DelegateToAgentTool {
    pub fn new(
        runtimes: Arc<tokio::sync::RwLock<std::collections::HashMap<String, Arc<crate::runtime::Runtime<crate::context::CharEstimator>>>>>,
        self_name: String,
    ) -> Self {
        Self { runtimes, self_name }
    }
}

#[async_trait]
impl Tool for DelegateToAgentTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "delegate_to_agent".into(),
            description: "Delegate a task to another named agent. The target agent \
                          has its own personality, system prompt, and capabilities. \
                          Use this when another agent is better suited for a task."
                .into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "agent": {
                        "type": "string",
                        "description": "Name of the target agent (case-insensitive)"
                    },
                    "task": {
                        "type": "string",
                        "description": "The task or question to delegate to the agent"
                    }
                },
                "required": ["agent", "task"]
            }),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
        let agent_name = input
            .get("agent")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'agent'".into()))?;

        let task = input
            .get("task")
            .and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidInput("missing 'task'".into()))?;

        let key = agent_name.to_lowercase();

        // Prevent self-delegation loops
        if key == self.self_name.to_lowercase() {
            return Err(ToolError::ExecutionFailed(
                "cannot delegate to yourself".into(),
            ));
        }

        let runtimes = self.runtimes.read().await;
        let runtime = runtimes
            .get(&key)
            .ok_or_else(|| {
                let available: Vec<&str> = runtimes.keys().map(|k| k.as_str()).collect();
                ToolError::ExecutionFailed(format!(
                    "agent '{}' not found. Available agents: {}",
                    agent_name,
                    available.join(", ")
                ))
            })?
            .clone();
        drop(runtimes);

        // Create a temporary namespace for this delegation
        let ns = crate::namespace::Namespace::parse(&format!(
            "delegation:{}:{}",
            self.self_name.to_lowercase(),
            uuid::Uuid::new_v4()
        ));

        let result = runtime
            .run(&ns, crate::message::Message::user(task))
            .await
            .map_err(|e| ToolError::ExecutionFailed(format!("agent '{}' failed: {}", agent_name, e)))?;

        Ok(format!(
            "[Agent '{}' responded ({} turns, {} tokens)]\n\n{}",
            agent_name,
            result.turns.len(),
            result.total_usage.total_tokens(),
            result.final_message.content,
        ))
    }
}

// ---------------------------------------------------------------------------
// Convenience registration
// ---------------------------------------------------------------------------

/// Create a sub-agent runner with the given provider and tool set.
pub fn create_runner(
    provider: Arc<dyn Provider>,
    tools: Arc<ToolRegistry>,
    config: SubAgentConfig,
) -> Arc<SubAgentRunner> {
    Arc::new(SubAgentRunner::new(provider, tools, config))
}

/// Register the spawn_agent tool into a tool registry.
pub fn register_tool(registry: &mut ToolRegistry, runner: &Arc<SubAgentRunner>) {
    registry.register(Box::new(SpawnAgentTool::new(runner.clone())));
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::{Message, ToolCall};
    use crate::provider::{CompletionResponse, ProviderError};

    // A mock provider that returns a fixed response
    struct FixedProvider {
        response: String,
    }

    #[async_trait]
    impl Provider for FixedProvider {
        async fn complete(
            &self,
            _request: CompletionRequest,
        ) -> Result<CompletionResponse, ProviderError> {
            Ok(CompletionResponse {
                message: Message::assistant(&self.response),
                usage: Usage {
                    input_tokens: 10,
                    output_tokens: 5,
                },
                finish_reason: FinishReason::Stop,
            })
        }
    }

    // A provider that makes a tool call on the first turn, then gives a final answer
    struct ToolUsingProvider {
        call_count: std::sync::atomic::AtomicUsize,
    }

    #[async_trait]
    impl Provider for ToolUsingProvider {
        async fn complete(
            &self,
            _request: CompletionRequest,
        ) -> Result<CompletionResponse, ProviderError> {
            let count = self
                .call_count
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);

            if count == 0 {
                // First call: make a tool call
                Ok(CompletionResponse {
                    message: Message::assistant_with_tool_calls(
                        "Let me look that up.",
                        vec![ToolCall {
                            id: "call_1".into(),
                            name: "echo".into(),
                            arguments: serde_json::json!({"text": "hello world"}),
                        }],
                    ),
                    usage: Usage {
                        input_tokens: 10,
                        output_tokens: 8,
                    },
                    finish_reason: FinishReason::ToolUse,
                })
            } else {
                // Second call: final answer
                Ok(CompletionResponse {
                    message: Message::assistant("The answer is: hello world"),
                    usage: Usage {
                        input_tokens: 20,
                        output_tokens: 6,
                    },
                    finish_reason: FinishReason::Stop,
                })
            }
        }
    }

    // Simple echo tool for tests
    struct EchoTool;

    #[async_trait]
    impl Tool for EchoTool {
        fn definition(&self) -> ToolDefinition {
            ToolDefinition {
                name: "echo".into(),
                description: "Echoes input back".into(),
                input_schema: serde_json::json!({
                    "type": "object",
                    "properties": {
                        "text": {"type": "string"}
                    },
                    "required": ["text"]
                }),
            }
        }

        async fn execute(&self, input: serde_json::Value) -> Result<String, ToolError> {
            let text = input
                .get("text")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::InvalidInput("missing 'text'".into()))?;
            Ok(text.to_string())
        }
    }

    fn make_tools() -> Arc<ToolRegistry> {
        let mut registry = ToolRegistry::new();
        registry.register(Box::new(EchoTool));
        Arc::new(registry)
    }

    #[tokio::test]
    async fn sub_agent_simple_response() {
        let provider = Arc::new(FixedProvider {
            response: "42".into(),
        });
        let tools = make_tools();
        let config = SubAgentConfig::default();

        let runner = SubAgentRunner::new(provider, tools, config);
        let result = runner.run("What is the meaning of life?", None).await.unwrap();

        assert_eq!(result.content, "42");
        assert_eq!(result.turns_used, 1);
        assert_eq!(result.usage.total_tokens(), 15);
    }

    #[tokio::test]
    async fn sub_agent_with_tool_use() {
        let provider = Arc::new(ToolUsingProvider {
            call_count: std::sync::atomic::AtomicUsize::new(0),
        });
        let tools = make_tools();
        let config = SubAgentConfig::default();

        let runner = SubAgentRunner::new(provider, tools, config);
        let result = runner.run("Echo hello world", None).await.unwrap();

        assert_eq!(result.content, "The answer is: hello world");
        assert_eq!(result.turns_used, 2);
        assert_eq!(result.usage.input_tokens, 30);
        assert_eq!(result.usage.output_tokens, 14);
    }

    #[tokio::test]
    async fn sub_agent_custom_system_prompt() {
        let provider = Arc::new(FixedProvider {
            response: "Done.".into(),
        });
        let tools = make_tools();
        let config = SubAgentConfig::default();

        let runner = SubAgentRunner::new(provider, tools, config);
        let result = runner
            .run("Do the thing", Some("You are a specialist."))
            .await
            .unwrap();

        assert_eq!(result.content, "Done.");
    }

    #[tokio::test]
    async fn spawn_agent_tool_works() {
        let provider = Arc::new(FixedProvider {
            response: "Result from sub-agent".into(),
        });
        let tools = make_tools();
        let runner = Arc::new(SubAgentRunner::new(
            provider,
            tools,
            SubAgentConfig::default(),
        ));

        let tool = SpawnAgentTool::new(runner);
        let output = tool
            .execute(serde_json::json!({
                "task": "Summarize this document"
            }))
            .await
            .unwrap();

        assert!(output.contains("Result from sub-agent"));
        assert!(output.contains("1 turn(s)"));
    }

    #[tokio::test]
    async fn spawn_agent_tool_missing_task() {
        let provider = Arc::new(FixedProvider {
            response: "ok".into(),
        });
        let tools = make_tools();
        let runner = Arc::new(SubAgentRunner::new(
            provider,
            tools,
            SubAgentConfig::default(),
        ));

        let tool = SpawnAgentTool::new(runner);
        let err = tool.execute(serde_json::json!({})).await.unwrap_err();
        assert!(matches!(err, ToolError::InvalidInput(_)));
    }

    #[test]
    fn tool_definition_is_valid() {
        let provider: Arc<dyn Provider> = Arc::new(FixedProvider {
            response: "ok".into(),
        });
        let tools = make_tools();
        let runner = Arc::new(SubAgentRunner::new(
            provider,
            tools,
            SubAgentConfig::default(),
        ));

        let tool = SpawnAgentTool::new(runner);
        let def = tool.definition();
        assert_eq!(def.name, "spawn_agent");
        assert!(def.input_schema["required"]
            .as_array()
            .unwrap()
            .contains(&serde_json::json!("task")));
    }

    #[test]
    fn sub_agent_config_defaults() {
        let config = SubAgentConfig::default();
        assert_eq!(config.max_turns, 5);
        assert!(config.default_system_prompt.is_none());
        assert!(config.max_tokens.is_none());
        assert!(config.temperature.is_none());
    }
}