tuitbot-server 0.1.49

HTTP API server for Tuitbot autonomous X growth assistant
Documentation
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
//! Onboarding-specific endpoints for pre-account X sign-in and profile analysis.
//!
//! These endpoints let users authenticate with X during onboarding,
//! before any account or config exists. Tokens are stored temporarily
//! at `{data_dir}/onboarding_tokens.json` and migrated to the default
//! account's token path when `POST /api/settings/init` completes.
//!
//! - `POST /api/onboarding/x-auth/start`       — start OAuth PKCE flow
//! - `POST /api/onboarding/x-auth/callback`     — exchange code for tokens
//! - `GET  /api/onboarding/x-auth/status`       — check connection status
//! - `POST /api/onboarding/analyze-profile`     — analyze X profile for prefill

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use axum::extract::State;
use axum::Json;
use serde::Deserialize;
use serde_json::{json, Value};
use tuitbot_core::startup::{build_auth_url, build_redirect_uri, generate_pkce};
use tuitbot_core::x_api::auth;
use tuitbot_core::x_api::client::XApiHttpClient;
use tuitbot_core::x_api::XApiClient;

use crate::error::ApiError;
use crate::state::{AppState, PendingOAuth};

/// Sentinel account ID used for onboarding PKCE entries.
const ONBOARDING_ACCOUNT_ID: &str = "__onboarding__";

/// Maximum age for a pending OAuth state entry before it expires.
const OAUTH_STATE_TTL: Duration = Duration::from_secs(600);

/// Return the path for temporary onboarding tokens.
pub fn onboarding_token_path(data_dir: &Path) -> PathBuf {
    data_dir.join("onboarding_tokens.json")
}

/// Serialize the X user profile fields we care about during onboarding.
fn user_to_json(user: &tuitbot_core::x_api::types::User) -> Value {
    json!({
        "id": user.id,
        "username": user.username,
        "name": user.name,
        "profile_image_url": user.profile_image_url,
        "description": user.description,
        "location": user.location,
        "url": user.url,
    })
}

/// Optional request body for starting onboarding auth.
#[derive(Deserialize, Default)]
pub struct StartAuthRequest {
    /// Optional client_id override. If absent, uses the server's in-memory value.
    #[serde(default)]
    pub client_id: Option<String>,
}

/// `POST /api/onboarding/x-auth/start` — start an OAuth PKCE flow.
///
/// No account exists yet. Generates a PKCE challenge and stores it
/// under the `__onboarding__` sentinel. Returns the authorization URL.
///
/// Accepts an optional `client_id` in the request body. If absent or empty,
/// falls back to the server's in-memory `x_client_id` (useful after factory
/// reset when the client_id is still in memory).
pub async fn start_onboarding_auth(
    State(state): State<Arc<AppState>>,
    Json(body): Json<StartAuthRequest>,
) -> Result<Json<Value>, ApiError> {
    let effective_id = body
        .client_id
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| state.x_client_id.clone());

    if effective_id.is_empty() {
        return Err(ApiError::BadRequest(
            "X API client_id not configured. Set x_api.client_id in config.toml.".to_string(),
        ));
    }

    // Read auth config for redirect URI.
    let contents = std::fs::read_to_string(&state.config_path).unwrap_or_default();
    let config: tuitbot_core::config::Config = toml::from_str(&contents).unwrap_or_default();
    let redirect_uri = build_redirect_uri(&config.auth.callback_host, config.auth.callback_port);

    let pkce = generate_pkce();

    let auth_url = build_auth_url(&effective_id, &redirect_uri, &pkce.state, &pkce.challenge);

    // Store pending PKCE state with onboarding sentinel.
    {
        let mut pending = state.pending_oauth.lock().await;
        // Clean up expired entries.
        pending.retain(|_, v| v.created_at.elapsed() < OAUTH_STATE_TTL);

        pending.insert(
            pkce.state.clone(),
            PendingOAuth {
                code_verifier: pkce.verifier,
                created_at: std::time::Instant::now(),
                account_id: ONBOARDING_ACCOUNT_ID.to_string(),
                client_id: effective_id,
            },
        );
    }

    Ok(Json(json!({
        "authorization_url": auth_url,
        "state": pkce.state,
    })))
}

/// Request body for the onboarding OAuth callback.
#[derive(Deserialize)]
pub struct OnboardingCallbackRequest {
    /// The authorization code from X.
    pub code: String,
    /// The state parameter to validate the flow.
    pub state: String,
}

/// `POST /api/onboarding/x-auth/callback` — exchange code for tokens.
///
/// Validates the state parameter against the `__onboarding__` sentinel,
/// exchanges the code for tokens, saves them to `onboarding_tokens.json`,
/// then calls `get_me()` to fetch the authenticated user's profile.
pub async fn complete_onboarding_auth(
    State(state): State<Arc<AppState>>,
    Json(body): Json<OnboardingCallbackRequest>,
) -> Result<Json<Value>, ApiError> {
    // Look up and consume the pending PKCE state.
    let (code_verifier, flow_client_id) = {
        let mut pending = state.pending_oauth.lock().await;
        match pending.remove(&body.state) {
            Some(p) if p.created_at.elapsed() < OAUTH_STATE_TTL => {
                if p.account_id != ONBOARDING_ACCOUNT_ID {
                    return Err(ApiError::BadRequest(
                        "state parameter does not match onboarding flow".to_string(),
                    ));
                }
                (p.code_verifier, p.client_id)
            }
            Some(_) => {
                return Err(ApiError::BadRequest("state expired".to_string()));
            }
            None => {
                return Err(ApiError::BadRequest("invalid or expired state".to_string()));
            }
        }
    };

    // Read auth config for redirect URI.
    let contents = std::fs::read_to_string(&state.config_path).unwrap_or_default();
    let config: tuitbot_core::config::Config = toml::from_str(&contents).unwrap_or_default();
    let redirect_uri = build_redirect_uri(&config.auth.callback_host, config.auth.callback_port);

    // Exchange code for tokens using the client_id from the start flow.
    let stored_tokens = tuitbot_core::startup::exchange_auth_code(
        &flow_client_id,
        &body.code,
        &redirect_uri,
        &code_verifier,
    )
    .await
    .map_err(|e| ApiError::Internal(format!("token exchange failed: {e}")))?;

    // Convert StoredTokens → auth::Tokens.
    let tokens = auth::Tokens {
        access_token: stored_tokens.access_token,
        refresh_token: stored_tokens.refresh_token.unwrap_or_default(),
        expires_at: stored_tokens
            .expires_at
            .unwrap_or_else(|| chrono::Utc::now() + chrono::TimeDelta::hours(2)),
        scopes: stored_tokens.scopes,
    };

    // Save to temporary onboarding path.
    let token_path = onboarding_token_path(&state.data_dir);
    auth::save_tokens(&tokens, &token_path)
        .map_err(|e| ApiError::Internal(format!("failed to save onboarding tokens: {e}")))?;

    // Fetch user identity using the new access token.
    let client = XApiHttpClient::new(tokens.access_token.clone());
    let user = client
        .get_me()
        .await
        .map_err(|e| ApiError::Internal(format!("failed to fetch user profile after auth: {e}")))?;

    Ok(Json(json!({
        "status": "connected",
        "user": user_to_json(&user),
    })))
}

/// `GET /api/onboarding/x-auth/status` — check onboarding auth status.
///
/// Returns whether `onboarding_tokens.json` exists with valid tokens,
/// and if so, fetches the authenticated user's profile.
pub async fn onboarding_auth_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<Value>, ApiError> {
    let token_path = onboarding_token_path(&state.data_dir);

    if !token_path.exists() {
        return Ok(Json(json!({ "connected": false })));
    }

    // Load tokens and check validity.
    let tokens = match auth::load_tokens(&token_path) {
        Ok(Some(t)) if t.expires_at > chrono::Utc::now() => t,
        _ => {
            return Ok(Json(json!({ "connected": false })));
        }
    };

    // Fetch user identity.
    let client = XApiHttpClient::new(tokens.access_token.clone());
    match client.get_me().await {
        Ok(user) => Ok(Json(json!({
            "connected": true,
            "user": user_to_json(&user),
        }))),
        Err(_) => Ok(Json(json!({ "connected": false }))),
    }
}

/// Request body for profile analysis.
#[derive(Deserialize)]
pub struct AnalyzeProfileRequest {
    /// Optional LLM config for enrichment. If absent, heuristic-only.
    pub llm: Option<LlmConfigInput>,
}

/// LLM configuration passed from the frontend during onboarding.
#[derive(Deserialize)]
pub struct LlmConfigInput {
    pub provider: String,
    pub api_key: Option<String>,
    pub model: String,
    pub base_url: Option<String>,
}

/// `POST /api/onboarding/analyze-profile` — analyze X profile for onboarding prefill.
///
/// Loads the onboarding tokens, fetches the user's profile and recent tweets,
/// then runs a two-pass inference pipeline (heuristics + optional LLM) to
/// produce normalized `InferredProfile` suggestions.
pub async fn analyze_profile(
    State(state): State<Arc<AppState>>,
    Json(body): Json<AnalyzeProfileRequest>,
) -> Result<Json<Value>, ApiError> {
    use tuitbot_core::config::LlmConfig;
    use tuitbot_core::llm::factory::create_provider;
    use tuitbot_core::toolkit::profile_inference::{
        enrich_with_llm, extract_heuristics, ProfileInput,
    };

    // 1. Load onboarding tokens.
    let token_path = onboarding_token_path(&state.data_dir);
    if !token_path.exists() {
        return Ok(Json(json!({
            "status": "x_api_error",
            "error": "Not connected. Complete X sign-in first."
        })));
    }

    let tokens = match auth::load_tokens(&token_path) {
        Ok(Some(t)) if t.expires_at > chrono::Utc::now() => t,
        Ok(Some(_)) => {
            return Ok(Json(json!({
                "status": "x_api_error",
                "error": "X tokens expired. Please re-authenticate."
            })));
        }
        _ => {
            return Ok(Json(json!({
                "status": "x_api_error",
                "error": "Failed to load onboarding tokens."
            })));
        }
    };

    // 2. Create X API client and fetch profile + tweets.
    let client = XApiHttpClient::new(tokens.access_token.clone());

    let user = match client.get_me().await {
        Ok(u) => u,
        Err(e) => {
            return Ok(Json(json!({
                "status": "x_api_error",
                "error": format!("Failed to fetch profile: {e}")
            })));
        }
    };

    let tweets = match client.get_user_tweets(&user.id, 50, None).await {
        Ok(resp) => resp.data,
        Err(e) => {
            tracing::warn!(error = %e, "Failed to fetch tweets for profile analysis, continuing with profile-only");
            Vec::new()
        }
    };

    let mut warnings: Vec<String> = Vec::new();

    if tweets.is_empty() {
        warnings.push("No recent tweets found. Analysis relies on profile data only.".into());
    }

    // 3. Run heuristic extraction.
    let input = ProfileInput {
        user: user.clone(),
        tweets,
    };
    let mut profile = extract_heuristics(&input);

    // 4. Optionally enrich with LLM.
    let mut status = "partial";

    if let Some(llm_input) = body.llm {
        let llm_config = LlmConfig {
            provider: llm_input.provider,
            api_key: llm_input.api_key,
            model: llm_input.model,
            base_url: llm_input.base_url,
        };

        match create_provider(&llm_config) {
            Ok(provider) => match enrich_with_llm(profile.clone(), &input, provider.as_ref()).await
            {
                Ok(enriched) => {
                    profile = enriched;
                    status = "ok";
                }
                Err(e) => {
                    warnings.push(format!(
                        "LLM enrichment failed: {e}. Using heuristics only."
                    ));
                }
            },
            Err(e) => {
                warnings.push(format!(
                    "LLM provider configuration error: {e}. Using heuristics only."
                ));
            }
        }
    } else {
        warnings
            .push("No LLM configured. Using heuristic analysis only (limited accuracy).".into());
    }

    Ok(Json(json!({
        "status": status,
        "profile": profile,
        "warnings": warnings,
    })))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn onboarding_token_path_is_in_data_dir() {
        let path = onboarding_token_path(Path::new("/data"));
        assert_eq!(path, PathBuf::from("/data/onboarding_tokens.json"));
    }

    #[test]
    fn user_to_json_includes_profile_fields() {
        let user = tuitbot_core::x_api::types::User {
            id: "123".into(),
            username: "test".into(),
            name: "Test".into(),
            profile_image_url: Some("https://img.example.com/a.jpg".into()),
            description: Some("A bio".into()),
            location: Some("NYC".into()),
            url: Some("https://example.com".into()),
            public_metrics: Default::default(),
        };
        let json = user_to_json(&user);
        assert_eq!(json["username"], "test");
        assert_eq!(json["description"], "A bio");
        assert_eq!(json["location"], "NYC");
        assert_eq!(json["url"], "https://example.com");
    }

    #[test]
    fn user_to_json_with_none_fields() {
        let user = tuitbot_core::x_api::types::User {
            id: "456".into(),
            username: "minimal".into(),
            name: "Minimal User".into(),
            profile_image_url: None,
            description: None,
            location: None,
            url: None,
            public_metrics: Default::default(),
        };
        let json = user_to_json(&user);
        assert_eq!(json["id"], "456");
        assert_eq!(json["username"], "minimal");
        assert_eq!(json["name"], "Minimal User");
        assert!(json["profile_image_url"].is_null());
        assert!(json["description"].is_null());
        assert!(json["location"].is_null());
        assert!(json["url"].is_null());
    }

    #[test]
    fn onboarding_token_path_nested() {
        let path = onboarding_token_path(Path::new("/home/user/.tuitbot"));
        assert_eq!(
            path,
            PathBuf::from("/home/user/.tuitbot/onboarding_tokens.json")
        );
    }

    #[test]
    fn start_auth_request_default() {
        let req: StartAuthRequest = serde_json::from_str("{}").unwrap();
        assert!(req.client_id.is_none());
    }

    #[test]
    fn start_auth_request_with_client_id() {
        let req: StartAuthRequest = serde_json::from_str(r#"{"client_id": "my-id"}"#).unwrap();
        assert_eq!(req.client_id.as_deref(), Some("my-id"));
    }

    #[test]
    fn onboarding_callback_request_deserialize() {
        let req: OnboardingCallbackRequest =
            serde_json::from_str(r#"{"code": "auth_code_123", "state": "csrf_state_456"}"#)
                .unwrap();
        assert_eq!(req.code, "auth_code_123");
        assert_eq!(req.state, "csrf_state_456");
    }

    #[test]
    fn analyze_profile_request_no_llm() {
        let req: AnalyzeProfileRequest = serde_json::from_str(r#"{}"#).unwrap();
        assert!(req.llm.is_none());
    }

    #[test]
    fn analyze_profile_request_with_llm() {
        let req: AnalyzeProfileRequest = serde_json::from_str(
            r#"{"llm": {"provider": "openai", "api_key": "sk-test", "model": "gpt-4", "base_url": null}}"#,
        )
        .unwrap();
        let llm = req.llm.unwrap();
        assert_eq!(llm.provider, "openai");
        assert_eq!(llm.model, "gpt-4");
        assert_eq!(llm.api_key.as_deref(), Some("sk-test"));
    }

    #[test]
    fn oauth_state_ttl_is_ten_minutes() {
        assert_eq!(OAUTH_STATE_TTL, Duration::from_secs(600));
    }

    #[test]
    fn onboarding_account_id_sentinel() {
        assert_eq!(ONBOARDING_ACCOUNT_ID, "__onboarding__");
    }
}