|
| 1 | +import { eq } from 'drizzle-orm' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { auth } from '@/lib/auth' |
| 4 | +import { createLogger } from '@/lib/logs/console/logger' |
| 5 | +import { db } from '@/../../packages/db' |
| 6 | +import { settings } from '@/../../packages/db/schema' |
| 7 | + |
| 8 | +const logger = createLogger('CopilotUserModelsAPI') |
| 9 | + |
| 10 | +const DEFAULT_ENABLED_MODELS: Record<string, boolean> = { |
| 11 | + 'gpt-4o': false, |
| 12 | + 'gpt-4.1': false, |
| 13 | + 'gpt-5-fast': false, |
| 14 | + 'gpt-5': true, |
| 15 | + 'gpt-5-medium': true, |
| 16 | + 'gpt-5-high': false, |
| 17 | + o3: true, |
| 18 | + 'claude-4-sonnet': true, |
| 19 | + 'claude-4.5-sonnet': true, |
| 20 | + 'claude-4.1-opus': true, |
| 21 | +} |
| 22 | + |
| 23 | +// GET - Fetch user's enabled models |
| 24 | +export async function GET(request: NextRequest) { |
| 25 | + try { |
| 26 | + const session = await auth.api.getSession({ headers: request.headers }) |
| 27 | + |
| 28 | + if (!session?.user?.id) { |
| 29 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 30 | + } |
| 31 | + |
| 32 | + const userId = session.user.id |
| 33 | + |
| 34 | + // Try to fetch existing settings record |
| 35 | + const [userSettings] = await db |
| 36 | + .select() |
| 37 | + .from(settings) |
| 38 | + .where(eq(settings.userId, userId)) |
| 39 | + .limit(1) |
| 40 | + |
| 41 | + if (userSettings) { |
| 42 | + const userModelsMap = (userSettings.copilotEnabledModels as Record<string, boolean>) || {} |
| 43 | + |
| 44 | + // Merge: start with defaults, then override with user's existing preferences |
| 45 | + const mergedModels = { ...DEFAULT_ENABLED_MODELS } |
| 46 | + for (const [modelId, enabled] of Object.entries(userModelsMap)) { |
| 47 | + mergedModels[modelId] = enabled |
| 48 | + } |
| 49 | + |
| 50 | + // If we added any new models, update the database |
| 51 | + const hasNewModels = Object.keys(DEFAULT_ENABLED_MODELS).some( |
| 52 | + (key) => !(key in userModelsMap) |
| 53 | + ) |
| 54 | + |
| 55 | + if (hasNewModels) { |
| 56 | + await db |
| 57 | + .update(settings) |
| 58 | + .set({ |
| 59 | + copilotEnabledModels: mergedModels, |
| 60 | + updatedAt: new Date(), |
| 61 | + }) |
| 62 | + .where(eq(settings.userId, userId)) |
| 63 | + } |
| 64 | + |
| 65 | + return NextResponse.json({ |
| 66 | + enabledModels: mergedModels, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + // If no settings record exists, create one with empty object (client will use defaults) |
| 71 | + const [created] = await db |
| 72 | + .insert(settings) |
| 73 | + .values({ |
| 74 | + id: userId, |
| 75 | + userId, |
| 76 | + copilotEnabledModels: {}, |
| 77 | + }) |
| 78 | + .returning() |
| 79 | + |
| 80 | + return NextResponse.json({ |
| 81 | + enabledModels: DEFAULT_ENABLED_MODELS, |
| 82 | + }) |
| 83 | + } catch (error) { |
| 84 | + logger.error('Failed to fetch user models', { error }) |
| 85 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// PUT - Update user's enabled models |
| 90 | +export async function PUT(request: NextRequest) { |
| 91 | + try { |
| 92 | + const session = await auth.api.getSession({ headers: request.headers }) |
| 93 | + |
| 94 | + if (!session?.user?.id) { |
| 95 | + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) |
| 96 | + } |
| 97 | + |
| 98 | + const userId = session.user.id |
| 99 | + const body = await request.json() |
| 100 | + |
| 101 | + if (!body.enabledModels || typeof body.enabledModels !== 'object') { |
| 102 | + return NextResponse.json({ error: 'enabledModels must be an object' }, { status: 400 }) |
| 103 | + } |
| 104 | + |
| 105 | + // Check if settings record exists |
| 106 | + const [existing] = await db.select().from(settings).where(eq(settings.userId, userId)).limit(1) |
| 107 | + |
| 108 | + if (existing) { |
| 109 | + // Update existing record |
| 110 | + await db |
| 111 | + .update(settings) |
| 112 | + .set({ |
| 113 | + copilotEnabledModels: body.enabledModels, |
| 114 | + updatedAt: new Date(), |
| 115 | + }) |
| 116 | + .where(eq(settings.userId, userId)) |
| 117 | + } else { |
| 118 | + // Create new settings record |
| 119 | + await db.insert(settings).values({ |
| 120 | + id: userId, |
| 121 | + userId, |
| 122 | + copilotEnabledModels: body.enabledModels, |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + return NextResponse.json({ success: true }) |
| 127 | + } catch (error) { |
| 128 | + logger.error('Failed to update user models', { error }) |
| 129 | + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) |
| 130 | + } |
| 131 | +} |
0 commit comments