2023-12-20 09:36:55 +00:00
|
|
|
import * as vscode from "vscode";
|
2023-12-21 00:27:42 +00:00
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
let VSConfig: vscode.WorkspaceConfiguration;
|
|
|
|
let apiEndpoint: string;
|
2024-06-02 13:42:29 +00:00
|
|
|
let apiAuthentication: string;
|
2023-12-21 00:27:42 +00:00
|
|
|
let apiModel: string;
|
2024-03-11 18:27:51 +00:00
|
|
|
let apiTemperature: number;
|
2023-12-21 00:27:42 +00:00
|
|
|
let numPredict: number;
|
|
|
|
let promptWindowSize: number;
|
2024-01-06 21:49:41 +00:00
|
|
|
let completionKeys: string;
|
2024-01-06 23:33:17 +00:00
|
|
|
let responsePreview: boolean | undefined;
|
2024-01-09 02:21:32 +00:00
|
|
|
let responsePreviewMaxTokens: number;
|
2024-01-28 02:22:47 +00:00
|
|
|
let responsePreviewDelay: number;
|
|
|
|
let continueInline: boolean | undefined;
|
2024-06-02 13:42:29 +00:00
|
|
|
let keepAlive: number | undefined;
|
|
|
|
let topP: number | undefined;
|
2024-09-10 14:39:33 +00:00
|
|
|
let inlinePreviewDecoration: vscode.TextEditorDecorationType;
|
|
|
|
let currentPreviewText: string | undefined;
|
|
|
|
|
|
|
|
function createInlinePreviewDecoration() {
|
|
|
|
return vscode.window.createTextEditorDecorationType({
|
|
|
|
after: {
|
|
|
|
color: '#808080',
|
|
|
|
fontStyle: 'italic'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-06-02 14:48:14 +00:00
|
|
|
|
2023-12-21 00:27:42 +00:00
|
|
|
function updateVSConfig() {
|
2024-08-13 20:22:53 +00:00
|
|
|
VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder");
|
|
|
|
apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate";
|
|
|
|
apiAuthentication = VSConfig.get("authentication") || "";
|
2024-08-14 05:31:21 +00:00
|
|
|
apiModel = VSConfig.get("model") || "fabelous-coder:latest";
|
2024-08-13 20:22:53 +00:00
|
|
|
numPredict = VSConfig.get("max tokens predicted") || 1000;
|
|
|
|
promptWindowSize = VSConfig.get("prompt window size") || 2000;
|
|
|
|
completionKeys = VSConfig.get("completion keys") || " ";
|
2024-09-10 14:39:33 +00:00
|
|
|
// Here's where the response preview setting is read
|
2024-08-13 20:22:53 +00:00
|
|
|
responsePreview = VSConfig.get("response preview");
|
|
|
|
responsePreviewMaxTokens = VSConfig.get("preview max tokens") || 50;
|
|
|
|
responsePreviewDelay = VSConfig.get("preview delay") || 0;
|
|
|
|
continueInline = VSConfig.get("continue inline");
|
|
|
|
apiTemperature = VSConfig.get("temperature") || 0.7;
|
|
|
|
keepAlive = VSConfig.get("keep alive") || 30;
|
|
|
|
topP = VSConfig.get("top p") || 1;
|
2023-12-21 00:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateVSConfig();
|
|
|
|
vscode.workspace.onDidChangeConfiguration(updateVSConfig);
|
2024-08-13 20:22:53 +00:00
|
|
|
|
|
|
|
function getContextLines(document: vscode.TextDocument, position: vscode.Position): string {
|
|
|
|
const lines = [];
|
2024-08-21 10:46:48 +00:00
|
|
|
const startLine = Math.max(0, position.line - 1);
|
|
|
|
const endLine = position.line;
|
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
for (let i = startLine; i <= endLine; i++) {
|
|
|
|
lines.push(document.lineAt(i).text);
|
|
|
|
}
|
2024-08-21 10:46:48 +00:00
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
return lines.join("\n");
|
|
|
|
}
|
|
|
|
|
2024-08-21 10:46:48 +00:00
|
|
|
function createFIMPrompt(prefix: string, language: string): string {
|
|
|
|
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
|
2024-08-14 05:31:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 01:04:50 +00:00
|
|
|
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
|
2024-07-04 06:46:02 +00:00
|
|
|
const document = textEditor.document;
|
|
|
|
const position = textEditor.selection.active;
|
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
const context = getContextLines(document, position);
|
2024-07-04 06:46:02 +00:00
|
|
|
|
2024-08-21 10:46:48 +00:00
|
|
|
const fimPrompt = createFIMPrompt(context, document.languageId);
|
2024-07-04 06:46:02 +00:00
|
|
|
|
|
|
|
vscode.window.withProgress(
|
|
|
|
{
|
|
|
|
location: vscode.ProgressLocation.Notification,
|
2024-08-13 20:22:53 +00:00
|
|
|
title: "Fabelous Autocoder",
|
2024-07-04 06:46:02 +00:00
|
|
|
cancellable: true,
|
|
|
|
},
|
|
|
|
async (progress, progressCancellationToken) => {
|
|
|
|
try {
|
|
|
|
progress.report({ message: "Starting model..." });
|
|
|
|
|
|
|
|
let axiosCancelPost: () => void;
|
|
|
|
const axiosCancelToken = new axios.CancelToken((c) => {
|
2024-08-14 05:31:21 +00:00
|
|
|
axiosCancelPost = () => {
|
2024-07-04 06:46:02 +00:00
|
|
|
c("Autocompletion request terminated by user cancel");
|
|
|
|
};
|
2024-08-14 05:31:21 +00:00
|
|
|
if (cancellationToken) cancellationToken.onCancellationRequested(axiosCancelPost);
|
|
|
|
progressCancellationToken.onCancellationRequested(axiosCancelPost);
|
|
|
|
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
2024-07-04 06:46:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = await axios.post(apiEndpoint, {
|
2024-08-13 20:22:53 +00:00
|
|
|
model: apiModel,
|
2024-08-14 05:31:21 +00:00
|
|
|
prompt: fimPrompt,
|
2024-08-19 11:51:41 +00:00
|
|
|
stream: false,
|
2024-07-04 06:46:02 +00:00
|
|
|
raw: true,
|
|
|
|
options: {
|
|
|
|
num_predict: numPredict,
|
|
|
|
temperature: apiTemperature,
|
2024-08-13 20:22:53 +00:00
|
|
|
stop: ["<fim_suffix>", "```"]
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
cancelToken: axiosCancelToken,
|
|
|
|
headers: {
|
|
|
|
'Authorization': apiAuthentication
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-19 11:51:41 +00:00
|
|
|
progress.report({ message: "Generating..." });
|
2024-08-13 20:22:53 +00:00
|
|
|
|
2024-08-19 11:51:41 +00:00
|
|
|
let completionText = response.data.response;
|
|
|
|
// Remove any FIM tags and leading/trailing whitespace
|
|
|
|
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
2024-08-13 20:22:53 +00:00
|
|
|
|
2024-08-21 10:46:48 +00:00
|
|
|
// Remove the context lines
|
|
|
|
const startLine = Math.max(0, position.line - 1);
|
|
|
|
const endLine = position.line;
|
|
|
|
const rangeToReplace = new vscode.Range(
|
|
|
|
new vscode.Position(startLine, 0),
|
|
|
|
new vscode.Position(endLine, document.lineAt(endLine).text.length)
|
|
|
|
);
|
2024-08-19 11:51:41 +00:00
|
|
|
|
|
|
|
// Apply the edit
|
|
|
|
const edit = new vscode.WorkspaceEdit();
|
2024-08-21 10:46:48 +00:00
|
|
|
edit.replace(document.uri, rangeToReplace, completionText);
|
2024-08-19 11:51:41 +00:00
|
|
|
await vscode.workspace.applyEdit(edit);
|
|
|
|
|
|
|
|
// Move the cursor to the end of the inserted text
|
2024-08-21 10:46:48 +00:00
|
|
|
const newPosition = new vscode.Position(startLine + completionText.split('\n').length - 1, completionText.split('\n').pop()!.length);
|
2024-08-19 11:51:41 +00:00
|
|
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
|
|
|
|
|
|
|
progress.report({ message: "Fabelous completion finished." });
|
2024-08-13 20:22:53 +00:00
|
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
vscode.window.showErrorMessage(
|
|
|
|
"Fabelous Autocoder encountered an error: " + err.message
|
|
|
|
);
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-12-20 22:06:47 +00:00
|
|
|
}
|
2024-08-13 20:22:53 +00:00
|
|
|
|
2024-01-28 03:22:14 +00:00
|
|
|
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
|
2024-08-13 20:22:53 +00:00
|
|
|
const item = new vscode.CompletionItem("Fabelous autocompletion");
|
|
|
|
item.insertText = new vscode.SnippetString('${1:}');
|
|
|
|
|
2024-09-10 14:39:33 +00:00
|
|
|
// This is where the response preview functionality is implemented
|
2024-08-13 20:22:53 +00:00
|
|
|
if (responsePreview) {
|
2024-08-14 05:31:21 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
|
|
|
|
if (cancellationToken.isCancellationRequested) {
|
|
|
|
return [ item ];
|
|
|
|
}
|
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
const context = getContextLines(document, position);
|
2024-08-21 10:46:48 +00:00
|
|
|
const fimPrompt = createFIMPrompt(context, document.languageId);
|
2024-08-14 05:31:21 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response_preview = await axios.post(apiEndpoint, {
|
|
|
|
model: apiModel,
|
|
|
|
prompt: fimPrompt,
|
|
|
|
stream: false,
|
|
|
|
raw: true,
|
|
|
|
options: {
|
|
|
|
num_predict: responsePreviewMaxTokens,
|
|
|
|
temperature: apiTemperature,
|
|
|
|
stop: ['<fim_suffix>', '\n', '```'],
|
|
|
|
...(keepAlive && { keep_alive: keepAlive }),
|
|
|
|
...(topP && { top_p: topP }),
|
|
|
|
}
|
|
|
|
});
|
2024-09-10 14:39:33 +00:00
|
|
|
|
|
|
|
currentPreviewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
|
|
|
|
updateInlinePreview(document, position);
|
|
|
|
|
|
|
|
if (currentPreviewText) {
|
|
|
|
item.label = currentPreviewText.split('\n')[0];
|
|
|
|
}
|
2024-08-14 05:31:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching preview:", error);
|
2024-08-13 20:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 14:39:33 +00:00
|
|
|
item.documentation = new vscode.MarkdownString('Press `Enter` to accept the autocompletion, or `Escape` to dismiss');
|
|
|
|
item.command = {
|
|
|
|
command: 'fabelous-autocoder.acceptCompletion',
|
|
|
|
title: 'Accept Fabelous Autocompletion',
|
|
|
|
};
|
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
return [item];
|
2024-01-28 03:22:14 +00:00
|
|
|
}
|
2024-08-13 20:22:53 +00:00
|
|
|
|
2024-09-10 14:39:33 +00:00
|
|
|
function removeInlinePreview() {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor) {
|
|
|
|
editor.setDecorations(inlinePreviewDecoration, []);
|
|
|
|
}
|
|
|
|
currentPreviewText = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function acceptCompletion() {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor && currentPreviewText) {
|
|
|
|
editor.edit(editBuilder => {
|
|
|
|
const position = editor.selection.active;
|
|
|
|
editBuilder.insert(position, currentPreviewText as string);
|
|
|
|
});
|
|
|
|
removeInlinePreview();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 09:36:55 +00:00
|
|
|
function activate(context: vscode.ExtensionContext) {
|
2024-09-10 14:39:33 +00:00
|
|
|
inlinePreviewDecoration = createInlinePreviewDecoration();
|
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
const completionProvider = vscode.languages.registerCompletionItemProvider("*", {
|
|
|
|
provideCompletionItems
|
|
|
|
},
|
|
|
|
...completionKeys.split("")
|
|
|
|
);
|
2024-09-10 14:39:33 +00:00
|
|
|
|
|
|
|
const acceptCompletionCommand = vscode.commands.registerCommand(
|
|
|
|
"fabelous-autocoder.acceptCompletion",
|
|
|
|
acceptCompletion
|
2024-08-13 20:22:53 +00:00
|
|
|
);
|
2024-09-10 14:39:33 +00:00
|
|
|
|
2024-08-13 20:22:53 +00:00
|
|
|
context.subscriptions.push(completionProvider);
|
2024-09-10 14:39:33 +00:00
|
|
|
context.subscriptions.push(acceptCompletionCommand);
|
|
|
|
|
|
|
|
// Handle Escape key to remove preview
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('type', args => {
|
|
|
|
if (args.text === 'Escape') {
|
|
|
|
removeInlinePreview();
|
|
|
|
}
|
|
|
|
return vscode.commands.executeCommand('default:type', args);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Remove preview when cursor moves
|
|
|
|
context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(() => {
|
|
|
|
removeInlinePreview();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateInlinePreview(document: vscode.TextDocument, position: vscode.Position) {
|
|
|
|
if (currentPreviewText) {
|
|
|
|
const editor = vscode.window.activeTextEditor;
|
|
|
|
if (editor && editor.document === document) {
|
|
|
|
const decorations = [{
|
|
|
|
range: new vscode.Range(position, position),
|
|
|
|
renderOptions: {
|
|
|
|
after: {
|
|
|
|
contentText: currentPreviewText
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
editor.setDecorations(inlinePreviewDecoration, decorations);
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 09:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deactivate() { }
|
|
|
|
|
2024-09-10 14:39:33 +00:00
|
|
|
export {
|
2024-08-13 20:22:53 +00:00
|
|
|
activate,
|
|
|
|
deactivate,
|
2024-09-10 14:39:33 +00:00
|
|
|
};
|