Class: Pdf::StudioSession

Inherits:
Object
  • Object
show all
Defined in:
app/services/pdf/studio_session.rb

Overview

Starts a scoped "PDF studio" Sunny session: an AssistantConversation locked
to the +pdf_tools+ service, optionally seeded with a publication's current PDF
as an attachment plus an opening instruction.

This is the conversational entry point the PDF studio and a publication's
"Create PDF variation" action hand off to — the user describes the change in
plain language and Sunny inspects / edits / generates, staging results back
into the studio (see Assistant::PdfToolBuilder#stage_for_review!, which reads
the conversation's +pdf_source_publication_id+ so a staged PDF can be imported
as a revision or replacement of the source publication).

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, source_publication: nil) ⇒ StudioSession

Returns a new instance of StudioSession.

Parameters:

  • user (Object)

    the chat user (a CRM +current_user+)

  • source_publication (Item, nil) (defaults to: nil)

    the publication being revised, if any



25
26
27
28
# File 'app/services/pdf/studio_session.rb', line 25

def initialize(user:, source_publication: nil)
  @user = user
  @source_publication = source_publication
end

Class Method Details

.startObject



21
# File 'app/services/pdf/studio_session.rb', line 21

def self.start(...) = new(...).start

Instance Method Details

#startResult

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/pdf/studio_session.rb', line 31

def start
  conversation  = nil
  conversation  = Assistant::SunnyAgent.create!(user: @user)
  seeded_upload = seed_publication_pdf!(conversation)
  conversation.update!(
    title:                     session_title,
    tool_services:             ['pdf_tools'],
    conversation_type:         'pdf_work',
    default_tool_handles:      ['pdf-tools'],
    draft_prefill:             opening_prompt(seeded_upload),
    pdf_source_publication_id: @source_publication&.id
  )
  Result.new(conversation: conversation)
rescue StandardError => e
  # Don't leave a half-configured conversation (and its seeded upload) behind.
  conversation&.destroy if conversation&.persisted?
  Rails.logger.error("[Pdf::StudioSession] #{e.class}: #{e.message}")
  Result.new(error: e.message)
end