diff --git a/sim/app/api/codegen/route.ts b/sim/app/api/codegen/route.ts index 711cac3561..164c7b99c6 100644 --- a/sim/app/api/codegen/route.ts +++ b/sim/app/api/codegen/route.ts @@ -1,3 +1,4 @@ +import { unstable_noStore as noStore } from 'next/cache' import { NextRequest, NextResponse } from 'next/server' import OpenAI from 'openai' import { createLogger } from '@/lib/logs/console-logger' @@ -196,6 +197,7 @@ export async function POST(req: NextRequest) { try { const body = (await req.json()) as RequestBody + noStore() // Destructure history along with other fields const { prompt, generationType, context, stream = false, history = [] } = body diff --git a/sim/app/api/workflows/[id]/deploy/route.ts b/sim/app/api/workflows/[id]/deploy/route.ts index 4f81bc1d56..f302dd7f1c 100644 --- a/sim/app/api/workflows/[id]/deploy/route.ts +++ b/sim/app/api/workflows/[id]/deploy/route.ts @@ -66,6 +66,30 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{ .where(eq(apiKey.userId, workflowData.userId)) .limit(1) + let userKey = null + + // If no API key exists, create one automatically + if (userApiKey.length === 0) { + try { + const newApiKey = generateApiKey() + await db.insert(apiKey).values({ + id: uuidv4(), + userId: workflowData.userId, + name: 'Default API Key', + key: newApiKey, + createdAt: new Date(), + updatedAt: new Date(), + }) + userKey = newApiKey + logger.info(`[${requestId}] Generated new API key for user: ${workflowData.userId}`) + } catch (keyError) { + // If key generation fails, log the error but continue with the request + logger.error(`[${requestId}] Failed to generate API key:`, keyError) + } + } else { + userKey = userApiKey[0].key + } + // Check if the workflow has meaningful changes that would require redeployment let needsRedeployment = false if (workflowData.deployedState) { @@ -78,7 +102,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{ logger.info(`[${requestId}] Successfully retrieved deployment info: ${id}`) return createSuccessResponse({ - apiKey: userApiKey.length > 0 ? userApiKey[0].key : null, + apiKey: userKey, isDeployed: workflowData.isDeployed, deployedAt: workflowData.deployedAt, needsRedeployment, @@ -134,16 +158,22 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{ // If no API key exists, create one if (userApiKey.length === 0) { - const newApiKey = generateApiKey() - await db.insert(apiKey).values({ - id: uuidv4(), - userId, - name: 'Default API Key', - key: newApiKey, - createdAt: new Date(), - updatedAt: new Date(), - }) - userKey = newApiKey + try { + const newApiKey = generateApiKey() + await db.insert(apiKey).values({ + id: uuidv4(), + userId, + name: 'Default API Key', + key: newApiKey, + createdAt: new Date(), + updatedAt: new Date(), + }) + userKey = newApiKey + logger.info(`[${requestId}] Generated new API key for user: ${userId}`) + } catch (keyError) { + // If key generation fails, log the error but continue with the request + logger.error(`[${requestId}] Failed to generate API key:`, keyError) + } } else { userKey = userApiKey[0].key } diff --git a/sim/app/w/[id]/components/control-bar/components/deployment-controls/deployment-controls.tsx b/sim/app/w/[id]/components/control-bar/components/deployment-controls/deployment-controls.tsx index e1a38dbb03..0499b572ef 100644 --- a/sim/app/w/[id]/components/control-bar/components/deployment-controls/deployment-controls.tsx +++ b/sim/app/w/[id]/components/control-bar/components/deployment-controls/deployment-controls.tsx @@ -102,13 +102,11 @@ export function DeploymentControls({ }, { label: 'API Key', - content: apiKey || 'No API key found. Visit your account settings to create one.', + content: apiKey, }, { label: 'Example curl command', - content: apiKey - ? `curl -X POST -H "X-API-Key: ${apiKey}" -H "Content-Type: application/json"${inputFormatExample} ${endpoint}` - : `You need an API key to call this endpoint. Visit your account settings to create one.`, + content: `curl -X POST -H "X-API-Key: ${apiKey}" -H "Content-Type: application/json"${inputFormatExample} ${endpoint}`, }, ], needsRedeployment,