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: {
color: '#888888', // Grayed-out preview text
fontStyle: 'italic',
},
textDecoration: 'none; display: none;', // Hide the original text
});
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document;
const position = textEditor.selection.active;
const contextLines = 2;
const startLine = Math.max(0, position.line - contextLines);
const context = getContextLines(document, position);
let isHandlingChange = false;
const fimPrompt = createFIMPrompt(context, document.languageId);
vscode.window.withProgress(
{
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) => { const axiosCancelToken = new axios.CancelToken((c) => {
axiosCancelPost = () => { cancellationToken.onCancellationRequested(() => c('Request cancelled'));
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(config.apiEndpoint, {
const response = await axios.post(apiEndpoint, { model: config.apiModel,
model: apiModel, prompt: prompt,
prompt: fimPrompt,
stream: false, stream: false,
raw: true, raw: true,
options: { options: {
num_predict: numPredict, num_predict: config.numPredict + 100, // Generate extra lines for preview
temperature: apiTemperature, temperature: config.apiTemperature,
stop: ["<fim_suffix>", "```"] stop: ['<fim_suffix>', '```'],
keep_alive: config.keepAlive,
top_p: config.topP,
} }
}, { }, {
cancelToken: axiosCancelToken, cancelToken: axiosCancelToken,
headers: { headers: {
'Authorization': apiAuthentication 'Authorization': config.apiAuthentication
} }
}); });
progress.report({ message: "Generating..." }); 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;
let completionText = response.data.response; // Remove used context and take extra lines for preview
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim(); return completionLines.slice(usedContextLines, usedContextLines + config.numPredict).join('\n');
}
let previewInserted = false; class CompletionManager {
let originalContent: string; private textEditor: vscode.TextEditor;
let previewStartLine: number; private document: vscode.TextDocument;
let previewEndLine: number; private startPosition: vscode.Position;
private completionText: string;
private previewLines: string[];
const storeAndInsertPreview = async () => { constructor(textEditor: vscode.TextEditor, startPosition: vscode.Position, completionText: string) {
const currentLine = position.line; this.textEditor = textEditor;
previewStartLine = currentLine; this.document = textEditor.document;
const previewLines = completionText.split('\n'); this.startPosition = startPosition;
previewEndLine = previewStartLine + previewLines.length; this.completionText = completionText;
this.previewLines = completionText.split('\n');
}
// Ensure the previewEndLine doesn't exceed the document's line count public async showPreview() {
const documentEndLine = document.lineCount - 1; const previewRanges: vscode.DecorationOptions[] = this.previewLines.map((line, index) => ({
previewEndLine = Math.min(previewEndLine, documentEndLine + 1); range: new vscode.Range(this.startPosition.translate(index, 0), this.startPosition.translate(index, 0)),
// 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: { renderOptions: {
after: { after: {
contentText: line, contentText: line,
} }
} }
})); }));
textEditor.setDecorations(previewDecorationType, previewRanges); this.textEditor.setDecorations(previewDecorationType, previewRanges);
previewInserted = true; }
};
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) => { const disposable = vscode.window.onDidChangeTextEditorSelection(async (event) => {
const textEditor = vscode.window.activeTextEditor; if (event.textEditor !== textEditor) return;
if (!textEditor || !previewInserted || isHandlingChange) {
return;
}
isHandlingChange = true; if (event.kind === vscode.TextEditorSelectionChangeKind.Keyboard) {
await completionManager.acceptCompletion();
try { disposable.dispose();
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) { } catch (err: any) {
vscode.window.showErrorMessage( vscode.window.showErrorMessage(`Fabelous Autocoder encountered an error: ${err.message}`);
"Fabelous Autocoder encountered an error: " + err.message console.error(err);
); } finally {
console.log(err); 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,
};