iachat/enterprise/app/services/captain/llm/conversation_insight_service.rb
Rodrigo Borba 87bff8126c feat(captain): add AI reports page with insights generation
Implementa a página Relatórios IA com geração de análises semanais
por IA baseadas nas conversas de cada unidade/caixa de entrada.

Funcionalidades:
- Página /settings/captain/reports com dois tabs (Insights IA / Operacional)
- Botão "Gerar Análise" que enfileira job Sidekiq
- Filtro por unidade ou caixa de entrada
- Exibe insights com status (pendente/processando/concluído/falhou)
- Mostra top_topics, ai_failures e period_summary
- Estado vazio com CTA para gerar primeiro relatório

Backend:
- InsightsController com endpoints index/show/generate
- GenerateInsightsJob que processa conversas com LLM
- ConversationInsightService com chunking e merge inteligente
- Migração para adicionar inbox_id à tabela captain_conversation_insights
- Link sidebar "Relatórios IA" em /settings/captain/reports

Frontend:
- Vuex store captainReports com actions/mutations/getters
- API client CaptainReportsAPI (getInsights, generateInsight)
- i18n en e pt_BR para CAPTAIN_REPORTS.*

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-27 07:05:58 -03:00

163 lines
4.6 KiB
Ruby

class Captain::Llm::ConversationInsightService < Llm::BaseAiService
include Integrations::LlmInstrumentation
MAX_CHARS_PER_CHUNK = 40_000
def initialize(account:, conversations:, unit: nil, inbox: nil)
super()
@account = account
@unit = unit
@inbox = inbox
@conversations = conversations
end
# Analisa as conversas e retorna o payload de insights
def analyze
chunks = build_chunks
return empty_payload if chunks.empty?
results = chunks.filter_map { |chunk| analyze_chunk(chunk) }
return empty_payload if results.empty?
merge_results(results)
end
private
attr_reader :account, :unit, :inbox, :conversations
def build_chunks
texts = conversations.map(&:to_llm_text).reject(&:blank?)
return [] if texts.empty?
chunks = []
current = []
current_size = 0
texts.each do |text|
if current_size + text.length > MAX_CHARS_PER_CHUNK && current.any?
chunks << current.join("\n\n---\n\n")
current = []
current_size = 0
end
current << text
current_size += text.length
end
chunks << current.join("\n\n---\n\n") if current.any?
chunks
end
def analyze_chunk(chunk)
response = instrument_llm_call(instrumentation_params) do
chat
.with_params(response_format: { type: 'json_object' })
.with_instructions(system_prompt)
.ask(chunk)
end
parse_response(response.content)
rescue RubyLLM::Error => e
Rails.logger.error "[Captain::Llm::ConversationInsightService] LLM Error: #{e.message}"
nil
end
def system_prompt
entity_name = inbox&.name || unit&.name || 'Geral'
Captain::Llm::SystemPromptsService.conversation_insights_analyzer(
entity_name,
account.locale_english_name
)
end
def instrumentation_params
{
span_name: 'llm.captain.conversation_insights',
model: @model,
temperature: @temperature,
feature_name: 'conversation_insights',
account_id: account.id,
messages: [{ role: 'system', content: system_prompt }]
}
end
def merge_results(results)
base = results.first.dup
results.drop(1).each do |result|
merge_arrays!(base, result)
merge_sentiment!(base, result)
merge_highlights!(base, result)
base['recommendations'] = ((base['recommendations'] || []) + (result['recommendations'] || [])).uniq
end
base
end
def merge_arrays!(base, result)
base['top_topics'] = merge_by_topic(base['top_topics'], result['top_topics'])
base['ai_failures'] = merge_by_description(base['ai_failures'], result['ai_failures'])
base['faq_gaps'] = merge_by_question(base['faq_gaps'], result['faq_gaps'])
base['most_requested_suites'] = merge_by_suite(base['most_requested_suites'], result['most_requested_suites'])
end
def merge_sentiment!(base, result)
%w[positive_count negative_count neutral_count].each do |key|
base['sentiment'][key] = base.dig('sentiment', key).to_i + result.dig('sentiment', key).to_i
end
end
def merge_highlights!(base, result)
%w[praises complaints].each do |key|
base['highlights'][key] = (base.dig('highlights', key) || []) + (result.dig('highlights', key) || [])
end
end
def merge_by_topic(arr_a, arr_b)
merge_arrays_by_key(arr_a, arr_b, 'topic', 'count')
end
def merge_by_description(arr_a, arr_b)
merge_arrays_by_key(arr_a, arr_b, 'description', 'frequency')
end
def merge_by_question(arr_a, arr_b)
merge_arrays_by_key(arr_a, arr_b, 'question', 'frequency')
end
def merge_by_suite(arr_a, arr_b)
merge_arrays_by_key(arr_a, arr_b, 'suite', 'count')
end
def merge_arrays_by_key(arr_a, arr_b, label_key, count_key)
merged = ((arr_a || []) + (arr_b || [])).group_by { |item| item[label_key] }
merged
.map { |_label, items| items.first.merge(count_key => items.sum { |i| i[count_key].to_i }) }
.sort_by { |item| -item[count_key].to_i }
.take(10)
end
def parse_response(content)
return nil if content.nil?
JSON.parse(content.strip)
rescue JSON::ParserError => e
Rails.logger.error "[Captain::Llm::ConversationInsightService] JSON parse error: #{e.message}"
nil
end
def empty_payload
{
'top_topics' => [],
'ai_failures' => [],
'faq_gaps' => [],
'sentiment' => { 'positive_count' => 0, 'negative_count' => 0, 'neutral_count' => 0, 'summary' => '' },
'highlights' => { 'praises' => [], 'complaints' => [] },
'most_requested_suites' => [],
'price_reactions' => { 'summary' => '', 'objections_count' => 0 },
'recommendations' => [],
'period_summary' => 'Sem conversas suficientes para análise no período.'
}
end
end