Class: Agents::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/actions/action.rb

Overview

Takes a block and executes it when the action is called.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: "", arguments: [], examples: [], &block) ⇒ Action

Create a new action

Parameters:

  • name (String)
  • description (String) (defaults to: "")
  • arguments (Array<ActionArgument>) (defaults to: [])
  • examples (Array<ActionExample>) (defaults to: [])
  • block (Block)


13
14
15
16
17
18
19
# File 'lib/actions/action.rb', line 13

def initialize(name:, description: "", arguments: [], examples: [], &block)
  @name = name
  @description = description
  @arguments = arguments
  @examples = examples
  @block = block
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



5
6
7
# File 'lib/actions/action.rb', line 5

def arguments
  @arguments
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/actions/action.rb', line 5

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



5
6
7
# File 'lib/actions/action.rb', line 5

def examples
  @examples
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/actions/action.rb', line 5

def name
  @name
end

Instance Method Details

#call(args) ⇒ Object

Execute the block with the given arguments.



22
23
24
# File 'lib/actions/action.rb', line 22

def call(args)
  @block.call(args)
end

#for_promptObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/actions/action.rb', line 26

def for_prompt
  s = "#{name}: #{description}\n"

  unless arguments.empty?
    s << "  Arguments:\n"
    s << arguments.collect(&:for_prompt).join("\n") << "\n"
  end

  unless examples.empty?
    s << "  For example:\n"
    s << examples.collect(&:for_prompt).join("\n") << "\n"
  end

  s
end