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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result_hash) ⇒ NativeResponseAdapter

Returns a new instance of NativeResponseAdapter.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/legion/llm/call/dispatch.rb', line 15

def initialize(result_hash)
  result_hash = self.class.coerce_result(result_hash)
  @content             = result_hash[:result].to_s
  @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

#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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/llm/call/dispatch.rb', line 29

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/llm/call/dispatch.rb', line 46

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