h-message-bus 0.0.40__tar.gz → 0.0.41__tar.gz

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.
Files changed (82) hide show
  1. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/PKG-INFO +106 -85
  2. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/README.md +106 -85
  3. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/pyproject.toml +1 -1
  4. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/infrastructure/nats_client_repository.py +2 -2
  5. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/infrastructure/nats_config.py +1 -1
  6. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/PKG-INFO +106 -85
  7. h_message_bus-0.0.41/src/h_message_bus.egg-info/SOURCES.txt +23 -0
  8. h_message_bus-0.0.40/src/h_message_bus/domain/models/event_message_topic.py +0 -20
  9. h_message_bus-0.0.40/src/h_message_bus/domain/models/request_message_topic.py +0 -100
  10. h_message_bus-0.0.40/src/h_message_bus/domain/models/twitter_message_metadata.py +0 -31
  11. h_message_bus-0.0.40/src/h_message_bus/domain/models/twitter_user_metadata.py +0 -76
  12. h_message_bus-0.0.40/src/h_message_bus/domain/models/vector_collection_metadata.py +0 -30
  13. h_message_bus-0.0.40/src/h_message_bus/domain/models/vector_query_answer.py +0 -22
  14. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/__init__.py +0 -0
  15. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_clear_request_message.py +0 -26
  16. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_get_all_request_message.py +0 -27
  17. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_get_all_result_response_message.py +0 -45
  18. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_add_request_message.py +0 -64
  19. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_added_response_message.py +0 -69
  20. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_get_request_message.py +0 -39
  21. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_get_result_response_message.py +0 -70
  22. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_update_request_message.py +0 -56
  23. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_node_update_response_message.py +0 -69
  24. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_nodes_by_property_request_message.py +0 -45
  25. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_nodes_by_property_response_message.py +0 -80
  26. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_query_operation_request_message.py +0 -114
  27. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_query_operation_response_message.py +0 -85
  28. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_query_request_message.py +0 -48
  29. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/graph_relationship_added_request_message.py +0 -63
  30. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/init_knowledgebase_request.py +0 -26
  31. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/tg_message_request_message.py +0 -48
  32. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/tg_user_message_reply_request_message.py +0 -56
  33. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/tg_user_message_request_message.py +0 -93
  34. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_follow_user_request_message.py +0 -40
  35. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_follow_user_response_message.py +0 -43
  36. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_tweet_request_message.py +0 -40
  37. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_tweet_response_message.py +0 -54
  38. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_user_request_message.py +0 -35
  39. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_user_response_message.py +0 -96
  40. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_users_request_message.py +0 -42
  41. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_get_users_response_message.py +0 -44
  42. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_home_timeline_request_message.py +0 -56
  43. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_home_timeline_response_message.py +0 -74
  44. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_post_tweet_request_message.py +0 -40
  45. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_post_tweet_response_message.py +0 -47
  46. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_quote_retweet_request_message.py +0 -48
  47. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_quote_retweet_response_message.py +0 -55
  48. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_reply_request_message.py +0 -48
  49. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_reply_response_message.py +0 -54
  50. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_retweet_request_message.py +0 -40
  51. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_retweet_response_message.py +0 -43
  52. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_search_request_message.py +0 -73
  53. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_search_response_message.py +0 -73
  54. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_user_tweets_request_message.py +0 -63
  55. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/twitter_user_tweets_response_message.py +0 -89
  56. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/vector_query_collection_request_message.py +0 -51
  57. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/vector_query_collection_response_message.py +0 -47
  58. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/vector_read_metadata_request_message.py +0 -25
  59. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/vector_read_metadata_response_message.py +0 -45
  60. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/vector_save_request_message.py +0 -66
  61. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/web_get_docs_request_message.py +0 -46
  62. h_message_bus-0.0.40/src/h_message_bus/domain/request_messages/web_search_request_message.py +0 -32
  63. h_message_bus-0.0.40/src/h_message_bus/infrastructure/__init__.py +0 -0
  64. h_message_bus-0.0.40/src/h_message_bus.egg-info/SOURCES.txt +0 -79
  65. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/setup.cfg +0 -0
  66. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/__init__.py +0 -0
  67. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/__init__.py +0 -0
  68. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/adapters/__init__.py +0 -0
  69. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_message_adapter.py +0 -0
  70. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_publisher_adapter.py +0 -0
  71. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_subscriber_adapter.py +0 -0
  72. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/application/__init__.py +0 -0
  73. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/application/message_processor.py +0 -0
  74. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/application/message_publisher.py +0 -0
  75. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/application/message_subcriber.py +0 -0
  76. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/domain/__init__.py +0 -0
  77. {h_message_bus-0.0.40/src/h_message_bus/domain/event_messages → h_message_bus-0.0.41/src/h_message_bus/domain/models}/__init__.py +0 -0
  78. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus/domain/models/hai_message.py +0 -0
  79. {h_message_bus-0.0.40/src/h_message_bus/domain/models → h_message_bus-0.0.41/src/h_message_bus/infrastructure}/__init__.py +0 -0
  80. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/dependency_links.txt +0 -0
  81. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/requires.txt +0 -0
  82. {h_message_bus-0.0.40 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: h_message_bus
3
- Version: 0.0.40
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 includes predefined topics following the convention: `hai.[source].[destination].[action]`
43
+ H Message Bus uses topic strings for message routing. It's recommended to follow the convention: `hai.[source].[destination].[action]`
44
44
 
45
- Available topics:
45
+ Example topics:
46
46
 
47
- | Topic Constant | Topic String | Description |
48
- |---------------------------------|------------------------|-------------------------------------|
49
- | `Topic.AI_SEND_TG_CHAT_MESSAGE` | `hai.ai.tg.chat.send` | AI sending message to Telegram chat |
50
- | `Topic.AI_VECTORS_SAVE` | `hai.ai.vectors.save` | AI saving data to vector database |
51
- | `Topic.AI_VECTORS_QUERY` | `hai.ai.vectors.query` | AI querying vector database |
52
- | `Topic.TG_SEND_AI_CHAT_MESSAGE` | `hai.tg.ai.chat.send` | Telegram sending message to AI |
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 predefined topics or create your own topic strings.
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, Topic
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(config)
79
-
81
+ publisher = NatsPublisherAdapter(client_repository)
82
+
80
83
  # Connect to NATS
81
- await publisher.connect()
82
-
83
- # Create and publish a message using a predefined topic
84
+ await client_repository.connect()
85
+
86
+ # Create and publish a message
84
87
  message = HaiMessage(
85
- message_id=str(uuid.uuid4()),
86
- sender="service-a",
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, Topic
107
+ from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, NatsClientRepository, MessageProcessor
106
108
 
107
- async def message_handler(message: HaiMessage):
108
- print(f"Received message: {message.message_id}")
109
- print(f"From: {message.sender}")
110
- print(f"Topic: {message.topic}")
111
- print(f"Payload: {message.payload}")
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(config)
119
-
124
+ subscriber = NatsSubscriberAdapter(client_repository)
125
+
120
126
  # Connect to NATS
121
- await subscriber.connect()
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(Topic.TG_SEND_AI_CHAT_MESSAGE, message_handler)
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 subscriber.close()
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, Topic
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
- publisher = NatsPublisherAdapter(config)
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 publisher.connect()
154
-
168
+ await client_repository.connect()
169
+
155
170
  request_message = HaiMessage(
156
- message_id=str(uuid.uuid4()),
157
- sender="client-service",
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
- await publisher.close()
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 with Request Handler
191
+ ### Creating a Service for Request-Response
177
192
 
178
193
  ```python
179
194
  import asyncio
180
195
  import uuid
181
- from h_message_bus import NatsConfig, NatsSubscriberAdapter, NatsPublisherAdapter, HaiMessage, Topic
182
-
183
- async def request_handler(request: HaiMessage):
184
- print(f"Received request: {request.message_id}")
185
- print(f"Payload: {request.payload}")
186
-
187
- # Process the request
188
- result = {"status": "success", "data": {"result": 42}}
189
-
190
- # Create a response message
191
- return HaiMessage(
192
- message_id=str(uuid.uuid4()),
193
- sender="service-b",
194
- topic=f"{request.topic}.response",
195
- payload=result,
196
- correlation_id=request.message_id
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 subscriber for handling requests
204
- subscriber = NatsSubscriberAdapter(config)
205
- publisher = NatsPublisherAdapter(config)
206
-
223
+
224
+ # Create NATS client repository
225
+ client_repository = NatsClientRepository(config)
226
+
207
227
  # Connect to NATS
208
- await subscriber.connect()
209
- await publisher.connect()
210
-
211
- # Register request handler for vector database queries
212
- await subscriber.subscribe_with_response(Topic.AI_VECTORS_QUERY, request_handler, publisher)
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 subscriber.close()
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
- message_id: str # Unique identifier for the message
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
- correlation_id: str = None # Optional reference to a related message
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
@@ -26,18 +26,18 @@ pip install h_message_bus
26
26
 
27
27
  ## Topics
28
28
 
29
- H Message Bus includes predefined topics following the convention: `hai.[source].[destination].[action]`
29
+ H Message Bus uses topic strings for message routing. It's recommended to follow the convention: `hai.[source].[destination].[action]`
30
30
 
31
- Available topics:
31
+ Example topics:
32
32
 
33
- | Topic Constant | Topic String | Description |
34
- |---------------------------------|------------------------|-------------------------------------|
35
- | `Topic.AI_SEND_TG_CHAT_MESSAGE` | `hai.ai.tg.chat.send` | AI sending message to Telegram chat |
36
- | `Topic.AI_VECTORS_SAVE` | `hai.ai.vectors.save` | AI saving data to vector database |
37
- | `Topic.AI_VECTORS_QUERY` | `hai.ai.vectors.query` | AI querying vector database |
38
- | `Topic.TG_SEND_AI_CHAT_MESSAGE` | `hai.tg.ai.chat.send` | Telegram sending message to AI |
33
+ | Topic String | Description |
34
+ |------------------------|-------------------------------------|
35
+ | `hai.ai.tg.chat.send` | AI sending message to Telegram chat |
36
+ | `hai.ai.vectors.save` | AI saving data to vector database |
37
+ | `hai.ai.vectors.query` | AI querying vector database |
38
+ | `hai.tg.ai.chat.send` | Telegram sending message to AI |
39
39
 
40
- You can use these predefined topics or create your own topic strings.
40
+ You can use these example topics or create your own topic strings based on your application's needs.
41
41
 
42
42
  ## Quick Start
43
43
 
@@ -54,29 +54,31 @@ docker-compose up -d
54
54
  ```python
55
55
  import asyncio
56
56
  import uuid
57
- from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, Topic
57
+ from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, NatsClientRepository
58
58
 
59
59
  async def main():
60
60
  # Configure NATS connection
61
61
  config = NatsConfig(server="nats://localhost:4222")
62
-
62
+
63
+ # Create NATS client repository
64
+ client_repository = NatsClientRepository(config)
65
+
63
66
  # Create publisher adapter
64
- publisher = NatsPublisherAdapter(config)
65
-
67
+ publisher = NatsPublisherAdapter(client_repository)
68
+
66
69
  # Connect to NATS
67
- await publisher.connect()
68
-
69
- # Create and publish a message using a predefined topic
70
+ await client_repository.connect()
71
+
72
+ # Create and publish a message
70
73
  message = HaiMessage(
71
- message_id=str(uuid.uuid4()),
72
- sender="service-a",
73
- topic=Topic.TG_SEND_AI_CHAT_MESSAGE,
74
+ id=str(uuid.uuid4()),
75
+ topic="hai.tg.ai.chat.send",
74
76
  payload={"text": "Hello AI, this is a message from Telegram", "chat_id": 12345}
75
77
  )
76
-
78
+
77
79
  # Publish message
78
80
  await publisher.publish(message)
79
-
81
+
80
82
  # Clean up
81
83
  await publisher.close()
82
84
 
@@ -88,27 +90,34 @@ if __name__ == "__main__":
88
90
 
89
91
  ```python
90
92
  import asyncio
91
- from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, Topic
93
+ from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, NatsClientRepository, MessageProcessor
92
94
 
93
- async def message_handler(message: HaiMessage):
94
- print(f"Received message: {message.message_id}")
95
- print(f"From: {message.sender}")
96
- print(f"Topic: {message.topic}")
97
- print(f"Payload: {message.payload}")
95
+ class MyMessageProcessor(MessageProcessor):
96
+ async def process(self, message: HaiMessage):
97
+ print(f"Received message: {message.id}")
98
+ print(f"Topic: {message.topic}")
99
+ print(f"Payload: {message.payload}")
100
+ print(f"Timestamp: {message.timestamp}")
98
101
 
99
102
  async def main():
100
103
  # Configure NATS connection
101
104
  config = NatsConfig(server="nats://localhost:4222")
102
-
105
+
106
+ # Create NATS client repository
107
+ client_repository = NatsClientRepository(config)
108
+
103
109
  # Create subscriber
104
- subscriber = NatsSubscriberAdapter(config)
105
-
110
+ subscriber = NatsSubscriberAdapter(client_repository)
111
+
106
112
  # Connect to NATS
107
- await subscriber.connect()
108
-
113
+ await client_repository.connect()
114
+
115
+ # Create message processor
116
+ processor = MyMessageProcessor()
117
+
109
118
  # Subscribe to a topic
110
- await subscriber.subscribe(Topic.TG_SEND_AI_CHAT_MESSAGE, message_handler)
111
-
119
+ await subscriber.subscribe("hai.tg.ai.chat.send", processor)
120
+
112
121
  # Keep the application running
113
122
  try:
114
123
  print("Subscriber running. Press Ctrl+C to exit.")
@@ -116,7 +125,7 @@ async def main():
116
125
  await asyncio.sleep(1)
117
126
  except KeyboardInterrupt:
118
127
  # Clean up
119
- await subscriber.close()
128
+ await client_repository.close()
120
129
 
121
130
  if __name__ == "__main__":
122
131
  asyncio.run(main())
@@ -129,74 +138,91 @@ if __name__ == "__main__":
129
138
  ```python
130
139
  import asyncio
131
140
  import uuid
132
- from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, Topic
141
+ from h_message_bus import NatsConfig, NatsPublisherAdapter, HaiMessage, NatsClientRepository
133
142
 
134
143
  async def main():
144
+ # Configure NATS connection
135
145
  config = NatsConfig(server="nats://localhost:4222")
136
- publisher = NatsPublisherAdapter(config)
137
-
146
+
147
+ # Create NATS client repository
148
+ client_repository = NatsClientRepository(config)
149
+
150
+ # Create publisher adapter
151
+ publisher = NatsPublisherAdapter(client_repository)
152
+
138
153
  # Connect to NATS
139
- await publisher.connect()
140
-
154
+ await client_repository.connect()
155
+
141
156
  request_message = HaiMessage(
142
- message_id=str(uuid.uuid4()),
143
- sender="client-service",
144
- topic=Topic.AI_VECTORS_QUERY,
157
+ id=str(uuid.uuid4()),
158
+ topic="hai.ai.vectors.query",
145
159
  payload={"query": "find similar documents", "limit": 10}
146
160
  )
147
-
161
+
148
162
  # Send request and wait for response (with timeout)
149
163
  response = await publisher.request(request_message, timeout=5.0)
150
-
164
+
151
165
  if response:
152
166
  print(f"Received response: {response.payload}")
153
167
  else:
154
168
  print("Request timed out")
155
-
156
- await publisher.close()
169
+
170
+ # Clean up
171
+ await client_repository.close()
157
172
 
158
173
  if __name__ == "__main__":
159
174
  asyncio.run(main())
160
175
  ```
161
176
 
162
- ### Creating a Service with Request Handler
177
+ ### Creating a Service for Request-Response
163
178
 
164
179
  ```python
165
180
  import asyncio
166
181
  import uuid
167
- from h_message_bus import NatsConfig, NatsSubscriberAdapter, NatsPublisherAdapter, HaiMessage, Topic
168
-
169
- async def request_handler(request: HaiMessage):
170
- print(f"Received request: {request.message_id}")
171
- print(f"Payload: {request.payload}")
172
-
173
- # Process the request
174
- result = {"status": "success", "data": {"result": 42}}
175
-
176
- # Create a response message
177
- return HaiMessage(
178
- message_id=str(uuid.uuid4()),
179
- sender="service-b",
180
- topic=f"{request.topic}.response",
181
- payload=result,
182
- correlation_id=request.message_id
183
- )
182
+ import json
183
+ from h_message_bus import NatsConfig, NatsSubscriberAdapter, NatsPublisherAdapter, HaiMessage, NatsClientRepository, MessageProcessor
184
+
185
+ class RequestResponseProcessor(MessageProcessor):
186
+ def __init__(self, publisher):
187
+ self.publisher = publisher
188
+
189
+ async def process(self, request: HaiMessage):
190
+ print(f"Received request: {request.id}")
191
+ print(f"Payload: {request.payload}")
192
+
193
+ # Process the request
194
+ result = {"status": "success", "data": {"result": 42}}
195
+
196
+ # Create a response message
197
+ response = HaiMessage(
198
+ id=str(uuid.uuid4()),
199
+ topic=f"{request.topic}.response",
200
+ payload=result
201
+ )
202
+
203
+ # Publish the response
204
+ await self.publisher.publish(response)
184
205
 
185
206
  async def main():
186
207
  # Configure NATS connection
187
208
  config = NatsConfig(server="nats://localhost:4222")
188
-
189
- # Create subscriber for handling requests
190
- subscriber = NatsSubscriberAdapter(config)
191
- publisher = NatsPublisherAdapter(config)
192
-
209
+
210
+ # Create NATS client repository
211
+ client_repository = NatsClientRepository(config)
212
+
193
213
  # Connect to NATS
194
- await subscriber.connect()
195
- await publisher.connect()
196
-
197
- # Register request handler for vector database queries
198
- await subscriber.subscribe_with_response(Topic.AI_VECTORS_QUERY, request_handler, publisher)
199
-
214
+ await client_repository.connect()
215
+
216
+ # Create publisher and subscriber
217
+ publisher = NatsPublisherAdapter(client_repository)
218
+ subscriber = NatsSubscriberAdapter(client_repository)
219
+
220
+ # Create request processor that uses the publisher
221
+ processor = RequestResponseProcessor(publisher)
222
+
223
+ # Subscribe to the request topic
224
+ await subscriber.subscribe("hai.ai.vectors.query", processor)
225
+
200
226
  # Keep the application running
201
227
  try:
202
228
  print("Service running. Press Ctrl+C to exit.")
@@ -204,8 +230,7 @@ async def main():
204
230
  await asyncio.sleep(1)
205
231
  except KeyboardInterrupt:
206
232
  # Clean up
207
- await subscriber.close()
208
- await publisher.close()
233
+ await client_repository.close()
209
234
 
210
235
  if __name__ == "__main__":
211
236
  asyncio.run(main())
@@ -223,7 +248,6 @@ The `NatsConfig` class allows you to customize your NATS connection:
223
248
  | `connection_timeout` | Connection timeout (seconds) | 2 |
224
249
  | `ping_interval` | Interval for ping frames (seconds) | 20 |
225
250
  | `max_outstanding_pings` | Maximum unanswered pings before disconnect | 5 |
226
- | `max_payload` | Maximum size of the payload in bytes | 1048576 (1MB) |
227
251
 
228
252
  ## API Reference
229
253
 
@@ -237,7 +261,6 @@ The following classes are exported directly from the package:
237
261
  - `NatsSubscriberAdapter` - Adapter for subscribing to messages
238
262
  - `MessageProcessor` - Processing incoming messages
239
263
  - `NatsClientRepository` - Low-level NATS client operations
240
- - `Topic` - Enumeration of predefined topic strings
241
264
 
242
265
  ### HaiMessage Structure
243
266
 
@@ -245,12 +268,10 @@ The `HaiMessage` class is the core data structure used for all messaging:
245
268
 
246
269
  ```python
247
270
  class HaiMessage:
248
- message_id: str # Unique identifier for the message
249
- sender: str # Identifier of the sender
271
+ id: str # Unique identifier for the message
250
272
  topic: str # The topic or channel for the message
251
273
  payload: dict # Actual message data
252
- correlation_id: str = None # Optional reference to a related message
253
- timestamp: float = None # Optional message creation timestamp
274
+ timestamp: str = None # Optional message creation timestamp (ISO format)
254
275
  ```
255
276
 
256
277
  ## Contributing
@@ -259,4 +280,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
259
280
 
260
281
  ## License
261
282
 
262
- This project is licensed under the MIT License - see the LICENSE file for details.
283
+ This project is licensed under the MIT License - see the LICENSE file for details.
@@ -4,7 +4,7 @@
4
4
 
5
5
  [project]
6
6
  name = "h_message_bus"
7
- version = "0.0.40"
7
+ version = "0.0.41"
8
8
  authors = [
9
9
  {name = "shoebill", email = "shoebill.hai@gmail.com"},
10
10
  ]
@@ -60,7 +60,7 @@ class NatsClientRepository:
60
60
  raise ConnectionError("Could not establish connection to NATS server.")
61
61
 
62
62
  async def publish(self, subject: str, payload: bytes, retries: int = 3) -> None:
63
- """Publish raw message to NATS with retries and backoff."""
63
+ """Publish a raw message to NATS with retries and backoff."""
64
64
  for attempt in range(retries):
65
65
  try:
66
66
  await self._ensure_connected()
@@ -83,7 +83,7 @@ class NatsClientRepository:
83
83
  return None
84
84
 
85
85
  async def request(self, subject: str, payload: bytes, timeout: float = 2.0, retries: int = 3) -> Optional[bytes]:
86
- """Send a request and get raw response with retries and backoff."""
86
+ """Send a request and get a raw response with retries and backoff."""
87
87
  for attempt in range(retries):
88
88
  try:
89
89
  await self._ensure_connected()
@@ -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