using System.Text.Json.Nodes;
namespace XFEWorkflow.Plugins.AgentBrowser.WinUI;
public sealed partial class AgentBrowserPage
{
private async Task ShowWorkspaceAsync()
{
if (!workspaces.TryGetValue(session.Id, out var value))
{
var id = session.Id;
value = new(session, host, () =>
{
if (disposed) return;
if (workspace == workspaces.GetValueOrDefault(id)) address.Text = documents[id].BrowserUrl;
ScheduleSave(id);
}, () => { if (session.Id == id) active?.Cancel(); }, text => { if (session.Id == id) ShowError(text); else host.Log("会话浏览器:" + text, true); }, lifetime.Token);
workspaces.Add(id, value);
}
workspace?.Suspend(); workspace = value;
browserHost.Children.Clear(); browserHost.Children.Add(value.View);
value.Resume();
address.Text = session.BrowserUrl;
await value.EnsureActiveAsync(); ScheduleSave();
}
private async Task<JsonObject> InvokeBrowserTabsAsync(AgentBrowserWorkspace expected, JsonObject arguments, CancellationToken ct)
{
var result = new TaskCompletionSource<JsonObject>(TaskCreationOptions.RunContinuationsAsynchronously);
if (!root.DispatcherQueue.TryEnqueue(async () =>
{
try
{
ct.ThrowIfCancellationRequested();
if (disposed || workspace != expected) throw new InvalidOperationException("浏览器会话已切换或关闭");
result.TrySetResult(await expected.InvokeAsync(arguments));
}
catch (OperationCanceledException) { result.TrySetCanceled(ct); }
catch (Exception ex) { result.TrySetException(ex); }
})) result.TrySetCanceled();
return await result.Task.WaitAsync(ct);
}
}
using System.Text.Json.Nodes;
namespace XFEWorkflow.Plugins.AgentBrowser.WinUI;
public sealed partial class AgentBrowserPage
{
private async Task ShowWorkspaceAsync()
{
if (!workspaces.TryGetValue(session.Id, out var value))
{
var id = session.Id;
value = new(session, host, () =>
{
if (disposed) return;
if (workspace == workspaces.GetValueOrDefault(id)) address.Text = documents[id].BrowserUrl;
ScheduleSave(id);
}, () => { if (session.Id == id) active?.Cancel(); }, text => { if (session.Id == id) ShowError(text); else host.Log("会话浏览器:" + text, true); }, lifetime.Token);
workspaces.Add(id, value);
}
workspace?.Suspend(); workspace = value;
browserHost.Children.Clear(); browserHost.Children.Add(value.View);
value.Resume();
address.Text = session.BrowserUrl;
await value.EnsureActiveAsync(); ScheduleSave();
}
private async Task<JsonObject> InvokeBrowserTabsAsync(AgentBrowserWorkspace expected, JsonObject arguments, CancellationToken ct)
{
var result = new TaskCompletionSource<JsonObject>(TaskCreationOptions.RunContinuationsAsynchronously);
if (!root.DispatcherQueue.TryEnqueue(async () =>
{
try
{
ct.ThrowIfCancellationRequested();
if (disposed || workspace != expected) throw new InvalidOperationException("浏览器会话已切换或关闭");
result.TrySetResult(await expected.InvokeAsync(arguments));
}
catch (OperationCanceledException) { result.TrySetCanceled(ct); }
catch (Exception ex) { result.TrySetException(ex); }
})) result.TrySetCanceled();
return await result.Task.WaitAsync(ct);
}
}