This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathprobeProvider.ts
315 lines (283 loc) · 9.76 KB
/
probeProvider.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as http from 'http';
import * as net from 'net';
import * as os from 'os';
import * as vscode from 'vscode';
import { AnswerSplitOnNewLineAccumulatorStreaming, reportAgentEventsToChat, StreamProcessor } from '../../chatState/convertStreamToMessage';
import postHogClient from '../../posthog/client';
import { applyEdits, applyEditsDirectly, Limiter } from '../../server/applyEdits';
import { handleRequest } from '../../server/requestHandler';
import { EditedCodeStreamingRequest, SidecarApplyEditsRequest, SidecarContextEvent } from '../../server/types';
import { SideCarClient } from '../../sidecar/client';
import { getUniqueId } from '../../utilities/uniqueId';
import { RecentEditsRetriever } from '../../server/editedFiles';
export class AideProbeProvider implements vscode.Disposable {
private _sideCarClient: SideCarClient;
private _editorUrl: string | undefined;
private _rootPath: string;
private _limiter = new Limiter(1);
private editsMap = new Map();
private _requestHandler: http.Server | null = null;
private _openResponseStream: vscode.ProbeResponseStream | undefined;
private _iterationEdits = new vscode.WorkspaceEdit();
private async isPortOpen(port: number): Promise<boolean> {
return new Promise((resolve, _) => {
const s = net.createServer();
s.once('error', (err) => {
s.close();
// @ts-ignore
if (err['code'] === 'EADDRINUSE') {
resolve(false);
} else {
resolve(false); // or throw error!!
// reject(err);
}
});
s.once('listening', () => {
resolve(true);
s.close();
});
s.listen(port);
});
}
private async getNextOpenPort(startFrom: number = 42427) {
let openPort: number | null = null;
while (startFrom < 65535 || !!openPort) {
if (await this.isPortOpen(startFrom)) {
openPort = startFrom;
break;
}
startFrom++;
}
return openPort;
}
constructor(
sideCarClient: SideCarClient,
rootPath: string,
recentEditsRetriever: RecentEditsRetriever,
) {
this._sideCarClient = sideCarClient;
this._rootPath = rootPath;
// Server for the sidecar to talk to the editor
this._requestHandler = http.createServer(
handleRequest(this.provideEdit.bind(this), this.provideEditStreamed.bind(this), recentEditsRetriever.retrieveSidecar.bind(recentEditsRetriever))
);
this.getNextOpenPort().then((port) => {
if (port === null) {
throw new Error('Could not find an open port');
}
// can still grab it by listenting to port 0
this._requestHandler?.listen(port);
const editorUrl = `http://localhost:${port}`;
console.log('editorUrl', editorUrl);
this._editorUrl = editorUrl;
// console.log(this._editorUrl);
});
vscode.aideProbe.registerProbeResponseProvider(
'aideProbeProvider',
{
provideProbeResponse: this.provideProbeResponse.bind(this),
onDidSessionAction: this.sessionFollowup.bind(this),
onDidUserAction: this.userFollowup.bind(this),
}
);
}
/**
*
* @returns Retuns the optional editor url (which is weird, maybe we should just crash
* if we don't get the editor url as its a necessary component now?)
*/
editorUrl(): string | undefined {
return this._editorUrl;
}
async sendContextRecording(events: SidecarContextEvent[]) {
await this._sideCarClient.sendContextRecording(events, this._editorUrl);
}
async sessionFollowup(sessionAction: vscode.AideProbeSessionAction) {
if (sessionAction.action.type === 'newIteration') {
// @theskcd - This is where we can accept the iteration
console.log('newIteration', sessionAction);
await this._sideCarClient.codeSculptingFollowup(sessionAction.action.newPrompt, sessionAction.sessionId);
}
if (sessionAction.action.type === 'followUpRequest') {
console.log('followUpRequest');
this._iterationEdits = new vscode.WorkspaceEdit();
await this._sideCarClient.codeSculptingFollowups(sessionAction.sessionId, this._rootPath);
}
postHogClient?.capture({
distinctId: getUniqueId(),
event: sessionAction.action.type,
properties: {
platform: os.platform(),
requestId: sessionAction.sessionId,
},
});
}
async userFollowup(userAction: vscode.AideProbeUserAction) {
if (userAction.type === 'contextChange') {
console.log('contextChange');
if (!this._editorUrl) {
console.log('skipping_no_editor_url');
return;
}
await this._sideCarClient.warmupCodeSculptingCache(userAction.newContext, this._editorUrl);
}
postHogClient?.capture({
distinctId: getUniqueId(),
event: userAction.type,
properties: {
platform: os.platform(),
},
});
}
async provideEditStreamed(request: EditedCodeStreamingRequest): Promise<{
fs_file_path: String;
success: boolean;
}> {
if (!this._openResponseStream) {
console.log('editing_streamed::no_open_response_stream');
return {
fs_file_path: '',
success: false,
};
}
const editStreamEvent = request;
const fileDocument = editStreamEvent.fs_file_path;
if ('Start' === editStreamEvent.event) {
const timeNow = Date.now();
const document = await vscode.workspace.openTextDocument(fileDocument);
if (document === undefined || document === null) {
return {
fs_file_path: '',
success: false,
};
}
console.log('editsStreamed::content', timeNow, document.getText());
const documentLines = document.getText().split(/\r\n|\r|\n/g);
console.log('editStreaming.start', editStreamEvent.fs_file_path);
console.log(editStreamEvent.range);
console.log(documentLines);
this.editsMap.set(editStreamEvent.edit_request_id, {
answerSplitter: new AnswerSplitOnNewLineAccumulatorStreaming(),
streamProcessor: new StreamProcessor(
this._openResponseStream,
documentLines,
undefined,
vscode.Uri.file(editStreamEvent.fs_file_path),
editStreamEvent.range,
null,
this._iterationEdits,
editStreamEvent.apply_directly,
),
});
} else if ('End' === editStreamEvent.event) {
// drain the lines which might be still present
const editsManager = this.editsMap.get(editStreamEvent.edit_request_id);
while (true) {
const currentLine = editsManager.answerSplitter.getLine();
if (currentLine === null) {
break;
}
await editsManager.streamProcessor.processLine(currentLine);
}
editsManager.streamProcessor.cleanup();
await vscode.workspace.save(vscode.Uri.file(editStreamEvent.fs_file_path)); // save files upon stream completion
console.log('provideEditsStreamed::finished', editStreamEvent.fs_file_path);
// delete this from our map
this.editsMap.delete(editStreamEvent.edit_request_id);
// we have the updated code (we know this will be always present, the types are a bit meh)
} else if (editStreamEvent.event.Delta) {
const editsManager = this.editsMap.get(editStreamEvent.edit_request_id);
if (editsManager !== undefined) {
editsManager.answerSplitter.addDelta(editStreamEvent.event.Delta);
while (true) {
const currentLine = editsManager.answerSplitter.getLine();
if (currentLine === null) {
break;
}
await editsManager.streamProcessor.processLine(currentLine);
}
}
}
return {
fs_file_path: '',
success: true,
};
}
async provideEdit(request: SidecarApplyEditsRequest): Promise<{
fs_file_path: String;
success: boolean;
}> {
if (request.apply_directly) {
applyEditsDirectly(request);
return {
fs_file_path: request.fs_file_path,
success: true,
};
}
if (!this._openResponseStream) {
console.log('returning early over here');
return {
fs_file_path: request.fs_file_path,
success: true,
};
}
const response = await applyEdits(request, this._openResponseStream, this._iterationEdits);
return response;
}
private async provideProbeResponse(request: vscode.ProbeRequest, response: vscode.ProbeResponseStream, token: vscode.CancellationToken) {
if (!this._editorUrl) {
return;
}
this._openResponseStream = response;
let { query } = request;
query = query.trim();
const startTime = process.hrtime();
postHogClient?.capture({
distinctId: getUniqueId(),
event: 'probe_requested',
properties: {
platform: os.platform(),
query,
requestId: request.requestId,
},
});
//if there is a selection present in the references: this is what it looks like:
const isAnchorEditing = isAnchorBasedEditing(request.scope);
// let probeResponse: AsyncIterableIterator<SideCarAgentEvent>;
// if (request.mode === 'AGENTIC' || request.mode === 'ANCHORED') {
const probeResponse = this._sideCarClient.startAgentCodeEdit(query, request.references, this._editorUrl, request.requestId, request.scope === 'WholeCodebase', isAnchorEditing);
// } else {
// probeResponse = this._sideCarClient.startAgentProbe(query, request.references, this._editorUrl, request.requestId,);
// }
// const isEditMode = request.mode === 'AGENTIC' || request.mode === 'ANCHORED';
await reportAgentEventsToChat(true, probeResponse, response, request.requestId, token, this._sideCarClient, this._iterationEdits, this._limiter);
const endTime = process.hrtime(startTime);
postHogClient?.capture({
distinctId: getUniqueId(),
event: 'probe_completed',
properties: {
platform: os.platform(),
query,
timeElapsed: `${endTime[0]}s ${endTime[1] / 1000000}ms`,
requestId: request.requestId,
},
});
return {
iterationEdits: this._iterationEdits,
};
}
dispose() {
this._requestHandler?.close();
}
}
function isAnchorBasedEditing(scope: vscode.AideProbeScope): boolean {
if (scope === 'Selection') {
return true;
} else {
return false;
}
}