ai-chat 0.6.0 → 0.6.1

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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +304 -672
  3. data/ai-chat.gemspec +2 -2
  4. data/lib/ai/chat.rb +31 -4
  5. metadata +3 -3
data/ai-chat.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "ai-chat"
5
- spec.version = "0.6.0"
5
+ spec.version = "0.6.1"
6
6
  spec.authors = ["Raghu Betina", "Jelani Woods"]
7
7
  spec.email = ["raghu@firstdraft.com", "jelani@firstdraft.com"]
8
8
  spec.homepage = "https://github.com/firstdraft/ai-chat"
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  }
20
20
 
21
21
  spec.required_ruby_version = ">= 3.2"
22
- spec.add_runtime_dependency "openai", "~> 0.43"
22
+ spec.add_runtime_dependency "openai", "~> 0.59"
23
23
  spec.add_runtime_dependency "marcel", "~> 1.0"
24
24
  spec.add_runtime_dependency "base64", "~> 0.1", "> 0.1.1"
25
25
  spec.add_runtime_dependency "json", "~> 2.0"
data/lib/ai/chat.rb CHANGED
@@ -18,8 +18,8 @@ module AI
18
18
  # :reek:IrresponsibleModule
19
19
  class Chat
20
20
  # :reek:Attribute
21
- attr_accessor :background, :code_interpreter, :conversation_id, :image_generation, :image_folder, :messages, :model, :reasoning_effort, :web_search
22
- attr_reader :client, :last_response_id, :proxy, :schema, :schema_file, :verbosity
21
+ attr_accessor :background, :code_interpreter, :conversation_id, :image_folder, :messages, :model, :reasoning_effort, :web_search
22
+ attr_reader :client, :image_generation, :last_response_id, :proxy, :schema, :schema_file, :verbosity
23
23
 
24
24
  BASE_PROXY_URL = "https://prepend.me/api.openai.com/v1"
25
25
  PROXY_ENV = "AICHAT_PROXY"
@@ -205,6 +205,19 @@ module AI
205
205
  end
206
206
  end
207
207
 
208
+ def image_generation=(value)
209
+ case value
210
+ when true
211
+ @image_generation = true
212
+ when false, nil
213
+ @image_generation = false
214
+ when Hash
215
+ @image_generation = value.transform_keys(&:to_sym)
216
+ else
217
+ raise ArgumentError, "Invalid image_generation value: #{value.inspect}. Must be true, false, or a Hash of tool options (e.g., { size: \"1536x1024\", quality: \"low\" })."
218
+ end
219
+ end
220
+
208
221
  def last
209
222
  messages.last
210
223
  end
@@ -523,7 +536,8 @@ module AI
523
536
  tools_list << {type: "web_search"}
524
537
  end
525
538
  if image_generation
526
- tools_list << {type: "image_generation"}
539
+ options = image_generation.is_a?(Hash) ? image_generation : {}
540
+ tools_list << options.merge(type: "image_generation")
527
541
  end
528
542
  if code_interpreter
529
543
  tools_list << {type: "code_interpreter", container: {type: "auto"}}
@@ -577,7 +591,8 @@ module AI
577
591
  result = output.result
578
592
  image_data = Base64.strict_decode64(result)
579
593
 
580
- filename = "#{(index + 1).to_s.rjust(3, "0")}.png"
594
+ extension = image_extension_for(image_data)
595
+ filename = "#{(index + 1).to_s.rjust(3, "0")}.#{extension}"
581
596
  file_path = File.join(subfolder_path, filename)
582
597
 
583
598
  File.binwrite(file_path, image_data)
@@ -589,6 +604,18 @@ module AI
589
604
  image_filenames
590
605
  end
591
606
 
607
+ IMAGE_EXTENSIONS_BY_MIME_TYPE = {
608
+ "image/png" => "png",
609
+ "image/jpeg" => "jpg",
610
+ "image/webp" => "webp"
611
+ }.freeze
612
+
613
+ # :reek:UtilityFunction
614
+ def image_extension_for(bytes)
615
+ mime_type = Marcel::MimeType.for(StringIO.new(bytes))
616
+ IMAGE_EXTENSIONS_BY_MIME_TYPE.fetch(mime_type, "png")
617
+ end
618
+
592
619
  def create_images_folder(response_id)
593
620
  # ISO 8601 basic format with centisecond precision
594
621
  timestamp = Time.now.strftime("%Y%m%dT%H%M%S%2N")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raghu Betina
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.43'
19
+ version: '0.59'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.43'
26
+ version: '0.59'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: marcel
29
29
  requirement: !ruby/object:Gem::Requirement