File size: 1,611 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
"""
POST /ai/fix
Given code + an error message, suggest a fix.
"""
from fastapi import APIRouter
from pydantic import BaseModel
from core.gemini_client import ask_gemini

router = APIRouter()

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

class FixResponse(BaseModel):
    fixed_code: str
    explanation: str

@router.post("/fix", response_model=FixResponse)
async def fix_code(body: FixRequest):
    prompt = f"""
The user's H-Script code has an error. Help them fix it.

Original code:
```
{body.code}
```

Error message:
"{body.error}"

Instructions:
1. Identify what caused the error
2. Provide the COMPLETE fixed code (not just the changed line)
3. Explain the fix in 2-3 friendly Hinglish sentences

Format your response EXACTLY like this:
```hscript
[complete fixed code here]
```
EXPLANATION: [what was wrong and how you fixed it]
"""
    raw = await ask_gemini(prompt)

    # Parse fixed code and explanation
    fixed_code = ""
    explanation = ""
    if "```" in raw:
        parts = raw.split("```")
        for i, part in enumerate(parts):
            if i % 2 == 1:
                lines = part.strip().splitlines()
                if lines and len(lines[0].strip()) < 20 and not lines[0].strip().startswith("let_him_cook"):
                    lines = lines[1:]
                fixed_code = "\n".join(lines).strip()
                break
    if "EXPLANATION:" in raw:
        explanation = raw.split("EXPLANATION:")[-1].strip()

    if not fixed_code:
        fixed_code = body.code
        explanation = raw

    return FixResponse(fixed_code=fixed_code, explanation=explanation)