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, configuration: nil) ⇒ BaseWorkflow

Returns a new instance of BaseWorkflow.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/roast/workflow/base_workflow.rb', line 27

def initialize(file = nil, name: nil, context_path: nil, resource: nil, session_name: nil, configuration: nil)
  @file = file
  @name = name || self.class.name.underscore.split("/").last
  @context_path = context_path || determine_context_path
  @final_output = []
  @output = ActiveSupport::HashWithIndifferentAccess.new
  @resource = resource || Roast::Resources.for(file)
  @session_name = session_name || @name
  @session_timestamp = nil
  @configuration = configuration
  transcript << { system: read_sidecar_prompt }
  Roast::Tools.setup_interrupt_handler(transcript)
  Roast::Tools.setup_exit_handler(self)
end

Instance Attribute Details

#conciseObject

Returns the value of attribute concise.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def concise
  @concise
end

#configurationObject

Returns the value of attribute configuration.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def configuration
  @configuration
end

#context_pathObject

Returns the value of attribute context_path.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def context_path
  @context_path
end

#fileObject

Returns the value of attribute file.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def file
  @file
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def name
  @name
end

#outputObject

Returns the value of attribute output.



15
16
17
# File 'lib/roast/workflow/base_workflow.rb', line 15

def output
  @output
end

#output_fileObject

Returns the value of attribute output_file.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def output_file
  @output_file
end

#resourceObject

Returns the value of attribute resource.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def resource
  @resource
end

#session_nameObject

Returns the value of attribute session_name.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def session_name
  @session_name
end

#session_timestampObject

Returns the value of attribute session_timestamp.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def session_timestamp
  @session_timestamp
end

#verboseObject

Returns the value of attribute verbose.



16
17
18
# File 'lib/roast/workflow/base_workflow.rb', line 16

def verbose
  @verbose
end

Instance Method Details

#append_to_final_output(message) ⇒ Object



51
52
53
# File 'lib/roast/workflow/base_workflow.rb', line 51

def append_to_final_output(message)
  @final_output << message
end

#chat_completion(**kwargs) ⇒ Object

Override chat_completion to add instrumentation



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
# File 'lib/roast/workflow/base_workflow.rb', line 69

def chat_completion(**kwargs)
  start_time = Time.now
  model = kwargs[:openai] || "default"

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

  result = super(**kwargs)
  execution_time = Time.now - start_time

  ActiveSupport::Notifications.instrument("roast.chat_completion.complete", {
    success: true,
    model: model,
    parameters: kwargs.except(:openai),
    execution_time: execution_time,
    response_size: result.to_s.length,
  })

  result
rescue => e
  execution_time = Time.now - start_time

  ActiveSupport::Notifications.instrument("roast.chat_completion.error", {
    error: e.class.name,
    message: e.message,
    model: model,
    parameters: kwargs.except(:openai),
    execution_time: execution_time,
  })
  raise
end

#final_outputObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/roast/workflow/base_workflow.rb', line 55

def final_output
  return @final_output if @final_output.is_a?(String)
  return "" if @final_output.nil?

  # Handle array case (expected normal case)
  if @final_output.respond_to?(:join)
    @final_output.join("\n\n")
  else
    # Handle any other unexpected type by converting to string
    @final_output.to_s
  end
end