Skip to main content

sqlite_graphrag/
backend_choice.rs

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