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.
128 lines
4.8 KiB
TypeScript
128 lines
4.8 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { extractNotes, notesToText } from "@/lib/ai";
|
|
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";
|
|
import { acquireModelGate } from "@/lib/model-guard";
|
|
import { dataUrlSchema } from "@/lib/schemas";
|
|
import { translate } from "@/lib/i18n-dict";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
export const maxDuration = 300;
|
|
|
|
/** Same ceiling as deck generation: up to 16 resized photos. */
|
|
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(),
|
|
});
|
|
|
|
/**
|
|
* Turns chat attachments (photos, scanned pages, pasted document text) into
|
|
* plain text with the existing vision pipeline, so any chat model — including
|
|
* text-only ones — can use them. Never stores anything.
|
|
*/
|
|
export async function POST(request: Request) {
|
|
const locale = requestLocale(request);
|
|
const crossOrigin = rejectCrossOrigin(request);
|
|
if (crossOrigin) return crossOrigin;
|
|
const oversized = rejectOversizedBody(request, MAX_BODY_BYTES);
|
|
if (oversized) return oversized;
|
|
|
|
const config = getServerConfig();
|
|
|
|
const limit = rateLimit(`chat:${clientKey(request)}`, config.rateLimits.chat, config.rateLimits.windowMs);
|
|
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
|
|
|
// 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 {
|
|
payload = await request.json();
|
|
} catch {
|
|
return Response.json({ error: translate(locale, "server.bodyNotJson") }, { status: 400 });
|
|
}
|
|
|
|
const parsed = requestSchema.safeParse(payload);
|
|
if (!parsed.success) {
|
|
return Response.json(
|
|
{ error: translate(locale, "server.invalidRequest", { detail: parsed.error.issues[0]?.message ?? "" }) },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const images = (parsed.data.images ?? []).map(stripDataUrlPrefix).filter(Boolean);
|
|
const text = parsed.data.text?.trim() ?? "";
|
|
if (images.length === 0 && !text) {
|
|
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();
|
|
const timer = setTimeout(() => abort.abort(), config.timeoutMs);
|
|
try {
|
|
if (images.length === 0) {
|
|
// Plain text needs no model call at all.
|
|
return Response.json({ text: text.slice(0, 48_000) });
|
|
}
|
|
const extraction = await extractNotes({
|
|
model: visionModel,
|
|
repairModel: textModel,
|
|
auth: baseAuth,
|
|
provider,
|
|
images,
|
|
text: text || undefined,
|
|
signal: abort.signal,
|
|
});
|
|
const extracted = notesToText(extraction.notes).slice(0, 48_000);
|
|
if (!extracted.trim()) {
|
|
return Response.json({ error: translate(locale, "chat.attachEmpty"), kind: "bad_response" }, { status: 422 });
|
|
}
|
|
return Response.json({ text: extracted });
|
|
} catch (error) {
|
|
console.error("[api/chat/extract]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
|
return Response.json(
|
|
{
|
|
error: friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || '', locale),
|
|
kind: error instanceof OllamaError ? error.kind : "unknown",
|
|
},
|
|
{ status: 502 },
|
|
);
|
|
} finally {
|
|
clearTimeout(timer);
|
|
gate.gate.release();
|
|
}
|
|
}
|
|
|
|
function stripDataUrlPrefix(value: string): string {
|
|
const commaIndex = value.indexOf(",");
|
|
if (value.startsWith("data:") && commaIndex !== -1) return value.slice(commaIndex + 1);
|
|
return value;
|
|
}
|