marona 1.1.2

Universal Model Gateway and MCP/Skill SDK with mandatory project-scoped Marona authentication
Documentation
use std::{pin::Pin, sync::Arc};

use async_stream::try_stream;
use futures_util::{Stream, StreamExt};
use reqwest::Method;
use serde_json::{json, Map, Value};

use crate::{
    core::Core, Error, Input, McpConnection, Result, RoutingOptions, RunEvent, RuntimeResponse,
    ToolChoice,
};

pub type EventStream = Pin<Box<dyn Stream<Item = Result<RunEvent>> + Send>>;

#[derive(Debug, Clone)]
pub struct CreateRequest {
    pub model: String,
    pub input: Input,
    pub tools: Option<McpConnection>,
    pub interface: String,
    pub user_id: Option<String>,
    pub conversation_id: Option<String>,
    pub developer_instructions: Option<String>,
    pub routing: Option<RoutingOptions>,
    pub output: Option<Value>,
    pub tool_choice: ToolChoice,
    pub metadata: Map<String, Value>,
}

impl CreateRequest {
    pub fn new(model: impl Into<String>, input: impl Into<Input>) -> Self {
        Self {
            model: model.into(),
            input: input.into(),
            tools: None,
            interface: "api".into(),
            user_id: None,
            conversation_id: None,
            developer_instructions: None,
            routing: None,
            output: None,
            tool_choice: ToolChoice::Auto,
            metadata: Map::new(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Responses {
    pub(crate) core: Arc<Core>,
}

impl Responses {
    pub async fn create(&self, request: CreateRequest) -> Result<RuntimeResponse> {
        let body = runtime_body(&request)?;
        let payload: Value = self
            .core
            .json(
                &self.core.runtime_url,
                Method::POST,
                "/runtime/message",
                Some(&body),
            )
            .await?;
        response_from_value(payload)
    }

    pub async fn stream(&self, request: CreateRequest) -> Result<EventStream> {
        let body = runtime_body(&request)?;
        let core = self.core.clone();
        Ok(Box::pin(try_stream! {
            let api_key = core.require_api_key()?;
            let response = core.client.post(format!("{}/runtime/message/stream", core.runtime_url))
                .bearer_auth(api_key).json(&body).send().await?;
            let status = response.status();
            let response = if status.is_success() {
                response
            } else {
                let payload = response.bytes().await.ok().and_then(|bytes| serde_json::from_slice::<Value>(&bytes).ok());
                Err(Error::api(status, payload))?
            };
            let mut bytes = response.bytes_stream();
            let mut pending = String::new();
            while let Some(chunk) = bytes.next().await {
                pending.push_str(&String::from_utf8_lossy(&chunk?));
                while let Some(index) = pending.find("\n\n") {
                    let frame = pending[..index].to_owned();
                    pending.drain(..index + 2);
                    let data = frame.lines().filter_map(|line| line.strip_prefix("data:")).collect::<Vec<_>>().join("\n");
                    if data.trim().is_empty() || data.trim() == "[DONE]" { continue; }
                    let value: Value = serde_json::from_str(data.trim()).map_err(|e| Error::Protocol(e.to_string()))?;
                    if value.get("error").is_some() {
                        Err(Error::stream(&value))?;
                    }
                    yield event_from_value(value);
                }
            }
        }))
    }
}

pub(crate) fn runtime_body(request: &CreateRequest) -> Result<Value> {
    validate_model_namespace(&request.model)?;
    if let Some(routing) = request.routing.as_ref() {
        for model in &routing.allowed_models {
            validate_model_namespace(model)?;
        }
    }
    let (text, attachments) = normalize_input(&request.input)?;
    let mut metadata = request.metadata.clone();
    let mut agent = metadata
        .remove("marona_agent")
        .and_then(|value| value.as_object().cloned())
        .unwrap_or_default();
    agent.insert("tool_choice".into(), json!(request.tool_choice));
    agent.entry("tools_enabled").or_insert_with(|| {
        json!(request.tools.is_some() && request.tool_choice != ToolChoice::None)
    });
    metadata.insert("marona_agent".into(), Value::Object(agent));
    let apps = request
        .tools
        .as_ref()
        .and_then(|value| value.payload.get("apps"))
        .cloned();
    let skills = request
        .tools
        .as_ref()
        .and_then(|value| value.payload.get("skills"))
        .cloned();
    Ok(json!({
        "interface": request.interface,
        "model": request.model,
        "user_id": request.user_id,
        "conversation_id": request.conversation_id,
        "text": text,
        "developer_instructions": request.developer_instructions,
        "execute": true,
        "attachments": attachments,
        "metadata": metadata,
        "routing": request.routing,
        "output": request.output,
        "apps": apps,
        "skills": skills,
    }))
}

pub(crate) fn validate_model_namespace(model: &str) -> Result<()> {
    let value = model.trim();
    if !value.starts_with("marona/") || value == "marona/" {
        return Err(Error::invalid_model_namespace());
    }
    Ok(())
}

pub(crate) fn normalize_input(input: &Input) -> Result<(String, Vec<Value>)> {
    match input {
        Input::Text(text) => Ok((text.clone(), vec![])),
        Input::Message(value) => normalize_messages(std::slice::from_ref(value)),
        Input::Messages(values) => normalize_messages(values),
    }
}

fn normalize_messages(messages: &[Value]) -> Result<(String, Vec<Value>)> {
    let mut text = Vec::new();
    let mut attachments = Vec::new();
    for message in messages {
        let content = message.get("content").cloned().unwrap_or(Value::Null);
        if let Some(value) = content.as_str() {
            text.push(value.to_owned());
            continue;
        }
        for block in content.as_array().into_iter().flatten() {
            match block.get("type").and_then(Value::as_str).unwrap_or("") {
                "input_text" => if let Some(value) = block.get("text").and_then(Value::as_str) { text.push(value.to_owned()); },
                "input_image" => attachments.push(json!({"kind":"image","url":block.get("image_url"),"metadata":{"detail":block.get("detail")}})),
                "input_audio" => attachments.push(json!({"kind":"audio","url":block.get("audio_url"),"data_base64":block.get("audio_data")})),
                "input_video" => attachments.push(json!({"kind":"video","url":block.get("video_url"),"data_base64":block.get("video_data")})),
                "input_file" => attachments.push(json!({"kind":"file","filename":block.get("filename"),"url":block.get("file_url"),"data_base64":block.get("file_data"),"metadata":{"detail":block.get("detail")}})),
                _ => {}
            }
        }
    }
    if text.is_empty() && attachments.is_empty() {
        return Err(Error::Validation("input is empty".into()));
    }
    Ok((text.join("\n"), attachments))
}

pub(crate) fn response_from_value(mut value: Value) -> Result<RuntimeResponse> {
    let object = value
        .as_object_mut()
        .ok_or_else(|| Error::Protocol("runtime response must be an object".into()))?;
    if !object.contains_key("output") {
        let reply = object
            .get("reply")
            .cloned()
            .unwrap_or(Value::String(String::new()));
        let artifacts = object
            .get("artifacts")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default();
        object.insert(
            "output".into(),
            if artifacts.is_empty() {
                reply
            } else {
                let mut output = Vec::new();
                if let Some(text) = reply.as_str().filter(|v| !v.is_empty()) {
                    output.push(json!({"type":"output_text","text":text}));
                }
                output.extend(artifacts);
                Value::Array(output)
            },
        );
    }
    let object = value
        .as_object()
        .ok_or_else(|| Error::Protocol("runtime response must be an object".into()))?;
    let mut safe = Map::new();
    for name in [
        "status",
        "reply",
        "output",
        "interface",
        "conversation_id",
        "ui",
        "artifacts",
        "selected_app",
        "selected_tool",
        "permission_request",
        "service_connection",
        "tool_call",
        "model",
        "usage",
        "agent",
    ] {
        if let Some(item) = object.get(name) {
            safe.insert(name.into(), item.clone());
        }
    }
    let raw_routing = object.get("routing").and_then(Value::as_object);
    let mut routing = Map::new();
    for name in [
        "requested_model",
        "selected_model",
        "strategy",
        "fallback_used",
    ] {
        if let Some(item) = raw_routing.and_then(|value| value.get(name)) {
            routing.insert(name.into(), item.clone());
        }
    }
    safe.insert("routing".into(), Value::Object(routing));
    serde_json::from_value(Value::Object(safe)).map_err(|error| Error::Protocol(error.to_string()))
}

fn event_from_value(value: Value) -> RunEvent {
    RunEvent {
        event_type: value
            .get("type")
            .and_then(Value::as_str)
            .unwrap_or("unknown")
            .to_owned(),
        delta: value
            .get("delta")
            .and_then(Value::as_str)
            .map(str::to_owned),
        output: value.get("output").cloned().unwrap_or(Value::Null),
        data: value
            .get("data")
            .and_then(Value::as_object)
            .cloned()
            .unwrap_or_default(),
    }
}