
    `gje                        U d Z ddlZddlmZmZmZmZmZ ddlm	Z	m
Z
  ej                  e      Zeeeef   geeef   f   ZdededefdZd	Zd
eeef   deeef   fdZdeiZeeef   ed<   d ded
eeeef      deeef   fdZdddddddddd	dededee   d
eeeef      dededee   dedeeeef      defdZdedefdZy)!u[  Shared one-off LLM requests for non-conversational helpers.

A "one-shot" is a single, stateless model call that runs *outside* any
conversation: it never touches a session's history, never breaks prompt
caching, and returns plain text. UI surfaces use it for small generative
chores — a commit message from a diff, a rename suggestion, a summary —
where spinning up an agent turn would be wrong (it would pollute the thread)
and hand-rolling an LLM call at every call site would be worse.

Two ways to call it:

  * ``run_oneshot(instructions=..., user_input=...)`` — caller supplies the
    full prompt.
  * ``run_oneshot(template="commit_message", variables={...})`` — caller
    names a registered template and passes its variables; the template owns
    the prompt engineering so it stays consistent across CLI/TUI/desktop.

Model selection rides the same auxiliary plumbing as title generation
(:func:`agent.auxiliary_client.call_llm`): pass ``main_runtime`` to inherit
the live session's provider/model, otherwise the configured ``task`` (default
``title_generation``) resolves a cheap/fast backend.
    N)AnyCallableDictOptionalTuple)call_llmextract_content_or_reasoningtextlimitreturnc                 Z    | xs d} t        |       |k  r| S | d | j                         dz   S )N u   
…(truncated))lenrstrip)r
   r   s     @/root/.hermes/venv/lib/python3.12/site-packages/agent/oneshot.py	_truncater   %   s7    :2D
4yE< #555    u  You write git commit messages. Given a diff of staged changes, write ONE concise Conventional Commits message describing what the change does and why.
Rules:
- Subject line: type(scope): summary — imperative mood, lower-case, no trailing period, ≤ 72 characters. Types: feat, fix, refactor, perf, docs, test, build, chore, style, ci.
- Omit the scope if it isn't obvious.
- Add a short body (wrapped at ~72 cols) ONLY when the change needs explanation; skip it for small/obvious changes.
- Describe the actual change, never restate the diff line-by-line.
- Return ONLY the commit message text — no quotes, no markdown fences, no preamble.	variablesc                    t        t        | j                  d      xs d      d      }t        t        | j                  d      xs d      d      }g }|j                         r|j	                  d|        |j	                  d|xs dz          t        t        | j                  d	      xs d      j                         d
      }|r|j	                  d|        t
        dj                  |      fS )Ndiffr   i.  recent_commitsi  zGRecent commit subjects from this repo (match their style/conventions):
zDiff to describe:
z(no textual diff available)avoidi  u   You already proposed the message below and the user wants a different one. Write a NEW message with different wording (and, if reasonable, a different emphasis or scope framing) — do not repeat it:
z

)r   strgetstripappend_COMMIT_INSTRUCTIONSjoin)r   r   recentpartsr   s        r   _commit_message_templater!   <   s    Sv.4"5u=Ds9==)9:@bA4HFE||~Vh	
 
LL&$*O2OPQ
 c)--06B7==?FE 7	
  U!333r   commit_messagePROMPT_TEMPLATESnamec                 d    t         j                  |       }|t        d|         ||xs i       S )zResolve a registered template into (instructions, user_input).

    Raises KeyError if the template name is unknown so callers fail loudly
    instead of silently sending an empty prompt.
    zunknown one-shot template: )r#   r   KeyError)r$   r   templates      r   render_templater(   ^   s<      ##D)H4TF;<<IO$$r   r   title_generationi   g333333?g      N@)	instructions
user_inputr'   r   task
max_tokenstemperaturetimeoutmain_runtimer*   r+   r'   r,   r-   r.   r/   r0   c        	            |rt        ||      \  } }| xs dj                         s|xs dj                         st        d      g }	| xs dj                         r|	j                  d| d       |	j                  d|xs dd       t	        ||	||||      }
t        |
      xs dj                         }t        |      S )a  Run a single stateless LLM request and return its text.

    Provide either a registered ``template`` (+ ``variables``) or an explicit
    ``instructions`` / ``user_input`` pair. Returns the model's text answer,
    stripped of surrounding whitespace and any wrapping code fence.

    Raises RuntimeError when no LLM provider is configured (surfaced from
    :func:`call_llm`) and KeyError for an unknown template name.
    r   z:run_oneshot requires a template or instructions/user_inputsystem)rolecontentuser)r,   messagesr-   r.   r/   r0   )r(   r   
ValueErrorr   r   r	   _strip_code_fence)r*   r+   r'   r   r,   r-   r.   r/   r0   r6   responser
   s               r   run_oneshotr:   j   s    * #28Y#G jB%%'1Ar0H0H0JUVVH!!#lCDOOV
0@bAB!H )28b??ADT""r   c                     | j                  d      s| S | j                         }t        |      dk\  rL|d   j                  d      r8|d   j                         dk(  r"dj	                  |dd       j                         S | S )z:Drop a single wrapping ``` fence the model may have added.z```   r   
   )
startswith
splitlinesr   r   r   )r
   liness     r   r8   r8      sp    ??5!OOE
5zQ58..u5%)//:Ku:Tyyq%++--Kr   )N)__doc__loggingtypingr   r   r   r   r   agent.auxiliary_clientr   r	   	getLogger__name__loggerr   PromptTemplateintr   r   r!   r#   __annotations__r(   floatr:   r8    r   r   <module>rO      s  .  7 7 I			8	$
 4S>*E#s(O;<6C 6 6 6  4S#X 45c? 4< ./ $sN*+ 
	%# 	%(4S>*B 	%eTWY\T\o 	% "*."#&-1*#*# *# sm	*#
 S#X'*# *# *# %*# *# 4S>**# 	*#ZC C r   