mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 15:38:08 +01:00
Create lib/chat/ module with shared utilities: - embedding.ts: generateEmbeddingWithTimeout (embedding + timeout guard) - settings.ts: getUserLLMSettings (provider/apiKey/ollamaUrl lookup) - context.ts: truncateChunks + joinContext (context building) - llm-errors.ts: handleLLMError (API key fallback, Ollama, AI unavailable) - parse.ts: parseResponseWithSuggestions (extract >>> suggestions) Update all 3 chat routes to use shared utilities, removing ~220 lines of duplicated code. Also fix module-scope getServerEnv() call in llm-client that caused SSG build failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
38 lines
994 B
TypeScript
38 lines
994 B
TypeScript
/**
|
|
* Shared user LLM settings lookup
|
|
* @module lib/chat/settings
|
|
*/
|
|
|
|
import { getServiceClient } from '@/lib/supabase';
|
|
import type { ModelProvider } from '@/lib/llm-client';
|
|
|
|
export interface LLMSettings {
|
|
provider: ModelProvider;
|
|
apiKey: string | null | undefined;
|
|
ollamaUrl: string | null | undefined;
|
|
}
|
|
|
|
/**
|
|
* Look up a user's preferred LLM provider and API keys.
|
|
* Falls back to 'groq' if no settings exist.
|
|
*/
|
|
export async function getUserLLMSettings(userId: string): Promise<LLMSettings> {
|
|
const supabase = getServiceClient();
|
|
const { data: settings } = await supabase
|
|
.from('user_settings')
|
|
.select('*')
|
|
.eq('id', userId)
|
|
.single();
|
|
|
|
const provider: ModelProvider = settings?.preferred_model || 'groq';
|
|
const apiKey =
|
|
provider === 'groq'
|
|
? settings?.groq_api_key
|
|
: provider === 'openrouter'
|
|
? settings?.openrouter_api_key
|
|
: null;
|
|
const ollamaUrl = settings?.ollama_url;
|
|
|
|
return { provider, apiKey, ollamaUrl };
|
|
}
|