Skip to main content

codex_api/requests/
headers.rs

1use codex_protocol::protocol::SessionSource;
2use http::HeaderMap;
3use http::HeaderValue;
4
5pub fn build_session_headers(session_id: Option<String>, thread_id: Option<String>) -> HeaderMap {
6    let mut headers = HeaderMap::new();
7    if let Some(id) = session_id {
8        insert_header(&mut headers, "session-id", &id);
9    }
10    if let Some(id) = thread_id {
11        insert_header(&mut headers, "thread-id", &id);
12    }
13    headers
14}
15
16pub(crate) fn subagent_header(source: &Option<SessionSource>) -> Option<String> {
17    let SessionSource::SubAgent(sub) = source.as_ref()? else {
18        return None;
19    };
20    match sub {
21        codex_protocol::protocol::SubAgentSource::Review => Some("review".to_string()),
22        codex_protocol::protocol::SubAgentSource::Compact => Some("compact".to_string()),
23        codex_protocol::protocol::SubAgentSource::MemoryConsolidation => {
24            Some("memory_consolidation".to_string())
25        }
26        codex_protocol::protocol::SubAgentSource::ThreadSpawn { .. } => {
27            Some("collab_spawn".to_string())
28        }
29        codex_protocol::protocol::SubAgentSource::Other(label) => Some(label.clone()),
30    }
31}
32
33pub(crate) fn insert_header(headers: &mut HeaderMap, name: &str, value: &str) {
34    if let (Ok(header_name), Ok(header_value)) = (
35        name.parse::<http::HeaderName>(),
36        HeaderValue::from_str(value),
37    ) {
38        headers.insert(header_name, header_value);
39    }
40}