Class: Roast::Workflow::BaseWorkflow

Inherits:
Object
  • Object
show all
Includes:
Raix::ChatCompletion
Defined in:
lib/roast/workflow/base_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, name: nil, context_path: nil, resource: nil, session_name: nil, workflow_configuration: nil, pre_processing_data: nil) ⇒ BaseWorkflow

Returns a new instance of BaseWorkflow.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roast/workflow/base_workflow.rb', line 30

def initialize(file = nil, name: nil, context_path: nil, resource: nil, session_name: nil, workflow_configuration: nil, pre_processing_data: nil)
  @file = file
  @name = name || self.class.name.underscore.split("/").last
  @context_path = context_path || ContextPathResolver.resolve(self.class)
  @resource = resource || Roast::Resources.for(file)
  @session_name = session_name || @name
  @session_timestamp = nil
  @workflow_configuration = workflow_configuration
  @pre_processing_data = pre_processing_data ? DotAccessHash.new(pre_processing_data).freeze : nil

  # Initialize managers
  @output_manager = OutputManager.new
  @metadata_manager = MetadataManager.new
  @context_manager = ContextManager.new
  @context_management_config = {}

  # Setup prompt and handlers
  read_sidecar_prompt.then do |prompt|
    next unless prompt

    transcript << { system: prompt }
  end
  Roast::Tools.setup_interrupt_handler(transcript)
  Roast::Tools.setup_exit_handler(self)
end

Instance Attribute Details

#conciseObject

Returns the value of attribute concise.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def concise
  @concise
end

#context_management_configObject

Returns the value of attribute context_management_config.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def context_management_config
  @context_management_config
end

#context_managerObject (readonly)

Returns the value of attribute context_manager.



23
24
25
# File 'lib/roast/workflow/base_workflow.rb', line 23

def context_manager
  @context_manager
end

#context_pathObject

Returns the value of attribute context_path.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def context_path
  @context_path
end

#fileObject

Returns the value of attribute file.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def file
  @file
end

#metadata_managerObject (readonly)

Expose output and metadata managers for state management



131
132
133
# File 'lib/roast/workflow/base_workflow.rb', line 131

def 
  @metadata_manager
end

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def model
  @model
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def name
  @name
end

#output_fileObject

Returns the value of attribute output_file.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def output_file
  @output_file
end

#output_managerObject (readonly)

Expose output and metadata managers for state management



131
132
133
# File 'lib/roast/workflow/base_workflow.rb', line 131

def output_manager
  @output_manager
end

#pause_step_nameObject

Returns the value of attribute pause_step_name.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def pause_step_name
  @pause_step_name
end

#pre_processing_dataObject (readonly)

Returns the value of attribute pre_processing_data.



23
24
25
# File 'lib/roast/workflow/base_workflow.rb', line 23

def pre_processing_data
  @pre_processing_data
end

#resourceObject

Returns the value of attribute resource.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def resource
  @resource
end

#session_nameObject

Returns the value of attribute session_name.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def session_name
  @session_name
end

#session_timestampObject

Returns the value of attribute session_timestamp.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def session_timestamp
  @session_timestamp
end

#storage_typeObject

Returns the value of attribute storage_type.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def storage_type
  @storage_type
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def verbose
  @verbose
end

#workflow_configurationObject

Returns the value of attribute workflow_configuration.



8
9
10
# File 'lib/roast/workflow/base_workflow.rb', line 8

def workflow_configuration
  @workflow_configuration
end

Instance Method Details

#chat_completion(**kwargs) ⇒ Object

Override chat_completion to add instrumentation



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roast/workflow/base_workflow.rb', line 57

def chat_completion(**kwargs)
  start_time = Time.now
  step_model = kwargs[:model]

  with_model(step_model) do
    # Configure context manager if needed
    if @context_management_config.any?
      @context_manager.configure(@context_management_config)
    end

    # Track token usage before API call
    messages = kwargs[:messages] || transcript.flatten.compact
    if @context_management_config[:enabled]
      @context_manager.track_usage(messages)
      @context_manager.check_warnings
    end

    ActiveSupport::Notifications.instrument("roast.chat_completion.start", {
      model: model,
      parameters: kwargs.except(:openai, :model),
    })

    # Clear any previous response
    Thread.current[:chat_completion_response] = nil

    # Call the parent module's chat_completion
    # skip model because it is read directly from the model method
    result = super(**kwargs.except(:model))
    execution_time = Time.now - start_time

    # Extract token usage from the raw response stored by Raix
    raw_response = Thread.current[:chat_completion_response]
    token_usage = extract_token_usage(raw_response) if raw_response

    # Update context manager with actual token usage if available
    if token_usage && @context_management_config[:enabled]
      actual_total = token_usage.dig("total_tokens") || token_usage.dig(:total_tokens)
      @context_manager.update_with_actual_usage(actual_total) if actual_total
    end

    ActiveSupport::Notifications.instrument("roast.chat_completion.complete", {
      success: true,
      model: model,
      parameters: kwargs.except(:openai, :model),
      execution_time: execution_time,
      response_size: result.to_s.length,
      token_usage: token_usage,
    })
    result
  end
rescue Faraday::ResourceNotFound => e
  execution_time = Time.now - start_time
  message = e.response.dig(:body, "error", "message") || e.message
  error = Roast::Errors::ResourceNotFoundError.new(message)
  error.set_backtrace(e.backtrace)
  log_and_raise_error(error, message, step_model || model, kwargs, execution_time)
rescue => e
  execution_time = Time.now - start_time
  log_and_raise_error(e, e.message, step_model || model, kwargs, execution_time)
end

#with_model(model) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/roast/workflow/base_workflow.rb', line 118

def with_model(model)
  previous_model = @model
  @model = model
  yield
ensure
  @model = previous_model
end

#workflowObject



126
127
128
# File 'lib/roast/workflow/base_workflow.rb', line 126

def workflow
  self
end