Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

1.2 KB
Raw Blame
# frozen_string_literal: true

class Captain::Assistant::InstrumentationAttributeProvider
  include Integrations::LlmInstrumentationConstants

  def initialize(service)
    @service = service
  end

  def call(context_wrapper)
    @service.send(:dynamic_trace_attributes, context_wrapper)
  end

  def generation_attributes(context_wrapper, chat, message)
    attributes = {
      format(ATTR_LANGFUSE_OBSERVATION_METADATA, 'generation_stage') => generation_stage(message)
    }
    protocol = context_wrapper&.context&.dig(:__otel_tracing, :request_attributes, :protocol)
    Captain::ResponsesConfig.metadata(chat, protocol: protocol).each do |key, value|
      attributes[format(ATTR_LANGFUSE_OBSERVATION_METADATA, key)] = value.to_s
    end
    if @service.send(:message_burst_protection_active?)
      attributes[format(ATTR_LANGFUSE_OBSERVATION_METADATA, 'discarded')] = @service.send(:newer_customer_message_arrived?).to_s
    end
    attributes
  end

  private

  def generation_stage(message)
    message_has_tool_calls?(message) ? 'tool_call' : 'final_response'
  end

  def message_has_tool_calls?(message)
    return false unless message.respond_to?(:tool_calls)

    tool_calls = message.tool_calls
    tool_calls.respond_to?(:any?) && tool_calls.any?
  end
end