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::{ClaudeCodeRunner, CopilotRunner, CursorAgentRunner, OpenCodeRunner};
13
14/// Create an `LlmProvider` instance for the given runner type
15///
16/// Resolves the CLI binary via environment variable override or PATH lookup,
17/// then constructs the appropriate runner with default configuration.
18pub fn create_runner(runner_type: CliRunnerType) -> Result<Box<dyn LlmProvider>, RunnerError> {
19    let binary_name = runner_type.binary_name();
20    let env_key = runner_type.env_override_key();
21    let env_override = env::var(env_key).ok();
22
23    let binary_path = resolve_binary(binary_name, env_override.as_deref())?;
24    let config = RunnerConfig::new(binary_path);
25
26    let runner: Box<dyn LlmProvider> = match runner_type {
27        CliRunnerType::ClaudeCode => Box::new(ClaudeCodeRunner::new(config)),
28        CliRunnerType::Copilot => Box::new(CopilotRunner::new(config)),
29        CliRunnerType::CursorAgent => Box::new(CursorAgentRunner::new(config)),
30        CliRunnerType::OpenCode => Box::new(OpenCodeRunner::new(config)),
31    };
32
33    Ok(runner)
34}