Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion priompt_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,21 @@
from priompt_sdk.v1 import prompt_pb2, prompt_pb2_grpc


_TOKEN_SAFE = frozenset(
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
)


def _subject(uri):
return "priompt." + uri.removeprefix("priompt://").replace("/", ".")
"""Map a prompt URI to the server's NATS subject (pubsub.Subject): one
token per URI segment, every byte outside [A-Za-z0-9_-] percent-encoded
with uppercase hex, so "priompt://acme.corp/x/a.b" ->
"priompt.acme%2Ecorp.x.a%2Eb". Dot-free URIs map as before."""
segs = uri.removeprefix("priompt://").split("/")
return "priompt." + ".".join(
"".join(chr(c) if c in _TOKEN_SAFE else f"%{c:02X}" for c in s.encode())
for s in segs
)


def _parse_url(raw):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,28 @@ def make_client(cache_ttl=0):
("priompt://a", "priompt.a"),
("team/agent", "priompt.team.agent"), # no scheme prefix
("priompt://a/b/", "priompt.a.b."), # trailing slash preserved
# Server's encoding (priompt pubsub.Subject): bytes outside [A-Za-z0-9_-]
# are percent-encoded per segment, uppercase hex.
("priompt://acme/support/agent-v2_x", "priompt.acme.support.agent-v2_x"),
("priompt://acme.corp/x/a.b", "priompt.acme%2Ecorp.x.a%2Eb"),
("priompt://a/*/>", "priompt.a.%2A.%3E"), # no wildcards
("priompt://a/caf茅", "priompt.a.caf%C3%A9"), # UTF-8 bytes
])
def test_subject(uri, expected):
assert _subject(uri) == expected


@pytest.mark.parametrize("a,b", [
# The server's collision cases (TestSubjectIsInjective): distinct URIs
# must get distinct subjects even when a segment contains a dot.
("priompt://acme/a.b", "priompt://acme/a/b"),
("priompt://acme/corp/x", "priompt://acme.corp/x"),
("priompt://acme/a.b/c", "priompt://acme/a/b.c"),
])
def test_subject_is_injective(a, b):
assert _subject(a) != _subject(b)


# --- caching happy path ---------------------------------------------------

def test_get_caches_within_ttl(clock):
Expand Down
Loading