layered-assistant-rails 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 (105) hide show
  1. checksums.yaml +7 -0
  2. data/AGENTS.md +94 -0
  3. data/LICENSE +201 -0
  4. data/README.md +176 -0
  5. data/Rakefile +15 -0
  6. data/app/assets/tailwind/layered/assistant/styles.css +13 -0
  7. data/app/controllers/concerns/layered/assistant/message_creation.rb +49 -0
  8. data/app/controllers/concerns/layered/assistant/public/session_conversations.rb +50 -0
  9. data/app/controllers/layered/assistant/application_controller.rb +25 -0
  10. data/app/controllers/layered/assistant/assistants_controller.rb +59 -0
  11. data/app/controllers/layered/assistant/conversations_controller.rb +77 -0
  12. data/app/controllers/layered/assistant/messages_controller.rb +57 -0
  13. data/app/controllers/layered/assistant/models_controller.rb +61 -0
  14. data/app/controllers/layered/assistant/panel/conversations_controller.rb +63 -0
  15. data/app/controllers/layered/assistant/panel/messages_controller.rb +44 -0
  16. data/app/controllers/layered/assistant/providers_controller.rb +55 -0
  17. data/app/controllers/layered/assistant/public/application_controller.rb +16 -0
  18. data/app/controllers/layered/assistant/public/assistants_controller.rb +16 -0
  19. data/app/controllers/layered/assistant/public/conversations_controller.rb +33 -0
  20. data/app/controllers/layered/assistant/public/messages_controller.rb +42 -0
  21. data/app/controllers/layered/assistant/public/panel/conversations_controller.rb +62 -0
  22. data/app/controllers/layered/assistant/public/panel/messages_controller.rb +50 -0
  23. data/app/controllers/layered/assistant/setup_controller.rb +9 -0
  24. data/app/helpers/layered/assistant/access_helper.rb +45 -0
  25. data/app/helpers/layered/assistant/messages_helper.rb +41 -0
  26. data/app/helpers/layered/assistant/panel_helper.rb +38 -0
  27. data/app/javascript/layered_assistant/composer_controller.js +30 -0
  28. data/app/javascript/layered_assistant/index.js +14 -0
  29. data/app/javascript/layered_assistant/message_streaming.js +124 -0
  30. data/app/javascript/layered_assistant/messages_controller.js +62 -0
  31. data/app/javascript/layered_assistant/panel_controller.js +36 -0
  32. data/app/javascript/layered_assistant/panel_nav_controller.js +16 -0
  33. data/app/javascript/layered_assistant/provider_template_controller.js +45 -0
  34. data/app/javascript/layered_assistant/vendor/marked.esm.js +72 -0
  35. data/app/jobs/layered/assistant/application_job.rb +6 -0
  36. data/app/jobs/layered/assistant/messages/response_job.rb +36 -0
  37. data/app/models/layered/assistant/application_record.rb +7 -0
  38. data/app/models/layered/assistant/assistant.rb +22 -0
  39. data/app/models/layered/assistant/conversation.rb +39 -0
  40. data/app/models/layered/assistant/message.rb +56 -0
  41. data/app/models/layered/assistant/model.rb +21 -0
  42. data/app/models/layered/assistant/provider.rb +49 -0
  43. data/app/services/layered/assistant/chunk_service.rb +80 -0
  44. data/app/services/layered/assistant/client_service.rb +18 -0
  45. data/app/services/layered/assistant/clients/anthropic.rb +24 -0
  46. data/app/services/layered/assistant/clients/base.rb +29 -0
  47. data/app/services/layered/assistant/clients/openai.rb +33 -0
  48. data/app/services/layered/assistant/messages_service.rb +58 -0
  49. data/app/services/layered/assistant/models/create_service.rb +50 -0
  50. data/app/services/layered/assistant/token_estimator.rb +11 -0
  51. data/app/views/layered/assistant/assistants/_form.html.erb +42 -0
  52. data/app/views/layered/assistant/assistants/edit.html.erb +6 -0
  53. data/app/views/layered/assistant/assistants/index.html.erb +45 -0
  54. data/app/views/layered/assistant/assistants/new.html.erb +6 -0
  55. data/app/views/layered/assistant/conversations/_form.html.erb +29 -0
  56. data/app/views/layered/assistant/conversations/edit.html.erb +6 -0
  57. data/app/views/layered/assistant/conversations/index.html.erb +63 -0
  58. data/app/views/layered/assistant/conversations/new.html.erb +6 -0
  59. data/app/views/layered/assistant/conversations/show.html.erb +25 -0
  60. data/app/views/layered/assistant/messages/_composer.html.erb +15 -0
  61. data/app/views/layered/assistant/messages/_message.html.erb +25 -0
  62. data/app/views/layered/assistant/messages/_system_prompt.html.erb +10 -0
  63. data/app/views/layered/assistant/messages/create.turbo_stream.erb +9 -0
  64. data/app/views/layered/assistant/messages/index.html.erb +46 -0
  65. data/app/views/layered/assistant/models/_form.html.erb +30 -0
  66. data/app/views/layered/assistant/models/edit.html.erb +6 -0
  67. data/app/views/layered/assistant/models/index.html.erb +54 -0
  68. data/app/views/layered/assistant/models/new.html.erb +6 -0
  69. data/app/views/layered/assistant/panel/conversations/_header.html.erb +23 -0
  70. data/app/views/layered/assistant/panel/conversations/index.html.erb +46 -0
  71. data/app/views/layered/assistant/panel/conversations/new.html.erb +23 -0
  72. data/app/views/layered/assistant/panel/conversations/show.html.erb +24 -0
  73. data/app/views/layered/assistant/panel/messages/_composer.html.erb +15 -0
  74. data/app/views/layered/assistant/panel/messages/create.turbo_stream.erb +9 -0
  75. data/app/views/layered/assistant/providers/_form.html.erb +81 -0
  76. data/app/views/layered/assistant/providers/edit.html.erb +6 -0
  77. data/app/views/layered/assistant/providers/index.html.erb +47 -0
  78. data/app/views/layered/assistant/providers/new.html.erb +6 -0
  79. data/app/views/layered/assistant/public/assistants/index.html.erb +34 -0
  80. data/app/views/layered/assistant/public/assistants/show.html.erb +23 -0
  81. data/app/views/layered/assistant/public/conversations/show.html.erb +24 -0
  82. data/app/views/layered/assistant/public/messages/_composer.html.erb +7 -0
  83. data/app/views/layered/assistant/public/messages/create.turbo_stream.erb +9 -0
  84. data/app/views/layered/assistant/public/panel/conversations/_header.html.erb +16 -0
  85. data/app/views/layered/assistant/public/panel/conversations/index.html.erb +48 -0
  86. data/app/views/layered/assistant/public/panel/conversations/new.html.erb +17 -0
  87. data/app/views/layered/assistant/public/panel/conversations/show.html.erb +23 -0
  88. data/app/views/layered/assistant/public/panel/messages/_composer.html.erb +7 -0
  89. data/app/views/layered/assistant/public/panel/messages/create.turbo_stream.erb +9 -0
  90. data/app/views/layered/assistant/setup/_setup.html.erb +121 -0
  91. data/app/views/layered/assistant/setup/index.html.erb +2 -0
  92. data/app/views/layouts/layered/assistant/_host_navigation.html.erb +0 -0
  93. data/app/views/layouts/layered/assistant/application.html.erb +32 -0
  94. data/config/importmap.rb +8 -0
  95. data/config/routes.rb +31 -0
  96. data/data/models.json +42 -0
  97. data/db/migrate/20260312000000_create_layered_assistant_tables.rb +63 -0
  98. data/lib/generators/layered/assistant/install_generator.rb +113 -0
  99. data/lib/generators/layered/assistant/migrations_generator.rb +47 -0
  100. data/lib/generators/layered/assistant/templates/initializer.rb +26 -0
  101. data/lib/layered/assistant/engine.rb +29 -0
  102. data/lib/layered/assistant/version.rb +5 -0
  103. data/lib/layered/assistant.rb +19 -0
  104. data/lib/layered-assistant-rails.rb +1 -0
  105. metadata +449 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7b9f6574ccdd2e1fe605f6f74bcb50feb69c6eca079a5d5add6bc1b615488972
4
+ data.tar.gz: cc618f8dde0d9211ae7445dc10f0f75f6e63012a5b3c52a66afbca595ed1aa7f
5
+ SHA512:
6
+ metadata.gz: 1b853a8464bf2eb6507d8287cefc46c51268e908fe0c6045e6d2265084fd829aec1b6ebfb92cf2a35f41ca15f3a85a19bc1c756c91678e6d872c3234888ff443
7
+ data.tar.gz: ca37eee8b57612ab350522dec5cbc46c334ed168043d2c8ee581813d1ba224ae42e5c685618669f2af76b61c0c81c46eb044564a3008de3c137ce670cf987c81
data/AGENTS.md ADDED
@@ -0,0 +1,94 @@
1
+ # AGENTS.md
2
+
3
+ This file provides guidance to AI agents when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ **layered-assistant-rails** is a Rails 8+ engine gem (`Layered::Assistant`) providing AI assistant UI components. It uses `isolate_namespace Layered::Assistant` and distributes assets (Tailwind CSS + importmap JS) to host applications via a generator.
8
+
9
+ Requires Rails >= 8.0.0, Ruby >= 3.2.0. Depends on sibling gem `layered-ui-rails` (path dependency at `../layered-ui-rails`).
10
+
11
+ ## Architecture
12
+
13
+ ### Engine Setup
14
+
15
+ - **Entry point**: `lib/layered-assistant-rails.rb` → `lib/layered/assistant.rb`
16
+ - **Engine**: `lib/layered/assistant/engine.rb` — registers importmap paths and JS asset paths via two initializers
17
+ - **Namespace**: All models/controllers use `Layered::Assistant::*`, tables prefixed `layered_assistant_*`
18
+
19
+ ### Asset Distribution
20
+
21
+ CSS and JS are authored in the engine and delivered to host apps:
22
+
23
+ - **CSS**: `app/assets/tailwind/layered/assistant/styles.css` — copied to host app by the install generator
24
+ - **JS**: `app/javascript/layered_assistant/index.js` — pinned via `config/importmap.rb`, made available through engine initializer
25
+
26
+ The install generator (`lib/generators/layered/assistant/install_generator.rb`) verifies `layered-ui-rails` is installed first, then copies CSS and injects import lines into `application.css` and `application.js`.
27
+
28
+ ### Test Harness
29
+
30
+ `test/dummy/` is a full Rails 8 app with Devise authentication used for development and testing. Test helper loads migrations from both `db/migrate/` (engine) and `test/dummy/db/migrate/`.
31
+
32
+ You can run the tests with: `bundle exec rake test`
33
+
34
+ ## Conventions
35
+
36
+ ### Database
37
+
38
+ - Enums stored as strings (not integers)
39
+ - Tables prefixed `layered_assistant_`
40
+ - Models inherit from `Layered::Assistant::ApplicationRecord`
41
+
42
+ ### Generators
43
+
44
+ Verify dependencies before modifying host app files. Use `inject_into_file` for safe insertion. Check for existing imports to ensure idempotency.
45
+
46
+ ## Making changes to `layered-ui-rails`
47
+
48
+ The UI kit gem for this app is located at `../layered-ui-rails`.
49
+ You can find and make edits to that project directly if necessary.
50
+ It has its own `AGENTS.md` file — inspect it for context on the apps's conventions, architecture, and development workflows.
51
+
52
+ ## Conventions
53
+
54
+ - Use "layered-ui-assistant" not "Layered UI" when referring to the project
55
+ - Use normal dashes '-' not long ones '—'
56
+ - Locale: Favour en-GB (British English), unless terms are defined by technical standards (e.g. LICENSE, COLOR).
57
+ - Titles: capitalise first word only (e.g. "This title")
58
+ - Document new styles in the dummy app
59
+ - Use l-ui classes only in engine views, with no additional Tailwind utilities, as Tailwind classes referenced only inside the engine will not be generated by the host app’s build
60
+ - Importmap for JS (no bundler)
61
+
62
+ - CRITICAL: Retain WCAG 2.2 AA compliance. Do not be too pedantic though as this introduces audit loops which are undesirable.
63
+ - A WCAG 2.2 AA table looks like this:
64
+ ```
65
+ <table>
66
+ <caption>Agent availability by region</caption>
67
+
68
+ <thead>
69
+ <tr>
70
+ <th scope="col">Agent</th>
71
+ <th scope="col">Region</th>
72
+ <th scope="col">Status</th>
73
+ </tr>
74
+ </thead>
75
+
76
+ <tbody>
77
+ <tr>
78
+ <th scope="row">Agent A</th>
79
+ <td>UK</td>
80
+ <td>Available</td>
81
+ </tr>
82
+ <tr>
83
+ <th scope="row">Agent B</th>
84
+ <td>EU</td>
85
+ <td>Busy</td>
86
+ </tr>
87
+ <tr>
88
+ <th scope="row">Agent C</th>
89
+ <td>US</td>
90
+ <td>Offline</td>
91
+ </tr>
92
+ </tbody>
93
+ </table>
94
+ ```
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 LAYERED AI LIMITED (UK company number: 17056830)
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,176 @@
1
+ # layered-assistant-rails
2
+
3
+ [![CI](https://github.com/layered-ai-public/layered-ui-rails/actions/workflows/ci.yml/badge.svg)](https://github.com/layered-ai-public/layered-assistant-rails/actions/workflows/ci.yml)
4
+ [![WCAG 2.2 AA](https://img.shields.io/badge/WCAG_2.2-AA-green)](https://www.w3.org/WAI/WCAG22/quickref/)
5
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
+ [![Website](https://img.shields.io/badge/Website-layered.ai-purple)](https://www.layered.ai/)
7
+ [![GitHub](https://img.shields.io/badge/GitHub-layered--assistant--rails-black)](https://github.com/layered-ai-public/layered-assistant-rails)
8
+ [![Discord](https://img.shields.io/badge/Discord-join-5865F2)](https://discord.gg/aCGqz9Bx)
9
+
10
+ An open source Rails 8+ engine built on [layered-ui-rails](https://github.com/layered-ai-public/layered-ui-rails) that provides a multi-provider AI assistant with streaming responses and a full conversation UI.
11
+
12
+ ## Requirements
13
+
14
+ - Ruby >= 3.2.0
15
+ - Ruby on Rails >= 8.0
16
+ - [layered-ui-rails](https://github.com/layered-ai-public/layered-ui-rails) installed in the host app
17
+
18
+ ## Installation
19
+
20
+ Add to your Gemfile:
21
+
22
+ ```ruby
23
+ gem "layered-assistant-rails"
24
+ ```
25
+
26
+ Then run:
27
+
28
+ ```bash
29
+ bundle install
30
+ ```
31
+
32
+ ## Setup
33
+
34
+ ### Install generator
35
+
36
+ Run the install generator to copy CSS and register imports:
37
+
38
+ ```bash
39
+ bin/rails generate layered:assistant:install
40
+ ```
41
+
42
+ This will:
43
+ - Copy `layered_ui.css` to `app/assets/tailwind/`
44
+ - Add `@import "./layered_ui";` to your `application.css`
45
+ - Add `import "layered_ui"` to your `application.js`
46
+ - Copy `layered_assistant.css` to `app/assets/tailwind/layered_assistant.css`
47
+ - Add `@import "./layered_assistant";` to your `app/assets/tailwind/application.css` (after the layered-ui import)
48
+ - Add `import "layered_assistant"` to your `app/javascript/application.js` (after the layered-ui import)
49
+ - Mount the engine at `/layered/assistant` in your `config/routes.rb`
50
+ - Copy engine migrations into your application
51
+
52
+ All steps are idempotent - re-running the generator will not duplicate imports, routes, or migrations.
53
+
54
+ ## Authorization
55
+
56
+ All non-public engine routes are **blocked by default** (403 Forbidden) until you configure an `authorize` block. The install generator creates a starter initialiser at `config/initializers/layered_assistant.rb` - uncomment one of the examples to get started.
57
+
58
+ Once configured, visit `/layered/assistant` (or wherever you mounted the engine) to verify access.
59
+
60
+ ### Allow all requests
61
+
62
+ ```ruby
63
+ Layered::Assistant.authorize do
64
+ # No-op: all requests permitted
65
+ end
66
+ ```
67
+
68
+ ### Require sign-in (Devise)
69
+
70
+ ```ruby
71
+ Layered::Assistant.authorize do
72
+ redirect_to main_app.new_user_session_path unless user_signed_in?
73
+ end
74
+ ```
75
+
76
+ ### Restrict to admins
77
+
78
+ ```ruby
79
+ Layered::Assistant.authorize do
80
+ head :forbidden unless current_user&.admin?
81
+ end
82
+ ```
83
+
84
+ The block runs in controller context, so you have access to `request`, `current_user`, `redirect_to`, `head`, `main_app`, and all other controller methods.
85
+
86
+ ### Checking access in views
87
+
88
+ The `l_assistant_accessible?` helper evaluates the authorize block without side effects. Use it to conditionally show navigation or links to the engine:
89
+
90
+ ```erb
91
+ <% if l_assistant_accessible? %>
92
+ <%= link_to "Assistant", layered_assistant.root_path %>
93
+ <% end %>
94
+ ```
95
+
96
+ ## Panel helpers
97
+
98
+ The engine provides two convenience helpers for wiring the layered-ui panel to the assistant. Use them inside `content_for` blocks in your application layout:
99
+
100
+ ```erb
101
+ <% content_for :l_ui_panel_heading do %>
102
+ <%= layered_assistant_panel_header %>
103
+ <% end %>
104
+
105
+ <% content_for :l_ui_panel_body do %>
106
+ <%= layered_assistant_panel_body %>
107
+ <% end %>
108
+
109
+ <%= render template: "layouts/layered_ui/application" %>
110
+ ```
111
+
112
+ Both helpers accept keyword arguments that are forwarded as HTML attributes to the underlying `turbo_frame_tag`:
113
+
114
+ ```erb
115
+ <%= layered_assistant_panel_body data: { controller: "panel" } %>
116
+ ```
117
+
118
+ | Helper | Description |
119
+ |---|---|
120
+ | `layered_assistant_panel_header` | Empty Turbo Frame (`assistant_panel_header`) populated by the engine's panel views |
121
+ | `layered_assistant_panel_body` | Turbo Frame (`assistant_panel`) that loads the conversation list from the engine's panel routes |
122
+
123
+ ## Configuration
124
+
125
+ ### Environment Variables
126
+
127
+ | Variable | Default | Description |
128
+ |---|---|---|
129
+ | `LAYERED_ASSISTANT_DANGEROUSLY_SKIP_DB_ENCRYPTION` | `nil` | Set to `"yes"` to skip Active Record Encryption on `Provider#secret`. Only for development/test environments without encryption keys configured |
130
+ | `LAYERED_ASSISTANT_LOG_ERRORS` | `nil` | Set to `"yes"` to enable error logging from the AI API clients |
131
+
132
+ ## Demo
133
+
134
+ A dummy Rails app is included for development and testing:
135
+
136
+ ```bash
137
+ cd test/dummy
138
+ bin/setup
139
+ bin/dev
140
+ ```
141
+
142
+ Then visit `http://localhost:3000`.
143
+
144
+ ### Deploying the dummy app
145
+
146
+ The dummy app can be deployed with [Kamal](https://kamal-deploy.org). Set the required environment variables and deploy from `test/dummy`:
147
+
148
+ ```bash
149
+ cd test/dummy
150
+ export KAMAL_DEPLOY_IP=<server-ip>
151
+ export KAMAL_DEPLOY_DOMAIN=<domain>
152
+ export KAMAL_SSH_KEY=<path-to-ssh-key>
153
+ kamal deploy
154
+ ```
155
+
156
+ ## Testing
157
+
158
+ Run the gem tests from the root directory:
159
+
160
+ ```bash
161
+ bundle exec rake test
162
+ ```
163
+
164
+ ## License
165
+
166
+ Released under the [Apache 2.0 License](LICENSE).
167
+
168
+ Copyright 2026 LAYERED AI LIMITED (UK company number: 17056830). See [NOTICE](NOTICE) for attribution details.
169
+
170
+ ## Trademarks
171
+
172
+ The source code is fully open, but the layered.ai name, logo, and brand assets are trademarks of LAYERED AI LIMITED. The Apache 2.0 license does not grant rights to use the layered.ai branding. Forks and redistributions must use a distinct name. See [TRADEMARK.md](TRADEMARK.md) for the full policy.
173
+
174
+ ## Contributing
175
+
176
+ - [CLA.md](CLA.md) - contributor license agreement
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"
7
+
8
+ require "rake/testtask"
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << "test"
12
+ t.pattern = "test/**/*_test.rb"
13
+ end
14
+
15
+ task default: :test
@@ -0,0 +1,13 @@
1
+ /*
2
+ * CSS Formatting Convention
3
+ *
4
+ * All @apply directives use multi-line formatting with logical grouping:
5
+ * 1. Layout (flex, block, fixed, relative, etc.)
6
+ * 2. Spacing (padding, margin, gap, width, height)
7
+ * 3. Typography (text, font)
8
+ * 4. Colors (bg, text, border)
9
+ * 5. Effects (hover, transitions, shadows, etc.)
10
+ *
11
+ * Single-utility classes may remain on one line for brevity.
12
+ * This structure improves readability, maintainability, and makes diffs easier to review.
13
+ */
@@ -0,0 +1,49 @@
1
+ module Layered
2
+ module Assistant
3
+ module MessageCreation
4
+ private
5
+
6
+ def create_messages_for(conversation:, content:, model_id:)
7
+ message = conversation.messages.create(
8
+ role: :user,
9
+ content: content,
10
+ model_id: model_id,
11
+ input_tokens: TokenEstimator.estimate(content),
12
+ tokens_estimated: true
13
+ )
14
+
15
+ return { message: message } unless message.persisted?
16
+
17
+ conversation.update_name_from_content!(content)
18
+ message.broadcast_created
19
+
20
+ models = Model.available
21
+ selected_model_id = model_id
22
+ assistant_message = nil
23
+ error = nil
24
+
25
+ begin
26
+ assistant_message = conversation.messages.create!(
27
+ role: :assistant,
28
+ content: nil,
29
+ model_id: model_id
30
+ )
31
+ assistant_message.broadcast_created
32
+
33
+ Messages::ResponseJob.perform_later(assistant_message.id)
34
+ rescue => e
35
+ Rails.logger.error("Assistant response failed: #{e.message}")
36
+ error = "Something went wrong while generating a response."
37
+ end
38
+
39
+ {
40
+ message: message,
41
+ assistant_message: assistant_message,
42
+ models: models,
43
+ selected_model_id: selected_model_id,
44
+ error: error
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,50 @@
1
+ module Layered
2
+ module Assistant
3
+ module Public
4
+ module SessionConversations
5
+ extend ActiveSupport::Concern
6
+
7
+ private
8
+
9
+ def session_conversation_ids
10
+ session[:layered_assistant_conversation_ids] ||= []
11
+ end
12
+
13
+ MAX_SESSION_CONVERSATIONS = 50
14
+
15
+ def add_conversation_to_session(conversation)
16
+ ids = session_conversation_ids
17
+ ids << conversation.id unless ids.include?(conversation.id)
18
+ ids.shift while ids.size > MAX_SESSION_CONVERSATIONS
19
+ end
20
+
21
+ def find_session_conversation(id)
22
+ id = id.to_i
23
+ unless session_conversation_ids.include?(id)
24
+ raise ActiveRecord::RecordNotFound, "Conversation not found in session"
25
+ end
26
+
27
+ Conversation.joins(:assistant).merge(Assistant.publicly_available).find(id)
28
+ rescue ActiveRecord::RecordNotFound
29
+ remove_conversation_from_session(id)
30
+ raise
31
+ end
32
+
33
+ def remove_conversation_from_session(id)
34
+ session_conversation_ids.delete(id.to_i)
35
+ end
36
+
37
+ def existing_session_conversation_for(assistant)
38
+ return nil if session_conversation_ids.empty?
39
+
40
+ Conversation
41
+ .joins(:assistant)
42
+ .merge(Assistant.publicly_available)
43
+ .where(id: session_conversation_ids, assistant: assistant)
44
+ .order(created_at: :desc)
45
+ .first
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,25 @@
1
+ module Layered
2
+ module Assistant
3
+ class ApplicationController < ActionController::Base
4
+ include Pagy::Method
5
+ include Layered::Ui::AuthenticationHelper
6
+
7
+ before_action :layered_assistant_authorize!
8
+
9
+ helper Rails.application.routes.url_helpers
10
+
11
+ private
12
+
13
+ def layered_assistant_authorize!
14
+ block = Layered::Assistant.authorize_block
15
+
16
+ unless block
17
+ head :forbidden
18
+ return
19
+ end
20
+
21
+ instance_exec(&block)
22
+ end
23
+ end
24
+ end
25
+ end