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
use std::time::Duration;
use std::{io, str};

use async_openai::types::{
  AssistantObject, AssistantTools, AssistantToolsCode, CreateAssistantRequestArgs, CreateMessageRequestArgs, CreateRunRequestArgs, CreateThreadRequestArgs, MessageContent, RunStatus
};
use async_openai::config::OpenAIConfig;
use async_openai::error::OpenAIError;
use indicatif::ProgressBar;
use async_openai::Client;
use tokio::time::sleep;
use git2::Repository;
use thiserror::Error;
use anyhow::Context;

use crate::config;

#[derive(Error, Debug)]
pub enum ChatError {
  #[error("Failed to build HTTP client")]
  HttpClientBuildError,
  #[error("HTTP error: {0}")]
  HttpRequestError(#[from] reqwest::Error),
  #[error("IO error: {0}")]
  IOError(#[from] io::Error),
  #[error("Failed to parse JSON: {0}")]
  JsonParseError(#[from] serde_json::Error),
  #[error("Anyhow error: {0}")]
  Anyhow(#[from] anyhow::Error),
  #[error("OpenAI error: {0}")]
  OpenAIError(String),
  #[error("Failed to parse response: {1} ({0})")]
  ParseError(serde_json::Error, String),
  #[error("OpenAI error: {0}")]
  OpenAI(#[from] OpenAIError)
}

fn instruction() -> String {
  include_str!("../resources/prompt.md").to_string()
}

#[derive(Debug, Clone, PartialEq)]
pub struct Session {
  pub thread_id:    String,
  pub assistant_id: String
}

impl Session {
  pub async fn new_from_client(client: &Client<OpenAIConfig>) -> Result<Self, ChatError> {
    log::debug!("Creating new session from client");
    let assistant = create_assistant(client).await?;
    let thread_request = CreateThreadRequestArgs::default().build()?;
    let thread = client.threads().create(thread_request).await?;

    Ok(Session {
      thread_id:    thread.id,
      assistant_id: assistant.id
    })
  }

  // Load the session from the repository
  pub async fn load_from_repo(repo: &Repository) -> anyhow::Result<Option<Self>> {
    log::debug!("Loading session from repo");
    let mut config = repo.config().context("Failed to load config")?;
    let thread_id = config.get_string("ai.thread-id").ok();

    let global_config = config
      .open_global()
      .context("Failed to open global config")?;
    let assistant_id = global_config.get_string("ai.assistant-id").ok();
    log::debug!("Loaded session from repo: thread_id: {:?}, assistant_id: {:?}", thread_id, assistant_id);

    match (thread_id, assistant_id) {
      (Some(thread_id), Some(assistant_id)) => {
        Ok(Some(Session {
          thread_id,
          assistant_id
        }))
      }
      _ => Ok(None)
    }
  }

  // Save the session to the repository
  pub async fn save_to_repo(&self, repo: &Repository) -> anyhow::Result<()> {
    log::debug!("Saving session to repo");
    let mut config = repo.config().context("Failed to load config")?;
    config.set_str("ai.thread-id", self.thread_id.as_str())?;
    config.snapshot().context("Failed to save config")?;

    let mut global_config = config
      .open_global()
      .context("Failed to open global config")?;
    global_config.set_str("ai.assistant-id", self.assistant_id.as_str())?;
    global_config
      .snapshot()
      .context("Failed to save global config")?;
    Ok(())
  }
}

#[derive(Debug, Clone, PartialEq)]
pub struct OpenAIResponse {
  pub session:  Session,
  pub response: String
}

// Create a new assistant
async fn create_assistant(client: &Client<OpenAIConfig>) -> Result<AssistantObject, ChatError> {
  let model = config::APP.model.clone();
  let instruction = instruction();
  // let example_jsonl_id = "file-a8ghhy1FbWtBKEadAj5OHJWz";

  let tools = vec![AssistantTools::Code(AssistantToolsCode {
    r#type: "code_interpreter".to_string()
  })];

  let assistant_request = CreateAssistantRequestArgs::default()
    .name("Git Commit Assistant")
    .instructions(&instruction)
    .tools(tools)
    .model(model)
    .build()?;

  Ok(client.assistants().create(assistant_request).await?)
}

#[derive(Debug, Clone)]
struct Connection {
  client:  Client<OpenAIConfig>,
  session: Session
}

impl Connection {
  pub async fn new(session: Option<Session>) -> Result<Self, ChatError> {
    let api_key = config::APP
      .openai_api_key
      .clone()
      .context("Failed to get OpenAI API key, please run `git-ai config set openai-api")?;
    let config = OpenAIConfig::new().with_api_key(api_key);
    let client = Client::with_config(config);

    let session = match session {
      Some(session) => session,
      None => Session::new_from_client(&client).await?
    };

    Ok(Connection {
      client,
      session
    })
  }

  // Create a new run
  async fn create_run(&self) -> Result<Run, ChatError> {
    let request = CreateRunRequestArgs::default()
      .assistant_id(self.session.clone().assistant_id)
      .build()?;
    let run = self
      .client
      .threads()
      .runs(&self.session.thread_id)
      .create(request)
      .await?;
    Ok(Run {
      id:         run.id,
      connection: self.clone()
    })
  }

  // Get the last message from the thread
  async fn last_message(&self) -> Result<String, ChatError> {
    let query = [("limit", "1")];
    let response = self
      .client
      .threads()
      .messages(&self.session.thread_id)
      .list(&query)
      .await?;
    let message_id = response.data.get(0).unwrap().id.clone();
    let message = self
      .client
      .threads()
      .messages(&self.session.thread_id)
      .retrieve(&message_id)
      .await?;
    let content = message.content.get(0).unwrap();
    let MessageContent::Text(text) = &content else {
      return Err(ChatError::OpenAIError("Message content is not text".to_string()));
    };

    Ok(text.text.value.clone())
  }

  async fn create_message(&self, message: &str) -> Result<(), ChatError> {
    let message = CreateMessageRequestArgs::default()
      .role("user")
      .content(message)
      .build()?;
    self
      .client
      .threads()
      .messages(&self.session.thread_id)
      .create(message)
      .await?;
    Ok(())
  }

  async fn into_response(&self) -> Result<OpenAIResponse, ChatError> {
    let message = self.last_message().await?;
    let response = OpenAIResponse {
      response: message,
      session:  self.session.clone()
    };
    Ok(response)
  }
}

#[derive(Debug, Clone)]
struct Run {
  id:         String,
  connection: Connection
}

impl Run {
  pub async fn pull_status(&self) -> Result<RunStatus, ChatError> {
    Ok(
      self
        .connection
        .client
        .threads()
        .runs(&self.connection.session.thread_id)
        .retrieve(self.id.as_str())
        .await?
        .status
    )
  }
}

pub async fn generate(
  diff: String, session: Option<Session>, progressbar: Option<ProgressBar>
) -> Result<OpenAIResponse, ChatError> {
  progressbar
    .clone()
    .map(|pb| pb.set_message("Generating commit message..."));

  let connection = Connection::new(session).await?;
  connection.create_message(&diff).await?;
  let run = connection.create_run().await?;

  return loop {
    match run.pull_status().await? {
      RunStatus::Completed => {
        break connection.into_response().await;
      }
      RunStatus::Failed => {
        break Err(ChatError::OpenAIError("Run failed".to_string()));
      }
      RunStatus::Cancelled => {
        break Err(ChatError::OpenAIError("Run cancelled".to_string()));
      }
      RunStatus::Expired => {
        break Err(ChatError::OpenAIError("Run expired".to_string()));
      }
      RunStatus::RequiresAction => {
        break Err(ChatError::OpenAIError("Run requires action".to_string()));
      }
      RunStatus::InProgress => {
        log::debug!("Run is in progress");
        // progressbar.clone().map(|pb| pb.set_message("In progress..."));
      }
      RunStatus::Queued => {
        log::debug!("Run is queued");
        // progressbar.clone().map(|pb| pb.set_message("Queued..."));
      }
      RunStatus::Cancelling => {
        log::debug!("Run is cancelling");
        // progressbar.clone().map(|pb| pb.set_message("Cancelling..."));
      }
    }

    sleep(Duration::from_millis(300)).await;
  };
}