Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,32 @@
|
|
| 1 |
-
|
| 2 |
-
import difflib
|
| 3 |
-
import json
|
| 4 |
-
import os
|
| 5 |
-
from typing import Dict, Any
|
| 6 |
|
| 7 |
-
|
| 8 |
-
from fastapi.responses import JSONResponse
|
| 9 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
-
|
| 11 |
-
APP_NAME = "Python_ai"
|
| 12 |
-
app = FastAPI(title=APP_NAME)
|
| 13 |
-
|
| 14 |
-
# Allow CORS just in case you call it directly from the Brain UI
|
| 15 |
-
app.add_middleware(
|
| 16 |
-
CORSMiddleware,
|
| 17 |
-
allow_origins=["*"], allow_credentials=True,
|
| 18 |
-
allow_methods=[""], allow_headers=[""],
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
def ok(payload: Dict[str, Any] | None = None) -> JSONResponse:
|
| 22 |
-
return JSONResponse({"ok": True, **(payload or {})})
|
| 23 |
|
| 24 |
@app.get("/health")
|
| 25 |
def health():
|
| 26 |
-
return ok
|
| 27 |
-
|
| 28 |
-
def make_unified_diff(filename: str, old_text: str, new_text: str) -> str:
|
| 29 |
-
# Ensure both end with newline so ranges are sane
|
| 30 |
-
if not old_text.endswith("\n"): old_text += "\n"
|
| 31 |
-
if not new_text.endswith("\n"): new_text += "\n"
|
| 32 |
-
diff = difflib.unified_diff(
|
| 33 |
-
old_text.splitlines(keepends=True),
|
| 34 |
-
new_text.splitlines(keepends=True),
|
| 35 |
-
fromfile=f"a/{filename}",
|
| 36 |
-
tofile=f"b/{filename}",
|
| 37 |
-
n=3,
|
| 38 |
-
)
|
| 39 |
-
return "".join(diff)
|
| 40 |
-
|
| 41 |
-
def minimal_fix(view_text: str) -> tuple[str, str, str]:
|
| 42 |
-
"""
|
| 43 |
-
Very small heuristic fixer:
|
| 44 |
-
- reslt -> result (common typo)
|
| 45 |
-
- NameError 'bar' if foo returns bar but bar undefined -> return 'result' instead
|
| 46 |
-
Only edits when the change is obvious; otherwise returns original text.
|
| 47 |
-
"""
|
| 48 |
-
old = view_text
|
| 49 |
-
new = old
|
| 50 |
-
|
| 51 |
-
# Trivial typo fix
|
| 52 |
-
if "reslt" in new:
|
| 53 |
-
new = new.replace("reslt", "result")
|
| 54 |
-
|
| 55 |
-
# If there's a pattern like:
|
| 56 |
-
# def foo():\n return bar\n\nresult = foo()
|
| 57 |
-
# we can't know bar; safer to leave it alone here.
|
| 58 |
-
# Keep this service minimal/fast.
|
| 59 |
-
|
| 60 |
-
return old, new, "Fixed trivial misspelling(s) where obvious."
|
| 61 |
|
| 62 |
@app.post("/code_help")
|
| 63 |
-
|
| 64 |
-
"""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
except Exception:
|
| 71 |
-
body = {}
|
| 72 |
-
|
| 73 |
-
# Extract with defaults (never 422)
|
| 74 |
-
utterance = body.get("utterance", "")
|
| 75 |
-
telemetry = body.get("telemetry", {}) or {}
|
| 76 |
-
filename = telemetry.get("file", "main.py")
|
| 77 |
-
viewport = telemetry.get("viewport", {}) or {}
|
| 78 |
-
view_text = viewport.get("text", "")
|
| 79 |
-
|
| 80 |
-
# Minimal edit logic
|
| 81 |
-
old_text, new_text, why = minimal_fix(view_text)
|
| 82 |
-
|
| 83 |
-
if new_text != old_text:
|
| 84 |
-
patch = make_unified_diff(filename, old_text, new_text)
|
| 85 |
-
resp = {
|
| 86 |
"mode": "patch",
|
| 87 |
-
"patch": patch,
|
| 88 |
-
"full_text": "",
|
| 89 |
-
"explanation": why,
|
| 90 |
-
"confidence": 0.96,
|
| 91 |
-
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 92 |
-
}
|
| 93 |
-
else:
|
| 94 |
-
# Nothing obvious to fix; ask for more context (keeps contract)
|
| 95 |
-
resp = {
|
| 96 |
-
"mode": "ask",
|
| 97 |
-
"patch": "",
|
| 98 |
"full_text": "",
|
| 99 |
-
"explanation": "
|
| 100 |
-
"confidence": 0.
|
| 101 |
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 102 |
}
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Body
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
app = FastAPI(title="Python_ai")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
@app.get("/health")
|
| 6 |
def health():
|
| 7 |
+
return {"ok": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@app.post("/code_help")
|
| 10 |
+
def code_help(payload: dict = Body(...)):
|
| 11 |
+
# Trivial fixer: rename "reslt" -> "result" if present
|
| 12 |
+
viewport = payload.get("telemetry", {}).get("viewport", {})
|
| 13 |
+
text = viewport.get("text", "")
|
| 14 |
+
if "reslt" in text and "result" not in text:
|
| 15 |
+
patch = text.replace("reslt", "result")
|
| 16 |
+
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"mode": "patch",
|
| 18 |
+
"patch": f"--- a/main.py\n+++ b/main.py\n@@ -1 +1 @@\n-{text}\n\n+{patch}\n\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"full_text": "",
|
| 20 |
+
"explanation": "Fixed misspelling 'reslt' -> 'result'.",
|
| 21 |
+
"confidence": 0.95,
|
| 22 |
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 23 |
}
|
| 24 |
+
# Default no-op
|
| 25 |
+
return {
|
| 26 |
+
"mode": "patch",
|
| 27 |
+
"patch": "",
|
| 28 |
+
"full_text": "",
|
| 29 |
+
"explanation": "No obvious fix.",
|
| 30 |
+
"confidence": 0.5,
|
| 31 |
+
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 32 |
+
}
|