Bring-your-own keys for cloud providers (OpenAI-compatible)
Settings -> Providers takes keys for OpenAI, Anthropic (their official OpenAI-compatibility layer), DeepSeek, Groq, OpenRouter and any custom OpenAI-compatible endpoint. One adapter (lib/openai-compat) covers listing, streaming chat, JSON generation and vision, and every model flow can run on a provider: the chat picker lists each enabled provider's models (labelled, remembered), and a "Study generation" choice moves decks, homework, grading and attachment transcription onto a provider model. Keys stay in this browser exactly like the Ollama one (freezmo.providers.v1): never stored server-side, never in a backup or the sync payload, forwarded only with the request that needs them. The provider test asserts the key reaches the provider while the routing header stays on the app side. New suite tests/providers-test.mjs (13 checks) stubs both protocols: model listing, chat streaming, study generation and the Settings flow. Privacy policy updated (en+cs). Contrast 0 findings, axe clean, mobile 126/126, chat 102/102, wizard 95/95, bubbles 34/34, regression 44/44, i18n 13/13, native 33/33. Version 0.4.7, SW -> freezmo-v72.
This commit is contained in:
parent
37f7d22041
commit
4372423167
28 changed files with 1538 additions and 150 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { extractNotes, notesToText } from "@/lib/ai";
|
||||
import { authErrorMessage, authErrorKind, getServerConfig, resolveAuth } from "@/lib/config";
|
||||
import { getServerConfig } from "@/lib/config";
|
||||
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
|
@ -18,6 +19,8 @@ const MAX_BODY_BYTES = 33554432;
|
|||
const MAX_IMAGE_CHARS = 8_000_000;
|
||||
|
||||
const requestSchema = z.object({
|
||||
|
||||
model: z.string().min(1).max(200).optional(),
|
||||
images: z.array(dataUrlSchema(MAX_IMAGE_CHARS)).max(16).optional(),
|
||||
text: z.string().max(120_000).optional(),
|
||||
});
|
||||
|
|
@ -39,13 +42,14 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`chat:${clientKey(request)}`, config.rateLimits.chat, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
const provider = target.kind === "provider" ? target.target : undefined;
|
||||
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -68,6 +72,17 @@ export async function POST(request: Request) {
|
|||
return Response.json({ error: translate(locale, "server.needContent") }, { status: 400 });
|
||||
}
|
||||
|
||||
// Provider runs carry the model the user picked in Settings → Providers.
|
||||
const providerModel = provider ? parsed.data.model : undefined;
|
||||
if (provider && !providerModel) {
|
||||
return Response.json(
|
||||
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const textModel = provider ? providerModel! : config.textModel;
|
||||
const visionModel = provider ? providerModel! : config.visionModel;
|
||||
|
||||
const gate = acquireModelGate(request, config, locale);
|
||||
if ("response" in gate) return gate.response;
|
||||
const abort = new AbortController();
|
||||
|
|
@ -78,9 +93,10 @@ export async function POST(request: Request) {
|
|||
return Response.json({ text: text.slice(0, 48_000) });
|
||||
}
|
||||
const extraction = await extractNotes({
|
||||
model: config.visionModel,
|
||||
repairModel: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: visionModel,
|
||||
repairModel: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
images,
|
||||
text: text || undefined,
|
||||
signal: abort.signal,
|
||||
|
|
@ -94,7 +110,7 @@ export async function POST(request: Request) {
|
|||
console.error("[api/chat/extract]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
return Response.json(
|
||||
{
|
||||
error: friendlyErrorMessage(error, auth.auth.baseUrl, locale),
|
||||
error: friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale),
|
||||
kind: error instanceof OllamaError ? error.kind : "unknown",
|
||||
},
|
||||
{ status: 502 },
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { authErrorMessage, authErrorKind, canonicalModelName, getServerConfig, resolveAuth, resolveModelName } from "@/lib/config";
|
||||
import { canonicalModelName, getServerConfig, resolveModelName } from "@/lib/config";
|
||||
import { friendlyErrorMessage, ollamaChatStream, ollamaListModels, OllamaError } from "@/lib/ollama";
|
||||
import { providerChatStream } from "@/lib/openai-compat";
|
||||
import { resolveModelTarget } from "@/lib/providers-server";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
import { acquireModelGate } from "@/lib/model-guard";
|
||||
|
|
@ -66,13 +68,12 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`chat:${clientKey(request)}`, config.rateLimits.chat, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -106,10 +107,13 @@ export async function POST(request: Request) {
|
|||
const model = parsed.data.model ?? allowed[0];
|
||||
const wanted = canonicalModelName(model);
|
||||
const inFavourites = allowed.some((entry) => canonicalModelName(entry) === wanted);
|
||||
if (!inFavourites) {
|
||||
if (target.kind === "provider") {
|
||||
// The provider's own catalogue is the authority (the picker lists exactly
|
||||
// what /models returned); an unknown name comes back as its 404.
|
||||
} else if (!inFavourites) {
|
||||
let listed = false;
|
||||
try {
|
||||
const models = await ollamaListModels(auth.auth, config.healthTimeoutMs);
|
||||
const models = await ollamaListModels(target.auth, config.healthTimeoutMs);
|
||||
listed = models.some((entry) => canonicalModelName(entry.name) === wanted);
|
||||
} catch {
|
||||
listed = false;
|
||||
|
|
@ -166,21 +170,35 @@ export async function POST(request: Request) {
|
|||
};
|
||||
|
||||
try {
|
||||
const result = await ollamaChatStream({
|
||||
model,
|
||||
auth: auth.auth,
|
||||
messages: [{ role: "system", content: system }, ...history],
|
||||
signal: abort.signal,
|
||||
onToken: (text) => send({ type: "token", text }),
|
||||
onThinking: (text) => send({ type: "thinking", text }),
|
||||
const messages = [{ role: "system" as const, content: system }, ...history];
|
||||
const result =
|
||||
target.kind === "provider"
|
||||
? await providerChatStream({
|
||||
target: target.target,
|
||||
model,
|
||||
messages,
|
||||
signal: abort.signal,
|
||||
onToken: (text) => send({ type: "token", text }),
|
||||
onThinking: (text) => send({ type: "thinking", text }),
|
||||
})
|
||||
: await ollamaChatStream({
|
||||
model,
|
||||
auth: target.auth,
|
||||
messages,
|
||||
signal: abort.signal,
|
||||
onToken: (text) => send({ type: "token", text }),
|
||||
onThinking: (text) => send({ type: "thinking", text }),
|
||||
});
|
||||
send({
|
||||
type: "done",
|
||||
model: target.kind === "provider" ? result.model : resolveModelName(result.model, target.auth.directCloud),
|
||||
});
|
||||
send({ type: "done", model: resolveModelName(result.model, auth.auth.directCloud) });
|
||||
} catch (error) {
|
||||
// A client-side stop aborts the upstream call: nothing to report.
|
||||
if (abort.signal.aborted && request.signal.aborted) {
|
||||
/* stopped by the user */
|
||||
} else {
|
||||
const message = friendlyErrorMessage(error, auth.auth.baseUrl, locale);
|
||||
const message = friendlyErrorMessage(error, target.kind === "provider" ? target.target.baseUrl : target.auth.baseUrl, locale);
|
||||
console.error("[api/chat]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
send({
|
||||
type: "error",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { extractNotes, generateActivities, notesToText, type PipelineStage } from "@/lib/ai";
|
||||
import { authErrorMessage, authErrorKind, getServerConfig, resolveAuth, resolveModelName } from "@/lib/config";
|
||||
import { getServerConfig, resolveModelName } from "@/lib/config";
|
||||
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
|
@ -19,6 +20,9 @@ export const maxDuration = 300;
|
|||
const MAX_IMAGE_CHARS = 8_000_000;
|
||||
|
||||
const requestSchema = z.object({
|
||||
|
||||
textModel: z.string().min(1).max(200).optional(),
|
||||
visionModel: z.string().min(1).max(200).optional(),
|
||||
images: z.array(dataUrlSchema(MAX_IMAGE_CHARS)).max(16).optional(),
|
||||
text: z.string().max(120_000).optional(),
|
||||
difficulty: difficultySchema.default("medium"),
|
||||
|
|
@ -63,13 +67,14 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`generate:${clientKey(request)}`, config.rateLimits.generate, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
const provider = target.kind === "provider" ? target.target : undefined;
|
||||
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -105,6 +110,19 @@ export async function POST(request: Request) {
|
|||
|
||||
// Concurrency + the account-wide model budget (lib/model-guard). Taken
|
||||
// only now, after every cheap rejection, so a 400 can never leak a slot.
|
||||
// Provider runs carry the model the user picked in Settings → Providers;
|
||||
// the server's Ollama models stay the default.
|
||||
const providerTextModel = provider ? parsed.data.textModel ?? parsed.data.visionModel : undefined;
|
||||
const providerVisionModel = provider ? parsed.data.visionModel ?? parsed.data.textModel : undefined;
|
||||
if (provider && !providerTextModel) {
|
||||
return Response.json(
|
||||
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const textModel = provider ? providerTextModel! : config.textModel;
|
||||
const visionModel = provider ? providerVisionModel ?? providerTextModel! : config.visionModel;
|
||||
|
||||
const gate = acquireModelGate(request, config, locale);
|
||||
if ("response" in gate) return gate.response;
|
||||
|
||||
|
|
@ -140,9 +158,10 @@ export async function POST(request: Request) {
|
|||
if (imageList.length > 0) {
|
||||
send({ type: "stage", stage: "vision", message: "Reading your notes…" });
|
||||
const extraction = await extractNotes({
|
||||
model: config.visionModel,
|
||||
repairModel: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: visionModel,
|
||||
repairModel: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
images: imageList,
|
||||
text,
|
||||
signal: abort.signal,
|
||||
|
|
@ -165,8 +184,9 @@ export async function POST(request: Request) {
|
|||
}
|
||||
|
||||
const generated = await generateActivities({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
notes: sourceText,
|
||||
difficulty,
|
||||
count: requestedCount,
|
||||
|
|
@ -198,12 +218,12 @@ export async function POST(request: Request) {
|
|||
generated: activities.length,
|
||||
repaired,
|
||||
language: generated.deck.language || detectedLanguage || null,
|
||||
visionModel: imageList.length ? resolveModelName(config.visionModel, auth.auth.directCloud) : null,
|
||||
textModel: resolveModelName(config.textModel, auth.auth.directCloud),
|
||||
visionModel: imageList.length ? (provider ? visionModel : resolveModelName(config.visionModel, baseAuth.directCloud)) : null,
|
||||
textModel: provider ? textModel : resolveModelName(config.textModel, baseAuth.directCloud),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const message = friendlyErrorMessage(error, auth.auth.baseUrl, locale);
|
||||
const message = friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale);
|
||||
// Never log the request body: it may contain the user's notes.
|
||||
console.error("[api/generate]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
send({
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { gradeShortAnswer } from "@/lib/ai";
|
||||
import { authErrorMessage, authErrorKind, getServerConfig, resolveAuth } from "@/lib/config";
|
||||
import { getServerConfig } from "@/lib/config";
|
||||
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
|
@ -12,6 +13,8 @@ export const runtime = "nodejs";
|
|||
export const dynamic = "force-dynamic";
|
||||
|
||||
const requestSchema = z.object({
|
||||
|
||||
model: z.string().min(1).max(200).optional(),
|
||||
question: z.string().min(1).max(1000),
|
||||
referenceAnswer: z.string().min(1).max(2000),
|
||||
keyPoints: z.array(z.string().max(300)).max(8).default([]),
|
||||
|
|
@ -35,13 +38,14 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`grade:${clientKey(request)}`, config.rateLimits.grade, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
const provider = target.kind === "provider" ? target.target : undefined;
|
||||
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -55,12 +59,23 @@ export async function POST(request: Request) {
|
|||
return Response.json({ error: translate(locale, "server.invalidGrade") }, { status: 400 });
|
||||
}
|
||||
|
||||
// Provider runs carry the model the user picked in Settings → Providers.
|
||||
const providerModel = provider ? parsed.data.model : undefined;
|
||||
if (provider && !providerModel) {
|
||||
return Response.json(
|
||||
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const textModel = provider ? providerModel! : config.textModel;
|
||||
|
||||
const gate = acquireModelGate(request, config, locale);
|
||||
if ("response" in gate) return gate.response;
|
||||
try {
|
||||
const result = await gradeShortAnswer({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
...parsed.data,
|
||||
// Grading is quick; don't let a hung request block the feed for minutes.
|
||||
signal: AbortSignal.timeout(Math.min(config.timeoutMs, 120_000)),
|
||||
|
|
@ -70,7 +85,7 @@ export async function POST(request: Request) {
|
|||
console.error("[api/grade]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
return Response.json(
|
||||
{
|
||||
error: friendlyErrorMessage(error, auth.auth.baseUrl, locale),
|
||||
error: friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale),
|
||||
kind: error instanceof OllamaError ? error.kind : "unknown",
|
||||
},
|
||||
{ status: 502 },
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { refineHomeworkText } from "@/lib/ai";
|
||||
import { authErrorKind, authErrorMessage, getServerConfig, resolveAuth, resolveModelName } from "@/lib/config";
|
||||
import { getServerConfig, resolveModelName } from "@/lib/config";
|
||||
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { translate } from "@/lib/i18n-dict";
|
||||
|
|
@ -17,6 +18,8 @@ export const dynamic = "force-dynamic";
|
|||
*/
|
||||
|
||||
const requestSchema = z.object({
|
||||
|
||||
model: z.string().min(1).max(200).optional(),
|
||||
instruction: z.enum(["shorter", "more", "explain"]),
|
||||
text: z.string().min(1).max(20_000),
|
||||
/** The assignment, for context (optional). */
|
||||
|
|
@ -35,13 +38,14 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`refine:${clientKey(request)}`, config.rateLimits.refine, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
const provider = target.kind === "provider" ? target.target : undefined;
|
||||
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -65,18 +69,29 @@ export async function POST(request: Request) {
|
|||
);
|
||||
}
|
||||
|
||||
// Provider runs carry the model the user picked in Settings → Providers.
|
||||
const providerModel = provider ? parsed.data.model : undefined;
|
||||
if (provider && !providerModel) {
|
||||
return Response.json(
|
||||
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const textModel = provider ? providerModel! : config.textModel;
|
||||
|
||||
const gate = acquireModelGate(request, config, locale);
|
||||
if ("response" in gate) return gate.response;
|
||||
try {
|
||||
const text = await refineHomeworkText({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
instruction: parsed.data.instruction,
|
||||
text: parsed.data.text,
|
||||
context: parsed.data.context,
|
||||
});
|
||||
return Response.json(
|
||||
{ text, model: resolveModelName(config.textModel, auth.auth.directCloud) },
|
||||
{ text, model: provider ? textModel : resolveModelName(config.textModel, baseAuth.directCloud) },
|
||||
{ headers: { "Cache-Control": "no-store" } },
|
||||
);
|
||||
} catch (error) {
|
||||
|
|
@ -84,7 +99,7 @@ export async function POST(request: Request) {
|
|||
console.error("[api/homework/refine]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
return Response.json(
|
||||
{
|
||||
error: friendlyErrorMessage(error, auth.auth.baseUrl, locale),
|
||||
error: friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale),
|
||||
kind: error instanceof OllamaError ? error.kind : "unknown",
|
||||
},
|
||||
{ status: 502 },
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { buildHomeworkGuide, checkHomework, extractNotes, makeHomework, notesToText, type PipelineStage } from "@/lib/ai";
|
||||
import { authErrorMessage, authErrorKind, getServerConfig, resolveAuth, resolveModelName } from "@/lib/config";
|
||||
import { getServerConfig, resolveModelName } from "@/lib/config";
|
||||
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
|
@ -20,6 +21,9 @@ const MAX_IMAGE_CHARS = 8_000_000;
|
|||
const MAX_BODY_BYTES = 33554432;
|
||||
|
||||
const requestSchema = z.object({
|
||||
|
||||
textModel: z.string().min(1).max(200).optional(),
|
||||
visionModel: z.string().min(1).max(200).optional(),
|
||||
mode: homeworkModeSchema,
|
||||
images: z.array(dataUrlSchema(MAX_IMAGE_CHARS)).max(16).optional(),
|
||||
text: z.string().max(120_000).optional(),
|
||||
|
|
@ -56,13 +60,14 @@ export async function POST(request: Request) {
|
|||
const limit = rateLimit(`homework:${clientKey(request)}`, config.rateLimits.homework, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: authErrorMessage(locale, auth.error), kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
// Ollama (server-configured) or a cloud provider the browser sent a key for.
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
const target = resolved.target;
|
||||
const provider = target.kind === "provider" ? target.target : undefined;
|
||||
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
|
|
@ -99,6 +104,19 @@ export async function POST(request: Request) {
|
|||
|
||||
// Concurrency + the account-wide model budget (lib/model-guard), taken
|
||||
// after every cheap rejection so a 400 can never leak a slot.
|
||||
// Provider runs carry the model the user picked in Settings → Providers;
|
||||
// the server's Ollama models stay the default.
|
||||
const providerTextModel = provider ? parsed.data.textModel ?? parsed.data.visionModel : undefined;
|
||||
const providerVisionModel = provider ? parsed.data.visionModel ?? parsed.data.textModel : undefined;
|
||||
if (provider && !providerTextModel) {
|
||||
return Response.json(
|
||||
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const textModel = provider ? providerTextModel! : config.textModel;
|
||||
const visionModel = provider ? providerVisionModel ?? providerTextModel! : config.visionModel;
|
||||
|
||||
const gate = acquireModelGate(request, config, locale);
|
||||
if ("response" in gate) return gate.response;
|
||||
|
||||
|
|
@ -128,9 +146,10 @@ export async function POST(request: Request) {
|
|||
if (imageList.length > 0) {
|
||||
send({ type: "stage", stage: "vision", message: translate(locale, "homework.loading.reading") });
|
||||
const extraction = await extractNotes({
|
||||
model: config.visionModel,
|
||||
repairModel: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: visionModel,
|
||||
repairModel: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
images: imageList,
|
||||
text,
|
||||
signal: abort.signal,
|
||||
|
|
@ -159,8 +178,8 @@ export async function POST(request: Request) {
|
|||
const meta = {
|
||||
repaired,
|
||||
language: detectedLanguage || null,
|
||||
visionModel: imageList.length ? resolveModelName(config.visionModel, auth.auth.directCloud) : null,
|
||||
textModel: resolveModelName(config.textModel, auth.auth.directCloud),
|
||||
visionModel: imageList.length ? (provider ? visionModel : resolveModelName(config.visionModel, baseAuth.directCloud)) : null,
|
||||
textModel: provider ? textModel : resolveModelName(config.textModel, baseAuth.directCloud),
|
||||
};
|
||||
|
||||
if (mode === "check") {
|
||||
|
|
@ -174,9 +193,10 @@ export async function POST(request: Request) {
|
|||
message: translate(locale, "homework.loading.readingSolution"),
|
||||
});
|
||||
const transcription = await extractNotes({
|
||||
model: config.visionModel,
|
||||
repairModel: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: visionModel,
|
||||
repairModel: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
images: solutionImageList,
|
||||
signal: abort.signal,
|
||||
onProgress: (message) => send({ type: "stage", stage: "vision", message }),
|
||||
|
|
@ -185,8 +205,9 @@ export async function POST(request: Request) {
|
|||
}
|
||||
|
||||
const result = await checkHomework({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
assignment: assignmentText,
|
||||
solution: solutionText,
|
||||
language: detectedLanguage,
|
||||
|
|
@ -211,8 +232,9 @@ export async function POST(request: Request) {
|
|||
});
|
||||
} else if (mode === "make") {
|
||||
const result = await makeHomework({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
assignment: assignmentText,
|
||||
language: detectedLanguage,
|
||||
signal: abort.signal,
|
||||
|
|
@ -232,8 +254,9 @@ export async function POST(request: Request) {
|
|||
});
|
||||
} else {
|
||||
const guide = await buildHomeworkGuide({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
model: textModel,
|
||||
auth: baseAuth,
|
||||
provider,
|
||||
assignment: assignmentText,
|
||||
language: detectedLanguage,
|
||||
signal: abort.signal,
|
||||
|
|
@ -257,7 +280,7 @@ export async function POST(request: Request) {
|
|||
});
|
||||
}
|
||||
} catch (error) {
|
||||
const message = friendlyErrorMessage(error, auth.auth.baseUrl, locale);
|
||||
const message = friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale);
|
||||
// Never log the request body: it may contain the student's assignment.
|
||||
console.error("[api/homework]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
send({
|
||||
|
|
|
|||
36
app/api/providers/route.ts
Normal file
36
app/api/providers/route.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { getServerConfig } from "@/lib/config";
|
||||
import { requestLocale } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
import { providerListModels } from "@/lib/openai-compat";
|
||||
import { resolveModelTarget } from "@/lib/providers-server";
|
||||
|
||||
/**
|
||||
* Lists the models a cloud provider offers to the browser's key — the same
|
||||
* listing the chat picker and the Settings rows show. The key travels in the
|
||||
* request headers and is never stored.
|
||||
*/
|
||||
export async function GET(request: Request) {
|
||||
const locale = requestLocale(request);
|
||||
const config = getServerConfig();
|
||||
|
||||
const limit = rateLimit(`providers:${clientKey(request)}`, config.rateLimits.health, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
||||
|
||||
const resolved = resolveModelTarget(config, request, locale);
|
||||
if ("error" in resolved) {
|
||||
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
||||
}
|
||||
if (resolved.target.kind !== "provider") {
|
||||
return Response.json({ error: "Pick a provider to list its models.", kind: "bad_provider" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const models = await providerListModels(resolved.target.target, config.healthTimeoutMs * 2);
|
||||
return Response.json({ provider: resolved.target.provider, models }, { headers: { "Cache-Control": "no-store" } });
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Could not list the models.";
|
||||
const kind = (error as { kind?: string }).kind ?? "unknown";
|
||||
console.error("[api/providers]", kind, message);
|
||||
return Response.json({ error: message, kind }, { status: 502, headers: { "Cache-Control": "no-store" } });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue