Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions core/llm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
constructLlmApi,
} from "@continuedev/openai-adapters";
import Handlebars from "handlebars";
import * as path from "path";
import * as vscode from "vscode";

import { DevDataSqliteDb } from "../data/devdataSqlite.js";
import { DataLogger } from "../data/log.js";
Expand Down Expand Up @@ -62,6 +64,42 @@ import {
toFimBody,
} from "./openaiTypeConverters.js";

const { spawn } = require("child_process");

function callPythonFunction(
path: string,
name: string,
ws: string,
): Promise<string> {
return new Promise((resolve, reject) => {
let stdoutData = "";
let stderrData = "";

const pythonProcess = spawn("python", [path, name, ws]);
pythonProcess.stdout.on("data", (data: Buffer) => {
stdoutData += data.toString();
console.log(`Output: ${data.toString()}`);
});

pythonProcess.stderr.on("data", (data: Buffer) => {
stderrData += data.toString();
console.error(`Error: ${data.toString()}`);
});

pythonProcess.on("close", (code: number) => {
if (code !== 0) {
reject(
new Error(
`Python process exited with code ${code}: ${stderrData.trim()}`,
),
);
} else {
resolve(stdoutData.trim());
}
});
});
}

export class LLMError extends Error {
constructor(
message: string,
Expand Down Expand Up @@ -926,6 +964,7 @@ export abstract class BaseLLM implements ILLM {

let thinking = "";
let completion = "";
let response = "";

try {
if (this.templateMessages) {
Expand Down Expand Up @@ -984,6 +1023,7 @@ export abstract class BaseLLM implements ILLM {
)) {
if (chunk.role === "assistant") {
completion += this._formatChatMessage(chunk);
response += chunk.content;
} else if (chunk.role === "thinking") {
thinking += chunk.content;
}
Expand All @@ -996,6 +1036,49 @@ export abstract class BaseLLM implements ILLM {
}
}
}

let script_response = "";
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
if (rootPath) {
try {
script_response = await callPythonFunction(
rootPath + "\\script.py" /* TODO: do not hardcode script name */,
response,
vscode.workspace.workspaceFolders[0].uri.fsPath,
);
} catch (error) {}

/* analyze the resonse from python script */
const lines = script_response
.split("\n")
.filter((line) => line.trim() !== "");
const result: Record<string, string> = {};

lines.forEach((line) => {
const [key, value] = line.split("=");
if (key && value) {
result[key.trim()] = value.trim();
}
});

const startLine = Number(result.STARTLINE);
const endLine = Number(result.ENDLINE);
const filename = result.FILENAME || "";
const selection = new vscode.Selection(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 0),
);
const filePath = path.join(rootPath, filename);
const fileUri = vscode.Uri.file(filePath);
/* TODO: check if it exists before opening */
const document = await vscode.workspace.openTextDocument(fileUri);
await vscode.window.showTextDocument(document, {
selection: selection,
viewColumn: vscode.ViewColumn.Active,
preserveFocus: false,
});
}

status = this._logEnd(
completionOptions.model,
prompt,
Expand Down
18 changes: 18 additions & 0 deletions manual-testing-sandbox/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import sys

def create_file(filename, content):
# Create a new file with the given filename and content
with open(filename, 'w') as f:
f.write(content)

if __name__ == "__main__":
filename = "D:/02.Continental/AI/example.txt"
content = "[This is the response]\n"
content += "STARTLINE=2\n"
content += "ENDLINE=10\n"
content += "FILENAME=data.json\n"
content += "\n---\n" + sys.argv[1]
content += "\n---\n" + sys.argv[2]
print(content)
create_file(filename, content)
18 changes: 18 additions & 0 deletions script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
import sys

def create_file(filename, content):
# Create a new file with the given filename and content
with open(filename, 'w') as f:
f.write(content)

if __name__ == "__main__":
filename = "script.py.txt"
content = "[This is the response]\n"
content += "STARTLINE=2\n"
content += "ENDLINE=10\n"
content += "FILENAME=manual-testing-sandbox\data.json\n"
content += "\n---\n" + sys.argv[1]
content += "\n---\n" + sys.argv[2]
print(content)
create_file(filename, content)
14 changes: 14 additions & 0 deletions tryrun/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
使用AI CHAT时,在获取AI RESPONSE后,会默认调用当前打开工程目录下的script.py。
关于script.py:
- 输入
arg[0]: 程序名, 如:script.py
arg[1]: AI RESPONSE
arg[2]: 当前工作目录
- 输出
如果script.py根据AI RESPONE修改了一些文件,那么需要按照如下格式说明修改哪个文件的哪些行。
即:在标准输出需要输出一下内容,VScode会根据此信息打开FILENAME, 同时选中STARTLINE到ENDLINE。
STARTLINE=${linenum}
ENDLINE=${linenum}
FILENAME=${filename}

!!! This is just a temporary solution !!!
Binary file added tryrun/TRYRUN_CSSDC_DEBUG_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tryrun/TRYRUN_CSSDC_DEBUG_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.