airprompter-datastore-redis 0.3.0__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.
@@ -0,0 +1,6 @@
1
+ __pycache__/
2
+ *.pyc
3
+ .pytest_cache/
4
+ *.egg-info/
5
+ dist/
6
+ build/
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, AirPrompter
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.5
2
+ Name: airprompter-datastore-redis
3
+ Version: 0.3.0
4
+ Summary: The AirPrompter release datastore over Redis or Valkey: a KvStore with atomic Lua conditional writes and a sorted-set index, for kv_release_datastore. Bring your own client (redis-py).
5
+ Project-URL: Homepage, https://github.com/airprompter/airprompter-agent-sdk
6
+ Project-URL: Source, https://github.com/airprompter/airprompter-agent-sdk/tree/main/sdk-python/packages/datastore-redis
7
+ Project-URL: Protocol, https://github.com/airprompter/airprompter-agent-sdk/blob/main/protocol/datastore-format.md
8
+ Project-URL: Changelog, https://github.com/airprompter/airprompter-agent-sdk/blob/main/CHANGELOG.md
9
+ Project-URL: Issues, https://github.com/airprompter/airprompter-agent-sdk/issues
10
+ Author: AirPrompter
11
+ License-Expression: BSD-3-Clause
12
+ License-File: LICENSE
13
+ Keywords: agent,airprompter,datastore,llm,prompts,redis,valkey
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Topic :: Software Development :: Libraries
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: airprompter-agent-sync==0.3.0
25
+ Provides-Extra: redis
26
+ Requires-Dist: redis>=5; extra == 'redis'
27
+ Description-Content-Type: text/markdown
28
+
29
+ # airprompter-datastore-redis
30
+
31
+ The AirPrompter release datastore over Redis (6.2+) or Valkey: a `KvStore`
32
+ for `kv_release_datastore` (`airprompter-agent-sync`)
33
+ ([docs/datastore.md](https://github.com/airprompter/airprompter-agent-sdk/blob/main/docs/datastore.md)).
34
+ The keys, hashes, index and Lua scripts are `@airprompter/datastore-redis`'s,
35
+ so Python and TypeScript share one namespace.
36
+
37
+ ```python
38
+ import redis
39
+ from airprompter_agent import kv_release_datastore
40
+ from airprompter_datastore_redis import redis_kv_store
41
+
42
+ releases = kv_release_datastore(redis_kv_store(client=redis.Redis.from_url(os.environ["REDIS_URL"])))
43
+ ```
44
+
45
+ - `client` is anything with `execute_command(*args)`; bytes or decoded replies both work.
46
+ - Each conditional write is one atomic `EVAL`; `list` is an exact range over
47
+ a sorted-set index (never a `SCAN` glob); every key is `{namespace}:…`, one
48
+ cluster slot.
49
+ - Durability is Redis's: run with AOF, or accept that a restart loses the
50
+ rows (runtimes keep serving; the next pull writes the release again).
@@ -0,0 +1,22 @@
1
+ # airprompter-datastore-redis
2
+
3
+ The AirPrompter release datastore over Redis (6.2+) or Valkey: a `KvStore`
4
+ for `kv_release_datastore` (`airprompter-agent-sync`)
5
+ ([docs/datastore.md](https://github.com/airprompter/airprompter-agent-sdk/blob/main/docs/datastore.md)).
6
+ The keys, hashes, index and Lua scripts are `@airprompter/datastore-redis`'s,
7
+ so Python and TypeScript share one namespace.
8
+
9
+ ```python
10
+ import redis
11
+ from airprompter_agent import kv_release_datastore
12
+ from airprompter_datastore_redis import redis_kv_store
13
+
14
+ releases = kv_release_datastore(redis_kv_store(client=redis.Redis.from_url(os.environ["REDIS_URL"])))
15
+ ```
16
+
17
+ - `client` is anything with `execute_command(*args)`; bytes or decoded replies both work.
18
+ - Each conditional write is one atomic `EVAL`; `list` is an exact range over
19
+ a sorted-set index (never a `SCAN` glob); every key is `{namespace}:…`, one
20
+ cluster slot.
21
+ - Durability is Redis's: run with AOF, or accept that a restart loses the
22
+ rows (runtimes keep serving; the next pull writes the release again).
@@ -0,0 +1,44 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.25"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "airprompter-datastore-redis"
7
+ version = "0.3.0"
8
+ description = "The AirPrompter release datastore over Redis or Valkey: a KvStore with atomic Lua conditional writes and a sorted-set index, for kv_release_datastore. Bring your own client (redis-py)."
9
+ readme = "README.md"
10
+ license = "BSD-3-Clause"
11
+ requires-python = ">=3.10"
12
+ authors = [{ name = "AirPrompter" }]
13
+ keywords = ["airprompter", "prompts", "llm", "agent", "datastore", "redis", "valkey"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Operating System :: OS Independent",
23
+ "Topic :: Software Development :: Libraries",
24
+ ]
25
+ # The client library is the application's: the adapter imports none, and the extras below only name a known-good one.
26
+ dependencies = [
27
+ "airprompter-agent-sync==0.3.0",
28
+ ]
29
+
30
+ [project.optional-dependencies]
31
+ redis = ["redis>=5"]
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/airprompter/airprompter-agent-sdk"
35
+ Source = "https://github.com/airprompter/airprompter-agent-sdk/tree/main/sdk-python/packages/datastore-redis"
36
+ Protocol = "https://github.com/airprompter/airprompter-agent-sdk/blob/main/protocol/datastore-format.md"
37
+ Changelog = "https://github.com/airprompter/airprompter-agent-sdk/blob/main/CHANGELOG.md"
38
+ Issues = "https://github.com/airprompter/airprompter-agent-sdk/issues"
39
+
40
+ [tool.hatch.build.targets.wheel]
41
+ packages = ["src/airprompter_datastore_redis"]
42
+
43
+ [tool.hatch.build.targets.sdist]
44
+ include = ["src/airprompter_datastore_redis", "README.md"]
@@ -0,0 +1,93 @@
1
+ """``airprompter-datastore-redis`` — the release datastore's ``KvStore`` over Redis or Valkey (T40,
2
+ ``protocol/datastore-format.md``). The same keys, hashes, index and Lua scripts as ``@airprompter/datastore-redis``, so
3
+ a Python puller and a TypeScript runtime share one namespace. No client is bundled: hand in anything with
4
+ ``execute_command(*args)`` (``redis.Redis``, ``redis.cluster.RedisCluster``, Valkey's client). Each key is a hash
5
+ (``value``, ``version``); a sorted set indexes the keys so ``list`` is an exact prefix range; every key carries one
6
+ hash tag (``{namespace}``), so a cluster keeps them in one slot. Redis 6.2 or later.
7
+
8
+ Example::
9
+
10
+ import redis
11
+ from airprompter_agent import kv_release_datastore
12
+ from airprompter_datastore_redis import redis_kv_store
13
+
14
+ releases = kv_release_datastore(redis_kv_store(client=redis.Redis.from_url(os.environ["REDIS_URL"])))
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import re
20
+ from typing import Any, Optional
21
+
22
+ from airprompter_agent_sync.store.kv_store import KvEntry, check_condition
23
+
24
+ __all__ = ["RedisKvStore", "redis_kv_store"]
25
+
26
+ # The same scripts, byte for byte, as the TypeScript adapter. KEYS[1] the entry hash, KEYS[2] the index;
27
+ # ARGV[1] the value, ARGV[2] the logical key, ARGV[3] the version read.
28
+ PUT_IF_ABSENT = """if redis.call('EXISTS', KEYS[1]) == 1 then return 0 end
29
+ redis.call('HSET', KEYS[1], 'value', ARGV[1], 'version', '1')
30
+ redis.call('ZADD', KEYS[2], 0, ARGV[2])
31
+ return 1"""
32
+ PUT_IF_VERSION = """local current = redis.call('HGET', KEYS[1], 'version')
33
+ if not current or current ~= ARGV[3] then return 0 end
34
+ redis.call('HSET', KEYS[1], 'value', ARGV[1], 'version', tostring(tonumber(current) + 1))
35
+ return 1"""
36
+ DELETE = """redis.call('DEL', KEYS[1])
37
+ redis.call('ZREM', KEYS[2], ARGV[1])
38
+ return 1"""
39
+
40
+
41
+ def _text(reply: Any) -> Optional[str]:
42
+ if reply is None:
43
+ return None
44
+ if isinstance(reply, (bytes, bytearray)):
45
+ return bytes(reply).decode("utf-8")
46
+ return str(reply)
47
+
48
+
49
+ class RedisKvStore:
50
+ def __init__(self, *, client: Any, namespace: str = "airprompter", list_batch: int = 500) -> None:
51
+ if not re.fullmatch(r"[A-Za-z0-9_.:-]{1,64}", namespace):
52
+ raise ValueError(f"not a namespace: {namespace!r}")
53
+ self._client = client
54
+ self._tag = "{" + namespace + "}"
55
+ self._index = f"{self._tag}:index"
56
+ self._batch = max(1, list_batch)
57
+
58
+ def _entry(self, key: str) -> str:
59
+ return f"{self._tag}:v:{key}"
60
+
61
+ def get(self, key: str) -> Optional[KvEntry]:
62
+ reply = self._client.execute_command("HMGET", self._entry(key), "value", "version") or [None, None]
63
+ value, version = _text(reply[0]), _text(reply[1])
64
+ return None if value is None or version is None else KvEntry(value, version)
65
+
66
+ def put(self, key: str, value: str, *, if_absent: bool = False, if_version: Optional[str] = None) -> bool:
67
+ check_condition(if_absent, if_version)
68
+ if if_absent:
69
+ reply = self._client.execute_command("EVAL", PUT_IF_ABSENT, 2, self._entry(key), self._index, value, key)
70
+ else:
71
+ reply = self._client.execute_command("EVAL", PUT_IF_VERSION, 2, self._entry(key), self._index, value, key, if_version)
72
+ return int(reply) == 1
73
+
74
+ def list(self, prefix: str) -> list[str]:
75
+ # The index is ordered byte-wise, so every key with the prefix sits in one run right after "[prefix".
76
+ keys: list[str] = []
77
+ offset = 0
78
+ while True:
79
+ page = [_text(member) or "" for member in self._client.execute_command("ZRANGEBYLEX", self._index, "-" if prefix == "" else f"[{prefix}", "+", "LIMIT", offset, self._batch) or []]
80
+ for key in page:
81
+ if not key.startswith(prefix):
82
+ return keys
83
+ keys.append(key)
84
+ if len(page) < self._batch:
85
+ return keys
86
+ offset += len(page)
87
+
88
+ def delete(self, key: str) -> None:
89
+ self._client.execute_command("EVAL", DELETE, 2, self._entry(key), self._index, key)
90
+
91
+
92
+ def redis_kv_store(*, client: Any, namespace: str = "airprompter", list_batch: int = 500) -> RedisKvStore:
93
+ return RedisKvStore(client=client, namespace=namespace, list_batch=list_batch)