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": `def change_direction(new_direction): \n\n\tglobal direction \n\n\tif new_direction == 'left': \n\t\tif direction != 'right': \n\t\t\tdirection = new_direction \n\telif new_direction == 'right': \n\t\tif direction != 'left': \n\t\t\tdirection = new_direction \n\telif new_direction == 'up': \n\t\tif direction != 'down': \n\t\t\tdirection = new_direction \n\telif new_direction == 'down': \n\t\tif direction != 'up': \n\t\t\tdirection = new_direction` // 源码片段
}

let req = {
    "cmd"       : "exec_fix",
    "request_id": 123,                                                                        // 随机生成 （必填）
    "model"     : "local deepseek-r1:32b",                                                    //（必填）
    "stream"    : false,
    "selected_text": selected_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()
