File size: 6,113 Bytes
8e944c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getAnonId } from "@/lib/anon-id";
import { rateLimit } from "@/lib/rate-limit";
import { submitIdeaSchema, specSchema } from "@/lib/validators";
import { llm, LLM_MODEL } from "@/lib/llm";
import { IDEA_TO_SPEC_SYSTEM_PROMPT, buildIdeaPrompt } from "@/lib/prompts";
import { generateEmbedding } from "@/lib/embedding";
import { hotScore } from "@/lib/hot-score";

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams;
  const sort = searchParams.get("sort") || "hot";
  const search = searchParams.get("search") || "";
  const page = Math.max(1, parseInt(searchParams.get("page") || "1"));
  const limit = Math.min(50, Math.max(1, parseInt(searchParams.get("limit") || "20")));

  const where = search
    ? {
        OR: [
          { title: { contains: search } },
          { problemStatement: { contains: search } },
        ],
      }
    : {};

  if (sort === "new") {
    const [ideas, total] = await Promise.all([
      prisma.idea.findMany({
        where,
        orderBy: { createdAt: "desc" },
        skip: (page - 1) * limit,
        take: limit,
        include: {
          tasks: { select: { id: true, status: true } },
        },
      }),
      prisma.idea.count({ where }),
    ]);

    return NextResponse.json({
      ideas: ideas.map(serializeIdea),
      total,
      page,
      totalPages: Math.ceil(total / limit),
    });
  }

  // Hot sort: fetch all, compute score, sort in JS
  const ideas = await prisma.idea.findMany({
    where,
    include: {
      tasks: { select: { id: true, status: true } },
    },
  });

  const scored = ideas
    .map((idea) => ({
      ...idea,
      _hotScore: hotScore(idea.upvotesCount, idea.createdAt),
    }))
    .sort((a, b) => b._hotScore - a._hotScore);

  const total = scored.length;
  const paginated = scored.slice((page - 1) * limit, page * limit);

  return NextResponse.json({
    ideas: paginated.map(serializeIdea),
    total,
    page,
    totalPages: Math.ceil(total / limit),
  });
}

export async function POST(request: NextRequest) {
  const anonId = await getAnonId();

  // Rate limit by anon_id
  const rl = rateLimit(`idea:${anonId}`, 5);
  if (!rl.success) {
    return NextResponse.json(
      { error: { message: "Rate limited. Max 5 ideas per hour.", code: "RATE_LIMITED" } },
      { status: 429 }
    );
  }

  // Also rate limit by IP
  const ip = request.headers.get("x-forwarded-for") || "unknown";
  const rlIp = rateLimit(`idea:ip:${ip}`, 10);
  if (!rlIp.success) {
    return NextResponse.json(
      { error: { message: "Rate limited by IP.", code: "RATE_LIMITED" } },
      { status: 429 }
    );
  }

  const body = await request.json();
  const parsed = submitIdeaSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json(
      { error: { message: "Validation failed", code: "VALIDATION_ERROR", details: parsed.error.issues } },
      { status: 400 }
    );
  }

  const { rawInputText, targetUsers, platform, constraints } = parsed.data;
  const userPrompt = buildIdeaPrompt(rawInputText, targetUsers, platform, constraints);

  // Generate spec via LLM
  let spec;
  try {
    spec = await generateSpec(userPrompt);
  } catch (error) {
    console.error("LLM spec generation failed:", error);
    return NextResponse.json(
      { error: { message: "Failed to generate specification. Please try again.", code: "LLM_ERROR" } },
      { status: 502 }
    );
  }

  // Generate embedding (non-blocking failure)
  const embedding = await generateEmbedding(rawInputText);

  // Create idea + tasks in a transaction
  const idea = await prisma.$transaction(async (tx) => {
    const idea = await tx.idea.create({
      data: {
        createdByAnonId: anonId,
        rawInputText,
        title: spec.title,
        problemStatement: spec.problemStatement,
        tags: JSON.stringify(spec.tags),
        features: JSON.stringify(spec.features),
        openQuestions: JSON.stringify(spec.openQuestions),
        embedding: embedding ? JSON.stringify(embedding) : null,
      },
    });

    if (spec.tasks.length > 0) {
      await tx.task.createMany({
        data: spec.tasks.map((t) => ({
          ideaId: idea.id,
          title: t.title,
          description: t.description,
          acceptanceCriteria: t.acceptanceCriteria,
          effort: t.effort,
        })),
      });
    }

    return idea;
  });

  const created = await prisma.idea.findUnique({
    where: { id: idea.id },
    include: { tasks: true },
  });

  return NextResponse.json({ data: serializeIdea(created!) }, { status: 201 });
}

async function generateSpec(userPrompt: string, retries = 1) {
  for (let attempt = 0; attempt <= retries; attempt++) {
    const completion = await llm.chat.completions.create({
      model: LLM_MODEL,
      messages: [
        { role: "system", content: IDEA_TO_SPEC_SYSTEM_PROMPT },
        { role: "user", content: userPrompt },
      ],
      temperature: 0.7,
    });

    const content = completion.choices[0].message.content;
    if (!content) throw new Error("Empty LLM response");

    try {
      // Strip markdown fences if present
      const cleaned = content.replace(/^```json?\s*\n?/i, "").replace(/\n?```\s*$/i, "");
      const parsed = JSON.parse(cleaned);
      return specSchema.parse(parsed);
    } catch (err) {
      if (attempt === retries) throw err;
      // Retry with a fix prompt
      console.warn("Invalid spec JSON, retrying...", err);
    }
  }
  throw new Error("Failed to generate valid spec");
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function serializeIdea(idea: any) {
  return {
    id: idea.id,
    createdAt: idea.createdAt,
    title: idea.title,
    problemStatement: idea.problemStatement,
    tags: JSON.parse(idea.tags || "[]"),
    features: JSON.parse(idea.features || "[]"),
    openQuestions: JSON.parse(idea.openQuestions || "[]"),
    upvotesCount: idea.upvotesCount,
    commentsCount: idea.commentsCount,
    tasks: idea.tasks || [],
  };
}