iatoolkit 0.3.9__py3-none-any.whl → 0.107.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of iatoolkit might be problematic. Click here for more details.
- iatoolkit/__init__.py +27 -35
- iatoolkit/base_company.py +3 -35
- iatoolkit/cli_commands.py +18 -47
- iatoolkit/common/__init__.py +0 -0
- iatoolkit/common/exceptions.py +48 -0
- iatoolkit/common/interfaces/__init__.py +0 -0
- iatoolkit/common/interfaces/asset_storage.py +34 -0
- iatoolkit/common/interfaces/database_provider.py +39 -0
- iatoolkit/common/model_registry.py +159 -0
- iatoolkit/common/routes.py +138 -0
- iatoolkit/common/session_manager.py +26 -0
- iatoolkit/common/util.py +353 -0
- iatoolkit/company_registry.py +66 -29
- iatoolkit/core.py +514 -0
- iatoolkit/infra/__init__.py +5 -0
- iatoolkit/infra/brevo_mail_app.py +123 -0
- iatoolkit/infra/call_service.py +140 -0
- iatoolkit/infra/connectors/__init__.py +5 -0
- iatoolkit/infra/connectors/file_connector.py +17 -0
- iatoolkit/infra/connectors/file_connector_factory.py +57 -0
- iatoolkit/infra/connectors/google_cloud_storage_connector.py +53 -0
- iatoolkit/infra/connectors/google_drive_connector.py +68 -0
- iatoolkit/infra/connectors/local_file_connector.py +46 -0
- iatoolkit/infra/connectors/s3_connector.py +33 -0
- iatoolkit/infra/google_chat_app.py +57 -0
- iatoolkit/infra/llm_providers/__init__.py +0 -0
- iatoolkit/infra/llm_providers/deepseek_adapter.py +278 -0
- iatoolkit/infra/llm_providers/gemini_adapter.py +350 -0
- iatoolkit/infra/llm_providers/openai_adapter.py +124 -0
- iatoolkit/infra/llm_proxy.py +268 -0
- iatoolkit/infra/llm_response.py +45 -0
- iatoolkit/infra/redis_session_manager.py +122 -0
- iatoolkit/locales/en.yaml +222 -0
- iatoolkit/locales/es.yaml +225 -0
- iatoolkit/repositories/__init__.py +5 -0
- iatoolkit/repositories/database_manager.py +187 -0
- iatoolkit/repositories/document_repo.py +33 -0
- iatoolkit/repositories/filesystem_asset_repository.py +36 -0
- iatoolkit/repositories/llm_query_repo.py +105 -0
- iatoolkit/repositories/models.py +279 -0
- iatoolkit/repositories/profile_repo.py +171 -0
- iatoolkit/repositories/vs_repo.py +150 -0
- iatoolkit/services/__init__.py +5 -0
- iatoolkit/services/auth_service.py +193 -0
- {services → iatoolkit/services}/benchmark_service.py +7 -7
- iatoolkit/services/branding_service.py +153 -0
- iatoolkit/services/company_context_service.py +214 -0
- iatoolkit/services/configuration_service.py +375 -0
- iatoolkit/services/dispatcher_service.py +134 -0
- {services → iatoolkit/services}/document_service.py +20 -8
- iatoolkit/services/embedding_service.py +148 -0
- iatoolkit/services/excel_service.py +156 -0
- {services → iatoolkit/services}/file_processor_service.py +36 -21
- iatoolkit/services/history_manager_service.py +208 -0
- iatoolkit/services/i18n_service.py +104 -0
- iatoolkit/services/jwt_service.py +80 -0
- iatoolkit/services/language_service.py +89 -0
- iatoolkit/services/license_service.py +82 -0
- iatoolkit/services/llm_client_service.py +438 -0
- iatoolkit/services/load_documents_service.py +174 -0
- iatoolkit/services/mail_service.py +213 -0
- {services → iatoolkit/services}/profile_service.py +200 -101
- iatoolkit/services/prompt_service.py +303 -0
- iatoolkit/services/query_service.py +467 -0
- iatoolkit/services/search_service.py +55 -0
- iatoolkit/services/sql_service.py +169 -0
- iatoolkit/services/tool_service.py +246 -0
- iatoolkit/services/user_feedback_service.py +117 -0
- iatoolkit/services/user_session_context_service.py +213 -0
- iatoolkit/static/images/fernando.jpeg +0 -0
- iatoolkit/static/images/iatoolkit_core.png +0 -0
- iatoolkit/static/images/iatoolkit_logo.png +0 -0
- iatoolkit/static/js/chat_feedback_button.js +80 -0
- iatoolkit/static/js/chat_filepond.js +85 -0
- iatoolkit/static/js/chat_help_content.js +124 -0
- iatoolkit/static/js/chat_history_button.js +110 -0
- iatoolkit/static/js/chat_logout_button.js +36 -0
- iatoolkit/static/js/chat_main.js +401 -0
- iatoolkit/static/js/chat_model_selector.js +227 -0
- iatoolkit/static/js/chat_onboarding_button.js +103 -0
- iatoolkit/static/js/chat_prompt_manager.js +94 -0
- iatoolkit/static/js/chat_reload_button.js +38 -0
- iatoolkit/static/styles/chat_iatoolkit.css +559 -0
- iatoolkit/static/styles/chat_modal.css +133 -0
- iatoolkit/static/styles/chat_public.css +135 -0
- iatoolkit/static/styles/documents.css +598 -0
- iatoolkit/static/styles/landing_page.css +398 -0
- iatoolkit/static/styles/llm_output.css +148 -0
- iatoolkit/static/styles/onboarding.css +176 -0
- iatoolkit/system_prompts/__init__.py +0 -0
- iatoolkit/system_prompts/query_main.prompt +30 -23
- iatoolkit/system_prompts/sql_rules.prompt +47 -12
- iatoolkit/templates/_company_header.html +45 -0
- iatoolkit/templates/_login_widget.html +42 -0
- iatoolkit/templates/base.html +78 -0
- iatoolkit/templates/change_password.html +66 -0
- iatoolkit/templates/chat.html +337 -0
- iatoolkit/templates/chat_modals.html +185 -0
- iatoolkit/templates/error.html +51 -0
- iatoolkit/templates/forgot_password.html +51 -0
- iatoolkit/templates/onboarding_shell.html +106 -0
- iatoolkit/templates/signup.html +79 -0
- iatoolkit/views/__init__.py +5 -0
- iatoolkit/views/base_login_view.py +96 -0
- iatoolkit/views/change_password_view.py +116 -0
- iatoolkit/views/chat_view.py +76 -0
- iatoolkit/views/embedding_api_view.py +65 -0
- iatoolkit/views/forgot_password_view.py +75 -0
- iatoolkit/views/help_content_api_view.py +54 -0
- iatoolkit/views/history_api_view.py +56 -0
- iatoolkit/views/home_view.py +63 -0
- iatoolkit/views/init_context_api_view.py +74 -0
- iatoolkit/views/llmquery_api_view.py +59 -0
- iatoolkit/views/load_company_configuration_api_view.py +49 -0
- iatoolkit/views/load_document_api_view.py +65 -0
- iatoolkit/views/login_view.py +170 -0
- iatoolkit/views/logout_api_view.py +57 -0
- iatoolkit/views/profile_api_view.py +46 -0
- iatoolkit/views/prompt_api_view.py +37 -0
- iatoolkit/views/root_redirect_view.py +22 -0
- iatoolkit/views/signup_view.py +100 -0
- iatoolkit/views/static_page_view.py +27 -0
- iatoolkit/views/user_feedback_api_view.py +60 -0
- iatoolkit/views/users_api_view.py +33 -0
- iatoolkit/views/verify_user_view.py +60 -0
- iatoolkit-0.107.4.dist-info/METADATA +268 -0
- iatoolkit-0.107.4.dist-info/RECORD +132 -0
- iatoolkit-0.107.4.dist-info/licenses/LICENSE +21 -0
- iatoolkit-0.107.4.dist-info/licenses/LICENSE_COMMUNITY.md +15 -0
- {iatoolkit-0.3.9.dist-info → iatoolkit-0.107.4.dist-info}/top_level.txt +0 -1
- iatoolkit/iatoolkit.py +0 -413
- iatoolkit/system_prompts/arquitectura.prompt +0 -32
- iatoolkit-0.3.9.dist-info/METADATA +0 -252
- iatoolkit-0.3.9.dist-info/RECORD +0 -32
- services/__init__.py +0 -5
- services/api_service.py +0 -75
- services/dispatcher_service.py +0 -351
- services/excel_service.py +0 -98
- services/history_service.py +0 -45
- services/jwt_service.py +0 -91
- services/load_documents_service.py +0 -212
- services/mail_service.py +0 -62
- services/prompt_manager_service.py +0 -172
- services/query_service.py +0 -334
- services/search_service.py +0 -32
- services/sql_service.py +0 -42
- services/tasks_service.py +0 -188
- services/user_feedback_service.py +0 -67
- services/user_session_context_service.py +0 -85
- {iatoolkit-0.3.9.dist-info → iatoolkit-0.107.4.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from iatoolkit.common.exceptions import IAToolkitException
|
|
7
|
+
from injector import inject
|
|
8
|
+
# call_service.py
|
|
9
|
+
import requests
|
|
10
|
+
from typing import Optional, Dict, Any, Tuple, Union
|
|
11
|
+
from requests import RequestException
|
|
12
|
+
|
|
13
|
+
class CallServiceClient:
|
|
14
|
+
@inject
|
|
15
|
+
def __init__(self):
|
|
16
|
+
self.headers = {'Content-Type': 'application/json'}
|
|
17
|
+
|
|
18
|
+
def _merge_headers(self, extra: Optional[Dict[str, str]]) -> Dict[str, str]:
|
|
19
|
+
if not extra:
|
|
20
|
+
return dict(self.headers)
|
|
21
|
+
merged = dict(self.headers)
|
|
22
|
+
merged.update(extra)
|
|
23
|
+
return merged
|
|
24
|
+
|
|
25
|
+
def _normalize_timeout(self, timeout: Union[int, float, Tuple[int, int], Tuple[float, float]]) -> Tuple[float, float]:
|
|
26
|
+
# Si pasan un solo número → (connect, read) = (10, timeout)
|
|
27
|
+
if isinstance(timeout, (int, float)):
|
|
28
|
+
return (10, float(timeout))
|
|
29
|
+
return (float(timeout[0]), float(timeout[1]))
|
|
30
|
+
|
|
31
|
+
def _deserialize_response(self, response) -> Tuple[Any, int]:
|
|
32
|
+
try:
|
|
33
|
+
return response.json(), response.status_code
|
|
34
|
+
except ValueError:
|
|
35
|
+
# No es JSON → devolver texto
|
|
36
|
+
return response.text, response.status_code
|
|
37
|
+
|
|
38
|
+
def get(
|
|
39
|
+
self,
|
|
40
|
+
endpoint: str,
|
|
41
|
+
params: Optional[Dict[str, Any]] = None,
|
|
42
|
+
headers: Optional[Dict[str, str]] = None,
|
|
43
|
+
timeout: Union[int, float, Tuple[int, int]] = 10
|
|
44
|
+
):
|
|
45
|
+
try:
|
|
46
|
+
response = requests.get(
|
|
47
|
+
endpoint,
|
|
48
|
+
params=params,
|
|
49
|
+
headers=self._merge_headers(headers),
|
|
50
|
+
timeout=self._normalize_timeout(timeout)
|
|
51
|
+
)
|
|
52
|
+
except RequestException as e:
|
|
53
|
+
raise IAToolkitException(IAToolkitException.ErrorType.REQUEST_ERROR, str(e))
|
|
54
|
+
return self._deserialize_response(response)
|
|
55
|
+
|
|
56
|
+
def post(
|
|
57
|
+
self,
|
|
58
|
+
endpoint: str,
|
|
59
|
+
json_dict: Optional[Dict[str, Any]] = None,
|
|
60
|
+
params: Optional[Dict[str, Any]] = None,
|
|
61
|
+
headers: Optional[Dict[str, str]] = None,
|
|
62
|
+
timeout: Union[int, float, Tuple[int, int]] = 10
|
|
63
|
+
):
|
|
64
|
+
try:
|
|
65
|
+
response = requests.post(
|
|
66
|
+
endpoint,
|
|
67
|
+
params=params,
|
|
68
|
+
json=json_dict,
|
|
69
|
+
headers=self._merge_headers(headers),
|
|
70
|
+
timeout=self._normalize_timeout(timeout)
|
|
71
|
+
)
|
|
72
|
+
except RequestException as e:
|
|
73
|
+
raise IAToolkitException(IAToolkitException.ErrorType.REQUEST_ERROR, str(e))
|
|
74
|
+
return self._deserialize_response(response)
|
|
75
|
+
|
|
76
|
+
def put(
|
|
77
|
+
self,
|
|
78
|
+
endpoint: str,
|
|
79
|
+
json_dict: Optional[Dict[str, Any]] = None,
|
|
80
|
+
params: Optional[Dict[str, Any]] = None,
|
|
81
|
+
headers: Optional[Dict[str, str]] = None,
|
|
82
|
+
timeout: Union[int, float, Tuple[int, int]] = 10
|
|
83
|
+
):
|
|
84
|
+
try:
|
|
85
|
+
response = requests.put(
|
|
86
|
+
endpoint,
|
|
87
|
+
params=params,
|
|
88
|
+
json=json_dict,
|
|
89
|
+
headers=self._merge_headers(headers),
|
|
90
|
+
timeout=self._normalize_timeout(timeout)
|
|
91
|
+
)
|
|
92
|
+
except RequestException as e:
|
|
93
|
+
raise IAToolkitException(IAToolkitException.ErrorType.REQUEST_ERROR, str(e))
|
|
94
|
+
return self._deserialize_response(response)
|
|
95
|
+
|
|
96
|
+
def delete(
|
|
97
|
+
self,
|
|
98
|
+
endpoint: str,
|
|
99
|
+
json_dict: Optional[Dict[str, Any]] = None,
|
|
100
|
+
params: Optional[Dict[str, Any]] = None,
|
|
101
|
+
headers: Optional[Dict[str, str]] = None,
|
|
102
|
+
timeout: Union[int, float, Tuple[int, int]] = 10
|
|
103
|
+
):
|
|
104
|
+
try:
|
|
105
|
+
response = requests.delete(
|
|
106
|
+
endpoint,
|
|
107
|
+
params=params,
|
|
108
|
+
json=json_dict,
|
|
109
|
+
headers=self._merge_headers(headers),
|
|
110
|
+
timeout=self._normalize_timeout(timeout)
|
|
111
|
+
)
|
|
112
|
+
except RequestException as e:
|
|
113
|
+
raise IAToolkitException(IAToolkitException.ErrorType.REQUEST_ERROR, str(e))
|
|
114
|
+
return self._deserialize_response(response)
|
|
115
|
+
|
|
116
|
+
def post_files(
|
|
117
|
+
self,
|
|
118
|
+
endpoint: str,
|
|
119
|
+
data: Dict[str, Any],
|
|
120
|
+
params: Optional[Dict[str, Any]] = None,
|
|
121
|
+
headers: Optional[Dict[str, str]] = None,
|
|
122
|
+
timeout: Union[int, float, Tuple[int, int]] = 10
|
|
123
|
+
):
|
|
124
|
+
# Para multipart/form-data no imponemos Content-Type por defecto
|
|
125
|
+
merged_headers = dict(self.headers)
|
|
126
|
+
merged_headers.pop('Content-Type', None)
|
|
127
|
+
if headers:
|
|
128
|
+
merged_headers.update(headers)
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
response = requests.post(
|
|
132
|
+
endpoint,
|
|
133
|
+
params=params,
|
|
134
|
+
files=data,
|
|
135
|
+
headers=merged_headers,
|
|
136
|
+
timeout=self._normalize_timeout(timeout)
|
|
137
|
+
)
|
|
138
|
+
except RequestException as e:
|
|
139
|
+
raise IAToolkitException(IAToolkitException.ErrorType.REQUEST_ERROR, str(e))
|
|
140
|
+
return self._deserialize_response(response)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import List
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FileConnector(ABC):
|
|
11
|
+
@abstractmethod
|
|
12
|
+
def list_files(self) -> List[str]:
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
@abstractmethod
|
|
16
|
+
def get_file_content(self, file_path: str) -> bytes:
|
|
17
|
+
pass
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from iatoolkit.infra.connectors.file_connector import FileConnector
|
|
7
|
+
from iatoolkit.infra.connectors.local_file_connector import LocalFileConnector
|
|
8
|
+
from iatoolkit.infra.connectors.s3_connector import S3Connector
|
|
9
|
+
from iatoolkit.infra.connectors.google_drive_connector import GoogleDriveConnector
|
|
10
|
+
from iatoolkit.infra.connectors.google_cloud_storage_connector import GoogleCloudStorageConnector
|
|
11
|
+
from typing import Dict
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FileConnectorFactory:
|
|
16
|
+
@staticmethod
|
|
17
|
+
def create(config: Dict) -> FileConnector:
|
|
18
|
+
"""
|
|
19
|
+
Configuración esperada:
|
|
20
|
+
{
|
|
21
|
+
"type": "local" | "s3" | "gdrive" | "gcs",
|
|
22
|
+
"path": "/ruta/local", # solo para local
|
|
23
|
+
"bucket": "mi-bucket", "prefix": "datos/", "auth": {...}, # solo para S3
|
|
24
|
+
"folder_id": "xxxxxxx", # solo para Google Drive
|
|
25
|
+
"bucket": "mi-bucket", "service_account": "/ruta/service_account.json" # solo para GCS
|
|
26
|
+
}
|
|
27
|
+
"""
|
|
28
|
+
connector_type = config.get('type')
|
|
29
|
+
|
|
30
|
+
if connector_type == 'local':
|
|
31
|
+
return LocalFileConnector(config['path'])
|
|
32
|
+
|
|
33
|
+
elif connector_type == 's3':
|
|
34
|
+
auth = {
|
|
35
|
+
'aws_access_key_id': os.getenv('AWS_ACCESS_KEY_ID'),
|
|
36
|
+
'aws_secret_access_key': os.getenv('AWS_SECRET_ACCESS_KEY'),
|
|
37
|
+
'region_name': os.getenv('AWS_REGION', 'us-east-1')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return S3Connector(
|
|
41
|
+
bucket=config['bucket'],
|
|
42
|
+
prefix=config.get('prefix', ''),
|
|
43
|
+
auth=auth
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
elif connector_type == 'gdrive':
|
|
47
|
+
return GoogleDriveConnector(config['folder_id'],
|
|
48
|
+
'service_account.json')
|
|
49
|
+
|
|
50
|
+
elif connector_type == 'gcs':
|
|
51
|
+
return GoogleCloudStorageConnector(
|
|
52
|
+
bucket_name=config['bucket'],
|
|
53
|
+
service_account_path=config.get('service_account', 'service_account.json')
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
else:
|
|
57
|
+
raise ValueError(f"Unknown connector type: {connector_type}")
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from iatoolkit.infra.connectors.file_connector import FileConnector
|
|
7
|
+
from google.cloud import storage
|
|
8
|
+
from typing import List
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GoogleCloudStorageConnector(FileConnector):
|
|
12
|
+
def __init__(self, bucket_name: str, service_account_path: str = "service_account.json"):
|
|
13
|
+
"""
|
|
14
|
+
Inicializa el conector de Google Cloud Storage utilizando la API oficial de Google.
|
|
15
|
+
:param bucket_name: Nombre del bucket en Google Cloud Storage.
|
|
16
|
+
:param service_account_path: Ruta al archivo JSON de la cuenta de servicio.
|
|
17
|
+
"""
|
|
18
|
+
self.bucket_name = bucket_name
|
|
19
|
+
self.service_account_path = service_account_path
|
|
20
|
+
self.storage_client = self._authenticate()
|
|
21
|
+
self.bucket = self.storage_client.bucket(bucket_name)
|
|
22
|
+
|
|
23
|
+
def _authenticate(self):
|
|
24
|
+
"""
|
|
25
|
+
Autentica en Google Cloud Storage utilizando una cuenta de servicio.
|
|
26
|
+
"""
|
|
27
|
+
# Crear cliente de GCS con las credenciales
|
|
28
|
+
client = storage.Client.from_service_account_json(self.service_account_path)
|
|
29
|
+
return client
|
|
30
|
+
|
|
31
|
+
def list_files(self) -> List[dict]:
|
|
32
|
+
"""
|
|
33
|
+
Lista todos los archivos en el bucket de GCS como diccionarios con claves 'path', 'name' y 'metadata'.
|
|
34
|
+
"""
|
|
35
|
+
blobs = self.bucket.list_blobs()
|
|
36
|
+
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
"path": blob.name, # Nombre o "ruta" del blob en el bucket
|
|
40
|
+
"name": blob.name.split("/")[-1], # Nombre del archivo (última parte del path)
|
|
41
|
+
"metadata": {"size": blob.size} # Incluye tamaño como metadata (u otros metadatos relevantes)
|
|
42
|
+
}
|
|
43
|
+
for blob in blobs
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
def get_file_content(self, file_path: str) -> bytes:
|
|
47
|
+
"""
|
|
48
|
+
Descarga el contenido de un archivo en GCS dado su path (nombre del blob).
|
|
49
|
+
"""
|
|
50
|
+
blob = self.bucket.blob(file_path)
|
|
51
|
+
file_buffer = blob.download_as_bytes() # Descarga el contenido como bytes
|
|
52
|
+
|
|
53
|
+
return file_buffer
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from iatoolkit.infra.connectors.file_connector import FileConnector
|
|
7
|
+
from googleapiclient.discovery import build
|
|
8
|
+
from googleapiclient.http import MediaIoBaseDownload
|
|
9
|
+
from google.oauth2.service_account import Credentials
|
|
10
|
+
import io
|
|
11
|
+
from typing import List
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class GoogleDriveConnector(FileConnector):
|
|
15
|
+
def __init__(self, folder_id: str, service_account_path: str = "service_account.json"):
|
|
16
|
+
"""
|
|
17
|
+
Inicializa el conector de Google Drive utilizando la API oficial de Google.
|
|
18
|
+
:param folder_id: ID de la carpeta en Google Drive.
|
|
19
|
+
:param service_account_path: Ruta al archivo JSON de la cuenta de servicio.
|
|
20
|
+
"""
|
|
21
|
+
self.folder_id = folder_id
|
|
22
|
+
self.service_account_path = service_account_path
|
|
23
|
+
self.drive_service = self._authenticate()
|
|
24
|
+
|
|
25
|
+
def _authenticate(self):
|
|
26
|
+
"""
|
|
27
|
+
Autentica en Google Drive utilizando una cuenta de servicio.
|
|
28
|
+
"""
|
|
29
|
+
# Cargar credenciales desde el archivo de servicio
|
|
30
|
+
credentials = Credentials.from_service_account_file(
|
|
31
|
+
self.service_account_path,
|
|
32
|
+
scopes=["https://www.googleapis.com/auth/drive"]
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Crear el cliente de Google Drive API
|
|
36
|
+
service = build('drive', 'v3', credentials=credentials)
|
|
37
|
+
return service
|
|
38
|
+
|
|
39
|
+
def list_files(self) -> List[dict]:
|
|
40
|
+
"""
|
|
41
|
+
Estándar: Lista todos los archivos como diccionarios con claves 'path', 'name' y 'metadata'.
|
|
42
|
+
"""
|
|
43
|
+
query = f"'{self.folder_id}' in parents and trashed=false"
|
|
44
|
+
results = self.drive_service.files().list(q=query, fields="files(id, name)").execute()
|
|
45
|
+
files = results.get('files', [])
|
|
46
|
+
|
|
47
|
+
return [
|
|
48
|
+
{
|
|
49
|
+
"path": file['id'], # ID único del archivo en Google Drive
|
|
50
|
+
"name": file['name'], # Nombre del archivo en Google Drive
|
|
51
|
+
"metadata": {} # No hay metadatos adicionales en este caso
|
|
52
|
+
}
|
|
53
|
+
for file in files
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
def get_file_content(self, file_path: str) -> bytes:
|
|
57
|
+
"""
|
|
58
|
+
Obtiene el contenido de un archivo en Google Drive utilizando su ID (file_path).
|
|
59
|
+
"""
|
|
60
|
+
request = self.drive_service.files().get_media(fileId=file_path)
|
|
61
|
+
file_buffer = io.BytesIO()
|
|
62
|
+
downloader = MediaIoBaseDownload(file_buffer, request)
|
|
63
|
+
|
|
64
|
+
done = False
|
|
65
|
+
while not done:
|
|
66
|
+
status, done = downloader.next_chunk()
|
|
67
|
+
|
|
68
|
+
return file_buffer.getvalue()
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
from iatoolkit.infra.connectors.file_connector import FileConnector
|
|
8
|
+
from typing import List
|
|
9
|
+
from iatoolkit.common.exceptions import IAToolkitException
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LocalFileConnector(FileConnector):
|
|
13
|
+
def __init__(self, directory: str):
|
|
14
|
+
local_root = os.getenv("ROOT_DIR_LOCAL_FILES", '')
|
|
15
|
+
self.directory = os.path.join(local_root, directory)
|
|
16
|
+
|
|
17
|
+
def list_files(self) -> List[dict]:
|
|
18
|
+
"""
|
|
19
|
+
Estándar: Lista todos los archivos como diccionarios con claves 'path', 'name' y 'metadata'.
|
|
20
|
+
"""
|
|
21
|
+
try:
|
|
22
|
+
files = [
|
|
23
|
+
os.path.join(self.directory, f)
|
|
24
|
+
for f in os.listdir(self.directory)
|
|
25
|
+
if os.path.isfile(os.path.join(self.directory, f))
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
"path": file, # Ruta completa al archivo local
|
|
31
|
+
"name": os.path.basename(file), # Nombre del archivo
|
|
32
|
+
"metadata": {"size": os.path.getsize(file), "last_modified": os.path.getmtime(file)}
|
|
33
|
+
}
|
|
34
|
+
for file in files
|
|
35
|
+
]
|
|
36
|
+
except Exception as e:
|
|
37
|
+
raise IAToolkitException(IAToolkitException.ErrorType.FILE_IO_ERROR,
|
|
38
|
+
f"Error procesando el directorio {self.directory}: {e}")
|
|
39
|
+
|
|
40
|
+
def get_file_content(self, file_path: str) -> bytes:
|
|
41
|
+
try:
|
|
42
|
+
with open(file_path, 'rb') as f:
|
|
43
|
+
return f.read()
|
|
44
|
+
except Exception as e:
|
|
45
|
+
raise IAToolkitException(IAToolkitException.ErrorType.FILE_IO_ERROR,
|
|
46
|
+
f"Error leyendo el archivo {file_path}: {e}")
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
import boto3
|
|
7
|
+
from iatoolkit.infra.connectors.file_connector import FileConnector
|
|
8
|
+
from typing import List
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class S3Connector(FileConnector):
|
|
12
|
+
def __init__(self, bucket: str, prefix: str, auth: dict):
|
|
13
|
+
self.bucket = bucket
|
|
14
|
+
self.prefix = prefix
|
|
15
|
+
self.s3 = boto3.client('s3', **auth)
|
|
16
|
+
|
|
17
|
+
def list_files(self) -> List[dict]:
|
|
18
|
+
# list all the files as dictionaries, with keys: 'path', 'name' y 'metadata'.
|
|
19
|
+
response = self.s3.list_objects_v2(Bucket=self.bucket, Prefix=self.prefix)
|
|
20
|
+
files = response.get('Contents', [])
|
|
21
|
+
|
|
22
|
+
return [
|
|
23
|
+
{
|
|
24
|
+
"path": obj['Key'], # s3 key
|
|
25
|
+
"name": obj['Key'].split('/')[-1], # filename
|
|
26
|
+
"metadata": {"size": obj.get('Size'), "last_modified": obj.get('LastModified')}
|
|
27
|
+
}
|
|
28
|
+
for obj in files
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
def get_file_content(self, file_path: str) -> bytes:
|
|
32
|
+
response = self.s3.get_object(Bucket=self.bucket, Key=file_path)
|
|
33
|
+
return response['Body'].read()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2024 Fernando Libedinsky
|
|
2
|
+
# Product: IAToolkit
|
|
3
|
+
#
|
|
4
|
+
# IAToolkit is open source software.
|
|
5
|
+
|
|
6
|
+
from injector import inject
|
|
7
|
+
from iatoolkit.infra.call_service import CallServiceClient
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
from typing import Dict, Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class GoogleChatApp:
|
|
14
|
+
@inject
|
|
15
|
+
def __init__(self, call_service: CallServiceClient):
|
|
16
|
+
self.call_service = call_service
|
|
17
|
+
|
|
18
|
+
def send_message(self, message_data: Dict[str, Any]) -> Dict[str, Any]:
|
|
19
|
+
"""
|
|
20
|
+
Sends a message to Google Chat.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
message_data: Complete message data structure with type, space, and message
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
Dict with the service response
|
|
27
|
+
"""
|
|
28
|
+
try:
|
|
29
|
+
# get the bot URL from environment variables
|
|
30
|
+
bot_url = os.getenv('GOOGLE_CHAT_BOT_URL')
|
|
31
|
+
if not bot_url:
|
|
32
|
+
raise Exception('GOOGLE_CHAT_BOT_URL no está configurada en las variables de entorno')
|
|
33
|
+
|
|
34
|
+
# send the POST request with the complete message data
|
|
35
|
+
response, status_code = self.call_service.post(bot_url, message_data)
|
|
36
|
+
|
|
37
|
+
if status_code == 200:
|
|
38
|
+
return {
|
|
39
|
+
"success": True,
|
|
40
|
+
"message": "Mensaje enviado correctamente",
|
|
41
|
+
"response": response
|
|
42
|
+
}
|
|
43
|
+
else:
|
|
44
|
+
logging.error(f"Error al enviar mensaje a Google Chat. Status: {status_code}, Response: {response}")
|
|
45
|
+
return {
|
|
46
|
+
"success": False,
|
|
47
|
+
"message": f"Error al enviar mensaje. Status: {status_code}",
|
|
48
|
+
"response": response
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
except Exception as e:
|
|
52
|
+
logging.exception(f"Error inesperado al enviar mensaje a Google Chat: {e}")
|
|
53
|
+
return {
|
|
54
|
+
"success": False,
|
|
55
|
+
"message": f"Error interno del servidor: {str(e)}",
|
|
56
|
+
"response": None
|
|
57
|
+
}
|
|
File without changes
|