// src/backends/opencode/mcp.ts
function mcpConfigToOpencode(mcpConfig) {
  const mcp = {};
  for (const [name, server] of Object.entries(mcpConfig)) {
    const entry = {
      url: server.url,
      headers: server.headers,
      env: server.env
    };
    if (server.type === "stdio") {
      entry.type = "local";
      if (typeof server.command === "string") {
        entry.command = [server.command, ...server.args || []];
      } else if (Array.isArray(server.command)) {
        entry.command = [...server.command, ...server.args || []];
      }
    } else if (server.type === "http" || server.type === "sse") {
      entry.type = "remote";
    }
    mcp[name] = entry;
  }
  return mcp;
}
function isOllamaModel(model) {
  if (model.includes("/")) return false;
  const cloudPrefixes = ["claude-", "gpt-", "o1-", "o3-", "o4-", "codex-", "chatgpt-", "gemini-"];
  return !cloudPrefixes.some((p) => model.startsWith(p));
}
function buildOpencodeConfigEnv(agent, ollamaBaseUrl) {
  if (ollamaBaseUrl === void 0 && isOllamaModel(agent.model.id)) {
    ollamaBaseUrl = "http://localhost:11434/v1";
  }
  const config = {};
  if (agent.mcp_servers && agent.mcp_servers.length > 0) {
    const mcpRecord = {};
    for (const s of agent.mcp_servers) {
      const { name, ...rest } = s;
      mcpRecord[name] = rest;
    }
    config.mcp = mcpConfigToOpencode(mcpRecord);
  }
  if (isOllamaModel(agent.model.id) && ollamaBaseUrl) {
    config.provider = {
      ollama: {
        npm: "@ai-sdk/openai-compatible",
        name: "Ollama (local)",
        options: { baseURL: ollamaBaseUrl },
        models: {
          [agent.model.id]: { name: agent.model.id }
        }
      }
    };
  }
  if (Object.keys(config).length === 0) return {};
  return { OPENCODE_CONFIG_CONTENT: JSON.stringify(config) };
}
var buildOpencodeMcpEnv = (agent) => buildOpencodeConfigEnv(agent);

export {
  mcpConfigToOpencode,
  buildOpencodeConfigEnv,
  buildOpencodeMcpEnv
};
