agents_core/
messaging.rs

1use serde::{Deserialize, Serialize};
2
3/// Core message structure exchanged between runtimes, planners, and tools.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct AgentMessage {
6    pub role: MessageRole,
7    pub content: MessageContent,
8    #[serde(default, skip_serializing_if = "Option::is_none")]
9    pub metadata: Option<MessageMetadata>,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub enum MessageRole {
14    User,
15    Agent,
16    Tool,
17    System,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(tag = "type", content = "value")]
22pub enum MessageContent {
23    Text(String),
24    Json(serde_json::Value),
25}
26
27impl MessageContent {
28    pub fn as_text(&self) -> Option<&str> {
29        match self {
30            MessageContent::Text(text) => Some(text.as_str()),
31            _ => None,
32        }
33    }
34
35    pub fn as_json(&self) -> Option<&serde_json::Value> {
36        match self {
37            MessageContent::Json(value) => Some(value),
38            _ => None,
39        }
40    }
41}
42
43/// Payload passed into tools when invoked by the runtime.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct ToolInvocation {
46    pub tool_name: String,
47    pub args: serde_json::Value,
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub tool_call_id: Option<String>,
50}
51
52#[derive(Debug, Clone, Default, Serialize, Deserialize)]
53pub struct MessageMetadata {
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub tool_call_id: Option<String>,
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub cache_control: Option<CacheControl>,
58}
59
60/// Cache control metadata for Anthropic prompt caching
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct CacheControl {
63    /// Cache type - currently only "ephemeral" is supported by Anthropic
64    #[serde(rename = "type")]
65    pub cache_type: String,
66}