using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using XFEWorkflow.Abstractions;
namespace XFEWorkflow.Plugins.AgentBrowser.WinUI;
public sealed partial class AgentBrowserPage
{
private async Task ShowSettingsAsync()
{
if (active is not null) return;
var endpointBox = new TextBox { Header = "DeepSeek API 地址", Text = options.Endpoint };
var modelBox = new ComboBox { Header = "模型", IsEditable = true, HorizontalAlignment = HorizontalAlignment.Stretch, Text = options.Model };
foreach (var model in new[] { "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-v4-flash-vision-exp" }) modelBox.Items.Add(model);
if (modelBox.Items.Contains(options.Model)) modelBox.SelectedItem = options.Model;
modelBox.Text = options.Model;
modelBox.Loaded += (_, _) => modelBox.Text = options.Model;
var credential = new ComboBox { Header = "API Key 凭据名称", IsEditable = true, HorizontalAlignment = HorizontalAlignment.Stretch };
foreach (var item in host.CredentialReferences) credential.Items.Add(item);
credential.Text = options.Credential;
credential.Loaded += (_, _) => credential.Text = options.Credential;
var secret = new PasswordBox { Header = "设置 / 更新 API Key(留空沿用已有凭据)", PlaceholderText = "仅存入 Windows 当前用户加密凭据库" };
var thinking = new CheckBox { Content = "启用 DeepSeek 思考模式", IsChecked = options.Thinking };
var vision = new CheckBox { Content = "将截图发送给模型(仅用于支持图片输入的模型)", IsChecked = options.Vision };
var skill = new TextBox { Header = "browser-debugger 技能目录", Text = options.SkillDirectory };
var node = new TextBox { Header = "Node.js 22+ 可执行文件", Text = options.NodeExecutable };
var steps = new NumberBox { Header = "单轮最多工具轮数", Minimum = 1, Maximum = 100, Value = options.MaximumSteps, SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Compact };
var skillNotice = new TextBlock { Text = "每次发送前重新导入 SKILL.md,并使用该目录的 scripts/browser-session.mjs。", TextWrapping = TextWrapping.Wrap, FontSize = 12, Opacity = .75 };
var check = new Button { Content = "检查技能与运行环境" };
check.Click += async (_, _) =>
{
check.IsEnabled = false;
try
{
var loaded = await BrowserSkill.LoadAsync(skill.Text, lifetime.Token);
using var process = new Process { StartInfo = new ProcessStartInfo(node.Text)
{
UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true,
WorkingDirectory = loaded.Directory
} };
process.StartInfo.ArgumentList.Add(Path.Combine(loaded.Directory, "scripts", "browser.mjs")); process.StartInfo.ArgumentList.Add("doctor");
process.Start(); using var limit = CancellationTokenSource.CreateLinkedTokenSource(lifetime.Token); limit.CancelAfter(TimeSpan.FromSeconds(15));
using var registration = limit.Token.Register(() => { try { if (!process.HasExited) process.Kill(true); } catch (InvalidOperationException) { } });
var stdout = process.StandardOutput.ReadToEndAsync(limit.Token); var stderr = process.StandardError.ReadToEndAsync(limit.Token);
await process.WaitForExitAsync(limit.Token);
var result = JsonNode.Parse(await stdout)!.AsObject();
if (process.ExitCode != 0 || !result.Boolean("ready")) throw new IOException("运行环境未就绪:在技能目录执行 npm.cmd ci --ignore-scripts --no-audit --no-fund;需要 Node.js 22+。" + await stderr);
skillNotice.Text = "技能已就绪 · SHA256 " + loaded.Hash[..12] + " · " + result.String("node");
}
catch (Exception ex) { skillNotice.Text = host.Redact(ex.Message); }
finally { check.IsEnabled = true; }
};
var fields = new StackPanel { Spacing = 12, MinWidth = 380, MaxWidth = 620 };
foreach (var item in new UIElement[] { endpointBox, modelBox, credential, secret, thinking, vision, skill, node, steps, check, skillNotice }) fields.Children.Add(item);
var dialog = new ContentDialog
{
Title = "DeepSeek 浏览器 Agent 设置", Content = new ScrollViewer { Content = fields, MaxHeight = Math.Max(300, root.XamlRoot.Size.Height - 260) },
PrimaryButtonText = "保存", CloseButtonText = "取消", DefaultButton = ContentDialogButton.Primary, XamlRoot = root.XamlRoot, RequestedTheme = root.ActualTheme
};
var validation = new InfoBar { Severity = InfoBarSeverity.Error, IsOpen = false }; fields.Children.Add(validation);
dialog.PrimaryButtonClick += async (_, args) =>
{
var deferral = args.GetDeferral();
try
{
var proposed = new AgentOptions
{
Endpoint = endpointBox.Text.Trim(), Model = modelBox.Text.Trim(), Credential = credential.Text.Trim(),
SkillDirectory = Path.GetFullPath(skill.Text.Trim()), NodeExecutable = node.Text.Trim(), MaximumSteps = (int)steps.Value,
Thinking = thinking.IsChecked == true, Vision = vision.IsChecked == true
};
proposed.CompletionUri();
if (proposed.Credential.Length == 0 && secret.Password.Length > 0) throw new ArgumentException("请输入凭据名称");
if (proposed.NodeExecutable.Length == 0) throw new ArgumentException("请输入 Node.js 路径");
if (secret.Password.Length > 0) await host.SetCredentialAsync(proposed.Credential, secret.Password, lifetime.Token);
await host.SaveSettingsAsync(JsonSerializer.SerializeToNode(proposed, WorkflowJson.Options)!.AsObject(), lifetime.Token);
options = proposed; modelLabel.Text = options.Model; ToolTipService.SetToolTip(status, options.SkillDirectory);
conversation!.Clear(); Post(new { type = "clear" }); status.Text = "设置已保存 · 已开启新对话";
}
catch (Exception ex) { args.Cancel = true; validation.Message = host.Redact(ex.Message); validation.IsOpen = true; }
finally { deferral.Complete(); }
};
await dialog.ShowAsync();
}
}
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using XFEWorkflow.Abstractions;
namespace XFEWorkflow.Plugins.AgentBrowser.WinUI;
public sealed partial class AgentBrowserPage
{
private async Task ShowSettingsAsync()
{
if (active is not null) return;
var endpointBox = new TextBox { Header = "DeepSeek API 地址", Text = options.Endpoint };
var modelBox = new ComboBox { Header = "模型", IsEditable = true, HorizontalAlignment = HorizontalAlignment.Stretch, Text = options.Model };
foreach (var model in new[] { "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-v4-flash-vision-exp" }) modelBox.Items.Add(model);
if (modelBox.Items.Contains(options.Model)) modelBox.SelectedItem = options.Model;
modelBox.Text = options.Model;
modelBox.Loaded += (_, _) => modelBox.Text = options.Model;
var credential = new ComboBox { Header = "API Key 凭据名称", IsEditable = true, HorizontalAlignment = HorizontalAlignment.Stretch };
foreach (var item in host.CredentialReferences) credential.Items.Add(item);
credential.Text = options.Credential;
credential.Loaded += (_, _) => credential.Text = options.Credential;
var secret = new PasswordBox { Header = "设置 / 更新 API Key(留空沿用已有凭据)", PlaceholderText = "仅存入 Windows 当前用户加密凭据库" };
var thinking = new CheckBox { Content = "启用 DeepSeek 思考模式", IsChecked = options.Thinking };
var vision = new CheckBox { Content = "将截图发送给模型(仅用于支持图片输入的模型)", IsChecked = options.Vision };
var skill = new TextBox { Header = "browser-debugger 技能目录", Text = options.SkillDirectory };
var node = new TextBox { Header = "Node.js 22+ 可执行文件", Text = options.NodeExecutable };
var steps = new NumberBox { Header = "单轮最多工具轮数", Minimum = 1, Maximum = 100, Value = options.MaximumSteps, SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Compact };
var skillNotice = new TextBlock { Text = "每次发送前重新导入 SKILL.md,并使用该目录的 scripts/browser-session.mjs。", TextWrapping = TextWrapping.Wrap, FontSize = 12, Opacity = .75 };
var check = new Button { Content = "检查技能与运行环境" };
check.Click += async (_, _) =>
{
check.IsEnabled = false;
try
{
var loaded = await BrowserSkill.LoadAsync(skill.Text, lifetime.Token);
using var process = new Process { StartInfo = new ProcessStartInfo(node.Text)
{
UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true,
WorkingDirectory = loaded.Directory
} };
process.StartInfo.ArgumentList.Add(Path.Combine(loaded.Directory, "scripts", "browser.mjs")); process.StartInfo.ArgumentList.Add("doctor");
process.Start(); using var limit = CancellationTokenSource.CreateLinkedTokenSource(lifetime.Token); limit.CancelAfter(TimeSpan.FromSeconds(15));
using var registration = limit.Token.Register(() => { try { if (!process.HasExited) process.Kill(true); } catch (InvalidOperationException) { } });
var stdout = process.StandardOutput.ReadToEndAsync(limit.Token); var stderr = process.StandardError.ReadToEndAsync(limit.Token);
await process.WaitForExitAsync(limit.Token);
var result = JsonNode.Parse(await stdout)!.AsObject();
if (process.ExitCode != 0 || !result.Boolean("ready")) throw new IOException("运行环境未就绪:在技能目录执行 npm.cmd ci --ignore-scripts --no-audit --no-fund;需要 Node.js 22+。" + await stderr);
skillNotice.Text = "技能已就绪 · SHA256 " + loaded.Hash[..12] + " · " + result.String("node");
}
catch (Exception ex) { skillNotice.Text = host.Redact(ex.Message); }
finally { check.IsEnabled = true; }
};
var fields = new StackPanel { Spacing = 12, MinWidth = 380, MaxWidth = 620 };
foreach (var item in new UIElement[] { endpointBox, modelBox, credential, secret, thinking, vision, skill, node, steps, check, skillNotice }) fields.Children.Add(item);
var dialog = new ContentDialog
{
Title = "DeepSeek 浏览器 Agent 设置", Content = new ScrollViewer { Content = fields, MaxHeight = Math.Max(300, root.XamlRoot.Size.Height - 260) },
PrimaryButtonText = "保存", CloseButtonText = "取消", DefaultButton = ContentDialogButton.Primary, XamlRoot = root.XamlRoot, RequestedTheme = root.ActualTheme
};
var validation = new InfoBar { Severity = InfoBarSeverity.Error, IsOpen = false }; fields.Children.Add(validation);
dialog.PrimaryButtonClick += async (_, args) =>
{
var deferral = args.GetDeferral();
try
{
var proposed = new AgentOptions
{
Endpoint = endpointBox.Text.Trim(), Model = modelBox.Text.Trim(), Credential = credential.Text.Trim(),
SkillDirectory = Path.GetFullPath(skill.Text.Trim()), NodeExecutable = node.Text.Trim(), MaximumSteps = (int)steps.Value,
Thinking = thinking.IsChecked == true, Vision = vision.IsChecked == true
};
proposed.CompletionUri();
if (proposed.Credential.Length == 0 && secret.Password.Length > 0) throw new ArgumentException("请输入凭据名称");
if (proposed.NodeExecutable.Length == 0) throw new ArgumentException("请输入 Node.js 路径");
if (secret.Password.Length > 0) await host.SetCredentialAsync(proposed.Credential, secret.Password, lifetime.Token);
await host.SaveSettingsAsync(JsonSerializer.SerializeToNode(proposed, WorkflowJson.Options)!.AsObject(), lifetime.Token);
options = proposed; modelLabel.Text = options.Model; ToolTipService.SetToolTip(status, options.SkillDirectory);
conversation!.Clear(); Post(new { type = "clear" }); status.Text = "设置已保存 · 已开启新对话";
}
catch (Exception ex) { args.Cancel = true; validation.Message = host.Redact(ex.Message); validation.IsOpen = true; }
finally { deferral.Complete(); }
};
await dialog.ShowAsync();
}
}