h-message-bus 0.0.39__py3-none-any.whl → 0.0.41__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.
- h_message_bus/infrastructure/nats_client_repository.py +70 -54
- h_message_bus/infrastructure/nats_config.py +1 -1
- {h_message_bus-0.0.39.dist-info → h_message_bus-0.0.41.dist-info}/METADATA +106 -85
- h_message_bus-0.0.41.dist-info/RECORD +20 -0
- {h_message_bus-0.0.39.dist-info → h_message_bus-0.0.41.dist-info}/WHEEL +1 -1
- h_message_bus/domain/event_messages/__init__.py +0 -0
- h_message_bus/domain/models/event_message_topic.py +0 -20
- h_message_bus/domain/models/request_message_topic.py +0 -100
- h_message_bus/domain/models/twitter_message_metadata.py +0 -31
- h_message_bus/domain/models/twitter_user_metadata.py +0 -76
- h_message_bus/domain/models/vector_collection_metadata.py +0 -30
- h_message_bus/domain/models/vector_query_answer.py +0 -22
- h_message_bus/domain/request_messages/__init__.py +0 -0
- h_message_bus/domain/request_messages/graph_clear_request_message.py +0 -26
- h_message_bus/domain/request_messages/graph_get_all_request_message.py +0 -27
- h_message_bus/domain/request_messages/graph_get_all_result_response_message.py +0 -45
- h_message_bus/domain/request_messages/graph_node_add_request_message.py +0 -64
- h_message_bus/domain/request_messages/graph_node_added_response_message.py +0 -69
- h_message_bus/domain/request_messages/graph_node_get_request_message.py +0 -39
- h_message_bus/domain/request_messages/graph_node_get_result_response_message.py +0 -70
- h_message_bus/domain/request_messages/graph_node_update_request_message.py +0 -56
- h_message_bus/domain/request_messages/graph_node_update_response_message.py +0 -69
- h_message_bus/domain/request_messages/graph_nodes_by_property_request_message.py +0 -45
- h_message_bus/domain/request_messages/graph_nodes_by_property_response_message.py +0 -80
- h_message_bus/domain/request_messages/graph_query_operation_request_message.py +0 -114
- h_message_bus/domain/request_messages/graph_query_operation_response_message.py +0 -85
- h_message_bus/domain/request_messages/graph_query_request_message.py +0 -48
- h_message_bus/domain/request_messages/graph_relationship_added_request_message.py +0 -63
- h_message_bus/domain/request_messages/init_knowledgebase_request.py +0 -26
- h_message_bus/domain/request_messages/tg_message_request_message.py +0 -48
- h_message_bus/domain/request_messages/tg_user_message_reply_request_message.py +0 -56
- h_message_bus/domain/request_messages/tg_user_message_request_message.py +0 -93
- h_message_bus/domain/request_messages/twitter_follow_user_request_message.py +0 -40
- h_message_bus/domain/request_messages/twitter_follow_user_response_message.py +0 -43
- h_message_bus/domain/request_messages/twitter_get_tweet_request_message.py +0 -40
- h_message_bus/domain/request_messages/twitter_get_tweet_response_message.py +0 -54
- h_message_bus/domain/request_messages/twitter_get_user_request_message.py +0 -35
- h_message_bus/domain/request_messages/twitter_get_user_response_message.py +0 -96
- h_message_bus/domain/request_messages/twitter_get_users_request_message.py +0 -42
- h_message_bus/domain/request_messages/twitter_get_users_response_message.py +0 -44
- h_message_bus/domain/request_messages/twitter_home_timeline_request_message.py +0 -56
- h_message_bus/domain/request_messages/twitter_home_timeline_response_message.py +0 -74
- h_message_bus/domain/request_messages/twitter_post_tweet_request_message.py +0 -40
- h_message_bus/domain/request_messages/twitter_post_tweet_response_message.py +0 -47
- h_message_bus/domain/request_messages/twitter_quote_retweet_request_message.py +0 -48
- h_message_bus/domain/request_messages/twitter_quote_retweet_response_message.py +0 -55
- h_message_bus/domain/request_messages/twitter_reply_request_message.py +0 -48
- h_message_bus/domain/request_messages/twitter_reply_response_message.py +0 -54
- h_message_bus/domain/request_messages/twitter_retweet_request_message.py +0 -40
- h_message_bus/domain/request_messages/twitter_retweet_response_message.py +0 -43
- h_message_bus/domain/request_messages/twitter_search_request_message.py +0 -73
- h_message_bus/domain/request_messages/twitter_search_response_message.py +0 -73
- h_message_bus/domain/request_messages/twitter_user_tweets_request_message.py +0 -63
- h_message_bus/domain/request_messages/twitter_user_tweets_response_message.py +0 -89
- h_message_bus/domain/request_messages/vector_query_collection_request_message.py +0 -51
- h_message_bus/domain/request_messages/vector_query_collection_response_message.py +0 -47
- h_message_bus/domain/request_messages/vector_read_metadata_request_message.py +0 -25
- h_message_bus/domain/request_messages/vector_read_metadata_response_message.py +0 -45
- h_message_bus/domain/request_messages/vector_save_request_message.py +0 -66
- h_message_bus/domain/request_messages/web_get_docs_request_message.py +0 -46
- h_message_bus/domain/request_messages/web_search_request_message.py +0 -32
- h_message_bus-0.0.39.dist-info/RECORD +0 -76
- {h_message_bus-0.0.39.dist-info → h_message_bus-0.0.41.dist-info}/top_level.txt +0 -0
@@ -1,4 +1,5 @@
|
|
1
1
|
import logging
|
2
|
+
import asyncio
|
2
3
|
from typing import Optional, Callable, Any
|
3
4
|
|
4
5
|
import nats
|
@@ -6,37 +7,34 @@ from nats.aio.client import Client as NatsClient
|
|
6
7
|
|
7
8
|
from ..infrastructure.nats_config import NatsConfig
|
8
9
|
|
9
|
-
|
10
10
|
class NatsClientRepository:
|
11
11
|
"""
|
12
12
|
Repository for managing connection and interaction with a NATS server.
|
13
|
-
|
14
|
-
This class provides methods to establish a connection with a NATS server,
|
15
|
-
publish and subscribe to subjects, send requests and handle responses, and
|
16
|
-
cleanly disconnect from the server. It abstracts the connection and ensures
|
17
|
-
seamless communication with the NATS server.
|
18
|
-
|
19
|
-
:ivar config: Configuration details for the NATS client, including server
|
20
|
-
connection parameters, timeouts, and limits.
|
21
|
-
:type config: NatsConfig
|
22
|
-
:ivar client: Instance of the NATS client used for communication. Initialized
|
23
|
-
as None and assigned upon connecting to a NATS server.
|
24
|
-
:type client: Optional[NatsClient]
|
25
|
-
:ivar subscriptions: List of active subscriptions for NATS subjects.
|
26
|
-
:type subscriptions: list
|
27
13
|
"""
|
28
14
|
|
29
15
|
def __init__(self, config: NatsConfig):
|
30
16
|
self.config = config
|
31
|
-
self.client: NatsClient
|
32
|
-
|
17
|
+
self.client: Optional[NatsClient] = None
|
33
18
|
self.subscriptions = []
|
34
19
|
self.logger = logging.getLogger(__name__)
|
35
20
|
|
36
21
|
async def connect(self) -> None:
|
37
|
-
"""Connect to NATS server."""
|
22
|
+
"""Connect to NATS server with event handlers and logging."""
|
38
23
|
if self.client and self.client.is_connected:
|
39
24
|
return
|
25
|
+
|
26
|
+
async def error_cb(e):
|
27
|
+
self.logger.error(f"NATS client error: {e}")
|
28
|
+
|
29
|
+
async def disconnect_cb():
|
30
|
+
self.logger.warning("NATS client disconnected.")
|
31
|
+
|
32
|
+
async def reconnect_cb():
|
33
|
+
self.logger.info("NATS client reconnected.")
|
34
|
+
|
35
|
+
async def closed_cb():
|
36
|
+
self.logger.warning("NATS client connection closed.")
|
37
|
+
|
40
38
|
self.logger.info(f"Connecting to NATS server at {self.config.server}")
|
41
39
|
|
42
40
|
self.client = await nats.connect(
|
@@ -45,49 +43,67 @@ class NatsClientRepository:
|
|
45
43
|
reconnect_time_wait=self.config.reconnect_time_wait,
|
46
44
|
connect_timeout=self.config.connection_timeout,
|
47
45
|
ping_interval=self.config.ping_interval,
|
48
|
-
max_outstanding_pings=self.config.max_outstanding_pings
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
46
|
+
max_outstanding_pings=self.config.max_outstanding_pings,
|
47
|
+
error_cb=error_cb,
|
48
|
+
disconnected_cb=disconnect_cb,
|
49
|
+
reconnected_cb=reconnect_cb,
|
50
|
+
closed_cb=closed_cb,
|
51
|
+
) # type: ignore
|
52
|
+
|
53
|
+
async def _ensure_connected(self, retries: int = 5):
|
54
|
+
"""Wait and retry until the client is connected."""
|
55
|
+
for attempt in range(retries):
|
56
|
+
if self.client and self.client.is_connected:
|
57
|
+
return
|
58
|
+
self.logger.info(f"Waiting for NATS connection (attempt {attempt+1})...")
|
59
|
+
await asyncio.sleep(2 ** attempt)
|
60
|
+
raise ConnectionError("Could not establish connection to NATS server.")
|
61
|
+
|
62
|
+
async def publish(self, subject: str, payload: bytes, retries: int = 3) -> None:
|
63
|
+
"""Publish a raw message to NATS with retries and backoff."""
|
64
|
+
for attempt in range(retries):
|
65
|
+
try:
|
66
|
+
await self._ensure_connected()
|
67
|
+
await self.client.publish(subject, payload)
|
68
|
+
return
|
69
|
+
except Exception as e:
|
70
|
+
self.logger.error(f"Failed to publish message (attempt {attempt+1}): {e}")
|
71
|
+
await asyncio.sleep(2 ** attempt)
|
72
|
+
self.logger.error("Giving up publishing after retries.")
|
61
73
|
|
62
74
|
async def subscribe(self, subject: str, callback: Callable) -> Any:
|
63
75
|
"""Subscribe to a subject with a callback."""
|
64
|
-
|
65
|
-
await self.connect()
|
66
|
-
|
67
|
-
subscription = await self.client.subscribe(subject, cb=callback)
|
68
|
-
self.subscriptions.append(subscription)
|
69
|
-
return subscription
|
70
|
-
|
71
|
-
async def request(self, subject: str, payload: bytes, timeout: float = 2.0) -> Optional[bytes]:
|
72
|
-
"""Send a request and get raw response."""
|
73
|
-
if not self.client or not self.client.is_connected:
|
74
|
-
await self.connect()
|
75
|
-
|
76
|
+
await self._ensure_connected()
|
76
77
|
try:
|
77
|
-
|
78
|
-
|
78
|
+
subscription = await self.client.subscribe(subject, cb=callback)
|
79
|
+
self.subscriptions.append(subscription)
|
80
|
+
return subscription
|
79
81
|
except Exception as e:
|
80
|
-
|
82
|
+
self.logger.error(f"Failed to subscribe to {subject}: {e}")
|
81
83
|
return None
|
82
84
|
|
85
|
+
async def request(self, subject: str, payload: bytes, timeout: float = 2.0, retries: int = 3) -> Optional[bytes]:
|
86
|
+
"""Send a request and get a raw response with retries and backoff."""
|
87
|
+
for attempt in range(retries):
|
88
|
+
try:
|
89
|
+
await self._ensure_connected()
|
90
|
+
response = await self.client.request(subject, payload, timeout=timeout)
|
91
|
+
return response.data
|
92
|
+
except Exception as e:
|
93
|
+
self.logger.error(f"NATS request failed (attempt {attempt+1}): {e}")
|
94
|
+
await asyncio.sleep(2 ** attempt)
|
95
|
+
self.logger.error("Giving up request after retries.")
|
96
|
+
return None
|
97
|
+
|
83
98
|
async def close(self) -> None:
|
84
99
|
"""Close all subscriptions and NATS connection."""
|
85
100
|
if self.client and self.client.is_connected:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
101
|
+
try:
|
102
|
+
for sub in self.subscriptions:
|
103
|
+
await sub.unsubscribe()
|
104
|
+
await self.client.drain()
|
105
|
+
except Exception as e:
|
106
|
+
self.logger.error(f"Error during NATS cleanup: {e}")
|
107
|
+
finally:
|
108
|
+
self.client = None
|
109
|
+
self.subscriptions = []
|
@@ -4,7 +4,7 @@ from dataclasses import dataclass
|
|
4
4
|
@dataclass
|
5
5
|
class NatsConfig:
|
6
6
|
"""
|
7
|
-
Configuration class for NATS client.
|
7
|
+
Configuration class for the NATS client.
|
8
8
|
|
9
9
|
This class encapsulates the configuration options required for connecting and
|
10
10
|
interacting with a NATS (NATS.io) server. It includes options for setting
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: h_message_bus
|
3
|
-
Version: 0.0.
|
3
|
+
Version: 0.0.41
|
4
4
|
Summary: Message bus integration for HAI
|
5
5
|
Author-email: shoebill <shoebill.hai@gmail.com>
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
@@ -40,18 +40,18 @@ pip install h_message_bus
|
|
40
40
|
|
41
41
|
## Topics
|
42
42
|
|
43
|
-
H Message Bus
|
43
|
+
H Message Bus uses topic strings for message routing. It's recommended to follow the convention: `hai.[source].[destination].[action]`
|
44
44
|
|
45
|
-
|
45
|
+
Example topics:
|
46
46
|
|
47
|
-
| Topic
|
48
|
-
|
49
|
-
| `
|
50
|
-
| `
|
51
|
-
| `
|
52
|
-
| `
|
47
|
+
| Topic String | Description |
|
48
|
+
|------------------------|-------------------------------------|
|
49
|
+
| `hai.ai.tg.chat.send` | AI sending message to Telegram chat |
|
50
|
+
| `hai.ai.vectors.save` | AI saving data to vector database |
|
51
|
+
| `hai.ai.vectors.query` | AI querying vector database |
|
52
|
+
| `hai.tg.ai.chat.send` | Telegram sending message to AI |
|
53
53
|
|
54
|
-
You can use these
|
54
|
+
You can use these example topics or create your own topic strings based on your application's needs.
|
55
55
|
|
56
56
|
## Quick Start
|
57
57
|
|
@@ -68,29 +68,31 @@ docker-compose up -d
|
|
68
68
|
```python
|
69
69
|
import asyncio
|
70
70
|
import uuid
|
71
|
-
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage,
|
71
|
+
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, NatsClientRepository
|
72
72
|
|
73
73
|
async def main():
|
74
74
|
# Configure NATS connection
|
75
75
|
config = NatsConfig(server="nats://localhost:4222")
|
76
|
-
|
76
|
+
|
77
|
+
# Create NATS client repository
|
78
|
+
client_repository = NatsClientRepository(config)
|
79
|
+
|
77
80
|
# Create publisher adapter
|
78
|
-
publisher = NatsPublisherAdapter(
|
79
|
-
|
81
|
+
publisher = NatsPublisherAdapter(client_repository)
|
82
|
+
|
80
83
|
# Connect to NATS
|
81
|
-
await
|
82
|
-
|
83
|
-
# Create and publish a message
|
84
|
+
await client_repository.connect()
|
85
|
+
|
86
|
+
# Create and publish a message
|
84
87
|
message = HaiMessage(
|
85
|
-
|
86
|
-
|
87
|
-
topic=Topic.TG_SEND_AI_CHAT_MESSAGE,
|
88
|
+
id=str(uuid.uuid4()),
|
89
|
+
topic="hai.tg.ai.chat.send",
|
88
90
|
payload={"text": "Hello AI, this is a message from Telegram", "chat_id": 12345}
|
89
91
|
)
|
90
|
-
|
92
|
+
|
91
93
|
# Publish message
|
92
94
|
await publisher.publish(message)
|
93
|
-
|
95
|
+
|
94
96
|
# Clean up
|
95
97
|
await publisher.close()
|
96
98
|
|
@@ -102,27 +104,34 @@ if __name__ == "__main__":
|
|
102
104
|
|
103
105
|
```python
|
104
106
|
import asyncio
|
105
|
-
from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage,
|
107
|
+
from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, NatsClientRepository, MessageProcessor
|
106
108
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
109
|
+
class MyMessageProcessor(MessageProcessor):
|
110
|
+
async def process(self, message: HaiMessage):
|
111
|
+
print(f"Received message: {message.id}")
|
112
|
+
print(f"Topic: {message.topic}")
|
113
|
+
print(f"Payload: {message.payload}")
|
114
|
+
print(f"Timestamp: {message.timestamp}")
|
112
115
|
|
113
116
|
async def main():
|
114
117
|
# Configure NATS connection
|
115
118
|
config = NatsConfig(server="nats://localhost:4222")
|
116
|
-
|
119
|
+
|
120
|
+
# Create NATS client repository
|
121
|
+
client_repository = NatsClientRepository(config)
|
122
|
+
|
117
123
|
# Create subscriber
|
118
|
-
subscriber = NatsSubscriberAdapter(
|
119
|
-
|
124
|
+
subscriber = NatsSubscriberAdapter(client_repository)
|
125
|
+
|
120
126
|
# Connect to NATS
|
121
|
-
await
|
122
|
-
|
127
|
+
await client_repository.connect()
|
128
|
+
|
129
|
+
# Create message processor
|
130
|
+
processor = MyMessageProcessor()
|
131
|
+
|
123
132
|
# Subscribe to a topic
|
124
|
-
await subscriber.subscribe(
|
125
|
-
|
133
|
+
await subscriber.subscribe("hai.tg.ai.chat.send", processor)
|
134
|
+
|
126
135
|
# Keep the application running
|
127
136
|
try:
|
128
137
|
print("Subscriber running. Press Ctrl+C to exit.")
|
@@ -130,7 +139,7 @@ async def main():
|
|
130
139
|
await asyncio.sleep(1)
|
131
140
|
except KeyboardInterrupt:
|
132
141
|
# Clean up
|
133
|
-
await
|
142
|
+
await client_repository.close()
|
134
143
|
|
135
144
|
if __name__ == "__main__":
|
136
145
|
asyncio.run(main())
|
@@ -143,74 +152,91 @@ if __name__ == "__main__":
|
|
143
152
|
```python
|
144
153
|
import asyncio
|
145
154
|
import uuid
|
146
|
-
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage,
|
155
|
+
from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, NatsClientRepository
|
147
156
|
|
148
157
|
async def main():
|
158
|
+
# Configure NATS connection
|
149
159
|
config = NatsConfig(server="nats://localhost:4222")
|
150
|
-
|
151
|
-
|
160
|
+
|
161
|
+
# Create NATS client repository
|
162
|
+
client_repository = NatsClientRepository(config)
|
163
|
+
|
164
|
+
# Create publisher adapter
|
165
|
+
publisher = NatsPublisherAdapter(client_repository)
|
166
|
+
|
152
167
|
# Connect to NATS
|
153
|
-
await
|
154
|
-
|
168
|
+
await client_repository.connect()
|
169
|
+
|
155
170
|
request_message = HaiMessage(
|
156
|
-
|
157
|
-
|
158
|
-
topic=Topic.AI_VECTORS_QUERY,
|
171
|
+
id=str(uuid.uuid4()),
|
172
|
+
topic="hai.ai.vectors.query",
|
159
173
|
payload={"query": "find similar documents", "limit": 10}
|
160
174
|
)
|
161
|
-
|
175
|
+
|
162
176
|
# Send request and wait for response (with timeout)
|
163
177
|
response = await publisher.request(request_message, timeout=5.0)
|
164
|
-
|
178
|
+
|
165
179
|
if response:
|
166
180
|
print(f"Received response: {response.payload}")
|
167
181
|
else:
|
168
182
|
print("Request timed out")
|
169
|
-
|
170
|
-
|
183
|
+
|
184
|
+
# Clean up
|
185
|
+
await client_repository.close()
|
171
186
|
|
172
187
|
if __name__ == "__main__":
|
173
188
|
asyncio.run(main())
|
174
189
|
```
|
175
190
|
|
176
|
-
### Creating a Service
|
191
|
+
### Creating a Service for Request-Response
|
177
192
|
|
178
193
|
```python
|
179
194
|
import asyncio
|
180
195
|
import uuid
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
196
|
+
import json
|
197
|
+
from h_message_bus import NatsConfig, NatsSubscriberAdapter, NatsPublisherAdapter, HaiMessage, NatsClientRepository, MessageProcessor
|
198
|
+
|
199
|
+
class RequestResponseProcessor(MessageProcessor):
|
200
|
+
def __init__(self, publisher):
|
201
|
+
self.publisher = publisher
|
202
|
+
|
203
|
+
async def process(self, request: HaiMessage):
|
204
|
+
print(f"Received request: {request.id}")
|
205
|
+
print(f"Payload: {request.payload}")
|
206
|
+
|
207
|
+
# Process the request
|
208
|
+
result = {"status": "success", "data": {"result": 42}}
|
209
|
+
|
210
|
+
# Create a response message
|
211
|
+
response = HaiMessage(
|
212
|
+
id=str(uuid.uuid4()),
|
213
|
+
topic=f"{request.topic}.response",
|
214
|
+
payload=result
|
215
|
+
)
|
216
|
+
|
217
|
+
# Publish the response
|
218
|
+
await self.publisher.publish(response)
|
198
219
|
|
199
220
|
async def main():
|
200
221
|
# Configure NATS connection
|
201
222
|
config = NatsConfig(server="nats://localhost:4222")
|
202
|
-
|
203
|
-
# Create
|
204
|
-
|
205
|
-
|
206
|
-
|
223
|
+
|
224
|
+
# Create NATS client repository
|
225
|
+
client_repository = NatsClientRepository(config)
|
226
|
+
|
207
227
|
# Connect to NATS
|
208
|
-
await
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
228
|
+
await client_repository.connect()
|
229
|
+
|
230
|
+
# Create publisher and subscriber
|
231
|
+
publisher = NatsPublisherAdapter(client_repository)
|
232
|
+
subscriber = NatsSubscriberAdapter(client_repository)
|
233
|
+
|
234
|
+
# Create request processor that uses the publisher
|
235
|
+
processor = RequestResponseProcessor(publisher)
|
236
|
+
|
237
|
+
# Subscribe to the request topic
|
238
|
+
await subscriber.subscribe("hai.ai.vectors.query", processor)
|
239
|
+
|
214
240
|
# Keep the application running
|
215
241
|
try:
|
216
242
|
print("Service running. Press Ctrl+C to exit.")
|
@@ -218,8 +244,7 @@ async def main():
|
|
218
244
|
await asyncio.sleep(1)
|
219
245
|
except KeyboardInterrupt:
|
220
246
|
# Clean up
|
221
|
-
await
|
222
|
-
await publisher.close()
|
247
|
+
await client_repository.close()
|
223
248
|
|
224
249
|
if __name__ == "__main__":
|
225
250
|
asyncio.run(main())
|
@@ -237,7 +262,6 @@ The `NatsConfig` class allows you to customize your NATS connection:
|
|
237
262
|
| `connection_timeout` | Connection timeout (seconds) | 2 |
|
238
263
|
| `ping_interval` | Interval for ping frames (seconds) | 20 |
|
239
264
|
| `max_outstanding_pings` | Maximum unanswered pings before disconnect | 5 |
|
240
|
-
| `max_payload` | Maximum size of the payload in bytes | 1048576 (1MB) |
|
241
265
|
|
242
266
|
## API Reference
|
243
267
|
|
@@ -251,7 +275,6 @@ The following classes are exported directly from the package:
|
|
251
275
|
- `NatsSubscriberAdapter` - Adapter for subscribing to messages
|
252
276
|
- `MessageProcessor` - Processing incoming messages
|
253
277
|
- `NatsClientRepository` - Low-level NATS client operations
|
254
|
-
- `Topic` - Enumeration of predefined topic strings
|
255
278
|
|
256
279
|
### HaiMessage Structure
|
257
280
|
|
@@ -259,12 +282,10 @@ The `HaiMessage` class is the core data structure used for all messaging:
|
|
259
282
|
|
260
283
|
```python
|
261
284
|
class HaiMessage:
|
262
|
-
|
263
|
-
sender: str # Identifier of the sender
|
285
|
+
id: str # Unique identifier for the message
|
264
286
|
topic: str # The topic or channel for the message
|
265
287
|
payload: dict # Actual message data
|
266
|
-
|
267
|
-
timestamp: float = None # Optional message creation timestamp
|
288
|
+
timestamp: str = None # Optional message creation timestamp (ISO format)
|
268
289
|
```
|
269
290
|
|
270
291
|
## Contributing
|
@@ -0,0 +1,20 @@
|
|
1
|
+
__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
h_message_bus/__init__.py,sha256=T_07sHtXLlRuLwK-BRVfFIIOGc48TKbtVLvKtIw2K5U,516
|
3
|
+
h_message_bus/adapters/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
+
h_message_bus/adapters/nats_message_adapter.py,sha256=-hzx0UJmKJT4VWfRA_zVT68hddl9dejvUiSdLU1rY-w,1378
|
5
|
+
h_message_bus/adapters/nats_publisher_adapter.py,sha256=EgrGioGuZfLpJsb5wiialMGMe8E4oUktS8zMdGri3Lc,1982
|
6
|
+
h_message_bus/adapters/nats_subscriber_adapter.py,sha256=UJN2Es7hokbO5YsSUAf0KQ2B15QcGzGtQx-G1S3iQWQ,2105
|
7
|
+
h_message_bus/application/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
8
|
+
h_message_bus/application/message_processor.py,sha256=1vdVfPn1BUDmswJ-Y5aS-YDbLSizrjX6_CMIBa5SGjc,734
|
9
|
+
h_message_bus/application/message_publisher.py,sha256=pYX4JzJLe7L_59gKVYBLqT6iTwFB73zhbzxd7yv2TqI,834
|
10
|
+
h_message_bus/application/message_subcriber.py,sha256=2yqUUHVHtZlA6-zzyHzghdWQ7ZmdcFic5c0xi_rgG-M,741
|
11
|
+
h_message_bus/domain/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
12
|
+
h_message_bus/domain/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
13
|
+
h_message_bus/domain/models/hai_message.py,sha256=b5CfX7hi5uNq77IVnZzEi9iotc4b_U2MNYwV6JY7JcU,2146
|
14
|
+
h_message_bus/infrastructure/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
15
|
+
h_message_bus/infrastructure/nats_client_repository.py,sha256=aAwtUhDZAWOF8fF4IPfcUH5Gc6UscO41ST7rh-HsN2A,4505
|
16
|
+
h_message_bus/infrastructure/nats_config.py,sha256=xg-Ak6H92T32DZKCsbWYQMO9J8nUBI0JZcUY3aA5w1c,1308
|
17
|
+
h_message_bus-0.0.41.dist-info/METADATA,sha256=gVKKEx7HpSIfCGt9omofxOpUqJx077emtCAG0HT2FO0,9171
|
18
|
+
h_message_bus-0.0.41.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
19
|
+
h_message_bus-0.0.41.dist-info/top_level.txt,sha256=BArjhm_lwFR9yJJEIf-LT_X64psuLkXFdbpQRJUreFE,23
|
20
|
+
h_message_bus-0.0.41.dist-info/RECORD,,
|
File without changes
|
@@ -1,20 +0,0 @@
|
|
1
|
-
from enum import Enum
|
2
|
-
|
3
|
-
|
4
|
-
class EventMessageTopic(str, Enum):
|
5
|
-
"""
|
6
|
-
Represents a collection of predefined topics as an enumeration.
|
7
|
-
|
8
|
-
This class is an enumeration that defines constant string values for use
|
9
|
-
as topic identifiers. These topics represent specific actions or messages
|
10
|
-
within a messaging or vector database management context. It ensures
|
11
|
-
consistent usage of these predefined topics across the application.
|
12
|
-
|
13
|
-
syntax: [hai].[source].[destination].[action]
|
14
|
-
|
15
|
-
"""
|
16
|
-
# Events
|
17
|
-
STARTUP = "hai.startup"
|
18
|
-
SHUTDOWN = "hai.shutdown"
|
19
|
-
ENTITY_OFFICIAL_UPDATE = "hai.entity.official.update"
|
20
|
-
VECTOR_STALE_INFORMATION = "hai.vector.collection.stale.information"
|
@@ -1,100 +0,0 @@
|
|
1
|
-
from enum import Enum
|
2
|
-
|
3
|
-
|
4
|
-
class RequestMessageTopic(str, Enum):
|
5
|
-
"""
|
6
|
-
Represents a collection of predefined topics as an enumeration.
|
7
|
-
|
8
|
-
This class is an enumeration that defines constant string values for use
|
9
|
-
as topic identifiers. These topics represent specific actions or messages
|
10
|
-
within a messaging or vector database management context. It ensures
|
11
|
-
consistent usage of these predefined topics across the application.
|
12
|
-
|
13
|
-
syntax: [hai].[source].[destination].[action]
|
14
|
-
|
15
|
-
"""
|
16
|
-
# Telegram
|
17
|
-
TG_CHAT_SEND = "hai.tg.chat.send"
|
18
|
-
TG_USER_CHAT_SEND = "hai.tg.user.chat.send"
|
19
|
-
TG_CHAT_REPLY = "hai.tg.chat.reply"
|
20
|
-
|
21
|
-
# vector database
|
22
|
-
VECTORS_SAVE = "hai.vectors.save"
|
23
|
-
|
24
|
-
VECTORS_QUERY = "hai.vectors.query"
|
25
|
-
VECTORS_QUERY_RESPONSE = "hai.vectors.query.response"
|
26
|
-
|
27
|
-
VECTORS_METADATA_READ = "hai.vectors.metadata.read"
|
28
|
-
VECTORS_METADATA_READ_RESPONSE = "hai.vectors.metadata.read.response"
|
29
|
-
|
30
|
-
# Twitter
|
31
|
-
TWITTER_GET_USER = "hai.twitter.get.user"
|
32
|
-
TWITTER_GET_USER_RESPONSE = "hai.twitter.get.user.response"
|
33
|
-
TWITTER_GET_USERS = "hai.twitter.get.users"
|
34
|
-
TWITTER_GET_USERS_RESPONSE = "hai.twitter.get.users.response"
|
35
|
-
TWITTER_GET_USER_TWEETS = "hai.twitter.get.user.tweets"
|
36
|
-
TWITTER_GET_USER_TWEETS_RESPONSE = "hai.twitter.get.user.tweets.response"
|
37
|
-
|
38
|
-
TWITTER_USER_SEND_AI_CHAT_SEND = "hai.twitter.user.chat.send"
|
39
|
-
TWITTER_USER_SEND_AI_CHAT_SEND_RESPONSE = "hai.twitter.user.chat.send.response"
|
40
|
-
|
41
|
-
TWITTER_FOLLOW_USER= "hai.twitter.follow.user"
|
42
|
-
TWITTER_FOLLOW_USER_RESPONSE = "hai.twitter.follow.user.response"
|
43
|
-
|
44
|
-
TWITTER_GET_HOME_TIMELINE= "hai.twitter.get.home.timeline"
|
45
|
-
TWITTER_GET_HOME_TIMELINE_RESPONSE = "hai.twitter.get.home.timeline.response"
|
46
|
-
|
47
|
-
TWITTER_RETWEET= "hai.twitter.retweet"
|
48
|
-
TWITTER_RETWEET_RESPONSE = "hai.twitter.retweet.response"
|
49
|
-
|
50
|
-
TWITTER_REPLY= "hai.twitter.reply"
|
51
|
-
TWITTER_REPLY_RESPONSE = "hai.twitter.reply.response"
|
52
|
-
|
53
|
-
TWITTER_POST_TWEET= "hai.twitter.post.tweet"
|
54
|
-
TWITTER_POST_TWEET_RESPONSE = "hai.twitter.post.tweet.response"
|
55
|
-
|
56
|
-
TWITTER_QUOTE_RETWEET= "hai.twitter.quote.retweet"
|
57
|
-
TWITTER_QUOTE_RETWEET_RESPONSE = "hai.twitter.quote.retweet.response"
|
58
|
-
|
59
|
-
TWITTER_GET_TWEET= "hai.twitter.get.tweet"
|
60
|
-
TWITTER_GET_TWEET_RESPONSE = "hai.twitter.get.tweet.response"
|
61
|
-
|
62
|
-
TWITTER_SEARCH= "hai.twitter.search"
|
63
|
-
TWITTER_SEARCH_RESPONSE = "hai.twitter.search.response"
|
64
|
-
|
65
|
-
# tools
|
66
|
-
WEB_SEARCH = "hai.tools.web.search"
|
67
|
-
WEB_SEARCH_RESPONSE = "hai.tools.web.search.response"
|
68
|
-
|
69
|
-
WEB_GET_DOCS = "hai.tools.web.get.docs"
|
70
|
-
WEB_GET_DOCS_RESPONSE = "hai.tools.web.get.docs.response"
|
71
|
-
|
72
|
-
WEB_FIND_RELATED = "hai.tools.web.find.related"
|
73
|
-
WEB_FIND_RELATED_RESPONSE = "hai.tools.web.find.related.response"
|
74
|
-
|
75
|
-
INIT_KNOWLEDGE_BASE = "hai.tools.init.knowledge.base"
|
76
|
-
INIT_KNOWLEDGE_BASE_RESPONSE = "hai.tools.init.knowledge.base.response"
|
77
|
-
|
78
|
-
# graph database
|
79
|
-
GRAPH_NODE_ADD = "hai.graph.node.add"
|
80
|
-
GRAPH_NODE_UPDATE = "hai.graph.node.update"
|
81
|
-
GRAPH_NODE_GET = "hai.graph.node.get"
|
82
|
-
GRAPH_NODE_DELETE = "hai.graph.node.delete"
|
83
|
-
GRAPH_RELATIONSHIP_ADD = "hai.graph.relationship.add"
|
84
|
-
GRAPH_RELATIONSHIP_DELETE = "hai.graph.relationship.delete"
|
85
|
-
GRAPH_QUERY = "hai.graph.query"
|
86
|
-
GRAPH_CLEAR = "hai.graph.clear"
|
87
|
-
GRAPH_GET_ALL = "hai.graph.get.all"
|
88
|
-
GRAPH_NODES_BY_PROPERTY = "hai.graph.nodes.by.property"
|
89
|
-
|
90
|
-
GRAPH_QUERY_OPERATION = "hai.graph.query.operation"
|
91
|
-
GRAPH_QUERY_OPERATION_RESPONSE = "hai.graph.query.operation.response"
|
92
|
-
|
93
|
-
GRAPH_NODE_ADD_RESPONSE = "hai.graph.node.add.response"
|
94
|
-
GRAPH_NODE_UPDATE_RESPONSE = "hai.graph.node.update.response"
|
95
|
-
GRAPH_NODE_GET_RESPONSE = "hai.graph.node.get.response"
|
96
|
-
GRAPH_RELATIONSHIP_ADD_RESPONSE = "hai.graph.relationship.add.response"
|
97
|
-
GRAPH_QUERY_RESPONSE = "hai.graph.query.response"
|
98
|
-
GRAPH_CLEAR_RESPONSE = "hai.graph.clear.response"
|
99
|
-
GRAPH_GET_ALL_RESPONSE = "hai.graph.get.all.response"
|
100
|
-
GRAPH_NODES_BY_PROPERTY_RESPONSE = "hai.graph.nodes.by.property.response"
|
@@ -1,31 +0,0 @@
|
|
1
|
-
from ...domain.models.twitter_user_metadata import TwitterUserMetadata
|
2
|
-
|
3
|
-
|
4
|
-
class TwitterMessageMetaData:
|
5
|
-
def __init__(self, tweet_id: str, user: TwitterUserMetadata, message: str, created_at: str, view_count: int, retweet_count: int, reply_count: int):
|
6
|
-
self.tweet_id = tweet_id
|
7
|
-
self.message = message
|
8
|
-
self.user = user
|
9
|
-
self.created_at = created_at,
|
10
|
-
self.view_count = view_count
|
11
|
-
self.retweet_count = retweet_count
|
12
|
-
self.reply_count = reply_count
|
13
|
-
|
14
|
-
def to_dict(self) -> dict:
|
15
|
-
"""
|
16
|
-
Convert TwitterMessage instance to a dictionary.
|
17
|
-
|
18
|
-
Returns:
|
19
|
-
dict: A dictionary representation of the TwitterMessage instance.
|
20
|
-
If the user attribute is a TwitterUser instance, it will be converted
|
21
|
-
to a dictionary using its __dict__ attribute.
|
22
|
-
"""
|
23
|
-
return {
|
24
|
-
'tweet_id': self.tweet_id,
|
25
|
-
'message': self.message,
|
26
|
-
'created_at': self.created_at,
|
27
|
-
'view_count': self.view_count,
|
28
|
-
'retweet_count': self.retweet_count,
|
29
|
-
'reply_count': self.reply_count,
|
30
|
-
'user': self.user.to_dict()
|
31
|
-
}
|