XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json.Nodes;

namespace XFEWorkflow.Abstractions;

public static class WorkflowSdk
{
    public const string CurrentVersion = "1.7.0";
}

public interface IWorkflow
{
    ValueTask<JsonObject> ExecuteAsync(IWorkflowContext context, CancellationToken cancellationToken);
}

public interface IWorkflowContext
{
    string RunId { get; }
    JsonObject Inputs { get; }
    JsonObject Variables { get; }
    ValueTask<JsonObject> InvokeAsync(string capabilityId, JsonObject inputs, CancellationToken cancellationToken = default);
    ValueTask<JsonObject> InvokeWorkflowAsync(string workflowId, JsonObject inputs, CancellationToken cancellationToken = default);
    ValueTask<JsonObject> ExecuteGraphAsync(GraphDefinition graph, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("当前宿主不支持从 C# 执行图形,请使用 SDK 1.2 或更高版本");
    ValueTask<JsonObject> ExecuteWorkflowAsync(WorkflowDefinition workflow, JsonObject inputs, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("封装节点需要 SDK 1.3 或更高版本");
    ValueTask<string> GetSecretAsync(string reference, CancellationToken cancellationToken = default);
    ValueTask<JsonObject> InvokeHostAsync(string method, JsonObject parameters, CancellationToken cancellationToken = default);
    ValueTask LogAsync(string message, CancellationToken cancellationToken = default);
}

public interface ICapabilityInvoker
{
    ValueTask<JsonObject> InvokeAsync(string capabilityId, JsonObject inputs, IWorkflowContext context, CancellationToken cancellationToken);
}

public interface IWorkflowPlugin
{
    void Register(IPluginRegistry registry);
}

public interface IPluginRegistry
{
    void RegisterAction(CapabilityDescriptor descriptor, Func<IWorkflowAction> factory);
    void RegisterTrigger(CapabilityDescriptor descriptor, Func<IWorkflowTrigger> factory);
    void RegisterPage(PluginPageDescriptor descriptor) => throw new NotSupportedException("插件页面需要 SDK 1.7 或更高版本");
}

public interface IWorkflowAction
{
    ValueTask<JsonObject> ExecuteAsync(JsonObject inputs, IWorkflowContext context, CancellationToken cancellationToken);
}

public interface IWorkflowTrigger
{
    Task RunAsync(JsonObject parameters, IWorkflowContext context, Func<JsonObject, ValueTask> emit, CancellationToken cancellationToken);
}

public sealed class CapabilityDescriptor
{
    public string Id { get; set; } = "";
    public string PluginId { get; set; } = "";
    public string Name { get; set; } = "";
    public string Category { get; set; } = "插件";
    public string Description { get; set; } = "";
    public bool IsTrigger { get; set; }
    public List<PortDefinition> Inputs { get; set; } = [];
    public List<PortDefinition> Outputs { get; set; } = [];
    public List<string> ExecutionOutputs { get; set; } = ["next"];
    public string? ExecutionResultPort { get; set; }
    public List<string> ChildGraphs { get; set; } = [];
    public override string ToString() => Name;
}

public sealed class PluginManifest
{
    public int SchemaVersion { get; set; } = 1;
    public int SdkMajorVersion { get; set; } = 1;
    public string SdkMinimumVersion { get; set; } = "1.0.0";
    public string SdkMaximumVersionExclusive { get; set; } = "2.0.0";
    public string Id { get; set; } = "";
    public string Name { get; set; } = "";
    public string Version { get; set; } = "1.0.0";
    public string Description { get; set; } = "";
    public string Author { get; set; } = "";
    public string Assembly { get; set; } = "";
    public string EntryType { get; set; } = "";
    public List<string> SupportedRids { get; set; } = [];
    public List<string> Permissions { get; set; } = [];
    public List<PluginDependency> Dependencies { get; set; } = [];
    public List<CapabilityDescriptor> Capabilities { get; set; } = [];
    public List<PluginPageDescriptor> Pages { get; set; } = [];
}

/// <summary>Optional desktop extension. The main plugin entry stays platform independent; its view assembly is loaded only by the desktop host.</summary>
public sealed class PluginPageDescriptor
{
    public string Id { get; set; } = "";
    public string PluginId { get; set; } = "";
    public string Name { get; set; } = "";
    public string Description { get; set; } = "";
    public string IconGlyph { get; set; } = "\uE774";
    public string Assembly { get; set; } = "";
    public string EntryType { get; set; } = "";
}

public sealed class PluginDependency
{
    public string Id { get; set; } = "";
    public string MinimumVersion { get; set; } = "1.0.0";
    public string? MaximumVersionExclusive { get; set; }
    public string? ExactVersion { get; set; }
    public bool Accepts(string version) => (ExactVersion is null || ExactVersion == version)
        && SemanticVersion.Parse(version).CompareTo(SemanticVersion.Parse(MinimumVersion)) >= 0
        && (MaximumVersionExclusive is null || SemanticVersion.Parse(version).CompareTo(SemanticVersion.Parse(MaximumVersionExclusive)) < 0);
}

public sealed class InstalledPlugin
{
    public PluginManifest Manifest { get; set; } = new();
    public bool Enabled { get; set; } = true;
    public bool PendingRemoval { get; set; }
    public string Directory { get; set; } = "";
    public string Source { get; set; } = "本地导入";
    public override string ToString() => $"{Manifest.Name}  {Manifest.Version}";
}

public interface ICodeWorkflowExecutor
{
    ValueTask<JsonObject> ExecuteAsync(string source, bool isNodeBody, string fileName, IWorkflowContext context, CancellationToken cancellationToken);
}