Skip to content

1.17.5

Choose a tag to compare

@ilvalerione ilvalerione released this 30 Aug 09:09
· 3 commits to 1.x since this release

Improvements from #275

String-based node keys to the Workflow system

  • String-based node keys: Use descriptive string keys like 'add1', 'multiply_first' instead of class names
  • Multiple node instances: Instantiate the same node class multiple times with different parameters
  • Full backward compatibility: Existing workflows using class names continue to work without modification
  • Smart detection: Automatically detects whether nodes use string keys or class names
  • Enhanced Mermaid export: Diagram generation handles both string keys and class names

Example

<?php

use NeuronAI\Workflow\Edge;
use NeuronAI\Workflow\Node;
use NeuronAI\Workflow\Workflow;
use NeuronAI\Workflow\WorkflowState;

// Reusable node classes
class AddNode extends Node
{
    public function __construct(private int $value) {}

    public function run(WorkflowState $state): WorkflowState
    {
        $current = $state->get('value', 0);
        $state->set('value', $current + $this->value);
        return $state;
    }
}

class MultiplyNode extends Node
{
    public function __construct(private int $value) {}

    public function run(WorkflowState $state): WorkflowState
    {
        $current = $state->get('value', 0);
        $state->set('value', $current * $this->value);
        return $state;
    }
}

class SubtractNode extends Node
{
    public function __construct(private int $value) {}

    public function run(WorkflowState $state): WorkflowState
    {
        $current = $state->get('value', 0);
        $state->set('value', $current - $this->value);
        return $state;
    }
}

// Workflow that calculates: ((value + 1) * 3) * 3) - 1
class CalculatorWorkflow extends Workflow
{
    public function nodes(): array
    {
        return [
            'add1' => new AddNode(1),
            'multiply3_first' => new MultiplyNode(3),
            'multiply3_second' => new MultiplyNode(3),  // Same class, different instance!
            'sub1' => new SubtractNode(1),
            'finish' => new FinishNode()
        ];
    }

    public function edges(): array
    {
        return [
            new Edge('add1', 'multiply3_first'),
            new Edge('multiply3_first', 'multiply3_second'),
            new Edge('multiply3_second', 'sub1'),
            new Edge('sub1', 'finish')
        ];
    }

    protected function start(): string
    {
        return 'add1';
    }

    protected function end(): array
    {
        return ['finish'];
    }
}