
    `gj                        d Z ddlmZ ddlZddlmZmZmZ  ej                  e	      Z
d
dZddZddd	 	 	 	 	 	 	 dd	Zy)a  Shared ``/suggestions`` command logic for CLI and gateway.

Both surfaces call ``handle_suggestions_command(args, origin=...)`` and present
the returned text however they present command output. Keeping the logic here
(not in cli.py / gateway/run.py) means the two surfaces can never drift.

Subcommands:
  /suggestions                 list pending suggestions (numbered)
  /suggestions accept <N|id>   create the cron job for that suggestion
  /suggestions dismiss <N|id>  dismiss it (latched, never re-offered)
  /suggestions catalog         seed the curated starter automations as pending
  /suggestions clear           drop accepted records (housekeeping)
    )annotationsN)AnyDictOptionalc                   | s	 ydg}t        | d      D ]  \  }}|j                  di       xs i }|j                  dd      }|j                  dd      }|j                  d| d	|j                  d
d       d| d| d	       |j                  dd      j                         }|s|j                  d|         dj	                  |      S )NzNo suggested automations right now.
Try `/suggestions catalog` to see the curated starter set, or install a blueprint skill to get one.uB   Suggested automations — `/suggestions accept N` or `dismiss N`:
   job_specschedule?sourcez  . titlez
(untitled)z  [z]  ()description z     
)	enumerategetappendstripjoin)pendinglinesisspecschedsrcdescs           M/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/suggestions_cmd.py_fmt_pendingr!      s    4	

 SSE'1% )1uuZ$*S)eeHc"r!BquuWl;<Cwd3%qQRuu]B'--/LL5() 99U    c                     	 ddl m}   | d      } | d      }|r|r|| | d      xs d | d      xs ddS y# t        $ r Y yw xY w)	a5  Best-effort current-chat origin from session env (CLI and gateway both set it).

    Mirrors cron's ``_origin_from_env`` so an accepted suggestion's job delivers
    back to the chat where it was accepted. Returns None if unavailable, in
    which case create_job falls back to a configured home channel.
    r   )get_session_envHERMES_SESSION_PLATFORMHERMES_SESSION_CHAT_IDHERMES_SESSION_CHAT_NAMENHERMES_SESSION_THREAD_ID)platformchat_id	chat_name	thread_id)gateway.session_contextr$   	Exception)r$   r)   r*   s      r    _resolve_originr/   *   sm    ;"#<=!":;$",-GHPD,-GHPD	    s   48 	AAcli)originsurfacec                  |
t               }	 ddlm} | xs dj                         j                         }|r|d   j                         nd}dj                  |dd       j                         }|st        |j                               S |d	v r|sy
|j                  ||      }|d| dS |j                  d      xs& |j                  di       xs i j                  dd      }	|j                  dd      }
|dk(  rdnd}d|
 d|	rd|	 dndz   d| z   S |dv r|sy|j                  |      }|rdS d| dS |dk(  r<	 dd lm}  |       }|s	 y#d$j                  d% |D              }d&t#        |       d'| d(S |d)k(  r|j%                         }d*| d+S 	 y,# t        $ r }t        j                  d|       Y d}~yd}~ww xY w# t        $ r }t        j                  d!|       Y d}~y"d}~ww xY w)-u  Dispatch a ``/suggestions`` invocation. Returns text to show the user.

    ``args`` is everything after ``/suggestions`` (already stripped of the
    command word). ``origin`` is the platform/chat dict so an accepted job's
    "origin" delivery routes back to where the user accepted; when omitted it
    is resolved from the session environment. ``surface`` (``"cli"`` |
    ``"gateway"``) picks the wording for follow-up hints — ``/cron`` only
    exists on the CLI.
    Nr   )suggestionsz#suggestions store import failed: %sz*Suggestions are unavailable in this build.r    r   )acceptaddr
   z&Usage: /suggestions accept <number|id>)r1   zNo pending suggestion matches 'z!'. Run /suggestions to list them.schedule_displayr	   r
   name
automationr0   zManage it with /cron.z-Ask me to list, pause, or remove it any time.zScheduled ''z (r   r   )dismissnorejectz'Usage: /suggestions dismiss <number|id>z$Dismissed. Won't suggest that again.z'.catalog)seed_catalog_suggestionszcatalog seed failed: %szCouldn't load the catalog.zNo new catalog automations to add (already offered, dismissed, or your suggestion list is full). Run /suggestions to see pending.z, c              3  @   K   | ]  }|j                  d d        yw)r   r   N)r   ).0cs     r    	<genexpr>z-handle_suggestions_command.<locals>.<genexpr>   s     ?!!%%-?s   zAdded z suggestion(s): z.
Run /suggestions to review.clearzCleared z resolved suggestion record(s).zUsage:
  /suggestions              list pending
  /suggestions accept N     schedule suggestion N
  /suggestions dismiss N    dismiss suggestion N
  /suggestions catalog      add curated starter automations
  /suggestions clear        housekeeping)r/   cronr4   r.   loggerdebugr   splitlowerr   r!   list_pendingaccept_suggestionr   dismiss_suggestioncron.suggestion_catalogr@   lenclear_resolved)argsr1   r2   storeepartssubrestjobr   r9   manageokr@   createdaddedremoveds                    r    handle_suggestions_commandr]   B   s^    ~ "<-
 ZR &&(E#%(..
C88E!"I$$&D E..011
++;%%d6%:;4TF:[\\*+b
B0G0M2/R/RS]_a/bwwv|, % $@ 	 $q! %E7!}2/6(m	
 ))<%%d+  3	
 34&;	
 i	0H.0G U 		?w??G~%5eW<Z[[
g~&&('"ABB	3{  <:A>;<\  	0LL2A6/	0s/   F ?G 	G  F;;G 	G,G''G,)r   listreturnstr)r_   Optional[Dict[str, Any]])rQ   r`   r1   ra   r2   r`   r_   r`   )__doc__
__future__r   loggingtypingr   r   r   	getLogger__name__rG   r!   r/   r]    r"   r    <module>ri      sl    #  & &			8	$&6 (,	W
W %W 	W
 	Wr"   