| 1 | use crate::{DispatchError, params_or_object, parse_params}; |
| 2 | use codewhale_app_server_protocol::lifecycle::{InitializeParams, InitializeResponse}; |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 6 | pub(crate) struct ConnectionId(u64); |
| 7 | |
| 8 | impl ConnectionId { |
| 9 | fn mint() -> Self { |
| 10 | static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); |
| 11 | Self(NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | impl std::fmt::Display for ConnectionId { |
| 16 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 17 | self.0.fmt(f) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | pub(crate) struct ConnectionSession { |
| 22 | id: ConnectionId, |
| 23 | initialization: Option<InitializeParams>, |
| 24 | } |
| 25 | |
| 26 | impl Default for ConnectionSession { |
| 27 | fn default() -> Self { |
| 28 | Self { |
| 29 | id: ConnectionId::mint(), |
| 30 | initialization: None, |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl ConnectionSession { |
| 36 | pub(crate) fn id(&self) -> ConnectionId { |
| 37 | self.id |
| 38 | } |
| 39 | |
| 40 | pub(crate) fn initialized(&self) -> bool { |
| 41 | self.initialization.is_some() |
| 42 | } |
| 43 | |
| 44 | pub(crate) fn initialize(&mut self, params: Value) -> Result<Value, DispatchError> { |
| 45 | if self.initialized() { |
| 46 | return Err(DispatchError::invalid_request("Already initialized")); |
| 47 | } |
| 48 | let params: InitializeParams = parse_params(params_or_object(params))?; |
| 49 | axum::http::HeaderValue::from_str(¶ms.client_info.name) |
| 50 | .map_err(|_| DispatchError::invalid_request("Invalid clientInfo.name"))?; |
| 51 | if let Some(capabilities) = params.capabilities.as_ref() |
| 52 | && (capabilities.explicit_gateway_oauth |
| 53 | || capabilities.request_attestation |
| 54 | || capabilities.mcp_server_openai_form_elicitation |
| 55 | || capabilities |
| 56 | .opt_out_notification_methods |
| 57 | .as_ref() |
| 58 | .is_some_and(|methods| !methods.is_empty()) |
| 59 | || capabilities |
| 60 | .extensions |
| 61 | .as_ref() |
| 62 | .is_some_and(|extensions| !extensions.is_empty())) |
| 63 | { |
| 64 | return Err(DispatchError::invalid_request( |
| 65 | "Requested initialization capability is not implemented", |
| 66 | )); |
| 67 | } |
| 68 | let home = codewhale_paths::codewhale_home() |
| 69 | .map_err(|error| DispatchError::internal(error.to_string()))? |
| 70 | .filter(|path| path.is_absolute()) |
| 71 | .ok_or_else(|| DispatchError::internal("absolute Codewhale home unavailable"))?; |
| 72 | let response = InitializeResponse { |
| 73 | user_agent: format!( |
| 74 | "Codewhale/{} ({}; {})", |
| 75 | env!("CARGO_PKG_VERSION"), |
| 76 | params.client_info.name, |
| 77 | params.client_info.version |
| 78 | ), |
| 79 | codex_home: home, |
| 80 | platform_family: std::env::consts::FAMILY.to_string(), |
| 81 | platform_os: std::env::consts::OS.to_string(), |
| 82 | }; |
| 83 | let value = serde_json::to_value(response) |
| 84 | .map_err(|error| DispatchError::internal(error.to_string()))?; |
| 85 | self.initialization = Some(params); |
| 86 | Ok(value) |
| 87 | } |
| 88 | } |
| 89 |