-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_handler.go
More file actions
81 lines (68 loc) · 2.55 KB
/
Copy pathchat_handler.go
File metadata and controls
81 lines (68 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (C) 2025-2026 Jose R F Junior <web2ajax@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import (
"encoding/json"
"eva-mind/internal/cortex/gemini"
"net/http"
"github.com/rs/zerolog/log"
)
// ============================================================================
// geminiSemMemoria — Chat REST Stateless para Malaria-Angolar
// ============================================================================
// Consumer: geminiSemMemoria
// Rota: POST /api/chat
// Client: internal/cortex/gemini → AnalyzeText() (REST v1beta, nao WebSocket)
// Frontend: Malaria-Angolar (qualquer componente)
// Protocolo: REST HTTP — request/response simples, sem sessao, sem streaming
// Ver: GEMINI_ARCHITECTURE.md para documentacao completa
// chatRequest representa o body do POST /api/chat
type chatRequest struct {
CPF string `json:"cpf"`
Message string `json:"message"`
Context string `json:"context,omitempty"` // contexto do sistema (vem do frontend)
}
// chatResponse representa a resposta do POST /api/chat
type chatResponse struct {
Response string `json:"response"`
CPF string `json:"cpf,omitempty"`
}
// handleChat processa mensagens de texto via REST usando Gemini
func (s *SignalingServer) handleChat(w http.ResponseWriter, r *http.Request) {
var req chatRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, `{"error":"JSON invalido"}`, http.StatusBadRequest)
return
}
if req.Message == "" {
http.Error(w, `{"error":"campo message obrigatorio"}`, http.StatusBadRequest)
return
}
// Busca dados do paciente se CPF fornecido
patientContext := ""
if req.CPF != "" {
idoso, err := s.db.GetIdosoByCPF(req.CPF)
if err == nil && idoso != nil {
patientContext = "\n\n[Contexto do paciente: " + idoso.Nome + "]"
}
}
// Contexto vem do frontend. Se nao enviou, usa generico minimo.
systemPrompt := req.Context
if systemPrompt == "" {
systemPrompt = "Voce e a EVA, assistente virtual inteligente. Responda em portugues de forma clara e profissional."
}
systemPrompt += patientContext
fullPrompt := systemPrompt + "\n\nUsuario: " + req.Message
// Chama Gemini via REST client do EVA-Mind
response, err := gemini.AnalyzeText(s.cfg, fullPrompt)
if err != nil {
log.Error().Err(err).Msg("Erro ao chamar Gemini para chat")
http.Error(w, `{"error":"erro ao processar mensagem"}`, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(chatResponse{
Response: response,
CPF: req.CPF,
})
}