Skip to content

Commit

Permalink
Shows image in conversation turn
Browse files Browse the repository at this point in the history
  • Loading branch information
apmiller108 committed Jan 26, 2025
1 parent e6d1806 commit 9730593
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
13 changes: 12 additions & 1 deletion app/views/components/conversation_turn_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ConversationTurnComponent < ApplicationViewComponent

delegate :prompt, :response, :created?, :in_progress?, :failed?, :completed?,
:model, :temperature, :generate_text_preset, :response_token_count,
to: :generate_text_request
:file, to: :generate_text_request

# @param [GenerateTextRequest] generate_text_request
def initialize(generate_text_request:)
Expand All @@ -32,4 +32,15 @@ def dataset
token_count: response_token_count
}.compact
end

def image?
file.attached? && file.image?
end

def image_variant_options
{
resize_to_limit: [100, 100],
**ActiveStorage::Blob::WEBP_VARIANT_OPTS
}
end
end
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
.c-conversation-turn{ id:, data: { controller: 'conversation-turn', **dataset } }
= render ModalComponent.new(size: :lg)
.turn.grid.mb-3
.g-col-1.d-flex.justify-content-center
%i.bi-person-fill.fs-1
.g-col-11
.card.segment{ class: "segment-user" }
.card-body
= prompt
.card-body.d-flex
- if image?
.image
= image_tag file.variant(**image_variant_options), data: { 'conversation-turn-target' => 'image' },
title: file.filename
.prompt
= prompt
.turn.grid.mb-3
.g-col-1.d-flex.justify-content-center
%i.bi-robot.fs-1
Expand Down
3 changes: 2 additions & 1 deletion app/views/components/modal_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ def self.turbo_frame_body_id

attr_reader :size

def initialize(size: nil, centered: true)
def initialize(size: nil, centered: true, id: nil)
@size = size
@centered = centered
@id = nil
end

def bs_size_class
Expand Down
6 changes: 3 additions & 3 deletions app/views/components/modal_component/modal_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import autoAnimate from '@formkit/auto-animate'
export default class extends Controller {
static targets = ['bodyTurboFrame', 'closeButton', 'header']

onOpenLister = 'modal-opener:openModal@window->modal#onOpenModal'
onCloseListner = 'modal-closer:closeModal@window->modal#onCloseModal'
onOpenListener = 'modal-opener:openModal@window->modal#onOpenModal'
onCloseListener = 'modal-closer:closeModal@window->modal#onCloseModal'
observer = new MutationObserver((mutations) => {
mutations.forEach(() => {
if (this.bodyTurboFrameTarget.innerHTML.trim() === '') {
Expand All @@ -15,7 +15,7 @@ export default class extends Controller {
})

connect() {
this.element.dataset.action = [this.onOpenLister, this.onCloseListner].join(' ')
this.element.dataset.action = [this.onOpenListener, this.onCloseListener].join(' ')
this.element.addEventListener('hide.bs.modal', e => this.reset(e));
// Automatically close the modal if the body is blank (eg, on turbo deletion of a resource)
this.observer.observe(this.bodyTurboFrameTarget, { childList: true, subtree: true })
Expand Down

0 comments on commit 9730593

Please sign in to comment.