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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Versioned durable input part records.
use serde::{Deserialize, Serialize};
use serde_json::Value;
use starweaver_core::Metadata;
use starweaver_model::{CachePointTtl, ContentPart};
use thiserror::Error;
/// Provider-scoped legacy file reference submitted as session input.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct FileRef {
/// File URI or provider reference.
pub uri: String,
/// Optional media type.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub media_type: Option<String>,
/// Optional display name.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
impl FileRef {
/// Build a file reference.
#[must_use]
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: uri.into(),
media_type: None,
name: None,
}
}
}
/// Binary resource legacy reference submitted as session input.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct BinaryRef {
/// Resource URI, object key, or upload token.
pub uri: String,
/// Optional media type.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub media_type: Option<String>,
/// Optional byte length.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bytes: Option<u64>,
}
impl BinaryRef {
/// Build a binary reference.
#[must_use]
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: uri.into(),
media_type: None,
bytes: None,
}
}
}
/// Serializable run input submitted by SDKs and product hosts.
///
/// Every canonical model [`ContentPart`] has an explicit durable variant. The
/// legacy `url`, `file`, `binary`, `mode`, and `command` variants remain readable
/// for previous-release evidence but are not used as a content escape hatch.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum InputPart {
/// Prompt-cache boundary after the preceding content part.
CachePoint {
/// Optional provider-compatible cache lifetime.
#[serde(default, skip_serializing_if = "Option::is_none")]
ttl: Option<CachePointTtl>,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Natural language prompt text.
Text {
/// Text content.
text: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Image URL.
ImageUrl {
/// Image URL.
url: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Generic file URL with an explicit media type.
FileUrl {
/// File URL.
url: String,
/// File media type.
media_type: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Inline binary content.
InlineBinary {
/// Binary bytes.
data: Vec<u8>,
/// Declared media type.
media_type: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Resource-backed content reference.
ResourceRef {
/// Resource URI.
uri: String,
/// Resource media type.
media_type: String,
/// Resource type such as `image`, `video`, or `document`.
resource_type: String,
/// Resource metadata preserved for model preparation.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
resource_metadata: Metadata,
/// Application metadata that is not model-visible by default.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Inline data URL.
DataUrl {
/// Data URL payload.
data_url: String,
/// Media type carried by the data URL.
media_type: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Legacy generic URL reference. New content writers use `image_url` or `file_url`.
Url {
/// URL string.
url: String,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Legacy provider-scoped file reference.
File {
/// File reference.
file: FileRef,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Legacy binary resource reference. New inline bytes use `inline_binary`.
Binary {
/// Binary reference.
binary: BinaryRef,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Legacy product mode or planning hint.
Mode {
/// Mode name.
mode: String,
/// Optional structured configuration.
#[serde(default, skip_serializing_if = "Value::is_null")]
config: Value,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
/// Legacy slash-command or product command evidence.
Command {
/// Command name.
command: String,
/// Command arguments.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
args: Vec<String>,
/// Optional structured payload.
#[serde(default, skip_serializing_if = "Value::is_null")]
payload: Value,
/// Application metadata.
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
metadata: Metadata,
},
}
impl InputPart {
/// Build text input.
#[must_use]
pub fn text(text: impl Into<String>) -> Self {
Self::Text {
text: text.into(),
metadata: Metadata::default(),
}
}
/// Build an explicit image URL input.
#[must_use]
pub fn image_url(url: impl Into<String>) -> Self {
Self::ImageUrl {
url: url.into(),
metadata: Metadata::default(),
}
}
/// Build a legacy generic URL input.
#[must_use]
pub fn url(url: impl Into<String>) -> Self {
Self::Url {
url: url.into(),
metadata: Metadata::default(),
}
}
/// Build legacy command evidence.
#[must_use]
pub fn command(command: impl Into<String>, args: Vec<String>) -> Self {
Self::Command {
command: command.into(),
args,
payload: Value::Null,
metadata: Metadata::default(),
}
}
/// Return whether this part is a previous-release product-edge input.
#[must_use]
pub const fn is_legacy_product_input(&self) -> bool {
matches!(self, Self::Mode { .. } | Self::Command { .. })
}
}
impl From<ContentPart> for InputPart {
fn from(part: ContentPart) -> Self {
let metadata = Metadata::default();
match part {
ContentPart::CachePoint { ttl } => Self::CachePoint { ttl, metadata },
ContentPart::Text { text } => Self::Text { text, metadata },
ContentPart::ImageUrl { url } => Self::ImageUrl { url, metadata },
ContentPart::FileUrl { url, media_type } => Self::FileUrl {
url,
media_type,
metadata,
},
ContentPart::Binary { data, media_type } => Self::InlineBinary {
data,
media_type,
metadata,
},
ContentPart::ResourceRef {
uri,
media_type,
resource_type,
metadata: resource_metadata,
} => Self::ResourceRef {
uri,
media_type,
resource_type,
resource_metadata,
metadata,
},
ContentPart::DataUrl {
data_url,
media_type,
} => Self::DataUrl {
data_url,
media_type,
metadata,
},
}
}
}
impl TryFrom<InputPart> for ContentPart {
type Error = InputConversionError;
fn try_from(part: InputPart) -> Result<Self, Self::Error> {
match part {
InputPart::CachePoint { ttl, .. } => Ok(Self::CachePoint { ttl }),
InputPart::Text { text, .. } => Ok(Self::Text { text }),
InputPart::ImageUrl { url, .. } | InputPart::Url { url, .. } => {
Ok(Self::ImageUrl { url })
}
InputPart::FileUrl {
url, media_type, ..
} => Ok(Self::FileUrl { url, media_type }),
InputPart::InlineBinary {
data, media_type, ..
} => Ok(Self::Binary { data, media_type }),
InputPart::ResourceRef {
uri,
media_type,
resource_type,
resource_metadata,
..
} => Ok(Self::ResourceRef {
uri,
media_type,
resource_type,
metadata: resource_metadata,
}),
InputPart::DataUrl {
data_url,
media_type,
..
} => Ok(Self::DataUrl {
data_url,
media_type,
}),
InputPart::File { file, .. } => {
let mut metadata = Metadata::default();
if let Some(name) = file.name {
metadata.insert("name".to_string(), Value::String(name));
}
Ok(Self::ResourceRef {
uri: file.uri,
media_type: file
.media_type
.unwrap_or_else(|| "application/octet-stream".to_string()),
resource_type: "file".to_string(),
metadata,
})
}
InputPart::Binary { binary, .. } => {
let mut metadata = Metadata::default();
if let Some(bytes) = binary.bytes {
metadata.insert("bytes".to_string(), Value::from(bytes));
}
Ok(Self::ResourceRef {
uri: binary.uri,
media_type: binary
.media_type
.unwrap_or_else(|| "application/octet-stream".to_string()),
resource_type: "binary".to_string(),
metadata,
})
}
InputPart::Mode { mode, .. } => Err(InputConversionError::ProductMode(mode)),
InputPart::Command { command, .. } => {
Err(InputConversionError::ProductCommand(command))
}
}
}
}
/// Failure converting legacy durable input into canonical model content.
#[derive(Debug, Error)]
pub enum InputConversionError {
/// Previous content-part escape hatch contains invalid content JSON.
#[error("legacy content_part input is invalid: {0}")]
LegacyContent(serde_json::Error),
/// Product mode must be handled before runtime input conversion.
#[error("product mode {0} is not model content")]
ProductMode(String),
/// Product command must be handled before runtime input conversion.
#[error("product command {0} is not model content")]
ProductCommand(String),
}