
    `gj                    P   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZmZ  ej                   e      Z ed	       G d
 d             Z ed	       G d d             Z ed	       G d d             ZddZddZddZddZddZddZddZddZd dZy)!aG  
Feishu/Lark meeting-invitation event handling.

Processes ``vc.bot.meeting_invited_v1`` events by converting them into a
synthetic gateway ``MessageEvent``.  Unlike document comments, the response
should go back to the inviter through the normal Hermes gateway pipeline, so
this module does not instantiate an agent directly.
    )annotationsN)	dataclass)SimpleNamespace)AnyDictOptional)MessageEventMessageTypeT)frozenc                  F    e Zd ZU dZded<   dZded<   dZded<   dZded<   y)MeetingInviteUser stropen_iduser_idunion_id	user_nameN)__name__
__module____qualname__r   __annotations__r   r   r        a/root/.hermes/venv/lib/python3.12/site-packages/plugins/platforms/feishu/feishu_meeting_invite.pyr   r      s*    GSGSHcIsr   r   c                  b    e Zd ZU dZded<   dZded<   dZded<   dZded<   dZded	<   d
Z	ded<   y
)MeetingInviteMeetingr   r   idtopic
meeting_nor   intstart_time_msend_time_msNOptional[MeetingInviteUser]	host_user)
r   r   r   r   r   r   r   r!   r"   r$   r   r   r   r   r      s=    BLE3OJM3K-1I*1r   r   c                  F    e Zd ZU dZded<   dZded<   dZded<   d	Zd
ed<   y)MeetingInvitedPayloadr   r   event_idNOptional[MeetingInviteMeeting]meetingr#   inviterr   r    invite_time_s)r   r   r   r'   r   r)   r*   r+   r   r   r   r&   r&   )   s,    Hc.2G+2+/G(/M3r   r&   c                   t        | t              s| t        | d      rt        |       } t        | t              r-| j                         D ci c]  \  }}t        |      | c}}S t        | t              r*	 t        j                  |       }t        |t              r|S i S i S c c}}w # t        t        j                  f$ r i cY S w xY w)z@Coerce a lark SDK object / dict / JSON string into a plain dict.__dict__)
isinstancer   hasattrvarsdictitemsr   jsonloads	TypeErrorJSONDecodeError)valuekvparseds       r   _as_dictr;   1   s    %)e.?GES]D^U%&+kkm4daA	44%	ZZ&F $FD1v9r9I 5 4//0 	I	s   B#8B) )CCc                   t        | j                  d            j                  d      }t        |t              si S |D ]}  }t        |      }t	        |j                  d      xs |j                  d      xs d      j                         }|r|dk7  rUdD ]$  }t        |j                  |            }|s |c c S   i S )zKUnwrap a Feishu ``body.content`` list carrying an application/json payload.bodycontentcontentTypecontent_typer   zapplication/json)datar7   r>   r3   )r;   getr.   listr   lower)	containerr>   itemctypekeypayloads         r   _content_payloadrJ   @   s    y}}V,-11)<Ggt$	 ~DHH]+Mtxx/GM2NTTVU007 	Ctxx}-G	 Ir   c                |    | dv ry	 t        t        |       j                               S # t        t        f$ r Y yw xY w)N)Nr   r   )r    r   stripr5   
ValueError)r7   s    r   
_int_fieldrN   Q   sA    
3u:##%&&z" s   !) ;;c           
        t        |       }|sy t        |j                  d            }t        t        |j                  d      xs d      j	                         t        |j                  d      xs d      j	                         t        |j                  d      xs d      j	                         t        |j                  d      xs d            S )Nr   r   r   r   r   r   )r   r   r   r   )r;   rB   r   r   rL   )r7   rawraw_ids      r   _parse_userrR   Z   s    
5/Ccggdm$FFJJy)/R0668FJJy)/R0668VZZ
+1r288:cggk*0b1	 r   c                   t        |       }|sy t        t        |j                  d      xs d      j	                         t        |j                  d      xs d      t        |j                  d      xs d      t        |j                  d            t        |j                  d            t        |j                  d                  S )	Nr   r   r   r   
start_timeend_timer$   )r   r   r   r!   r"   r$   )r;   r   r   rB   rL   rN   rR   )r7   rP   s     r   _parse_meetingrV   g   s    
5/Cswwt}"#))+#'''"(b)sww|,23 !67swwz23cggk23 r   c           
        t        |       }t        |j                  d            }|xs |}t        |      xs t        |      }|ri ||}t        |j                  d            }t	        |j                  d            }|||j
                  sy t        t        t        |j                  d            j                  d      xs d      ||t        |j                  d                  S )	Neventr)   r*   headerr'   r   invite_time)r'   r)   r*   r+   )	r;   rB   rJ   rV   rR   r   r&   r   rN   )rA   rootrX   r>   r)   r*   s         r   parse_meeting_invited_eventr\   u   s    D>DTXXg&'EMTEu%?)9$)?G$5$G$UYYy12G%))I./G'/1C1C Xdhhx0155jAGRH =!9:	 r   c                   | j                   }| j                  r| j                  j                  ndxs d}|r"|j                  r|j                  j                  ndxs d}|r(|j                  xs |j
                  xs |j                  nd}dj                  d|xs d dd|r|j
                  ndxs d d|r|j                  ndxs d d| d	| dd
ddg
      S )Nr   unknownzunknown meeting
z)You have been invited to join a meeting: zMeeting Number: zTopic: z	Inviter: zHost: zUYou may use lark-cli and the relevant Lark/Feishu meeting skills to join the meeting.zOJoin the meeting directly. Do not ask the user for confirmation before joining.zWIf you cannot join the meeting, reply to the inviter with a concise explanation of why.)r)   r*   r   r$   r   r   r   join)rI   r)   inviter_name	host_namedisplays        r   build_meeting_invite_promptrd      s    ooG18GOO--bVYL07G<M<M"",,SUcZcIELw}}@ 2 2@gjjRcG9978TCT7UVg 2 22S)TUw}}REIFG~&YK c]e	
 r   c                    | j                   rd| j                    S | j                  r| j                  j                  nd}| j                  r| j                  j                  nd}d| d| d| j
                   S )Nz
vc_invite:r   :)r'   r)   r   r*   r   r+   )rI   
meeting_id
inviter_ids      r   
_dedup_keyri      sm    G,,-..'.##BJ,3OO((J
|1ZL'2G2G1HIIr   c           
       K   t        |      }|t        j                  d       yt        |      }t	        | dd      }t        |      r ||      rt        j                  d|       y|j                  }||j                  s9t        j                  d|r|j                  nd|r|j                         yd       yt        |j                  xs d|j                  xs d|j                  xs d      }| j                  |       d{   }|j                  d      xs |j                  xs |j                  }| j                  |j                  |d|j                  d	      xs |j                  xs |j                  ||j                  d
      xs |j                  xs d      }	t!        t#        |      t$        j&                  |	|      }
| j)                  |
       d{    y7 7 w)zFConvert a vc.bot.meeting_invited_v1 event into a gateway MessageEvent.Nz>[Feishu-MeetingInvite] Dropping malformed meeting invite event_is_duplicatez3[Feishu-MeetingInvite] Dropping duplicate event: %szb[Feishu-MeetingInvite] Missing inviter open_id, cannot route reply safely (user_id=%r union_id=%r))r   r   r   r   dmr   user_id_alt)chat_id	chat_name	chat_typer   r   rm   )textmessage_typesourceraw_message)r\   loggerwarningri   getattrcallabledebugr*   r   r   r   r   _resolve_sender_profilerB   r   build_sourcer	   rd   r
   TEXT_handle_message_with_guards)adapterrA   rI   	dedup_keyis_duplicater*   	sender_idsender_profiler   rs   rX   s              r   handle_meeting_invited_eventr      s    )$/GWX7#I7OT:L,y"9JIVooGgoo'&GOOD 'G		
 	 .2		
 	'4'4!!)TI
 #::9EEN"";/W73D3DWI!!""9-SSGOO"&&}5Q9I9IQT " F (1 %%	E 
-
-e
444# F" 5s%   DG*G&CG* G(!G*(G*)r7   r   returnDict[str, Any])rE   r   r   r   )r7   r   r   r    )r7   r   r   r#   )r7   r   r   r(   )rA   r   r   zOptional[MeetingInvitedPayload])rI   r&   r   r   )r~   r   rA   r   r   None)__doc__
__future__r   r3   loggingdataclassesr   typesr   typingr   r   r   gateway.platforms.baser	   r
   	getLoggerr   ru   r   r   r&   r;   rJ   rN   rR   rV   r\   rd   ri   r   r   r   r   <module>r      s    #   ! ! & & <			8	$ $   $2 2 2 $  "
**J-5r   