Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify WebView initialization #98

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
26 changes: 9 additions & 17 deletions src/Cody.AgentTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
using Cody.Core.Settings;
using Cody.VisualStudio.Client;
using Cody.VisualStudio.Services;
using Microsoft.VisualStudio.Setup.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Cody.AgentTester
{
internal class Program
{
private static AgentClient client;
private static AgentClientProvider clientProvider;
private static readonly ConsoleLogger logger = new ConsoleLogger();
private static readonly ConsoleLogger agentLogger = new ConsoleLogger();
private static IAgentService agentService;
private static IAgentClient agentClient;

static async Task Main(string[] args)
{
Expand All @@ -33,28 +30,25 @@ static async Task Main(string[] args)
var secretStorageService = new SecretStorageService(new FakeSecretStorageProvider());
var settingsService = new UserSettingsService(new MemorySettingsProvider(), secretStorageService, logger);
var editorService = new FileService(new FakeServiceProvider(), logger);
var options = new AgentClientOptions
var options = new AgentClientProviderOptions
{
CallbackHandlers = new List<object> { new NotificationHandlers(settingsService, logger, editorService, secretStorageService) },
CallbackHandlers = new List<object> { new NotificationHandlers(logger, editorService, secretStorageService) },
AgentDirectory = "../../../Cody.VisualStudio/Agent",
RestartAgentOnFailure = true,
Debug = true,
ConnectToRemoteAgent = devPort != null,
RemoteAgentPort = portNumber,
ClientInfo = GetClientInfo()
};

client = new AgentClient(options, logger, agentLogger);

client.Start();

await Initialize();
clientProvider = new AgentClientProvider(options, logger, agentLogger);
agentClient = await clientProvider.CreateAgentClient();

Console.ReadKey();
}

private static async Task Initialize()
private static ClientInfo GetClientInfo()
{
var clientInfo = new ClientInfo
return new ClientInfo
{
Name = "VisualStudio",
Version = "1.0",
Expand Down Expand Up @@ -93,8 +87,6 @@ private static async Task Initialize()

}
};

await agentService.Initialize(clientInfo);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Cody.Core.Agent
//---------------------------------------------------------
// For notifications return type MUST be void!
//---------------------------------------------------------
public interface IAgentService
public interface IAgentClient
{
[AgentCall("initialize")]
Task<ServerInfo> Initialize(ClientInfo clientInfo);
Expand Down
15 changes: 0 additions & 15 deletions src/Cody.Core/Agent/IAgentProxy.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

namespace Cody.Core.Agent
{
public interface INotificationHandler
public interface IInjectAgentClient
{
event EventHandler OnOptionsPageShowRequest;
event EventHandler<string> OnRegisterWebViewRequest;
IAgentClient AgentClient { set; }
}
}
125 changes: 3 additions & 122 deletions src/Cody.Core/Agent/NotificationHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,27 @@
using Cody.Core.Agent.Protocol;
using Cody.Core.Workspace;
using Cody.Core.Logging;
using Cody.Core.Settings;
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;
using Cody.Core.Infrastructure;

namespace Cody.Core.Agent
{
public class NotificationHandlers : INotificationHandler
public class NotificationHandlers : IInjectAgentClient
{
private readonly WebviewMessageHandler _messageFilter;
private readonly IUserSettingsService _settingsService;
private readonly IFileService _fileService;
private readonly ISecretStorageService _secretStorage;
private readonly ILog _logger;

public IAgentService agentClient;
public IAgentClient AgentClient { set; private get; }

public delegate Task PostWebMessageAsJsonDelegate(string message);
public PostWebMessageAsJsonDelegate PostWebMessageAsJson { get; set; }

public event EventHandler<SetHtmlEvent> OnSetHtmlEvent;

public event EventHandler<string> OnRegisterWebViewRequest;
public event EventHandler OnOptionsPageShowRequest;
public event EventHandler OnFocusSidebarRequest;

public event EventHandler<AgentResponseEvent> OnPostMessageEvent;

public NotificationHandlers(IUserSettingsService settingsService, ILog logger, IFileService fileService, ISecretStorageService secretStorage)
public NotificationHandlers(ILog logger, IFileService fileService, ISecretStorageService secretStorage)
{
_settingsService = settingsService;
_fileService = fileService;
_secretStorage = secretStorage;
_logger = logger;
_messageFilter = new WebviewMessageHandler(settingsService, fileService, () => OnOptionsPageShowRequest?.Invoke(this, EventArgs.Empty));
}

public void SetAgentClient(IAgentService client)
{
agentClient = client;
}

// Send a message to the host from webview.
public async Task SendWebviewMessage(string handle, string message)
{
bool handled = _messageFilter.HandleMessage(message);
if (!handled)
await agentClient.ReceiveMessageStringEncoded(new ReceiveMessageStringEncodedParams
{
Id = handle,
MessageStringEncoded = message
});
}

[AgentCallback("debug/message")]
Expand All @@ -62,87 +30,6 @@ public void Debug(string channel, string message)
_logger.Debug($"[{channel} {message}]");
}

[AgentCallback("webview/registerWebview")]
public void RegisterWebview(string handle)
{
_logger.Debug(handle);
}

[AgentCallback("webview/registerWebviewViewProvider")]
public void RegisterWebviewViewProvider(string viewId, bool retainContextWhenHidden)
{
_logger.Debug(viewId);
OnRegisterWebViewRequest?.Invoke(this, viewId);
}

[AgentCallback("webview/createWebviewPanel", deserializeToSingleObject: true)]
public void CreateWebviewPanel(CreateWebviewPanelParams panelParams)
{
_logger.Debug(panelParams.ToString());
}

[AgentCallback("webview/setOptions")]
public void SetOptions(string handle, DefiniteWebviewOptions options)
{
if (options.EnableCommandUris is bool enableCmd)
{
_logger.Debug(handle);
}
else if (options.EnableCommandUris is JArray jArray)
{
var uris = jArray.ToObject<string[]>();
}
}

[AgentCallback("webview/setHtml")]
public void SetHtml(string handle, string html)
{
OnSetHtmlEvent?.Invoke(this, new SetHtmlEvent() { Handle = handle, Html = html });
}

[AgentCallback("webview/PostMessage")]
public void PostMessage(string handle, string message)
{
PostMessageStringEncoded(handle, message);
}

[AgentCallback("webview/postMessageStringEncoded")]
public void PostMessageStringEncoded(string id, string stringEncodedMessage)
{
_logger.Debug(stringEncodedMessage);
PostWebMessageAsJson?.Invoke(stringEncodedMessage);
}

[AgentCallback("webview/didDisposeNative")]
public void DidDisposeNative(string handle)
{
_logger.Debug(handle);
}

[AgentCallback("webview/dispose")]
public void Dispose(string handle)
{
_logger.Debug(handle);
}

[AgentCallback("webview/reveal")]
public void Reveal(string handle, int viewColumn, bool preserveFocus)
{
_logger.Debug(handle);
}

[AgentCallback("webview/setTitle")]
public void SetTitle(string handle, string title)
{
_logger.Debug(title);
}

[AgentCallback("webview/setIconPath")]
public void SetIconPath(string handle, string iconPathUri)
{
_logger.Debug(iconPathUri);
}

[AgentCallback("window/didChangeContext")]
public void WindowDidChangeContext(string key, string value)
{
Expand All @@ -165,12 +52,6 @@ public void ExtensionConfigDidChange(ExtensionConfiguration config)
_logger.Debug(config.ToString());
}

[AgentCallback("ignore/didChange")]
public void IgnoreDidChange()
{
_logger.Debug("Changed");
}

[AgentCallback("textDocument/show", deserializeToSingleObject: true)]
public Task<bool> ShowTextDocument(TextDocumentShowParams param)
{
Expand Down
11 changes: 4 additions & 7 deletions src/Cody.Core/Agent/ProgressNotificationHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cody.Core.Agent
{
public class ProgressNotificationHandlers
public class ProgressNotificationHandlers : IInjectAgentClient
{
private IProgressService progressService;
private IAgentService agentService;

public IAgentClient AgentClient { set; private get; }

public ProgressNotificationHandlers(IProgressService progressService)
{
this.progressService = progressService;
}

public void SetAgentService(IAgentService agentService) => this.agentService = agentService;

[AgentCallback("progress/start", deserializeToSingleObject: true)]
public void Start(ProgressStartParams progressStart)
{
Action cancelAction = null;
if (progressStart.Options.Cancellable == true)
{
cancelAction = () => agentService.CancelProgress(progressStart.Id);
cancelAction = () => AgentClient.CancelProgress(progressStart.Id);
};

progressService.Start(progressStart.Id, progressStart.Options.Title, cancelAction);
Expand Down
23 changes: 0 additions & 23 deletions src/Cody.Core/Agent/SetHtmlEvent.cs

This file was deleted.

Loading
Loading