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

Inherits:
Transport::Message
  • Object
show all
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



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

def app_id
  @options[:app_id] || 'legion-llm'
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).



32
33
34
# File 'lib/legion/llm/transport/message.rb', line 32

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

#headersObject



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

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

#messageObject



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

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

#message_contextObject



18
19
20
# File 'lib/legion/llm/transport/message.rb', line 18

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

#message_idObject



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

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

#tracing_headersObject



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

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