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
use async_openai::config::OpenAIConfig;
use async_openai::types::{
    ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestUserMessageArgs,
};
use async_openai::Client;
use utils::app_config::AppConfig;
use utils::error::Result;

use crate::db;

/// Show the configuration file
pub fn config() -> Result<()> {
    let config = AppConfig::fetch()?;
    println!("{:#?}", config);

    Ok(())
}

pub fn history() -> Result<()> {
    db::display_commands()?;

    Ok(())
}

pub fn undo() -> Result<()> {
    let last_command = db::get_last_command()?;

    //println!("Undoing: {}", last_command);
    let config =
        OpenAIConfig::new().with_api_key("");

    let client = Client::with_config(config);

    let request = async_openai::types::CreateChatCompletionRequestArgs::default()
        .model("gpt-4")
        .messages([
            ChatCompletionRequestSystemMessageArgs::default()
                .content(
                    "You are an assistant that produces Git commands.

IMPORTANT:
- ONLY RETURN THE CLI COMMAND IN BASH.
- DO NOT ADD ```bash ```
- IF THE COMMAND IS NOT REVESIBLE, RETURN 'NOT REVERSIBLE'
- IF THE COMMAND IS NOT VALID, RETURN 'NOT VALID'",
                )
                .build()
                .unwrap()
                .into(),
            ChatCompletionRequestUserMessageArgs::default()
                .content(format!("Reverse this git command: {}", last_command))
                .build()
                .unwrap()
                .into(),
        ])
        .max_tokens(50_u16)
        .build()
        .unwrap();

    let response = async_std::task::block_on(
        client
            .chat() // Get the API "group" (completions, images, etc.) from the client
            .create(request), // Make the API call in that "group"
    )
    .unwrap();

    println!("{:?}", response);

    Ok(())
}