Fix AI Gateway CORS by proxying through Vite dev server

Direct browser fetch to https://ai-gateway.vercel.sh/v1 is blocked
(No Access-Control-Allow-Origin). In dev the AI SDK now calls
/__llm/v1 via same-origin Vite middleware (llmGatewayProxy), which
forwards server-side to the real gateway — no CORS. Production still
calls the gateway directly (deployments needing CORS-free prod should
add a serverless /api/llm proxy).

- vite.config.js: new llmGatewayProxy middleware for /__llm/* —
  handles OPTIONS preflight, forwards via Node fetch to
  x-llm-gateway (fallback Vercel), mirrors status/headers +
  access-control-allow-origin, preserves query/body.
- src/llm/llmTweak.js: in DEV use baseURL /__llm/v1 + proxyFetch
  that injects x-llm-gateway; prod keeps cfg.gatewayUrl.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Dejvino
2026-09-13 20:25:19 +02:00
co-authored by Claude
parent 904ae09486
commit a0eaf2ef95
2 changed files with 109 additions and 2 deletions
+11 -1
View File
@@ -87,7 +87,17 @@ export async function requestSongTweak({ spec, track, look, hint = '', config =
const messages = buildTweakPrompt({ spec, track, look, hint });
recordInvocation();
const provider = createOpenAI({ baseURL: cfg.gatewayUrl, apiKey: cfg.apiKey });
const isDev = import.meta.env && import.meta.env.DEV;
const proxyFetch = async (url, init = {}) => {
const headers = new Headers(init.headers || {});
if (isDev) headers.set('x-llm-gateway', cfg.gatewayUrl);
return fetch(url, { ...init, headers });
};
const provider = createOpenAI({
baseURL: isDev ? '/__llm/v1' : cfg.gatewayUrl,
apiKey: cfg.apiKey,
fetch: isDev ? proxyFetch : undefined,
});
const modelId = cfg.model;
try {