Class: Desiru::Module

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AsyncCapable, Core::Traceable, ErrorHandling
Defined in:
lib/desiru/errors.rb,
lib/desiru.rb,
lib/desiru/module.rb

Overview

Base class for all Desiru modules Implements the core module pattern with service-oriented design

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AsyncCapable

#call_async, #call_batch_async

Methods included from ErrorHandling

#safe_execute, #with_error_context, #with_retry

Methods included from Core::Traceable

#disable_trace!, #enable_trace!, #trace_enabled?

Constructor Details

#initialize(signature, model: nil, config: {}, demos: [], metadata: {}) ⇒ Module

Returns a new instance of Module.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/desiru/module.rb', line 17

def initialize(signature, model: nil, config: {}, demos: [], metadata: {})
  @signature = case signature
               when Signature
                 signature
               when String
                 Signature.new(signature)
               else
                 raise ModuleError, 'Signature must be a String or Signature instance'
               end

  @model = model || Desiru.configuration.default_model
  @config = default_config.merge(config)
  @demos = demos
  @metadata = 
  @call_count = 0

  # Raise error if no model available
  raise ArgumentError, 'No model provided and no default model configured' if @model.nil?

  validate_model!
  register_module
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/desiru/module.rb', line 15

def config
  @config
end

#demosObject (readonly)

Returns the value of attribute demos.



15
16
17
# File 'lib/desiru/module.rb', line 15

def demos
  @demos
end

#metadataObject (readonly)

Returns the value of attribute metadata.



15
16
17
# File 'lib/desiru/module.rb', line 15

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



15
16
17
# File 'lib/desiru/module.rb', line 15

def model
  @model
end

#signatureObject (readonly)

Returns the value of attribute signature.



15
16
17
# File 'lib/desiru/module.rb', line 15

def signature
  @signature
end

Instance Method Details

#call(inputs = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/desiru/module.rb', line 40

def call(inputs = {})
  @call_count += 1
  @retry_count = 0

  begin
    # Validate inputs first, then coerce
    signature.valid_inputs?(inputs)
    coerced_inputs = signature.coerce_inputs(inputs)

    # Execute the module logic
    result = forward(**coerced_inputs)

    # Validate outputs first, then coerce
    signature.valid_outputs?(result)
    coerced_outputs = signature.coerce_outputs(result)

    # Return result object
    ModuleResult.new(coerced_outputs, metadata: )
  rescue StandardError => e
    if should_retry?(e)
      @retry_count += 1
      log_retry(e)
      sleep(retry_delay_for(e))
      retry
    else
      handle_error(e)
    end
  end
end

#forward(_inputs) ⇒ Object

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/desiru/module.rb', line 70

def forward(_inputs)
  raise NotImplementedError, 'Subclasses must implement #forward'
end

#resetObject



74
75
76
77
# File 'lib/desiru/module.rb', line 74

def reset
  @demos = []
  @call_count = 0
end

#to_hObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/desiru/module.rb', line 83

def to_h
  {
    class: self.class.name,
    signature: signature.to_h,
    config: config,
    demos_count: demos.size,
    call_count: @call_count,
    metadata: 
  }
end

#with_demos(new_demos) ⇒ Object



79
80
81
# File 'lib/desiru/module.rb', line 79

def with_demos(new_demos)
  self.class.new(signature, model: model, config: config, demos: new_demos, metadata: )
end