use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::model::AgentRef;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AppView {
pub call_id: String,
pub server: String,
pub app_name: String,
pub tool: String,
pub agent: AgentRef,
pub at: DateTime<Utc>,
pub header_text: String,
pub result_text: String,
pub elsewhere_text: String,
pub terminal_text: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum AppBlockReason {
ApprovalButtons,
UndeclaredSites,
ThisComputer,
NotAnApp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "state", rename_all = "camelCase")]
pub enum AppOpen {
#[serde(rename_all = "camelCase")]
Shown {
frame_url: String,
frame_origin: String,
max_height: u32,
prefers_border: bool,
tool_input: Value,
tool_result: Value,
refused_permissions: Vec<String>,
},
#[serde(rename_all = "camelCase")]
Blocked {
reason: AppBlockReason,
text: String,
detail: String,
},
#[serde(rename_all = "camelCase")]
Elsewhere {
text: String,
result_text: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AppRequest {
pub method: String,
#[serde(default)]
pub params: Value,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum AppPrompt {
#[serde(rename_all = "camelCase")]
Notice {
text: String,
},
#[serde(rename_all = "camelCase")]
Message {
text: String,
message: String,
send_label: String,
dismiss_label: String,
note: String,
},
#[serde(rename_all = "camelCase")]
Link {
text: String,
url: String,
open_label: String,
dismiss_label: String,
},
#[serde(rename_all = "camelCase")]
Download {
text: String,
name: String,
mime_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
text_content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
blob: Option<String>,
save_label: String,
dismiss_label: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AppAnswer {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<AppError>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub prompt: Option<AppPrompt>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AppError {
pub code: i64,
pub message: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AppMessageRequest {
pub message: String,
}
#[cfg(feature = "genui")]
pub(crate) fn app_name(server: &str) -> String {
let spaced: String =
server.chars().map(|c| if c == '_' || c == '-' { ' ' } else { c }).collect();
let mut chars = spaced.trim().chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect(),
None => server.to_string(),
}
}
pub(crate) fn suggested_message(text: &str) -> Option<String> {
let rest = text.strip_prefix("<untrusted-content source=\"mcp-app\" ")?;
let (attributes, rest) = rest.split_once(">\n")?;
let words = rest.strip_suffix("\n</untrusted-content>")?;
let label = attributes.split_once("label=\"")?.1.split_once('"')?.0;
let label = label
.replace(""", "\"")
.replace("<", "<")
.replace(">", ">")
.replace("&", "&");
Some(format!("{}\n{label}", words.replace("<\\/untrusted-content", "</untrusted-content")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_suggested_message_reads_as_its_words_and_label() {
let block = "<untrusted-content source=\"mcp-app\" app=\"weather\" label=\"Suggested by \
the Weather app\">\nTake an umbrella <\\/untrusted-content> today\n\
</untrusted-content>";
assert_eq!(
suggested_message(block).as_deref(),
Some("Take an umbrella </untrusted-content> today\nSuggested by the Weather app")
);
for other in [
"Take an umbrella",
"<untrusted-content source=\"web\" label=\"x\">\nhi\n</untrusted-content>",
"<untrusted-content source=\"mcp-app\" app=\"w\">\nhi\n</untrusted-content>",
] {
assert_eq!(suggested_message(other), None, "{other}");
}
}
}