Class: Legion::LLM::Call::NativeResponseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/llm/call/dispatch.rb

Overview

Wraps a native dispatch result hash so Pipeline::Executor and ConversationStore can consume a stable provider response object.

Constant Summary collapse

HASH_KEY_MAP =
{
  result: :content, content: :content,
  input_tokens: :input_tokens, output_tokens: :output_tokens,
  cache_read_tokens: :cache_read_tokens, cache_write_tokens: :cache_write_tokens,
  usage: :usage, metadata: :metadata,
  tool_calls: :tool_calls, stop_reason: :stop_reason,
  data: :content, model: :model
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_hash) ⇒ NativeResponseAdapter

Returns a new instance of NativeResponseAdapter.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/llm/call/dispatch.rb', line 24

def initialize(result_hash)
  result_hash = self.class.coerce_result(result_hash)
  @content             = result_hash[:result].to_s
  @model               = result_hash[:model]
  @metadata            = result_hash[:metadata] || {}
  @tool_calls          = result_hash[:tool_calls] || []
  @stop_reason         = result_hash[:stop_reason]
  usage                = self.class.coerce_usage(result_hash[:usage])
  @usage               = usage
  @input_tokens        = usage.input_tokens
  @output_tokens       = usage.output_tokens
  @cache_read_tokens   = usage.cache_read_tokens
  @cache_write_tokens  = usage.cache_write_tokens
end

Instance Attribute Details

#cache_read_tokensObject (readonly)

Returns the value of attribute cache_read_tokens.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def cache_read_tokens
  @cache_read_tokens
end

#cache_write_tokensObject (readonly)

Returns the value of attribute cache_write_tokens.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def cache_write_tokens
  @cache_write_tokens
end

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def content
  @content
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def input_tokens
  @input_tokens
end

#metadataObject (readonly)

Returns the value of attribute metadata.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def model
  @model
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def output_tokens
  @output_tokens
end

#stop_reasonObject (readonly)

Returns the value of attribute stop_reason.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def stop_reason
  @stop_reason
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def tool_calls
  @tool_calls
end

#usageObject (readonly)

Returns the value of attribute usage.



11
12
13
# File 'lib/legion/llm/call/dispatch.rb', line 11

def usage
  @usage
end

Class Method Details

.coerce_result(raw) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/llm/call/dispatch.rb', line 52

def self.coerce_result(raw)
  return raw if raw.is_a?(Hash)

  {
    result:      raw.respond_to?(:content) ? raw.content : raw,
    usage:       Usage.new(
      input_tokens:       raw.respond_to?(:input_tokens) ? raw.input_tokens.to_i : 0,
      output_tokens:      raw.respond_to?(:output_tokens) ? raw.output_tokens.to_i : 0,
      cache_read_tokens:  raw.respond_to?(:cached_tokens) ? raw.cached_tokens.to_i : 0,
      cache_write_tokens: raw.respond_to?(:cache_creation_tokens) ? raw.cache_creation_tokens.to_i : 0
    ),
    metadata:    raw.respond_to?(:metadata) && raw..is_a?(Hash) ? raw. : {},
    tool_calls:  raw.respond_to?(:tool_calls) ? raw.tool_calls : [],
    stop_reason: raw.respond_to?(:stop_reason) ? raw.stop_reason : nil
  }.compact
end

.coerce_usage(raw_usage) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/legion/llm/call/dispatch.rb', line 69

def self.coerce_usage(raw_usage)
  return raw_usage if raw_usage.is_a?(Usage)
  return Usage.new unless raw_usage.is_a?(Hash)

  Usage.new(
    input_tokens:       (raw_usage[:input_tokens] || raw_usage['input_tokens']).to_i,
    output_tokens:      (raw_usage[:output_tokens] || raw_usage['output_tokens']).to_i,
    cache_read_tokens:  (raw_usage[:cache_read_tokens] || raw_usage['cache_read_tokens']).to_i,
    cache_write_tokens: (raw_usage[:cache_write_tokens] || raw_usage['cache_write_tokens']).to_i
  )
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
# File 'lib/legion/llm/call/dispatch.rb', line 39

def [](key)
  attr = HASH_KEY_MAP[key.to_sym]
  attr ? public_send(attr) : nil
end

#dig(*keys) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/legion/llm/call/dispatch.rb', line 44

def dig(*keys)
  value = self[keys.first]
  return value if keys.length == 1
  return nil unless value.respond_to?(:dig)

  value.dig(*keys[1..])
end