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}"})

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.9.1.tar.gz (9.4 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.9.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pattern_agentic_messaging-0.9.1.tar.gz
Algorithm Hash digest
SHA256 d1da89760878c709c285e7653cf52ee5514097f3672cb789593e321c5bf6ea95
MD5 aae918cfad8ec204e7955ce4f31d20be
BLAKE2b-256 c9abe6229180a0487338a4f29371e6a56d0407f562f8fbc3f667b6783522cccc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pattern_agentic_messaging-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df60f24192825dae0e0226a2a8dde36941407014802895b0c3e4fa84e7c03b8e
MD5 95f90de3ded8baefc96fb45aaccc5906
BLAKE2b-256 d0d55b401a1fef553bd2cdbb9ace990afca893dc061176ed1dd6cb24c2ba014f

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