develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +00:00
1 changed files with 168 additions and 283 deletions
Showing only changes of commit 424cc124b2 - Show all commits

View File

@ -1,269 +1,179 @@
import * as vscode from "vscode"; import * as vscode from 'vscode';
import axios from "axios"; import axios from 'axios';
let VSConfig: vscode.WorkspaceConfiguration; let config: {
let apiEndpoint: string; apiEndpoint: string;
let apiAuthentication: string; apiAuthentication: string;
let apiModel: string; apiModel: string;
let apiTemperature: number; apiTemperature: number;
let numPredict: number; numPredict: number;
let promptWindowSize: number; promptWindowSize: number;
let completionKeys: string; completionKeys: string[];
let responsePreview: boolean | undefined; responsePreview: boolean;
let responsePreviewMaxTokens: number; responsePreviewMaxTokens: number;
let responsePreviewDelay: number; responsePreviewDelay: number;
let continueInline: boolean | undefined; continueInline: boolean;
let keepAlive: number | undefined; keepAlive: number;
let topP: number | undefined; topP: number;
};
function updateVSConfig() { let previewDecorationType: vscode.TextEditorDecorationType;
VSConfig = vscode.workspace.getConfiguration("fabelous-autocoder");
apiEndpoint = VSConfig.get("endpoint") || "http://localhost:11434/api/generate"; function updateConfig() {
apiAuthentication = VSConfig.get("authentication") || ""; const vsConfig = vscode.workspace.getConfiguration('fabelous-autocoder');
apiModel = VSConfig.get("model") || "fabelous-coder:latest"; config = {
numPredict = VSConfig.get("max tokens predicted") || 1000; apiEndpoint: vsConfig.get('endpoint') || 'http://localhost:11434/api/generate',
promptWindowSize = VSConfig.get("prompt window size") || 2000; apiAuthentication: vsConfig.get('authentication') || '',
completionKeys = VSConfig.get("completion keys") || " "; apiModel: vsConfig.get('model') || 'fabelous-coder:latest',
responsePreview = VSConfig.get("response preview"); apiTemperature: vsConfig.get('temperature') || 0.7,
responsePreviewMaxTokens = VSConfig.get("preview max tokens") || 50; numPredict: vsConfig.get('max tokens predicted') || 1000,
responsePreviewDelay = VSConfig.get("preview delay") || 0; promptWindowSize: vsConfig.get('prompt window size') || 2000,
continueInline = VSConfig.get("continue inline"); completionKeys: (vsConfig.get('completion keys') as string || ' ').split(''),
apiTemperature = VSConfig.get("temperature") || 0.7; responsePreview: vsConfig.get('response preview') || false,
keepAlive = VSConfig.get("keep alive") || 30; responsePreviewMaxTokens: vsConfig.get('preview max tokens') || 50,
topP = VSConfig.get("top p") || 1; responsePreviewDelay: vsConfig.get('preview delay') || 0,
continueInline: vsConfig.get('continue inline') || false,
keepAlive: vsConfig.get('keep alive') || 30,
topP: vsConfig.get('top p') || 1,
};
} }
updateVSConfig(); function createPreviewDecorationType() {
vscode.workspace.onDidChangeConfiguration(updateVSConfig); previewDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: '#888888',
fontStyle: 'italic',
},
textDecoration: 'none; display: none;',
});
}
function getContextLines(document: vscode.TextDocument, position: vscode.Position): string { function getContextLines(document: vscode.TextDocument, position: vscode.Position): string {
const lines = [];
const startLine = Math.max(0, position.line - 1); const startLine = Math.max(0, position.line - 1);
const endLine = position.line; const endLine = position.line;
return document.getText(new vscode.Range(startLine, 0, endLine, position.character));
for (let i = startLine; i <= endLine; i++) {
lines.push(document.lineAt(i).text);
}
return lines.join("\n");
} }
function createFIMPrompt(prefix: string, language: string): string { function createFIMPrompt(prefix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`; return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
} }
const previewDecorationType = vscode.window.createTextEditorDecorationType({ async function generateCompletion(prompt: string, cancellationToken: vscode.CancellationToken): Promise<string> {
after: { const axiosCancelToken = new axios.CancelToken((c) => {
color: '#888888', // Grayed-out preview text cancellationToken.onCancellationRequested(() => c('Request cancelled'));
fontStyle: 'italic', });
},
textDecoration: 'none; display: none;', // Hide the original text
});
const response = await axios.post(config.apiEndpoint, {
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) { model: config.apiModel,
const document = textEditor.document; prompt: prompt,
const position = textEditor.selection.active; stream: false,
const contextLines = 2; raw: true,
const startLine = Math.max(0, position.line - contextLines); options: {
const context = getContextLines(document, position); num_predict: config.numPredict + 100, // Generate extra lines for preview
let isHandlingChange = false; temperature: config.apiTemperature,
const fimPrompt = createFIMPrompt(context, document.languageId); stop: ['<fim_suffix>', '```'],
keep_alive: config.keepAlive,
vscode.window.withProgress( top_p: config.topP,
{
location: vscode.ProgressLocation.Notification,
title: "Fabelous Autocoder",
cancellable: true,
},
async (progress, progressCancellationToken) => {
try {
progress.report({ message: "Starting model..." });
let axiosCancelPost: () => void;
const axiosCancelToken = new axios.CancelToken((c) => {
axiosCancelPost = () => {
c("Autocompletion request terminated by user cancel");
};
if (cancellationToken) cancellationToken.onCancellationRequested(axiosCancelPost);
progressCancellationToken.onCancellationRequested(axiosCancelPost);
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
});
// Make the API request
const response = await axios.post(apiEndpoint, {
model: apiModel,
prompt: fimPrompt,
stream: false,
raw: true,
options: {
num_predict: numPredict,
temperature: apiTemperature,
stop: ["<fim_suffix>", "```"]
}
}, {
cancelToken: axiosCancelToken,
headers: {
'Authorization': apiAuthentication
}
});
progress.report({ message: "Generating..." });
let completionText = response.data.response;
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
let previewInserted = false;
let originalContent: string;
let previewStartLine: number;
let previewEndLine: number;
const storeAndInsertPreview = async () => {
const currentLine = position.line;
previewStartLine = currentLine;
const previewLines = completionText.split('\n');
previewEndLine = previewStartLine + previewLines.length;
// Ensure the previewEndLine doesn't exceed the document's line count
const documentEndLine = document.lineCount - 1;
previewEndLine = Math.min(previewEndLine, documentEndLine + 1);
// Store original content
const fullRange = new vscode.Range(previewStartLine, 0, previewEndLine, document.lineAt(previewEndLine - 1).text.length);
originalContent = document.getText(fullRange);
// Prepare the new content
const newContent = previewLines.join('\n');
const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, fullRange, newContent);
await vscode.workspace.applyEdit(edit);
// Highlight only the new lines
const previewRanges: vscode.DecorationOptions[] = previewLines.map((line: string, index: number) => ({
range: new vscode.Range(previewStartLine + index, 0, previewStartLine + index, line.length),
renderOptions: {
after: {
contentText: line,
}
}
}));
textEditor.setDecorations(previewDecorationType, previewRanges);
previewInserted = true;
};
const disposable = vscode.window.onDidChangeTextEditorSelection(async (event) => {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor || !previewInserted || isHandlingChange) {
return;
}
isHandlingChange = true;
try {
const activeSelection = textEditor.selection;
const changeStartLine = activeSelection.active.line;
if (event.kind === vscode.TextEditorSelectionChangeKind.Keyboard && changeStartLine >= previewStartLine) {
const changeText = textEditor.document.getText(activeSelection);
if (changeText === '') {
// Tab key (empty selection) -> Accept the preview
await acceptPreview(textEditor, textEditor.document, startLine, activeSelection.active, completionText);
}
}
} finally {
isHandlingChange = false;
}
});
vscode.workspace.onDidChangeTextDocument(async (event) => {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted || isHandlingChange) {
return;
}
isHandlingChange = true;
try {
for (const change of event.contentChanges) {
const changeStartLine = change.range.start.line;
if (change.text.includes('\n') && changeStartLine >= previewStartLine) {
await acceptPreview(textEditor, textEditor.document, startLine, textEditor.selection.active, completionText);
await vscode.commands.executeCommand('default:type', { text: '\n' });
break;
}
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
await restoreOriginalContent();
break;
}
}
} finally {
isHandlingChange = false;
}
});
const restoreOriginalContent = async () => {
if (!previewInserted) return;
const fullRange = new vscode.Range(previewStartLine, 0, previewEndLine, 0);
const edit = new vscode.WorkspaceEdit();
edit.replace(document.uri, fullRange, originalContent);
await vscode.workspace.applyEdit(edit);
textEditor.setDecorations(previewDecorationType, []);
previewInserted = false;
disposable.dispose(); // Cancel listener when preview is discarded
};
const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit();
const previewLines = completionText.split('\n');
const endLine = startLine + previewLines.length;
// Ensure that endLine does not exceed document bounds
const documentEndLine = document.lineCount - 1;
const finalEndLine = Math.min(endLine, documentEndLine + 1);
// Prepare the new content
const newContent = previewLines.join('\n');
// Replace the range with the new content
const replaceRange = new vscode.Range(startLine, 0, finalEndLine, document.lineAt(finalEndLine - 1).text.length);
edit.replace(document.uri, replaceRange, newContent.trim());
await vscode.workspace.applyEdit(edit);
await document.save();
disposable.dispose(); // Cancel listener when preview is accepted
previewInserted = false;
};
await storeAndInsertPreview();
} catch (err: any) {
vscode.window.showErrorMessage(
"Fabelous Autocoder encountered an error: " + err.message
);
console.log(err);
}
} }
); }, {
cancelToken: axiosCancelToken,
headers: {
'Authorization': config.apiAuthentication
}
});
const fullCompletion = response.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
const completionLines = fullCompletion.split('\n');
const usedContextLines = prompt.split('\n').length;
// Remove used context and take extra lines for preview
return completionLines.slice(usedContextLines, usedContextLines + config.numPredict).join('\n');
} }
class CompletionManager {
private textEditor: vscode.TextEditor;
private document: vscode.TextDocument;
private startPosition: vscode.Position;
private completionText: string;
private previewLines: string[];
constructor(textEditor: vscode.TextEditor, startPosition: vscode.Position, completionText: string) {
this.textEditor = textEditor;
this.document = textEditor.document;
this.startPosition = startPosition;
this.completionText = completionText;
this.previewLines = completionText.split('\n');
}
public async showPreview() {
const previewRanges: vscode.DecorationOptions[] = this.previewLines.map((line, index) => ({
range: new vscode.Range(this.startPosition.translate(index, 0), this.startPosition.translate(index, 0)),
renderOptions: {
after: {
contentText: line,
}
}
}));
this.textEditor.setDecorations(previewDecorationType, previewRanges);
}
public async acceptCompletion() {
const edit = new vscode.WorkspaceEdit();
edit.insert(this.document.uri, this.startPosition, this.completionText);
await vscode.workspace.applyEdit(edit);
this.clearPreview();
}
public clearPreview() {
this.textEditor.setDecorations(previewDecorationType, []);
}
}
async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) {
const cancellationTokenSource = new vscode.CancellationTokenSource();
const cancellationToken = cancellationTokenSource.token;
try {
const document = textEditor.document;
const position = textEditor.selection.active;
const context = getContextLines(document, position);
const fimPrompt = createFIMPrompt(context, document.languageId);
const completionText = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: 'Fabelous Autocoder',
cancellable: true,
}, async (progress, progressCancellationToken) => {
progress.report({ message: 'Generating...' });
return await generateCompletion(fimPrompt, progressCancellationToken);
});
const completionManager = new CompletionManager(textEditor, position, completionText);
await completionManager.showPreview();
const disposable = vscode.window.onDidChangeTextEditorSelection(async (event) => {
if (event.textEditor !== textEditor) return;
if (event.kind === vscode.TextEditorSelectionChangeKind.Keyboard) {
await completionManager.acceptCompletion();
disposable.dispose();
}
});
} catch (err: any) {
vscode.window.showErrorMessage(`Fabelous Autocoder encountered an error: ${err.message}`);
console.error(err);
} finally {
cancellationTokenSource.dispose();
}
}
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
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:}');
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder');
if (responsePreview) { if (config.responsePreview) {
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000)); await new Promise(resolve => setTimeout(resolve, config.responsePreviewDelay * 1000));
if (cancellationToken.isCancellationRequested) { if (cancellationToken.isCancellationRequested) {
return [item]; return [item];
} }
@ -272,58 +182,33 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
const fimPrompt = createFIMPrompt(context, document.languageId); const fimPrompt = createFIMPrompt(context, document.languageId);
try { try {
const response_preview = await axios.post(apiEndpoint, { const previewText = await generateCompletion(fimPrompt, cancellationToken);
model: apiModel, item.detail = previewText.split('\n')[0]; // Show first line as preview
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"));
})
});
} catch (error) { } catch (error) {
console.error("Error fetching preview:", error); console.error('Error fetching preview:', error);
} }
} }
item.documentation = new vscode.MarkdownString('Press `Enter` to get an autocompletion from Fabelous Autocoder'); if (config.continueInline || !config.responsePreview) {
if (continueInline || !responsePreview) {
item.command = { item.command = {
command: 'fabelous-autocoder.autocomplete', command: 'fabelous-autocoder.autocomplete',
title: 'Fabelous Autocomplete', title: 'Fabelous Autocomplete',
arguments: [cancellationToken] arguments: []
}; };
} }
return [item]; return [item];
} }
function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
const completionProvider = vscode.languages.registerCompletionItemProvider("*", { updateConfig();
provideCompletionItems createPreviewDecorationType();
},
...completionKeys.split("") context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(updateConfig),
vscode.languages.registerCompletionItemProvider('*', { provideCompletionItems }, ...config.completionKeys),
vscode.commands.registerTextEditorCommand('fabelous-autocoder.autocomplete', autocompleteCommand)
); );
const externalAutocompleteCommand = vscode.commands.registerTextEditorCommand(
"fabelous-autocoder.autocomplete",
(textEditor, _, cancellationToken?) => {
autocompleteCommand(textEditor, cancellationToken);
}
);
context.subscriptions.push(completionProvider);
context.subscriptions.push(externalAutocompleteCommand);
} }
function deactivate() { } export function deactivate() {}
module.exports = {
activate,
deactivate,
};