-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontent.rs
More file actions
241 lines (222 loc) · 8.74 KB
/
Copy pathcontent.rs
File metadata and controls
241 lines (222 loc) · 8.74 KB
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
//! Content parts shared between user-facing and provider-facing messages.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use ts_rs::TS;
/// A part of a multi-part message.
///
/// Shared between `ModelMessage` (user-facing) and `LanguageModelPrompt` (provider-facing).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
#[serde(tag = "type", rename_all = "snake_case")]
#[ts(export)]
pub enum ContentPart {
/// A text segment.
Text {
text: String,
/// Provider-specific options for this part (e.g. `openai.promptCacheBreakpoint`).
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// An image (raw bytes + MIME type).
Image {
image: Vec<u8>,
media_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A file (raw bytes + MIME type).
File {
data: Vec<u8>,
media_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
filename: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A file whose inline data is an already-base64-encoded string.
///
/// Mirrors the Vercel AI SDK `file` part with `data: { type: 'data', data:
/// '<base64>' }`: the `data` field holds the raw base64 string verbatim
/// (it is NOT decoded into bytes), so it round-trips through providers that
/// emit a `base64` source unchanged.
FileBase64 {
data: String,
media_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
filename: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A file referenced by URL.
///
/// Mirrors the Vercel AI SDK `file` part with `data: { type: 'url', url }`.
FileUrl {
url: String,
media_type: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A file referenced by a provider-specific reference (e.g. an OpenAI file ID).
///
/// Mirrors the Vercel AI SDK `file` part with `data: { type: 'reference',
/// reference: { openai: 'file-xxx' } }`. The `reference` field is a JSON
/// object keyed by provider name.
FileReference {
media_type: String,
reference: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
filename: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A reasoning / thinking segment produced by the model.
///
/// Mirrors the Vercel AI SDK `reasoning` part. `signature` is the Anthropic
/// thinking-block signature (from `providerOptions.anthropic.signature`);
/// when absent the part is treated as having unsupported metadata (unless
/// `providerOptions.anthropic.redactedData` is present, in which case a
/// `redacted_thinking` block is emitted).
Reasoning {
text: String,
signature: Option<String>,
/// Provider-specific options for this part (e.g.
/// `anthropic.signature`, `anthropic.redactedData`,
/// `anthropic.cacheControl`).
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// A tool call requested by the model.
ToolCall {
tool_call_id: String,
tool_name: String,
/// Arguments as a JSON value (usually an object).
input: Value,
/// Provider-assigned thought signature (e.g. Google Gemini
/// `thoughtSignature`). Must be echoed back verbatim on the follow-up
/// turn when the tool result is sent.
#[serde(default, skip_serializing_if = "Option::is_none")]
thought_signature: Option<String>,
/// Provider-specific options for this part (e.g.
/// `anthropic.cacheControl`).
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
/// The result of executing a tool call.
ToolResult {
tool_call_id: String,
/// The tool's output (usually a JSON value or plain text).
///
/// Accepts the legacy `output` field name (used by the Vercel AI SDK
/// and the 0.1.0 TypeScript bindings) as an alias during deserialization,
/// so multi-part tool messages constructed either way round-trip
/// correctly. Serialization always emits `result`.
#[serde(alias = "output")]
result: Value,
/// The name of the tool that produced this result (optional on the
/// user-input side; providers that need it can look it up from the
/// preceding tool call).
#[serde(default, skip_serializing_if = "Option::is_none")]
tool_name: Option<String>,
/// Whether the result is an error or error message.
#[serde(default, skip_serializing_if = "Option::is_none")]
is_error: Option<bool>,
/// Whether the result is preliminary (replaces prior, e.g. image previews).
#[serde(default, skip_serializing_if = "Option::is_none")]
preliminary: Option<bool>,
/// Whether the tool is dynamic (defined at runtime, e.g. MCP tools).
#[serde(default, skip_serializing_if = "Option::is_none")]
dynamic: Option<bool>,
/// Provider-specific options for this part (e.g.
/// `anthropic.cacheControl`).
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_options: Option<Value>,
},
}
impl ContentPart {
/// Convenience constructor for a text part (no provider options).
pub fn text(text: impl Into<String>) -> Self {
ContentPart::Text {
text: text.into(),
provider_options: None,
}
}
/// Convenience constructor for a tool-call part (no provider options).
pub fn tool_call(
tool_call_id: impl Into<String>,
tool_name: impl Into<String>,
input: Value,
) -> Self {
ContentPart::ToolCall {
tool_call_id: tool_call_id.into(),
tool_name: tool_name.into(),
input,
thought_signature: None,
provider_options: None,
}
}
/// Convenience constructor for a tool-result part (no provider options).
pub fn tool_result(tool_call_id: impl Into<String>, result: Value) -> Self {
ContentPart::ToolResult {
tool_call_id: tool_call_id.into(),
result,
tool_name: None,
is_error: None,
preliminary: None,
dynamic: None,
provider_options: None,
}
}
/// Convenience constructor for a reasoning/thinking part (no provider
/// options, no signature).
pub fn reasoning(text: impl Into<String>) -> Self {
ContentPart::Reasoning {
text: text.into(),
signature: None,
provider_options: None,
}
}
/// Convenience constructor for an image part (no provider options).
pub fn image(image: Vec<u8>, media_type: impl Into<String>) -> Self {
ContentPart::Image {
image,
media_type: media_type.into(),
provider_options: None,
}
}
/// Convenience constructor for a file part (no provider options, no filename).
pub fn file(data: Vec<u8>, media_type: impl Into<String>) -> Self {
ContentPart::File {
data,
media_type: media_type.into(),
filename: None,
provider_options: None,
}
}
/// Convenience constructor for a base64-encoded file part (no provider
/// options, no filename).
pub fn file_base64(data: impl Into<String>, media_type: impl Into<String>) -> Self {
ContentPart::FileBase64 {
data: data.into(),
media_type: media_type.into(),
filename: None,
provider_options: None,
}
}
/// Convenience constructor for a URL-referenced file part (no provider options).
pub fn file_url(url: impl Into<String>, media_type: impl Into<String>) -> Self {
ContentPart::FileUrl {
url: url.into(),
media_type: media_type.into(),
provider_options: None,
}
}
/// Convenience constructor for a provider-referenced file part (no provider
/// options, no filename).
pub fn file_reference(media_type: impl Into<String>, reference: Value) -> Self {
ContentPart::FileReference {
media_type: media_type.into(),
reference,
filename: None,
provider_options: None,
}
}
}