Module: Legion::LLM::API::Native::Helpers
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/native/helpers.rb
Class Method Summary collapse
-
.registered(app) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity.
Class Method Details
.registered(app) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
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 144 145 146 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/legion/llm/api/native/helpers.rb', line 117 def self.registered(app) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity log.debug('[llm][api][helpers] registering shared helpers') app.helpers do # rubocop:disable Metrics/BlockLength include Legion::Logging::Helper unless method_defined?(:parse_request_body) define_method(:parse_request_body) do log.debug('[llm][api][helpers] parse_request_body action=parsing') raw = request.body.read return {} if raw.nil? || raw.empty? parsed = begin Legion::JSON.load(raw) rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.api.parse_request_body') halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'invalid_json', message: 'request body is not valid JSON' } }) end unless parsed.respond_to?(:transform_keys) halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'invalid_request_body', message: 'request body must be a JSON object' } }) end parsed.transform_keys(&:to_sym) end end unless method_defined?(:validate_required!) define_method(:validate_required!) do |body, *keys| missing = keys.select { |k| body[k].nil? || (body[k].respond_to?(:empty?) && body[k].empty?) } return if missing.empty? log.debug("[llm][api][helpers] validate_required! missing=#{missing.join(',')}") halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'missing_fields', message: "required: #{missing.join(', ')}" } }) end end unless method_defined?(:json_response) define_method(:json_response) do |data, status_code: 200| content_type :json status status_code Legion::JSON.dump({ data: data }) end end unless method_defined?(:json_error) define_method(:json_error) do |code, , status_code: 400| content_type :json status status_code Legion::JSON.dump({ error: { code: code, message: } }) end end unless method_defined?(:require_llm!) define_method(:require_llm!) do return if defined?(Legion::LLM) && Legion::LLM.respond_to?(:started?) && Legion::LLM.started? log.debug('[llm][api][helpers] require_llm! action=halting reason=not_started') halt 503, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'llm_unavailable', message: 'LLM subsystem is not available' } }) end end unless method_defined?(:cache_available?) define_method(:cache_available?) do defined?(Legion::Cache) && Legion::Cache.respond_to?(:connected?) && Legion::Cache.connected? end end unless method_defined?(:validate_tools!) define_method(:validate_tools!) do |tool_list| unless tool_list.is_a?(Array) && tool_list.all? { |t| t.respond_to?(:transform_keys) } halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'invalid_tools', message: 'tools must be an array of objects' } }) end invalid = tool_list.any? do |t| ts = t.transform_keys(&:to_sym) ts[:name].to_s.empty? end return unless invalid halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'invalid_tools', message: 'each tool must have a non-empty name' } }) end end unless method_defined?(:validate_messages!) define_method(:validate_messages!) do |msg_list| valid = msg_list.all? do |m| next false unless m.respond_to?(:key?) && m.respond_to?(:[]) role = m[:role] || m['role'] content_value = m[:content] || m['content'] !role.to_s.empty? && (m.key?(:content) || m.key?('content')) && !content_value.nil? && !(content_value.respond_to?(:empty?) && content_value.empty?) end return if valid halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { code: 'invalid_messages', message: 'each message must be an object with non-empty role and content' } }) end end define_method(:build_client_tool_class) do |tname, tdesc, tschema| log.debug("[llm][api][helpers] build_client_tool_class name=#{tname}") tool_ref = tname klass = Class.new(RubyLLM::Tool) do include Legion::LLM::API::Native::ClientToolMethods description tdesc define_method(:name) { tool_ref } define_method(:execute) do |**kwargs| summary = summarize_tool_args(tool_ref, kwargs) log_tool(:info, tool_ref, 'executing', **summary) t0 = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) result = dispatch_client_tool(tool_ref, **kwargs) ms = ((::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - t0) * 1000).round(1) log_tool(:info, tool_ref, 'completed', duration_ms: ms, result_size: result.to_s.bytesize) notify_tool_event(:tool_result, tool_ref, result: result.to_s[0, 4096]) result rescue StandardError => e ms = begin ((::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - t0) * 1000).round(1) rescue StandardError nil end log_tool(:error, tool_ref, 'failed', duration_ms: ms, error: e.) notify_tool_event(:tool_error, tool_ref, error: e.) Legion::Logging.log_exception(e, payload_summary: "client tool #{tool_ref} failed", component_type: :api) "Tool error: #{e.}" end end klass.params(tschema) if tschema.is_a?(Hash) && tschema[:properties] klass rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: "llm.api.build_client_tool_class.#{tname}") nil end define_method(:extract_tool_calls) do |pipeline_response| tools_data = pipeline_response.tools return [] unless tools_data.is_a?(Array) && !tools_data.empty? tools_data.map do |tc| { id: tc.respond_to?(:id) ? tc.id : (tc[:id] || tc['id']), name: tc.respond_to?(:name) ? tc.name : (tc[:name] || tc['name'] || tc.to_s), arguments: tc.respond_to?(:arguments) ? tc.arguments : (tc[:arguments] || tc['arguments'] || {}) } end end define_method(:emit_sse_event) do |stream, event_name, payload| level = event_name == 'text-delta' ? :debug : :info log.send(level, "[sse][emit] event=#{event_name} keys=#{payload.is_a?(Hash) ? payload.keys.join(',') : 'n/a'}") stream << "event: #{event_name}\ndata: #{Legion::JSON.dump(payload)}\n\n" end define_method(:emit_timeline_tool_events) do |stream, pipeline_response, skip_tool_results: false| timeline = Array(pipeline_response.timeline) log.debug("[llm][api][helpers] emit_timeline_tool_events count=#{timeline.size} skip_tool_results=#{skip_tool_results}") timeline.each do |event| key = event[:key].to_s detail = event[:detail] data = event[:data].is_a?(Hash) ? event[:data] : {} name = key.split(':', 3).last next if name.to_s.empty? if key.start_with?('tool:result:') next if skip_tool_results event_name = data[:status].to_s == 'error' ? 'tool-error' : 'tool-result' emit_sse_event(stream, event_name, { toolCallId: data[:tool_call_id], toolName: name, result: data[:result] || detail, status: data[:status], timestamp: Time.now.utc.iso8601 }) elsif key.start_with?('tool:execute:') emit_sse_event(stream, 'tool-progress', { toolCallId: data[:tool_call_id], toolName: name, type: 'execution_complete', args: data[:arguments] || {}, source: data[:source], status: detail, timestamp: Time.now.utc.iso8601 }) end end end define_method(:token_value) do |tokens, key| return nil if tokens.nil? return tokens[key] || tokens[key.to_s] if tokens.is_a?(Hash) method_name = { input: :input_tokens, output: :output_tokens, total: :total_tokens }[key] return tokens.public_send(method_name) if method_name && tokens.respond_to?(method_name) nil end end log.debug('[llm][api][helpers] shared helpers registered') end |