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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/smart_prompt/message.rb', line 11

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
  # Tool-call pairing and reasoning fields must survive serialization so
  # HistoryManager round-trips don't drop tool_calls / tool_call_id /
  # reasoning_content and produce malformed tool requests.
  @tool_calls = data[:tool_calls] || data["tool_calls"]
  @tool_call_id = data[:tool_call_id] || data["tool_call_id"]
  @reasoning_content = data[:reasoning_content] || data["reasoning_content"]
end

Instance Attribute Details

#content ⇒ Object (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#importance_score ⇒ Object

Returns the value of attribute importance_score.



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

def importance_score
  @importance_score
end

#is_summary ⇒ Object

Returns the value of attribute is_summary.



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

def is_summary
  @is_summary
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#reasoning_content ⇒ Object (readonly)

Returns the value of attribute reasoning_content.



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

def reasoning_content
  @reasoning_content
end

#role ⇒ Object (readonly)

Returns the value of attribute role.



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

def role
  @role
end

#timestamp ⇒ Object (readonly)

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

#token_count ⇒ Object (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

#tool_call_id ⇒ Object (readonly)

Returns the value of attribute tool_call_id.



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

def tool_call_id
  @tool_call_id
end

#tool_calls ⇒ Object (readonly)

Returns the value of attribute tool_calls.



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

def tool_calls
  @tool_calls
end

Instance Method Details

#calculate_tokens(counter) ⇒ Object

Calculate token count using provided counter



28
29
30
# File 'lib/smart_prompt/message.rb', line 28

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

#system_message? ⇒ Boolean

Check if this is a system message

Returns:

  • (Boolean)


33
34
35
# File 'lib/smart_prompt/message.rb', line 33

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

#to_h ⇒ Object

Convert message to hash format



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smart_prompt/message.rb', line 38

def to_h
  h = {
    role: @role,
    content: @content,
    timestamp: @timestamp.iso8601,
    metadata: @metadata,
    importance_score: @importance_score,
    is_summary: @is_summary
  }
  h[:tool_calls] = @tool_calls if @tool_calls
  h[:tool_call_id] = @tool_call_id if @tool_call_id
  h[:reasoning_content] = @reasoning_content if @reasoning_content
  h
end