Skip to main content
In many cases, agents need to be able to listen for changes to a document. Imagine a scenario where an agent is proofreading a document in order to check it for accuracy. In order to check the document in realtime, the agent first needs to know what changed in realtime. MeshDocuments make listening for changes in realtime a snap. The easiest way to get started is with the built in Listener base class.

import asyncio
import logging
from typing import Optional
from meshagent.otel import otel_config
from meshagent.api import Element
from meshagent.api.services import ServiceHost
from meshagent.agents import Listener, ListenerContext


otel_config(service_name="listener")
log = logging.getLogger(__name__)
service = ServiceHost()


@service.path("/listener")
class SampleListener(Listener):
    def __init__(self):
        # We want the listener to only deliver new changes, so we'll tell it to start listening after the document has been synchronized
        super().__init__(
            name="samples.listener",
            title="sample listener",
            description="a sample agent that listens to a document",
            wait_for_synchronize=True,
        )

    # this method will be called when we start listening
    async def on_listening_started(self, listener_context: ListenerContext):
        print("The listener has started")

    # this method will be called any time a new element is inserted
    async def on_element_inserted(
        self, listener_context: ListenerContext, element: Element
    ) -> bool:
        print(f"an element was inserted {element.tag_name}")

        # If we return True, the listener will stop, returning False keeps the listener active
        return False

    # this method will be called any time an attribute is updated
    async def on_attribute_changed(
        self,
        listener_context: ListenerContext,
        element: Element,
        attribute: Optional[str],
    ) -> bool:
        print(
            f"an attribute was changed {element.tag_name} {attribute}={element[attribute]}"
        )

        # If we return True, the listener will stop, returning False keeps the listener active
        return False


asyncio.run(service.run())

Running the Sample

From the terminal run:
bash
meshagent setup
meshagent service run "listener.py" --room=listener
  • Launch the sample and then connect to the room “examples” with MeshAgent Studio.
  • Add a document in the room
  • Press “Run” to select the sample listener agent
  • Select the agent “proxy.sample-listener”.
  • You will be asked the path of the document that the agent should listen to, enter the name (including extension) of the document you created.
  • After connecting to the agent, the agent will print a message letting you know it has started listening.
  • Under the source tab, you can edit the document. Right click the root node to add a new element and the listener will print out that the element has changed.
  • Modify an attribute and the listener will print that the attribute changed

Taking it further

Now that you are listening to the document, your agent can take action based on changes that have been made to the content. For example, an agent might verify that changes made to the document do not introduce factual inaccuracies and then send a message or even talk to the user to let them know if they have made an error that should be corrected.