Class: SmartPrompt::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_prompt/message.rb

Overview

Message represents a single message in a conversation history It contains role, content, timestamp, and metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
# File 'lib/smart_prompt/message.rb', line 10

def initialize(data)
  @role = data[:role] || data["role"]
  @content = data[:content] || data["content"]
  @timestamp = parse_timestamp(data[:timestamp] || data["timestamp"])
  @metadata = data[:metadata] || data["metadata"] || {}
  @token_count = nil  # Lazy calculation
  @importance_score = data[:importance_score] || data["importance_score"]
  @is_summary = data[:is_summary] || data["is_summary"] || false
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/smart_prompt/message.rb', line 7

def content
  @content
end

#importance_scoreObject

Returns the value of attribute importance_score.



8
9
10
# File 'lib/smart_prompt/message.rb', line 8

def importance_score
  @importance_score
end

#is_summaryObject

Returns the value of attribute is_summary.



8
9
10
# File 'lib/smart_prompt/message.rb', line 8

def is_summary
  @is_summary
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/smart_prompt/message.rb', line 7

def 
  @metadata
end

#roleObject (readonly)

Returns the value of attribute role.



7
8
9
# File 'lib/smart_prompt/message.rb', line 7

def role
  @role
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/smart_prompt/message.rb', line 7

def timestamp
  @timestamp
end

#token_countObject (readonly)

Returns the value of attribute token_count.



7
8
9
# File 'lib/smart_prompt/message.rb', line 7

def token_count
  @token_count
end

Instance Method Details

#calculate_tokens(counter) ⇒ Object

Calculate token count using provided counter



21
22
23
# File 'lib/smart_prompt/message.rb', line 21

def calculate_tokens(counter)
  @token_count ||= counter.count(@content)
end

#system_message?Boolean

Check if this is a system message

Returns:

  • (Boolean)


26
27
28
# File 'lib/smart_prompt/message.rb', line 26

def system_message?
  @role == "system" || @role == :system
end

#to_hObject

Convert message to hash format



31
32
33
34
35
36
37
38
39
40
# File 'lib/smart_prompt/message.rb', line 31

def to_h
  {
    role: @role,
    content: @content,
    timestamp: @timestamp.iso8601,
    metadata: @metadata,
    importance_score: @importance_score,
    is_summary: @is_summary
  }
end