action_prompt 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d077c592d1944448a6fee2d5eb1184c3e8a3c42cccb21929fdae60fa9806aed2
4
- data.tar.gz: 347712c9bc12ce13c43fe3a983c29a90776426e1f2d88a2ef7ea8477cb1aae3a
3
+ metadata.gz: aadef0f0c0b9de5ed1dcc33d550137a92b69fcd84b003dbc081d25f9e741e3f4
4
+ data.tar.gz: 023eabdc185943d87c587f424b20b71cc425ca8d59b13688ffc54133ef19eca0
5
5
  SHA512:
6
- metadata.gz: 17ffba4a0d8ec79e77b25c1337bacfbe83523976cfa69f3c26d4272da9d859e1d4821575b6695073916b5bac71cd7ba990924dbdcc37acb683363822f488fe55
7
- data.tar.gz: ee62f030e9df50de5b0a5783bf103d036a028e6cdaab5c829a97021892595b2bbfd2ecc795e1f8f9a04032370cce31dcc4f11df2cab5d3e872f1058d61f7ac81
6
+ metadata.gz: cfe7c7884e48024e04187aa8a92390994d84300c022fd0f4dc780aa79a01e083f2c79f703a964dcf22be7d81ae26de5d936a741773cc3267a019572d0db99834
7
+ data.tar.gz: 7762444c108aaee485b964e90002ad2361db3fb46f38a5dcb08697af38068535f88126bb3d326157f1d87c0b2f41c0fa86499473c929d3d6780ddb4941f6ba11
@@ -0,0 +1,22 @@
1
+ class ActionPrompt::PreviewsController < ActionController::Base
2
+ prepend_view_path ActionPrompt::Engine.root.join("app", "views")
3
+ layout "application"
4
+ # Ideally, we'd like to inhert from Rails::ApplicationController, but that
5
+ # would prevent us from using a Tailwind CDN. So instead, we're using ActionController::Base.
6
+ #
7
+ # If we are able to use Rails::ApplicationController, then re-enable this line
8
+ # before_action :require_local!
9
+
10
+ def index
11
+ @page_title = "Action Prompt Previews"
12
+ @previews = ActionPrompt::Preview.all
13
+ end
14
+
15
+ def show
16
+ preview_class_name = params[:preview_name].camelize + "Preview"
17
+ @preview_class = ActionPrompt::Preview.find(preview_class_name)
18
+ slug = "#{params[:preview_name]}/#{params[:prompt_name]}"
19
+ @prompt = @preview_class.find_prompt(slug)
20
+ @prompt_output = @preview_class.new.send(params[:prompt_name].to_sym)
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ <h1 class="text-xl font-semibold">Action Prompt Previews</h1>
2
+ <div class="py-4">
3
+ <% if @previews.blank? %>
4
+ <p>You have not defined any Action Prompt Previews.</p>
5
+ <!-- TODO: Add a link to the Action Prompt documentation here -->
6
+ <% else %>
7
+ <div class="space-y-4">
8
+ <% @previews.each do |preview| %>
9
+ <div>
10
+ <h2 class="text-lg font-semibold"><%= preview.preview_name.titleize %></h2>
11
+ <ul>
12
+ <% preview.prompts.each do |prompt| %>
13
+ <li><%= link_to prompt.name, "/action_prompt/previews/#{prompt.slug}", class: "cursor-pointer text-blue-500 underline hover:text-blue-600" %></li>
14
+ <% end %>
15
+ </ul>
16
+ </div>
17
+ <% end %>
18
+ </div>
19
+ <% end %>
20
+ </div>
@@ -0,0 +1,12 @@
1
+ <h1 class="text-xl font-semibold">
2
+ <%= @preview_class.name %>: <%= @prompt.name %>
3
+ </h1>
4
+ <div class="pb-2">
5
+ <%= link_to "<< Back to prompts", "/action_prompt/previews", class: "cursor-pointer text-blue-500 underline hover:text-blue-600 text-sm" %>
6
+ </div>
7
+ <div class="py-2">
8
+ <div>
9
+ Output
10
+ </div>
11
+ <div class="bg-gray-100 p-2 rounded-md border shadow-sm" style="word-wrap: break-word; white-space: pre-wrap;"><%= @prompt_output %></div>
12
+ </div>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title><%= @page_title %></title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ </head>
9
+ <body class="min-h-screen bg-yellow-100 pt-2">
10
+
11
+ <div class="mx-auto w-2/3 bg-white rounded-lg p-8 shadow-lg">
12
+ <%= yield %>
13
+ </div>
14
+
15
+ </body>
16
+ </html>
@@ -1,4 +1,5 @@
1
1
  module ActionPrompt
2
2
  class Engine < ::Rails::Engine
3
+ isolate_namespace ActionPrompt
3
4
  end
4
5
  end
@@ -0,0 +1,62 @@
1
+ require "active_support/descendants_tracker"
2
+
3
+ module ActionPrompt
4
+ class Preview
5
+ # We need to be able to do the following:
6
+ # - find all the preview objects
7
+ # - find all the preview methods on those objects
8
+ # - render a preview
9
+ #
10
+ class << self
11
+ def all
12
+ load_previews if descendants.empty?
13
+ descendants.sort_by(&:name)
14
+ end
15
+
16
+ def load_previews
17
+ # TODO: this preview path could be made configurable in order to have equivalent
18
+ # functionality to Rails mailer previews
19
+ Dir[Rails.root.join("test", "prompts", "**", "*_preview.rb")].each do |path|
20
+ require path
21
+ end
22
+ end
23
+
24
+ def preview_name
25
+ name.delete_suffix("Preview").underscore
26
+ end
27
+
28
+ def prompts
29
+ # TODO: this might benefit from some memoization
30
+ prompt_methods = public_instance_methods(false).map(&:to_s).sort
31
+
32
+ prompt_methods.map do |method_name|
33
+ Prompt.new(name: method_name.humanize,
34
+ slug: "#{preview_name}/#{method_name}")
35
+ end
36
+ end
37
+
38
+ def find(name)
39
+ all.find { |preview| preview.name == name }
40
+ end
41
+
42
+ def find_prompt(slug)
43
+ prompts.find { |p| p.slug == slug }
44
+ end
45
+ end
46
+
47
+
48
+ class Prompt
49
+ attr_reader :name, :slug
50
+
51
+ # NOTE: this could probably be a Struct
52
+ def initialize(name:, slug:)
53
+ @name = name
54
+ @slug = slug
55
+ end
56
+ end
57
+
58
+ def render(template_name, locals: {})
59
+ ActionPrompt::Renderer.new.render(template_name, locals: locals)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,16 @@
1
+ require "rails/application_controller"
2
+
3
+ module ActionPrompt
4
+ class Railtie < Rails::Railtie
5
+ config.autoload_paths << Rails.root.join("tests/prompts")
6
+
7
+ config.after_initialize do |app|
8
+ if Rails.env.development? || Rails.env.test?
9
+ app.routes.prepend do
10
+ get "/action_prompt/previews", to: "action_prompt/previews#index" # , internal: true
11
+ get "/action_prompt/previews/:preview_name/:prompt_name", to: "action_prompt/previews#show" # , internal: true
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module ActionPrompt
2
+ class Renderer
3
+ def initialize
4
+ # NOOP
5
+ end
6
+
7
+ def render(template_name, locals: {})
8
+ controller = ApplicationController.new
9
+ controller.prepend_view_path(Rails.root.join("app", "prompts"))
10
+ controller.render_to_string(template: template_name, locals: locals, layout: false)
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module ActionPrompt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/action_prompt.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "action_prompt/version"
2
2
  require "action_prompt/engine"
3
-
3
+ require "action_prompt/railtie"
4
+ require "action_prompt/preview"
5
+ require "action_prompt/renderer"
4
6
  module ActionPrompt
5
- # Your code goes here...
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_prompt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Arnold
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-28 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -35,12 +35,17 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - app/assets/config/action_prompt_manifest.js
38
+ - app/controllers/action_prompt/previews_controller.rb
39
+ - app/views/action_prompt/previews/index.html.erb
40
+ - app/views/action_prompt/previews/show.html.erb
41
+ - app/views/layouts/application.html.erb
38
42
  - config/routes.rb
39
43
  - lib/action_prompt.rb
40
- - lib/action_prompt/base.rb
41
44
  - lib/action_prompt/engine.rb
45
+ - lib/action_prompt/preview.rb
46
+ - lib/action_prompt/railtie.rb
47
+ - lib/action_prompt/renderer.rb
42
48
  - lib/action_prompt/version.rb
43
- - lib/tasks/action_prompt_tasks.rake
44
49
  homepage: https://github.com/evdevdev/action_prompt
45
50
  licenses:
46
51
  - MIT
@@ -61,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
66
  - !ruby/object:Gem::Version
62
67
  version: '0'
63
68
  requirements: []
64
- rubygems_version: 3.5.7
69
+ rubygems_version: 3.5.16
65
70
  signing_key:
66
71
  specification_version: 4
67
72
  summary: ActionPrompt is a Rails plugin for managing templated LLM prompts
@@ -1,6 +0,0 @@
1
- module ActionPrompt
2
- class Base
3
- def self.render_prompt(template_name, locals: {})
4
- end
5
- end
6
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :action_prompt do
3
- # # Task goes here
4
- # end