using w4c_workflows.Models.Nodes; namespace w4c_workflows.Services.Nodes.Executors; /// /// Limit node. Keeps the first or last N items of the incoming batch and drops /// the rest. Runs in all-items mode because "last N" needs the whole input. /// public sealed class LimitNodeExecutor : INodeExecutor { public string Type => "core.limit"; public Task RunAsync(NodeExecutionContext context, CancellationToken ct) { var maxItems = NodeValueAccess.ReadInt(context.Parameters, "maxItems", 1); if (maxItems < 0) return Task.FromResult(NodeExecutionOutcome.Failed("maxItems must not be negative", "invalid_parameter")); var items = context.Input(0); var keep = NodeValueAccess.ReadString(context.Parameters, "keep") ?? "firstItems"; IReadOnlyList output = keep switch { "lastItems" => items.Skip(Math.Max(0, items.Count - maxItems)).ToList(), _ => items.Take(maxItems).ToList(), }; return Task.FromResult(NodeExecutionOutcome.Single(output)); } }