Ë
    öfýjË   ã                   ó˜   — d dl Z d dlmZm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 d dlmZ d dlmZmZmZmZmZmZ  G d	„ d
«      Zy)é    N)ÚAnyÚOptional)ÚAsyncSession)ÚConflictErrorÚNotFoundErrorÚValidationError)ÚPluginExecutor)ÚPluginÚTenantPlugin)ÚPluginRepository)ÚActivatePluginRequestÚExecutePluginHookRequestÚExecutePluginHookResponseÚPluginResponseÚTenantPluginResponseÚUpdatePluginConfigRequestc                   óD  — e Zd ZdZdefd„Zdd„Zdee   fd„Z	de
j                  d	edefd
„Zde
j                  dee   fd„Zde
j                  de
j                  d	edefd„Zde
j                  de
j                  ddfd„Zde
j                  de
j                  d	edefd„Zy)ÚPluginServicezNManages the plugin catalog, tenant installation, and sandboxed hook execution.Údbc                 ó2   — || _         t        |«      | _        y ©N)r   r   Úrepo)Úselfr   s     ú./root/telegram-saas/modules/plugins/service.pyÚ__init__zPluginService.__init__   s   € ØˆŒÜ$ RÓ(ˆ�	ó    ÚreturnNc              ƒ   ó.  K  — t        ddddddg d¢dd	d
œdd¬«
      t        ddddddg d¢dddœdd¬«
      t        dddddddgdddœdd¬«
      t        dd d!d"d#d$g d¢d%dd&œdd¬«
      g}|D ]%  }| j                  j                  |«      ƒ d'{  –—†  Œ' y'7 Œ­w)(z/Populates the platform with 4 official plugins.Úwelcome_captchazAnti-Spam Welcome CaptchazNPrompts new group or private chat members with a math captcha before unmuting.z1.0.0zPlatform Security TeamÚsecurity)úmessages:readúmessages:writez
users:readé<   Úkick)Útimeout_secondsÚaction_on_failT)
ÚidÚnameÚdescriptionÚversionÚauthorÚcategoryÚrequired_permissionsÚdefault_configÚis_officialÚ	is_activeÚcrypto_price_trackerzCrypto Price & Alert TrackerzULive real-time cryptocurrency price quotes, charting alerts, and conversion commands.z1.2.0zPlatform Financial LabsÚutility)r!   r"   zexternal:httpÚBTCÚUSD)Údefault_symbolÚfiat_currencyÚsentiment_analyzerzUser Chat Sentiment Classifierz\Classifies chat messages in real time into positive, neutral, or angry for admin escalation.zAI Platform LabsÚair!   gffffffæ¿)Úalert_on_negativeÚescalate_thresholdÚauto_support_routerzAutomated Support Ticket RouterzVRoutes incoming user queries to support channels or creates CRM tickets automatically.z2.0.0zPlatform IntegrationsÚintegrationlýÿÿÿ5¥] )Úsupport_group_idÚauto_tagN)r
   r   Úupsert_plugin)r   Úofficial_pluginsÚps      r   Úseed_default_pluginsz"PluginService.seed_default_plugins   sô   è ø€ ô Ø$Ø0ØlØØ/Ø#Ú%VØ35ÈÑPØ Øôô Ø)Ø3ØsØØ0Ø"Ú%YØ27È%ÑPØ Øôô Ø'Ø5ØzØØ)ØØ&5Ð%6Ø59ÐQUÑVØ Øôô Ø(Ø6ØtØØ.Ø&Ú%YØ4AÈtÑTØ ØôðK1
Ððd "ò 	-ˆAØ—)‘)×)Ñ)¨!Ó,×,Ñ,ñ	-Ø,ús   ‚B	BÂBÂBc              ƒ   ó"  K  — | j                   j                  «       ƒ d {  –—† }|s:| j                  «       ƒ d {  –—†  | j                   j                  «       ƒ d {  –—† }|D �cg c]  }t        j                  |«      ‘Œ c}S 7 Œc7 ŒK7 Œ+c c}w ­wr   )r   Úlist_pluginsrB   r   Úmodel_validate)r   ÚpluginsrA   s      r   Úlist_available_pluginsz$PluginService.list_available_pluginsP   sq   è ø€ ØŸ	™	×.Ñ.Ó0×0ˆÙØ×+Ñ+Ó-×-Ð-Ø ŸI™I×2Ñ2Ó4×4ˆGØ:AÖB°Q”×-Ñ-¨aÕ0ÒBÐBð	 1øà-øØ4úÚBùsC   ‚B B¡BºB»!BÁBÁBÁ%B
ÂBÂBÂBÂ
BÚ	tenant_idÚreqc              ƒ   óB  K  — | j                   j                  |j                  «      ƒ d {  –—† }|st        d|j                  › d�«      ‚| j                   j	                  ||j                  |j
                  «      ƒ d {  –—† }|rt        d|j                  › d�«      ‚|j                  xs |j                  }i |j                  ¥|j                  xs i ¥}| j                   j                  ||j                  |j
                  ||j                  |¬«      ƒ d {  –—† }t        |j                  |j                   |j
                  |j                  |j                  |j"                  |j                  |j$                  |j                  |j&                  ¬«
      S 7 �Œl7 �Œ7 Œƒ­w)NzPlugin 'z' not found in registryz*' is already installed for this tenant/bot)rH   Ú	plugin_idÚbot_idÚconfigÚinstalled_versionÚgranted_permissions©
r'   rH   rL   rK   Úplugin_nameÚ
is_enabledrM   rN   rO   Ú
created_at)r   Úget_plugin_by_idrK   r   Úget_tenant_pluginrL   r   r(   rO   r-   r.   rM   Úcreate_tenant_pluginr'   r*   r   rH   rR   rN   rS   )r   rH   rI   ÚpluginÚexistingÚpermissionsÚmerged_configÚtps           r   Úactivate_pluginzPluginService.activate_pluginW   s^  è ø€ ð
 —y‘y×1Ñ1°#·-±-Ó@×@ˆÙÜ (¨3¯=©=¨/Ð9PÐ QÓRÐRð Ÿ™×4Ñ4°YÀÇÁÈsÏzÉzÓZ×ZˆÙÜ (¨6¯;©;¨-Ð7aÐ bÓcÐcð ×-Ñ-ÒL°×1LÑ1LˆØG˜6×0Ñ0ÐG°S·Z±ZÒ5EÀ2ÐGˆà—9‘9×1Ñ1ØØ—i‘iØ—:‘:Ø Ø$Ÿn™nØ +ð 2ó 
÷ 
ˆô $Ø�u‰uØ—l‘lØ—9‘9Ø—l‘lØŸ™Ø—}‘}Ø—9‘9Ø ×2Ñ2Ø "× 6Ñ 6Ø—}‘}ô
ð 	
ð- Aùð
 [ùð
ús6   ‚)F«F¬AFÁ?FÂ BFÄFÄA>FÆFÆFc              ƒ   óô  K  — | j                   j                  |«      ƒ d {  –—† }g }|D ]Å  }| j                   j                  |j                  «      ƒ d {  –—† }|j	                  t        |j                  |j                  |j                  |j                  |r|j                  n|j                  |j                  |j                  |j                  |j                  |j                  ¬«
      «       ŒÇ |S 7 ŒÒ7 Œ ­w)NrP   )r   Úlist_tenant_pluginsrT   rK   Úappendr   r'   rH   rL   r(   rR   rM   rN   rO   rS   )r   rH   Ú	installedÚresr[   rW   s         r   r^   z!PluginService.list_tenant_plugins   sÆ   è ø€ ØŸ)™)×7Ñ7¸	ÓB×Bˆ	ØˆØò 	ˆBØŸ9™9×5Ñ5°b·l±lÓC×CˆFØ�J‰JÜ$Ø—u‘uØ Ÿl™lØŸ9™9Ø Ÿl™lÙ/5 §¢¸2¿<¹<Ø!Ÿ}™}ØŸ9™9Ø&(×&:Ñ&:Ø(*×(>Ñ(>Ø!Ÿ}™}ôõð	ð  ˆ
ð% Cøð Dús"   ‚C8¡C4¢3C8ÁC6ÁBC8Ã6C8Útenant_plugin_idc              ƒ   ó¸  K  — | j                   j                  ||«      ƒ d {  –—† }|st        d«      ‚i }|j                  �i |j                  ¥|j                  ¥|d<   |j                  �|j                  |d<    | j                   j
                  |fi |¤Žƒ d {  –—† }| j                   j                  |j                  «      ƒ d {  –—† }t        |j                  |j                  |j                  |j                  |r|j                  n|j                  |j                  |j                  |j                  |j                  |j                  ¬«
      S 7 �Œ17 Œ»7 Œ�­w)NúInstalled plugin not foundrM   rR   rP   )r   Úget_tenant_plugin_by_idr   rM   rR   Úupdate_tenant_pluginrT   rK   r   r'   rH   rL   r(   rN   rO   rS   )r   rH   rb   rI   r[   ÚupdatesÚupdatedrW   s           r   rf   z"PluginService.update_tenant_plugin”   s)  è ø€ ð —9‘9×4Ñ4°YÐ@PÓQ×QˆÙÜÐ <Ó=Ð=à"$ˆØ�:‰:Ð!Ø ; 2§9¡9Ð ;°·
±
Ð ;ˆG�HÑØ�>‰>Ð%Ø$'§N¡NˆG�LÑ!à6˜Ÿ	™	×6Ñ6°rÑE¸WÑE×EˆØ—y‘y×1Ñ1°'×2CÑ2CÓD×DˆÜ#Ø�z‰zØ×'Ñ'Ø—>‘>Ø×'Ñ'Ù'-˜Ÿš°7×3DÑ3DØ×)Ñ)Ø—>‘>Ø%×7Ñ7Ø '× ;Ñ ;Ø×)Ñ)ô
ð 	
ð Rùð FøØDús5   ‚ E¢E£A7EÂEÂ,EÃEÃBEÅEÅEc              ƒ   ó¼   K  — | j                   j                  ||«      ƒ d {  –—† }|st        d«      ‚| j                   j                  |«      ƒ d {  –—†  y 7 Œ57 Œ­w)Nrd   )r   re   r   Údelete_tenant_plugin)r   rH   rb   r[   s       r   Údeactivate_pluginzPluginService.deactivate_plugin³   sU   è ø€ ð
 —9‘9×4Ñ4°YÐ@PÓQ×QˆÙÜÐ <Ó=Ð=Ø�i‰i×,Ñ,¨RÓ0×0Ñ0ð Røð 	1ús!   ‚ A¢A£/AÁAÁAÁAc              ƒ   ó„  K  — | j                   j                  ||«      ƒ d {  –—† }|st        d«      ‚|j                  st	        d«      ‚| j                   j                  |j                  «      ƒ d {  –—† }|st        d«      ‚t        j                  |||j                  |j                  ¬«      ƒ d {  –—† S 7 Œ—7 ŒH7 Œ­w)Nrd   zPlugin is currently disabledz Base plugin definition not found)Útenant_pluginÚplugin_metaÚ	hook_nameÚpayload)r   re   r   rR   r   rT   rK   r	   Úexecute_hookro   rp   )r   rH   rb   rI   r[   rW   s         r   rq   zPluginService.execute_hook½   s­   è ø€ ð —9‘9×4Ñ4°YÐ@PÓQ×QˆÙÜÐ <Ó=Ð=Ø�}Š}Ü!Ð"@ÓAÐAà—y‘y×1Ñ1°"·,±,Ó?×?ˆÙÜÐ BÓCÐCä#×0Ñ0ØØØ—m‘mØ—K‘Kô	
÷ 
ð 	
ð Røð @øð
ús5   ‚ C ¢B:£AC Á3B<Á4AC Â5B>Â6C Â<C Â>C )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rB   Úlistr   rG   ÚuuidÚUUIDr   r   r\   r^   r   rf   rk   r   r   rq   © r   r   r   r      sü   „ ÙXð)˜<ó )ó5-ðnC¨d°>Ñ.Bó Cð&
à—9‘9ð&
ð #ð&
ð 
ó	&
ðP°4·9±9ð ÀÐFZÑA[ó ð*
à—9‘9ð
ð Ÿ)™)ð
ð 'ð	
ð
 
ó
ð>1à—9‘9ð1ð Ÿ)™)ð1ð 
ó	1ð
à—9‘9ð
ð Ÿ)™)ð
ð &ð	
ð
 
#ô
r   r   )rw   Útypingr   r   Úsqlalchemy.ext.asyncior   Úcore.exceptionsr   r   r   Úmodules.plugins.executorr	   Úmodules.plugins.modelsr
   r   Úmodules.plugins.repositoryr   Úmodules.plugins.schemasr   r   r   r   r   r   r   ry   r   r   ú<module>r�      s3   ðÛ ß  Ý /ß IÑ IÝ 3ß 7Ý 7÷÷ ÷@
ò @
r   