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
84
lib/ai.ts
84
lib/ai.ts
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import { OllamaError, ollamaChat, type OllamaMessage } from "./ollama";
|
||||
import { providerChatJson, type ProviderTarget } from "./openai-compat";
|
||||
import { needsLanguageRepair } from "./language";
|
||||
import {
|
||||
buildGenerationUserPrompt,
|
||||
|
|
@ -46,6 +47,8 @@ export type PipelineStage = "vision" | "generation" | "done";
|
|||
type JsonStepOptions<T> = {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
system: string;
|
||||
user: string;
|
||||
images?: string[];
|
||||
|
|
@ -67,6 +70,7 @@ type JsonStepResult<T> = {
|
|||
async function runJsonStep<T>({
|
||||
model,
|
||||
auth,
|
||||
provider,
|
||||
system,
|
||||
user,
|
||||
images,
|
||||
|
|
@ -85,14 +89,22 @@ async function runJsonStep<T>({
|
|||
let lastError = "";
|
||||
|
||||
for (let attempt = 1; attempt <= 2; attempt++) {
|
||||
const result = await ollamaChat({
|
||||
model,
|
||||
auth,
|
||||
messages,
|
||||
json: true,
|
||||
signal,
|
||||
temperature: temperature ?? 0.4,
|
||||
});
|
||||
const result = provider
|
||||
? await providerChatJson({
|
||||
target: provider,
|
||||
model,
|
||||
messages,
|
||||
signal,
|
||||
temperature: temperature ?? 0.4,
|
||||
})
|
||||
: await ollamaChat({
|
||||
model,
|
||||
auth,
|
||||
messages,
|
||||
json: true,
|
||||
signal,
|
||||
temperature: temperature ?? 0.4,
|
||||
});
|
||||
lastRaw = result.content;
|
||||
|
||||
let parsed: unknown;
|
||||
|
|
@ -151,6 +163,8 @@ export async function extractNotes(input: {
|
|||
/** Model used to restore the language if the transcription was translated. */
|
||||
repairModel: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
images: string[];
|
||||
text?: string;
|
||||
signal?: AbortSignal;
|
||||
|
|
@ -163,6 +177,7 @@ export async function extractNotes(input: {
|
|||
const { value, repaired } = await runJsonStep<ExtractedNotes>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: VISION_SYSTEM_PROMPT,
|
||||
user,
|
||||
images: input.images,
|
||||
|
|
@ -187,6 +202,7 @@ export async function extractNotes(input: {
|
|||
const restored = await restoreLanguage({
|
||||
model: input.repairModel,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
language: value.language ?? "the original language of the notes",
|
||||
extraction: value,
|
||||
signal: input.signal,
|
||||
|
|
@ -218,6 +234,8 @@ Rules:
|
|||
export async function restoreLanguage(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
language: string;
|
||||
extraction: ExtractedNotes;
|
||||
signal?: AbortSignal;
|
||||
|
|
@ -225,6 +243,7 @@ export async function restoreLanguage(input: {
|
|||
const value = await runJsonStep<ExtractedNotes>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: RESTORE_SYSTEM_PROMPT,
|
||||
user: [
|
||||
`ORIGINAL LANGUAGE: ${input.language}`,
|
||||
|
|
@ -338,6 +357,8 @@ function validateDeck(parsed: unknown, minimum: number, requested: number) {
|
|||
export async function generateActivities(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
notes: string;
|
||||
difficulty: Difficulty;
|
||||
count: number;
|
||||
|
|
@ -360,6 +381,7 @@ export async function generateActivities(input: {
|
|||
}>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: GENERATION_SYSTEM_PROMPT,
|
||||
user: buildGenerationUserPrompt({
|
||||
notes: input.notes,
|
||||
|
|
@ -388,6 +410,8 @@ const GRADE_SHAPE_HINT = `{"correct":true,"score":85,"feedback":"short feedback"
|
|||
export async function gradeShortAnswer(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
question: string;
|
||||
referenceAnswer: string;
|
||||
keyPoints: string[];
|
||||
|
|
@ -398,6 +422,7 @@ export async function gradeShortAnswer(input: {
|
|||
const { value } = await runJsonStep<GradeResult>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: GRADING_SYSTEM_PROMPT,
|
||||
user: buildGradingUserPrompt(input),
|
||||
validate: (parsed) => {
|
||||
|
|
@ -425,6 +450,8 @@ const HOMEWORK_MAKE_SHAPE_HINT = `{"title":"...","language":"English name of the
|
|||
export async function makeHomework(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
assignment: string;
|
||||
language?: string;
|
||||
signal?: AbortSignal;
|
||||
|
|
@ -432,6 +459,7 @@ export async function makeHomework(input: {
|
|||
const { value } = await runJsonStep<HomeworkMake>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: HOMEWORK_MAKE_SYSTEM_PROMPT,
|
||||
user: buildHomeworkMakeUserPrompt({ assignment: input.assignment, language: input.language }),
|
||||
validate: (parsed) => {
|
||||
|
|
@ -458,6 +486,8 @@ const HOMEWORK_GUIDE_SHAPE_HINT = `{"title":"...","language":"English name of th
|
|||
export async function buildHomeworkGuide(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
assignment: string;
|
||||
language?: string;
|
||||
signal?: AbortSignal;
|
||||
|
|
@ -465,6 +495,7 @@ export async function buildHomeworkGuide(input: {
|
|||
const { value } = await runJsonStep<HomeworkGuide>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: HOMEWORK_GUIDE_SYSTEM_PROMPT,
|
||||
user: buildHomeworkGuideUserPrompt({ assignment: input.assignment, language: input.language }),
|
||||
validate: (parsed) => {
|
||||
|
|
@ -526,6 +557,8 @@ const HOMEWORK_CHECK_SHAPE_HINT = `{"title":"...","language":"English name of th
|
|||
export async function checkHomework(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
assignment: string;
|
||||
solution: string;
|
||||
language?: string;
|
||||
|
|
@ -534,6 +567,7 @@ export async function checkHomework(input: {
|
|||
const { value } = await runJsonStep<HomeworkCheck>({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
provider: input.provider,
|
||||
system: HOMEWORK_CHECK_SYSTEM_PROMPT,
|
||||
user: buildHomeworkCheckUserPrompt({
|
||||
assignment: input.assignment,
|
||||
|
|
@ -558,29 +592,27 @@ export async function checkHomework(input: {
|
|||
export async function refineHomeworkText(input: {
|
||||
model: string;
|
||||
auth: OllamaAuth;
|
||||
/** Cloud provider override (OpenAI-compatible); falls back to `auth`. */
|
||||
provider?: ProviderTarget;
|
||||
instruction: "shorter" | "more" | "explain";
|
||||
text: string;
|
||||
context?: string;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<string> {
|
||||
const result = await ollamaChat({
|
||||
model: input.model,
|
||||
auth: input.auth,
|
||||
json: false,
|
||||
signal: input.signal,
|
||||
temperature: 0.4,
|
||||
messages: [
|
||||
{ role: "system", content: REFINE_SYSTEM_PROMPT },
|
||||
{
|
||||
role: "user",
|
||||
content: buildRefineUserPrompt({
|
||||
instruction: input.instruction,
|
||||
text: input.text,
|
||||
context: input.context,
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
const messages: OllamaMessage[] = [
|
||||
{ role: "system", content: REFINE_SYSTEM_PROMPT },
|
||||
{
|
||||
role: "user",
|
||||
content: buildRefineUserPrompt({
|
||||
instruction: input.instruction,
|
||||
text: input.text,
|
||||
context: input.context,
|
||||
}),
|
||||
},
|
||||
];
|
||||
const result = input.provider
|
||||
? await providerChatJson({ target: input.provider, model: input.model, messages, signal: input.signal, temperature: 0.4 })
|
||||
: await ollamaChat({ model: input.model, auth: input.auth, json: false, signal: input.signal, temperature: 0.4, messages });
|
||||
return result.content.trim();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue