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

912 B
Raw Blame
import { getApiKeyRequestScope, resolveEffectiveApiKeyId } from "./apiKeyScope";
import { createMediaSink } from "@/lib/media/mediaStore";
import { resolveMediaOrigin } from "@/server/origin/mediaOrigin";

/**
 * Media sink for one generation request (#16372): media is stored under the same owner the
 * files API would record for this caller (`resolveEffectiveApiKeyId`) — the API key, the shared
 * anonymous-owner sentinel under REQUIRE_API_KEY=false, or none for a dashboard session — and
 * served from the caller-facing origin. Injected into the open-sse video handlers.
 */
export async function createRequestMediaSink(
  request: Request,
  policyApiKeyInfo: { id: string } | null | undefined
) {
  const scope = await getApiKeyRequestScope(request);
  return createMediaSink({
    origin: resolveMediaOrigin(request),
    ownerId: resolveEffectiveApiKeyId(scope, policyApiKeyInfo ?? null),
  });
}