Spaces:
Sleeping
Sleeping
Delete aiEngine.js
Browse files- aiEngine.js +0 -248
aiEngine.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
| 1 |
-
import { GoogleGenAI } from '@google/genai';
|
| 2 |
-
import fs from 'fs';
|
| 3 |
-
import path from 'path';
|
| 4 |
-
import mime from 'mime';
|
| 5 |
-
|
| 6 |
-
// Load prompts safely
|
| 7 |
-
const promptsPath = path.resolve('./prompts.json');
|
| 8 |
-
const prompts = JSON.parse(fs.readFileSync(promptsPath, 'utf8'));
|
| 9 |
-
|
| 10 |
-
// Initialize SDK
|
| 11 |
-
// Make sure process.env.GEMINI_API_KEY is set in your environment
|
| 12 |
-
const genAI = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
|
| 13 |
-
|
| 14 |
-
export const AIEngine = {
|
| 15 |
-
/**
|
| 16 |
-
* 1. PROJECT MANAGER (Reasoning & Delegation)
|
| 17 |
-
* Uses High-Reasoning Model
|
| 18 |
-
*/
|
| 19 |
-
callPM: async (history, input) => {
|
| 20 |
-
const modelId = 'gemini-3-pro-preview';
|
| 21 |
-
|
| 22 |
-
const config = {
|
| 23 |
-
thinkingConfig: { thinkingLevel: 'HIGH' },
|
| 24 |
-
tools: [{ googleSearch: {} }],
|
| 25 |
-
systemInstruction: {
|
| 26 |
-
parts: [{ text: prompts.pm_system_prompt }]
|
| 27 |
-
}
|
| 28 |
-
};
|
| 29 |
-
|
| 30 |
-
const contents = [
|
| 31 |
-
...history,
|
| 32 |
-
{ role: 'user', parts: [{ text: input }] }
|
| 33 |
-
];
|
| 34 |
-
|
| 35 |
-
try {
|
| 36 |
-
const response = await genAI.models.generateContent({
|
| 37 |
-
model: modelId,
|
| 38 |
-
config,
|
| 39 |
-
contents,
|
| 40 |
-
});
|
| 41 |
-
|
| 42 |
-
console.log(response.usageMetadata.total_token_count);
|
| 43 |
-
|
| 44 |
-
// Return both text and usage metadata
|
| 45 |
-
return {
|
| 46 |
-
text: response.text,
|
| 47 |
-
usage: response.usageMetadata
|
| 48 |
-
};
|
| 49 |
-
|
| 50 |
-
} catch (error) {
|
| 51 |
-
console.error("PM AI Error:", error);
|
| 52 |
-
throw error;
|
| 53 |
-
}
|
| 54 |
-
},
|
| 55 |
-
|
| 56 |
-
/**
|
| 57 |
-
* 2. WORKER (Coding & Execution)
|
| 58 |
-
* Uses Flash Model (Fast) + Image Support
|
| 59 |
-
*/
|
| 60 |
-
callWorker: async (history, input, images = []) => {
|
| 61 |
-
const modelId = "gemini-3-flash-preview"; // 'gemini-3-pro-preview';
|
| 62 |
-
const config = {
|
| 63 |
-
thinkingConfig: { thinkingLevel: 'HIGH' },
|
| 64 |
-
// tools: [{ googleSearch: {} }],
|
| 65 |
-
systemInstruction: {
|
| 66 |
-
parts: [{ text: prompts.worker_system_prompt }]
|
| 67 |
-
}
|
| 68 |
-
};
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
const currentParts = [{ text: input }];
|
| 72 |
-
|
| 73 |
-
// Handle Image Injection (Base64)
|
| 74 |
-
if (images && images.length > 0) {
|
| 75 |
-
images.forEach(base64String => {
|
| 76 |
-
// Strip prefix if present
|
| 77 |
-
const cleanData = base64String.replace(/^data:image\/\w+;base64,/, "");
|
| 78 |
-
currentParts.push({
|
| 79 |
-
inlineData: {
|
| 80 |
-
mimeType: "image/png",
|
| 81 |
-
data: cleanData
|
| 82 |
-
}
|
| 83 |
-
});
|
| 84 |
-
});
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
const contents = [
|
| 88 |
-
...history,
|
| 89 |
-
{ role: 'user', parts: currentParts }
|
| 90 |
-
];
|
| 91 |
-
|
| 92 |
-
try {
|
| 93 |
-
const response = await genAI.models.generateContent({
|
| 94 |
-
model: modelId,
|
| 95 |
-
config,
|
| 96 |
-
contents,
|
| 97 |
-
});
|
| 98 |
-
|
| 99 |
-
console.log(response.usageMetadata.total_token_count);
|
| 100 |
-
|
| 101 |
-
// Return both text and usage metadata
|
| 102 |
-
return {
|
| 103 |
-
text: response.text,
|
| 104 |
-
usage: response.usageMetadata
|
| 105 |
-
};
|
| 106 |
-
|
| 107 |
-
} catch (error) {
|
| 108 |
-
console.error("Worker AI Error:", error);
|
| 109 |
-
throw error;
|
| 110 |
-
}
|
| 111 |
-
},
|
| 112 |
-
|
| 113 |
-
/**
|
| 114 |
-
* 3. ONBOARDING ANALYST (Question Generation)
|
| 115 |
-
* Returns STRICT JSON for the Frontend
|
| 116 |
-
*/
|
| 117 |
-
generateEntryQuestions: async (description) => {
|
| 118 |
-
const modelId = "gemini-3-flash-preview"; // 'gemini-2.5-flash';
|
| 119 |
-
// Using the updated prompt which handles REJECTED/ACCEPTED logic
|
| 120 |
-
const input = `[MODE 1: QUESTIONS]\nAnalyze this game idea: "${description}". Check for TOS violations or nonsense. If good, ask 3 questions. Output ONLY raw JSON.`;
|
| 121 |
-
|
| 122 |
-
try {
|
| 123 |
-
const response = await genAI.models.generateContent({
|
| 124 |
-
model: modelId,
|
| 125 |
-
config: {
|
| 126 |
-
responseMimeType: "application/json",
|
| 127 |
-
systemInstruction: { parts: [{ text: prompts.analyst_system_prompt }] }
|
| 128 |
-
},
|
| 129 |
-
contents: [{ role: 'user', parts: [{ text: input }] }]
|
| 130 |
-
});
|
| 131 |
-
|
| 132 |
-
const text = response.text;
|
| 133 |
-
const parsed = JSON.parse(text);
|
| 134 |
-
|
| 135 |
-
console.log(response.usageMetadata.total_token_count)
|
| 136 |
-
|
| 137 |
-
// Attach usage to the JSON object
|
| 138 |
-
return {
|
| 139 |
-
...parsed,
|
| 140 |
-
usage: response.usageMetadata
|
| 141 |
-
};
|
| 142 |
-
|
| 143 |
-
} catch (e) {
|
| 144 |
-
console.error("Analyst Error:", e);
|
| 145 |
-
// On failure, we don't return usage, so no charge applies
|
| 146 |
-
// return { status: "ACCEPTED", questions: [{ id: "fallback", label: "Please describe the core gameplay loop in detail.", type: "textarea" }] };
|
| 147 |
-
throw e;
|
| 148 |
-
}
|
| 149 |
-
},
|
| 150 |
-
|
| 151 |
-
/**
|
| 152 |
-
* 4. PROJECT GRADER (Feasibility Check)
|
| 153 |
-
* Returns STRICT JSON
|
| 154 |
-
*/
|
| 155 |
-
gradeProject: async (description, answers) => {
|
| 156 |
-
const modelId = "gemini-3-flash-preview"; // 'gemini-2.5-flash';
|
| 157 |
-
// Using the updated prompt to respect Title and relaxed Grading
|
| 158 |
-
const input = `[MODE 2: GRADING]\nIdea: "${description}"\nUser Answers: ${JSON.stringify(answers)}\n\nAssess feasibility. Output JSON with title and rating.`;
|
| 159 |
-
|
| 160 |
-
try {
|
| 161 |
-
const response = await genAI.models.generateContent({
|
| 162 |
-
model: modelId,
|
| 163 |
-
config: {
|
| 164 |
-
responseMimeType: "application/json",
|
| 165 |
-
systemInstruction: { parts: [{ text: prompts.analyst_system_prompt }] }
|
| 166 |
-
},
|
| 167 |
-
contents: [{ role: 'user', parts: [{ text: input }] }]
|
| 168 |
-
});
|
| 169 |
-
|
| 170 |
-
const parsed = JSON.parse(response.text);
|
| 171 |
-
console.log(response.usageMetadata.total_token_count);
|
| 172 |
-
|
| 173 |
-
// Attach usage to the JSON object
|
| 174 |
-
return {
|
| 175 |
-
...parsed,
|
| 176 |
-
usage: response.usageMetadata
|
| 177 |
-
};
|
| 178 |
-
|
| 179 |
-
} catch (e) {
|
| 180 |
-
console.error("Grading Error:", e);
|
| 181 |
-
// On failure, no usage returned
|
| 182 |
-
// return { feasibility: 80, rating: "B", title: "Untitled Project", summary: "Standard project structure detected." };
|
| 183 |
-
throw e;
|
| 184 |
-
}
|
| 185 |
-
},
|
| 186 |
-
|
| 187 |
-
/**
|
| 188 |
-
* 5. IMAGE GENERATOR (Visual Assets)
|
| 189 |
-
* Uses Gemini 2.5 Flash Image with Stream (Correct Implementation)
|
| 190 |
-
*/
|
| 191 |
-
generateImage: async (prompt) => {
|
| 192 |
-
// Inject the prompt template from JSON to ensure adherence to instructions
|
| 193 |
-
const finalPrompt = prompts.image_gen_prompt.replace('{{DESCRIPTION}}', prompt);
|
| 194 |
-
|
| 195 |
-
const config = {
|
| 196 |
-
responseModalities: ['IMAGE', 'TEXT'],
|
| 197 |
-
};
|
| 198 |
-
const model = 'gemini-2.5-flash-image';
|
| 199 |
-
const contents = [
|
| 200 |
-
{
|
| 201 |
-
role: 'user',
|
| 202 |
-
parts: [{ text: finalPrompt }],
|
| 203 |
-
},
|
| 204 |
-
];
|
| 205 |
-
|
| 206 |
-
try {
|
| 207 |
-
const response = await genAI.models.generateContentStream({
|
| 208 |
-
model,
|
| 209 |
-
config,
|
| 210 |
-
contents,
|
| 211 |
-
});
|
| 212 |
-
|
| 213 |
-
let finalDataUrl = null;
|
| 214 |
-
|
| 215 |
-
for await (const chunk of response) {
|
| 216 |
-
if (!chunk.candidates || !chunk.candidates[0].content || !chunk.candidates[0].content.parts) {
|
| 217 |
-
continue;
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
// Capture image data if present
|
| 221 |
-
if (chunk.candidates?.[0]?.content?.parts?.[0]?.inlineData) {
|
| 222 |
-
const inlineData = chunk.candidates[0].content.parts[0].inlineData;
|
| 223 |
-
const rawB64 = (inlineData.data || "").replace(/\s+/g, "");
|
| 224 |
-
const mimeType = inlineData.mimeType || "image/png";
|
| 225 |
-
const buffer = Buffer.from(rawB64, "base64");
|
| 226 |
-
const base64 = buffer.toString("base64");
|
| 227 |
-
|
| 228 |
-
finalDataUrl = `data:${mimeType};base64,${base64}`;
|
| 229 |
-
// We do NOT return here immediately, we continue to allow the stream to finish
|
| 230 |
-
// so we can access the aggregated usage metadata at the end.
|
| 231 |
-
}
|
| 232 |
-
}
|
| 233 |
-
|
| 234 |
-
// Retrieve the full response object to get usage metadata
|
| 235 |
-
const aggregatedResponse = await response.response;
|
| 236 |
-
// console.log(aggregatedResponse.usageMetadata.total_token_count);
|
| 237 |
-
return {
|
| 238 |
-
image: finalDataUrl,
|
| 239 |
-
usage: 2000// aggregatedResponse.usageMetadata
|
| 240 |
-
};
|
| 241 |
-
|
| 242 |
-
} catch (error) {
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