using System.Text.Json.Nodes; using XFEWorkflow.Abstractions; namespace XFEWorkflow.Core.Runtime; internal sealed record WorkflowIteration(JsonNode? Item, int Index, Guid Scope); internal sealed class WorkflowLoopSignal(Guid scope, bool next) : Exception { public Guid Scope { get; } = scope; public bool Continue { get; } = next; } internal sealed record WorkflowCaughtError(string Message, string NodeId, string Type); public sealed class WorkflowExecutionContext : IWorkflowContext { public required string RunId { get; init; } public string WorkflowId { get; init; } = ""; public int DebugDepth { get; init; } public WorkflowDebugger? Debugger { get; init; } public JsonObject Inputs { get; init; } = []; public JsonObject Variables { get; init; } = []; public required ICapabilityInvoker Capabilities { get; init; } public required Func> Subworkflow { get; init; } public Func>? SubworkflowWithContext { get; init; } public required Func> Secret { get; init; } public required Func> Host { get; init; } public Func Report { get; init; } = _ => ValueTask.CompletedTask; public Dictionary NodeOutputs { get; init; } = []; public string? NodeId { get; init; } public string? EntryNodeId { get; init; } internal Func>? GraphExecutor { get; set; } internal Func>? WorkflowExecutor { get; set; } internal int CallDepth { get; init; } internal WorkflowIteration? Iteration { get; private set; } internal bool LoopControlAllowed { get; private set; } = true; internal WorkflowCaughtError? CaughtError { get; private set; } public WorkflowExecutionContext Fork(JsonObject? inputs = null, bool isolate = false, string? nodeId = null, int? debugDepth = null) => new() { RunId = RunId, WorkflowId = WorkflowId, Debugger = Debugger, DebugDepth = debugDepth ?? DebugDepth, Inputs = inputs ?? Inputs, Variables = isolate ? WorkflowJson.Copy(Variables) : Variables, Capabilities = Capabilities, Subworkflow = Subworkflow, SubworkflowWithContext = SubworkflowWithContext, Secret = Secret, Host = Host, Report = Report, NodeOutputs = isolate ? NodeOutputs.ToDictionary(x => x.Key, x => WorkflowJson.Copy(x.Value)) : NodeOutputs, NodeId = nodeId ?? NodeId, EntryNodeId = EntryNodeId, GraphExecutor = GraphExecutor, WorkflowExecutor = WorkflowExecutor, CallDepth = CallDepth, Iteration = Iteration, LoopControlAllowed = LoopControlAllowed, CaughtError = CaughtError }; internal WorkflowExecutionContext ForIteration(JsonNode? item, int index) { var child = Fork(); child.Iteration = new(item?.DeepClone(), index, Guid.NewGuid()); child.LoopControlAllowed = true; return child; } internal WorkflowExecutionContext ParallelScope() { var child = Fork(isolate: true, debugDepth: DebugDepth + 1); child.LoopControlAllowed = false; return child; } internal WorkflowExecutionContext ErrorScope(Exception error) { var child = Fork(isolate: true); child.CaughtError = new(error.Message, (error as WorkflowExecutionException)?.NodeId ?? "", (error.InnerException ?? error).GetType().FullName ?? error.GetType().Name); child.Variables["error"] = error.Message; return child; } internal WorkflowExecutionContext MethodScope(WorkflowDefinition workflow, JsonObject inputs) => new() { RunId = RunId, WorkflowId = workflow.Id, Debugger = Debugger, DebugDepth = DebugDepth + 1, Inputs = WorkflowJson.Copy(inputs), Variables = WorkflowJson.Copy(workflow.Variables), Capabilities = Capabilities, Subworkflow = Subworkflow, SubworkflowWithContext = SubworkflowWithContext, Secret = Secret, Host = Host, Report = Report, CallDepth = CallDepth + 1 }; public ValueTask InvokeAsync(string capabilityId, JsonObject inputs, CancellationToken cancellationToken = default) => Capabilities.InvokeAsync(capabilityId, inputs, this, cancellationToken); public ValueTask InvokeWorkflowAsync(string workflowId, JsonObject inputs, CancellationToken cancellationToken = default) => SubworkflowWithContext?.Invoke(workflowId, inputs, this, cancellationToken) ?? Subworkflow(workflowId, inputs, cancellationToken); public ValueTask ExecuteGraphAsync(GraphDefinition graph, CancellationToken cancellationToken = default) => GraphExecutor?.Invoke(graph, this, cancellationToken) ?? throw new InvalidOperationException("当前上下文尚未连接工作流引擎"); public ValueTask ExecuteWorkflowAsync(WorkflowDefinition workflow, JsonObject inputs, CancellationToken cancellationToken = default) => WorkflowExecutor?.Invoke(workflow, inputs, this, cancellationToken) ?? throw new InvalidOperationException("当前上下文尚未连接工作流引擎"); public ValueTask GetSecretAsync(string reference, CancellationToken cancellationToken = default) => Secret(reference, cancellationToken); public ValueTask InvokeHostAsync(string method, JsonObject parameters, CancellationToken cancellationToken = default) => Host(method, parameters, cancellationToken); public ValueTask LogAsync(string message, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); return Report(new() { RunId = RunId, NodeId = NodeId, Kind = RunEventKind.Log, Message = message }); } }