+
    i3                        ^ RI t ^ RIt^ RIt^ RIt^ RIHt ^ RIHt ^ RIHt ^ RI	t	]! R4      t
]
R,          t]R,          t]R,          t]
R	,          R
,          t]P                  ! RR4      P!                  R4      t] U u. uF*  q P%                  4       '       g   K  V P%                  4       NK,  	  up t]
R,          tRtRtR tR R ltR tR tR tR R ltR tR R ltR tR tR t R t!R  t"]#R!8X  d
   ]"! 4        R# R# u up i )"    N)defaultdict)datetime)Pathz /Users/ahmad/.openclaw/workspaceknowledge_basezemail_triage_daily.mdzemail_sender_heatmap_weekly.mdmemoryzemail_sender_intelligence.jsonEMAIL_OPS_ACCOUNTSzahmad@bigalc.com,zclickup_credentials.md901613526439zhttps://api.clickup.com/api/v2c                     \         P                  ! V R R R7      pVP                  ^ 8w  d.   \        VP                  P                  4       ;'       g    R4      hVP                  # )T)capture_outputtextzcommand failed)
subprocessrun
returncodeRuntimeErrorstderrstripstdout)cmdps   & :/Users/ahmad/.openclaw/workspace/scripts/email_ops_full.pyr   r      sH    s4d;A||q188>>+??/?@@88O    c                0    V ^8  d   QhR\         R\         /# )   
from_fieldreturnstr)formats   "r   __annotate__r       s     W Wc Wc Wr   c                    \         P                  ! R T ;'       g    R4      pV'       d.   VP                  ^4      P                  4       P	                  4       # \         P                  ! RT ;'       g    R4      pV'       d.   VP                  ^ 4      P                  4       P	                  4       # T ;'       g    RP                  4       P	                  4       # )z	<([^>]+)> .[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})researchgrouplowerr   )r   mm2s   &  r   extract_emailr*      s    
		,
 0 0b1Awwqz!''))	DjFVFVTV	WB+-BHHQK%%'VJ4D4D"3K3K3M3S3S3UVr   c                      \         P                  4       '       d   \         P                  R R7      MRp \        P                  ! RV 4      pV'       d   VP                  ^ 4      # R# )utf-8encodingr"   z\bpk_[A-Za-z0-9_]+\bN)CLICKUP_CREDexists	read_textr$   r%   r&   )txtr(   s     r   load_clickup_tokenr3   '   sL    6B6I6I6K6K,
 
 '
 
2QSC
		)3/A1771:$$r   c                     \         P                  ! \         R \         R2RV /RR/^R7      pVP	                  4        VP                  4       P                  R. 4      # )/list//taskAuthorizationinclude_closedtrue)headersparamstimeouttasks)requestsgetBASEFOLLOWUP_LIST_IDraise_for_statusjson)tokenrs   & r   clickup_get_tasksrF   -   sa    vV$4#5U;oW\E]gwy  gA  KM  	NA668<<$$r   c                     R VRVRRR./p\         P                  ! \         R\         R2RV RR	/V^R
7      pVP	                  4        VP                  4       P                  R4      # )namedescriptiontagsz	email-ops	follow-upr5   r6   r7   zContent-Typezapplication/json)r:   rC   r<   id)r>   postr@   rA   rB   rC   r?   )rD   rH   rI   payloadrE   s   &&&  r   clickup_create_taskrO   3   s~    t]K+{A[\Gf%5$6e<X]_m  pB  GC  JQ  []  	^A668<<r   c                <    V ^8  d   QhR\         R\         R\         /# )r   subjectsenderr   r   )r   s   "r   r    r    :   s!      c 3 3 r   c                   aa T ;'       g    R P                  4       oT;'       g    R P                  4       o. ROp. ROp. ROp\        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       d   R# \        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       d   R# \        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       gA   \        ;QJ d    V3R lR 4       F  '       g   K   RM	  RM! V3R lR 4       4      '       d   R	# R
# )r"   c              3   ,   <"   T F	  qS9   x  K  	  R # 5iN .0kss   & r   	<genexpr>classify.<locals>.<genexpr>B        
%9a69   TF	P1 urgentc              3   ,   <"   T F	  qS9   x  K  	  R # 5irU   rV   rW   s   & r   r[   r\   D   r]   r^   P2 needs responsec              3   ,   <"   T F	  qS9   x  K  	  R # 5irU   rV   rW   s   & r   r[   r\   F   r]   r^   c              3   ,   <"   T F	  qS9   x  K  	  R # 5irU   rV   )rX   xfs   & r   r[   r\   F   s       -H  >G!V  >Gr^   P4 archive/newsletterP3 FYI)urgentasapinvoicepaymentcontractproposalmeetingz	follow uprK   zaction requiredapproval)zre:zfwd:reviewquestionconfirmscheduleinvitedeadline)	
newsletterdiscountsaleoffermasterclasspromo
invitationzdeal of theunsubscribe)noreplyzno-replyzmailer-daemonzrewards@znews@zupdates@)r'   any)rQ   rR   	urgent_kw	action_kw	ignore_kwre   rZ   s   &&   @@r   classifyr   :   s    	BA	2A UIbII
s
%9
%sss
%9
%%%
s
%9
%sss
%9
%%%"
s
%9
%sss
%9
%%%  -H  >G  -H  -H  >G  -H  *H  *H&r   c                     . p \          F  p \        P                  ! \        R RRRVRRRR.	4      4      p\        P                  ! \        R RRRVRRRR.	4      4      p\	        V\
        4      '       d   VP                  R	. 4      M. \	        V\
        4      '       d   VP                  R	. 4      M. ,           pV F  pWR
&   K	  	  V P                  V4       K  	  \        4       p. pV  F]  pVP                  R4      pVP                  R
R4       RV 2p	V'       d   W9   d   K;  VP                  V	4       VP                  V4       K_  	  V#   \         d     EKO  i ; i)goggmailr%   	--accountzis:unread newer_than:2dz--max80--jsonznewer_than:1dthreads_accountrL   r"   -)ACCOUNTSrC   loadsr   
isinstancedictr?   extend	Exceptionsetaddappend)
r   accountq1q2batchtseenouttidkeys
             r   fetch_threadsr   K   sY   G	C;Qjlsuy  |D  !E  F  GBC;Q`bikoqy z{|B.8T.B.BRVVIr*hrsuw{h|h|rvvV_acOd  CE  FE '* NN5!  5D
CeeDkz"%&au-ck

1  J  		s   CE		EEc                0    V ^8  d   QhR\         R\         /# )r   	thread_idr   r   )r   s   "r   r    r    e   s     [ [c [C [r   c                     \         P                  ! \        R RRRRWR.4      4      pVP                  R/ 4      pVP                  R. 4      p. p\	        4       pV EFP  pVP                  R/ 4      P                  R. 4      ;'       g    .  Uu/ uF3  qP                  R	R
4      P                  4       VP                  RR
4      bK5  	  p	pV	P                  RR
4      P                  4       p
V	P                  RR
4      P                  4       pV	P                  RR
4      P                  4       pV
'       d   VP                  V
4       V'       d   VP                  \        V4      4       V'       g   EK  \        P                  ! RV4       F"  pVP                  VP                  4       4       K$  	  EKS  	  V'       d
   VR,          MRpR\        V4      RVR\        \        V4      4      R,          /# u upi   \         d    R^ RRR. /u # i ; i)r   r   threadr?   r   r   messagesrN   r:   rH   r"   valuerQ   fromtor#   z(no subject)message_countlatest_subjectparticipantsN   Nz(unavailable))rC   r   r   r?   r   r'   r   r   r   r*   r$   findalllensortedlistr   )r   r   rawr   msgssubjectsr   r(   hr:   subfrmr   r   
latest_subs   &&             r   thread_summaryr   e   s   [jjeWh{G`hijk2&zz*b)uAOPuuU^`bOcOgOghqsuOvO|O|z|O|~O|uuVR(..0!%%2DDO|G~++i,224C++fb)//1CT2&,,.B$  s!34r$UWYZA $$QWWY/ [  &.Xb\>
SYjF4#56r:
 	
 "  [$4o~WYZZ[s>   BG1 G1 9G,BG1 G1 -A	G1 74G1 ,G1 1HHc                 Z    V R ,          pV R,          pV R,          R8X  d   RV R2# RV R2# )r   rQ   classr_   zSubject: Re: u   

Hi,

Thanks for your message. I reviewed this and I’m prioritizing it now. I’ll send you a concrete update shortly today.

Best,
Ahmadur   

Hi,

Thanks for reaching out. Got it — I’ll review and get back to you with the next step soon.

Best,
AhmadrV   )itemrR   subjs   &  r   draft_replyr      sR    &\F	?DG}#tf  %x  y  	y4&  !Z  [  [r   c                 ,   \         P                  P                  R R R7       / p\         P                  4       '       d,    \        P
                  ! \         P                  RR7      4      pV  EFc  p\        VR,          4      pVP                  VR^ R^ R^ R^ R	^ R
RRR/4      pVR;;,          ^,          uu&   VR,          P                  R4      '       d   VR;;,          ^,          uu&   M}VR,          P                  R4      '       d   VR;;,          ^,          uu&   MIVR,          P                  R4      '       d   VR;;,          ^,          uu&   MVR	;;,          ^,          uu&   VR,          VR
&   VR,          VR&   VR,          ^,          VR,          ^,          ,           VR,          ^,          ,           VR,          ,           VR&   WAV&   EKf  	  \         P                  \        P                  ! V^R7      RR7       V#   \         d    / p ELi ; i)Tparentsexist_okr,   r-   r   countp1p2p3p4last_subjectr"   	last_dater   P1P2P3rQ   datescore)indent)SENDER_MEMORYparentmkdirr0   rC   r   r1   r   r*   r?   
startswith
write_textdumps)itemsmemitemrecs   &    r   update_sender_memoryr      s   td;
C	**]44g4FGC 2f:&ggb7AtQaq$>[]_jlnopGg;!!$''INI[##D))INI[##D))INIINI mNf:K4y1}s4y1}4s4y1}Ds7|SGB! $ TZZA6IJ-  	C	s   *H HHc                 0   \        V P                  4       R  RR7      pRR\        P                  ! 4       P	                  RR7       2RR.pV'       g   VP                  R	4       MVR
,           F  w  r4VP                  RV RVP                  R^ 4       RVP                  R^ 4       RVP                  R^ 4       RVP                  R^ 4       RVP                  RR4      R,           24       K  	  \        P                  RP                  V4      RR7       R# )c                 4    V ^,          P                  R^ 4      # )   r   )r?   )kvs   &r   <lambda>write_heatmap.<locals>.<lambda>   s    1		'10Er   T)r   reversez,# Email Sender Intelligence Heatmap (Weekly)- Generated: secondstimespecr"   z"## Top Senders by Importance Scorez- No data yet.N   Nz- z	 | score r   z | P1:r   z P2:r   z P3:r   z	 | last: r   :NP   N
r,   r-   N)
r   r   r   now	isoformatr   r?   OUT_HEATMAPr   join)r   rankedlinesr   rE   s   &    r   write_heatmapr      s7   CIIK%EtTF6
00)0DEF
,	E %&C[[EBLL2bT155+;*<F155a=/QUVWV[V[\`abVcUddhijininostuivhw  xA  BC  BG  BG  HV  WY  BZ  [^  B_  A`  a  b !499U+g>r   c                 6  a
  \         P                  ! \        . RO4      4      pVP                  R. 4      R,          p. pT  EF/  pTR,          ;'       g    RP                  4       p\        TR,          4      pRT9   d   TP                  R4      R,          MRpT EF  pTP                  R4      ;'       g    RP                  4       o
T'       d   TP                  R4      ^ ,          S
9   dp   TP                  TR,          TP                  RR4      TP                  R	/ 4      P                  R
TP                  R	/ 4      P                  RR4      4      34        EK  T'       g   K  \        ;QJ dJ    T
3R l\        P                  ! RT4      R,           U	u. uF  qNK  	  up	 4       F  '       g   K   RM?	  RM;! T
3R l\        P                  ! RT4      R,           U	u. uF  qNK  	  up	 4       4      '       g   EKd  TP                  TR,          TP                  RR4      TP                  R	/ 4      P                  R
TP                  R	/ 4      P                  RR4      4      34        EK-  	  EK2  	  TR,          #   \         d    . u # i ; iu up	i u up	i )r   events:N<   NrQ   r"   r   @summary.startdateTimer   c              3   ,   <"   T F	  qS9   x  K  	  R # 5irU   rV   )rX   tokess   & r   r[   #calendar_linkage.<locals>.<genexpr>   s     a1`#2I1`r^   z	[a-z]{4,}:N   NTF:N   N)r   calendarr   r   r   )rC   r   r   r?   r   r'   r*   splitr   r   r$   r   )r   calr   linksr   r   sender_emailsender_domainevwr   s   &         @r   calendar_linkager     sI   jjBCD2&s+ E9##**,$RZ07:l7J**3/3PRB&&#))r002B!4!4S!9!!<!BbmRVVIr-BBFF7TVDWD[D[\fhjhnhnovxzh{hh  AG  IK  iL  EM  N  OtaRZZVZ=[\^=_1`=_!=_1`aaRZZVZ=[\^=_1`=_!=_1`aaabmRVVIr-BBFF7TVDWD[D[\fhjhnhnovxzh{hh  AG  IK  iL  EM  N  O 	  :!  	 2a1`s   :I> 
J
 
J>JJc                     \         P                  R R R7       \        4       p . pV R,           F  pRVP                  RR4      RVP                  RR4      RVP                  RR4      RVP                  RR4      R	VP                  R	R4      R
\	        VP                  R	R4      VP                  RR4      4      RVP                  R^ 4      /p\        VR,          VR,          ;'       g    \        ^ ,          4      VR&   VP                  V4       K  	  V Uu. uF  qDR
,          R8X  g   K  VNK  	  ppV Uu. uF  qDR
,          R8X  g   K  VNK  	  ppV Uu. uF  qDR
,          R8X  g   K  VNK  	  ppV Uu. uF  qDR
,          R8X  g   K  VNK  	  ppWV,           R,           Uu. uF"  pRVR,          R	VR	,          R\        V4      /NK$  	  p	p\        4       p
. pV
'       Ed   \        V
4       Uu0 uF0  q"P                  RR4      P                  4       P                  4       kK2  	  ppWV,           R,           F  pRVR	,          R,           2pVP                  4       V9   d   K-  VR,          pRpVR	,          pRV RV RV RVR,           RVR
,           RVR,          R,           RVR,          R,           R R!P                  VR,          R",          4       R#2p \        WV4      pVP                  VV34       K  	  \!        V4      p\#        V4       \%        V4      p. pVP                  R$4       VP                  R%\&        P(                  ! 4       P+                  R&R'7       24       VP                  R4       VP                  R(4       VP                  R)\-        V4       24       VP                  R*\-        V4       24       VP                  R+\-        V4       24       VP                  R,\-        V4       24       VP                  R-\-        V4       24       VP                  R.\-        V	4       24       VP                  R/\-        V4       24       VP                  R4       VP                  R04       RV3RV3RV33 F  w  ppVP                  R1V 24       V'       g   VP                  R24       MxVR3,           Fk  pVR,          pVP                  R4VR,           R5VR	,           R6VR,           R7VR,           R82	4       VP                  R9VR,           R:VR,           24       Km  	  VP                  R4       K  	  VP                  R;4       V'       d,   VR<,           F  w  ppVP                  R=V R>V 24       K  	  MVP                  R?4       VP                  R4       VP                  R@4       V	'       dq   V	RA,           Fb  pVP                  RBVR,           RCVR	,           24       VP                  RD4       VP                  VR,          4       VP                  RD4       Kd  	  MVP                  RE4       VP                  R4       VP                  RF4       V'       d0   VRA,           F!  w  pppVP                  RGV RHV RIV 24       K#  	  MVP                  RJ4       VP                  R4       VP                  RK4       VP                  RL4       VP                  RM4       VP                  RN4       VP                  R4       VP                  RO4       VP                  RP4       VP                  RQ4       VP                  RR4       \.        P1                  RSP                  V4      RTRU7       \3        \5        \.        4      4       RV# u upi u upi u upi u upi u upi u upi   \         d     EK  i ; i)WTr   :Nx   NrL   r"   r   r   r   r   rQ   r   messageCountr   r_   ra   rg   rf   :N   NdraftrH   r   zEmail Follow-up: TodayzWho: z
What: z
When: z
Thread ID: z
Priority: z

Thread summary:
- messages: r   z
- latest: r   z
- participants: z, r   z(

Action: review + respond + close loop.z!# Email Ops v1/v2/v3 Daily Digestr   r   r   z
## Summaryz- Total threads scanned: z- P1 urgent: z- P2 needs response: z
- P3 FYI: z- P4 archive/newsletter: z"- Reply drafts generated (P1/P2): z#- ClickUp follow-up tasks created: z## Key Findingsz### z- Noner   z- [z] u    — z	 (thread )z  - Thread summary: z msgs | latest: z
## Actions:N   Nz- Created ClickUp follow-up: z -> https://app.clickup.com/t/zB- No new follow-up tasks created (none needed or already existed).z### Reply Drafts (P1/P2):N
   Nz	- Thread z | z```z(- No P1/P2 reply drafts needed this run.u8   ### V3 linkage (Email ↔ Calendar/Fathom context proxy)z	- Email: z -> Event: z @ z#- No strong linkage found this run.z## Risks / GapszX- Classification is heuristic and should be tuned with explicit allow/deny sender lists.z7- Drafts are suggested only; no auto-send is performed.z[- Fathom linkage currently uses lightweight proxy matching (full API linkage can be added).z## Next Run Improvementsz4- Add sender-specific rules and domain trust scores.zK- Add promised-followup detection from thread text snippets when available.zK- Add full CRM timeline rollup per contact in dedicated daily/weekly files.r   r,   r-   N)KBr   r   r?   r   r   r   r   r   r3   rF   r   r'   r   rO   r   r   r   r  r   r   r   r   	OUT_DAILYr   printr   )r   r   r   r   rd   r   r   r   r   draftsrD   createdexistingrH   whowhenwhatdescr   r   r  r   titlearrtsndabcs                                 r   mainr$     s^   HHTDH)oGET]]!%%b/quuZ,AEE&"%AEE&"%quuY+XaeeIr2AEE&"4EFAEE.!4
 "0T
DO<Z<ZxXY{![T  	8UjK7!!UB	8	@Uj,??!!UB	@	5UjH4!!UB	5	DUj,CC!!UB	D []ZacfYghYgTUtQtWi9wAOYgFh  EGu?PQV?WX?W!EE&"%++-335?WX'3A&q|D'9&:;Dzz|x'F)CDY<D3%xvXdV=4	Q]^_`g^h]i  jK  LM  N^  L_  `o  Lp  Kq  q}  ~  @P  ~Q  Rb  ~c  }d  dv  w{  w@  w@  AB  CS  AT  Uc  Ad  we  vf  fP  QD)%t<c{+   u
%C#U#EE	LL45	LL=!9!99!9!M NOP	LL	LL	LL,SZL9:	LL=R	*+	LL(R	23	LL:c"gY'(	LL,SWI67	LL5c&k]CD	LL6s7|nEF	LL	LL"##R(+>*ChPR^T
stE7^$LL"WW'(s1V9+R)~U1V9+YWXY]W^V__`ab3B4G3HHXY[\lYmXnop  	R U 
LLcllFAsLL8;YZ]Y^_` # 	YZ	LL	LL+,ALL9QtWIS9?@LLLL7$LL	  	?@	LL	LLKLSzzGAq!LL9QC{1#S<= " 	:;	LL	LL"#	LLkl	LLJK	LLno	LL	LL+,	LLGH	LL^_	LL^_5)G<	#i.A 
9	@	5	D i Y  sN   ^)^5^	^^ )^ 5^%	^%"(^*.6^/^44____main__)$rC   osr$   r   collectionsr   r   pathlibr   r>   	WORKSPACEr  r  r   r   getenvr  r   r   r/   rA   r@   r   r*   r3   rF   rO   r   r   r   r   r   r   r  r$  __name__)r!  s   0r   <module>r,     s    	 	  #   34	!!((	33H$'GG99)+=>DDSI'5x!779IAGGIx533! 'W%%"4[<[>? 0rj zF m	 6s    DD