This commit is contained in:
Falko Victor Habel 2024-08-14 07:31:21 +02:00
parent 74a467bbd4
commit 1a98cfe1e1
1 changed files with 55 additions and 71 deletions

View File

@ -5,7 +5,6 @@ let VSConfig: vscode.WorkspaceConfiguration;
let apiEndpoint: string; let apiEndpoint: string;
let apiAuthentication: string; let apiAuthentication: string;
let apiModel: string; let apiModel: string;
let apiMessageHeader: string;
let apiTemperature: number; let apiTemperature: number;
let numPredict: number; let numPredict: number;
let promptWindowSize: number; let promptWindowSize: number;
@ -21,8 +20,7 @@ function updateVSConfig() {
VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder"); VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder");
apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate"; apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate";
apiAuthentication = VSConfig.get("authentication") || ""; apiAuthentication = VSConfig.get("authentication") || "";
apiModel = VSConfig.get("model") || "fabelous-coder:latest"; // Updated to use FIM model apiModel = VSConfig.get("model") || "fabelous-coder:latest";
apiMessageHeader = VSConfig.get("message header") || "";
numPredict = VSConfig.get("max tokens predicted") || 1000; numPredict = VSConfig.get("max tokens predicted") || 1000;
promptWindowSize = VSConfig.get("prompt window size") || 2000; promptWindowSize = VSConfig.get("prompt window size") || 2000;
completionKeys = VSConfig.get("completion keys") || " "; completionKeys = VSConfig.get("completion keys") || " ";
@ -38,21 +36,12 @@ function updateVSConfig() {
updateVSConfig(); updateVSConfig();
vscode.workspace.onDidChangeConfiguration(updateVSConfig); vscode.workspace.onDidChangeConfiguration(updateVSConfig);
function messageHeaderSub(document: vscode.TextDocument) {
const sub = apiMessageHeader
.replace("{LANG}", document.languageId)
.replace("{FILE_NAME}", document.fileName)
.replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled");
return sub;
}
function getContextLines(document: vscode.TextDocument, position: vscode.Position): string { function getContextLines(document: vscode.TextDocument, position: vscode.Position): string {
const lines = []; const lines = [];
const lineCount = document.lineCount; const lineCount = document.lineCount;
// Get more context for FIM const startLine = Math.max(0, position.line - promptWindowSize / 2);
const startLine = Math.max(0, position.line - 10); const endLine = Math.min(lineCount - 1, position.line + promptWindowSize / 2);
const endLine = Math.min(lineCount - 1, position.line + 10);
for (let i = startLine; i <= endLine; i++) { for (let i = startLine; i <= endLine; i++) {
lines.push(document.lineAt(i).text); lines.push(document.lineAt(i).text);
@ -61,24 +50,21 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio
return lines.join("\n"); return lines.join("\n");
} }
function createFIMPrompt(prefix: string, suffix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_suffix>${suffix}<fim_middle>\`\`\`${language}\n`;
}
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document; const document = textEditor.document;
const position = textEditor.selection.active; const position = textEditor.selection.active;
// Get the current context
const context = getContextLines(document, position); const context = getContextLines(document, position);
// Split the context into prefix and suffix for FIM
const lines = context.split("\n"); const lines = context.split("\n");
const currentLineIndex = position.line - Math.max(0, position.line - 10); const currentLineIndex = position.line - Math.max(0, position.line - promptWindowSize / 2);
const prefix = lines.slice(0, currentLineIndex + 1).join("\n"); const prefix = lines.slice(0, currentLineIndex + 1).join("\n");
const suffix = lines.slice(currentLineIndex + 1).join("\n"); const suffix = lines.slice(currentLineIndex + 1).join("\n");
// Create FIM prompt const fimPrompt = createFIMPrompt(prefix, suffix, document.languageId);
const fimPrompt = `<fim_prefix>${prefix}<fim_suffix>${suffix}<fim_middle>`;
// Replace {Prompt} with the FIM prompt
const sub = messageHeaderSub(document).replace("{PROMPT}", fimPrompt);
vscode.window.withProgress( vscode.window.withProgress(
{ {
@ -92,19 +78,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
let axiosCancelPost: () => void; let axiosCancelPost: () => void;
const axiosCancelToken = new axios.CancelToken((c) => { const axiosCancelToken = new axios.CancelToken((c) => {
const cancelPost = function () { axiosCancelPost = () => {
c("Autocompletion request terminated by user cancel"); c("Autocompletion request terminated by user cancel");
}; };
if (cancellationToken) cancellationToken.onCancellationRequested(axiosCancelPost);
axiosCancelPost = cancelPost; progressCancellationToken.onCancellationRequested(axiosCancelPost);
if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost); vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
progressCancellationToken.onCancellationRequested(cancelPost);
vscode.workspace.onDidCloseTextDocument(cancelPost);
}); });
const response = await axios.post(apiEndpoint, { const response = await axios.post(apiEndpoint, {
model: apiModel, model: apiModel,
prompt: sub, prompt: fimPrompt,
stream: true, stream: true,
raw: true, raw: true,
options: { options: {
@ -124,7 +108,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
let completionText = ""; let completionText = "";
response.data.on('data', async (d: Uint8Array) => { response.data.on('data', async (d: Uint8Array) => {
progress.report({ message: "Generating..." }); progress.report({ message: "Generating..." });
if (currentPosition.line != textEditor.selection.end.line || currentPosition.character != textEditor.selection.end.character) { if (currentPosition.line !== textEditor.selection.end.line || currentPosition.character !== textEditor.selection.end.character) {
axiosCancelPost(); axiosCancelPost();
return; return;
} }
@ -145,10 +129,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
currentPosition.line + completionLines.length - 1, currentPosition.line + completionLines.length - 1,
(completionLines.length > 1 ? 0 : currentPosition.character) + completionLines[completionLines.length - 1].length (completionLines.length > 1 ? 0 : currentPosition.character) + completionLines[completionLines.length - 1].length
); );
const newSelection = new vscode.Selection( const newSelection = new vscode.Selection(position, newPosition);
position,
newPosition
);
currentPosition = newPosition; currentPosition = newPosition;
progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) }); progress.report({ message: "Generating...", increment: 1 / (numPredict / 100) });
@ -166,7 +147,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
}); });
await finished; await finished;
// Remove any remaining FIM tokens from the completion
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, ''); completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '');
const finalEdit = new vscode.WorkspaceEdit(); const finalEdit = new vscode.WorkspaceEdit();
finalEdit.replace(document.uri, new vscode.Range(position, currentPosition), completionText); finalEdit.replace(document.uri, new vscode.Range(position, currentPosition), completionText);
@ -186,52 +166,56 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
const item = new vscode.CompletionItem("Fabelous autocompletion"); const item = new vscode.CompletionItem("Fabelous autocompletion");
item.insertText = new vscode.SnippetString('${1:}'); item.insertText = new vscode.SnippetString('${1:}');
if (responsePreview) await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) {
return [ item ];
}
if (responsePreview) { if (responsePreview) {
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) {
return [ item ];
}
const context = getContextLines(document, position); const context = getContextLines(document, position);
const lines = context.split("\n"); const lines = context.split("\n");
const currentLineIndex = position.line - Math.max(0, position.line - 10); const currentLineIndex = position.line - Math.max(0, position.line - promptWindowSize / 2);
const prefix = lines.slice(0, currentLineIndex + 1).join("\n"); const prefix = lines.slice(0, currentLineIndex + 1).join("\n");
const suffix = lines.slice(currentLineIndex + 1).join("\n"); const suffix = lines.slice(currentLineIndex + 1).join("\n");
const fimPrompt = `<fim_prefix>${prefix}<fim_suffix>${suffix}<fim_middle>`; const fimPrompt = createFIMPrompt(prefix, suffix, document.languageId);
const response_preview = await axios.post(apiEndpoint, { try {
model: apiModel, const response_preview = await axios.post(apiEndpoint, {
prompt: messageHeaderSub(document) + fimPrompt, model: apiModel,
stream: false, prompt: fimPrompt,
raw: true, stream: false,
options: { raw: true,
num_predict: responsePreviewMaxTokens, options: {
temperature: apiTemperature, num_predict: responsePreviewMaxTokens,
stop: ['<fim_suffix>', '\n', '```'], temperature: apiTemperature,
...keepAlive && { keep_alive: keepAlive }, stop: ['<fim_suffix>', '\n', '```'],
...topP && { top_p: topP }, ...(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() !== "") {
const previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
item.label = previewText;
item.insertText = previewText;
} }
}, { } catch (error) {
cancelToken: new axios.CancelToken((c) => { console.error("Error fetching preview:", error);
const cancelPost = function () {
c("Autocompletion request terminated by completion cancel");
};
cancellationToken.onCancellationRequested(cancelPost);
})
});
if (response_preview.data.response.trim() != "") {
const previewText = response_preview.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trimStart();
item.label = previewText;
item.insertText = previewText;
} }
} }
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder'); item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder');
if (continueInline || !responsePreview) item.command = { if (continueInline || !responsePreview) {
command: 'fabelous-autocoder.autocomplete', item.command = {
title: 'Fabelous Autocomplete', command: 'fabelous-autocoder.autocomplete',
arguments: [cancellationToken] title: 'Fabelous Autocomplete',
}; arguments: [cancellationToken]
};
}
return [item]; return [item];
} }