Class: ActiveAgent::Dashboard::Agent

Inherits:
ApplicationRecord show all
Defined in:
lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb

Overview

Represents an AI agent configuration.

Agents are the core entity in the dashboard, storing all configuration needed to execute AI interactions including provider settings, instructions, tools, and appearance.

Supports both local (single-user) and multi-tenant (account-scoped) modes.

Examples:

Creating an agent

agent = ActiveAgent::Dashboard::Agent.create!(
  name: "Code Assistant",
  provider: "openai",
  model: "gpt-4o"
)

Executing an agent

run = agent.execute("Explain this code", code: "def foo; end")

Constant Summary collapse

PRESET_TYPES =

Available presets matching AgentAvatar component

%w[
  terminal webDeveloper documentAnalysis writing translation
  playwright research imageAnalysis computerUse productDesign
].freeze
INSTRUCTION_SETS =

Available instruction sets

%w[
  github ruby rails aws gcp python typescript docker kubernetes
].freeze
AVAILABLE_TOOLS =

Available tools/MCPs

%w[
  terminal playwright filesystem code database slack fetch search edit translate memory
].freeze
PROVIDERS =

Available providers

%w[openai anthropic ollama openrouter].freeze

Instance Method Summary collapse

Methods inherited from ApplicationRecord

for_owner, owner_association, table_name

Instance Method Details

#configuration_snapshotObject

Returns the configuration as a hash for versioning



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 77

def configuration_snapshot
  {
    name: name,
    description: description,
    provider: provider,
    model: model,
    instructions: instructions,
    preset_type: preset_type,
    appearance: appearance,
    instruction_sets: instruction_sets,
    tools: tools,
    mcp_servers: mcp_servers,
    model_config: model_config,
    response_format: response_format
  }
end

#execute(input_prompt, **params) ⇒ Object

Execute a run with this agent



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 133

def execute(input_prompt, **params)
  run = agent_runs.create!(
    input_prompt: input_prompt,
    input_params: params,
    status: :pending,
    trace_id: SecureRandom.uuid
  )

  # Queue the execution job
  ActiveAgent::Dashboard::AgentExecutionJob.perform_later(run.id)

  run
end

#latest_versionObject

Get the latest version



110
111
112
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 110

def latest_version
  agent_versions.order(version_number: :desc).first
end

#restore_from_version!(version) ⇒ Object

Restore from a version



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 95

def restore_from_version!(version)
  config = version.configuration_snapshot
  update!(
    instructions: config["instructions"],
    preset_type: config["preset_type"],
    appearance: config["appearance"],
    instruction_sets: config["instruction_sets"],
    tools: config["tools"],
    mcp_servers: config["mcp_servers"],
    model_config: config["model_config"],
    response_format: config["response_format"]
  )
end

#test_execute(input_prompt, **params) ⇒ Object

Quick test execution (synchronous)



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 148

def test_execute(input_prompt, **params)
  run = agent_runs.create!(
    input_prompt: input_prompt,
    input_params: params,
    status: :running,
    trace_id: SecureRandom.uuid,
    started_at: Time.current
  )

  begin
    result = build_and_execute_agent(input_prompt, **params)

    run.update!(
      output: result[:output],
      output_metadata: result[:metadata],
      status: :complete,
      completed_at: Time.current,
      duration_ms: ((Time.current - run.started_at) * 1000).to_i,
      input_tokens: result.dig(:usage, :input_tokens),
      output_tokens: result.dig(:usage, :output_tokens),
      total_tokens: result.dig(:usage, :total_tokens)
    )
  rescue => e
    run.update!(
      status: :failed,
      completed_at: Time.current,
      error_message: e.message,
      error_backtrace: e.backtrace&.first(10)&.join("\n")
    )
  end

  run
end

#to_agent_class_codeObject

Generate Ruby agent class code



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 120

def to_agent_class_code
  <<~RUBY
    class #{agent_class_name || name.camelize}Agent < ApplicationAgent
      generate_with :#{provider}, model: "#{model}"#{model_config_code}

      def perform
        prompt#{instructions_code}
      end
    end
  RUBY
end

#version_countObject

Get version count



115
116
117
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb', line 115

def version_count
  agent_versions.count
end