
    4AjG                    >    d Z ddlmZ ddlZdZddZddd
ZdddZdS )u7  Shared Google service-account authentication for OpenMontage tools.

Lets the Google provider tools (``google_tts``, ``google_imagen``)
authenticate with a service-account JSON key file via OAuth Bearer tokens —
in addition to the existing API-key path. This is what makes
``GOOGLE_APPLICATION_CREDENTIALS`` actually work end to end.

The ``google-auth`` package is imported lazily so this module never adds an
import-time cost for tools that only use API keys, and so a missing dependency
surfaces as an actionable runtime error rather than a hard import failure.
    )annotationsNz.https://www.googleapis.com/auth/cloud-platformreturnboolc                     t           j                            d          } t          | ot           j                            |                     S )zDTrue when GOOGLE_APPLICATION_CREDENTIALS points to an existing file.GOOGLE_APPLICATION_CREDENTIALS)osenvirongetr   pathexists)r   s    G/app/working/workspaces/default/OpenMontage/tools/google_credentials.pyservice_account_configuredr      s7    :>>:;;D---...    creds_project_id
str | Nonec                    t           j                            d          p?t           j                            d          p t           j                            d          p| S )zResolve the GCP project id from env vars, falling back to the key file's.

    Vertex AI needs an explicit project id; TTS does not. We prefer an explicit
    env override so users can target a project other than the key's own.
    GOOGLE_CLOUD_PROJECTGOOGLE_CLOUD_PROJECT_IDGCLOUD_PROJECT)r   r	   r
   )r   s    r   resolve_project_idr      sS     	
-.. 	:>>344	:>>*++	 	r   scopeslist[str] | Nonetuple[str, str | None]c                
   | t           g} 	 ddlm} ddlm} n"# t
          $ r}t          d          |d}~ww xY wt          j        	                    d          }|rt          j
                            |          st          d          	 |j                            ||           }|                     |                       n(# t          $ r}t          d	| d
|           |d}~ww xY w|j        t#          |dd          fS )u  Mint an OAuth access token from the service-account JSON.

    Returns ``(access_token, project_id)``. ``project_id`` is the one embedded
    in the key file (callers should still prefer :func:`resolve_project_id`).

    Raises:
        RuntimeError: if ``google-auth`` is missing or the credentials cannot
            be loaded/refreshed — with a message the agent can surface verbatim.
    Nr   )Request)service_accountzaService-account auth requires the 'google-auth' package. Install it with: pip install google-authr   zqGOOGLE_APPLICATION_CREDENTIALS is not set or points to a missing file; cannot use service-account authentication.)r   z8Failed to load/refresh service-account credentials from z: 
project_id)CLOUD_PLATFORM_SCOPEgoogle.auth.transport.requestsr   google.oauth2r   ImportErrorRuntimeErrorr   r	   r
   r   r   Credentialsfrom_service_account_filerefresh	Exceptiontokengetattr)r   r   r   excr   credss         r   get_access_tokenr+   )   si    ~&'::::::1111111   7
 
 	 :>>:;;D 
rw~~d++ 
?
 
 	

+EE F 
 
 	ggii       TtTTsTT
 
	
 ;|T::::s(    
8389C 
C*C%%C*)r   r   )N)r   r   r   r   )r   r   r   r   )__doc__
__future__r   r   r   r   r   r+    r   r   <module>r/      s   
 
 # " " " " " 				 H / / / /    '; '; '; '; '; '; ';r   