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
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
use anyhow::{Context, Result};
use clap::Subcommand;
use colored::*;
use regex::Regex;
use std::time::Instant;
// use toml;

use super::common;
use crate::{RegistryClient, Storage};

// Helper functions moved to common module

#[derive(Subcommand)]
pub enum SuggestionsCommands {
    /// List all suggestions for your shared prompts
    #[command(alias = "ls")]
    List {
        /// Show suggestions for specific shared prompt only
        #[arg(short = 's', long = "share")]
        share_id: Option<String>,
        /// Show only pending suggestions (default shows all)
        #[arg(long = "pending")]
        pending: bool,
    },
    /// View detailed information about a specific suggestion
    #[command(alias = "show")]
    View {
        /// Suggestion ID to view
        suggestion_id: String,
    },
    /// Accept a suggestion and apply the improvement
    Accept {
        /// Suggestion ID to accept
        suggestion_id: String,
        /// Apply suggestion to the original prompt
        #[arg(long = "apply")]
        apply: bool,
    },
    /// Reject a suggestion with optional reason
    Reject {
        /// Suggestion ID to reject
        suggestion_id: String,
        /// Optional reason for rejection
        #[arg(short = 'r', long = "reason")]
        reason: Option<String>,
    },
}

pub async fn handle_share(
    storage: &Storage,
    prompt: &str,
    public: bool,
    invite: Option<&str>,
    allow_suggestions: bool,
    expires: Option<u32>,
    start: Instant,
) -> Result<()> {
    // Check if user has API key (Pro feature)
    let api_key = common::require_api_key("Viral sharing")?;

    let registry_url = super::configuration::get_registry_url();
    let client = RegistryClient::new(registry_url).with_api_key(api_key);

    // Validate that either public or invite is specified
    if !public && invite.is_none() {
        eprintln!(
            "{}: Must specify either --public or --invite <emails>",
            "Error".red()
        );
        std::process::exit(1);
    }

    if public {
        handle_share_public(storage, &client, prompt, allow_suggestions, expires, start).await
    } else if let Some(emails) = invite {
        handle_share_invite(
            storage,
            &client,
            prompt,
            emails,
            allow_suggestions,
            expires,
            start,
        )
        .await
    } else {
        unreachable!()
    }
}

async fn handle_share_public(
    storage: &Storage,
    client: &RegistryClient,
    prompt: &str,
    allow_suggestions: bool,
    expires: Option<u32>,
    start: Instant,
) -> Result<()> {
    // Resolve prompt name using fuzzy matching
    let resolved_name = common::resolve_prompt_name(storage, prompt)?;

    // Read the prompt
    let (metadata, body) = storage.read_prompt(&resolved_name)?;

    println!("🌐 {}", "Creating public sharing link...".blue());

    let response = client
        .create_public_share(
            &resolved_name,
            &metadata.description,
            &body,
            allow_suggestions,
            expires,
        )
        .await
        .context("Failed to create public share")?;

    println!(
        "{} created for '{}'",
        "Public link".green(),
        resolved_name.bold()
    );
    println!();
    println!("🔗 {}: {}", "Share URL".cyan(), response.share_url.bold());
    println!("🆔 {}: {}", "Share ID".bright_black(), response.share_id);

    if allow_suggestions {
        println!(
            "💡 {}: Viewers can suggest improvements",
            "Suggestions".yellow()
        );
    } else {
        println!("📖 {}: View-only (no suggestions)", "Mode".bright_black());
    }

    if let Some(exp) = expires {
        println!("{}: {} hours", "Expires in".yellow(), exp);
    } else {
        println!("♾️  {}: Never expires", "Duration".bright_black());
    }

    println!();
    println!(
        "💡 {}: Anyone with this link can view your prompt",
        "Note".bright_blue()
    );
    println!(
        "📊 {}: Track views and suggestions at the registry",
        "Analytics".bright_blue()
    );

    println!(
        "\n⏱️  {} ({}ms)",
        "Public share created".green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

async fn handle_share_invite(
    storage: &Storage,
    client: &RegistryClient,
    prompt: &str,
    emails: &str,
    allow_suggestions: bool,
    expires: Option<u32>,
    start: Instant,
) -> Result<()> {
    // Parse email list
    let email_list: Vec<&str> = emails.split(',').map(|e| e.trim()).collect();
    if email_list.is_empty() {
        eprintln!("{}: No valid emails provided", "Error".red());
        std::process::exit(1);
    }

    // Validate email formats
    let email_regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
    let invalid_emails: Vec<&str> = email_list
        .iter()
        .filter(|email| !email_regex.is_match(email))
        .cloned()
        .collect();
    if !invalid_emails.is_empty() {
        eprintln!(
            "{}: Invalid email format(s): {}",
            "Error".red(),
            invalid_emails.join(", ")
        );
        std::process::exit(1);
    }

    // Resolve prompt name using fuzzy matching
    let resolved_name = common::resolve_prompt_name(storage, prompt)?;

    // Read the prompt
    let (metadata, body) = storage.read_prompt(&resolved_name)?;

    println!("📧 {}", "Creating private sharing invitations...".blue());

    let response = client
        .create_invite_share(
            &resolved_name,
            &metadata.description,
            &body,
            &email_list,
            allow_suggestions,
            expires,
        )
        .await
        .context("Failed to create invite share")?;

    println!(
        "{} created for '{}'",
        "Private invitations".green(),
        resolved_name.bold()
    );
    println!();
    println!("📧 {}: {}", "Invited".cyan(), email_list.join(", ").bold());
    println!("🆔 {}: {}", "Share ID".bright_black(), response.share_id);

    if allow_suggestions {
        println!(
            "💡 {}: Invitees can suggest improvements",
            "Suggestions".yellow()
        );
    } else {
        println!("📖 {}: View-only (no suggestions)", "Mode".bright_black());
    }

    if let Some(exp) = expires {
        println!("{}: {} hours", "Expires in".yellow(), exp);
    } else {
        println!("♾️  {}: Never expires", "Duration".bright_black());
    }

    println!();
    println!(
        "💌 {}: Invitation emails sent to all recipients",
        "Status".bright_blue()
    );
    println!(
        "📊 {}: Track engagement at the registry",
        "Analytics".bright_blue()
    );

    println!(
        "\n⏱️  {} ({}ms)",
        "Invitations sent".green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

pub async fn handle_suggestions(
    storage: &Storage,
    action: &Option<SuggestionsCommands>,
    start: Instant,
) -> Result<()> {
    // Check if user has API key
    let api_key = common::require_api_key("Suggestion management")?;

    let registry_url = super::configuration::get_registry_url();
    let client = RegistryClient::new(registry_url).with_api_key(api_key);

    match action {
        Some(SuggestionsCommands::List { share_id, pending }) => {
            handle_suggestions_list(storage, &client, share_id.as_deref(), *pending, start).await
        }
        Some(SuggestionsCommands::View { suggestion_id }) => {
            handle_suggestions_view(storage, &client, suggestion_id, start).await
        }
        Some(SuggestionsCommands::Accept {
            suggestion_id,
            apply,
        }) => handle_suggestions_accept(storage, &client, suggestion_id, *apply, start).await,
        Some(SuggestionsCommands::Reject {
            suggestion_id,
            reason,
        }) => {
            handle_suggestions_reject(storage, &client, suggestion_id, reason.as_deref(), start)
                .await
        }
        None => {
            // Default action - list all suggestions
            handle_suggestions_list(storage, &client, None, false, start).await
        }
    }
}

async fn handle_suggestions_list(
    _storage: &Storage,
    client: &RegistryClient,
    share_id: Option<&str>,
    pending_only: bool,
    start: Instant,
) -> Result<()> {
    println!("📋 {}", "Fetching suggestions...".blue());

    let suggestions = match client.list_suggestions(share_id, pending_only).await {
        Ok(suggestions) => suggestions,
        Err(e) => {
            eprintln!("{}: Failed to fetch suggestions: {}", "Error".red(), e);
            std::process::exit(1);
        }
    };

    if suggestions.is_empty() {
        println!("📭 {}", "No suggestions found".yellow());
        if pending_only {
            println!(
                "💡 Try running {} to see all suggestions",
                "ph suggestions list".green()
            );
        }
        return Ok(());
    }

    println!();
    println!("📝 {} suggestions found:", suggestions.len());
    println!();

    for suggestion in &suggestions {
        let status_display = match suggestion.status.as_str() {
            "pending" => suggestion.status.yellow(),
            "accepted" => suggestion.status.green(),
            "rejected" => suggestion.status.red(),
            _ => suggestion.status.normal(),
        };

        println!(
            "  {} {} | {} | {}",
            "📝".cyan(),
            suggestion.id.bright_blue(),
            status_display,
            suggestion.shared_prompt_name.bold()
        );

        if !suggestion.suggestion_text.is_empty() {
            println!("     {}", suggestion.suggestion_text.dimmed());
        }

        if let Some(email) = &suggestion.suggested_by_email {
            println!("     By: {}", email.bright_black());
        }

        println!();
    }

    println!("💡 Commands:");
    println!(
        "   View suggestion: {} view <suggestion-id>",
        "ph suggestions".green()
    );
    println!(
        "   Accept: {} accept <suggestion-id> --apply",
        "ph suggestions".green()
    );
    println!(
        "   Reject: {} reject <suggestion-id>",
        "ph suggestions".green()
    );

    println!(
        "\n⏱️  {} ({}ms)",
        "Suggestions listed".green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

async fn handle_suggestions_view(
    _storage: &Storage,
    client: &RegistryClient,
    suggestion_id: &str,
    start: Instant,
) -> Result<()> {
    println!("🔍 Fetching suggestion details...");

    let suggestion = match client.get_suggestion(suggestion_id).await {
        Ok(suggestion) => suggestion,
        Err(e) => {
            eprintln!("{}: Failed to fetch suggestion: {}", "Error".red(), e);
            std::process::exit(1);
        }
    };

    println!();
    println!(
        "📝 {} {}",
        "Suggestion".blue().bold(),
        suggestion.id.bright_blue()
    );
    println!();

    println!(
        "📋 {}: {}",
        "Status".bold(),
        match suggestion.status.as_str() {
            "pending" => suggestion.status.yellow(),
            "accepted" => suggestion.status.green(),
            "rejected" => suggestion.status.red(),
            _ => suggestion.status.normal(),
        }
    );

    println!(
        "🎯 {}: {}",
        "For prompt".bold(),
        suggestion.shared_prompt_name.bright_blue()
    );

    if let Some(email) = &suggestion.suggested_by_email {
        println!("👤 {}: {}", "Suggested by".bold(), email);
    }

    println!("📅 {}: {}", "Created".bold(), &suggestion.created_at);

    println!();

    if !suggestion.suggestion_text.is_empty() {
        println!("📄 {}:", "Summary".bright_blue().bold());
        println!("{}", suggestion.suggestion_text);
        println!();
    }

    if let Some(content) = &suggestion.improvement_content {
        println!("{}:", "Improved Content".bright_green().bold());
        println!("{}", content);
        println!();
    }

    if !suggestion.suggestion_text.is_empty() {
        println!("💭 {}:", "Reasoning".bright_yellow().bold());
        println!("{}", suggestion.suggestion_text);
        println!();
    }

    if suggestion.status == "pending" {
        println!("🎯 Next Steps:");
        println!(
            "   Accept: {} accept {} --apply",
            "ph suggestions".green(),
            suggestion.id
        );
        println!(
            "   Reject: {} reject {} --reason \"reason\"",
            "ph suggestions".green(),
            suggestion.id
        );
    }

    println!(
        "\n⏱️  {} ({}ms)",
        "Suggestion viewed".green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

async fn handle_suggestions_accept(
    _storage: &Storage,
    client: &RegistryClient,
    suggestion_id: &str,
    apply: bool,
    start: Instant,
) -> Result<()> {
    println!("✅ Accepting suggestion...");

    let result = match client.accept_suggestion(suggestion_id).await {
        Ok(result) => result,
        Err(e) => {
            eprintln!("{}: Failed to accept suggestion: {}", "Error".red(), e);
            std::process::exit(1);
        }
    };

    println!("{}", "Suggestion accepted successfully".green());

    if apply && result.improvement_content.is_some() {
        println!("🔄 Applying improvement to local prompt...");

        // Apply the suggestion to the local prompt
        if let Some(content) = &result.improvement_content {
            let prompt_name = &result.shared_prompt_name;

            // Try to find and update the local prompt
            // Try to update the local prompt - simplified for now
            println!(
                "{} would be updated locally",
                prompt_name.bright_blue().bold()
            );
            println!("💡 Feature: Apply improvement to local prompt");
            println!("📝 Improved content would be:");
            println!("{}", content);
        }
    } else if result.improvement_content.is_some() {
        println!(
            "📝 To apply improvements locally, run: {} accept {} --apply",
            "ph suggestions".green(),
            suggestion_id
        );
    }

    if let Some(email) = &result.suggested_by_email {
        println!(
            "📧 {}: {} will be notified of acceptance",
            "Notification".blue(),
            email
        );
    }

    println!(
        "\n⏱️  {} ({}ms)",
        "Suggestion accepted".green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

async fn handle_suggestions_reject(
    _storage: &Storage,
    client: &RegistryClient,
    suggestion_id: &str,
    reason: Option<&str>,
    start: Instant,
) -> Result<()> {
    println!("❌ Rejecting suggestion...");

    let result = match client.reject_suggestion(suggestion_id, reason).await {
        Ok(result) => result,
        Err(e) => {
            eprintln!("{}: Failed to reject suggestion: {}", "Error".red(), e);
            std::process::exit(1);
        }
    };

    println!("{}", "Suggestion rejected".red());

    if let Some(reason_text) = reason {
        println!("📝 {}: {}", "Reason".bold(), reason_text);
    }

    if let Some(email) = &result.suggested_by_email {
        println!(
            "📧 {}: {} will be notified of rejection",
            "Notification".blue(),
            email
        );
    }

    println!(
        "💡 {}: Constructive feedback helps improve future suggestions",
        "Tip".bright_blue()
    );

    println!(
        "\n⏱️  {} ({}ms)",
        "Suggestion rejected".red(),
        start.elapsed().as_millis()
    );

    Ok(())
}