everydaycats commited on
Commit
7df4949
·
verified ·
1 Parent(s): 69029e9

Create aiEngine.js

Browse files
Files changed (1) hide show
  1. aiEngine.js +105 -0
aiEngine.js ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { GoogleGenAI } from '@google/genai';
2
+ import fs from 'fs';
3
+
4
+ // Initialize SDK
5
+ const genAI = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
6
+ const prompts = JSON.parse(fs.readFileSync('./prompts.json', 'utf8'));
7
+
8
+ export const AIEngine = {
9
+ /**
10
+ * 1. PROJECT MANAGER (Reasoning & Delegation)
11
+ */
12
+ callPM: async (history, input) => {
13
+ const modelId = 'gemini-2.0-pro-exp-02-05';
14
+ const config = {
15
+ thinkingConfig: { thinkingLevel: 'HIGH' },
16
+ tools: [{ googleSearch: {} }],
17
+ systemInstruction: { parts: [{ text: prompts.pm_system_prompt }] }
18
+ };
19
+
20
+ const contents = [...history, { role: 'user', parts: [{ text: input }] }];
21
+
22
+ try {
23
+ const response = await genAI.models.generateContent({ model: modelId, config, contents });
24
+ return response.text();
25
+ } catch (error) {
26
+ console.error("PM AI Error:", error);
27
+ throw error;
28
+ }
29
+ },
30
+
31
+ /**
32
+ * 2. WORKER (Coding & Execution)
33
+ */
34
+ callWorker: async (history, input, images = []) => {
35
+ /* const modelId = 'gemini-2.0-flash';
36
+ const config = {
37
+ tools: [{ googleSearch: {} }],
38
+ systemInstruction: { parts: [{ text: prompts.worker_system_prompt }] }
39
+ };
40
+ */
41
+ const modelId = 'gemini-2.0-pro-exp-02-05';
42
+ const config = {
43
+ thinkingConfig: { thinkingLevel: 'HIGH' },
44
+ tools: [{ googleSearch: {} }],
45
+ systemInstruction: { parts: [{ text: prompts.worker_system_prompt }] }
46
+ };
47
+
48
+
49
+ const currentParts = [{ text: input }];
50
+
51
+ // Process incoming images from Roblox to the AI
52
+ if (images && images.length > 0) {
53
+ images.forEach(base64String => {
54
+ const cleanData = base64String.replace(/^data:image\/\w+;base64,/, "");
55
+ currentParts.push({
56
+ inlineData: { mimeType: "image/png", data: cleanData }
57
+ });
58
+ });
59
+ }
60
+
61
+ const contents = [...history, { role: 'user', parts: currentParts }];
62
+
63
+ try {
64
+ const response = await genAI.models.generateContent({ model: modelId, config, contents });
65
+ return response.text();
66
+ } catch (error) {
67
+ console.error("Worker AI Error:", error);
68
+ throw error;
69
+ }
70
+ },
71
+
72
+ /**
73
+ * 3. IMAGE GENERATOR (Visual Assets)
74
+ * Uses Imagen 3 to generate textures/UI assets
75
+ */
76
+ generateImage: async (prompt) => {
77
+ const modelId = 'imagen-3.0-generate-001';
78
+
79
+ try {
80
+ console.log(`[AIEngine] Generating Image for: "${prompt}"...`);
81
+ const response = await genAI.models.generateImages({
82
+ model: modelId,
83
+ prompt: prompt,
84
+ config: {
85
+ numberOfImages: 1,
86
+ aspectRatio: "1:1", // Standard for textures/icons
87
+ outputMimeType: "image/png" // Roblox supports PNG
88
+ }
89
+ });
90
+
91
+ // The SDK returns the image data directly in the response
92
+ const image = response.generatedImages[0];
93
+
94
+ if (!image || !image.image) {
95
+ throw new Error("No image data received from Imagen.");
96
+ }
97
+ console.log("Yay, image generated")
98
+ // Return Base64 string
99
+ return image.image.toString('base64');
100
+ } catch (error) {
101
+ console.error("Image Gen Error:", error);
102
+ return null; // Fail gracefully so the server doesn't crash
103
+ }
104
+ }
105
+ };