agent-auth.ts
1 import { getDb } from '@/server/storage/db' 2 3 export type AgentAuthBackendKey = 'jules' 4 5 interface AgentAuthSettingsRow { 6 backend_id: string 7 api_key: string | null 8 updated_at: string 9 } 10 11 export function getAgentApiKey(backendId: AgentAuthBackendKey): string | null { 12 const db = getDb() 13 const row = db 14 .prepare( 15 `SELECT backend_id, api_key, updated_at 16 FROM agent_auth_settings 17 WHERE backend_id = ?`, 18 ) 19 .get(backendId) as AgentAuthSettingsRow | undefined 20 21 const key = row?.api_key?.trim() ?? '' 22 return key.length > 0 ? key : null 23 } 24 25 export function setAgentApiKey(input: { 26 backendId: AgentAuthBackendKey 27 apiKey: string | null 28 }): void { 29 const db = getDb() 30 const now = new Date().toISOString() 31 const apiKey = input.apiKey?.trim() ?? null 32 db.prepare( 33 `INSERT INTO agent_auth_settings (backend_id, api_key, updated_at) 34 VALUES (?, ?, ?) 35 ON CONFLICT(backend_id) DO UPDATE SET 36 api_key = excluded.api_key, 37 updated_at = excluded.updated_at`, 38 ).run(input.backendId, apiKey, now) 39 } 40 41 export function clearAgentApiKey(backendId: AgentAuthBackendKey): void { 42 setAgentApiKey({ 43 backendId, 44 apiKey: null, 45 }) 46 } 47 48 export function hasAgentApiKey(backendId: AgentAuthBackendKey): boolean { 49 return Boolean(getAgentApiKey(backendId)) 50 }