+
    i                         R t ^ RIt^ RIt^ RIt^ RIHtHtHtHt ^RI	H
t
 R R ltRtRRRRR	R/tR
tRR R lltR R ltR# )z)xAI API client for X (Twitter) discovery.N)AnyDictListOptional)httpc                $    V ^8  d   QhR\         /# )   msg)str)formats   "Q/Users/bowang/.openclaw/workspace/skills/last30days-official/scripts/lib/xai_x.py__annotate__r      s      C     c                    \         P                  P                  RV  R24       \         P                  P                  4        R# )zLog error to stderr.z
[X ERROR] 
N)sysstderrwriteflush)r	   s   &r   
_log_errorr      s-    JJz#b)*JJr   zhttps://api.x.ai/v1/responsesquickdefaultdeepa  You have access to real-time X (Twitter) data. Search for posts about: {topic}

Focus on posts from {from_date} to {to_date}. Find {min_items}-{max_items} high-quality, relevant posts.

IMPORTANT: Return ONLY valid JSON in this exact format, no other text:
{{
  "items": [
    {{
      "text": "Post text content (truncated if long)",
      "url": "https://x.com/user/status/...",
      "author_handle": "username",
      "date": "YYYY-MM-DD or null if unknown",
      "engagement": {{
        "likes": 100,
        "reposts": 25,
        "replies": 15,
        "quotes": 5
      }},
      "why_relevant": "Brief explanation of relevance",
      "relevance": 0.85
    }}
  ]
}}

Rules:
- relevance is 0.0 to 1.0 (1.0 = highly relevant)
- date must be YYYY-MM-DD format or null
- engagement can be null if unknown
- Include diverse voices/accounts if applicable
- Prefer posts with substantive content, not just linksc                    V ^8  d   QhR\         R\         R\         R\         R\         R\         R\        \        ,          R\        \         \        3,          /# )	r   api_keymodeltopic	from_dateto_datedepthmock_responsereturn)r
   r   r   r   )r   s   "r   r   r   :   sn     8S 8S8S8S 8S 	8S
 8S 8S D>8S 
#s(^8Sr   c                   Ve   V# \         P                  V\         R,          4      w  rxRRV  2RR/p	VR8X  d   ^ZM
VR8X  d   ^xM^p
RVRR	R
/.RRRR\        P                  VVVVVR7      /./p\        P
                  ! \        WV
R7      # )a\  Search X for relevant posts using xAI API with live search.

Args:
    api_key: xAI API key
    model: Model to use
    topic: Search topic
    from_date: Start date (YYYY-MM-DD)
    to_date: End date (YYYY-MM-DD)
    depth: Research depth - "quick", "default", or "deep"
    mock_response: Mock response for testing

Returns:
    Raw API response
r   AuthorizationzBearer zContent-Typezapplication/jsonr   r   toolstypex_searchinputroleusercontent)r   r   r   	min_items	max_items)headerstimeout)DEPTH_CONFIGgetX_SEARCH_PROMPTr   r   postXAI_RESPONSES_URL)r   r   r   r   r   r   r    r+   r,   r-   r.   payloads   &&&&&&&     r   search_xr5   :   s    .  '++E<	3JKI 	77),*G W$b)1C#G 	Z 
 	?11'#'' 2 	
G& 99&'RRr   c                    V ^8  d   QhR\         \        \        3,          R\        \         \        \        3,          ,          /# )r   responser!   )r   r
   r   r   )r   s   "r   r   r   u   s3     d dtCH~ d$tCH~2F dr   c                	   . pRV 9   d   V R,          '       d   V R,          p\        V\        4      '       d   VP                  R\        V4      4      M
\        V4      p\	        RV 24       \
        P                  '       d,   \	        R\        P                  ! V ^R7      R,           24       V# RpRV 9   d   V R,          p\        V\        4      '       d   TpM\        V\        4      '       d   V F  p\        V\        4      '       d   VP                  R	4      R8X  d_   VP                  R
. 4      pV FE  p\        V\        4      '       g   K  VP                  R	4      R8X  g   K3  VP                  RR4      p M,	  M)RV9   d
   VR,          pM\        V\        4      '       d   TpV'       g   K   M	  V'       g:   RV 9   d3   V R,           F%  p	RV	9   g   K  V	R,          P                  R
R4      p M	  V'       g   V# \        P                  ! RV4      p
V
'       d8    \        P                  ! V
P                  4       4      pVP                  R. 4      p. p\        V4       EFj  w  r\        V\        4      '       g   K  VP                  RR4      pV'       g   K:  RpVP                  R4      p\        V\        4      '       d   RVP                  R4      '       d   \!        VP                  R^ 4      4      MRRVP                  R4      '       d   \!        VP                  R^ 4      4      MRRVP                  R4      '       d   \!        VP                  R^ 4      4      MRRVP                  R4      '       d   \!        VP                  R^ 4      4      MR/pRRV^,            2R\        VP                  RR4      4      P#                  4       R,          RVR\        VP                  RR4      4      P#                  4       P%                  R4      RVP                  R4      RVR\        VP                  RR4      4      P#                  4       R\'        R\)        R \+        VP                  RR!4      4      4      4      /pVR,          '       d3   \        P,                  ! R"\        VR,          4      4      '       g   RVR&   VP/                  V4       EKm  	  V#   \        P                   d     ELi ; i)#znParse xAI response to extract X items.

Args:
    response: Raw API response

Returns:
    List of item dicts
errormessagezxAI API error: zFull error response: )indent:Ni  N outputr%   r*   output_texttextchoicesz\{[\s\S]*"items"[\s\S]*\}itemsurlN
engagementlikesrepostsrepliesquotesidX:Ni  Nauthor_handle@datewhy_relevant	relevanceg      ?g        g      ?z^\d{4}-\d{2}-\d{2}$)
isinstancedictr0   r
   r   r   DEBUGjsondumpslistresearchloadsgroupJSONDecodeError	enumerateintstriplstripminmaxfloatmatchappend)r7   rA   r9   err_msgr>   r=   itemr*   cchoice
json_matchdataclean_itemsirB   rC   eng_raw
clean_items   &                 r   parse_x_responserm   u   s    E (x00!6@6M6M%))Is5z2SVW\S]_WI./:::.tzz(1/Me/T.UVW K8(#fc"" K%%dD))xx'94"&((9b"9!(A)!T22quuV}7U./eeFB.? % ")  4&*6lc**"&K;   90y))FF"$Y/33IrB *
  7EJ	::j..01DHHWb)E
 KU#$%%hhub! 
((<(gt$$W9M9MW[[!45SWW[[=S=S3w{{9a89Y]W[[=S=S3w{{9a89Y]7;;x;P;P#gkk(A67VZ	J AacU)C,-335d;3S/2!>?EEGNNsSDHHV$*C <=CCES#c5+s1K+L"MN	

 f882C
68J4KLL%)
6":&G $J U ## 		s   !6S S.-S.)      )      )(   <   )r   N)__doc__rR   rU   r   typingr   r   r   r   r<   r   r   r3   r/   r1   r5   rm    r   r   <module>rw      sT    /  	 
 , ,  4  Wx
H;@8Svdr   