freezmo/lib/chat.ts
Martin Dvorak 4372423167 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.
2026-10-14 06:29:39 +00:00

92 lines
2.6 KiB
TypeScript

"use client";
/**
* Chat helpers and types shared by the chat UI. The record shapes themselves
* live in `lib/schemas.ts` (zod, shared with backup/sync); this module
* re-exports them so UI code has one import.
*/
import type { ChatAttachment, ChatMessage, ChatScope, ChatSource } from "./schemas";
export type { ChatAttachment, ChatMessage, ChatScope, ChatSource };
export type ChatError = { message: string; kind?: string };
export type ChatModelChoice = {
name: string;
badge: "text" | "vision" | "both";
available: boolean | null;
/** Cloud provider the model belongs to (Ollama when omitted). */
provider?: string;
};
/** Last model choice, remembered across visits. */
const MODEL_KEY = "freezmo.chat.model.v1";
const MODEL_PROVIDER_KEY = "freezmo.chat.provider.v1";
export function readStoredChatModel(): string | null {
try {
const value = window.localStorage.getItem(MODEL_KEY);
return value && value.trim() ? value : null;
} catch {
return null;
}
}
export function storeChatModel(model: string): void {
try {
window.localStorage.setItem(MODEL_KEY, model);
} catch {
/* private mode */
}
}
/** The provider that owns the remembered model (Ollama unless a cloud one). */
export function readStoredChatProvider(): string {
try {
return window.localStorage.getItem(MODEL_PROVIDER_KEY) ?? "ollama";
} catch {
return "ollama";
}
}
export function storeChatProvider(provider: string): void {
try {
window.localStorage.setItem(MODEL_PROVIDER_KEY, provider);
} catch {
/* private mode */
}
}
export function chatMessageId(prefix: string): string {
const rand =
typeof crypto !== "undefined" && "randomUUID" in crypto
? crypto.randomUUID().slice(0, 8)
: Math.random().toString(36).slice(2, 10);
return `${prefix}_${Date.now().toString(36)}_${rand}`;
}
/** Auto title from the first user turn (markdown stripped, one line). */
export function titleFromMessage(content: string): string {
const line = content
.replace(/[#*`>_]/g, "")
.replace(/\s+/g, " ")
.trim();
if (!line) return "";
return line.length > 60 ? `${line.slice(0, 57)}…` : line;
}
/** Case/diacritic-insensitive search over titles and message text. */
export function conversationMatches(title: string, messages: ChatMessage[], query: string): boolean {
const needle = fold(query);
if (!needle) return true;
if (fold(title).includes(needle)) return true;
return messages.some((message) => fold(message.content).includes(needle));
}
function fold(value: string): string {
return value
.toLowerCase()
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "");
}