dorian-commit 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/commit +44 -14
  3. metadata +19 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb7eea33291b322da0856645ec3841fc51490ca72d821c48bbf1a29aea1d0e4e
4
- data.tar.gz: 047d11c7de11e00d5dd654b9d18bd660bbdc684c29e8bd3cdc7fc6abdfdac953
3
+ metadata.gz: 96ab8fd00e486fb48cb313023efd6ecf14763427593916b018304628f468eb05
4
+ data.tar.gz: 3bae8a19f1e528450a06600b08a7b4e6e63ad9e80dd742b79984edde5a08f1f2
5
5
  SHA512:
6
- metadata.gz: a9d82f215a19897a242e50f2ea72347e6bdc623e6a9ad7b324678d2896881f87ef62792111df7453b324cf65394eec170e3327e6bf0d56eaa6e6e975364e9f84
7
- data.tar.gz: 9cdf35126c7b589ab989bf23aa20367ef31c928bfdfc2c24e69286c6953d563ae152f06fd409a22962ac6b9fa26355537de1e71b4d7e2aef3fce1397d89a9600
6
+ metadata.gz: be08309f81e4dfa4100f8186e7de3a3cd4e6b95dbb2dab6b17f434918f92c8e73e30b2830ffaaa9d84c87318c4beaa181a2004bb92f7f3e1a65991646a62c901
7
+ data.tar.gz: 7eca41f69e2d35094437282a05dcd0c26b64c0a8ddf35f23e71f554765c4e7ea9fce849396bfe27c93b218915e120d220892e50f7fec9f9f63e1d2fcc5906a59
data/bin/commit CHANGED
@@ -5,6 +5,7 @@ require "dorian/arguments"
5
5
  require "git"
6
6
  require "json"
7
7
  require "net/http"
8
+ require "tiktoken_ruby"
8
9
  require "uri"
9
10
 
10
11
  TOKEN_FILE = File.join(Dir.home, ".commit")
@@ -23,12 +24,25 @@ PROMPT_2 = "for the following diff:"
23
24
  PROMPT_3 = "for the following git status:"
24
25
  PROMPT_4 = "for the following comment:"
25
26
 
26
- parsed = Dorian::Arguments.parse(version: { alias: :v }, help: { alias: :h })
27
+ parsed =
28
+ Dorian::Arguments.parse(
29
+ tokens: {
30
+ type: :integer,
31
+ alias: :t,
32
+ default: 1000
33
+ },
34
+ version: {
35
+ alias: :v
36
+ },
37
+ help: {
38
+ alias: :h
39
+ }
40
+ )
27
41
 
28
42
  abort parsed.help if parsed.options.help
29
43
 
30
44
  if parsed.options.version
31
- abort File.read(File.expand_path("../../VERSION", __FILE__))
45
+ abort File.read(File.expand_path("../VERSION", __dir__))
32
46
  end
33
47
 
34
48
  content_2 = `git diff --staged`
@@ -38,14 +52,32 @@ content_4 += parsed.files.map { |file| File.read(file) }.join("\n")
38
52
 
39
53
  abort "no staged files" if content_2.empty?
40
54
 
41
- if content_4.empty?
42
- messages_4 = []
43
- else
44
- messages_4 = [
45
- { role: :system, content: PROMPT_4 },
46
- { role: :user, content: content_4 }
47
- ]
48
- end
55
+ encoder = Tiktoken.encoding_for_model("gpt-4o")
56
+
57
+ content_2_encoded = encoder.encode(content_2)
58
+
59
+ messages_2 =
60
+ if content_2_encoded.length > parsed.options.tokens
61
+ []
62
+ else
63
+ [{ role: :system, content: PROMPT_2 }, { role: :user, content: content_2 }]
64
+ end
65
+
66
+ content_3_encoded = encoder.encode(content_3)
67
+
68
+ messages_3 =
69
+ if content_3_encoded.length > parsed.options.tokens
70
+ []
71
+ else
72
+ [{ role: :system, content: PROMPT_3 }, { role: :user, content: content_3 }]
73
+ end
74
+
75
+ messages_4 =
76
+ if content_4.empty?
77
+ []
78
+ else
79
+ [{ role: :system, content: PROMPT_4 }, { role: :user, content: content_4 }]
80
+ end
49
81
 
50
82
  uri = URI.parse("https://api.openai.com/v1/chat/completions")
51
83
 
@@ -65,10 +97,8 @@ request.body = {
65
97
  model: "gpt-4o",
66
98
  messages: [
67
99
  { role: :system, content: PROMPT_1 },
68
- { role: :system, content: PROMPT_2 },
69
- { role: :user, content: content_2 },
70
- { role: :system, content: PROMPT_3 },
71
- { role: :user, content: content_3 },
100
+ *messages_2,
101
+ *messages_3,
72
102
  *messages_4
73
103
  ]
74
104
  }.to_json
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
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-24 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dorian-arguments
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: git
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -25,7 +39,7 @@ dependencies:
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
- name: dorian-arguments
42
+ name: tiktoken_ruby
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
@@ -57,9 +71,9 @@ require_paths:
57
71
  - lib
58
72
  required_ruby_version: !ruby/object:Gem::Requirement
59
73
  requirements:
60
- - - ">="
74
+ - - '='
61
75
  - !ruby/object:Gem::Version
62
- version: '0'
76
+ version: 3.3.4
63
77
  required_rubygems_version: !ruby/object:Gem::Requirement
64
78
  requirements:
65
79
  - - ">="