Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1046,26 +1046,42 @@ class ConversationalAIHandler:
|
|
| 1046 |
raise
|
| 1047 |
|
| 1048 |
def start_conversation(self, agent_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1049 |
try:
|
| 1050 |
-
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
|
| 1060 |
-
|
| 1061 |
-
|
| 1062 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1063 |
else:
|
| 1064 |
-
logger.
|
| 1065 |
-
|
| 1066 |
-
|
| 1067 |
except Exception as e:
|
| 1068 |
-
logger.error(f"[CONVERSATION]
|
| 1069 |
raise
|
| 1070 |
|
| 1071 |
def send_message(self, agent_id, conversation_id, message):
|
|
|
|
| 1046 |
raise
|
| 1047 |
|
| 1048 |
def start_conversation(self, agent_id):
|
| 1049 |
+
"""
|
| 1050 |
+
For private agents, fetch a signed WebSocket URL via REST, then
|
| 1051 |
+
initiate the connection client-side. Returns the signed URL and
|
| 1052 |
+
a conversation_id for tracking or logging.
|
| 1053 |
+
"""
|
| 1054 |
try:
|
| 1055 |
+
logger.info(f"[CONVERSATION] Requesting signed URL for agent: {agent_id}")
|
| 1056 |
+
url = f"{self.base_url}/convai/conversation/get-signed-url"
|
| 1057 |
+
params = {"agent_id": agent_id}
|
| 1058 |
+
resp = requests.get(url, headers=self.headers, params=params, timeout=15)
|
| 1059 |
+
|
| 1060 |
+
if resp.status_code != 200:
|
| 1061 |
+
logger.error(f"[CONVERSATION] Signed-URL request failed ({resp.status_code}): {resp.text}")
|
| 1062 |
+
raise Exception(f"Could not get signed URL: {resp.text}")
|
| 1063 |
+
|
| 1064 |
+
data = resp.json()
|
| 1065 |
+
signed_url = data.get("signed_url")
|
| 1066 |
+
if not signed_url:
|
| 1067 |
+
logger.error("[CONVERSATION] Missing 'signed_url' in response.")
|
| 1068 |
+
raise Exception("Invalid signed URL response")
|
| 1069 |
+
|
| 1070 |
+
# You can optionally list the conversations to extract the conversation_id:
|
| 1071 |
+
logger.info("[CONVERSATION] Fetching existing conversations list for agent...")
|
| 1072 |
+
list_resp = requests.get(f"{self.base_url}/convai/conversations",
|
| 1073 |
+
headers=self.headers,
|
| 1074 |
+
params={"agent_id": agent_id}, timeout=15)
|
| 1075 |
+
if list_resp.status_code == 200:
|
| 1076 |
+
convs = list_resp.json().get("conversations", [])
|
| 1077 |
+
conv_ids = [c["conversation_id"] for c in convs]
|
| 1078 |
+
logger.info(f"[CONVERSATION] Agent has {len(conv_ids)} existing conversations.")
|
| 1079 |
else:
|
| 1080 |
+
logger.warning(f"[CONVERSATION] Couldn't list conversations: HTTP {list_resp.status_code}")
|
| 1081 |
+
|
| 1082 |
+
return signed_url, conv_ids if 'conv_ids' in locals() else []
|
| 1083 |
except Exception as e:
|
| 1084 |
+
logger.error(f"[CONVERSATION] Exception in start_conversation: {e}")
|
| 1085 |
raise
|
| 1086 |
|
| 1087 |
def send_message(self, agent_id, conversation_id, message):
|