66import os
77import uuid
88from typing import Any
9- from urllib .parse import urlparse
9+ from urllib .parse import urlencode , urlparse
1010
1111from uipath .core .chat import (
1212 UiPathConversationEvent ,
1818 UiPathConversationToolCallConfirmationInterruptStartEvent ,
1919 UiPathConversationToolCallConfirmationValue ,
2020)
21+ from uipath ._utils .constants import (
22+ ENV_FOLDER_KEY ,
23+ ENV_JOB_KEY ,
24+ )
2125from uipath .core .triggers import UiPathResumeTrigger
2226from uipath .runtime .chat import UiPathChatProtocol
2327from uipath .runtime .context import UiPathRuntimeContext
@@ -389,7 +393,6 @@ def get_chat_bridge(
389393 assert context .conversation_id is not None , "conversation_id must be set in context"
390394 assert context .exchange_id is not None , "exchange_id must be set in context"
391395
392- # Extract host from UIPATH_URL
393396 base_url = os .environ .get ("UIPATH_URL" )
394397 if not base_url :
395398 raise RuntimeError (
@@ -401,9 +404,20 @@ def get_chat_bridge(
401404 raise RuntimeError (f"Invalid UIPATH_URL format: { base_url } " )
402405
403406 host = parsed .netloc
407+ conversation_id = context .conversation_id
408+ folder_key = os .environ .get (ENV_FOLDER_KEY )
409+ job_key = os .environ .get (ENV_JOB_KEY )
410+
411+ # Build query params for CAS for use verifying the requesting agent should have access when runAsMe=False
412+ query_params : dict [str , str ] = {
413+ "conversationId" : conversation_id ,
414+ "folderKey" : folder_key or "" ,
415+ "jobKey" : job_key or "" ,
416+ }
417+ query_params = {k : v for k , v in query_params .items () if v }
418+ query_string = urlencode (query_params )
404419
405- # Construct WebSocket URL for CAS
406- websocket_url = f"wss://{ host } ?conversationId={ context .conversation_id } "
420+ websocket_url = f"wss://{ host } ?{ query_string } "
407421 websocket_path = "autopilotforeveryone_/websocket_/socket.io"
408422
409423 if os .environ .get ("CAS_WEBSOCKET_HOST" ):
@@ -420,13 +434,13 @@ def get_chat_bridge(
420434 or os .environ .get ("UIPATH_TENANT_ID" , "" ),
421435 "X-UiPath-Internal-AccountId" : f"{ context .org_id } "
422436 or os .environ .get ("UIPATH_ORGANIZATION_ID" , "" ),
423- "X-UiPath-ConversationId" : context . conversation_id ,
437+ "X-UiPath-ConversationId" : conversation_id ,
424438 }
425439
426440 return SocketIOChatBridge (
427441 websocket_url = websocket_url ,
428442 websocket_path = websocket_path ,
429- conversation_id = context . conversation_id ,
443+ conversation_id = conversation_id ,
430444 exchange_id = context .exchange_id ,
431445 headers = headers ,
432446 )
0 commit comments