Replace case statement in Claude Message.from_hash with dynamic class resolution

Aperta
#911 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@juniper-shopify ci sta già lavorando.

Dal 2/6/2026.

  • #913 di @juniper-shopify — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

enhancement

Summary

Message.from_hash in lib/roast/cogs/agent/providers/claude/message.rb uses an 8-branch case statement to map a :type symbol to a Messages::*Message class. Since every branch follows the exact same naming convention (:"#{type}_message".to_s.camelize), we can replace the static dispatch with dynamic constant resolution.

Current code (lines 23–44)

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:)
  else
    Messages::UnknownMessage.new(type:, hash:)
  end
end

Proposed replacement

def from_hash(hash)
  type = hash.delete(:type)&.to_sym
  class_name = :"#{type}_message".to_s.camelize  # e.g. :tool_use → "ToolUseMessage"
  klass = if Messages.const_defined?(class_name, false)
    Messages.const_get(class_name, false)
  else
    Messages::UnknownMessage
  end
  klass.new(type:, hash:)
end

The false argument to const_defined?/const_get restricts the lookup to Messages itself (no ancestor walk), which is the safest approach.

Why

  • Open/Closed Principle: Adding a new message type (e.g. :image) only requires creating Messages::ImageMessage — no need to touch the factory method.
  • Precedent: This mirrors how Workflow#load_cog_or_provider already uses camelize/constantize for dynamic class resolution (see lib/roast/workflow.rb:116-119).
  • Less surface area: Eliminates 16 lines of mechanical case branches that are easy to forget when adding new types.

Edge cases to verify

  • type is nil → "_message".camelize → "Message" — Messages.const_defined?("Message", false) returns false, so we correctly fall through to UnknownMessage.
  • Unknown type like :foo → "FooMessage" — not defined in Messages, falls through to UnknownMessage. ✓
  • Existing types all resolve correctly (confirmed: the naming convention holds for all 8 current types). ✓

Acceptance criteria

  • Replace the case statement with dynamic resolution
  • All existing tests in test/roast/cogs/agent/providers/claude/messages/ continue to pass
  • Add a test confirming that an unknown type falls back to UnknownMessage
Lingua principale
Ruby
Stelle
1.3k
Fork
80
Merge medio
21m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Shopify/roast

Tutte le issue di Shopify/roast

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.