
    L0&j                     	   U d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
 daeed<   defdZddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZmZmZ  ej        e          Zdddddddddddddh ddd	dddddddddddd ddd!d"d#h d$d"d	d%d&d'd(dddddd)d dddd*h d+dd	d,d-d.d/d0d1d2d3dd dd!d4d5h d6dd	d7d8d9d:d;d<d=d>dd?d dd@h dAdd	dBdCdDdEddFddGdd?d dd@h dHdd	dIdJdKdLddddddMd"dNdOh dPdd	dQd-dRdSddddddTdih dUdd	dVdWdXdYddddddZd[d dd\d]h d^dd	d_d`dadbd0d1d2d3ddcd?ih dddd	dedfdgdhd0d1d2d3ddcd?ih dddd	diZe	ee	eef         f         edj<   dkZ dlZ!dZ"dmZ#dnZ$dZ%doZ&dpZ'dqZ(drZ)dZ*dsZ+ edtduv          Z,da-da. ej/                    Z0dw Z1dx Z2dyedze	eef         fd{Z3de4fd|Z5e!ddfd}ed~ed0ede
e6         de
e	eef                  de	eef         fdZ7dedede
e	eef                  fdZ8dede9fdZ:dedz  fdZ;dededz  fdZ<dedededz  fdZ=deddfdZ>ddededz  defdZ?e!dddddfd~ed0ede
e6         de
e@         de
e6         de
e         de
e6         defdZAde9fdZBdefdZCde9fdZDedk    r eEd            eEd            eB            s, eEd            eEd            eEd            eFd            eEd           	 ddla eEd           n# eG$ r  eEd            eFd           w xY w e5            \  ZHZI eEdeIJ                    deH           deH d            eEdeIJ                    dd           deIJ                    dd                       eEdeIJ                    d          rdnd             eEd           eK                                D ]O\  ZLZMeLeHk    rdndZN eEdeLddeMJ                    dd          ddeMJ                    dd           eN            Pe,jO        r eEde,jP                    ddlQmRZRmSZS ddddddd eTe"          de!ddd~gddZUd ZVd ZWd~ed0efdÄZXdĄ ZY eRjZ        ddeUeYeDg ddƬǦ           dS )u  
Image Generation Tools Module

Provides image generation via FAL.ai. Multiple FAL models are supported and
selectable via ``hermes tools`` → Image Generation; the active model is
persisted to ``image_gen.model`` in ``config.yaml``.

Architecture:
- ``FAL_MODELS`` is a catalog of supported models with per-model metadata
  (size-style family, defaults, ``supports`` whitelist, upscaler flag).
- ``_build_fal_payload()`` translates the agent's unified inputs (prompt +
  aspect_ratio) into the model-specific payload and filters to the
  ``supports`` whitelist so models never receive rejected keys.
- Upscaling via FAL's Clarity Upscaler is gated per-model via the ``upscale``
  flag — on for FLUX 2 Pro (backward-compat), off for all faster/newer models
  where upscaling would either hurt latency or add marginal quality.

Pricing shown in UI strings is as-of the initial commit; we accept drift and
update when it's noticed.
    N)AnyDictOptional
fal_clientreturnc                  L    t           t           S ddlm}   |             a t           S )u'  Lazily import fal_client and rebind the module global on first use.

    Idempotent. Returns the (now-loaded) ``fal_client`` module reference.
    Skips the import if the global is already truthy — this preserves the
    test pattern of monkeypatching the module global to install a mock.
    Nr   import_fal_client)r   tools.fal_commonr
   r	   s    @/home/ubuntu/.hermes/hermes-agent/tools/image_generation_tool.py_load_fal_clientr   ,   s6     222222""$$J    )DebugSession)_ManagedFalSyncClient_extract_http_status_normalize_fal_queue_url_format)resolve_managed_tool_gateway)fal_key_is_configuredmanaged_nous_tools_enabled%nous_tool_gateway_unavailable_messageprefers_gatewayzFLUX 2 Klein 9Bz<1szFast, crisp textz	$0.006/MPimage_size_presetlandscape_16_9	square_hdportrait_16_9)	landscapesquareportrait   pngF)num_inference_stepsoutput_formatenable_safety_checker>   seedprompt
image_sizer"   r!   r#   )	displayspeed	strengthsprice
size_stylesizesdefaultssupportsupscalez
FLUX 2 Proz~6szStudio photorealismz$0.03/MP2   g      @   5T)r!   guidance_scale
num_imagesr"   r#   safety_tolerance	sync_mode>
   r$   r%   r6   r&   r4   r"   r3   r5   r!   r#   zZ-Image Turboz~2szBilingual EN/CN, 6Bz	$0.005/MP   )r!   r4   r"   r#   enable_prompt_expansion>   r$   r%   r&   r4   r"   r!   r#   r8   z$Nano Banana Pro (Gemini 3 Pro Image)z~8sz-Gemini 3 Pro, reasoning depth, text renderingz$0.15/image (1K)aspect_ratioz16:9z1:1z9:161K)r4   r"   r5   
resolution>
   r$   r%   r6   r4   r;   r9   r"   r5   enable_web_searchlimit_generationszGPT Image 1.5z~15szPrompt adherencez$0.034/imagegpt_literal	1536x1024	1024x1024	1024x1536medium)qualityr4   r"   >   r%   rC   r6   
backgroundr&   r4   r"   zGPT Image 2z~20sz3SOTA text rendering + CJK, world-aware photorealismu   $0.04–0.06/imagelandscape_4_3portrait_4_3>   r%   rC   r6   r&   r4   r"   zIdeogram V3z~5szBest typographyz$0.03-0.09/imageBALANCEDAUTO)rendering_speedexpand_promptstyle>   r$   rK   r%   r&   rJ   rI   zRecraft V4 Proz'Design, brand systems, production-readyz$0.25/imager#   >   colorsr%   r&   background_colorr#   z
Qwen Imagez~12szLLM-based, complex textz$0.02/MP   g      @regular)r!   r3   r4   r"   acceleration>	   r$   r%   r6   r&   r4   rP   r"   r3   r!   zKrea 2 Mediumz~15-25sz9Illustration, anime, painting, expressive/artistic stylesz#$0.030 (text) / $0.035 (style refs)
creativity>   r$   r%   rQ   r9   image_style_referenceszKrea 2 Largez~25-60sz;Photorealism, raw textured looks (motion blur, grain, film)z#$0.060 (text) / $0.065 (style refs))fal-ai/flux-2/klein/9bzfal-ai/flux-2-prozfal-ai/z-image/turbozfal-ai/nano-banana-prozfal-ai/gpt-image-1.5zfal-ai/gpt-image-2zfal-ai/ideogram/v3z#fal-ai/recraft/v4/pro/text-to-imagezfal-ai/qwen-imagez#fal-ai/krea/v2/medium/text-to-imagez"fal-ai/krea/v2/large/text-to-image
FAL_MODELSrS   r   zfal-ai/clarity-upscaler   z"masterpiece, best quality, highresz.(worst quality, low quality, normal quality:2)gffffff?g333333?   image_toolsIMAGE_TOOLS_DEBUG)env_varc                  ^    t                      rt          d          sdS t          d          S )zsReturn managed fal-queue gateway config when the user prefers the gateway
    or direct FAL credentials are absent.	image_genNz	fal-queue)r   r   r    r   r   _resolve_managed_fal_gatewayr]     s4      {'C'C t'444r   c                 @   | j                             d          | j        f}t          5  t          t
          |k    rt          cddd           S t                       t          t          | j        | j                   a|at          cddd           S # 1 swxY w Y   dS )zQReuse the managed FAL client so its internal httpx.Client is not leaked per call./N)keyqueue_run_origin)	gateway_originrstripnous_user_token_managed_fal_client_lock_managed_fal_client_managed_fal_client_configr   r   r   )managed_gatewayclient_configs     r   _get_managed_fal_clientrj     s
   
 	&--c22'M 
" # #*/I]/Z/Z&# # # # # # # # 	3/,;
 
 

 &3""# # # # # # # # # # # # # # # # # #s   B7BBBmodel	argumentsc           	         t                       dt          t          j                              i}t	                      }|t          j        | ||          S t          |          }	 |                    | ||          S # t          $ rZ}t          |          }|Cd|cxk    rdk     r6n n3d}|dv rdt          d	d
          z   }t          d|  d| d|           | d}~ww xY w)zKSubmit a FAL request using direct credentials or the managed queue gateway.zx-idempotency-keyN)rl   headersi  i   >         z

managed FAL image generationT)force_freshz*Nous Subscription gateway rejected model 'z' (HTTP u   ). This model may not yet be enabled on the Nous Portal's FAL proxy. Either:
  • Set FAL_KEY in your environment to use FAL.ai directly, or
  • Pick a different model via `hermes tools` → Image Generation.)r   struuiduuid4r]   r   submitrj   	Exceptionr   r   
ValueError)rk   rl   request_headersrh   managed_clientexcstatusgateway_messages           r   _submit_fal_requestr     s`    *C
,=,=>O244O )_UUUU,_==N$$# % 
 
 	

    
 &c**#"5"5"5"5#"5"5"5"5"5 O((;6$(     %U % %% %
 #% %   	1s   (B   
C$
ACC$c                     d} 	 ddl m}  |            }t          |t                    r|                    d          nd}t          |t                    r>|                    d          }t          |t
                    r|                                } n2# t          $ r%}t          	                    d|           Y d}~nd}~ww xY w| s't          j        dd                                          } | st          t          t                   fS | t          vr:t                              d	| t                     t          t          t                   fS | t          |          fS )
zResolve the active FAL model from config.yaml (primary) or default.

    Returns (model_id, metadata_dict). Falls back to DEFAULT_MODEL if the
    configured model is unknown (logged as a warning).
    ro   r   load_configr[   Nrk   z.Could not load image_gen.model from config: %sFAL_IMAGE_MODELz4Unknown FAL model '%s' in config; falling back to %s)hermes_cli.configr   
isinstancedictgetru   stripry   loggerdebugosgetenvDEFAULT_MODELrT   warning)model_idr   cfgimg_cfgrawr}   s         r   _resolve_fal_modelr     sf    H	L111111kmm*4S$*?*?I#''+&&&Tgt$$ 	'++g&&C#s## '99;; L L LEsKKKKKKKKL  <9.3399;; 8j777z!!Bm	
 	
 	
 j777Z)))s   BB 
CB>>Cr   r%   r$   	overridesc                 \   t           |          }|d         }|d         }|pt                                                                          }||vrt          }t	          |                    di                     }	|pd                                |	d<   |dv r||         |	d<   n$|dk    r||         |	d<   nt          d	|          |t          |t                    r||	d<   |r!|	                                D ]\  }
}|||	|
<   |d         fd|		                                D             S )a)  Build a FAL request payload for `model_id` from unified inputs.

    Translates aspect_ratio into the model's native size spec (preset enum,
    aspect-ratio enum, or GPT literal string), merges model defaults, applies
    caller overrides, then filters to the model's ``supports`` whitelist.
    r+   r,   r-   ro   r%   >   r>   r   r&   r9   zUnknown size_style: Nr$   r.   c                 $    i | ]\  }}|v 	||S r\   r\   ).0kvr.   s      r   
<dictcomp>z&_build_fal_payload.<locals>.<dictcomp>*  s$    >>>TQXAqr   )
rT   DEFAULT_ASPECT_RATIOlowerr   r   r   rz   r   intitems)r   r%   r9   r$   r   metar+   r,   aspectpayloadr   r   r.   s               @r   _build_fal_payloadr     sX    hDl#JME2299;;AACCFU%"488J#;#;<<G2,,..GH999 %f	~	%	%"'->
>>???JtS11 OO%% 	 	DAq}
JH>>>>W]]__>>>>r   	image_urloriginal_promptc           
         	 t                               d           | t           d| t          t          t
          t          t          t          t          d	}t          t          |          }|                                }|rd|v r|d         }t                               d|                    dd          |                    d	d                     |d
         |                    dd          |                    d	d          dt          dS t                               d           dS # t          $ r(}t                               d|d           Y d}~dS d}~ww xY w)zUpscale an image using FAL.ai's Clarity Upscaler.

    Returns upscaled image dict, or None on failure (caller falls back to
    the original image).
    z(Upscaling image with Clarity Upscaler...z, )	r   r%   upscale_factornegative_promptrQ   resemblancer3   r!   r#   rl   imagez$Image upscaled successfully to %sx%swidthunknownheighturlr   T)r   r   r   upscaledr   z"Upscaler returned invalid responseNzError upscaling image: %sexc_info)r   infoUPSCALER_DEFAULT_PROMPTUPSCALER_FACTORUPSCALER_NEGATIVE_PROMPTUPSCALER_CREATIVITYUPSCALER_RESEMBLANCEUPSCALER_GUIDANCE_SCALEUPSCALER_NUM_INFERENCE_STEPSUPSCALER_SAFETY_CHECKERr   UPSCALER_MODELr   errorry   )r   r   upscaler_argumentshandlerresultupscaled_imagees          r   _upscale_imager   0  sr   %>??? #0EEOEE-7-/5#?%<

 

 &n@RSSS 	g''#G_NKK6""7I66""8Y77   &e,'++GQ77(,,Xq99 "1   	9:::t   0!dCCCttttts   D	D( D( (
E2EEvaluec                    | rt          | t                    sdS |                                 }|                    d          rdS t          j                            |           rdS t          |           dk    o| d         dk    o	| d         dv S )	NF)zhttp://zhttps://zdata:T   r1   :rU   >   r_   \)r   ru   r   
startswithr   pathisabslen)r   r   s     r   _looks_like_absolute_file_pathr   a  s     
5#.. uKKMME899 u	w}}U tu::?JuQx3J58{3JJr   task_idc                     	 ddl m}  || pd          S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)Nr   )get_active_envdefaultz1Could not inspect active terminal environment: %s)tools.terminal_toolr   ry   r   r   )r   r   r}   s      r   _active_terminal_envr   l  so    666666~g2333   H#NNNttttts    
AA  Aenvc                 2   | t          | dd           }t          |          rb	  |            }|r"t          |                              d          S n2# t          $ r%}t
                              d|           Y d }~nd }~ww xY wt          | dd           }|r%t          |                              d           dS | j        j        }|dv rdS t          j
        d          pd	                                                                }|d
v rdS |dk    rdS d S )Nagent_visible_cache_baser_   z.active env agent_visible_cache_base failed: %s_remote_homez/.hermes>   ModalEnvironmentDockerEnvironmentSingularityEnvironmentz/root/.hermesTERMINAL_ENVlocal>   modaldockersingularitysshz	~/.hermes)getattrcallableru   rc   ry   r   r   	__class____name__r   r   r   r   )r   explicitr   r}   remote_homeenv_namebackends          r   _agent_cache_base_for_envr   v  sZ   

 3 :DAAH 	TT 

 2u::,,S1112 T T TMsSSSSSSSST c>488 	=+&&--c22<<<<=)ZZZ"? y((3G::<<BBDDG444%{4s   -A 
BA==B	host_pathc                     t          |           sd S t          |          }|sd S 	 ddlm}  || |          S # t          $ r%}t
                              d|           Y d }~nd }~ww xY wd S )Nr   )map_cache_path_to_container)container_basez4Could not translate image cache path for backend: %s)r   r   tools.credential_filesr   ry   r   r   )r   r   
cache_baser   r}   s        r   _agent_visible_cache_pathr     s    ))44 t*3//J tRFFFFFF**9ZPPPP R R RKSQQQQQQQQR4s   9 
A(A##A(c                     t          | dd           }|d S 	 |                    d           d S # t          $ r&}t                              d|           Y d }~d S d }~ww xY w)N_sync_managerTforcez1Could not force-sync generated image artifact: %s)r   syncry   r   r   )r   sync_managerr}   s      r   _force_artifact_syncr     s    366LQ%%%%% Q Q QJCPPPPPPPPPQs   / 
AAAr   c                 F   	 t          | t                    rt          j        |           n| }n# t          $ r | cY S w xY wt          |t
                    r|                    d          s| S |                    d          }t          |t                    rt          |          s| S t          |          }t          ||          }|r||k    r| S |t          |           |                    d|           |                    d|           t          j        |d          S )a  Annotate successful local image results with backend-visible paths.

    ``image`` remains the host/gateway-deliverable path.  When the active
    terminal backend has a different filesystem, ``agent_visible_image`` gives
    the path the agent can use with terminal/file tools.
    successr   N
host_imageagent_visible_imageF)ensure_ascii)r   ru   jsonloadsry   r   r   r   r   r   r   
setdefaultdumps)r   r   r   r   r   
agent_paths         r   "_postprocess_image_generate_resultr    s9   %/S%9%9B$*S///s   


 gt$$ GKK	,B,B 
KK  EeS!! )G)N)N 

w
'
'C*5#66J u,,

S!!!|U+++,j999:gE2222s   +. ==r!   r3   r4   r"   c           	      	   t                      \  }}|| ||||||dddddd}	t          j                                        }
	 | r:t          | t                    r%t          |                                           dk    rt          d          t                      s)t                      st          t                                |pt                                                                          }|t          vr(t                              d|t                     t          }i }|||d<   |||d	<   |||d
<   |||d<   t!          || |||          }t                              d|                    d|          || dd                    t'          ||          }|                                }t          j                                        |
z
                                  }|rd|vrt          d          |                    dg           }|st          d          t+          |                    dd                    }g }|D ]}t          |t,                    rd|v s|d         |                    dd          |                    dd          d}|rZt/          |d         |                                           }|r|                    |           t                              d           d|d<   |                    |           |st          d          t3          d |D                       }t                              dt          |          |||           d|r|d         d         ndd}d|	d <   t          |          |	d!<   ||	d"<   t4                              d#|	           t4                                           t;          j        |d$d%          S # t>          $ r}t          j                                        |
z
                                  }d&t	          |           }t                               d'|d(           ddt	          |          tC          |          j"        d)}||	d*<   ||	d"<   t4                              d#|	           t4                                           t;          j        |d$d%          cY d}~S d}~ww xY w)+a  Generate an image from a text prompt using the configured FAL model.

    The agent-facing schema exposes only ``prompt`` and ``aspect_ratio``; the
    remaining kwargs are overrides for direct Python callers and are filtered
    per-model via the ``supports`` whitelist (unsupported overrides are
    silently dropped so legacy callers don't break when switching models).

    Returns a JSON string with ``{"success": bool, "image": url | None,
    "error": str, "error_type": str}``.
    )r%   r9   r!   r3   r4   r"   r$   NFr   )rk   
parametersr   r   images_generatedgeneration_timez1Prompt is required and must be a non-empty stringz-Invalid aspect_ratio '%s', defaulting to '%s'r!   r3   r4   r"   )r$   r   u,   Generating image with %s (%s) — prompt: %sr'   P   r   imagesu7   Invalid response from FAL.ai API — no images returnedzNo images were generatedr/   r   r   r   )r   r   r   z1Using original image as fallback (upscale failed)r   z%No valid image URLs returned from APIc              3   D   K   | ]}|                     d           dV  dS )r   r1   N)r   )r   imgs     r   	<genexpr>z&image_generate_tool.<locals>.<genexpr>C  s3      RR3cggj>Q>QRQRRRRRRr   z3Generated %s image(s) in %.1fs (%s upscaled) via %sT)r   r   r   r  r  image_generate_toolrU   )indentr   zError generating image: z%sr   r   r   r   
error_typer   )#r   datetimenowr   ru   r   r   rz   r   r]   _build_no_backend_setup_messager   r   VALID_ASPECT_RATIOSr   r   r   r   r   r   total_secondsboolr   r   appendsum_debuglog_callsaver   r   ry   r   typer   )r%   r9   r!   r3   r4   r"   r$   r   r   debug_call_data
start_time	aspect_lcr   rl   r   r   r  r  should_upscaleformatted_imagesr
  original_imager   upscaled_countresponse_datar   	error_msgs                              r   r  r    sM   & ())NHd (#6,$*
 
  O" "&&((JkG 	RZ44 	RFLLNN8K8Kq8P8PPQQQ%'' 	@+G+I+I 	@<>>???!9%9@@BBHHJJ	///NN?2   -I$&	*/BI+,%*8I&'!&0Il#$)6Io&&fidi
 
 
	 	:HHY))8VCRC[	
 	
 	

 &h)DDD#,0022Z?NNPP 	X//VWWWHb)) 	97888dhhy%8899 	4 	4CsD)) esll5z!,,''(A.. N  T!/E
FLLNN!K!K! $++N;;;RSSS).N:&##N3333 	FDEEERR*:RRRRRA !!?NH	
 	
 	
 3CM%a(//
 

 &*	".12B.C.C*+-<)*-???z-FFFF G G G#,0022Z?NNPP7s1vv77	T9t444 VVq''*	
 
 $- -<)*-???z-FFFFFFFFF#Gs    NO   
S*CSSSc                  T    t          t                      pt                                S )zDTrue if the FAL.ai API key (direct or managed gateway) is available.)r  r   r]   r\   r   r   check_fal_api_keyr&  j  s#    %''I+G+I+IJJJr   c                     ddg} |                      d           t                      r|                      d           n>|                      d           t          d          }|r|                      d|            |                      d           |                      d           |                      d	           t                      r|                      d
           |                      d           d                    |           S )aR  Build an actionable error string when no FAL backend is reachable.

    Used by the in-tree FAL path. Mentions:
      - FAL_KEY signup link
      - managed-gateway status (if Nous tools are enabled)
      - plugin alternative pointer (so users on a stale ``image_gen.provider``
        know the registry exists and how to inspect it)
    z4Image generation is unavailable in this environment.ro   zMissing requirements:zA  - FAL_KEY is not set and the managed FAL gateway is unreachablez+  - FAL_KEY environment variable is not setrs   z  - z&To enable image generation, do one of:z_  1. Get a free API key at https://fal.ai and set FAL_KEY=<your-key> (then restart the session)zX  2. Sign in to a Nous account that has the managed FAL gateway enabled (`hermes setup`)u     3. Configure a different image_gen provider via `hermes tools` → Image Generation (run `hermes plugins list` to see installed backends)
)r  r   r   join)linesr   s     r   r  r  o  s6    DRHE	LL()))!## 
3O	
 	
 	
 	
 	BCCC?*
 
  	3LL111222	LL	LL9:::	LL	8   "## 
/	
 	
 	
 
LL	  
 99Ur   c                      	 t                      rt                       dS n# t          $ r Y nw xY w	 ddlm}  ddlm}  |              |             D ]*}	 |                                r dS # t          $ r Y 'w xY wn# t          $ r Y nw xY wdS )a  True if any image gen backend is available.

    Providers are considered in this order:

    1. The in-tree FAL backend (FAL_KEY or managed gateway).
    2. Any plugin-registered provider whose ``is_available()`` returns True.

    Plugins win only when the in-tree FAL path is NOT ready, which matches
    the historical behavior: shipping hermes with a FAL key configured
    should still expose the tool. The active selection among ready
    providers is resolved per-call by ``image_gen.provider``.
    Tr   )list_providers_ensure_plugins_discoveredF)	r&  r   ImportErroragent.image_gen_registryr,  hermes_cli.pluginsr.  is_availablery   )r,  r.  providers      r   #check_image_generation_requirementsr4    s   	 	
 4	    ;;;;;;AAAAAA""$$$&(( 	 	H((**  44    		     5sG   ! 
.."A> A-)A> ,A> -
A:7A> 9A::A> >
B
B__main__u:   🎨 Image Generation Tools — FAL.ai multi-model supportz<============================================================u(   ❌ FAL_KEY environment variable not setz-   Set it via: export FAL_KEY='your-key-here'z   Get a key: https://fal.ai/u   ✅ FAL.ai API key foundu    ✅ fal_client library availableu;   ❌ fal_client library not found — pip install fal-clientu   🤖 Active model: r'   z ()z
   Speed: r(   ?u     ·  Price: r*   z   Upscaler: r/   onoffz
Available models:u    ← activero   z  z<32z<6u%   
🐛 Debug mode enabled — session )registry
tool_errorimage_generatea  Generate high-quality images from text prompts. The underlying backend (FAL, OpenAI, etc.) and model are user-configured and not selectable by the agent. Returns either a URL or an absolute file path in the `image` field; display it with markdown ![description](url-or-path) and the gateway will deliver it. When the active terminal backend has a different filesystem, successful local-file results may also include `agent_visible_image` for follow-up terminal/file operations.objectstringzJThe text prompt describing the desired image. Be detailed and descriptive.)r  descriptionzlThe aspect ratio of the generated image. 'landscape' is 16:9 wide, 'portrait' is 16:9 tall, 'square' is 1:1.)r  enumr?  r   r%   r9   )r  
propertiesrequired)namer?  r  c                     	 ddl m}   |             }t          |t                    r|                    d          nd}t          |t                    rR|                    d          }t          |t
                    r(|                                r|                                S n2# t          $ r%}t          	                    d|           Y d}~nd}~ww xY wdS )zBReturn the value of ``image_gen.model`` from config.yaml, or None.r   r   r[   Nrk   z"Could not read image_gen.model: %s
r   r   r   r   r   ru   r   ry   r   r   r   r   sectionr   r}   s        r   _read_configured_image_modelrI  	  s    	@111111kmm*4S$*?*?I#''+&&&Tgt$$ 	%KK((E%%% %%++-- %{{}}$ @ @ @93????????@4   B"B& &
C0CCc                     	 ddl m}   |             }t          |t                    r|                    d          nd}t          |t                    rR|                    d          }t          |t
                    r(|                                r|                                S n2# t          $ r%}t          	                    d|           Y d}~nd}~ww xY wdS )u  Return the value of ``image_gen.provider`` from config.yaml, or None.

    We only consult the plugin registry when this is explicitly set — an
    unset value keeps users on the in-tree FAL fallback even when other
    providers happen to be registered (e.g. a user has OPENAI_API_KEY set
    for other features but never asked for OpenAI image gen). ``"fal"``
    explicitly routes through ``plugins/image_gen/fal/`` (which delegates
    back into this module's pipeline via call-time indirection — see
    issue #26241).
    r   r   r[   Nr3  z%Could not read image_gen.provider: %srF  rG  s        r   _read_configured_image_providerrL    s    	C111111kmm*4S$*?*?I#''+&&&Tgt$$ 	%KK
++E%%% %%++-- %{{}}$ C C C<cBBBBBBBBC4rJ  c                 `   t                      }|sdS t                      }	 ddlm} ddlm}  |              ||          }n3# t          $ r&}t                              d|           Y d}~dS d}~ww xY w|K	  |d            ||          }n2# t          $ r%}t                              d|           Y d}~nd}~ww xY w|t          j
        d	dd
| ddd          S 	 | |d}|r||d<    |j        di |}	np# t          $ rc}t                              dt          |dd          |           t          j
        d	ddt          |dd           d| dd          cY d}~S d}~ww xY wt          |	t                    st          j
        d	dddd          S t          j
        |	          S )u  Route the call to a plugin-registered provider when one is selected.

    Returns a JSON string on dispatch, or ``None`` to fall through to the
    in-tree FAL fallback in ``image_generate_tool``.

    Dispatch fires when ``image_gen.provider`` is explicitly set — including
    ``"fal"`` itself, which now resolves to the
    ``plugins/image_gen/fal/`` plugin (the plugin re-enters this module's
    pipeline via ``_it`` indirection so behavior is identical to the
    direct call, just routed through the registry).
    Nr   )get_providerr-  z%image_gen plugin dispatch skipped: %sTr   z*image_gen plugin force-refresh skipped: %sFzimage_gen.provider='zk' is set but no plugin registered that name. Run `hermes plugins list` to see available image gen backends.provider_not_registeredr  rA  rk   z"Image gen provider '%s' raised: %srD  r7  z
Provider 'z	' error: provider_exceptionz#Provider returned a non-dict resultprovider_contractr\   )rL  rI  r0  rN  r1  r.  ry   r   r   r   r   generater   r   r   r   )
r%   r9   
configuredconfigured_modelrN  r.  r3  r}   kwargsr   s
             r   _dispatch_to_plugin_providerrV  0  s    122J t 455
 	:99999AAAAAA""$$$<
++   <cBBBttttt 	L '&T2222#|J//HH 	L 	L 	LLLEsKKKKKKKK	L z1z 1 1 1 4	
 	
 	 	 		"LAA 	/.F7O"",,V,, 
 
 
0Hfc**C	
 	
 	
 zP'(FC"@"@PP3PP.	
 
   	 	 	 	 	 	
 fd## z:-	
 
   	 :fsN   !A 
A4A//A4:B 
CB<<C$C> >
E+AE& E+&E+c                 :   |                      dd          }|st          d          S |                      dt                    }|                     d          }t          ||          }|t	          ||          S t          ||          }t	          ||          S )Nr%   ro   z'prompt is required for image generationr9   r   )r   rA  )r   r;  r   rV  r  r  )argskwr%   r9   r   
dispatchedr   s          r   _handle_image_generater[    s    XXh##F ECDDD88N,@AALffYG .flCCJ1*gNNNN
!  C .c7CCCCr   r[   u   🎨)rD  toolsetschemar   check_fnrequires_envis_asyncemoji)N)[__doc__r   loggingr   r  	threadingrv   typingr   r   r   r   __annotations__r   tools.debug_helpersr   r   r   r   r   tools.managed_tool_gatewayr   tools.tool_backend_helpersr   r   r   r   	getLoggerr   r   rT   ru   r   r   r  r   r   r   r   r   r   r   r   r   r  rf   rg   Lockre   r]   rj   r   tupler   r   r   r   r  r   r   r   r   r   r  floatr  r&  r  r4  print
SystemExitr/  r   r   r   r   midmmarkeractive
session_idtools.registryr:  r;  listIMAGE_GENERATE_SCHEMArI  rL  rV  r[  registerr\   r   r   <module>ry     sA    *   				       & & & & & & & & & & 
C   #     - , , , , ,         
 D C C C C C            
	8	$	$4 %'))!'
 
 $%"%*
 


 
 
 ) .  *))!'
 
 $&!"%* #
 

 
 

 3 8 #*))!'
 
 $%"%*',
 

 
 

 / 4 :D#$
 
 " # 
 

 
 

 1 6 #'#$!#
 
  "
 

 
 
 - 2 !J% *(!&
 
  "
 

 
 
 = B !&#))!'
 
  *!
 


 
 
 ) . $>))!'
 
 $U

 
 
 ', ,,  .))!'
 
 $&!"%
 

 
 
 - > #P6$  
 
 (

 
 
 ), ,. "R6$
 
 (

 
 
 %+ +yP) P)
Dd38n$% P P Pf )" 9  + > K    !  
m-@	A	A	A ! )9>++ 5 5 5# # #0(s (tCH~ ( ( ( (\ *E  *  *  *  *L -*.(? (?(?(? (? 3-	(?
 S#X'(? 
#s(^(? (? (? (?\+c +C +HT#s(^<T + + + +bK# K$ K K K K#*     3  3:        F 3 3:    "Qc Qd Q Q Q Q3 3C 3#* 3PS 3 3 3 3D -)-&* $#'SG SGSGSG "#SG UO	SG
 SG C=SG 3-SG 	SG SG SG SGlK4 K K K K
& & & & &R'T ' ' ' 'Z z	E
FGGG	E(OOO 8999=>>>-...jmm	E
$%%%01111   KLLLjmm ('))NHd	E
LH = =
L
L
L
L
LMMM	E
Ttxx--
T
TDHHWc<R<R
T
TUUU	E
B$((9"5"5@$$5
B
BCCC	E
   ""$$ W WQ"%//rU3UUUaeeGS11UUUgs8K8KUVUUVVVV} LJv7HJJKKK 0 / / / / / / / 	.  !k 
 !011  N/	 
 
 J   >    0L LC L L L L^D D D(  	 "0
	 	 	 	 	 	s   ,K< <L