File size: 1,508 Bytes
50b6338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)