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



30
31
32
# File 'lib/smart_prompt/worker.rb', line 30

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

.workersObject



26
27
28
# File 'lib/smart_prompt/worker.rb', line 26

def workers
  @workers ||= {}
end

Instance Method Details

#execute(params = {}) ⇒ Object



13
14
15
16
17
# File 'lib/smart_prompt/worker.rb', line 13

def execute(params = {})
  @conversation = Conversation.new(@engine, params[:tools]) unless @conversation
  context = WorkerContext.new(@conversation, params, @engine)
  context.instance_eval(&@code)
end

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



19
20
21
22
23
# File 'lib/smart_prompt/worker.rb', line 19

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