Module: Legion::LLM::API::ClientTranslators::SharedExtractors

Included in:
AnthropicMessages, OpenAIChat, OpenAIResponses, StreamAssembler
Defined in:
lib/legion/llm/api/client_translators/shared_extractors.rb

Overview

Token-count and thinking-text extraction shared by all client translators. Single source of truth — replaces the 3-way duplicate that lived in AnthropicMessages, OpenAIChat, and OpenAIResponses (P6 dedup).

Instance Method Summary collapse

Instance Method Details

#args_as_json_string(args) ⇒ Object

Coerce raw tool arguments into the OpenAI wire shape: a JSON string. Non-string inputs are JSON-dumped; nil becomes “{}”; malformed values are stringified as a last resort so the wire never carries a Ruby object.



119
120
121
122
123
124
125
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 119

def args_as_json_string(args)
  return args if args.is_a?(String)

  Legion::JSON.dump(args || {})
rescue StandardError
  args.to_s
end

#args_as_object(args) ⇒ Object

Coerce raw tool arguments into the Anthropic wire shape: a Hash. Strings are parsed as JSON when possible; non-Hash literals (numbers, arrays from degraded model output) collapse to ‘{}` rather than violating the contract.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 131

def args_as_object(args)
  return args if args.is_a?(Hash)
  return {} if args.nil?

  if args.is_a?(String)
    return {} if args.empty?

    parsed = Legion::JSON.load(args)
    return parsed if parsed.is_a?(Hash)
  end

  {}
rescue StandardError
  {}
end

#extract_content_text(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 41

def extract_content_text(value)
  return '' if value.nil?
  return value if value.is_a?(String)

  if value.is_a?(Array)
    return value.filter_map do |part|
      text = extract_content_text(part)
      text.empty? ? nil : text
    end.join
  end

  if value.is_a?(Hash)
    normalized = value.transform_keys { |key| key.respond_to?(:to_sym) ? key.to_sym : key }
    content = normalized[:content]
    return extract_content_text(content) unless content.nil?

    return '' unless text_content_type?(normalized[:type])

    text = normalized[:text] || normalized[:output_text] || normalized[:value]
    return extract_content_text(text) unless text.nil?

    return ''
  end

  if value.respond_to?(:text)
    type = value.respond_to?(:type) ? value.type : nil
    return '' unless text_content_type?(type)

    return value.text.to_s
  end

  return extract_content_text(value.content) if value.respond_to?(:content)

  value.to_s
end

#extract_thinking_text(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 25

def extract_thinking_text(value)
  return '' if value.nil?
  return value.to_s if value.is_a?(String)

  if value.is_a?(Hash)
    normalized = value.transform_keys { |k| k.respond_to?(:to_sym) ? k.to_sym : k }
    text = normalized[:content] || normalized[:text] || normalized[:thinking] || normalized[:reasoning]
    return text.to_s if text
  end

  return value.content.to_s if value.respond_to?(:content) && value.content
  return value.text.to_s if value.respond_to?(:text) && value.text

  value.to_s
end

#legion_routing_explicit_from_env(env) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 90

def legion_routing_explicit_from_env(env)
  flags = {
    model:    env.key?('HTTP_X_LEGION_MODEL'),
    provider: env.key?('HTTP_X_LEGION_PROVIDER'),
    instance: env.key?('HTTP_X_LEGION_INSTANCE'),
    tier:     env.key?('HTTP_X_LEGION_TIER')
  }.select { |_, value| value }
  flags.empty? ? nil : flags
end

#legion_routing_from_env(env) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 82

def legion_routing_from_env(env)
  {
    model:    env['HTTP_X_LEGION_MODEL'],
    provider: env['HTTP_X_LEGION_PROVIDER'],
    instance: env['HTTP_X_LEGION_INSTANCE']
  }.compact
end

#text_content_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 77

def text_content_type?(type)
  type_string = type.to_s
  type_string.empty? || %w[text output_text input_text].include?(type_string)
end

#token_value(tokens, *keys) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/llm/api/client_translators/shared_extractors.rb', line 11

def token_value(tokens, *keys)
  return 0 if tokens.nil?

  keys.each do |key|
    value = if tokens.is_a?(Hash)
              tokens[key] || tokens[key.to_s]
            elsif tokens.respond_to?(key)
              tokens.public_send(key)
            end
    return value.to_i unless value.nil?
  end
  0
end