everydaycats commited on
Commit
3443915
·
verified ·
1 Parent(s): d545d57

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +52 -25
app.js CHANGED
@@ -67,9 +67,8 @@ function extractImagePrompt(text) {
67
  }
68
 
69
  // --- ONBOARDING ENDPOINTS ---
70
-
71
  /**
72
- * 1. ANALYZE (User Idea -> Questions)
73
  */
74
  app.post('/onboarding/analyze', validateRequest, async (req, res) => {
75
  const { description } = req.body;
@@ -77,8 +76,18 @@ app.post('/onboarding/analyze', validateRequest, async (req, res) => {
77
 
78
  try {
79
  console.log(`[Onboarding] Analyzing idea...`);
80
- const questions = await AIEngine.generateEntryQuestions(description);
81
- res.json({ questions });
 
 
 
 
 
 
 
 
 
 
82
  } catch (err) {
83
  console.error(err);
84
  res.status(500).json({ error: "Analysis failed" });
@@ -86,46 +95,64 @@ app.post('/onboarding/analyze', validateRequest, async (req, res) => {
86
  });
87
 
88
  /**
89
- * 2. CREATE (User Answers -> Grade + Thumbnail -> Save)
90
  */
91
  app.post('/onboarding/create', validateRequest, async (req, res) => {
92
  const { userId, description, answers } = req.body;
93
  const projectId = "proj_" + Date.now();
94
 
95
  try {
96
- console.log(`[Onboarding] Creating Project ${projectId}...`);
97
 
98
- // Run Grading and Image Gen in Parallel
99
- const [grading, thumbnailBase64] = await Promise.all([
100
- AIEngine.gradeProject(description, answers),
101
- AIEngine.generateImage(`Roblox game thumbnail, cinematic, high quality: ${description}`)
102
- ]);
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  const projectData = {
105
  id: projectId,
106
  userId,
 
107
  description,
108
  answers,
109
  stats: grading,
110
- thumbnail: thumbnailBase64, // ? `data:image/png;base64,${thumbnailBase64}` : null,
111
  createdAt: Date.now(),
112
- status: "initialized"
113
  };
114
 
115
- // Save to Firebase
116
- if (db) {
117
- await db.ref(`projects/${projectId}`).set(projectData);
118
- }
119
 
120
- // Also Init StateManager so it's ready for /new/project call later
121
- await StateManager.updateProject(projectId, {
122
- ...projectData,
123
- workerHistory: [],
124
- pmHistory: [],
125
- failureCount: 0
126
- });
 
 
127
 
128
- res.json({ success: true, projectId, stats: grading, thumbnail: projectData.thumbnail });
 
 
 
 
 
 
129
 
130
  } catch (err) {
131
  console.error("Create Error:", err);
 
67
  }
68
 
69
  // --- ONBOARDING ENDPOINTS ---
 
70
  /**
71
+ * 1. ANALYZE (Initial Gatekeeper)
72
  */
73
  app.post('/onboarding/analyze', validateRequest, async (req, res) => {
74
  const { description } = req.body;
 
76
 
77
  try {
78
  console.log(`[Onboarding] Analyzing idea...`);
79
+ const result = await AIEngine.generateEntryQuestions(description);
80
+
81
+ // CHECK 1: Immediate Rejection
82
+ if (result.status === "REJECTED") {
83
+ return res.json({
84
+ rejected: true,
85
+ reason: result.reason || "Idea violates TOS or guidelines."
86
+ });
87
+ }
88
+
89
+ // If Accepted, send questions
90
+ res.json({ questions: result.questions });
91
  } catch (err) {
92
  console.error(err);
93
  res.status(500).json({ error: "Analysis failed" });
 
95
  });
96
 
97
  /**
98
+ * 2. CREATE (Final Gatekeeper)
99
  */
100
  app.post('/onboarding/create', validateRequest, async (req, res) => {
101
  const { userId, description, answers } = req.body;
102
  const projectId = "proj_" + Date.now();
103
 
104
  try {
105
+ console.log(`[Onboarding] Grading Project ${projectId}...`);
106
 
107
+ // STEP 1: GRADE FIRST (Don't generate image yet)
108
+ const grading = await AIEngine.gradeProject(description, answers);
109
+
110
+ // STEP 2: CHECK FAIL CONDITIONS
111
+ // Fail if Feasibility < 40 OR Rating is D or F
112
+ const isFailure = grading.feasibility < 40 || ['D', 'F'].includes(grading.rating);
113
+
114
+ let thumbnailBase64 = null;
115
+
116
+ if (isFailure) {
117
+ console.log(`[Onboarding] ❌ Project Failed Grading (${grading.rating}). Skipping Image.`);
118
+ } else {
119
+ // STEP 3: GENERATE IMAGE ONLY IF PASSED
120
+ console.log(`[Onboarding] ✅ Passed. Generating Thumbnail...`);
121
+ thumbnailBase64 = await AIEngine.generateImage(`Roblox game thumbnail, cinematic: ${grading.title || description}`);
122
+ }
123
 
124
  const projectData = {
125
  id: projectId,
126
  userId,
127
+ title: grading.title || "Untitled Project",
128
  description,
129
  answers,
130
  stats: grading,
131
+ thumbnail: thumbnailBase64 ? `data:image/png;base64,${thumbnailBase64}` : null,
132
  createdAt: Date.now(),
133
+ status: isFailure ? "rejected" : "initialized"
134
  };
135
 
136
+ // Save (even failed ones, for history, or you can skip)
137
+ if (db) await db.ref(`projects/${projectId}`).set(projectData);
 
 
138
 
139
+ // Only load into memory if it passed
140
+ if (!isFailure) {
141
+ await StateManager.updateProject(projectId, {
142
+ ...projectData,
143
+ workerHistory: [],
144
+ pmHistory: [],
145
+ failureCount: 0
146
+ });
147
+ }
148
 
149
+ res.json({
150
+ success: !isFailure, // Frontend uses this to toggle Red X
151
+ projectId,
152
+ stats: grading,
153
+ title: projectData.title,
154
+ thumbnail: projectData.thumbnail
155
+ });
156
 
157
  } catch (err) {
158
  console.error("Create Error:", err);