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, opening: nil, title: 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

  • opening (String, nil) (defaults to: nil)

    overrides the default chat prefill (e.g. the
    translate action seeds a translation request instead of "make this change")

  • title (String, nil) (defaults to: nil)

    overrides the default conversation title



30
31
32
33
34
35
# File 'app/services/pdf/studio_session.rb', line 30

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

Class Method Details

.startObject



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

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

Instance Method Details

#startResult

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/pdf/studio_session.rb', line 38

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