
    Ui0                        d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ erddlmZ dd	lm Z   G d
 de      Z!g dZ"y)z1PII detection and handling middleware for agents.    )annotations)TYPE_CHECKINGAnyLiteral)	AIMessage
AnyMessageHumanMessageToolMessage)override)
PIIDetectionErrorPIIMatchRedactionRuleResolvedRedactionRuleapply_strategydetect_credit_carddetect_email	detect_ipdetect_mac_address
detect_url)AgentMiddleware
AgentStatehook_config)Callable)Runtimec                       e Zd ZdZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZedd       Zdd	Z ed
g      e		 	 	 	 	 	 dd              Z
 ed
g      	 	 	 	 	 	 dd       Ze		 	 	 	 	 	 dd       Z	 	 	 	 	 	 ddZ xZS )PIIMiddlewarea	  Detect and handle Personally Identifiable Information (PII) in conversations.

    This middleware detects common PII types and applies configurable strategies
    to handle them. It can detect emails, credit cards, IP addresses, MAC addresses, and
    URLs in both user input and agent output.

    Built-in PII types:

    - `email`: Email addresses
    - `credit_card`: Credit card numbers (validated with Luhn algorithm)
    - `ip`: IP addresses (validated with stdlib)
    - `mac_address`: MAC addresses
    - `url`: URLs (both `http`/`https` and bare URLs)

    Strategies:

    - `block`: Raise an exception when PII is detected
    - `redact`: Replace PII with `[REDACTED_TYPE]` placeholders
    - `mask`: Partially mask PII (e.g., `****-****-****-1234` for credit card)
    - `hash`: Replace PII with deterministic hash (e.g., `<email_hash:a1b2c3d4>`)

    Strategy Selection Guide:

    | Strategy | Preserves Identity? | Best For                                |
    | -------- | ------------------- | --------------------------------------- |
    | `block`  | N/A                 | Avoid PII completely                    |
    | `redact` | No                  | General compliance, log sanitization    |
    | `mask`   | No                  | Human readability, customer service UIs |
    | `hash`   | Yes (pseudonymous)  | Analytics, debugging                    |

    Example:
        ```python
        from langchain.agents.middleware import PIIMiddleware
        from langchain.agents import create_agent

        # Redact all emails in user input
        agent = create_agent(
            "openai:gpt-5",
            middleware=[
                PIIMiddleware("email", strategy="redact"),
            ],
        )

        # Use different strategies for different PII types
        agent = create_agent(
            "openai:gpt-4o",
            middleware=[
                PIIMiddleware("credit_card", strategy="mask"),
                PIIMiddleware("url", strategy="redact"),
                PIIMiddleware("ip", strategy="hash"),
            ],
        )

        # Custom PII type with regex
        agent = create_agent(
            "openai:gpt-5",
            middleware=[
                PIIMiddleware("api_key", detector=r"sk-[a-zA-Z0-9]{32}", strategy="block"),
            ],
        )
        ```
    redactNTF)strategydetectorapply_to_inputapply_to_outputapply_to_tool_resultsc               0   t         |           || _        || _        || _        t        |||      j                         | _        | j                  j                  | _        | j                  j                  | _	        | j                  j                  | _
        y)a  Initialize the PII detection middleware.

        Args:
            pii_type: Type of PII to detect.

                Can be a built-in type (`email`, `credit_card`, `ip`, `mac_address`,
                `url`) or a custom type name.
            strategy: How to handle detected PII.

                Options:

                * `block`: Raise `PIIDetectionError` when PII is detected
                * `redact`: Replace with `[REDACTED_TYPE]` placeholders
                * `mask`: Partially mask PII (show last few characters)
                * `hash`: Replace with deterministic hash (format: `<type_hash:digest>`)

            detector: Custom detector function or regex pattern.

                * If `Callable`: Function that takes content string and returns
                    list of `PIIMatch` objects
                * If `str`: Regex pattern to match PII
                * If `None`: Uses built-in detector for the `pii_type`
            apply_to_input: Whether to check user messages before model call.
            apply_to_output: Whether to check AI messages after model call.
            apply_to_tool_results: Whether to check tool result messages after tool execution.

        Raises:
            ValueError: If `pii_type` is not built-in and no detector is provided.
        )pii_typer   r   N)super__init__r    r!   r"   r   resolve_resolved_ruler$   r   r   )selfr$   r   r   r    r!   r"   	__class__s          l/var/www/html/eventheodds/airagagent/rag_env/lib/python3.12/site-packages/langchain/agents/middleware/pii.pyr&   zPIIMiddleware.__init__^   s    R 	,.%:"5B6
 ')	 	
 ++44++44++44    c                N    | j                   j                   d| j                   dS )zName of the middleware.[])r*   __name__r$   )r)   s    r+   namezPIIMiddleware.name   s&     ..))*!DMM?!<<r,   c                f    | j                  |      }|s|g fS t        ||| j                        }||fS )z<Apply the configured redaction rule to the provided content.)r   r   r   )r)   contentmatches	sanitizeds       r+   _process_contentzPIIMiddleware._process_content   s:    --(B;"7GT]]C	'!!r,   end)can_jump_toc                   | j                   s| j                  sy|d   }|syt        |      }d}| j                   rd}d}t        t	        |      dz
  dd      D ]  }t        ||   t              s||   }|} n |b|r`|j                  rTt        |j                        }	| j                  |	      \  }
}|r)t        |
|j                  |j                        }|||<   d}| j                  rd}t        t	        |      dz
  dd      D ]  }t        ||   t              s|} n |t        |dz   t	        |            D ]  }||   }t        |t              s|}|j                  s(t        |j                        }	| j                  |	      \  }
}|sTt        |
|j                  |j                  |j                        }|||<   d} |rd|iS y)	a  Check user messages and tool results for PII before model invocation.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            Updated state with PII handled according to strategy, or `None` if no PII
                detected.

        Raises:
            PIIDetectionError: If PII is detected and strategy is `'block'`.
        NmessagesF   )r3   idr1   T)r3   r=   r1   tool_call_id)r    r"   listrangelen
isinstancer	   r3   strr6   r=   r1   r   r
   r>   )r)   stateruntimer:   new_messagesany_modifiedlast_user_msglast_user_idxir3   new_contentr4   updated_messagelast_ai_idxmsgtool_msgs                   r+   before_modelzPIIMiddleware.before_model   s   ( ""4+E+E$H~  M M3x=1,b"5 hqk<8$,QKM$%M	 (]}?T?Tm334'+'<'<W'E$W2> +(++*//3O 3BL/#'L %%K3x=1,b"5 hqk95"#K
 &{QH> ,A"1+C!#{3#&'//$"%h&6&6"7/3/D/DW/M,W&$ +6$/'{{!))1)>)>	+ +:Q'+-,0 --r,   c                .   K   | j                  ||      S w)a  Async check user messages and tool results for PII before model invocation.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            Updated state with PII handled according to strategy, or `None` if no PII
                detected.

        Raises:
            PIIDetectionError: If PII is detected and strategy is `'block'`.
        )rP   r)   rD   rE   s      r+   abefore_modelzPIIMiddleware.abefore_model  s     &   00   c                   | j                   sy|d   }|syd}d}t        t        |      dz
  dd      D ]  }||   }t        |t              s|}|} n ||r|j
                  syt        |j
                        }| j                  |      \  }	}
|
syt	        |	|j                  |j                  |j                        }t        |      }|||<   d|iS )a  Check AI messages for PII after model invocation.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            Updated state with PII handled according to strategy, or None if no PII
                detected.

        Raises:
            PIIDetectionError: If PII is detected and strategy is `'block'`.
        Nr:   r;   r<   )r3   r=   r1   
tool_calls)r!   r@   rA   rB   r   r3   rC   r6   r=   r1   rV   r?   )r)   rD   rE   r:   last_ai_msgrM   rJ   rN   r3   rK   r4   rL   rF   s                r+   after_modelzPIIMiddleware.after_model  s    & ##$ s8}q("b1 	A1+C#y)!	 k9L9L k))*#44W=W $~~!!"--	
 H~$3[!L))r,   c                .   K   | j                  ||      S w)a  Async check AI messages for PII after model invocation.

        Args:
            state: The current agent state.
            runtime: The langgraph runtime.

        Returns:
            Updated state with PII handled according to strategy, or None if no PII
                detected.

        Raises:
            PIIDetectionError: If PII is detected and strategy is `'block'`.
        )rX   rR   s      r+   aafter_modelzPIIMiddleware.aafter_modelT  s     $ w//rT   )r$   zALiteral['email', 'credit_card', 'ip', 'mac_address', 'url'] | strr   z*Literal['block', 'redact', 'mask', 'hash']r   z,Callable[[str], list[PIIMatch]] | str | Noner    boolr!   r[   r"   r[   returnNone)r\   rC   )r3   rC   r\   ztuple[str, list[PIIMatch]])rD   r   rE   r   r\   zdict[str, Any] | None)r0   
__module____qualname____doc__r&   propertyr1   r6   r   r   rP   rS   rX   rZ   __classcell__)r*   s   @r+   r   r      sP   =J @HAE# %&+65 T	65 =65 ?65 65 65  $65 
65p = =" eW%\\ \ 
	\  &\| eW%11 1 
	1 &1( 9*9* 9* 
	9* 9*v00 0 
	0r,   r   )r   r   r   r   r   r   r   N)#r`   
__future__r   typingr   r   r   langchain_core.messagesr   r   r	   r
   typing_extensionsr   &langchain.agents.middleware._redactionr   r   r   r   r   r   r   r   r   r   !langchain.agents.middleware.typesr   r   r   collections.abcr   langgraph.runtimer   r   __all__ r,   r+   <module>rm      sQ    7 " . . T T &   W V()H0O H0V
r,   