✨ autocomplete in core protocol
This commit is contained in:
parent
805e002fc7
commit
00e526ecaf
6 changed files with 149 additions and 71 deletions
|
|
@ -29,6 +29,7 @@ import { getTemplateForModel } from "./templates";
|
|||
import { GeneratorReuseManager } from "./util";
|
||||
|
||||
export interface AutocompleteInput {
|
||||
completionId: string;
|
||||
filepath: string;
|
||||
pos: Position;
|
||||
recentlyEditedFiles: RangeInFileWithContents[];
|
||||
|
|
@ -309,67 +310,26 @@ export class CompletionProvider {
|
|||
}
|
||||
}
|
||||
|
||||
public async provideInlineCompletionItems(
|
||||
input: AutocompleteInput,
|
||||
token: AbortSignal,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
// Debounce
|
||||
const uuid = uuidv4();
|
||||
CompletionProvider.lastUUID = uuid;
|
||||
public cancel() {
|
||||
this._abortControllers.forEach((abortController, id) => {
|
||||
abortController.abort();
|
||||
});
|
||||
this._abortControllers.clear();
|
||||
}
|
||||
|
||||
const config = await this.configHandler.loadConfig();
|
||||
const options = {
|
||||
...DEFAULT_AUTOCOMPLETE_OPTS,
|
||||
...config.tabAutocompleteOptions,
|
||||
};
|
||||
private _abortControllers = new Map<string, AbortController>();
|
||||
private _logRejectionTimeouts = new Map<string, NodeJS.Timeout>();
|
||||
private _outcomes = new Map<string, AutocompleteOutcome>();
|
||||
|
||||
if (CompletionProvider.debouncing) {
|
||||
CompletionProvider.debounceTimeout?.refresh();
|
||||
const lastUUID = await new Promise((resolve) =>
|
||||
setTimeout(() => {
|
||||
resolve(CompletionProvider.lastUUID);
|
||||
}, options.debounceDelay),
|
||||
);
|
||||
if (uuid !== lastUUID) {
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
CompletionProvider.debouncing = true;
|
||||
CompletionProvider.debounceTimeout = setTimeout(async () => {
|
||||
CompletionProvider.debouncing = false;
|
||||
}, options.debounceDelay);
|
||||
public accept(completionId: string) {
|
||||
if (this._logRejectionTimeouts.has(completionId)) {
|
||||
clearTimeout(this._logRejectionTimeouts.get(completionId));
|
||||
this._logRejectionTimeouts.delete(completionId);
|
||||
}
|
||||
|
||||
// Get completion
|
||||
const llm = await this.getLlm();
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
const outcome = await getTabCompletion(
|
||||
token,
|
||||
options,
|
||||
llm,
|
||||
this.ide,
|
||||
this.generatorReuseManager,
|
||||
input,
|
||||
this.getDefinitionsFromLsp,
|
||||
);
|
||||
const completion = outcome?.completion;
|
||||
|
||||
if (!completion) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Do some stuff later so as not to block return. Latency matters
|
||||
setTimeout(async () => {
|
||||
if (!outcome.cacheHit) {
|
||||
(await this.autocompleteCache).put(outcome.prompt, completion);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
const logRejectionTimeout = setTimeout(() => {
|
||||
// Wait 10 seconds, then assume it wasn't accepted
|
||||
outcome.accepted = false;
|
||||
if (this._outcomes.has(completionId)) {
|
||||
const outcome = this._outcomes.get(completionId)!;
|
||||
outcome.accepted = true;
|
||||
logDevData("autocomplete", outcome);
|
||||
Telemetry.capture("autocomplete", {
|
||||
accepted: outcome.accepted,
|
||||
|
|
@ -378,8 +338,95 @@ export class CompletionProvider {
|
|||
time: outcome.time,
|
||||
cacheHit: outcome.cacheHit,
|
||||
});
|
||||
}, 10_000);
|
||||
this._outcomes.delete(completionId);
|
||||
}
|
||||
}
|
||||
|
||||
return outcome;
|
||||
public async provideInlineCompletionItems(
|
||||
input: AutocompleteInput,
|
||||
token: AbortSignal | undefined,
|
||||
): Promise<AutocompleteOutcome | undefined> {
|
||||
// Create abort signal if not given
|
||||
if (!token) {
|
||||
const controller = new AbortController();
|
||||
token = controller.signal;
|
||||
this._abortControllers.set(input.completionId, controller);
|
||||
}
|
||||
|
||||
try {
|
||||
// Debounce
|
||||
const uuid = uuidv4();
|
||||
CompletionProvider.lastUUID = uuid;
|
||||
|
||||
const config = await this.configHandler.loadConfig();
|
||||
const options = {
|
||||
...DEFAULT_AUTOCOMPLETE_OPTS,
|
||||
...config.tabAutocompleteOptions,
|
||||
};
|
||||
|
||||
if (CompletionProvider.debouncing) {
|
||||
CompletionProvider.debounceTimeout?.refresh();
|
||||
const lastUUID = await new Promise((resolve) =>
|
||||
setTimeout(() => {
|
||||
resolve(CompletionProvider.lastUUID);
|
||||
}, options.debounceDelay),
|
||||
);
|
||||
if (uuid !== lastUUID) {
|
||||
return undefined;
|
||||
}
|
||||
} else {
|
||||
CompletionProvider.debouncing = true;
|
||||
CompletionProvider.debounceTimeout = setTimeout(async () => {
|
||||
CompletionProvider.debouncing = false;
|
||||
}, options.debounceDelay);
|
||||
}
|
||||
|
||||
// Get completion
|
||||
const llm = await this.getLlm();
|
||||
if (!llm) {
|
||||
return undefined;
|
||||
}
|
||||
const outcome = await getTabCompletion(
|
||||
token,
|
||||
options,
|
||||
llm,
|
||||
this.ide,
|
||||
this.generatorReuseManager,
|
||||
input,
|
||||
this.getDefinitionsFromLsp,
|
||||
);
|
||||
const completion = outcome?.completion;
|
||||
|
||||
if (!completion) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Do some stuff later so as not to block return. Latency matters
|
||||
setTimeout(async () => {
|
||||
if (!outcome.cacheHit) {
|
||||
(await this.autocompleteCache).put(outcome.prompt, completion);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
outcome.accepted = false;
|
||||
const logRejectionTimeout = setTimeout(() => {
|
||||
// Wait 10 seconds, then assume it wasn't accepted
|
||||
logDevData("autocomplete", outcome);
|
||||
Telemetry.capture("autocomplete", {
|
||||
accepted: outcome.accepted,
|
||||
modelName: outcome.modelName,
|
||||
modelProvider: outcome.modelProvider,
|
||||
time: outcome.time,
|
||||
cacheHit: outcome.cacheHit,
|
||||
});
|
||||
this._logRejectionTimeouts.delete(input.completionId);
|
||||
}, 10_000);
|
||||
this._outcomes.set(input.completionId, outcome);
|
||||
this._logRejectionTimeouts.set(input.completionId, logRejectionTimeout);
|
||||
|
||||
return outcome;
|
||||
} finally {
|
||||
this._abortControllers.delete(input.completionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import {
|
|||
SerializedContinueConfig,
|
||||
SessionInfo,
|
||||
} from ".";
|
||||
import { AutocompleteInput } from "./autocomplete/completionProvider";
|
||||
import { IdeProtocol } from "./web/webviewProtocol";
|
||||
|
||||
export type ProtocolGeneratorType<T> = AsyncGenerator<{
|
||||
|
|
@ -56,10 +57,7 @@ export type Protocol = {
|
|||
Promise<ContextSubmenuItem[]>,
|
||||
];
|
||||
"context/addDocs": [{ title: string; url: string }, void];
|
||||
"autocomplete/complete": [
|
||||
{ filepath: string; line: number; column: number },
|
||||
string[],
|
||||
];
|
||||
"autocomplete/complete": [AutocompleteInput, Promise<string[]>];
|
||||
"autocomplete/cancel": [undefined, void];
|
||||
"autocomplete/accept": [{ completionId: string }, void];
|
||||
"command/run": [
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.github.continuedev.continueintellijextension.autocomplete
|
|||
|
||||
import com.github.continuedev.continueintellijextension.`continue`.uuid
|
||||
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.application.invokeLater
|
||||
import com.intellij.openapi.components.Service
|
||||
import com.intellij.openapi.components.service
|
||||
|
|
@ -30,12 +31,11 @@ class AutocompleteService(private val project: Project) {
|
|||
pendingCompletion = PendingCompletion(editor, offset, completionId, null)
|
||||
|
||||
// Request a completion from the core
|
||||
val completion = "Hello, World!"
|
||||
pendingCompletion = pendingCompletion?.copy(text = completion)
|
||||
renderCompletion(editor, offset, completion)
|
||||
// service<ContinuePluginService>().coreMessenger?.request("autocomplete/complete", completionId, null, ({ response ->
|
||||
// renderCompletion(editor, offset, response)
|
||||
// }))
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/complete", completionId, null, ({ response ->
|
||||
val completion = response;
|
||||
renderCompletion(editor, offset, response)
|
||||
pendingCompletion = pendingCompletion?.copy(text = completion)
|
||||
}))
|
||||
}
|
||||
|
||||
private fun renderCompletion(editor: Editor, offset: Int, text: String) {
|
||||
|
|
@ -49,11 +49,12 @@ class AutocompleteService(private val project: Project) {
|
|||
val offset = completion.offset
|
||||
editor.document.insertString(offset, text)
|
||||
clearCompletions(editor)
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/accept", completion.completionId, null, ({}))
|
||||
}
|
||||
|
||||
private fun cancelCompletion(completion: PendingCompletion) {
|
||||
// Send cancellation message to core
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/cancel", completion.completionId,null, ({}))
|
||||
project.service<ContinuePluginService>().coreMessenger?.request("autocomplete/cancel", null,null, ({}))
|
||||
}
|
||||
|
||||
fun clearCompletions(editor: Editor) {
|
||||
|
|
|
|||
|
|
@ -140,6 +140,7 @@ class CoreMessenger(esbuildPath: String, continueCorePath: String, ideProtocolCl
|
|||
|
||||
// Start the subprocess
|
||||
val processBuilder = ProcessBuilder(continueCorePath)
|
||||
.directory(File(continueCorePath).parentFile)
|
||||
process = processBuilder.start()
|
||||
|
||||
val outputStream = process.outputStream
|
||||
|
|
@ -149,7 +150,8 @@ class CoreMessenger(esbuildPath: String, continueCorePath: String, ideProtocolCl
|
|||
reader = BufferedReader(InputStreamReader(inputStream))
|
||||
|
||||
process.onExit().thenRun {
|
||||
val err = process.errorStream.bufferedReader().readText()
|
||||
val err = process.errorStream.bufferedReader().readText().trim()
|
||||
println("Core process exited with output: $err")
|
||||
ideProtocolClient.showMessage("Core process exited with output: $err")
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {
|
|||
import { ConfigHandler } from "core/config/handler";
|
||||
import { logDevData } from "core/util/devdata";
|
||||
import { Telemetry } from "core/util/posthog";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import * as vscode from "vscode";
|
||||
import { TabAutocompleteModel } from "../util/loadAutocompleteModel";
|
||||
import { getDefinitionsFromLsp } from "./lsp";
|
||||
|
|
@ -62,6 +63,7 @@ export class ContinueCompletionProvider
|
|||
const signal = abortController.signal;
|
||||
token.onCancellationRequested(() => abortController.abort());
|
||||
const input: AutocompleteInput = {
|
||||
completionId: uuidv4(),
|
||||
filepath: document.uri.fsPath,
|
||||
pos: { line: position.line, character: position.character },
|
||||
recentlyEditedFiles: [],
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { ContextItemId, IDE } from "core";
|
||||
import { CompletionProvider } from "core/autocomplete/completionProvider";
|
||||
import { ConfigHandler } from "core/config/handler";
|
||||
import { addModel, addOpenAIKey, deleteModel } from "core/config/util";
|
||||
import { indexDocs } from "core/indexing/docs";
|
||||
|
|
@ -16,6 +17,7 @@ export class Core {
|
|||
private readonly ide: IDE;
|
||||
private readonly configHandler: ConfigHandler;
|
||||
private readonly codebaseIndexer: CodebaseIndexer;
|
||||
private readonly completionProvider: CompletionProvider;
|
||||
|
||||
private abortedMessageIds: Set<string> = new Set();
|
||||
|
||||
|
|
@ -46,6 +48,18 @@ export class Core {
|
|||
new PauseToken(false),
|
||||
);
|
||||
|
||||
const getLlm = async () => {
|
||||
const config = await this.configHandler.loadConfig();
|
||||
return config.tabAutocompleteModel;
|
||||
};
|
||||
this.completionProvider = new CompletionProvider(
|
||||
this.configHandler,
|
||||
ide,
|
||||
getLlm,
|
||||
(e) => {},
|
||||
(..._) => Promise.resolve([]),
|
||||
);
|
||||
|
||||
const on = this.messenger.on.bind(this.messenger);
|
||||
|
||||
// New
|
||||
|
|
@ -255,6 +269,20 @@ export class Core {
|
|||
on("command/run", (msg) =>
|
||||
runNodeJsSlashCommand(this.configHandler, this.abortedMessageIds, msg),
|
||||
);
|
||||
|
||||
// Autocomplete
|
||||
on("autocomplete/complete", async (msg) => {
|
||||
const outcome =
|
||||
await this.completionProvider.provideInlineCompletionItems(
|
||||
msg.data,
|
||||
undefined,
|
||||
);
|
||||
return outcome ? [outcome.completion] : [];
|
||||
});
|
||||
on("autocomplete/accept", async (msg) => {});
|
||||
on("autocomplete/cancel", async (msg) => {
|
||||
this.completionProvider.cancel();
|
||||
});
|
||||
}
|
||||
|
||||
public invoke<T extends keyof Protocol>(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue