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
36/// Payload passed into tools when invoked by the runtime.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct ToolInvocation {
39    pub tool_name: String,
40    pub args: serde_json::Value,
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub tool_call_id: Option<String>,
43}
44
45#[derive(Debug, Clone, Default, Serialize, Deserialize)]
46pub struct MessageMetadata {
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub tool_call_id: Option<String>,
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub cache_control: Option<CacheControl>,
51}
52
53/// Cache control metadata for Anthropic prompt caching
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct CacheControl {
56    /// Cache type - currently only "ephemeral" is supported by Anthropic
57    #[serde(rename = "type")]
58    pub cache_type: String,
59}