Skip to main content

codex_convert_proxy/proxy/
core.rs

1//! Codex proxy core structures.
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use crate::config::BackendRouter;
7use crate::providers::Provider;
8use crate::proxy::context_store::{ConversationSnapshot, ConversationStore};
9
10/// Codex proxy handler implementing ProxyHttp trait.
11pub struct CodexProxy {
12    /// Backend router for multi-backend routing.
13    pub router: Arc<BackendRouter>,
14    /// Providers for each backend (shared, stateless).
15    pub providers: HashMap<String, Arc<dyn Provider>>,
16    /// Whether to log request/response bodies.
17    pub log_body: bool,
18    /// Directory for debug log files.
19    pub log_dir: std::path::PathBuf,
20    /// In-memory conversation store for previous_response_id expansion.
21    pub conversation_store: Arc<ConversationStore>,
22}
23
24impl CodexProxy {
25    /// Create a new CodexProxy instance.
26    pub fn new(
27        router: Arc<BackendRouter>,
28        providers: HashMap<String, Arc<dyn Provider>>,
29        log_body: bool,
30        log_dir: std::path::PathBuf,
31    ) -> Self {
32        Self {
33            router,
34            providers,
35            log_body,
36            log_dir,
37            conversation_store: Arc::new(ConversationStore::new()),
38        }
39    }
40
41    /// Get a shared handle to the provider for a backend.
42    pub fn get_provider(&self, name: &str) -> Option<Arc<dyn Provider>> {
43        self.providers.get(name).map(Arc::clone)
44    }
45
46    /// Lookup conversation snapshot by response id.
47    pub fn get_conversation(&self, response_id: &str) -> Option<ConversationSnapshot> {
48        self.conversation_store.get(response_id)
49    }
50
51    /// Persist conversation snapshot for follow-up turns.
52    pub fn store_conversation(&self, response_id: String, snapshot: ConversationSnapshot) {
53        self.conversation_store.insert(response_id, snapshot);
54    }
55}