wuhp commited on
Commit
b070ee4
·
verified ·
1 Parent(s): f60b8b9

Update services/geminiService.ts

Browse files
Files changed (1) hide show
  1. services/geminiService.ts +99 -2
services/geminiService.ts CHANGED
@@ -25,7 +25,7 @@ const getAiClient = () => {
25
 
26
  const MODEL_NAME = 'gemini-2.5-flash';
27
 
28
- export type AgentStatus = 'idle' | 'architect' | 'critic' | 'refiner' | 'complete' | 'error';
29
 
30
  /**
31
  * Internal helper to build the raw specification string.
@@ -476,4 +476,101 @@ export const generateCodeWithAgents = async (
476
  } catch(e) {
477
  throw new Error("Polisher agent failed.");
478
  }
479
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  const MODEL_NAME = 'gemini-2.5-flash';
27
 
28
+ export type AgentStatus = 'idle' | 'architect' | 'critic' | 'refiner' | 'debugger' | 'patcher' | 'complete' | 'error';
29
 
30
  /**
31
  * Internal helper to build the raw specification string.
 
476
  } catch(e) {
477
  throw new Error("Polisher agent failed.");
478
  }
479
+ };
480
+
481
+ /**
482
+ * Fix Architecture Errors with Agents (Debugger -> Architect -> Patcher)
483
+ */
484
+ export const fixArchitectureErrors = async (
485
+ nodes: Node<NodeData>[],
486
+ edges: Edge[],
487
+ errorMsg: string,
488
+ onStatusUpdate: (status: AgentStatus, log: string) => void
489
+ ): Promise<{ nodes: any[], edges: any[] } | null> => {
490
+ const ai = getAiClient();
491
+ const graphJson = JSON.stringify({
492
+ nodes: nodes.map(n => ({ id: n.id, type: n.data.type, label: n.data.label, params: n.data.params, position: n.position })),
493
+ edges: edges.map(e => ({ source: e.source, target: e.target }))
494
+ });
495
+
496
+ // --- Step 1: Debugger ---
497
+ onStatusUpdate('debugger', 'Debugger Agent is analyzing the error trace...');
498
+
499
+ const debuggerPrompt = `
500
+ Role: Senior Systems Debugger.
501
+ Task: Analyze the architecture graph and the reported error to pinpoint the root cause.
502
+
503
+ Graph: ${graphJson}
504
+ Error Message: "${errorMsg}"
505
+
506
+ Output a technical analysis of exactly what is wrong (e.g. "Node A connects to Node B but shapes [X] and [Y] are incompatible").
507
+ `;
508
+
509
+ let debugAnalysis = "";
510
+ try {
511
+ const response = await ai.models.generateContent({
512
+ model: MODEL_NAME,
513
+ contents: debuggerPrompt,
514
+ });
515
+ debugAnalysis = response.text.trim();
516
+ } catch (e) {
517
+ throw new Error("Debugger agent failed.");
518
+ }
519
+
520
+ // --- Step 2: Architect ---
521
+ onStatusUpdate('architect', 'Architect Agent is planning the fix...');
522
+
523
+ const architectPrompt = `
524
+ Role: Solution Architect.
525
+ Task: Propose a specific fix for the identified issue.
526
+
527
+ Issue Analysis: ${debugAnalysis}
528
+
529
+ Instructions:
530
+ - Determine if nodes need to be added (e.g. Flatten, Reshape), removed, or reconnected.
531
+ - Determine if parameters need changing.
532
+
533
+ Output the plan in clear steps.
534
+ `;
535
+
536
+ let fixPlan = "";
537
+ try {
538
+ const response = await ai.models.generateContent({
539
+ model: MODEL_NAME,
540
+ contents: architectPrompt,
541
+ });
542
+ fixPlan = response.text.trim();
543
+ } catch (e) {
544
+ fixPlan = "Apply necessary structural corrections.";
545
+ }
546
+
547
+ // --- Step 3: Patcher ---
548
+ onStatusUpdate('patcher', 'Patcher Agent is applying the fix to the graph...');
549
+
550
+ const patcherPrompt = `
551
+ Role: DevOps Engineer.
552
+ Task: Apply the fix to the graph JSON.
553
+
554
+ Current Graph: ${graphJson}
555
+ Fix Plan: ${fixPlan}
556
+
557
+ Requirements:
558
+ 1. Return the complete, valid JSON with "nodes" and "edges".
559
+ 2. Maintain existing node positions where possible, offset new nodes if added.
560
+ 3. Ensure all LayerTypes are valid.
561
+ 4. Return ONLY raw JSON.
562
+ `;
563
+
564
+ try {
565
+ const response = await ai.models.generateContent({
566
+ model: MODEL_NAME,
567
+ contents: patcherPrompt,
568
+ config: { responseMimeType: "application/json" }
569
+ });
570
+ const finalJson = JSON.parse(response.text.trim());
571
+ onStatusUpdate('complete', 'Fix applied successfully!');
572
+ return finalJson;
573
+ } catch (e) {
574
+ throw new Error("Patcher agent failed to generate valid JSON.");
575
+ }
576
+ };