h-message-bus 0.0.39__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.
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/PKG-INFO +106 -85
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/README.md +106 -85
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/pyproject.toml +1 -1
- h_message_bus-0.0.41/src/h_message_bus/infrastructure/nats_client_repository.py +109 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/infrastructure/nats_config.py +1 -1
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/PKG-INFO +106 -85
- h_message_bus-0.0.41/src/h_message_bus.egg-info/SOURCES.txt +23 -0
- h_message_bus-0.0.39/src/h_message_bus/domain/models/event_message_topic.py +0 -20
- h_message_bus-0.0.39/src/h_message_bus/domain/models/request_message_topic.py +0 -100
- h_message_bus-0.0.39/src/h_message_bus/domain/models/twitter_message_metadata.py +0 -31
- h_message_bus-0.0.39/src/h_message_bus/domain/models/twitter_user_metadata.py +0 -76
- h_message_bus-0.0.39/src/h_message_bus/domain/models/vector_collection_metadata.py +0 -30
- h_message_bus-0.0.39/src/h_message_bus/domain/models/vector_query_answer.py +0 -22
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/__init__.py +0 -0
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_clear_request_message.py +0 -26
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_get_all_request_message.py +0 -27
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_get_all_result_response_message.py +0 -45
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_add_request_message.py +0 -64
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_added_response_message.py +0 -69
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_get_request_message.py +0 -39
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_get_result_response_message.py +0 -70
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_update_request_message.py +0 -56
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_node_update_response_message.py +0 -69
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_nodes_by_property_request_message.py +0 -45
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_nodes_by_property_response_message.py +0 -80
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_query_operation_request_message.py +0 -114
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_query_operation_response_message.py +0 -85
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_query_request_message.py +0 -48
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/graph_relationship_added_request_message.py +0 -63
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/init_knowledgebase_request.py +0 -26
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/tg_message_request_message.py +0 -48
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/tg_user_message_reply_request_message.py +0 -56
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/tg_user_message_request_message.py +0 -93
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_follow_user_request_message.py +0 -40
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_follow_user_response_message.py +0 -43
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_tweet_request_message.py +0 -40
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_tweet_response_message.py +0 -54
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_user_request_message.py +0 -35
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_user_response_message.py +0 -96
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_users_request_message.py +0 -42
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_get_users_response_message.py +0 -44
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_home_timeline_request_message.py +0 -56
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_home_timeline_response_message.py +0 -74
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_post_tweet_request_message.py +0 -40
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_post_tweet_response_message.py +0 -47
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_quote_retweet_request_message.py +0 -48
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_quote_retweet_response_message.py +0 -55
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_reply_request_message.py +0 -48
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_reply_response_message.py +0 -54
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_retweet_request_message.py +0 -40
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_retweet_response_message.py +0 -43
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_search_request_message.py +0 -73
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_search_response_message.py +0 -73
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_user_tweets_request_message.py +0 -63
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/twitter_user_tweets_response_message.py +0 -89
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/vector_query_collection_request_message.py +0 -51
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/vector_query_collection_response_message.py +0 -47
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/vector_read_metadata_request_message.py +0 -25
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/vector_read_metadata_response_message.py +0 -45
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/vector_save_request_message.py +0 -66
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/web_get_docs_request_message.py +0 -46
- h_message_bus-0.0.39/src/h_message_bus/domain/request_messages/web_search_request_message.py +0 -32
- h_message_bus-0.0.39/src/h_message_bus/infrastructure/__init__.py +0 -0
- h_message_bus-0.0.39/src/h_message_bus/infrastructure/nats_client_repository.py +0 -93
- h_message_bus-0.0.39/src/h_message_bus.egg-info/SOURCES.txt +0 -79
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/setup.cfg +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/adapters/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_message_adapter.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_publisher_adapter.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/adapters/nats_subscriber_adapter.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/application/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/application/message_processor.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/application/message_publisher.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/application/message_subcriber.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/domain/__init__.py +0 -0
- {h_message_bus-0.0.39/src/h_message_bus/domain/event_messages → h_message_bus-0.0.41/src/h_message_bus/domain/models}/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus/domain/models/hai_message.py +0 -0
- {h_message_bus-0.0.39/src/h_message_bus/domain/models → h_message_bus-0.0.41/src/h_message_bus/infrastructure}/__init__.py +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/dependency_links.txt +0 -0
- {h_message_bus-0.0.39 → h_message_bus-0.0.41}/src/h_message_bus.egg-info/requires.txt +0 -0
- {h_message_bus-0.0.39 → 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.
|
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
|
@@ -26,18 +26,18 @@ pip install h_message_bus
|
|
26
26
|
|
27
27
|
## Topics
|
28
28
|
|
29
|
-
H Message Bus
|
29
|
+
H Message Bus uses topic strings for message routing. It's recommended to follow the convention: `hai.[source].[destination].[action]`
|
30
30
|
|
31
|
-
|
31
|
+
Example topics:
|
32
32
|
|
33
|
-
| Topic
|
34
|
-
|
35
|
-
| `
|
36
|
-
| `
|
37
|
-
| `
|
38
|
-
| `
|
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
|
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,
|
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(
|
65
|
-
|
67
|
+
publisher = NatsPublisherAdapter(client_repository)
|
68
|
+
|
66
69
|
# Connect to NATS
|
67
|
-
await
|
68
|
-
|
69
|
-
# Create and publish a message
|
70
|
+
await client_repository.connect()
|
71
|
+
|
72
|
+
# Create and publish a message
|
70
73
|
message = HaiMessage(
|
71
|
-
|
72
|
-
|
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,
|
93
|
+
from h_message_bus import NatsConfig, NatsSubscriberAdapter, HaiMessage, NatsClientRepository, MessageProcessor
|
92
94
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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(
|
105
|
-
|
110
|
+
subscriber = NatsSubscriberAdapter(client_repository)
|
111
|
+
|
106
112
|
# Connect to NATS
|
107
|
-
await
|
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(
|
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
|
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,
|
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
|
-
|
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
|
140
|
-
|
154
|
+
await client_repository.connect()
|
155
|
+
|
141
156
|
request_message = HaiMessage(
|
142
|
-
|
143
|
-
|
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
|
-
|
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
|
177
|
+
### Creating a Service for Request-Response
|
163
178
|
|
164
179
|
```python
|
165
180
|
import asyncio
|
166
181
|
import uuid
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
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
|
190
|
-
|
191
|
-
|
192
|
-
|
209
|
+
|
210
|
+
# Create NATS client repository
|
211
|
+
client_repository = NatsClientRepository(config)
|
212
|
+
|
193
213
|
# Connect to NATS
|
194
|
-
await
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
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
|
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
|
-
|
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
|
-
|
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.
|