Class: Assistant::GammaToolBuilder::ReadTool

Inherits:
RubyLLM::Tool
  • Object
show all
Includes:
GammaIdExtractable
Defined in:
app/services/assistant/gamma_tool_builder.rb

Overview

Read and analyse the content of an existing Gamma presentation or document.

Instance Method Summary collapse

Instance Method Details

#execute(gamma_url:, **_) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/services/assistant/gamma_tool_builder.rb', line 329

def execute(gamma_url:, **_)
  require 'pdf/reader'

  temp_path = nil
  gamma_id  = extract_gamma_id(gamma_url)
  full_url  = canonical_gamma_url(gamma_url)

  if gamma_id.blank? || gamma_id !~ /\A[a-z0-9]+\z/
    return {
      success: false,
      error:   "Could not extract a valid gammaId from '#{gamma_url}'. " \
               'Please provide the full Gamma URL (e.g. https://gamma.app/docs/my-deck-abc123).'
    }.to_json
  end

  content = nil
  method  = nil

  # ── Level 1: Gamma API PDF export (works for Sunny-created or template gammas) ──
  Rails.logger.info("[GammaToolBuilder] gamma_read: trying Gamma API for #{gamma_id}")
  client     = GammaClient.new
  pdf_result = client.fetch_pdf_url(gamma_id: gamma_id)

  if pdf_result.success? && pdf_result.data[:pdf_url].present?
    pdf_url   = pdf_result.data[:pdf_url]
    temp_path = Rails.application.config.x.temp_storage_path
                     .join("gamma_read_#{gamma_id}_#{Time.current.to_i}.pdf")

    Down::Http.download(pdf_url, destination: temp_path) { |http_client| http_client.timeout(read: 60) }

    text_parts = []
    PDF::Reader.open(temp_path.to_s) do |reader|
      reader.pages.each_with_index do |page, idx|
        page_text = page.text.to_s.strip
        text_parts << "## Slide #{idx + 1}\n#{page_text}" if page_text.present?
      end
    end

    if text_parts.any?
      content = text_parts.join("\n\n")
      method  = 'gamma_api_pdf'
      Rails.logger.info("[GammaToolBuilder] gamma_read: Level 1 (Gamma API) succeeded")
    end
  end

  # ── Level 2: Oxylabs headless-browser scrape ──────────────────────────────────
  if content.nil?
    Rails.logger.info("[GammaToolBuilder] gamma_read: escalating to Oxylabs scrape for #{full_url}")
    scraped = scrape_gamma_page(full_url)

    if scraped.present?
      content = scraped
      method  = 'oxylabs_scrape'
      Rails.logger.info("[GammaToolBuilder] gamma_read: Level 2 (Oxylabs) succeeded, #{content.length} chars")
    end
  end

  if content.nil?
    return {
      success: false,
      error:   'Could not retrieve presentation content. Gamma.app is protected by ' \
               'Cloudflare, which blocks automated rendering for this presentation right now — ' \
               'this usually clears up within minutes. ' \
               'Alternatively, export the PDF from Gamma (Share → Export → PDF) and upload it here, ' \
               'or paste the slide text directly into the chat.'
    }.to_json
  end

  # Truncate to ~25 000 chars to stay within context limits
  truncated = content.length > 25_000
  content   = content[0, 25_000] if truncated

  result = {
    success:          true,
    gamma_id:         gamma_id,
    retrieval_method: method,
    content:          content
  }
  result[:truncated] = true if truncated

  result.to_json
rescue PDF::Reader::MalformedPDFError, PDF::Reader::UnsupportedFeatureError => e
  Rails.logger.error("[GammaToolBuilder] gamma_read PDF parse error: #{e.message}")
  { success: false, error: "PDF could not be parsed: #{e.message}" }.to_json
rescue StandardError => e
  Rails.logger.error("[GammaToolBuilder] gamma_read failed: #{e.class} #{e.message}")
  { success: false, error: "Failed to read Gamma presentation: #{e.message}" }.to_json
ensure
  File.delete(temp_path) if temp_path && File.exist?(temp_path.to_s)
end

#nameObject



327
# File 'app/services/assistant/gamma_tool_builder.rb', line 327

def name = 'gamma_read'