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, 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({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue