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.



55
56
57
58
59
60
61
# File 'lib/smart_prompt/worker.rb', line 55

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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
# File 'lib/smart_prompt/worker.rb', line 72

def method_missing(method, *args, &block)
  if @conversation.respond_to?(method)
    if method == :send_msg
      send_params = params
      if params[:with_history] && !@transient_messages.empty?
        # The default send path would send *only* history_messages when
        # with_history=true, silently dropping the transient prompt just
        # added to @conversation.messages. Merge both sources for this
        # request and use the ordinary send path. When there is no transient
        # prompt, keep the original with_history send so the persisted
        # prompt pattern is not duplicated.
        prepare_transient_history_request!
        send_params = params.merge(with_history: false)
      end
      if @proc.nil?
        @conversation.send_msg(send_params)
      else
        @conversation.send_msg_by_stream(send_params, &@proc)
      end
    elsif method == :sys_msg
      # The system message always belongs to the current request. Its
      # durable session copy is upserted by Conversation#sys_msg so a worker
      # loop never accumulates one preserved system message per round.
      @conversation.sys_msg(*args, with_history: params[:with_history])
    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



125
126
127
128
# File 'lib/smart_prompt/worker.rb', line 125

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



130
131
132
133
# File 'lib/smart_prompt/worker.rb', line 130

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

#engine ⇒ Object

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.



121
122
123
# File 'lib/smart_prompt/worker.rb', line 121

def engine
  @engine
end

#params ⇒ Object



110
111
112
# File 'lib/smart_prompt/worker.rb', line 110

def params
  @params
end

#proc ⇒ Object



114
115
116
# File 'lib/smart_prompt/worker.rb', line 114

def proc
  @proc
end

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/smart_prompt/worker.rb', line 106

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

#transient_prompt(content) ⇒ Object

Add a user prompt to the current request only, without persisting it to HistoryManager. Progress, retry notes and hard-intervention text are snapshots, not conversation history; persisting one large user message per round would evict the assistant/tool evidence we need to retain.



67
68
69
70
# File 'lib/smart_prompt/worker.rb', line 67

def transient_prompt(content)
  @transient_messages << content
  @conversation.prompt(content, with_history: false)
end