kcode_k1_codex_web_search_protocol_values/
lib.rs1use std::error::Error;
2use std::fmt;
3
4#[derive(Debug)]
5pub enum ProtocolError {
6 Invalid(&'static str),
7 Json(serde_json::Error),
8}
9
10impl fmt::Display for ProtocolError {
11 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
12 match self {
13 Self::Invalid(message) => formatter.write_str(message),
14 Self::Json(error) => error.fmt(formatter),
15 }
16 }
17}
18
19impl Error for ProtocolError {
20 fn source(&self) -> Option<&(dyn Error + 'static)> {
21 match self {
22 Self::Invalid(_) => None,
23 Self::Json(error) => Some(error),
24 }
25 }
26}
27
28impl From<serde_json::Error> for ProtocolError {
29 fn from(error: serde_json::Error) -> Self {
30 Self::Json(error)
31 }
32}
33
34#[derive(Clone, Debug, Eq, PartialEq)]
35pub enum RpcResponse<T> {
36 Success(T),
37 Error(RpcError),
38}
39
40#[derive(Clone, Debug, Eq, PartialEq)]
41pub struct RpcError {
42 pub code: i64,
43 pub message: String,
44}
45
46#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47pub struct ChatGptAccount;
48
49#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct ReasoningEffort {
51 pub reasoning_effort: String,
52}
53
54#[derive(Clone, Debug, Eq, PartialEq)]
55pub struct Model {
56 pub id: String,
57 pub supported_reasoning_efforts: Vec<ReasoningEffort>,
58 pub default_reasoning_effort: Option<String>,
59}
60
61#[derive(Clone, Debug, Eq, PartialEq)]
62pub struct ModelPage {
63 pub data: Vec<Model>,
64 pub next_cursor: Option<String>,
65}
66
67#[derive(Clone, Debug, Eq, PartialEq)]
68pub struct Started {
69 pub id: String,
70}
71
72pub type ThreadStarted = Started;
73pub type TurnStarted = Started;
74
75#[derive(Clone, Copy, Debug, Eq, PartialEq)]
76pub enum MessagePhase {
77 Commentary,
78 FinalAnswer,
79 Unknown,
80}
81
82#[derive(Clone, Debug, Eq, PartialEq)]
83pub struct EventScope {
84 pub thread_id: String,
85 pub turn_id: String,
86}
87
88#[derive(Clone, Debug, Eq, PartialEq)]
89pub struct SourceCandidate {
90 pub url: String,
91 pub title: String,
92}
93
94#[derive(Clone, Copy, Debug, Eq, PartialEq)]
95pub enum TurnStatus {
96 Completed,
97 Failed,
98 Interrupted,
99}
100
101#[derive(Clone, Copy, Debug, Eq, PartialEq)]
102pub struct Usage {
103 pub input_tokens: Option<u64>,
104 pub cached_input_tokens: Option<u64>,
105 pub output_tokens: Option<u64>,
106 pub reasoning_output_tokens: Option<u64>,
107}
108
109pub type ProviderError = String;
110
111#[derive(Clone, Debug, Eq, PartialEq)]
112pub enum InboundEvent {
113 AgentMessage {
114 scope: EventScope,
115 id: String,
116 phase: MessagePhase,
117 text: String,
118 },
119 SourceCandidates {
120 scope: EventScope,
121 id: String,
122 sources: Vec<SourceCandidate>,
123 },
124 UsageUpdated {
125 scope: EventScope,
126 usage: Usage,
127 },
128 TurnTerminal {
129 id: String,
130 status: TurnStatus,
131 usage: Option<Usage>,
132 error: Option<ProviderError>,
133 },
134 ProviderError(ProviderError),
135 Ignored {
136 method: String,
137 item_type: Option<String>,
138 },
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 fn assert_value_traits<T: Clone + fmt::Debug + Eq + PartialEq>() {}
146 fn assert_scalar_traits<T: Copy>() {}
147
148 #[test]
149 fn values_have_expected_traits_and_shape() {
150 assert_value_traits::<RpcResponse<ChatGptAccount>>();
151 assert_value_traits::<InboundEvent>();
152 assert_scalar_traits::<MessagePhase>();
153 assert_scalar_traits::<TurnStatus>();
154 assert_scalar_traits::<Usage>();
155
156 assert_eq!(
157 RpcResponse::Success(ChatGptAccount),
158 RpcResponse::Success(ChatGptAccount)
159 );
160 assert!(matches!(
161 InboundEvent::Ignored {
162 method: "unknown".to_owned(),
163 item_type: None,
164 },
165 InboundEvent::Ignored {
166 method,
167 item_type: None,
168 } if method == "unknown"
169 ));
170 assert!(matches!(
171 InboundEvent::UsageUpdated {
172 scope: EventScope {
173 thread_id: "thread".to_owned(),
174 turn_id: "turn".to_owned(),
175 },
176 usage: Usage {
177 input_tokens: Some(1),
178 cached_input_tokens: Some(2),
179 output_tokens: Some(3),
180 reasoning_output_tokens: Some(4),
181 },
182 },
183 InboundEvent::UsageUpdated {
184 scope: EventScope { thread_id, turn_id },
185 usage: Usage {
186 input_tokens: Some(1),
187 cached_input_tokens: Some(2),
188 output_tokens: Some(3),
189 reasoning_output_tokens: Some(4),
190 },
191 } if thread_id == "thread" && turn_id == "turn"
192 ));
193 }
194}