File size: 2,220 Bytes
eeb9404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/auth/session';
import { taskManager, eventBus } from '@/lib/server-generate/singleton';
import { runServerGeneration } from '@/lib/server-generate/server-orchestrator-runner';
import type { StartGenerationRequest } from '@/lib/server-generate/types';

export async function POST(request: NextRequest) {
  const session = await getSession();
  if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

  let body: StartGenerationRequest;
  try {
    body = await request.json();
  } catch {
    return NextResponse.json({ error: 'Invalid request body' }, { status: 400 });
  }

  if (!body.projectId || !body.prompt || !body.model || !body.apiKey) {
    return NextResponse.json(
      { error: 'Missing required fields: projectId, prompt, model, apiKey' },
      { status: 400 },
    );
  }

  const sessionId = session.userId;
  const workspaceId = body.workspaceId;

  let taskId: string;
  try {
    taskId = taskManager.createTask(body.projectId, sessionId, body.apiKey, workspaceId);
  } catch (error) {
    if (error instanceof Error && error.message.includes('limit')) {
      return NextResponse.json({ error: error.message }, { status: 429 });
    }
    throw error;
  }

  const task = taskManager.getTask(taskId)!;
  task.prompt = body.prompt;
  task.model = body.model;
  task.projectName = body.projectName;

  const port = process.env.PORT || '3000';
  const apiBaseUrl = `http://localhost:${port}`;

  // Run generation in background — don't await
  runServerGeneration(taskId, body, {
    taskManager,
    eventBus,
    createVFS: async (_projectId) => {
      const { VirtualFileSystem } = await import('@/lib/vfs');
      const { getWorkspaceAdapter, getSQLiteAdapter } = await import('@/lib/vfs/adapters/server');
      const adapter = workspaceId ? getWorkspaceAdapter(workspaceId) : getSQLiteAdapter();
      const serverVFS = new VirtualFileSystem(adapter);
      await serverVFS.init();
      return serverVFS;
    },
    apiBaseUrl,
  }).catch((err) => {
    console.error(`Server generation failed for task ${taskId}:`, err);
  });

  return NextResponse.json({ taskId });
}