import ws from 'ws';
import { makeClient } from "./common";

let selected_text = {
    "filepath": "example\\\\game.py",  // 文件路径 
    "range": {
        "start": { "line": 33, "character": 0},   // 从33行0字符开始
        "end"  : { "line": 45, "character": 41}   // 到45行41字符结束
    },
    "text": `class Food: \n\n\tdef __init__(self): \n\n\t\tx = random.randint(0, \n\t\t\t\t(WIDTH / SPACE_SIZE)-1) * SPACE_SIZE \n\t\ty = random.randint(0, \n\t\t\t\t(HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE \n\n\t\tself.coordinates = [x, y] \n\n\t\tcanvas.create_oval(x, y, x + SPACE_SIZE, y +\n\t\t\t\t\t\tSPACE_SIZE, fill=FOOD, tag="food") ` // 源码片段
}

let visible_text = {
    "filepath": "g:\\\\workspace\\\\example\\\\game.py",
    "range": {
        "start": { "line": 6, "character": 0},   
        "end"  : { "line": 69, "character": 0}   
    },
    "text": `WIDTH = 500\nHEIGHT = 500\nSPEED = 200\nSPACE_SIZE = 20\nBODY_SIZE = 2\nSNAKE = "#0000FF"\nFOOD = "#FFFF00"\nBACKGROUND = "#000000"\n\n# Class to design the snake \nclass Snake: \n\n\tdef __init__(self): \n\t\tself.body_size = BODY_SIZE \n\t\tself.coordinates = [] \n\t\tself.squares = [] \n\n\t\tfor i in range(0, BODY_SIZE): \n\t\t\tself.coordinates.append([0, 0]) \n\n\t\tfor x, y in self.coordinates: \n\t\t\tsquare = canvas.create_rectangle( \n\t\t\t\tx, y, x + SPACE_SIZE, y + SPACE_SIZE, \n\t\t\t\t\tfill=SNAKE, tag="snake") \n\t\t\tself.squares.append(square) \n\n# Class to design the food \nclass Food: \n\n\tdef __init__(self): \n\n\t\tx = random.randint(0, \n\t\t\t\t(WIDTH / SPACE_SIZE)-1) * SPACE_SIZE \n\t\ty = random.randint(0, \n\t\t\t\t(HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE \n\n\t\tself.coordinates = [x, y] \n\n\t\tcanvas.create_oval(x, y, x + SPACE_SIZE, y +\n\t\t\t\t\t\tSPACE_SIZE, fill=FOOD, tag="food") \n\n# Function to check the next move of snake \ndef next_turn(snake, food): \n\n\tx, y = snake.coordinates[0] \n\n\tif direction == "up": \n\t\ty -= SPACE_SIZE \n\telif direction == "down": \n\t\ty += SPACE_SIZE \n\telif direction == "left": \n\t\tx -= SPACE_SIZE \n\telif direction == "right": \n\t\tx += SPACE_SIZE \n\n\tsnake.coordinates.insert(0, (x, y)) \n\n\tsquare = canvas.create_rectangle( \n\t\tx, y, x + SPACE_SIZE, \n\t\t\t\ty + SPACE_SIZE, fill=SNAKE) \n\n\tsnake.squares.insert(0, square) \n\n\tif x == food.coordinates[0] and y == food.coordinates[1]: `
}

let req = {
    "cmd"       : "exec_explain",
    "request_id": 123,                                                                        // 随机生成 （必填）
    "model"     : "local deepseek-r1:32b",                                                    //（必填）
    "stream"    : false,                    
    "selected_text": selected_text,
    "visible_text" : visible_text
}

async function run() {
    let client = await makeClient()
    req.stream = false
    client.send(JSON.stringify(req))
}


async function run_stream() {
    let client = await makeClient((data: ws.RawData)=>{
        let d = JSON.parse(data.toString())
        process.stdout.write(d.msg)
        // console.log(data.toString())
    })
    req.stream = true
    client.send(JSON.stringify(req))
}


run_stream()
