Module: Legion::LLM::API::Namespaces::Helpers

Includes:
SharedHelpers
Defined in:
lib/legion/llm/api/namespaces/helpers.rb

Instance Method Summary collapse

Methods included from SharedHelpers

#build_client_tool_class, #build_response_metrics, #build_server_caller, #build_tool_definitions, #cache_available?, #detect_modality, #emit_response_tool_call_events, #emit_sse_event, #emit_timeline_tool_events, #extract_text_content, #extract_tool_calls, #identity_caller_hash, #identity_canonical_name, #identity_request_from_env, #json_error, #json_response, #log_native_inference_response, #openai_tool_call_arguments, #openai_tool_call_name, #parse_request_body, #require_llm!, #returned_client_tool_call_payload, #token_value, #validate_messages!, #validate_required!, #validate_tools!

Instance Method Details

#anthropic_error(error_type, message, status_code: 500) ⇒ Object



21
22
23
24
25
# File 'lib/legion/llm/api/namespaces/helpers.rb', line 21

def anthropic_error(error_type, message, status_code: 500)
  content_type :json
  status status_code
  Legion::JSON.dump({ type: 'error', error: { type: error_type, message: message } })
end

#data_subsystem_available?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/legion/llm/api/namespaces/helpers.rb', line 43

def data_subsystem_available?
  defined?(Legion::Data) && Legion::Data.respond_to?(:connected?) && Legion::Data.connected?
rescue StandardError
  false
end

#detect_client(rack_env) ⇒ Object



27
28
29
30
31
32
# File 'lib/legion/llm/api/namespaces/helpers.rb', line 27

def detect_client(rack_env)
  return :anthropic if rack_env['HTTP_ANTHROPIC_VERSION']
  return :anthropic if rack_env['HTTP_X_API_KEY'] && !rack_env['HTTP_AUTHORIZATION']

  :openai
end

#openai_error(message, type: 'server_error', code: nil, status_code: 500) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/legion/llm/api/namespaces/helpers.rb', line 13

def openai_error(message, type: 'server_error', code: nil, status_code: 500)
  content_type :json
  status status_code
  body = { error: { message: message, type: type } }
  body[:error][:code] = code if code
  Legion::JSON.dump(body)
end

#require_data!Object



34
35
36
37
38
39
40
41
# File 'lib/legion/llm/api/namespaces/helpers.rb', line 34

def require_data!
  return if data_subsystem_available?

  halt 503, { 'Content-Type' => 'application/json' },
       Legion::JSON.dump({ error: { code:    'data_required',
                                    message: 'Legion::Data is required for this operation',
                                    type:    'server_error' } })
end