stalwart_agents.local_app

stalwart_agents.local_app: Your own app's side -- a program on the owner's machine that the bot works with (stalwart_agents.own_apps), with nothing passing through Stalwart.

from stalwart_agents.local_app import LocalApp, Refused

app = LocalApp("till")                         # the name in the bot's `own_apps`; its secret from STALWART_APP_SECRET

@app.tool
def stock_of(item: str) -> dict:
    '''How many of an item are left.'''
    return {"item": item, "left": 12}

@app.on_send
def send(to: str, text: str, in_reply_to: str) -> None:
    viber.send(to, text)                       # raise Refused("why") to tell the owner it could not be sent

app.event("new_order", {"order_id": "4411"}, id="order-4411")      # starts the task the trigger names
app.message("viber:+447700900123", "do you have oat milk?", id="m-1")   # a turn of that sender's conversation
app.run()                                      # serves the tools at http://127.0.0.1:8765/mcp

The tools are served over MCP (streamable HTTP, a JSON answer to each request) at /mcp, on this machine only: a call from another machine is refused unless the app was made with lan=True, as the bot's lan: allow admits it. Their input schema is read from each function's type hints and docstring. The bot holds every one unclassified until the owner classifies it, so each write waits for approval.

What the app sends the bot -- event and message -- is signed with the app's secret (Stalwart-Signature) and goes to the bot's host on this machine. An id makes a retry harmless: the bot starts nothing for one it has seen. A conversation's replies come back through the function on_send registers, served as the tool send_message(to, text, in_reply_to).

BOT = 'http://127.0.0.1:8700'

The bot's host, where none is given: stalwart serve's address on this machine.

SECRET_ENV = 'STALWART_APP_SECRET'

Where the app's secret is read from, where none is given.

class LocalApp(stalwart_agents.servers.ToolServer):

An own app: its tools, its conversation's replies, and what it sends the bot.

Args: name: its name in the bot's own_apps. secret: its secret; STALWART_APP_SECRET where None. bot: the bot's host on this machine. host: the address the tools are served on. port: the port they are served on. lan: serve callers on the local network too, as the bot's lan: allow does. http: posts to the bot: (url, body, headers) -> (status, answer); a test passes its own.

Example:

app = LocalApp("till", secret="s3cret") @app.tool ... def stock_of(item: str) -> dict: ... '''How many of an item are left.''' ... return {"item": item, "left": 12} [t["name"] for t in app.tools_list()] ['stock_of'] app.call("stock_of", {"item": "oat milk"})["structuredContent"] {'item': 'oat milk', 'left': 12}

LocalApp( name: str, *, secret: str | None = None, bot: str = 'http://127.0.0.1:8700', host: str = '127.0.0.1', port: int = 8765, lan: bool = False, http: Callable[[str, bytes, Mapping[str, str]], tuple[int, Any]] | None = None)
bot
http
secret: str

The app's secret: the one given, or STALWART_APP_SECRET.

Returns: The secret.

Raises: RuntimeError: neither is set.

def on_send( self, function: Callable[[str, str, str], Any]) -> Callable[[str, str, str], Any]:

Registers how the bot's replies are sent, served as the tool send_message.

Args: function: sends one reply: function(to, text, in_reply_to); it raises Refused to say why it could not.

Returns: The function, unchanged.

def serve(self) -> http.server.ThreadingHTTPServer:

Starts serving the tools at /mcp in a thread.

Returns: The server: server.server_address is where.

def run(self) -> None:

Serves the tools over HTTP until the process is stopped.

def stop(self) -> None:

Stops serving the tools over HTTP.

def event( self, trigger: str, values: Mapping[str, Any] | None = None, *, id: str | None = None) -> Any:

Starts the task @p trigger starts, with @p values: POST /hooks/{trigger}, signed. The same @p id again starts nothing.

Returns: The bot's answer: the runs it started.

def message(self, sender: str, text: str, *, id: str | None = None) -> Any:

A turn of @p sender's conversation: POST /channels/custom/{name}, signed.

Args: sender: who wrote it, as the app knows them; each sender is one conversation. text: what they wrote. id: the message's id: the same id again starts nothing.

Returns: The bot's answer: the runs it started.

class Refused(builtins.Exception):

Raised by a tool: the call is refused, and the caller is told why, in these words.

Example:

server = ToolServer("shop") @server.tool ... def refund(order_id: str) -> dict: ... raise Refused("refunds are made at the till") server.call("refund", {"order_id": "4411"})["content"][0]["text"] 'refunds are made at the till'