SLIM-powered messaging
Project description
Pattern Agentic Messaging
An async SLIM wrapper with a FastAPI-like interface
Installation
pip install pattern_agentic_messaging
Server
Route messages to decorated handlers based on a discriminator field:
from pattern_agentic_messaging import PASlimApp, PASlimConfig
from .models import QuestionRequest, StatusRequest, AnswerResponse
config = PASlimConfig(
local_name="org/ns/server",
endpoint="https://slim.example.com",
auth_secret="shared-secret-at-least-32-bytes!",
message_discriminator="type",
)
app = PASlimApp(config)
@app.on_session_connect
async def on_connect(session):
session.context["agent"] = await create_agent(...)
@app.on_message
async def handle_prompt(session, msg: QuestionRequest):
agent = session.context["agent"]
response = await agent.ask(msg.question)
await session.send(AnswerResponse(answer=response))
@app.on_message
async def handle_status(session, msg: StatusRequest):
await session.send({"type": "status", "value": "ready"})
@app.on_message
async def handle_other(session, msg):
await session.send({"error": f"Unknown message type: {msg}"})
app.run()
Discriminator models are Pydantic models with a Literal field matching the discriminator:
from pydantic import BaseModel
from typing import Literal
class QuestionRequest(BaseModel):
type: Literal["question"] = "question"
prompt: str
class AnswerResponse(BaseModel):
type: Literal["answer"] = "answer"
answer: str
Authentication
Fluent configuration for three auth modes:
from pattern_agentic_messaging import PASlimConfig
# No auth (for local dev / data planes configured with auth: none)
config = PASlimConfig(local_name="org/ns/app", endpoint="...").with_no_auth()
# Shared secret
config = PASlimConfig(local_name="org/ns/app", endpoint="...").with_shared_secret("my-secret-key-at-least-32-bytes!")
# JWT with JWKS verification
config = PASlimConfig(
local_name="org/ns/app", endpoint="...",
).with_jwt_auth(
"path/to/service.token",
jwks_url="https://auth.example.com/.well-known/jwks.json",
issuer="my-issuer",
audience=["svc-b"],
)
Passing auth_secret= directly still works for backward compatibility with shared secret auth.
MessageContext
Handlers can optionally receive SLIM message context (sender identity, metadata, etc.) by adding a typed parameter:
from pattern_agentic_messaging import MessageContext
@app.on_message
async def handle(session, msg, msg_context: MessageContext):
print(msg_context.source_name) # sender identity
print(msg_context.destination_name) # destination
print(msg_context.metadata) # sender-supplied k/v pairs
Handlers without this parameter work as before.
Client
Connect to a specific peer:
config = PASlimConfig(
local_name="org/ns/client",
endpoint="https://slim.example.com",
auth_secret="shared-secret-at-least-32-bytes!",
)
async with PASlimApp(config) as app:
async with await app.connect("org/ns/server") as session:
await session.send({"type": "prompt", "prompt": "Hello world!"})
async for msg in session:
print(f"RECEIVED: {msg}")
Join a group channel:
async with PASlimApp(config) as app:
async with await app.join_channel() as session:
async for msg in session:
await session.send({"type": "response", "msg": "received"})
A2A Message Models
Lightweight Pydantic models for the A2A protocol message types, useful for constructing A2A-compliant payloads from clients:
from pattern_agentic_messaging.a2a import Message, Part, Role
msg = Message(role=Role.USER, parts=[Part.from_text("What time is it?")])
await session.send(msg.model_dump(by_alias=True, exclude_none=True))
Low-level usage
The decorator pattern's underlying API can be used directly:
async with PASlimApp(config) as app:
async for session, msg in app:
result = await process(msg["prompt"])
await session.send({"result": result})
Message Types
All messages carry a metadata.__pa_type discriminator. See docs/message-types.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pattern_agentic_messaging-1.3.1.tar.gz.
File metadata
- Download URL: pattern_agentic_messaging-1.3.1.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebf61b393577820b182c1492a467116f2d96be35848a121688b25cdd07101400
|
|
| MD5 |
533fb5553a36c3c14c0e90855880095b
|
|
| BLAKE2b-256 |
a1ebf14c0d380a1b4772b5497f245f7052c8e4f1c52d1db76c7db66dcdf63f69
|
File details
Details for the file pattern_agentic_messaging-1.3.1-py3-none-any.whl.
File metadata
- Download URL: pattern_agentic_messaging-1.3.1-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3862af807af528e06c38d7d1a8faafed911fa1ed2ef5b59866003929b4b04507
|
|
| MD5 |
0f5c048a2421e3e8b8413db1aec3bf80
|
|
| BLAKE2b-256 |
06ac2fbe0bf4b4b6d649669a059b43c2330e5e92f98a621df167e8859ca3a60a
|