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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { assignProxyToScope, getProxyAssignments } from "@/lib/db/proxies";
import { resolveProxyForConnection } from "@/lib/db/settings";
import { proxyAssignmentSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { createErrorResponse, createErrorResponseFromUnknown } from "@/lib/api/errorResponse";
import { clearDispatcherCache } from "@omniroute/open-sse/utils/proxyDispatcher";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
export async function GET(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
try {
const { searchParams } = new URL(request.url);
const proxyId = searchParams.get("proxyId");
const scope = searchParams.get("scope");
const scopeId = searchParams.get("scopeId");
const resolveConnectionId = searchParams.get("resolveConnectionId");
if (resolveConnectionId) {
const resolved = await resolveProxyForConnection(resolveConnectionId);
return Response.json(resolved);
}
const assignments = await getProxyAssignments({
proxyId: proxyId || undefined,
scope: scope || undefined,
});
const filtered = scopeId
? assignments.filter((entry) => entry.scopeId === scopeId)
: assignments;
return Response.json({ items: filtered, total: filtered.length });
} catch (error) {
return createErrorResponseFromUnknown(error, "Failed to load proxy assignments");
}
}
export async function PUT(request: Request) {
const authError = await requireManagementAuth(request);
if (authError) return authError;
let rawBody: unknown;
try {
rawBody = await request.json();
} catch {
return createErrorResponse({
status: 400,
message: "Invalid JSON body",
type: "invalid_request",
});
}
try {
const validation = validateBody(proxyAssignmentSchema, rawBody);
if (isValidationFailure(validation)) {
return createErrorResponse({
status: 400,
message: validation.error.message,
details: validation.error.details,
type: "invalid_request",
});
}
const { scope, scopeId, proxyId } = validation.data;
const assigned = await assignProxyToScope(scope, scopeId || null, proxyId || null);
clearDispatcherCache();
return Response.json({ success: true, assignment: assigned });
} catch (error) {
return createErrorResponseFromUnknown(error, "Failed to update assignment");
}
}