systemprompt_api/services/gateway/protocol/
canonical_response.rs1use super::canonical::CanonicalContent;
2
3#[derive(Debug, Clone, Copy, Default)]
4pub struct CanonicalUsage {
5 pub input_tokens: u32,
6 pub output_tokens: u32,
7}
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum CanonicalStopReason {
11 EndTurn,
12 MaxTokens,
13 StopSequence,
14 ToolUse,
15 Other,
16}
17
18impl CanonicalStopReason {
19 pub const fn anthropic_str(self) -> &'static str {
20 match self {
21 Self::MaxTokens => "max_tokens",
22 Self::StopSequence => "stop_sequence",
23 Self::ToolUse => "tool_use",
24 Self::EndTurn | Self::Other => "end_turn",
25 }
26 }
27
28 pub const fn openai_str(self) -> &'static str {
29 match self {
30 Self::MaxTokens => "length",
31 Self::ToolUse => "tool_calls",
32 Self::EndTurn | Self::StopSequence | Self::Other => "stop",
33 }
34 }
35
36 pub fn from_anthropic(s: &str) -> Self {
37 match s {
38 "end_turn" => Self::EndTurn,
39 "max_tokens" => Self::MaxTokens,
40 "stop_sequence" => Self::StopSequence,
41 "tool_use" => Self::ToolUse,
42 _ => Self::Other,
43 }
44 }
45
46 pub fn from_openai(s: &str) -> Self {
47 match s {
48 "stop" => Self::EndTurn,
49 "length" => Self::MaxTokens,
50 "tool_calls" | "function_call" => Self::ToolUse,
51 _ => Self::Other,
52 }
53 }
54}
55
56#[derive(Debug, Clone)]
57pub struct CanonicalResponse {
58 pub id: String,
59 pub model: String,
60 pub content: Vec<CanonicalContent>,
61 pub stop_reason: Option<CanonicalStopReason>,
62 pub usage: CanonicalUsage,
63}
64
65#[derive(Debug, Clone)]
66pub enum CanonicalEvent {
67 MessageStart {
68 id: String,
69 model: String,
70 usage: CanonicalUsage,
71 },
72 ContentBlockStart {
73 index: u32,
74 block: ContentBlockKind,
75 },
76 TextDelta {
77 index: u32,
78 text: String,
79 },
80 ThinkingDelta {
81 index: u32,
82 text: String,
83 },
84 ToolUseDelta {
85 index: u32,
86 partial_json: String,
87 },
88 ContentBlockStop {
89 index: u32,
90 },
91 UsageDelta(CanonicalUsage),
92 MessageStop {
93 id: String,
94 stop_reason: Option<CanonicalStopReason>,
95 },
96 Error(String),
97}
98
99#[derive(Debug, Clone)]
100pub enum ContentBlockKind {
101 Text,
102 Thinking { signature: Option<String> },
103 ToolUse { id: String, name: String },
104}