activeagent 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -4
  3. data/lib/active_agent/base.rb +3 -2
  4. data/lib/active_agent/concerns/provider.rb +6 -2
  5. data/lib/active_agent/concerns/rescue.rb +39 -0
  6. data/lib/active_agent/concerns/streaming.rb +2 -1
  7. data/lib/active_agent/dashboard/app/controllers/active_agent/dashboard/api/traces_controller.rb +117 -0
  8. data/lib/active_agent/dashboard/app/controllers/active_agent/dashboard/application_controller.rb +54 -0
  9. data/lib/active_agent/dashboard/app/controllers/active_agent/dashboard/dashboard_controller.rb +126 -0
  10. data/lib/active_agent/dashboard/app/controllers/active_agent/dashboard/traces_controller.rb +103 -0
  11. data/lib/active_agent/dashboard/app/jobs/active_agent/dashboard/agent_execution_job.rb +56 -0
  12. data/lib/active_agent/dashboard/app/jobs/active_agent/dashboard/application_job.rb +14 -0
  13. data/lib/active_agent/dashboard/app/jobs/active_agent/dashboard/sandbox_cleanup_job.rb +49 -0
  14. data/lib/active_agent/dashboard/app/jobs/active_agent/dashboard/sandbox_provision_job.rb +65 -0
  15. data/lib/active_agent/dashboard/app/jobs/active_agent/process_telemetry_traces_job.rb +77 -0
  16. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/agent.rb +256 -0
  17. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_run.rb +113 -0
  18. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_template.rb +208 -0
  19. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/agent_version.rb +60 -0
  20. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/application_record.rb +46 -0
  21. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/recording_action.rb +125 -0
  22. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/recording_snapshot.rb +83 -0
  23. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/sandbox_run.rb +52 -0
  24. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/sandbox_session.rb +169 -0
  25. data/lib/active_agent/dashboard/app/models/active_agent/dashboard/session_recording.rb +193 -0
  26. data/lib/active_agent/dashboard/app/models/active_agent/telemetry_trace.rb +198 -0
  27. data/lib/active_agent/dashboard/app/views/active_agent/dashboard/traces/_trace_detail.html.erb +105 -0
  28. data/lib/active_agent/dashboard/app/views/active_agent/dashboard/traces/index.html.erb +135 -0
  29. data/lib/active_agent/dashboard/app/views/active_agent/dashboard/traces/metrics.html.erb +143 -0
  30. data/lib/active_agent/dashboard/app/views/active_agent/dashboard/traces/show.html.erb +36 -0
  31. data/lib/active_agent/dashboard/app/views/layouts/active_agent/dashboard/application.html.erb +94 -0
  32. data/lib/active_agent/dashboard/config/routes.rb +78 -0
  33. data/lib/active_agent/dashboard/engine.rb +39 -0
  34. data/lib/active_agent/dashboard.rb +151 -0
  35. data/lib/active_agent/providers/_base_provider.rb +2 -1
  36. data/lib/active_agent/providers/anthropic_provider.rb +14 -4
  37. data/lib/active_agent/providers/azure/_types.rb +5 -0
  38. data/lib/active_agent/providers/azure/options.rb +111 -0
  39. data/lib/active_agent/providers/azure_open_ai_provider.rb +2 -0
  40. data/lib/active_agent/providers/azure_openai_provider.rb +2 -0
  41. data/lib/active_agent/providers/azure_provider.rb +133 -0
  42. data/lib/active_agent/providers/azureopenai_provider.rb +2 -0
  43. data/lib/active_agent/providers/bedrock/_types.rb +8 -0
  44. data/lib/active_agent/providers/bedrock/bearer_client.rb +109 -0
  45. data/lib/active_agent/providers/bedrock/options.rb +77 -0
  46. data/lib/active_agent/providers/bedrock_provider.rb +84 -0
  47. data/lib/active_agent/providers/common/messages/_types.rb +6 -2
  48. data/lib/active_agent/providers/concerns/exception_handler.rb +1 -0
  49. data/lib/active_agent/providers/gemini/_types.rb +19 -0
  50. data/lib/active_agent/providers/gemini/options.rb +41 -0
  51. data/lib/active_agent/providers/gemini_provider.rb +94 -0
  52. data/lib/active_agent/providers/open_ai/chat/transforms.rb +37 -1
  53. data/lib/active_agent/providers/open_ai/chat_provider.rb +2 -0
  54. data/lib/active_agent/providers/ruby_llm/_types.rb +77 -0
  55. data/lib/active_agent/providers/ruby_llm/embedding_request.rb +16 -0
  56. data/lib/active_agent/providers/ruby_llm/messages/_types.rb +109 -0
  57. data/lib/active_agent/providers/ruby_llm/messages/assistant.rb +27 -0
  58. data/lib/active_agent/providers/ruby_llm/messages/base.rb +48 -0
  59. data/lib/active_agent/providers/ruby_llm/messages/system.rb +18 -0
  60. data/lib/active_agent/providers/ruby_llm/messages/tool.rb +24 -0
  61. data/lib/active_agent/providers/ruby_llm/messages/user.rb +18 -0
  62. data/lib/active_agent/providers/ruby_llm/options.rb +28 -0
  63. data/lib/active_agent/providers/ruby_llm/request.rb +30 -0
  64. data/lib/active_agent/providers/ruby_llm/tool_proxy.rb +45 -0
  65. data/lib/active_agent/providers/ruby_llm_provider.rb +407 -0
  66. data/lib/active_agent/railtie.rb +32 -1
  67. data/lib/active_agent/telemetry/configuration.rb +213 -0
  68. data/lib/active_agent/telemetry/instrumentation.rb +155 -0
  69. data/lib/active_agent/telemetry/reporter.rb +176 -0
  70. data/lib/active_agent/telemetry/span.rb +267 -0
  71. data/lib/active_agent/telemetry/tracer.rb +184 -0
  72. data/lib/active_agent/telemetry.rb +162 -0
  73. data/lib/active_agent/version.rb +1 -1
  74. data/lib/active_agent.rb +2 -0
  75. data/lib/generators/active_agent/dashboard/install/install_generator.rb +96 -0
  76. data/lib/generators/active_agent/dashboard/install/templates/initializer.rb +89 -0
  77. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_agent_runs.rb +42 -0
  78. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_agent_templates.rb +38 -0
  79. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_agent_versions.rb +22 -0
  80. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_agents.rb +53 -0
  81. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_sandbox_runs.rb +28 -0
  82. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_sandbox_sessions.rb +43 -0
  83. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_session_recordings.rb +44 -0
  84. data/lib/generators/active_agent/dashboard/install/templates/migrations/create_active_agent_telemetry_traces.rb +56 -0
  85. data/lib/generators/active_agent/dashboard/install_generator.rb +64 -0
  86. data/lib/generators/active_agent/dashboard/templates/active_agent_dashboard.rb.erb +30 -0
  87. data/lib/generators/active_agent/dashboard/templates/create_active_agent_telemetry_traces.rb.erb +30 -0
  88. metadata +99 -13
@@ -0,0 +1,208 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Pre-built agent templates for quick agent creation.
6
+ #
7
+ # Templates provide starting configurations for common use cases like
8
+ # code assistance, research, writing, and browser automation.
9
+ #
10
+ # @example Creating an agent from a template
11
+ # template = ActiveAgent::Dashboard::AgentTemplate.find_by(slug: "code-assistant")
12
+ # agent = template.create_agent_for(user)
13
+ #
14
+ class AgentTemplate < ApplicationRecord
15
+ # Validations
16
+ validates :name, presence: true
17
+ validates :slug, presence: true, uniqueness: true
18
+ validates :category, presence: true
19
+
20
+ # Scopes
21
+ scope :featured, -> { where(featured: true) }
22
+ scope :by_category, ->(cat) { where(category: cat) }
23
+ scope :popular, -> { order(usage_count: :desc) }
24
+ scope :public_templates, -> { where(public: true) }
25
+ scope :free_tier, -> { where(free_tier: true) }
26
+
27
+ # Categories
28
+ CATEGORIES = %w[
29
+ productivity
30
+ development
31
+ research
32
+ creative
33
+ data
34
+ automation
35
+ ].freeze
36
+
37
+ # Create an agent from this template for a user/account
38
+ def create_agent_for(owner, name: nil)
39
+ agent_class = ActiveAgent::Dashboard::Agent
40
+
41
+ agent = agent_class.new(
42
+ name: name || self.name,
43
+ description: description,
44
+ provider: provider,
45
+ model: model,
46
+ instructions: instructions,
47
+ preset_type: preset_type,
48
+ appearance: appearance,
49
+ instruction_sets: instruction_sets,
50
+ tools: tools,
51
+ mcp_servers: mcp_servers,
52
+ model_config: model_config,
53
+ status: :draft
54
+ )
55
+
56
+ # Set owner based on mode
57
+ if ActiveAgent::Dashboard.multi_tenant? && owner.respond_to?(:id)
58
+ agent.account = owner
59
+ elsif owner.respond_to?(:id)
60
+ agent.user = owner if agent.respond_to?(:user=)
61
+ end
62
+
63
+ if agent.save
64
+ increment!(:usage_count)
65
+ end
66
+
67
+ agent
68
+ end
69
+
70
+ # Seed default templates
71
+ def self.seed_defaults!
72
+ templates = [
73
+ {
74
+ name: "Code Assistant",
75
+ slug: "code-assistant",
76
+ description: "A helpful coding assistant that can explain code, suggest improvements, and help debug issues.",
77
+ category: "development",
78
+ provider: "openai",
79
+ model: "gpt-4o",
80
+ preset_type: "terminal",
81
+ appearance: { hat: "fedora", heldItem: "terminal" },
82
+ instruction_sets: %w[github ruby rails typescript],
83
+ tools: %w[terminal code filesystem],
84
+ model_config: { temperature: 0.3 },
85
+ instructions: "You are a senior software engineer with expertise in multiple programming languages. Help users with:\n- Code explanations and reviews\n- Debugging issues\n- Suggesting best practices\n- Writing tests\n\nAlways explain your reasoning and provide examples when helpful.",
86
+ icon: "💻",
87
+ featured: true,
88
+ free_tier: true
89
+ },
90
+ {
91
+ name: "Research Assistant",
92
+ slug: "research-assistant",
93
+ description: "Helps research topics, summarize information, and organize findings.",
94
+ category: "research",
95
+ provider: "anthropic",
96
+ model: "claude-sonnet-4-20250514",
97
+ preset_type: "research",
98
+ appearance: { hat: "safari", heldItem: "magnifyingGlass" },
99
+ instruction_sets: %w[github python],
100
+ tools: %w[fetch search memory],
101
+ model_config: { temperature: 0.5 },
102
+ instructions: "You are a thorough research assistant. Help users by:\n- Searching for relevant information\n- Summarizing complex topics\n- Organizing findings into clear reports\n- Identifying key insights and patterns\n\nAlways cite sources when available and distinguish between facts and opinions.",
103
+ icon: "🔍",
104
+ featured: true,
105
+ free_tier: true
106
+ },
107
+ {
108
+ name: "Writing Assistant",
109
+ slug: "writing-assistant",
110
+ description: "Helps with writing, editing, and improving text content.",
111
+ category: "creative",
112
+ provider: "openai",
113
+ model: "gpt-4o",
114
+ preset_type: "writing",
115
+ appearance: { hat: "fedora", hatAccessory: "feather", heldItem: "scroll" },
116
+ instruction_sets: [],
117
+ tools: %w[edit translate],
118
+ model_config: { temperature: 0.7 },
119
+ instructions: "You are a skilled writer and editor. Help users with:\n- Writing and editing content\n- Improving clarity and flow\n- Adjusting tone for different audiences\n- Grammar and style corrections\n\nMaintain the author's voice while suggesting improvements.",
120
+ icon: "✍️",
121
+ featured: true,
122
+ free_tier: true
123
+ },
124
+ {
125
+ name: "Browser Automation",
126
+ slug: "browser-automation",
127
+ description: "Automates web browsing tasks like form filling, data extraction, and testing.",
128
+ category: "automation",
129
+ provider: "anthropic",
130
+ model: "claude-sonnet-4-20250514",
131
+ preset_type: "playwright",
132
+ appearance: { hat: "fedora", hatAccessory: "theaterMasks", heldItem: "browser" },
133
+ instruction_sets: %w[typescript],
134
+ tools: %w[playwright filesystem],
135
+ model_config: { temperature: 0.2 },
136
+ instructions: "You are a browser automation specialist. Help users by:\n- Navigating web pages\n- Filling out forms\n- Extracting data from websites\n- Testing web applications\n\nAlways wait for page loads and handle errors gracefully.",
137
+ icon: "🎭",
138
+ featured: false,
139
+ free_tier: true
140
+ },
141
+ {
142
+ name: "Data Analyst",
143
+ slug: "data-analyst",
144
+ description: "Analyzes data, creates visualizations, and provides insights.",
145
+ category: "data",
146
+ provider: "openai",
147
+ model: "gpt-4o",
148
+ preset_type: "documentAnalysis",
149
+ appearance: { hat: "fedora", heldItem: "document" },
150
+ instruction_sets: %w[python],
151
+ tools: %w[code database filesystem],
152
+ model_config: { temperature: 0.3 },
153
+ instructions: "You are a data analyst. Help users by:\n- Analyzing datasets\n- Creating visualizations\n- Finding patterns and insights\n- Generating reports\n\nExplain your methodology and provide clear interpretations of results.",
154
+ icon: "📊",
155
+ featured: true,
156
+ free_tier: true
157
+ },
158
+ {
159
+ name: "DevOps Assistant",
160
+ slug: "devops-assistant",
161
+ description: "Helps with infrastructure, deployments, and system administration.",
162
+ category: "development",
163
+ provider: "openai",
164
+ model: "gpt-4o",
165
+ preset_type: "terminal",
166
+ appearance: { hat: "fedora", heldItem: "terminal" },
167
+ instruction_sets: %w[docker kubernetes aws gcp],
168
+ tools: %w[terminal filesystem code],
169
+ model_config: { temperature: 0.2 },
170
+ instructions: "You are a DevOps engineer. Help users with:\n- Infrastructure setup and management\n- CI/CD pipeline configuration\n- Container orchestration\n- Cloud resource management\n\nAlways prioritize security and follow best practices.",
171
+ icon: "🚀",
172
+ featured: false,
173
+ free_tier: true
174
+ },
175
+ {
176
+ name: "PlaywrightMCP Demo",
177
+ slug: "playwright-mcp-demo",
178
+ description: "Free browser automation demo using Playwright MCP. Navigate sites, take screenshots, and extract content.",
179
+ category: "automation",
180
+ provider: "anthropic",
181
+ model: "claude-sonnet-4-20250514",
182
+ preset_type: "playwright",
183
+ appearance: { hat: "fedora", hatAccessory: "theaterMasks", heldItem: "browser" },
184
+ instruction_sets: [],
185
+ tools: %w[playwright],
186
+ mcp_servers: {
187
+ playwright: {
188
+ command: "npx",
189
+ args: [ "-y", "@anthropic/mcp-server-playwright" ]
190
+ }
191
+ },
192
+ model_config: { temperature: 0.2, max_tokens: 4096 },
193
+ instructions: "You are a browser automation assistant using Playwright MCP.\n\nAvailable actions:\n- browser_navigate: Go to a URL\n- browser_snapshot: Get the accessibility tree\n- browser_click: Click on an element\n- browser_type: Type text into an input\n- browser_take_screenshot: Capture the page\n- browser_wait_for: Wait for text or element\n\nGuidelines:\n1. Always take a snapshot first to understand the page\n2. Use element refs from snapshots for interactions\n3. Wait for page loads before taking actions\n4. Handle errors gracefully\n5. Limit yourself to 10 steps maximum\n\nAlways describe what you see and what actions you're taking.",
194
+ icon: "🎭",
195
+ featured: true,
196
+ free_tier: true
197
+ }
198
+ ]
199
+
200
+ templates.each do |template_attrs|
201
+ find_or_create_by!(slug: template_attrs[:slug]) do |t|
202
+ t.assign_attributes(template_attrs)
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Tracks version history for agent configurations.
6
+ #
7
+ # Each time an agent's configuration changes, a new version is created
8
+ # with a snapshot of the configuration at that point in time.
9
+ #
10
+ # @example Comparing versions
11
+ # v1 = agent.agent_versions.find_by(version_number: 1)
12
+ # v2 = agent.agent_versions.find_by(version_number: 2)
13
+ # changes = v2.diff(v1)
14
+ #
15
+ class AgentVersion < ApplicationRecord
16
+ belongs_to :agent, class_name: "ActiveAgent::Dashboard::Agent"
17
+
18
+ validates :version_number, presence: true, uniqueness: { scope: :agent_id }
19
+ validates :configuration_snapshot, presence: true
20
+
21
+ # Scopes
22
+ scope :recent, -> { order(version_number: :desc) }
23
+ scope :by_version, ->(num) { where(version_number: num) }
24
+
25
+ # Compare two versions
26
+ def diff(other_version)
27
+ return {} unless other_version
28
+
29
+ changes = {}
30
+ configuration_snapshot.each do |key, value|
31
+ other_value = other_version.configuration_snapshot[key]
32
+ if value != other_value
33
+ changes[key] = { from: other_value, to: value }
34
+ end
35
+ end
36
+ changes
37
+ end
38
+
39
+ # Get previous version
40
+ def previous
41
+ agent.agent_versions.where("version_number < ?", version_number).order(version_number: :desc).first
42
+ end
43
+
44
+ # Get next version
45
+ def next_version
46
+ agent.agent_versions.where("version_number > ?", version_number).order(version_number: :asc).first
47
+ end
48
+
49
+ # Check if this is the latest version
50
+ def latest?
51
+ agent.latest_version&.id == id
52
+ end
53
+
54
+ # Check if this is the initial version
55
+ def initial?
56
+ version_number == 1
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Base class for all Dashboard engine models.
6
+ #
7
+ # Provides multi-tenant support when configured, allowing the same models
8
+ # to work in both local (single-tenant) and platform (multi-tenant) modes.
9
+ class ApplicationRecord < ::ActiveRecord::Base
10
+ self.abstract_class = true
11
+
12
+ # Override table name calculation to use active_agent_ prefix
13
+ # without the "dashboard_" from the module namespace
14
+ def self.table_name
15
+ @table_name ||= "active_agent_#{name.demodulize.underscore.pluralize}"
16
+ end
17
+
18
+ class << self
19
+ # Returns the owner association name based on configuration.
20
+ # In multi-tenant mode, this returns :account.
21
+ # In local mode, this returns :user (optional).
22
+ def owner_association
23
+ if ActiveAgent::Dashboard.multi_tenant?
24
+ :account
25
+ else
26
+ :user
27
+ end
28
+ end
29
+
30
+ # Scopes records to the current owner (account or user).
31
+ # No-op in local mode without owner configuration.
32
+ def for_owner(owner)
33
+ return all if owner.nil?
34
+
35
+ if ActiveAgent::Dashboard.multi_tenant?
36
+ where(account: owner)
37
+ elsif column_names.include?("user_id")
38
+ where(user: owner)
39
+ else
40
+ all
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Represents a single browser action within a session recording.
6
+ #
7
+ # Actions capture user and agent interactions like clicks, typing,
8
+ # navigation, and form submissions.
9
+ #
10
+ class RecordingAction < ApplicationRecord
11
+ belongs_to :session_recording, class_name: "ActiveAgent::Dashboard::SessionRecording"
12
+ has_one :snapshot, class_name: "ActiveAgent::Dashboard::RecordingSnapshot", dependent: :nullify
13
+
14
+ ACTION_TYPES = %w[
15
+ navigate
16
+ click
17
+ type
18
+ scroll
19
+ snapshot
20
+ select
21
+ hover
22
+ drag
23
+ file_upload
24
+ dialog
25
+ evaluate
26
+ wait
27
+ form_fill
28
+ key_press
29
+ focus
30
+ submit
31
+ handoff
32
+ user_action
33
+ completion
34
+ ].freeze
35
+
36
+ validates :action_type, presence: true, inclusion: { in: ACTION_TYPES }
37
+ validates :sequence, presence: true, uniqueness: { scope: :session_recording_id }
38
+ validates :timestamp_ms, presence: true
39
+
40
+ scope :ordered, -> { order(:sequence) }
41
+ scope :with_screenshots, -> { where.not(screenshot_key: nil) }
42
+
43
+ # Get the screenshot URL (signed URL from storage)
44
+ def screenshot_url(expires_in: 15.minutes)
45
+ return nil unless screenshot_key.present?
46
+
47
+ ActiveAgent::Dashboard.storage_service&.signed_url_for(screenshot_key, expires_in: expires_in)
48
+ end
49
+
50
+ # Get the DOM snapshot content
51
+ def dom_snapshot_content
52
+ return nil unless dom_snapshot_key.present?
53
+
54
+ ActiveAgent::Dashboard.storage_service&.fetch_snapshot(dom_snapshot_key)
55
+ end
56
+
57
+ # Format for API response
58
+ def as_json_for_api
59
+ {
60
+ id: id,
61
+ action_type: action_type,
62
+ sequence: sequence,
63
+ timestamp_ms: timestamp_ms,
64
+ selector: selector,
65
+ value: redacted_value,
66
+ screenshot_url: screenshot_url,
67
+ has_dom_snapshot: dom_snapshot_key.present?,
68
+ metadata: safe_metadata,
69
+ created_at: created_at.iso8601
70
+ }
71
+ end
72
+
73
+ # Get browser state at this action (for handoff)
74
+ def browser_state
75
+ {
76
+ url: extract_url,
77
+ form_values: metadata["form_values"],
78
+ scroll_position: metadata["scroll_position"],
79
+ active_element: selector,
80
+ action_type: action_type
81
+ }
82
+ end
83
+
84
+ private
85
+
86
+ def redacted_value
87
+ return value unless should_redact?
88
+
89
+ "[REDACTED]"
90
+ end
91
+
92
+ def should_redact?
93
+ return false unless value.present?
94
+
95
+ sensitive_patterns = [
96
+ /password/i,
97
+ /credit.?card/i,
98
+ /cvv/i,
99
+ /ssn/i,
100
+ /social.?security/i,
101
+ /\b\d{16}\b/,
102
+ /\b\d{3}-\d{2}-\d{4}\b/
103
+ ]
104
+
105
+ selector_is_sensitive = sensitive_patterns.any? { |p| selector&.match?(p) }
106
+ value_is_sensitive = sensitive_patterns.any? { |p| value.match?(p) }
107
+
108
+ selector_is_sensitive || value_is_sensitive
109
+ end
110
+
111
+ def safe_metadata
112
+ metadata.except("password", "credit_card", "cvv", "ssn")
113
+ end
114
+
115
+ def extract_url
116
+ case action_type
117
+ when "navigate"
118
+ value
119
+ else
120
+ metadata["url"] || metadata["page_url"]
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Stores screenshots and DOM snapshots from session recordings.
6
+ #
7
+ # Supports Active Storage for file attachment and provides
8
+ # signed URLs for secure access.
9
+ #
10
+ class RecordingSnapshot < ApplicationRecord
11
+ belongs_to :session_recording, class_name: "ActiveAgent::Dashboard::SessionRecording"
12
+ belongs_to :recording_action, class_name: "ActiveAgent::Dashboard::RecordingAction", optional: true
13
+
14
+ has_one_attached :file if defined?(ActiveStorage)
15
+
16
+ SNAPSHOT_TYPES = %w[screenshot dom full_page].freeze
17
+
18
+ validates :storage_key, presence: true, uniqueness: true
19
+ validates :snapshot_type, presence: true, inclusion: { in: SNAPSHOT_TYPES }
20
+
21
+ scope :screenshots, -> { where(snapshot_type: "screenshot") }
22
+ scope :dom_snapshots, -> { where(snapshot_type: "dom") }
23
+ scope :ordered, -> { order(:created_at) }
24
+
25
+ # Get a signed URL for the file
26
+ def signed_url(expires_in: 15.minutes)
27
+ return nil unless respond_to?(:file) && file.attached?
28
+
29
+ file.url(expires_in: expires_in)
30
+ rescue StandardError
31
+ nil
32
+ end
33
+
34
+ # Store file data
35
+ def store!(data, filename: nil, content_type: nil)
36
+ return unless respond_to?(:file)
37
+
38
+ content_type ||= infer_content_type
39
+ filename ||= generate_filename
40
+
41
+ file.attach(
42
+ io: StringIO.new(data),
43
+ filename: filename,
44
+ content_type: content_type
45
+ )
46
+
47
+ update!(file_size_bytes: data.bytesize)
48
+ end
49
+
50
+ # For API response
51
+ def as_json_for_api
52
+ {
53
+ id: id,
54
+ storage_key: storage_key,
55
+ snapshot_type: snapshot_type,
56
+ width: width,
57
+ height: height,
58
+ file_size_bytes: file_size_bytes,
59
+ url: signed_url,
60
+ created_at: created_at.iso8601
61
+ }
62
+ end
63
+
64
+ private
65
+
66
+ def infer_content_type
67
+ case snapshot_type
68
+ when "screenshot", "full_page"
69
+ "image/png"
70
+ when "dom"
71
+ "text/html"
72
+ else
73
+ "application/octet-stream"
74
+ end
75
+ end
76
+
77
+ def generate_filename
78
+ ext = snapshot_type == "dom" ? "html" : "png"
79
+ "#{storage_key.tr('/', '_')}.#{ext}"
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveAgent
4
+ module Dashboard
5
+ # Tracks individual task executions within a sandbox session.
6
+ #
7
+ # Each sandbox run represents a single agent task execution,
8
+ # capturing input, output, timing, and any errors.
9
+ #
10
+ class SandboxRun < ApplicationRecord
11
+ belongs_to :sandbox_session, class_name: "ActiveAgent::Dashboard::SandboxSession", optional: true
12
+
13
+ enum :status, {
14
+ pending: 0,
15
+ running: 1,
16
+ completed: 2,
17
+ failed: 3,
18
+ cancelled: 4
19
+ }
20
+
21
+ validates :task, presence: true
22
+ validates :status, presence: true
23
+
24
+ scope :recent, -> { order(created_at: :desc) }
25
+ scope :completed_runs, -> { where(status: [ :completed, :failed ]) }
26
+
27
+ # Summary for API responses
28
+ def summary
29
+ {
30
+ id: id,
31
+ task: task.truncate(100),
32
+ status: status,
33
+ duration_ms: duration_ms,
34
+ tokens_used: tokens_used,
35
+ created_at: created_at&.iso8601,
36
+ completed_at: completed_at&.iso8601
37
+ }
38
+ end
39
+
40
+ # Detailed info including full result
41
+ def details
42
+ summary.merge(
43
+ task: task,
44
+ result: result,
45
+ error: error,
46
+ screenshots: screenshots || [],
47
+ started_at: started_at&.iso8601
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end