@@ -61,6 +61,7 @@ export class ChatProvider implements vscode.WebviewViewProvider {
6161 private _view ?: vscode . WebviewView ;
6262 private _extensionUri : vscode . Uri ;
6363 private _assetsUri : vscode . Uri ;
64+ private _panel ?: vscode . WebviewPanel ; // Track the current panel
6465
6566 public recipes : Recipe [ ] = [ ] ; // this data is used in the "Ask Sourcery" command prompt, so can't be removed
6667
@@ -131,6 +132,77 @@ export class ChatProvider implements vscode.WebviewViewProvider {
131132 ) ;
132133 }
133134
135+ /**
136+ * Creates a new webview panel with chat functionality or reveals an existing one
137+ */
138+ public async createOrShowWebviewPanel ( ) : Promise < vscode . WebviewPanel > {
139+ if ( this . _panel ) {
140+ // If panel already exists, reveal it
141+ this . _panel . reveal ( ) ;
142+ return this . _panel ;
143+ }
144+
145+ // Create a new panel
146+ const panel = vscode . window . createWebviewPanel (
147+ "sourceryChatPanel" , // Unique identifier
148+ "Analytics" , // Title
149+ vscode . ViewColumn . Active ,
150+ {
151+ enableScripts : true ,
152+ retainContextWhenHidden : true ,
153+ localResourceRoots : [ this . _extensionUri , this . _assetsUri ] ,
154+ }
155+ ) ;
156+
157+ // Set the panel's HTML content
158+ panel . webview . html = await this . _getHtmlForWebview ( panel . webview ) ;
159+
160+ // Set up message handling
161+ panel . webview . onDidReceiveMessage (
162+ async ( { message, ...rest } : { message : OutboundMessage } ) => {
163+ switch ( message . target ) {
164+ case "languageServer" :
165+ vscode . commands . executeCommand ( "sourcery.coding_assistant" , {
166+ message,
167+ ...rest ,
168+ } ) ;
169+ break ;
170+ case "extension" :
171+ switch ( message . request ) {
172+ case "openLink" :
173+ this . handleOpenLinkRequest ( message ) ;
174+ break ;
175+ case "copyToClipboard" : {
176+ this . handleCopyToClipboardRequest ( message ) ;
177+ break ;
178+ }
179+ case "insertAtCursor" : {
180+ this . handleInsertAtCursorRequest ( message ) ;
181+ break ;
182+ }
183+ case "updateConfiguration" : {
184+ await vscode . workspace
185+ . getConfiguration ( )
186+ . update (
187+ message . section ,
188+ message . value ,
189+ message . configurationTarget
190+ ) ;
191+ }
192+ }
193+ }
194+ }
195+ ) ;
196+
197+ // Track this panel and handle disposal
198+ this . _panel = panel ;
199+ panel . onDidDispose ( ( ) => {
200+ this . _panel = undefined ;
201+ } ) ;
202+
203+ return panel ;
204+ }
205+
134206 public postCommand ( command : any ) {
135207 // Intercept the addRecipes command
136208 // The "Ask Sourcery" user interface needs to have the recipes available
@@ -142,7 +214,22 @@ export class ChatProvider implements vscode.WebviewViewProvider {
142214 break ;
143215 }
144216 }
145- this . _view . webview . postMessage ( command ) ;
217+
218+ // Send to the sidebar view if it exists
219+ if ( this . _view ?. webview ) {
220+ this . _view . webview . postMessage ( command ) ;
221+ }
222+
223+ // Also send to the panel if it exists
224+ if ( this . _panel ?. webview ) {
225+ switch ( command . command ) {
226+ case "context/update" : {
227+ command [ "updates" ] [ "isAnalyticsPanel" ] = true ;
228+ break ;
229+ }
230+ }
231+ this . _panel . webview . postMessage ( command ) ;
232+ }
146233 }
147234
148235 private handleOpenLinkRequest ( {
0 commit comments