Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.1] - 2026-10-19

### Fixed

- **`pp` of an Array of messages**: `pp chat.messages` (and IRB's echo of `chat.messages`) printed `},{` and `}{` between messages and piled the leftover separators up before the closing bracket (`} , ]`). Messages now print as `[{...}, {...}]`, matching `p`. `AI::Items` had the same problem inside an Array and gets the same fix.

## [0.8.0] - 2026-10-19

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ai-chat (0.8.0)
ai-chat (0.8.1)
amazing_print (~> 2.0)
base64 (~> 0.1, > 0.1.1)
json (~> 2.0)
Expand Down
2 changes: 1 addition & 1 deletion ai-chat.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "ai-chat"
spec.version = "0.8.0"
spec.version = "0.8.1"
spec.authors = ["Raghu Betina", "Jelani Woods"]
spec.email = ["[email protected]", "[email protected]"]
spec.homepage = "https://github.com/firstdraft/ai-chat"
Expand Down
2 changes: 2 additions & 0 deletions lib/ai/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def pretty_inspect
"#{inspect}\n"
end

# Same direct write as AI::Message#pretty_print, for the same reasons.
def pretty_print(q)
q.flush
q.output << inspect
end

Expand Down
5 changes: 4 additions & 1 deletion lib/ai/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ def pretty_inspect
end

# IRB's ColorPrinter calls pretty_print and re-colorizes text,
# which escapes our ANSI codes. Write directly to output to bypass.
# which escapes our ANSI codes. Write directly to output to bypass,
# but flush first: inside an Array, PP is still buffering the "[" and
# earlier separators, so an unflushed write lands ahead of them.
def pretty_print(q)
q.flush
q.output << inspect
end

Expand Down
45 changes: 45 additions & 0 deletions spec/unit/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,51 @@ def schema_client_double
it "returns a String" do
expect(chat.inspect).to be_a(String)
end

# Lessons show this output verbatim, so pin the exact layout.
context "when not writing to a terminal" do
before { allow($stdout).to receive(:tty?).and_return(false) }

it "lists a new chat's attributes one per line" do
new_chat = AI::Chat.new(api_key: "test-key", proxy: false)

expect(new_chat.inspect).to eq(<<~TEXT.chomp)
#<AI::Chat
@model: "gpt-6-luna"
@reasoning_effort: "low"
@conversation_id: nil
@messages: []
@verbosity: :medium
>
TEXT
end

it "lists messages by index with aligned keys" do
proxied_chat = AI::Chat.new(api_key: "test-key", proxy: true)
proxied_chat.system("You are an assistant that talks like Shakespeare.")
proxied_chat.user("What is the motto of Ruby lang?")

expect(proxied_chat.inspect).to eq(<<~TEXT.chomp)
#<AI::Chat
@model: "gpt-6-luna"
@reasoning_effort: "low"
@conversation_id: nil
@messages: [
[0] {
role: "system",
content: "You are an assistant that talks like Shakespeare."
},
[1] {
role: "user",
content: "What is the motto of Ruby lang?"
}
]
@proxy: true
@verbosity: :medium
>
TEXT
end
end
end

describe "#pretty_inspect" do
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/items_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
expect(out).to include("Conversation: conv_abc123")
expect(out).to include("Items: 2")
end

it "keeps separators in place inside an Array" do
out = PP.pp([items, items], +"")

expect(out).to eq("[#{items.inspect}, #{items.inspect}]\n")
end
end

describe "#to_html" do
Expand Down
26 changes: 25 additions & 1 deletion spec/unit/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,35 @@
end
end

describe "printing an Array of messages with pp" do
before { allow($stdout).to receive(:tty?).and_return(false) }

it "keeps each separator between the messages it separates" do
messages = [
AI::Message[role: "user", content: "Hello"],
AI::Message[role: "assistant", content: "Hi!", response: {id: "resp_1"}]
]

expect(PP.pp(messages, +"")).to eq(<<~TEXT)
[{
role: "user",
content: "Hello"
}, {
role: "assistant",
content: "Hi!",
response: {
id: "resp_1"
}
}]
TEXT
end
end

describe "#pretty_print" do
it "writes directly to output to bypass IRB colorization" do
message = AI::Message[role: "user", content: "Hello"]
output = StringIO.new
mock_q = double("PrettyPrint", output: output)
mock_q = double("PrettyPrint", output: output, flush: nil)

message.pretty_print(mock_q)

Expand Down
Loading