llm-trait 0.2.0

Trait definitions and core types for a unified LLM provider layer
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Message types for LLM conversations.
//!
//! These types define the conversation format shared across all LLM providers.

use serde::{Deserialize, Serialize};

/// A tool call embedded in an assistant message.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ToolCallMessage {
    pub id: String,
    pub name: String,
    pub arguments: String,
}

/// Image attachment for multimodal messages.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ImageAttachment {
    Url {
        url: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        detail: Option<ImageDetail>,
    },
    Base64 {
        data: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        media_type: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        detail: Option<ImageDetail>,
    },
}

/// Image detail level.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ImageDetail {
    Low,
    High,
    Auto,
}

/// `skip_serializing_if` helper for `bool` flags: a message that is not
/// synthetic serializes exactly as it did before the flag existed.
fn is_false(value: &bool) -> bool {
    !*value
}

/// A chat message in a conversation.
///
/// Supports system, user, assistant, tool, and custom message types.
/// Each variant carries the data needed for LLM API calls.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ChatMessage {
    System {
        content: String,
        /// Ephemeral messages are cleaned up after each turn and skipped during persistence.
        #[serde(default, skip_serializing)]
        ephemeral: bool,
    },
    User {
        content: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        images: Vec<ImageAttachment>,
        /// Ephemeral messages are cleaned up after each turn and skipped during persistence.
        #[serde(default, skip_serializing)]
        ephemeral: bool,
        /// Machine-composed input on the user role (tool-failure notices,
        /// background-task wakes, the window seed): the model sees it like
        /// any user message, but it is never the user's own words. Context
        /// rotation skips synthetic messages when carrying the task trail
        /// into a fresh window. Unlike `ephemeral`, this flag survives
        /// persistence, so a resumed session keeps the distinction.
        #[serde(default, skip_serializing_if = "is_false")]
        synthetic: bool,
    },
    Assistant {
        content: Option<String>,
        reasoning_content: Option<String>,
        /// Anthropic requires thinking blocks with signature to be sent back
        /// in multi-turn conversations. This field stores the signature.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        thinking_signature: Option<String>,
        tool_calls: Option<Vec<ToolCallMessage>>,
    },
    /// A tool's result, paired with the assistant's `tool_calls` entry.
    ///
    /// `content` is the textual face; `images` carries pictures the tool's
    /// output wants the model to *look at* (a screenshot, a rendered
    /// diagram). A provider whose tool-result shape can carry images sends
    /// them beside the text; one that cannot says so in `content` rather
    /// than dropping the picture silently.
    Tool {
        tool_call_id: String,
        name: Option<String>,
        content: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        images: Vec<ImageAttachment>,
    },
    /// Application-defined message type for extensibility.
    ///
    /// Custom messages are preserved in the transcript but filtered out
    /// by the default conversion before being sent to the LLM provider.
    Custom {
        role: String,
        data: serde_json::Value,
    },
}

impl ChatMessage {
    pub fn system(content: impl Into<String>) -> Self {
        Self::System {
            content: content.into(),
            ephemeral: false,
        }
    }

    /// Create an ephemeral system message: auto-cleaned after turn, not persisted.
    pub fn system_ephemeral(content: impl Into<String>) -> Self {
        Self::System {
            content: content.into(),
            ephemeral: true,
        }
    }

    pub fn user(content: impl Into<String>) -> Self {
        Self::User {
            content: content.into(),
            images: Vec::new(),
            ephemeral: false,
            synthetic: false,
        }
    }

    /// Create an ephemeral user message: auto-cleaned after turn, not persisted.
    pub fn user_ephemeral(content: impl Into<String>) -> Self {
        Self::User {
            content: content.into(),
            images: Vec::new(),
            ephemeral: true,
            synthetic: false,
        }
    }

    /// Create a synthetic user message: machine-composed input on the user
    /// role (a notice, a wake, a seed) that must never stand in for the
    /// user's own words — context rotation skips it when collecting the task
    /// trail. The flag is persisted, so a resumed session keeps the
    /// distinction.
    pub fn user_synthetic(content: impl Into<String>) -> Self {
        Self::User {
            content: content.into(),
            images: Vec::new(),
            ephemeral: false,
            synthetic: true,
        }
    }

    pub fn user_with_images(content: impl Into<String>, images: Vec<ImageAttachment>) -> Self {
        Self::User {
            content: content.into(),
            images,
            ephemeral: false,
            synthetic: false,
        }
    }

    /// Whether this is an ephemeral message (auto-cleaned after turn, not persisted).
    pub fn is_ephemeral(&self) -> bool {
        match self {
            Self::System { ephemeral, .. } => *ephemeral,
            Self::User { ephemeral, .. } => *ephemeral,
            _ => false,
        }
    }

    /// Whether this is machine-composed input on the user role (never the
    /// user's own words).
    pub fn is_synthetic(&self) -> bool {
        match self {
            Self::User { synthetic, .. } => *synthetic,
            _ => false,
        }
    }

    pub fn assistant(content: impl Into<String>) -> Self {
        Self::Assistant {
            content: Some(content.into()),
            reasoning_content: None,
            thinking_signature: None,
            tool_calls: None,
        }
    }

    pub fn assistant_with_reasoning(
        content: impl Into<String>,
        reasoning: impl Into<String>,
    ) -> Self {
        Self::Assistant {
            content: Some(content.into()),
            reasoning_content: Some(reasoning.into()),
            thinking_signature: None,
            tool_calls: None,
        }
    }

    pub fn assistant_tool_call(
        tool_call_id: impl Into<String>,
        tool_name: impl Into<String>,
        arguments: impl Into<String>,
    ) -> Self {
        Self::Assistant {
            content: None,
            reasoning_content: None,
            thinking_signature: None,
            tool_calls: Some(vec![ToolCallMessage {
                id: tool_call_id.into(),
                name: tool_name.into(),
                arguments: arguments.into(),
            }]),
        }
    }

    pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self::Tool {
            tool_call_id: tool_call_id.into(),
            name: None,
            content: content.into(),
            images: Vec::new(),
        }
    }

    /// A tool result that also hands the model pictures to look at.
    pub fn tool_with_images(
        tool_call_id: impl Into<String>,
        content: impl Into<String>,
        images: Vec<ImageAttachment>,
    ) -> Self {
        Self::Tool {
            tool_call_id: tool_call_id.into(),
            name: None,
            content: content.into(),
            images,
        }
    }

    pub fn tool_with_name(
        tool_call_id: impl Into<String>,
        name: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::Tool {
            tool_call_id: tool_call_id.into(),
            name: Some(name.into()),
            content: content.into(),
            images: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn chat_message_user() {
        let msg = ChatMessage::user("hello");
        match &msg {
            ChatMessage::User {
                content,
                images,
                ephemeral,
                synthetic,
            } => {
                assert_eq!(content, "hello");
                assert!(images.is_empty());
                assert!(!ephemeral);
                assert!(!synthetic);
            }
            _ => panic!("Expected User variant"),
        }
    }

    #[test]
    fn chat_message_user_synthetic() {
        let msg = ChatMessage::user_synthetic("Background tasks finished");
        assert!(msg.is_synthetic());
        assert!(!msg.is_ephemeral());
        // A plain user message is the user's own words.
        assert!(!ChatMessage::user("hello").is_synthetic());
    }

    /// The synthetic flag survives persistence — a resumed session must keep
    /// the distinction between the user's words and machine notices.
    #[test]
    fn a_synthetic_user_message_round_trips_its_flag() {
        let wire =
            serde_json::to_string(&ChatMessage::user_synthetic("Tool calls failed")).unwrap();
        assert!(wire.contains("synthetic"), "flag must persist: {wire}");
        let back: ChatMessage = serde_json::from_str(&wire).unwrap();
        assert!(back.is_synthetic());
    }

    /// An all-real transcript serializes exactly as it did before the flag
    /// existed, and history written before it still loads as real.
    #[test]
    fn a_real_user_message_serializes_without_a_synthetic_key() {
        let wire = serde_json::to_string(&ChatMessage::user("hello")).unwrap();
        assert!(
            !wire.contains("synthetic"),
            "no empty flag on the wire: {wire}"
        );
        let old = r#"{"User":{"content":"hello","images":[]}}"#;
        let back: ChatMessage = serde_json::from_str(old).unwrap();
        assert!(!back.is_synthetic());
    }

    #[test]
    fn chat_message_system() {
        let msg = ChatMessage::system("sys");
        assert!(!msg.is_ephemeral());
    }

    /// Pictures ride the tool result through a persisted transcript too —
    /// a session resumed later still hands the model what it was shown.
    #[test]
    fn a_tool_result_round_trips_its_pictures() {
        let msg = ChatMessage::tool_with_images(
            "tc1",
            "image: the whole desktop",
            vec![ImageAttachment::Base64 {
                data: "aGVsbG8=".to_string(),
                media_type: Some("image/png".to_string()),
                detail: None,
            }],
        );
        let wire = serde_json::to_string(&msg).unwrap();
        let back: ChatMessage = serde_json::from_str(&wire).unwrap();
        match back {
            ChatMessage::Tool {
                tool_call_id,
                content,
                images,
                ..
            } => {
                assert_eq!(tool_call_id, "tc1");
                assert_eq!(content, "image: the whole desktop");
                assert_eq!(images.len(), 1);
            }
            other => panic!("expected a tool result, got {other:?}"),
        }
    }

    /// Transcripts written before the image face existed have no `images`
    /// key at all. They must keep loading — a resume must never fail on
    /// history the product itself wrote.
    #[test]
    fn a_tool_result_written_before_images_still_loads() {
        let old = r#"{"Tool":{"tool_call_id":"tc1","name":null,"content":"done"}}"#;
        let msg: ChatMessage = serde_json::from_str(old).unwrap();
        match msg {
            ChatMessage::Tool {
                content, images, ..
            } => {
                assert_eq!(content, "done");
                assert!(images.is_empty());
            }
            other => panic!("expected a tool result, got {other:?}"),
        }
    }

    /// An empty image list is not written at all: a word-only tool result
    /// serializes byte-for-byte as it did before this field existed.
    #[test]
    fn a_word_only_tool_result_serializes_without_an_images_key() {
        let wire = serde_json::to_string(&ChatMessage::tool("tc1", "done")).unwrap();
        assert!(
            !wire.contains("images"),
            "no empty field on the wire: {wire}"
        );
    }

    #[test]
    fn chat_message_system_ephemeral() {
        let msg = ChatMessage::system_ephemeral("sys");
        assert!(msg.is_ephemeral());
    }

    #[test]
    fn chat_message_user_ephemeral() {
        let msg = ChatMessage::user_ephemeral("hi");
        assert!(msg.is_ephemeral());
    }

    #[test]
    fn chat_message_assistant() {
        let msg = ChatMessage::assistant("response");
        match &msg {
            ChatMessage::Assistant { content, .. } => {
                assert_eq!(content.as_deref(), Some("response"));
            }
            _ => panic!("Expected Assistant variant"),
        }
    }

    #[test]
    fn chat_message_tool_call() {
        let msg = ChatMessage::assistant_tool_call("id1", "echo", r#"{"x":1}"#);
        match &msg {
            ChatMessage::Assistant { tool_calls, .. } => {
                let tc = tool_calls.as_ref().unwrap();
                assert_eq!(tc.len(), 1);
                assert_eq!(tc[0].id, "id1");
                assert_eq!(tc[0].name, "echo");
            }
            _ => panic!("Expected Assistant variant"),
        }
    }

    #[test]
    fn chat_message_tool() {
        let msg = ChatMessage::tool("id1", "result");
        match &msg {
            ChatMessage::Tool {
                tool_call_id,
                name,
                content,
                images,
            } => {
                assert_eq!(tool_call_id, "id1");
                assert!(name.is_none());
                assert_eq!(content, "result");
                assert!(
                    images.is_empty(),
                    "the plain constructor carries no pictures"
                );
            }
            _ => panic!("Expected Tool variant"),
        }
    }

    #[test]
    fn chat_message_tool_with_name() {
        let msg = ChatMessage::tool_with_name("id1", "echo", "result");
        match &msg {
            ChatMessage::Tool {
                tool_call_id,
                name,
                content,
                images,
            } => {
                assert_eq!(tool_call_id, "id1");
                assert_eq!(name.as_deref(), Some("echo"));
                assert_eq!(content, "result");
                assert!(
                    images.is_empty(),
                    "the plain constructor carries no pictures"
                );
            }
            _ => panic!("Expected Tool variant"),
        }
    }

    #[test]
    fn custom_message_not_ephemeral() {
        let msg = ChatMessage::Custom {
            role: "artifact".to_string(),
            data: serde_json::json!({}),
        };
        assert!(!msg.is_ephemeral());
    }

    #[test]
    fn serialization_roundtrip() {
        let msg = ChatMessage::user("hello");
        let json = serde_json::to_string(&msg).unwrap();
        let deserialized: ChatMessage = serde_json::from_str(&json).unwrap();
        match deserialized {
            ChatMessage::User { content, .. } => assert_eq!(content, "hello"),
            _ => panic!("Expected User"),
        }
    }
}