Skip to content

Commit

Permalink
Chat: register webview (#35)
Browse files Browse the repository at this point in the history
Run client.ResolveWebviewView after client has been initialized.
  • Loading branch information
abeatrix authored Aug 13, 2024
1 parent aec1d77 commit c144cba
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 111 deletions.
3 changes: 0 additions & 3 deletions src/Cody.Core/Agent/InitializeCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
using Cody.Core.Logging;
using Cody.Core.Settings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cody.Core.Agent
Expand Down
18 changes: 13 additions & 5 deletions src/Cody.Core/Agent/NotificationHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Cody.Core.Agent.Protocol;
using EnvDTE80;
using Newtonsoft.Json.Linq;
using System;
using EnvDTE80;
using System.Threading.Tasks;

namespace Cody.Core.Agent
Expand All @@ -19,7 +19,14 @@ public NotificationHandlers()

public IAgentService agentClient;

public void SetAgentClient(IAgentService agentClient) => this.agentClient = agentClient;
private TaskCompletionSource<bool> agentClientReady = new TaskCompletionSource<bool>();


public void SetAgentClient(IAgentService client)
{
this.agentClient = client;
agentClientReady.SetResult(true);
}

// Send a message to the host from webview.
public async Task SendWebviewMessage(string handle, string message)
Expand Down Expand Up @@ -82,14 +89,15 @@ public void RegisterWebview(string handle)
[AgentNotification("webview/registerWebviewViewProvider")]
public async Task RegisterWebviewViewProvider(string viewId, bool retainContextWhenHidden)
{
System.Diagnostics.Debug.WriteLine(viewId, retainContextWhenHidden, "Agent registerWebviewViewProvider");
agentClientReady.Task.Wait();
await agentClient.ResolveWebviewView(new ResolveWebviewViewParams
{
// cody.chat for sidebar view, or cody.editorPanel for editor panel
ViewId = "cody.chat",
ViewId = viewId,
// TODO: Create dynmically when we support editor panel
WebviewHandle = "visual-studio-sidebar",
});
System.Diagnostics.Debug.WriteLine(viewId, retainContextWhenHidden, "Agent registerWebviewViewProvider");
}

[AgentNotification("webview/createWebviewPanel", deserializeToSingleObject: true)]
Expand Down Expand Up @@ -134,7 +142,7 @@ public void PostMessageStringEncoded(string id, string stringEncodedMessage)
[AgentNotification("webview/didDisposeNative")]
public void DidDisposeNative(string handle)
{

}

[AgentNotification("extensionConfiguration/didChange", deserializeToSingleObject: true)]
Expand Down
5 changes: 2 additions & 3 deletions src/Cody.UI/Controls/WebView2Dev.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
d:DesignWidth="800"
>
<Grid>
<Grid Background="MediumPurple">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<wpf:WebView2 Name="webView" Grid.Row="1"
<wpf:WebView2 Name="webView" Grid.Row="1" Loaded="InitWebView2"

/>
</Grid>

</Grid>
</UserControl>
29 changes: 18 additions & 11 deletions src/Cody.UI/Controls/WebView2Dev.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using Cody.Core.Agent;
using Microsoft.Web.WebView2.Core;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Web.WebView2.Core;
using System.IO;
using System.Windows.Input;
using Cody.Core.Agent;

namespace Cody.UI.Controls
{
Expand All @@ -17,15 +17,17 @@ public partial class WebView2Dev : UserControl

private static WebviewController _controller = new WebviewController();

private bool isInitialized = false;

public WebView2Dev()
{
InitializeComponent();
InitializeWebView();
System.Diagnostics.Debug.WriteLine("InitializeComponent", "WebView2Dev");
}

private void InitWebView2(object sender, RoutedEventArgs e)
private async void InitWebView2(object sender, RoutedEventArgs e)
{
InitializeWebView();
await InitializeWebView();
}

public static WebviewController InitializeController(string themeScript)
Expand All @@ -34,14 +36,19 @@ public static WebviewController InitializeController(string themeScript)
return _controller;
}

private async Task<CoreWebView2> InitializeWebView()
private async Task InitializeWebView()
{
var env = await CreateWebView2Environment();
await webView.EnsureCoreWebView2Async(env);
if (isInitialized != true)
{
var env = await CreateWebView2Environment();
await webView.EnsureCoreWebView2Async(env);
isInitialized = true;
}

_controller.WebViewMessageReceived += (sender, message) => SendMessage?.Execute(message);

return await _controller.InitializeWebView(webView.CoreWebView2);
await _controller.InitializeWebView(webView.CoreWebView2);
System.Diagnostics.Debug.WriteLine("InitializeWebView", "WebView2Dev");
}

private async Task<CoreWebView2Environment> CreateWebView2Environment()
Expand Down
6 changes: 3 additions & 3 deletions src/Cody.UI/Controls/WebviewController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Web.WebView2.Core;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace Cody.UI.Controls
{
Expand Down
4 changes: 2 additions & 2 deletions src/Cody.UI/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public MainViewModel(NotificationHandlers notificationHandlers, ILog logger)
NotificationHandlers.OnSetHtmlEvent += OnSetHtmlHandler;
NotificationHandlers.OnPostMessageEvent += OnPostMessageHandler;

_logger.Debug("Initialized.");
_logger.Debug("MainViewModel Initialized.");
}

private void OnPostMessageHandler(object sender, AgentResponseEvent e)
Expand All @@ -44,7 +44,7 @@ public ICommand WebviewMessageSendCommand

private void WebviewSendMessage(object message)
{
NotificationHandlers.SendWebviewMessage("visual-studio", (string)message);
NotificationHandlers.SendWebviewMessage("visual-studio-sidebar", (string)message);
}

private void OnSetHtmlHandler(object sender, SetHtmlEvent e)
Expand Down
5 changes: 0 additions & 5 deletions src/Cody.VisualStudio/Client/AgentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using StreamJsonRpc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cody.VisualStudio.Client
{
Expand Down
53 changes: 25 additions & 28 deletions src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using Microsoft.VisualStudio.Shell;
using Cody.Core.Agent;
using Cody.Core.DocumentSync;
using Cody.Core.Ide;
using Cody.Core.Inf;
using Cody.Core.Infrastructure;
using Cody.Core.Logging;
using Cody.Core.Settings;
using Cody.UI.Controls;
using Cody.UI.Views;
using Cody.VisualStudio.Client;
using Cody.VisualStudio.Inf;
using Cody.VisualStudio.Services;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Cody.Core.Ide;
using Cody.Core.Inf;
using Cody.Core.Logging;
using Cody.UI.Views;
using Cody.UI.Controls;
using Cody.VisualStudio.Inf;
using Cody.VisualStudio.Services;
using Task = System.Threading.Tasks.Task;
using System.Reflection;
using System.IO;
using Cody.Core.Settings;
using Cody.Core.Infrastructure;
using Cody.Core.Agent;
using Cody.Core.DocumentSync;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell.Interop;
using Cody.VisualStudio.Client;
using System.Collections.Generic;

namespace Cody.VisualStudio
{
Expand Down Expand Up @@ -104,6 +104,7 @@ private void InitializeServices()
StatusbarService = new StatusbarService();
InitializeService = new InitializeCallback(UserSettingsService, VersionService, VsVersionService, StatusbarService, Logger);
ThemeService = new ThemeService(this);
NotificationHandlers = new NotificationHandlers();

var runningDocumentTable = this.GetService<SVsRunningDocumentTable, IVsRunningDocumentTable>();
var componentModel = this.GetService<SComponentModel, IComponentModel>();
Expand All @@ -117,16 +118,14 @@ private async Task InitOleMenu()
{
try
{
var oleMenuService = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
if (oleMenuService != null)
if (await GetServiceAsync(typeof(IMenuCommandService)) is OleMenuCommandService oleMenuService)
{
var commandId = new CommandID(Guids.CodyPackageCommandSet, (int)CommandIds.CodyToolWindow);
var menuItem = new MenuCommand(ShowToolWindow, commandId);
oleMenuService.AddCommand(menuItem);
oleMenuService.AddCommand(new MenuCommand(ShowToolWindow, commandId));
}
else
{
Logger.Error($"Cannot get {typeof(OleMenuCommandService)}");
throw new NotSupportedException($"Cannot get {nameof(OleMenuCommandService)}");
}
}
catch (Exception ex)
Expand Down Expand Up @@ -165,8 +164,6 @@ private async Task InitializeAgent()
{
var agentDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Agent");

NotificationHandlers = new NotificationHandlers();

var devPort = Environment.GetEnvironmentVariable("CODY_VS_DEV_PORT");
var portNumber = int.TryParse(devPort, out int port) ? port : 3113;

Expand All @@ -185,11 +182,11 @@ private async Task InitializeAgent()
NotificationHandlers.PostWebMessageAsJson = WebView2Dev.PostWebMessageAsJson;

_ = Task.Run(() => AgentClient.Start())
.ContinueWith(async x =>
.ContinueWith(async x =>
{
AgentService = AgentClient.CreateAgentService<IAgentService>();
NotificationHandlers.SetAgentClient(AgentService);
await InitializeService.Initialize(AgentService);
NotificationHandlers.SetAgentClient(AgentService);
})
.ContinueWith(x =>
{
Expand Down
Loading

0 comments on commit c144cba

Please sign in to comment.