forked from continuedev/continue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContinueGUIWebviewViewProvider.ts
239 lines (210 loc) · 7.74 KB
/
ContinueGUIWebviewViewProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import type { FileEdit } from "core";
import { ConfigHandler } from "core/config/ConfigHandler";
import * as vscode from "vscode";
import { getTheme } from "./util/getTheme";
import { getExtensionVersion } from "./util/util";
import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode";
import { VsCodeWebviewProtocol } from "./webviewProtocol";
export class ContinueGUIWebviewViewProvider
implements vscode.WebviewViewProvider
{
public static readonly viewType = "continue.continueGUIView";
public webviewProtocol: VsCodeWebviewProtocol;
private updateDebugLogsStatus() {
const settings = vscode.workspace.getConfiguration("continue");
this.enableDebugLogs = settings.get<boolean>("enableDebugLogs", false);
if (this.enableDebugLogs) {
this.outputChannel.show(true);
} else {
this.outputChannel.hide();
}
}
// Show or hide the output channel on enableDebugLogs
private setupDebugLogsListener() {
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('continue.enableDebugLogs')) {
const settings = vscode.workspace.getConfiguration("continue");
const enableDebugLogs = settings.get<boolean>("enableDebugLogs", false);
if (enableDebugLogs) {
this.outputChannel.show(true);
} else {
this.outputChannel.hide();
}
}
});
}
private async handleWebviewMessage(message: any) {
if (message.messageType === "log") {
const settings = vscode.workspace.getConfiguration("continue");
const enableDebugLogs = settings.get<boolean>("enableDebugLogs", false);
if (message.level === "debug" && !enableDebugLogs) {
return; // Skip debug logs if enableDebugLogs is false
}
const timestamp = new Date().toISOString().split(".")[0];
const logMessage = `[${timestamp}] [${message.level.toUpperCase()}] ${message.text}`;
this.outputChannel.appendLine(logMessage);
}
}
resolveWebviewView(
webviewView: vscode.WebviewView,
_context: vscode.WebviewViewResolveContext,
_token: vscode.CancellationToken,
): void | Thenable<void> {
this._webview = webviewView.webview;
this._webview.onDidReceiveMessage((message) =>
this.handleWebviewMessage(message),
);
webviewView.webview.html = this.getSidebarContent(
this.extensionContext,
webviewView,
);
}
private _webview?: vscode.Webview;
private _webviewView?: vscode.WebviewView;
private outputChannel: vscode.OutputChannel;
private enableDebugLogs: boolean;
get isVisible() {
return this._webviewView?.visible;
}
get webview() {
return this._webview;
}
public resetWebviewProtocolWebview(): void {
if (this._webview) {
this.webviewProtocol.webview = this._webview;
} else {
console.warn("no webview found during reset");
}
}
sendMainUserInput(input: string) {
this.webview?.postMessage({
type: "userInput",
input,
});
}
constructor(
private readonly configHandlerPromise: Promise<ConfigHandler>,
private readonly windowId: string,
private readonly extensionContext: vscode.ExtensionContext,
) {
this.outputChannel = vscode.window.createOutputChannel("Continue");
this.enableDebugLogs = false;
this.updateDebugLogsStatus();
this.setupDebugLogsListener();
this.webviewProtocol = new VsCodeWebviewProtocol(
(async () => {
const configHandler = await this.configHandlerPromise;
return configHandler.reloadConfig();
}).bind(this),
);
}
getSidebarContent(
context: vscode.ExtensionContext | undefined,
panel: vscode.WebviewPanel | vscode.WebviewView,
page: string | undefined = undefined,
edits: FileEdit[] | undefined = undefined,
isFullScreen = false,
): string {
const extensionUri = getExtensionUri();
let scriptUri: string;
let styleMainUri: string;
const vscMediaUrl: string = panel.webview
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui"))
.toString();
const inDevelopmentMode =
context?.extensionMode === vscode.ExtensionMode.Development;
if (!inDevelopmentMode) {
scriptUri = panel.webview
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.js"))
.toString();
styleMainUri = panel.webview
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.css"))
.toString();
} else {
scriptUri = "http://localhost:5173/src/main.tsx";
styleMainUri = "http://localhost:5173/src/index.css";
}
panel.webview.options = {
enableScripts: true,
localResourceRoots: [
vscode.Uri.joinPath(extensionUri, "gui"),
vscode.Uri.joinPath(extensionUri, "assets"),
],
enableCommandUris: true,
portMapping: [
{
webviewPort: 65433,
extensionHostPort: 65433,
},
],
};
const nonce = getNonce();
const currentTheme = getTheme();
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration("workbench.colorTheme")) {
// Send new theme to GUI to update embedded Monaco themes
this.webviewProtocol?.request("setTheme", { theme: getTheme() });
}
});
this.webviewProtocol.webview = panel.webview;
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>const vscode = acquireVsCodeApi();</script>
<link href="${styleMainUri}" rel="stylesheet">
<title>Continue</title>
</head>
<body>
<div id="root"></div>
${`<script>
function log(level, ...args) {
const text = args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ');
vscode.postMessage({ messageType: 'log', level, text, messageId: "log" });
}
window.console.log = (...args) => log('log', ...args);
window.console.info = (...args) => log('info', ...args);
window.console.warn = (...args) => log('warn', ...args);
window.console.error = (...args) => log('error', ...args);
window.console.debug = (...args) => log('debug', ...args);
console.debug('Logging initialized');
</script>`}
${
inDevelopmentMode
? `<script type="module">
import RefreshRuntime from "http://localhost:5173/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>`
: ""
}
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
<script>localStorage.setItem("ide", '"vscode"')</script>
<script>localStorage.setItem("extensionVersion", '"${getExtensionVersion()}"')</script>
<script>window.windowId = "${this.windowId}"</script>
<script>window.vscMachineId = "${getUniqueId()}"</script>
<script>window.vscMediaUrl = "${vscMediaUrl}"</script>
<script>window.ide = "vscode"</script>
<script>window.fullColorTheme = ${JSON.stringify(currentTheme)}</script>
<script>window.colorThemeName = "dark-plus"</script>
<script>window.workspacePaths = ${JSON.stringify(
vscode.workspace.workspaceFolders?.map(
(folder) => folder.uri.fsPath,
) || [],
)}</script>
<script>window.isFullScreen = ${isFullScreen}</script>
${
edits
? `<script>window.edits = ${JSON.stringify(edits)}</script>`
: ""
}
${page ? `<script>window.location.pathname = "${page}"</script>` : ""}
</body>
</html>`;
}
}