rairo commited on
Commit
05f9336
·
verified ·
1 Parent(s): 60e9507

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -17
main.py CHANGED
@@ -1046,26 +1046,42 @@ class ConversationalAIHandler:
1046
  raise
1047
 
1048
  def start_conversation(self, agent_id):
 
 
 
 
 
1049
  try:
1050
-
1051
- logger.info(f"[CONVERSATION] Starting conversation with agent: {agent_id}")
1052
- conversation_url = f"{self.base_url}/convai/conversations"
1053
- payload = {"agent_id": agent_id}
1054
-
1055
- response = requests.post(conversation_url, headers=self.headers, json=payload, timeout=30)
1056
-
1057
- if response.status_code == 200:
1058
- conversation_info = response.json()
1059
- conversation_id = conversation_info.get("conversation_id")
1060
- logger.info(f"[CONVERSATION] Conversation started successfully: {conversation_id}")
1061
-
1062
- return conversation_id
 
 
 
 
 
 
 
 
 
 
 
1063
  else:
1064
- logger.error(f"[CONVERSATION] Failed to start conversation. Status: {response.status_code}, Response: {response.text}")
1065
- raise Exception(f"Failed to start conversation: {response.text}")
1066
-
1067
  except Exception as e:
1068
- logger.error(f"[CONVERSATION] An exception occurred in start_conversation: {e}")
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):