Spaces:
Running
Running
Update aiEngine.js
Browse files- aiEngine.js +30 -17
aiEngine.js
CHANGED
|
@@ -35,12 +35,11 @@ const azureOpenAI = new OpenAI({
|
|
| 35 |
});
|
| 36 |
|
| 37 |
// --- HELPER: History Transformer ---
|
| 38 |
-
// Converts your App's { role: 'user', parts: [{text: ''}] } format
|
| 39 |
-
// to formats acceptable by Azure/Bedrock
|
| 40 |
function toAzureHistory(history) {
|
| 41 |
return history.map(h => ({
|
| 42 |
role: h.role === 'model' ? 'assistant' : 'user',
|
| 43 |
-
|
|
|
|
| 44 |
}));
|
| 45 |
}
|
| 46 |
|
|
@@ -57,7 +56,6 @@ export const AIEngine = {
|
|
| 57 |
* Uses AWS Bedrock -> Claude 3.5 Sonnet (v2/4.6)
|
| 58 |
*/
|
| 59 |
callPM: async (history, input) => {
|
| 60 |
-
// Prepare History
|
| 61 |
const chatHistory = toBedrockHistory(history);
|
| 62 |
const sysPrompt = prompts.pm_system_prompt || "You are a Senior Project Manager.";
|
| 63 |
|
|
@@ -76,7 +74,6 @@ export const AIEngine = {
|
|
| 76 |
const response = await bedrockClient.send(command);
|
| 77 |
const text = response.output.message.content.find(b => b.text)?.text;
|
| 78 |
|
| 79 |
-
// Extract usage
|
| 80 |
const inputTokens = response.usage?.inputTokens || 0;
|
| 81 |
const outputTokens = response.usage?.outputTokens || 0;
|
| 82 |
|
|
@@ -95,22 +92,44 @@ export const AIEngine = {
|
|
| 95 |
/**
|
| 96 |
* 2. WORKER (Coding & Execution)
|
| 97 |
* Uses Azure OpenAI -> GPT-5 Mini
|
|
|
|
| 98 |
*/
|
| 99 |
callWorker: async (history, input, images = []) => {
|
| 100 |
const chatHistory = toAzureHistory(history);
|
| 101 |
const sysPrompt = prompts.worker_system_prompt || "You are a specialized Worker.";
|
| 102 |
|
| 103 |
-
//
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
try {
|
| 108 |
const response = await azureOpenAI.chat.completions.create({
|
| 109 |
model: process.env.AZURE_DEPLOYMENT_NAME, // "gpt-5-mini"
|
| 110 |
messages: [
|
| 111 |
{ role: "system", content: sysPrompt },
|
| 112 |
...chatHistory,
|
| 113 |
-
{ role: "user", content:
|
| 114 |
],
|
| 115 |
reasoning_effort: "high"
|
| 116 |
});
|
|
@@ -132,7 +151,6 @@ export const AIEngine = {
|
|
| 132 |
|
| 133 |
/**
|
| 134 |
* 3. ONBOARDING ANALYST (Question Generation)
|
| 135 |
-
* Returns STRICT JSON
|
| 136 |
*/
|
| 137 |
generateEntryQuestions: async (description) => {
|
| 138 |
const sysPrompt = prompts.analyst_system_prompt || "Return JSON only.";
|
|
@@ -155,7 +173,6 @@ export const AIEngine = {
|
|
| 155 |
};
|
| 156 |
} catch (e) {
|
| 157 |
console.error("Analyst Error:", e);
|
| 158 |
-
// Fallback structure
|
| 159 |
return {
|
| 160 |
status: "ACCEPTED",
|
| 161 |
questions: [{ id: "fallback", label: "Elaborate?", type: "textarea" }],
|
|
@@ -166,7 +183,6 @@ export const AIEngine = {
|
|
| 166 |
|
| 167 |
/**
|
| 168 |
* 4. PROJECT GRADER (Feasibility Check)
|
| 169 |
-
* Returns STRICT JSON
|
| 170 |
*/
|
| 171 |
gradeProject: async (description, answers) => {
|
| 172 |
const sysPrompt = prompts.analyst_system_prompt || "Return JSON only.";
|
|
@@ -199,12 +215,9 @@ export const AIEngine = {
|
|
| 199 |
},
|
| 200 |
|
| 201 |
/**
|
| 202 |
-
* 5. IMAGE GENERATOR
|
| 203 |
-
* Kept as placeholder since you said "not being used" for now,
|
| 204 |
-
* but prevents crashes if App.js calls it.
|
| 205 |
*/
|
| 206 |
generateImage: async (prompt) => {
|
| 207 |
-
console.log("Image generation currently skipped in Azure/Bedrock migration.");
|
| 208 |
return null;
|
| 209 |
}
|
| 210 |
};
|
|
|
|
| 35 |
});
|
| 36 |
|
| 37 |
// --- HELPER: History Transformer ---
|
|
|
|
|
|
|
| 38 |
function toAzureHistory(history) {
|
| 39 |
return history.map(h => ({
|
| 40 |
role: h.role === 'model' ? 'assistant' : 'user',
|
| 41 |
+
// We only extract text from history to ensure DB remains lightweight
|
| 42 |
+
content: h.parts[0].text
|
| 43 |
}));
|
| 44 |
}
|
| 45 |
|
|
|
|
| 56 |
* Uses AWS Bedrock -> Claude 3.5 Sonnet (v2/4.6)
|
| 57 |
*/
|
| 58 |
callPM: async (history, input) => {
|
|
|
|
| 59 |
const chatHistory = toBedrockHistory(history);
|
| 60 |
const sysPrompt = prompts.pm_system_prompt || "You are a Senior Project Manager.";
|
| 61 |
|
|
|
|
| 74 |
const response = await bedrockClient.send(command);
|
| 75 |
const text = response.output.message.content.find(b => b.text)?.text;
|
| 76 |
|
|
|
|
| 77 |
const inputTokens = response.usage?.inputTokens || 0;
|
| 78 |
const outputTokens = response.usage?.outputTokens || 0;
|
| 79 |
|
|
|
|
| 92 |
/**
|
| 93 |
* 2. WORKER (Coding & Execution)
|
| 94 |
* Uses Azure OpenAI -> GPT-5 Mini
|
| 95 |
+
* UPDATED: Accepts images but does not persist them in history (App.js handles persistence of text only)
|
| 96 |
*/
|
| 97 |
callWorker: async (history, input, images = []) => {
|
| 98 |
const chatHistory = toAzureHistory(history);
|
| 99 |
const sysPrompt = prompts.worker_system_prompt || "You are a specialized Worker.";
|
| 100 |
|
| 101 |
+
// Construct the Current User Message
|
| 102 |
+
let userMessageContent;
|
| 103 |
+
|
| 104 |
+
if (images && images.length > 0) {
|
| 105 |
+
// Multimodal Request
|
| 106 |
+
userMessageContent = [
|
| 107 |
+
{ type: "text", text: input }
|
| 108 |
+
];
|
| 109 |
+
|
| 110 |
+
images.forEach(img => {
|
| 111 |
+
// Ensure the base64 string has the data prefix if missing (or pass as is if client handles it)
|
| 112 |
+
// Assuming client sends "data:image/png;base64,..."
|
| 113 |
+
userMessageContent.push({
|
| 114 |
+
type: "image_url",
|
| 115 |
+
image_url: {
|
| 116 |
+
url: img,
|
| 117 |
+
detail: "auto"
|
| 118 |
+
}
|
| 119 |
+
});
|
| 120 |
+
});
|
| 121 |
+
} else {
|
| 122 |
+
// Text-only Request
|
| 123 |
+
userMessageContent = input;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
try {
|
| 127 |
const response = await azureOpenAI.chat.completions.create({
|
| 128 |
model: process.env.AZURE_DEPLOYMENT_NAME, // "gpt-5-mini"
|
| 129 |
messages: [
|
| 130 |
{ role: "system", content: sysPrompt },
|
| 131 |
...chatHistory,
|
| 132 |
+
{ role: "user", content: userMessageContent }
|
| 133 |
],
|
| 134 |
reasoning_effort: "high"
|
| 135 |
});
|
|
|
|
| 151 |
|
| 152 |
/**
|
| 153 |
* 3. ONBOARDING ANALYST (Question Generation)
|
|
|
|
| 154 |
*/
|
| 155 |
generateEntryQuestions: async (description) => {
|
| 156 |
const sysPrompt = prompts.analyst_system_prompt || "Return JSON only.";
|
|
|
|
| 173 |
};
|
| 174 |
} catch (e) {
|
| 175 |
console.error("Analyst Error:", e);
|
|
|
|
| 176 |
return {
|
| 177 |
status: "ACCEPTED",
|
| 178 |
questions: [{ id: "fallback", label: "Elaborate?", type: "textarea" }],
|
|
|
|
| 183 |
|
| 184 |
/**
|
| 185 |
* 4. PROJECT GRADER (Feasibility Check)
|
|
|
|
| 186 |
*/
|
| 187 |
gradeProject: async (description, answers) => {
|
| 188 |
const sysPrompt = prompts.analyst_system_prompt || "Return JSON only.";
|
|
|
|
| 215 |
},
|
| 216 |
|
| 217 |
/**
|
| 218 |
+
* 5. IMAGE GENERATOR (Placeholder)
|
|
|
|
|
|
|
| 219 |
*/
|
| 220 |
generateImage: async (prompt) => {
|
|
|
|
| 221 |
return null;
|
| 222 |
}
|
| 223 |
};
|