rusticai-redis 0.0.1__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.
- rusticai_redis-0.0.1/PKG-INFO +35 -0
- rusticai_redis-0.0.1/README.md +16 -0
- rusticai_redis-0.0.1/pyproject.toml +65 -0
- rusticai_redis-0.0.1/src/rustic_ai/redis/__init__.py +0 -0
- rusticai_redis-0.0.1/src/rustic_ai/redis/agent_ext/__init__.py +0 -0
- rusticai_redis-0.0.1/src/rustic_ai/redis/agent_ext/redis_kvstore.py +33 -0
- rusticai_redis-0.0.1/src/rustic_ai/redis/messaging/__init__.py +0 -0
- rusticai_redis-0.0.1/src/rustic_ai/redis/messaging/backend.py +186 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: rusticai-redis
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Redis package for Rustic AI
|
|
5
|
+
Home-page: https://www.rustic.ai/
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Author: Dragonscale Industries Inc.
|
|
8
|
+
Author-email: dev@dragonscale.ai
|
|
9
|
+
Requires-Python: >=3.12,<4.0
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Dist: redis[hiredis] (>=5.2.1,<6.0.0)
|
|
15
|
+
Requires-Dist: rusticai-core (>=0.0.2,<0.1.0)
|
|
16
|
+
Project-URL: Repository, https://github.com/rustic-ai/python-framework
|
|
17
|
+
Project-URL: Rustic AI Core, https://pypi.org/project/rusticai-core/
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# Rustic AI Redis
|
|
21
|
+
|
|
22
|
+
It provides a Redis based implementations of [rusticai-core](https://pypi.org/project/rusticai-core/)'s messaging backend, key value store, etc.
|
|
23
|
+
|
|
24
|
+
## Installing
|
|
25
|
+
|
|
26
|
+
```shell
|
|
27
|
+
pip install rusticai-redis
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Building from Source
|
|
31
|
+
|
|
32
|
+
```shell
|
|
33
|
+
poetry install --with dev
|
|
34
|
+
poetry build
|
|
35
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Rustic AI Redis
|
|
2
|
+
|
|
3
|
+
It provides a Redis based implementations of [rusticai-core](https://pypi.org/project/rusticai-core/)'s messaging backend, key value store, etc.
|
|
4
|
+
|
|
5
|
+
## Installing
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
pip install rusticai-redis
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Building from Source
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
poetry install --with dev
|
|
15
|
+
poetry build
|
|
16
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "rusticai-redis"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Redis package for Rustic AI"
|
|
9
|
+
authors = ["Dragonscale Industries Inc. <dev@dragonscale.ai>"]
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
homepage = "https://www.rustic.ai/"
|
|
13
|
+
repository = "https://github.com/rustic-ai/python-framework"
|
|
14
|
+
packages = [{ include = "rustic_ai", from = "src" }]
|
|
15
|
+
|
|
16
|
+
[tool.poetry.urls]
|
|
17
|
+
"Rustic AI Core" = "https://pypi.org/project/rusticai-core/"
|
|
18
|
+
|
|
19
|
+
[tool.poetry.dependencies]
|
|
20
|
+
python = "^3.12"
|
|
21
|
+
rusticai-core = { version = "0.0.2"}
|
|
22
|
+
redis = { extras = ["hiredis"], version = "^5.2.1" }
|
|
23
|
+
|
|
24
|
+
[tool.poetry-monorepo.deps]
|
|
25
|
+
|
|
26
|
+
[tool.poetry.group.dev.dependencies]
|
|
27
|
+
pytest = "^8.3.4"
|
|
28
|
+
black = { version = "^24.2.0", extras = ["jupyter"] }
|
|
29
|
+
flake8 = "^7.1.2"
|
|
30
|
+
tox = "^4.24.1"
|
|
31
|
+
isort = "^6.0.0"
|
|
32
|
+
mypy = "^1.15.0"
|
|
33
|
+
fakeredis = "^2.27.0"
|
|
34
|
+
types-redis = "^4.6.0.20241004"
|
|
35
|
+
|
|
36
|
+
[tool.black]
|
|
37
|
+
line-length = 120
|
|
38
|
+
target-version = ['py312']
|
|
39
|
+
include = '\.pyi?$'
|
|
40
|
+
exclude = '''
|
|
41
|
+
/(
|
|
42
|
+
\.git
|
|
43
|
+
| \.mypy_cache
|
|
44
|
+
| \.tox
|
|
45
|
+
| \.venv
|
|
46
|
+
| _build
|
|
47
|
+
| buck-out
|
|
48
|
+
| build
|
|
49
|
+
| dist
|
|
50
|
+
)/
|
|
51
|
+
'''
|
|
52
|
+
|
|
53
|
+
[tool.mypy]
|
|
54
|
+
python_version = "3.12"
|
|
55
|
+
ignore_missing_imports = true
|
|
56
|
+
check_untyped_defs = true
|
|
57
|
+
plugins = "pydantic.mypy"
|
|
58
|
+
explicit_package_bases = true
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
[tool.isort]
|
|
62
|
+
profile = "black"
|
|
63
|
+
known_third_party = ["pydantic"]
|
|
64
|
+
known_first_party = ["rustic_ai.core", "rustic_ai.redis"]
|
|
65
|
+
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import redis
|
|
4
|
+
from rustic_ai.core.guild.agent_ext.depends.dependency_resolver import (
|
|
5
|
+
DependencyResolver,
|
|
6
|
+
)
|
|
7
|
+
from rustic_ai.core.guild.agent_ext.depends.kvstore.base import BaseKVStore
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RedisKVStore(BaseKVStore):
|
|
11
|
+
def __init__(self, redis_client: redis.StrictRedis, guild_id: str, agent_id: str):
|
|
12
|
+
self.store = redis_client
|
|
13
|
+
self.guild_id = guild_id
|
|
14
|
+
self.agent_id = agent_id
|
|
15
|
+
|
|
16
|
+
def get(self, key: str):
|
|
17
|
+
value = self.store.hget(f"{self.guild_id}_{self.agent_id}", key)
|
|
18
|
+
return json.loads(value) if value else None
|
|
19
|
+
|
|
20
|
+
def set(self, key: str, value):
|
|
21
|
+
self.store.hset(f"{self.guild_id}_{self.agent_id}", mapping={key: json.dumps(value)})
|
|
22
|
+
|
|
23
|
+
def delete(self, key: str):
|
|
24
|
+
self.store.hdel(f"{self.guild_id}_{self.agent_id}", key)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RedisKVStoreResolver(DependencyResolver[RedisKVStore]):
|
|
28
|
+
def __init__(self, host: str, port: int) -> None:
|
|
29
|
+
super().__init__()
|
|
30
|
+
self.redis_client = redis.StrictRedis(host=host, port=port)
|
|
31
|
+
|
|
32
|
+
def resolve(self, guild_id: str, agent_id: str) -> RedisKVStore:
|
|
33
|
+
return RedisKVStore(redis_client=self.redis_client, guild_id=guild_id, agent_id=agent_id)
|
|
File without changes
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Callable, Dict, List, Optional, Set, Union
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel, Field
|
|
5
|
+
|
|
6
|
+
import redis
|
|
7
|
+
from rustic_ai.core.messaging.core.message import Message
|
|
8
|
+
from rustic_ai.core.messaging.core.messaging_backend import MessagingBackend
|
|
9
|
+
from rustic_ai.core.utils import GemstoneID
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class RedisBackendConfig(BaseModel):
|
|
13
|
+
host: str
|
|
14
|
+
port: int
|
|
15
|
+
username: Optional[str] = Field(default=None)
|
|
16
|
+
password: Optional[str] = Field(default=None)
|
|
17
|
+
ssl: bool = Field(default=False)
|
|
18
|
+
ssl_certfile: Optional[str] = Field(default=None)
|
|
19
|
+
ssl_keyfile: Optional[str] = Field(default=None)
|
|
20
|
+
ssl_ca_certs: Optional[str] = Field(default=None)
|
|
21
|
+
is_cluster: bool = Field(default=False)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RedisMessagingBackend(MessagingBackend):
|
|
25
|
+
def __init__(self, redis_client: Union[redis.StrictRedis, RedisBackendConfig, dict]) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Initialize with a Redis client.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
redis_client: A Redis client instance.
|
|
31
|
+
"""
|
|
32
|
+
self.r: redis.StrictRedis | redis.RedisCluster
|
|
33
|
+
if isinstance(redis_client, dict):
|
|
34
|
+
redis_client = RedisBackendConfig(**redis_client)
|
|
35
|
+
|
|
36
|
+
if isinstance(redis_client, RedisBackendConfig):
|
|
37
|
+
if redis_client.is_cluster:
|
|
38
|
+
self.r = redis.RedisCluster(
|
|
39
|
+
host=redis_client.host,
|
|
40
|
+
port=redis_client.port,
|
|
41
|
+
username=redis_client.username,
|
|
42
|
+
password=redis_client.password,
|
|
43
|
+
ssl=redis_client.ssl,
|
|
44
|
+
ssl_certfile=redis_client.ssl_certfile,
|
|
45
|
+
ssl_keyfile=redis_client.ssl_keyfile,
|
|
46
|
+
ssl_ca_certs=redis_client.ssl_ca_certs,
|
|
47
|
+
)
|
|
48
|
+
else:
|
|
49
|
+
self.r = redis.StrictRedis(
|
|
50
|
+
host=redis_client.host,
|
|
51
|
+
port=redis_client.port,
|
|
52
|
+
username=redis_client.username,
|
|
53
|
+
password=redis_client.password,
|
|
54
|
+
ssl=redis_client.ssl,
|
|
55
|
+
ssl_certfile=redis_client.ssl_certfile,
|
|
56
|
+
ssl_keyfile=redis_client.ssl_keyfile,
|
|
57
|
+
ssl_ca_certs=redis_client.ssl_ca_certs,
|
|
58
|
+
)
|
|
59
|
+
elif isinstance(redis_client, redis.StrictRedis):
|
|
60
|
+
self.r = redis_client
|
|
61
|
+
else:
|
|
62
|
+
raise ValueError("Invalid Redis client") # pragma: no cover
|
|
63
|
+
|
|
64
|
+
self.p = self.r.pubsub(ignore_subscribe_messages=True)
|
|
65
|
+
self.redis_thread = self.p.run_in_thread(sleep_time=0.001, daemon=True)
|
|
66
|
+
|
|
67
|
+
def store_message(self, topic: str, message: Message) -> None:
|
|
68
|
+
"""
|
|
69
|
+
Store a message in Redis, sorted by timestamp.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
message (Message): The message object to be stored.
|
|
73
|
+
"""
|
|
74
|
+
# Using the timestamp as the score for sorting in Redis sorted set.
|
|
75
|
+
self.r.zadd(topic, {message.to_json(): message.timestamp})
|
|
76
|
+
self.r.publish(topic, message.to_json())
|
|
77
|
+
|
|
78
|
+
def get_messages_for_topic(self, topic: str) -> List[Message]:
|
|
79
|
+
"""
|
|
80
|
+
Retrieve all messages for a given topic.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
topic (str): The topic to retrieve messages for.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
List[Message]: A list of messages for the given topic.
|
|
87
|
+
"""
|
|
88
|
+
raw_messages = self.r.zrange(topic, 0, -1)
|
|
89
|
+
messages = [Message.from_json(raw_message) for raw_message in raw_messages]
|
|
90
|
+
return sorted(messages, key=lambda msg: msg.id)
|
|
91
|
+
|
|
92
|
+
def get_messages_for_topic_since(self, topic: str, msg_id_since: int) -> List[Message]:
|
|
93
|
+
"""
|
|
94
|
+
Retrieve all messages for a given topic since a given message ID.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
topic (str): The topic to retrieve messages for.
|
|
98
|
+
msg_id_since (int): The ID of the message since which to retrieve messages.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
List[Message]: A list of messages for the given topic since the given message ID.
|
|
102
|
+
"""
|
|
103
|
+
# Retrieve the timestamp corresponding to the given message ID.
|
|
104
|
+
timestamp_since = self._get_timestamp_for_id(msg_id_since) + 1
|
|
105
|
+
raw_messages = self.r.zrangebyscore(topic, timestamp_since, "+inf")
|
|
106
|
+
messages = [Message.from_json(raw_message) for raw_message in raw_messages]
|
|
107
|
+
return sorted(messages, key=lambda msg: msg.id)
|
|
108
|
+
|
|
109
|
+
def get_next_message_for_topic_since(self, topic: str, last_message_id: int) -> Optional[Message]:
|
|
110
|
+
"""
|
|
111
|
+
Retrieve the next message for a given topic since a given message ID.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
topic (str): The topic to retrieve messages for.
|
|
115
|
+
last_message_id (int): The ID of the last message received.
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
Optional[Message]: The next message for the given topic since the given message ID.
|
|
119
|
+
"""
|
|
120
|
+
timestamp_since = self._get_timestamp_for_id(last_message_id) + 1
|
|
121
|
+
raw_messages = self.r.zrangebyscore(topic, timestamp_since, "+inf", start=0, num=1)
|
|
122
|
+
return Message.from_json(raw_messages[0]) if raw_messages else None
|
|
123
|
+
|
|
124
|
+
def load_subscribers(self, namespace: str) -> Dict[str, Set[str]]:
|
|
125
|
+
"""
|
|
126
|
+
Load all subscribers from Redis.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
Dict[str, Set[str]]: A dictionary mapping topics to sets of client IDs.
|
|
130
|
+
"""
|
|
131
|
+
# TBD: Implement the logic to load subscribers.
|
|
132
|
+
return {}
|
|
133
|
+
|
|
134
|
+
def _get_timestamp_for_id(self, msg_id: int) -> float:
|
|
135
|
+
"""
|
|
136
|
+
Helper method to retrieve the timestamp for a given message ID.
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
msg_id (int): The ID of the message.
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
float: The timestamp of the message.
|
|
143
|
+
"""
|
|
144
|
+
# Implement logic to retrieve the timestamp from a message ID.
|
|
145
|
+
# This method needs to be adjusted based on how the timestamp is stored or derived from the message ID.
|
|
146
|
+
return GemstoneID.from_int(msg_id).timestamp
|
|
147
|
+
|
|
148
|
+
def subscribe(self, topic: str, handler: Callable[[Message], None]) -> None:
|
|
149
|
+
"""
|
|
150
|
+
Subscribe to a topic and handle incoming messages.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
topic (str): The topic to subscribe to.
|
|
154
|
+
handler (Callable[[Message], None]): The handler function to handle incoming messages.
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
def _handler(redis_message: Dict) -> None:
|
|
158
|
+
logging.debug(f"[RedisStorage] Received message: {redis_message}")
|
|
159
|
+
handler(Message.from_json(redis_message["data"]))
|
|
160
|
+
|
|
161
|
+
logging.debug(f"Subscribing to topic: {topic}")
|
|
162
|
+
|
|
163
|
+
self.p.subscribe(**{topic: _handler})
|
|
164
|
+
|
|
165
|
+
def unsubscribe(self, topic: str) -> None:
|
|
166
|
+
"""
|
|
167
|
+
Unsubscribe from a topic.
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
topic (str): The topic to unsubscribe from.
|
|
171
|
+
"""
|
|
172
|
+
logging.debug(f"Unsubscribing from topic: {topic}")
|
|
173
|
+
self.p.unsubscribe(topic)
|
|
174
|
+
logging.debug(f"Unsubscribed from topic: {topic}")
|
|
175
|
+
|
|
176
|
+
def cleanup(self) -> None:
|
|
177
|
+
"""
|
|
178
|
+
Clean up the Redis storage.
|
|
179
|
+
"""
|
|
180
|
+
self.redis_thread.stop()
|
|
181
|
+
self.redis_thread.join(timeout=1)
|
|
182
|
+
self.p.close()
|
|
183
|
+
self.r.close()
|
|
184
|
+
|
|
185
|
+
def supports_subscription(self) -> bool:
|
|
186
|
+
return True
|