geminize 0.1.0 → 1.0.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.
@@ -1,125 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module GeminizeHelper
4
- # Render the Gemini conversation as HTML
5
- # @param conversation [Geminize::Models::Conversation, nil] Conversation to render (defaults to current_gemini_conversation)
6
- # @param options [Hash] Optional rendering options
7
- # @option options [String] :user_class CSS class for user messages
8
- # @option options [String] :ai_class CSS class for AI messages
9
- # @option options [Boolean] :include_timestamps Include message timestamps
10
- # @return [String] HTML representation of the conversation
11
- def render_gemini_conversation(conversation = nil, options = {})
12
- conversation ||= current_gemini_conversation if respond_to?(:current_gemini_conversation)
13
- return content_tag(:div, "No conversation available", class: "gemini-empty-conversation") unless conversation
14
-
15
- content_tag(:div, class: "gemini-conversation") do
16
- conversation.messages.map do |message|
17
- render_gemini_message(message, options)
18
- end.join.html_safe
19
- end
20
- end
21
-
22
- # Render a single message from the conversation
23
- # @param message [Geminize::Models::Message] The message to render
24
- # @param options [Hash] Optional rendering options
25
- # @return [String] HTML representation of the message
26
- def render_gemini_message(message, options = {})
27
- is_user = message.role == "user"
28
-
29
- message_class = if is_user
30
- options[:user_class] || "gemini-user-message"
31
- else
32
- options[:ai_class] || "gemini-ai-message"
33
- end
34
-
35
- content_tag(:div, class: "gemini-message #{message_class}") do
36
- output = []
37
-
38
- # Add the role label
39
- output << content_tag(:div, is_user ? "You" : "AI", class: "gemini-message-role")
40
-
41
- # Add the message content (convert newlines to <br> tags)
42
- output << content_tag(:div, simple_format(message.parts.first["text"]), class: "gemini-message-content")
43
-
44
- # Add timestamp if requested
45
- if options[:include_timestamps] && message.respond_to?(:timestamp) && message.timestamp
46
- timestamp = message.timestamp.is_a?(Time) ? message.timestamp : Time.parse(message.timestamp.to_s)
47
- output << content_tag(:div, timestamp.strftime("%Y-%m-%d %H:%M:%S"), class: "gemini-message-timestamp")
48
- end
49
-
50
- output.join.html_safe
51
- end
52
- end
53
-
54
- # Create a chat form that handles submitting messages to Gemini
55
- # @param options [Hash] Form options
56
- # @option options [String] :submit_text Text for the submit button (default: "Send")
57
- # @option options [String] :placeholder Placeholder text (default: "Type your message...")
58
- # @option options [String] :form_class CSS class for the form
59
- # @option options [String] :input_class CSS class for the input field
60
- # @option options [String] :submit_class CSS class for the submit button
61
- # @option options [String] :url URL to submit the form to (default: current URL)
62
- # @return [String] HTML form
63
- def gemini_chat_form(options = {})
64
- default_options = {
65
- submit_text: "Send",
66
- placeholder: "Type your message...",
67
- form_class: "gemini-chat-form",
68
- input_class: "gemini-chat-input",
69
- submit_class: "gemini-chat-submit",
70
- url: request.path
71
- }
72
-
73
- opts = default_options.merge(options)
74
-
75
- form_tag(opts[:url], method: :post, class: opts[:form_class]) do
76
- output = []
77
- output << text_area_tag(:message, nil, placeholder: opts[:placeholder], class: opts[:input_class])
78
- output << submit_tag(opts[:submit_text], class: opts[:submit_class])
79
- output.join.html_safe
80
- end
81
- end
82
-
83
- # Render Markdown text as HTML
84
- # @param text [String] Markdown text to render
85
- # @param options [Hash] Options for rendering
86
- # @return [String] HTML content
87
- def markdown_to_html(text, options = {})
88
- return "" if text.blank?
89
-
90
- # Check if the markdown gem is available
91
- if defined?(Redcarpet)
92
- renderer = Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: false)
93
- markdown = Redcarpet::Markdown.new(renderer, options)
94
- markdown.render(text).html_safe
95
- else
96
- # Fall back to simple formatting if Redcarpet is not available
97
- simple_format(text)
98
- end
99
- end
100
-
101
- # Add syntax highlighting to code blocks
102
- # @param html [String] HTML content that may contain code blocks
103
- # @return [String] HTML with syntax highlighting applied
104
- def highlight_code(html)
105
- return html unless defined?(Rouge)
106
-
107
- formatter = Rouge::Formatters::HTML.new
108
-
109
- # Find all code blocks and apply syntax highlighting
110
- doc = Nokogiri::HTML::DocumentFragment.parse(html)
111
- doc.css("pre code").each do |code|
112
- lang = code["class"]&.sub("language-", "") || "text"
113
- lexer = Rouge::Lexer.find(lang) || Rouge::Lexers::PlainText.new
114
-
115
- # Get the code content and format it
116
- code_text = code.text
117
- highlighted = formatter.format(lexer.lex(code_text))
118
-
119
- # Replace the original code block with the highlighted version
120
- code.parent.replace("<pre class=\"highlight #{lang}\">#{highlighted}</pre>")
121
- end
122
-
123
- doc.to_html.html_safe
124
- end
125
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Geminize
4
- module Rails
5
- # Module for adding Geminize functionality to controllers
6
- # Provides methods that make it easy to include Geminize controller concerns.
7
- #
8
- # This module is automatically included in ActionController::Base
9
- # when the gem is used in a Rails application.
10
- #
11
- # @example Including in a specific controller
12
- # class ChatController < ApplicationController
13
- # geminize_controller
14
- #
15
- # def create
16
- # @response = send_gemini_message(params[:message])
17
- # render :show
18
- # end
19
- # end
20
- module ControllerAdditions
21
- # Add Geminize functionality to a controller
22
- # This method includes the Geminize::Controller concern in your controller,
23
- # which provides methods for working with the Gemini API.
24
- #
25
- # @return [void]
26
- # @example
27
- # class ApplicationController < ActionController::Base
28
- # include Geminize::Rails::ControllerAdditions
29
- # geminize_controller
30
- # end
31
- def geminize_controller
32
- include Geminize::Controller
33
- end
34
- end
35
- end
36
- end
37
-
38
- # Add the module to ActionController::Base if it exists
39
- ActiveSupport.on_load(:action_controller) do
40
- include Geminize::Rails::ControllerAdditions
41
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Geminize
4
- module Rails
5
- # Rails engine for Geminize
6
- # Provides Rails integration for the Gemini API.
7
- class Engine < ::Rails::Engine
8
- isolate_namespace Geminize
9
-
10
- initializer "geminize.configure" do |app|
11
- # Set up configuration if needed
12
- end
13
-
14
- initializer "geminize.load_concerns" do
15
- ActiveSupport.on_load(:action_controller) do
16
- require "geminize/rails/app/controllers/concerns/geminize/controller"
17
- end
18
-
19
- ActiveSupport.on_load(:action_view) do
20
- require "geminize/rails/app/helpers/geminize_helper"
21
- end
22
- end
23
-
24
- config.to_prepare do
25
- # Load any dependencies that need to be available
26
- end
27
- end
28
- end
29
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Geminize
4
- module Rails
5
- # Module for adding Geminize view helpers to Rails applications
6
- # Provides methods to simplify the inclusion of Geminize helpers in views.
7
- #
8
- # This module is automatically included in ActionView::Base
9
- # when the gem is used in a Rails application.
10
- #
11
- # @example Using Geminize helpers in a view
12
- # <%# After including the helpers in ApplicationHelper %>
13
- # <%= render_gemini_conversation %>
14
- # <%= gemini_chat_form %>
15
- module HelperAdditions
16
- # Add Geminize helpers to views
17
- # This method includes the GeminizeHelper module in your view context,
18
- # providing helper methods for rendering Gemini conversations, chat forms,
19
- # and formatting responses.
20
- #
21
- # @return [void]
22
- # @example
23
- # module ApplicationHelper
24
- # include Geminize::Rails::HelperAdditions
25
- # geminize_helper
26
- # end
27
- def geminize_helper
28
- include GeminizeHelper
29
- end
30
- end
31
- end
32
- end
33
-
34
- # Add our helpers module to ActionView::Base if it exists
35
- ActiveSupport.on_load(:action_view) do
36
- include Geminize::Rails::HelperAdditions
37
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "geminize/rails/engine" if defined?(::Rails::Engine)
4
- require "geminize/railtie" if defined?(::Rails::Railtie)
5
- require "geminize/rails/controller_additions" if defined?(::ActionController::Base)
6
- require "geminize/rails/helper_additions" if defined?(::ActionView::Base)
7
-
8
- module Geminize
9
- # Rails integration module for Geminize
10
- # Provides Rails integration for the Google Gemini API.
11
- #
12
- # The integration includes:
13
- # - A Rails engine to load all required components
14
- # - Controller concerns with helper methods for Gemini operations
15
- # - View helpers for rendering conversations and responses
16
- # - Generators for creating configuration files
17
- #
18
- # @example Setting up in a Rails application
19
- # # In config/initializers/geminize.rb (created by the generator)
20
- # Geminize.configure do |config|
21
- # config.api_key = ENV.fetch("GEMINI_API_KEY")
22
- # end
23
- #
24
- # # In app/controllers/chat_controller.rb
25
- # class ChatController < ApplicationController
26
- # geminize_controller
27
- #
28
- # def create
29
- # @response = send_gemini_message(params[:message])
30
- # redirect_to chat_path
31
- # end
32
- # end
33
- #
34
- # # In app/views/chat/show.html.erb
35
- # <%= render_gemini_conversation %>
36
- # <%= gemini_chat_form %>
37
- module Rails
38
- # Returns true if running in a Rails environment
39
- # Useful for conditionally executing code only in Rails apps.
40
- #
41
- # @return [Boolean] true if Rails is defined
42
- # @example
43
- # if Geminize::Rails.rails?
44
- # # Rails-specific code
45
- # end
46
- def self.rails?
47
- defined?(::Rails)
48
- end
49
- end
50
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Geminize
4
- # Railtie for lightweight Rails integration
5
- # Provides basic Rails integration when the full engine is not required.
6
- # This is automatically loaded when the gem is used in a Rails application.
7
- #
8
- # @example Basic usage in a Rails application
9
- # # No additional setup needed - Railtie is loaded automatically
10
- # # In your controller:
11
- # class ExamplesController < ApplicationController
12
- # def example
13
- # @response = Geminize.generate_text("What is Ruby on Rails?").text
14
- # end
15
- # end
16
- class Railtie < ::Rails::Railtie
17
- # Configure Geminize when used in a Rails application
18
- # @return [void]
19
- initializer "geminize.configure" do |app|
20
- # Set up configuration for Geminize when used in a Rails app
21
- Geminize.configure do |config|
22
- # Set conversations path to Rails tmp directory by default
23
- config.conversations_path = Rails.root.join("tmp", "conversations") if config.conversations_path.nil?
24
- end
25
- end
26
-
27
- # Set up any rake tasks if needed
28
- # @return [void]
29
- rake_tasks do
30
- # Define rake tasks here if needed
31
- end
32
- end
33
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators/base"
4
-
5
- module Geminize
6
- module Generators
7
- # Generator for installing Geminize Rails integration
8
- # This generator creates an initializer with default configuration
9
- class InstallGenerator < Rails::Generators::Base
10
- source_root File.expand_path("templates", __dir__)
11
- desc "Creates a Geminize initializer for Rails."
12
-
13
- def create_initializer_file
14
- template "initializer.rb", "config/initializers/geminize.rb"
15
- end
16
-
17
- def show_readme
18
- readme "README" if behavior == :invoke
19
- end
20
- end
21
- end
22
- end
@@ -1,31 +0,0 @@
1
- ===============================================================================
2
-
3
- Geminize has been successfully installed!
4
-
5
- A configuration initializer has been created at:
6
- config/initializers/geminize.rb
7
-
8
- You need to edit the initializer and add your Google Gemini API key.
9
- You can get an API key from https://ai.google.dev/
10
-
11
- ===============================================================================
12
-
13
- Usage Examples:
14
-
15
- # Basic text generation
16
- text = Geminize.generate_text("What is Ruby on Rails?").text
17
- puts text
18
-
19
- # Create a chat session
20
- chat = Geminize.create_chat("My Support Chat")
21
- response = Geminize.chat("How can I create a Ruby gem?", chat)
22
- puts response.text
23
-
24
- # Follow-up question (keeps conversation context)
25
- response = Geminize.chat("What about adding a Rails engine to my gem?", chat)
26
- puts response.text
27
-
28
- # For more examples, see the documentation at:
29
- # https://github.com/nhlongnguyen/geminize
30
-
31
- ===============================================================================
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Geminize Configuration
4
- #
5
- # This file contains the configuration for the Geminize gem.
6
- # It is used to set up the Google Gemini API integration.
7
-
8
- Geminize.configure do |config|
9
- # Your Google Gemini API key
10
- # You can get one from https://ai.google.dev/
11
- # Can also be set via GEMINI_API_KEY environment variable
12
- # config.api_key = ENV.fetch("GEMINI_API_KEY", nil)
13
-
14
- # The API version to use (default: v1beta)
15
- # config.api_version = "v1beta"
16
-
17
- # The default model to use (default: gemini-2.0-flash)
18
- # config.default_model = "gemini-2.0-flash"
19
-
20
- # The base URL for the Gemini API (default: https://generativelanguage.googleapis.com)
21
- # config.api_base_url = "https://generativelanguage.googleapis.com"
22
-
23
- # Logging level for Geminize (default: :info)
24
- # Valid values: :debug, :info, :warn, :error, :fatal
25
- # config.log_level = Rails.env.production? ? :info : :debug
26
-
27
- # Where to store conversation data (default: Rails.root.join("tmp", "conversations"))
28
- # Only applicable when using FileConversationRepository
29
- # config.conversations_path = Rails.root.join("tmp", "conversations")
30
-
31
- # Default parameters for text generation
32
- # config.generation_defaults = {
33
- # temperature: 0.7,
34
- # max_tokens: 500,
35
- # top_p: 0.95,
36
- # top_k: 40
37
- # }
38
- end