
    `gj7+                       d Z ddlmZ ddlmZmZ ddlmZ ddlZddl	m
Z
mZmZmZmZmZmZmZ ed   Zed   Zd$d	Zd%d
Zddd	 	 	 	 	 	 	 d&dZdd	 	 	 	 	 d'dZ	 	 	 	 d(dZ	 	 	 	 	 	 d)dZ	 	 	 	 	 	 d)dZd*dZ	 	 	 	 	 	 	 	 d+dZd*dZ	 	 	 	 	 	 	 	 d+dZd,dZd-dZ d.dZ!d/dZ"d0dZ#d1dZ$d1dZ%d2dZ&d2dZ'd d!d3d"Z(d4d#Z)y)5a  Shared renderers for session export commands.

The CLI, dashboard, and slash-command surfaces all deal with the same
session-shaped data: a session dict with a ``messages`` list. Keep filtering
and human-readable rendering here so each surface only has to load sessions
and write bytes.
    )annotations)datetimetimezone)escapeN)AnyDictIterableIteratorListLiteralOptionalTuple)jsonlmarkdownuser-promptsc                |    | xs dj                         j                         }|dk(  rd}|dvrt        d|        |S )z(Return the canonical export format name.r   mdr   >   r   r   z#Unsupported session export format: striplower
ValueError)fmtvalues     L/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/session_export.pynormalize_export_formatr      sJ    ^G""$**,E}))>seDEEL    c                j    | y| j                         j                         }|dv ryt        d|        )z(Return the canonical export filter name.N>   userpromptsuser_promptsr   r   z#Unsupported session export filter: r   )onlyr   s     r   normalize_export_onlyr"      s=    |JJL ECC
:4&A
BBr   r   )r   r!   c                   t        |       }t        |      }t        |      }|dk(  rt        ||      S t	        ||      S )a]  Render exported sessions in a stable, reusable format.

    ``fmt=jsonl`` with no filter intentionally preserves the legacy shape:
    one full session object per line. ``only=user-prompts`` switches the unit
    of export to one prompt record per line so the output is easy to pipe into
    review, memory-ingestion, or prompt-library tooling.
    r   r!   )listr   r"   _render_jsonl_render_markdown)sessionsr   r!   session_listexport_formatexport_onlys         r   render_sessions_exportr,   )   sC     >L+C0M'-K\<<L{;;r   r$   c                   t        |       }t        |      }|dk(  rt        d t        |      D              dfS t	        |      dfS )z=Return ``(count, noun)`` for status messages after an export.r   c              3      K   | ]  }d   yw)   N ).0_s     r   	<genexpr>z&export_record_count.<locals>.<genexpr>F   s     E1Es   promptsession)r%   r"   sumiter_user_prompt_recordslen)r(   r!   r)   r+   s       r   export_record_countr9   ?   sK     >L'-Kn$E6|DEExOO|i''r   c              #    K   | D ]  }t        |j                  d      xs |j                  d      xs d      }d}t        |      D ]  }|j                  d      dk7  r|dz  }||t        |j                  d            dt	        |j                  d	            d
}|j                  d      }|||d<   |j                  d      xs |j                  d      }|r||d<   |   yw)z:Yield one normalized record for each user-authored prompt.id
session_id r   roler   r/   	timestampcontent)r<   index
created_atr>   textN
message_idplatform_message_idevent_id)strget	_messages_format_timestamp_message_text)r(   r5   r<   rA   messagerecordrD   rF   s           r   r7   r7   J   s       T*Mgkk,.GM2N
 ) 	G{{6"f,QJE(/K0HI%gkk)&<=&F !T*J%'1|${{#89TW[[=TH%-z"L#	s   C%C'c                   |dk(  rt        |       }nt        |       }|D cg c]  }t        j                  |d       }}|rdj	                  |      dz   S dS c c}w )Nr   F)ensure_ascii
r=   )r7   iterjsondumpsjoin)r(   r!   rowsrowliness        r   r&   r&   e   s^     ~'1H~<@ASTZZ%0AEA(-DIIet#525 Bs   Ac               8    |dk(  rt        |       S t        |       S )Nr   )_render_user_prompts_markdown_render_full_markdown)r(   r!   s     r   r'   r'   p   s"     ~,X66 **r   c           	     Z   g }t        |       dk(  re| d   }|j                  dt        t        |                    |j	                  t        |             |j                  d       t        ||d       n|j                  d       |j                  d       | D ]a  }|j                  dt        t        |                    |j	                  t        |             |j                  d       t        ||d	       c | s"|j                  d
       |j                  d       t        |      S )Nr/   r   z# User prompts for session r=      heading_levelz# User prompts exportz## Session    _No user prompts found._)r8   append_heading_text_session_idextend_session_metadata_lines_append_prompt_records_finish_markdownr(   rW   r5   s      r   rY   rY   x   s    E
8}1+2=WAU3V2WXY,W56RugQ?,-R 	DGLL;}[5I'J&KLMLL09:LL"5'C		D
 /0RE""r   c                  t        t        |g            }|s#| j                  d       | j                  d       y d|z  }|D ]  }|j                  d      xs d}| j                  | d|d    d|        |j                  d	      }|&| j                  d
| d       | j                  d       | j                  t	        |j                  d      xs d             | j                  d        y )Nr`   r=   #rB   ztimestamp unavailable rA   z. rD   zMessage ID: ``rC   )r%   r7   ra   rH   rG   )rW   r5   r^   r   markerr4   r?   rD   s           r   rf   rf      s     +WI67G/0R= F JJ|,G0G	xq 1I;?@ZZ-
!LL=A67LLSF+1r23Rr   c           	     &   g }t        |       dk(  ro| d   }|j                  dt        t        |                    |j	                  t        |             |j                  d       t        ||d       t        |      S |j                  d       |j                  d       | D ]a  }|j                  dt        t        |                    |j	                  t        |             |j                  d       t        ||d	       c t        |      S )
Nr/   r   z# Session: r=   r\   r]   z# Hermes sessions exportz## Session: r_   )r8   ra   rb   _session_title_or_idrd   re   _append_session_messagesrg   rh   s      r   rZ   rZ      s    E
8}1+{=1Eg1N#O"PQR,W56R qA E"" 	/0R 	FGLL<6J76S(T'UVWLL09:LL$UG1E		F
 E""r   c          
     X   d|z  }t        |      D cg c]  }|j                  d      dk7  s| }}|s#| j                  d       | j                  d       y |D ]  }t        |j                  d      xs d      }t	        |j                  d            }|rd| nd}|d	k(  rt        |j                  d
      xs |j                  d      xs d	      }	| j                  | dt        |	       |        | j                  d       | j                  dt        |	       d       | j                  d       | j                  t        t        |j                  d                         | j                  d       | j                  d       | j                  d       Cdddj                  ||j                               }
| j                  | d|
 |        | j                  d       | j                  t        |j                  d                   | j                  d        y c c}w )Nrj   r>   systemz_No messages found._r=   unknownr?   z - tool	tool_namenamez Tool: z<details><summary>z
</summary>r@   z
</details>User	Assistant)r   	assistantrk   )
rI   rH   ra   rG   rJ   rb   html_escape_fenced_textrK   title)rW   r5   r^   rm   rL   visible_messagesr>   r?   suffixru   labels              r   rp   rp      s    = F(1W[[5HH5T  +,R# 7;;v&3)4%gkk+&>?	&/3yk"R6>GKK4UF8KUvVILLF87=+C*DVHMNLLLL-k).D-EZPQLLLLmGKK	4J&KLMLLLL&LL $
 #dDJJL
! 	 	xqx01R]7;;y#9:;R/s
   H'H'c                x    | j                  d      xs g }|D cg c]  }t        |t              s| c}S c c}w )Nmessages)rH   
isinstancedict)r5   r   rL   s      r   rI   rI      s2    {{:&,"H#+Iz'4/HGIIIs   77c                t   | yt        | t              r| S t        | t              r0| D cg c]  }t        |       }}dj	                  d |D              S t        | t
              rDdD ]'  }| j                  |      }t        |t              s%|c S  t        j                  | dd      S t        |       S c c}w )Nr=   rP   c              3  &   K   | ]	  }|s|  y w)Nr0   )r1   parts     r   r3   z _message_text.<locals>.<genexpr>   s     8$48s   rC   r@   FTrO   	sort_keys)	r   rG   r%   _content_part_textrT   r   rH   rR   rS   )r@   r   partskeyr   s        r   rK   rK      s    '3'4 6=>d#D)>>yy8%888'4 & 	CKK$E%%	 zz'FFw< ?s   B5c                    t        | t              r| S t        | t              rDdD ]'  }| j                  |      }t        |t              s%|c S  t	        j
                  | dd      S t        |       S )Nr   FTr   )r   rG   r   rH   rR   rS   )r   r   r   s      r   r   r      se    $$& 	CHHSME%%	 zz$UdCCt9r   c                   | y t        | t        t        f      rNt        j                  t        |       t
        j                        j                  d      j                  dd      S t        | t              rm| j                  r| n| j                  t
        j                        }|j                  t
        j                        j                  d      j                  dd      S t        |       S )N)tzseconds)timespecz+00:00Z)tzinfo)r   intfloatr   fromtimestampr   utc	isoformatreplacer   
astimezonerG   )r   dts     r   rJ   rJ      s    }%#u&""5<HLLAY	Y*WXs#	

 %"llUX\\(J}}X\\*44i4HPPc
 	
 u:r   c                   dt        |        dg}| j                  d      }|r|j                  d| d       | j                  d      }|r|j                  d| d       | j                  d      }|r&|j                  dt        t	        |                    t        | j                  d	            }|r|j                  d
|        | j                  d      }||j                  d|        |S )Nz- Session ID: `rl   sourcez- Source: `modelz
- Model: `r|   z	- Title: 
started_atz- Started: message_countz- Messages: )rc   rH   ra   _inline_textrG   rJ   )r5   rW   r   r   r|   startedr   s          r   re   re     s    )+g*>)?qABE[["F{6(!,-KK Ez%*+KK Eyc%j!9 :;<L 9:G{7),-KK0M |M?34Lr   c                d    t        | j                  d      xs | j                  d      xs d      S )Nr;   r<   rs   )rG   rH   )r5   s    r   rc   rc   "  s)    w{{4 JGKK$=JKKr   c                x    t        | j                  d      xs d      j                         }|xs t        |       S )Nr|   r=   )rG   rH   r   rc   )r5   r|   s     r   ro   ro   &  s3    G$*+113E(K((r   c                v    dj                  t        |       j                               j                         xs dS )Nrk   rs   )rT   rG   
splitlinesr   r   s    r   rb   rb   +  s,    88CJ))+,224A	Ar   c                \    dj                  | j                               j                         S )Nrk   )rT   r   r   r   s    r   r   r   /  s#    88E$$&'--//r   rC   )languagec               :    d}|| v r
|dz  }|| v r
| | d|  d| S )Nz```rl   rP   r0   )rC   r   fences      r   r{   r{   3  s;    E
4- 4-WXJbb00r   c                t    | r#| d   dk(  r| j                          | r	| d   dk(  rdj                  |       dz   S )Nr=   rP   )poprT   )rW   s    r   rg   rg   :  s;    
E"IO		 E"IO99Ud""r   )r   rG   returnExportFormat)r!   Optional[str]r   Optional[ExportOnly])r(   Iterable[Dict[str, Any]]r   rG   r!   r   r   rG   )r(   r   r!   r   r   zTuple[int, str])r(   r   r   zIterator[Dict[str, Any]])r(   List[Dict[str, Any]]r!   r   r   rG   )r(   r   r   rG   )rW   	List[str]r5   Dict[str, Any]r^   r   r   None)r5   r   r   r   )r@   r   r   rG   )r   r   r   rG   )r   r   r   r   )r5   r   r   r   )r5   r   r   rG   )r   rG   r   rG   )rC   rG   r   rG   r   rG   )rW   r   r   rG   )*__doc__
__future__r   r   r   htmlr   rz   rR   typingr   r   r	   r
   r   r   r   r   r   
ExportOnlyr   r"   r,   r9   r7   r&   r'   rY   rf   rZ   rp   rI   rK   r   rJ   re   rc   ro   rb   r   r{   rg   r0   r   r   <module>r      s}   # ' &  P P P *+^$
C 	<&< 
< 	<
 	<. BF(&(1>((&66"6-A66+"+-A++#,-AD	(#&##-#AD#	#LJ
"	"(L)
B0 06 1#r   