-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy path2388.js
305 lines (287 loc) · 10.1 KB
/
2388.js
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
Object.defineProperty(exports, "__esModule", {
value: !0,
});
exports.launchSolutions = exports.normalizeCompletionText = undefined;
const M_uuid_utils = require("uuid-utils");
const M_async_iterable_utils_maybe = require("async-iterable-utils");
const M_config_stuff = require("config-stuff");
const M_completion_context = require("completion-context");
const M_logging_utils = require("logging-utils");
const M_openai_conn_utils = require("openai_conn_utils");
const M_openai_choices_utils = require("openai-choices-utils");
const M_status_reporter_maybe = require("status-reporter");
const M_context_extractor_from_identation_maybe = require("context-extractor-from-identation-maybe");
const M_prompt_extractor = require("prompt-extractor");
const M_get_prompt_parsing_utils_maybe = require("get-prompt-parsing-utils");
const M_background_context_provider = require("background-context-provider");
const M_postprocess_choice = require("postprocess-choice");
const M_telemetry_stuff = require("telemetry-stuff");
const M_location_factory = require("location-factory");
const logger = new M_logging_utils.Logger(
M_logging_utils.LogLevel.INFO,
"solutions"
);
function isBlockBodyFinished(e, t, n, r) {
return async (o) => {
if (r instanceof Array) {
const [i, s] = r;
return M_context_extractor_from_identation_maybe.isBlockBodyFinishedWithPrefix(
e,
t,
n,
o,
s
);
}
return M_context_extractor_from_identation_maybe.isBlockBodyFinished(
e,
t,
n,
o
);
};
}
async function solnList(statusReporter, cancellationToken, solnIter) {
if (cancellationToken.isCancellationRequested) {
statusReporter.removeProgress();
return {
status: "FinishedWithError",
error: "Cancelled",
};
}
const r = await solnIter.next();
return !0 === r.done
? (statusReporter.removeProgress(),
{
status: "FinishedNormally",
})
: {
status: "Solution",
solution: r.value,
next: solnList(statusReporter, cancellationToken, solnIter),
};
}
exports.normalizeCompletionText = function (e) {
return e.replace(/\s+/g, "");
};
exports.launchSolutions = async function (ctx, t) {
var next;
var prev;
var w;
const ctxInsertPos = t.completionContext.insertPosition;
const ctxPrependToCompletion = t.completionContext.prependToCompletion;
const ctxIndentation = t.completionContext.indentation;
const locFactory = ctx.get(M_location_factory.LocationFactory);
const doc = await t.getDocument();
const k = await M_prompt_extractor.extractPrompt(ctx, doc, ctxInsertPos);
if ("contextTooShort" === k.type) {
t.reportCancelled();
return {
status: "FinishedWithError",
error: "Context too short",
};
}
const prompt = k.prompt;
const trailingWs = k.trailingWs;
if (trailingWs.length > 0) {
t.startPosition = locFactory.position(
t.startPosition.line,
t.startPosition.character - trailingWs.length
);
}
const cancellationToken = t.getCancellationToken();
const requestId = M_uuid_utils.v4();
t.savedTelemetryData = M_telemetry_stuff.TelemetryData.createAndMarkAsIssued(
{
headerRequestId: requestId,
languageId: doc.languageId,
source: M_completion_context.completionTypeToString(
t.completionContext.completionType
),
},
{
...M_telemetry_stuff.telemetrizePromptLength(prompt),
solutionCount: t.solutionCountTarget,
promptEndPos: doc.offsetAt(ctxInsertPos),
}
);
if (
t.completionContext.completionType ===
M_completion_context.CompletionType.TODO_QUICK_FIX
) {
const prefixLines = prompt.prefix.split("\n"),
lastLine = prefixLines.pop(),
secondLastLine = prefixLines.pop();
if (secondLastLine) {
const match = /^\W+(todo:?\s+)/i.exec(secondLastLine);
if (match) {
const o = match[1],
i = secondLastLine.replace(o, "");
prompt.prefix = prefixLines.join("\n") + "\n" + i + "\n" + lastLine;
}
}
}
if (
t.completionContext.completionType ===
M_completion_context.CompletionType.UNKNOWN_FUNCTION_QUICK_FIX
) {
prompt.prefix += t.completionContext.prependToCompletion;
}
logger.info(ctx, `prompt: ${JSON.stringify(prompt)}`);
logger.debug(ctx, `prependToCompletion: ${ctxPrependToCompletion}`);
M_telemetry_stuff.telemetry(ctx, "solution.requested", t.savedTelemetryData);
const blockMode = await ctx
.get(M_config_stuff.BlockModeConfig)
.forLanguage(ctx, doc.languageId);
const isLangSupported = M_get_prompt_parsing_utils_maybe.isSupportedLanguageId(
doc.languageId
);
const contextIndentation = M_context_extractor_from_identation_maybe.contextIndentation(doc, ctxInsertPos);
const postOptions = {
stream: !0,
extra: {
language: doc.languageId,
next_indent: null !== (next = contextIndentation.next) && undefined !== next ? next : 0,
},
};
if ("parsing" !== blockMode || isLangSupported) {
postOptions.stop = ["\n\n", "\r\n\r\n"];
}
const repoInfo = M_background_context_provider.extractRepoInfoInBackground(
ctx,
doc.fileName
);
const request = {
prompt: prompt,
languageId: doc.languageId,
repoInfo: repoInfo,
ourRequestId: requestId,
engineUrl: await M_openai_conn_utils.getEngineURL(
ctx,
M_background_context_provider.tryGetGitHubNWO(repoInfo),
doc.languageId,
M_background_context_provider.getDogFood(repoInfo),
await M_background_context_provider.getUserKind(ctx),
t.savedTelemetryData
),
count: t.solutionCountTarget,
uiKind: M_openai_choices_utils.CopilotUiKind.Panel,
postOptions: postOptions,
requestLogProbs: !0,
};
let F;
const completionType =
t.completionContext.completionType ===
M_completion_context.CompletionType.UNKNOWN_FUNCTION_QUICK_FIX
? [
M_completion_context.CompletionType.UNKNOWN_FUNCTION_QUICK_FIX,
t.completionContext.prependToCompletion,
]
: t.completionContext.completionType;
switch (blockMode) {
case M_config_stuff.BlockMode.Server:
F = async (e) => {};
postOptions.extra.force_indent = null !== (prev = contextIndentation.prev) && undefined !== prev ? prev : -1;
postOptions.extra.trim_by_indentation = !0;
break;
case M_config_stuff.BlockMode.ParsingAndServer:
F = isLangSupported ? isBlockBodyFinished(ctx, doc, t.startPosition, completionType) : async (e) => {};
postOptions.extra.force_indent = null !== (w = contextIndentation.prev) && undefined !== w ? w : -1;
postOptions.extra.trim_by_indentation = !0;
break;
case M_config_stuff.BlockMode.Parsing:
default:
F = isLangSupported ? isBlockBodyFinished(ctx, doc, t.startPosition, completionType) : async (e) => {};
}
ctx.get(M_status_reporter_maybe.StatusReporter).setProgress();
const response = await ctx
.get(M_openai_choices_utils.OpenAIFetcher)
.fetchAndStreamCompletions(
ctx,
request,
M_telemetry_stuff.TelemetryData.createAndMarkAsIssued(),
F,
cancellationToken
);
if ("failed" === response.type || "canceled" === response.type) {
t.reportCancelled();
ctx.get(M_status_reporter_maybe.StatusReporter).removeProgress();
return {
status: "FinishedWithError",
error: `${response.type}: ${response.reason}`,
};
}
let choices = response.choices;
choices = (async function* (choices, prependToCompletion) {
for await (const choice of choices) {
const choice = {
...choice,
};
choice.completionText = prependToCompletion + choice.completionText.trimRight();
yield choice;
}
})(choices, ctxPrependToCompletion);
if (null !== ctxIndentation) {
choices = M_openai_choices_utils.cleanupIndentChoices(choices, ctxIndentation);
}
choices = M_async_iterable_utils_maybe.asyncIterableMapFilter(choices, async (t) =>
M_postprocess_choice.postProcessChoice(ctx, "solution", doc, ctxInsertPos, t, !1, logger)
);
const choicesProcessor = M_async_iterable_utils_maybe.asyncIterableMapFilter(
choices,
async (choice) => {
let displayText = choice.completionText;
logger.info(ctx, `Open Copilot completion: [${choice.completionText}]`);
if (
t.completionContext.completionType ===
M_completion_context.CompletionType.OPEN_COPILOT ||
t.completionContext.completionType ===
M_completion_context.CompletionType.TODO_QUICK_FIX
) {
let textBeforeInsertPosSameLine = "";
const nodeStart = await M_context_extractor_from_identation_maybe.getNodeStart(
ctx,
doc,
ctxInsertPos,
choice.completionText
);
if (nodeStart)
[textBeforeInsertPosSameLine] = M_prompt_extractor.trimLastLine(
doc.getText(locFactory.range(locFactory.position(nodeStart.line, nodeStart.character), ctxInsertPos))
);
else {
const e = locFactory.position(ctxInsertPos.line, 0);
textBeforeInsertPosSameLine = doc.getText(locFactory.range(e, ctxInsertPos));
}
displayText = textBeforeInsertPosSameLine + displayText;
}
let completionText = choice.completionText;
if (
t.completionContext.completionType ===
M_completion_context.CompletionType.TODO_QUICK_FIX
) {
if (doc.lineAt(ctxInsertPos.line).isEmptyOrWhitespace) {
completionText += "\n";
}
}
if (trailingWs.length > 0 && completionText.startsWith(trailingWs)) {
completionText = completionText.substring(trailingWs.length);
}
const meanLogProb = choice.meanLogProb;
return {
displayText: displayText,
meanProb: undefined !== meanLogProb ? Math.exp(meanLogProb) : 0,
meanLogProb: meanLogProb || 0,
completionText: completionText,
requestId: choice.requestId,
choiceIndex: choice.choiceIndex,
prependToCompletion: ctxPrependToCompletion,
};
}
);
return solnList(
ctx.get(M_status_reporter_maybe.StatusReporter),
cancellationToken,
choicesProcessor[Symbol.asyncIterator]()
);
};