Make Qwen OAuth work on Vercel
This commit is contained in:
57
app/api/ollama/chat/route.ts
Normal file
57
app/api/ollama/chat/route.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { normalizeOllamaBase, DEFAULT_OLLAMA_BASE } from "../constants";
|
||||
|
||||
const API_PREFIX = "/api";
|
||||
|
||||
function getApiKey(request: NextRequest): string | null {
|
||||
return request.headers.get("x-ollama-api-key");
|
||||
}
|
||||
|
||||
function getBaseUrl(request: NextRequest): string {
|
||||
const header = request.headers.get("x-ollama-endpoint");
|
||||
if (header && header.trim().length > 0) {
|
||||
return normalizeOllamaBase(header);
|
||||
}
|
||||
return DEFAULT_OLLAMA_BASE;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const apiKey = getApiKey(request);
|
||||
if (!apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: "Ollama API key is required" },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const baseUrl = getBaseUrl(request);
|
||||
const targetUrl = `${baseUrl}${API_PREFIX}/chat`;
|
||||
|
||||
try {
|
||||
const response = await fetch(targetUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const payload = await response.text();
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Ollama chat request failed", details: payload },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(payload ? JSON.parse(payload) : {});
|
||||
} catch (error) {
|
||||
console.error("Ollama chat proxy failed", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Ollama chat request failed" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
7
app/api/ollama/constants.ts
Normal file
7
app/api/ollama/constants.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const DEFAULT_OLLAMA_BASE = process.env.NEXT_PUBLIC_OLLAMA_ENDPOINT || process.env.OLLAMA_ENDPOINT || "https://ollama.com";
|
||||
export function normalizeOllamaBase(url?: string): string {
|
||||
if (!url) return DEFAULT_OLLAMA_BASE.replace(/\/$/, "");
|
||||
const trimmed = url.trim();
|
||||
if (!trimmed) return DEFAULT_OLLAMA_BASE.replace(/\/$/, "");
|
||||
return trimmed.replace(/\/$/, "");
|
||||
}
|
||||
88
app/api/ollama/models/route.ts
Normal file
88
app/api/ollama/models/route.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { normalizeOllamaBase, DEFAULT_OLLAMA_BASE } from "../constants";
|
||||
|
||||
const API_PREFIX = "/api";
|
||||
|
||||
function getApiKey(request: NextRequest): string | null {
|
||||
return request.headers.get("x-ollama-api-key");
|
||||
}
|
||||
|
||||
function getBaseUrl(request: NextRequest): string {
|
||||
const header = request.headers.get("x-ollama-endpoint");
|
||||
if (header && header.trim().length > 0) {
|
||||
return normalizeOllamaBase(header);
|
||||
}
|
||||
return DEFAULT_OLLAMA_BASE;
|
||||
}
|
||||
|
||||
async function fetchModelNames(url: string, apiKey: string): Promise<string[]> {
|
||||
const response = await fetch(`${url}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text().catch(() => "Failed to parse");
|
||||
throw new Error(`${response.status} ${response.statusText} - ${errorText}`);
|
||||
}
|
||||
|
||||
const json = await response.json().catch(() => null);
|
||||
const candidates = Array.isArray(json?.models)
|
||||
? json.models
|
||||
: Array.isArray(json?.data)
|
||||
? json.data
|
||||
: Array.isArray(json)
|
||||
? json
|
||||
: [];
|
||||
|
||||
const names: string[] = [];
|
||||
for (const entry of candidates) {
|
||||
if (!entry) continue;
|
||||
const name = entry.name || entry.model || entry.id;
|
||||
if (typeof name === "string" && name.length > 0) {
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const apiKey = getApiKey(request);
|
||||
if (!apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: "Ollama API key is required" },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const baseUrl = getBaseUrl(request);
|
||||
const primaryUrl = `${baseUrl}${API_PREFIX}/v1/models`;
|
||||
const fallbackUrl = `${baseUrl}${API_PREFIX}/tags`;
|
||||
|
||||
try {
|
||||
const primaryModels = await fetchModelNames(primaryUrl, apiKey);
|
||||
if (primaryModels.length > 0) {
|
||||
return NextResponse.json({ models: primaryModels });
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Ollama] Primary model fetch failed:", error);
|
||||
}
|
||||
|
||||
try {
|
||||
const fallbackModels = await fetchModelNames(fallbackUrl, apiKey);
|
||||
if (fallbackModels.length > 0) {
|
||||
return NextResponse.json({ models: fallbackModels });
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[Ollama] Fallback model fetch failed:", error);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ models: [] },
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user