
    `gj                        d Z ddlmZ ddlZddlZddlmZmZmZm	Z	  ej                  dej                        ZddZddZdddZdd	Zdd
ZddZy)u  Prompt-size diagnostic: ``hermes prompt-size``.

Reports a byte/char breakdown of the system prompt the agent would build for
a fresh session — system prompt total, the ``<available_skills>`` index,
memory + user profile, and tool-schema JSON. Lets users see where their fixed
prompt budget goes (issue #34667) without parsing a saved session JSON by hand.

The diagnostic builds a real inspection agent (so the numbers match what
actually ships on the wire) but never makes a network call: it passes dummy
credentials so ``AIAgent.__init__`` takes the direct-construction path, then
calls ``build_system_prompt_parts`` / inspects ``agent.tools`` offline.
    )annotationsN)AnyDictListTuplez(<available_skills>.*?</available_skills>c                6    t        | j                  d            S )Nzutf-8)lenencode)ss    I/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/prompt_size.py_bytesr      s    qxx !!    c           
        ddl m} ddlm} ddlm}  |       }t        |j                  d      t              r|j                  di       ni }|j                  d      xs |j                  d      xs d}t         |||             }|j                  d      xs i }|j                  d	      xs d
}	 ||dddd| ||	      S )a  Construct an offline AIAgent for prompt inspection.

    Dummy ``api_key`` + ``base_url`` force the direct-construction path in
    ``run_agent.py`` (no provider auto-detection, no network). Toolsets and
    platform come from the caller so the breakdown matches a real session.
    r   )AIAgent)load_config)_get_platform_toolsmodeldefault agentdisabled_toolsetsNzinspect-onlyzhttps://openrouter.ai/api/v1TF)r   api_keybase_url
quiet_modesave_trajectoriesplatformenabled_toolsetsr   )
	run_agentr   hermes_cli.configr   hermes_cli.tools_configr   
isinstancegetdictsorted)
r   r   r   r   cfg	model_cfgr   r   	agent_cfgr   s
             r   _build_inspection_agentr(      s     "-;
-C(237773CT(J$PRIMM)$D	g(>D"E 1#x@A &BI!&9:Bd/)+	 	r   c           
        ddl m}m} t        |       } ||      } ||      }|j	                  dd      }|j	                  dd      }|j	                  dd      }t
        j                  |      }	|	r|	j                  d      nd}
d}d}t        |dd      }|E	 t        |d	d
      r|j                  d      xs d}t        |dd
      r|j                  d      xs d}t        |dd      xs g }t        j                  |d      }dt        |      t        |      fdt        |      t        |      fdt        |      t        |      fg}| t        |dd      xs dt        |      t        |      dt        |
      t        |
      dt        |      t        |      dt        |      t        |      dt        |      t        |      d|dS # t        $ r Y w xY w)a  Return a dict of prompt-size measurements for a fresh session.

    Keys: ``system_prompt`` (chars/bytes), ``skills_index``, ``memory``,
    ``user_profile``, ``tools`` (count + json bytes), and ``sections`` (a list
    of (label, chars, bytes) for the three prompt tiers).
    r   )build_system_promptbuild_system_prompt_partsstabler   contextvolatile_memory_storeN_memory_enabledTmemory_user_profile_enabledusertoolsF)ensure_asciiz!stable (identity/guidance/skills)zcontext (AGENTS.md/cwd files)z#volatile (memory/profile/timestamp)r   )charsbytes)count
json_bytes)r   r   system_promptskills_indexr1   user_profiler4   sections)agent.system_promptr*   r+   r(   r"   _SKILLS_BLOCK_REsearchgroupgetattrformat_for_system_prompt	Exceptionjsondumpsr	   r   )r   r*   r+   r   partsfullr,   r-   r.   skills_matchr;   memory_block
user_blockstorer4   
tools_jsonr=   s                    r   compute_prompt_breakdownrN   <   s    S#H-E%e,Eu%DYYx$Fii	2&GyyR(H $**62L,8<%%a(bL
 LJE?D1E	u/6$==hGM2u5t<";;FCIr

 E7D)/REE6J 
-c&k6&>J	(#g,wH	.Hvh?OP,H ,2#&t9vd|D"%l"3f\>RS-|8LM"%j/F:<NOu:VJ5GH	 	  		s   AG 	GGc                    | dz  ddS )Ni   z.1fz KB )ns    r   _fmt_kbrR   x   s    $hs^3r   c                |   g }| d   }|j                  d| d    d| d   xs d d       |j                  d       |j                  d	|d
   ddt        |d
          d|d   dd       |j                  d       |j                  d       | d   }| d   }| d   }|j                  d|d
   ddt        |d
          d       |j                  d|d
   ddt        |d
          d       |j                  d|d
   ddt        |d
          d       |j                  d       |j                  d       | d   D ],  \  }}}|j                  d|dd|ddt        |       d       . |j                  d       | d   }	|j                  d|	d   ddt        |	d          d|	d     d!       d"j                  |      S )#z;Render the breakdown as plain text suitable for a terminal.r:   z Prompt-size breakdown (platform=r   z, model=r   unset)r   z  System prompt total : r7   z>8,z B  (z, r6   ,z chars)z  Major blocks:r;   r1   r<   z    skills index       : z    memory             : z    user profile       : z  Prompt tiers:r=   z    z<36z: r4   z  Tool schemas         : r9   r8   z tools)
)appendrR   join)
datalinesspsimemuplabelr6   bytsr4   s
             r   render_breakdownrb   |   s   E	o	B	LL3D4D3EXdSZmNf_fMgghij	LL	LL+BwK+<E'"W+BVAWWYZ\]dZefgYhhopq	LL	LL"#	n	B
x.C	n	B	LL,R[,=U72g;CWBXXYZ[	LL,S\#,>eGCPWLDYCZZ[\]	LL,R[,=U72g;CWBXXYZ[	LL	LL"#":. JudtE#;bc
%aHIJ	LLME	LL,U<-@,EU7SXYeSfKgJhhjkpqxkyjz  {B  C  D99Ur   c                   t        | dd      xs d}t        | dd      }	 t        |      }|r"t        t	        j
                  |dd             yt        t        |             y# t        $ r}t        d|        Y d}~yd}~ww xY w)	z'Entry point for ``hermes prompt-size``.r   clirE   Fz)Could not compute prompt-size breakdown: N   )r5   indent)rB   rN   rD   printrE   rF   rb   )argsr   as_jsonrZ   es        r   cmd_prompt_sizerk      s    tZ/85HdFE*G'1 djjE!<=t$%  9!=>s   A$ $	B-B  B)r   strreturnint)r   rl   rm   r   )rd   )r   rl   rm   Dict[str, Any])rQ   rn   rm   rl   )rZ   ro   rm   rl   )rh   r   rm   None)__doc__
__future__r   rE   retypingr   r   r   r   compileDOTALLr?   r   r(   rN   rR   rb   rk   rP   r   r   <module>rw      sU    #  	 ) ) 2::I299U "@9x 2&r   