File size: 1,652 Bytes
e266561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
solution_node.py — Reveals the full optimal solution with explanation.

Improvements over v1:
  - Uses llm.with_structured_output() for guaranteed schema compliance
  - Imports SolutionOutput from models (no local import needed)
"""

from agent.models import AgentState, SolutionOutput
from agent.llm_factory import get_llm
from agent.prompts import SOLUTION_PROMPT

_llm = get_llm()
_structured_llm = _llm.with_structured_output(SolutionOutput, method="function_calling")


def reveal_solution(state: AgentState) -> dict:
    """Provides the full, optimal solution with explanation and complexity analysis."""
    topic = state.get("problem_topic", "DSA")

    try:
        result: SolutionOutput = _structured_llm.invoke(
            SOLUTION_PROMPT.format_messages(
                topic=topic,
                problem=state["problem"],
            )
        )
        return {
            "final_response": {
                "solution":    result.solution_code,
                "explanation": result.explanation,
                "complexity":  result.complexity_analysis,
                "type":        "Solution",
                "score":       0,  # Requested solution — no independent credit
            }
        }
    except Exception as e:
        print(f"[solution_node] Structured output error: {e}")
        return {
            "final_response": {
                "solution":    "# Error generating solution",
                "explanation": "Failed to generate solution. Please try again.",
                "complexity":  "N/A",
                "type":        "Solution",
                "score":       0,
            }
        }