| 1 | use serde::{Deserialize, Deserializer, Serialize}; |
| 2 | use serde_json::Value; |
| 3 | |
| 4 | pub use crate::v2::{ThreadExecutor, ThreadPlacement as ThreadEnvironment}; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 7 | #[serde(untagged)] |
| 8 | pub enum RequestId { |
| 9 | String(String), |
| 10 | Integer(i64), |
| 11 | } |
| 12 | |
| 13 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 14 | pub struct W3cTraceContext { |
| 15 | pub traceparent: Option<String>, |
| 16 | pub tracestate: Option<String>, |
| 17 | } |
| 18 | |
| 19 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 20 | pub struct RpcRequest { |
| 21 | pub id: RequestId, |
| 22 | pub method: String, |
| 23 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 24 | pub params: Option<Value>, |
| 25 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 26 | pub trace: Option<W3cTraceContext>, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 30 | pub struct RpcNotification { |
| 31 | pub method: String, |
| 32 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 33 | pub params: Option<Value>, |
| 34 | } |
| 35 | |
| 36 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 37 | pub struct RpcResponse { |
| 38 | pub id: RequestId, |
| 39 | pub result: Value, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 43 | pub struct RpcErrorResponse { |
| 44 | pub id: RequestId, |
| 45 | pub error: RpcErrorDetails, |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 49 | pub struct RpcErrorDetails { |
| 50 | pub code: i64, |
| 51 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 52 | pub data: Option<Value>, |
| 53 | pub message: String, |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug, Clone, Serialize)] |
| 57 | pub struct RpcCall { |
| 58 | #[serde(skip_serializing_if = "Option::is_none")] |
| 59 | pub id: Option<RequestId>, |
| 60 | pub method: String, |
| 61 | #[serde(skip_serializing_if = "Value::is_null")] |
| 62 | pub params: Value, |
| 63 | #[serde(skip_serializing_if = "Option::is_none")] |
| 64 | pub trace: Option<W3cTraceContext>, |
| 65 | } |
| 66 | |
| 67 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 68 | #[serde(untagged)] |
| 69 | pub enum RpcMessage { |
| 70 | Request(RpcRequest), |
| 71 | Notification(RpcNotification), |
| 72 | Response(RpcResponse), |
| 73 | Error(RpcErrorResponse), |
| 74 | } |
| 75 | |
| 76 | impl RpcMessage { |
| 77 | pub fn into_call(self) -> Option<RpcCall> { |
| 78 | match self { |
| 79 | Self::Request(request) => Some(RpcCall { |
| 80 | id: Some(request.id), |
| 81 | method: request.method, |
| 82 | params: request.params.unwrap_or(Value::Null), |
| 83 | trace: request.trace, |
| 84 | }), |
| 85 | Self::Notification(notification) => Some(RpcCall { |
| 86 | id: None, |
| 87 | method: notification.method, |
| 88 | params: notification.params.unwrap_or(Value::Null), |
| 89 | trace: None, |
| 90 | }), |
| 91 | Self::Response(_) | Self::Error(_) => None, |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl<'de> Deserialize<'de> for RpcCall { |
| 97 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 98 | RpcMessage::deserialize(deserializer)? |
| 99 | .into_call() |
| 100 | .ok_or_else(|| serde::de::Error::custom("RPC response is not a call")) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] |
| 105 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 106 | #[serde(rename_all = "camelCase")] |
| 107 | pub struct ThreadLoadedListParams { |
| 108 | pub cursor: Option<String>, |
| 109 | pub limit: Option<u32>, |
| 110 | } |
| 111 | |
| 112 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 113 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 114 | #[serde(rename_all = "camelCase")] |
| 115 | pub struct ThreadLoadedListResponse { |
| 116 | pub data: Vec<String>, |
| 117 | pub next_cursor: Option<String>, |
| 118 | } |
| 119 | |
| 120 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 121 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 122 | #[serde(rename_all = "camelCase")] |
| 123 | pub struct ThreadListResponse { |
| 124 | pub data: Vec<ThreadListItem>, |
| 125 | pub next_cursor: Option<String>, |
| 126 | pub backwards_cursor: Option<String>, |
| 127 | } |
| 128 | |
| 129 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 130 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 131 | #[serde(rename_all = "camelCase")] |
| 132 | pub struct ThreadListItem { |
| 133 | pub id: String, |
| 134 | pub session_id: String, |
| 135 | pub preview: String, |
| 136 | pub ephemeral: bool, |
| 137 | pub model_provider: String, |
| 138 | pub created_at: i64, |
| 139 | pub updated_at: i64, |
| 140 | pub status: ThreadListStatus, |
| 141 | pub cwd: String, |
| 142 | pub cli_version: String, |
| 143 | pub source: ThreadListSource, |
| 144 | pub project_id: Option<String>, |
| 145 | pub turns: Vec<serde_json::Value>, |
| 146 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 147 | pub name: Option<String>, |
| 148 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 149 | pub executor: Option<ThreadExecutor>, |
| 150 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 151 | pub environment: Option<ThreadEnvironment>, |
| 152 | } |
| 153 | |
| 154 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 155 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 156 | #[serde(tag = "type", rename_all = "camelCase")] |
| 157 | pub enum ThreadListStatus { |
| 158 | NotLoaded, |
| 159 | Idle, |
| 160 | SystemError, |
| 161 | Active { |
| 162 | #[serde(rename = "activeFlags")] |
| 163 | active_flags: Vec<String>, |
| 164 | }, |
| 165 | } |
| 166 | |
| 167 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 168 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 169 | #[serde(rename_all = "camelCase")] |
| 170 | pub enum ThreadListSource { |
| 171 | Cli, |
| 172 | AppServer, |
| 173 | Unknown, |
| 174 | } |
| 175 | |
| 176 | #[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] |
| 177 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 178 | #[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 179 | pub struct ThreadStartParams { |
| 180 | pub operation_key: Option<String>, |
| 181 | pub model: Option<String>, |
| 182 | pub model_provider: Option<String>, |
| 183 | pub cwd: Option<String>, |
| 184 | pub ephemeral: Option<bool>, |
| 185 | pub approval_policy: Option<Value>, |
| 186 | pub approvals_reviewer: Option<Value>, |
| 187 | pub sandbox: Option<Value>, |
| 188 | pub config: Option<Value>, |
| 189 | pub base_instructions: Option<String>, |
| 190 | pub developer_instructions: Option<String>, |
| 191 | pub service_name: Option<String>, |
| 192 | pub service_tier: Option<String>, |
| 193 | pub session_start_source: Option<Value>, |
| 194 | pub personality: Option<Value>, |
| 195 | pub thread_source: Option<String>, |
| 196 | } |
| 197 | |
| 198 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 199 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 200 | #[serde(rename_all = "camelCase")] |
| 201 | pub struct ThreadStartResponse { |
| 202 | pub thread: ThreadListItem, |
| 203 | pub model: String, |
| 204 | pub model_provider: String, |
| 205 | pub cwd: String, |
| 206 | pub approval_policy: String, |
| 207 | pub approvals_reviewer: String, |
| 208 | pub sandbox: ThreadSandboxPolicy, |
| 209 | } |
| 210 | |
| 211 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 212 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 213 | #[serde(tag = "type", rename_all = "camelCase")] |
| 214 | pub enum ThreadSandboxPolicy { |
| 215 | ReadOnly, |
| 216 | } |
| 217 | |
| 218 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 219 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 220 | #[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 221 | pub struct ThreadReadParams { |
| 222 | pub thread_id: String, |
| 223 | #[serde(default)] |
| 224 | pub include_turns: bool, |
| 225 | } |
| 226 | |
| 227 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 228 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 229 | #[serde(rename_all = "camelCase")] |
| 230 | pub struct ThreadReadResponse { |
| 231 | pub thread: ThreadListItem, |
| 232 | } |
| 233 | |
| 234 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 235 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 236 | #[serde(rename_all = "camelCase")] |
| 237 | pub struct ThreadTurn { |
| 238 | pub id: String, |
| 239 | pub items: Vec<ThreadTurnItem>, |
| 240 | pub items_view: ThreadTurnItemsView, |
| 241 | pub status: ThreadTurnStatus, |
| 242 | pub error: Option<ThreadTurnError>, |
| 243 | pub started_at: Option<i64>, |
| 244 | pub completed_at: Option<i64>, |
| 245 | pub duration_ms: Option<i64>, |
| 246 | } |
| 247 | |
| 248 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 249 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 250 | #[serde(rename_all = "camelCase")] |
| 251 | pub enum ThreadTurnItemsView { |
| 252 | Summary, |
| 253 | Full, |
| 254 | } |
| 255 | |
| 256 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 257 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 258 | #[serde(rename_all = "camelCase")] |
| 259 | pub enum ThreadTurnStatus { |
| 260 | Completed, |
| 261 | Interrupted, |
| 262 | Failed, |
| 263 | InProgress, |
| 264 | } |
| 265 | |
| 266 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 267 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 268 | #[serde(rename_all = "camelCase")] |
| 269 | pub struct ThreadTurnError { |
| 270 | pub message: String, |
| 271 | } |
| 272 | |
| 273 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 274 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 275 | #[serde(tag = "type", rename_all = "camelCase")] |
| 276 | pub enum ThreadTurnItem { |
| 277 | UserMessage { |
| 278 | id: String, |
| 279 | content: Vec<ThreadUserInput>, |
| 280 | }, |
| 281 | AgentMessage { |
| 282 | id: String, |
| 283 | text: String, |
| 284 | }, |
| 285 | Reasoning { |
| 286 | id: String, |
| 287 | content: Vec<String>, |
| 288 | }, |
| 289 | ContextCompaction { |
| 290 | id: String, |
| 291 | }, |
| 292 | } |
| 293 | |
| 294 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 295 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 296 | #[serde(tag = "type", rename_all = "camelCase")] |
| 297 | pub enum ThreadUserInput { |
| 298 | Text { text: String }, |
| 299 | } |
| 300 | |
| 301 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 302 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 303 | #[serde(rename_all = "camelCase")] |
| 304 | pub struct ThreadSetNameParams { |
| 305 | pub thread_id: String, |
| 306 | pub name: String, |
| 307 | } |
| 308 | |
| 309 | #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] |
| 310 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 311 | #[serde(deny_unknown_fields)] |
| 312 | pub struct ThreadSetNameResponse {} |
| 313 |