Skip to main content

embacle_server/runner/
factory.rs

1// ABOUTME: Factory for creating embacle LlmProvider instances from runner type identifiers
2// ABOUTME: Resolves binary paths and constructs the appropriate runner with default config
3//
4// SPDX-License-Identifier: Apache-2.0
5// Copyright (c) 2026 dravr.ai
6
7use std::env;
8
9use embacle::config::{CliRunnerType, RunnerConfig};
10use embacle::discovery::resolve_binary;
11use embacle::types::{LlmProvider, RunnerError};
12use embacle::{
13    ClaudeCodeRunner, CodexCliRunner, CopilotRunner, CursorAgentRunner, GeminiCliRunner,
14    OpenCodeRunner,
15};
16
17/// Create an `LlmProvider` instance for the given runner type
18///
19/// Resolves the CLI binary via environment variable override or PATH lookup,
20/// then constructs the appropriate runner with default configuration.
21pub fn create_runner(runner_type: CliRunnerType) -> Result<Box<dyn LlmProvider>, RunnerError> {
22    let binary_name = runner_type.binary_name();
23    let env_key = runner_type.env_override_key();
24    let env_override = env::var(env_key).ok();
25
26    let binary_path = resolve_binary(binary_name, env_override.as_deref())?;
27    let config = RunnerConfig::new(binary_path);
28
29    let runner: Box<dyn LlmProvider> = match runner_type {
30        CliRunnerType::ClaudeCode => Box::new(ClaudeCodeRunner::new(config)),
31        CliRunnerType::Copilot => Box::new(CopilotRunner::new(config)),
32        CliRunnerType::CursorAgent => Box::new(CursorAgentRunner::new(config)),
33        CliRunnerType::OpenCode => Box::new(OpenCodeRunner::new(config)),
34        CliRunnerType::GeminiCli => Box::new(GeminiCliRunner::new(config)),
35        CliRunnerType::CodexCli => Box::new(CodexCliRunner::new(config)),
36    };
37
38    Ok(runner)
39}