Skip to main content

kcode_k1_codex_web_search_protocol_values/
lib.rs

1use 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    TurnTerminal {
125        id: String,
126        status: TurnStatus,
127        usage: Option<Usage>,
128        error: Option<ProviderError>,
129    },
130    ProviderError(ProviderError),
131    Ignored {
132        method: String,
133        item_type: Option<String>,
134    },
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140
141    fn assert_value_traits<T: Clone + fmt::Debug + Eq + PartialEq>() {}
142    fn assert_scalar_traits<T: Copy>() {}
143
144    #[test]
145    fn values_have_expected_traits_and_shape() {
146        assert_value_traits::<RpcResponse<ChatGptAccount>>();
147        assert_value_traits::<InboundEvent>();
148        assert_scalar_traits::<MessagePhase>();
149        assert_scalar_traits::<TurnStatus>();
150        assert_scalar_traits::<Usage>();
151
152        assert_eq!(
153            RpcResponse::Success(ChatGptAccount),
154            RpcResponse::Success(ChatGptAccount)
155        );
156        assert!(matches!(
157            InboundEvent::Ignored {
158                method: "unknown".to_owned(),
159                item_type: None,
160            },
161            InboundEvent::Ignored {
162                method,
163                item_type: None,
164            } if method == "unknown"
165        ));
166    }
167}