Skip to main content

SLIM-powered messaging

Project description

Pattern Agentic Messaging

An async SLIM wrapper with a FastAPI-like interface

Installation

pip install pattern_agentic_messaging

Usage

Server

Route messages to decorated methods based on a discriminator field like type:

from pattern_agentic_messaging import PASlimApp, PASlimConfig
from .models import QuestionRequest, StatusRequest, AnswerResponse

config = PASlimConfig(
    local_name="org/ns/server/instance1",
    endpoint="https://slim.example.com",
    auth_secret="shared-secret",
    message_discriminator="type"
)

app = PASlimApp(config)
agent = None

@app.on_session_connect
async def on_connect(session):
    agent = await create_agent(...)
    session.context = {
        "agent": agent
    }
    

@app.on_message
async def handle_prompt(session, msg: QuestionRequest):
    agent = session.context.get("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.get('type')}"})

app.run()

Use PASlimConfigGroup to create a group channel.

The models are pydantic models, which must have a literal field corresponding to the discriminator:

from pydantic import BaseModel

class QuestionRequest(BaseModel):
    type: Literal["question"] = "question"
    prompt: str


class StatusRequest(BaseModel):
    type: Literal["status"] = "status"


class AnswerResponse(BaseModel):
    type: Literal["answer"] = "answer"
    answer: str

Client

Connect to a specific peer:

from pattern_agentic_messaging import PASlimApp, PASlimConfig

config = PASlimConfig(
    local_name="org/ns/client/instance1",
    endpoint="https://slim.example.com",
    auth_secret="shared-secret"
)

async with PASlimApp(config) as app:
    async with await app.connect("org/ns/server/instance1") as session:
        await session.send({"type": "prompt", "prompt": "Hello world!"})
        async for msg in session:
            print(f"RECEIVED: {msg}")

Alternatively to join a group channel:

from pattern_agentic_messaging import PASlimApp, PASlimConfig

config = PASlimConfig(
    local_name="org/ns/participant/p1",
    endpoint="https://slim.example.com",
    auth_secret="shared-secret"
)

async with PASlimApp(config) as app:
    async with await app.join_channel() as session:
        async for msg in session:
            print(f"Channel message: {msg}")
            await session.send({"type": "response", "msg": "received"})

Low-level usage

The API behind the decorator pattern can be used directly:

from pattern_agentic_messaging import PASlimApp, PASlimConfig

config = PASlimConfig(
    local_name="org/ns/server/instance1",
    endpoint="https://slim.example.com",
    auth_secret="shared-secret"
)

async with PASlimApp(config) as app:
    async for session, msg in app:
        if not isinstance(msg, dict) or "prompt" not in msg:
            await session.send({"error": "Invalid format"})
            continue

        result = await process(msg["prompt"])
        await session.send({"result": result})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pattern_agentic_messaging-0.8.4.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pattern_agentic_messaging-0.8.4-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file pattern_agentic_messaging-0.8.4.tar.gz.

File metadata

File hashes

Hashes for pattern_agentic_messaging-0.8.4.tar.gz
Algorithm Hash digest
SHA256 6884db2ab5b3605427e4920dbf820e4cf86360222ae15de6dc271684b08c6308
MD5 fd78289fe84f6eddbe76ec0ad024d5a1
BLAKE2b-256 275792d604a1354a32dc79eb61a5d4a2bde203bcca3d9c0d1331158f5b608c9d

See more details on using hashes here.

File details

Details for the file pattern_agentic_messaging-0.8.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pattern_agentic_messaging-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 36fbb6e7327a6d00d9215985915bd71db04bcc6e29a697bebd0c8575d95b039d
MD5 64b0d584e614f9fc5c483d355c155f3f
BLAKE2b-256 4294a3e194cd482716a03005f928b4ce2805631665696725c23ec1a1ed8f8f8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page