Class: Legion::Extensions::Llm::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/message.rb

Overview

A single message in a chat conversation.

Direct Known Subclasses

Chunk

Constant Summary collapse

ROLES =
%i[system user assistant tool].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Returns a new instance of Message.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/llm/message.rb', line 13

def initialize(options = {})
  @role = options.fetch(:role).to_sym
  @tool_calls = options[:tool_calls]
  @content = normalize_content(options.fetch(:content), role: @role, tool_calls: @tool_calls)
  @model_id = options[:model_id]
  @tool_call_id = options[:tool_call_id]
  @tokens = options[:tokens] || Tokens.build(
    input: options[:input_tokens],
    output: options[:output_tokens],
    cached: options[:cached_tokens],
    cache_creation: options[:cache_creation_tokens],
    thinking: options[:thinking_tokens],
    reasoning: options[:reasoning_tokens]
  )
  @raw = options[:raw]
  @thinking = options[:thinking]

  ensure_valid_role
end

Instance Attribute Details

#contentObject



33
34
35
36
37
38
39
# File 'lib/legion/extensions/llm/message.rb', line 33

def content
  if @content.is_a?(Content) && @content.text && @content.attachments.empty?
    @content.text
  else
    @content
  end
end

#model_idObject (readonly)

Returns the value of attribute model_id.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def model_id
  @model_id
end

#rawObject (readonly)

Returns the value of attribute raw.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def raw
  @raw
end

#roleObject (readonly)

Returns the value of attribute role.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def role
  @role
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def thinking
  @thinking
end

#tokensObject (readonly)

Returns the value of attribute tokens.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def tokens
  @tokens
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



10
11
12
# File 'lib/legion/extensions/llm/message.rb', line 10

def tool_calls
  @tool_calls
end

Instance Method Details

#cache_creation_tokensObject



65
66
67
# File 'lib/legion/extensions/llm/message.rb', line 65

def cache_creation_tokens
  tokens&.cache_creation
end

#cached_tokensObject



61
62
63
# File 'lib/legion/extensions/llm/message.rb', line 61

def cached_tokens
  tokens&.cached
end

#input_tokensObject



53
54
55
# File 'lib/legion/extensions/llm/message.rb', line 53

def input_tokens
  tokens&.input
end

#instance_variablesObject



89
90
91
# File 'lib/legion/extensions/llm/message.rb', line 89

def instance_variables
  super - [:@raw]
end

#output_tokensObject



57
58
59
# File 'lib/legion/extensions/llm/message.rb', line 57

def output_tokens
  tokens&.output
end

#reasoning_tokensObject



73
74
75
# File 'lib/legion/extensions/llm/message.rb', line 73

def reasoning_tokens
  tokens&.thinking
end

#thinking_tokensObject



69
70
71
# File 'lib/legion/extensions/llm/message.rb', line 69

def thinking_tokens
  tokens&.thinking
end

#to_hObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/legion/extensions/llm/message.rb', line 77

def to_h
  {
    role: role,
    content: content,
    model_id: model_id,
    tool_calls: tool_calls,
    tool_call_id: tool_call_id,
    thinking: thinking&.text,
    thinking_signature: thinking&.signature
  }.merge(tokens ? tokens.to_h : {}).compact
end

#tool_call?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/legion/extensions/llm/message.rb', line 41

def tool_call?
  !tool_calls.nil? && !tool_calls.empty?
end

#tool_result?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/extensions/llm/message.rb', line 45

def tool_result?
  !tool_call_id.nil? && !tool_call_id.empty?
end

#tool_resultsObject



49
50
51
# File 'lib/legion/extensions/llm/message.rb', line 49

def tool_results
  content if tool_result?
end