marona 1.1.2

Universal Model Gateway and MCP/Skill SDK with mandatory project-scoped Marona authentication
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
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};

use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{json, Map, Value};
use url::form_urlencoded::byte_serialize;

use crate::{
    CreateRequest, Error, EventStream, FunctionTool, Input, Marona, McpConnection, Result,
    RoutingOptions, RunEvent, RuntimeResponse, ToolChoice, ToolContext, VoiceOptions,
};

#[async_trait]
pub trait LifecycleHooks: Send + Sync {
    async fn on_start(&self, _context: &AgentContext, _input: &Input) -> Result<()> {
        Ok(())
    }
    async fn on_event(&self, _context: &AgentContext, _event: &RunEvent) -> Result<()> {
        Ok(())
    }
    async fn on_complete(&self, _context: &AgentContext, _result: &RunResult) -> Result<()> {
        Ok(())
    }
    async fn on_error(&self, _context: &AgentContext, _error: &Error) -> Result<()> {
        Ok(())
    }
}

#[derive(Debug, Clone, Default)]
pub struct AgentContext {
    pub user_id: Option<String>,
    pub session_id: String,
    pub agent_name: String,
    pub metadata: Map<String, Value>,
}

#[derive(Clone)]
pub enum AgentTool {
    Function(FunctionTool),
    Delegation {
        name: String,
        description: String,
        agent: Arc<Agent>,
    },
}

impl std::fmt::Debug for AgentTool {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Function(value) => formatter.debug_tuple("Function").field(value).finish(),
            Self::Delegation {
                name,
                description,
                agent,
            } => formatter
                .debug_struct("Delegation")
                .field("name", name)
                .field("description", description)
                .field("agent", &agent.name)
                .finish(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Handoff {
    pub name: String,
    pub description: String,
    pub agent: Arc<Agent>,
}

pub fn handoff(
    agent: Arc<Agent>,
    name: impl Into<String>,
    description: impl Into<String>,
) -> Handoff {
    Handoff {
        agent,
        name: name.into(),
        description: description.into(),
    }
}

#[derive(Clone)]
pub struct Agent {
    pub(crate) marona: Marona,
    pub name: String,
    pub description: Option<String>,
    pub model: String,
    pub instructions: String,
    pub hub_tools: Option<McpConnection>,
    pub tools: Vec<AgentTool>,
    pub handoffs: Vec<Handoff>,
    pub peers: Vec<crate::A2APeer>,
    pub tool_choice: ToolChoice,
    pub voice: Option<VoiceOptions>,
    pub modalities: Vec<String>,
    pub output_modalities: Vec<String>,
    pub routing: Option<RoutingOptions>,
    pub metadata: Map<String, Value>,
    pub max_turns: u32,
    pub hooks: Option<Arc<dyn LifecycleHooks>>,
}

impl std::fmt::Debug for Agent {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("Agent")
            .field("name", &self.name)
            .field("model", &self.model)
            .field("tools", &self.tools)
            .field("handoffs", &self.handoffs)
            .field("peers", &self.peers)
            .finish_non_exhaustive()
    }
}

impl Agent {
    pub fn new(marona: &Marona, name: impl Into<String>) -> Self {
        Self {
            marona: marona.clone(),
            name: name.into(),
            description: None,
            model: "marona/auto".into(),
            instructions: String::new(),
            hub_tools: None,
            tools: vec![],
            handoffs: vec![],
            peers: vec![],
            tool_choice: ToolChoice::Auto,
            voice: None,
            modalities: vec![],
            output_modalities: vec![],
            routing: None,
            metadata: Map::new(),
            max_turns: 20,
            hooks: None,
        }
    }
    pub fn description(mut self, value: impl Into<String>) -> Self {
        self.description = Some(value.into());
        self
    }
    pub fn model(mut self, value: impl Into<String>) -> Self {
        self.model = value.into();
        self
    }
    pub fn instructions(mut self, value: impl Into<String>) -> Self {
        self.instructions = value.into();
        self
    }
    pub fn hub_tools(mut self, value: McpConnection) -> Self {
        self.hub_tools = Some(value);
        self
    }
    pub fn tool(mut self, value: FunctionTool) -> Self {
        self.tools.push(AgentTool::Function(value));
        self
    }
    pub fn agent_tool(mut self, value: AgentTool) -> Self {
        self.tools.push(value);
        self
    }
    pub fn handoff(mut self, value: Handoff) -> Self {
        self.handoffs.push(value);
        self
    }
    pub fn peer(mut self, value: crate::A2APeer) -> Self {
        self.peers.push(value);
        self
    }
    pub fn tool_choice(mut self, value: ToolChoice) -> Self {
        self.tool_choice = value;
        self
    }
    pub fn routing(mut self, value: RoutingOptions) -> Self {
        self.routing = Some(value);
        self
    }
    pub fn voice(mut self, value: VoiceOptions) -> Self {
        self.voice = Some(value);
        self
    }
    pub fn modalities(mut self, values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.modalities = values.into_iter().map(Into::into).collect();
        self
    }
    pub fn output_modalities(
        mut self,
        values: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.output_modalities = values.into_iter().map(Into::into).collect();
        self
    }
    pub fn metadata(mut self, value: Map<String, Value>) -> Self {
        self.metadata = value;
        self
    }
    pub fn hooks(mut self, value: Arc<dyn LifecycleHooks>) -> Self {
        self.hooks = Some(value);
        self
    }
    pub fn max_turns(mut self, value: u32) -> Self {
        self.max_turns = value;
        self
    }
    pub fn as_tool(
        self: &Arc<Self>,
        name: impl Into<String>,
        description: impl Into<String>,
    ) -> AgentTool {
        AgentTool::Delegation {
            name: name.into(),
            description: description.into(),
            agent: self.clone(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct RunResult {
    pub output: Value,
    pub last_agent: String,
    pub active_agent: String,
    pub agent_runs: Vec<crate::AgentRun>,
    pub usage: Map<String, Value>,
    pub routing: Map<String, Value>,
    pub trace_id: Option<String>,
    pub a2a_events: Vec<Value>,
    pub response: RuntimeResponse,
}

pub struct Runner;

impl Runner {
    pub async fn run(
        agent: &Agent,
        input: impl Into<Input>,
        user_id: Option<&str>,
        session_id: Option<&str>,
    ) -> Result<RunResult> {
        validate_graph(agent)?;
        let input = input.into();
        let session_id = session_id.unwrap_or("default").to_owned();
        let context = AgentContext {
            user_id: user_id.map(str::to_owned),
            session_id: session_id.clone(),
            agent_name: agent.name.clone(),
            metadata: agent.metadata.clone(),
        };
        if let Some(hooks) = &agent.hooks {
            hooks.on_start(&context, &input).await?;
        }
        let conversation_id = scoped_conversation(user_id.unwrap_or("anonymous"), &session_id);
        let mut request = request_for(agent, input, user_id, &conversation_id);
        let functions = function_map(agent);
        let response = loop {
            match agent.marona.responses.create(request.clone()).await {
                Ok(response) => {
                    let calls = response
                        .agent
                        .get("function_tool_calls")
                        .and_then(Value::as_array)
                        .cloned()
                        .unwrap_or_default();
                    if calls.is_empty() {
                        break response;
                    }
                    let mut results = Vec::new();
                    for call in calls {
                        let name = call.get("name").and_then(Value::as_str).unwrap_or("");
                        let (tool, owner) = functions.get(name).ok_or_else(|| {
                            Error::Tool(format!("function tool '{name}' is unavailable"))
                        })?;
                        let arguments = call.get("arguments").cloned().unwrap_or_else(|| json!({}));
                        let output = tool
                            .invoke(arguments.clone(), tool_context(owner, user_id, &session_id))
                            .await?;
                        results.push(json!({"call_id":call.get("call_id"),"name":name,"arguments":arguments,"output":output}));
                    }
                    request
                        .metadata
                        .insert("marona_function_tool_results".into(), json!(results));
                }
                Err(error) => {
                    if let Some(hooks) = &agent.hooks {
                        hooks.on_error(&context, &error).await?;
                    }
                    return Err(error);
                }
            }
        };
        let active = response
            .agent
            .get("active_agent")
            .and_then(Value::as_str)
            .unwrap_or(&agent.name)
            .to_owned();
        let last = response
            .agent
            .get("last_agent")
            .and_then(Value::as_str)
            .unwrap_or(&active)
            .to_owned();
        let runs = response
            .agent
            .get("runs")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .filter_map(|value| serde_json::from_value(value.clone()).ok())
            .collect();
        let result = RunResult {
            output: response.output.clone(),
            last_agent: last,
            active_agent: active,
            agent_runs: runs,
            usage: response.usage.clone(),
            routing: response.routing.clone(),
            trace_id: response
                .agent
                .get("trace_id")
                .and_then(Value::as_str)
                .map(str::to_owned),
            a2a_events: vec![],
            response,
        };
        if let Some(hooks) = &agent.hooks {
            hooks.on_complete(&context, &result).await?;
        }
        Ok(result)
    }

    pub async fn run_typed<I, O>(
        agent: &Agent,
        input: &I,
        user_id: Option<&str>,
        session_id: Option<&str>,
    ) -> Result<O>
    where
        I: Serialize + JsonSchema,
        O: DeserializeOwned + JsonSchema,
    {
        let input_value =
            serde_json::to_value(input).map_err(|e| Error::Validation(e.to_string()))?;
        let result = Self::run(agent, Input::Message(json!({"role":"user","content":[{"type":"input_text","text":input_value.to_string()}]})), user_id, session_id).await?;
        serde_json::from_value(result.output)
            .map_err(|e| Error::Validation(format!("output validation failed: {e}")))
    }

    pub async fn stream(
        agent: &Agent,
        input: impl Into<Input>,
        user_id: Option<&str>,
        session_id: Option<&str>,
    ) -> Result<EventStream> {
        validate_graph(agent)?;
        let conversation = scoped_conversation(
            user_id.unwrap_or("anonymous"),
            session_id.unwrap_or("default"),
        );
        agent
            .marona
            .responses
            .stream(request_for(agent, input.into(), user_id, &conversation))
            .await
    }
}

fn request_for(
    agent: &Agent,
    input: Input,
    user_id: Option<&str>,
    conversation_id: &str,
) -> CreateRequest {
    let mut request = CreateRequest::new(agent.model.clone(), input);
    request.tools = agent.hub_tools.clone();
    request.user_id = user_id.map(str::to_owned);
    request.conversation_id = Some(conversation_id.to_owned());
    request.developer_instructions = Some(agent.instructions.clone());
    request.routing = agent.routing.clone();
    request.tool_choice = agent.tool_choice;
    request.metadata.insert("marona_agent".into(), json!({"name":agent.name,"modalities":agent.modalities,"output_modalities":agent.output_modalities,"voice":agent.voice,"tool_choice":agent.tool_choice,"tools_enabled":agent.hub_tools.is_some() || !agent.tools.is_empty() || !agent.peers.is_empty()}));
    request
        .metadata
        .insert("marona_multi_agent".into(), graph_value(agent));
    let functions = function_map(agent)
        .values()
        .map(|(tool, _)| tool.definition())
        .collect::<Vec<_>>();
    if !functions.is_empty() {
        request
            .metadata
            .insert("marona_function_tools".into(), json!(functions));
    }
    request
}

pub(crate) fn function_map(agent: &Agent) -> HashMap<String, (FunctionTool, Agent)> {
    fn visit(
        agent: &Agent,
        values: &mut HashMap<String, (FunctionTool, Agent)>,
        seen: &mut HashSet<String>,
    ) {
        if !seen.insert(agent.name.clone()) {
            return;
        }
        for item in &agent.tools {
            match item {
                AgentTool::Function(tool) => {
                    values.insert(tool.name.clone(), (tool.clone(), agent.clone()));
                }
                AgentTool::Delegation { agent, .. } => visit(agent, values, seen),
            }
        }
        for peer in &agent.peers {
            let tool = peer.function_tool();
            values.insert(tool.name.clone(), (tool, agent.clone()));
        }
        for handoff in &agent.handoffs {
            visit(&handoff.agent, values, seen);
        }
    }
    let mut values = HashMap::new();
    visit(agent, &mut values, &mut HashSet::new());
    values
}

pub(crate) fn tool_context(agent: &Agent, user_id: Option<&str>, session_id: &str) -> ToolContext {
    ToolContext {
        user_id: user_id.map(str::to_owned),
        session_id: session_id.to_owned(),
        developer_id: None,
        permissions: agent
            .metadata
            .get("permissions")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .filter_map(Value::as_str)
            .map(str::to_owned)
            .collect(),
        approved_tools: agent
            .metadata
            .get("approved_tools")
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
            .filter_map(Value::as_str)
            .map(str::to_owned)
            .collect(),
        metadata: agent.metadata.clone(),
    }
}

fn scoped_conversation(user: &str, session: &str) -> String {
    ["rust", user, session]
        .into_iter()
        .map(|value| byte_serialize(value.as_bytes()).collect::<String>())
        .collect::<Vec<_>>()
        .join(":")
}

pub(crate) fn graph_value(agent: &Agent) -> Value {
    let (root, agents) = collect_agents(agent);
    json!({"version":1,"root":root,"agents":agents,"limits":{"max_turns":agent.max_turns,"max_agent_calls":10,"max_handoffs":5,"max_depth":4,"timeout_ms":60000}})
}

#[cfg(feature = "realtime")]
pub(crate) fn realtime_tool_definitions(agent: &Agent) -> Vec<Value> {
    let mut tools = Vec::new();
    for value in &agent.tools {
        match value {
            AgentTool::Function(tool) => tools.push(provider_tool(
                &tool.name,
                &tool.description,
                tool.input_schema.clone(),
            )),
            AgentTool::Delegation {
                name, description, ..
            } => tools.push(provider_tool(name, description, delegated_schema())),
        }
    }
    for peer in &agent.peers {
        let tool = peer.function_tool();
        tools.push(provider_tool(
            &tool.name,
            &tool.description,
            tool.input_schema,
        ));
    }
    for value in &agent.handoffs {
        tools.push(provider_tool(
            &value.name,
            &value.description,
            delegated_schema(),
        ));
    }
    tools
}

#[cfg(feature = "realtime")]
fn provider_tool(name: &str, description: &str, parameters: Value) -> Value {
    json!({
        "type": "function",
        "name": name,
        "description": description,
        "parameters": parameters,
    })
}

#[cfg(feature = "realtime")]
fn delegated_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "input": {
                "type": "string",
                "description": "The bounded task or transfer context for the target agent."
            }
        },
        "required": ["input"],
        "additionalProperties": false,
    })
}

fn collect_agents(root: &Agent) -> (String, Map<String, Value>) {
    fn visit(
        agent: &Agent,
        values: &mut Map<String, Value>,
        ids: &mut HashMap<String, String>,
    ) -> String {
        if let Some(id) = ids.get(&agent.name) {
            return id.clone();
        }
        let id = format!("agent_{}", ids.len());
        ids.insert(agent.name.clone(), id.clone());
        let mut delegations = Vec::new();
        let mut functions = Vec::new();
        for tool in &agent.tools {
            match tool {
                AgentTool::Function(value) => functions.push(value.definition()),
                AgentTool::Delegation {
                    name,
                    description,
                    agent,
                } => {
                    let target = visit(agent, values, ids);
                    delegations.push(json!({"name":name,"description":description,"target":target,"history":"none","timeout_ms":60000}));
                }
            }
        }
        for peer in &agent.peers {
            functions.push(peer.function_tool().definition());
        }
        let handoffs = agent.handoffs.iter().map(|value| {
            let target = visit(&value.agent, values, ids);
            json!({"name":value.name,"description":value.description,"target":target,"history":"full"})
        }).collect::<Vec<_>>();
        values.insert(id.clone(), json!({"name":agent.name,"description":agent.description,"model":agent.model,"instructions":agent.instructions,
            "routing":agent.routing,"tool_choice":agent.tool_choice,"max_turns":agent.max_turns,"modalities":agent.modalities,
            "output_modalities":agent.output_modalities,"voice":agent.voice,"hub_tools":agent.hub_tools.is_some(),
            "function_tools":functions,"delegations":delegations,"handoffs":handoffs,"metadata":agent.metadata}));
        id
    }
    let mut values = Map::new();
    let root_id = visit(root, &mut values, &mut HashMap::new());
    (root_id, values)
}

fn validate_graph(root: &Agent) -> Result<()> {
    fn visit(
        agent: &Agent,
        visiting: &mut HashSet<String>,
        complete: &mut HashSet<String>,
    ) -> Result<()> {
        if complete.contains(&agent.name) {
            return Ok(());
        }
        if !visiting.insert(agent.name.clone()) {
            return Err(Error::Validation(format!(
                "agent graph contains a cycle at '{}'",
                agent.name
            )));
        }
        for tool in &agent.tools {
            if let AgentTool::Delegation { agent, .. } = tool {
                visit(agent, visiting, complete)?;
            }
        }
        for handoff in &agent.handoffs {
            visit(&handoff.agent, visiting, complete)?;
        }
        visiting.remove(&agent.name);
        complete.insert(agent.name.clone());
        Ok(())
    }
    visit(root, &mut HashSet::new(), &mut HashSet::new())
}