Skip to main content

mockql_core/llm_provider/cli/
mod.rs

1// Copyright 2026 Expedia, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! CLI-backed provider client implementations.
16
17mod claude;
18mod codex;
19mod opencode;
20
21use crate::GraphQLResponse;
22use crate::graphql::mock_response_prompt::MockResponsePrompt;
23use crate::llm_provider::ProviderError;
24pub use claude::ClaudeCliProvider;
25pub use codex::CodexCliProvider;
26pub use opencode::OpenCodeCliProvider;
27use std::env;
28use std::process::Output;
29use std::time::Duration;
30use tokio::process::Child;
31use tokio::time::timeout;
32
33/// Supported local CLI providers.
34#[derive(Debug, Clone)]
35pub enum CliProvider {
36  /// Anthropic Claude CLI.
37  Claude(ClaudeCliProvider),
38  /// OpenAI Codex CLI.
39  Codex(CodexCliProvider),
40  /// Anomaly OpenCode CLI.
41  OpenCode(OpenCodeCliProvider),
42}
43
44impl CliProvider {
45  pub(crate) async fn run(
46    &self,
47    prompt: &MockResponsePrompt,
48    timeout: Duration,
49  ) -> Result<GraphQLResponse, ProviderError> {
50    if env::var("MOCKQL_DEBUG").is_ok()
51      && let Ok(markdown) = prompt.to_markdown()
52    {
53      let _ = std::fs::write("prompt.dbg.txt", markdown);
54    }
55    match self {
56      Self::Claude(claude) => claude.run(prompt, timeout).await,
57      Self::Codex(codex) => codex.run(prompt, timeout).await,
58      Self::OpenCode(opencode) => opencode.run(prompt, timeout).await,
59    }
60  }
61}
62
63/// Waits for a spawned CLI child process with a timeout and checks its exit status.
64pub(crate) async fn exec_cli(timeout_duration: Duration, child: Child) -> Result<Output, ProviderError> {
65  let output = timeout(timeout_duration, child.wait_with_output())
66    .await
67    .map_err(|_| ProviderError::Request("CLI command timed out".to_string()))?
68    .map_err(|error| ProviderError::Request(format!("CLI command failed while waiting for output: {error}")))?;
69
70  if !output.status.success() {
71    let stdout = String::from_utf8_lossy(&output.stdout);
72    let stderr = String::from_utf8_lossy(&output.stderr);
73    let details = if stderr.trim().is_empty() {
74      stdout.trim()
75    } else {
76      stderr.trim()
77    };
78    return Err(ProviderError::Request(if details.is_empty() {
79      format!("CLI command exited with status {}", output.status)
80    } else {
81      format!("CLI command exited with status {}: {details}", output.status)
82    }));
83  }
84
85  Ok(output)
86}