Class: LangGraphRB::ConditionalEdge

Inherits:
Object
  • Object
show all
Defined in:
lib/langgraph_rb/edge.rb

Overview

Conditional edge that uses a router function to determine destination(s)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, router, path_map = nil) ⇒ ConditionalEdge

Returns a new instance of ConditionalEdge.



32
33
34
35
36
# File 'lib/langgraph_rb/edge.rb', line 32

def initialize(from, router, path_map = nil)
  @from = from.to_sym
  @router = router
  @path_map = path_map || {}
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



30
31
32
# File 'lib/langgraph_rb/edge.rb', line 30

def from
  @from
end

#path_mapObject (readonly)

Returns the value of attribute path_map.



30
31
32
# File 'lib/langgraph_rb/edge.rb', line 30

def path_map
  @path_map
end

#routerObject (readonly)

Returns the value of attribute router.



30
31
32
# File 'lib/langgraph_rb/edge.rb', line 30

def router
  @router
end

Instance Method Details

#inspectObject



73
74
75
# File 'lib/langgraph_rb/edge.rb', line 73

def inspect
  "#<ConditionalEdge: #{to_s}>"
end

#route(state, context: nil) ⇒ Object

Route based on the router function result



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/langgraph_rb/edge.rb', line 39

def route(state, context: nil)
  result = case @router.arity
           when 0
             @router.call
           when 1
             @router.call(state)
           else
             @router.call(state, context)
           end

  # Convert result to destinations
  destinations = case result
                when Array
                  result
                when String, Symbol
                  [result]
                when Hash
                  # Support for multiple destinations with different states
                  result.keys
                else
                  [result]
                end

  # Map through path_map if provided
  destinations.map do |dest|
    mapped = @path_map[dest.to_s] || @path_map[dest.to_sym] || dest
    mapped.to_sym
  end
end

#to_sObject



69
70
71
# File 'lib/langgraph_rb/edge.rb', line 69

def to_s
  "#{@from} -> [conditional]"
end