dorian-commit 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/commit +54 -17
  3. metadata +18 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1755970ac251ca93f075f8377be124343de1bd927127e8dd98739527d0dec435
4
- data.tar.gz: 55f49464c33b2e61c9c81e5f30c3a1354fa34a0168f4edb050d1aa4d07a04e54
3
+ metadata.gz: 8277450427f579a722a664da512e6cee4b3a31fa00699c6506a6b5ec53271115
4
+ data.tar.gz: '08f33e3dd2936e5c881e5cb21ffcf7699104b5a68cbbce8b1c9940a251bba861'
5
5
  SHA512:
6
- metadata.gz: caec97a14207833cc2bb3958a8c3d96a58ea8428b933da382f807d64db15d46b11a26374597a94370082d6125428c86a4e980b5161d419c6484e5d7708c2d52e
7
- data.tar.gz: 4d151d4dc995c2affd1c7cef5b755815d3d88c751334c14037555e71835685eb699cb4d68cb586195b1871c9b65486c7922a4e4ad602aafbd8f855d3e721fdc7
6
+ metadata.gz: f30d67a1bb421eab3e523d8bc58dba9691ec53cbcf0420b470acf28dc17cc05b2edef192e45510cf96ae524fd830c67833aa3530f521371f28c1f734ce3c55aa
7
+ data.tar.gz: c84cf08482ef6e85183f1063933f9a4c2c38a98678bde8b736f8185dd9bde4494d28c2a3776f9789846aa94006b49b9b983a57d1ebd9c35a11b1f6830e63b59d
data/bin/commit CHANGED
@@ -1,15 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require "dorian/arguments"
5
+ require "git"
4
6
  require "json"
5
7
  require "net/http"
6
8
  require "uri"
7
- require "git"
8
-
9
- abort "USAGE: commit" if ARGV.any?
10
9
 
11
10
  TOKEN_FILE = File.join(Dir.home, ".commit")
12
- PROMPT = "simple, clear, short, lowercase commit message for the following diff:"
13
11
 
14
12
  if File.exist?(TOKEN_FILE)
15
13
  TOKEN = File.read(TOKEN_FILE).strip
@@ -20,29 +18,68 @@ else
20
18
  puts "token written to #{TOKEN_FILE}"
21
19
  end
22
20
 
23
- STAGED = `git diff --staged`
21
+ PROMPT_1 = "simple, clear, short, lowercase commit message"
22
+ PROMPT_2 = "for the following diff:"
23
+ PROMPT_3 = "for the following git status:"
24
+ PROMPT_4 = "for the following comment:"
25
+
26
+ parsed = Dorian::Arguments.parse(version: { alias: :v }, help: { alias: :h })
27
+
28
+ abort parsed.help if parsed.options.help
29
+
30
+ if parsed.options.version
31
+ abort File.read(File.expand_path("../../VERSION", __FILE__))
32
+ end
33
+
34
+ content_2 = `git diff --staged`
35
+ content_3 = `git status`
36
+ content_4 = parsed.arguments.join(" ")
37
+ content_4 += parsed.files.map { |file| File.read(file) }.join("\n")
24
38
 
25
- abort "no staged files" if STAGED.strip.empty?
39
+ if content_4.empty?
40
+ messages_4 = []
41
+ else
42
+ messages_4 = [
43
+ { role: :system, content: PROMPT_4 },
44
+ { role: :user, content: content_4 }
45
+ ]
46
+ end
47
+
48
+ uri = URI.parse("https://api.openai.com/v1/chat/completions")
26
49
 
27
- uri = URI("https://api.openai.com/v1/chat/completions")
28
50
  http = Net::HTTP.new(uri.host, uri.port)
29
51
  http.use_ssl = true
30
52
 
31
- request = Net::HTTP::Post.new(uri.path, {
32
- "Content-Type" => "application/json",
33
- "Authorization" => "Bearer #{TOKEN}"
34
- })
53
+ request =
54
+ Net::HTTP::Post.new(
55
+ uri.path,
56
+ {
57
+ "Content-Type" => "application/json",
58
+ "Authorization" => "Bearer #{TOKEN}"
59
+ }
60
+ )
35
61
 
36
62
  request.body = {
37
63
  model: "gpt-4o",
38
64
  messages: [
39
- { role: "system", content: PROMPT },
40
- { role: "user", content: STAGED }
41
- ],
65
+ { role: :system, content: PROMPT_1 },
66
+ { role: :system, content: PROMPT_2 },
67
+ { role: :user, content: content_2 },
68
+ { role: :system, content: PROMPT_3 },
69
+ { role: :user, content: content_3 },
70
+ *messages_4
71
+ ]
42
72
  }.to_json
43
73
 
44
74
  response = http.request(request)
75
+
45
76
  json = JSON.parse(response.body)
46
- message = json.dig('choices', 0, 'message', 'content').strip
47
- Git.open(".").commit(message)
48
- puts message
77
+
78
+ message = json.dig("choices", 0, "message", "content")
79
+
80
+ if message
81
+ Git.open(".").commit(message)
82
+ puts message
83
+ else
84
+ puts JSON.pretty_generate(json)
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-16 00:00:00.000000000 Z
11
+ date: 2024-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -24,10 +24,21 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: |-
28
- commit with openai api
29
-
30
- e.g. `commit`
27
+ - !ruby/object:Gem::Dependency
28
+ name: dorian-arguments
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: commit with ai
31
42
  email: dorian@dorianmarie.com
32
43
  executables:
33
44
  - commit
@@ -58,5 +69,5 @@ requirements: []
58
69
  rubygems_version: 3.5.11
59
70
  signing_key:
60
71
  specification_version: 4
61
- summary: commit with openai api
72
+ summary: commit with ai
62
73
  test_files: []