Python_ai / app.py
Percy3822's picture
Create app.py
50b6338 verified
raw
history blame
1.51 kB
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List, Literal
app = FastAPI(title="Python AI Stub")
class Cursor(BaseModel):
l: int
c: int
class Viewport(BaseModel):
start: int
end: int
text: str
class Diagnostic(BaseModel):
l: int
sev: str
msg: str
class Memory(BaseModel):
short: List[str] = []
sess: List[str] = []
proj: List[str] = []
class Inp(BaseModel):
intent: str
file: str
lang: str
cursor: Cursor
viewport: Viewport
diag: List[Diagnostic] = []
term: str = ""
mem: Memory = Memory()
class Need(BaseModel):
function: bool = False
xrefs: List[str] = []
page_ids: List[str] = []
class Out(BaseModel):
mode: Literal["patch","full","ask"]
patch: str = ""
full_text: str = ""
explanation: str = ""
confidence: float = 0.9
need: Need = Need()
@app.post("/code_help", response_model=Out)
def code_help(x: Inp):
before = x.viewport.text
if "print(reslt)" in before:
after = before.replace("print(reslt)","print(result)",1)
h_start = x.viewport.start
h_count = x.viewport.end - x.viewport.start + 1
patch = f"--- a/{x.file}\n+++ b/{x.file}\n@@ -{h_start},{h_count} +{h_start},{h_count} @@\n-{before}\n+{after}\n"
return Out(mode="patch", patch=patch, full_text="", explanation="Fixed misspelling: reslt -> result.", confidence=0.96)
return Out(mode="ask", explanation="Need more context.", confidence=0.6)