Download codex-rs/app-server-protocol/src/protocol/v2/shared.rs from SaylorTwift/codex: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/app-server-protocol/src/protocol/v2/shared.rs
- Command line
-
hf download hf://SaylorTwift/codex/codex-rs/app-server-protocol/src/protocol/v2/shared.rs
-
curl -L -o shared.rs https://huggingface.co/SaylorTwift/codex/resolve/main/codex-rs/app-server-protocol/src/protocol/v2/shared.rs
12.2 kB
| use crate::JsonSchema; | |
| use crate::TS; | |
| use codex_experimental_api_macros::ExperimentalApi; | |
| use codex_protocol::config_types::ApprovalsReviewer as CoreApprovalsReviewer; | |
| use codex_protocol::config_types::SandboxMode as CoreSandboxMode; | |
| use codex_protocol::protocol::AskForApproval as CoreAskForApproval; | |
| use codex_protocol::protocol::CodexErrorInfo as CoreCodexErrorInfo; | |
| use codex_protocol::protocol::GranularApprovalConfig as CoreGranularApprovalConfig; | |
| use codex_protocol::protocol::NonSteerableTurnKind as CoreNonSteerableTurnKind; | |
| use schemars::r#gen::SchemaGenerator; | |
| use schemars::schema::InstanceType; | |
| use schemars::schema::Metadata; | |
| use schemars::schema::Schema; | |
| use schemars::schema::SchemaObject; | |
| use serde::Deserialize; | |
| use serde::Serialize; | |
| use serde_json::Value as JsonValue; | |
| // Macro to declare a camelCased API v2 enum mirroring a core enum which | |
| // tends to use either snake_case or kebab-case. | |
| macro_rules! v2_enum_from_core { | |
| ( | |
| $()* | |
| pub enum $Name:ident from $Src:path { | |
| $( $()* $Variant:ident ),+ $(,)? | |
| } | |
| ) => { | |
| $()* | |
| pub enum $Name { | |
| $( $()* $Variant ),+ | |
| } | |
| impl $Name { | |
| pub fn to_core(self) -> $Src { | |
| match self { $( $Name::$Variant => <$Src>::$Variant ),+ } | |
| } | |
| } | |
| impl From<$Src> for $Name { | |
| fn from(value: $Src) -> Self { | |
| match value { $( <$Src>::$Variant => $Name::$Variant ),+ } | |
| } | |
| } | |
| }; | |
| } | |
| pub(super) use v2_enum_from_core; | |
| pub(super) const fn default_enabled() -> bool { | |
| true | |
| } | |
| pub enum NonSteerableTurnKind { | |
| Review, | |
| Compact, | |
| } | |
| /// This translation layer make sure that we expose codex error code in camel case. | |
| /// | |
| /// When an upstream HTTP status is available (for example, from the Responses API or a provider), | |
| /// it is forwarded in `httpStatusCode` on the relevant `codexErrorInfo` variant. | |
| pub enum CodexErrorInfo { | |
| ContextWindowExceeded, | |
| SessionBudgetExceeded, | |
| UsageLimitExceeded, | |
| RateLimitExceeded, | |
| ServerOverloaded, | |
| CyberPolicy, | |
| MisalignmentPolicyViolation, | |
| HttpConnectionFailed { | |
| http_status_code: Option<u16>, | |
| }, | |
| /// Failed to connect to the response SSE stream. | |
| ResponseStreamConnectionFailed { | |
| http_status_code: Option<u16>, | |
| }, | |
| InternalServerError, | |
| Unauthorized, | |
| BadRequest, | |
| ThreadRollbackFailed, | |
| SandboxError, | |
| /// The response SSE stream disconnected in the middle of a turn before completion. | |
| ResponseStreamDisconnected { | |
| http_status_code: Option<u16>, | |
| }, | |
| /// Reached the retry limit for responses. | |
| ResponseTooManyFailedAttempts { | |
| http_status_code: Option<u16>, | |
| }, | |
| /// Returned when `turn/start` or `turn/steer` is submitted while the current active turn | |
| /// cannot accept same-turn steering, for example `/review` or manual `/compact`. | |
| ActiveTurnNotSteerable { | |
| turn_kind: NonSteerableTurnKind, | |
| }, | |
| Other, | |
| } | |
| impl From<CoreCodexErrorInfo> for CodexErrorInfo { | |
| fn from(value: CoreCodexErrorInfo) -> Self { | |
| match value { | |
| CoreCodexErrorInfo::ContextWindowExceeded => CodexErrorInfo::ContextWindowExceeded, | |
| CoreCodexErrorInfo::SessionBudgetExceeded => CodexErrorInfo::SessionBudgetExceeded, | |
| CoreCodexErrorInfo::UsageLimitExceeded => CodexErrorInfo::UsageLimitExceeded, | |
| CoreCodexErrorInfo::RateLimitExceeded => CodexErrorInfo::RateLimitExceeded, | |
| CoreCodexErrorInfo::ServerOverloaded => CodexErrorInfo::ServerOverloaded, | |
| CoreCodexErrorInfo::CyberPolicy => CodexErrorInfo::CyberPolicy, | |
| CoreCodexErrorInfo::MisalignmentPolicyViolation => { | |
| CodexErrorInfo::MisalignmentPolicyViolation | |
| } | |
| CoreCodexErrorInfo::HttpConnectionFailed { http_status_code } => { | |
| CodexErrorInfo::HttpConnectionFailed { http_status_code } | |
| } | |
| CoreCodexErrorInfo::ResponseStreamConnectionFailed { http_status_code } => { | |
| CodexErrorInfo::ResponseStreamConnectionFailed { http_status_code } | |
| } | |
| CoreCodexErrorInfo::InternalServerError => CodexErrorInfo::InternalServerError, | |
| CoreCodexErrorInfo::Unauthorized => CodexErrorInfo::Unauthorized, | |
| CoreCodexErrorInfo::BadRequest => CodexErrorInfo::BadRequest, | |
| CoreCodexErrorInfo::ThreadRollbackFailed => CodexErrorInfo::ThreadRollbackFailed, | |
| CoreCodexErrorInfo::SandboxError => CodexErrorInfo::SandboxError, | |
| CoreCodexErrorInfo::ResponseStreamDisconnected { http_status_code } => { | |
| CodexErrorInfo::ResponseStreamDisconnected { http_status_code } | |
| } | |
| CoreCodexErrorInfo::ResponseTooManyFailedAttempts { http_status_code } => { | |
| CodexErrorInfo::ResponseTooManyFailedAttempts { http_status_code } | |
| } | |
| CoreCodexErrorInfo::ActiveTurnNotSteerable { turn_kind } => { | |
| CodexErrorInfo::ActiveTurnNotSteerable { | |
| turn_kind: turn_kind.into(), | |
| } | |
| } | |
| CoreCodexErrorInfo::Other => CodexErrorInfo::Other, | |
| } | |
| } | |
| } | |
| impl From<CoreNonSteerableTurnKind> for NonSteerableTurnKind { | |
| fn from(value: CoreNonSteerableTurnKind) -> Self { | |
| match value { | |
| CoreNonSteerableTurnKind::Review => Self::Review, | |
| CoreNonSteerableTurnKind::Compact => Self::Compact, | |
| } | |
| } | |
| } | |
| pub enum AskForApproval { | |
| UnlessTrusted, | |
| OnRequest, | |
| Granular { | |
| sandbox_approval: bool, | |
| rules: bool, | |
| skill_approval: bool, | |
| request_permissions: bool, | |
| mcp_elicitations: bool, | |
| }, | |
| Never, | |
| } | |
| impl AskForApproval { | |
| pub fn to_core(self) -> CoreAskForApproval { | |
| match self { | |
| AskForApproval::UnlessTrusted => CoreAskForApproval::UnlessTrusted, | |
| AskForApproval::OnRequest => CoreAskForApproval::OnRequest, | |
| AskForApproval::Granular { | |
| sandbox_approval, | |
| rules, | |
| skill_approval, | |
| request_permissions, | |
| mcp_elicitations, | |
| } => CoreAskForApproval::Granular(CoreGranularApprovalConfig { | |
| sandbox_approval, | |
| rules, | |
| skill_approval, | |
| request_permissions, | |
| mcp_elicitations, | |
| }), | |
| AskForApproval::Never => CoreAskForApproval::Never, | |
| } | |
| } | |
| } | |
| impl From<CoreAskForApproval> for AskForApproval { | |
| fn from(value: CoreAskForApproval) -> Self { | |
| match value { | |
| CoreAskForApproval::UnlessTrusted => AskForApproval::UnlessTrusted, | |
| CoreAskForApproval::OnRequest => AskForApproval::OnRequest, | |
| CoreAskForApproval::Granular(granular_config) => AskForApproval::Granular { | |
| sandbox_approval: granular_config.sandbox_approval, | |
| rules: granular_config.rules, | |
| skill_approval: granular_config.skill_approval, | |
| request_permissions: granular_config.request_permissions, | |
| mcp_elicitations: granular_config.mcp_elicitations, | |
| }, | |
| CoreAskForApproval::Never => AskForApproval::Never, | |
| } | |
| } | |
| } | |
| /// Configures who approval requests are routed to for review. Examples | |
| /// include sandbox escapes, blocked network access, MCP approval prompts, and | |
| /// ARC escalations. Defaults to `user`. `auto_review` uses a carefully | |
| /// prompted subagent to gather relevant context and apply a risk-based | |
| /// decision framework before approving or denying the request. | |
| pub enum ApprovalsReviewer { | |
| User, | |
| AutoReview, | |
| } | |
| impl JsonSchema for ApprovalsReviewer { | |
| fn schema_name() -> String { | |
| "ApprovalsReviewer".to_string() | |
| } | |
| fn json_schema(_generator: &mut SchemaGenerator) -> Schema { | |
| string_enum_schema_with_description( | |
| &["user", "auto_review", "guardian_subagent"], | |
| "Configures who approval requests are routed to for review. Examples include sandbox escapes, blocked network access, MCP approval prompts, and ARC escalations. Defaults to `user`. `auto_review` uses a carefully prompted subagent to gather relevant context and apply a risk-based decision framework before approving or denying the request. The legacy value `guardian_subagent` is accepted for compatibility.", | |
| ) | |
| } | |
| } | |
| fn string_enum_schema_with_description(values: &[&str], description: &str) -> Schema { | |
| let mut schema = SchemaObject { | |
| instance_type: Some(InstanceType::String.into()), | |
| metadata: Some(Box::new(Metadata { | |
| description: Some(description.to_string()), | |
| ..Default::default() | |
| })), | |
| ..Default::default() | |
| }; | |
| schema.enum_values = Some( | |
| values | |
| .iter() | |
| .map(|value| JsonValue::String((*value).to_string())) | |
| .collect(), | |
| ); | |
| Schema::Object(schema) | |
| } | |
| impl ApprovalsReviewer { | |
| pub fn to_core(self) -> CoreApprovalsReviewer { | |
| match self { | |
| ApprovalsReviewer::User => CoreApprovalsReviewer::User, | |
| ApprovalsReviewer::AutoReview => CoreApprovalsReviewer::AutoReview, | |
| } | |
| } | |
| } | |
| impl From<CoreApprovalsReviewer> for ApprovalsReviewer { | |
| fn from(value: CoreApprovalsReviewer) -> Self { | |
| match value { | |
| CoreApprovalsReviewer::User => ApprovalsReviewer::User, | |
| CoreApprovalsReviewer::AutoReview => ApprovalsReviewer::AutoReview, | |
| } | |
| } | |
| } | |
| pub enum SandboxMode { | |
| ReadOnly, | |
| WorkspaceWrite, | |
| DangerFullAccess, | |
| } | |
| impl SandboxMode { | |
| pub fn to_core(self) -> CoreSandboxMode { | |
| match self { | |
| SandboxMode::ReadOnly => CoreSandboxMode::ReadOnly, | |
| SandboxMode::WorkspaceWrite => CoreSandboxMode::WorkspaceWrite, | |
| SandboxMode::DangerFullAccess => CoreSandboxMode::DangerFullAccess, | |
| } | |
| } | |
| } | |
| impl From<CoreSandboxMode> for SandboxMode { | |
| fn from(value: CoreSandboxMode) -> Self { | |
| match value { | |
| CoreSandboxMode::ReadOnly => SandboxMode::ReadOnly, | |
| CoreSandboxMode::WorkspaceWrite => SandboxMode::WorkspaceWrite, | |
| CoreSandboxMode::DangerFullAccess => SandboxMode::DangerFullAccess, | |
| } | |
| } | |
| } | |