letschatty 0.4.333__py3-none-any.whl → 0.4.334__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.
- letschatty/models/ai_microservices/__init__.py +3 -3
- letschatty/models/ai_microservices/expected_output.py +2 -29
- letschatty/models/ai_microservices/lambda_events.py +28 -137
- letschatty/models/ai_microservices/lambda_invokation_types.py +1 -3
- letschatty/models/ai_microservices/n8n_ai_agents_payload.py +1 -3
- letschatty/models/analytics/events/__init__.py +2 -3
- letschatty/models/analytics/events/chat_based_events/chat_funnel.py +69 -13
- letschatty/models/analytics/events/company_based_events/asset_events.py +9 -2
- letschatty/models/analytics/events/event_type_to_classes.py +6 -3
- letschatty/models/analytics/events/event_types.py +9 -50
- letschatty/models/chat/chat.py +2 -0
- letschatty/models/chat/chat_with_assets.py +6 -1
- letschatty/models/chat/client.py +0 -2
- letschatty/models/chat/continuous_conversation.py +1 -1
- letschatty/models/company/CRM/funnel.py +365 -33
- letschatty/models/company/__init__.py +2 -1
- letschatty/models/company/assets/ai_agents_v2/ai_agents_decision_output.py +1 -1
- letschatty/models/company/assets/ai_agents_v2/chatty_ai_agent_in_chat.py +1 -4
- letschatty/models/company/assets/ai_agents_v2/chatty_ai_mode.py +2 -2
- letschatty/models/company/assets/ai_agents_v2/get_chat_with_prompt_response.py +0 -1
- letschatty/models/company/assets/ai_agents_v2/pre_qualify_config.py +2 -14
- letschatty/models/company/assets/automation.py +10 -19
- letschatty/models/company/assets/company_assets.py +2 -0
- letschatty/models/data_base/collection_interface.py +29 -101
- letschatty/models/data_base/mongo_connection.py +9 -92
- letschatty/models/messages/chatty_messages/schema/chatty_content/content_document.py +4 -2
- letschatty/models/messages/chatty_messages/schema/chatty_content/content_media.py +4 -3
- letschatty/models/utils/custom_exceptions/custom_exceptions.py +1 -14
- letschatty/services/ai_agents/smart_follow_up_context_builder_v2.py +2 -5
- letschatty/services/chatty_assets/__init__.py +0 -12
- letschatty/services/chatty_assets/asset_service.py +13 -190
- letschatty/services/chatty_assets/base_container.py +2 -3
- letschatty/services/chatty_assets/base_container_with_collection.py +26 -35
- letschatty/services/continuous_conversation_service/continuous_conversation_helper.py +0 -11
- letschatty/services/events/events_manager.py +1 -218
- letschatty/services/factories/analytics/events_factory.py +6 -66
- letschatty/services/factories/lambda_ai_orchestrartor/lambda_events_factory.py +8 -25
- letschatty/services/messages_helpers/get_caption_or_body_or_preview.py +4 -6
- {letschatty-0.4.333.dist-info → letschatty-0.4.334.dist-info}/METADATA +1 -1
- {letschatty-0.4.333.dist-info → letschatty-0.4.334.dist-info}/RECORD +42 -78
- letschatty/models/analytics/events/chat_based_events/ai_agent_execution_event.py +0 -71
- letschatty/services/chatty_assets/assets_collections.py +0 -137
- letschatty/services/chatty_assets/collections/__init__.py +0 -38
- letschatty/services/chatty_assets/collections/ai_agent_collection.py +0 -19
- letschatty/services/chatty_assets/collections/ai_agent_in_chat_collection.py +0 -32
- letschatty/services/chatty_assets/collections/ai_component_collection.py +0 -21
- letschatty/services/chatty_assets/collections/chain_of_thought_collection.py +0 -30
- letschatty/services/chatty_assets/collections/chat_collection.py +0 -21
- letschatty/services/chatty_assets/collections/contact_point_collection.py +0 -21
- letschatty/services/chatty_assets/collections/fast_answer_collection.py +0 -21
- letschatty/services/chatty_assets/collections/filter_criteria_collection.py +0 -18
- letschatty/services/chatty_assets/collections/flow_collection.py +0 -20
- letschatty/services/chatty_assets/collections/product_collection.py +0 -20
- letschatty/services/chatty_assets/collections/sale_collection.py +0 -20
- letschatty/services/chatty_assets/collections/source_collection.py +0 -21
- letschatty/services/chatty_assets/collections/tag_collection.py +0 -19
- letschatty/services/chatty_assets/collections/topic_collection.py +0 -21
- letschatty/services/chatty_assets/collections/user_collection.py +0 -20
- letschatty/services/chatty_assets/example_usage.py +0 -44
- letschatty/services/chatty_assets/services/__init__.py +0 -37
- letschatty/services/chatty_assets/services/ai_agent_in_chat_service.py +0 -73
- letschatty/services/chatty_assets/services/ai_agent_service.py +0 -23
- letschatty/services/chatty_assets/services/chain_of_thought_service.py +0 -70
- letschatty/services/chatty_assets/services/chat_service.py +0 -25
- letschatty/services/chatty_assets/services/contact_point_service.py +0 -29
- letschatty/services/chatty_assets/services/fast_answer_service.py +0 -32
- letschatty/services/chatty_assets/services/filter_criteria_service.py +0 -30
- letschatty/services/chatty_assets/services/flow_service.py +0 -25
- letschatty/services/chatty_assets/services/product_service.py +0 -30
- letschatty/services/chatty_assets/services/sale_service.py +0 -25
- letschatty/services/chatty_assets/services/source_service.py +0 -28
- letschatty/services/chatty_assets/services/tag_service.py +0 -32
- letschatty/services/chatty_assets/services/topic_service.py +0 -31
- letschatty/services/chatty_assets/services/user_service.py +0 -32
- letschatty/services/events/__init__.py +0 -6
- letschatty/services/factories/analytics/ai_agent_event_factory.py +0 -161
- {letschatty-0.4.333.dist-info → letschatty-0.4.334.dist-info}/LICENSE +0 -0
- {letschatty-0.4.333.dist-info → letschatty-0.4.334.dist-info}/WHEEL +0 -0
|
@@ -4,7 +4,6 @@ from typing import TYPE_CHECKING, Dict, List, Generic, TypeVar, Type, Optional,
|
|
|
4
4
|
from bson.objectid import ObjectId
|
|
5
5
|
from pymongo.collection import Collection
|
|
6
6
|
from pymongo.database import Database
|
|
7
|
-
from motor.motor_asyncio import AsyncIOMotorDatabase, AsyncIOMotorCollection
|
|
8
7
|
|
|
9
8
|
from letschatty.models.chat.chat import Chat
|
|
10
9
|
from ...models.base_models.chatty_asset_model import ChattyAssetModel, CompanyAssetModel, ChattyAssetPreview
|
|
@@ -27,116 +26,58 @@ P = TypeVar('P', bound=ChattyAssetPreview)
|
|
|
27
26
|
class ChattyAssetCollectionInterface(Generic[T, P], ABC):
|
|
28
27
|
def __init__(self, database: str, collection: str, connection: MongoConnection, type: Type[T], preview_type: Optional[Type[P]] = None):
|
|
29
28
|
logger.info(f"Initializing collection {collection} in database {database}")
|
|
30
|
-
# Sync database and collection (existing)
|
|
31
29
|
self.db: Database = connection.client[database]
|
|
32
30
|
self.collection: Collection = connection.client[database][collection]
|
|
33
|
-
|
|
34
|
-
# NEW: Async database and collection
|
|
35
|
-
# Store connection reference to ensure we use current event loop
|
|
36
|
-
self._connection = connection
|
|
37
|
-
self._database_name = database
|
|
38
|
-
self._collection_name = collection
|
|
39
|
-
self._async_db: Optional[AsyncIOMotorDatabase] = None
|
|
40
|
-
self._async_collection: Optional[AsyncIOMotorCollection] = None
|
|
41
|
-
|
|
42
31
|
self.type = type
|
|
43
32
|
self.preview_type = preview_type
|
|
44
|
-
|
|
45
|
-
@property
|
|
46
|
-
def async_db(self) -> AsyncIOMotorDatabase:
|
|
47
|
-
"""Get async database, ensuring it uses the current event loop"""
|
|
48
|
-
# Always ensure connection's async client is using current loop (for Lambda compatibility)
|
|
49
|
-
self._connection._ensure_async_client_loop()
|
|
50
|
-
# Recreate database reference to ensure it uses the current client
|
|
51
|
-
self._async_db = self._connection.async_client[self._database_name]
|
|
52
|
-
return self._async_db
|
|
53
|
-
|
|
54
|
-
@property
|
|
55
|
-
def async_collection(self) -> AsyncIOMotorCollection:
|
|
56
|
-
"""Get async collection, ensuring it uses the current event loop"""
|
|
57
|
-
# Always ensure connection's async client is using current loop (for Lambda compatibility)
|
|
58
|
-
self._connection._ensure_async_client_loop()
|
|
59
|
-
# Recreate collection reference to ensure it uses the current client
|
|
60
|
-
self._async_collection = self._connection.async_client[self._database_name][self._collection_name]
|
|
61
|
-
return self._async_collection
|
|
62
33
|
@abstractmethod
|
|
63
34
|
def create_instance(self, data: dict) -> T:
|
|
64
35
|
"""Factory method to create instance from data"""
|
|
65
36
|
pass
|
|
66
37
|
|
|
67
|
-
|
|
68
|
-
async def insert(self, asset: T) -> StrObjectId:
|
|
69
|
-
"""Async insert operation"""
|
|
38
|
+
def insert(self, asset: T) -> StrObjectId:
|
|
70
39
|
if not isinstance(asset, self.type):
|
|
71
40
|
raise ValueError(f"Asset must be of type {self.type.__name__}")
|
|
72
41
|
document = asset.model_dump_json(serializer=SerializerType.DATABASE)
|
|
73
42
|
logger.debug(f"Inserting document: {document}")
|
|
74
|
-
result =
|
|
43
|
+
result = self.collection.insert_one(document)
|
|
75
44
|
if not result.inserted_id:
|
|
76
45
|
raise Exception("Failed to insert document")
|
|
77
46
|
logger.debug(f"Inserted document with id {result.inserted_id}")
|
|
78
47
|
return result.inserted_id
|
|
79
48
|
|
|
80
|
-
|
|
81
|
-
"""Async update operation"""
|
|
49
|
+
def update(self, asset: T) -> StrObjectId:
|
|
82
50
|
logger.debug(f"Updating document with id {asset.id}")
|
|
83
51
|
if not isinstance(asset, self.type):
|
|
84
52
|
raise ValueError(f"Asset must be of type {self.type.__name__}")
|
|
85
53
|
asset.update_now()
|
|
86
54
|
document = asset.model_dump_json(serializer=SerializerType.DATABASE)
|
|
87
|
-
document.pop('_id', None)
|
|
88
|
-
result =
|
|
89
|
-
{"_id": ObjectId(asset.id)},
|
|
90
|
-
{"$set": document}
|
|
91
|
-
)
|
|
55
|
+
document.pop('_id', None) # Still needed
|
|
56
|
+
result = self.collection.update_one({"_id": ObjectId(asset.id)}, {"$set": document})
|
|
92
57
|
if result.matched_count == 0:
|
|
93
58
|
raise NotFoundError(f"No document found with id {asset.id}")
|
|
94
59
|
if result.modified_count == 0:
|
|
95
60
|
logger.debug(f"No changes were made to the document with id {asset.id} probably because the values were the same")
|
|
96
61
|
return asset.id
|
|
97
62
|
|
|
98
|
-
|
|
99
|
-
"
|
|
100
|
-
|
|
101
|
-
|
|
63
|
+
def get_by_id(self, doc_id: str) -> T:
|
|
64
|
+
logger.debug(f"Getting document with id {doc_id} from collection {self.collection.name} and db {self.db.name}")
|
|
65
|
+
doc = self.collection.find_one({"_id": ObjectId(doc_id)})
|
|
66
|
+
|
|
102
67
|
if doc:
|
|
103
68
|
return self.create_instance(doc)
|
|
104
69
|
else:
|
|
105
|
-
raise NotFoundError(f"No document found with id {doc_id} in collection")
|
|
70
|
+
raise NotFoundError(f"No document found with id {doc_id} in db collection {self.collection.name} and db {self.db.name}")
|
|
106
71
|
|
|
107
|
-
|
|
108
|
-
"
|
|
109
|
-
logger.debug(f"Getting documents from collection with company_id {company_id} and query {query}")
|
|
72
|
+
def get_docs(self, company_id:Optional[StrObjectId], query = {}, limit = 0) -> List[T]:
|
|
73
|
+
logger.debug(f"Getting documents from collection {self.collection.name} with company_id {company_id} and query {query}")
|
|
110
74
|
if company_id:
|
|
111
|
-
query = query.copy()
|
|
75
|
+
query = query.copy() # Create a copy to avoid modifying the original
|
|
112
76
|
query["company_id"] = company_id
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
cursor = cursor.limit(limit)
|
|
116
|
-
docs = await cursor.to_list(length=limit if limit > 0 else None)
|
|
117
|
-
logger.debug(f"Found {len(docs)} documents")
|
|
77
|
+
docs = list(self.collection.find(filter=query).limit(limit))
|
|
78
|
+
logger.debug(f"Found {len(docs)} documents in collection {self.collection.name}")
|
|
118
79
|
return [self.create_instance(doc) for doc in docs]
|
|
119
80
|
|
|
120
|
-
async def delete(self, doc_id: str, deletion_type: DeletionType = DeletionType.LOGICAL) -> StrObjectId:
|
|
121
|
-
"""Delete operation"""
|
|
122
|
-
logger.debug(f"Deleting document with id {doc_id} - deletion type: {deletion_type}")
|
|
123
|
-
if deletion_type == DeletionType.LOGICAL:
|
|
124
|
-
result = await self.async_collection.update_one(
|
|
125
|
-
{"_id": ObjectId(doc_id)},
|
|
126
|
-
{"$set": {"deleted_at": datetime.now(ZoneInfo("UTC")), "updated_at": datetime.now(ZoneInfo("UTC"))}}
|
|
127
|
-
)
|
|
128
|
-
if result.modified_count == 0:
|
|
129
|
-
raise NotFoundError(f"No document found with id {doc_id}")
|
|
130
|
-
return doc_id
|
|
131
|
-
elif deletion_type == DeletionType.PHYSICAL:
|
|
132
|
-
result = await self.async_collection.delete_one({"_id": ObjectId(doc_id)})
|
|
133
|
-
if result.deleted_count == 0:
|
|
134
|
-
raise NotFoundError(f"No document found with id {doc_id}")
|
|
135
|
-
return doc_id
|
|
136
|
-
else:
|
|
137
|
-
raise ValueError(f"Invalid deletion type: {deletion_type}")
|
|
138
|
-
|
|
139
|
-
# Additional methods - keeping these sync as they're less critical
|
|
140
81
|
def get_preview_docs(self, projection = {}, all=True) -> List[P]:
|
|
141
82
|
"""We get the previews of all the documents in the collection for all companies"""
|
|
142
83
|
if not self.preview_type:
|
|
@@ -157,31 +98,18 @@ class ChattyAssetCollectionInterface(Generic[T, P], ABC):
|
|
|
157
98
|
docs = self.collection.find(query)
|
|
158
99
|
return [self.create_instance(doc) for doc in docs]
|
|
159
100
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
object_ids = [ObjectId(id) for id in ids]
|
|
175
|
-
|
|
176
|
-
# Query for all filter criteria with matching IDs
|
|
177
|
-
query = {
|
|
178
|
-
"_id": {"$in": object_ids},
|
|
179
|
-
"deleted_at": None
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
# Use the sync collection directly (inherited from ChattyAssetCollectionInterface)
|
|
183
|
-
docs = await self.async_collection.find(query).to_list(length=None)
|
|
184
|
-
|
|
185
|
-
# Create FilterCriteria instances
|
|
186
|
-
return [self.create_instance(doc) for doc in docs]
|
|
101
|
+
def delete(self, doc_id: str, deletion_type : DeletionType = DeletionType.LOGICAL) -> StrObjectId:
|
|
102
|
+
logger.debug(f"Deleting document with id {doc_id} - deletion type: {deletion_type}")
|
|
103
|
+
if deletion_type == DeletionType.LOGICAL:
|
|
104
|
+
result = self.collection.update_one({"_id": ObjectId(doc_id)}, {"$set": {"deleted_at": datetime.now(ZoneInfo("UTC")), "updated_at": datetime.now(ZoneInfo("UTC"))}})
|
|
105
|
+
if result.modified_count == 0:
|
|
106
|
+
raise NotFoundError(f"No document found with id {doc_id}")
|
|
107
|
+
return doc_id
|
|
108
|
+
elif deletion_type == DeletionType.PHYSICAL:
|
|
109
|
+
result = self.collection.delete_one({"_id": ObjectId(doc_id)})
|
|
110
|
+
if result.deleted_count == 0:
|
|
111
|
+
raise NotFoundError(f"No document found with id {doc_id}")
|
|
112
|
+
return doc_id
|
|
113
|
+
else:
|
|
114
|
+
raise ValueError(f"Invalid deletion type: {deletion_type}")
|
|
187
115
|
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
from ..base_models.singleton import SingletonMeta
|
|
2
2
|
from pymongo import MongoClient
|
|
3
|
-
from motor.motor_asyncio import AsyncIOMotorClient
|
|
4
3
|
from typing import Optional
|
|
5
|
-
from urllib.parse import quote_plus
|
|
6
4
|
import os
|
|
7
5
|
import atexit
|
|
8
|
-
import asyncio
|
|
9
|
-
import logging
|
|
10
|
-
|
|
11
|
-
logger = logging.getLogger(__name__)
|
|
12
6
|
|
|
13
7
|
class MongoConnection(metaclass=SingletonMeta):
|
|
14
8
|
def __init__(
|
|
@@ -16,103 +10,26 @@ class MongoConnection(metaclass=SingletonMeta):
|
|
|
16
10
|
username: Optional[str] = None,
|
|
17
11
|
password: Optional[str] = None,
|
|
18
12
|
uri_base: Optional[str] = None,
|
|
19
|
-
instance: Optional[str] = None
|
|
20
|
-
verify_on_init: bool = True
|
|
13
|
+
instance: Optional[str] = None
|
|
21
14
|
):
|
|
22
15
|
self.username = username or os.getenv('MONGO_USERNAME')
|
|
23
16
|
self.password = password or os.getenv('MONGO_PASSWORD')
|
|
24
17
|
self.uri_base = uri_base or os.getenv('MONGO_URI_BASE')
|
|
25
18
|
self.instance = instance or os.getenv('MONGO_INSTANCE_COMPONENT')
|
|
26
|
-
|
|
19
|
+
|
|
27
20
|
if not all([self.username, self.password, self.uri_base, self.instance]):
|
|
28
21
|
raise ValueError("Missing required MongoDB connection parameters")
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
encoded_username = quote_plus(self.username)
|
|
32
|
-
encoded_password = quote_plus(self.password)
|
|
33
|
-
|
|
34
|
-
uri = f"{self.uri_base}://{encoded_username}:{encoded_password}@{self.instance}.mongodb.net"
|
|
35
|
-
|
|
36
|
-
# Sync client (existing)
|
|
22
|
+
|
|
23
|
+
uri = f"{self.uri_base}://{self.username}:{self.password}@{self.instance}.mongodb.net"
|
|
37
24
|
self.client = MongoClient(uri)
|
|
38
|
-
|
|
39
|
-
# NEW: Async client for async operations
|
|
40
|
-
# Don't pass io_loop - Motor will automatically use the current event loop
|
|
41
|
-
# This is important for Lambda where the event loop changes between invocations
|
|
42
|
-
self.async_client = AsyncIOMotorClient(uri)
|
|
43
|
-
|
|
44
|
-
# Verify connections if requested
|
|
45
|
-
if verify_on_init:
|
|
46
|
-
try:
|
|
47
|
-
# Try to get running loop
|
|
48
|
-
loop = asyncio.get_running_loop()
|
|
49
|
-
# If we get here, there's a running loop
|
|
50
|
-
logger.warning(
|
|
51
|
-
"Event loop is already running. Skipping connection verification in __init__. "
|
|
52
|
-
"Call verify_connection_async() from async context to verify connection."
|
|
53
|
-
)
|
|
54
|
-
self._connection_verified = False
|
|
55
|
-
except RuntimeError:
|
|
56
|
-
# No running loop, safe to use run_until_complete
|
|
57
|
-
try:
|
|
58
|
-
# Test sync client
|
|
59
|
-
self.client.admin.command('ping')
|
|
60
|
-
|
|
61
|
-
# Test async client in sync context
|
|
62
|
-
loop = asyncio.new_event_loop()
|
|
63
|
-
asyncio.set_event_loop(loop)
|
|
64
|
-
loop.run_until_complete(self.async_client.admin.command('ping'))
|
|
65
|
-
self._connection_verified = True
|
|
66
|
-
loop.close()
|
|
67
|
-
except Exception as e:
|
|
68
|
-
self.client.close()
|
|
69
|
-
self.async_client.close()
|
|
70
|
-
raise ConnectionError(f"Failed to connect to MongoDB: {str(e)}")
|
|
71
|
-
else:
|
|
72
|
-
self._connection_verified = False
|
|
73
|
-
|
|
74
|
-
atexit.register(self.close)
|
|
75
|
-
|
|
76
|
-
def _ensure_async_client_loop(self):
|
|
77
|
-
"""Ensure async client is using the current event loop (for Lambda compatibility)"""
|
|
78
25
|
try:
|
|
79
|
-
|
|
80
|
-
# Check if client's loop is closed or different
|
|
81
|
-
client_loop = getattr(self.async_client, '_io_loop', None)
|
|
82
|
-
if client_loop is not None:
|
|
83
|
-
try:
|
|
84
|
-
# Try to check if the loop is closed
|
|
85
|
-
if client_loop.is_closed():
|
|
86
|
-
# Recreate client with current loop
|
|
87
|
-
logger.warning("Async client's event loop is closed, recreating client")
|
|
88
|
-
old_client = self.async_client
|
|
89
|
-
uri = f"{self.uri_base}://{quote_plus(self.username)}:{quote_plus(self.password)}@{self.instance}.mongodb.net"
|
|
90
|
-
self.async_client = AsyncIOMotorClient(uri)
|
|
91
|
-
try:
|
|
92
|
-
old_client.close()
|
|
93
|
-
except:
|
|
94
|
-
pass
|
|
95
|
-
except AttributeError:
|
|
96
|
-
# _io_loop might not exist in newer Motor versions
|
|
97
|
-
pass
|
|
98
|
-
except RuntimeError:
|
|
99
|
-
# No running loop, which is fine - Motor will handle it
|
|
100
|
-
pass
|
|
101
|
-
|
|
102
|
-
async def verify_connection_async(self) -> bool:
|
|
103
|
-
"""Verify MongoDB connection asynchronously. Safe to call from async context."""
|
|
104
|
-
try:
|
|
105
|
-
# Ensure we're using the current event loop
|
|
106
|
-
self._ensure_async_client_loop()
|
|
107
|
-
await self.async_client.admin.command('ping')
|
|
108
|
-
self._connection_verified = True
|
|
109
|
-
return True
|
|
26
|
+
self.client.admin.command('ping')
|
|
110
27
|
except Exception as e:
|
|
111
|
-
|
|
112
|
-
raise ConnectionError(f"Failed to
|
|
28
|
+
self.client.close()
|
|
29
|
+
raise ConnectionError(f"Failed to connect to MongoDB: {str(e)}")
|
|
113
30
|
|
|
31
|
+
atexit.register(self.close)
|
|
32
|
+
|
|
114
33
|
def close(self) -> None:
|
|
115
34
|
if hasattr(self, 'client'):
|
|
116
35
|
self.client.close()
|
|
117
|
-
if hasattr(self, 'async_client'):
|
|
118
|
-
self.async_client.close()
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from pydantic import BaseModel, Field, model_validator, ValidationInfo
|
|
2
2
|
from typing import Optional
|
|
3
|
+
from urllib.parse import urlparse, unquote
|
|
3
4
|
from .content_media import ChattyContentMedia
|
|
4
5
|
|
|
5
6
|
class ChattyContentDocument(ChattyContentMedia):
|
|
@@ -8,5 +9,6 @@ class ChattyContentDocument(ChattyContentMedia):
|
|
|
8
9
|
@model_validator(mode='before')
|
|
9
10
|
def validate_filename(cls, data: dict, info: ValidationInfo):
|
|
10
11
|
if not data.get("filename") and data.get("url"):
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
parsed = urlparse(data["url"])
|
|
13
|
+
data["filename"] = unquote(parsed.path.split("/")[-1])
|
|
14
|
+
return data
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from pydantic import BaseModel, Field, field_validator, HttpUrl
|
|
2
2
|
from typing import Optional
|
|
3
|
+
from urllib.parse import quote
|
|
3
4
|
class ChattyContentMedia(BaseModel):
|
|
4
5
|
id: Optional[str] = Field(description="Unique identifier for the image. Also known as media_id", default="")
|
|
5
6
|
url: str = Field(description="URL of the media from S3")
|
|
@@ -11,9 +12,9 @@ class ChattyContentMedia(BaseModel):
|
|
|
11
12
|
def validate_url(cls, v):
|
|
12
13
|
if not v:
|
|
13
14
|
raise ValueError("URL is required")
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
encoded = quote(str(v), safe=":/?&=%#")
|
|
16
|
+
HttpUrl(encoded)
|
|
17
|
+
return encoded
|
|
16
18
|
|
|
17
19
|
def get_body_or_caption(self) -> str:
|
|
18
20
|
return self.caption
|
|
19
|
-
|
|
@@ -5,7 +5,6 @@ import json
|
|
|
5
5
|
from datetime import timedelta
|
|
6
6
|
|
|
7
7
|
from letschatty.models.utils.definitions import Area
|
|
8
|
-
from pydantic_core.core_schema import custom_error_schema
|
|
9
8
|
logger = logging.getLogger("logger")
|
|
10
9
|
|
|
11
10
|
class Context(BaseModel):
|
|
@@ -230,16 +229,4 @@ class OpenAIError(CustomException):
|
|
|
230
229
|
|
|
231
230
|
class NewerAvailableMessageToBeProcessedByAiAgent(CustomException):
|
|
232
231
|
def __init__(self, message="Duplicated incoming message call for ai agent", status_code=400, **context_data):
|
|
233
|
-
super().__init__(message, status_code=status_code, **context_data)
|
|
234
|
-
|
|
235
|
-
class MissingAIAgentInChat(CustomException):
|
|
236
|
-
def __init__(self, message="Missing AI agent in chat", status_code=400, **context_data):
|
|
237
|
-
super().__init__(message, status_code=status_code, **context_data)
|
|
238
|
-
|
|
239
|
-
class ChattyAIModeOff(CustomException):
|
|
240
|
-
def __init__(self, message="Chatty AI agent is in OFF mode", status_code=400, **context_data):
|
|
241
|
-
super().__init__(message, status_code=status_code, **context_data)
|
|
242
|
-
|
|
243
|
-
class ChatWithActiveContinuousConversation(CustomException):
|
|
244
|
-
def __init__(self, message="Chat has active continuous conversation", status_code=400, **context_data):
|
|
245
|
-
super().__init__(message, status_code=status_code, **context_data)
|
|
232
|
+
super().__init__(message, status_code=status_code, **context_data)
|
|
@@ -14,16 +14,13 @@ class SmartFollowUpContextBuilder(ContextBuilder):
|
|
|
14
14
|
|
|
15
15
|
@staticmethod
|
|
16
16
|
def check_minimum_time_since_last_message(chat: Chat, follow_up_strategy: FollowUpStrategy,smart_follow_up_state: FlowStateAssignedToChat) -> bool:
|
|
17
|
-
|
|
18
|
-
# So we add 1 to get the interval for the follow-up we're about to send
|
|
19
|
-
next_followup_number = smart_follow_up_state.consecutive_count + 1
|
|
20
|
-
expected_interval_minutes = follow_up_strategy.get_interval_for_followup(next_followup_number)
|
|
17
|
+
expected_interval_minutes = follow_up_strategy.get_interval_for_followup(smart_follow_up_state.consecutive_count)
|
|
21
18
|
last_message_timestamp = chat.last_message_timestamp
|
|
22
19
|
if last_message_timestamp is None:
|
|
23
20
|
raise HumanInterventionRequired("There's no last message in the chat, can't validate the minimum time since last message for the smart follow up")
|
|
24
21
|
time_since_last_message = datetime.now(ZoneInfo('UTC')) - last_message_timestamp
|
|
25
22
|
if time_since_last_message.total_seconds() < expected_interval_minutes * 60:
|
|
26
|
-
raise PostponeFollowUp(time_delta= timedelta(seconds=expected_interval_minutes * 60 - time_since_last_message.total_seconds()), message=f"Se pospuso el Smart Follow Up porque no ha pasado el tiempo mínimo esperado de {expected_interval_minutes/60} horas para el seguimiento #{
|
|
23
|
+
raise PostponeFollowUp(time_delta= timedelta(seconds=expected_interval_minutes * 60 - time_since_last_message.total_seconds()), message=f"Se pospuso el Smart Follow Up porque no ha pasado el tiempo mínimo esperado de {expected_interval_minutes/60} horas para el seguimiento #{smart_follow_up_state.consecutive_count}")
|
|
27
24
|
return True
|
|
28
25
|
|
|
29
26
|
|
|
@@ -1,14 +1,2 @@
|
|
|
1
1
|
from .base_container import ChattyAssetBaseContainer
|
|
2
2
|
from .base_container_with_collection import ChattyAssetContainerWithCollection
|
|
3
|
-
from .assets_collections import AssetsCollections
|
|
4
|
-
from .services import (
|
|
5
|
-
ProductService,
|
|
6
|
-
TagService,
|
|
7
|
-
UserService,
|
|
8
|
-
ChatService,
|
|
9
|
-
SourceService,
|
|
10
|
-
FlowService,
|
|
11
|
-
SaleService,
|
|
12
|
-
ContactPointService,
|
|
13
|
-
AiAgentService
|
|
14
|
-
)
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
from typing import TypeVar, Generic, Type, Callable, Protocol, Optional
|
|
3
|
-
|
|
4
|
-
from bson import ObjectId
|
|
5
|
-
from letschatty.models.utils.types import StrObjectId
|
|
2
|
+
from typing import TypeVar, Generic, Type, Callable, Protocol, Optional
|
|
6
3
|
from .base_container_with_collection import ChattyAssetCollectionInterface, ChattyAssetContainerWithCollection, CacheConfig
|
|
7
4
|
from ...models.base_models import ChattyAssetModel
|
|
8
5
|
from ...models.base_models.chatty_asset_model import ChattyAssetPreview
|
|
9
6
|
from ...models.data_base.mongo_connection import MongoConnection
|
|
10
7
|
import logging
|
|
11
8
|
import os
|
|
12
|
-
|
|
13
|
-
if TYPE_CHECKING:
|
|
14
|
-
from ...models.analytics.events.base import EventType
|
|
15
|
-
from ...models.company.empresa import EmpresaModel
|
|
16
|
-
from ...models.execution.execution import ExecutionContext
|
|
17
|
-
from ...models.company.assets.company_assets import CompanyAssetType
|
|
18
|
-
from ...models.utils.types.deletion_type import DeletionType
|
|
19
|
-
|
|
20
9
|
logger = logging.getLogger("AssetService")
|
|
21
10
|
|
|
22
11
|
# Protocol for assets that specify their preview type
|
|
@@ -55,75 +44,17 @@ class AssetCollection(Generic[T, P], ChattyAssetCollectionInterface[T, P]):
|
|
|
55
44
|
raise ValueError(f"Data must be a dictionary, got {type(data)}: {data}")
|
|
56
45
|
return self._create_instance_method(data)
|
|
57
46
|
|
|
58
|
-
|
|
59
47
|
class AssetService(Generic[T, P], ChattyAssetContainerWithCollection[T, P]):
|
|
60
|
-
"""
|
|
61
|
-
Generic service for handling CRUD operations for any Chatty asset.
|
|
62
|
-
|
|
63
|
-
Supports optional automatic event handling for API implementations.
|
|
64
|
-
Set these class attributes to enable events:
|
|
65
|
-
- asset_type_enum: CompanyAssetType (e.g., CompanyAssetType.PRODUCTS)
|
|
66
|
-
- event_type_created: EventType (e.g., EventType.PRODUCT_CREATED)
|
|
67
|
-
- event_type_updated: EventType (e.g., EventType.PRODUCT_UPDATED)
|
|
68
|
-
- event_type_deleted: EventType (e.g., EventType.PRODUCT_DELETED)
|
|
69
|
-
"""
|
|
70
|
-
|
|
71
|
-
# Optional: Set these in subclasses to enable automatic event handling
|
|
72
|
-
asset_type_enum: ClassVar[Optional['CompanyAssetType']] = None
|
|
73
|
-
event_type_created: ClassVar[Optional['EventType']] = None
|
|
74
|
-
event_type_updated: ClassVar[Optional['EventType']] = None
|
|
75
|
-
event_type_deleted: ClassVar[Optional['EventType']] = None
|
|
76
|
-
|
|
77
|
-
collection: AssetCollection[T, P] # Type annotation for better type checking
|
|
48
|
+
"""Generic service for handling CRUD operations for any Chatty asset"""
|
|
78
49
|
|
|
79
50
|
def __init__(self,
|
|
80
|
-
|
|
51
|
+
collection_name: str,
|
|
52
|
+
asset_type: Type[T],
|
|
53
|
+
connection: MongoConnection,
|
|
54
|
+
create_instance_method: Callable[[dict], T],
|
|
55
|
+
preview_type: Optional[Type[P]] = None,
|
|
81
56
|
cache_config: CacheConfig = CacheConfig.default()):
|
|
82
|
-
""
|
|
83
|
-
Initialize AssetService with a pre-configured collection.
|
|
84
|
-
|
|
85
|
-
The item_type and preview_type are automatically extracted from the collection,
|
|
86
|
-
eliminating redundancy and simplifying the API.
|
|
87
|
-
|
|
88
|
-
Args:
|
|
89
|
-
collection: Pre-configured AssetCollection subclass
|
|
90
|
-
cache_config: Cache configuration
|
|
91
|
-
"""
|
|
92
|
-
logger.debug(f"AssetService {self.__class__.__name__} initializing with collection")
|
|
93
|
-
super().__init__(
|
|
94
|
-
item_type=collection.type,
|
|
95
|
-
preview_type=collection.preview_type,
|
|
96
|
-
collection=collection,
|
|
97
|
-
cache_config=cache_config,
|
|
98
|
-
)
|
|
99
|
-
logger.debug(f"AssetService {self.__class__.__name__} initialized")
|
|
100
|
-
|
|
101
|
-
@classmethod
|
|
102
|
-
def from_config(cls,
|
|
103
|
-
collection_name: str,
|
|
104
|
-
asset_type: Type[T],
|
|
105
|
-
connection: MongoConnection,
|
|
106
|
-
create_instance_method: Callable[[dict], T],
|
|
107
|
-
preview_type: Optional[Type[P]] = None,
|
|
108
|
-
cache_config: CacheConfig = CacheConfig.default()) -> 'AssetService[T, P]':
|
|
109
|
-
"""
|
|
110
|
-
Create an AssetService using the legacy configuration pattern.
|
|
111
|
-
|
|
112
|
-
This class method is provided for backward compatibility.
|
|
113
|
-
New code should use pre-configured AssetCollection subclasses.
|
|
114
|
-
|
|
115
|
-
Args:
|
|
116
|
-
collection_name: MongoDB collection name
|
|
117
|
-
asset_type: The asset model type
|
|
118
|
-
connection: MongoDB connection
|
|
119
|
-
create_instance_method: Factory method to create asset instances
|
|
120
|
-
preview_type: Optional preview type
|
|
121
|
-
cache_config: Cache configuration
|
|
122
|
-
|
|
123
|
-
Returns:
|
|
124
|
-
AssetService instance
|
|
125
|
-
"""
|
|
126
|
-
logger.debug(f"AssetService creating from config for {collection_name}")
|
|
57
|
+
logger.debug(f"AssetService {self.__class__.__name__} initializing for {collection_name}")
|
|
127
58
|
asset_collection = AssetCollection(
|
|
128
59
|
collection=collection_name,
|
|
129
60
|
asset_type=asset_type,
|
|
@@ -131,102 +62,13 @@ class AssetService(Generic[T, P], ChattyAssetContainerWithCollection[T, P]):
|
|
|
131
62
|
create_instance_method=create_instance_method,
|
|
132
63
|
preview_type=preview_type
|
|
133
64
|
)
|
|
134
|
-
|
|
65
|
+
super().__init__(
|
|
66
|
+
item_type=asset_type,
|
|
67
|
+
preview_type=preview_type,
|
|
135
68
|
collection=asset_collection,
|
|
136
|
-
cache_config=cache_config
|
|
69
|
+
cache_config=cache_config,
|
|
137
70
|
)
|
|
138
|
-
|
|
139
|
-
def _should_handle_events(self) -> bool:
|
|
140
|
-
"""Check if this service should handle events automatically"""
|
|
141
|
-
return (self.asset_type_enum is not None and
|
|
142
|
-
self.event_type_created is not None and
|
|
143
|
-
self.event_type_updated is not None and
|
|
144
|
-
self.event_type_deleted is not None)
|
|
145
|
-
|
|
146
|
-
def _queue_event(self, item: T, event_type: 'EventType', execution_context: 'ExecutionContext', company_info: 'EmpresaModel'):
|
|
147
|
-
"""Queue an event for this asset if event handling is enabled"""
|
|
148
|
-
if not self._should_handle_events() or not self.asset_type_enum:
|
|
149
|
-
return
|
|
150
|
-
|
|
151
|
-
try:
|
|
152
|
-
from ...services.factories.analytics.events_factory import EventFactory
|
|
153
|
-
from ...services.events import events_manager
|
|
154
|
-
|
|
155
|
-
# Type guard - company_id should exist on ChattyAssetModel
|
|
156
|
-
if not hasattr(item, 'company_id'):
|
|
157
|
-
logger.warning(f"Asset {type(item).__name__} missing company_id, skipping event")
|
|
158
|
-
return
|
|
159
|
-
|
|
160
|
-
events = EventFactory.asset_events(
|
|
161
|
-
company_id=item.company_id, # type: ignore[attr-defined]
|
|
162
|
-
executor_id=execution_context.executor.id,
|
|
163
|
-
asset=item,
|
|
164
|
-
asset_type=self.asset_type_enum,
|
|
165
|
-
event_type=event_type,
|
|
166
|
-
time=execution_context.time,
|
|
167
|
-
trace_id=execution_context.trace_id,
|
|
168
|
-
executor_type=execution_context.executor.type,
|
|
169
|
-
company_info=company_info
|
|
170
|
-
)
|
|
171
|
-
events_manager.queue_events(events)
|
|
172
|
-
except ImportError:
|
|
173
|
-
# Events not available (microservice context) - skip
|
|
174
|
-
pass
|
|
175
|
-
|
|
176
|
-
# All methods are now async-only for better performance
|
|
177
|
-
async def insert(self, item: T, execution_context: 'ExecutionContext', company_info: Optional['EmpresaModel'] = None) -> T:
|
|
178
|
-
"""Insert with automatic event handling if configured"""
|
|
179
|
-
result = await super().insert(item, execution_context)
|
|
180
|
-
if company_info and self._should_handle_events() and self.event_type_created:
|
|
181
|
-
self._queue_event(result, self.event_type_created, execution_context, company_info)
|
|
182
|
-
return result
|
|
183
|
-
|
|
184
|
-
async def update(self, id: str, new_item: T, execution_context: 'ExecutionContext', company_info: Optional['EmpresaModel'] = None) -> T:
|
|
185
|
-
"""Update with automatic event handling if configured"""
|
|
186
|
-
result = await super().update(id, new_item, execution_context)
|
|
187
|
-
if company_info and self._should_handle_events() and self.event_type_updated:
|
|
188
|
-
self._queue_event(result, self.event_type_updated, execution_context, company_info)
|
|
189
|
-
return result
|
|
190
|
-
|
|
191
|
-
async def delete(self, id: str, execution_context: 'ExecutionContext', company_info: Optional['EmpresaModel'] = None, deletion_type: Optional['DeletionType'] = None) -> T:
|
|
192
|
-
"""Delete with automatic event handling if configured"""
|
|
193
|
-
from ...models.utils.types.deletion_type import DeletionType as DT
|
|
194
|
-
result = await super().delete(id, execution_context, deletion_type or DT.LOGICAL)
|
|
195
|
-
if company_info and self._should_handle_events() and self.event_type_deleted:
|
|
196
|
-
self._queue_event(result, self.event_type_deleted, execution_context, company_info)
|
|
197
|
-
return result
|
|
198
|
-
|
|
199
|
-
async def restore(self, id: str, execution_context: 'ExecutionContext', company_info: Optional['EmpresaModel'] = None) -> T:
|
|
200
|
-
"""Restore with automatic event handling if configured"""
|
|
201
|
-
result = await super().restore(id, execution_context)
|
|
202
|
-
if company_info and self._should_handle_events() and self.event_type_updated:
|
|
203
|
-
self._queue_event(result, self.event_type_updated, execution_context, company_info)
|
|
204
|
-
return result
|
|
205
|
-
|
|
206
|
-
# Generic convenience methods
|
|
207
|
-
async def create_asset(self, data: dict, execution_context: 'ExecutionContext', company_info: 'EmpresaModel') -> T:
|
|
208
|
-
"""
|
|
209
|
-
Generic create method - creates instance from dict and inserts with events.
|
|
210
|
-
Can be called as create_asset or aliased to create_product/create_tag/etc.
|
|
211
|
-
"""
|
|
212
|
-
data["company_id"] = execution_context.company_id
|
|
213
|
-
item = self.collection.create_instance(data)
|
|
214
|
-
return await self.insert(item, execution_context, company_info)
|
|
215
|
-
|
|
216
|
-
async def update_asset(self, id: str, data: dict, execution_context: 'ExecutionContext', company_info: 'EmpresaModel') -> T:
|
|
217
|
-
"""
|
|
218
|
-
Generic update method - creates instance from dict and updates with events.
|
|
219
|
-
Can be called as update_asset or aliased to update_product/update_tag/etc.
|
|
220
|
-
"""
|
|
221
|
-
new_item = self.collection.create_instance(data)
|
|
222
|
-
return await self.update(id, new_item, execution_context, company_info)
|
|
223
|
-
|
|
224
|
-
async def delete_asset(self, id: str, execution_context: 'ExecutionContext', company_info: 'EmpresaModel') -> T:
|
|
225
|
-
"""
|
|
226
|
-
Generic delete method - deletes with events.
|
|
227
|
-
Can be called as delete_asset or aliased to delete_product/delete_tag/etc.
|
|
228
|
-
"""
|
|
229
|
-
return await self.delete(id, execution_context, company_info)
|
|
71
|
+
logger.debug(f"AssetService {self.__class__.__name__} initialized for {collection_name}")
|
|
230
72
|
|
|
231
73
|
def get_preview_type(self) -> Type[P]:
|
|
232
74
|
"""Get the preview type from the asset class if it has one"""
|
|
@@ -239,22 +81,3 @@ class AssetService(Generic[T, P], ChattyAssetContainerWithCollection[T, P]):
|
|
|
239
81
|
preview_type = self.get_preview_type()
|
|
240
82
|
return super().get_preview_by_id(id, company_id, preview_type)
|
|
241
83
|
|
|
242
|
-
# Additional async read methods (passthrough to base class)
|
|
243
|
-
async def get_by_id(self, id: str) -> T:
|
|
244
|
-
"""Get by ID"""
|
|
245
|
-
return await super().get_by_id(id)
|
|
246
|
-
|
|
247
|
-
async def get_all(self, company_id: str) -> List[T]:
|
|
248
|
-
"""Get all for company"""
|
|
249
|
-
return await super().get_all(company_id)
|
|
250
|
-
|
|
251
|
-
async def get_by_query(self, query: dict, company_id: Optional[str]) -> List[T]:
|
|
252
|
-
"""Get by query"""
|
|
253
|
-
return await super().get_by_query(query, company_id)
|
|
254
|
-
|
|
255
|
-
async def get_item_dumped(self, id: str) -> dict:
|
|
256
|
-
"""Get item by ID and return as JSON serialized dict for frontend"""
|
|
257
|
-
from ...models.utils.types.serializer_type import SerializerType
|
|
258
|
-
item = await self.get_by_id(id)
|
|
259
|
-
return item.model_dump_json(serializer=SerializerType.FRONTEND)
|
|
260
|
-
|