Class: Rubino::Session::Message

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

Overview

Handles message persistence within a session. Messages include user input, assistant responses, tool calls and results.

Constant Summary collapse

VALID_ROLES =
%w[system user assistant tool].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubino/session/message.rb', line 16

def initialize(attrs = {})
  @id = attrs[:id] || SecureRandom.uuid
  @session_id = attrs[:session_id]
  @role = attrs[:role]
  @content = attrs[:content]
  @tool_name = attrs[:tool_name]
  @tool_call_id = attrs[:tool_call_id]
  @token_count = attrs[:token_count] || 0
  @metadata = attrs[:metadata] || {}
  @created_at = attrs[:created_at] || Time.now.utc.iso8601
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def 
  @metadata
end

#roleObject (readonly)

Returns the value of attribute role.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def role
  @role
end

#session_idObject (readonly)

Returns the value of attribute session_id.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def session_id
  @session_id
end

#token_countObject (readonly)

Returns the value of attribute token_count.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def token_count
  @token_count
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def tool_call_id
  @tool_call_id
end

#tool_nameObject (readonly)

Returns the value of attribute tool_name.



13
14
15
# File 'lib/rubino/session/message.rb', line 13

def tool_name
  @tool_name
end

Instance Method Details

#to_contextObject

Returns a hash for LLM context building. A user message that collapsed a large paste keeps the compact “[Pasted text #N …]” placeholder in its stored/displayed content (#213); here we expand each placeholder back to its full body for the model, so the provider sees everything while the transcript echo (live AND on resume) stays clean.



53
54
55
56
57
58
59
60
61
# File 'lib/rubino/session/message.rb', line 53

def to_context
  msg = { role: @role, content: expand_pastes(@content) }
  msg[:tool_call_id] = @tool_call_id if @tool_call_id
  msg[:name] = @tool_name if @tool_name
  # Surface assistant tool_calls (persisted as metadata) so the adapter
  # can rebuild the toolUse block expected by strict providers on resume.
  msg[:tool_calls] = @metadata[:tool_calls] if @metadata.is_a?(Hash) && @metadata[:tool_calls]
  msg
end

#to_rowObject

Returns a hash suitable for database insertion



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubino/session/message.rb', line 34

def to_row
  {
    id: @id,
    session_id: @session_id,
    role: @role,
    content: @content,
    tool_name: @tool_name,
    tool_call_id: @tool_call_id,
    token_count: @token_count,
    metadata_json: @metadata.empty? ? nil : JSON.generate(@metadata),
    created_at: @created_at
  }
end

#valid?Boolean

Validates the message attributes

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubino/session/message.rb', line 29

def valid?
  VALID_ROLES.include?(@role) && @session_id
end