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
//! Universal content types that cross every protocol boundary.
use serde::{Deserialize, Serialize};
/// The universal content type. Crosses every boundary.
/// Intentionally simple — complex structured content uses
/// ContentBlock variants, not nested Content.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Content {
/// Plain text content.
Text(String),
/// Structured content blocks.
Blocks(Vec<ContentBlock>),
}
/// A single block of structured content.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ContentBlock {
/// Plain text block.
#[serde(rename = "text")]
Text {
/// The text content.
text: String,
},
/// Image content block.
#[serde(rename = "image")]
Image {
/// The image source (base64 or URL).
source: ImageSource,
/// The MIME type of the image.
media_type: String,
},
/// A tool use request from the model.
#[serde(rename = "tool_use")]
ToolUse {
/// Unique identifier for this tool use.
id: String,
/// Name of the tool to invoke.
name: String,
/// Tool input parameters.
input: serde_json::Value,
},
/// Result from a tool execution.
#[serde(rename = "tool_result")]
ToolResult {
/// The tool_use id this result corresponds to.
tool_use_id: String,
/// The result content.
content: String,
/// Whether the tool execution errored.
is_error: bool,
},
/// Escape hatch for future content types.
/// If a new modality is invented, it goes here first.
/// When it stabilizes, it graduates to a named variant.
#[serde(rename = "custom")]
Custom {
/// The custom content type identifier.
content_type: String,
/// Arbitrary payload.
data: serde_json::Value,
},
}
/// Source for image content.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ImageSource {
/// Base64-encoded image data.
Base64 {
/// The base64-encoded image data.
data: String,
},
/// URL pointing to an image.
Url {
/// The URL of the image.
url: String,
},
}
impl Content {
/// Create a text content value.
pub fn text(s: impl Into<String>) -> Self {
Content::Text(s.into())
}
/// Extract plain text content, ignoring non-text blocks.
pub fn as_text(&self) -> Option<&str> {
match self {
Content::Text(s) => Some(s),
Content::Blocks(blocks) => {
// Return first text block's content
blocks.iter().find_map(|b| match b {
ContentBlock::Text { text } => Some(text.as_str()),
_ => None,
})
}
}
}
}