Class: PromptBuilder::Items::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_builder/items/base.rb

Overview

Base class for conversation items. Provides polymorphic deserialization via the type field in the hash representation.

Constant Summary collapse

TYPES =

Item type registry for polymorphic dispatch.

{
  "compaction" => "Compaction",
  "function_call" => "FunctionCall",
  "function_call_output" => "FunctionCallOutput",
  "item_reference" => "ItemReference",
  "message" => "Message",
  "reasoning" => "Reasoning"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Items::Base

Deserialize an item from a Hash by dispatching on the type field.

Parameters:

  • hash (Hash)

    a Hash with string keys including a “type” field

Returns:

Raises:



24
25
26
27
28
29
30
# File 'lib/prompt_builder/items/base.rb', line 24

def from_h(hash)
  type = hash["type"]
  class_name = TYPES[type]
  raise InvalidItemError, "Unknown item type: #{type.inspect}" unless class_name

  Items.const_get(class_name).from_h(hash)
end

Instance Method Details

#to_hHash

Serialize the item to a Hash with string keys.

Returns:

  • (Hash)

    the hash representation

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/prompt_builder/items/base.rb', line 36

def to_h
  raise NotImplementedError
end