messenger v0.9
This commit is contained in:
parent
5b54f3f636
commit
ae6bf0f96d
13 changed files with 105 additions and 115 deletions
78
agent.py
78
agent.py
|
|
@ -13,74 +13,44 @@ class Agent:
|
|||
|
||||
"""
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
|
||||
# Submit a request for info, return response.
|
||||
# Must disclose what service it's for.
|
||||
async def fetch(self, entities, service):
|
||||
|
||||
# Assert list.
|
||||
if type(entities) == str:
|
||||
entities = [entities]
|
||||
|
||||
print(f"Fetching {[e for e in entities]} for {service}")
|
||||
|
||||
# Build intent.
|
||||
params = {
|
||||
"service": service,
|
||||
"entities": entities
|
||||
}; intent = typs.Intent("fetch", params)
|
||||
|
||||
# Send intent to agent, wait for response.
|
||||
await self.send_agent_intent(intent)
|
||||
response = await self.recv_agent_intent()
|
||||
|
||||
# Parse response and return entities.
|
||||
entities = response.params.get("found")
|
||||
print(f"Fetch response: {entities}")
|
||||
return entities
|
||||
|
||||
# Text the client something. Also, let agent know
|
||||
# when your service is complete via the complete
|
||||
# attribute.
|
||||
# Must disclose what service it's for.
|
||||
async def notify(self, text, end=False):
|
||||
|
||||
print(f"Notifying: {text} for {self.service}")
|
||||
self.service = None
|
||||
self.client_connection = connection
|
||||
|
||||
def fetch(self, entity, invocation):
|
||||
# Create the intent.
|
||||
params = {
|
||||
"entity": entity,
|
||||
"service": self.service,
|
||||
"text": text,
|
||||
"invocation": invocation
|
||||
}
|
||||
intent = typs.Intent(name="fetch", params=params)
|
||||
|
||||
self.send_agent_intent(intent)
|
||||
intent = self.recv_agent_intent()
|
||||
|
||||
response = intent.params.get("response")
|
||||
return response
|
||||
|
||||
def notify(self, text, end=False):
|
||||
# Create the intent.
|
||||
params = {
|
||||
"text": text,
|
||||
"service": "message",
|
||||
"end": end
|
||||
}
|
||||
|
||||
# Build the intent
|
||||
intent = typs.Intent("notify", params)
|
||||
|
||||
# Send intent to agent.
|
||||
await self.send_agent_intent(intent)
|
||||
|
||||
# Get user info.
|
||||
async def sync(self):
|
||||
|
||||
intent = typs.Intent("sync", params={})
|
||||
intent = typs.Intent(name="notify", params=params)
|
||||
|
||||
# Send intent to agent.
|
||||
await self.send_agent_intent(intent)
|
||||
|
||||
# Recv and return profile.
|
||||
intent = await self.recv_agent_intent()
|
||||
profile = intent.params.get("profile")
|
||||
return profile
|
||||
self.send_agent_intent(intent)
|
||||
|
||||
async def recv_agent_intent(self):
|
||||
package = await self.connection.recv()
|
||||
package = await self.client_connection.recv()
|
||||
intent = self.__decode(package)
|
||||
return intent
|
||||
|
||||
async def send_agent_intent(self, intent):
|
||||
package = self.__encode(intent)
|
||||
await self.connection.send(package)
|
||||
await self.client_connection.send(package)
|
||||
|
||||
def __encode(self, intent: typs.Intent):
|
||||
package = json.dumps(intent.__dict__)
|
||||
|
|
|
|||
Loading…
Reference in a new issue