Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 6.2 KiBRawPermalink
1import { LLMInteractionItem } from "core";
2import { EXTENSION_NAME } from "core/control-plane/env";
3import { LLMLogger } from "core/llm/logger";
4import * as vscode from "vscode";
5
6import { getExtensionUri, getNonce } from "./util/vscode";
7
8interface FromConsoleView {
9 type: "start" | "stop";
10 uuid: string;
11}
12
13// Maximum interactions we retain; when we exceed this, we drop the
14// oldest and also send a message to the view to do the same.
15const MAX_INTERACTIONS = 50;
16
17export class ContinueConsoleWebviewViewProvider
18 implements vscode.WebviewViewProvider
19{
20 public static readonly viewType = "continue.continueConsoleView";
21
22 resolveWebviewView(
23 webviewView: vscode.WebviewView,
24 _context: vscode.WebviewViewResolveContext,
25 _token: vscode.CancellationToken,
26 ): void | Thenable<void> {
27 this._webviewView = webviewView;
28 this._webview = webviewView.webview;
29 this._webviewView.onDidDispose(() => {
30 this._webviewView = undefined;
31 this._webview = undefined;
32 });
33 webviewView.webview.html = this.getSidebarContent(
34 this.extensionContext,
35 webviewView,
36 );
37 this._webview.onDidReceiveMessage((message: FromConsoleView) => {
38 if (message.type === "start") {
39 this._currentUuid = message.uuid;
40 this._webview?.postMessage({
41 type: "init",
42 uuid: this._currentUuid,
43 items: this.getAllItems(),
44 });
45 }
46 });
47 this._webviewView.onDidDispose(() => {
48 this._webview = undefined;
49 this._webviewView = undefined;
50 this._currentUuid = undefined;
51 });
52 }
53
54 private _webview?: vscode.Webview;
55 private _webviewView?: vscode.WebviewView;
56 private _currentUuid?: string;
57 private _currentInteractions = new Map<string, LLMInteractionItem[]>();
58 private _completedInteractions: LLMInteractionItem[][] = [];
59 private _saveLog;
60
61 constructor(
62 private readonly windowId: string,
63 private readonly extensionContext: vscode.ExtensionContext,
64 private readonly llmLogger: LLMLogger,
65 ) {
66 const config = vscode.workspace.getConfiguration(EXTENSION_NAME);
67 this._saveLog = config.get<boolean>("enableConsole");
68
69 vscode.workspace.onDidChangeConfiguration((e) => {
70 if (e.affectsConfiguration(`${EXTENSION_NAME}.enableConsole`)) {
71 const config = vscode.workspace.getConfiguration(EXTENSION_NAME);
72 this._saveLog = config.get<boolean>("enableConsole");
73 if (!this._saveLog) {
74 this.clearLog();
75 }
76 }
77 });
78
79 llmLogger.onLogItem((item) => this.addItem(item));
80 }
81
82 private addItem(item: LLMInteractionItem) {
83 if (!this._saveLog) {
84 return;
85 }
86
87 let iteractionItems = this._currentInteractions.get(item.interactionId);
88 if (iteractionItems === undefined) {
89 iteractionItems = [];
90 this._currentInteractions.set(item.interactionId, iteractionItems);
91 }
92
93 iteractionItems.push(item);
94 switch (item.kind) {
95 case "success":
96 case "cancel":
97 case "error":
98 this._completedInteractions.push(iteractionItems);
99 this._currentInteractions.delete(item.interactionId);
100 break;
101 default:
102 break;
103 }
104
105 if (this._currentUuid) {
106 this._webview?.postMessage({
107 type: "item",
108 uuid: this._currentUuid,
109 item,
110 });
111 }
112
113 while (
114 this._completedInteractions.length > 0 &&
115 this._completedInteractions.length + this._currentInteractions.size >
116 MAX_INTERACTIONS
117 ) {
118 let toRemove = this._completedInteractions.shift();
119 this._webview?.postMessage({
120 type: "remove",
121 uuid: this._currentUuid,
122 interactionId: toRemove![0].interactionId,
123 });
124 }
125 }
126
127 private getAllItems() {
128 let items = this._completedInteractions.flat();
129
130 for (const interactionItems of this._currentInteractions.values()) {
131 items.push(...interactionItems);
132 }
133
134 return items;
135 }
136
137 clearLog() {
138 this._completedInteractions = [];
139 this._currentInteractions = new Map();
140
141 if (this._currentUuid) {
142 this._webview?.postMessage({
143 type: "clear",
144 uuid: this._currentUuid,
145 });
146 }
147 }
148
149 private getSidebarContent(
150 context: vscode.ExtensionContext | undefined,
151 panel: vscode.WebviewPanel | vscode.WebviewView,
152 page: string | undefined = undefined,
153 ): string {
154 const extensionUri = getExtensionUri();
155 let scriptUri: string;
156 let styleMainUri: string;
157
158 const inDevelopmentMode =
159 context?.extensionMode === vscode.ExtensionMode.Development;
160 if (!inDevelopmentMode) {
161 scriptUri = panel.webview
162 .asWebviewUri(
163 vscode.Uri.joinPath(extensionUri, "gui/assets/indexConsole.js"),
164 )
165 .toString();
166 styleMainUri = panel.webview
167 .asWebviewUri(
168 vscode.Uri.joinPath(extensionUri, "gui/assets/indexConsole.css"),
169 )
170 .toString();
171 } else {
172 scriptUri = "http://localhost:5173/src/console.tsx";
173 styleMainUri = "http://localhost:5173/src/indexConsole.css";
174 }
175
176 panel.webview.options = {
177 enableScripts: true,
178 localResourceRoots: [
179 vscode.Uri.joinPath(extensionUri, "gui"),
180 vscode.Uri.joinPath(extensionUri, "assets"),
181 ],
182 enableCommandUris: true,
183 portMapping: [
184 {
185 webviewPort: 65433,
186 extensionHostPort: 65433,
187 },
188 ],
189 };
190
191 const nonce = getNonce();
192
193 return `<!DOCTYPE html>
194 <html lang="en">
195 <head>
196 <meta charset="UTF-8">
197 <meta name="viewport" content="width=device-width, initial-scale=1.0">
198 <script>const vscode = acquireVsCodeApi();</script>
199 <link href="${styleMainUri}" rel="stylesheet">
200
201 <title>Continue</title>
202 </head>
203 <body>
204 <div id="root"></div>
205
206 ${
207 inDevelopmentMode
208 ? `<script type="module">
209 import RefreshRuntime from "http://localhost:5173/@react-refresh"
210 RefreshRuntime.injectIntoGlobalHook(window)
211 window.$RefreshReg$ = () => {}
212 window.$RefreshSig$ = () => (type) => type
213 window.__vite_plugin_react_preamble_installed__ = true
214 </script>`
215 : ""
216 }
217 <script type="module" nonce="${nonce}" src="${scriptUri}"></script>
218 </body>
219 </html>`;
220 }
221}