-
Notifications
You must be signed in to change notification settings - Fork 97
/
provider.ts
171 lines (146 loc) · 6.83 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import vscode from 'vscode';
import { info, warn } from '../modules/log';
import { autocomplete } from './autocomplete';
import { preparePrompt } from './preparePrompt';
import { AsyncLock } from '../modules/lock';
import { getFromPromptCache, setPromptToCache } from './promptCache';
import { isNotNeeded, isSupported } from './filter';
import { ollamaCheckModel } from '../modules/ollamaCheckModel';
import { ollamaDownloadModel } from '../modules/ollamaDownloadModel';
import { config } from '../config';
export class PromptProvider implements vscode.InlineCompletionItemProvider {
lock = new AsyncLock();
statusbar: vscode.StatusBarItem;
context: vscode.ExtensionContext;
constructor(statusbar: vscode.StatusBarItem, context: vscode.ExtensionContext) {
this.statusbar = statusbar;
this.context = context;
}
async delayCompletion(delay: number, token: vscode.CancellationToken): Promise<boolean> {
if (config.inference.delay < 0) {
return false;
}
await new Promise(p => setTimeout(p, delay));
if (token.isCancellationRequested) {
return false;
}
return true;
}
async provideInlineCompletionItems(document: vscode.TextDocument, position: vscode.Position, context: vscode.InlineCompletionContext, token: vscode.CancellationToken): Promise<vscode.InlineCompletionItem[] | vscode.InlineCompletionList | undefined | null> {
if (!await this.delayCompletion(config.inference.delay, token)) {
return;
}
try {
// Ignore unsupported documents
if (!isSupported(document)) {
info(`Unsupported document: ${document.uri.toString()} ignored.`);
return;
}
// Ignore if not needed
if (isNotNeeded(document, position, context)) {
info('No inline completion required');
return;
}
// Ignore if already canceled
if (token.isCancellationRequested) {
info(`Canceled before AI completion.`);
return;
}
// Execute in lock
return await this.lock.inLock(async () => {
// Prepare context
let prepared = await preparePrompt(document, position, context);
if (token.isCancellationRequested) {
info(`Canceled before AI completion.`);
return;
}
// Result
let res: string | null = null;
// Check if in cache
let cached = getFromPromptCache({
prefix: prepared.prefix,
suffix: prepared.suffix
});
// If not cached
if (cached === undefined) {
// Config
let inferenceConfig = config.inference;
// Update status
this.statusbar.text = `$(sync~spin) Llama Coder`;
try {
// Check model exists
let modelExists = await ollamaCheckModel(inferenceConfig.endpoint, inferenceConfig.modelName);
if (token.isCancellationRequested) {
info(`Canceled after AI completion.`);
return;
}
// Download model if not exists
if (!modelExists) {
// Check if user asked to ignore download
if (this.context.globalState.get('llama-coder-download-ignored') === inferenceConfig.modelName) {
info(`Ingoring since user asked to ignore download.`);
return;
}
// Ask for download
let download = await vscode.window.showInformationMessage(`Model ${inferenceConfig.modelName} is not downloaded. Do you want to download it? Answering "No" would require you to manually download model.`, 'Yes', 'No');
if (download === 'No') {
info(`Ingoring since user asked to ignore download.`);
this.context.globalState.update('llama-coder-download-ignored', inferenceConfig.modelName);
return;
}
// Perform download
this.statusbar.text = `$(sync~spin) Downloading`;
await ollamaDownloadModel(inferenceConfig.endpoint, inferenceConfig.modelName);
this.statusbar.text = `$(sync~spin) Llama Coder`;
}
if (token.isCancellationRequested) {
info(`Canceled after AI completion.`);
return;
}
// Run AI completion
info(`Running AI completion...`);
res = await autocomplete({
prefix: prepared.prefix,
suffix: prepared.suffix,
endpoint: inferenceConfig.endpoint,
model: inferenceConfig.modelName,
format: inferenceConfig.modelFormat,
maxLines: inferenceConfig.maxLines,
maxTokens: inferenceConfig.maxTokens,
temperature: inferenceConfig.temperature,
canceled: () => token.isCancellationRequested,
});
info(`AI completion completed: ${res}`);
// Put to cache
setPromptToCache({
prefix: prepared.prefix,
suffix: prepared.suffix,
value: res
});
} finally {
this.statusbar.text = `$(chip) Llama Coder`;
}
} else {
if (cached !== null) {
res = cached;
}
}
if (token.isCancellationRequested) {
info(`Canceled after AI completion.`);
return;
}
// Return result
if (res && res.trim() !== '') {
return [{
insertText: res,
range: new vscode.Range(position, position),
}];
}
// Nothing to complete
return;
});
} catch (e) {
warn('Error during inference:', e);
}
}
}