Class: RailsAgents::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_agents/base.rb

Overview

Prefer product-facing typed subclasses: KnowledgeAgent, WorkflowAgent, OperationsAgent, or MonitoringAgent.

Defined Under Namespace

Classes: RunResult

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Base

Returns a new instance of Base.



125
126
127
# File 'lib/rails_agents/base.rb', line 125

def initialize(client: nil)
  @client = client || Client.new
end

Class Attribute Details

.agent_nameObject (readonly)

Returns the value of attribute agent_name.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def agent_name
  @agent_name
end

.channel_definitionsObject (readonly)

Returns the value of attribute channel_definitions.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def channel_definitions
  @channel_definitions
end

.knowledge_globObject (readonly)

Returns the value of attribute knowledge_glob.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def knowledge_glob
  @knowledge_glob
end

.memory_providerObject (readonly)

Returns the value of attribute memory_provider.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def memory_provider
  @memory_provider
end

.memory_recallObject (readonly)

Returns the value of attribute memory_recall.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def memory_recall
  @memory_recall
end

.memory_settingObject (readonly)

Returns the value of attribute memory_setting.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def memory_setting
  @memory_setting
end

.model_credentialObject (readonly)

Returns the value of attribute model_credential.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def model_credential
  @model_credential
end

.model_providerObject (readonly)

Returns the value of attribute model_provider.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def model_provider
  @model_provider
end

.model_settingObject (readonly)

Returns the value of attribute model_setting.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def model_setting
  @model_setting
end

.skill_definitionsObject (readonly)

Returns the value of attribute skill_definitions.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def skill_definitions
  @skill_definitions
end

.tool_definitionsObject (readonly)

Returns the value of attribute tool_definitions.



11
12
13
# File 'lib/rails_agents/base.rb', line 11

def tool_definitions
  @tool_definitions
end

Class Method Details

.agent_directoryObject



86
87
88
# File 'lib/rails_agents/base.rb', line 86

def agent_directory
  @agent_directory ||= resolve_agent_directory
end

.agent_kindObject



15
16
17
# File 'lib/rails_agents/base.rb', line 15

def agent_kind
  :base
end

.channel(kind, via: nil) ⇒ Object



65
66
67
68
69
70
# File 'lib/rails_agents/base.rb', line 65

def channel(kind, via: nil)
  @channel_definitions ||= []
  @channel_definitions << kind.to_sym
  @channel_via ||= {}
  @channel_via[kind.to_sym] = via.to_sym if via
end

.channel_message_handlerObject



82
83
84
# File 'lib/rails_agents/base.rb', line 82

def channel_message_handler
  @channel_message_handler
end

.channel_via(kind) ⇒ Object



72
73
74
# File 'lib/rails_agents/base.rb', line 72

def channel_via(kind)
  (@channel_via || {})[kind.to_sym]
end

.coerce_run_message(message, inputs) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rails_agents/base.rb', line 103

def coerce_run_message(message, inputs)
  if inputs.any?
    if !message.nil?
      raise ArgumentError, "pass either a message string or keyword inputs, not both"
    end

    inputs.map { |key, value| "#{key}: #{value}" }.join("\n")
  elsif message.nil?
    raise ArgumentError, "message is required (or pass keyword inputs like order_id:)"
  else
    message.to_s
  end
end

.connector(name, **opts) ⇒ Object Also known as: plugin

Declares a SaaS connector (manifest lives under connectors/). plugin remains as a deprecated alias.



59
60
61
62
# File 'lib/rails_agents/base.rb', line 59

def connector(name, **opts)
  @connector_definitions ||= {}
  @connector_definitions[name.to_sym] = opts
end

.deploy(client: nil) ⇒ Object



95
96
97
# File 'lib/rails_agents/base.rb', line 95

def deploy(client: nil)
  new(client: client).deploy
end

.inherited(subclass) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rails_agents/base.rb', line 19

def inherited(subclass)
  super
  subclass.instance_variable_set(:@tool_definitions, {})
  subclass.instance_variable_set(:@skill_definitions, {})
  subclass.instance_variable_set(:@channel_definitions, [])
  subclass.instance_variable_set(:@channel_via, {})
  subclass.instance_variable_set(:@channel_message_handler, nil)
end

.knowledge_from(glob) ⇒ Object



43
44
45
# File 'lib/rails_agents/base.rb', line 43

def knowledge_from(glob)
  @knowledge_glob = glob
end

.memory(value, provider: nil, recall: nil) ⇒ Object

memory :conversation, provider: :mem0, recall: 5



37
38
39
40
41
# File 'lib/rails_agents/base.rb', line 37

def memory(value, provider: nil, recall: nil)
  @memory_setting = value
  @memory_provider = provider
  @memory_recall = recall
end

.model(value, provider: nil, credential: nil) ⇒ Object

model :gpt_5_mini, provider: :openai, credential: :company_openai model :auto # legacy cloud-routed default (still supported)



30
31
32
33
34
# File 'lib/rails_agents/base.rb', line 30

def model(value, provider: nil, credential: nil)
  @model_setting = value
  @model_provider = provider
  @model_credential = credential
end

.on_channel_message(&block) ⇒ Object

Callback for Open-Wire (and future channel) inbound messages. on_channel_message { |msg| … }



78
79
80
# File 'lib/rails_agents/base.rb', line 78

def on_channel_message(&block)
  @channel_message_handler = block
end

.resolve_agent_directoryObject



118
119
120
121
# File 'lib/rails_agents/base.rb', line 118

def resolve_agent_directory
  caller_path = Pathname.new(caller_locations(1, 1).first.absolute_path)
  caller_path.dirname
end

.run(message = nil, session_id: nil, client: nil, **inputs) ⇒ Object



90
91
92
93
# File 'lib/rails_agents/base.rb', line 90

def run(message = nil, session_id: nil, client: nil, **inputs)
  text = coerce_run_message(message, inputs)
  new(client: client).run(text, session_id: session_id)
end

.skill(name, from:) ⇒ Object



52
53
54
55
# File 'lib/rails_agents/base.rb', line 52

def skill(name, from:)
  @skill_definitions ||= {}
  @skill_definitions[name.to_sym] = from
end

.sync_knowledge(client: nil) ⇒ Object



99
100
101
# File 'lib/rails_agents/base.rb', line 99

def sync_knowledge(client: nil)
  new(client: client).sync_knowledge
end

.tool(name, &block) ⇒ Object



47
48
49
50
# File 'lib/rails_agents/base.rb', line 47

def tool(name, &block)
  @tool_definitions ||= {}
  @tool_definitions[name.to_sym] = block
end

Instance Method Details

#agent_identifierObject



193
194
195
196
197
198
199
# File 'lib/rails_agents/base.rb', line 193

def agent_identifier
  if self.class.name && !self.class.name.empty?
    self.class.name.underscore
  else
    self.class.agent_directory.basename.to_s
  end
end

#agent_metadataObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/rails_agents/base.rb', line 150

def 
  {
    kind: self.class.agent_kind,
    model: ,
    memory: ,
    tools: self.class.tool_definitions&.keys || [],
    skills: self.class.skill_definitions || {},
    channels: self.class.channel_definitions || []
  }
end

#deployObject



141
142
143
# File 'lib/rails_agents/base.rb', line 141

def deploy
  @client.deploy(agent: agent_identifier, bundle_path: self.class.agent_directory.to_s)
end

#knowledge_pathsObject



201
202
203
204
205
206
207
208
209
210
# File 'lib/rails_agents/base.rb', line 201

def knowledge_paths
  glob = self.class.knowledge_glob
  return [] unless glob

  dir = self.class.agent_directory
  local = Dir.glob(dir.join(glob).to_s).map do |path|
    Pathname.new(path).relative_path_from(dir).to_s
  end
  (local + LibraryImports.new(dir).knowledge_paths).uniq
end

#memory_metadataObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rails_agents/base.rb', line 177

def 
  setting = self.class.memory_setting
  provider = self.class.memory_provider
  recall = self.class.memory_recall

  if provider.nil? && recall.nil?
    setting
  else
    {
      type: setting,
      provider: provider,
      recall: recall
    }.compact
  end
end

#model_metadataObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rails_agents/base.rb', line 161

def 
  setting = self.class.model_setting
  provider = self.class.model_provider
  credential = self.class.model_credential

  if provider.nil? && credential.nil?
    setting
  else
    {
      name: setting,
      provider: provider,
      credential: credential
    }.compact
  end
end

#run(message = nil, session_id: nil, **inputs) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rails_agents/base.rb', line 129

def run(message = nil, session_id: nil, **inputs)
  text = self.class.send(:coerce_run_message, message, inputs)
  response = @client.create_run(
    agent: agent_identifier,
    message: text,
    session_id: session_id,
    metadata: 
  )

  RunResult.new(response, client: @client)
end

#sync_knowledgeObject



145
146
147
148
# File 'lib/rails_agents/base.rb', line 145

def sync_knowledge
  paths = knowledge_paths
  @client.sync_knowledge(agent: agent_identifier, paths: paths)
end