ruby_llm-agents 0.3.6 → 0.5.0

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +46 -13
  3. data/app/controllers/ruby_llm/agents/api_configurations_controller.rb +214 -0
  4. data/app/controllers/ruby_llm/agents/{settings_controller.rb → system_config_controller.rb} +3 -3
  5. data/app/controllers/ruby_llm/agents/tenants_controller.rb +109 -0
  6. data/app/models/ruby_llm/agents/api_configuration.rb +386 -0
  7. data/app/models/ruby_llm/agents/tenant_budget.rb +62 -7
  8. data/app/views/layouts/ruby_llm/agents/application.html.erb +3 -1
  9. data/app/views/ruby_llm/agents/api_configurations/_api_key_field.html.erb +34 -0
  10. data/app/views/ruby_llm/agents/api_configurations/_form.html.erb +288 -0
  11. data/app/views/ruby_llm/agents/api_configurations/edit.html.erb +95 -0
  12. data/app/views/ruby_llm/agents/api_configurations/edit_tenant.html.erb +97 -0
  13. data/app/views/ruby_llm/agents/api_configurations/show.html.erb +211 -0
  14. data/app/views/ruby_llm/agents/api_configurations/tenant.html.erb +179 -0
  15. data/app/views/ruby_llm/agents/dashboard/_action_center.html.erb +1 -1
  16. data/app/views/ruby_llm/agents/executions/show.html.erb +82 -0
  17. data/app/views/ruby_llm/agents/{settings → system_config}/show.html.erb +1 -1
  18. data/app/views/ruby_llm/agents/tenants/_form.html.erb +150 -0
  19. data/app/views/ruby_llm/agents/tenants/edit.html.erb +13 -0
  20. data/app/views/ruby_llm/agents/tenants/index.html.erb +129 -0
  21. data/app/views/ruby_llm/agents/tenants/show.html.erb +374 -0
  22. data/config/routes.rb +12 -1
  23. data/lib/generators/ruby_llm_agents/api_configuration_generator.rb +100 -0
  24. data/lib/generators/ruby_llm_agents/templates/create_api_configurations_migration.rb.tt +90 -0
  25. data/lib/ruby_llm/agents/base/dsl.rb +65 -13
  26. data/lib/ruby_llm/agents/base/execution.rb +113 -6
  27. data/lib/ruby_llm/agents/base/reliability_dsl.rb +82 -0
  28. data/lib/ruby_llm/agents/base.rb +28 -0
  29. data/lib/ruby_llm/agents/budget_tracker.rb +285 -23
  30. data/lib/ruby_llm/agents/configuration.rb +38 -1
  31. data/lib/ruby_llm/agents/deprecations.rb +77 -0
  32. data/lib/ruby_llm/agents/engine.rb +1 -0
  33. data/lib/ruby_llm/agents/instrumentation.rb +71 -3
  34. data/lib/ruby_llm/agents/reliability/breaker_manager.rb +80 -0
  35. data/lib/ruby_llm/agents/reliability/execution_constraints.rb +69 -0
  36. data/lib/ruby_llm/agents/reliability/executor.rb +124 -0
  37. data/lib/ruby_llm/agents/reliability/fallback_routing.rb +72 -0
  38. data/lib/ruby_llm/agents/reliability/retry_strategy.rb +76 -0
  39. data/lib/ruby_llm/agents/resolved_config.rb +348 -0
  40. data/lib/ruby_llm/agents/result.rb +72 -2
  41. data/lib/ruby_llm/agents/version.rb +1 -1
  42. data/lib/ruby_llm/agents.rb +6 -0
  43. metadata +26 -3
@@ -0,0 +1,13 @@
1
+ <%= render "ruby_llm/agents/shared/breadcrumbs", items: [
2
+ { label: "Dashboard", path: ruby_llm_agents.root_path },
3
+ { label: "Tenants", path: ruby_llm_agents.tenants_path },
4
+ { label: @tenant.display_name, path: ruby_llm_agents.tenant_path(@tenant) },
5
+ { label: "Edit" }
6
+ ] %>
7
+
8
+ <div class="mb-6">
9
+ <h1 class="text-2xl font-bold text-gray-900 dark:text-gray-100">Edit Tenant</h1>
10
+ <p class="text-gray-500 dark:text-gray-400 mt-1">Update budget configuration for <%= @tenant.display_name %></p>
11
+ </div>
12
+
13
+ <%= render "form", tenant: @tenant %>
@@ -0,0 +1,129 @@
1
+ <div class="mb-6">
2
+ <h1 class="text-2xl font-bold text-gray-900 dark:text-gray-100">Tenants</h1>
3
+ <p class="text-gray-500 dark:text-gray-400 mt-1">Manage tenant budget configurations</p>
4
+ </div>
5
+
6
+ <% if @tenants.empty? %>
7
+ <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-8 text-center">
8
+ <svg class="w-12 h-12 mx-auto text-gray-400 dark:text-gray-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
9
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
10
+ </svg>
11
+ <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100 mb-1">No tenants configured</h3>
12
+ <p class="text-gray-500 dark:text-gray-400">Tenant budgets will appear here once created via the API or configuration.</p>
13
+ </div>
14
+ <% else %>
15
+ <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 overflow-hidden">
16
+ <div class="overflow-x-auto">
17
+ <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
18
+ <thead class="bg-gray-50 dark:bg-gray-900/50">
19
+ <tr>
20
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
21
+ Tenant
22
+ </th>
23
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
24
+ Daily Budget
25
+ </th>
26
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
27
+ Monthly Budget
28
+ </th>
29
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
30
+ Daily Tokens
31
+ </th>
32
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
33
+ Monthly Tokens
34
+ </th>
35
+ <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
36
+ Enforcement
37
+ </th>
38
+ <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
39
+ Actions
40
+ </th>
41
+ </tr>
42
+ </thead>
43
+ <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
44
+ <% @tenants.each do |tenant| %>
45
+ <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors cursor-pointer" data-href="<%= tenant_path(tenant) %>">
46
+ <td class="px-6 py-4 whitespace-nowrap">
47
+ <div class="flex items-center">
48
+ <div class="flex-shrink-0 h-8 w-8 rounded-full bg-blue-100 dark:bg-blue-900/50 flex items-center justify-center">
49
+ <svg class="w-4 h-4 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
50
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
51
+ </svg>
52
+ </div>
53
+ <div class="ml-3">
54
+ <div class="text-sm font-medium text-gray-900 dark:text-gray-100">
55
+ <%= tenant.display_name %>
56
+ </div>
57
+ <% if tenant.name.present? && tenant.name != tenant.tenant_id %>
58
+ <div class="text-xs text-gray-500 dark:text-gray-400 font-mono">
59
+ <%= tenant.tenant_id %>
60
+ </div>
61
+ <% end %>
62
+ </div>
63
+ </div>
64
+ </td>
65
+ <td class="px-6 py-4 whitespace-nowrap">
66
+ <% if tenant.effective_daily_limit %>
67
+ <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
68
+ $<%= number_with_precision(tenant.effective_daily_limit, precision: 2) %>
69
+ </span>
70
+ <% else %>
71
+ <span class="text-sm text-gray-400 dark:text-gray-500">No limit</span>
72
+ <% end %>
73
+ </td>
74
+ <td class="px-6 py-4 whitespace-nowrap">
75
+ <% if tenant.effective_monthly_limit %>
76
+ <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
77
+ $<%= number_with_precision(tenant.effective_monthly_limit, precision: 2) %>
78
+ </span>
79
+ <% else %>
80
+ <span class="text-sm text-gray-400 dark:text-gray-500">No limit</span>
81
+ <% end %>
82
+ </td>
83
+ <td class="px-6 py-4 whitespace-nowrap">
84
+ <% if tenant.effective_daily_token_limit %>
85
+ <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
86
+ <%= number_with_delimiter(tenant.effective_daily_token_limit) %>
87
+ </span>
88
+ <% else %>
89
+ <span class="text-sm text-gray-400 dark:text-gray-500">No limit</span>
90
+ <% end %>
91
+ </td>
92
+ <td class="px-6 py-4 whitespace-nowrap">
93
+ <% if tenant.effective_monthly_token_limit %>
94
+ <span class="text-sm font-medium text-gray-900 dark:text-gray-100">
95
+ <%= number_with_delimiter(tenant.effective_monthly_token_limit) %>
96
+ </span>
97
+ <% else %>
98
+ <span class="text-sm text-gray-400 dark:text-gray-500">No limit</span>
99
+ <% end %>
100
+ </td>
101
+ <td class="px-6 py-4 whitespace-nowrap">
102
+ <%
103
+ enforcement = tenant.effective_enforcement.to_s
104
+ badge_class = case enforcement
105
+ when "hard" then "bg-red-100 dark:bg-red-900/50 text-red-800 dark:text-red-200"
106
+ when "soft" then "bg-yellow-100 dark:bg-yellow-900/50 text-yellow-800 dark:text-yellow-200"
107
+ else "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400"
108
+ end
109
+ %>
110
+ <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <%= badge_class %>">
111
+ <%= enforcement.capitalize %>
112
+ </span>
113
+ </td>
114
+ <td class="px-6 py-4 whitespace-nowrap text-right text-sm">
115
+ <%= link_to tenant_path(tenant), class: "text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300" do %>
116
+ View
117
+ <% end %>
118
+ <span class="text-gray-300 dark:text-gray-600 mx-2">|</span>
119
+ <%= link_to edit_tenant_path(tenant), class: "text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300" do %>
120
+ Edit
121
+ <% end %>
122
+ </td>
123
+ </tr>
124
+ <% end %>
125
+ </tbody>
126
+ </table>
127
+ </div>
128
+ </div>
129
+ <% end %>
@@ -0,0 +1,374 @@
1
+ <%= render "ruby_llm/agents/shared/breadcrumbs", items: [
2
+ { label: "Dashboard", path: ruby_llm_agents.root_path },
3
+ { label: "Tenants", path: ruby_llm_agents.tenants_path },
4
+ { label: @tenant.display_name }
5
+ ] %>
6
+
7
+ <!-- Header -->
8
+ <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-6">
9
+ <div class="flex items-start justify-between">
10
+ <div>
11
+ <div class="flex items-center space-x-3">
12
+ <div class="flex-shrink-0 h-10 w-10 rounded-full bg-blue-100 dark:bg-blue-900/50 flex items-center justify-center">
13
+ <svg class="w-5 h-5 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
14
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
15
+ </svg>
16
+ </div>
17
+ <div>
18
+ <h1 class="text-2xl font-bold text-gray-900 dark:text-gray-100">
19
+ <%= @tenant.display_name %>
20
+ </h1>
21
+ <% if @tenant.name.present? && @tenant.name != @tenant.tenant_id %>
22
+ <p class="text-sm text-gray-500 dark:text-gray-400 font-mono"><%= @tenant.tenant_id %></p>
23
+ <% end %>
24
+ </div>
25
+ <%
26
+ enforcement = @tenant.effective_enforcement.to_s
27
+ badge_class = case enforcement
28
+ when "hard" then "bg-red-100 dark:bg-red-900/50 text-red-800 dark:text-red-200"
29
+ when "soft" then "bg-yellow-100 dark:bg-yellow-900/50 text-yellow-800 dark:text-yellow-200"
30
+ else "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400"
31
+ end
32
+ %>
33
+ <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <%= badge_class %>">
34
+ <%= enforcement.capitalize %> Enforcement
35
+ </span>
36
+ </div>
37
+ </div>
38
+
39
+ <div class="flex items-center space-x-3">
40
+ <%= link_to tenant_api_configuration_path(@tenant.tenant_id), class: "inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors" do %>
41
+ <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
42
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z" />
43
+ </svg>
44
+ API Keys
45
+ <% end %>
46
+
47
+ <%= link_to edit_tenant_path(@tenant), class: "inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors" do %>
48
+ <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
49
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
50
+ </svg>
51
+ Edit
52
+ <% end %>
53
+ </div>
54
+ </div>
55
+ </div>
56
+
57
+ <!-- Budget Usage Stats -->
58
+ <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
59
+ <!-- Daily Spend -->
60
+ <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl p-4">
61
+ <div class="flex items-center justify-between mb-2">
62
+ <p class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Daily Spend</p>
63
+ <span class="text-amber-500">
64
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
65
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
66
+ </svg>
67
+ </span>
68
+ </div>
69
+ <p class="text-xl font-semibold text-gray-900 dark:text-gray-100">
70
+ $<%= number_with_precision(@usage_stats[:daily_spend], precision: 4) %>
71
+ </p>
72
+ <% if @tenant.effective_daily_limit %>
73
+ <div class="mt-2">
74
+ <div class="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400 mb-1">
75
+ <span>of $<%= number_with_precision(@tenant.effective_daily_limit, precision: 2) %></span>
76
+ <span><%= @usage_stats[:daily_spend_percentage] %>%</span>
77
+ </div>
78
+ <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
79
+ <%
80
+ pct = [@usage_stats[:daily_spend_percentage], 100].min
81
+ bar_color = pct >= 90 ? 'bg-red-500' : pct >= 70 ? 'bg-yellow-500' : 'bg-green-500'
82
+ %>
83
+ <div class="<%= bar_color %> h-1.5 rounded-full transition-all" style="width: <%= pct %>%"></div>
84
+ </div>
85
+ </div>
86
+ <% else %>
87
+ <p class="text-xs text-gray-400 dark:text-gray-500 mt-1">No limit set</p>
88
+ <% end %>
89
+ </div>
90
+
91
+ <!-- Monthly Spend -->
92
+ <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl p-4">
93
+ <div class="flex items-center justify-between mb-2">
94
+ <p class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Monthly Spend</p>
95
+ <span class="text-amber-500">
96
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
97
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
98
+ </svg>
99
+ </span>
100
+ </div>
101
+ <p class="text-xl font-semibold text-gray-900 dark:text-gray-100">
102
+ $<%= number_with_precision(@usage_stats[:monthly_spend], precision: 4) %>
103
+ </p>
104
+ <% if @tenant.effective_monthly_limit %>
105
+ <div class="mt-2">
106
+ <div class="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400 mb-1">
107
+ <span>of $<%= number_with_precision(@tenant.effective_monthly_limit, precision: 2) %></span>
108
+ <span><%= @usage_stats[:monthly_spend_percentage] %>%</span>
109
+ </div>
110
+ <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
111
+ <%
112
+ pct = [@usage_stats[:monthly_spend_percentage], 100].min
113
+ bar_color = pct >= 90 ? 'bg-red-500' : pct >= 70 ? 'bg-yellow-500' : 'bg-green-500'
114
+ %>
115
+ <div class="<%= bar_color %> h-1.5 rounded-full transition-all" style="width: <%= pct %>%"></div>
116
+ </div>
117
+ </div>
118
+ <% else %>
119
+ <p class="text-xs text-gray-400 dark:text-gray-500 mt-1">No limit set</p>
120
+ <% end %>
121
+ </div>
122
+
123
+ <!-- Daily Tokens -->
124
+ <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl p-4">
125
+ <div class="flex items-center justify-between mb-2">
126
+ <p class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Daily Tokens</p>
127
+ <span class="text-indigo-500">
128
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
129
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 20l4-16m2 16l4-16M6 9h14M4 15h14"/>
130
+ </svg>
131
+ </span>
132
+ </div>
133
+ <p class="text-xl font-semibold text-gray-900 dark:text-gray-100">
134
+ <%= number_with_delimiter(@usage_stats[:daily_tokens]) %>
135
+ </p>
136
+ <% if @tenant.effective_daily_token_limit %>
137
+ <div class="mt-2">
138
+ <div class="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400 mb-1">
139
+ <span>of <%= number_with_delimiter(@tenant.effective_daily_token_limit) %></span>
140
+ <span><%= @usage_stats[:daily_token_percentage] %>%</span>
141
+ </div>
142
+ <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
143
+ <%
144
+ pct = [@usage_stats[:daily_token_percentage], 100].min
145
+ bar_color = pct >= 90 ? 'bg-red-500' : pct >= 70 ? 'bg-yellow-500' : 'bg-green-500'
146
+ %>
147
+ <div class="<%= bar_color %> h-1.5 rounded-full transition-all" style="width: <%= pct %>%"></div>
148
+ </div>
149
+ </div>
150
+ <% else %>
151
+ <p class="text-xs text-gray-400 dark:text-gray-500 mt-1">No limit set</p>
152
+ <% end %>
153
+ </div>
154
+
155
+ <!-- Monthly Tokens -->
156
+ <div class="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl p-4">
157
+ <div class="flex items-center justify-between mb-2">
158
+ <p class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wide font-medium">Monthly Tokens</p>
159
+ <span class="text-indigo-500">
160
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
161
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 20l4-16m2 16l4-16M6 9h14M4 15h14"/>
162
+ </svg>
163
+ </span>
164
+ </div>
165
+ <p class="text-xl font-semibold text-gray-900 dark:text-gray-100">
166
+ <%= number_with_delimiter(@usage_stats[:monthly_tokens]) %>
167
+ </p>
168
+ <% if @tenant.effective_monthly_token_limit %>
169
+ <div class="mt-2">
170
+ <div class="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400 mb-1">
171
+ <span>of <%= number_with_delimiter(@tenant.effective_monthly_token_limit) %></span>
172
+ <span><%= @usage_stats[:monthly_token_percentage] %>%</span>
173
+ </div>
174
+ <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5">
175
+ <%
176
+ pct = [@usage_stats[:monthly_token_percentage], 100].min
177
+ bar_color = pct >= 90 ? 'bg-red-500' : pct >= 70 ? 'bg-yellow-500' : 'bg-green-500'
178
+ %>
179
+ <div class="<%= bar_color %> h-1.5 rounded-full transition-all" style="width: <%= pct %>%"></div>
180
+ </div>
181
+ </div>
182
+ <% else %>
183
+ <p class="text-xs text-gray-400 dark:text-gray-500 mt-1">No limit set</p>
184
+ <% end %>
185
+ </div>
186
+ </div>
187
+
188
+ <!-- Summary Stats -->
189
+ <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
190
+ <%= render "ruby_llm/agents/shared/stat_card",
191
+ title: "Total Executions",
192
+ value: number_with_delimiter(@usage_stats[:total_executions]),
193
+ icon: "M13 10V3L4 14h7v7l9-11h-7z",
194
+ icon_color: "text-blue-500" %>
195
+
196
+ <%= render "ruby_llm/agents/shared/stat_card",
197
+ title: "Total Cost",
198
+ value: "$#{number_with_precision(@usage_stats[:total_cost], precision: 4)}",
199
+ icon: "M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z",
200
+ icon_color: "text-amber-500" %>
201
+
202
+ <%= render "ruby_llm/agents/shared/stat_card",
203
+ title: "Total Tokens",
204
+ value: number_with_delimiter(@usage_stats[:total_tokens]),
205
+ icon: "M7 20l4-16m2 16l4-16M6 9h14M4 15h14",
206
+ icon_color: "text-indigo-500" %>
207
+ </div>
208
+
209
+ <!-- Budget Configuration -->
210
+ <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-6">
211
+ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-4">Budget Configuration</h3>
212
+
213
+ <div class="grid grid-cols-2 md:grid-cols-4 gap-6">
214
+ <div>
215
+ <p class="text-sm text-gray-500 dark:text-gray-400">Daily Limit (USD)</p>
216
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
217
+ <% if @tenant.effective_daily_limit %>
218
+ $<%= number_with_precision(@tenant.effective_daily_limit, precision: 2) %>
219
+ <% else %>
220
+ <span class="text-gray-400 dark:text-gray-500">Not set</span>
221
+ <% end %>
222
+ </p>
223
+ </div>
224
+
225
+ <div>
226
+ <p class="text-sm text-gray-500 dark:text-gray-400">Monthly Limit (USD)</p>
227
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
228
+ <% if @tenant.effective_monthly_limit %>
229
+ $<%= number_with_precision(@tenant.effective_monthly_limit, precision: 2) %>
230
+ <% else %>
231
+ <span class="text-gray-400 dark:text-gray-500">Not set</span>
232
+ <% end %>
233
+ </p>
234
+ </div>
235
+
236
+ <div>
237
+ <p class="text-sm text-gray-500 dark:text-gray-400">Daily Token Limit</p>
238
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
239
+ <% if @tenant.effective_daily_token_limit %>
240
+ <%= number_with_delimiter(@tenant.effective_daily_token_limit) %>
241
+ <% else %>
242
+ <span class="text-gray-400 dark:text-gray-500">Not set</span>
243
+ <% end %>
244
+ </p>
245
+ </div>
246
+
247
+ <div>
248
+ <p class="text-sm text-gray-500 dark:text-gray-400">Monthly Token Limit</p>
249
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
250
+ <% if @tenant.effective_monthly_token_limit %>
251
+ <%= number_with_delimiter(@tenant.effective_monthly_token_limit) %>
252
+ <% else %>
253
+ <span class="text-gray-400 dark:text-gray-500">Not set</span>
254
+ <% end %>
255
+ </p>
256
+ </div>
257
+ </div>
258
+
259
+ <div class="border-t border-gray-100 dark:border-gray-700 mt-4 pt-4">
260
+ <div class="grid grid-cols-2 gap-6">
261
+ <div>
262
+ <p class="text-sm text-gray-500 dark:text-gray-400">Enforcement Mode</p>
263
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
264
+ <%= @tenant.effective_enforcement.to_s.capitalize %>
265
+ <span class="text-xs text-gray-500 dark:text-gray-400 ml-2">
266
+ <% case @tenant.effective_enforcement.to_s %>
267
+ <% when "hard" %>
268
+ (blocks requests when limits exceeded)
269
+ <% when "soft" %>
270
+ (logs warnings when limits exceeded)
271
+ <% else %>
272
+ (no enforcement)
273
+ <% end %>
274
+ </span>
275
+ </p>
276
+ </div>
277
+
278
+ <div>
279
+ <p class="text-sm text-gray-500 dark:text-gray-400">Inherit Global Defaults</p>
280
+ <p class="font-medium text-gray-900 dark:text-gray-100 mt-1">
281
+ <%= @tenant.inherit_global_defaults ? "Yes" : "No" %>
282
+ </p>
283
+ </div>
284
+ </div>
285
+ </div>
286
+
287
+ <% if @tenant.per_agent_daily.present? || @tenant.per_agent_monthly.present? %>
288
+ <div class="border-t border-gray-100 dark:border-gray-700 mt-4 pt-4">
289
+ <p class="text-xs text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-3">Per-Agent Limits</p>
290
+
291
+ <div class="space-y-2">
292
+ <% (@tenant.per_agent_daily || {}).each do |agent, limit| %>
293
+ <div class="flex items-center text-sm">
294
+ <code class="bg-gray-100 dark:bg-gray-700 dark:text-gray-200 px-2 py-0.5 rounded font-mono">
295
+ <%= agent %>
296
+ </code>
297
+ <span class="ml-2 text-gray-600 dark:text-gray-300">
298
+ Daily: $<%= number_with_precision(limit, precision: 2) %>
299
+ </span>
300
+ </div>
301
+ <% end %>
302
+
303
+ <% (@tenant.per_agent_monthly || {}).each do |agent, limit| %>
304
+ <div class="flex items-center text-sm">
305
+ <code class="bg-gray-100 dark:bg-gray-700 dark:text-gray-200 px-2 py-0.5 rounded font-mono">
306
+ <%= agent %>
307
+ </code>
308
+ <span class="ml-2 text-gray-600 dark:text-gray-300">
309
+ Monthly: $<%= number_with_precision(limit, precision: 2) %>
310
+ </span>
311
+ </div>
312
+ <% end %>
313
+ </div>
314
+ </div>
315
+ <% end %>
316
+ </div>
317
+
318
+ <!-- Recent Executions -->
319
+ <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
320
+ <div class="flex items-center justify-between mb-4">
321
+ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">Recent Executions</h3>
322
+ <%= link_to ruby_llm_agents.executions_path(tenant_id: @tenant.tenant_id), class: "text-sm text-blue-600 dark:text-blue-400 hover:underline" do %>
323
+ View all
324
+ <% end %>
325
+ </div>
326
+
327
+ <% if @executions.empty? %>
328
+ <p class="text-gray-500 dark:text-gray-400 text-sm">No executions found for this tenant.</p>
329
+ <% else %>
330
+ <div class="overflow-x-auto">
331
+ <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
332
+ <thead>
333
+ <tr>
334
+ <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Agent</th>
335
+ <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Status</th>
336
+ <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Cost</th>
337
+ <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Tokens</th>
338
+ <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Time</th>
339
+ </tr>
340
+ </thead>
341
+ <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
342
+ <% @executions.each do |execution| %>
343
+ <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 cursor-pointer" data-href="<%= ruby_llm_agents.execution_path(execution) %>">
344
+ <td class="px-4 py-3 whitespace-nowrap">
345
+ <span class="text-sm font-medium text-gray-900 dark:text-gray-100"><%= execution.agent_type.gsub(/Agent$/, '') %></span>
346
+ </td>
347
+ <td class="px-4 py-3 whitespace-nowrap">
348
+ <%
349
+ status_class = case execution.status
350
+ when "success" then "badge-success"
351
+ when "error" then "badge-error"
352
+ when "running" then "badge-running"
353
+ when "timeout" then "badge-timeout"
354
+ else "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400"
355
+ end
356
+ %>
357
+ <span class="badge <%= status_class %>"><%= execution.status %></span>
358
+ </td>
359
+ <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">
360
+ $<%= number_with_precision(execution.total_cost || 0, precision: 6) %>
361
+ </td>
362
+ <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">
363
+ <%= number_with_delimiter(execution.total_tokens || 0) %>
364
+ </td>
365
+ <td class="px-4 py-3 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
366
+ <%= time_ago_in_words(execution.created_at) %> ago
367
+ </td>
368
+ </tr>
369
+ <% end %>
370
+ </tbody>
371
+ </table>
372
+ </div>
373
+ <% end %>
374
+ </div>
data/config/routes.rb CHANGED
@@ -16,7 +16,18 @@ RubyLLM::Agents::Engine.routes.draw do
16
16
  end
17
17
  end
18
18
 
19
+ resources :tenants, only: [:index, :show, :edit, :update]
20
+
21
+ # Global API Configuration
22
+ resource :api_configuration, only: [:show, :edit, :update]
23
+
24
+ # Tenant API Configurations
25
+ get "tenants/:tenant_id/api_configuration", to: "api_configurations#tenant", as: :tenant_api_configuration
26
+ get "tenants/:tenant_id/api_configuration/edit", to: "api_configurations#edit_tenant", as: :edit_tenant_api_configuration
27
+ patch "tenants/:tenant_id/api_configuration", to: "api_configurations#update_tenant"
28
+ post "api_configuration/test_connection", to: "api_configurations#test_connection", as: :test_api_connection
29
+
19
30
  # Redirect old analytics route to dashboard
20
31
  get "analytics", to: redirect("/")
21
- resource :settings, only: [:show]
32
+ resource :system_config, only: [:show], controller: "system_config"
22
33
  end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module RubyLlmAgents
7
+ # API Configuration generator for ruby_llm-agents
8
+ #
9
+ # Usage:
10
+ # rails generate ruby_llm_agents:api_configuration
11
+ #
12
+ # This will create migrations for:
13
+ # - ruby_llm_agents_api_configurations table for storing API keys and settings
14
+ #
15
+ # API keys are encrypted at rest using Rails encrypted attributes.
16
+ # Supports both global configuration and per-tenant overrides.
17
+ #
18
+ class ApiConfigurationGenerator < ::Rails::Generators::Base
19
+ include ::ActiveRecord::Generators::Migration
20
+
21
+ source_root File.expand_path("templates", __dir__)
22
+
23
+ desc "Adds database-backed API configuration support to RubyLLM::Agents"
24
+
25
+ def create_api_configurations_migration
26
+ if table_exists?(:ruby_llm_agents_api_configurations)
27
+ say_status :skip, "ruby_llm_agents_api_configurations table already exists", :yellow
28
+ return
29
+ end
30
+
31
+ migration_template(
32
+ "create_api_configurations_migration.rb.tt",
33
+ File.join(db_migrate_path, "create_ruby_llm_agents_api_configurations.rb")
34
+ )
35
+ end
36
+
37
+ def show_post_install_message
38
+ say ""
39
+ say "API Configuration migration created!", :green
40
+ say ""
41
+ say "Next steps:"
42
+ say " 1. Ensure Rails encryption is configured (if not already):"
43
+ say ""
44
+ say " bin/rails db:encryption:init"
45
+ say ""
46
+ say " Then add the generated keys to your credentials or environment."
47
+ say ""
48
+ say " 2. Run the migration:"
49
+ say ""
50
+ say " rails db:migrate"
51
+ say ""
52
+ say " 3. Access the API Configuration UI:"
53
+ say ""
54
+ say " Navigate to /agents/api_configuration in your browser"
55
+ say ""
56
+ say " 4. (Optional) Configure API keys programmatically:"
57
+ say ""
58
+ say " # Set global configuration"
59
+ say " config = RubyLLM::Agents::ApiConfiguration.global"
60
+ say " config.update!("
61
+ say " openai_api_key: 'sk-...',"
62
+ say " anthropic_api_key: 'sk-ant-...'"
63
+ say " )"
64
+ say ""
65
+ say " # Set tenant-specific configuration"
66
+ say " tenant_config = RubyLLM::Agents::ApiConfiguration.for_tenant!('acme_corp')"
67
+ say " tenant_config.update!("
68
+ say " openai_api_key: 'sk-tenant-specific-key',"
69
+ say " inherit_global_defaults: true"
70
+ say " )"
71
+ say ""
72
+ say "Configuration Resolution Priority:"
73
+ say " 1. Per-tenant database configuration (if multi-tenancy enabled)"
74
+ say " 2. Global database configuration"
75
+ say " 3. RubyLLM.configure block settings"
76
+ say ""
77
+ say "Security Notes:"
78
+ say " - API keys are encrypted at rest using Rails encrypted attributes"
79
+ say " - Keys are masked in the UI (e.g., sk-ab****wxyz)"
80
+ say " - Dashboard authentication inherits from your authenticate_dashboard! method"
81
+ say ""
82
+ end
83
+
84
+ private
85
+
86
+ def migration_version
87
+ "[#{::ActiveRecord::VERSION::STRING.to_f}]"
88
+ end
89
+
90
+ def db_migrate_path
91
+ "db/migrate"
92
+ end
93
+
94
+ def table_exists?(table)
95
+ ActiveRecord::Base.connection.table_exists?(table)
96
+ rescue StandardError
97
+ false
98
+ end
99
+ end
100
+ end