feat: API key settings page — store ANTHROPIC/VOYAGE keys in DB with admin UI

- 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
This commit is contained in:
2026-07-11 17:46:40 +08:00
co-authored by Claude Sonnet 4.6
parent a07c9f0910
commit b9ab94c9da
14 changed files with 277 additions and 19 deletions
+32
View File
@@ -0,0 +1,32 @@
export const dynamic = 'force-dynamic'
import { redirect } from 'next/navigation'
import { createClient } from '@/lib/supabase/server'
import { ApiKeyForm } from '@/components/settings/api-key-form'
export default async function SettingsPage() {
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) redirect('/login')
const { data: profile } = await supabase.from('users').select('role').eq('id', user.id).single()
if (!profile || profile.role !== 'admin') redirect('/hse/dashboard')
const { data: settings } = await supabase
.from('app_settings')
.select('key, value, updated_at')
const settingsMap = Object.fromEntries(
(settings ?? []).map(s => [s.key, { set: Boolean(s.value), updated_at: s.updated_at }])
)
return (
<main className="max-w-2xl mx-auto px-4 py-6">
<h1 className="text-2xl font-bold text-gray-900 mb-2">Settings</h1>
<p className="text-sm text-gray-500 mb-6">
API keys are stored securely in the database. Leave blank to use environment variables.
</p>
<ApiKeyForm settings={settingsMap} />
</main>
)
}