Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.3 KB
Raw Blame
import { NextResponse } from "next/server";
import { ensureCliConfigWriteAllowed } from "@/shared/services/cliRuntime";
import { isContainerWriteRefusal } from "@/shared/utils/containerConfigGuard";

/**
 * Shared gate for API routes that write a host CLI's config file.
 *
 * Returns `null` when the write may proceed, otherwise the response to send:
 *   - 422 + `containerEphemeralTarget` when OmniRoute runs in a container and
 *     the target is not bind-mounted from the host (the write would vanish),
 *   - 403 when CLI config writes are switched off entirely.
 *
 * Clients key off `containerEphemeralTarget` to render the host-CLI guidance
 * inline, the same way the Zed import card handles its Docker 422.
 */
export function guardCliConfigWrite(
  targetPath: string,
  options: { toolLabel?: string; hostCommand?: string } = {}
): NextResponse | null {
  const writeError = ensureCliConfigWriteAllowed(targetPath, options);
  if (!writeError) return null;

  const containerEphemeralTarget = isContainerWriteRefusal(writeError);
  return NextResponse.json(
    {
      error: writeError,
      ...(containerEphemeralTarget
        ? { containerEphemeralTarget, hostSetupCommand: options.hostCommand }
        : {}),
    },
    { status: containerEphemeralTarget ? 422 : 403 }
  );
}