Merge pull request #6443 from continuedev/jacob/feature/nextedit-telemetry
Jacob/feature/nextedit telemetry
This commit is contained in:
commit
203ad49f99
10 changed files with 463 additions and 259 deletions
139
core/nextEdit/NextEditLoggingService.ts
Normal file
139
core/nextEdit/NextEditLoggingService.ts
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
import { COUNT_COMPLETION_REJECTED_AFTER } from "../util/parameters";
|
||||
|
||||
import { DataLogger } from "../data/log";
|
||||
import { Telemetry } from "../util/posthog";
|
||||
import { NextEditOutcome } from "./types";
|
||||
|
||||
export class NextEditLoggingService {
|
||||
private static instance: NextEditLoggingService;
|
||||
|
||||
// Key is completionId
|
||||
private _abortControllers = new Map<string, AbortController>();
|
||||
private _logRejectionTimeouts = new Map<string, NodeJS.Timeout>();
|
||||
private _outcomes = new Map<string, NextEditOutcome>();
|
||||
_lastDisplayedCompletion: { id: string; displayedAt: number } | undefined =
|
||||
undefined;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): NextEditLoggingService {
|
||||
if (!NextEditLoggingService.instance) {
|
||||
NextEditLoggingService.instance = new NextEditLoggingService();
|
||||
}
|
||||
return NextEditLoggingService.instance;
|
||||
}
|
||||
|
||||
public createAbortController(completionId: string): AbortController {
|
||||
const abortController = new AbortController();
|
||||
this._abortControllers.set(completionId, abortController);
|
||||
return abortController;
|
||||
}
|
||||
|
||||
public deleteAbortController(completionId: string) {
|
||||
this._abortControllers.delete(completionId);
|
||||
}
|
||||
|
||||
public cancel() {
|
||||
this._abortControllers.forEach((abortController, id) => {
|
||||
abortController.abort();
|
||||
});
|
||||
this._abortControllers.clear();
|
||||
}
|
||||
|
||||
public accept(completionId: string): NextEditOutcome | undefined {
|
||||
if (this._logRejectionTimeouts.has(completionId)) {
|
||||
clearTimeout(this._logRejectionTimeouts.get(completionId));
|
||||
this._logRejectionTimeouts.delete(completionId);
|
||||
}
|
||||
if (this._outcomes.has(completionId)) {
|
||||
const outcome = this._outcomes.get(completionId)!;
|
||||
outcome.accepted = true;
|
||||
this.logNextEditOutcome(outcome);
|
||||
this._outcomes.delete(completionId);
|
||||
return outcome;
|
||||
}
|
||||
}
|
||||
|
||||
public reject(completionId: string): NextEditOutcome | undefined {
|
||||
if (this._logRejectionTimeouts.has(completionId)) {
|
||||
clearTimeout(this._logRejectionTimeouts.get(completionId));
|
||||
this._logRejectionTimeouts.delete(completionId);
|
||||
}
|
||||
|
||||
if (this._outcomes.has(completionId)) {
|
||||
const outcome = this._outcomes.get(completionId)!;
|
||||
outcome.accepted = false;
|
||||
this.logNextEditOutcome(outcome);
|
||||
this._outcomes.delete(completionId);
|
||||
return outcome;
|
||||
}
|
||||
}
|
||||
|
||||
public cancelRejectionTimeout(completionId: string) {
|
||||
if (this._logRejectionTimeouts.has(completionId)) {
|
||||
clearTimeout(this._logRejectionTimeouts.get(completionId)!);
|
||||
this._logRejectionTimeouts.delete(completionId);
|
||||
}
|
||||
if (this._outcomes.has(completionId)) {
|
||||
this._outcomes.delete(completionId);
|
||||
}
|
||||
}
|
||||
|
||||
public cancelRejectionTimeoutButKeepCompletionId(completionId: string) {
|
||||
if (this._logRejectionTimeouts.has(completionId)) {
|
||||
clearTimeout(this._logRejectionTimeouts.get(completionId)!);
|
||||
}
|
||||
}
|
||||
|
||||
public markDisplayed(completionId: string, outcome: NextEditOutcome) {
|
||||
const logRejectionTimeout = setTimeout(() => {
|
||||
// Wait 10 seconds, then assume it wasn't accepted
|
||||
outcome.accepted = false;
|
||||
this.logNextEditOutcome(outcome);
|
||||
this._logRejectionTimeouts.delete(completionId);
|
||||
}, COUNT_COMPLETION_REJECTED_AFTER);
|
||||
this._outcomes.set(completionId, outcome);
|
||||
this._logRejectionTimeouts.set(completionId, logRejectionTimeout);
|
||||
|
||||
// If the previously displayed completion is still waiting for rejection,
|
||||
// and this one is a continuation of that (the outcome.completion is the same modulo prefix)
|
||||
// then we should cancel the rejection timeout
|
||||
const previous = this._lastDisplayedCompletion;
|
||||
const now = Date.now();
|
||||
if (previous && this._logRejectionTimeouts.has(previous.id)) {
|
||||
const previousOutcome = this._outcomes.get(previous.id);
|
||||
const c1 = previousOutcome?.completion.split("\n")[0] ?? "";
|
||||
const c2 = outcome.completion.split("\n")[0];
|
||||
if (
|
||||
previousOutcome &&
|
||||
(c1.endsWith(c2) ||
|
||||
c2.endsWith(c1) ||
|
||||
c1.startsWith(c2) ||
|
||||
c2.startsWith(c1))
|
||||
) {
|
||||
this.cancelRejectionTimeout(previous.id);
|
||||
} else if (now - previous.displayedAt < 500) {
|
||||
// If a completion isn't shown for more than
|
||||
this.cancelRejectionTimeout(previous.id);
|
||||
}
|
||||
}
|
||||
|
||||
this._lastDisplayedCompletion = {
|
||||
id: completionId,
|
||||
displayedAt: now,
|
||||
};
|
||||
}
|
||||
|
||||
private logNextEditOutcome(outcome: NextEditOutcome) {
|
||||
void DataLogger.getInstance().logDevData({
|
||||
name: "nextEditOutcome",
|
||||
data: outcome,
|
||||
// data: {
|
||||
// ...outcome, // TODO: this is somehow getting messed up with autocomplete schema.
|
||||
// },
|
||||
});
|
||||
|
||||
// const { prompt, completion, prefix, suffix, ...restOfOutcome } = outcome;
|
||||
void Telemetry.capture("nextEditOutcome", outcome, true);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import * as path from "path";
|
||||
import { ConfigHandler } from "../config/ConfigHandler.js";
|
||||
import { ChatMessage, IDE, ILLM } from "../index.js";
|
||||
import OpenAI from "../llm/llms/OpenAI.js";
|
||||
|
|
@ -11,20 +12,21 @@ import { shouldPrefilter } from "../autocomplete/prefiltering/index.js";
|
|||
import { getAllSnippetsWithoutRace } from "../autocomplete/snippets/index.js";
|
||||
import { GetLspDefinitionsFunction } from "../autocomplete/types.js";
|
||||
import { AutocompleteDebouncer } from "../autocomplete/util/AutocompleteDebouncer.js";
|
||||
import { AutocompleteLoggingService } from "../autocomplete/util/AutocompleteLoggingService.js";
|
||||
import AutocompleteLruCache from "../autocomplete/util/AutocompleteLruCache.js";
|
||||
import { HelperVars } from "../autocomplete/util/HelperVars.js";
|
||||
import {
|
||||
AutocompleteInput,
|
||||
AutocompleteOutcome,
|
||||
} from "../autocomplete/util/types.js";
|
||||
import { AutocompleteInput } from "../autocomplete/util/types.js";
|
||||
import { replaceEscapedCharacters } from "../util/text.js";
|
||||
import {
|
||||
Prompt,
|
||||
NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
|
||||
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
|
||||
} from "./constants.js";
|
||||
import { NextEditLoggingService } from "./NextEditLoggingService.js";
|
||||
import {
|
||||
renderDefaultSystemPrompt,
|
||||
renderDefaultUserPrompt,
|
||||
renderPrompt,
|
||||
} from "./templating/NextEditPromptEngine.js";
|
||||
import { NextEditOutcome, Prompt, PromptMetadata } from "./types.js";
|
||||
// import { renderPrompt } from "./templating/NextEditPromptEngine.js";
|
||||
|
||||
const autocompleteCache = AutocompleteLruCache.get();
|
||||
|
|
@ -45,10 +47,11 @@ export class NextEditProvider {
|
|||
private bracketMatchingService = new BracketMatchingService();
|
||||
private debouncer = new AutocompleteDebouncer();
|
||||
private completionStreamer: CompletionStreamer;
|
||||
private loggingService = new AutocompleteLoggingService();
|
||||
private loggingService: NextEditLoggingService;
|
||||
private contextRetrievalService: ContextRetrievalService;
|
||||
private endpointType: "default" | "fineTuned";
|
||||
private diffContext: string = "";
|
||||
private promptMetadata: PromptMetadata | null = null;
|
||||
|
||||
private constructor(
|
||||
private readonly configHandler: ConfigHandler,
|
||||
|
|
@ -61,6 +64,7 @@ export class NextEditProvider {
|
|||
this.completionStreamer = new CompletionStreamer(this.onError.bind(this));
|
||||
this.contextRetrievalService = new ContextRetrievalService(this.ide);
|
||||
this.endpointType = endpointType;
|
||||
this.loggingService = NextEditLoggingService.getInstance();
|
||||
}
|
||||
|
||||
public static initialize(
|
||||
|
|
@ -154,18 +158,21 @@ export class NextEditProvider {
|
|||
this.loggingService.cancel();
|
||||
}
|
||||
|
||||
public accept(completionId: string) {
|
||||
const outcome = this.loggingService.accept(completionId);
|
||||
if (!outcome) {
|
||||
return;
|
||||
}
|
||||
this.bracketMatchingService.handleAcceptedCompletion(
|
||||
outcome.completion,
|
||||
outcome.filepath,
|
||||
);
|
||||
}
|
||||
// public accept(completionId: string) {
|
||||
// const outcome = this.loggingService.accept(completionId);
|
||||
// if (!outcome) {
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
public markDisplayed(completionId: string, outcome: AutocompleteOutcome) {
|
||||
// public reject(completionId: string) {
|
||||
// const outcome = this.loggingService.reject(completionId);
|
||||
// if (!outcome) {
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
public markDisplayed(completionId: string, outcome: NextEditOutcome) {
|
||||
this.loggingService.markDisplayed(completionId, outcome);
|
||||
}
|
||||
|
||||
|
|
@ -181,7 +188,7 @@ export class NextEditProvider {
|
|||
public async provideInlineCompletionItems(
|
||||
input: AutocompleteInput,
|
||||
token: AbortSignal | undefined,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
): Promise<NextEditOutcome | undefined> {
|
||||
try {
|
||||
// Create abort signal if not given
|
||||
if (!token) {
|
||||
|
|
@ -235,67 +242,41 @@ export class NextEditProvider {
|
|||
prompts.push(renderDefaultSystemPrompt());
|
||||
prompts.push(renderDefaultUserPrompt(snippetPayload, helper));
|
||||
} else {
|
||||
prompts.push(
|
||||
// await renderFineTunedUserPrompt(snippetPayload, this.ide, helper),
|
||||
// await renderFineTunedBasicUserPrompt(
|
||||
// snippetPayload,
|
||||
// this.ide,
|
||||
// helper,
|
||||
// this.diffContext,
|
||||
// ),
|
||||
renderPrompt(helper, this.diffContext),
|
||||
);
|
||||
const promptMetadata = renderPrompt(helper, this.diffContext);
|
||||
this.promptMetadata = promptMetadata;
|
||||
prompts.push(promptMetadata.prompt);
|
||||
}
|
||||
|
||||
if (this.endpointType === "default") {
|
||||
const msg: ChatMessage = await llm.chat(prompts, token);
|
||||
if (typeof msg.content === "string") {
|
||||
const nextCompletion = JSON.parse(msg.content).newCode;
|
||||
const outcomeNext: AutocompleteOutcome = {
|
||||
time: Date.now() - startTime,
|
||||
completion: nextCompletion,
|
||||
prefix: "",
|
||||
suffix: "",
|
||||
prompt: "",
|
||||
const outcomeNext: NextEditOutcome = {
|
||||
elapsed: Date.now() - startTime,
|
||||
modelProvider: llm.underlyingProviderName,
|
||||
modelName: llm.model,
|
||||
modelName: llm.model + ":zetaDataset",
|
||||
completionOptions: null,
|
||||
cacheHit: false,
|
||||
filepath: helper.filepath,
|
||||
numLines: nextCompletion.split("\n").length,
|
||||
// filepath: helper.filepath,
|
||||
completionId: helper.input.completionId,
|
||||
gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
uniqueId: await this.ide.getUniqueId(),
|
||||
timestamp: Date.now(),
|
||||
fileUri: helper.filepath,
|
||||
workspaceDirUri:
|
||||
helper.workspaceUris[0] ?? path.dirname(helper.filepath),
|
||||
prompt: prompts.join("\n"),
|
||||
userEdits: "",
|
||||
userExcerpts: "",
|
||||
originalEditableRange: "",
|
||||
completion: nextCompletion,
|
||||
cursorPosition: helper.pos,
|
||||
...helper.options,
|
||||
};
|
||||
return outcomeNext;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
// const body = {
|
||||
// model: defaultModel,
|
||||
// messages: prompts,
|
||||
// // max_tokens: 15000,
|
||||
// // stop: ["<|editable_region_end|>"],
|
||||
// };
|
||||
//
|
||||
// const resp = await fetch(defaultEndpoint, {
|
||||
// method: "POST",
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${mercuryToken} `,
|
||||
// "Content-Type": "application/json",
|
||||
// },
|
||||
// body: JSON.stringify(body),
|
||||
// });
|
||||
//
|
||||
// const respJson = await resp.json();
|
||||
|
||||
// TODO: Do some zod schema validation here if needed.
|
||||
} else {
|
||||
// const testController = new AbortController();
|
||||
// const msg: ChatMessage = await llm.chat(prompts, testController.signal);
|
||||
// const testToken = testController.signal;
|
||||
const msg: ChatMessage = await llm.chat(prompts, token);
|
||||
if (typeof msg.content === "string") {
|
||||
// TODO: There are cases where msg.conetnt.split("<|start|>")[1] is undefined
|
||||
|
|
@ -303,166 +284,46 @@ export class NextEditProvider {
|
|||
msg.content.split("<|editable_region_start|>\n")[1],
|
||||
).replace(/\n$/, "");
|
||||
|
||||
// const diffLines = myersDiff(helper.fileContents, nextCompletion);
|
||||
const currCursorPos = helper.pos;
|
||||
const editableRegionStartLine = Math.max(
|
||||
currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
|
||||
0,
|
||||
);
|
||||
const editableRegionEndLine = Math.min(
|
||||
currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN,
|
||||
helper.fileLines.length - 1,
|
||||
);
|
||||
const oldEditRangeSlice = helper.fileContents
|
||||
.split("\n")
|
||||
.slice(editableRegionStartLine, editableRegionEndLine + 1)
|
||||
.join("\n");
|
||||
|
||||
// const diff = getRenderableDiff(diffLines);
|
||||
const outcomeNext: AutocompleteOutcome = {
|
||||
time: Date.now() - startTime,
|
||||
completion: nextCompletion,
|
||||
prefix: "",
|
||||
suffix: "",
|
||||
prompt: "",
|
||||
const outcomeNext: NextEditOutcome = {
|
||||
elapsed: Date.now() - startTime,
|
||||
modelProvider: llm.underlyingProviderName,
|
||||
modelName: llm.model,
|
||||
modelName: llm.model + ":zetaDataset",
|
||||
completionOptions: null,
|
||||
cacheHit: false,
|
||||
filepath: helper.filepath,
|
||||
numLines: nextCompletion.split("\n").length,
|
||||
// filepath: helper.filepath,
|
||||
completionId: helper.input.completionId,
|
||||
gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
uniqueId: await this.ide.getUniqueId(),
|
||||
timestamp: Date.now(),
|
||||
fileUri: helper.filepath,
|
||||
workspaceDirUri:
|
||||
helper.workspaceUris[0] ?? path.dirname(helper.filepath),
|
||||
prompt: this.promptMetadata!.prompt.content,
|
||||
userEdits: this.promptMetadata!.userEdits,
|
||||
userExcerpts: this.promptMetadata!.userExcerpts,
|
||||
originalEditableRange: oldEditRangeSlice,
|
||||
completion: nextCompletion,
|
||||
cursorPosition: helper.pos,
|
||||
...helper.options,
|
||||
};
|
||||
return outcomeNext;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
// const body = {
|
||||
// model: fineTunedModel,
|
||||
// messages: prompts,
|
||||
// max_tokens: 15000,
|
||||
// stop: ["<|editable_region_end|>"],
|
||||
// };
|
||||
//
|
||||
// const resp = await fetch(fineTunedEndpoint, {
|
||||
// method: "POST",
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${mercuryToken} `,
|
||||
// "Content-Type": "application/json",
|
||||
// },
|
||||
// body: JSON.stringify(body),
|
||||
// });
|
||||
//
|
||||
// const respJson = await resp.json();
|
||||
//
|
||||
// const nextCompletion = respJson.choices[0].message.content
|
||||
// .split("<|editable_region_start|>\n")[1]
|
||||
// .split("<|editable_region_end|>")[0]
|
||||
// .slice(1)
|
||||
// .slice(0, -1)
|
||||
// .replaceAll("\\n", "\n")
|
||||
// .replaceAll('\\"', '"');
|
||||
//
|
||||
// const outcomeNext: AutocompleteOutcome = {
|
||||
// time: Date.now() - startTime,
|
||||
// completion: nextCompletion,
|
||||
// prefix: "",
|
||||
// suffix: "",
|
||||
// prompt: "",
|
||||
// modelProvider: llm.underlyingProviderName,
|
||||
// modelName: llm.model,
|
||||
// completionOptions: null,
|
||||
// cacheHit: false,
|
||||
// filepath: helper.filepath,
|
||||
// numLines: nextCompletion.split("\n").length,
|
||||
// completionId: helper.input.completionId,
|
||||
// gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
// uniqueId: await this.ide.getUniqueId(),
|
||||
// timestamp: Date.now(),
|
||||
// ...helper.options,
|
||||
// };
|
||||
// return outcomeNext;
|
||||
}
|
||||
|
||||
// // Completion
|
||||
// let completion: string | undefined = "";
|
||||
|
||||
// const cache = await autocompleteCache;
|
||||
// const cachedCompletion = helper.options.useCache
|
||||
// ? await cache.get(helper.prunedPrefix)
|
||||
// : undefined;
|
||||
// let cacheHit = false;
|
||||
// if (cachedCompletion) {
|
||||
// // Cache
|
||||
// cacheHit = true;
|
||||
// completion = cachedCompletion;
|
||||
// } else {
|
||||
// const multiline =
|
||||
// !helper.options.transform || shouldCompleteMultiline(helper);
|
||||
|
||||
// const completionStream =
|
||||
// this.completionStreamer.streamCompletionWithFilters(
|
||||
// token,
|
||||
// llm,
|
||||
// prefix,
|
||||
// suffix,
|
||||
// prompt,
|
||||
// multiline,
|
||||
// completionOptions,
|
||||
// helper,
|
||||
// );
|
||||
|
||||
// for await (const update of completionStream) {
|
||||
// completion += update;
|
||||
// }
|
||||
|
||||
// // Don't postprocess if aborted
|
||||
// if (token.aborted) {
|
||||
// return undefined;
|
||||
// }
|
||||
|
||||
// const processedCompletion = helper.options.transform
|
||||
// ? postprocessCompletion({
|
||||
// completion,
|
||||
// prefix: helper.prunedPrefix,
|
||||
// suffix: helper.prunedSuffix,
|
||||
// llm,
|
||||
// })
|
||||
// : completion;
|
||||
|
||||
// completion = processedCompletion;
|
||||
// }
|
||||
|
||||
// if (!completion) {
|
||||
// return undefined;
|
||||
// }
|
||||
|
||||
// const outcome: AutocompleteOutcome = {
|
||||
// time: Date.now() - startTime,
|
||||
// completion,
|
||||
// prefix,
|
||||
// suffix,
|
||||
// prompt,
|
||||
// modelProvider: llm.underlyingProviderName,
|
||||
// modelName: llm.model,
|
||||
// completionOptions,
|
||||
// cacheHit,
|
||||
// filepath: helper.filepath,
|
||||
// numLines: completion.split("\n").length,
|
||||
// completionId: helper.input.completionId,
|
||||
// gitRepo: await this.ide.getRepoName(helper.filepath),
|
||||
// uniqueId: await this.ide.getUniqueId(),
|
||||
// timestamp: Date.now(),
|
||||
// ...helper.options,
|
||||
// };
|
||||
|
||||
// //////////
|
||||
|
||||
// // Save to cache
|
||||
// if (!outcome.cacheHit && helper.options.useCache) {
|
||||
// (await this.autocompleteCache)
|
||||
// .put(outcome.prefix, outcome.completion)
|
||||
// .catch((e) => console.warn(`Failed to save to cache: ${e.message}`));
|
||||
// }
|
||||
|
||||
// // When using the JetBrains extension, Mark as displayed
|
||||
// const ideType = (await this.ide.getIdeInfo()).ideType;
|
||||
// if (ideType === "jetbrains") {
|
||||
// this.markDisplayed(input.completionId, outcome);
|
||||
// }
|
||||
|
||||
// return outcome;
|
||||
} catch (e: any) {
|
||||
this.onError(e);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -9,28 +9,13 @@ import {
|
|||
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
|
||||
USER_CURSOR_IS_HERE_TOKEN,
|
||||
} from "../constants";
|
||||
|
||||
export type Prompt = SystemPrompt | UserPrompt;
|
||||
|
||||
interface SystemPrompt {
|
||||
role: "system";
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface UserPrompt {
|
||||
role: "user";
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface NextEditTemplate {
|
||||
template: string;
|
||||
}
|
||||
|
||||
export interface TemplateVars {
|
||||
userEdits: string;
|
||||
languageShorthand: string;
|
||||
userExcerpts: string;
|
||||
}
|
||||
import {
|
||||
NextEditTemplate,
|
||||
PromptMetadata,
|
||||
SystemPrompt,
|
||||
TemplateVars,
|
||||
UserPrompt,
|
||||
} from "../types";
|
||||
|
||||
type TemplateRenderer = (vars: TemplateVars) => string;
|
||||
|
||||
|
|
@ -64,13 +49,17 @@ function templateRendererOfModel(
|
|||
export function renderPrompt(
|
||||
helper: HelperVars,
|
||||
userEdits: string,
|
||||
): UserPrompt {
|
||||
): PromptMetadata {
|
||||
let modelName = helper.modelName as NextEditModelName;
|
||||
|
||||
if (modelName === "this field is not used") {
|
||||
return {
|
||||
role: "user",
|
||||
content: "NEXT_EDIT",
|
||||
prompt: {
|
||||
role: "user",
|
||||
content: "NEXT_EDIT",
|
||||
},
|
||||
userEdits,
|
||||
userExcerpts: helper.fileContents,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -103,8 +92,12 @@ export function renderPrompt(
|
|||
};
|
||||
|
||||
return {
|
||||
role: "user",
|
||||
content: renderer(tv),
|
||||
prompt: {
|
||||
role: "user",
|
||||
content: renderer(tv),
|
||||
},
|
||||
userEdits,
|
||||
userExcerpts: editedCodeWithTokens,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,20 +26,60 @@ export interface AutocompleteInput {
|
|||
}
|
||||
|
||||
export interface NextEditOutcome extends TabAutocompleteOptions {
|
||||
accepted?: boolean;
|
||||
time: number;
|
||||
prefix: string;
|
||||
suffix: string;
|
||||
prompt: string;
|
||||
completion: string;
|
||||
// Originally from Autocomplete.
|
||||
// accepted?: boolean;
|
||||
elapsed: number;
|
||||
// prefix: string;
|
||||
// suffix: string;
|
||||
// prompt: string;
|
||||
// completion: string;
|
||||
modelProvider: string;
|
||||
modelName: string;
|
||||
completionOptions: any;
|
||||
cacheHit: boolean;
|
||||
numLines: number;
|
||||
filepath: string;
|
||||
gitRepo?: string;
|
||||
// cacheHit: boolean;
|
||||
// numLines: number;
|
||||
// filepath: string;
|
||||
completionId: string;
|
||||
gitRepo?: string;
|
||||
uniqueId: string;
|
||||
timestamp: number;
|
||||
|
||||
// New for Next Edit.
|
||||
fileUri: string;
|
||||
workspaceDirUri: string;
|
||||
prompt: string;
|
||||
userEdits: string;
|
||||
userExcerpts: string;
|
||||
originalEditableRange: string;
|
||||
completion: string;
|
||||
cursorPosition: { line: number; character: number };
|
||||
accepted?: boolean;
|
||||
}
|
||||
|
||||
export interface PromptMetadata {
|
||||
prompt: UserPrompt;
|
||||
userEdits: string;
|
||||
userExcerpts: string;
|
||||
}
|
||||
|
||||
export type Prompt = SystemPrompt | UserPrompt;
|
||||
|
||||
export interface SystemPrompt {
|
||||
role: "system";
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface UserPrompt {
|
||||
role: "user";
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface NextEditTemplate {
|
||||
template: string;
|
||||
}
|
||||
|
||||
export interface TemplateVars {
|
||||
userEdits: string;
|
||||
languageShorthand: string;
|
||||
userExcerpts: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import {
|
|||
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
|
||||
} from "core/nextEdit/constants";
|
||||
import { getOffsetPositionAtLastNewLine } from "core/nextEdit/diff/diff";
|
||||
import { NextEditLoggingService } from "core/nextEdit/NextEditLoggingService";
|
||||
import { getThemeString } from "../util/getTheme";
|
||||
|
||||
export interface TextApplier {
|
||||
|
|
@ -89,10 +90,16 @@ export class NextEditWindowManager {
|
|||
|
||||
// Current active decoration
|
||||
private currentDecoration: vscode.TextEditorDecorationType | null = null;
|
||||
// A short-lived checker to determine if the cursor moved because of us accepting the next edit, or not.
|
||||
// Distinguishing the two is necessary to determine if we should log it as an accepted or rejected.
|
||||
private accepted: boolean = false;
|
||||
// Track which editor has the active decoration
|
||||
private activeEditor: vscode.TextEditor | null = null;
|
||||
// Store the current tooltip text for accepting
|
||||
private currentTooltipText: string | null = null;
|
||||
// Track for logging purposes.
|
||||
private loggingService: NextEditLoggingService;
|
||||
private mostRecentCompletionId: string | null = null;
|
||||
|
||||
// Disposables
|
||||
private disposables: vscode.Disposable[] = [];
|
||||
|
|
@ -136,6 +143,8 @@ export class NextEditWindowManager {
|
|||
const editorConfig = vscode.workspace.getConfiguration("editor");
|
||||
this.fontSize = editorConfig.get<number>("fontSize") ?? 14;
|
||||
this.fontFamily = editorConfig.get<string>("fontFamily") ?? "monospace";
|
||||
|
||||
this.loggingService = NextEditLoggingService.getInstance();
|
||||
}
|
||||
|
||||
public static async reserveTabAndEsc() {
|
||||
|
|
@ -175,7 +184,7 @@ export class NextEditWindowManager {
|
|||
// Register HIDE_TOOLTIP_COMMAND and ACCEPT_NEXT_EDIT_COMMAND with their corresponding callbacks.
|
||||
this.registerCommandSafely(
|
||||
HIDE_NEXT_EDIT_SUGGESTION_COMMAND,
|
||||
async () => await this.hideAllNextEditWindows(),
|
||||
async () => await this.hideAllNextEditWindowsAndResetCompletionId(),
|
||||
);
|
||||
this.registerCommandSafely(
|
||||
ACCEPT_NEXT_EDIT_SUGGESTION_COMMAND,
|
||||
|
|
@ -192,6 +201,14 @@ export class NextEditWindowManager {
|
|||
await this.codeRenderer.setTheme(this.theme);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the most recent completion id.
|
||||
* @param completionId The id of current completion request.
|
||||
*/
|
||||
public updateCurrentCompletionId(completionId: string) {
|
||||
this.mostRecentCompletionId = completionId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers our two custom commands to the extension context.
|
||||
* @param commandId Custom commands to help set up next edit.
|
||||
|
|
@ -297,6 +314,18 @@ export class NextEditWindowManager {
|
|||
await NextEditWindowManager.freeTabAndEsc();
|
||||
}
|
||||
|
||||
public async hideAllNextEditWindowsAndResetCompletionId() {
|
||||
this.hideAllNextEditWindows();
|
||||
|
||||
// Log with accept = false.
|
||||
await vscode.commands.executeCommand(
|
||||
"continue.logNextEditOutcomeReject",
|
||||
this.mostRecentCompletionId,
|
||||
this.loggingService,
|
||||
);
|
||||
this.mostRecentCompletionId = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept the current next edit suggestion by inserting it at cursor position.
|
||||
*/
|
||||
|
|
@ -304,6 +333,7 @@ export class NextEditWindowManager {
|
|||
if (!this.activeEditor || !this.currentTooltipText) {
|
||||
return;
|
||||
}
|
||||
this.accepted = true;
|
||||
|
||||
const editor = this.activeEditor;
|
||||
const text = this.currentTooltipText;
|
||||
|
|
@ -362,6 +392,17 @@ export class NextEditWindowManager {
|
|||
await this.hideAllNextEditWindows();
|
||||
}
|
||||
}
|
||||
|
||||
// Log with accept = true.
|
||||
await vscode.commands.executeCommand(
|
||||
"continue.logNextEditOutcomeAccept",
|
||||
this.mostRecentCompletionId,
|
||||
this.loggingService,
|
||||
);
|
||||
this.mostRecentCompletionId = null;
|
||||
|
||||
// Reset to false for future logging.
|
||||
this.accepted = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -425,6 +466,10 @@ export class NextEditWindowManager {
|
|||
this.activeEditor &&
|
||||
!vscode.window.visibleTextEditors.includes(this.activeEditor)
|
||||
) {
|
||||
if (this.mostRecentCompletionId)
|
||||
this.loggingService.cancelRejectionTimeout(
|
||||
this.mostRecentCompletionId,
|
||||
);
|
||||
await this.hideAllNextEditWindows();
|
||||
}
|
||||
});
|
||||
|
|
@ -433,6 +478,11 @@ export class NextEditWindowManager {
|
|||
vscode.window.onDidChangeTextEditorSelection(async (e) => {
|
||||
// If the selection changed in our active editor, hide the tooltip.
|
||||
if (this.activeEditor && e.textEditor === this.activeEditor) {
|
||||
// If the cursor moved because of something other than accepting next edit, stop logging it.
|
||||
if (!this.accepted && this.mostRecentCompletionId)
|
||||
this.loggingService.cancelRejectionTimeout(
|
||||
this.mostRecentCompletionId,
|
||||
);
|
||||
await this.hideAllNextEditWindows();
|
||||
}
|
||||
});
|
||||
|
|
@ -685,6 +735,12 @@ export class NextEditWindowManager {
|
|||
hoverMessage: [this.buildHideTooltipHoverMsg()],
|
||||
},
|
||||
]);
|
||||
|
||||
// Clear the timeout while SVG is on the editor.
|
||||
if (this.currentDecoration && this.mostRecentCompletionId)
|
||||
this.loggingService.cancelRejectionTimeoutButKeepCompletionId(
|
||||
this.mostRecentCompletionId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ import {
|
|||
NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN,
|
||||
} from "core/nextEdit/constants";
|
||||
import { checkFim } from "core/nextEdit/diff/diff";
|
||||
import { NextEditLoggingService } from "core/nextEdit/NextEditLoggingService";
|
||||
import { NextEditProvider } from "core/nextEdit/NextEditProvider";
|
||||
import { NextEditOutcome } from "core/nextEdit/types";
|
||||
import { NextEditWindowManager } from "../activation/NextEditWindowManager";
|
||||
import { getDefinitionsFromLsp } from "./lsp";
|
||||
import { RecentlyEditedTracker } from "./recentlyEdited";
|
||||
|
|
@ -63,6 +65,7 @@ export class ContinueCompletionProvider
|
|||
|
||||
private completionProvider: CompletionProvider;
|
||||
private nextEditProvider: NextEditProvider;
|
||||
private nextEditLoggingService: NextEditLoggingService;
|
||||
public recentlyVisitedRanges: RecentlyVisitedRangesService;
|
||||
public recentlyEditedTracker: RecentlyEditedTracker;
|
||||
|
||||
|
|
@ -98,6 +101,8 @@ export class ContinueCompletionProvider
|
|||
getDefinitionsFromLsp,
|
||||
);
|
||||
|
||||
// Logging service must be created first.
|
||||
this.nextEditLoggingService = NextEditLoggingService.getInstance();
|
||||
this.nextEditProvider = NextEditProvider.initialize(
|
||||
this.configHandler,
|
||||
this.ide,
|
||||
|
|
@ -110,7 +115,7 @@ export class ContinueCompletionProvider
|
|||
this.recentlyVisitedRanges = new RecentlyVisitedRangesService(ide);
|
||||
}
|
||||
|
||||
_lastShownCompletion: AutocompleteOutcome | undefined;
|
||||
_lastShownCompletion: AutocompleteOutcome | NextEditOutcome | undefined;
|
||||
|
||||
public async provideInlineCompletionItems(
|
||||
document: vscode.TextDocument,
|
||||
|
|
@ -231,7 +236,9 @@ export class ContinueCompletionProvider
|
|||
|
||||
setupStatusBar(undefined, true);
|
||||
|
||||
const outcome = this.isNextEditActive
|
||||
// TODO: fix type of outcome to be a union between NextEditOutcome and AutocompleteOutcome.
|
||||
const outcome: AutocompleteOutcome | NextEditOutcome | undefined = this
|
||||
.isNextEditActive
|
||||
? await this.nextEditProvider.provideInlineCompletionItems(
|
||||
input,
|
||||
signal,
|
||||
|
|
@ -272,8 +279,19 @@ export class ContinueCompletionProvider
|
|||
return null;
|
||||
}
|
||||
|
||||
// Mark displayed
|
||||
this.completionProvider.markDisplayed(input.completionId, outcome);
|
||||
// Marking the outcome as displayed saves
|
||||
// the current outcome as a value of the key completionId.
|
||||
if (this.isNextEditActive) {
|
||||
this.nextEditProvider.markDisplayed(
|
||||
input.completionId,
|
||||
outcome as NextEditOutcome,
|
||||
);
|
||||
} else {
|
||||
this.completionProvider.markDisplayed(
|
||||
input.completionId,
|
||||
outcome as AutocompleteOutcome,
|
||||
);
|
||||
}
|
||||
this._lastShownCompletion = outcome;
|
||||
|
||||
// Construct the range/text to show
|
||||
|
|
@ -283,6 +301,8 @@ export class ContinueCompletionProvider
|
|||
let range = new vscode.Range(startPos, startPos);
|
||||
// let range = new vscode.Range(startPos, endPos);
|
||||
let completionText = outcome.completion;
|
||||
|
||||
// NOTE: This seems like an autocomplete logic.
|
||||
const isSingleLineCompletion = outcome.completion.split("\n").length <= 1;
|
||||
|
||||
if (isSingleLineCompletion) {
|
||||
|
|
@ -313,7 +333,7 @@ export class ContinueCompletionProvider
|
|||
range = new vscode.Range(startPos, document.lineAt(startPos).range.end);
|
||||
}
|
||||
|
||||
const completionItem = new vscode.InlineCompletionItem(
|
||||
const autocompleteCompletionItem = new vscode.InlineCompletionItem(
|
||||
completionText,
|
||||
range,
|
||||
{
|
||||
|
|
@ -323,7 +343,7 @@ export class ContinueCompletionProvider
|
|||
},
|
||||
);
|
||||
|
||||
(completionItem as any).completeBracketPairs = true;
|
||||
(autocompleteCompletionItem as any).completeBracketPairs = true;
|
||||
|
||||
if (this.isNextEditActive) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
|
@ -336,6 +356,9 @@ export class ContinueCompletionProvider
|
|||
|
||||
// We don't need to show the next edit window if the predicted edits is empty.
|
||||
if (newEditRangeSlice === "") {
|
||||
this.nextEditLoggingService.cancelRejectionTimeout(
|
||||
input.completionId,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
|
@ -357,6 +380,9 @@ export class ContinueCompletionProvider
|
|||
|
||||
// We don't need to show the next edit window if the predicted edits are identical to the previous version.
|
||||
if (oldEditRangeSlice === newEditRangeSlice) {
|
||||
this.nextEditLoggingService.cancelRejectionTimeout(
|
||||
input.completionId,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
|
@ -367,37 +393,41 @@ export class ContinueCompletionProvider
|
|||
currCursorPos,
|
||||
);
|
||||
if (isFim) {
|
||||
const completionItem = new vscode.InlineCompletionItem(
|
||||
const nextEditCompletionItem = new vscode.InlineCompletionItem(
|
||||
fimText,
|
||||
new vscode.Range(
|
||||
new vscode.Position(currCursorPos.line, currCursorPos.character),
|
||||
new vscode.Position(currCursorPos.line, currCursorPos.character),
|
||||
),
|
||||
{
|
||||
title: "Log Autocomplete Outcome",
|
||||
command: "continue.logAutocompleteOutcome",
|
||||
arguments: [input.completionId, this.completionProvider],
|
||||
title: "Log Next Edit Outcome",
|
||||
command: "continue.logNextEditOutcomeAccept",
|
||||
arguments: [input.completionId, this.nextEditLoggingService], // TODO: this may have to be this.completionProvider.
|
||||
},
|
||||
);
|
||||
return [completionItem];
|
||||
return [nextEditCompletionItem];
|
||||
}
|
||||
|
||||
// Else, render a next edit window.
|
||||
const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice);
|
||||
|
||||
if (NextEditWindowManager.isInstantiated()) {
|
||||
NextEditWindowManager.getInstance().updateCurrentCompletionId(
|
||||
input.completionId,
|
||||
);
|
||||
|
||||
await NextEditWindowManager.getInstance().showNextEditWindow(
|
||||
editor,
|
||||
currCursorPos,
|
||||
editableRegionStartLine,
|
||||
completionText,
|
||||
newEditRangeSlice,
|
||||
diffLines,
|
||||
);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
} else {
|
||||
return [completionItem];
|
||||
return [autocompleteCompletionItem];
|
||||
}
|
||||
} finally {
|
||||
stopStatusBarLoading();
|
||||
|
|
@ -408,7 +438,7 @@ export class ContinueCompletionProvider
|
|||
document: vscode.TextDocument,
|
||||
selectedCompletionInfo: vscode.SelectedCompletionInfo | undefined,
|
||||
abortSignal: AbortSignal,
|
||||
outcome: AutocompleteOutcome,
|
||||
outcome: AutocompleteOutcome | NextEditOutcome,
|
||||
): boolean {
|
||||
if (selectedCompletionInfo) {
|
||||
const { text, range } = selectedCompletionInfo;
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import * as YAML from "yaml";
|
|||
|
||||
import { convertJsonToYamlConfig } from "../../../packages/config-yaml/dist";
|
||||
|
||||
import { NextEditLoggingService } from "core/nextEdit/NextEditLoggingService";
|
||||
import {
|
||||
getAutocompleteStatusBarDescription,
|
||||
getAutocompleteStatusBarTitle,
|
||||
|
|
@ -544,6 +545,18 @@ const getCommandsMap: (
|
|||
) => {
|
||||
completionProvider.accept(completionId);
|
||||
},
|
||||
"continue.logNextEditOutcomeAccept": (
|
||||
completionId: string,
|
||||
nextEditLoggingService: NextEditLoggingService,
|
||||
) => {
|
||||
nextEditLoggingService.accept(completionId);
|
||||
},
|
||||
"continue.logNextEditOutcomeReject": (
|
||||
completionId: string,
|
||||
nextEditLoggingService: NextEditLoggingService,
|
||||
) => {
|
||||
nextEditLoggingService.reject(completionId);
|
||||
},
|
||||
"continue.toggleTabAutocompleteEnabled": () => {
|
||||
captureCommandTelemetry("toggleTabAutocompleteEnabled");
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,11 @@ import {
|
|||
editOutcomeEventSchema_0_2_0,
|
||||
editOutcomeEventSchema_0_2_0_noCode,
|
||||
} from "./editOutcome/v0.2.0.js";
|
||||
import { nextEditOutcomeEventAllSchema } from "./nextEditOutcome/index.js";
|
||||
import {
|
||||
nextEditOutcomeEventSchema_0_2_0,
|
||||
nextEditOutcomeEventSchema_0_2_0_noCode,
|
||||
} from "./nextEditOutcome/v0.2.0.js";
|
||||
import { nextEditEventAllSchema } from "./nextEditWithHistory/index.js";
|
||||
import {
|
||||
nextEditEventSchema_0_2_0,
|
||||
|
|
@ -88,6 +93,7 @@ const devEventAllVersionDataSchemas = z.object({
|
|||
chatInteraction: chatInteractionEventAllSchema,
|
||||
editInteraction: editInteractionEventAllSchema,
|
||||
editOutcome: editOutcomeEventAllSchema,
|
||||
nextEditOutcome: nextEditOutcomeEventAllSchema,
|
||||
nextEditWithHistory: nextEditEventAllSchema,
|
||||
toolUsage: toolUsageEventAllSchema,
|
||||
});
|
||||
|
|
@ -116,6 +122,7 @@ export const devDataVersionedSchemas = {
|
|||
chatInteraction: chatInteractionEventSchema_0_2_0,
|
||||
editInteraction: editInteractionEventSchema_0_2_0,
|
||||
editOutcome: editOutcomeEventSchema_0_2_0,
|
||||
nextEditOutcome: nextEditOutcomeEventSchema_0_2_0,
|
||||
nextEditWithHistory: nextEditEventSchema_0_2_0,
|
||||
toolUsage: toolUsageEventSchema_0_2_0,
|
||||
},
|
||||
|
|
@ -126,6 +133,7 @@ export const devDataVersionedSchemas = {
|
|||
chatInteraction: chatInteractionEventSchema_0_2_0_noCode,
|
||||
editInteraction: editInteractionEventSchema_0_2_0_noCode,
|
||||
editOutcome: editOutcomeEventSchema_0_2_0_noCode,
|
||||
nextEditOutcome: nextEditOutcomeEventSchema_0_2_0_noCode,
|
||||
nextEditWithHistory: nextEditEventSchema_0_2_0_noCode,
|
||||
toolUsage: toolUsageEventSchema_0_2_0_noCode,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
import { z } from "zod";
|
||||
import { baseDevDataAllSchema } from "../base.js";
|
||||
|
||||
export const nextEditOutcomeEventAllSchema = baseDevDataAllSchema.extend({
|
||||
elapsed: z.number(),
|
||||
completionOptions: z.any(),
|
||||
completionId: z.string(),
|
||||
gitRepo: z.string().optional(),
|
||||
uniqueId: z.string(),
|
||||
timestamp: z.number(),
|
||||
fileUri: z.string(),
|
||||
workspaceDirUri: z.string(),
|
||||
prompt: z.string(),
|
||||
userEdits: z.string(),
|
||||
userExcerpts: z.string(),
|
||||
originalEditableRange: z.string(),
|
||||
completion: z.string(),
|
||||
cursorPosition: z.object({ line: z.number(), character: z.number() }),
|
||||
accepted: z.boolean().optional(),
|
||||
modelProvider: z.string(),
|
||||
modelName: z.string(),
|
||||
});
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
import { nextEditOutcomeEventAllSchema } from "./index.js";
|
||||
|
||||
export const nextEditOutcomeEventSchema_0_2_0 =
|
||||
nextEditOutcomeEventAllSchema.pick({
|
||||
// base
|
||||
timestamp: true,
|
||||
userId: true,
|
||||
userAgent: true,
|
||||
selectedProfileId: true,
|
||||
eventName: true,
|
||||
schema: true,
|
||||
|
||||
// nextEditOutcome-specific
|
||||
elapsed: true,
|
||||
completionOptions: true,
|
||||
completionId: true,
|
||||
gitRepo: true,
|
||||
uniqueId: true,
|
||||
// timestamp: z.number(),
|
||||
fileUri: true,
|
||||
workspaceDirUri: true,
|
||||
prompt: true,
|
||||
userEdits: true,
|
||||
userExcerpts: true,
|
||||
originalEditableRange: true,
|
||||
completion: true,
|
||||
cursorPosition: true,
|
||||
accepted: true,
|
||||
modelProvider: true,
|
||||
modelName: true,
|
||||
});
|
||||
|
||||
export const nextEditOutcomeEventSchema_0_2_0_noCode =
|
||||
nextEditOutcomeEventSchema_0_2_0.omit({
|
||||
fileUri: true,
|
||||
workspaceDirUri: true,
|
||||
prompt: true,
|
||||
userEdits: true,
|
||||
userExcerpts: true,
|
||||
originalEditableRange: true,
|
||||
completion: true,
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue