Class: Legion::Extensions::Agentic::Social::Attachment::Helpers::AttachmentStore

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb

Instance Method Summary collapse

Constructor Details

#initializeAttachmentStore

Returns a new instance of AttachmentStore.



10
11
12
13
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 10

def initialize
  @models = {}
  @dirty  = false
end

Instance Method Details

#all_modelsObject



27
28
29
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 27

def all_models
  @models.values
end

#dirty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 31

def dirty?
  @dirty
end

#from_apollo(store:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 49

def from_apollo(store:)
  result = store.query(text: 'bond attachment', tags: %w[bond attachment])
  return false unless result[:success] && result[:results]&.any?

  result[:results].each do |entry|
    parsed = deserialize(entry[:content])
    next unless parsed && parsed[:agent_id]

    @models[parsed[:agent_id].to_s] = AttachmentModel.from_h(parsed)
  end
  true
rescue StandardError => e
  warn "[attachment_store] from_apollo error: #{e.message}"
  false
end

#get(agent_id) ⇒ Object



15
16
17
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 15

def get(agent_id)
  @models[agent_id.to_s]
end

#get_or_create(agent_id) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 19

def get_or_create(agent_id)
  key = agent_id.to_s
  @models[key] ||= begin
    @dirty = true
    AttachmentModel.new(agent_id: key)
  end
end

#mark_clean!Object



35
36
37
38
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 35

def mark_clean!
  @dirty = false
  self
end

#to_apollo_entriesObject



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/social/attachment/helpers/attachment_store.rb', line 40

def to_apollo_entries
  @models.map do |agent_id, model|
    tags = Constants::TAG_PREFIX.dup + [agent_id]
    tags << 'partner' if partner?(agent_id)
    content = serialize(model.to_h)
    { content: content, tags: tags }
  end
end