File size: 4,995 Bytes
60e766b
 
 
346d937
60e766b
614d3ec
 
 
 
 
60e766b
614d3ec
 
60e766b
 
 
614d3ec
60e766b
 
 
 
 
 
 
346d937
3590954
 
 
346d937
 
 
3590954
 
 
 
346d937
 
3590954
 
 
 
346d937
 
3590954
 
 
346d937
 
 
3590954
 
 
 
346d937
 
3590954
 
 
 
346d937
 
3590954
 
 
346d937
 
60e766b
 
346d937
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3590954
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e766b
 
 
 
 
 
 
 
 
 
 
 
 
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
import OpenAI from "openai";
import { generatedSpecSchema, type GeneratedSpec } from "@/lib/spec-schema";

const openai = process.env.OPENAI_API_KEY ? new OpenAI({ apiKey: process.env.OPENAI_API_KEY }) : null;

function fallbackSpec(
  rawInputText: string,
  _platform?: string | null,
  _targetUsers?: string | null,
): GeneratedSpec {
  const title = rawInputText.slice(0, 80) || "Community Idea";
  const problemStatement =
    rawInputText.trim() || "Turn the submitted idea into a practical, low-friction software product.";

  return {
    title,
    problem_statement: problemStatement,
    tags: ["community", "spec"],
    features: [
      "Idea submission form with optional context fields",
      "LLM-based requirement generation",
      "Public idea detail page with structured sections",
    ],
    tasks: [
      {
        title: "Platform architecture and stack decisions",
        description: "Define system boundaries, data model shape, and deployment strategy.",
        acceptance_criteria: ["Architecture doc covers frontend, API, data, hosting"],
        effort: "M",
      },
      {
        title: "Core idea lifecycle implementation",
        description: "Implement create, browse, detail, and deletion lifecycle for ideas.",
        acceptance_criteria: ["Users can submit and manage idea threads end-to-end"],
        effort: "L",
      },
      {
        title: "Community interaction layer",
        description: "Support upvotes, discussion comments, and moderation-safe posting.",
        acceptance_criteria: ["Voting and commenting are stable and abuse-guarded"],
        effort: "M",
      },
      {
        title: "Developer collaboration workflow",
        description: "Support task claiming, working signals, and progress status updates.",
        acceptance_criteria: ["Developers can coordinate without account friction"],
        effort: "M",
      },
      {
        title: "Solution submission and evaluation",
        description: "Provide solution posting, feedback votes/comments, and submitter approval flow.",
        acceptance_criteria: ["Approved solutions are clearly visible in the thread"],
        effort: "M",
      },
      {
        title: "Authentication and identity policy",
        description: "Establish anonymous defaults and optional authenticated identity actions.",
        acceptance_criteria: ["Access checks are enforced across APIs and UI"],
        effort: "M",
      },
      {
        title: "Quality, monitoring, and release readiness",
        description: "Add validation, error handling, and basic operational diagnostics for production.",
        acceptance_criteria: ["Critical paths have test coverage and observable logs"],
        effort: "M",
      },
    ],
    open_questions: [
      "Should submitters be able to edit/delete ideas in this version?",
      "Do you want to add similar-ideas matching via embeddings now or later?",
    ],
  };
}

function promptForSpec(input: {
  raw_input_text: string;
  target_users?: string;
  platform?: string;
  constraints?: string;
}): string {
  return [
    "Turn the idea into a buildable product specification JSON.",
    "Return strict JSON only.",
    "Rules:",
    "- 3 to 7 features",
    "- 6 to 12 general implementation tasks (workstreams, not tiny subtasks)",
    "- each task should be outcome-oriented and broad enough for a developer to own",
    "- do not hallucinate external integrations unless requested",
    "- place missing details in open_questions",
    "Input:",
    JSON.stringify(input),
  ].join("\n");
}

export async function generateSpec(input: {
  raw_input_text: string;
  target_users?: string;
  platform?: string;
  constraints?: string;
}): Promise<GeneratedSpec> {
  if (!openai) {
    return fallbackSpec(input.raw_input_text, input.platform, input.target_users);
  }

  const model = process.env.OPENAI_MODEL || "gpt-4.1-mini";
  const basePrompt = promptForSpec(input);

  for (let attempt = 0; attempt < 2; attempt += 1) {
    const prompt = attempt === 0 ? basePrompt : `${basePrompt}\nFix output as valid JSON matching required schema.`;

    let content: string | null = null;

    try {
      const response = await openai.chat.completions.create({
        model,
        temperature: 0.2,
        messages: [
          {
            role: "system",
            content: "You are a product requirements assistant. Always output valid JSON only, no markdown.",
          },
          { role: "user", content: prompt },
        ],
        response_format: { type: "json_object" },
      });

      content = response.choices[0]?.message?.content ?? null;
    } catch {
      // Network/auth/provider errors should not break idea submission.
      continue;
    }

    if (!content) continue;

    try {
      const parsed = JSON.parse(content);
      return generatedSpecSchema.parse(parsed);
    } catch {
      continue;
    }
  }

  return fallbackSpec(input.raw_input_text, input.platform, input.target_users);
}