Class: Legion::Data::Models::LLM::MessageInferenceRequest

Inherits:
Object
  • Object
show all
Includes:
ModelHelpers
Defined in:
lib/legion/data/models/llm/message_inference_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModelHelpers

#before_create

Class Method Details

.audit_lineage_for(reference) ⇒ Object



31
32
33
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 31

def audit_lineage_for(reference)
  lookup(reference)&.audit_lineage
end

.lookup(reference) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 22

def lookup(reference)
  return reference if reference.is_a?(self)

  value = reference.to_s
  scope = where(uuid: value).or(request_ref: value)
  scope = scope.or(id: value.to_i) if value.match?(/\A\d+\z/)
  scope.first
end

Instance Method Details

#audit_lineageObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 36

def audit_lineage
  responses = message_inference_responses_dataset.order(:id).all
  response_ids = responses.map(&:id)
  tool_calls = ToolCall.where(message_inference_response_id: response_ids).order(:tool_call_index, :id).all
  tool_call_ids = tool_calls.map(&:id)

  {
    request:            self,
    request_id:         id,
    request_ref:        request_ref,
    conversation:       conversation,
    latest_message:     latest_message,
    caller_principal:   caller_principal,
    caller_identity:    caller_identity,
    route_attempts:     route_attempts_dataset.order(:attempt_no, :id).all,
    responses:          responses,
    response_messages:  responses.filter_map(&:response_message),
    metrics:            message_inference_metrics_dataset.order(:recorded_at, :id).all,
    policy_evaluations: policy_evaluations_dataset.order(:evaluated_at, :id).all,
    security_events:    security_events_dataset.order(:detected_at, :id).all,
    tool_calls:         tool_calls,
    tool_call_attempts: ToolCallAttempt.where(tool_call_id: tool_call_ids).order(:tool_call_id, :attempt_no, :id).all
  }
end

#caller_identityObject



71
72
73
74
75
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 71

def caller_identity
  return nil unless caller_identity_id && defined?(Legion::Data::Model::Identity::Identity)

  Legion::Data::Model::Identity::Identity.first(id: caller_identity_id)
end

#caller_principalObject



65
66
67
68
69
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 65

def caller_principal
  return nil unless caller_principal_id && defined?(Legion::Data::Model::Identity::Principal)

  Legion::Data::Model::Identity::Principal.first(id: caller_principal_id)
end

#requestObject



61
62
63
# File 'lib/legion/data/models/llm/message_inference_request.rb', line 61

def request
  self
end