package controllers import ( "fmt" "github.com/gofiber/fiber/v2" "github.com/sujit-baniya/fiber-boilerplate/pkg/models" "github.com/sujit-baniya/fiber-boilerplate/pkg/services" ) // AsistenteView renderiza el chat propio dentro del dashboard: mismo motor y // mismas tools que el bot de Telegram (services.ProcessAgentMessage), solo que // accesible directamente desde la web sin pasar por Telegram. func AsistenteView(c *fiber.Ctx) error { return c.Render("automatizacion/asistente", fiber.Map{ "user": c.Locals("user"), "modules": c.Locals("userModules"), }, "layouts/main") } type asistenteChatRequest struct { Mensaje string `json:"mensaje"` } // asistenteChatID deriva un chat_id sintético y estable por usuario del dashboard, // en un rango que nunca colisiona con IDs reales de chats de Telegram (positivos // < 10^10 para chats privados, negativos para grupos/canales). func asistenteChatID(c *fiber.Ctx) int64 { user, _ := c.Locals("user").(map[string]interface{}) var userID uint if v, ok := user["ID"]; ok { switch x := v.(type) { case uint: userID = x case int: userID = uint(x) case float64: userID = uint(x) } } return 9_000_000_000 + int64(userID) } // PostAsistenteChat procesa un mensaje del chat propio del dashboard. // POST /app/api/asistente/chat func PostAsistenteChat(c *fiber.Ctx) error { var req asistenteChatRequest if err := c.BodyParser(&req); err != nil { return c.Status(400).JSON(fiber.Map{"error": err.Error()}) } if req.Mensaje == "" { return c.Status(400).JSON(fiber.Map{"error": "mensaje requerido"}) } ai, err := models.GetAgenteBotAiConfig() if err != nil { return c.Status(503).JSON(fiber.Map{"error": fmt.Sprintf("Asistente no configurado: %v", err)}) } respuesta, err := services.ProcessAgentMessage(asistenteChatID(c), req.Mensaje, ai) if err != nil { return c.Status(500).JSON(fiber.Map{"error": err.Error()}) } return c.JSON(fiber.Map{"ok": true, "respuesta": respuesta}) } // DeleteAsistenteHistorial borra el historial de la conversación del usuario actual. // DELETE /app/api/asistente/historial func DeleteAsistenteHistorial(c *fiber.Ctx) error { if err := models.ClearAgentHistory(asistenteChatID(c)); err != nil { return c.Status(500).JSON(fiber.Map{"error": err.Error()}) } return c.JSON(fiber.Map{"ok": true}) }