137 lines
4.2 KiB
TypeScript
137 lines
4.2 KiB
TypeScript
// The module 'vscode' contains the VS Code extensibility API
|
|
// Import the module and reference it with the alias vscode in your code below
|
|
import * as vscode from 'vscode';
|
|
import { Ollama } from 'ollama/browser';
|
|
|
|
const ollama = new Ollama({
|
|
// host: 'http://defiant:11434'
|
|
});
|
|
|
|
// This method is called when your extension is activated
|
|
// Your extension is activated the very first time the command is executed
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
|
|
// Use the console to output diagnostic information (console.log) and errors (console.error)
|
|
// This line of code will only be executed once when your extension is activated
|
|
console.log('Congratulations, your extension "ai-code" is now active!');
|
|
|
|
// The command has been defined in the package.json file
|
|
// Now provide the implementation of the command with registerCommand
|
|
// The commandId parameter must match the command field in package.json
|
|
const disposable = vscode.commands.registerCommand('ai-code.helloWorld', async () => {
|
|
// The code you place here will be executed every time your command is executed
|
|
// Display a message box to the user
|
|
// vscode.window.showInformationMessage('Hello world!');
|
|
// try {
|
|
// vscode.window.showInformationMessage("asking ollama");
|
|
// const response = await ollama.chat({
|
|
// model: 'deepseek-coder:1.3b',
|
|
// messages: [{ role: 'user', content: 'Why is the sky blue?' }],
|
|
// stream: true,
|
|
// });
|
|
// for await (const part of response) {
|
|
// vscode.window.showInformationMessage(part.message.content);
|
|
// }
|
|
// // vscode.window.showInformationMessage(response.message.content);
|
|
// }
|
|
// catch (err) {
|
|
// console.log(err)
|
|
// }
|
|
});
|
|
|
|
const provider: vscode.InlineCompletionItemProvider = {
|
|
async provideInlineCompletionItems(document, position, _context, _token) {
|
|
console.log('provideInlineCompletionItems triggered');
|
|
|
|
try {
|
|
|
|
const DOCUMENT_START = '<file>'
|
|
const DOCUMENT_END = '</file>'
|
|
|
|
const MESSAGE_HEADER = `\n{PROJECT_NAME}\nfile {FILE_NAME}\nlanguage {LANG}\n${DOCUMENT_START}\n`;
|
|
|
|
const prefix = document.getText(new vscode.Range(0, 0, position.line, position.character))
|
|
const suffix = document.getText(new vscode.Range(position.line, position.character, document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length))
|
|
|
|
const prompt = MESSAGE_HEADER
|
|
.replace("{PROJECT_NAME}", vscode.workspace.name || "Untitled")
|
|
.replace("{FILE_NAME}", document.fileName)
|
|
.replace("{LANG}", document.languageId) + prefix;
|
|
|
|
|
|
const response = await ollama.generate({
|
|
model: 'deepseek-r1:8b',
|
|
prompt,
|
|
suffix,
|
|
raw: true,
|
|
stream: true,
|
|
options: {
|
|
num_predict: 10,
|
|
stop: [DOCUMENT_END]
|
|
},
|
|
})
|
|
|
|
const buffer = []
|
|
for await (const part of response) {
|
|
process.stdout.write(part.response)
|
|
buffer.push(part.response)
|
|
}
|
|
|
|
return [
|
|
{
|
|
insertText: buffer.join(''),
|
|
range: new vscode.Range(position, position),
|
|
}
|
|
]
|
|
} catch (err) {
|
|
console.log(err)
|
|
}
|
|
|
|
return []
|
|
|
|
// const regexp = /\/\/ \[(.+?),(.+?)\)(.*?):(.*)/;
|
|
// if (position.line <= 0) {
|
|
// return;
|
|
// }
|
|
|
|
// const result: vscode.InlineCompletionItem[] = []
|
|
|
|
// let offset = 1;
|
|
// while (offset > 0) {
|
|
// if (position.line - offset < 0) {
|
|
// break;
|
|
// }
|
|
|
|
// const lineBefore = document.lineAt(position.line - offset).text;
|
|
// const matches = lineBefore.match(regexp);
|
|
// if (!matches) {
|
|
// break;
|
|
// }
|
|
// offset++;
|
|
|
|
// const start = matches[1];
|
|
// const startInt = parseInt(start, 10);
|
|
// const end = matches[2];
|
|
// const endInt =
|
|
// end === '*'
|
|
// ? document.lineAt(position.line).text.length
|
|
// : parseInt(end, 10);
|
|
// const text = matches[4].replace(/\\n/g, '\n');
|
|
|
|
// result.push({
|
|
// insertText: text,
|
|
// range: new vscode.Range(position.line, startInt, position.line, endInt),
|
|
// });
|
|
// }
|
|
|
|
// return result;
|
|
},
|
|
};
|
|
|
|
vscode.languages.registerInlineCompletionItemProvider({ pattern: '**' }, provider);
|
|
|
|
context.subscriptions.push(disposable);
|
|
}
|
|
|
|
// This method is called when your extension is deactivated
|
|
export function deactivate() {}
|