-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathindex.ts
225 lines (209 loc) · 7.74 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import * as vscode from 'vscode';
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
// import { v4 as uuidv4 } from 'uuid';
let client: LanguageClient;
// We set the default configurations here becauase VS Code tries to combine keys from defaults and user provided values which is annoying to deal with
const defaultServerConfiguration =
{
"memory": {
"file_store": {}
},
"models": {
"model1": {
"type": "open_ai",
"chat_endpoint": "https://api.openai.com/v1/chat/completions",
"model": "gpt-4o",
"auth_token_env_var_name": "OPENAI_API_KEY"
}
}
}
const defaultGenerationConfiguration = {
"model": "model1",
"parameters": {
"max_tokens": 128,
"max_context": 1024,
"messages": [
{
"role": "system",
"content": "Instructions:\n- You are an AI programming assistant.\n- Given a piece of code with the cursor location marked by \"<CURSOR>\", replace \"<CURSOR>\" with the correct code or comment.\n- First, think step-by-step.\n- Describe your plan for what to build in pseudocode, written out in great detail.\n- Then output the code replacing the \"<CURSOR>\".\n- Ensure that your completion fits within the language context of the provided code snippet.\n\nRules:\n- Only respond with code or comments.\n- Only replace \"<CURSOR>\"; do not include any previously written code.\n- Never include \"<CURSOR>\" in your response.\n- If the cursor is within a comment, complete the comment meaningfully.\n- Handle ambiguous cases by providing the most contextually appropriate completion.\n- Be consistent with your responses."
},
{
"role": "user",
"content": "def greet(name):\n print(f\"Hello, {<CURSOR>}\")"
},
{
"role": "assistant",
"content": "name"
},
{
"role": "user",
"content": "function sum(a, b) {\n return a + <CURSOR>;\n}"
},
{
"role": "assistant",
"content": "b"
},
{
"role": "user",
"content": "fn multiply(a: i32, b: i32) -> i32 {\n a * <CURSOR>\n}"
},
{
"role": "assistant",
"content": "b"
},
{
"role": "user",
"content": "# <CURSOR>\ndef add(a, b):\n return a + b"
},
{
"role": "assistant",
"content": "Adds two numbers"
},
{
"role": "user",
"content": "# This function checks if a number is even\n<CURSOR>"
},
{
"role": "assistant",
"content": "def is_even(n):\n return n % 2 == 0"
},
{
"role": "user",
"content": "public class HelloWorld {\n public static void main(String[] args) {\n System.out.println(\"Hello, <CURSOR>\");\n }\n}"
},
{
"role": "assistant",
"content": "world"
},
{
"role": "user",
"content": "try:\n # Trying to open a file\n file = open(\"example.txt\", \"r\")\n # <CURSOR>\nfinally:\n file.close()"
},
{
"role": "assistant",
"content": "content = file.read()"
},
{
"role": "user",
"content": "#include <iostream>\nusing namespace std;\n\nint main() {\n int a = 5, b = 10;\n cout << \"Sum: \" << (a + <CURSOR>) << endl;\n return 0;\n}"
},
{
"role": "assistant",
"content": "b"
},
{
"role": "user",
"content": "<!DOCTYPE html>\n<html>\n<head>\n <title>My Page</title>\n</head>\n<body>\n <h1>Welcome to My Page</h1>\n <p>This is a sample page with a list of items:</p>\n <ul>\n <li>Item 1</li>\n <li>Item 2</li>\n <li><CURSOR></li>\n </ul>\n</body>\n</html>"
},
{
"role": "assistant",
"content": "Item 3"
},
{
"role": "user",
"content": "{CODE}"
}
]
}
}
export function activate(context: vscode.ExtensionContext) {
// Configure the server options
const serverOptions: ServerOptions = {
command: "lsp-ai",
transport: TransportKind.stdio,
};
// Set the serverConfiguration
let serverConfiguration;
if (Object.keys(vscode.workspace.getConfiguration("lsp-ai").serverConfiguration).length != 0) {
serverConfiguration = vscode.workspace.getConfiguration("lsp-ai").serverConfiguration;
} else {
serverConfiguration = defaultServerConfiguration;
}
// Set the generationConfiguration
let generationConfiguration;
if (Object.keys(vscode.workspace.getConfiguration("lsp-ai").generationConfiguration).length != 0) {
generationConfiguration = vscode.workspace.getConfiguration("lsp-ai").generationConfiguration;
} else {
generationConfiguration = defaultGenerationConfiguration;
}
// Set the inlineCompletionConfiguration
const inlineCompletionConfiguration = vscode.workspace.getConfiguration("lsp-ai").inlineCompletionConfiguration;
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: "file" }],
initializationOptions: serverConfiguration
};
// Create the language client and start the client
client = new LanguageClient(
'lsp-ai',
'lsp-ai',
serverOptions,
clientOptions
);
// Start the client. This will also launch the server
client.start();
// Register generate function
const generateCommand = 'lsp-ai.generation';
const generateCommandHandler = (editor: vscode.TextEditor) => {
let params = {
textDocument: {
uri: editor.document.uri.toString(),
},
position: editor.selection.active,
model: generationConfiguration.model,
parameters: generationConfiguration.parameters
};
client.sendRequest("textDocument/generation", params).then(result => {
editor.edit((edit) => {
edit.insert(editor.selection.active, result["generatedText"]);
});
}).catch(error => {
console.error("Error making generate request", error);
});
};
context.subscriptions.push(vscode.commands.registerTextEditorCommand(generateCommand, generateCommandHandler));
// Register as an inline completion provider
// We want to sort of debounce queries
let lastInlineCompletion = Date.now();
let inlineCompletionRequestCounter = 0;
vscode.languages.registerInlineCompletionItemProvider({ pattern: '**' },
{
provideInlineCompletionItems: async (document: vscode.TextDocument, position: vscode.Position) => {
let params = {
textDocument: {
uri: document.uri.toString(),
},
position: position,
model: generationConfiguration.model,
parameters: generationConfiguration.parameters
};
inlineCompletionRequestCounter += 1;
let localInlineCompletionRequestCounter = inlineCompletionRequestCounter;
if ((Date.now() - lastInlineCompletion) / 1000 < 1 / inlineCompletionConfiguration["maxCompletionsPerSecond"]) {
await new Promise(r => setTimeout(r, ((1 / inlineCompletionConfiguration["maxCompletionsPerSecond"]) - ((Date.now() - lastInlineCompletion) / 1000 )) * 1000));
if (inlineCompletionRequestCounter == localInlineCompletionRequestCounter) {
lastInlineCompletion = Date.now();
const result = await client.sendRequest("textDocument/generation", params);
return [new vscode.InlineCompletionItem(result["generatedText"])];
} else {
return [];
}
} else {
lastInlineCompletion = Date.now();
const result = await client.sendRequest("textDocument/generation", params);
return [new vscode.InlineCompletionItem(result["generatedText"])];
}
}
}
);
}
export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}