
    iſ                        U d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZ  ej        e          Zi Ze	eef         ed<   i Ze	eef         ed<   d Zdefd	Zdefd
Zd ZdedefdZdZdefdZdedefdZg dZd\dee	eef                  dee         fdZd\dee	eef                  defdZ d\dee	eef                  defdZ!dee
e                  fdZ"g dZ#dedefdZ$dedefdZ%de
e	         de
e	         fdZ&de
e	         fdZ'de
e	         deee
e	                  e
e	         f         fd Z(d!edefd"Z)d#e	defd$Z*defd%Z+	 	 	 	 d]defd&Z,	 	 	 	 	 	 d^d(ede
e	         dee
e	                  d)e-d*ee.         d+ee.         d,ee
e                  d-ee	         de	eef         fd.Z/	 	 	 	 	 	 d^ded(ede
e	         dee
e	                  d)e-d*ee.         d+ee.         d,ee
e                  d-ee	         defd/Z0	 	 	 	 	 	 d^ded(ede
e	         dee
e	                  d)e-d*ee.         d+ee.         d,ee
e                  d-ee	         defd0Z1i Z2e	eef         ed1<   d2Z3d3 Z4	 d\ded4ee
e                  de
e	eef                  fd5Z5d6edefd7Z6dede
e         fd8Z7 ej8        d9ej9                   ej8        d:ej9                   ej8        d;ej9                  gZ: ej8        d<ej9                   ej8        d=ej9                   ej8        d>ej9                  gZ; ej8        d?ej9                   ej8        d@ej9                   ej8        dAej9                  gZ<dBedefdCZ=dBedefdDZ>i dEdFdGdFdHdFdIdFdJdFdKdFdLdFdMdFdNdFdOdFdPdFdQdRdSdRdTdUdVdUdWdUdXdUdUdUdYZ?e	ee-f         edZ<   dUZ@dede-fd[ZAdS )_u  AWS Bedrock Converse API adapter for Hermes Agent.

Provides native integration with Amazon Bedrock using the Converse API,
bypassing the OpenAI-compatible endpoint in favor of direct AWS SDK calls.
This enables full access to the Bedrock ecosystem:

  - **Native Converse API**: Unified interface for all Bedrock models
    (Claude, Nova, Llama, Mistral, etc.) with streaming support.
  - **AWS credential chain**: IAM roles, SSO profiles, environment variables,
    instance metadata — zero API key management for AWS-native environments.
  - **Dynamic model discovery**: Auto-discovers available foundation models
    and cross-region inference profiles via the Bedrock control plane.
  - **Guardrails support**: Optional Bedrock Guardrails configuration for
    content filtering and safety policies.
  - **Inference profiles**: Supports cross-region inference profiles
    (us.anthropic.claude-*, global.anthropic.claude-*) for better capacity
    and automatic failover.

Architecture follows the same pattern as ``anthropic_adapter.py``:
  - All Bedrock-specific logic is isolated in this module.
  - Messages/tools are converted between OpenAI format and Converse format.
  - Responses are normalized back to OpenAI-compatible objects for the agent loop.

Reference: OpenClaw's ``extensions/amazon-bedrock/`` plugin, which implements
the same Converse API integration in TypeScript via ``@aws-sdk/client-bedrock``.

Requires: ``boto3`` (optional dependency — only needed when using the Bedrock provider).
    N)SimpleNamespace)AnyDictListOptionalTuple_bedrock_runtime_client_cache_bedrock_control_client_cachec                  J    	 ddl } | S # t          $ r t          d          w xY w)z5Import boto3, raising a clear error if not installed.r   NzThe 'boto3' package is required for the AWS Bedrock provider. Install it with: pip install boto3
Or install Hermes with Bedrock support: pip install -e '.[bedrock]')boto3ImportError)r   s    :/home/ubuntu/.hermes/hermes-agent/agent/bedrock_adapter.py_require_boto3r   0   sH    
 
 
 
R
 
 	

s    "regionc                     | t           vr-t                      }|                    d|           t           | <   t           |          S )u   Get or create a cached ``bedrock-runtime`` client for the given region.

    Uses the default AWS credential chain (env vars → profile → instance role).
    zbedrock-runtimeregion_name)r	   r   clientr   r   s     r   _get_bedrock_runtime_clientr   =   sK    
 222  056 1= 1
 1
%f- )00    c                     | t           vr-t                      }|                    d|           t           | <   t           |          S )zLGet or create a cached ``bedrock`` control-plane client for model discovery.bedrockr   )r
   r   r   r   s     r   _get_bedrock_control_clientr   J   sI    222  056 1= 1
 1
%f- )00r   c                  j    t                                            t                                           dS )z?Clear cached boto3 clients. Used in tests and profile switches.N)r	   clearr
    r   r   reset_client_cacher   T   s,    !'')))!'')))))r   returnc                 N    | t           v }t                               | d           |S )a  Evict the cached ``bedrock-runtime`` client for a single region.

    Per-region counterpart to :func:`reset_client_cache`. Used by the converse
    call wrappers to discard clients whose underlying HTTP connection has
    gone stale, so the next call allocates a fresh client (with a fresh
    connection pool) instead of reusing a dead socket.

    Returns True if a cached entry was evicted, False if the region was not
    cached.
    N)r	   pop)r   existeds     r   invalidate_runtime_clientr#   Z   s*     55G!%%fd333Nr   )zurllib3.z	botocore.zboto3.excc              #      K   t          | dd          }|3|j        }|j                            dd          }|pdV  |j        }|1dS dS )zJYield ``__name__``-style module strings for each frame in exc's traceback.__traceback__N__name__ )getattrtb_frame	f_globalsgettb_next)r$   tbframemodules       r   _traceback_frames_modulesr1      s`      	ot	,	,B
.$$Z44lZ	 .....r   c                 r   	 ddl m}m} ||f}n# t          $ r d}Y nw xY w|rt	          | |          rdS 	 ddlm}m}m} |||f}n# t          $ r d}Y nw xY w|rt	          | |          rdS t	          | t                    r5t          |           D ]%t          fdt          D                       r dS &dS )u  Return True if ``exc`` indicates a dead/stale Bedrock HTTP connection.

    Matches:
      * ``botocore.exceptions.ConnectionError`` and subclasses
        (``ConnectionClosedError``, ``EndpointConnectionError``,
        ``ReadTimeoutError``, ``ConnectTimeoutError``).
      * ``urllib3.exceptions.ProtocolError`` / ``NewConnectionError`` /
        ``ConnectionError`` (best-effort import — urllib3 is a transitive
        dependency of botocore so it is always available in practice).
      * Bare ``AssertionError`` raised from a frame inside urllib3, botocore,
        or boto3. These are internal-invariant failures (typically triggered
        by corrupted connection-pool state after a dropped socket) and are
        recoverable by swapping the client.

    Non-library ``AssertionError``s (from application code or tests) are
    intentionally not matched — only library-internal asserts signal stale
    connection state.
    r   )ConnectionErrorHTTPClientErrorr   T)ProtocolErrorNewConnectionErrorr3   c              3   B   K   | ]}                     |          V  d S N)
startswith).0prefixr0   s     r   	<genexpr>z,is_stale_connection_error.<locals>.<genexpr>   s1      VV6$$V,,VVVVVVr   F)botocore.exceptionsr3   r4   r   
isinstanceurllib3.exceptionsr5   r6   AssertionErrorr1   any_STALE_LIB_MODULE_PREFIXES)	r$   BotoConnectionErrorr4   botocore_errorsr5   r6   Urllib3ConnectionErrorurllib3_errorsr0   s	           @r   is_stale_connection_errorrG      so   .	
 	
 	
 	
 	
 	
 	
 	
 #6!G    :c?;; t	
 	
 	
 	
 	
 	
 	
 	
 	
 	

 ();=ST    *S.99 t #~&& /44 	 	FVVVV;UVVVVV tt 5s    A AA)AWS_BEARER_TOKEN_BEDROCKAWS_ACCESS_KEY_IDAWS_PROFILE&AWS_CONTAINER_CREDENTIALS_RELATIVE_URIAWS_WEB_IDENTITY_TOKEN_FILEenvc                    | | nt           j        } |                     dd                                          rdS |                     dd                                          r*|                     dd                                          rdS |                     dd                                          rdS |                     dd                                          rdS |                     dd                                          rdS 	 d	dl}|j                                        }|                                }||                                }|r	|j	        rd
S n# t          $ r Y nw xY wdS )u  Return the name of the AWS auth source that is active, or None.

    Checks environment variables first, then falls back to boto3's credential
    chain for implicit sources (EC2 IMDS, ECS task role, etc.).

    This mirrors OpenClaw's ``resolveAwsSdkEnvVarName()`` — used to detect
    whether the user has any AWS credentials configured without actually
    attempting to authenticate.
    NrH   r(   rI   AWS_SECRET_ACCESS_KEYrJ   rK   rL   r   ziam-role)osenvironr,   stripbotocore.sessionsessionget_sessionget_credentialsget_frozen_credentials
access_key	ExceptionrM   botocorerT   credentialsresolveds        r   resolve_aws_auth_env_varr^      s    ##bjC
ww)2..4466 *))#R((..00 #/44::<<#""
ww}b!!'')) }
ww7<<BBDD 877
ww,b117799 -,,	"..00--//""99;;H "H/ "!z   4s   AE 
E,+E,c                     t          |           dS 	 ddl}|j                                        }|                                }||                                }|r	|j        rdS n# t          $ r Y nw xY wdS )a`  Return True if any AWS credential source is detected.

    Checks environment variables first (fast, no I/O), then falls back to
    boto3's credential chain which covers EC2 instance roles, ECS task roles,
    Lambda execution roles, and other IMDS-based sources that don't set
    environment variables.

    This two-tier approach mirrors the pattern from OpenClaw PR #62673:
    cloud environments (EC2, ECS, Lambda) provide credentials via instance
    metadata, not environment variables. The env-var check is a fast path
    for local development; the boto3 fallback covers all cloud deployments.
    NTr   F)r^   rS   rT   rU   rV   rW   rX   rY   rZ   s        r   has_aws_credentialsr`     s      $$0t	"..00--//""99;;H H/ t   5s   AA& &
A32A3c                 Z   | | nt           j        } |                     dd                                          p'|                     dd                                          }|r|S 	 ddl}|j                                                            d          }|r|S n# t          $ r Y nw xY wdS )u  Resolve the AWS region for Bedrock API calls.

    Priority:
      1. AWS_REGION env var
      2. AWS_DEFAULT_REGION env var
      3. boto3/botocore configured region (from ~/.aws/config or SSO profile)
      4. us-east-1 (hard fallback)

    The boto3 fallback is critical for EU/AP users who configure their region
    in ~/.aws/config via a named profile rather than env vars — without it,
    live model discovery would always return us.* profile IDs regardless of
    the user's actual region.
    N
AWS_REGIONr(   AWS_DEFAULT_REGIONr   r   z	us-east-1)	rP   rQ   r,   rR   rS   rT   rU   get_config_variablerY   )rM   explicitr[   r   s       r   resolve_bedrock_regionrf   #  s     ##bjCb!!'')) 	577',,2244   !--//CCHMM 	M	   ;s   &3B 
B('B(c                  |    	 t          t                                } | rd | D             S n# t          $ r Y nw xY wdS )u  Live-discover Bedrock model IDs for the active region.

    Returns a list of model ID strings if discovery succeeds and yields
    at least one model, or ``None`` on failure / empty result.  Callers
    should fall back to the static curated list when ``None`` is returned.

    This helper consolidates the discover → extract-ids → fallback
    pattern that was previously duplicated across ``provider_model_ids``,
    ``list_authenticated_providers`` section 2, and section 3.
    c                     g | ]
}|d          S idr   r:   ms     r   
<listcomp>z-bedrock_model_ids_or_none.<locals>.<listcomp>P  s    000AdG000r   N)discover_bedrock_modelsrf   rY   )
discovereds    r   bedrock_model_ids_or_nonerp   B  sa    ,-C-E-EFF
 	100Z0000	1   4s   (, 
99)zdeepseek.r1zdeepseek-r1z
stability.zcohere.embedzamazon.titan-embedmodel_idc                 n    |                                  t          fdt          D                        S )zReturn True if the model is expected to support tool/function calling.

    Models in the denylist are known to reject toolConfig in the Converse API.
    Unknown models default to True (assume tool support).
    c              3       K   | ]}|v V  	d S r8   r   )r:   patternmodel_lowers     r   r<   z+_model_supports_tool_use.<locals>.<genexpr>p  s(      TTg7k)TTTTTTr   )lowerrA   _NON_TOOL_CALLING_PATTERNS)rq   ru   s    @r   _model_supports_tool_userx   i  s;     ..""KTTTT9STTTTTTTr   c                     |                                  }dD ]0}|                    |          r|t          |          d         } n1|                    d          S )a  Return True if the model is an Anthropic Claude model on Bedrock.

    These models should use the AnthropicBedrock SDK path for full feature
    parity (prompt caching, thinking budgets, adaptive thinking).
    Non-Claude models use the Converse API path.

    Matches:
      - ``anthropic.claude-*`` (foundation model IDs)
      - ``us.anthropic.claude-*`` (US inference profiles)
      - ``global.anthropic.claude-*`` (global inference profiles)
      - ``eu.anthropic.claude-*`` (EU inference profiles)
    )zus.global.zeu.zap.zjp.Nzanthropic.claude)rv   r9   len)rq   ru   r;   s      r   is_anthropic_bedrock_modelr|   s  sm     ..""K9  !!&)) 	%c&kkll3KE	 !!"4555r   toolsc                    | sg S g }| D ]z}|                     di           }|                     dd          }|                     dd          }|                     ddi d          }|                    d||d	|id
i           {|S )a  Convert OpenAI-format tool definitions to Bedrock Converse ``toolConfig``.

    OpenAI format::

        {"type": "function", "function": {"name": "...", "description": "...",
         "parameters": {"type": "object", "properties": {...}}}}

    Converse format::

        {"toolSpec": {"name": "...", "description": "...",
         "inputSchema": {"json": {"type": "object", "properties": {...}}}}}
    functionnamer(   description
parametersobject)type
propertiestoolSpecjson)r   r   inputSchema)r,   append)r}   resulttfnr   r   r   s          r   convert_tools_to_converser     s      	F  UU:r""vvfb!!ff]B//VVL82*N*NOO
* &
3 
 	 	 	 	 Mr   c                 (   | ddigS t          | t                    r|                                 rd| ignddigS t          | t                    rg }| D ]}t          |t                    r|                    d|i           0t          |t
                    sF|                    dd          }|dk    r2|                    dd          }|                    d|r|ndi           |dk    r|                    di           }t          |t
                    r|                    dd          nd}|                    d          r|                    d	          \  }}}	d
}
|                    d          r'|dd         	                    d          d         }|r|}
|                    dd|
v r|
	                    d          d         ndd|	idi           |                    dd| di           |r|nddigS dt          |           igS )u  Convert OpenAI message content (string or list) to Converse content blocks.

    Handles:
      - Plain text strings → [{"text": "..."}]
      - Content arrays with text/image_url parts → mixed text/image blocks

    Filters out empty text blocks — Bedrock's Converse API rejects messages
    where a text content block has an empty ``text`` field (ValidationException:
    "text content blocks must be non-empty"). Ref: issue #9486.
    Ntext r   r(   	image_urlurlzdata:,z
image/jpeg   ;r   image/jpegbytes)formatsourcez[Image: ])
r>   strrR   listr   dictr,   r9   	partitionsplit)contentblockspart	part_typer   r   r   header_data
media_type	mime_parts               r   _convert_content_to_converser     sj    '3 K&-mmooJ!""VSM?J'4   !5 	? 	?D$$$ vtn---dD)) ,,IF""xx++vt'<tt=>>>>k)) HH["55	2<Y2M2MUimmE2...SU>>'** ?&)mmC&8&8OFAt!-J((11 3$*122J$4$4S$9$9!$<	$ 3)2JMMCF*CTCTj&6&6s&;&;B&?&?Z`'.o" "#     MM6+<c+<+<+<"=>>>4vvvsm_4S\\"##r   messagesc           
      z   g }g }| D ]}|                     dd          }|                     d          }|dk    rt          |t                    r,|                                r|                    d|i           nt          |t
                    r|D ]}t          |t                    rE|                     d          dk    r,|                    d|                     dd          i           \t          |t                    r|                    d|i           |dk    r|                     dd          }t          |t                    r|nt          j        |          }d	|d|igd
i}	|r4|d         d         dk    r"|d         d                             |	           n|                    d|	gd           |dk    rg }
t          |t                    r,|                                r|
                    d|i           n7t          |t
                    r"|
	                    t          |                     |                     dg           }|pg D ]}|                     di           }|                     dd          }	 t          |t                    rt          j        |          n|}n# t          j        t          f$ r i }Y nw xY w|
                    d|                     dd          |                     dd          |di           |
sddig}
|r4|d         d         dk    r"|d         d         	                    |
           n|                    d|
d           h|dk    r_t          |          }
|r4|d         d         dk    r"|d         d         	                    |
           n|                    d|
d           ͐|r.|d         d         dk    r|                    ddddigd           |r-|d         d         dk    r|                    dddigd           |r|nd|fS )u  Convert OpenAI-format messages to Bedrock Converse format.

    Returns ``(system_prompt, converse_messages)`` where:
      - ``system_prompt`` is a list of system content blocks (or None)
      - ``converse_messages`` is the conversation in Converse format

    Handles:
      - System messages → extracted as system prompt
      - User messages → ``{"role": "user", "content": [...]}``
      - Assistant messages → ``{"role": "assistant", "content": [...]}``
      - Tool calls → ``{"toolUse": {"toolUseId": ..., "name": ..., "input": ...}}``
      - Tool results → ``{"toolResult": {"toolUseId": ..., "content": [...]}}``

    Converse requires strict user/assistant alternation. Consecutive messages
    with the same role are merged into a single message.
    roler(   r   systemr   r   tooltool_call_id
toolResult)	toolUseIdr   r   user)r   r   	assistant
tool_callsr   	argumentsz{}toolUserj   r   )r   r   inputr   r   N)r,   r>   r   rR   r   r   r   r   dumpsextendr   loadsJSONDecodeError	TypeErrorinsert)r   system_blocksconverse_msgsmsgr   r   r   r   result_contenttool_result_blockcontent_blocksr   tcr   args_str	args_dicts                   r   convert_messages_to_converser     s   & !#M "M T Twwvr""'')$$8'3'' =GMMOO =$$fg%67777GT** =# = =D!$-- =$((62B2Bf2L2L%,,fdhhvr6J6J-KLLLL#D#.. =%,,fd^<<<6>>77>266L(27C(@(@YWWdjQXFYFYN!-!' 89 !  r!26!:f!D!Db!),334EFFFF$$" 12& &    ;N'3'' MGMMOO M%%vw&78888GT** M%%&B7&K&KLLL r22J!'R  VVJ++66+t44#8B8S8Q8Q _
8 4 4 4W_II,i8 # # # "III#%%%'VVD"%5%5 "vr 2 2!*   '     " 1#)3-  r!26!:k!I!Ib!),33NCCCC$$'-& &    6>>9'BBN r!26!:f!D!Db!),33NCCCC$$"-& &       Nq)&1V;;Qfc]O L LMMM  Kr*62f<<f63-IIJJJ*4MMmDDs   
+J66KKstop_reasonc                 @    ddddddd}|                     | d          S )zAMap Bedrock Converse stop reasons to OpenAI finish_reason values.stopr   lengthcontent_filter)end_turnstop_sequencetool_use
max_tokenscontent_filteredguardrail_intervened)r,   )r   mappings     r   _converse_stop_reason_to_openair   [  s8      , 0 G ;;{F+++r   responsec                    |                      di           }|                     di           }|                     dg           }|                      dd          }g }g }|D ]}d|v r|                    |d                    "d|v r|d         }|                    t          |                     dd	          d
t          |                     dd	          t          j        |                     di                                                    t          d|rd                    |          nd|r|nd          }	|                      di           }
t          |
                     dd          |
                     dd          |
                     dd          |
                     dd          z             }t          |          }|r|dk    rd}t          d|	|          }t          |g||                      dd	                    S )u  Convert a Bedrock Converse API response to an OpenAI-compatible object.

    The agent loop in ``run_agent.py`` expects responses shaped like
    ``openai.ChatCompletion`` — this function bridges the gap.

    Returns a SimpleNamespace with:
      - ``.choices[0].message.content`` — text response
      - ``.choices[0].message.tool_calls`` — tool call list (if any)
      - ``.choices[0].finish_reason`` — stop/tool_calls/length
      - ``.usage`` — token usage stats
    outputmessager   
stopReasonr   r   r   r   r(   r   r   r   r   r   rj   r   r   r   
Nr   r   r   usageinputTokensr   outputTokensprompt_tokenscompletion_tokenstotal_tokensr   r   indexr   finish_reasonmodelIdchoicesr   model)r,   r   r   r   r   joinr   )r   r   r   r   r   
text_partsr   blocktur   
usage_datar   r   choices                 r   normalize_converse_responser   h  s@    \\(B''FjjB''G[[B//N,,|Z88KJJ  U??eFm,,,,%y!Bo66+r**(++"j)<)<==        )3=		*%%%!+5::  C gr**J nn]A66$..;;NN=!,,z~~na/P/PP	  E 4K@@M %mv--$#  F ll9b))   r   c                      t          |           S )u  Consume a Bedrock ConverseStream event stream and build an OpenAI-compatible response.

    Processes the stream events in order:
      - ``messageStart`` — role info
      - ``contentBlockStart`` — new text or toolUse block
      - ``contentBlockDelta`` — incremental text or toolUse input
      - ``contentBlockStop`` — block complete
      - ``messageStop`` — stop reason
      - ``metadata`` — usage stats

    Returns the same shape as ``normalize_converse_response()``.
    )stream_converse_with_callbacks)event_streams    r    normalize_converse_stream_eventsr     s     *,777r   c                    g }g }d}g }d}	d}
i }|                      dg           D ]}|r |            r nd|v r|d                              di           }d|v r{d}	|r*|                    d	                    |                     g }|d                              d
d	          |d                              dd	          d	d}|r ||d                    d|v r|d                              di           }d|v r.|d         }|                    |           |r|	s ||           d|v r.|*|dxx         |d                              dd	          z  cc<   6d|v rB|d         }t          |t                    r%|                     dd	          }|r|r ||           ~d|v r|	 |d         rt          j        |d                   ni }n# t
          j        t          f$ r i }Y nw xY w|                    t          |d
         dt          |d         t          j
        |                                         d}|r*|                    d	                    |                     g }Ld|v r|d                              dd          }
nd|v rI|d                              di           }|                     dd          |                     dd          d}|r(|                    d	                    |                     t          d|rd                     |          nd|r|nd!          }t          |                     dd          |                     dd          |                     dd          |                     dd          z   "          }t          |
          }|r|d#k    rd$}t          d||%          }t          |g|d	&          S )'a  Process a Bedrock ConverseStream event stream with real-time callbacks.

    This is the core streaming function that powers both the CLI's live token
    display and the gateway's progressive message updates.

    Args:
        event_stream: The boto3 ``converse_stream()`` response containing a
            ``stream`` key with an iterable of events.
        on_text_delta: Called with each text chunk as it arrives. Only fires
            when no tool_use blocks have been seen (same semantics as the
            Anthropic and chat_completions streaming paths).
        on_tool_start: Called with the tool name when a toolUse block begins.
            Lets the TUI show a spinner while tool arguments are generated.
        on_reasoning_delta: Called with reasoning/thinking text chunks.
            Bedrock surfaces thinking via ``reasoning`` content block deltas
            on supported models (Claude 4.6+).
        on_interrupt_check: Called on each event. Should return True if the
            agent has been interrupted and streaming should stop.

    Returns:
        An OpenAI-compatible SimpleNamespace response, identical in shape to
        ``normalize_converse_response()``.
    NFr   streamcontentBlockStartstartr   Tr(   r   r   )r   r   
input_jsoncontentBlockDeltadeltar   r   r   reasoningContentcontentBlockStopr   r   r   messageStopr   metadatar   r   r   r   )r   r   r   r   r   r   r   r   r   r   )r,   r   r   r>   r   r   r   r   r   r   r   r   )r   on_text_deltaon_tool_starton_reasoning_deltaon_interrupt_checkr   r   current_toolcurrent_text_bufferhas_tool_user   r   eventr   r   r   	reasoningthinking_text
input_dict
meta_usager   r   r   r   s                           r   r   r     s   < J(*J#'L%'LK!#J!!(B// D D 	"4"4"6"6 	E%''-.227B??EE!!#& -%%bgg.A&B&BCCC*,'!&y!1!5!5k2!F!F!),00<<"$   
 ! 8!M,v"6777 E))-.227B??EV}#**4000 ! ( (!M$'''e##+ ...%	2B2F2FwPR2S2SS...#u,,!"45	i.. :$-MM&"$=$=M$ :); :**=9995(('$KWXdKe!mL,F!G!G!GkmJJ,i8 $ $ $!#JJJ$!!/#K0#,)&1"&*Z"8"8  # # #     $$ )!!"''*=">">???&(#e##.22<LLKK5  z*..w;;J)~~mQ?? *~q A A J  8"''"566777
)3=		*%%%!+5::  C  nn]A66$..;;NN=!,,z~~na/P/PP	  E 4K@@M %mv--$#  F    s   *$GG*)G*   r   r   temperaturetop_pstop_sequencesguardrail_configc                 .   t          |          \  }}	| |	d|id}
|r||
d<   |||
d         d<   |||
d         d<   |r||
d         d<   |rCt          |          }|r2t          |           rd	|i|
d
<   nt                              d|            |r||
d<   |
S )zBuild kwargs for ``bedrock-runtime.converse()`` or ``converse_stream()``.

    Converts OpenAI-format inputs to Converse API parameters.
    	maxTokens)r   r   inferenceConfigr   Nr  r  topPstopSequencesr}   
toolConfigud   Model %s does not support tool calling — tools stripped. The agent will operate in text-only mode.guardrailConfig)r   r   rx   loggerwarning)r   r   r}   r   r  r  r  r  system_promptconverse_messageskwargsconverse_toolss               r   build_converse_kwargsr#  S  s	    (DH'M'M$M$ %
 F  )(x3> !-0,1 !&) D5C !/2 2599 	 (.. (/'@|$$@AF  
  5$4 !Mr   c	           
      J   t          |           }	t          ||||||||          }
	  |	j        di |
}n`# t          $ rS}t	          |          r>t
                              d| |t          |          j                   t          |             d}~ww xY wt          |          S )zCall Bedrock Converse API (non-streaming) and return an OpenAI-compatible response.

    This is the primary entry point for the agent loop when using the Bedrock provider.
    r   r   r}   r   r  r  r  r  u|   bedrock: stale-connection error on converse(region=%s, model=%s): %s — evicting cached client so the next call reconnects.Nr   )r   r#  converserY   rG   r  r  r   r'   r#   r   r   r   r   r}   r   r  r  r  r  r   r!  r   r$   s                r   call_converser(    s     )00F"%)	 	 	F
"6?,,V,,   $S)) 	.NNMtCyy1  
 &f--- 'x000   6 
B ABBc	           
      J   t          |           }	t          ||||||||          }
	  |	j        di |
}n`# t          $ rS}t	          |          r>t
                              d| |t          |          j                   t          |             d}~ww xY wt          |          S )zCall Bedrock ConverseStream API and return an OpenAI-compatible response.

    Consumes the full stream and returns the assembled response. For true
    streaming with delta callbacks, use ``iter_converse_stream()`` instead.
    r%  u   bedrock: stale-connection error on converse_stream(region=%s, model=%s): %s — evicting cached client so the next call reconnects.Nr   )r   r#  converse_streamrY   rG   r  r  r   r'   r#   r   r'  s                r   call_converse_streamr,    s      )00F"%)	 	 	F
)6)33F33   $S)) 	.NNXtCyy1  
 &f--- ,H555r)  _discovery_cachei  c                  8    t                                            dS )z/Clear the model discovery cache. Used in tests.N)r-  r   r   r   r   reset_discovery_cacher/    s    r   provider_filterc           
      	   ddl }|  dd                    t          |pg                      }t                              |          }|r.|                                 |d         z
  t
          k     r|d         S 	 t          |           }n4# t          $ r'}t          	                    d|           g cY d}~S d}~ww xY wg }t                      }d |pg D             	 |                                }	|	                    d	g           D ]}
|
                    d
          pd                                }|s/re|
                    d          pd                                }d|v r-|                    d          d                                         nd}|vr|vr|
                    di           }|                    dd                                          dk    r|
                    dd          s|
                    dg           }d|vr|                    ||
                    d          p|                                |
                    d          pd                                |
                    dg           |dd           |                    |                                           n2# t          $ r%}t          	                    d|           Y d}~nd}~ww xY w	 g }d}	 i }|r||d<    |j        d$i |}	|	                    dg           D ]}|                    |           |	                    d          }|sn]|D ]}|                    d          pd                                }|s/|                    d          dk    rI|                                |v r`r4|                    dg           }t'          fd|D                       }|s|                    ||                    d          p|                                ddgdgdd           |                    |                                           n2# t          $ r%}t                              d |           Y d}~nd}~ww xY w|                    d! "           |                                 |d#t          |<   |S )%a  Discover available Bedrock foundation models and inference profiles.

    Returns a list of model info dicts with keys:
      - ``id``: Model ID (e.g. "anthropic.claude-sonnet-4-6-20250514-v1:0")
      - ``name``: Human-readable name
      - ``provider``: Model provider (e.g. "Anthropic", "Amazon", "Meta")
      - ``input_modalities``: List of input types (e.g. ["TEXT", "IMAGE"])
      - ``output_modalities``: List of output types
      - ``streaming``: Whether streaming is supported

    Caches results for 1 hour per region to avoid repeated API calls.

    Mirrors OpenClaw's ``discoverBedrockModels()`` in
    ``extensions/amazon-bedrock/discovery.ts``.
    r   N:r   	timestampmodelsz7Failed to create Bedrock client for model discovery: %sc                 6    h | ]}|                                 S r   )rv   )r:   fs     r   	<setcomp>z*discover_bedrock_models.<locals>.<setcomp>  s     ===!''))===r   modelSummariesr   r(   providerName.modelLifecyclestatusACTIVEresponseStreamingSupportedFoutputModalitiesTEXT	modelNameinputModalitiesT)rj   r   providerinput_modalitiesoutput_modalities	streamingz,Failed to list Bedrock foundation models: %s	nextTokeninferenceProfileSummariesinferenceProfileIdc              3      K   | ];}t          |                    d d                                                    v V  <dS )modelArnr(   N)_extract_provider_from_arnr,   rv   )r:   rl   
filter_sets     r   r<   z*discover_bedrock_models.<locals>.<genexpr>Q  s_         /quuZ/D/DEEKKMMQ[[     r   inferenceProfileNamezinference-profilez(Skipping inference profile discovery: %sc                 t    | d                              d          rdnd| d                                         fS )Nrj   rz   r      r   )r9   rv   )rl   s    r   <lambda>z)discover_bedrock_models.<locals>.<lambda>e  s8    tW	**1	&	 r   )key)r3  r4  r   )timer   sortedr-  r,   _DISCOVERY_CACHE_TTL_SECONDSr   rY   r  r  setlist_foundation_modelsrR   rv   r   upperr   addlist_inference_profilesrA   debugsort)r   r0  rS  	cache_keycachedr   er4  seen_idsr   summaryrq   provider_namemodel_prefix	lifecycleoutput_modsprofiles
next_tokenr!  profile
profile_idprofile_modelsmatchesrM  s                          @r   rn   rn     s   & KKKEECHHVO,Ar%B%BCCEEI!!),,F  499;;!448TTTh,V44   PRSTTT						 FuuH==o&;===J"J0022||$4b99 	+ 	+GI..4";;==H   !(^!<!<!B I I K KADx~~c2215;;===VX 
22|:7U7U  $4b99I}}Xr**0022h>>;;;UCC !++&8"==K[((MM [11=XDDFF$[[88>BEEGG$+KK0A2$F$F%0!     LL))****=	+>  J J JEqIIIIIIIIJ+D
		F 1&0{#5v5????H#<<(CRHH ) )((((!k22J 		   	- 	-G!++&:;;ArHHJJJ {{8$$00!!X--  !(Xr!:!:    +      MM  %;<<J
QQSS/%+H&,X!     LL))++,,,,7	-8  D D D?CCCCCCCCD KK  K    YY[[# #Y MsP   :B
 

B;B60B;6B;GJ; ;
K*K%%K*.E(Q 
R!RRarnc                 ^    t          j        d|           }|r|                    d          ndS )u   Extract the model provider from a Bedrock model ARN.

    Example: "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-v2"
    → "anthropic"
    zfoundation-model/([^.]+)rP  r(   )researchgroup)rl  matchs     r   rL  rL  q  s/     I1377E"*5;;q>>>*r   c                 8    t          |           }d |D             S )zReturn a flat list of available Bedrock model IDs for the given region.

    Convenience wrapper around ``discover_bedrock_models()`` for use in
    the model selection UI.
    c                     g | ]
}|d          S ri   r   rk   s     r   rm   z)get_bedrock_model_ids.<locals>.<listcomp>  s    $$$AdG$$$r   )rn   )r   r4  s     r   get_bedrock_model_idsrt  {  s%     %V,,F$$V$$$$r   zNValidationException.*(?:input is too long|max input token|input token.*exceed)zVValidationException.*(?:exceeds? the (?:maximum|max) (?:number of )?(?:input )?tokens)zFModelStreamErrorException.*(?:Input is too long|too many input tokens)ThrottlingExceptionzToo many concurrent requestsServiceQuotaExceededExceptionModelNotReadyExceptionModelTimeoutExceptionInternalServerExceptionerror_messagec                 D     t           fdt          D                       S )zReturn True if the error indicates the input context was too large.

    When this returns True, the agent should compress context and retry
    rather than treating it as a fatal error.
    c              3   B   K   | ]}|                               V  d S r8   ro  r:   prz  s     r   r<   z,is_context_overflow_error.<locals>.<genexpr>  s/      JJ1qxx&&JJJJJJr   )rA   CONTEXT_OVERFLOW_PATTERNSrz  s   `r   is_context_overflow_errorr    s)     JJJJ0IJJJJJJr   c                      t                     rdS t           fdt          D                       rdS t           fdt          D                       rdS dS )uC  Classify a Bedrock error for retry/failover decisions.

    Returns:
      - ``"context_overflow"`` — input too long, compress and retry
      - ``"rate_limit"`` — throttled, backoff and retry
      - ``"overloaded"`` — model temporarily unavailable, retry with delay
      - ``"unknown"`` — unclassified error
    context_overflowc              3   B   K   | ]}|                               V  d S r8   r}  r~  s     r   r<   z)classify_bedrock_error.<locals>.<genexpr>  /      
>
>q188M""
>
>
>
>
>
>r   
rate_limitc              3   B   K   | ]}|                               V  d S r8   r}  r~  s     r   r<   z)classify_bedrock_error.<locals>.<genexpr>  r  r   
overloadedunknown)r  rA   THROTTLE_PATTERNSOVERLOAD_PATTERNSr  s   `r   classify_bedrock_errorr    sv     !// "!!

>
>
>
>,=
>
>
>>> |

>
>
>
>,=
>
>
>>> |9r   zanthropic.claude-opus-4-6i@ zanthropic.claude-sonnet-4-6zanthropic.claude-sonnet-4-5zanthropic.claude-haiku-4-5zanthropic.claude-opus-4zanthropic.claude-sonnet-4zanthropic.claude-3-5-sonnetzanthropic.claude-3-5-haikuzanthropic.claude-3-opuszanthropic.claude-3-sonnetzanthropic.claude-3-haikuzamazon.nova-proi zamazon.nova-litezamazon.nova-microi  zmeta.llama4-maverickzmeta.llama4-scoutzmeta.llama3-3-70b-instruct)zmistral.mistral-largezdeepseek.v3BEDROCK_CONTEXT_LENGTHSc                     |                                  }d}t          }t                                          D ]-\  }}||v r$t	          |          t	          |          k    r|}|}.|S )zLook up the context window size for a Bedrock model.

    Uses substring matching so versioned IDs like
    ``anthropic.claude-sonnet-4-6-20250514-v1:0`` resolve correctly.
    r(   )rv   BEDROCK_DEFAULT_CONTEXT_LENGTHr  itemsr{   )rq   ru   best_keybest_valrR  vals         r   get_bedrock_context_lengthr    sm     ..""KH-H+1133  S+#c((S]]":":HHOr   r8   )NNNN)Nr  NNNN)B__doc__r   loggingrP   rn  typesr   typingr   r   r   r   r   	getLoggerr'   r  r	   r   __annotations__r
   r   r   r   r   boolr#   rB   BaseExceptionr1   rG   _AWS_CREDENTIAL_ENV_VARSr^   r`   rf   rp   rw   rx   r|   r   r   r   r   r   r   r   intfloatr#  r(  r,  r-  rU  r/  rn   rL  rt  compile
IGNORECASEr  r  r  r  r  r  r  r  r   r   r   <module>r     s	    :   				 				 ! ! ! ! ! ! 3 3 3 3 3 3 3 3 3 3 3 3 3 3		8	$	$ 13 tCH~ 2 2 202 tCH~ 2 2 2

 

 


1 
1 
1 
1 
11 1 1 1 1* * *c d    F =    5= 5T 5 5 5 5B   ' '(4S>": 'hsm ' ' ' 'T Xd38n5     > c3h 8 C    >8DI#6    <   Us Ut U U U U6 6 6 6 6 64T$Z DJ    >1$T$Z 1$ 1$ 1$ 1$htE4jtE
8DJd+,tE tE tE tEv
, 
, 
, 
, 
, 
,A$ A? A A A AP8o 8 8 8 8$ L L L L L Ll #'#'!*.'+7 774j7 DJ7 	7
 %7 E?7 T#Y'7 tn7 
#s(^7 7 7 7| #'#'!*.'+&1 &1&1&1 4j&1 DJ	&1
 &1 %&1 E?&1 T#Y'&1 tn&1 &1 &1 &1 &1Z #'#'!*.'+'6 '6'6'6 4j'6 DJ	'6
 '6 %'6 E?'6 T#Y''6 tn'6 '6 '6 '6 '6\ $& $sCx. % % %#    ,0A AAd3i(A 
$sCx.A A A AH+C +C + + + +%# %$s) % % % %& BJ`bdboppBJhjljwxxBJXZ\Zghh  BJ%r}55BJ.>>BJ/??  BJ("-88BJ'77BJ)2=99 KS KT K K K K# #    2+W+ "W+ "W	+
 !W+ W+  W+ "W+ !W+ W+  W+ W+ W+ W+  W!+$ W%+& W'+( !W)+, &-%,1+ + + c3h   8 ")        r   