Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
PATH
remote: .
specs:
ai-chat (0.1.0)
ai-chat (0.1.1)
base64 (~> 0.1)
mime-types (~> 3.0)
openai (~> 0.14)
refinements (~> 11.1)
zeitwerk (~> 2.7)
openai (0.16.0)
connection_pool

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -169,6 +170,7 @@ DEPENDENCIES
dotenv
rake (~> 13.3)
reek (~> 6.5)
refinements (~> 11.1)
repl_type_completor (~> 0.1)
rspec (~> 3.13)
simplecov (~> 0.22)
Expand Down
29 changes: 29 additions & 0 deletions lib/ai/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ def inspect
"#<#{self.class.name} @messages=#{messages.inspect} @model=#{@model.inspect} @schema=#{@schema.inspect} @reasoning_effort=#{@reasoning_effort.inspect}>"
end

def to_hash
hash = { "#{self.class.name}" => {} }

# Define which instance variables to skip
skip_vars = [:@api_key, :@client]

instance_variables.sort.each do |var|
next if skip_vars.include?(var)

value = instance_variable_get(var)

# Special handling for @messages to truncate content
if var == :@messages
value = value.map do |msg|
truncated_msg = msg.dup
if msg[:content].is_a?(String) && msg[:content].length > 80
truncated_msg[:content] = msg[:content][0..77] + "..."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-by-one error in truncation: using [0..77] yields 78 characters, so adding '...' makes 81 characters in total. Adjust the slice (e.g., [0..76]) to meet the 80-character limit.

Suggested change
truncated_msg[:content] = msg[:content][0..77] + "..."
truncated_msg[:content] = msg[:content][0..76] + "..."

end
truncated_msg
end
end

# Skip nil values for cleaner output
hash["#{self.class.name}"][var.to_s] = value unless value.nil?
end

hash
end

private

# Custom exception class for input classification errors.
Expand Down
Loading