Module: Legion::LLM::API::Namespaces::OpenAI::Responses
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/namespaces/openai/responses.rb
Class Method Summary collapse
- .build_output_tool_calls(pipeline_response) ⇒ Object
- .build_tool_declarations(tools) ⇒ Object
- .build_usage(tokens) ⇒ Object
- .call_executor(executor, upstream_body: nil) ⇒ Object
- .extract_token(tokens, key) ⇒ Object
- .flush_pending_tool_calls(messages, pending) ⇒ Object
- .format_response(pipeline_response, request_id:, model:) ⇒ Object
-
.normalize_input_array(input) ⇒ Object
— Support methods —.
-
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- .sse(name, payload) ⇒ Object
- .stream_response(out, executor, request_id:, model:, upstream_body: nil) ⇒ Object
Class Method Details
.build_output_tool_calls(pipeline_response) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 305 def self.build_output_tool_calls(pipeline_response) tools_data = pipeline_response.respond_to?(:tools) ? pipeline_response.tools : nil return [] unless tools_data.is_a?(Array) && !tools_data.empty? tools_data.filter_map do |tc| name = tc.respond_to?(:name) ? tc.name : (tc[:name] || tc['name']) args = tc.respond_to?(:arguments) ? tc.arguments : (tc[:arguments] || tc['arguments'] || {}) tc_id = tc.respond_to?(:id) ? tc.id : (tc[:id] || tc['id'] || "call_#{SecureRandom.hex(8)}") next unless name { type: 'function_call', id: "fc_#{SecureRandom.hex(12)}", call_id: tc_id, name: name.to_s, arguments: args.is_a?(String) ? args : Legion::JSON.dump(args), status: 'completed' } end end |
.build_tool_declarations(tools) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 190 def self.build_tool_declarations(tools) return [] unless tools.is_a?(Array) && !tools.empty? tools.filter_map do |tool| fn = nil t = tool.respond_to?(:transform_keys) ? tool.transform_keys(&:to_sym) : tool fn = t[:function] || t fn = fn.transform_keys(&:to_sym) if fn.respond_to?(:transform_keys) next unless fn[:name].to_s.length.positive? Legion::LLM::Types::ToolDefinition.build( name: fn[:name].to_s, description: fn[:description].to_s, parameters: fn[:parameters] || {}, source: { type: :client, executable: true } ) rescue StandardError => e tool_name = fn.is_a?(Hash) ? fn[:name] : nil Legion::Logging::Helper.log.warn("[llm][api][namespaces][openai][responses] build_tool failed name=#{tool_name} error=#{e.}") nil end end |
.build_usage(tokens) ⇒ Object
320 321 322 323 324 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 320 def self.build_usage(tokens) i = extract_token(tokens, :input_tokens) o = extract_token(tokens, :output_tokens) { input_tokens: i, output_tokens: o, total_tokens: i + o } end |
.call_executor(executor, upstream_body: nil) ⇒ Object
297 298 299 300 301 302 303 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 297 def self.call_executor(executor, upstream_body: nil, &) if upstream_body && executor.respond_to?(:call_responses) executor.call_responses(body: upstream_body, stream: true, &) else executor.call_stream(&) end end |
.extract_token(tokens, key) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 326 def self.extract_token(tokens, key) return 0 if tokens.nil? if tokens.is_a?(Hash) v = tokens[key] || tokens[key.to_s] return v.to_i unless v.nil? alt = key == :input_tokens ? :input : :output v2 = tokens[alt] || tokens[alt.to_s] return v2.to_i unless v2.nil? return 0 end method_name = { input_tokens: :input_tokens, output_tokens: :output_tokens, input: :input_tokens, output: :output_tokens }[key] return tokens.public_send(method_name).to_i if method_name && tokens.respond_to?(method_name) 0 end |
.flush_pending_tool_calls(messages, pending) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 177 def self.flush_pending_tool_calls(, pending) return if pending.empty? << { role: 'assistant', content: '', tool_calls: pending.map do |tc| { id: tc[:id], type: 'function', function: { name: tc[:name], arguments: tc[:arguments] } } end } pending.clear end |
.format_response(pipeline_response, request_id:, model:) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 213 def self.format_response(pipeline_response, request_id:, model:) routing = pipeline_response.routing || {} tokens = pipeline_response.tokens || {} raw_msg = pipeline_response. content = raw_msg.is_a?(Hash) ? (raw_msg[:content] || raw_msg['content']).to_s : raw_msg.to_s resolved_model = (routing[:model] || routing['model'] || model).to_s tool_calls = build_output_tool_calls(pipeline_response) output = [*tool_calls, { type: 'message', id: "msg_#{SecureRandom.hex(12)}", role: 'assistant', content: [{ type: 'output_text', text: content }], status: 'completed' }] { id: request_id, object: 'response', created_at: Time.now.to_i, model: resolved_model, output: output, usage: build_usage(tokens), status: 'completed' } end |
.normalize_input_array(input) ⇒ Object
— Support methods —
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 147 def self.normalize_input_array(input) = [] pending_tool_calls = [] input.each do |item| item = item.transform_keys(&:to_sym) if item.respond_to?(:transform_keys) case item[:type]&.to_s when 'function_call' pending_tool_calls << { id: item[:call_id] || item[:id], name: item[:name].to_s, arguments: item[:arguments].is_a?(String) ? item[:arguments] : Legion::JSON.dump(item[:arguments] || {}) } when 'function_call_output' flush_pending_tool_calls(, pending_tool_calls) << { role: 'tool', tool_call_id: item[:call_id], content: item[:output].to_s } else flush_pending_tool_calls(, pending_tool_calls) role = item[:role]&.to_s next unless role content = item[:content] content = content.to_s if content && !content.is_a?(Array) << { role: role, content: content }.compact end end flush_pending_tool_calls(, pending_tool_calls) end |
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 69 70 71 72 73 74 75 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 17 def self.registered(app) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength log.debug('[llm][api][namespaces][openai][responses] registering routes') # rubocop:disable Metrics/BlockLength app.post '/v1/responses' do require_llm! body = parse_request_body request_id = "resp_#{SecureRandom.hex(16)}" input = body[:input] = case input when Array Responses.normalize_input_array(input) when String [{ role: 'user', content: input }] else return openai_error('input is required (string or array)', type: 'invalid_request_error', status_code: 400) end = [{ role: 'system', content: body[:instructions].to_s }] + if body[:instructions] model = body[:model] || Legion::LLM::Settings.value(:default_model) || 'default' streaming = body[:stream] == true tool_decls = Responses.build_tool_declarations(body[:tools]) log.info("[llm][api][namespaces][openai][responses] action=accepted request_id=#{request_id} model=#{model} stream=#{streaming}") inference_request = Legion::LLM::Inference::Request.build( id: request_id, messages: , routing: { model: model }, tools: tool_decls, caller: build_server_caller(source: 'openai_responses', path: request.path, env: env), stream: streaming, cache: { strategy: :default, cacheable: true } ) executor = Legion::LLM::Inference::Executor.new(inference_request) if streaming content_type 'text/event-stream' headers 'Cache-Control' => 'no-cache', 'Connection' => 'keep-alive', 'X-Accel-Buffering' => 'no' stream do |out| Responses.stream_response(out, executor, request_id: request_id, model: model, upstream_body: body) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.namespaces.openai.responses.stream', request_id: request_id) out << "event: error\ndata: #{Legion::JSON.dump({ type: 'server_error', message: e. })}\n\n" end else pipeline_response = executor.call_responses(body: body, stream: false) response_body = Responses.format_response(pipeline_response, request_id: request_id, model: model) log.info("[llm][api][namespaces][openai][responses] action=complete request_id=#{request_id}") content_type :json status 200 Legion::JSON.dump(response_body) end rescue Legion::LLM::AuthError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.namespaces.openai.responses.auth') openai_error(e., type: 'authentication_error', status_code: 401) rescue Legion::LLM::RateLimitError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.api.namespaces.openai.responses.rate_limit') openai_error(e., type: 'rate_limit_error', code: 'rate_limit_exceeded', status_code: 429) rescue Legion::LLM::ProviderDown, Legion::LLM::ProviderError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.namespaces.openai.responses.provider') openai_error(e., type: 'server_error', status_code: 502) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.namespaces.openai.responses') openai_error(e., type: 'server_error', status_code: 500) end # rubocop:enable Metrics/BlockLength app.get '/v1/responses/:id' do log.debug("[llm][api][namespaces][openai][responses] action=retrieve id=#{params[:id]}") openai_error("Response '#{params[:id]}' not found", type: 'invalid_request_error', code: 'response_not_found', status_code: 404) end app.delete '/v1/responses/:id' do log.debug("[llm][api][namespaces][openai][responses] action=delete id=#{params[:id]}") content_type :json Legion::JSON.dump({ id: params[:id], object: 'response', deleted: true }) end app.post '/v1/responses/:id/cancel' do log.debug("[llm][api][namespaces][openai][responses] action=cancel id=#{params[:id]}") openai_error("Response '#{params[:id]}' not found or already completed", type: 'invalid_request_error', status_code: 404) end app.get '/v1/responses/:id/input_items' do log.debug("[llm][api][namespaces][openai][responses] action=input_items id=#{params[:id]}") content_type :json Legion::JSON.dump({ object: 'list', data: [], has_more: false }) end app.post '/v1/responses/:id/input_tokens/count' do body = parse_request_body input = body[:input] model = body[:model] || params[:id] = case input when Array then Responses.normalize_input_array(input) when String then [{ role: 'user', content: input }] else [] end result = Legion::LLM::TokenEstimation.estimate(messages: , model: model.to_s) content_type :json Legion::JSON.dump(result) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.namespaces.openai.responses.count_tokens') openai_error(e., type: 'server_error', status_code: 500) end app.post '/v1/responses/:id/compact' do log.debug("[llm][api][namespaces][openai][responses] action=compact id=#{params[:id]}") openai_error("Response '#{params[:id]}' not found", type: 'invalid_request_error', status_code: 404) end # Legacy alias — preserved for clients using the pre-namespace path. app.post '/api/llm/inference/v1/responses' do log.debug('[llm][api][namespaces][openai][responses] action=legacy_alias forwarding to /v1/responses handler') call env.merge('PATH_INFO' => '/v1/responses') end log.debug('[llm][api][namespaces][openai][responses] routes registered') rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.namespaces.openai.responses.register') end |
.sse(name, payload) ⇒ Object
347 348 349 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 347 def self.sse(name, payload) "event: #{name}\ndata: #{Legion::JSON.dump(payload)}\n\n" end |
.stream_response(out, executor, request_id:, model:, upstream_body: nil) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/legion/llm/api/namespaces/openai/responses.rb', line 233 def self.stream_response(out, executor, request_id:, model:, upstream_body: nil) created_at = Time.now.to_i seq = 0 base_resp = { id: request_id, object: 'response', created_at: created_at, status: 'in_progress', model: model, output: [], usage: nil } out << sse('response.created', { type: 'response.created', sequence_number: seq += 1, response: base_resp }) out << sse('response.in_progress', { type: 'response.in_progress', sequence_number: seq += 1, response: base_resp }) msg_id = "msg_#{SecureRandom.hex(12)}" out << sse('response.output_item.added', { type: 'response.output_item.added', sequence_number: seq += 1, output_index: 0, item: { id: msg_id, type: 'message', role: 'assistant', content: [], status: 'in_progress' } }) out << sse('response.content_part.added', { type: 'response.content_part.added', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, part: { type: 'output_text', text: '', annotations: [] } }) full_text = +'' pipeline_response = call_executor(executor, upstream_body: upstream_body) do |chunk| text = chunk.respond_to?(:content) ? chunk.content.to_s : chunk.to_s next if text.empty? full_text << text out << sse('response.output_text.delta', { type: 'response.output_text.delta', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, delta: text }) end routing = pipeline_response.routing || {} tokens = pipeline_response.tokens || {} resolved_model = (routing[:model] || routing['model'] || model).to_s usage = build_usage(tokens) function_calls = build_output_tool_calls(pipeline_response) out << sse('response.output_text.done', { type: 'response.output_text.done', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, text: full_text }) out << sse('response.content_part.done', { type: 'response.content_part.done', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, part: { type: 'output_text', text: full_text, annotations: [] } }) completed_item = { id: msg_id, type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: full_text, annotations: [] }] } out << sse('response.output_item.done', { type: 'response.output_item.done', sequence_number: seq += 1, output_index: 0, item: completed_item }) function_calls.each_with_index do |fc, idx| oi = idx + 1 out << sse('response.output_item.added', { type: 'response.output_item.added', sequence_number: seq += 1, output_index: oi, item: fc.merge(status: 'in_progress', arguments: '') }) out << sse('response.function_call_arguments.delta', { type: 'response.function_call_arguments.delta', sequence_number: seq += 1, output_index: oi, item_id: fc[:id], delta: fc[:arguments] }) out << sse('response.function_call_arguments.done', { type: 'response.function_call_arguments.done', sequence_number: seq += 1, output_index: oi, item_id: fc[:id], arguments: fc[:arguments] }) out << sse('response.output_item.done', { type: 'response.output_item.done', sequence_number: seq += 1, output_index: oi, item: fc }) end out << sse('response.completed', { type: 'response.completed', sequence_number: seq + 1, response: { id: request_id, object: 'response', created_at: created_at, status: 'completed', model: resolved_model, output: [completed_item, *function_calls], usage: usage } }) end |