Skip to content
Open
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
34 changes: 16 additions & 18 deletions lib/roast/cogs/agent/providers/claude/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@ def from_json(json, raw_dump_file: nil)
#: (Hash[Symbol, untyped]) -> Message
def from_hash(hash)
type = hash.delete(:type)&.to_sym
case type
when :assistant
Messages::AssistantMessage.new(type:, hash:)
when :result
Messages::ResultMessage.new(type:, hash:)
when :system
Messages::SystemMessage.new(type:, hash:)
when :text
Messages::TextMessage.new(type:, hash:)
when :thinking
Messages::ThinkingMessage.new(type:, hash:)
when :tool_result
Messages::ToolResultMessage.new(type:, hash:)
when :tool_use
Messages::ToolUseMessage.new(type:, hash:)
when :user
Messages::UserMessage.new(type:, hash:)
message_class = resolve_message_class(type)
message_class.new(type:, hash:)
end

private

#: (Symbol?) -> singleton(Message)
def resolve_message_class(type)
return Messages::UnknownMessage if type.nil?

class_name = "#{type}_message".camelize
Comment thread
juniper-shopify marked this conversation as resolved.
if Messages.const_defined?(class_name, false)
Messages.const_get(class_name, false) # rubocop:disable Sorbet/ConstantsFromStrings
else
Messages::UnknownMessage.new(type:, hash:)
Messages::UnknownMessage
end
rescue NameError
Messages::UnknownMessage
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/roast/cogs/agent/providers/claude/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ class MessageTest < ActiveSupport::TestCase
assert_kind_of Messages::UnknownMessage, message
end

test "from_hash with invalid constant name type creates UnknownMessage" do
hash = { type: :"some-hyphenated-type", foo: "bar" }
message = Message.from_hash(hash)

assert_kind_of Messages::UnknownMessage, message
end

test "from_hash removes type from hash" do
hash = { type: :text, text: "Hello" }
Message.from_hash(hash)
Expand Down
Loading