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
//! Multi-agent runtime management.
//!
//! Provides [`RuntimeSet`] for managing a collection of named runtimes,
//! one per agent. Handles the chicken-and-egg problem of inter-agent
//! delegation (tools need runtimes, runtimes need tools) by using a
//! shared `Arc<RwLock<HashMap>>` that is populated after all runtimes
//! are constructed.

use std::collections::HashMap;
use std::sync::Arc;

use tokio::sync::RwLock;

use crate::agent::AgentProfile;
use crate::context::{CharEstimator, ContextBudget, Tokenizer};
use crate::hook::HookRegistry;
use crate::policy::PolicyRegistry;
use crate::prompt::PromptBuilder;
use crate::provider::Provider;
use crate::runtime::{Runtime, RuntimeConfig};
use crate::store::SessionStore;
use crate::tool::ToolRegistry;
use crate::tools::delegation::DelegateToAgentTool;

/// A shared map of named runtimes, keyed by lowercase agent name.
pub type RuntimeMap<T> = Arc<RwLock<HashMap<String, Arc<Runtime<T>>>>>;

/// Configuration for building a [`RuntimeSet`].
pub struct RuntimeSetConfig {
    /// Maximum turns per agent.
    pub max_turns: usize,
    /// Maximum tokens per completion.
    pub max_tokens: Option<u32>,
    /// Temperature for completions.
    pub temperature: Option<f32>,
    /// Context budget for the context window.
    pub context_budget: ContextBudget,
    /// Whether to enable parallel tool execution.
    pub parallel_tool_execution: bool,
    /// Whether to register the `DelegateToAgentTool` for inter-agent
    /// delegation. Only useful when there are multiple agents.
    pub enable_delegation: bool,
}

impl Default for RuntimeSetConfig {
    fn default() -> Self {
        Self {
            max_turns: 10,
            max_tokens: None,
            temperature: None,
            context_budget: ContextBudget::default(),
            parallel_tool_execution: true,
            enable_delegation: true,
        }
    }
}

/// Result of building a runtime set.
pub struct RuntimeSetResult<T: Tokenizer> {
    /// The shared map of named runtimes (lowercase keys).
    pub runtimes: RuntimeMap<T>,
    /// The name of the default agent (first in the list).
    pub default_agent: String,
}

/// Builder for creating a set of named runtimes from agent profiles.
///
/// Handles the common pattern of:
/// 1. Creating a shared `RuntimeMap` upfront
/// 2. Building per-agent runtimes with shared provider/store
/// 3. Optionally registering `DelegateToAgentTool` for inter-agent delegation
/// 4. Populating the shared map after all runtimes are built
///
/// # Example
///
/// ```ignore
/// let agents = vec![
///     AgentProfile::new("Atlas").with_personality("helpful"),
///     AgentProfile::new("CodeBot").with_personality("precise coder"),
/// ];
///
/// let result = RuntimeSetBuilder::new()
///     .provider(my_provider)
///     .store(my_store)
///     .agents(agents)
///     .tool_factory(|_agent| {
///         let mut registry = ToolRegistry::new();
///         // register tools...
///         registry
///     })
///     .build()
///     .await;
///
/// // result.runtimes contains both agents
/// // result.default_agent is "Atlas"
/// ```
pub struct RuntimeSetBuilder<F>
where
    F: FnMut(&AgentProfile) -> ToolRegistry,
{
    agents: Vec<AgentProfile>,
    provider: Option<Arc<dyn Provider>>,
    store: Option<Arc<dyn SessionStore>>,
    tool_factory: Option<F>,
    prompt_builder: Option<PromptBuilder>,
    hook_factory: Option<Box<dyn FnMut(&AgentProfile) -> HookRegistry>>,
    config: RuntimeSetConfig,
}

impl RuntimeSetBuilder<fn(&AgentProfile) -> ToolRegistry> {
    /// Create a new builder with no configuration.
    pub fn new() -> RuntimeSetBuilder<fn(&AgentProfile) -> ToolRegistry> {
        RuntimeSetBuilder {
            agents: Vec::new(),
            provider: None,
            store: None,
            tool_factory: None,
            prompt_builder: None,
            hook_factory: None,
            config: RuntimeSetConfig::default(),
        }
    }
}

impl<F> RuntimeSetBuilder<F>
where
    F: FnMut(&AgentProfile) -> ToolRegistry,
{
    /// Set the agent profiles.
    pub fn agents(mut self, agents: Vec<AgentProfile>) -> Self {
        self.agents = agents;
        self
    }

    /// Set the shared provider for all agents.
    pub fn provider(mut self, provider: Arc<dyn Provider>) -> Self {
        self.provider = Some(provider);
        self
    }

    /// Set the shared session store for all agents.
    pub fn store(mut self, store: Arc<dyn SessionStore>) -> Self {
        self.store = Some(store);
        self
    }

    /// Set a factory function that creates a `ToolRegistry` for each agent.
    pub fn tool_factory<G>(self, factory: G) -> RuntimeSetBuilder<G>
    where
        G: FnMut(&AgentProfile) -> ToolRegistry,
    {
        RuntimeSetBuilder {
            agents: self.agents,
            provider: self.provider,
            store: self.store,
            tool_factory: Some(factory),
            prompt_builder: self.prompt_builder,
            hook_factory: self.hook_factory,
            config: self.config,
        }
    }

    /// Set a prompt builder for generating per-agent system prompts.
    pub fn prompt_builder(mut self, builder: PromptBuilder) -> Self {
        self.prompt_builder = Some(builder);
        self
    }

    /// Set a factory function that creates a `HookRegistry` for each agent.
    pub fn hook_factory(mut self, factory: impl FnMut(&AgentProfile) -> HookRegistry + 'static) -> Self {
        self.hook_factory = Some(Box::new(factory));
        self
    }

    /// Set the runtime configuration.
    pub fn config(mut self, config: RuntimeSetConfig) -> Self {
        self.config = config;
        self
    }

    /// Build the runtime set.
    ///
    /// Creates a `Runtime` for each agent profile, optionally registers
    /// `DelegateToAgentTool` for inter-agent delegation, and returns
    /// the shared runtime map.
    ///
    /// # Panics
    ///
    /// Panics if `provider` or `store` are not set, or if `agents` is empty.
    pub async fn build(mut self) -> RuntimeSetResult<CharEstimator> {
        let provider = self.provider.expect("provider is required");
        let store = self.store.expect("store is required");
        assert!(!self.agents.is_empty(), "at least one agent is required");

        let default_agent = self.agents[0].name.clone();
        let prompt_builder = self.prompt_builder.unwrap_or_default();

        // Create the shared map upfront so DelegateToAgentTool can reference it
        let runtimes: RuntimeMap<CharEstimator> = Arc::new(RwLock::new(HashMap::new()));
        let mut runtimes_map = HashMap::new();

        let enable_delegation = self.config.enable_delegation && self.agents.len() > 1;

        for agent in &self.agents {
            let system_prompt = prompt_builder.build(agent);

            let runtime_config = RuntimeConfig {
                system_prompt: Some(system_prompt),
                max_turns: self.config.max_turns,
                max_tokens: self.config.max_tokens,
                temperature: self.config.temperature,
                context_budget: self.config.context_budget.clone(),
                parallel_tool_execution: self.config.parallel_tool_execution,
            };

            let mut tools = if let Some(ref mut factory) = self.tool_factory {
                factory(agent)
            } else {
                ToolRegistry::new()
            };

            // Register inter-agent delegation if enabled
            if enable_delegation {
                tools.register(Box::new(DelegateToAgentTool::new(
                    runtimes.clone(),
                    agent.name.clone(),
                )));
            }

            let hooks = if let Some(ref mut factory) = self.hook_factory {
                factory(agent)
            } else {
                HookRegistry::default()
            };

            let mut rt = Runtime::new(
                provider.clone(),
                store.clone(),
                tools,
                PolicyRegistry::default(),
                CharEstimator::default(),
                runtime_config,
            );
            rt.set_hooks(hooks);

            let key = agent.name.to_lowercase();
            runtimes_map.insert(key, Arc::new(rt));
        }

        // Populate the shared map now that all runtimes are built
        *runtimes.write().await = runtimes_map;

        RuntimeSetResult {
            runtimes,
            default_agent,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::Message;
    use crate::provider::{CompletionRequest, CompletionResponse, FinishReason, ProviderError, Usage};
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicUsize, Ordering};

    struct FixedProvider {
        call_count: AtomicUsize,
    }

    #[async_trait]
    impl Provider for FixedProvider {
        async fn complete(
            &self,
            _request: CompletionRequest,
        ) -> Result<CompletionResponse, ProviderError> {
            self.call_count.fetch_add(1, Ordering::SeqCst);
            Ok(CompletionResponse {
                message: Message::assistant("ok"),
                usage: Usage::default(),
                finish_reason: FinishReason::Stop,
            })
        }
    }

    #[tokio::test]
    async fn build_single_agent() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        let result = RuntimeSetBuilder::new()
            .agents(vec![AgentProfile::new("Atlas")])
            .provider(provider)
            .store(store)
            .config(RuntimeSetConfig {
                enable_delegation: false,
                ..Default::default()
            })
            .build()
            .await;

        assert_eq!(result.default_agent, "Atlas");
        let map = result.runtimes.read().await;
        assert_eq!(map.len(), 1);
        assert!(map.contains_key("atlas"));
    }

    #[tokio::test]
    async fn build_multiple_agents() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        let result = RuntimeSetBuilder::new()
            .agents(vec![
                AgentProfile::new("Atlas").with_personality("helpful"),
                AgentProfile::new("CodeBot").with_personality("precise"),
            ])
            .provider(provider)
            .store(store)
            .build()
            .await;

        assert_eq!(result.default_agent, "Atlas");
        let map = result.runtimes.read().await;
        assert_eq!(map.len(), 2);
        assert!(map.contains_key("atlas"));
        assert!(map.contains_key("codebot"));
    }

    #[tokio::test]
    async fn build_with_tool_factory() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        let result = RuntimeSetBuilder::new()
            .agents(vec![AgentProfile::new("Atlas")])
            .provider(provider)
            .store(store)
            .tool_factory(|_agent| {
                // Could register tools here
                ToolRegistry::new()
            })
            .config(RuntimeSetConfig {
                enable_delegation: false,
                ..Default::default()
            })
            .build()
            .await;

        let map = result.runtimes.read().await;
        assert_eq!(map.len(), 1);
    }

    #[tokio::test]
    async fn build_with_custom_prompt() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        let mut prompt_builder = PromptBuilder::new();
        prompt_builder.set_channel_context("a Discord server");

        let result = RuntimeSetBuilder::new()
            .agents(vec![AgentProfile::new("Atlas")])
            .provider(provider)
            .store(store)
            .prompt_builder(prompt_builder)
            .config(RuntimeSetConfig {
                enable_delegation: false,
                ..Default::default()
            })
            .build()
            .await;

        let map = result.runtimes.read().await;
        assert_eq!(map.len(), 1);
    }

    #[tokio::test]
    async fn default_agent_is_first() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        let result = RuntimeSetBuilder::new()
            .agents(vec![
                AgentProfile::new("SecondBot"),
                AgentProfile::new("FirstBot"),
            ])
            .provider(provider)
            .store(store)
            .config(RuntimeSetConfig {
                enable_delegation: false,
                ..Default::default()
            })
            .build()
            .await;

        // Default is the first in the list, not alphabetical
        assert_eq!(result.default_agent, "SecondBot");
    }

    #[tokio::test]
    #[should_panic(expected = "at least one agent")]
    async fn build_with_no_agents_panics() {
        let provider = Arc::new(FixedProvider { call_count: AtomicUsize::new(0) });
        let store = Arc::new(crate::store::InMemoryStore::new());

        RuntimeSetBuilder::new()
            .agents(vec![])
            .provider(provider)
            .store(store)
            .build()
            .await;
    }
}