using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace XFEWorkflow.Abstractions; public enum WorkflowKind { Graph, CSharp } public enum PortKind { Execution, Data } public enum WorkflowValueType { Any, String, Number, Boolean, Object, Array, Resource } public enum RunStatus { Pending, Running, Succeeded, Failed, Cancelled, Interrupted, Skipped } public enum TriggerKind { Manual, Interval, Daily, FileChanged, Plugin, WorkflowCompleted } public enum RunEventKind { Started, NodeStarted, NodeSucceeded, NodeFailed, Log, Finished } public sealed class WorkflowDefinition { public int SchemaVersion { get; set; } = 1; public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string Name { get; set; } = "新建工作流"; public string Description { get; set; } = ""; public bool? Enabled { get; set; } public int Priority { get; set; } public WorkflowKind Kind { get; set; } public GraphDefinition Graph { get; set; } = new(); public string Code { get; set; } = ""; public string GeneratedCodeHash { get; set; } = ""; public string PreviousCode { get; set; } = ""; public List Inputs { get; set; } = []; public List Outputs { get; set; } = []; public JsonObject InputDefaults { get; set; } = []; public JsonObject Variables { get; set; } = []; public Dictionary PluginVersions { get; set; } = []; public List Triggers { get; set; } = []; public DateTimeOffset ModifiedAt { get; set; } = DateTimeOffset.UtcNow; [JsonIgnore] public string DisplayName => Name; } public sealed class GraphDefinition { public List Nodes { get; set; } = []; public List Edges { get; set; } = []; } public sealed class NodeDefinition { public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string CapabilityId { get; set; } = "core.log"; public string Name { get; set; } = ""; public double X { get; set; } = 300; public double Y { get; set; } = 160; public JsonObject Parameters { get; set; } = []; public string Code { get; set; } = ""; public TriggerDefinition? Trigger { get; set; } public Dictionary Children { get; set; } = []; public int TimeoutSeconds { get; set; } public int RetryCount { get; set; } public int RetryDelayMilliseconds { get; set; } = 500; public bool Breakpoint { get; set; } } public sealed class EdgeDefinition { public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string FromNode { get; set; } = ""; public string FromPort { get; set; } = "next"; public string ToNode { get; set; } = ""; public string ToPort { get; set; } = "in"; public PortKind Kind { get; set; } } public sealed class PortDefinition { public string Name { get; set; } = ""; public string Label { get; set; } = ""; public WorkflowValueType Type { get; set; } // Optional nominal type for structured plugin values (for example a live message reference). public string TypeId { get; set; } = ""; public string Description { get; set; } = ""; // Trigger outputs read this path from the emitted event; an empty path means the whole event. public string? EventPath { get; set; } public List Aliases { get; set; } = []; public bool Required { get; set; } public bool Secret { get; set; } public JsonNode? DefaultValue { get; set; } public List Choices { get; set; } = []; public string EditorHint { get; set; } = ""; } public sealed class TriggerDefinition { public string Id { get; set; } = Guid.NewGuid().ToString("N"); public TriggerKind Kind { get; set; } public bool Enabled { get; set; } public int IntervalSeconds { get; set; } = 300; public string DailyTime { get; set; } = "09:00"; public string Path { get; set; } = ""; public string Filter { get; set; } = "*"; public string CapabilityId { get; set; } = ""; public JsonObject Parameters { get; set; } = []; public JsonObject InputBindings { get; set; } = []; public string SourceWorkflowId { get; set; } = ""; public RunStatus? CompletionStatus { get; set; } = RunStatus.Succeeded; } public sealed class RunRecord { public string Id { get; set; } = Guid.NewGuid().ToString("N"); public string WorkflowId { get; set; } = ""; public string WorkflowName { get; set; } = ""; public string Trigger { get; set; } = "手动"; public string? EntryNodeId { get; set; } public RunStatus Status { get; set; } = RunStatus.Pending; public DateTimeOffset StartedAt { get; set; } = DateTimeOffset.UtcNow; public DateTimeOffset? FinishedAt { get; set; } public string? Error { get; set; } public string? ErrorNodeId { get; set; } public JsonObject Inputs { get; set; } = []; public List WorkflowChain { get; set; } = []; public Dictionary NodeNames { get; set; } = []; public JsonObject Outputs { get; set; } = []; public List Events { get; set; } = []; [JsonIgnore] public WorkflowDebugState? DebugState { get; set; } [JsonIgnore] public string Summary => $"{StartedAt.ToLocalTime():MM-dd HH:mm:ss} {WorkflowName} · {Status}"; } public sealed class RunEvent { public string RunId { get; set; } = ""; public string? NodeId { get; set; } public RunEventKind Kind { get; set; } public string Message { get; set; } = ""; public DateTimeOffset Timestamp { get; set; } = DateTimeOffset.UtcNow; [JsonIgnore] public string Display => $"{Timestamp.ToLocalTime():HH:mm:ss.fff} {Message}"; } public sealed record WorkflowDiagnostic(string Message, string? NodeId = null, string? File = null, int Line = 0, int Column = 0, bool IsError = true); public static partial class WorkflowJson { public static JsonSerializerOptions Options { get; } = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, WriteIndented = true, Converters = { new JsonStringEnumConverter() } }; public static T Clone(T value) => JsonSerializer.Deserialize(JsonSerializer.Serialize(value, Options), Options)!; public static JsonObject Copy(JsonObject? value) => value?.DeepClone().AsObject() ?? []; public static JsonNode? ReadPath(JsonNode? value, string path) { foreach (var part in path.Split('.', StringSplitOptions.RemoveEmptyEntries)) { if (value is JsonObject obj) value = obj[part]; else if (value is JsonArray array && int.TryParse(part, out var index) && index >= 0 && index < array.Count) value = array[index]; else return null; } return value; } public static string Text(this JsonNode? value, string fallback = "") => value is JsonValue scalar && scalar.TryGetValue(out var text) ? text : value is null ? fallback : value.Display(); public static string String(this JsonObject obj, string key, string fallback = "") => obj[key].Text(fallback); public static int Integer(this JsonObject obj, string key, int fallback = 0) => obj[key] is JsonValue scalar && scalar.TryGetValue(out var number) ? number : fallback; public static bool Boolean(this JsonObject obj, string key, bool fallback = false) => obj[key] is JsonValue scalar && scalar.TryGetValue(out var flag) ? flag : fallback; }