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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{ExtensionMap, ModelError, ModelErrorKind};
/// The author role of a model message.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Role {
/// High-priority system or developer instruction.
System,
/// End-user input.
User,
/// Model output.
Assistant,
/// A tool result represented as a message by a provider.
Tool,
}
/// A serializable media source.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum MediaSource {
/// An externally accessible URL.
Url {
/// Media URL.
url: String,
/// Optional MIME type.
media_type: Option<String>,
},
/// An inline base64 payload.
Base64 {
/// MIME type.
media_type: String,
/// Base64-encoded bytes.
data: String,
},
/// A reference into an application-owned artifact store.
Artifact {
/// Complete scope- and integrity-bound reference.
reference: crate::ArtifactRef,
},
/// A file already uploaded to a provider control plane.
ProviderFile {
/// Provider namespace that owns the file.
provider: String,
/// Provider-assigned file identity.
file_id: String,
},
}
impl MediaSource {
/// Creates a provider-owned file reference.
pub fn provider_file(provider: impl Into<String>, file_id: impl Into<String>) -> Self {
Self::ProviderFile {
provider: provider.into(),
file_id: file_id.into(),
}
}
}
/// Provider-specific data retained without normalization.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ProviderData {
/// Provider namespace.
pub provider: String,
/// Provider-defined data kind.
pub kind: String,
/// Unmodified structured data.
pub value: Value,
}
/// A normalized citation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Citation {
/// Referenced URI, when available.
pub uri: Option<String>,
/// Human-readable title.
pub title: Option<String>,
/// Optional character start offset in the associated text.
pub start: Option<u64>,
/// Optional character end offset in the associated text.
pub end: Option<u64>,
}
/// Model reasoning retained for valid round trips.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ReasoningPart {
/// Reasoning text or provider-generated summary, when exposed.
pub text: Option<String>,
/// Provider signature or encrypted continuation token.
pub signature: Option<String>,
/// Whether the reasoning body was redacted by the provider.
pub redacted: bool,
/// Provider information that has no normalized representation.
pub provider_data: Vec<ProviderData>,
}
/// A completed tool call requested by a model.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ToolCall {
/// Provider- or runtime-assigned call identity.
pub id: String,
/// Tool name.
pub name: String,
/// Parsed JSON arguments.
pub arguments: Value,
/// Original argument text, when preserving it matters.
pub raw_arguments: Option<String>,
/// Namespaced metadata.
pub metadata: ExtensionMap,
}
/// A completed tool result supplied to a model.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ToolResult {
/// Identity of the tool call being answered.
pub call_id: String,
/// Tool name, required by providers that do not correlate results by ID.
#[serde(default)]
pub name: Option<String>,
/// Rich result content.
pub content: Vec<ContentPart>,
/// Optional structured result value kept separate from presentation
/// content so protocol adapters can preserve both representations.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub structured_content: Option<Value>,
/// Whether tool execution failed.
pub is_error: bool,
/// Namespaced metadata.
pub metadata: ExtensionMap,
}
/// One ordered unit of model-visible content.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ContentPart {
/// Plain text.
Text {
/// Text body.
text: String,
},
/// Image content.
Image {
/// Image source.
source: MediaSource,
},
/// Audio content.
Audio {
/// Audio source.
source: MediaSource,
},
/// Document content.
Document {
/// Document source.
source: MediaSource,
/// Optional display name.
name: Option<String>,
},
/// A link to a resource that may be fetched by an authorized host.
ResourceLink {
/// Resource URI.
uri: String,
/// Stable logical name.
name: String,
/// Optional human-readable title.
title: Option<String>,
/// Optional model-facing description.
description: Option<String>,
/// Optional MIME type.
media_type: Option<String>,
/// Raw resource size before encoding or tokenization.
size: Option<u64>,
},
/// A model-requested tool call.
ToolCall(ToolCall),
/// A tool result returned to a model.
ToolResult(ToolResult),
/// Provider reasoning or thinking data.
Reasoning(ReasoningPart),
/// A provider refusal.
Refusal {
/// Refusal explanation.
text: String,
},
/// A citation associated with preceding or adjacent content.
Citation(Citation),
/// Information that cannot yet be normalized without loss.
ProviderOpaque(ProviderData),
}
impl ContentPart {
/// Creates a text content part.
pub fn text(value: impl Into<String>) -> Self {
Self::Text { text: value.into() }
}
}
/// An ordered message sent to or returned by a model.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Message {
/// Message author role.
pub role: Role,
/// Ordered rich content.
pub content: Vec<ContentPart>,
/// Namespaced metadata.
pub metadata: BTreeMap<String, Value>,
}
impl Message {
/// Creates a non-empty message.
///
/// # Errors
///
/// Returns [`ModelError`] when `content` is empty.
pub fn new(role: Role, content: Vec<ContentPart>) -> Result<Self, ModelError> {
if content.is_empty() {
return Err(ModelError::local(
ModelErrorKind::InvalidRequest,
"a message must contain at least one content part",
));
}
Ok(Self {
role,
content,
metadata: BTreeMap::new(),
})
}
/// Creates a user text message.
pub fn user(text: impl Into<String>) -> Self {
Self {
role: Role::User,
content: vec![ContentPart::text(text)],
metadata: BTreeMap::new(),
}
}
/// Creates a system text message.
pub fn system(text: impl Into<String>) -> Self {
Self {
role: Role::System,
content: vec![ContentPart::text(text)],
metadata: BTreeMap::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::{ContentPart, Message, Role, ToolResult};
use crate::ModelErrorKind;
#[test]
fn empty_messages_are_rejected() {
let error = Message::new(Role::User, Vec::new()).unwrap_err();
assert_eq!(error.kind, ModelErrorKind::InvalidRequest);
}
#[test]
fn content_round_trips_without_erasing_opaque_data() {
let message = Message::new(
Role::Assistant,
vec![
ContentPart::text("answer"),
ContentPart::ProviderOpaque(super::ProviderData {
provider: "example".into(),
kind: "future_block".into(),
value: serde_json::json!({"x": 1}),
}),
],
)
.unwrap();
let encoded = serde_json::to_value(&message).unwrap();
let decoded: Message = serde_json::from_value(encoded).unwrap();
assert_eq!(decoded, message);
}
#[test]
fn legacy_tool_results_without_a_name_still_deserialize() {
let result: ToolResult = serde_json::from_value(serde_json::json!({
"call_id":"call_1",
"content":[{"type":"text","text":"ok"}],
"is_error":false,
"metadata":{}
}))
.unwrap();
assert_eq!(result.name, None);
}
}