Fabelous-Autocoder/src/extension.ts

254 lines
9.9 KiB
TypeScript
Raw Normal View History

2023-12-20 09:36:55 +00:00
import * as vscode from "vscode";
import axios from "axios";
let VSConfig: vscode.WorkspaceConfiguration;
let apiEndpoint: string;
let apiAuthentication: string;
let apiModel: string;
let apiTemperature: number;
let numPredict: number;
let promptWindowSize: number;
let completionKeys: string;
let responsePreview: boolean | undefined;
let responsePreviewMaxTokens: number;
2024-01-28 02:22:47 +00:00
let responsePreviewDelay: number;
let continueInline: boolean | undefined;
let keepAlive: number | undefined;
let topP: number | undefined;
2024-06-02 14:48:14 +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") || " ";
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;
}
updateVSConfig();
vscode.workspace.onDidChangeConfiguration(updateVSConfig);
2024-08-13 20:22:53 +00:00
function getContextLines(document: vscode.TextDocument, position: vscode.Position): string {
const lines = [];
const lineCount = document.lineCount;
2024-08-14 05:31:21 +00:00
const startLine = Math.max(0, position.line - promptWindowSize / 2);
const endLine = Math.min(lineCount - 1, position.line + promptWindowSize / 2);
2024-08-13 20:22:53 +00:00
for (let i = startLine; i <= endLine; i++) {
lines.push(document.lineAt(i).text);
}
return lines.join("\n");
}
2024-08-14 05:31:21 +00:00
function createFIMPrompt(prefix: string, suffix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${suffix}\n\`\`\`${language}\n`;
2024-08-14 05:31:21 +00:00
}
function removeMatchingPrefix(generatedContent: string): string {
const functionHeaderPatterns = [
// Java, C#, TypeScript, JavaScript
/^(public|private|protected)?\s*(static\s+)?(async\s+)?\w+(\s*<[^>]+>)?\s+\w+\s*\([^)]*\)\s*{/m,
// Python
/^def\s+\w+\s*\([^)]*\):/m,
// C++, C
/^(\w+\s+)*\w+\s+\w+\s*\([^)]*\)\s*{/m,
// Go
/^func\s+\w+\s*\([^)]*\)(\s+\w+)?\s*{/m,
// Rust
/^(pub\s+)?(fn|async fn)\s+\w+\s*(<[^>]+>)?\s*\([^)]*\)(\s*->\s*\w+)?\s*{/m
];
const lines = generatedContent.split('\n');
const firstNonEmptyLineIndex = lines.findIndex(line => line.trim() !== '');
if (firstNonEmptyLineIndex === -1) {
return '\n' + generatedContent;
}
const firstLine = lines[firstNonEmptyLineIndex];
for (const pattern of functionHeaderPatterns) {
if (pattern.test(firstLine)) {
lines.splice(firstNonEmptyLineIndex, 1);
return '\n' + lines.join('\n');
}
}
return '\n' + generatedContent;
}
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
2024-08-13 20:22:53 +00:00
const context = getContextLines(document, position);
const lines = context.split("\n");
2024-08-14 05:31:21 +00:00
const currentLineIndex = position.line - Math.max(0, position.line - promptWindowSize / 2);
2024-08-13 20:22:53 +00:00
const prefix = lines.slice(0, currentLineIndex + 1).join("\n");
const suffix = lines.slice(currentLineIndex + 1).join("\n");
2024-08-14 05:31:21 +00:00
const fimPrompt = createFIMPrompt(prefix, suffix, document.languageId);
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
2024-08-13 20:22:53 +00:00
title: "Fabelous Autocoder",
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 = () => {
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);
});
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,
stream: false,
raw: true,
options: {
num_predict: numPredict,
temperature: apiTemperature,
2024-08-13 20:22:53 +00:00
stop: ["<fim_suffix>", "```"]
}
}, {
cancelToken: axiosCancelToken,
headers: {
'Authorization': apiAuthentication
}
});
progress.report({ message: "Generating..." });
2024-08-13 20:22:53 +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
completionText = removeMatchingPrefix(completionText);
// Apply the edit
const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, position, completionText);
await vscode.workspace.applyEdit(edit);
// Move the cursor to the end of the inserted text
const newPosition = position.translate(0, completionText.length);
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);
}
}
);
}
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:}');
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);
const lines = context.split("\n");
2024-08-14 05:31:21 +00:00
const currentLineIndex = position.line - Math.max(0, position.line - promptWindowSize / 2);
2024-08-13 20:22:53 +00:00
const prefix = lines.slice(0, currentLineIndex + 1).join("\n");
const suffix = lines.slice(currentLineIndex + 1).join("\n");
2024-08-14 05:31:21 +00:00
const fimPrompt = createFIMPrompt(prefix, suffix, document.languageId);
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 }),
}
}, {
cancelToken: new axios.CancelToken((c) => {
cancellationToken.onCancellationRequested(() => c("Autocompletion request terminated by completion cancel"));
})
});
if (response_preview.data.response.trim() !== "") {
let previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
// Remove matching prefix and repeated function header
const existingContent = document.getText();
previewText = removeMatchingPrefix(existingContent);
if (previewText.trim() !== "") {
item.label = previewText;
item.insertText = previewText;
}
2024-08-13 20:22:53 +00:00
}
2024-08-14 05:31:21 +00:00
} catch (error) {
console.error("Error fetching preview:", error);
2024-08-13 20:22:53 +00:00
}
}
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder');
2024-08-14 05:31:21 +00:00
if (continueInline || !responsePreview) {
item.command = {
command: 'fabelous-autocoder.autocomplete',
title: 'Fabelous Autocomplete',
arguments: [cancellationToken]
};
}
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
2023-12-20 09:36:55 +00:00
function activate(context: vscode.ExtensionContext) {
2024-08-13 20:22:53 +00:00
const completionProvider = vscode.languages.registerCompletionItemProvider("*", {
provideCompletionItems
},
...completionKeys.split("")
);
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"fabelous-autocoder.autocomplete",
(textEditor, _, cancellationToken?) => {
autocompleteCommand(textEditor, cancellationToken);
}
);
context.subscriptions.push(completionProvider);
context.subscriptions.push(externalAutocompleteCommand);
2023-12-20 09:36:55 +00:00
}
function deactivate() { }
module.exports = {
2024-08-13 20:22:53 +00:00
activate,
deactivate,
};