tea-protocol 0.1.0

Canonical protocol types for the tea-rs project
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use std::fmt;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use thiserror::Error;

use crate::content::{ContentBlock, ContentValidationError, validate_tool_name};
use crate::{MessageId, ProtocolTimestamp, ToolCallId};

/// Role of a canonical message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageRole {
    /// User-authored input.
    User,
    /// Model-authored output.
    Assistant,
    /// Result of a tool call.
    ToolResult,
}

/// A provider-neutral conversation message.
#[derive(Debug, Clone, PartialEq)]
pub enum CanonicalMessage {
    /// User-authored input.
    User {
        /// Stable message identifier.
        id: MessageId,
        /// User-visible text or images.
        content: Vec<ContentBlock>,
        /// Message creation time.
        timestamp: ProtocolTimestamp,
    },
    /// Model-authored output.
    Assistant {
        /// Stable message identifier.
        id: MessageId,
        /// Text, thinking, and tool calls.
        content: Vec<ContentBlock>,
        /// Normalized completion reason.
        stop_reason: StopReason,
        /// Message completion time.
        timestamp: ProtocolTimestamp,
    },
    /// Result returned for a tool call.
    ToolResult {
        /// Stable message identifier.
        id: MessageId,
        /// Tool call receiving this result.
        tool_call_id: ToolCallId,
        /// Registered tool name.
        tool_name: String,
        /// Text or image result content.
        content: Vec<ContentBlock>,
        /// Whether execution failed.
        is_error: bool,
        /// Machine-readable failure details, present only on failure.
        error: Option<ToolFailure>,
        /// Result completion time.
        timestamp: ProtocolTimestamp,
    },
}

impl CanonicalMessage {
    /// Creates a validated user message.
    ///
    /// # Errors
    ///
    /// Returns an error when content is empty, too numerous, or contains a
    /// block that is invalid for the user role.
    pub fn user(
        id: MessageId,
        content: Vec<ContentBlock>,
        timestamp: ProtocolTimestamp,
    ) -> Result<Self, MessageValidationError> {
        validate_content(&content, ContentBlock::valid_for_user)?;
        Ok(Self::User {
            id,
            content,
            timestamp,
        })
    }

    /// Creates a validated assistant message.
    ///
    /// # Errors
    ///
    /// Returns an error when content is empty, too numerous, or contains a
    /// block that is invalid for the assistant role.
    pub fn assistant(
        id: MessageId,
        content: Vec<ContentBlock>,
        stop_reason: StopReason,
        timestamp: ProtocolTimestamp,
    ) -> Result<Self, MessageValidationError> {
        validate_content(&content, ContentBlock::valid_for_assistant)?;
        Ok(Self::Assistant {
            id,
            content,
            stop_reason,
            timestamp,
        })
    }

    /// Creates a successful tool-result message.
    ///
    /// # Errors
    ///
    /// Returns an error when the tool name or result content is invalid.
    pub fn tool_result_success(
        id: MessageId,
        tool_call_id: ToolCallId,
        tool_name: impl Into<String>,
        content: Vec<ContentBlock>,
        timestamp: ProtocolTimestamp,
    ) -> Result<Self, MessageValidationError> {
        Self::tool_result(id, tool_call_id, tool_name.into(), content, None, timestamp)
    }

    /// Creates a failed tool-result message.
    ///
    /// # Errors
    ///
    /// Returns an error when the tool name or result content is invalid.
    pub fn tool_result_failure(
        id: MessageId,
        tool_call_id: ToolCallId,
        tool_name: impl Into<String>,
        content: Vec<ContentBlock>,
        error: ToolFailure,
        timestamp: ProtocolTimestamp,
    ) -> Result<Self, MessageValidationError> {
        Self::tool_result(
            id,
            tool_call_id,
            tool_name.into(),
            content,
            Some(error),
            timestamp,
        )
    }

    /// Returns the message role.
    #[must_use]
    pub const fn role(&self) -> MessageRole {
        match self {
            Self::User { .. } => MessageRole::User,
            Self::Assistant { .. } => MessageRole::Assistant,
            Self::ToolResult { .. } => MessageRole::ToolResult,
        }
    }

    fn validate(&self) -> Result<(), MessageValidationError> {
        match self {
            Self::User { content, .. } => validate_content(content, ContentBlock::valid_for_user),
            Self::Assistant { content, .. } => {
                validate_content(content, ContentBlock::valid_for_assistant)
            }
            Self::ToolResult {
                tool_name,
                content,
                is_error,
                error,
                ..
            } => {
                validate_tool_name(tool_name)?;
                validate_content(content, ContentBlock::valid_for_tool_result)?;
                if *is_error != error.is_some() {
                    return Err(MessageValidationError::InconsistentToolFailure);
                }
                Ok(())
            }
        }
    }

    fn tool_result(
        id: MessageId,
        tool_call_id: ToolCallId,
        tool_name: String,
        content: Vec<ContentBlock>,
        error: Option<ToolFailure>,
        timestamp: ProtocolTimestamp,
    ) -> Result<Self, MessageValidationError> {
        validate_tool_name(&tool_name)?;
        validate_content(&content, ContentBlock::valid_for_tool_result)?;
        Ok(Self::ToolResult {
            id,
            tool_call_id,
            tool_name,
            content,
            is_error: error.is_some(),
            error,
            timestamp,
        })
    }
}

impl Serialize for CanonicalMessage {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.validate().map_err(serde::ser::Error::custom)?;
        SerializableCanonicalMessage::from(self).serialize(serializer)
    }
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum SerializableCanonicalMessage<'a> {
    User {
        id: &'a MessageId,
        content: &'a [ContentBlock],
        timestamp: &'a ProtocolTimestamp,
    },
    Assistant {
        id: &'a MessageId,
        content: &'a [ContentBlock],
        #[serde(rename = "stopReason")]
        stop_reason: &'a StopReason,
        timestamp: &'a ProtocolTimestamp,
    },
    ToolResult {
        id: &'a MessageId,
        #[serde(rename = "toolCallId")]
        tool_call_id: &'a ToolCallId,
        #[serde(rename = "toolName")]
        tool_name: &'a str,
        content: &'a [ContentBlock],
        #[serde(rename = "isError")]
        is_error: bool,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: &'a Option<ToolFailure>,
        timestamp: &'a ProtocolTimestamp,
    },
}

impl<'a> From<&'a CanonicalMessage> for SerializableCanonicalMessage<'a> {
    fn from(value: &'a CanonicalMessage) -> Self {
        match value {
            CanonicalMessage::User {
                id,
                content,
                timestamp,
            } => Self::User {
                id,
                content,
                timestamp,
            },
            CanonicalMessage::Assistant {
                id,
                content,
                stop_reason,
                timestamp,
            } => Self::Assistant {
                id,
                content,
                stop_reason,
                timestamp,
            },
            CanonicalMessage::ToolResult {
                id,
                tool_call_id,
                tool_name,
                content,
                is_error,
                error,
                timestamp,
            } => Self::ToolResult {
                id,
                tool_call_id,
                tool_name,
                content,
                is_error: *is_error,
                error,
                timestamp,
            },
        }
    }
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum RawCanonicalMessage {
    User {
        id: MessageId,
        content: Vec<ContentBlock>,
        timestamp: ProtocolTimestamp,
    },
    Assistant {
        id: MessageId,
        content: Vec<ContentBlock>,
        #[serde(rename = "stopReason")]
        stop_reason: StopReason,
        timestamp: ProtocolTimestamp,
    },
    ToolResult {
        id: MessageId,
        #[serde(rename = "toolCallId")]
        tool_call_id: ToolCallId,
        #[serde(rename = "toolName")]
        tool_name: String,
        content: Vec<ContentBlock>,
        #[serde(rename = "isError")]
        is_error: bool,
        #[serde(default)]
        error: Option<ToolFailure>,
        timestamp: ProtocolTimestamp,
    },
}

impl<'de> Deserialize<'de> for CanonicalMessage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match RawCanonicalMessage::deserialize(deserializer)? {
            RawCanonicalMessage::User {
                id,
                content,
                timestamp,
            } => Self::user(id, content, timestamp),
            RawCanonicalMessage::Assistant {
                id,
                content,
                stop_reason,
                timestamp,
            } => Self::assistant(id, content, stop_reason, timestamp),
            RawCanonicalMessage::ToolResult {
                id,
                tool_call_id,
                tool_name,
                content,
                is_error,
                error,
                timestamp,
            } => {
                if is_error != error.is_some() {
                    return Err(serde::de::Error::custom(
                        "tool-result isError must match error presence",
                    ));
                }
                Self::tool_result(id, tool_call_id, tool_name, content, error, timestamp)
            }
        }
        .map_err(serde::de::Error::custom)
    }
}

/// Normalized assistant stop reason with forward-compatible unknown values.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum StopReason {
    /// Model completed normally.
    Completed,
    /// Model reached an output limit.
    Length,
    /// Model requested one or more tools.
    ToolUse,
    /// Provider paused a server-side tool loop and requires transcript replay.
    PauseTurn,
    /// Operation was cancelled.
    Cancelled,
    /// Provider or runtime failed.
    Error,
    /// A future provider-neutral value not known by this crate.
    Unknown(String),
}

impl StopReason {
    /// Returns the serialized reason.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Completed => "completed",
            Self::Length => "length",
            Self::ToolUse => "tool_use",
            Self::PauseTurn => "pause_turn",
            Self::Cancelled => "cancelled",
            Self::Error => "error",
            Self::Unknown(value) => value,
        }
    }

    /// Returns whether this reason represents normal completion.
    #[must_use]
    pub const fn is_success(&self) -> bool {
        matches!(self, Self::Completed)
    }
}

impl Serialize for StopReason {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if !valid_error_code(self.as_str()) {
            return Err(serde::ser::Error::custom("invalid stop reason"));
        }
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for StopReason {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        if !valid_error_code(&value) {
            return Err(serde::de::Error::custom("invalid stop reason"));
        }
        Ok(match value.as_str() {
            "completed" => Self::Completed,
            "length" => Self::Length,
            "tool_use" => Self::ToolUse,
            "pause_turn" => Self::PauseTurn,
            "cancelled" => Self::Cancelled,
            "error" => Self::Error,
            _ => Self::Unknown(value),
        })
    }
}

/// Machine-readable failure returned by a tool.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolFailure {
    code: String,
    message: String,
}

impl ToolFailure {
    /// Creates the canonical model-visible approval-denied failure.
    #[must_use]
    pub fn approval_denied() -> Self {
        Self {
            code: "approval_denied".to_owned(),
            message: "tool invocation was denied by approval".to_owned(),
        }
    }

    /// Creates validated tool failure details.
    ///
    /// # Errors
    ///
    /// Returns an error when the code is not canonical snake case or the
    /// technical message is empty, oversized, or contains a null character.
    pub fn new(
        code: impl Into<String>,
        message: impl Into<String>,
    ) -> Result<Self, MessageValidationError> {
        let code = code.into();
        let message = message.into();
        if !valid_error_code(&code)
            || message.is_empty()
            || message.len() > 4096
            || message.contains('\0')
        {
            return Err(MessageValidationError::InvalidToolFailure);
        }
        Ok(Self { code, message })
    }

    /// Returns the machine-readable failure code.
    #[must_use]
    pub fn code(&self) -> &str {
        &self.code
    }

    /// Returns the English technical diagnostic message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }
}

#[derive(Deserialize)]
struct RawToolFailure {
    code: String,
    message: String,
}

impl<'de> Deserialize<'de> for ToolFailure {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = RawToolFailure::deserialize(deserializer)?;
        Self::new(raw.code, raw.message).map_err(serde::de::Error::custom)
    }
}

/// Error returned when validating canonical messages.
#[derive(Debug, Error)]
pub enum MessageValidationError {
    /// The content array is empty or contains a block invalid for the role.
    #[error("message content is empty or contains a block invalid for its role")]
    InvalidContent,
    /// Content or tool-name validation failed.
    #[error("invalid message content: {0}")]
    InvalidContentValue(#[from] ContentValidationError),
    /// Tool failure code or message is invalid.
    #[error("tool failure code or message is invalid")]
    InvalidToolFailure,
    /// Tool-result error flag does not match failure presence.
    #[error("tool-result isError must match error presence")]
    InconsistentToolFailure,
}

fn validate_content(
    content: &[ContentBlock],
    predicate: impl Fn(&ContentBlock) -> bool,
) -> Result<(), MessageValidationError> {
    if content.is_empty() || content.len() > 256 || !content.iter().all(predicate) {
        return Err(MessageValidationError::InvalidContent);
    }
    for block in content {
        block.validate()?;
    }
    Ok(())
}

fn valid_error_code(value: &str) -> bool {
    let mut bytes = value.bytes();
    bytes.next().is_some_and(|byte| byte.is_ascii_lowercase())
        && value.len() <= 128
        && bytes.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
}

impl fmt::Display for StopReason {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}