// src/codex-cli-fallback.ts
var VALID_CODEX_CLI_REASONING_EFFORTS = /* @__PURE__ */ new Set([
  "low",
  "medium",
  "high",
  "xhigh"
]);
var processRunner;
function setCodexCliFallbackRunnerForProcess(runner) {
  const previous = processRunner;
  processRunner = runner;
  return () => {
    processRunner = previous;
  };
}
async function callCodexCliFallback(config, modelId, messages, options = {}) {
  if (!processRunner) {
    throw new Error(
      'codex-cli fallback transport is not registered; install a runner with setCodexCliFallbackRunnerForProcess() before using api: "codex-cli"'
    );
  }
  return await processRunner({
    config: normalizeCodexCliFallbackConfig(config),
    modelId: normalizeCodexCliModel(modelId),
    messages,
    options: normalizeCodexCliFallbackOptions(options)
  });
}
function normalizeCodexCliFallbackConfig(config) {
  return {
    ...config,
    ...config.executable !== void 0 ? { executable: normalizeOptionalString(config.executable, "codex-cli executable") } : {},
    ...config.codexCliExecutable !== void 0 ? { codexCliExecutable: normalizeOptionalString(config.codexCliExecutable, "codex-cli executable") } : {},
    ...config.reasoningEffort !== void 0 ? { reasoningEffort: normalizeCodexCliReasoningEffort(config.reasoningEffort) } : {},
    ...config.codexCliReasoningEffort !== void 0 ? { codexCliReasoningEffort: normalizeCodexCliReasoningEffort(config.codexCliReasoningEffort) } : {},
    ...config.retryOptions?.timeoutMs !== void 0 ? { retryOptions: { timeoutMs: normalizeCodexCliTimeoutMs(config.retryOptions.timeoutMs) } } : {}
  };
}
function normalizeCodexCliFallbackOptions(options) {
  return {
    ...options.timeoutMs !== void 0 ? { timeoutMs: normalizeCodexCliTimeoutMs(options.timeoutMs) } : {}
  };
}
function normalizeOptionalString(value, label) {
  if (typeof value !== "string" || value.trim().length === 0) {
    throw new Error(`${label} must be a non-empty string`);
  }
  return value.trim();
}
function normalizeCodexCliModel(value) {
  const trimmed = value.trim();
  if (trimmed.length === 0) {
    throw new Error("codex-cli model must be a non-empty string");
  }
  return trimmed;
}
function normalizeCodexCliReasoningEffort(value) {
  if (typeof value !== "string") {
    throw new Error("codex-cli reasoningEffort must be one of low, medium, high, xhigh");
  }
  const normalized = value.trim().toLowerCase();
  if (VALID_CODEX_CLI_REASONING_EFFORTS.has(normalized)) {
    return normalized;
  }
  throw new Error("codex-cli reasoningEffort must be one of low, medium, high, xhigh");
}
function normalizeCodexCliTimeoutMs(value) {
  const parsed = typeof value === "number" ? value : typeof value === "string" && value.trim().length > 0 ? Number(value) : NaN;
  if (!Number.isFinite(parsed) || !Number.isInteger(parsed) || parsed <= 0) {
    throw new Error("codex-cli timeoutMs must be a positive integer");
  }
  return parsed;
}
var __codexCliFallbackTestHooks = {
  setRunCodexCliForTest: setCodexCliFallbackRunnerForProcess
};

export {
  setCodexCliFallbackRunnerForProcess,
  callCodexCliFallback,
  __codexCliFallbackTestHooks
};
//# sourceMappingURL=chunk-KWBPHZUU.js.map