git_revise/ai/
gemini.rs

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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::AI;
use crate::ReviseResult;

#[derive(Debug, Clone)]
pub struct Gemini {
    prompt: String,
    url: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Commit {
    #[serde(rename = "type")]
    pub kind: String,
    pub message: String,
    pub body: String,
}

impl Gemini {
    pub fn new(key: &str) -> Self {
        let prompt = r#"
        # Character
            You're a brilliant coding buddy with top-notch proficiency in Git. Your main duty is to assist users in crafting clear and precise Git commit messages.

        ## Skills

        ### Skill 1: Multilingual Translation
        - Recognize translation requests in the format: "SourceLanguage:TargetLanguage; Content"
        - Identify requests starting with "这是一个翻译commit" as translation tasks
        - Translate the given content from the source language to the target language
        - Preserve the original text alongside the translation in the output
        - Adapt the translation to fit the context of Git commit messages
        - Example input: "中文:English; 这是一个翻译commit, 优化用户界面布局"
        - Example output: 
          ```json
          [
            {
              "type": "translation",
              "message": "Optimize user interface layout",
              "body": "A long body with details about the changes made"
            },
            {
              "type": "translation",
              "message": "Optimize user interface layout",
              "body": "A long body with details about the changes made"
            },
            {
              "type": "translation",
              "message": "Optimize user interface layout",
              "body": "A long body with details about the changes made"
            }
          ]
          ```

        ### Skill 2: The Commit Message Maverick
        - Process the git diff or description given by the user
        - Curate commit messages that confidently and tersely summarize the changes made
        - Always provide exactly three alternative commit messages for each request
        - Ensure diversity in style and content among the three alternatives

        ## Output Format
        The outcome should adhere to the following structure:
        ```json
        [
          {"type": "<type>", "message": "<message>", "body": "<body>"},
          {"type": "<type>", "message": "<message>", "body": "<body>"},
          {"type": "<type>", "message": "<message>", "body": "<body>"}
        ]
        ```

        ## Constraints
        - Commit messages should be between 5-20 words
        - If the message surpasses this limit, abbreviate it without shedding essential details while employing the 'body' part for detailed elaboration
        - Do not include prefixes like "feat:", "fix:", etc. in the commit message, just put it in <type> part, and start the <message> with a verb
        - Guarantee that all dialogues are carried out in the English language, except for translation requests
        - Remain concentrated on tasks strictly linked with creating Git commit messages
        - Remember to always provide three distinct commit message options.

        ## Error Handling
        If the user's submission doesn't correspond with the demanded parameters, generate this response:
        ```json
        [{"type": "error", "message": "Request processing failure", "body":"The submitted input isn't compatible with the required parameters"}]
        ```

        "#;
        let url = format!(
            "{}/models/{}:{}?key={}",
            "https://generativelanguage.googleapis.com/v1beta",
            "gemini-1.5-pro-latest",
            "generateContent",
            key
        );
        Self {
            prompt: prompt.to_string(),
            url,
        }
    }

    pub async fn call(
        &self,
        input: &str,
    ) -> ReviseResult<HashMap<String, Commit>> {
        let txt_request = Request {
            contents: vec![
                Content {
                    role: Role::User,
                    parts: vec![Part {
                        text: Some(self.prompt.clone()),
                        inline_data: None,
                        file_data: None,
                        video_metadata: None,
                    }],
                },
                Content {
                    role: Role::User,
                    parts: vec![Part {
                        text: Some(input.to_string()),
                        inline_data: None,
                        file_data: None,
                        video_metadata: None,
                    }],
                },
            ],
            tools: vec![],
            safety_settings: vec![],
            generation_config: Some(GenerationConfig {
                temperature: None,
                top_p: None,
                top_k: None,
                candidate_count: None,
                max_output_tokens: None,
                stop_sequences: None,
                response_mime_type: Some("application/json".to_string()),
            }),

            system_instruction: None,
        };

        let client: reqwest::Client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| anyhow::anyhow!(e.to_string()))?;
        let request_builder = client
            .post(&self.url)
            .header(reqwest::header::USER_AGENT, "crate/revise")
            .header(reqwest::header::CONTENT_TYPE, "application/json");
        let result = request_builder.json(&txt_request).send().await?;
        match result.status() {
            reqwest::StatusCode::OK => {
                let response = result.json::<GeminiResponse>().await?;

                let text = response
                    .candidates
                    .first()
                    .ok_or_else(|| anyhow::anyhow!("No candidates found"))?
                    .content
                    .parts
                    .first()
                    .ok_or_else(|| anyhow::anyhow!("No parts found"))?
                    .text
                    .clone()
                    .ok_or_else(|| anyhow::anyhow!("No text found"))?
                    .clone();
                let messages: Vec<Commit> = serde_json::from_str(&text)?;
                let mut m = HashMap::new();
                for message in messages {
                    let msg = format!("Message: {}", message.message);
                    let body = format!("Body: {}", message.body);
                    m.insert(msg + "\n\r" + &body, message);
                }

                Ok(m)
            }
            _ => Err(anyhow::anyhow!(
                "Failed to get response from Gemini API: {}, response: {}",
                result.status(),
                result.text().await?
            )),
        }
    }
}

impl AI<HashMap<String, Commit>> for Gemini {
    async fn generate_response(
        &self,
        input: &str,
    ) -> ReviseResult<HashMap<String, Commit>> {
        self.call(input).await
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Request {
    pub contents: Vec<Content>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<Tools>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[serde(default, rename = "safetySettings")]
    pub safety_settings: Vec<SafetySettings>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, rename = "generationConfig")]
    pub generation_config: Option<GenerationConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, rename = "system_instruction")]
    pub system_instruction: Option<SystemInstructionContent>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GeminiResponse {
    pub candidates: Vec<Candidate>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Content {
    pub role: Role,
    #[serde(default)]
    pub parts: Vec<Part>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Part {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inline_data: Option<InlineData>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_data: Option<FileData>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_metadata: Option<VideoMetadata>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    User,
    Model,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InlineData {
    pub mime_type: String,
    pub data: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FileData {
    pub mime_type: String,
    pub file_uri: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VideoMetadata {
    pub start_offset: StartOffset,
    pub end_offset: EndOffset,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct StartOffset {
    pub seconds: i32,
    pub nanos: i32,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EndOffset {
    pub seconds: i32,
    pub nanos: i32,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Tools {
    #[serde(rename = "functionDeclarations")]
    pub function_declarations: Vec<FunctionDeclaration>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FunctionDeclaration {
    pub name: String,
    pub description: String,
    pub parameters: serde_json::Value,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SafetySettings {
    pub category: HarmCategory,
    pub threshold: HarmBlockThreshold,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GenerationConfig {
    pub temperature: Option<f32>,
    pub top_p: Option<f32>,
    pub top_k: Option<i32>,
    pub candidate_count: Option<i32>,
    pub max_output_tokens: Option<i32>,
    pub stop_sequences: Option<Vec<String>>,
    pub response_mime_type: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SystemInstructionContent {
    #[serde(default)]
    pub parts: Vec<SystemInstructionPart>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SystemInstructionPart {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Candidate {
    pub content: Content,
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HarmCategory {
    HarmCategorySexuallyExplicit,
    HarmCategoryHateSpeech,
    HarmCategoryHarassment,
    HarmCategoryDangerousContent,
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HarmProbability {
    HarmProbabilityUnspecified,
    Negligible,
    Low,
    Medium,
    High,
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HarmBlockThreshold {
    BlockNone,
    BlockLowAndAbove,
    BlockMedAndAbove,
    BlockHighAndAbove,
}

#[cfg(test)]
mod tests {
    use tokio::sync::oneshot;

    use super::*;

    #[ignore]
    #[tokio::test]
    async fn test_gemini_call() {
        dotenvy::dotenv().ok();
        let key = std::env::var("REVISE_GEMINI_KEY").unwrap();
        let gemini = Gemini::new(&key);

        let (tx, mut rx) = oneshot::channel();

        let task1 = tokio::spawn(async move {
            let spinner = ['|', '/', '-', '\\'];
            let mut idx = 0;
            loop {
                tokio::select! {
                    () = tokio::time::sleep(std::time::Duration::from_millis(100)) => {
                        print!("\rGenerating... {}", spinner[idx]);
                        std::io::Write::flush(&mut std::io::stdout()).unwrap(); // 确保立即打印字符
                        std::thread::sleep(std::time::Duration::from_millis(300));
                        idx = (idx + 1) % spinner.len();
                    }
                    _ = &mut rx => {
                        break;
                    }
                }
            }
        });

        let task2 = gemini.call("翻译: 这是一个测试");

        let result = task2.await.unwrap();

        let _ = tx.send(());

        let _ = task1.await;

        eprintln!("{result:#?}");
    }
}