Class: ActiveAgent::Dashboard::AgentRun

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

Overview

Tracks individual agent execution runs.

Each run captures input, output, timing, token usage, and any errors that occurred during execution.

Examples:

Creating a run

run = agent.execute("Analyze this code", code: code)
run.status # => "pending"

Monitoring a run

run.in_progress? # => true
run.finished?    # => false

Instance Method Summary collapse

Methods inherited from ApplicationRecord

for_owner, owner_association, table_name

Instance Method Details

#add_log(message, level: :info) ⇒ Object

Add a log entry



39
40
41
42
43
44
45
46
47
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 39

def add_log(message, level: :info)
  new_logs = logs || []
  new_logs << {
    timestamp: Time.current.iso8601,
    level: level.to_s,
    message: message
  }
  update!(logs: new_logs)
end

#broadcast_updateObject

Stream output updates via ActionCable



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 82

def broadcast_update
  return unless defined?(ActionCable)

  ActionCable.server.broadcast(
    "agent_run_#{id}",
    {
      type: "update",
      run: summary
    }
  )
end

#calculated_duration_msObject

Calculate duration if not set



50
51
52
53
54
55
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 50

def calculated_duration_ms
  return duration_ms if duration_ms.present?
  return nil unless started_at && completed_at

  ((completed_at - started_at) * 1000).to_i
end

#cancel!Object

Cancel a running execution



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

def cancel!
  return unless in_progress?

  update!(
    status: :cancelled,
    completed_at: Time.current,
    error_message: "Cancelled by user"
  )
  broadcast_update
end

#finished?Boolean

Check if run is finished

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 63

def finished?
  complete? || failed? || cancelled?
end

#in_progress?Boolean

Check if run is still in progress

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 58

def in_progress?
  pending? || running?
end

#summaryObject

Get a summary for display



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb', line 68

def summary
  {
    id: id,
    status: status,
    input_preview: input_prompt&.truncate(100),
    output_preview: output&.truncate(200),
    duration_ms: calculated_duration_ms,
    tokens: total_tokens,
    created_at: created_at,
    error: error_message
  }
end