import { IntelligenceBackend, BackendConfigSection, BackendUnavailableError } from "./types.js"; import { OpencodeBackend } from "./opencode.js"; import { OpenAIBackend } from "./openai.js"; import { OllamaLocalBackend } from "./ollama-local.js"; import { OllamaCloudBackend } from "./ollama-cloud.js"; import { AnthropicBackend } from "./anthropic.js"; const AUTO_DETECT_ORDER: Array<"opencode" | "openai" | "ollama-local" | "ollama-cloud" | "anthropic"> = [ "opencode", "openai", "ollama-local", "ollama-cloud", "anthropic", ]; export function createBackend( name: string, config: BackendConfigSection ): IntelligenceBackend { switch (name) { case "opencode": return new OpencodeBackend(config.agent_backends.opencode); case "openai": if (!config.llm_backends["openai"]) { throw new BackendUnavailableError("openai"); } return new OpenAIBackend(config.llm_backends["openai"]); case "ollama-local": return new OllamaLocalBackend(config.llm_backends["ollama-local"]); case "ollama-cloud": return new OllamaCloudBackend(config.llm_backends["ollama-cloud"]); case "anthropic": if (!config.llm_backends["anthropic"]) { throw new BackendUnavailableError("anthropic"); } return new AnthropicBackend(config.llm_backends["anthropic"]); default: throw new BackendUnavailableError(name); } } export async function resolveBackend( config: BackendConfigSection ): Promise { if (config.provider !== "auto") { const backend = createBackend(config.provider, config); if (!(await backend.isAvailable())) { throw new BackendUnavailableError(config.provider); } return backend; } for (const name of AUTO_DETECT_ORDER) { try { const backend = createBackend(name, config); if (await backend.isAvailable()) { return backend; } } catch {} } throw new BackendUnavailableError("auto"); } export { IntelligenceBackend, BackendConfigSection, BackendUnavailableError } from "./types.js"; export { LLMBaseBackend, ChatMessage, ChatCompletionResponse } from "./llm-base.js"; export { ToolRegistry, ToolDefinition, ToolCall, ToolResult } from "./tool-registry.js"; export { OpencodeBackend } from "./opencode.js"; export { OpenAIBackend } from "./openai.js"; export { OllamaLocalBackend } from "./ollama-local.js"; export { OllamaCloudBackend } from "./ollama-cloud.js"; export { AnthropicBackend } from "./anthropic.js";