Class: LlmGateway::Adapters::OpenAI::Responses::InputMapper

Inherits:
ChatCompletions::InputMapper show all
Defined in:
lib/llm_gateway/adapters/openai/responses/input_mapper.rb

Class Method Summary collapse

Methods inherited from ChatCompletions::InputMapper

map, map_system

Class Method Details

.map_assistant_history_message(msg) ⇒ Object

Map a full AssistantMessage#to_h into Responses API input items for stateless multi-turn conversations.

text blocks   → { role: "assistant", content: [{ type: "output_text", ... }] }
tool_use blocks → top-level function_call items
thinking blocks → omitted (model handles reasoning internally)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/llm_gateway/adapters/openai/responses/input_mapper.rb', line 76

def self.map_assistant_history_message(msg)
  blocks = (msg[:content] || []).map { |b| b.transform_keys(&:to_sym) }

  text_blocks     = blocks.select { |b| b[:type] == "text" }
  tool_use_blocks = blocks.select { |b| b[:type] == "tool_use" }

  result = []

  if text_blocks.any?
    result << {
      role: "assistant",
      content: text_blocks.map { |b| { type: "output_text", text: b[:text] } }
    }
  end

  tool_use_blocks.each do |b|
    result << {
      type: "function_call",
      call_id: b[:id],
      name: b[:name],
      arguments: b[:input].is_a?(Hash) ? b[:input].to_json : (b[:input] || {}).to_json
    }
  end

  result
end

.map_messages(messages) ⇒ Object



39
40
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
# File 'lib/llm_gateway/adapters/openai/responses/input_mapper.rb', line 39

def self.map_messages(messages)
  return messages unless messages
  mapper = message_mapper

  messages.flat_map do |msg|
    if msg[:id] && msg[:content].is_a?(Array)
      # Full AssistantMessage#to_h — expand content for stateless multi-turn
      map_assistant_history_message(msg)
    elsif msg[:id]
      # Bare item-reference (e.g. manually constructed { id: "item_xxx" })
      msg.slice(:id)
    else
      content = if msg[:content].is_a?(Array)
          msg[:content].map do |content|
            mapper.map_content(content)
          end
      else
        [ mapper.map_content(msg[:content]) ]
      end
      if msg.dig(:content).is_a?(Array) && msg.dig(:content, 0, :type) == "tool_result"
        content
      else
        {
          role: msg[:role],
          content: content
        }
      end
    end
  end
end

.map_tools(tools) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/llm_gateway/adapters/openai/responses/input_mapper.rb', line 15

def self.map_tools(tools)
  return tools unless tools
  mapper = message_mapper

  tools.map do |tool|
    mapped_tool = {
      type: "function",
      name: tool[:name],
      description: tool[:description],
      parameters: tool[:input_schema]
    }

    [ :contents, :content ].each do |key|
      next unless tool[key].is_a?(Array)

      mapped_tool[key] = tool[key].map do |entry|
        entry.is_a?(Hash) ? mapper.map_content(entry.transform_keys(&:to_sym)) : entry
      end
    end

    mapped_tool
  end
end

.message_mapperObject



11
12
13
# File 'lib/llm_gateway/adapters/openai/responses/input_mapper.rb', line 11

def self.message_mapper
  BidirectionalMessageMapper.new(LlmGateway::DIRECTION_IN)
end