Class: Phronomy::Workflow

Inherits:
Object
  • Object
show all
Includes:
Runnable
Defined in:
lib/phronomy/workflow.rb

Overview

StateChart-style workflow definition DSL.

Defines agent workflows in terms of states and events backed by Phronomy::WorkflowRunner. This is the primary high-level API for graph-based execution in phronomy.

== Basic usage

app = Phronomy::Workflow.define(MyContext) do initial :fetch

state :fetch,   action: FETCH_NODE
state :process, action: PROCESS_NODE

after :fetch,   to: :process
after :process, to: :__finish__

end

result = app.invoke({ url: "https://example.com" })

== Wait states

app = Phronomy::Workflow.define(MyContext) do initial :propose

state :propose, action: PROPOSE_NODE
wait_state :awaiting_approval
state :execute, action: EXECUTE_NODE

after :propose, to: :awaiting_approval
after :execute, to: :__finish__

event :approve, from: :awaiting_approval, to: :execute
event :reject,  from: :awaiting_approval, to: :propose

end

halted = app.invoke({ ... }) final = app.send_event(state: halted, event: :approve)

== Conditional transitions

event :route, from: :decide, guard: ->(s) { s.score > 5 }, to: :high event :route, from: :decide, to: :low # fallback (no guard)

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Runnable

#batch, #trace

Constructor Details

#initialize(runner) ⇒ Workflow

Returns a new instance of Workflow.

Parameters:



65
66
67
# File 'lib/phronomy/workflow.rb', line 65

def initialize(runner)
  @runner = runner
end

Class Method Details

.define(context_class) { ... } ⇒ Phronomy::Workflow

Defines a new Workflow.

Parameters:

  • context_class (Class)

    class that includes Phronomy::WorkflowContext

Yields:

  • block evaluated in DSL context

Returns:



58
59
60
61
62
# File 'lib/phronomy/workflow.rb', line 58

def self.define(context_class, &block)
  builder = Builder.new(context_class)
  builder.instance_eval(&block)
  builder.build
end

Instance Method Details

#invoke(input, config: {}) ⇒ Object

Executes the workflow from the initial state.

Parameters:

  • input (Hash)

    initial context field values

  • config (Hash) (defaults to: {})

    { thread_id:, recursion_limit:, user_id:, session_id: }

Returns:

  • (Object)

    final context



73
74
75
# File 'lib/phronomy/workflow.rb', line 73

def invoke(input, config: {})
  @runner.invoke(input, config: config)
end

#resume(state:, input: nil) ⇒ Object

Resumes a halted workflow. Generic resume that works for all halt types.

Parameters:

  • state (Object)

    halted context

  • input (Hash, nil) (defaults to: nil)

    optional field updates to merge before resuming

Returns:

  • (Object)

    final context



81
82
83
# File 'lib/phronomy/workflow.rb', line 81

def resume(state:, input: nil)
  @runner.resume(state: state, input: input)
end

#send_event(state:, event:, input: nil) ⇒ Object

Fires a named event to advance a halted workflow.

Parameters:

  • state (Object)

    halted context

  • event (Symbol)

    event name (e.g. :approve, :reject, :resume)

  • input (Hash, nil) (defaults to: nil)

    optional field updates to merge before resuming

Returns:

  • (Object)

    final context



90
91
92
# File 'lib/phronomy/workflow.rb', line 90

def send_event(state:, event:, input: nil)
  @runner.send_event(state: state, event: event, input: input)
end

#stream(input, config: {}) {|Hash| ... } ⇒ Object

Streaming execution. Yields { node: Symbol, state: Object } after each node.

Parameters:

  • input (Hash)
  • config (Hash) (defaults to: {})

Yields:

  • (Hash)

Returns:

  • (Object)

    final context



99
100
101
# File 'lib/phronomy/workflow.rb', line 99

def stream(input, config: {}, &block)
  @runner.stream(input, config: config, &block)
end