Class: PromptBuilder::Items::Compaction

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_builder/items/compaction.rb

Overview

Represents a compaction item that summarizes earlier conversation history.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, encrypted_content: nil, created_by: nil, **extra) ⇒ Compaction

Create a new Compaction item.

Parameters:

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

    the compaction item identifier

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

    encrypted compacted content

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

    who created the compaction

  • extra (Hash)

    provider-specific extra keyword arguments



25
26
27
28
29
30
# File 'lib/prompt_builder/items/compaction.rb', line 25

def initialize(id: nil, encrypted_content: nil, created_by: nil, **extra)
  @id = id&.to_s
  @encrypted_content = encrypted_content&.to_s
  @created_by = created_by&.to_s
  @extra = extra.transform_keys(&:to_s)
end

Instance Attribute Details

#created_byString? (readonly)

Returns who created the compaction (e.g. “user” or “assistant”).

Returns:

  • (String, nil)

    who created the compaction (e.g. “user” or “assistant”)



14
15
16
# File 'lib/prompt_builder/items/compaction.rb', line 14

def created_by
  @created_by
end

#encrypted_contentString? (readonly)

Returns encrypted compacted content.

Returns:

  • (String, nil)

    encrypted compacted content



11
12
13
# File 'lib/prompt_builder/items/compaction.rb', line 11

def encrypted_content
  @encrypted_content
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



17
18
19
# File 'lib/prompt_builder/items/compaction.rb', line 17

def extra
  @extra
end

#idString? (readonly)

Returns the compaction item identifier.

Returns:

  • (String, nil)

    the compaction item identifier



8
9
10
# File 'lib/prompt_builder/items/compaction.rb', line 8

def id
  @id
end

Class Method Details

.from_h(hash) ⇒ Compaction

Deserialize a Compaction item from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



37
38
39
40
41
42
43
44
# File 'lib/prompt_builder/items/compaction.rb', line 37

def from_h(hash)
  new(
    id: hash["id"],
    encrypted_content: hash["encrypted_content"],
    created_by: hash["created_by"],
    **hash.except("type", "id", "encrypted_content", "created_by").transform_keys(&:to_sym)
  )
end

Instance Method Details

#to_hHash

Serialize to a Hash with string keys. Nil values are omitted.

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
# File 'lib/prompt_builder/items/compaction.rb', line 50

def to_h
  h = {"type" => "compaction"}
  h["id"] = @id if @id
  h["encrypted_content"] = @encrypted_content if @encrypted_content
  h["created_by"] = @created_by if @created_by
  h = PromptBuilder.jsonify(@extra).merge(h) unless @extra.empty?
  h
end