Skip to main content

sqlite_graphrag/
backend_choice.rs

1//! LLM / embedding backend CLI choices (Wave C1).
2
3/// v1.0.82 (GAP-003): LLM backend for embedding. Accepts `auto` (default —
4/// detects `codex` or `claude` on the PATH), `codex` (forces codex exec), `claude`
5/// (forces claude -p), or `none` (skips embedding; useful for tests).
6#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
7pub enum LlmBackendChoice {
8    /// Auto variant.
9    Auto,
10    /// Claude variant.
11    Claude,
12    /// Codex variant.
13    Codex,
14    /// Opencode variant.
15    Opencode,
16    /// Open router variant.
17    OpenRouter,
18    /// None variant.
19    None,
20}
21
22/// v1.0.93: embedding backend selector. Separate from `--llm-backend` which
23/// controls enrichment (entity extraction, body enrichment) via subprocess.
24/// `auto` tries OpenRouter if API key is available, falls back to LLM subprocess.
25/// `openrouter` requires API key (exit 78 if absent).
26/// `llm` forces subprocess (codex/claude/opencode) — legacy behaviour.
27#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
28pub enum EmbeddingBackendChoice {
29    /// Auto variant.
30    Auto,
31    /// Openrouter variant.
32    Openrouter,
33    /// LLM variant.
34    Llm,
35}
36
37impl EmbeddingBackendChoice {
38    /// v1.0.93: produces a fallback chain that prepends OpenRouter when
39    /// the client is initialised. Falls back to the LLM subprocess chain.
40    pub fn to_chain(self, llm_choice: LlmBackendChoice) -> Vec<crate::embedder::LlmBackendKind> {
41        use crate::embedder::LlmBackendKind;
42        match self {
43            EmbeddingBackendChoice::Openrouter => vec![LlmBackendKind::OpenRouter],
44            EmbeddingBackendChoice::Llm => llm_choice.to_chain(),
45            EmbeddingBackendChoice::Auto => {
46                if crate::embedder::is_openrouter_initialized() {
47                    let mut chain = vec![LlmBackendKind::OpenRouter];
48                    chain.extend(llm_choice.to_chain());
49                    chain
50                } else {
51                    llm_choice.to_chain()
52                }
53            }
54        }
55    }
56}
57
58impl LlmBackendChoice {
59    /// v1.0.82 (GAP-003): converts the CLI choice into an ordered chain
60    /// of backends that `embedder::embed_with_fallback` iterates. The first
61    /// element of the chain is the preferred backend; subsequent elements
62    /// are fallbacks used when the preferred one fails with `LlmBackendError`.
63    ///
64    /// `Auto` produces `[Codex, Claude, None]` — codex is the default since v1.0.76+,
65    /// claude is the fallback if codex fails (OAuth contention, quota), and
66    /// `None` lets `embed_with_fallback` return an empty vector when
67    /// `skip_on_failure` is active.
68    pub fn to_chain(self) -> Vec<crate::embedder::LlmBackendKind> {
69        use crate::embedder::LlmBackendKind;
70        match self {
71            LlmBackendChoice::Codex => vec![LlmBackendKind::Codex, LlmBackendKind::None],
72            LlmBackendChoice::Claude => vec![LlmBackendKind::Claude, LlmBackendKind::None],
73            LlmBackendChoice::Opencode => vec![
74                LlmBackendKind::Opencode,
75                LlmBackendKind::Codex,
76                LlmBackendKind::Claude,
77                LlmBackendKind::None,
78            ],
79            LlmBackendChoice::OpenRouter => vec![
80                LlmBackendKind::OpenRouter,
81                LlmBackendKind::Codex,
82                LlmBackendKind::None,
83            ],
84            LlmBackendChoice::None => vec![LlmBackendKind::None],
85            LlmBackendChoice::Auto => {
86                parse_fallback_chain(&crate::runtime_config::llm_fallback("codex,claude,none"))
87            }
88        }
89    }
90}
91
92fn parse_fallback_chain(s: &str) -> Vec<crate::embedder::LlmBackendKind> {
93    use crate::embedder::LlmBackendKind;
94    let mut chain: Vec<LlmBackendKind> = s
95        .split(',')
96        .filter_map(|tok| match tok.trim().to_ascii_lowercase().as_str() {
97            "codex" => Some(LlmBackendKind::Codex),
98            "claude" | "claude-code" => Some(LlmBackendKind::Claude),
99            "opencode" => Some(LlmBackendKind::Opencode),
100            "openrouter" => Some(LlmBackendKind::OpenRouter),
101            "none" => Some(LlmBackendKind::None),
102            _ => {
103                tracing::warn!(
104                    token = tok.trim(),
105                    "unknown backend in --llm-fallback, skipping"
106                );
107                Option::None
108            }
109        })
110        .collect();
111    if chain.is_empty() {
112        chain = vec![
113            LlmBackendKind::Codex,
114            LlmBackendKind::Claude,
115            LlmBackendKind::None,
116        ];
117    }
118    chain
119}