mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-10 07:27:57 -05:00
* Add get ops examples * input format incorrectly created by copilot should not crash workflow * fix tool edits triggering overall delta * fix(db): add more options for SSL connection, add envvar for base64 db cert (#1533) * fix trigger additions * fix nested outputs for triggers * add condition subblock sanitization * fix custom tools json * Model selector * fix response format sanitization * remove dead code * fix export sanitization * Update migration * fix import race cond * Copilot settings * fix response format * stop loops/parallels copilot generation from breaking diff view * fix lint * Apply suggestion from @greptile-apps[bot] Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * fix tests * fix lint --------- Co-authored-by: Siddharth Ganesan <siddharthganesan@gmail.com> Co-authored-by: Waleed <walif6@gmail.com> Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
132 lines
3.8 KiB
TypeScript
132 lines
3.8 KiB
TypeScript
import { eq } from 'drizzle-orm'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/lib/auth'
|
|
import { createLogger } from '@/lib/logs/console/logger'
|
|
import { db } from '@/../../packages/db'
|
|
import { settings } from '@/../../packages/db/schema'
|
|
|
|
const logger = createLogger('CopilotUserModelsAPI')
|
|
|
|
const DEFAULT_ENABLED_MODELS: Record<string, boolean> = {
|
|
'gpt-4o': false,
|
|
'gpt-4.1': false,
|
|
'gpt-5-fast': false,
|
|
'gpt-5': true,
|
|
'gpt-5-medium': true,
|
|
'gpt-5-high': false,
|
|
o3: true,
|
|
'claude-4-sonnet': true,
|
|
'claude-4.5-sonnet': true,
|
|
'claude-4.1-opus': true,
|
|
}
|
|
|
|
// GET - Fetch user's enabled models
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await auth.api.getSession({ headers: request.headers })
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = session.user.id
|
|
|
|
// Try to fetch existing settings record
|
|
const [userSettings] = await db
|
|
.select()
|
|
.from(settings)
|
|
.where(eq(settings.userId, userId))
|
|
.limit(1)
|
|
|
|
if (userSettings) {
|
|
const userModelsMap = (userSettings.copilotEnabledModels as Record<string, boolean>) || {}
|
|
|
|
// Merge: start with defaults, then override with user's existing preferences
|
|
const mergedModels = { ...DEFAULT_ENABLED_MODELS }
|
|
for (const [modelId, enabled] of Object.entries(userModelsMap)) {
|
|
mergedModels[modelId] = enabled
|
|
}
|
|
|
|
// If we added any new models, update the database
|
|
const hasNewModels = Object.keys(DEFAULT_ENABLED_MODELS).some(
|
|
(key) => !(key in userModelsMap)
|
|
)
|
|
|
|
if (hasNewModels) {
|
|
await db
|
|
.update(settings)
|
|
.set({
|
|
copilotEnabledModels: mergedModels,
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(settings.userId, userId))
|
|
}
|
|
|
|
return NextResponse.json({
|
|
enabledModels: mergedModels,
|
|
})
|
|
}
|
|
|
|
// If no settings record exists, create one with empty object (client will use defaults)
|
|
const [created] = await db
|
|
.insert(settings)
|
|
.values({
|
|
id: userId,
|
|
userId,
|
|
copilotEnabledModels: {},
|
|
})
|
|
.returning()
|
|
|
|
return NextResponse.json({
|
|
enabledModels: DEFAULT_ENABLED_MODELS,
|
|
})
|
|
} catch (error) {
|
|
logger.error('Failed to fetch user models', { error })
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// PUT - Update user's enabled models
|
|
export async function PUT(request: NextRequest) {
|
|
try {
|
|
const session = await auth.api.getSession({ headers: request.headers })
|
|
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = session.user.id
|
|
const body = await request.json()
|
|
|
|
if (!body.enabledModels || typeof body.enabledModels !== 'object') {
|
|
return NextResponse.json({ error: 'enabledModels must be an object' }, { status: 400 })
|
|
}
|
|
|
|
// Check if settings record exists
|
|
const [existing] = await db.select().from(settings).where(eq(settings.userId, userId)).limit(1)
|
|
|
|
if (existing) {
|
|
// Update existing record
|
|
await db
|
|
.update(settings)
|
|
.set({
|
|
copilotEnabledModels: body.enabledModels,
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(settings.userId, userId))
|
|
} else {
|
|
// Create new settings record
|
|
await db.insert(settings).values({
|
|
id: userId,
|
|
userId,
|
|
copilotEnabledModels: body.enabledModels,
|
|
})
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
logger.error('Failed to update user models', { error })
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
}
|