agcodex_core/
embeddings_capability.rs

1//! Capability checks for embeddings providers.
2//! Per policy: OpenAI embeddings require an OpenAI API key. ChatGPT login
3//! (web tokens) is not sufficient and does not grant embeddings access.
4
5use crate::config::Config;
6use agcodex_login::OPENAI_API_KEY_ENV_VAR;
7use agcodex_login::get_auth_file;
8use agcodex_login::try_read_auth_json;
9
10/// Returns true if OpenAI embeddings can be used in the current environment.
11///
12/// Rules:
13/// - True when `OPENAI_API_KEY` is available via env or auth.json.
14/// - ChatGPT web login tokens do NOT enable OpenAI embeddings.
15pub fn can_use_openai_embeddings(config: &Config) -> bool {
16    // 1) Check environment variable directly
17    if std::env::var(OPENAI_API_KEY_ENV_VAR)
18        .map(|v| !v.trim().is_empty())
19        .unwrap_or(false)
20    {
21        return true;
22    }
23
24    // 2) Check auth.json stored API key
25    let auth_file = get_auth_file(&config.codex_home);
26    if let Ok(auth) = try_read_auth_json(&auth_file)
27        && auth
28            .openai_api_key
29            .as_deref()
30            .map(|s| !s.trim().is_empty())
31            .unwrap_or(false)
32    {
33        return true;
34    }
35
36    false
37}
38
39/// Optional helper to explain why embeddings are unavailable.
40pub fn openai_embeddings_unavailable_reason(config: &Config) -> Option<String> {
41    if can_use_openai_embeddings(config) {
42        return None;
43    }
44    Some("OpenAI embeddings require an OPENAI_API_KEY. ChatGPT login tokens do not grant API access.".to_string())
45}