Before you begin, you must export three environment variables. Make sure to substitute the placeholder values (xxxx-xxxx...) with your actual keys and secrets.
To connect to a room, you need a room URL and an authorized token for that room. You can generate tokens through the MeshAgent Admin Console or programmatically. Let’s connect to a room and create a document:
import asyncioimport osfrom meshagent.api import RoomClient, ParticipantToken, WebSocketClientProtocol, websocket_room_urlasyncdefmain(): room_name ="quickstart"# a participant token says who this participant is and what they are allowed to do.# Make sure your API secret in the MESHAGENT_SECRET environment variable. # This secret will be used to sign the token so that the server knows that the token is valid. token = ParticipantToken( name="my user", project_id=os.getenv("MESHAGENT_PROJECT_ID"), api_key_id=os.getenv("MESHAGENT_KEY_ID")) token.add_room_grant(room_name=room_name)# connect to the room, it will automatically be created if it does not exist yet. # We'll establish a connection to the server using a websocket. url = websocket_room_url(room_name=room_name)asyncwith RoomClient( protocol=WebSocketClientProtocol( url = url, token = token.to_jwt(token=os.getenv("MESHAGENT_SECRET"))))as room: path ="sample.document"# the document will be automatically created if it does not already exist# open the document and start synchronizing it document =await room.sync.open(path=path, create=True)try: document.root.append_child( tag_name="body", attributes={"text":"hello world!"})finally:# make sure to cleanup the document connection when we are done with itawait room.sync.close(path=path)print(f"Take a look at it at https://studio.meshagent.com/projects/{os.getenv("MESHAGENT_PROJECT_ID")}/rooms/{room_name}?p={path}")if __name__ =='__main__': loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncio.get_event_loop().run_until_complete(main())
Because Mesh documents automatically synchronize with all participants, if you also connect to the room in the MeshAgent Admin Console and open the document, you’ll see the content update in real time when you run this sample.
What if we want an agent to fill out the document for us? MeshAgent makes it easy to ask for agent assistance. As before, if you open this document in the MeshAgent Admin Console, you will see the changes occur in real time:
import asyncioimport osfrom meshagent.api import RoomClient, ParticipantToken, WebSocketClientProtocol, websocket_room_urlasyncdefmain(): room_name ="quickstart"# a participant token says who this participant is and what they are allowed to do.# Make sure your API secret in the MESHAGENT_SECRET environment variable. # This secret will be used to sign the token so that the server knows that the token is valid. token = ParticipantToken( name="my user", project_id = os.getenv("MESHAGENT_PROJECT_ID"), api_key_id = os.getenv("MESHAGENT_KEY_ID")) token.add_room_grant(room_name=room_name)# connect to the room, it will automatically be created if it does not exist yet. # We'll establish a connection to the server using a websocket. url = websocket_room_url(room_name=room_name)asyncwith RoomClient( protocol=WebSocketClientProtocol( url = url, token = token.to_jwt(token=os.getenv("MESHAGENT_SECRET"))))as room: path ="sample-agent.document"# We can ask an agent to perform some work in the document, in this case, we'll use the built in# document_writer agent to write content to our document.# The document will automatically be created if it does not already exist.await room.agents.ask( agent="meshagent.document-writer", arguments={"path": path,"prompt":"write a paragraph about ai and how agents are shaping the future"})print(f"Take a look at it at https://studio.meshagent.com/projects/{os.getenv("MESHAGENT_PROJECT_ID")}/rooms/{room_name}?p={path}")if __name__ =='__main__': loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncio.get_event_loop().run_until_complete(main())