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
from typing import Optional
from meshagent.api import RoomClient, Element
from meshagent.agents import Listener, ListenerContext, connect_development_agent

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
    

async def main():
    room_name = "examples"

    # start our agent in developer mode, it will connect to the room and be available immediately from the admin console UI    
    await connect_development_agent(room_name=room_name, agent=SampleListener())
            

if __name__ == '__main__':
    
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.get_event_loop().run_until_complete(main())

Running the Sample

  • 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.