Class: LangGraphRB::Node

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

Direct Known Subclasses

LLMNode, ToolNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, callable = nil, &block) ⇒ Node

Returns a new instance of Node.

Raises:



5
6
7
8
9
10
# File 'lib/langgraph_rb/node.rb', line 5

def initialize(name, callable = nil, &block)
  @name = name.to_sym
  @callable = callable || block
  
  raise NodeError, "Node '#{name}' must have a callable or block" unless @callable
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/langgraph_rb/node.rb', line 3

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/langgraph_rb/node.rb', line 3

def name
  @name
end

Instance Method Details

#call(state, context: nil) ⇒ Object

Execute the node with the given state and context Returns either a Hash (state delta), Command, or Send object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/langgraph_rb/node.rb', line 14

def call(state, context: nil)
  case @callable.arity
  when 0
    @callable.call
  when 1
    @callable.call(state)
  else
    @callable.call(state, context)
  end
rescue => e
  raise NodeError, "Error executing node '#{@name}': #{e.message}"
end

#inspectObject



31
32
33
# File 'lib/langgraph_rb/node.rb', line 31

def inspect
  to_s
end

#to_sObject



27
28
29
# File 'lib/langgraph_rb/node.rb', line 27

def to_s
  "#<Node:#{@name}>"
end