Class: ActiveAgent::Dashboard::SessionRecording

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb

Overview

Records browser sessions for playback and analysis.

Session recordings capture the sequence of actions taken during an agent run or sandbox session, including screenshots, DOM snapshots, and timing information.

Examples:

Starting a recording

recording = ActiveAgent::Dashboard::SessionRecording.start!(
  agent_run: run,
  name: "checkout_flow"
)

Recording an action

recording.record_action!(
  action_type: "click",
  selector: "button.submit",
  screenshot: screenshot_data
)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

for_owner, owner_association, table_name

Class Method Details

.generate_name(agent_run, sandbox_session) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 154

def self.generate_name(agent_run, sandbox_session)
  prefix = if agent_run&.agent
    agent_run.agent.name.parameterize
  elsif sandbox_session&.agent_template
    sandbox_session.agent_template.name.parameterize
  else
    "session"
  end

  "#{prefix}_#{Time.current.strftime('%Y%m%d_%H%M%S')}"
end

.start!(agent_run: nil, sandbox_session: nil, name: nil) ⇒ Object

Start a new recording session



52
53
54
55
56
57
58
59
60
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 52

def self.start!(agent_run: nil, sandbox_session: nil, name: nil)
  create!(
    agent_run: agent_run,
    sandbox_session: sandbox_session,
    name: name || generate_name(agent_run, sandbox_session),
    status: :recording,
    metadata: { started_at: Time.current.iso8601 }
  )
end

.start_user_session!(visitor_id: nil, parent_demo_id: nil, page_url: nil) ⇒ Object

Start a user takeover session (for lander demo analytics)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 63

def self.start_user_session!(visitor_id: nil, parent_demo_id: nil, page_url: nil)
  create!(
    name: "user_takeover_#{Time.current.strftime('%Y%m%d_%H%M%S')}_#{SecureRandom.hex(4)}",
    status: :recording,
    metadata: {
      started_at: Time.current.iso8601,
      session_type: "user_takeover",
      visitor_id: visitor_id,
      parent_demo_id: parent_demo_id,
      page_url: page_url,
      user_agent: nil
    }
  )
end

Instance Method Details

#complete!Object

Complete the recording



106
107
108
109
110
111
112
113
114
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 106

def complete!
  return unless recording?

  update!(
    status: :completed,
    duration_ms: elapsed_ms,
    metadata: .merge(completed_at: Time.current.iso8601)
  )
end

#demo_recording?Boolean

Check if this is a demo recording (doesn’t require parent)

Returns:

  • (Boolean)


42
43
44
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 42

def demo_recording?
  name&.start_with?("lander_") || name == "demo"
end

#fail!(error_message = nil) ⇒ Object

Mark recording as failed



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 117

def fail!(error_message = nil)
  return unless recording?

  update!(
    status: :failed,
    duration_ms: elapsed_ms,
    metadata: .merge(
      failed_at: Time.current.iso8601,
      error: error_message
    )
  )
end

#record_action!(action_type:, selector: nil, value: nil, screenshot: nil, dom_snapshot: nil, metadata: {}) ⇒ Object

Record a browser action



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 79

def record_action!(action_type:, selector: nil, value: nil, screenshot: nil, dom_snapshot: nil, metadata: {})
  raise "Recording already completed" unless recording?

  action = recording_actions.create!(
    action_type: action_type,
    sequence: next_sequence,
    timestamp_ms: elapsed_ms,
    selector: selector,
    value: value,
    metadata: 
  )

  if screenshot.present?
    snapshot = store_snapshot(screenshot, :screenshot, action)
    action.update!(screenshot_key: snapshot.storage_key)
  end

  if dom_snapshot.present?
    snapshot = store_snapshot(dom_snapshot, :dom, action)
    action.update!(dom_snapshot_key: snapshot.storage_key)
  end

  increment!(:action_count)
  action
end

#timelineObject

Get timeline data for playback



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

def timeline
  recording_actions.order(:sequence).map do |action|
    {
      id: action.id,
      type: action.action_type,
      sequence: action.sequence,
      timestamp_ms: action.timestamp_ms,
      selector: action.selector,
      value: action.value,
      screenshot_key: action.screenshot_key,
      metadata: action.
    }
  end
end

#user_session?Boolean

Check if this is a user takeover session (doesn’t require parent)

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb', line 47

def user_session?
  name&.start_with?("user_takeover_")
end