22import os
33from collections .abc import Iterator
44from typing import Any , Optional
5- from urllib .parse import quote
65
76from langchain_core .callbacks import CallbackManagerForLLMRun
87from langchain_core .messages import BaseMessage
98from langchain_core .outputs import ChatGenerationChunk , ChatResult
109from tenacity import AsyncRetrying , Retrying
1110from uipath .platform .common import EndpointManager , resource_override
1211
13- from .header_capture import HeaderCapture
14- from .retryers .bedrock import AsyncBedrockRetryer , BedrockRetryer
12+ from .http_client import build_uipath_headers , resolve_gateway_url
13+ from .http_client .header_capture import HeaderCapture
14+ from .http_client .retryers .bedrock import AsyncBedrockRetryer , BedrockRetryer
1515from .supported_models import BedrockModels
1616from .types import APIFlavor , LLMProvider
1717
@@ -72,6 +72,7 @@ def __init__(
7272 self .byo_connection_id = byo_connection_id
7373 self ._vendor = "awsbedrock"
7474 self ._url : Optional [str ] = None
75+ self ._is_override : bool = False
7576 self .header_capture = header_capture
7677
7778 @property
@@ -83,16 +84,10 @@ def endpoint(self) -> str:
8384 )
8485 return formatted_endpoint
8586
86- def _build_base_url (self ) -> str :
87+ def _resolve_url (self ) -> tuple [ str , bool ] :
8788 if not self ._url :
88- env_uipath_url = os .getenv ("UIPATH_URL" )
89-
90- if env_uipath_url :
91- self ._url = f"{ env_uipath_url .rstrip ('/' )} /{ self .endpoint } "
92- else :
93- raise ValueError ("UIPATH_URL environment variable is required" )
94-
95- return self ._url
89+ self ._url , self ._is_override = resolve_gateway_url (self .endpoint )
90+ return self ._url , self ._is_override
9691
9792 def _capture_response_headers (self , parsed , model , ** kwargs ):
9893 if "ResponseMetadata" in parsed :
@@ -122,29 +117,24 @@ def get_client(self):
122117 return client
123118
124119 def _modify_request (self , request , ** kwargs ):
125- """Intercept boto3 request and redirect to LLM Gateway"""
120+ """Intercept boto3 request and redirect to LLM Gateway. """
126121 # Detect streaming based on URL suffix:
127122 # - converse-stream / invoke-with-response-stream -> streaming
128123 # - converse / invoke -> non-streaming
129124 streaming = "true" if request .url .endswith ("-stream" ) else "false"
130- request .url = self ._build_base_url ()
131-
132- headers = {
133- "Authorization" : f"Bearer { self .token } " ,
134- "X-UiPath-LlmGateway-ApiFlavor" : self .api_flavor ,
135- "X-UiPath-Streaming-Enabled" : streaming ,
136- }
137-
138- if self .agenthub_config :
139- headers ["X-UiPath-AgentHub-Config" ] = self .agenthub_config
140- if self .byo_connection_id :
141- headers ["X-UiPath-LlmGateway-ByoIsConnectionId" ] = self .byo_connection_id
142- job_key = os .getenv ("UIPATH_JOB_KEY" )
143- process_key = os .getenv ("UIPATH_PROCESS_KEY" )
144- if job_key :
145- headers ["X-UiPath-JobKey" ] = job_key
146- if process_key :
147- headers ["X-UiPath-ProcessKey" ] = quote (process_key , safe = "" )
125+ url , is_override = self ._resolve_url ()
126+ request .url = url
127+
128+ headers : dict [str , str ] = {"Authorization" : f"Bearer { self .token } " }
129+ headers .update (
130+ build_uipath_headers (
131+ agenthub_config = self .agenthub_config ,
132+ byo_connection_id = self .byo_connection_id ,
133+ inject_routing = is_override ,
134+ )
135+ )
136+ headers ["X-UiPath-LlmGateway-ApiFlavor" ] = self .api_flavor
137+ headers ["X-UiPath-Streaming-Enabled" ] = streaming
148138
149139 request .headers .update (headers )
150140
0 commit comments