Class: Legion::LLM::Transport::Message

Inherits:
Transport::Message
  • Object
show all
Includes:
Legion::Logging::Helper
Defined in:
lib/legion/llm/transport/message.rb

Constant Summary collapse

LLM_ENVELOPE_KEYS =

Keys stripped from the JSON body (in addition to base ENVELOPE_KEYS). Do NOT add keys already in ENVELOPE_KEYS (:routing_key, :reply_to, etc.). Do NOT add :request_type — metering/audit need it in the body. Do NOT add :message_context — it MUST appear in the body of all 6 messages.

%i[
  fleet_correlation_id ttl
].freeze

Instance Method Summary collapse

Instance Method Details

#app_idObject



39
40
41
# File 'lib/legion/llm/transport/message.rb', line 39

def app_id
  @options[:app_id] || 'legion-llm'
end

#content_encodingObject



63
64
65
# File 'lib/legion/llm/transport/message.rb', line 63

def content_encoding
  @options[:content_encoding] || super
end

#correlation_idObject

Fleet messages use :fleet_correlation_id to avoid collision with the base class’s :correlation_id (which falls through to :parent_id/:task_id).



35
36
37
# File 'lib/legion/llm/transport/message.rb', line 35

def correlation_id
  @options[:fleet_correlation_id] || super
end

#encode_messageObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/llm/transport/message.rb', line 47

def encode_message
  payload = message
  payload = Legion::JSON.dump(payload) unless payload.is_a?(String)

  if encrypt? && defined?(Legion::Crypt) && Legion::Crypt.respond_to?(:encrypt)
    encrypted = Legion::Crypt.encrypt(payload)
    @encrypted_iv = encrypted[:iv]
    @options[:content_encoding] = 'encrypted/cs'
    log.debug "[llm][transport] encode_message action=encrypt class=#{self.class.name}"
    return encrypted[:enciphered_message]
  end

  @options[:content_encoding] = 'identity'
  payload
end

#headersObject



43
44
45
# File 'lib/legion/llm/transport/message.rb', line 43

def headers
  super.merge(llm_headers).merge(context_headers).merge(tracing_headers).merge(encryption_headers)
end

#messageObject



25
26
27
# File 'lib/legion/llm/transport/message.rb', line 25

def message
  @options.except(*ENVELOPE_KEYS, *LLM_ENVELOPE_KEYS)
end

#message_contextObject



21
22
23
# File 'lib/legion/llm/transport/message.rb', line 21

def message_context
  @options[:message_context] || {}
end

#message_idObject



29
30
31
# File 'lib/legion/llm/transport/message.rb', line 29

def message_id
  @message_id ||= @options[:message_id] || "#{message_id_prefix}_#{SecureRandom.uuid}"
end

#tracing_headersObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/llm/transport/message.rb', line 67

def tracing_headers
  tracing = @options[:tracing] || context_value(message_context, :tracing)
  return {} unless tracing.is_a?(Hash)

  trace_id = context_value(tracing, :trace_id)
  span_id = context_value(tracing, :span_id)
  parent_span_id = context_value(tracing, :parent_span_id)
  correlation_id = context_value(tracing, :correlation_id)
  baggage = baggage_header(context_value(tracing, :baggage))

  h = {}
  h['traceparent'] = "00-#{trace_id}-#{span_id}-01" if w3c_trace_id?(trace_id) && w3c_span_id?(span_id)
  h['baggage'] = baggage if baggage
  h['x-legion-trace-id']       = trace_id.to_s       if trace_id
  h['x-legion-span-id']        = span_id.to_s        if span_id
  h['x-legion-parent-span-id'] = parent_span_id.to_s if parent_span_id
  h['x-legion-correlation-id'] = correlation_id.to_s if correlation_id
  h
end