
    yj                         d dl Z d dlmZmZmZ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 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  e j        e          Z eddg e ed                    g          Ze                    de ej!                 d           ed           edd          efde"dej#        dede ej!                 fd            Z$e                    deej!                            ed           edd           edd          efde"d ej%        dz  d!e&dz  defd"            Z'e                    d#e ej!                            ed           edd$          efde"dej(        dede ej!                 fd%            Z)e*                    d&d'd(           ed           ed          efde"d)e"defd*            Z+dS )+    N)	APIRouterBodyDependsPathQuery)Page)	apaginate)AsyncSession)crudschemas)db)ResourceNotFoundExceptionValidationException)require_auth)EmbeddingCallPurpose)embedding_call_purposez&/workspaces/{workspace_id}/conclusionsconclusionsworkspace_id)workspace_name)prefixtagsdependencies    )response_modelstatus_code.zBatch of Conclusions to create)descriptionbodyr   returnc                    K   t          j        ||j        |            d{V }t                              dt          |          |            d |D             S )z
    Create one or more Conclusions.

    Conclusions are logical certainties derived from interactions between Peers. They form the basis of a Peer's Representation.
    )observationsr   Nz&Created %d conclusions in workspace %sc                 L    g | ]!}t           j                            |          "S  r   
Conclusionmodel_validate.0docs     ?/DATA/AppData/hermes/projects/honcho/src/routers/conclusions.py
<listcomp>z&create_conclusions.<locals>.<listcomp>5   )    HHHsG--c22HHH    )r   create_observationsr   loggerdebuglen)r   r   r   	documentss       r*   create_conclusionsr3      s      $ .
%#        I LL0I  
 IHiHHHHr-   z/list)r   z*Filtering options for the Conclusions listFz'Whether to reverse the order of resultsoptionsreversec                    K   d}|rt          |d          r|j        }|i k    rd}t          j        | ||pd          }t	          ||           d{V S )zv
    List Conclusions using optional filters, ordered by recency unless `reverse` is true. Results are paginated.
    NfiltersF)r   r7   r5   )hasattrr7   r   get_documents_with_filtersr	   )r   r4   r5   r   r7   stmts         r*   list_conclusionsr;   8   s      & G 77I.. /b==G*# 5  D 2t$$$$$$$$$r-   z/queryz*Semantic search parameters for Conclusionsc                   K   d}d}|j         rh|j                             d          p|j                             d          }|j                             d          p|j                             d          }|r|st          d          t          t          j        j        | d          5  t          j        || |j	        |||j         |j
        |j        	           d{V }ddd           n# 1 swxY w Y   d
 |D             S )zi
    Query Conclusions using semantic search. Use `top_k` to control the number of results returned.
    Nobserverobserver_idobservedobserved_idz;observer and observed must be specified for semantic searchapi)r   parent_category)r   queryr=   r?   r7   max_distancetop_kc                 L    g | ]!}t           j                            |          "S r#   r$   r'   s     r*   r+   z%query_conclusions.<locals>.<listcomp>   r,   r-   )r7   getr   r   r   GENERIC_DOCUMENT_SEARCHvaluer   query_documentsrC   distancerE   )r   r   r   r=   r?   r2   s         r*   query_conclusionsrL   Z   s~      HH| S<##J//R4<3C3CM3R3R<##J//R4<3C3CM3R3R 
8 
!I
 
 	
 
 4:#
 
 
 
 

 .'*L*	
 	
 	
 	
 	
 	
 	
 	
 	
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 IHiHHHHs   *7C--C14C1z/{conclusion_id}   )r   r   conclusion_idc           	      .  K   	 t          j        || |           d{V  t                              d|           dS # t          $ r  t
          $ rB}t                              d| dt          |                      t	          d          |d}~ww xY w)zN
    Delete a single Conclusion by ID.

    This action cannot be undone.
    )r   document_idNz"Conclusion %s deleted successfullyzFailed to delete conclusion z: zConclusion not found)r   delete_document_by_idr/   r0   r   
ValueErrorwarningstr)r   rN   r   es       r*   delete_conclusionrV      s      G('%
 
 
 	
 	
 	
 	
 	
 	
 	
 	9=IIIII$    G G GOmOOs1vvOOPPP'(>??QFGs   8> B=BB),loggingfastapir   r   r   r   r   fastapi_paginationr   !fastapi_pagination.ext.sqlalchemyr	   sqlalchemy.ext.asyncior
   srcr   r   src.dependenciesr   src.exceptionsr   r   src.securityr   src.telemetry.eventsr   src.utils.typesr   	getLogger__name__r/   routerpostlistr%   rT   ConclusionBatchCreater3   ConclusionGetboolr;   ConclusionQueryrL   deleterV   r#   r-   r*   <module>rl      s    9 9 9 9 9 9 9 9 9 9 9 9 9 9 # # # # # # 7 7 7 7 7 7 / / / / / /               I I I I I I I I % % % % % % 5 5 5 5 5 5 2 2 2 2 2 2		8	$	$	3
',,nEEEFFG
 
 
 *+    S		*.$4+ + + I II

'I 	I 
'
I I I 
I6 *+   
 S		,0D@- - - !5=   % %%"T)% D[% 	% % %	 %< *+   
 S		$(D@% % % %I %I%I

!%I 	%I 
'
%I %I %I	 %IP     S		cG GGG 	G G G 
G G Gr-   