-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for ActionText rich content #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raghubetina
wants to merge
3
commits into
add-activerecord-support
Choose a base branch
from
add-actiontext-support
base: add-activerecord-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AI | ||
| class Chat | ||
| # ActionText support for AI::Chat | ||
| module ActionTextSupport | ||
| # Extracts content from an ActionText object, preserving text and images | ||
| # @param rich_text [ActionText::RichText] the rich text object to process | ||
| # @return [Array<Hash>] array of content parts in AI::Chat compatible format | ||
| def extract_actiontext_content(rich_text) | ||
| # Skip if ActionText is not defined in the current environment | ||
| return [{text: rich_text.to_s}] unless defined?(ActionText::RichText) | ||
|
|
||
| # Get the HTML content | ||
| html_content = rich_text.to_s | ||
|
|
||
| # Parse the HTML to extract text and image references | ||
| content_parts = [] | ||
|
|
||
| # Try to use Nokogiri if available for robust HTML parsing | ||
| if defined?(Nokogiri) | ||
| doc = Nokogiri::HTML::DocumentFragment.parse(html_content) | ||
| process_with_nokogiri(doc, content_parts) | ||
| else | ||
| # Fallback to simpler regexp-based parsing | ||
| process_with_regexp(html_content, content_parts) | ||
| end | ||
|
|
||
| # Return original text if no parts were extracted | ||
| content_parts.empty? ? [{text: html_content}] : content_parts | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Process HTML content using Nokogiri | ||
| # @param doc [Nokogiri::HTML::DocumentFragment] the parsed HTML document | ||
| # @param content_parts [Array<Hash>] the array to add content parts to | ||
| def process_with_nokogiri(doc, content_parts) | ||
| # Current text buffer | ||
| text_buffer = "" | ||
|
|
||
| doc.children.each do |node| | ||
| if node.name == "action-text-attachment" | ||
| # Flush text buffer before processing attachment | ||
| unless text_buffer.empty? | ||
| content_parts << {text: text_buffer.strip} | ||
| text_buffer = "" | ||
| end | ||
|
|
||
| # Extract image from Rails attachment | ||
| sgid = node["sgid"] | ||
| if sgid && defined?(GlobalID::Locator) | ||
| begin | ||
| attachment = GlobalID::Locator.locate_signed(sgid) | ||
| if attachment && attachment.respond_to?(:blob) | ||
| if defined?(Rails) && Rails.application.respond_to?(:routes) | ||
| image_url = Rails.application.routes.url_helpers.rails_blob_path(attachment.blob, only_path: true) | ||
| content_parts << {image: image_url} | ||
| else | ||
| # Fallback to direct URL if available | ||
| content_parts << {image: attachment.blob.url} if attachment.blob.respond_to?(:url) | ||
| end | ||
| end | ||
| rescue => e | ||
| # Silently continue if attachment can't be loaded | ||
| end | ||
| end | ||
| elsif node.name == "figure" && node.at_css("img") | ||
| # Flush text buffer before processing figure | ||
| unless text_buffer.empty? | ||
| content_parts << {text: text_buffer.strip} | ||
| text_buffer = "" | ||
| end | ||
|
|
||
| # Extract image from figure tag | ||
| img = node.at_css("img") | ||
| src = img["src"] | ||
| content_parts << {image: src} if src | ||
| else | ||
| # Extract text, preserving basic formatting | ||
| text_buffer += node.text | ||
| end | ||
| end | ||
|
|
||
| # Add any remaining text | ||
| content_parts << {text: text_buffer.strip} unless text_buffer.empty? | ||
| end | ||
|
|
||
| # Process HTML content using regular expressions | ||
| # @param html_content [String] the HTML content to process | ||
| # @param content_parts [Array<Hash>] the array to add content parts to | ||
| def process_with_regexp(html_content, content_parts) | ||
| # Split by attachment tags or figure tags | ||
| parts = html_content.split(/(<action-text-attachment[^>]+>|<figure>.*?<\/figure>)/) | ||
|
|
||
| parts.each do |part| | ||
| if part.start_with?("<action-text-attachment") | ||
| # Extract image SGID from attachment | ||
| sgid_match = part.match(/sgid="([^"]+)"/) | ||
|
|
||
| if sgid_match && defined?(GlobalID::Locator) | ||
| begin | ||
| sgid = sgid_match[1] | ||
| attachment = GlobalID::Locator.locate_signed(sgid) | ||
| if attachment && attachment.respond_to?(:blob) | ||
| if defined?(Rails) && Rails.application.respond_to?(:routes) | ||
| image_url = Rails.application.routes.url_helpers.rails_blob_path(attachment.blob, only_path: true) | ||
| content_parts << {image: image_url} | ||
| else | ||
| # Fallback to direct URL if available | ||
| content_parts << {image: attachment.blob.url} if attachment.blob.respond_to?(:url) | ||
| end | ||
| end | ||
| rescue => e | ||
| # Silently continue if attachment can't be loaded | ||
| end | ||
| end | ||
| elsif part.start_with?("<figure") | ||
| # Extract image from figure tag | ||
| img_match = part.match(/src="([^"]+)"/) | ||
| content_parts << {image: img_match[1]} if img_match | ||
| elsif !part.strip.empty? | ||
| # Clean up text by removing HTML tags | ||
| if defined?(ActionController::Base) | ||
| clean_text = ActionController::Base.helpers.strip_tags(part).strip | ||
| else | ||
| # Simple HTML tag removal | ||
| clean_text = part.gsub(/<[^>]+>/, "").strip | ||
| end | ||
|
|
||
| content_parts << {text: clean_text} unless clean_text.empty? | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe AI::Chat, "actiontext support" do | ||
| let(:chat) { build(:chat) } | ||
| let(:test_image_path) { File.join(File.dirname(__FILE__), "../../fixtures/test1.jpg") } | ||
| let(:test_image_url) { "https://example.com/image.jpg" } | ||
|
|
||
| # Skip these tests if ActionText is not available | ||
| before(:each) do | ||
| skip "ActionText is not available" unless defined?(ActionText::RichText) | ||
| end | ||
|
|
||
| describe "#extract_actiontext_content" do | ||
| # Mock ActionText::RichText class if not available | ||
| let(:mock_rich_text) do | ||
| if defined?(ActionText::RichText) | ||
| # Use actual ActionText if available | ||
| ActionText::RichText.new(body: html_content) | ||
| else | ||
| # Mock it otherwise | ||
| Class.new do | ||
| attr_reader :body | ||
|
|
||
| def initialize(body) | ||
| @body = body | ||
| end | ||
|
|
||
| def to_s | ||
| @body | ||
| end | ||
|
|
||
| def to_plain_text | ||
| @body.gsub(/<[^>]+>/, "") | ||
| end | ||
| end.new(html_content) | ||
| end | ||
| end | ||
|
|
||
| context "with text only" do | ||
| let(:html_content) { "<div>This is a test message</div>" } | ||
|
|
||
| it "extracts plain text correctly" do | ||
| # Skip if module not loaded | ||
| skip "ActionTextSupport module not loaded" unless chat.respond_to?(:extract_actiontext_content) | ||
|
|
||
| result = chat.extract_actiontext_content(mock_rich_text) | ||
| expect(result).to be_an(Array) | ||
| expect(result.length).to eq(1) | ||
| expect(result[0][:text]).to include("This is a test message") | ||
| end | ||
| end | ||
|
|
||
| context "with text and image" do | ||
| let(:html_content) do | ||
| <<-HTML | ||
| <div>Text before image</div> | ||
| <figure> | ||
| <img src="#{test_image_url}"> | ||
| <figcaption>Image caption</figcaption> | ||
| </figure> | ||
| <div>Text after image</div> | ||
| HTML | ||
| end | ||
|
|
||
| it "extracts text and image correctly" do | ||
| # Skip if module not loaded | ||
| skip "ActionTextSupport module not loaded" unless chat.respond_to?(:extract_actiontext_content) | ||
|
|
||
| result = chat.extract_actiontext_content(mock_rich_text) | ||
| expect(result).to be_an(Array) | ||
| expect(result.length).to eq(3) | ||
| expect(result[0][:text]).to include("Text before image") | ||
| expect(result[1][:image]).to eq(test_image_url) | ||
| expect(result[2][:text]).to include("Text after image") | ||
| end | ||
| end | ||
|
|
||
| context "with ActionText attachment" do | ||
| let(:html_content) do | ||
| <<-HTML | ||
| <div>Text before attachment</div> | ||
| <action-text-attachment sgid="test-sgid"></action-text-attachment> | ||
| <div>Text after attachment</div> | ||
| HTML | ||
| end | ||
|
|
||
| it "attempts to process action-text-attachment tags" do | ||
| # Skip if module not loaded | ||
| skip "ActionTextSupport module not loaded" unless chat.respond_to?(:extract_actiontext_content) | ||
|
|
||
| # This test is mainly for code coverage, as we can't fully mock | ||
| # the GlobalID::Locator behavior in a simple spec | ||
| result = chat.extract_actiontext_content(mock_rich_text) | ||
| expect(result).to be_an(Array) | ||
| expect(result.length).to be >= 2 | ||
| expect(result[0][:text]).to include("Text before attachment") | ||
| expect(result[-1][:text]).to include("Text after attachment") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#messages=" do | ||
| # This test depends on the ActionText module being loaded | ||
| it "handles ActionText content" do | ||
| skip "ActionTextSupport module not loaded" unless chat.respond_to?(:extract_actiontext_content) | ||
|
|
||
| # Mock an ActionText-like object | ||
| mock_actiontext = double("ActionText::RichText") | ||
| allow(mock_actiontext).to receive(:is_a?).with(ActionText::RichText).and_return(true) | ||
| allow(mock_actiontext).to receive(:to_plain_text).and_return("Plain text content") | ||
| allow(mock_actiontext).to receive(:to_s).and_return("<div>Rich text content</div>") | ||
|
|
||
| # Mock a message with ActionText content | ||
| mock_message = double("Message") | ||
| allow(mock_message).to receive(:role).and_return("user") | ||
| allow(mock_message).to receive(:content).and_return(mock_actiontext) | ||
|
|
||
| # Simulate the behavior when extract_actiontext_content exists | ||
| allow(chat).to receive(:extract_actiontext_content).and_return([{text: "Parsed content"}]) | ||
|
|
||
| # Set messages | ||
| chat.messages = [mock_message] | ||
|
|
||
| # Verify that extract_actiontext_content was called | ||
| expect(chat).to have_received(:extract_actiontext_content).with(mock_actiontext) | ||
|
|
||
| # Check the message content | ||
| expect(chat.messages.length).to eq(1) | ||
| expect(chat.messages[0][:role]).to eq("user") | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
process_with_nokogiri, errors in processing attachments are silently ignored. Logging these errors might help with troubleshooting in production.