Class: AgentRuby::Step::CreateTask

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_ruby/step/create_task.rb

Instance Method Summary collapse

Constructor Details

#initialize(actions:, chat:, message:, results:, block: nil) ⇒ CreateTask

Returns a new instance of CreateTask.



6
7
8
9
10
11
12
13
# File 'lib/agent_ruby/step/create_task.rb', line 6

def initialize(actions:, chat:, message:, results:, block: nil)
  @message = message
  @results = results
  @block = block
  @actions = actions
  @chat = chat
  @response = nil
end

Instance Method Details

#messageObject



42
43
44
# File 'lib/agent_ruby/step/create_task.rb', line 42

def message
  @response.content.match(/<Message>(.+)<\/Message>/m)[1]
end

#metadataObject



46
47
48
49
50
51
52
# File 'lib/agent_ruby/step/create_task.rb', line 46

def 
  if @response.content.match(/<Metadata>(.+)<\/Metadata>/m)
    JSON.parse(@response.content.match(/<Metadata>(.+)<\/Metadata>/m)[1], symbolize_names: true)
  else
    {}
  end
end

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/agent_ruby/step/create_task.rb', line 15

def perform
  @response = @chat.ask([
    @message,
    Prompt::CreateTask.new(actions: @actions, results: @results).to_s,
  ].join) do |chunk|
    @block.call(chunk) if @block
  end

  if @response.content.match?(/<Error>/)
    raise Error, "Error: #{@response.content.match(/<Message>(.+)<\/Message>/m)[1]}"
  end
  
  unless @response.content.match?(/<Message>(.+)<\/Message>/m)
    raise NoMessageError, "Error: No message found in the response"
  end
  
  unless @response.content.match?(/<Task>(.+)<\/Task>/m)
    raise NoTaskError, "Error: No task found in the response"
  end

  @response
end

#taskObject



38
39
40
# File 'lib/agent_ruby/step/create_task.rb', line 38

def task
  Task.new(**JSON.parse(@response.content.match(/<Task>(.+)<\/Task>/m)[1], symbolize_names: true))
end