- Migration: app_settings table with admin-only RLS (ANTHROPIC_API_KEY, VOYAGE_API_KEY) - lib/settings.ts: getApiKey() reads DB first, falls back to env var - lib/claude/client.ts: factory createAnthropicClient(apiKey) replaces singleton - lib/claude/embed.ts: optional apiKey param, falls back to env - 3 Claude AI routes + similar route: fetch key from settings before calling AI - incidents/route.ts: fire-and-forget embed reads VOYAGE key from settings - GET/POST /api/settings: admin-only masked key management endpoint - /hse/settings page + ApiKeyForm client component Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr
18 lines
531 B
TypeScript
18 lines
531 B
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
|
|
export async function getApiKey(supabase: SupabaseClient, key: string): Promise<string> {
|
|
try {
|
|
const { data } = await supabase
|
|
.from('app_settings')
|
|
.select('value')
|
|
.eq('key', key)
|
|
.single()
|
|
if (data?.value) return data.value
|
|
} catch {
|
|
// fall through to env
|
|
}
|
|
const env = process.env[key]
|
|
if (env) return env
|
|
throw new Error(`${key} not configured. Add it in Settings (/hse/settings) or as an environment variable.`)
|
|
}
|