
    lOit"                        S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	  SSK
Jr  SSKJrJr  SSKJrJrJrJr  SS	KJr  \R,                  " S
S/S9\S 5       5       r\R,                  " SS/S9\" S5      S 5       5       r\R,                  " S
S/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " SS/S9\S 5       5       r\R,                  " S S/S9\S! 5       5       r g)"uP   
Prompt 配置 API

提供 Prompt 模板的管理、编译、验证等功能。
    N)request)category_api_bp)category_admin_requiredcategory_permission_requiredget_current_user)APIResponse)NotFoundErrorValidationError)dbCategoryConfigPromptTemplatePromptTemplateHistory)PromptCompilerz/<category_id>/promptsGET)methodsc                     [         R                  R                  U S9R                  5       n[        R
                  " SU Vs/ s H  o"R                  5       PM     sn0S9$ s  snf )u   获取 Prompt 模板列表)category_id	templatesdata)r   query	filter_byallr   successto_dict)r   r   ts      J   /Users/yu22x/Desktop/ddd_副本121/ctf/app/routes/admin/category/prompt.pyget_promptsr      sW     $$..;.GKKMI;AIIK;< ;s   A 
z/<category_id>/prompt-from-fileviewerc                    [         R                  R                  U 5      nU(       d  [        S5      eUR                  nU(       d  SU  S3n[
        R                  R                  [        5      nUn[        S5       H"  n[
        R                  R                  U5      nM$     [
        R                  R                  XB5      n[
        R                  R                  U5      (       d  [        SU 35      e [        USSS9 nUR                  5       nS	S	S	5        [        R                   " WUS
.S9$ ! , (       d  f       N%= f! ["         a,  n	[        R$                  " S['        U	5       35      s S	n	A	$ S	n	A	ff = f)u   从文件加载 Prompt 模板   方向不存在zge10/z
/CLAUDE.md   u   模板文件不存在: rzutf-8)encodingN)contentpathr   u   读取文件失败: )r   r   getr	   prompt_template_pathosr&   abspath__file__rangedirnamejoinexistsopenreadr   r   	Exceptionserver_errorstr)
r   categorytemplate_pathcurrent_filebase_dir_	full_pathfr%   es
             r   get_prompt_from_filer=      s5    ##''4H-.. 11M}J7
 77??8,LH1X77??8, X5I77>>)$$5m_EFF	I)S73qffhG 4 ""!)
  	 43  I''*>s1vh(GHHIs6   3D? >D.D? .
D<8D? ?
E5	!E0*E50E5POSTc                    [         R                  " 5       nU(       d  [        S5      e[        U UR	                  SS5      UR	                  SS5      UR	                  S5      UR	                  SS5      UR	                  S	S5      UR	                  S
S5      S9n[
        R                  R                  U5        [
        R                  R                  5         [        R                  " SUR                  5       0SS9$ )u   创建 Prompt 模板   无效的请求数据nameu	   新模板template_typemainstage_idr%    description	is_activeT)r   rA   rB   rD   r%   rF   rG   templateu   Prompt 模板创建成功r   message)r   get_jsonr
   r   r'   r   sessionaddcommitr   r   r   )r   r   rH   s      r   create_promptrO   C   s     D566XXfk*hh7*%B'HH]B/((;-H JJNN8JJ(**,-+     z(/<category_id>/prompts/<int:template_id>PUTc           	      p   [         R                  " 5       nU(       d  [        S5      e[        R                  R                  U5      nU(       a  UR                  U :w  a  [        S5      e[        5       nUR                  (       a  UR                  UR                  S5      :w  a  [        UR                  UR                  UR                  UR                  SS5      U(       a  UR                  OSS9n[        R                  R                  U5        U=R                  S-  sl        S	U;   a
  US	   Ul        SU;   a
  US   Ul	        S
U;   a
  US
   Ul        SU;   a
  US   Ul        SU;   a
  US   Ul        [        R                  R)                  5         [*        R,                  " SUR/                  5       0SS9$ )u   更新 Prompt 模板r@      模板不存在r%   change_noterE   N)template_idr%   versionrT   
changed_by   rA   rF   rG   rD   rH   u   Prompt 模板更新成功rI   )r   rK   r
   r   r   r'   r   r	   r   r%   r   idrV   r   rL   rM   rA   rF   rG   rD   rN   r   r   r   )r   rU   r   rH   userhistorys         r   update_promptr\   ^   sy    D566##''4Hx++{:-..D H,,0CC' $$$$3"&twwD
 	

wA ~VD	?#M2d!+.T ,JJ(**,-+ rP   DELETEc                 &   [         R                  R                  U5      nU(       a  UR                  U :w  a  [	        S5      e[
        R                  R                  U5        [
        R                  R                  5         [        R                  " SS9$ )u   删除 Prompt 模板rS   u   Prompt 模板删除成功)rJ   )r   r   r'   r   r	   r   rL   deleterN   r   r   )r   rU   rH   s      r   delete_promptr`      sj     ##''4Hx++{:-..JJhJJ'BCCrP   z0/<category_id>/prompts/<int:template_id>/historyc                     [         R                  R                  US9R                  [         R                  R                  5       5      R                  5       n[        R                  " SU Vs/ s H  o3R                  5       PM     sn0S9$ s  snf )u    获取 Prompt 模板历史版本)rU   r[   r   )
r   r   r   order_byrV   descr   r   r   r   )r   rU   r[   hs       r   get_prompt_historyre      ss     $))333L	'//446	7  w7w!))+w78 7s   ,B
z>/<category_id>/prompts/<int:template_id>/history/<int:version>c                     [         R                  R                  UUS9R                  5       nU(       d  [	        S5      e[
        R                  " SUR                  5       0S9$ )u#   获取特定版本的 Prompt 模板rU   rV      版本不存在rV   r   )r   r   r   firstr	   r   r   r   )r   rU   rV   r[   s       r   get_prompt_versionrj      s_     $))33 4  eg 
 -..Y0A$BCCrP   z>/<category_id>/prompts/<int:template_id>/restore/<int:version>c                 <   [         R                  R                  U5      nU(       d  [        S5      e[        R                  R                  UUS9R                  5       nU(       d  [        S5      e[	        UUR                  UR                  SU S3S9n[        R                  R                  U5        UR                  Ul        UR                  S-   Ul        [        R                  R                  5         [        R                  " SUR                  5       0S	U 3S
9$ )u   恢复到特定版本rS   rg   rh   u   恢复到版本 u    前的备份)rU   r%   rV   rT   rX   rH   u   已恢复到版本 rI   )r   r   r'   r	   r   r   ri   r%   rV   r   rL   rM   rN   r   r   r   )r   rU   rV   rH   r[   current_historys         r   restore_prompt_versionrm      s    ##''4H-..#))33 4  eg 
 -.. ,    &wi}=	O JJNN?# H''!+HJJ(**,-%gY/ rP   z/<category_id>/prompts/compilec                    [         R                  " 5       =(       d    0 n [        5       nSU;   a&  UR                  US   U UR	                  S5      5      nO UR                  XR	                  S5      5      n[        R                  " U[        UR                  S5      5      [        U5      S.S.S9$ ! [         a,  n[        R                  " S[        U5       35      s SnA$ SnAff = f)	u   编译 Prompt 模板template_content
user_input
)total_linestotal_chars)compiled_promptstatsr   u   编译失败: N)r   rK   r   compile_from_templater'   compiler   r   lensplitr2   r3   r4   )r   r   compilercompiledr<   s        r   compile_promptr|      s     #DC!# %55'(&H  ''XXl5KLH""'"8>>$#78"8})
  	  C''.Q(ABBCs   BB1 1
C';!C"C'"C'z/<category_id>/prompts/validatec                    [         R                  " 5       nU(       a  SU;  a  [        S5      e[        R                  R                  U 5      nU(       d  [        S5      e [        5       nUR                  US   UR                  SS95      n[        R                  " SU0S9$ ! [         a,  n[        R                  " S[        U5       35      s S	nA$ S	nAff = f)
u)   验证 Prompt 模板与配置的一致性ro   u   请提供模板内容r!   T)include_config
validationr   u   验证失败: N)r   rK   r
   r   r   r'   r	   r   validate_templater   r   r   r2   r3   r4   )r   r   r5   rz   resultr<   s         r   validate_promptr      s     D%T1566##''4H-..	C!#++#$D1

 ""v(>?? C''.Q(ABBCs    AB" "
C,!CCC)!__doc__r)   flaskr   app.routes.admin.categoryr   app.middleware.authr   r   r   app.utils.responser   app.utils.exceptionsr	   r
   app.models.database.modelsr   r   r   r   app.services.categoryr   router   r=   rO   r\   r`   re   rj   rm   r|   r    rP   r   <module>r      s
  
 
  5 g g * ?  1 /%A  B 85'Jh'!I ( K!IH /&B  C2 AE7S)  T)X AH:V	D  W	D ITYSZ[  \ Wbgahi
D  j
D Wbhaij   k F 7&JC  KC8 86(KC  LCrP   