Download codex-rs/code-mode-protocol/src/host/message.rs from SaylorTwift/codex: direct link, hf CLI and curl.
- Browser
- Download file 7.87 kB
-
https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/code-mode-protocol/src/host/message.rs
- Command line
-
hf download hf://SaylorTwift/codex/codex-rs/code-mode-protocol/src/host/message.rs
-
curl -L -o message.rs https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/code-mode-protocol/src/host/message.rs
7.87 kB
| use std::fmt; | |
| use serde::Deserialize; | |
| use serde::Serialize; | |
| use serde_json::Value as JsonValue; | |
| use super::Capability; | |
| use super::CapabilitySet; | |
| use super::DelegateRequestId; | |
| use super::HandshakeRejectReason; | |
| use super::ProtocolVersion; | |
| use super::RequestId; | |
| use super::SessionId; | |
| use super::SupportedProtocolVersions; | |
| use super::WireCellId; | |
| use super::WireExecuteRequest; | |
| use super::WireNestedToolCall; | |
| use super::WireRuntimeResponse; | |
| use super::WireSessionCellExecutionLimits; | |
| use super::WireWaitOutcome; | |
| use super::WireWaitRequest; | |
| pub struct ClientHello { | |
| supported_versions: SupportedProtocolVersions, | |
| required_capabilities: CapabilitySet, | |
| optional_capabilities: CapabilitySet, | |
| } | |
| impl ClientHello { | |
| pub fn new( | |
| supported_versions: SupportedProtocolVersions, | |
| required_capabilities: CapabilitySet, | |
| optional_capabilities: CapabilitySet, | |
| ) -> Result<Self, ClientHelloError> { | |
| if let Some(capability) = required_capabilities | |
| .iter() | |
| .find(|capability| optional_capabilities.contains(capability)) | |
| { | |
| return Err(ClientHelloError::OverlappingCapability(capability.clone())); | |
| } | |
| Ok(Self { | |
| supported_versions, | |
| required_capabilities, | |
| optional_capabilities, | |
| }) | |
| } | |
| pub fn supported_versions(&self) -> &SupportedProtocolVersions { | |
| &self.supported_versions | |
| } | |
| pub fn required_capabilities(&self) -> &CapabilitySet { | |
| &self.required_capabilities | |
| } | |
| pub fn optional_capabilities(&self) -> &CapabilitySet { | |
| &self.optional_capabilities | |
| } | |
| } | |
| struct ClientHelloWire { | |
| supported_versions: SupportedProtocolVersions, | |
| required_capabilities: CapabilitySet, | |
| optional_capabilities: CapabilitySet, | |
| } | |
| impl<'de> Deserialize<'de> for ClientHello { | |
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | |
| where | |
| D: serde::Deserializer<'de>, | |
| { | |
| let wire = ClientHelloWire::deserialize(deserializer)?; | |
| Self::new( | |
| wire.supported_versions, | |
| wire.required_capabilities, | |
| wire.optional_capabilities, | |
| ) | |
| .map_err(serde::de::Error::custom) | |
| } | |
| } | |
| pub enum ClientHelloError { | |
| OverlappingCapability(Capability), | |
| } | |
| impl fmt::Display for ClientHelloError { | |
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| match self { | |
| Self::OverlappingCapability(capability) => write!( | |
| formatter, | |
| "capability `{capability}` cannot be both required and optional" | |
| ), | |
| } | |
| } | |
| } | |
| impl std::error::Error for ClientHelloError {} | |
| pub struct HostHello { | |
| selected_version: ProtocolVersion, | |
| capabilities: CapabilitySet, | |
| } | |
| impl HostHello { | |
| pub fn new(selected_version: ProtocolVersion, capabilities: CapabilitySet) -> Self { | |
| Self { | |
| selected_version, | |
| capabilities, | |
| } | |
| } | |
| pub fn selected_version(&self) -> ProtocolVersion { | |
| self.selected_version | |
| } | |
| pub fn capabilities(&self) -> &CapabilitySet { | |
| &self.capabilities | |
| } | |
| } | |
| /// Messages sent from a client to the code-mode host. | |
| pub enum ClientToHost { | |
| ClientHello(ClientHello), | |
| Request { id: RequestId, request: HostRequest }, | |
| CancelRequest { id: RequestId }, | |
| DelegateResponse { | |
| id: DelegateRequestId, | |
| result: WireResult<DelegateResponse>, | |
| }, | |
| } | |
| /// Messages sent from the code-mode host to a client. | |
| pub enum HostToClient { | |
| HostHello(HostHello), | |
| HandshakeRejected { reason: HandshakeRejectReason }, | |
| Response { | |
| id: RequestId, | |
| result: WireResult<HostResponse>, | |
| }, | |
| InitialResponse { | |
| id: RequestId, | |
| result: WireResult<WireRuntimeResponse>, | |
| }, | |
| DelegateRequest { | |
| id: DelegateRequestId, | |
| session_id: SessionId, | |
| request: DelegateRequest, | |
| }, | |
| CancelDelegateRequest { id: DelegateRequestId }, | |
| CellClosed { | |
| session_id: SessionId, | |
| cell_id: WireCellId, | |
| }, | |
| } | |
| pub enum HostRequest { | |
| OpenSession { | |
| session_id: SessionId, | |
| cell_execution_limits: Option<WireSessionCellExecutionLimits>, | |
| }, | |
| Execute { | |
| session_id: SessionId, | |
| request: WireExecuteRequest, | |
| }, | |
| Wait { | |
| session_id: SessionId, | |
| request: WireWaitRequest, | |
| }, | |
| Terminate { | |
| session_id: SessionId, | |
| cell_id: WireCellId, | |
| }, | |
| ShutdownSession { session_id: SessionId }, | |
| } | |
| pub enum HostResponse { | |
| SessionReady { session_id: SessionId }, | |
| ExecutionStarted { cell_id: WireCellId }, | |
| WaitCompleted { outcome: WireWaitOutcome }, | |
| SessionClosed { session_id: SessionId }, | |
| } | |
| pub enum DelegateRequest { | |
| InvokeTool { invocation: WireNestedToolCall }, | |
| Notify { | |
| call_id: String, | |
| cell_id: WireCellId, | |
| text: String, | |
| }, | |
| } | |
| pub enum DelegateResponse { | |
| ToolResult { result: JsonValue }, | |
| NotificationDelivered, | |
| } | |
| pub enum WireResult<T> { | |
| Ok { value: T }, | |
| Err { message: String }, | |
| } | |
| impl<T> WireResult<T> { | |
| pub fn from_result(result: Result<T, String>) -> Self { | |
| match result { | |
| Ok(value) => Self::Ok { value }, | |
| Err(message) => Self::Err { message }, | |
| } | |
| } | |
| pub fn into_result(self) -> Result<T, String> { | |
| match self { | |
| Self::Ok { value } => Ok(value), | |
| Self::Err { message } => Err(message), | |
| } | |
| } | |
| } | |