Class: Agentic::Agent

Inherits:
Object
  • Object
show all
Includes:
FactoryMethods
Defined in:
lib/agentic/agent.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FactoryMethods

included

Constructor Details

#initializeAgent

Initialize with default values



13
14
15
16
17
# File 'lib/agentic/agent.rb', line 13

def initialize
  @capabilities = {}
  @tools = Set.new
  @llm_client = nil
end

Class Method Details

.from_h(hash) ⇒ Agent

Creates an agent from a hash representation

Parameters:

  • hash (Hash)

    The hash representation

Returns:



125
126
127
128
129
130
131
132
133
134
# File 'lib/agentic/agent.rb', line 125

def self.from_h(hash)
  new do |a|
    a.role = hash[:role] || hash["role"]
    a.purpose = hash[:purpose] || hash["purpose"]
    a.backstory = hash[:backstory] || hash["backstory"]
  end

  # Note: Capabilities need to be added separately after creation
  # since they require the registry to be available
end

Instance Method Details

#add_capability(capability_name, version = nil) ⇒ Boolean

Adds a capability to the agent

Parameters:

  • capability_name (String)

    The name of the capability

  • version (String, nil) (defaults to: nil)

    The version of the capability, or nil for latest

Returns:

  • (Boolean)

    True if the capability was added successfully



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/agentic/agent.rb', line 60

def add_capability(capability_name, version = nil)
  # Get the capability from the registry
  registry = AgentCapabilityRegistry.instance
  capability = registry.get(capability_name, version)
  raise "Capability not found: #{capability_name}" unless capability

  # Get the provider
  provider = registry.get_provider(capability_name, version)
  raise "Provider not found for capability: #{capability_name}" unless provider

  # Add to the agent's capabilities
  @capabilities[capability_name] = {
    specification: capability,
    provider: provider
  }

  # Also add to tools for backward compatibility
  @tools.add(capability_name.to_sym)

  true
end

#capability_specification(capability_name) ⇒ CapabilitySpecification?

Gets the specification for a capability

Parameters:

  • capability_name (String)

    The name of the capability

Returns:



92
93
94
95
# File 'lib/agentic/agent.rb', line 92

def capability_specification(capability_name)
  return nil unless @capabilities[capability_name]
  @capabilities[capability_name][:specification]
end

#execute(task) ⇒ Object

Executes a task using this agent

Parameters:

  • task (String, Task)

    The task to execute, either a task object or a prompt

Returns:

  • (Object)

    The result of the task execution



22
23
24
25
26
27
28
29
30
# File 'lib/agentic/agent.rb', line 22

def execute(task)
  if task.is_a?(String)
    # Simple string prompt
    execute_prompt(task)
  else
    # Task object
    task.perform(self)
  end
end

#execute_capability(capability_name, inputs = {}) ⇒ Hash

Executes a capability

Parameters:

  • capability_name (String)

    The name of the capability

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

    The inputs for the capability

Returns:

  • (Hash)

    The outputs from the capability



101
102
103
104
105
106
107
108
109
# File 'lib/agentic/agent.rb', line 101

def execute_capability(capability_name, inputs = {})
  raise "Capability not available: #{capability_name}" unless @capabilities.key?(capability_name)

  # Get the provider
  provider = @capabilities[capability_name][:provider]

  # Execute the capability
  provider.execute(inputs)
end

#execute_with_schema(prompt, schema) ⇒ Object

Executes a prompt with structured output schema

Parameters:

Returns:

  • (Object)

    The structured response



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/agentic/agent.rb', line 36

def execute_with_schema(prompt, schema)
  # If the agent has a text_generation capability, use it
  if has_capability?("text_generation")
    # For now, text_generation capabilities don't support schemas
    # Fall back to regular execution
    execute_capability("text_generation", {prompt: prompt})[:response]
  elsif @llm_client
    # Use the configured LLM client with structured output
    response = @llm_client.complete(build_messages(prompt), output_schema: schema)
    if response.successful?
      response.content
    else
      raise "LLM execution failed: #{response.error.message}"
    end
  else
    # Fallback error - agent not properly configured
    raise "Agent not configured with LLM capabilities. Use DefaultAgentProvider or configure llm_client directly."
  end
end

#has_capability?(capability_name) ⇒ Boolean

Checks if the agent has a capability

Parameters:

  • capability_name (String)

    The name of the capability

Returns:

  • (Boolean)

    True if the agent has the capability



85
86
87
# File 'lib/agentic/agent.rb', line 85

def has_capability?(capability_name)
  @capabilities.key?(capability_name)
end

#to_hHash

Converts the agent to a hash representation

Returns:

  • (Hash)

    The hash representation



113
114
115
116
117
118
119
120
# File 'lib/agentic/agent.rb', line 113

def to_h
  {
    role: @role,
    purpose: @purpose,
    backstory: @backstory,
    capability_names: @capabilities.keys
  }
end