o
    pj                     @  s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ h d	Zed
d
dG dd dZd+d,ddZd+d-ddZd.ddZd/d!d"Zd0d$d%Zd1d(d)Zd*S )2z8Agent Profile v1 contracts and effective tool filtering.    )annotations)	dataclassfield)Any)select)get_control_plane_session)
CAPABILITY
IDENTIFIERSEMVER)AgentProfile>   neveralwayspolicyT)frozenslotsc                   @  s   e Zd ZU ded< ded< ded< ded< dZded< eed	Zd
ed< dZded< dZ	ded< dZ
ded< dZded< dddZdddZdS )AgentProfileContractstr	agent_keynameversiontuple[str, ...]allowed_capabilities allowed_roles)default_factoryzdict[str, str]confirmation_policy   int	max_stepsNz
int | Nonemonthly_token_limitz1.0.0instructions_version descriptionreturn	list[str]c                 C  s>  g }t | js|d | j r| j s|d t| jr(t| j	s-|d | j
s5|d | j
D ]}t|sG|d|  q8| jD ]}t |sZ|d|  qK| j D ]\}}|dvrp|d|  |tvr||d	|  q`d
| j  krdksn |d | jd ur| jdk r|d |S )Nzagent_key is invalidz/agent profile name and description are requiredz3agent profile versions must use semantic versioningz6agent profile requires at least one allowed capabilityzinvalid profile capability: zinvalid profile role: >   read	calculatewrite_reversiblewrite_irreversiblez'invalid profile confirmation category: z%invalid profile confirmation policy:       z0agent profile max_steps must be between 1 and 25r   z4agent profile monthly_token_limit cannot be negative)r	   	fullmatchr   appendr   stripr"   r
   r   r    r   r   r   r   itemsCONFIRMATION_VALUESr   r   )selferrors
capabilityrolecategoryr   r   r   "bridge_platform/agents/profiles.pyvalidate   s8   









zAgentProfileContract.validatedict[str, Any]c              
   C  s>   | j | j| j| jt| jt| jt| j| j	| j
d| jd	S )N)r   r   )	r   r   r   r"   r   r   r   limitsr    )r   r   r   r"   listr   r   dictr   r   r   r    )r0   r   r   r5   to_dict:   s   zAgentProfileContract.to_dict)r#   r$   )r#   r7   )__name__
__module____qualname____annotations__r   r   r:   r   r   r   r    r"   r6   r;   r   r   r   r5   r      s   
 
r   r   	tenant_idr   rolesr   r#   list[AgentProfileContract]c                   sx   t  !}|tttj| ktjdktj	 
 }W d    n1 s'w   Y  dd |D } fdd|D S )Nactivec                 S  s   g | ]}t |qS r   )_contract_from_record).0rowr   r   r5   
<listcomp>Q   s    z!list_profiles.<locals>.<listcomp>c                   s   g | ]	}t | r|qS r   )_roles_allow)rE   profilerA   r   r5   rG   R   s    )r   executer   r   wherer@   statusorder_bydisplay_namescalarsall)r@   rA   sessionrowsprofilesr   rJ   r5   list_profilesK   s   rU   r   c                   s6   t | |}t fdd|D d }|d u rtd|S )Nc                 3  s    | ]
}|j  kr|V  qd S Nr   )rE   itemrW   r   r5   	<genexpr>W   s    zget_profile.<locals>.<genexpr>z4Agent profile is unavailable for this tenant or role)rU   nextPermissionError)r@   r   rA   rT   rI   r   rW   r5   get_profileU   s
   
r\   rI   toolslist[dict[str, Any]]c                 C  s   t | j}g }|D ]5}|d|vrq	t|}|dpi d}| jt|}|r9tt|dp3d||d< || q	|S )Nr2   riskr4   confirmationr   )setr   getr:   r   r   _stricter_confirmationr,   )rI   r]   allowedresulttool	effectiverisk_categoryprofile_confirmationr   r   r5   filter_profile_tools]   s   
rj   recordr   c                 C  s   | j pi }t| j| j| j| jt| jpdt| jpdt	| j
pi t|dd|d| jd
}| }|r?tdd| |S )Nr   r   r   r   )
r   r   r   r"   r   r   r   r   r   r    zInvalid stored Agent Profile: z; )limits_jsonr   r   rO   profile_versionr"   tupleallowed_capabilities_jsonallowed_roles_jsonr:   confirmation_policy_jsonr   rb   r    r6   
ValueErrorjoin)rk   r8   contractr1   r   r   r5   rD   n   s"   
rD   boolc                 C  s   | j  ptt| j |S rV   )r   ru   ra   intersection)rI   rA   r   r   r5   rH      s   rH   tool_policyprofile_policyc                   s$   dddd t | |f fdddS )Nr   r)      )r   r   r   c                   s    |  S rV   r   )valueorderr   r5   <lambda>   s    z(_stricter_confirmation.<locals>.<lambda>)key)max)rw   rx   r   r{   r5   rc      s   rc   N)r   )r@   r   rA   r   r#   rB   )r@   r   r   r   rA   r   r#   r   )rI   r   r]   r^   r#   r^   )rk   r   r#   r   )rI   r   rA   r   r#   ru   )rw   r   rx   r   r#   r   )__doc__
__future__r   dataclassesr   r   typingr   
sqlalchemyr   config.control_planer   bridge_platform.apps.contractsr   r	   r
   bridge_platform.tenants.modelsr   r/   r   rU   r\   rj   rD   rH   rc   r   r   r   r5   <module>   s"    
8



