Class: SmartPrompt::WorkerContext

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_prompt/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(conversation, params, engine, proc = nil) ⇒ WorkerContext

Returns a new instance of WorkerContext.



52
53
54
55
56
57
# File 'lib/smart_prompt/worker.rb', line 52

def initialize(conversation, params, engine, proc = nil)
  @conversation = conversation
  @params = params
  @engine = engine
  @proc = proc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smart_prompt/worker.rb', line 59

def method_missing(method, *args, &block)
  if @conversation.respond_to?(method)
    if method == :send_msg
      if @proc == nil
        @conversation.send_msg(params)
      else
        @conversation.send_msg_by_stream(params, &@proc)
      end
    elsif method == :sys_msg
      @conversation.sys_msg(*args)
    elsif method == :prompt
      @conversation.prompt(*args, with_history: params[:with_history])
    else
      @conversation.send(method, *args, &block)
    end
  else
    super
  end
end

Instance Method Details

#call_worker(worker_name, params = {}) ⇒ Object



98
99
100
101
# File 'lib/smart_prompt/worker.rb', line 98

def call_worker(worker_name, params = {})
  worker = Worker.new(worker_name, @engine)
  worker.execute(params)
end

#call_worker_by_stream(worker_name, params = {}, proc) ⇒ Object



103
104
105
106
# File 'lib/smart_prompt/worker.rb', line 103

def call_worker_by_stream(worker_name, params = {}, proc)
  worker = Worker.new(worker_name, @engine)
  worker.execute_by_stream(params, proc)
end

#engineObject

Expose the engine so workers can reach a configured adapter directly (e.g. ‘engine.llms`) for methods Conversation doesn’t delegate, such as generate_video / synthesize_to_file / transcribe_audio.



94
95
96
# File 'lib/smart_prompt/worker.rb', line 94

def engine
  @engine
end

#paramsObject



83
84
85
# File 'lib/smart_prompt/worker.rb', line 83

def params
  @params
end

#procObject



87
88
89
# File 'lib/smart_prompt/worker.rb', line 87

def proc
  @proc
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/smart_prompt/worker.rb', line 79

def respond_to_missing?(method, include_private = false)
  @conversation.respond_to?(method) || super
end