Spaces:
Running
Running
Update services/geminiService.ts
Browse files- services/geminiService.ts +11 -22
services/geminiService.ts
CHANGED
|
@@ -5,24 +5,13 @@ import { LAYER_DEFINITIONS } from '../constants';
|
|
| 5 |
|
| 6 |
// Key Management
|
| 7 |
const getEnvKey = () => {
|
| 8 |
-
//
|
| 9 |
-
//
|
| 10 |
// @ts-ignore
|
| 11 |
-
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.PROD) {
|
| 12 |
-
console.log("Production mode detected: Enforcing BYOK. Environment API keys are ignored.");
|
| 13 |
-
return '';
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
// 1. Try Vite standard env var (Matches Dockerfile VITE_ prefix) - Development Only
|
| 17 |
-
// @ts-ignore - import.meta is standard in Vite but TS might complain without config
|
| 18 |
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.VITE_GEMINI_API_KEY) {
|
| 19 |
// @ts-ignore
|
| 20 |
return import.meta.env.VITE_GEMINI_API_KEY;
|
| 21 |
}
|
| 22 |
-
// 2. Fallback to process.env (Safety check to prevent "process is not defined" crash)
|
| 23 |
-
if (typeof process !== 'undefined' && process.env && process.env.API_KEY) {
|
| 24 |
-
return process.env.API_KEY;
|
| 25 |
-
}
|
| 26 |
return '';
|
| 27 |
};
|
| 28 |
|
|
@@ -146,7 +135,7 @@ export const generateRefinedPrompt = async (nodes: Node<NodeData>[], edges: Edge
|
|
| 146 |
return response.text.trim();
|
| 147 |
} catch (error) {
|
| 148 |
console.error("Prompt refinement failed:", error);
|
| 149 |
-
|
| 150 |
}
|
| 151 |
};
|
| 152 |
|
|
@@ -185,7 +174,7 @@ export const validateArchitecture = async (nodes: Node<NodeData>[], edges: Edge[
|
|
| 185 |
});
|
| 186 |
return response.text;
|
| 187 |
} catch (error) {
|
| 188 |
-
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
|
@@ -230,7 +219,7 @@ export const getArchitectureSuggestions = async (nodes: Node<NodeData>[], edges:
|
|
| 230 |
});
|
| 231 |
return response.text;
|
| 232 |
} catch (error) {
|
| 233 |
-
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
|
@@ -285,7 +274,7 @@ export const implementArchitectureSuggestions = async (
|
|
| 285 |
const result = JSON.parse(response.text.trim());
|
| 286 |
return sanitizeGraph(result);
|
| 287 |
} catch (error) {
|
| 288 |
-
throw new Error("Failed to implement suggestions
|
| 289 |
}
|
| 290 |
};
|
| 291 |
|
|
@@ -396,7 +385,7 @@ export const generateGraphWithAgents = async (
|
|
| 396 |
});
|
| 397 |
draftJsonStr = response.text.trim();
|
| 398 |
} catch (e) {
|
| 399 |
-
throw
|
| 400 |
}
|
| 401 |
|
| 402 |
// --- Step 2: Critic ---
|
|
@@ -501,7 +490,7 @@ export const generateCodeWithAgents = async (
|
|
| 501 |
});
|
| 502 |
draftCode = response.text.trim().replace(/```python/g, '').replace(/```/g, '');
|
| 503 |
} catch(e) {
|
| 504 |
-
throw
|
| 505 |
}
|
| 506 |
|
| 507 |
// --- Step 2: Reviewer (Critic) ---
|
|
@@ -549,7 +538,7 @@ export const generateCodeWithAgents = async (
|
|
| 549 |
onStatusUpdate('complete', 'Code generation complete!');
|
| 550 |
return finalCode;
|
| 551 |
} catch(e) {
|
| 552 |
-
throw new Error("Polisher agent failed.");
|
| 553 |
}
|
| 554 |
};
|
| 555 |
|
|
@@ -589,7 +578,7 @@ export const fixArchitectureErrors = async (
|
|
| 589 |
});
|
| 590 |
debugAnalysis = response.text.trim();
|
| 591 |
} catch (e) {
|
| 592 |
-
throw
|
| 593 |
}
|
| 594 |
|
| 595 |
// --- Step 2: Architect ---
|
|
@@ -648,4 +637,4 @@ export const fixArchitectureErrors = async (
|
|
| 648 |
} catch (e) {
|
| 649 |
throw new Error("Patcher agent failed to generate valid JSON.");
|
| 650 |
}
|
| 651 |
-
};
|
|
|
|
| 5 |
|
| 6 |
// Key Management
|
| 7 |
const getEnvKey = () => {
|
| 8 |
+
// In Development: Returns key from .env file if available
|
| 9 |
+
// In Production (Docker): Returns undefined/empty because Dockerfile does not inject the ARG
|
| 10 |
// @ts-ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.VITE_GEMINI_API_KEY) {
|
| 12 |
// @ts-ignore
|
| 13 |
return import.meta.env.VITE_GEMINI_API_KEY;
|
| 14 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return '';
|
| 16 |
};
|
| 17 |
|
|
|
|
| 135 |
return response.text.trim();
|
| 136 |
} catch (error) {
|
| 137 |
console.error("Prompt refinement failed:", error);
|
| 138 |
+
throw error; // Re-throw so caller can handle auth errors
|
| 139 |
}
|
| 140 |
};
|
| 141 |
|
|
|
|
| 174 |
});
|
| 175 |
return response.text;
|
| 176 |
} catch (error) {
|
| 177 |
+
throw error;
|
| 178 |
}
|
| 179 |
}
|
| 180 |
|
|
|
|
| 219 |
});
|
| 220 |
return response.text;
|
| 221 |
} catch (error) {
|
| 222 |
+
throw error;
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
|
|
|
| 274 |
const result = JSON.parse(response.text.trim());
|
| 275 |
return sanitizeGraph(result);
|
| 276 |
} catch (error) {
|
| 277 |
+
throw new Error("Failed to implement suggestions: " + (error instanceof Error ? error.message : String(error)));
|
| 278 |
}
|
| 279 |
};
|
| 280 |
|
|
|
|
| 385 |
});
|
| 386 |
draftJsonStr = response.text.trim();
|
| 387 |
} catch (e) {
|
| 388 |
+
throw e;
|
| 389 |
}
|
| 390 |
|
| 391 |
// --- Step 2: Critic ---
|
|
|
|
| 490 |
});
|
| 491 |
draftCode = response.text.trim().replace(/```python/g, '').replace(/```/g, '');
|
| 492 |
} catch(e) {
|
| 493 |
+
throw e;
|
| 494 |
}
|
| 495 |
|
| 496 |
// --- Step 2: Reviewer (Critic) ---
|
|
|
|
| 538 |
onStatusUpdate('complete', 'Code generation complete!');
|
| 539 |
return finalCode;
|
| 540 |
} catch(e) {
|
| 541 |
+
throw new Error("Polisher agent failed to generate code.");
|
| 542 |
}
|
| 543 |
};
|
| 544 |
|
|
|
|
| 578 |
});
|
| 579 |
debugAnalysis = response.text.trim();
|
| 580 |
} catch (e) {
|
| 581 |
+
throw e;
|
| 582 |
}
|
| 583 |
|
| 584 |
// --- Step 2: Architect ---
|
|
|
|
| 637 |
} catch (e) {
|
| 638 |
throw new Error("Patcher agent failed to generate valid JSON.");
|
| 639 |
}
|
| 640 |
+
};
|