rails-agent-stack 0.1.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 (35) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +16 -0
  3. data/MIT-LICENSE +21 -0
  4. data/README.md +463 -0
  5. data/lib/generators/rails_agents/install_generator.rb +29 -0
  6. data/lib/generators/rails_agents/templates/initializer.rb +6 -0
  7. data/lib/rails/agent/stack.rb +4 -0
  8. data/lib/rails_agents/agent.rb +128 -0
  9. data/lib/rails_agents/configuration.rb +35 -0
  10. data/lib/rails_agents/error.rb +7 -0
  11. data/lib/rails_agents/generated_file.rb +15 -0
  12. data/lib/rails_agents/message.rb +27 -0
  13. data/lib/rails_agents/providers/anthropic/files.rb +51 -0
  14. data/lib/rails_agents/providers/anthropic.rb +111 -0
  15. data/lib/rails_agents/providers/base.rb +23 -0
  16. data/lib/rails_agents/providers/grok.rb +15 -0
  17. data/lib/rails_agents/providers/open_router.rb +19 -0
  18. data/lib/rails_agents/providers/openai.rb +15 -0
  19. data/lib/rails_agents/providers/openai_compatible.rb +78 -0
  20. data/lib/rails_agents/railtie.rb +25 -0
  21. data/lib/rails_agents/result.rb +13 -0
  22. data/lib/rails_agents/runner.rb +146 -0
  23. data/lib/rails_agents/skill.rb +24 -0
  24. data/lib/rails_agents/skill_declaration.rb +8 -0
  25. data/lib/rails_agents/skill_set.rb +139 -0
  26. data/lib/rails_agents/skills/anthropic_content.rb +29 -0
  27. data/lib/rails_agents/skills/portable/web_fetch.rb +29 -0
  28. data/lib/rails_agents/skills/portable/web_search.rb +33 -0
  29. data/lib/rails_agents/skills/registry.rb +67 -0
  30. data/lib/rails_agents/tool.rb +48 -0
  31. data/lib/rails_agents/tool_set.rb +46 -0
  32. data/lib/rails_agents/version.rb +5 -0
  33. data/lib/rails_agents.rb +44 -0
  34. data/rails-agent-stack.gemspec +49 -0
  35. metadata +150 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 64df13067bdec015136eaf95af4b809a0a8f6f9dcd304ff386ad468c1d57b6f6
4
+ data.tar.gz: eba3afd7708548de385c45219031e01819e5c67fe136ed91a512060f1c1fa45e
5
+ SHA512:
6
+ metadata.gz: 00d8cf682d9f83c8e5787b5f18f4bfaa5f71b56b8c9eed9befa289cb51d125a303495b3bb4e2dadbfecbc057bee8b52a5d56772870df9c50f7aebc79d796a2bf
7
+ data.tar.gz: 2c0a48bad95a2994d33f99a7838ebcffeaece8a292919df01904896416827651f4c906123eeea54d57541cb7b05e9da36c64527246abc64c23a9f38ff57fd0f3
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ ## [0.1.0] — 2026-07-09
6
+
7
+ First public release. Published on RubyGems as **`rails-agent-stack`** (Ruby API remains `RailsAgents`).
8
+
9
+ - `RailsAgents::Agent` — one class for every use case (`provider`, `model`, `description`, `tools`, `skills`)
10
+ - `RailsAgents::Tool` — app code as tools, auto-loaded from `app/agents/tools/`
11
+ - Skills: `:web_search`, `:web_fetch` (portable + Anthropic native); Anthropic `:code_execution`, `:memory`, `:pptx`, `:xlsx`, `:docx`, `:pdf`
12
+ - Providers: OpenAI, Anthropic, OpenRouter, Grok
13
+ - Install generator (`rails generate rails_agents:install`)
14
+ - Sample playground app in `spec/dummy/`
15
+ - Documentation site (VitePress) on GitHub Pages
16
+ - Fix: `Providers.build` no longer passes a nil API key that overrode config defaults
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tiny Bubble Company
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,463 @@
1
+ # Rails Agents
2
+
3
+ **Dead-simple AI agents for Rails — speed to production, not framework noise.**
4
+
5
+ Define an agent as a plain Ruby class. Say what it does, pick a provider and model, attach your app code as tools, call `.run`. No dashboards, no cloud accounts, no agent lifecycle UI.
6
+
7
+ | | |
8
+ |---|---|
9
+ | **Docs** | [tiny-bubble-company.github.io/rails-agents](https://tiny-bubble-company.github.io/rails-agents/) |
10
+ | **Gem** | [rubygems.org/gems/rails-agent-stack](https://rubygems.org/gems/rails-agent-stack) |
11
+ | **Source** | [github.com/Tiny-Bubble-Company/rails-agents](https://github.com/Tiny-Bubble-Company/rails-agents) |
12
+
13
+ ```ruby
14
+ class LeadQualifier < RailsAgents::Agent
15
+ provider :openrouter
16
+ model "meta-llama/llama-3.3-70b-instruct:free"
17
+ description "Qualifies inbound leads, answers basic questions, and creates a CRM note when a lead looks promising."
18
+ tools "SearchCrm", "CreateCrmNote"
19
+ end
20
+
21
+ LeadQualifier.run("New signup from acme.com — 50 employees, asked about enterprise pricing")
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Install
27
+
28
+ ```ruby
29
+ # Gemfile
30
+ gem "rails-agent-stack"
31
+ ```
32
+
33
+ ```bash
34
+ bundle install
35
+ bin/rails generate rails_agents:install
36
+ ```
37
+
38
+ The gem name is `rails-agent-stack`; the Ruby API is still `RailsAgents` (`require "rails_agents"`).
39
+
40
+ Set API keys in `config/initializers/rails_agents.rb`, create an agent in `app/agents/`, call `.run`.
41
+
42
+ Full walkthrough: **[Getting Started](https://tiny-bubble-company.github.io/rails-agents/guide/getting-started)**
43
+
44
+ ---
45
+
46
+ ## Contents
47
+
48
+ 1. [Philosophy](#philosophy) — what we optimize for and what we skip
49
+ 2. [The problem](#the-problem) — why this gem exists
50
+ 3. [How we're different](#how-were-different) — vs RubyLLM and rolling your own
51
+ 4. [Quick start](#quick-start) — install to first `.run` in minutes
52
+ 5. [Agents](#agents) — the only class you need
53
+ 6. [Tools](#tools) — wire in your app code
54
+ 7. [Skills](#skills) — built-in capabilities (web search, spreadsheets, …)
55
+ 8. [Providers](#providers) — OpenAI, Anthropic, OpenRouter, Grok
56
+ 9. [Try it](#try-it) — tests, playground app, recipes
57
+ 10. [Requirements](#requirements)
58
+
59
+ ---
60
+
61
+ ## Philosophy
62
+
63
+ Rails Agents competes on **simplicity** — not on having the most features.
64
+
65
+ We believe the best agent framework for Rails is the one that gets out of your way: a small gem, a familiar DSL, and a straight line from idea to working code.
66
+
67
+ ### What we optimize for
68
+
69
+ | Priority | What it means in practice |
70
+ |----------|---------------------------|
71
+ | **Developer experience** | One mental model, one agent class, a DSL that reads like Ruby |
72
+ | **Speed to implementation** | `rails generate`, drop a class in `app/agents/`, call `.run` — minutes, not days |
73
+ | **Lightweight** | API keys in an initializer, agents in your app — no engine, no control plane, no telemetry stack |
74
+ | **Ease of getting started** | Three required declarations: `provider`, `model`, `description`. Everything else is optional |
75
+ | **Rails-native** | Your tools are your models, jobs, and services — wired in with plain Ruby classes |
76
+
77
+ ### What we deliberately don't build
78
+
79
+ Chat persistence, model registries, agent versioning, hosted dashboards, or a general-purpose AI toolkit. Other gems do those well. We stay focused on one job:
80
+
81
+ > **Get a working agent into your Rails app with the least code and the least ceremony.**
82
+
83
+ ### Who this is for
84
+
85
+ - You want an agent **today**, not after reading a framework manual
86
+ - You already have Rails app code you want the model to call
87
+ - You prefer **one class per use case** over configuring agent types, versions, and lifecycles
88
+ - You want provider differences handled for you, without giving up control of your agents
89
+
90
+ ---
91
+
92
+ ## The problem
93
+
94
+ You want AI agents in your Rails app — to answer questions, run workflows, qualify leads, draft emails. Most options push you toward:
95
+
96
+ | Pain | What you get instead |
97
+ |------|----------------------|
98
+ | **Heavy setup** | Dashboards, provider wizards, version management, hosted control planes |
99
+ | **Framework sprawl** | Different APIs per provider, per agent type, per use case |
100
+ | **Unclear starting point** | "Which class do I use? What's an agent version? Where does configuration live?" |
101
+
102
+ **What you actually want:** define an agent in Ruby, connect your existing code as tools, call `.run`.
103
+
104
+ Rails Agents is built for exactly that.
105
+
106
+ ---
107
+
108
+ ## How we're different
109
+
110
+ ### vs RubyLLM
111
+
112
+ [RubyLLM](https://rubyllm.com) is an excellent general-purpose AI framework — chat, images, embeddings, 800+ models, Rails chat persistence. It's the right choice when you want a **full AI toolkit**.
113
+
114
+ | | **RubyLLM** | **Rails Agents** |
115
+ |---|-------------|------------------|
116
+ | **Goal** | Broad AI framework | **Agents only** — smallest path to a working agent |
117
+ | **Mental model** | `RubyLLM.chat` + `RubyLLM::Agent` with many macros | One class: `RailsAgents::Agent` |
118
+ | **Configuration** | API keys + model registry + many options | API keys in initializer; **model on each agent** |
119
+ | **What defines behavior** | `instructions`, `tools`, `model`, `temperature`, etc. | **`description`** — what the agent does |
120
+ | **Agent types** | You compose behavior yourself | Same class — change `description` for each use case |
121
+ | **Tools** | Tool classes / blocks | `RailsAgents::Tool` + auto-load from `app/agents/tools/` |
122
+ | **Providers** | Many built-in | OpenAI, Anthropic, OpenRouter, Grok — unified DSL, gem translates API calls |
123
+ | **Rails integration** | `acts_as_chat`, persistence | Lightweight: initializer + `app/agents/` |
124
+
125
+ **Use RubyLLM** if you need multimodal AI, embeddings, model discovery, or chat persistence.
126
+
127
+ **Use Rails Agents** if you want the fastest path from `gem install` to a working agent — one class, one description, your tools, done.
128
+
129
+ ### vs rolling your own
130
+
131
+ You could wire OpenAI or Anthropic HTTP calls directly. Rails Agents gives you a thin, opinionated layer so you don't re-solve:
132
+
133
+ - Multi-turn tool loops
134
+ - Provider-specific request/response shapes
135
+ - Anthropic skills, server tools, and file downloads
136
+ - Portable fallbacks when a skill isn't native to your provider
137
+
138
+ The gem stays small on purpose. You keep full control of your agents and tools; we handle the plumbing.
139
+
140
+ ---
141
+
142
+ ## Quick start
143
+
144
+ ### 1. Install
145
+
146
+ ```ruby
147
+ # Gemfile
148
+ gem "rails-agent-stack"
149
+ ```
150
+
151
+ ```bash
152
+ bundle install
153
+ bin/rails generate rails_agents:install
154
+ ```
155
+
156
+ This creates:
157
+
158
+ - `config/initializers/rails_agents.rb` — API keys only
159
+ - `app/agents/` and `app/agents/tools/` — where your agents live
160
+
161
+ ### 2. Configure API keys
162
+
163
+ The initializer holds **API keys only**. Models are set on each agent.
164
+
165
+ ```ruby
166
+ # config/initializers/rails_agents.rb
167
+ RailsAgents.configure do |config|
168
+ config.openai_api_key = ENV["OPENAI_API_KEY"]
169
+ config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"]
170
+ config.openrouter_api_key = ENV["OPENROUTER_API_KEY"]
171
+ config.grok_api_key = ENV["XAI_API_KEY"]
172
+ end
173
+ ```
174
+
175
+ Set only the keys you use.
176
+
177
+ ### 3. Define and run an agent
178
+
179
+ Every agent needs three things — nothing more to get started:
180
+
181
+ ```ruby
182
+ # app/agents/support_agent.rb
183
+ class SupportAgent < RailsAgents::Agent
184
+ provider :openai # :openai, :anthropic, :openrouter, :grok
185
+ model "gpt-4o-mini" # required — set per agent
186
+ description "Answers customer questions using docs and account data."
187
+ tools "SearchDocs", "LookupAccount" # optional
188
+ end
189
+ ```
190
+
191
+ ```bash
192
+ export OPENAI_API_KEY=sk-...
193
+ bin/rails console
194
+ ```
195
+
196
+ ```ruby
197
+ result = SupportAgent.run("How do I reset my password?")
198
+ result.output # => the agent's reply
199
+ result.success # => true/false
200
+ ```
201
+
202
+ That's it. Add tools and skills when you need them — not before.
203
+
204
+ More detail in the docs: [Agents](https://tiny-bubble-company.github.io/rails-agents/guide/agents) · [Tools](https://tiny-bubble-company.github.io/rails-agents/guide/tools) · [Skills](https://tiny-bubble-company.github.io/rails-agents/guide/skills)
205
+
206
+ ---
207
+
208
+ ## Agents
209
+
210
+ `RailsAgents::Agent` is the only agent class. Each use case is a new subclass with its own `description` — not a new framework concept.
211
+
212
+ ```ruby
213
+ class EmailDrafter < RailsAgents::Agent
214
+ provider :anthropic
215
+ model "claude-sonnet-4-20250514"
216
+ description "Draft short, professional follow-up emails from bullet points."
217
+ end
218
+
219
+ class LeadQualifier < RailsAgents::Agent
220
+ provider :openrouter
221
+ model "meta-llama/llama-3.3-70b-instruct:free"
222
+ description "Qualify inbound leads and create CRM notes for promising ones."
223
+ tools "SearchCrm", "CreateCrmNote"
224
+ end
225
+ ```
226
+
227
+ ### Run API
228
+
229
+ All equivalent:
230
+
231
+ ```ruby
232
+ SupportAgent.run("question")
233
+ SupportAgent.ask("question")
234
+ SupportAgent.call("question")
235
+ ```
236
+
237
+ ### Result object
238
+
239
+ ```ruby
240
+ result.output # agent's text reply
241
+ result.success # true/false
242
+ result.error # error message when success is false
243
+ result.files # generated files (Anthropic document skills)
244
+ ```
245
+
246
+ Pass `save_files_to:` to write downloaded files to disk:
247
+
248
+ ```ruby
249
+ ReportBuilder.run("Create Q1 sales report", save_files_to: "tmp/reports")
250
+ ```
251
+
252
+ ---
253
+
254
+ ## Tools
255
+
256
+ **Tools** are your app code — the agent calls them when it needs data or side effects.
257
+
258
+ Drop tool classes in `app/agents/tools/` (auto-loaded) or declare them on the agent:
259
+
260
+ ```ruby
261
+ # app/agents/tools/search_docs.rb
262
+ class SearchDocs < RailsAgents::Tool
263
+ description "Search product documentation"
264
+ param :query, :string
265
+
266
+ def call(query:)
267
+ Documentation.search(query).limit(5).pluck(:title)
268
+ end
269
+ end
270
+
271
+ # app/agents/doc_agent.rb
272
+ class DocAgent < RailsAgents::Agent
273
+ provider :openai
274
+ model "gpt-4o-mini"
275
+ description "Answer questions using internal docs."
276
+ tools "SearchDocs"
277
+ end
278
+ ```
279
+
280
+ **Tip:** Declare tools as **strings** when the agent lives in `app/agents/` (e.g. `tools "SearchDocs"`). Ruby otherwise looks up constants under the agent class first (`LeadQualifier::SearchCrm`). You can also use `tools ::SearchDocs`.
281
+
282
+ ---
283
+
284
+ ## Skills
285
+
286
+ **Skills** are built-in capabilities (web search, spreadsheets, etc.) provided by the gem. **Tools** are your app code.
287
+
288
+ ```ruby
289
+ class ResearchAgent < RailsAgents::Agent
290
+ provider :anthropic
291
+ model "claude-sonnet-4-20250514"
292
+ description "Research topics using current web data and internal docs."
293
+ skills :web_search, :web_fetch
294
+ tools "SearchInternalDocs"
295
+ end
296
+ ```
297
+
298
+ ### Built-in skills
299
+
300
+ | Skill | What it does | Anthropic | OpenAI / OpenRouter / Grok |
301
+ |-------|--------------|-----------|----------------------------|
302
+ | `:web_search` | Search the web | Native server tool | Portable Ruby tool |
303
+ | `:web_fetch` | Read a URL | Native server tool | Portable Ruby tool |
304
+ | `:code_execution` | Run code on Anthropic servers | Native | Anthropic only |
305
+ | `:memory` | Persistent memory | Native | Anthropic only |
306
+ | `:pptx` | Create/edit PowerPoint | Anthropic Agent Skill | Anthropic only |
307
+ | `:xlsx` | Create/edit Excel | Anthropic Agent Skill | Anthropic only |
308
+ | `:docx` | Create/edit Word | Anthropic Agent Skill | Anthropic only |
309
+ | `:pdf` | Generate PDFs | Anthropic Agent Skill | Anthropic only |
310
+
311
+ On **Anthropic**, skills run on their servers — the gem passes the right API payloads (including document skills and `code_execution` when needed).
312
+
313
+ On **other providers**, `:web_search` and `:web_fetch` use portable Ruby implementations so the same DSL works everywhere.
314
+
315
+ ### Skill options
316
+
317
+ ```ruby
318
+ skills :web_search, max_uses: 5, allowed_domains: ["wikipedia.org"]
319
+ # or
320
+ skills web_search: { max_uses: 5 }, :xlsx
321
+ ```
322
+
323
+ ### Anthropic document skills + file download
324
+
325
+ Document skills (`:pptx`, `:xlsx`, `:docx`, `:pdf`) run on Anthropic's servers. Generated files are **automatically downloaded** via the Files API:
326
+
327
+ ```ruby
328
+ class ReportBuilder < RailsAgents::Agent
329
+ provider :anthropic
330
+ model "claude-sonnet-4-20250514"
331
+ description "Build a quarterly sales spreadsheet with charts."
332
+ skills :xlsx
333
+ end
334
+
335
+ result = ReportBuilder.run("Create Q1 sales report", save_files_to: "tmp/reports")
336
+ result.output # => "Created your spreadsheet..."
337
+ result.files.first.filename # => "report.xlsx"
338
+ result.files.first.path # => "tmp/reports/report.xlsx"
339
+ ```
340
+
341
+ ```ruby
342
+ RailsAgents.configure do |config|
343
+ config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"]
344
+ config.anthropic_auto_download_files = true # default
345
+ config.anthropic_files_directory = Rails.root.join("tmp/rails_agents/files")
346
+ end
347
+ ```
348
+
349
+ `:pptx`, `:xlsx`, `:docx`, and `:pdf` automatically enable `code_execution`, attach Anthropic's published skills, and include the required beta headers.
350
+
351
+ ### Custom Anthropic skills
352
+
353
+ Upload skills via the [Anthropic Skills API](https://platform.claude.com/docs/en/build-with-claude/skills-guide), then reference by ID:
354
+
355
+ ```ruby
356
+ skills :web_search, "skill_01AbCdEfGhIjKlMnOpQrStUv"
357
+ ```
358
+
359
+ ---
360
+
361
+ ## Providers
362
+
363
+ | Provider | API key | Example model |
364
+ |----------|---------|---------------|
365
+ | `:openai` | `openai_api_key` | `"gpt-4o-mini"` |
366
+ | `:anthropic` | `anthropic_api_key` | `"claude-sonnet-4-20250514"` |
367
+ | `:openrouter` | `openrouter_api_key` | `"meta-llama/llama-3.3-70b-instruct:free"` |
368
+ | `:grok` | `grok_api_key` | `"grok-2-latest"` |
369
+
370
+ OpenRouter gives access to hundreds of open-source models through one API key — useful for trying agents without committing to a single vendor.
371
+
372
+ ---
373
+
374
+ ## Try it
375
+
376
+ ### Run the test suite (no API keys)
377
+
378
+ Tests use fakes and WebMock — fast, no network:
379
+
380
+ ```bash
381
+ git clone https://github.com/Tiny-Bubble-Company/rails-agents.git
382
+ cd rails-agents
383
+ bundle install
384
+ bundle exec rspec
385
+ ```
386
+
387
+ ### Sample playground app
388
+
389
+ A minimal Rails app at `spec/dummy/` for trying agents in a browser or console:
390
+
391
+ ```bash
392
+ bin/setup
393
+ cd spec/dummy
394
+ export OPENAI_API_KEY=sk-...
395
+ export ANTHROPIC_API_KEY=sk-ant-... # WebResearchAgent, SheetBuilderAgent
396
+ bin/rails server
397
+ ```
398
+
399
+ Open **http://localhost:3000** — pick an agent, send input, inspect the result.
400
+
401
+ | Agent | What it demonstrates |
402
+ |-------|----------------------|
403
+ | `HelloAgent` | OpenAI, basic `.run` |
404
+ | `LeadQualifier` | Custom tools |
405
+ | `WebResearchAgent` | Anthropic `:web_search` skill |
406
+ | `SheetBuilderAgent` | Anthropic `:xlsx` skill + file download |
407
+
408
+ See [`spec/dummy/README.md`](spec/dummy/README.md) for console examples.
409
+
410
+ > **Note:** Running agents in the playground calls live APIs and can take several seconds. The page itself loads instantly; only the "Run agent" action hits the network.
411
+
412
+ ### Recipes
413
+
414
+ **Tools:**
415
+
416
+ ```ruby
417
+ # app/agents/tools/current_time.rb
418
+ class CurrentTime < RailsAgents::Tool
419
+ description "Returns the current time in UTC"
420
+ def call = Time.now.utc.iso8601
421
+ end
422
+
423
+ class ClockAgent < RailsAgents::Agent
424
+ provider :openai
425
+ model "gpt-4o-mini"
426
+ description "Tell the user the current time using the tool."
427
+ tools "CurrentTime"
428
+ end
429
+
430
+ ClockAgent.run("What time is it?")
431
+ ```
432
+
433
+ **OpenRouter (free models):**
434
+
435
+ ```ruby
436
+ class CheapAgent < RailsAgents::Agent
437
+ provider :openrouter
438
+ model "meta-llama/llama-3.3-70b-instruct:free"
439
+ description "Answer briefly."
440
+ end
441
+
442
+ CheapAgent.run("What is Rails?")
443
+ ```
444
+
445
+ ---
446
+
447
+ ## Documentation
448
+
449
+ - **Website / docs:** [https://tiny-bubble-company.github.io/rails-agents/](https://tiny-bubble-company.github.io/rails-agents/)
450
+ - **Local docs:** `cd docs && npm install && npm run dev`
451
+
452
+ ---
453
+
454
+ ## Requirements
455
+
456
+ - Ruby 3.2+
457
+ - Rails 7.1+
458
+
459
+ ---
460
+
461
+ ## License
462
+
463
+ MIT — © Tiny Bubble Company
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module RailsAgents
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("templates", __dir__)
9
+ desc "Add Rails Agents config and agent directories"
10
+
11
+ def create_initializer
12
+ template "initializer.rb", "config/initializers/rails_agents.rb"
13
+ end
14
+
15
+ def create_directories
16
+ empty_directory "app/agents"
17
+ empty_directory "app/agents/tools"
18
+ create_file "app/agents/.keep"
19
+ create_file "app/agents/tools/.keep"
20
+ end
21
+
22
+ def finish
23
+ say "\n✓ Rails Agents installed.", :green
24
+ say " 1. Set API keys in config/initializers/rails_agents.rb"
25
+ say " 2. Create agents in app/agents/"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ RailsAgents.configure do |config|
2
+ config.openai_api_key = ENV["OPENAI_API_KEY"]
3
+ config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"]
4
+ config.openrouter_api_key = ENV["OPENROUTER_API_KEY"]
5
+ config.grok_api_key = ENV["XAI_API_KEY"]
6
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Bundler default require for gem "rails-agent-stack"
4
+ require_relative "../../rails_agents"
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module RailsAgents
6
+ class Agent
7
+ class << self
8
+ attr_reader :provider_name, :model_name, :description_text, :tool_classes,
9
+ :max_turns_config, :discover_tools_config
10
+
11
+ def inherited(subclass)
12
+ super
13
+ subclass.instance_variable_set(:@tool_classes, Array(tool_classes).dup)
14
+ subclass.instance_variable_set(:@skill_declarations, Array(skill_declarations).dup)
15
+ subclass.instance_variable_set(:@discover_tools_config, discover_tools_config)
16
+ end
17
+
18
+ def provider(value = nil)
19
+ return @provider_name if value.nil?
20
+ @provider_name = value.to_sym
21
+ end
22
+
23
+ def model(value = nil)
24
+ return @model_name if value.nil?
25
+ @model_name = value
26
+ end
27
+
28
+ def description(value = nil, &block)
29
+ return @description_text if value.nil? && !block
30
+ @description_text = block || value
31
+ end
32
+
33
+ def tools(*values)
34
+ return @tool_classes || [] if values.empty?
35
+ @tool_classes = values.flatten
36
+ end
37
+
38
+ def skills(*args, **kwargs)
39
+ return skill_declarations if args.empty? && kwargs.empty?
40
+
41
+ @skill_declarations ||= []
42
+ args.each do |arg|
43
+ if arg.is_a?(Hash)
44
+ arg.each { |name, options| merge_skill(name, options) }
45
+ else
46
+ merge_skill(arg, kwargs)
47
+ end
48
+ end
49
+ @skill_declarations
50
+ end
51
+
52
+ def skill_declarations
53
+ @skill_declarations || []
54
+ end
55
+
56
+ def max_turns(value = nil)
57
+ return @max_turns_config || Runner::DEFAULT_TURNS if value.nil?
58
+ @max_turns_config = value
59
+ end
60
+
61
+ # When false, only declared tools and portable skill tools are available.
62
+ # Useful when app/agents/tools contains tools for multiple agents.
63
+ def discover_tools(value = nil)
64
+ return @discover_tools_config.nil? ? true : @discover_tools_config if value.nil?
65
+ @discover_tools_config = value
66
+ end
67
+
68
+ def resolved_provider
69
+ provider_name || RailsAgents.config.default_provider
70
+ end
71
+
72
+ def resolved_model
73
+ model_name || raise(ConfigurationError, "Add a model to #{name}. Example: model \"gpt-4o-mini\"")
74
+ end
75
+
76
+ def provider_client
77
+ Providers.build(resolved_provider)
78
+ end
79
+
80
+ def skill_set
81
+ @skill_set ||= SkillSet.new(declarations: skill_declarations, provider: resolved_provider).tap(&:validate!)
82
+ end
83
+
84
+ def tool_set
85
+ discovered = discover_tools && defined?(Rails) ? ToolSet.from_directory : ToolSet.new
86
+ portable = ToolSet.new(*skill_set.portable_tool_classes)
87
+ declared = ToolSet.new(*tool_classes)
88
+ declared.+(portable).+(discovered)
89
+ end
90
+
91
+ def render_instructions(context = {})
92
+ text = description_text
93
+ case text
94
+ when Proc then text.call(context)
95
+ when nil then raise ConfigurationError, "Add a description to #{name}. Example: description \"What this agent does\""
96
+ else text.to_s
97
+ end
98
+ end
99
+
100
+ def run(input = nil, save_files_to: nil, callbacks: {}, parse_json: false, **context)
101
+ Runner.new(
102
+ self,
103
+ input: input.nil? ? context : input,
104
+ context: context.merge(save_files_to:),
105
+ callbacks: callbacks,
106
+ parse_json: parse_json
107
+ ).call
108
+ end
109
+
110
+ alias ask run
111
+ alias call run
112
+
113
+ private
114
+
115
+ def merge_skill(name, options)
116
+ key = name.to_s
117
+ existing = skill_declarations.find { |declaration| declaration.key.to_s == key }
118
+ merged_options = existing ? existing.options.merge(options) : options.dup
119
+
120
+ if existing
121
+ @skill_declarations[skill_declarations.index(existing)] = SkillDeclaration.new(key: existing.key, options: merged_options)
122
+ else
123
+ @skill_declarations << SkillDeclaration.new(key: name, options: merged_options)
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end