Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,54 @@ from openfloor import (
|
|
11 |
from openfloor.manifest import *
|
12 |
from openfloor.envelope import *
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
class OpenFloorAgent:
|
15 |
def __init__(self, url: str, name: str = None):
|
16 |
self.url = url
|
@@ -21,22 +69,14 @@ class OpenFloorAgent:
|
|
21 |
def get_manifest(self):
|
22 |
"""Get the agent's manifest and capabilities"""
|
23 |
try:
|
24 |
-
|
25 |
-
sender = Sender(
|
26 |
-
speakerUri="openfloor://mcp-bridge/client",
|
27 |
-
serviceUrl="http://mcp-bridge"
|
28 |
-
)
|
29 |
-
envelope = Envelope(conversation=conversation, sender=sender)
|
30 |
-
|
31 |
-
manifest_event = GetManifestsEvent(
|
32 |
-
to=To(serviceUrl=self.url, private=False)
|
33 |
-
)
|
34 |
-
envelope.events.append(manifest_event)
|
35 |
|
|
|
36 |
json_str = envelope.to_json()
|
37 |
envelope_dict = json.loads(json_str)
|
38 |
payload = {"openFloor": envelope_dict}
|
39 |
|
|
|
40 |
headers = {'Content-Type': 'application/json'}
|
41 |
response = requests.post(self.url, json=payload, headers=headers, timeout=10)
|
42 |
|
@@ -58,29 +98,14 @@ class OpenFloorAgent:
|
|
58 |
def send_utterance(self, message: str) -> str:
|
59 |
"""Send an utterance to the agent and get response"""
|
60 |
try:
|
61 |
-
|
62 |
-
sender = Sender(
|
63 |
-
speakerUri="openfloor://mcp-bridge/client",
|
64 |
-
serviceUrl="http://mcp-bridge"
|
65 |
-
)
|
66 |
-
envelope = Envelope(conversation=conversation, sender=sender)
|
67 |
-
|
68 |
-
dialog_event = DialogEvent(
|
69 |
-
speakerUri="openfloor://mcp-bridge/client",
|
70 |
-
features={"text": TextFeature(values=[message])}
|
71 |
-
)
|
72 |
-
|
73 |
-
utterance_event = UtteranceEvent(
|
74 |
-
dialogEvent=dialog_event,
|
75 |
-
to=To(serviceUrl=self.url, private=False)
|
76 |
-
)
|
77 |
-
|
78 |
-
envelope.events.append(utterance_event)
|
79 |
|
|
|
80 |
json_str = envelope.to_json()
|
81 |
envelope_dict = json.loads(json_str)
|
82 |
payload = {"openFloor": envelope_dict}
|
83 |
|
|
|
84 |
headers = {'Content-Type': 'application/json'}
|
85 |
response = requests.post(self.url, json=payload, headers=headers, timeout=30)
|
86 |
|
|
|
11 |
from openfloor.manifest import *
|
12 |
from openfloor.envelope import *
|
13 |
|
14 |
+
# Function to build the envelope for a manifest request using OpenFloor library
|
15 |
+
def build_manifest_request_envelope(agent_url):
|
16 |
+
# Create conversation and sender
|
17 |
+
conversation = Conversation()
|
18 |
+
sender = Sender(
|
19 |
+
speakerUri="openfloor://localhost/TestClient",
|
20 |
+
serviceUrl="http://localhost"
|
21 |
+
)
|
22 |
+
|
23 |
+
# Create envelope
|
24 |
+
envelope = Envelope(conversation=conversation, sender=sender)
|
25 |
+
|
26 |
+
# Add get manifests event
|
27 |
+
manifest_event = GetManifestsEvent(
|
28 |
+
to=To(serviceUrl=agent_url, private=False)
|
29 |
+
)
|
30 |
+
envelope.events.append(manifest_event)
|
31 |
+
|
32 |
+
return envelope
|
33 |
+
|
34 |
+
# Function to build an utterance envelope using OpenFloor library
|
35 |
+
def build_utterance_envelope(agent_url, message_text):
|
36 |
+
# Create conversation and sender
|
37 |
+
conversation = Conversation()
|
38 |
+
sender = Sender(
|
39 |
+
speakerUri="openfloor://localhost/TestClient",
|
40 |
+
serviceUrl="http://localhost"
|
41 |
+
)
|
42 |
+
|
43 |
+
# Create envelope
|
44 |
+
envelope = Envelope(conversation=conversation, sender=sender)
|
45 |
+
|
46 |
+
# Create dialog event with text feature
|
47 |
+
dialog_event = DialogEvent(
|
48 |
+
speakerUri="openfloor://localhost/TestClient",
|
49 |
+
features={"text": TextFeature(values=[message_text])}
|
50 |
+
)
|
51 |
+
|
52 |
+
# Create utterance event
|
53 |
+
utterance_event = UtteranceEvent(
|
54 |
+
dialogEvent=dialog_event,
|
55 |
+
to=To(serviceUrl=agent_url, private=False)
|
56 |
+
)
|
57 |
+
|
58 |
+
envelope.events.append(utterance_event)
|
59 |
+
|
60 |
+
return envelope
|
61 |
+
|
62 |
class OpenFloorAgent:
|
63 |
def __init__(self, url: str, name: str = None):
|
64 |
self.url = url
|
|
|
69 |
def get_manifest(self):
|
70 |
"""Get the agent's manifest and capabilities"""
|
71 |
try:
|
72 |
+
envelope = build_manifest_request_envelope(self.url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
# Convert to JSON using the library's method
|
75 |
json_str = envelope.to_json()
|
76 |
envelope_dict = json.loads(json_str)
|
77 |
payload = {"openFloor": envelope_dict}
|
78 |
|
79 |
+
# Send request
|
80 |
headers = {'Content-Type': 'application/json'}
|
81 |
response = requests.post(self.url, json=payload, headers=headers, timeout=10)
|
82 |
|
|
|
98 |
def send_utterance(self, message: str) -> str:
|
99 |
"""Send an utterance to the agent and get response"""
|
100 |
try:
|
101 |
+
envelope = build_utterance_envelope(self.url, message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
# Convert to JSON using the library's method
|
104 |
json_str = envelope.to_json()
|
105 |
envelope_dict = json.loads(json_str)
|
106 |
payload = {"openFloor": envelope_dict}
|
107 |
|
108 |
+
# Send request
|
109 |
headers = {'Content-Type': 'application/json'}
|
110 |
response = requests.post(self.url, json=payload, headers=headers, timeout=30)
|
111 |
|