new update to be able to replace the prompt with text from the document
This commit is contained in:
parent
71319b43ad
commit
68b7eda25c
|
@ -2,7 +2,7 @@
|
||||||
"name": "fabelous-autocoder",
|
"name": "fabelous-autocoder",
|
||||||
"displayName": "Fabelous Autocoder",
|
"displayName": "Fabelous Autocoder",
|
||||||
"description": "A simple to use Ollama autocompletion engine with options exposed and streaming functionality",
|
"description": "A simple to use Ollama autocompletion engine with options exposed and streaming functionality",
|
||||||
"version": "0.0.35",
|
"version": "0.0.4",
|
||||||
"icon": "icon.png",
|
"icon": "icon.png",
|
||||||
"publisher": "fabel",
|
"publisher": "fabel",
|
||||||
"license": "CC BY-ND 4.0",
|
"license": "CC BY-ND 4.0",
|
||||||
|
|
|
@ -53,9 +53,14 @@ function messageHeaderSub(document: vscode.TextDocument) {
|
||||||
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 prompt
|
// Get the current prompt
|
||||||
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
|
let prompt = document.getText(new vscode.Range(document.lineAt(0).range.start, position));
|
||||||
prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length);
|
prompt = prompt.substring(Math.max(0, prompt.length - promptWindowSize), prompt.length);
|
||||||
|
|
||||||
|
// Replace {Prompt} with the extracted text from the document
|
||||||
|
const sub = messageHeaderSub(document).replace("{Prompt}", prompt);
|
||||||
|
|
||||||
// Show a progress message
|
// Show a progress message
|
||||||
vscode.window.withProgress(
|
vscode.window.withProgress(
|
||||||
{
|
{
|
||||||
|
@ -66,20 +71,23 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
async (progress, progressCancellationToken) => {
|
async (progress, progressCancellationToken) => {
|
||||||
try {
|
try {
|
||||||
progress.report({ message: "Starting model..." });
|
progress.report({ message: "Starting model..." });
|
||||||
|
|
||||||
let axiosCancelPost: () => void;
|
let axiosCancelPost: () => void;
|
||||||
const axiosCancelToken = new axios.CancelToken((c) => {
|
const axiosCancelToken = new axios.CancelToken((c) => {
|
||||||
const cancelPost = function () {
|
const cancelPost = function () {
|
||||||
c("Autocompletion request terminated by user cancel");
|
c("Autocompletion request terminated by user cancel");
|
||||||
};
|
};
|
||||||
|
|
||||||
axiosCancelPost = cancelPost;
|
axiosCancelPost = cancelPost;
|
||||||
if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost);
|
if (cancellationToken) cancellationToken.onCancellationRequested(cancelPost);
|
||||||
progressCancellationToken.onCancellationRequested(cancelPost);
|
progressCancellationToken.onCancellationRequested(cancelPost);
|
||||||
vscode.workspace.onDidCloseTextDocument(cancelPost);
|
vscode.workspace.onDidCloseTextDocument(cancelPost);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Make a request to the ollama.ai REST API
|
// Make a request to the ollama.ai REST API
|
||||||
const response = await axios.post(apiEndpoint, {
|
const response = await axios.post(apiEndpoint, {
|
||||||
model: apiModel, // Change this to the model you want to use
|
model: apiModel, // Change this to the model you want to use
|
||||||
prompt: messageHeaderSub(textEditor.document) + prompt,
|
prompt: sub, // Use the modified sub string with the replaced prompt
|
||||||
stream: true,
|
stream: true,
|
||||||
raw: true,
|
raw: true,
|
||||||
options: {
|
options: {
|
||||||
|
@ -135,7 +143,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
// Keep cancel window available
|
// Keep cancel window available
|
||||||
const finished = new Promise((resolve) => {
|
const finished = new Promise((resolve) => {
|
||||||
response.data.on('end', () => {
|
response.data.on('end', () => {
|
||||||
progress.report({ message: "Ollama completion finished." });
|
progress.report({ message: "Fabelous completion finished." });
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
axiosCancelToken.promise.finally(() => { // prevent notification from freezing on user input cancel
|
axiosCancelToken.promise.finally(() => { // prevent notification from freezing on user input cancel
|
||||||
|
@ -146,7 +154,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// Show an error message
|
// Show an error message
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
"Ollama encountered an error: " + err.message
|
"Fabelous Autocoder encountered an error: " + err.message
|
||||||
);
|
);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue