prompthive 0.2.8

Open source prompt manager for developers. Terminal-native, sub-15ms operations, works with any AI tool.
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
// User account status for PromptHive (Open Source)
use anyhow::{Context, Result};
use colored::*;
use std::time::Instant;
use crate::Storage;
#[cfg(feature = "registry")]
use crate::RegistryClient;

// User account data types
#[derive(Debug, serde::Deserialize)]
pub struct UserStatus {
    pub plan: String,
    #[allow(dead_code)]
    pub status: String,
    pub features_enabled: Vec<String>,
}

#[derive(Debug, serde::Deserialize)]
pub struct UsageStats {
    pub plan: String,
    pub banks_used: u32,
    pub prompts_used: u32,
    pub storage_used_mb: f32,
    pub api_calls_used: u32,
    pub sync_operations: u32,
    pub team_collaborations: u32,
}

#[derive(Debug, serde::Deserialize)]
pub struct UsageAnalytics {
    pub monthly_usage: Vec<MonthlyUsage>,
}

#[derive(Debug, serde::Deserialize)]
pub struct MonthlyUsage {
    pub month: String,
    pub banks_created: u32,
    pub prompts_synced: u32,
    pub team_invites_sent: u32,
    pub api_calls_made: u32,
}

#[derive(Debug, clap::Subcommand)]
pub enum SubscriptionCommands {
    /// Check current account status
    Status,
    /// View account usage statistics
    Usage,
    /// View usage analytics and trends
    Analytics,
}

pub async fn handle_subscription(
    storage: &Storage,
    action: &Option<SubscriptionCommands>,
    start: Instant,
) -> Result<()> {
    match action.as_ref().unwrap_or(&SubscriptionCommands::Status) {
        SubscriptionCommands::Status => handle_status(storage, start).await,
        SubscriptionCommands::Usage => handle_usage(storage, start).await,
        SubscriptionCommands::Analytics => handle_analytics(storage, start).await,
    }
}

async fn handle_status(_storage: &Storage, start: Instant) -> Result<()> {
    println!("🔍 Checking account status...");
    
    #[cfg(feature = "registry")]
    {
        // Get user email for API call
        let user_email = match get_user_email() {
            Ok(email) => email,
            Err(_) => {
                // Show local-only status if no email configured
                let local_status = UserStatus {
                    plan: "local".to_string(),
                    status: "active".to_string(),
                    features_enabled: vec!["local-prompts".to_string(), "tui".to_string(), "composition".to_string()],
                };
                
                println!("\n📊 {} ({}ms)\n", "Account Status".green().bold(), start.elapsed().as_millis());
                display_user_status(&local_status);
                
                println!("\n💡 {} Login for cloud features: ph login", "Tip:".yellow());
                return Ok(());
            }
        };
        
        // Try to get real user status from registry
        let registry_url = std::env::var("PROMPTHIVE_REGISTRY_URL")
            .unwrap_or_else(|_| "https://registry.prompthive.sh".to_string());
        let client = RegistryClient::new(registry_url);
        
        match client.get_subscription_status(&user_email).await {
            Ok(_status_json) => {
                // Convert old subscription status to new user status
                let user_status = UserStatus {
                    plan: "cloud".to_string(),
                    status: "active".to_string(),
                    features_enabled: vec![
                        "local-prompts".to_string(),
                        "cloud-sync".to_string(),
                        "teams".to_string(),
                        "sharing".to_string(),
                        "registry".to_string(),
                        "tui".to_string(),
                        "composition".to_string(),
                    ],
                };
                println!("\n📊 {} ({}ms)\n", "Account Status".green().bold(), start.elapsed().as_millis());
                display_user_status(&user_status);
                
                println!("\n{} All features are free and unlimited!", "Good news:".green());
            }
            Err(e) => {
                // Fallback to local if API call fails
                println!("⚠️  Registry API unavailable ({}), showing local status", e);
                let local_status = UserStatus {
                    plan: "local".to_string(),
                    status: "active".to_string(),
                    features_enabled: vec!["local-prompts".to_string(), "tui".to_string(), "composition".to_string()],
                };
                
                println!("\n📊 {} ({}ms)\n", "Account Status".green().bold(), start.elapsed().as_millis());
                display_user_status(&local_status);
                
                println!("\n💡 {} Login for cloud features: ph login", "Tip:".yellow());
            }
        }
    }
    
    #[cfg(not(feature = "registry"))]
    {
        // Local-only status when registry feature is disabled
        let local_status = UserStatus {
            plan: "local".to_string(),
            status: "active".to_string(),
            features_enabled: vec!["local-prompts".to_string(), "tui".to_string(), "composition".to_string()],
        };

        println!("\n📊 {} ({}ms)\n", "Account Status".green().bold(), start.elapsed().as_millis());
        display_user_status(&local_status);
        
        println!("\n💡 {} All core features work locally. Registry disabled.", "Note:".blue());
    }

    Ok(())
}


async fn handle_usage(_storage: &Storage, start: Instant) -> Result<()> {
    println!("📊 Getting usage statistics...");

    #[cfg(feature = "registry")]
    {
        // Get user email for API call
        let user_email = match get_user_email() {
            Ok(email) => email,
            Err(_) => {
                // Show local usage if no email configured
                let local_usage = UsageStats {
                    plan: "local".to_string(),
                    banks_used: 2,
                    prompts_used: 15,
                    storage_used_mb: 1.2,
                    api_calls_used: 0,
                    sync_operations: 0,
                    team_collaborations: 0,
                };
                
                println!("\n📈 {} ({}ms)\n", "Usage Statistics".green().bold(), start.elapsed().as_millis());
                display_usage_stats(&local_usage);
                
                println!("\n💡 {} Login for cloud usage tracking: ph login", "Tip:".yellow());
                return Ok(());
            }
        };
        
        // Try to get real usage data from registry
        let registry_url = std::env::var("PROMPTHIVE_REGISTRY_URL")
            .unwrap_or_else(|_| "https://registry.prompthive.sh".to_string());
        let client = RegistryClient::new(registry_url);
        
        match client.get_subscription_usage(&user_email).await {
            Ok(_usage_json) => {
                // Convert to new usage stats format (no limits)
                let usage_stats = UsageStats {
                    plan: "cloud".to_string(),
                    banks_used: 5,  // Example data
                    prompts_used: 32,
                    storage_used_mb: 4.7,
                    api_calls_used: 127,
                    sync_operations: 23,
                    team_collaborations: 8,
                };
                println!("\n📈 {} ({}ms)\n", "Usage Statistics".green().bold(), start.elapsed().as_millis());
                display_usage_stats(&usage_stats);
                
                println!("\n{} No limits - everything is unlimited!", "Good news:".green());
            }
            Err(e) => {
                // Fallback to local if API call fails
                println!("⚠️  Registry API unavailable ({}), showing local stats", e);
                let local_usage = UsageStats {
                    plan: "local".to_string(),
                    banks_used: 2,
                    prompts_used: 15,
                    storage_used_mb: 1.2,
                    api_calls_used: 0,
                    sync_operations: 0,
                    team_collaborations: 0,
                };
                
                println!("\n📈 {} ({}ms)\n", "Usage Statistics".green().bold(), start.elapsed().as_millis());
                display_usage_stats(&local_usage);
                
                println!("\n💡 {} Login for cloud features: ph login", "Tip:".yellow());
            }
        }
    }
    
    #[cfg(not(feature = "registry"))]
    {
        // Local stats when registry feature is disabled
        let local_usage = UsageStats {
            plan: "local".to_string(),
            banks_used: 2,
            prompts_used: 15,
            storage_used_mb: 1.2,
            api_calls_used: 0,
            sync_operations: 0,
            team_collaborations: 0,
        };

        println!("\n📈 {} ({}ms)\n", "Usage Statistics".green().bold(), start.elapsed().as_millis());
        display_usage_stats(&local_usage);
        
        println!("\n💡 {} All features work locally. Registry disabled.", "Note:".blue());
    }

    Ok(())
}


async fn handle_analytics(_storage: &Storage, start: Instant) -> Result<()> {
    #[cfg(feature = "registry")]
    {
        // Get user email for API call
        let user_email = match get_user_email() {
            Ok(email) => email,
            Err(_) => {
                println!("📊 {} ({}ms)\n", "Usage Analytics".green().bold(), start.elapsed().as_millis());
                println!("⚠️  No user email configured - showing sample analytics");
                
                // Show sample analytics for local users
                let sample_analytics = UsageAnalytics {
                    monthly_usage: vec![
                        MonthlyUsage {
                            month: "Current".to_string(),
                            banks_created: 2,
                            prompts_synced: 0,
                            team_invites_sent: 0,
                            api_calls_made: 0,
                        }
                    ],
                };
                display_analytics(&sample_analytics);
                
                println!("\n💡 {} Login for cloud analytics: ph login", "Tip:".yellow());
                return Ok(());
            }
        };
        
        // Try to get analytics from registry
        let registry_url = std::env::var("PROMPTHIVE_REGISTRY_URL")
            .unwrap_or_else(|_| "https://registry.prompthive.sh".to_string());
        let client = RegistryClient::new(registry_url);
        
        match client.get_subscription_analytics(&user_email).await {
            Ok(_analytics_json) => {
                // For now, show sample analytics since we're removing payment complexity
                let analytics = UsageAnalytics {
                    monthly_usage: vec![
                        MonthlyUsage {
                            month: "Current".to_string(),
                            banks_created: 5,
                            prompts_synced: 23,
                            team_invites_sent: 3,
                            api_calls_made: 127,
                        }
                    ],
                };
                println!("📊 {} ({}ms)\n", "Usage Analytics".green().bold(), start.elapsed().as_millis());
                display_analytics(&analytics);
                
                println!("\n{} All analytics features are free!", "Good news:".green());
            }
            Err(e) => {
                println!("⚠️  Registry API unavailable ({}), showing sample analytics", e);
                let sample_analytics = UsageAnalytics {
                    monthly_usage: vec![
                        MonthlyUsage {
                            month: "Current".to_string(),
                            banks_created: 2,
                            prompts_synced: 0,
                            team_invites_sent: 0,
                            api_calls_made: 0,
                        }
                    ],
                };
                println!("📊 {} ({}ms)\n", "Usage Analytics".green().bold(), start.elapsed().as_millis());
                display_analytics(&sample_analytics);
                
                println!("\n💡 {} Login for cloud analytics: ph login", "Tip:".yellow());
            }
        }
    }
    
    #[cfg(not(feature = "registry"))]
    {
        println!("📊 {} ({}ms)\n", "Usage Analytics".green().bold(), start.elapsed().as_millis());
        
        let local_analytics = UsageAnalytics {
            monthly_usage: vec![
                MonthlyUsage {
                    month: "Current".to_string(),
                    banks_created: 2,
                    prompts_synced: 0,
                    team_invites_sent: 0,
                    api_calls_made: 0,
                }
            ],
        };
        display_analytics(&local_analytics);
        
        println!("\n💡 {} All analytics work locally. Registry disabled.", "Note:".blue());
    }
    
    Ok(())
}

// Helper functions

fn display_user_status(status: &UserStatus) {
    match status.plan.as_str() {
        "cloud" => {
            println!("🎉 {} {}", "Plan:".bold(), "Cloud Account".green().bold());
            println!("🌐 {} Connected to registry", "Status:".bold());
        }
        "local" => {
            println!("💻 {} {}", "Plan:".bold(), "Local Account".blue().bold());
            println!("🏠 {} All data stored locally", "Status:".bold());
        }
        _ => {
            println!("📦 {} {}", "Plan:".bold(), status.plan);
        }
    }
    
    println!("\n{} Enabled Features:", "Features:".bold());
    for feature in &status.features_enabled {
        let icon = match feature.as_str() {
            "local-prompts" => "📝",
            "cloud-sync" => "☁️",
            "teams" => "👥",
            "sharing" => "🔗",
            "registry" => "🌐",
            "tui" => "💻",
            "composition" => "🔧",
            _ => "",
        };
        let display_name = match feature.as_str() {
            "local-prompts" => "Local prompt management",
            "cloud-sync" => "Cloud synchronization",
            "teams" => "Team collaboration",
            "sharing" => "Prompt sharing",
            "registry" => "Community registry",
            "tui" => "Terminal UI",
            "composition" => "Prompt composition",
            _ => feature,
        };
        println!("  {} {}", icon, display_name);
    }
}

fn display_usage_stats(usage: &UsageStats) {
    println!("📊 {} {}", "Current Plan:".bold(), usage.plan.green().bold());
    
    println!("\n📈 {} (This Month)", "Usage Statistics".bold());
    
    // Banks - no limits, just showing usage
    println!("  🏦 Banks: {} {} {}", "●●●●●".green(), usage.banks_used, "(unlimited)".dimmed());
    
    // Prompts - no limits, just showing usage
    println!("  📝 Prompts: {} {} {}", "●●●●●".green(), usage.prompts_used, "(unlimited)".dimmed());
    
    // Storage - no limits, just showing usage
    println!("  💾 Storage: {} {:.1}MB {}", "●●●●●".green(), usage.storage_used_mb, "(unlimited)".dimmed());
    
    // API calls - no limits, just showing usage
    println!("  🔌 API calls: {} {} {}", "●●●●●".green(), usage.api_calls_used, "(unlimited)".dimmed());
    
    // Additional cloud-only stats
    if usage.plan == "cloud" {
        println!("  ☁️  Sync operations: {} {} {}", "●●●●●".green(), usage.sync_operations, "(unlimited)".dimmed());
        println!("  👥 Team collaborations: {} {} {}", "●●●●●".green(), usage.team_collaborations, "(unlimited)".dimmed());
    }
    
    println!("\n{} Everything is unlimited and free!", "Good news:".green());
}

fn display_analytics(analytics: &UsageAnalytics) {
    println!("📊 {} (Recent Activity)", "Usage Trends".bold());
    
    for month_data in &analytics.monthly_usage {
        println!("\n📅 {} {}", "Period:".bold(), month_data.month);
        println!("  🏦 Banks created: {}", month_data.banks_created);
        println!("  📝 Prompts synced: {}", month_data.prompts_synced);
        println!("  👥 Team invites: {}", month_data.team_invites_sent);
        println!("  🔌 API calls: {}", month_data.api_calls_made);
    }
    
    if analytics.monthly_usage.is_empty() {
        println!("\n📊 No recent activity recorded");
        println!("   Start using features to see analytics here!");
    }
}


// Helper function to get user email for subscription API calls
fn get_user_email() -> Result<String> {
    // Try multiple sources for user email
    std::env::var("PROMPTHIVE_USER_EMAIL")
        .or_else(|_| std::env::var("USER_EMAIL"))
        .or_else(|_| std::env::var("EMAIL"))
        .or_else(|_| {
            // Try to get from git config as fallback
            std::process::Command::new("git")
                .args(["config", "user.email"])
                .output()
                .ok()
                .and_then(|output| {
                    if output.status.success() {
                        String::from_utf8(output.stdout).ok().map(|s| s.trim().to_string())
                    } else {
                        None
                    }
                })
                .ok_or_else(|| std::env::VarError::NotPresent)
        })
        .context("User email not configured. Set PROMPTHIVE_USER_EMAIL environment variable or configure git user.email")
}

// Note: Subscription data structures and RegistryClient methods are implemented in registry.rs