Class: Roast::Cogs::Agent::Providers::Claude::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/cogs/agent/providers/claude/message.rb

Constant Summary collapse

IGNORED_FIELDS =
[
  :uuid,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, hash:) ⇒ Message

: (type: Symbol, hash: Hash[Symbol, untyped]) -> void



61
62
63
64
65
66
67
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 61

def initialize(type:, hash:)
  @session_id = hash.delete(:session_id)
  @type = type
  @error = hash.delete(:error)
  hash.except!(*IGNORED_FIELDS)
  @unparsed = hash
end

Instance Attribute Details

#errorObject (readonly)

: String?



55
56
57
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 55

def error
  @error
end

#session_idObject (readonly)

: String?



49
50
51
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 49

def session_id
  @session_id
end

#typeObject (readonly)

: Symbol



52
53
54
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 52

def type
  @type
end

#unparsedObject (readonly)

: Hash[Symbol, untyped]



58
59
60
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 58

def unparsed
  @unparsed
end

Class Method Details

.from_hash(hash) ⇒ Object

: (Hash[Symbol, untyped]) -> Message



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 23

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

.from_json(json, raw_dump_file: nil) ⇒ Object

: (String, ?raw_dump_file: Pathname?) -> Message



16
17
18
19
20
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 16

def from_json(json, raw_dump_file: nil)
  raw_dump_file&.dirname&.mkpath
  File.write("./tmp/claude-messages.log", "#{json}\n", mode: "a") if raw_dump_file
  from_hash(JSON.parse(json, symbolize_names: true))
end

Instance Method Details

#format(context) ⇒ Object

: (ClaudeInvocation::Context) -> String?



70
71
# File 'lib/roast/cogs/agent/providers/claude/message.rb', line 70

def format(context)
end