Class: SmartPrompt::Worker

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, engine) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
11
# File 'lib/smart_prompt/worker.rb', line 5

def initialize(name, engine)
  SmartPrompt.logger.info "Create worker's name is #{name}"
  @name = name
  @engine = engine
  @config = engine.config
  @code = self.class.workers[name]
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



3
4
5
# File 'lib/smart_prompt/worker.rb', line 3

def config_file
  @config_file
end

#conversationObject (readonly)

Returns the value of attribute conversation.



3
4
5
# File 'lib/smart_prompt/worker.rb', line 3

def conversation
  @conversation
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/smart_prompt/worker.rb', line 3

def name
  @name
end

Class Method Details

.define(name, &block) ⇒ Object



48
49
50
# File 'lib/smart_prompt/worker.rb', line 48

def define(name, &block)
  workers[name] = block
end

.workersObject



44
45
46
# File 'lib/smart_prompt/worker.rb', line 44

def workers
  @workers ||= {}
end

Instance Method Details

#execute(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smart_prompt/worker.rb', line 13

def execute(params = {})
  # Generate default session ID if using history and no session_id provided.
  # (Do NOT default to a literal "default" here — that would make every
  # history-using worker share one session and leave the worker-name branch
  # below as dead code, breaking per-worker session isolation.)
  session_id = params[:session_id]
  if params[:with_history] && !session_id && @engine.history_manager
    session_id = "worker_#{@name}_#{Time.now.to_i}"
    SmartPrompt.logger.info "Generated default session ID: #{session_id}"
  end
  if @conversation.nil? || @conversation.session_id != session_id
    @conversation = Conversation.new(@engine, params[:tools], session_id)
  end
  context = WorkerContext.new(@conversation, params, @engine)
  context.instance_eval(&@code)
end

#execute_by_stream(params = {}, &proc) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/smart_prompt/worker.rb', line 30

def execute_by_stream(params = {}, &proc)
  # Generate default session ID if using history and no session_id provided
  session_id = params[:session_id]
  if params[:with_history] && !session_id && @engine.history_manager
    session_id = "worker_#{@name}_#{Time.now.to_i}"
    SmartPrompt.logger.info "Generated default session ID: #{session_id}"
  end

  @conversation = Conversation.new(@engine, params[:tools], session_id)
  context = WorkerContext.new(@conversation, params, @engine, proc)
  context.instance_eval(&@code)
end