🚧 reverse webview protocol
This commit is contained in:
parent
060431e0bb
commit
7e314df380
33 changed files with 627 additions and 573 deletions
|
|
@ -19,7 +19,10 @@ export class CodebaseIndexer {
|
|||
|
||||
const indexes = [
|
||||
new ChunkCodebaseIndex(this.ide.readFile.bind(this.ide)), // Chunking must come first
|
||||
new LanceDbIndex(config.embeddingsProvider, this.ide.readFile),
|
||||
new LanceDbIndex(
|
||||
config.embeddingsProvider,
|
||||
this.ide.readFile.bind(this.ide)
|
||||
),
|
||||
new FullTextSearchCodebaseIndex(),
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
PersistedSessionInfo,
|
||||
RangeInFile,
|
||||
SerializedContinueConfig,
|
||||
SessionInfo,
|
||||
} from "..";
|
||||
import { Message } from "../util/messenger";
|
||||
|
||||
|
|
@ -29,7 +30,7 @@ export type Protocol = {
|
|||
abort: [undefined, void];
|
||||
|
||||
// History
|
||||
"history/list": [undefined, PersistedSessionInfo[]];
|
||||
"history/list": [undefined, SessionInfo[]];
|
||||
"history/delete": [{ id: string }, void];
|
||||
"history/load": [{ id: string }, PersistedSessionInfo];
|
||||
"history/save": [PersistedSessionInfo, void];
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { PersistedSessionInfo, SessionInfo } from "..";
|
|||
import { getSessionFilePath, getSessionsListPath } from "./paths";
|
||||
|
||||
class HistoryManager {
|
||||
list(): PersistedSessionInfo[] {
|
||||
list(): SessionInfo[] {
|
||||
const filepath = getSessionsListPath();
|
||||
if (!fs.existsSync(filepath)) {
|
||||
return [];
|
||||
|
|
|
|||
|
|
@ -1,137 +0,0 @@
|
|||
import { v4 as uuidv4 } from "uuid";
|
||||
import { ChatMessage, LLMFullCompletionOptions, LLMReturnValue } from "..";
|
||||
interface vscode {
|
||||
postMessage(message: any): vscode;
|
||||
}
|
||||
|
||||
declare const vscode: any;
|
||||
|
||||
function _postToIde(type: string, data: any) {
|
||||
if (typeof vscode === "undefined") {
|
||||
if (localStorage.getItem("ide") === "jetbrains") {
|
||||
if ((window as any).postIntellijMessage === undefined) {
|
||||
console.log(
|
||||
"Unable to send message: postIntellijMessage is undefined. ",
|
||||
type,
|
||||
data
|
||||
);
|
||||
throw new Error("postIntellijMessage is undefined");
|
||||
}
|
||||
(window as any).postIntellijMessage?.(type, data);
|
||||
return;
|
||||
} else {
|
||||
console.log("Unable to send message: vscode is undefined. ", type, data);
|
||||
return;
|
||||
}
|
||||
}
|
||||
vscode.postMessage({
|
||||
type,
|
||||
...data,
|
||||
});
|
||||
}
|
||||
|
||||
export function postToIde(type: string, data: any, attempt: number = 0) {
|
||||
try {
|
||||
_postToIde(type, data);
|
||||
} catch (error) {
|
||||
if (attempt < 5) {
|
||||
console.log(`Attempt ${attempt} failed. Retrying...`);
|
||||
setTimeout(
|
||||
() => postToIde(type, data, attempt + 1),
|
||||
Math.pow(2, attempt) * 1000
|
||||
);
|
||||
} else {
|
||||
console.error("Max attempts reached. Message could not be sent.", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function ideRequest(type: string, message: any): Promise<any> {
|
||||
// message, messageId, and type are passed back and forth
|
||||
const messageId = uuidv4();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const handler = (event: any) => {
|
||||
if (event.data.messageId === messageId) {
|
||||
window.removeEventListener("message", handler);
|
||||
resolve(event.data.message);
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
|
||||
postToIde(type, { message, messageId });
|
||||
});
|
||||
}
|
||||
|
||||
export async function* ideStreamRequest(
|
||||
type: string,
|
||||
message: any,
|
||||
cancelToken?: AbortSignal
|
||||
): AsyncGenerator<any, any> {
|
||||
const messageId = uuidv4();
|
||||
|
||||
postToIde(type, { message, messageId });
|
||||
|
||||
let buffer = "";
|
||||
let index = 0;
|
||||
let done = false;
|
||||
let returnVal = undefined;
|
||||
|
||||
const handler = (event: any) => {
|
||||
if (event.data.messageId === messageId) {
|
||||
if (event.data.message.done) {
|
||||
window.removeEventListener("message", handler);
|
||||
done = true;
|
||||
returnVal = event.data.message.data;
|
||||
} else {
|
||||
buffer += event.data.message.content;
|
||||
}
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
|
||||
cancelToken?.addEventListener("abort", () => {
|
||||
postToIde("abort", { messageId });
|
||||
});
|
||||
|
||||
while (!done) {
|
||||
if (buffer.length > index) {
|
||||
const chunk = buffer.slice(index);
|
||||
index = buffer.length;
|
||||
yield chunk;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
if (buffer.length > index) {
|
||||
const chunk = buffer.slice(index);
|
||||
index = buffer.length;
|
||||
yield chunk;
|
||||
}
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
export async function* llmStreamChat(
|
||||
modelTitle: string,
|
||||
cancelToken: AbortSignal | undefined,
|
||||
messages: ChatMessage[],
|
||||
options: LLMFullCompletionOptions = {}
|
||||
): AsyncGenerator<ChatMessage, LLMReturnValue> {
|
||||
const gen = ideStreamRequest(
|
||||
"llmStreamChat",
|
||||
{
|
||||
messages,
|
||||
title: modelTitle,
|
||||
completionOptions: options,
|
||||
},
|
||||
cancelToken
|
||||
);
|
||||
|
||||
let next = await gen.next();
|
||||
while (!next.done) {
|
||||
yield { role: "user", content: next.value };
|
||||
next = await gen.next();
|
||||
}
|
||||
return { prompt: next.value?.prompt, completion: next.value?.completion };
|
||||
}
|
||||
|
|
@ -1,36 +1,49 @@
|
|||
import {
|
||||
ChatMessage,
|
||||
CompletionOptions,
|
||||
ContextItem,
|
||||
ContextItemWithId,
|
||||
ContextSubmenuItem,
|
||||
ContinueRcJson,
|
||||
DiffLine,
|
||||
LLMFullCompletionOptions,
|
||||
MessageContent,
|
||||
ModelDescription,
|
||||
PersistedSessionInfo,
|
||||
Problem,
|
||||
Range,
|
||||
RangeInFile,
|
||||
SessionInfo,
|
||||
} from "..";
|
||||
import { RangeInFileWithContents } from "../commands/util";
|
||||
import { BrowserSerializedContinueConfig } from "../config/load";
|
||||
|
||||
export type WebviewProtocol = {
|
||||
abort: [undefined, void];
|
||||
listWorkspaceContents: [undefined, Promise<string[]>];
|
||||
getWorkspaceDirs: [undefined, Promise<string[]>];
|
||||
listFolders: [undefined, Promise<string[]>];
|
||||
abort: [string, void];
|
||||
onLoad: [
|
||||
undefined,
|
||||
{
|
||||
windowId: string;
|
||||
serverUrl: string;
|
||||
workspacePaths: string[];
|
||||
vscMachineId: string;
|
||||
vscMediaUrl: string;
|
||||
},
|
||||
];
|
||||
listWorkspaceContents: [undefined, string[]];
|
||||
getWorkspaceDirs: [undefined, string[]];
|
||||
listFolders: [undefined, string[]];
|
||||
writeFile: [{ path: string; contents: string }, void];
|
||||
showVirtualFile: [{ name: string; content: string }, void];
|
||||
getContinueDir: [undefined, Promise<string>];
|
||||
getContinueDir: [undefined, string];
|
||||
openFile: [{ path: string }, void];
|
||||
runCommand: [{ command: string }, void];
|
||||
getSearchResults: [{ query: string }, Promise<string>];
|
||||
subprocess: [{ command: string }, Promise<[string, string]>];
|
||||
history: [undefined, PersistedSessionInfo[]];
|
||||
getSearchResults: [{ query: string }, string];
|
||||
subprocess: [{ command: string }, [string, string]];
|
||||
history: [undefined, SessionInfo[]];
|
||||
saveSession: [PersistedSessionInfo, void];
|
||||
deleteSession: [string, void];
|
||||
loadSession: [string, PersistedSessionInfo];
|
||||
saveFile: [{ filepath: string }, void];
|
||||
readFile: [{ filepath: string }, Promise<string>];
|
||||
readFile: [{ filepath: string }, string];
|
||||
showDiff: [
|
||||
{ filepath: string; newContents: string; stepIndex: number },
|
||||
void,
|
||||
|
|
@ -44,10 +57,10 @@ export type WebviewProtocol = {
|
|||
},
|
||||
void,
|
||||
];
|
||||
getProblems: [{ filepath: string }, Promise<Problem[]>];
|
||||
getBranch: [{ dir: string }, Promise<string>];
|
||||
getOpenFiles: [undefined, Promise<string[]>];
|
||||
getPinnedFiles: [undefined, Promise<string[]>];
|
||||
getProblems: [{ filepath: string }, Problem[]];
|
||||
getBranch: [{ dir: string }, string];
|
||||
getOpenFiles: [undefined, string[]];
|
||||
getPinnedFiles: [undefined, string[]];
|
||||
showLines: [{ filepath: string; startLine: number; endLine: number }, void];
|
||||
errorPopup: [{ message: string }, void];
|
||||
logDevData: [{ tableName: string; data: any }, void];
|
||||
|
|
@ -55,20 +68,28 @@ export type WebviewProtocol = {
|
|||
deleteModel: [{ title: string }, void];
|
||||
addOpenAIKey: [{ key: string }, void];
|
||||
llmStreamComplete: [
|
||||
{ title: string; prompt: string; completionOptions: CompletionOptions },
|
||||
{
|
||||
title: string;
|
||||
prompt: string;
|
||||
completionOptions: LLMFullCompletionOptions;
|
||||
},
|
||||
AsyncGenerator<{ content: string; done?: boolean }>,
|
||||
];
|
||||
llmStreamChat: [
|
||||
{
|
||||
title: string;
|
||||
messages: ChatMessage[];
|
||||
completionOptions: CompletionOptions;
|
||||
completionOptions: LLMFullCompletionOptions;
|
||||
},
|
||||
AsyncGenerator<{ content: MessageContent; done?: boolean }>,
|
||||
];
|
||||
llmComplete: [
|
||||
{ title: string; prompt: string; completionOptions: CompletionOptions },
|
||||
Promise<{ content: string }>,
|
||||
{
|
||||
title: string;
|
||||
prompt: string;
|
||||
completionOptions: LLMFullCompletionOptions;
|
||||
},
|
||||
{ content: string },
|
||||
];
|
||||
runNodeJsSlashCommand: [
|
||||
{
|
||||
|
|
@ -82,7 +103,7 @@ export type WebviewProtocol = {
|
|||
},
|
||||
AsyncGenerator<{ content: string; done?: boolean }>,
|
||||
];
|
||||
loadSubmenuItems: [{ title: string }, Promise<ContextSubmenuItem[]>];
|
||||
loadSubmenuItems: [{ title: string }, ContextSubmenuItem[]];
|
||||
getContextItems: [
|
||||
{
|
||||
name: string;
|
||||
|
|
@ -90,19 +111,50 @@ export type WebviewProtocol = {
|
|||
fullInput: string;
|
||||
selectedCode: RangeInFile[];
|
||||
},
|
||||
Promise<ContextItem[]>,
|
||||
ContextItemWithId[],
|
||||
];
|
||||
addDocs: [{ url: string; title: string }, void];
|
||||
applyToCurrentFile: [{ text: string }, void];
|
||||
showTutorial: [undefined, void];
|
||||
showFile: [{ filepath: string }, void];
|
||||
openConfigJson: [undefined, void];
|
||||
readRangeInFile: [{ filepath: string }, void];
|
||||
readRangeInFile: [{ filepath: string; range: Range }, string];
|
||||
toggleDevTools: [undefined, void];
|
||||
reloadWindow: [undefined, void];
|
||||
focusEditor: [undefined, void];
|
||||
toggleFullScreen: [undefined, void];
|
||||
getDiff: [undefined, Promise<string>];
|
||||
getSerializedConfig: [undefined, Promise<BrowserSerializedContinueConfig>];
|
||||
getTerminalContents: [undefined, Promise<string>];
|
||||
getDiff: [undefined, string];
|
||||
getSerializedConfig: [undefined, BrowserSerializedContinueConfig];
|
||||
getTerminalContents: [undefined, string];
|
||||
isTelemetryEnabled: [undefined, boolean];
|
||||
getUniqueId: [undefined, string];
|
||||
getWorkspaceConfigs: [undefined, ContinueRcJson[]];
|
||||
getDefaultModelTitle: [{ defaultModelTitle: string }, void];
|
||||
};
|
||||
|
||||
export type ReverseWebviewProtocol = {
|
||||
setInactive: [undefined, void];
|
||||
configUpdate: [undefined, void];
|
||||
submitMessage: [{ message: any }, void]; // any -> JSONContent from TipTap
|
||||
addContextItem: [
|
||||
{
|
||||
historyIndex: number;
|
||||
item: ContextItemWithId;
|
||||
},
|
||||
void,
|
||||
];
|
||||
getDefaultModelTitle: [undefined, string];
|
||||
newSessionWithPrompt: [{ prompt: string }, void];
|
||||
userInput: [{ input: string }, void];
|
||||
focusContinueInput: [undefined, void];
|
||||
focusContinueInputWithoutClear: [undefined, void];
|
||||
focusContinueInputWithNewSession: [undefined, void];
|
||||
highlightedCode: [{ rangeInFileWithContents: RangeInFileWithContents }, void];
|
||||
addModel: [undefined, void];
|
||||
openSettings: [undefined, void];
|
||||
viewHistory: [undefined, void];
|
||||
indexProgress: [{ progress: number; desc: string }, void];
|
||||
newSession: [undefined, void];
|
||||
refreshSubmenuItems: [undefined, void];
|
||||
setTheme: [{ theme: any }, void];
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@ import { ContinueGUIWebviewViewProvider } from "./debugPanel";
|
|||
import { DiffManager } from "./diff/horizontal";
|
||||
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
|
||||
import { AutocompleteOutcome } from "./lang-server/completionProvider";
|
||||
import { VsCodeWebviewProtocol } from "./webviewProtocol";
|
||||
|
||||
function addHighlightedCodeToContext(
|
||||
edit: boolean,
|
||||
webview: vscode.Webview | undefined
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined
|
||||
) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor) {
|
||||
|
|
@ -36,10 +37,8 @@ function addHighlightedCodeToContext(
|
|||
},
|
||||
};
|
||||
|
||||
webview?.postMessage({
|
||||
type: "highlightedCode",
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
edit,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +46,7 @@ function addHighlightedCodeToContext(
|
|||
async function addEntireFileToContext(
|
||||
filepath: vscode.Uri,
|
||||
edit: boolean,
|
||||
debugPanelWebview: vscode.Webview | undefined
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined
|
||||
) {
|
||||
// If a directory, add all files in the directory
|
||||
const stat = await vscode.workspace.fs.stat(filepath);
|
||||
|
|
@ -58,7 +57,7 @@ async function addEntireFileToContext(
|
|||
addEntireFileToContext(
|
||||
vscode.Uri.joinPath(filepath, filename),
|
||||
edit,
|
||||
debugPanelWebview
|
||||
webviewProtocol
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -82,10 +81,8 @@ async function addEntireFileToContext(
|
|||
},
|
||||
};
|
||||
|
||||
debugPanelWebview?.postMessage({
|
||||
type: "highlightedCode",
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
edit,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -126,29 +123,28 @@ const commandsMap: (
|
|||
verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index);
|
||||
},
|
||||
"continue.quickFix": async (message: string, code: string, edit: boolean) => {
|
||||
sidebar.webview?.postMessage({
|
||||
type: "newSessionWithPrompt",
|
||||
sidebar.webviewProtocol?.request("newSessionWithPrompt", {
|
||||
prompt: `${
|
||||
edit ? "/edit " : ""
|
||||
}${code}\n\nHow do I fix this problem in the above code?: ${message}`,
|
||||
});
|
||||
|
||||
if (!edit) {
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
}
|
||||
},
|
||||
"continue.focusContinueInput": async () => {
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
sidebar.webview?.postMessage({
|
||||
type: "focusContinueInput",
|
||||
});
|
||||
addHighlightedCodeToContext(false, sidebar.webview);
|
||||
sidebar.webviewProtocol?.request("focusContinueInput", undefined);
|
||||
addHighlightedCodeToContext(false, sidebar.webviewProtocol);
|
||||
},
|
||||
"continue.focusContinueInputWithoutClear": async () => {
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
sidebar.webview?.postMessage({
|
||||
type: "focusContinueInputWithoutClear",
|
||||
});
|
||||
addHighlightedCodeToContext(true, sidebar.webview);
|
||||
sidebar.webviewProtocol?.request(
|
||||
"focusContinueInputWithoutClear",
|
||||
undefined
|
||||
);
|
||||
addHighlightedCodeToContext(true, sidebar.webviewProtocol);
|
||||
},
|
||||
"continue.toggleAuxiliaryBar": () => {
|
||||
vscode.commands.executeCommand("workbench.action.toggleAuxiliaryBar");
|
||||
|
|
@ -267,8 +263,7 @@ const commandsMap: (
|
|||
"continue.debugTerminal": async () => {
|
||||
const terminalContents = await ide.getTerminalContents();
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
sidebar.webview?.postMessage({
|
||||
type: "userInput",
|
||||
sidebar.webviewProtocol?.request("userInput", {
|
||||
input: `I got the following error, can you please help explain how to fix it?\n\n${terminalContents}`,
|
||||
});
|
||||
},
|
||||
|
|
@ -281,15 +276,11 @@ const commandsMap: (
|
|||
// Commands without keyboard shortcuts
|
||||
"continue.addModel": () => {
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
sidebar.webview?.postMessage({
|
||||
type: "addModel",
|
||||
});
|
||||
sidebar.webviewProtocol?.request("addModel", undefined);
|
||||
},
|
||||
"continue.openSettingsUI": () => {
|
||||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
sidebar.webview?.postMessage({
|
||||
type: "openSettings",
|
||||
});
|
||||
sidebar.webviewProtocol?.request("openSettings", undefined);
|
||||
},
|
||||
"continue.sendMainUserInput": (text: string) => {
|
||||
sidebar.sendMainUserInput(text);
|
||||
|
|
@ -323,14 +314,10 @@ const commandsMap: (
|
|||
ide.runCommand(text);
|
||||
},
|
||||
"continue.newSession": () => {
|
||||
sidebar.webview?.postMessage({
|
||||
type: "newSession",
|
||||
});
|
||||
sidebar.webviewProtocol?.request("newSession", undefined);
|
||||
},
|
||||
"continue.viewHistory": () => {
|
||||
sidebar.webview?.postMessage({
|
||||
type: "viewHistory",
|
||||
});
|
||||
sidebar.webviewProtocol?.request("viewHistory", undefined);
|
||||
},
|
||||
"continue.toggleFullScreen": () => {
|
||||
// Check if full screen is already open by checking open tabs
|
||||
|
|
@ -392,7 +379,7 @@ const commandsMap: (
|
|||
vscode.commands.executeCommand("continue.continueGUIView.focus");
|
||||
|
||||
for (const uri of uris) {
|
||||
addEntireFileToContext(uri, false, sidebar.webview);
|
||||
addEntireFileToContext(uri, false, sidebar.webviewProtocol);
|
||||
}
|
||||
},
|
||||
"continue.updateAllReferences": (filepath: vscode.Uri) => {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ export class ContinueGUIWebviewViewProvider
|
|||
implements vscode.WebviewViewProvider
|
||||
{
|
||||
public static readonly viewType = "continue.continueGUIView";
|
||||
public webviewProtocol?: VsCodeWebviewProtocol;
|
||||
|
||||
resolveWebviewView(
|
||||
webviewView: vscode.WebviewView,
|
||||
|
|
@ -121,11 +122,11 @@ export class ContinueGUIWebviewViewProvider
|
|||
vscode.workspace.onDidChangeConfiguration((e) => {
|
||||
if (e.affectsConfiguration("workbench.colorTheme")) {
|
||||
// Send new theme to GUI to update embedded Monaco themes
|
||||
this._webview?.postMessage({ type: "setTheme", theme: getTheme() });
|
||||
this.webviewProtocol?.request("setTheme", { theme: getTheme() });
|
||||
}
|
||||
});
|
||||
|
||||
const webviewProtocol = new VsCodeWebviewProtocol(
|
||||
this.webviewProtocol = new VsCodeWebviewProtocol(
|
||||
panel.webview,
|
||||
ide,
|
||||
configHandler,
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import * as path from "path";
|
|||
import * as vscode from "vscode";
|
||||
import { getMetaKeyLabel, getPlatform } from "../util/util";
|
||||
import { uriFromFilePath } from "../util/vscode";
|
||||
import { VsCodeWebviewProtocol } from "../webviewProtocol";
|
||||
|
||||
interface DiffInfo {
|
||||
originalFilepath: string;
|
||||
|
|
@ -47,16 +48,9 @@ export class DiffManager {
|
|||
}
|
||||
}
|
||||
|
||||
webview: vscode.Webview | undefined;
|
||||
private readonly extensionContext: vscode.ExtensionContext;
|
||||
|
||||
constructor(
|
||||
webview: vscode.Webview | undefined,
|
||||
extensionContext: vscode.ExtensionContext
|
||||
) {
|
||||
this.webview = webview;
|
||||
this.extensionContext = extensionContext;
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined;
|
||||
|
||||
constructor(private readonly extensionContext: vscode.ExtensionContext) {
|
||||
this.setupDirectory();
|
||||
|
||||
// Listen for file closes, and if it's a diff file, clean up
|
||||
|
|
@ -303,7 +297,7 @@ export class DiffManager {
|
|||
}
|
||||
|
||||
// Stop the step at step_index in case it is still streaming
|
||||
this.webview?.postMessage({ type: "setInactive" });
|
||||
this.webviewProtocol?.request("setInactive", undefined);
|
||||
|
||||
vscode.workspace.textDocuments
|
||||
.find((doc) => doc.uri.fsPath === newFilepath)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import {
|
|||
setupStatusBar,
|
||||
} from "../lang-server/completionProvider";
|
||||
import { TabAutocompleteModel } from "../util/loadAutocompleteModel";
|
||||
import { VsCodeWebviewProtocol } from "../webviewProtocol";
|
||||
|
||||
export class VsCodeExtension {
|
||||
private configHandler: ConfigHandler;
|
||||
|
|
@ -24,9 +25,10 @@ export class VsCodeExtension {
|
|||
private indexer: CodebaseIndexer;
|
||||
private diffManager: DiffManager;
|
||||
private verticalDiffManager: VerticalPerLineDiffManager;
|
||||
private webviewProtocol: VsCodeWebviewProtocol;
|
||||
|
||||
constructor(context: vscode.ExtensionContext) {
|
||||
this.diffManager = new DiffManager(undefined, context);
|
||||
this.diffManager = new DiffManager(context);
|
||||
this.ide = new VsCodeIde(this.diffManager);
|
||||
this.configHandler = new ConfigHandler(this.ide);
|
||||
this.verticalDiffManager = new VerticalPerLineDiffManager(
|
||||
|
|
@ -42,7 +44,20 @@ export class VsCodeExtension {
|
|||
this.extensionContext,
|
||||
this.verticalDiffManager
|
||||
);
|
||||
this.diffManager.webview = this.sidebar.webview;
|
||||
|
||||
// Sidebar
|
||||
context.subscriptions.push(
|
||||
vscode.window.registerWebviewViewProvider(
|
||||
"continue.continueGUIView",
|
||||
this.sidebar,
|
||||
{
|
||||
webviewOptions: { retainContextWhenHidden: true },
|
||||
}
|
||||
)
|
||||
);
|
||||
this.webviewProtocol = this.sidebar.webviewProtocol!;
|
||||
|
||||
this.diffManager.webviewProtocol = this.webviewProtocol;
|
||||
this.indexer = new CodebaseIndexer(this.configHandler, this.ide);
|
||||
|
||||
// CodeLens
|
||||
|
|
@ -74,17 +89,6 @@ export class VsCodeExtension {
|
|||
);
|
||||
}
|
||||
|
||||
// Sidebar
|
||||
context.subscriptions.push(
|
||||
vscode.window.registerWebviewViewProvider(
|
||||
"continue.continueGUIView",
|
||||
this.sidebar,
|
||||
{
|
||||
webviewOptions: { retainContextWhenHidden: true },
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// Commands
|
||||
registerAllCommands(
|
||||
context,
|
||||
|
|
@ -116,9 +120,7 @@ export class VsCodeExtension {
|
|||
filepath.endsWith(".continueignore") ||
|
||||
filepath.endsWith(".gitignore")
|
||||
) {
|
||||
this.sidebar.webview?.postMessage({
|
||||
type: "updateEmbeddings",
|
||||
});
|
||||
// Update embeddings! (TODO)
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -172,12 +174,8 @@ export class VsCodeExtension {
|
|||
private PREVIOUS_BRANCH_FOR_WORKSPACE_DIR: { [dir: string]: string } = {};
|
||||
|
||||
private async refreshCodebaseIndex(dirs: string[]) {
|
||||
for await (const { progress, desc } of this.indexer.refresh(dirs)) {
|
||||
this.sidebar.webview?.postMessage({
|
||||
type: "indexProgress",
|
||||
progress,
|
||||
desc,
|
||||
});
|
||||
for await (const update of this.indexer.refresh(dirs)) {
|
||||
this.webviewProtocol.request("indexProgress", update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,12 @@ import { logDevData } from "core/util/devdata";
|
|||
import historyManager from "core/util/history";
|
||||
import { Message } from "core/util/messenger";
|
||||
import { getConfigJsonPath } from "core/util/paths";
|
||||
import { WebviewProtocol } from "core/web/webviewProtocol";
|
||||
import {
|
||||
ReverseWebviewProtocol,
|
||||
WebviewProtocol,
|
||||
} from "core/web/webviewProtocol";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import * as vscode from "vscode";
|
||||
import { Webview } from "vscode";
|
||||
import { showTutorial } from "./activation/activate";
|
||||
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
|
||||
|
||||
|
|
@ -18,19 +20,21 @@ export class VsCodeWebviewProtocol {
|
|||
listeners = new Map<keyof WebviewProtocol, ((message: Message) => any)[]>();
|
||||
abortedMessageIds: Set<string> = new Set();
|
||||
|
||||
private send(messageType: string, message: any, messageId?: string): string {
|
||||
private send(messageType: string, data: any, messageId?: string): string {
|
||||
const id = messageId || uuidv4();
|
||||
this.webview.postMessage({
|
||||
type: messageType,
|
||||
message,
|
||||
messageId: messageId || id,
|
||||
messageType,
|
||||
data,
|
||||
messageId: id,
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
private on<T extends keyof WebviewProtocol>(
|
||||
messageType: T,
|
||||
handler: (message: Message<WebviewProtocol[T][0]>) => WebviewProtocol[T][1]
|
||||
handler: (
|
||||
message: Message<WebviewProtocol[T][0]>
|
||||
) => Promise<WebviewProtocol[T][1]> | WebviewProtocol[T][1]
|
||||
): void {
|
||||
if (!this.listeners.has(messageType)) {
|
||||
this.listeners.set(messageType, []);
|
||||
|
|
@ -39,7 +43,7 @@ export class VsCodeWebviewProtocol {
|
|||
}
|
||||
|
||||
constructor(
|
||||
private readonly webview: Webview,
|
||||
readonly webview: vscode.Webview,
|
||||
private readonly ide: IDE,
|
||||
private readonly configHandler: ConfigHandler,
|
||||
private readonly verticalDiffManager: VerticalPerLineDiffManager
|
||||
|
|
@ -101,15 +105,17 @@ export class VsCodeWebviewProtocol {
|
|||
this.on("openConfigJson", (msg) => {
|
||||
this.ide.openFile(getConfigJsonPath());
|
||||
});
|
||||
this.on("readRangeInFile", (msg) => {
|
||||
vscode.workspace.openTextDocument(msg.data.filepath).then((document) => {
|
||||
let start = new vscode.Position(0, 0);
|
||||
let end = new vscode.Position(5, 0);
|
||||
let range = new vscode.Range(start, end);
|
||||
this.on("readRangeInFile", async (msg) => {
|
||||
return await vscode.workspace
|
||||
.openTextDocument(msg.data.filepath)
|
||||
.then((document) => {
|
||||
let start = new vscode.Position(0, 0);
|
||||
let end = new vscode.Position(5, 0);
|
||||
let range = new vscode.Range(start, end);
|
||||
|
||||
let contents = document.getText(range);
|
||||
return contents;
|
||||
});
|
||||
let contents = document.getText(range);
|
||||
return contents;
|
||||
});
|
||||
});
|
||||
this.on("toggleDevTools", (msg) => {
|
||||
vscode.commands.executeCommand("workbench.action.toggleDevTools");
|
||||
|
|
@ -171,11 +177,9 @@ export class VsCodeWebviewProtocol {
|
|||
});
|
||||
this.on("saveSession", (msg) => {
|
||||
historyManager.save(msg.data);
|
||||
return {};
|
||||
});
|
||||
this.on("deleteSession", (msg) => {
|
||||
historyManager.delete(msg.data);
|
||||
return {};
|
||||
});
|
||||
this.on("loadSession", (msg) => {
|
||||
return historyManager.load(msg.data);
|
||||
|
|
@ -357,9 +361,9 @@ export class VsCodeWebviewProtocol {
|
|||
params,
|
||||
ide,
|
||||
addContextItem: (item) => {
|
||||
protocol.webview?.postMessage({
|
||||
type: "addContextItem",
|
||||
message: { item, historyIndex },
|
||||
protocol.request("addContextItem", {
|
||||
item,
|
||||
historyIndex,
|
||||
});
|
||||
},
|
||||
})) {
|
||||
|
|
@ -458,9 +462,7 @@ export class VsCodeWebviewProtocol {
|
|||
`🎉 Successfully indexed ${title}`
|
||||
);
|
||||
|
||||
this.webview?.postMessage({
|
||||
type: "refreshSubmenuItems",
|
||||
});
|
||||
this.request("refreshSubmenuItems", undefined);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
@ -487,4 +489,18 @@ export class VsCodeWebviewProtocol {
|
|||
showTutorial();
|
||||
});
|
||||
}
|
||||
|
||||
public request<T extends keyof ReverseWebviewProtocol>(
|
||||
messageType: T,
|
||||
data: ReverseWebviewProtocol[T][0]
|
||||
): Promise<ReverseWebviewProtocol[T][1]> {
|
||||
const messageId = uuidv4();
|
||||
return new Promise((resolve) => {
|
||||
this.send(messageType, data, messageId);
|
||||
const disposable = this.webview.onDidReceiveMessage((msg) => {
|
||||
resolve(msg);
|
||||
disposable.dispose();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import {
|
|||
Cog6ToothIcon,
|
||||
QuestionMarkCircleIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { postToIde } from "core/ide/messaging";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { Outlet, useLocation, useNavigate } from "react-router-dom";
|
||||
|
|
@ -13,6 +12,7 @@ import {
|
|||
vscForeground,
|
||||
vscInputBackground,
|
||||
} from ".";
|
||||
import { useWebviewListener } from "../hooks/useWebviewListener";
|
||||
import { defaultModelSelector } from "../redux/selectors/modelSelectors";
|
||||
import {
|
||||
setBottomMessage,
|
||||
|
|
@ -21,6 +21,7 @@ import {
|
|||
} from "../redux/slices/uiStateSlice";
|
||||
import { RootStore } from "../redux/store";
|
||||
import { getFontSize, isMetaEquivalentKeyPressed } from "../util";
|
||||
import { postToIde } from "../util/ide";
|
||||
import HeaderButtonWithText from "./HeaderButtonWithText";
|
||||
import TextDialog from "./dialogs";
|
||||
import IndexingProgressBar from "./loaders/IndexingProgressBar";
|
||||
|
|
@ -146,30 +147,35 @@ const Layout = () => {
|
|||
};
|
||||
}, [timeline]);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (event: any) => {
|
||||
if (event.data.type === "addModel") {
|
||||
navigate("/models");
|
||||
} else if (event.data.type === "openSettings") {
|
||||
// navigate("/settings");
|
||||
postToIde("openConfigJson", {});
|
||||
} else if (event.data.type === "viewHistory") {
|
||||
// Toggle the history page / main page
|
||||
if (location.pathname === "/history") {
|
||||
navigate("/");
|
||||
} else {
|
||||
navigate("/history");
|
||||
}
|
||||
} else if (event.data.type === "indexProgress") {
|
||||
setIndexingProgress(event.data.progress);
|
||||
setIndexingTask(event.data.desc);
|
||||
useWebviewListener(
|
||||
"addModel",
|
||||
async () => {
|
||||
navigate("/models");
|
||||
},
|
||||
[navigate]
|
||||
);
|
||||
|
||||
useWebviewListener("openSettings", async () => {
|
||||
postToIde("openConfigJson", undefined);
|
||||
});
|
||||
|
||||
useWebviewListener(
|
||||
"viewHistory",
|
||||
async () => {
|
||||
// Toggle the history page / main page
|
||||
if (location.pathname === "/history") {
|
||||
navigate("/");
|
||||
} else {
|
||||
navigate("/history");
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
return () => {
|
||||
window.removeEventListener("message", handler);
|
||||
};
|
||||
}, [location, navigate]);
|
||||
},
|
||||
[location, navigate]
|
||||
);
|
||||
|
||||
useWebviewListener("indexProgress", async (data) => {
|
||||
setIndexingProgress(data.progress);
|
||||
setIndexingTask(data.desc);
|
||||
});
|
||||
|
||||
const [indexingProgress, setIndexingProgress] = useState(1);
|
||||
const [indexingTask, setIndexingTask] = useState("Indexing Codebase");
|
||||
|
|
@ -259,7 +265,7 @@ const Layout = () => {
|
|||
<HeaderButtonWithText
|
||||
onClick={() => {
|
||||
// navigate("/settings");
|
||||
postToIde("openConfigJson", {});
|
||||
postToIde("openConfigJson", undefined);
|
||||
}}
|
||||
text="Config"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { postToIde } from "core/ide/messaging";
|
||||
import { usePostHog } from "posthog-js/react";
|
||||
import React, { useContext, useLayoutEffect } from "react";
|
||||
import { useDispatch } from "react-redux";
|
||||
|
|
@ -6,6 +5,7 @@ import styled from "styled-components";
|
|||
import { Button, Input } from "..";
|
||||
import { SubmenuContextProvidersContext } from "../../App";
|
||||
import { setShowDialog } from "../../redux/slices/uiStateSlice";
|
||||
import { postToIde } from "../../util/ide";
|
||||
|
||||
const GridDiv = styled.div`
|
||||
display: grid;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/outline";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { contextItemToRangeInFileWithContents } from "core/commands/util";
|
||||
import { ExtensionIde } from "core/ide";
|
||||
import React from "react";
|
||||
import styled from "styled-components";
|
||||
import {
|
||||
|
|
@ -10,6 +9,7 @@ import {
|
|||
vscBackground,
|
||||
vscForeground,
|
||||
} from "..";
|
||||
import { WebviewIde } from "../../util/webviewIde";
|
||||
import FileIcon from "../FileIcon";
|
||||
|
||||
const ContextItemDiv = styled.div`
|
||||
|
|
@ -49,16 +49,16 @@ const ContextItemsPeek = (props: ContextItemsPeekProps) => {
|
|||
) {
|
||||
if (contextItem.name.includes(" (") && contextItem.name.endsWith(")")) {
|
||||
const rif = contextItemToRangeInFileWithContents(contextItem);
|
||||
new ExtensionIde().showLines(
|
||||
new WebviewIde().showLines(
|
||||
rif.filepath,
|
||||
rif.range.start.line,
|
||||
rif.range.end.line
|
||||
);
|
||||
} else {
|
||||
new ExtensionIde().openFile(contextItem.description);
|
||||
new WebviewIde().openFile(contextItem.description);
|
||||
}
|
||||
} else {
|
||||
new ExtensionIde().showVirtualFile(contextItem.name, contextItem.content);
|
||||
new WebviewIde().showVirtualFile(contextItem.name, contextItem.content);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { JSONContent } from "@tiptap/react";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import styled, { keyframes } from "styled-components";
|
||||
import { defaultBorderRadius, vscBackground } from "..";
|
||||
import { useWebviewListener } from "../../hooks/useWebviewListener";
|
||||
import { selectSlashCommands } from "../../redux/selectors";
|
||||
import { newSession } from "../../redux/slices/stateSlice";
|
||||
import { RootStore } from "../../redux/store";
|
||||
|
|
@ -70,19 +71,10 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
|
||||
const [editorState, setEditorState] = useState(props.editorState);
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => {
|
||||
if (e.data.type === "newSessionWithPrompt") {
|
||||
dispatch(newSession());
|
||||
setEditorState(e.data.prompt);
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("message", listener);
|
||||
return () => {
|
||||
window.removeEventListener("message", listener);
|
||||
};
|
||||
}, []);
|
||||
useWebviewListener("newSessionWithPrompt", async (data) => {
|
||||
dispatch(newSession());
|
||||
setEditorState(data.prompt as any); // any -> JSONContent
|
||||
});
|
||||
|
||||
return (
|
||||
<div
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import {
|
|||
import { SubmenuContextProvidersContext } from "../../App";
|
||||
import useHistory from "../../hooks/useHistory";
|
||||
import useUpdatingRef from "../../hooks/useUpdatingRef";
|
||||
import { useWebviewListener } from "../../hooks/useWebviewListener";
|
||||
import { setEditingContextItemAtIndex } from "../../redux/slices/stateSlice";
|
||||
import { RootStore } from "../../redux/store";
|
||||
import { isMetaEquivalentKeyPressed } from "../../util";
|
||||
|
|
@ -225,7 +226,7 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
const editor = useEditor({
|
||||
const editor: Editor = useEditor({
|
||||
extensions: [
|
||||
Document,
|
||||
History,
|
||||
|
|
@ -358,7 +359,7 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
setIgnoreHighlightedCode(false);
|
||||
}, 100);
|
||||
} else if (event.key === "Escape") {
|
||||
postToIde("focusEditor", {});
|
||||
postToIde("focusEditor", undefined);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -370,6 +371,109 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
}, []);
|
||||
|
||||
// IDE event listeners
|
||||
useWebviewListener(
|
||||
"userInput",
|
||||
async (data) => {
|
||||
if (!props.isMainInput) {
|
||||
return;
|
||||
}
|
||||
editor?.commands.insertContent(data.input);
|
||||
onEnterRef.current(editor.getJSON());
|
||||
},
|
||||
[editor, onEnterRef.current, props.isMainInput]
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
"focusContinueInput",
|
||||
async (data) => {
|
||||
if (!props.isMainInput) {
|
||||
return;
|
||||
}
|
||||
if (historyLength > 0) {
|
||||
saveSession();
|
||||
}
|
||||
editor?.commands.focus("end");
|
||||
},
|
||||
[historyLength, saveSession, editor, props.isMainInput]
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
"focusContinueInputWithoutClear",
|
||||
async () => {
|
||||
if (!props.isMainInput) {
|
||||
return;
|
||||
}
|
||||
editor?.commands.focus("end");
|
||||
},
|
||||
[editor, props.isMainInput]
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
"focusContinueInputWithNewSession",
|
||||
async () => {
|
||||
if (!props.isMainInput) {
|
||||
return;
|
||||
}
|
||||
saveSession();
|
||||
editor?.commands.focus("end");
|
||||
},
|
||||
[editor, props.isMainInput]
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
"highlightedCode",
|
||||
async (data) => {
|
||||
if (!props.isMainInput || !editor) {
|
||||
return;
|
||||
}
|
||||
if (!ignoreHighlightedCode) {
|
||||
const rif: RangeInFile & { contents: string } =
|
||||
data.rangeInFileWithContents;
|
||||
const basename = getBasename(rif.filepath);
|
||||
const item: ContextItemWithId = {
|
||||
content: rif.contents,
|
||||
name: `${basename} (${rif.range.start.line + 1}-${
|
||||
rif.range.end.line + 1
|
||||
})`,
|
||||
description: rif.filepath,
|
||||
id: {
|
||||
providerTitle: "code",
|
||||
itemId: rif.filepath,
|
||||
},
|
||||
};
|
||||
|
||||
let index = 0;
|
||||
for (const el of editor.getJSON().content) {
|
||||
if (el.type === "codeBlock") {
|
||||
index += 2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
editor
|
||||
.chain()
|
||||
.insertContentAt(index, {
|
||||
type: "codeBlock",
|
||||
attrs: {
|
||||
item,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
setTimeout(() => {
|
||||
editor.commands.focus("end");
|
||||
}, 100);
|
||||
}
|
||||
setIgnoreHighlightedCode(false);
|
||||
},
|
||||
[
|
||||
editor,
|
||||
props.isMainInput,
|
||||
historyLength,
|
||||
ignoreHighlightedCode,
|
||||
props.isMainInput,
|
||||
]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.isMainInput) {
|
||||
return;
|
||||
|
|
@ -377,68 +481,6 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
if (editor && document.hasFocus()) {
|
||||
editor.commands.focus();
|
||||
}
|
||||
const handler = async (event: any) => {
|
||||
if (!editor) return;
|
||||
|
||||
if (event.data.type === "userInput") {
|
||||
const input = event.data.input;
|
||||
editor.commands.insertContent(input);
|
||||
onEnterRef.current(editor.getJSON());
|
||||
} else if (event.data.type === "focusContinueInput") {
|
||||
if (historyLength > 0) {
|
||||
saveSession();
|
||||
}
|
||||
editor.commands.focus("end");
|
||||
} else if (event.data.type === "focusContinueInputWithoutClear") {
|
||||
editor.commands.focus("end");
|
||||
} else if (event.data.type === "focusContinueInputWithNewSession") {
|
||||
saveSession();
|
||||
editor.commands.focus("end");
|
||||
} else if (event.data.type === "highlightedCode") {
|
||||
if (!ignoreHighlightedCode) {
|
||||
const rif: RangeInFile & { contents: string } =
|
||||
event.data.rangeInFileWithContents;
|
||||
const basename = getBasename(rif.filepath);
|
||||
const item: ContextItemWithId = {
|
||||
content: rif.contents,
|
||||
name: `${basename} (${rif.range.start.line + 1}-${
|
||||
rif.range.end.line + 1
|
||||
})`,
|
||||
description: rif.filepath,
|
||||
id: {
|
||||
providerTitle: "code",
|
||||
itemId: rif.filepath,
|
||||
},
|
||||
};
|
||||
|
||||
let index = 0;
|
||||
for (const el of editor.getJSON().content) {
|
||||
if (el.type === "codeBlock") {
|
||||
index += 2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
editor
|
||||
.chain()
|
||||
.insertContentAt(index, {
|
||||
type: "codeBlock",
|
||||
attrs: {
|
||||
item,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
setTimeout(() => {
|
||||
editor.commands.focus("end");
|
||||
}, 100);
|
||||
}
|
||||
setIgnoreHighlightedCode(false);
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
return () => {
|
||||
window.removeEventListener("message", handler);
|
||||
};
|
||||
}, [editor, props.isMainInput, historyLength, ignoreHighlightedCode]);
|
||||
|
||||
const [showDragOverMsg, setShowDragOverMsg] = useState(false);
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import {
|
|||
MessagePart,
|
||||
RangeInFile,
|
||||
} from "core";
|
||||
import { ExtensionIde } from "core/ide";
|
||||
import { ideRequest } from "core/ide/messaging";
|
||||
import { stripImages } from "core/llm/countTokens";
|
||||
import { getBasename } from "core/util";
|
||||
import { ideRequest } from "../../util/ide";
|
||||
import { WebviewIde } from "../../util/webviewIde";
|
||||
|
||||
interface MentionAttrs {
|
||||
label: string;
|
||||
|
|
@ -87,7 +87,7 @@ async function resolveEditorContent(
|
|||
|
||||
let contextItemsText = "";
|
||||
let contextItems: ContextItemWithId[] = [];
|
||||
const ide = new ExtensionIde();
|
||||
const ide = new WebviewIde();
|
||||
for (const item of contextItemAttrs) {
|
||||
if (item.itemType === "file") {
|
||||
// This is a quick way to resolve @file references
|
||||
|
|
@ -110,10 +110,7 @@ async function resolveEditorContent(
|
|||
fullInput: stripImages(parts),
|
||||
selectedCode,
|
||||
};
|
||||
const { items: resolvedItems } = await ideRequest(
|
||||
"getContextItems",
|
||||
data
|
||||
);
|
||||
const resolvedItems = await ideRequest("getContextItems", data);
|
||||
contextItems.push(...resolvedItems);
|
||||
for (const resolvedItem of resolvedItems) {
|
||||
contextItemsText += resolvedItem.content + "\n\n";
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ import {
|
|||
ClipboardIcon,
|
||||
PlayIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { postToIde } from "core/ide/messaging";
|
||||
import { useState } from "react";
|
||||
import styled from "styled-components";
|
||||
import { vscEditorBackground } from "..";
|
||||
import { postToIde } from "../../util/ide";
|
||||
import HeaderButtonWithText from "../HeaderButtonWithText";
|
||||
|
||||
const TopDiv = styled.div`
|
||||
|
|
|
|||
|
|
@ -5,13 +5,13 @@ import {
|
|||
XMarkIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { ExtensionIde } from "core/ide";
|
||||
import { getMarkdownLanguageTagForFile } from "core/util";
|
||||
import React from "react";
|
||||
import styled from "styled-components";
|
||||
import { defaultBorderRadius, lightGray, vscEditorBackground } from "..";
|
||||
import { getFontSize } from "../../util";
|
||||
import { postToIde } from "../../util/ide";
|
||||
import { WebviewIde } from "../../util/webviewIde";
|
||||
import FileIcon from "../FileIcon";
|
||||
import HeaderButtonWithText from "../HeaderButtonWithText";
|
||||
import StyledMarkdownPreview from "./StyledMarkdownPreview";
|
||||
|
|
@ -90,7 +90,7 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
|||
.split("(")[1]
|
||||
.split(")")[0]
|
||||
.split("-");
|
||||
new ExtensionIde().showLines(
|
||||
new WebviewIde().showLines(
|
||||
props.item.description,
|
||||
parseInt(lines[0]) - 1,
|
||||
parseInt(lines[1]) - 1
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import {
|
|||
MessageContent,
|
||||
SlashCommandDescription,
|
||||
} from "core";
|
||||
import { ideStreamRequest, llmStreamChat } from "core/ide/messaging";
|
||||
import { constructMessages } from "core/llm/constructMessages";
|
||||
import { stripImages } from "core/llm/countTokens";
|
||||
import { usePostHog } from "posthog-js/react";
|
||||
|
|
@ -26,7 +25,7 @@ import {
|
|||
streamUpdate,
|
||||
} from "../redux/slices/stateSlice";
|
||||
import { RootStore } from "../redux/store";
|
||||
import { errorPopup } from "../util/ide";
|
||||
import { errorPopup, ideStreamRequest, llmStreamChat } from "../util/ide";
|
||||
|
||||
function useChatHandler(dispatch: Dispatch) {
|
||||
const posthog = usePostHog();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
import { Dispatch } from "@reduxjs/toolkit";
|
||||
import { PersistedSessionInfo, SessionInfo } from "core";
|
||||
import { ideRequest, llmStreamChat } from "core/ide/messaging";
|
||||
|
||||
import { llmCanGenerateInParallel } from "core/llm/autodetect";
|
||||
import { stripImages } from "core/llm/countTokens";
|
||||
import { useSelector } from "react-redux";
|
||||
import { defaultModelSelector } from "../redux/selectors/modelSelectors";
|
||||
import { newSession } from "../redux/slices/stateSlice";
|
||||
import { RootStore } from "../redux/store";
|
||||
import { ideRequest } from "../util/ide";
|
||||
|
||||
function truncateText(text: string, maxLength: number) {
|
||||
if (text.length > maxLength) {
|
||||
|
|
@ -23,7 +24,7 @@ function useHistory(dispatch: Dispatch) {
|
|||
);
|
||||
|
||||
async function getHistory(): Promise<SessionInfo[]> {
|
||||
return await ideRequest("history", {});
|
||||
return await ideRequest("history", undefined);
|
||||
}
|
||||
|
||||
async function saveSession() {
|
||||
|
|
@ -46,24 +47,23 @@ function useHistory(dispatch: Dispatch) {
|
|||
!disableSessionTitles &&
|
||||
llmCanGenerateInParallel(defaultModel.provider, defaultModel.model)
|
||||
) {
|
||||
let fullContent = "";
|
||||
for await (const { content } of llmStreamChat(
|
||||
defaultModel.title,
|
||||
undefined,
|
||||
[
|
||||
...stateCopy.history.map((item) => item.message),
|
||||
{
|
||||
role: "user",
|
||||
content:
|
||||
"Give a maximum 40 character title to describe this conversation so far. The title should help me recall the conversation if I look for it later. DO NOT PUT QUOTES AROUND THE TITLE",
|
||||
},
|
||||
],
|
||||
{ maxTokens: 20 }
|
||||
)) {
|
||||
fullContent += content;
|
||||
}
|
||||
|
||||
title = stripImages(fullContent);
|
||||
// let fullContent = "";
|
||||
// for await (const { content } of llmStreamChat(
|
||||
// defaultModel.title,
|
||||
// undefined,
|
||||
// [
|
||||
// ...stateCopy.history.map((item) => item.message),
|
||||
// {
|
||||
// role: "user",
|
||||
// content:
|
||||
// "Give a maximum 40 character title to describe this conversation so far. The title should help me recall the conversation if I look for it later. DO NOT PUT QUOTES AROUND THE TITLE",
|
||||
// },
|
||||
// ],
|
||||
// { maxTokens: 20 }
|
||||
// )) {
|
||||
// fullContent += content;
|
||||
// }
|
||||
// title = stripImages(fullContent);
|
||||
}
|
||||
|
||||
const sessionInfo: PersistedSessionInfo = {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { useEffect } from 'react';
|
||||
import { useNavigate, useLocation } from 'react-router-dom';
|
||||
import { ReverseWebviewProtocol } from "core/web/webviewProtocol";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { useWebviewListener } from "./useWebviewListener";
|
||||
|
||||
const openGUITypes = [
|
||||
const openGUITypes: (keyof ReverseWebviewProtocol)[] = [
|
||||
"highlightedCode",
|
||||
"newSessionWithPrompt",
|
||||
"focusContinueInput",
|
||||
|
|
@ -13,30 +15,36 @@ export const useNavigationListener = () => {
|
|||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => {
|
||||
if(openGUITypes.includes(e.data.type)) {
|
||||
|
||||
for (const messageType of openGUITypes) {
|
||||
useWebviewListener(
|
||||
messageType,
|
||||
async (data) => {
|
||||
navigate("/");
|
||||
setTimeout(() => {
|
||||
window.postMessage(e.data, "*");
|
||||
window.postMessage(
|
||||
{
|
||||
messageType,
|
||||
data,
|
||||
messageId: uuidv4(),
|
||||
},
|
||||
"*"
|
||||
);
|
||||
}, 200);
|
||||
},
|
||||
[navigate]
|
||||
);
|
||||
}
|
||||
|
||||
useWebviewListener(
|
||||
"viewHistory",
|
||||
async () => {
|
||||
// Toggle the history page / main page
|
||||
if (location.pathname === "/history") {
|
||||
navigate("/");
|
||||
} else {
|
||||
navigate("/history");
|
||||
}
|
||||
|
||||
if(e.data.type === "viewHistory") {
|
||||
// Toggle the history page / main page
|
||||
if (location.pathname === "/history") {
|
||||
navigate("/");
|
||||
} else {
|
||||
navigate("/history");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("message", listener);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("message", listener);
|
||||
};
|
||||
}, [navigate]);
|
||||
}
|
||||
},
|
||||
[location, navigate]
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
import { Dispatch } from "@reduxjs/toolkit";
|
||||
import { useEffect } from "react";
|
||||
import { setServerStatusMessage } from "../redux/slices/miscSlice";
|
||||
import { isJetBrains, postToIde } from "../util/ide";
|
||||
|
||||
import { ideRequest } from "core/ide/messaging";
|
||||
import { useSelector } from "react-redux";
|
||||
import { VSC_THEME_COLOR_VARS } from "../components";
|
||||
import { setVscMachineId } from "../redux/slices/configSlice";
|
||||
|
|
@ -13,11 +9,13 @@ import {
|
|||
setInactive,
|
||||
} from "../redux/slices/stateSlice";
|
||||
import { RootStore } from "../redux/store";
|
||||
import { ideRequest, isJetBrains } from "../util/ide";
|
||||
import useChatHandler from "./useChatHandler";
|
||||
import { useWebviewListener } from "./useWebviewListener";
|
||||
|
||||
function useSetup(dispatch: Dispatch<any>) {
|
||||
const loadConfig = async () => {
|
||||
const config = await ideRequest("getSerializedConfig", {});
|
||||
const config = await ideRequest("getSerializedConfig", undefined);
|
||||
dispatch(setConfig(config));
|
||||
};
|
||||
|
||||
|
|
@ -31,7 +29,15 @@ function useSetup(dispatch: Dispatch<any>) {
|
|||
dispatch(setInactive());
|
||||
|
||||
// Tell JetBrains the webview is ready
|
||||
postToIde("onLoad", {});
|
||||
ideRequest("onLoad", undefined).then((msg) => {
|
||||
(window as any).windowId = msg.windowId;
|
||||
(window as any).serverUrl = msg.serverUrl;
|
||||
(window as any).workspacePaths = msg.workspacePaths;
|
||||
(window as any).vscMachineId = msg.vscMachineId;
|
||||
(window as any).vscMediaUrl = msg.vscMediaUrl;
|
||||
dispatch(setVscMachineId(msg.vscMachineId));
|
||||
// dispatch(setVscMediaUrl(msg.vscMediaUrl));
|
||||
});
|
||||
}, []);
|
||||
|
||||
const { streamResponse } = useChatHandler(dispatch);
|
||||
|
|
@ -41,47 +47,34 @@ function useSetup(dispatch: Dispatch<any>) {
|
|||
);
|
||||
|
||||
// IDE event listeners
|
||||
useEffect(() => {
|
||||
const eventListener = (event: any) => {
|
||||
switch (event.data.type) {
|
||||
case "onLoad":
|
||||
(window as any).windowId = event.data.windowId;
|
||||
(window as any).serverUrl = event.data.serverUrl;
|
||||
(window as any).workspacePaths = event.data.workspacePaths;
|
||||
(window as any).vscMachineId = event.data.vscMachineId;
|
||||
(window as any).vscMediaUrl = event.data.vscMediaUrl;
|
||||
dispatch(setVscMachineId(event.data.vscMachineId));
|
||||
// dispatch(setVscMediaUrl(event.data.vscMediaUrl));
|
||||
useWebviewListener("setInactive", async () => {
|
||||
dispatch(setInactive());
|
||||
});
|
||||
|
||||
break;
|
||||
case "serverStatus":
|
||||
dispatch(setServerStatusMessage(event.data.message));
|
||||
break;
|
||||
case "setInactive":
|
||||
dispatch(setInactive());
|
||||
break;
|
||||
case "configUpdate":
|
||||
loadConfig();
|
||||
break;
|
||||
case "submitMessage":
|
||||
streamResponse(event.data.message);
|
||||
break;
|
||||
case "addContextItem":
|
||||
dispatch(
|
||||
addContextItemsAtIndex({
|
||||
index: event.data.message.historyIndex,
|
||||
contextItems: [event.data.message.item],
|
||||
})
|
||||
);
|
||||
break;
|
||||
case "getDefaultModelTitle":
|
||||
postToIde("getDefaultModelTitle", { defaultModelTitle });
|
||||
break;
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", eventListener);
|
||||
return () => window.removeEventListener("message", eventListener);
|
||||
}, [defaultModelTitle]);
|
||||
useWebviewListener("configUpdate", async () => {
|
||||
loadConfig();
|
||||
});
|
||||
|
||||
useWebviewListener("submitMessage", async (data) => {
|
||||
streamResponse(data.message);
|
||||
});
|
||||
|
||||
useWebviewListener("addContextItem", async (data) => {
|
||||
dispatch(
|
||||
addContextItemsAtIndex({
|
||||
index: data.historyIndex,
|
||||
contextItems: [data.item],
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
useWebviewListener(
|
||||
"getDefaultModelTitle",
|
||||
async () => {
|
||||
return defaultModelTitle;
|
||||
},
|
||||
[defaultModelTitle]
|
||||
);
|
||||
|
||||
// Save theme colors to local storage for immediate loading in JetBrains
|
||||
useEffect(() => {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import { ContextSubmenuItem } from "core";
|
||||
import { ExtensionIde } from "core/ide";
|
||||
import { ideRequest } from "core/ide/messaging";
|
||||
import { getBasename, getLastNPathParts } from "core/util";
|
||||
import MiniSearch, { SearchResult } from "minisearch";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useSelector } from "react-redux";
|
||||
import { selectContextProviderDescriptions } from "../redux/selectors";
|
||||
import { ideRequest } from "../util/ide";
|
||||
import { WebviewIde } from "../util/webviewIde";
|
||||
import { useWebviewListener } from "./useWebviewListener";
|
||||
|
||||
const MINISEARCH_OPTIONS = {
|
||||
prefix: true,
|
||||
|
|
@ -29,7 +30,7 @@ function useSubmenuContextProviders() {
|
|||
const [loaded, setLoaded] = useState(false);
|
||||
|
||||
async function getOpenFileItems() {
|
||||
const openFiles = await new ExtensionIde().getOpenFiles();
|
||||
const openFiles = await new WebviewIde().getOpenFiles();
|
||||
return openFiles.map((file) => {
|
||||
return {
|
||||
id: file,
|
||||
|
|
@ -40,18 +41,9 @@ function useSubmenuContextProviders() {
|
|||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (e) => {
|
||||
if (e.data.type !== "refreshSubmenuItems") {
|
||||
return;
|
||||
}
|
||||
setLoaded(false);
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
return () => {
|
||||
window.removeEventListener("message", handler);
|
||||
};
|
||||
}, []);
|
||||
useWebviewListener("refreshSubmenuItems", async (data) => {
|
||||
setLoaded(true);
|
||||
});
|
||||
|
||||
function addItem(providerTitle: string, item: ContextSubmenuItem) {
|
||||
if (!minisearches[providerTitle]) {
|
||||
|
|
@ -82,7 +74,7 @@ function useSubmenuContextProviders() {
|
|||
fields: ["title"],
|
||||
storeFields: ["id", "title", "description"],
|
||||
});
|
||||
const { items } = await ideRequest("loadSubmenuItems", {
|
||||
const items = await ideRequest("loadSubmenuItems", {
|
||||
title: description.title,
|
||||
});
|
||||
minisearch.addAll(items);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useWebviewListener } from "./useWebviewListener";
|
||||
|
||||
const hljsToTextMate = {
|
||||
".hljs-comment": ["comment"],
|
||||
|
|
@ -81,18 +82,10 @@ export function useVscTheme() {
|
|||
constructTheme((window as any).fullColorTheme || {})
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => {
|
||||
if (e.data.type === "setTheme") {
|
||||
(window as any).fullColorTheme = e.data.theme;
|
||||
setTheme(constructTheme(e.data.theme));
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", listener);
|
||||
return () => {
|
||||
window.removeEventListener("message", listener);
|
||||
};
|
||||
}, []);
|
||||
useWebviewListener("setTheme", async (data) => {
|
||||
(window as any).fullColorTheme = data.theme;
|
||||
setTheme(constructTheme(data.theme));
|
||||
});
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
|
|
|||
25
gui/src/hooks/useWebviewListener.ts
Normal file
25
gui/src/hooks/useWebviewListener.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { Message } from "core/util/messenger";
|
||||
import { ReverseWebviewProtocol } from "core/web/webviewProtocol";
|
||||
import { useEffect } from "react";
|
||||
import { respondToIde } from "../util/ide";
|
||||
|
||||
export function useWebviewListener<T extends keyof ReverseWebviewProtocol>(
|
||||
messageType: T,
|
||||
handler: (
|
||||
data: ReverseWebviewProtocol[T][0]
|
||||
) => Promise<ReverseWebviewProtocol[T][1]>,
|
||||
dependencies?: any[]
|
||||
) {
|
||||
useEffect(() => {
|
||||
const listener = async (event: { data: Message }) => {
|
||||
if (event.data.messageType === messageType) {
|
||||
const result = await handler(event.data.data);
|
||||
respondToIde(messageType, result, event.data.messageId);
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", listener);
|
||||
return () => {
|
||||
window.removeEventListener("message", listener);
|
||||
};
|
||||
}, dependencies ?? []);
|
||||
}
|
||||
|
|
@ -30,6 +30,7 @@ import TimelineItem from "../components/gui/TimelineItem";
|
|||
import ContinueInputBox from "../components/mainInput/ContinueInputBox";
|
||||
import useChatHandler from "../hooks/useChatHandler";
|
||||
import useHistory from "../hooks/useHistory";
|
||||
import { useWebviewListener } from "../hooks/useWebviewListener";
|
||||
import { defaultModelSelector } from "../redux/selectors/modelSelectors";
|
||||
import { newSession, setInactive } from "../redux/slices/stateSlice";
|
||||
import {
|
||||
|
|
@ -348,18 +349,14 @@ function GUI(props: GUIProps) {
|
|||
|
||||
const { saveSession } = useHistory(dispatch);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (event: any) => {
|
||||
if (event.data.type === "newSession") {
|
||||
saveSession();
|
||||
mainTextInputRef.current?.focus?.();
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
return () => {
|
||||
window.removeEventListener("message", handler);
|
||||
};
|
||||
}, [saveSession]);
|
||||
useWebviewListener(
|
||||
"newSession",
|
||||
async () => {
|
||||
saveSession();
|
||||
mainTextInputRef.current?.focus?.();
|
||||
},
|
||||
[saveSession]
|
||||
);
|
||||
|
||||
const isLastUserInput = useCallback(
|
||||
(index: number): boolean => {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { ArrowLeftIcon } from "@heroicons/react/24/outline";
|
||||
import { postToIde } from "core/ide/messaging";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import styled from "styled-components";
|
||||
import { Button, lightGray, vscBackground, vscForeground } from "../components";
|
||||
import KeyboardShortcutsDialog from "../components/dialogs/KeyboardShortcuts";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
|
||||
|
||||
import { postToIde } from "../util/ide";
|
||||
|
||||
const IconDiv = styled.div<{ backgroundColor?: string }>`
|
||||
display: flex;
|
||||
|
|
@ -56,7 +54,7 @@ function HelpPage() {
|
|||
<h3 className="text-lg font-bold m-2 inline-block">Help Center</h3>
|
||||
<TutorialButton
|
||||
onClick={() => {
|
||||
postToIde("showTutorial", {});
|
||||
postToIde("showTutorial", undefined);
|
||||
}}
|
||||
>
|
||||
Open tutorial
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ import styled from "styled-components";
|
|||
import { lightGray, vscBackground, vscInputBackground } from "../components";
|
||||
import HeaderButtonWithText from "../components/HeaderButtonWithText";
|
||||
import useHistory from "../hooks/useHistory";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
import { newSession } from "../redux/slices/stateSlice";
|
||||
import { getFontSize } from "../util";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
|
||||
const Tr = styled.tr`
|
||||
&:hover {
|
||||
|
|
@ -128,7 +128,7 @@ function lastPartOfPath(path: string): string {
|
|||
function History() {
|
||||
useNavigationListener();
|
||||
const navigate = useNavigate();
|
||||
|
||||
|
||||
const [sessions, setSessions] = useState<SessionInfo[]>([]);
|
||||
const [filteredAndSortedSessions, setFilteredAndSortedSessions] = useState<
|
||||
SessionInfo[]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { ArrowLeftIcon } from "@heroicons/react/24/outline";
|
||||
import { postToIde } from "core/ide/messaging";
|
||||
import _ from "lodash";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { FormProvider, useForm } from "react-hook-form";
|
||||
|
|
@ -14,15 +13,15 @@ import {
|
|||
} from "../components";
|
||||
import StyledMarkdownPreview from "../components/markdown/StyledMarkdownPreview";
|
||||
import ModelCard from "../components/modelSelection/ModelCard";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
import { setDefaultModel } from "../redux/slices/stateSlice";
|
||||
import { addModel } from "../util/ide";
|
||||
import { postToIde } from "../util/ide";
|
||||
import {
|
||||
MODEL_PROVIDER_TAG_COLORS,
|
||||
ModelInfo,
|
||||
PROVIDER_INFO,
|
||||
updatedObj,
|
||||
} from "../util/modelData";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
|
||||
const GridDiv = styled.div`
|
||||
display: grid;
|
||||
|
|
@ -247,7 +246,7 @@ function ModelConfig() {
|
|||
...formParams,
|
||||
provider: modelInfo.provider,
|
||||
};
|
||||
addModel(model);
|
||||
postToIde("addModel", model);
|
||||
dispatch(setDefaultModel(model.title));
|
||||
navigate("/");
|
||||
}}
|
||||
|
|
@ -265,7 +264,7 @@ function ModelConfig() {
|
|||
<CustomModelButton
|
||||
disabled={false}
|
||||
onClick={(e) => {
|
||||
postToIde("openConfigJson", {});
|
||||
postToIde("openConfigJson", undefined);
|
||||
}}
|
||||
>
|
||||
<h3 className="text-center my-2">Open config.json</h3>
|
||||
|
|
|
|||
|
|
@ -1,19 +1,17 @@
|
|||
import { ArrowLeftIcon } from "@heroicons/react/24/outline";
|
||||
import _ from "lodash";
|
||||
import React from "react";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import styled from "styled-components";
|
||||
import { defaultBorderRadius, lightGray, vscBackground } from "../components";
|
||||
import ModelCard from "../components/modelSelection/ModelCard";
|
||||
|
||||
import { postToIde } from "core/ide/messaging";
|
||||
import { useDispatch } from "react-redux";
|
||||
import Toggle from "../components/modelSelection/Toggle";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
import { setDefaultModel } from "../redux/slices/stateSlice";
|
||||
import { addModel } from "../util/ide";
|
||||
import { postToIde } from "../util/ide";
|
||||
import { MODEL_INFO, PROVIDER_INFO } from "../util/modelData";
|
||||
import { CustomModelButton } from "./modelconfig";
|
||||
import { useNavigationListener } from "../hooks/useNavigationListener";
|
||||
|
||||
const IntroDiv = styled.div`
|
||||
padding: 8px 12px;
|
||||
|
|
@ -75,10 +73,8 @@ function Models() {
|
|||
<li>a model (the LLM being run, e.g. GPT-4, CodeLlama).</li>
|
||||
</ul>
|
||||
To read more about the options, check out our{" "}
|
||||
<a href="https://continue.dev/docs/model-setup/overview">
|
||||
overview
|
||||
</a>{" "}
|
||||
in the docs.
|
||||
<a href="https://continue.dev/docs/model-setup/overview">overview</a> in
|
||||
the docs.
|
||||
</IntroDiv>
|
||||
{providersSelected ? (
|
||||
<GridDiv>
|
||||
|
|
@ -121,7 +117,7 @@ function Models() {
|
|||
),
|
||||
provider: PROVIDER_INFO[selectedProvider].provider,
|
||||
};
|
||||
addModel(model);
|
||||
postToIde("addModel", model);
|
||||
dispatch(setDefaultModel(model.title));
|
||||
navigate("/");
|
||||
}}
|
||||
|
|
@ -138,7 +134,7 @@ function Models() {
|
|||
<CustomModelButton
|
||||
disabled={false}
|
||||
onClick={(e) => {
|
||||
postToIde("openConfigJson", {});
|
||||
postToIde("openConfigJson", undefined);
|
||||
}}
|
||||
>
|
||||
<h3 className="text-center my-2">Open config.json</h3>
|
||||
|
|
|
|||
|
|
@ -27,10 +27,7 @@ function EditorFrame(props: EditorFrameProps) {
|
|||
};
|
||||
window.addEventListener("message", eventListener);
|
||||
|
||||
postToIde("readRangeInFile", {
|
||||
filepath: props.filename,
|
||||
messageId: messageId,
|
||||
});
|
||||
postToIde("readFile", { filepath: props.filename });
|
||||
|
||||
return () => window.removeEventListener("message", eventListener);
|
||||
}, []);
|
||||
|
|
|
|||
|
|
@ -1,39 +1,59 @@
|
|||
import { ChatMessage, LLMFullCompletionOptions, LLMReturnValue } from "core";
|
||||
import { Message } from "core/util/messenger";
|
||||
import {
|
||||
ReverseWebviewProtocol,
|
||||
WebviewProtocol,
|
||||
} from "core/web/webviewProtocol";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import "vscode-webview";
|
||||
interface vscode {
|
||||
postMessage(message: any): vscode;
|
||||
}
|
||||
|
||||
declare const vscode: any;
|
||||
|
||||
function _postToIde(type: string, data: any) {
|
||||
function _postToIde(messageType: string, data: any, messageId?: string) {
|
||||
if (typeof vscode === "undefined") {
|
||||
if (localStorage.getItem("ide") === "jetbrains") {
|
||||
if ((window as any).postIntellijMessage === undefined) {
|
||||
console.log(
|
||||
"Unable to send message: postIntellijMessage is undefined. ",
|
||||
type,
|
||||
messageType,
|
||||
data
|
||||
);
|
||||
throw new Error("postIntellijMessage is undefined");
|
||||
}
|
||||
(window as any).postIntellijMessage?.(type, data);
|
||||
(window as any).postIntellijMessage?.(messageType, data);
|
||||
return;
|
||||
} else {
|
||||
console.log("Unable to send message: vscode is undefined. ", type, data);
|
||||
console.log(
|
||||
"Unable to send message: vscode is undefined. ",
|
||||
messageType,
|
||||
data
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
vscode.postMessage({
|
||||
type,
|
||||
...data,
|
||||
});
|
||||
const msg: Message = {
|
||||
messageId: messageId ?? uuidv4(),
|
||||
messageType,
|
||||
data,
|
||||
};
|
||||
vscode.postMessage(msg);
|
||||
}
|
||||
|
||||
export function postToIde(type: string, data: any, attempt: number = 0) {
|
||||
export function postToIde<T extends keyof WebviewProtocol>(
|
||||
messageType: T,
|
||||
data: WebviewProtocol[T][0],
|
||||
attempt: number = 0
|
||||
) {
|
||||
try {
|
||||
_postToIde(type, data);
|
||||
_postToIde(messageType, data);
|
||||
} catch (error) {
|
||||
if (attempt < 5) {
|
||||
console.log(`Attempt ${attempt} failed. Retrying...`);
|
||||
setTimeout(
|
||||
() => postToIde(type, data, attempt + 1),
|
||||
() => postToIde(messageType, data, attempt + 1),
|
||||
Math.pow(2, attempt) * 1000
|
||||
);
|
||||
} else {
|
||||
|
|
@ -42,17 +62,104 @@ export function postToIde(type: string, data: any, attempt: number = 0) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function ideRequest(type: string, data: any): Promise<any> {
|
||||
export function respondToIde<T extends keyof ReverseWebviewProtocol>(
|
||||
messageType: T,
|
||||
data: ReverseWebviewProtocol[T][1],
|
||||
messageId: string
|
||||
) {
|
||||
_postToIde(messageType, data, messageId);
|
||||
}
|
||||
|
||||
export async function ideRequest<T extends keyof WebviewProtocol>(
|
||||
messageType: T,
|
||||
data: WebviewProtocol[T][0]
|
||||
): Promise<WebviewProtocol[T][1]> {
|
||||
const messageId = uuidv4();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const handler = (event: any) => {
|
||||
if (event.data.type === type) {
|
||||
if (event.data.messageId === messageId) {
|
||||
window.removeEventListener("message", handler);
|
||||
resolve(event.data);
|
||||
resolve(event.data.data);
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
postToIde(type, data);
|
||||
|
||||
postToIde(messageType, data);
|
||||
}) as any;
|
||||
}
|
||||
|
||||
export async function* ideStreamRequest<T extends keyof WebviewProtocol>(
|
||||
messageType: T,
|
||||
data: WebviewProtocol[T][0],
|
||||
cancelToken?: AbortSignal
|
||||
): WebviewProtocol[T][1] {
|
||||
const messageId = uuidv4();
|
||||
|
||||
postToIde(messageType, data);
|
||||
|
||||
let buffer = "";
|
||||
let index = 0;
|
||||
let done = false;
|
||||
let returnVal = undefined;
|
||||
|
||||
const handler = (event: { data: Message }) => {
|
||||
if (event.data.messageId === messageId) {
|
||||
if (event.data.data.done) {
|
||||
window.removeEventListener("message", handler);
|
||||
done = true;
|
||||
returnVal = event.data.data;
|
||||
} else {
|
||||
buffer += event.data.data.content;
|
||||
}
|
||||
}
|
||||
};
|
||||
window.addEventListener("message", handler);
|
||||
|
||||
cancelToken?.addEventListener("abort", () => {
|
||||
postToIde("abort", messageId);
|
||||
});
|
||||
|
||||
while (!done) {
|
||||
if (buffer.length > index) {
|
||||
const chunk = buffer.slice(index);
|
||||
index = buffer.length;
|
||||
yield chunk;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
|
||||
if (buffer.length > index) {
|
||||
const chunk = buffer.slice(index);
|
||||
index = buffer.length;
|
||||
yield chunk;
|
||||
}
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
export async function* llmStreamChat(
|
||||
modelTitle: string,
|
||||
cancelToken: AbortSignal | undefined,
|
||||
messages: ChatMessage[],
|
||||
options: LLMFullCompletionOptions = {}
|
||||
): AsyncGenerator<ChatMessage, LLMReturnValue> {
|
||||
const gen = ideStreamRequest(
|
||||
"llmStreamChat",
|
||||
{
|
||||
messages,
|
||||
title: modelTitle,
|
||||
completionOptions: options,
|
||||
},
|
||||
cancelToken
|
||||
);
|
||||
|
||||
let next = await gen.next();
|
||||
while (!next.done) {
|
||||
yield { role: "user", content: next.value };
|
||||
next = await gen.next();
|
||||
}
|
||||
return { prompt: next.value?.prompt, completion: next.value?.completion };
|
||||
}
|
||||
|
||||
export function errorPopup(message: string) {
|
||||
|
|
@ -63,10 +170,6 @@ export function logDevData(tableName: string, data: any) {
|
|||
postToIde("logDevData", { tableName, data });
|
||||
}
|
||||
|
||||
export function addModel(model: any) {
|
||||
postToIde("addModel", { model });
|
||||
}
|
||||
|
||||
export function deleteModel(title: any) {
|
||||
postToIde("deleteModel", { title });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +1,45 @@
|
|||
import { ContinueRcJson, Problem, Range } from "..";
|
||||
import { IDE } from "../index";
|
||||
|
||||
import { ideRequest } from "./message";
|
||||
async function r(messageType: string, options: any = {}) {
|
||||
return await ideRequest(messageType, options);
|
||||
import { ContinueRcJson, IDE, Problem, Range } from "core";
|
||||
import { WebviewProtocol } from "core/web/webviewProtocol";
|
||||
import { ideRequest } from "./ide";
|
||||
function r<T extends keyof WebviewProtocol>(
|
||||
messageType: T,
|
||||
data: WebviewProtocol[T][0]
|
||||
): Promise<WebviewProtocol[T][1]> {
|
||||
return ideRequest(messageType, data);
|
||||
}
|
||||
export class WebviewIde implements IDE {
|
||||
readRangeInFile(filepath: string, range: Range): Promise<string> {
|
||||
return r("readRangeInFile", { filepath, range });
|
||||
}
|
||||
getStats(directory: string): Promise<{ [path: string]: number }> {
|
||||
return r("getStats", { directory });
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
isTelemetryEnabled(): Promise<boolean> {
|
||||
return r("isTelemetryEnabled");
|
||||
return r("isTelemetryEnabled", undefined);
|
||||
}
|
||||
|
||||
getUniqueId(): Promise<string> {
|
||||
return r("getUniqueId");
|
||||
return r("getUniqueId", undefined);
|
||||
}
|
||||
|
||||
getWorkspaceConfigs(): Promise<ContinueRcJson[]> {
|
||||
return r("getWorkspaceConfigs");
|
||||
return r("getWorkspaceConfigs", undefined);
|
||||
}
|
||||
|
||||
async getDiff() {
|
||||
return await r("getDiff");
|
||||
return await r("getDiff", undefined);
|
||||
}
|
||||
|
||||
async getTerminalContents() {
|
||||
return await r("getTerminalContents");
|
||||
return await r("getTerminalContents", undefined);
|
||||
}
|
||||
|
||||
async listWorkspaceContents(directory?: string): Promise<string[]> {
|
||||
return await r("listWorkspaceContents");
|
||||
return await r("listWorkspaceContents", undefined);
|
||||
}
|
||||
|
||||
async getWorkspaceDirs(): Promise<string[]> {
|
||||
return await r("getWorkspaceDirs");
|
||||
return await r("getWorkspaceDirs", undefined);
|
||||
}
|
||||
|
||||
async showLines(
|
||||
|
|
@ -49,7 +51,7 @@ export class WebviewIde implements IDE {
|
|||
}
|
||||
|
||||
async listFolders(): Promise<string[]> {
|
||||
return await r("listFolders");
|
||||
return await r("listFolders", undefined);
|
||||
}
|
||||
|
||||
_continueDir: string | null = null;
|
||||
|
|
@ -58,7 +60,7 @@ export class WebviewIde implements IDE {
|
|||
if (this._continueDir) {
|
||||
return this._continueDir;
|
||||
}
|
||||
const dir = await r("getContinueDir");
|
||||
const dir = await r("getContinueDir", undefined);
|
||||
this._continueDir = dir;
|
||||
return dir;
|
||||
}
|
||||
|
|
@ -94,11 +96,11 @@ export class WebviewIde implements IDE {
|
|||
}
|
||||
|
||||
getOpenFiles(): Promise<string[]> {
|
||||
return r("getOpenFiles");
|
||||
return r("getOpenFiles", undefined);
|
||||
}
|
||||
|
||||
getPinnedFiles(): Promise<string[]> {
|
||||
return r("getPinnedFiles");
|
||||
return r("getPinnedFiles", undefined);
|
||||
}
|
||||
|
||||
getSearchResults(query: string): Promise<string> {
|
||||
Loading…
Add table
Reference in a new issue