Module: Soka::Agents::DSLMethods::ClassMethods

Defined in:
lib/soka/agents/dsl_methods.rb

Overview

Class methods for DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_api_keyObject

Returns the value of attribute _api_key.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _api_key
  @_api_key
end

#_hooksObject

Returns the value of attribute _hooks.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _hooks
  @_hooks
end

#_instructionsObject

Returns the value of attribute _instructions.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _instructions
  @_instructions
end

#_max_iterationsObject

Returns the value of attribute _max_iterations.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _max_iterations
  @_max_iterations
end

#_modelObject

Returns the value of attribute _model.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _model
  @_model
end

#_providerObject

Returns the value of attribute _provider.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _provider
  @_provider
end

#_retry_configObject

Returns the value of attribute _retry_config.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _retry_config
  @_retry_config
end

#_think_inObject

Returns the value of attribute _think_in.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _think_in
  @_think_in
end

#_toolsObject

Returns the value of attribute _tools.



13
14
15
# File 'lib/soka/agents/dsl_methods.rb', line 13

def _tools
  @_tools
end

Instance Method Details

#after_action(method_name) ⇒ Object

Register after_action hook

Parameters:

  • method_name (Symbol)

    The method to call after action



100
101
102
# File 'lib/soka/agents/dsl_methods.rb', line 100

def after_action(method_name)
  @_hooks[:after_action] << method_name
end

#api_key(key) ⇒ Object

Define API key for the agent

Parameters:

  • key (String)

    The API key



37
38
39
# File 'lib/soka/agents/dsl_methods.rb', line 37

def api_key(key)
  @_api_key = key
end

#before_action(method_name) ⇒ Object

Register before_action hook

Parameters:

  • method_name (Symbol)

    The method to call before action



94
95
96
# File 'lib/soka/agents/dsl_methods.rb', line 94

def before_action(method_name)
  @_hooks[:before_action] << method_name
end

#inherited(subclass) ⇒ Object



16
17
18
19
20
21
# File 'lib/soka/agents/dsl_methods.rb', line 16

def inherited(subclass)
  super
  subclass._tools = []
  subclass._hooks = { before_action: [], after_action: [], on_error: [] }
  subclass._retry_config = {}
end

#instructions(text_or_method) ⇒ Object

Define custom instructions (system prompt) for the agent

Examples:

Using a string

instructions "You are a helpful assistant"

Using a method

instructions :generate_instructions

Parameters:

  • text_or_method (String, Symbol)

    The custom instructions/system prompt or method name



53
54
55
# File 'lib/soka/agents/dsl_methods.rb', line 53

def instructions(text_or_method)
  @_instructions = text_or_method
end

#max_iterations(num) ⇒ Object

Define maximum iterations for the agent

Parameters:

  • num (Integer)

    The maximum number of iterations



43
44
45
# File 'lib/soka/agents/dsl_methods.rb', line 43

def max_iterations(num)
  @_max_iterations = num
end

#model(model) ⇒ Object

Define model for the agent

Parameters:

  • model (String)

    The model name (e.g., ‘gemini-1.5-pro’)



31
32
33
# File 'lib/soka/agents/dsl_methods.rb', line 31

def model(model)
  @_model = model
end

#on_error(method_name) ⇒ Object

Register on_error hook

Parameters:

  • method_name (Symbol)

    The method to call on error



106
107
108
# File 'lib/soka/agents/dsl_methods.rb', line 106

def on_error(method_name)
  @_hooks[:on_error] << method_name
end

#provider(provider) ⇒ Object

Define provider for the agent

Parameters:

  • provider (Symbol)

    The LLM provider (:gemini, :openai, :anthropic)



25
26
27
# File 'lib/soka/agents/dsl_methods.rb', line 25

def provider(provider)
  @_provider = provider
end

#retry_config { ... } ⇒ Object

Configure retry behavior

Yields:

  • Configuration block



86
87
88
89
90
# File 'lib/soka/agents/dsl_methods.rb', line 86

def retry_config(&)
  config = Agents::RetryConfig.new
  config.instance_eval(&)
  @_retry_config = config.to_h
end

#think_in(language) ⇒ Object

Define thinking language for the agent

Parameters:

  • language (String)

    The language code (e.g., ‘zh-TW’, ‘ja-JP’, ‘en’)



59
60
61
# File 'lib/soka/agents/dsl_methods.rb', line 59

def think_in(language)
  @_think_in = language.to_s
end

#tool(tool_class_or_name, description_or_options = nil, options = {}) ⇒ Object

Register a tool for the agent

Parameters:

  • tool_class_or_name (Class, Symbol, String)

    The tool class or method name

  • description_or_options (String, Hash, nil) (defaults to: nil)

    Description (for function tools) or options

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

    Additional options (if description provided)



67
68
69
70
71
72
73
74
75
76
# File 'lib/soka/agents/dsl_methods.rb', line 67

def tool(tool_class_or_name, description_or_options = nil, options = {})
  if tool_class_or_name.is_a?(Symbol) || tool_class_or_name.is_a?(String)
    # Function tool - expects description and options
    add_function_tool(tool_class_or_name, description_or_options, options)
  else
    # Class tool - second parameter is options
    opts = description_or_options.is_a?(Hash) ? description_or_options : options
    add_class_tool(tool_class_or_name, opts)
  end
end

#tools(*tool_classes) ⇒ Object

Register multiple tools at once

Parameters:

  • tool_classes (Array<Class>)

    The tool classes to register



80
81
82
# File 'lib/soka/agents/dsl_methods.rb', line 80

def tools(*tool_classes)
  tool_classes.each { |tool_class| tool(tool_class) }
end