File size: 1,936 Bytes
51769a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
POST /ai/explain-error
Classifies error type and gives a beginner-friendly Hinglish explanation.
Auto-triggered when Run returns errors.
"""
from fastapi import APIRouter
from pydantic import BaseModel
from core.gemini_client import ask_gemini

router = APIRouter()

class ExplainErrorRequest(BaseModel):
    code: str
    error: str

class ExplainErrorResponse(BaseModel):
    error_type: str      # SyntaxError | ReferenceError | TypeError | RuntimeError | LogicError
    explanation: str     # friendly Hinglish explanation
    hint: str            # one-line fix hint

@router.post("/explain-error", response_model=ExplainErrorResponse)
async def explain_error(body: ExplainErrorRequest):
    prompt = f"""
A user is learning H-Script and got this error while running their code.

Code:
```
{body.code}
```

Error message:
"{body.error}"

Your job:
1. Classify the error into ONE of these types:
   SyntaxError | ReferenceError | TypeError | RuntimeError | LogicError

2. Write a SHORT friendly explanation (2-3 sentences) in Hinglish — like a helpful tutor.
   Use emojis. Don't be too technical.

3. Give ONE short fix hint (a single sentence).

Format your response EXACTLY like this (no extra text):
ERROR_TYPE: [one of the 5 types above]
EXPLANATION: [your 2-3 sentence Hinglish explanation]
HINT: [one-line fix suggestion]
"""
    raw = await ask_gemini(prompt)

    # Parse structured response
    error_type   = "RuntimeError"
    explanation  = raw
    hint         = ""

    for line in raw.splitlines():
        if line.startswith("ERROR_TYPE:"):
            error_type = line.split(":", 1)[1].strip()
        elif line.startswith("EXPLANATION:"):
            explanation = line.split(":", 1)[1].strip()
        elif line.startswith("HINT:"):
            hint = line.split(":", 1)[1].strip()

    return ExplainErrorResponse(
        error_type=error_type,
        explanation=explanation,
        hint=hint,
    )