Class: Ragdoll::DocumentManagement

Inherits:
Object
  • Object
show all
Defined in:
app/services/ragdoll/document_management.rb

Class Method Summary collapse

Class Method Details

.add_document(location, content, metadata = {}, force: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/ragdoll/document_management.rb', line 8

def add_document(location, content,  = {}, force: false)
  # Ensure location is an absolute path if it's a file path
  absolute_location = location.start_with?("http") || location.start_with?("ftp") ? location : File.expand_path(location)

  # Get file modification time if it's a file path
  file_modified_at = if File.exist?(absolute_location) && !absolute_location.start_with?("http")
                       File.mtime(absolute_location)
                     else
                       Time.current
                     end

  # Skip duplicate detection if force is true
  unless force
    existing_document = find_duplicate_document(absolute_location, content, , file_modified_at)
    return existing_document.id.to_s if existing_document
  end

  # Modify location if force is used to avoid unique constraint violation
  final_location = if force
                     "#{absolute_location}#forced_#{Time.current.to_i}_#{SecureRandom.hex(4)}"
                   else
                     absolute_location
                   end

  document = Ragdoll::Document.create!(
    location: final_location,
    title: [:title] || ["title"] || extract_title_from_location(location),
    document_type: [:document_type] || ["document_type"] || "text",
    metadata: .is_a?(Hash) ?  : {},
    status: "pending",
    file_modified_at: file_modified_at
  )

  # Set content using the model's setter to trigger TextContent creation
  document.content = content if content.present?

  document.id.to_s
end

.add_embedding(embeddable_id, chunk_index, embedding_vector, metadata = {}) ⇒ Object

FIXME: should this be here?



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/services/ragdoll/document_management.rb', line 88

def add_embedding(embeddable_id, chunk_index, embedding_vector,  = {})
  # The embeddable_type should be the actual STI subclass, not the base class
  embeddable_type = if [:embeddable_type]
                     [:embeddable_type]
                   else
                     # Look up the actual STI type from the content record
                     content = Ragdoll::Content.find(embeddable_id)
                     content.class.name
                   end
  
  Ragdoll::Embedding.create!(
    embeddable_id: embeddable_id,
    embeddable_type: embeddable_type,
    chunk_index: chunk_index,
    embedding_vector: embedding_vector,
    content: [:content] || ""
  ).id.to_s
end

.delete_document(id) ⇒ Object



67
68
69
70
71
72
73
# File 'app/services/ragdoll/document_management.rb', line 67

def delete_document(id)
  document = Ragdoll::Document.find_by(id: id)
  return nil unless document

  document.destroy!
  true
end

.get_document(id) ⇒ Object



47
48
49
50
51
52
53
54
# File 'app/services/ragdoll/document_management.rb', line 47

def get_document(id)
  document = Ragdoll::Document.find_by(id: id)
  return nil unless document

  hash = document.to_hash
  hash[:content] = document.content
  hash
end

.get_document_statsObject



82
83
84
# File 'app/services/ragdoll/document_management.rb', line 82

def get_document_stats
  Ragdoll::Document.stats
end

.list_documents(options = {}) ⇒ Object



75
76
77
78
79
80
# File 'app/services/ragdoll/document_management.rb', line 75

def list_documents(options = {})
  limit = options[:limit] || 100
  offset = options[:offset] || 0

  Ragdoll::Document.offset(offset).limit(limit).recent.map(&:to_hash)
end

.update_document(id, **updates) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'app/services/ragdoll/document_management.rb', line 56

def update_document(id, **updates)
  document = Ragdoll::Document.find_by(id: id)
  return nil unless document

  # Only update allowed fields
  allowed_updates = updates.slice(:title, :metadata, :status, :document_type)
  document.update!(allowed_updates) if allowed_updates.any?

  document.to_hash
end