using System.Text.Json.Nodes; using Microsoft.UI.Xaml.Input; using Windows.System; using XFEWorkflow.Abstractions; using XFEWorkflow.Infrastructure.Compilation; namespace XFEWorkflow.Views; public sealed partial class MainPage { private string? lastDebugPause; private readonly Dictionary liveBreakpoints = []; private readonly SemaphoreSlim breakpointUpdates = new(1); private void InitializeDebugging() { foreach (var (key, modifiers, command) in new[] { (VirtualKey.F5, VirtualKeyModifiers.None, "run"), (VirtualKey.F5, VirtualKeyModifiers.Shift, "stop"), (VirtualKey.F9, VirtualKeyModifiers.None, "breakpoint"), (VirtualKey.F10, VirtualKeyModifiers.None, "stepOver"), (VirtualKey.F11, VirtualKeyModifiers.None, "stepInto") }) { var accelerator = new KeyboardAccelerator { Key = key, Modifiers = modifiers }; accelerator.Invoked += async (_, e) => { if (documentLayout) return; e.Handled = true; await Guard(() => ExecuteEditorCommandAsync(command)); }; KeyboardAccelerators.Add(accelerator); } CodeEditor.EditorCommandInvoked += async command => await Guard(() => ExecuteEditorCommandAsync(command)); } private async Task RunOrDebugAsync(string command) { if (ViewModel.SelectedWorkflow is not { } workflow) return; if (ViewModel.IsRunning || services.Runs.IsRunning(workflow.Id)) { var state = services.Runs.DebugState(workflow.Id); if (state?.IsPaused == true) { if (command == "pause") return; var kind = command switch { "stepOver" => WorkflowDebugCommandKind.StepOver, "stepInto" => WorkflowDebugCommandKind.StepInto, _ => WorkflowDebugCommandKind.Continue }; await services.Runs.DebugAsync(workflow.Id, new() { Kind = kind, PauseId = state.PauseId }); } else if (command != "run") { if (!await services.Runs.DebugAsync(workflow.Id, new() { Kind = WorkflowDebugCommandKind.Pause })) ViewModel.Status = "此运行未开启调试,请停止后按 F10 或 F11 调试"; } return; } if (command == "pause") return; ValidateValueEditors(); if (command != "run" && workflow.Kind == WorkflowKind.CSharp && !GraphCodeGenerator.CanRegenerate(workflow)) { ViewModel.Status = "手写 C# 可放入 C# 节点调试输入输出;目前单步调试以图形节点为单位"; return; } lastDebugPause = null; liveBreakpoints.Clear(); foreach (var node in AllNodes(workflow.Graph)) liveBreakpoints[node.Id] = node.Breakpoint; var record = await ViewModel.RunAsync(new() { BreakOnEntry = command != "run" }); if (record?.ErrorNodeId is { } id) LocateNode(id); } private static IEnumerable AllNodes(GraphDefinition graph) => graph.Nodes.SelectMany(node => new[] { node }.Concat(node.Children.Values.SelectMany(AllNodes))); private async Task SyncLiveBreakpointsAsync() { if (ViewModel.SelectedWorkflow is not { } workflow) return; await breakpointUpdates.WaitAsync(); try { var nodes = AllNodes(workflow.Graph).ToArray(); foreach (var removed in liveBreakpoints.Keys.Except(nodes.Select(n => n.Id)).ToArray()) { if (liveBreakpoints[removed]) foreach (var running in services.Runs.ActiveWorkflowIds.Where(services.Runs.IsDebugging)) await services.Runs.DebugAsync(running, new() { Kind = WorkflowDebugCommandKind.SetBreakpoint, WorkflowId = workflow.Id, NodeId = removed, Enabled = false }); liveBreakpoints.Remove(removed); } foreach (var node in nodes) { if (liveBreakpoints.TryGetValue(node.Id, out var previous) && previous == node.Breakpoint) continue; liveBreakpoints[node.Id] = node.Breakpoint; foreach (var running in services.Runs.ActiveWorkflowIds.Where(services.Runs.IsDebugging)) await services.Runs.DebugAsync(running, new() { Kind = WorkflowDebugCommandKind.SetBreakpoint, WorkflowId = workflow.Id, NodeId = node.Id, Enabled = node.Breakpoint }); } } finally { breakpointUpdates.Release(); } } private void ApplyDebugState(WorkflowDebugState? state) { ViewModel.IsDebugPaused = state?.IsPaused == true; if (state?.IsPaused != true) { if (lastDebugPause is not null) DebugSummary.Text = ViewModel.IsRunning ? "正在执行 · F10 / F11 可请求在下一节点暂停" : "调试已结束 · 保留最后一次暂停数据"; return; } ViewModel.Status = "已暂停 · " + state.NodeName + " · F5 继续 / F10 越过 / F11 进入"; if (state.PauseId == lastDebugPause) return; lastDebugPause = state.PauseId; DebugSummary.Text = state.Reason + " · " + state.NodeName + " · 层级 " + state.Depth + (state.Truncated ? " · 大数据仅显示有限预览" : ""); DebugValues.Text = new JsonObject { ["parameters"] = state.Parameters.DeepClone(), ["variables"] = state.Variables.DeepClone(), ["inputs"] = state.Inputs.DeepClone(), ["nodeOutputs"] = state.NodeOutputs.DeepClone() }.Display(true); if (state.WorkflowId == ViewModel.SelectedWorkflow?.Id) { LocateNode(state.NodeId, false); CanvasEditor.SelectNode(state.NodeId); // The debug inspector is already below the canvas. Do not obscure // the paused node with the automatic property popup in narrow windows. if (PropertySplit.DisplayMode == SplitViewDisplayMode.Overlay) PropertySplit.IsPaneOpen = false; } else DebugSummary.Text += " · 子工作流 " + state.WorkflowId; if (outputCollapsed) ToggleOutput_Click(this, new()); RunPanel.SelectedItem = DebugTab; } private void AddBreakpointEditor(NodeDefinition node) { var breakpoint = new ToggleSwitch { Header = "断点 · F9", IsOn = node.Breakpoint, OnContent = "执行此节点前暂停", OffContent = "无断点" }; breakpoint.Toggled += (_, _) => { if (breakpoint.IsOn != node.Breakpoint) CanvasEditor.ToggleBreakpoint(node.Id); }; PropertyPanel.Children.Add(breakpoint); } }