o
    pj                     @  sz   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 edZG dd	 d	eZdddZddddddZdS )zFDeterministic baseline for resolving user instructions to agent tools.    )annotationsN)Any)Draft202012Validator)AgentProviderErrorToolSelectorz(?<!\d)(?:\d[\s-]?){11}(?!\d)c                      s"   e Zd ZdZd	 fddZ  ZS )
AgentResolutionErrorz?Safe error raised when no basic tool can handle an instruction.codestrmessagereturnNonec                   s   t  | || _d S N)super__init__r   )selfr   r
   	__class__ !bridge_platform/agents/runtime.pyr      s   
zAgentResolutionError.__init__)r   r	   r
   r	   r   r   )__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r      s    r   r
   r	   toolslist[dict[str, Any]]r   dict[str, Any]c           	      C  s   |   } | stddt| dkrtddtdd |D d}t| }|du s-|du r2td	d
tdd|d}|dd}|	dpGi }t
t||dd d}|r\tdd|	dpbi }d|d |d |	d||	d|	d|	dg dS )zAResolve the first supported read-only instruction without an LLM.empty_instructionz#Enter an instruction for the agent.i  instruction_too_longz(The instruction exceeds 1000 characters.c                 s  s"    | ]}| d dkr|V  qdS )
capabilityzbusiness.au.abn.lookupN)get).0toolr   r   r   	<genexpr>    s     z,resolve_basic_instruction.<locals>.<genexpr>Nno_matching_toolzUNo available basic agent tool matched the instruction. Include an exact 11-digit ABN.z\D r   F)abninclude_historyinput_schemac                 S  s
   t | jS r   )listpath)errorr   r   r   <lambda>.   s   
 z+resolve_basic_instruction.<locals>.<lambda>)keyinvalid_tool_argumentszIThe instruction produced arguments that do not satisfy the tool contract.	executiondeterministic_v1r   app_idactionriskconfirmationrequired_permissions)resolverr   r1   r2   	argumentsr3   r4   r5   )stripr   lennextABN_CANDIDATEsearchresubgroupr    sortedr   iter_errors)	r
   r   abn_toolmatchr&   r7   schemavalidation_errorsr/   r   r   r   resolve_basic_instruction   sH   




rF   )contextproviderrG   dict[str, Any] | NonerH   ToolSelector | Nonec          	      C  s   zt | |W S  ty" } z|jdks|du r W Y d}~nd}~ww z|| ||p+i }W n tyB } ztdt||d}~ww |d }|dpMi }|d|j|d|d |d	 |d
|d |d|d|dg d	S )zGPrefer the zero-cost deterministic resolver, then an optional provider.r$   Nai_selector_failedr"   r/   rH   modelr   r1   r2   r7   r3   r4   r5   )	r6   rL   r   r1   r2   r7   r3   r4   r5   )rF   r   r   selectr   r	   r    name)	r
   r   rG   rH   deterministic_errorselectedexcr"   r/   r   r   r   resolve_instructionC   s4   
rR   )r
   r	   r   r   r   r   )
r
   r	   r   r   rG   rI   rH   rJ   r   r   )r   
__future__r   r=   typingr   
jsonschemar    bridge_platform.agents.providersr   r   compiler;   
ValueErrorr   rF   rR   r   r   r   r   <module>   s    

0