xmaster 1.5.2

Enterprise-grade X/Twitter CLI — post, reply, like, retweet, DM, search, and more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::context::AppContext;
use crate::errors::XmasterError;
use crate::intel::store::IntelStore;
use crate::output::{self, OutputFormat, Tableable};
use crate::providers::xai::XaiSearch;
use serde::Serialize;
use std::collections::HashMap;
use std::sync::Arc;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
pub struct RecommendCandidate {
    pub rank: usize,
    pub username: String,
    pub followers: u64,
    pub reply_rate: f64,
    pub score: f64,
    pub source: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub reasons: Vec<String>,
}

/// Adaptive follower band based on user's own follower count.
/// Targets accounts 2x-20x your size for optimal reply ROI.
pub fn default_target_band(my_followers: u64) -> (u64, u64) {
    let min = (my_followers * 2).clamp(500, 5_000);
    let max = (my_followers * 20).clamp(5_000, 100_000);
    (min, max)
}

fn compute_size_fit(target_followers: u64, my_followers: u64) -> f64 {
    let (min, max) = default_target_band(my_followers);
    if target_followers >= min && target_followers <= max {
        1.0
    } else if target_followers < min {
        (target_followers as f64 / min as f64).max(0.2)
    } else {
        (max as f64 / target_followers as f64).max(0.1)
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct RecommendResult {
    pub candidates: Vec<RecommendCandidate>,
    pub suggested_next_commands: Vec<String>,
}

impl Tableable for RecommendResult {
    fn to_table(&self) -> comfy_table::Table {
        let mut table = comfy_table::Table::new();
        table.set_header(vec!["Rank", "@Username", "Followers", "Reply Rate", "Score", "Source"]);
        for c in &self.candidates {
            table.add_row(vec![
                c.rank.to_string(),
                format!("@{}", c.username),
                format_followers(c.followers),
                if c.reply_rate > 0.0 {
                    format!("{:.0}%", c.reply_rate * 100.0)
                } else {
                    "".into()
                },
                format!("{:.2}", c.score),
                c.source.clone(),
            ]);
        }
        table
    }
}

fn format_followers(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.1}K", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

// ---------------------------------------------------------------------------
// Candidate collection (internal)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
struct RawCandidate {
    username: String,
    followers: u64,
    reply_rate: f64,
    source: String,
    relevance: f64,
}

// ---------------------------------------------------------------------------
// Command handler
// ---------------------------------------------------------------------------

pub async fn recommend(
    ctx: Arc<AppContext>,
    format: OutputFormat,
    topic: Option<&str>,
    min_followers: u32,
    count: usize,
) -> Result<(), XmasterError> {
    let mut candidates: HashMap<String, RawCandidate> = HashMap::new();

    // Phase 1a: Local history — proven reciprocators
    if let Ok(store) = IntelStore::open() {
        if let Ok(reciprocators) = store.get_top_reciprocators(min_followers as i64, 20) {
            for r in reciprocators {
                let username = r.username.to_lowercase();
                candidates.entry(username.clone()).or_insert(RawCandidate {
                    username: r.username,
                    followers: r.avg_followers as u64,
                    reply_rate: r.reply_rate,
                    source: "history".into(),
                    relevance: 0.3,
                });
            }
        }
    }

    // Phase 1b: Live mentions — people already talking to you
    let xapi = crate::providers::xapi::XApi::new(ctx.clone());
    if let Ok(user_id) = xapi.get_authenticated_user_id().await {
        if let Ok(mentions) = xapi.get_user_mentions(&user_id, 20).await {
            if let Ok(store) = IntelStore::open() {
                let _ = store.record_discovered_posts("recommend_mentions", &mentions);
            }
            for tweet in &mentions {
                if let Some(username) = &tweet.author_username {
                    let key = username.to_lowercase();
                    if candidates.contains_key(&key) {
                        continue;
                    }
                    let followers = tweet.author_followers.unwrap_or(0);
                    candidates.entry(key).or_insert(RawCandidate {
                        username: username.clone(),
                        followers,
                        reply_rate: 0.0,
                        source: "mentions".into(),
                        relevance: 0.7,
                    });
                }
            }
        }
    }

    // Phase 1c: Topic discovery via xAI search
    if let Some(topic_str) = topic {
        let xai = XaiSearch::new(ctx.clone());
        if let Ok(result) = xai.search_posts(topic_str, 20, None, None, None).await {
            // Extract usernames from citations and text
            let usernames = extract_usernames_from_text(&result.text);
            for username in usernames {
                let key = username.to_lowercase();
                if candidates.contains_key(&key) {
                    continue;
                }
                candidates.entry(key).or_insert(RawCandidate {
                    username,
                    followers: 0,
                    reply_rate: 0.0,
                    source: "topic".into(),
                    relevance: 1.0,
                });
            }
        }
    }

    // Phase 1d: Enrich with reciprocity data from store
    if let Ok(store) = IntelStore::open() {
        for (_, cand) in candidates.iter_mut() {
            if cand.reply_rate == 0.0 {
                if let Ok(Some(info)) = store.get_engagement_reciprocity(&cand.username) {
                    cand.reply_rate = info.reply_rate;
                }
            }
        }
    }

    // Filter by min_followers (skip candidates with 0 followers unless from topic/mentions)
    let filtered: Vec<RawCandidate> = candidates
        .into_values()
        .filter(|c| c.followers >= min_followers as u64 || c.source != "history")
        .collect();

    if filtered.is_empty() {
        return Err(XmasterError::NotFound(
            "No recommendation candidates found. Try: `xmaster engage recommend --topic \"your niche\"` or engage with more accounts first".into(),
        ));
    }

    // Phase 2: Score with opportunity model
    // Try to get user's follower count for adaptive sizing
    let my_followers = {
        let api = crate::providers::xapi::XApi::new(ctx.clone());
        api.get_me().await.ok().and_then(|u| u.public_metrics.as_ref().map(|m| m.followers_count)).unwrap_or(100) as u64
    };

    let mut scored: Vec<RecommendCandidate> = filtered
        .into_iter()
        .map(|c| {
            let reciprocity = c.reply_rate;
            let reach = if c.followers > 0 {
                ((c.followers as f64).log2() / 20.0).min(1.0)
            } else {
                0.0
            };
            let size_fit = compute_size_fit(c.followers, my_followers);
            let relevance = c.relevance;

            // Opportunity scoring: reply_roi proxy + size_fit + reciprocity + reach + relevance
            let score = 0.25 * reciprocity + 0.25 * size_fit + 0.20 * reach + 0.20 * relevance + 0.10 * 1.0;

            let mut reasons = Vec::new();
            if reciprocity > 0.3 { reasons.push(format!("replied back {:.0}% of the time", reciprocity * 100.0)); }
            if size_fit > 0.8 { reasons.push("in your ideal follower band".into()); }
            if reach > 0.6 { reasons.push("large audience amplifies your reply".into()); }
            if relevance > 0.5 { reasons.push("topically relevant".into()); }

            RecommendCandidate {
                rank: 0,
                username: c.username,
                followers: c.followers,
                reply_rate: c.reply_rate,
                score,
                source: c.source,
                reasons,
            }
        })
        .collect();

    // Phase 3: Rank
    scored.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(std::cmp::Ordering::Equal));
    scored.truncate(count);
    for (i, c) in scored.iter_mut().enumerate() {
        c.rank = i + 1;
    }

    let suggested_next_commands: Vec<String> = scored
        .iter()
        .map(|c| format!("xmaster search \"from:{}\" -c 5", c.username))
        .collect();

    let result = RecommendResult {
        candidates: scored,
        suggested_next_commands,
    };

    let metadata = serde_json::json!({
        "suggested_next_commands": result.suggested_next_commands,
    });

    output::render(format, &result, Some(metadata));
    Ok(())
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Watchlist CRUD
// ---------------------------------------------------------------------------

pub async fn watchlist_add(
    ctx: Arc<AppContext>,
    format: OutputFormat,
    username: &str,
    topic: Option<&str>,
) -> Result<(), XmasterError> {
    let store = IntelStore::open().map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;
    let api = crate::providers::xapi::XApi::new(ctx.clone());

    // Fetch user info to get ID and follower count
    let user = api.get_user_by_username(username).await?;
    let followers = user.public_metrics.as_ref().map(|m| m.followers_count as i64).unwrap_or(0);

    store.add_watchlist(username, Some(&user.id), topic, followers)
        .map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;

    #[derive(Serialize)]
    struct WatchlistAddResult { username: String, user_id: String, followers: i64, topic: Option<String>, status: String }
    impl Tableable for WatchlistAddResult {
        fn to_table(&self) -> comfy_table::Table {
            let mut t = comfy_table::Table::new();
            t.set_header(vec!["Field", "Value"]);
            t.add_row(vec!["Username", &format!("@{}", self.username)]);
            t.add_row(vec!["Followers", &format_followers(self.followers as u64)]);
            t.add_row(vec!["Status", &self.status]);
            t
        }
    }
    let display = WatchlistAddResult {
        username: username.to_string(), user_id: user.id, followers, topic: topic.map(String::from), status: "added".into(),
    };
    output::render(format, &display, None);
    Ok(())
}

pub async fn watchlist_list(format: OutputFormat) -> Result<(), XmasterError> {
    let store = IntelStore::open().map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;
    let entries = store.list_watchlist().map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;

    if entries.is_empty() {
        return Err(XmasterError::NotFound("Watchlist is empty. Add accounts with: xmaster engage watchlist add <username>".into()));
    }

    #[derive(Serialize)]
    struct WatchlistDisplay { accounts: Vec<crate::intel::store::WatchlistEntry> }
    impl Tableable for WatchlistDisplay {
        fn to_table(&self) -> comfy_table::Table {
            let mut t = comfy_table::Table::new();
            t.set_header(vec!["Username", "Followers", "Topic"]);
            for a in &self.accounts {
                t.add_row(vec![
                    format!("@{}", a.username),
                    format_followers(a.followers as u64),
                    a.topic.clone().unwrap_or_default(),
                ]);
            }
            t
        }
    }

    output::render(format, &WatchlistDisplay { accounts: entries }, None);
    Ok(())
}

pub async fn watchlist_remove(format: OutputFormat, username: &str) -> Result<(), XmasterError> {
    let store = IntelStore::open().map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;
    let removed = store.remove_watchlist(username).map_err(|e| XmasterError::Config(format!("DB error: {e}")))?;

    if !removed {
        return Err(XmasterError::NotFound(format!("@{username} not in watchlist")));
    }

    #[derive(Serialize)]
    struct RemoveResult { username: String, status: String }
    impl Tableable for RemoveResult {
        fn to_table(&self) -> comfy_table::Table {
            let mut t = comfy_table::Table::new();
            t.add_row(vec![&format!("@{} removed from watchlist", self.username)]);
            t
        }
    }
    output::render(format, &RemoveResult { username: username.to_string(), status: "removed".into() }, None);
    Ok(())
}

// ---------------------------------------------------------------------------
// engage feed — find fresh posts from big accounts to reply to NOW
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
pub struct FeedPost {
    pub id: String,
    pub author: String,
    pub author_followers: u64,
    pub text: String,
    pub age_minutes: i64,
    pub likes: u64,
    pub replies: u64,
    pub reply_command: String,
    #[serde(skip_serializing_if = "is_zero_f32")]
    pub opportunity_score: f32,
}

fn is_zero_f32(v: &f32) -> bool { *v == 0.0 }

#[derive(Debug, Clone, Serialize)]
pub struct FeedResult {
    pub topic: String,
    pub posts: Vec<FeedPost>,
    pub total_found: usize,
    pub filtered_by_followers: usize,
}

impl Tableable for FeedResult {
    fn to_table(&self) -> comfy_table::Table {
        let mut table = comfy_table::Table::new();
        table.set_header(vec!["Age", "Author", "Followers", "Text", "Likes", "Reply cmd"]);
        for p in &self.posts {
            let text_preview: String = p.text.chars().take(60).collect::<String>()
                + if p.text.chars().count() > 60 { "..." } else { "" };
            table.add_row(vec![
                format!("{}m", p.age_minutes),
                format!("@{}", p.author),
                format_followers(p.author_followers),
                text_preview,
                p.likes.to_string(),
                p.reply_command.clone(),
            ]);
        }
        table
    }
}

pub async fn feed(
    ctx: Arc<AppContext>,
    format: OutputFormat,
    topic: &str,
    min_followers: u64,
    max_age_mins: u64,
    count: usize,
) -> Result<(), XmasterError> {
    let api = crate::providers::xapi::XApi::new(ctx.clone());

    // Phase 1: Check watchlist accounts first (saves API search calls)
    let mut watchlist_tweets = Vec::new();
    if let Ok(store) = IntelStore::open() {
        if let Ok(watchlist) = store.list_watchlist() {
            for entry in &watchlist {
                if let Some(ref uid) = entry.user_id {
                    let start_time = {
                        let since = chrono::Utc::now() - chrono::Duration::minutes(max_age_mins as i64);
                        since.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
                    };
                    if let Ok(tweets) = api.get_user_tweets_paginated(uid, 5, Some(&start_time), None).await {
                        for mut t in tweets {
                            // Inject known follower count from watchlist (avoids missing data)
                            if t.author_followers.is_none() {
                                t.author_followers = Some(entry.followers as u64);
                            }
                            if t.author_username.is_none() {
                                t.author_username = Some(entry.username.clone());
                            }
                            watchlist_tweets.push(t);
                        }
                    }
                }
            }
        }
    }

    // Phase 2: Cold search for discovery (only if watchlist didn't fill count)
    let start_time = {
        let now = chrono::Utc::now();
        let since = now - chrono::Duration::minutes(max_age_mins as i64);
        since.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
    };

    let search_tweets = if watchlist_tweets.len() < count {
        api.search_tweets_paginated(
            topic,
            "recent",
            100.min(count * 5),
            Some(&start_time),
            None,
        ).await.unwrap_or_default()
    } else {
        Vec::new()
    };

    // Combine: watchlist first, then search results
    let mut seen_ids = std::collections::HashSet::new();
    let mut tweets = Vec::new();
    for t in watchlist_tweets.into_iter().chain(search_tweets.into_iter()) {
        if seen_ids.insert(t.id.clone()) {
            tweets.push(t);
        }
    }

    if let Ok(store) = IntelStore::open() {
        let _ = store.record_discovered_posts("engage_feed", &tweets);
    }

    let now = chrono::Utc::now();
    let mut posts: Vec<FeedPost> = Vec::new();
    let total_found = tweets.len();
    let mut filtered_count = 0usize;

    for t in tweets {
        let author_followers = t.author_followers.unwrap_or(0);
        if author_followers < min_followers {
            filtered_count += 1;
            continue;
        }

        // Skip replies and retweets — we want original posts
        if let Some(refs) = &t.referenced_tweets {
            if refs.iter().any(|r| r.ref_type == "retweeted" || r.ref_type == "replied_to") {
                continue;
            }
        }

        let age_minutes = t.created_at.as_deref()
            .and_then(|s| chrono::DateTime::parse_from_rfc3339(s).ok())
            .map(|dt| (now - dt.with_timezone(&chrono::Utc)).num_minutes())
            .unwrap_or(0);

        let metrics = t.public_metrics.as_ref();
        let author = t.author_username
            .unwrap_or_else(|| t.author_id.unwrap_or_default());

        posts.push(FeedPost {
            reply_command: format!("xmaster reply {} \"your reply\"", t.id),
            id: t.id,
            author: author.clone(),
            author_followers,
            text: t.text,
            age_minutes,
            likes: metrics.map(|m| m.like_count).unwrap_or(0),
            replies: metrics.map(|m| m.reply_count).unwrap_or(0),
            opportunity_score: 0.0, // computed after collection
        });
    }

    // Score by opportunity: freshness + size_fit + conversation openness
    let my_followers = {
        let api2 = crate::providers::xapi::XApi::new(ctx.clone());
        api2.get_me().await.ok().and_then(|u| u.public_metrics.as_ref().map(|m| m.followers_count)).unwrap_or(100) as u64
    };
    for p in &mut posts {
        let freshness = 1.0 - (p.age_minutes as f64 / max_age_mins as f64).min(1.0);
        let size_fit = compute_size_fit(p.author_followers, my_followers);
        let openness = if p.likes > 0 { (p.replies as f64 / p.likes as f64).min(1.0) } else { 0.5 };
        p.opportunity_score = (0.30 * freshness + 0.30 * size_fit + 0.25 * openness + 0.15) as f32;
    }
    posts.sort_by(|a, b| b.opportunity_score.partial_cmp(&a.opportunity_score).unwrap_or(std::cmp::Ordering::Equal));
    posts.truncate(count);

    // Auto-add high-value accounts from search to watchlist (silent, never fails)
    if let Ok(store) = IntelStore::open() {
        for p in &posts {
            if p.author_followers >= 10_000 {
                let _ = store.add_watchlist(&p.author, None, Some(topic), p.author_followers as i64);
            }
        }
    }

    let result = FeedResult {
        topic: topic.to_string(),
        posts,
        total_found,
        filtered_by_followers: filtered_count,
    };

    output::render(format, &result, None);
    Ok(())
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Extract @usernames from xAI search result text.
fn extract_usernames_from_text(text: &str) -> Vec<String> {
    let mut usernames = Vec::new();
    let mut seen = std::collections::HashSet::new();

    for word in text.split_whitespace() {
        let trimmed = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '@' && c != '_');
        if let Some(name) = trimmed.strip_prefix('@') {
            let clean: String = name
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if clean.len() >= 2 && seen.insert(clean.to_lowercase()) {
                usernames.push(clean);
            }
        }
    }

    usernames
}