yaml-flow 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2300,6 +2300,733 @@ function resolveConfigTemplates(config) {
2300
2300
  return result;
2301
2301
  }
2302
2302
 
2303
- export { COMPLETION_STRATEGIES, CONFLICT_STRATEGIES, DEFAULTS, EXECUTION_MODES, EXECUTION_STATUS, FileStore, StepMachine as FlowEngine, LocalStorageStore, MemoryStore, StepMachine, TASK_STATUS, addDynamicTask, apply, applyAll, applyStepResult, batch, checkCircuitBreaker, computeAvailableOutputs, computeStepInput, createDefaultTaskState, createStepMachine as createEngine, createInitialExecutionState, createInitialState, createStepMachine, detectStuckState, exportGraphConfig, exportGraphConfigToFile, extractReturnData, flowToMermaid, getAllTasks, getCandidateTasks, getProvides, getRequires, getTask, graphToMermaid, hasTask, isExecutionComplete, isNonActiveTask, isRepeatableTask, isTaskCompleted, isTaskRunning, loadGraphConfig, loadStepFlow, next, planExecution, resolveConfigTemplates, resolveVariables, validateGraph, validateGraphConfig, validateStepFlowConfig };
2303
+ // src/continuous-event-graph/core.ts
2304
+ function createLiveGraph(config, executionId) {
2305
+ const id = executionId ?? `live-${Date.now()}`;
2306
+ const tasks = {};
2307
+ for (const taskName of Object.keys(config.tasks)) {
2308
+ tasks[taskName] = createDefaultTaskState3();
2309
+ }
2310
+ const state = {
2311
+ status: "running",
2312
+ tasks,
2313
+ availableOutputs: [],
2314
+ stuckDetection: { is_stuck: false, stuck_description: null, outputs_unresolvable: [], tasks_blocked: [] },
2315
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString(),
2316
+ executionId: id,
2317
+ executionConfig: {
2318
+ executionMode: config.settings.execution_mode ?? "eligibility-mode",
2319
+ conflictStrategy: config.settings.conflict_strategy ?? "alphabetical",
2320
+ completionStrategy: config.settings.completion
2321
+ }
2322
+ };
2323
+ return { config, state };
2324
+ }
2325
+ function applyEvent(live, event) {
2326
+ const { config, state } = live;
2327
+ if ("executionId" in event && event.executionId && event.executionId !== state.executionId) {
2328
+ return live;
2329
+ }
2330
+ let newState;
2331
+ switch (event.type) {
2332
+ case "task-started":
2333
+ newState = applyTaskStart(state, event.taskName);
2334
+ break;
2335
+ case "task-completed":
2336
+ newState = applyTaskCompletion(state, config, event.taskName, event.result);
2337
+ break;
2338
+ case "task-failed":
2339
+ newState = applyTaskFailure(state, config, event.taskName, event.error);
2340
+ break;
2341
+ case "task-progress":
2342
+ newState = applyTaskProgress(state, event.taskName, event.message, event.progress);
2343
+ break;
2344
+ case "inject-tokens":
2345
+ newState = {
2346
+ ...state,
2347
+ availableOutputs: [.../* @__PURE__ */ new Set([...state.availableOutputs, ...event.tokens])],
2348
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2349
+ };
2350
+ break;
2351
+ case "agent-action":
2352
+ newState = applyAgentAction2(state, event.action);
2353
+ break;
2354
+ default:
2355
+ return live;
2356
+ }
2357
+ return { config, state: newState };
2358
+ }
2359
+ function addNode(live, name, taskConfig) {
2360
+ if (live.config.tasks[name]) return live;
2361
+ return {
2362
+ config: {
2363
+ ...live.config,
2364
+ tasks: { ...live.config.tasks, [name]: taskConfig }
2365
+ },
2366
+ state: {
2367
+ ...live.state,
2368
+ tasks: { ...live.state.tasks, [name]: createDefaultTaskState3() },
2369
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2370
+ }
2371
+ };
2372
+ }
2373
+ function removeNode(live, name) {
2374
+ if (!live.config.tasks[name]) return live;
2375
+ const { [name]: _removedConfig, ...remainingTasks } = live.config.tasks;
2376
+ const { [name]: _removedState, ...remainingStates } = live.state.tasks;
2377
+ return {
2378
+ config: {
2379
+ ...live.config,
2380
+ tasks: remainingTasks
2381
+ },
2382
+ state: {
2383
+ ...live.state,
2384
+ tasks: remainingStates,
2385
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2386
+ }
2387
+ };
2388
+ }
2389
+ function addRequires(live, nodeName, tokens) {
2390
+ const task = live.config.tasks[nodeName];
2391
+ if (!task) return live;
2392
+ const current = getRequires(task);
2393
+ const toAdd = tokens.filter((t) => !current.includes(t));
2394
+ if (toAdd.length === 0) return live;
2395
+ return {
2396
+ config: {
2397
+ ...live.config,
2398
+ tasks: {
2399
+ ...live.config.tasks,
2400
+ [nodeName]: { ...task, requires: [...current, ...toAdd] }
2401
+ }
2402
+ },
2403
+ state: live.state
2404
+ };
2405
+ }
2406
+ function removeRequires(live, nodeName, tokens) {
2407
+ const task = live.config.tasks[nodeName];
2408
+ if (!task) return live;
2409
+ const current = getRequires(task);
2410
+ const remaining = current.filter((t) => !tokens.includes(t));
2411
+ if (remaining.length === current.length) return live;
2412
+ return {
2413
+ config: {
2414
+ ...live.config,
2415
+ tasks: {
2416
+ ...live.config.tasks,
2417
+ [nodeName]: { ...task, requires: remaining }
2418
+ }
2419
+ },
2420
+ state: live.state
2421
+ };
2422
+ }
2423
+ function addProvides(live, nodeName, tokens) {
2424
+ const task = live.config.tasks[nodeName];
2425
+ if (!task) return live;
2426
+ const current = getProvides(task);
2427
+ const toAdd = tokens.filter((t) => !current.includes(t));
2428
+ if (toAdd.length === 0) return live;
2429
+ return {
2430
+ config: {
2431
+ ...live.config,
2432
+ tasks: {
2433
+ ...live.config.tasks,
2434
+ [nodeName]: { ...task, provides: [...current, ...toAdd] }
2435
+ }
2436
+ },
2437
+ state: live.state
2438
+ };
2439
+ }
2440
+ function removeProvides(live, nodeName, tokens) {
2441
+ const task = live.config.tasks[nodeName];
2442
+ if (!task) return live;
2443
+ const current = getProvides(task);
2444
+ const remaining = current.filter((t) => !tokens.includes(t));
2445
+ if (remaining.length === current.length) return live;
2446
+ return {
2447
+ config: {
2448
+ ...live.config,
2449
+ tasks: {
2450
+ ...live.config.tasks,
2451
+ [nodeName]: { ...task, provides: remaining }
2452
+ }
2453
+ },
2454
+ state: live.state
2455
+ };
2456
+ }
2457
+ function injectTokens(live, tokens) {
2458
+ return applyEvent(live, {
2459
+ type: "inject-tokens",
2460
+ tokens,
2461
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
2462
+ });
2463
+ }
2464
+ function drainTokens(live, tokens) {
2465
+ const toRemove = new Set(tokens);
2466
+ const remaining = live.state.availableOutputs.filter((t) => !toRemove.has(t));
2467
+ if (remaining.length === live.state.availableOutputs.length) return live;
2468
+ return {
2469
+ config: live.config,
2470
+ state: {
2471
+ ...live.state,
2472
+ availableOutputs: remaining,
2473
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2474
+ }
2475
+ };
2476
+ }
2477
+ function resetNode(live, name) {
2478
+ if (!live.config.tasks[name] || !live.state.tasks[name]) return live;
2479
+ return {
2480
+ config: live.config,
2481
+ state: {
2482
+ ...live.state,
2483
+ tasks: {
2484
+ ...live.state.tasks,
2485
+ [name]: createDefaultTaskState3()
2486
+ },
2487
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2488
+ }
2489
+ };
2490
+ }
2491
+ function disableNode(live, name) {
2492
+ const taskState = live.state.tasks[name];
2493
+ if (!taskState || taskState.status === "inactivated") return live;
2494
+ return {
2495
+ config: live.config,
2496
+ state: {
2497
+ ...live.state,
2498
+ tasks: {
2499
+ ...live.state.tasks,
2500
+ [name]: { ...taskState, status: "inactivated", lastUpdated: (/* @__PURE__ */ new Date()).toISOString() }
2501
+ },
2502
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2503
+ }
2504
+ };
2505
+ }
2506
+ function enableNode(live, name) {
2507
+ const taskState = live.state.tasks[name];
2508
+ if (!taskState || taskState.status !== "inactivated") return live;
2509
+ return {
2510
+ config: live.config,
2511
+ state: {
2512
+ ...live.state,
2513
+ tasks: {
2514
+ ...live.state.tasks,
2515
+ [name]: { ...taskState, status: "not-started", lastUpdated: (/* @__PURE__ */ new Date()).toISOString() }
2516
+ },
2517
+ lastUpdated: (/* @__PURE__ */ new Date()).toISOString()
2518
+ }
2519
+ };
2520
+ }
2521
+ function getNode(live, name) {
2522
+ const config = live.config.tasks[name];
2523
+ if (!config) return void 0;
2524
+ const state = live.state.tasks[name] ?? createDefaultTaskState3();
2525
+ return { name, config, state };
2526
+ }
2527
+ function snapshot(live) {
2528
+ return {
2529
+ version: 1,
2530
+ config: live.config,
2531
+ state: live.state,
2532
+ snapshotAt: (/* @__PURE__ */ new Date()).toISOString()
2533
+ };
2534
+ }
2535
+ function restore(data) {
2536
+ if (!data || typeof data !== "object") {
2537
+ throw new Error("Invalid snapshot: expected an object");
2538
+ }
2539
+ const snap = data;
2540
+ if (!snap.config || typeof snap.config !== "object") {
2541
+ throw new Error('Invalid snapshot: missing or invalid "config"');
2542
+ }
2543
+ if (!snap.state || typeof snap.state !== "object") {
2544
+ throw new Error('Invalid snapshot: missing or invalid "state"');
2545
+ }
2546
+ const config = snap.config;
2547
+ const state = snap.state;
2548
+ if (!config.settings || typeof config.settings !== "object") {
2549
+ throw new Error("Invalid snapshot: config.settings missing");
2550
+ }
2551
+ if (!config.tasks || typeof config.tasks !== "object") {
2552
+ throw new Error("Invalid snapshot: config.tasks missing");
2553
+ }
2554
+ if (!state.tasks || typeof state.tasks !== "object") {
2555
+ throw new Error("Invalid snapshot: state.tasks missing");
2556
+ }
2557
+ if (!Array.isArray(state.availableOutputs)) {
2558
+ throw new Error("Invalid snapshot: state.availableOutputs must be an array");
2559
+ }
2560
+ return { config, state };
2561
+ }
2562
+ function createDefaultTaskState3() {
2563
+ return {
2564
+ status: "not-started",
2565
+ executionCount: 0,
2566
+ retryCount: 0,
2567
+ lastEpoch: 0,
2568
+ messages: [],
2569
+ progress: null
2570
+ };
2571
+ }
2572
+ function applyAgentAction2(state, action) {
2573
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2574
+ switch (action) {
2575
+ case "stop":
2576
+ return { ...state, status: "stopped", lastUpdated: now };
2577
+ case "pause":
2578
+ return { ...state, status: "paused", lastUpdated: now };
2579
+ case "resume":
2580
+ return { ...state, status: "running", lastUpdated: now };
2581
+ default:
2582
+ return state;
2583
+ }
2584
+ }
2585
+
2586
+ // src/continuous-event-graph/schedule.ts
2587
+ function schedule(live) {
2588
+ const { config, state } = live;
2589
+ const graphTasks = getAllTasks(config);
2590
+ const taskNames = Object.keys(graphTasks);
2591
+ if (taskNames.length === 0) {
2592
+ return { eligible: [], pending: [], unresolved: [], blocked: [], conflicts: {} };
2593
+ }
2594
+ const producerMap = buildProducerMap3(graphTasks);
2595
+ const computedOutputs = computeAvailableOutputs(config, state.tasks);
2596
+ const availableOutputs = /* @__PURE__ */ new Set([...computedOutputs, ...state.availableOutputs]);
2597
+ const eligible = [];
2598
+ const pending = [];
2599
+ const unresolved = [];
2600
+ const blocked = [];
2601
+ for (const [taskName, taskConfig] of Object.entries(graphTasks)) {
2602
+ const taskState = state.tasks[taskName];
2603
+ if (!isRepeatableTask(taskConfig)) {
2604
+ if (taskState?.status === TASK_STATUS.COMPLETED || taskState?.status === TASK_STATUS.RUNNING || isNonActiveTask(taskState)) {
2605
+ continue;
2606
+ }
2607
+ } else {
2608
+ if (taskState?.status === TASK_STATUS.RUNNING || isNonActiveTask(taskState)) {
2609
+ continue;
2610
+ }
2611
+ const maxExec = getRepeatableMax(taskConfig);
2612
+ if (maxExec !== void 0 && taskState && taskState.executionCount >= maxExec) {
2613
+ continue;
2614
+ }
2615
+ if (taskConfig.circuit_breaker && taskState && taskState.executionCount >= taskConfig.circuit_breaker.max_executions) {
2616
+ continue;
2617
+ }
2618
+ if (taskState?.status === TASK_STATUS.COMPLETED) {
2619
+ const requires2 = getRequires(taskConfig);
2620
+ if (requires2.length > 0) {
2621
+ const hasRefreshed = requires2.some((req) => {
2622
+ for (const [otherName, otherConfig] of Object.entries(graphTasks)) {
2623
+ if (getProvides(otherConfig).includes(req)) {
2624
+ const otherState = state.tasks[otherName];
2625
+ if (otherState && otherState.executionCount > taskState.lastEpoch) return true;
2626
+ }
2627
+ }
2628
+ return false;
2629
+ });
2630
+ if (!hasRefreshed) continue;
2631
+ } else {
2632
+ continue;
2633
+ }
2634
+ }
2635
+ }
2636
+ const requires = getRequires(taskConfig);
2637
+ if (requires.length === 0) {
2638
+ eligible.push(taskName);
2639
+ continue;
2640
+ }
2641
+ const missingTokens = [];
2642
+ const pendingTokens = [];
2643
+ const failedTokenInfo = [];
2644
+ for (const token of requires) {
2645
+ if (availableOutputs.has(token)) continue;
2646
+ const producers = producerMap[token] || [];
2647
+ if (producers.length === 0) {
2648
+ missingTokens.push(token);
2649
+ } else {
2650
+ const allFailed = producers.every((p) => isNonActiveTask(state.tasks[p]));
2651
+ if (allFailed) {
2652
+ failedTokenInfo.push({ token, failedProducer: producers[0] });
2653
+ } else {
2654
+ pendingTokens.push(token);
2655
+ }
2656
+ }
2657
+ }
2658
+ if (missingTokens.length > 0) {
2659
+ unresolved.push({ taskName, missingTokens });
2660
+ } else if (failedTokenInfo.length > 0) {
2661
+ blocked.push({
2662
+ taskName,
2663
+ failedTokens: failedTokenInfo.map((f) => f.token),
2664
+ failedProducers: [...new Set(failedTokenInfo.map((f) => f.failedProducer))]
2665
+ });
2666
+ } else if (pendingTokens.length > 0) {
2667
+ pending.push({ taskName, waitingOn: pendingTokens });
2668
+ } else {
2669
+ eligible.push(taskName);
2670
+ }
2671
+ }
2672
+ const conflicts = {};
2673
+ if (eligible.length > 1) {
2674
+ const outputGroups = groupTasksByProvides(eligible, graphTasks);
2675
+ for (const [outputKey, groupTasks] of Object.entries(outputGroups)) {
2676
+ if (groupTasks.length > 1) {
2677
+ conflicts[outputKey] = groupTasks;
2678
+ }
2679
+ }
2680
+ }
2681
+ return { eligible, pending, unresolved, blocked, conflicts };
2682
+ }
2683
+ function buildProducerMap3(tasks) {
2684
+ const map = {};
2685
+ for (const [name, config] of Object.entries(tasks)) {
2686
+ for (const token of getProvides(config)) {
2687
+ if (!map[token]) map[token] = [];
2688
+ map[token].push(name);
2689
+ }
2690
+ if (config.on) {
2691
+ for (const tokens of Object.values(config.on)) {
2692
+ for (const token of tokens) {
2693
+ if (!map[token]) map[token] = [];
2694
+ if (!map[token].includes(name)) map[token].push(name);
2695
+ }
2696
+ }
2697
+ }
2698
+ if (config.on_failure) {
2699
+ for (const token of config.on_failure) {
2700
+ if (!map[token]) map[token] = [];
2701
+ if (!map[token].includes(name)) map[token].push(name);
2702
+ }
2703
+ }
2704
+ }
2705
+ return map;
2706
+ }
2707
+
2708
+ // src/continuous-event-graph/inspect.ts
2709
+ function inspect(live) {
2710
+ const { config, state } = live;
2711
+ const graphTasks = getAllTasks(config);
2712
+ const taskNames = Object.keys(graphTasks);
2713
+ let running = 0, completed = 0, failed = 0, waiting = 0, notStarted = 0, disabled = 0;
2714
+ for (const taskName of taskNames) {
2715
+ const ts = state.tasks[taskName];
2716
+ if (!ts || ts.status === TASK_STATUS.NOT_STARTED) {
2717
+ notStarted++;
2718
+ } else {
2719
+ switch (ts.status) {
2720
+ case TASK_STATUS.RUNNING:
2721
+ running++;
2722
+ break;
2723
+ case TASK_STATUS.COMPLETED:
2724
+ completed++;
2725
+ break;
2726
+ case TASK_STATUS.FAILED:
2727
+ failed++;
2728
+ break;
2729
+ case "inactivated":
2730
+ disabled++;
2731
+ break;
2732
+ default:
2733
+ waiting++;
2734
+ }
2735
+ }
2736
+ }
2737
+ const producerMap = {};
2738
+ for (const [name, taskConfig] of Object.entries(graphTasks)) {
2739
+ for (const token of getProvides(taskConfig)) {
2740
+ if (!producerMap[token]) producerMap[token] = [];
2741
+ producerMap[token].push(name);
2742
+ }
2743
+ if (taskConfig.on) {
2744
+ for (const tokens of Object.values(taskConfig.on)) {
2745
+ for (const token of tokens) {
2746
+ if (!producerMap[token]) producerMap[token] = [];
2747
+ if (!producerMap[token].includes(name)) producerMap[token].push(name);
2748
+ }
2749
+ }
2750
+ }
2751
+ if (taskConfig.on_failure) {
2752
+ for (const token of taskConfig.on_failure) {
2753
+ if (!producerMap[token]) producerMap[token] = [];
2754
+ if (!producerMap[token].includes(name)) producerMap[token].push(name);
2755
+ }
2756
+ }
2757
+ }
2758
+ const openDeps = /* @__PURE__ */ new Set();
2759
+ let unresolvedCount = 0;
2760
+ let blockedCount = 0;
2761
+ for (const [taskName, taskConfig] of Object.entries(graphTasks)) {
2762
+ const ts = state.tasks[taskName];
2763
+ if (ts?.status === TASK_STATUS.COMPLETED || ts?.status === TASK_STATUS.RUNNING) continue;
2764
+ let hasOpen = false;
2765
+ let hasBlocked = false;
2766
+ for (const token of getRequires(taskConfig)) {
2767
+ const producers = producerMap[token] || [];
2768
+ if (producers.length === 0) {
2769
+ openDeps.add(token);
2770
+ hasOpen = true;
2771
+ } else {
2772
+ const allFailed = producers.every((p) => {
2773
+ const ps = state.tasks[p];
2774
+ return ps?.status === TASK_STATUS.FAILED || ps?.status === "inactivated";
2775
+ });
2776
+ if (allFailed) hasBlocked = true;
2777
+ }
2778
+ }
2779
+ if (hasOpen) unresolvedCount++;
2780
+ if (hasBlocked && !hasOpen) blockedCount++;
2781
+ }
2782
+ const conflictTokens = [];
2783
+ for (const [token, producers] of Object.entries(producerMap)) {
2784
+ if (producers.length > 1) conflictTokens.push(token);
2785
+ }
2786
+ const deps = buildTaskDeps2(graphTasks, producerMap);
2787
+ const cycles = detectCycles2(taskNames, deps);
2788
+ return {
2789
+ totalNodes: taskNames.length,
2790
+ running,
2791
+ completed,
2792
+ failed,
2793
+ waiting,
2794
+ notStarted,
2795
+ disabled,
2796
+ unresolvedCount,
2797
+ blockedCount,
2798
+ openDependencies: [...openDeps],
2799
+ cycles,
2800
+ conflictTokens
2801
+ };
2802
+ }
2803
+ function buildTaskDeps2(tasks, producerMap) {
2804
+ const deps = {};
2805
+ for (const [name, config] of Object.entries(tasks)) {
2806
+ deps[name] = /* @__PURE__ */ new Set();
2807
+ for (const token of getRequires(config)) {
2808
+ for (const producer of producerMap[token] || []) {
2809
+ if (producer !== name) deps[name].add(producer);
2810
+ }
2811
+ }
2812
+ }
2813
+ return deps;
2814
+ }
2815
+ function detectCycles2(taskNames, deps) {
2816
+ const WHITE = 0, GRAY = 1, BLACK = 2;
2817
+ const color = {};
2818
+ const parent = {};
2819
+ const cycles = [];
2820
+ for (const name of taskNames) {
2821
+ color[name] = WHITE;
2822
+ parent[name] = null;
2823
+ }
2824
+ function dfs(node) {
2825
+ color[node] = GRAY;
2826
+ for (const dep of deps[node] || []) {
2827
+ if (color[dep] === GRAY) {
2828
+ const cycle = [dep];
2829
+ let cur = node;
2830
+ while (cur !== dep) {
2831
+ cycle.push(cur);
2832
+ cur = parent[cur];
2833
+ }
2834
+ cycle.push(dep);
2835
+ cycle.reverse();
2836
+ cycles.push(cycle);
2837
+ } else if (color[dep] === WHITE) {
2838
+ parent[dep] = node;
2839
+ dfs(dep);
2840
+ }
2841
+ }
2842
+ color[node] = BLACK;
2843
+ }
2844
+ for (const name of taskNames) {
2845
+ if (color[name] === WHITE) dfs(name);
2846
+ }
2847
+ return cycles;
2848
+ }
2849
+ function buildProducerMap4(tasks) {
2850
+ const map = {};
2851
+ for (const [name, config] of Object.entries(tasks)) {
2852
+ for (const token of getProvides(config)) {
2853
+ if (!map[token]) map[token] = [];
2854
+ map[token].push(name);
2855
+ }
2856
+ if (config.on) {
2857
+ for (const tokens of Object.values(config.on)) {
2858
+ for (const token of tokens) {
2859
+ if (!map[token]) map[token] = [];
2860
+ if (!map[token].includes(name)) map[token].push(name);
2861
+ }
2862
+ }
2863
+ }
2864
+ if (config.on_failure) {
2865
+ for (const token of config.on_failure) {
2866
+ if (!map[token]) map[token] = [];
2867
+ if (!map[token].includes(name)) map[token].push(name);
2868
+ }
2869
+ }
2870
+ }
2871
+ return map;
2872
+ }
2873
+ function getUnreachableTokens(live) {
2874
+ const { config, state } = live;
2875
+ const graphTasks = getAllTasks(config);
2876
+ const producerMap = buildProducerMap4(graphTasks);
2877
+ const available = /* @__PURE__ */ new Set([...state.availableOutputs]);
2878
+ for (const [taskName, taskState] of Object.entries(state.tasks)) {
2879
+ if (taskState.status === "completed") {
2880
+ const tc = graphTasks[taskName];
2881
+ if (tc) getProvides(tc).forEach((t) => available.add(t));
2882
+ }
2883
+ }
2884
+ const allRequired = /* @__PURE__ */ new Set();
2885
+ for (const taskConfig of Object.values(graphTasks)) {
2886
+ for (const token of getRequires(taskConfig)) {
2887
+ allRequired.add(token);
2888
+ }
2889
+ }
2890
+ const unreachable = /* @__PURE__ */ new Set();
2891
+ const unreachableNodes = /* @__PURE__ */ new Set();
2892
+ for (const token of allRequired) {
2893
+ if (available.has(token)) continue;
2894
+ const producers = producerMap[token] || [];
2895
+ if (producers.length === 0) {
2896
+ unreachable.add(token);
2897
+ }
2898
+ }
2899
+ let changed = true;
2900
+ while (changed) {
2901
+ changed = false;
2902
+ for (const [name, taskConfig] of Object.entries(graphTasks)) {
2903
+ if (unreachableNodes.has(name)) continue;
2904
+ const ts = state.tasks[name];
2905
+ if (ts?.status === "completed") continue;
2906
+ const isNonActive = isNonActiveTask(ts);
2907
+ const requires = getRequires(taskConfig);
2908
+ const hasUnreachableDep = requires.some((t) => unreachable.has(t));
2909
+ if (isNonActive || hasUnreachableDep) {
2910
+ if (!unreachableNodes.has(name)) {
2911
+ unreachableNodes.add(name);
2912
+ changed = true;
2913
+ }
2914
+ }
2915
+ }
2916
+ for (const token of allRequired) {
2917
+ if (unreachable.has(token) || available.has(token)) continue;
2918
+ const producers = producerMap[token] || [];
2919
+ const allProducersUnreachable = producers.length > 0 && producers.every((p) => unreachableNodes.has(p) || isNonActiveTask(state.tasks[p]));
2920
+ if (producers.length === 0 || allProducersUnreachable) {
2921
+ if (!unreachable.has(token)) {
2922
+ unreachable.add(token);
2923
+ changed = true;
2924
+ }
2925
+ }
2926
+ }
2927
+ }
2928
+ const tokens = [];
2929
+ for (const token of unreachable) {
2930
+ const producers = producerMap[token] || [];
2931
+ let reason;
2932
+ if (producers.length === 0) {
2933
+ reason = "no-producer";
2934
+ } else {
2935
+ const allFailed = producers.every((p) => isNonActiveTask(state.tasks[p]));
2936
+ reason = allFailed ? "all-producers-failed" : "transitive";
2937
+ }
2938
+ tokens.push({ token, reason, producers });
2939
+ }
2940
+ return { tokens };
2941
+ }
2942
+ function getUnreachableNodes(live) {
2943
+ const { config, state } = live;
2944
+ const graphTasks = getAllTasks(config);
2945
+ const { tokens: unreachableTokens } = getUnreachableTokens(live);
2946
+ const unreachableTokenSet = new Set(unreachableTokens.map((t) => t.token));
2947
+ const nodes = [];
2948
+ for (const [name, taskConfig] of Object.entries(graphTasks)) {
2949
+ const ts = state.tasks[name];
2950
+ if (ts?.status === "completed") continue;
2951
+ const requires = getRequires(taskConfig);
2952
+ const missingTokens = requires.filter((t) => unreachableTokenSet.has(t));
2953
+ if (missingTokens.length > 0) {
2954
+ nodes.push({ nodeName: name, missingTokens });
2955
+ } else if (isNonActiveTask(ts)) {
2956
+ nodes.push({ nodeName: name, missingTokens: [] });
2957
+ }
2958
+ }
2959
+ return { nodes };
2960
+ }
2961
+ function getUpstream(live, nodeName) {
2962
+ const graphTasks = getAllTasks(live.config);
2963
+ if (!graphTasks[nodeName]) return { nodeName, nodes: [], tokens: [] };
2964
+ const producerMap = buildProducerMap4(graphTasks);
2965
+ const visited = /* @__PURE__ */ new Set();
2966
+ const tokenSet = /* @__PURE__ */ new Set();
2967
+ const nodeEntries = /* @__PURE__ */ new Map();
2968
+ function walk(current) {
2969
+ const taskConfig = graphTasks[current];
2970
+ if (!taskConfig) return;
2971
+ for (const token of getRequires(taskConfig)) {
2972
+ const producers = producerMap[token] || [];
2973
+ for (const producer of producers) {
2974
+ if (producer === nodeName) continue;
2975
+ tokenSet.add(token);
2976
+ if (!nodeEntries.has(producer)) nodeEntries.set(producer, /* @__PURE__ */ new Set());
2977
+ nodeEntries.get(producer).add(token);
2978
+ if (!visited.has(producer)) {
2979
+ visited.add(producer);
2980
+ walk(producer);
2981
+ }
2982
+ }
2983
+ }
2984
+ }
2985
+ walk(nodeName);
2986
+ const nodes = [...nodeEntries.entries()].map(([name, tokens]) => ({
2987
+ nodeName: name,
2988
+ providesTokens: [...tokens]
2989
+ }));
2990
+ return { nodeName, nodes, tokens: [...tokenSet] };
2991
+ }
2992
+ function getDownstream(live, nodeName) {
2993
+ const graphTasks = getAllTasks(live.config);
2994
+ if (!graphTasks[nodeName]) return { nodeName, nodes: [], tokens: [] };
2995
+ const consumerMap = {};
2996
+ for (const [name, config] of Object.entries(graphTasks)) {
2997
+ for (const token of getRequires(config)) {
2998
+ if (!consumerMap[token]) consumerMap[token] = [];
2999
+ consumerMap[token].push(name);
3000
+ }
3001
+ }
3002
+ const visited = /* @__PURE__ */ new Set();
3003
+ const tokenSet = /* @__PURE__ */ new Set();
3004
+ const nodeEntries = /* @__PURE__ */ new Map();
3005
+ function walk(current) {
3006
+ const taskConfig = graphTasks[current];
3007
+ if (!taskConfig) return;
3008
+ for (const token of getProvides(taskConfig)) {
3009
+ const consumers = consumerMap[token] || [];
3010
+ for (const consumer of consumers) {
3011
+ if (consumer === nodeName) continue;
3012
+ tokenSet.add(token);
3013
+ if (!nodeEntries.has(consumer)) nodeEntries.set(consumer, /* @__PURE__ */ new Set());
3014
+ nodeEntries.get(consumer).add(token);
3015
+ if (!visited.has(consumer)) {
3016
+ visited.add(consumer);
3017
+ walk(consumer);
3018
+ }
3019
+ }
3020
+ }
3021
+ }
3022
+ walk(nodeName);
3023
+ const nodes = [...nodeEntries.entries()].map(([name, tokens]) => ({
3024
+ nodeName: name,
3025
+ requiresTokens: [...tokens]
3026
+ }));
3027
+ return { nodeName, nodes, tokens: [...tokenSet] };
3028
+ }
3029
+
3030
+ export { COMPLETION_STRATEGIES, CONFLICT_STRATEGIES, DEFAULTS, EXECUTION_MODES, EXECUTION_STATUS, FileStore, StepMachine as FlowEngine, LocalStorageStore, MemoryStore, StepMachine, TASK_STATUS, addDynamicTask, addNode, addProvides, addRequires, apply, applyAll, applyEvent, applyStepResult, batch, checkCircuitBreaker, computeAvailableOutputs, computeStepInput, createDefaultTaskState, createStepMachine as createEngine, createInitialExecutionState, createInitialState, createLiveGraph, createStepMachine, detectStuckState, disableNode, drainTokens, enableNode, exportGraphConfig, exportGraphConfigToFile, extractReturnData, flowToMermaid, getAllTasks, getCandidateTasks, getDownstream, getNode, getProvides, getRequires, getTask, getUnreachableNodes, getUnreachableTokens, getUpstream, graphToMermaid, hasTask, injectTokens, inspect, isExecutionComplete, isNonActiveTask, isRepeatableTask, isTaskCompleted, isTaskRunning, loadGraphConfig, loadStepFlow, next, planExecution, removeNode, removeProvides, removeRequires, resetNode, resolveConfigTemplates, resolveVariables, restore, schedule, snapshot, validateGraph, validateGraphConfig, validateStepFlowConfig };
2304
3031
  //# sourceMappingURL=index.js.map
2305
3032
  //# sourceMappingURL=index.js.map