//! Prepare an ordinary cloud coding thread with server-owned managed environment defaults. use crate::Client; use crate::Error; use bytes::Bytes; use bytes::BytesMut; use prost::Message; /// A local project's GitHub origins to provision in the default cloud environment. /// Callers must supply credential-free origin URLs; this client does not inspect local Git state. #[derive(Clone)] pub struct ProjectEnvironment { pub name: String, pub origin_urls: Vec, /// Working directory inside the managed environment, when a project selects one. pub cwd: Option, } /// An explicit saved environment or the default environment with a project checkout. #[derive(Clone, Debug)] pub enum EnvironmentSelection { Saved(String), Project(ProjectEnvironment), } pub(super) const DEFAULT_ENVIRONMENT_CONFIG_ID: &str = "asenvcfg_base"; impl std::fmt::Debug for ProjectEnvironment { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("ProjectEnvironment([redacted])") } } impl Client { /// Prewarm an ordinary coding thread in the default managed environment. /// The service makes it discoverable when it admits the first user message. /// This mutation is never retried. A timeout or disconnect can leave a thread /// prepared even if its identity and settings were not received. The caller /// must not use this ordinary-thread API for an ephemeral session. pub async fn prewarm_default_thread( &self, model: Option, model_provider: Option, ) -> Result { self.prewarm_thread(model, model_provider, /*environment*/ None) .await } /// Prewarm a coding thread, deferring executor readiness for an explicit environment. pub async fn prewarm_thread( &self, model: Option, model_provider: Option, environment: Option, ) -> Result { let deferred_environment = environment.is_some().then_some(true); let managed = match environment { Some(EnvironmentSelection::Saved(id)) => wire::ManagedEnvironment { environment_config_id: Some(id), ..Default::default() }, Some(EnvironmentSelection::Project(ProjectEnvironment { name, origin_urls, cwd, })) => wire::ManagedEnvironment { environment_config_id: Some(DEFAULT_ENVIRONMENT_CONFIG_ID.to_owned()), cwd, overrides: Some(wire::EnvironmentOverrides { repositories: Some(wire::RepositoryOverrides { values: origin_urls .into_iter() .map(|origin_url| wire::RepositoryOverride { origin_url: Some(origin_url), }) .collect(), }), name: Some(name), }), }, None => wire::ManagedEnvironment::default(), }; let response = self .raw_unary( wire::PrewarmRequest { model, model_provider, plugins_mcp: Some(wire::PluginsMcp { product_sku: Some("codex".to_owned()), }), deferred_environment, environments: Some(wire::EnvironmentList { values: vec![wire::EnvironmentSelection { managed: Some(managed), }], }), }, "/oaiproto.codex_app_server_backend.ThreadService/Prewarm", ) .await?; let warm = wire::PrewarmResponse::decode(response).map_err(|_| Error::InvalidResponse)?; // PrewarmThread omits turns, but the native Thread requires an empty // ThreadTurns message. Prefix it so the original wire bytes, including // unknown fields, are preserved and malformed trailing data stays invalid. let mut thread = BytesMut::new(); prost::encoding::bytes::encode(/* tag */ 31, &Bytes::new(), &mut thread); thread.extend_from_slice(&warm.thread.ok_or(Error::InvalidResponse)?); crate::ThreadActivation::from_settings(crate::activation_wire::UpdateConfigResponse { active_permission_profile: warm.active_permission_profile, approval_policy: warm.approval_policy, approvals_reviewer: warm.approvals_reviewer, cwd: warm.cwd, model: warm.model, model_provider: warm.model_provider, reasoning_effort: warm.reasoning_effort, runtime_workspace_roots: warm.runtime_workspace_roots, sandbox: warm.sandbox, service_tier: warm.service_tier, thread: Some(thread.freeze()), ..Default::default() }) } } mod wire { use super::*; #[derive(Clone, Message)] pub(super) struct PrewarmRequest { #[prost(message, optional, tag = "10")] pub environments: Option, #[prost(string, optional, tag = "15")] pub model: Option, #[prost(string, optional, tag = "16")] pub model_provider: Option, #[prost(message, optional, tag = "28")] pub plugins_mcp: Option, #[prost(bool, optional, tag = "29")] pub deferred_environment: Option, } #[derive(Clone, Message)] pub(super) struct PluginsMcp { #[prost(string, optional, tag = "1")] pub product_sku: Option, } #[derive(Clone, Message)] pub(super) struct EnvironmentList { #[prost(message, repeated, tag = "1")] pub values: Vec, } #[derive(Clone, Message)] pub(super) struct EnvironmentSelection { #[prost(message, optional, tag = "1")] pub managed: Option, } #[derive(Clone, Message)] pub(super) struct ManagedEnvironment { #[prost(string, optional, tag = "1")] pub environment_config_id: Option, #[prost(string, optional, tag = "2")] pub cwd: Option, #[prost(message, optional, tag = "3")] pub overrides: Option, } #[derive(Clone, Message)] pub(super) struct EnvironmentOverrides { #[prost(message, optional, tag = "1")] pub repositories: Option, #[prost(string, optional, tag = "2")] pub name: Option, } #[derive(Clone, Message)] pub(super) struct RepositoryOverrides { #[prost(message, repeated, tag = "1")] pub values: Vec, } #[derive(Clone, Message)] pub(super) struct RepositoryOverride { #[prost(string, optional, tag = "5")] pub origin_url: Option, } #[derive(Clone, Message)] pub(super) struct PrewarmResponse { #[prost(message, optional, tag = "1")] pub active_permission_profile: Option, #[prost(message, optional, tag = "2")] pub approval_policy: Option, #[prost(int32, optional, tag = "3")] pub approvals_reviewer: Option, #[prost(string, optional, tag = "4")] pub cwd: Option, #[prost(string, optional, tag = "7")] pub model: Option, #[prost(string, optional, tag = "8")] pub model_provider: Option, #[prost(string, optional, tag = "10")] pub reasoning_effort: Option, #[prost(message, optional, tag = "11")] pub runtime_workspace_roots: Option, #[prost(message, optional, tag = "12")] pub sandbox: Option, #[prost(message, optional, tag = "13")] pub service_tier: Option, #[prost(bytes = "bytes", optional, tag = "14")] pub thread: Option, } }