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:



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

def initialize(runner)
  @runner = runner
end

Class Method Details

.define(context_class, state_store: nil) { ... } ⇒ Phronomy::Workflow

Defines a new Workflow.

Parameters:

  • context_class (Class)

    class that includes Phronomy::WorkflowContext

  • state_store (Object, nil) (defaults to: nil)

    optional state store override (passed to WorkflowRunner)

Yields:

  • block evaluated in DSL context

Returns:



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

def self.define(context_class, state_store: nil, &block)
  builder = Builder.new(context_class, state_store: state_store)
  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



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

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



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

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



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

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



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

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