Class: Foobara::Agent::AccomplishGoal

Inherits:
Command
  • Object
show all
Defined in:
foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome, #raw_result

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Attribute Details

#command_connectorObject



114
115
116
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 114

def command_connector
  @command_connector ||= existing_command_connector
end

#command_outcomeObject

Returns the value of attribute command_outcome.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def command_outcome
  @command_outcome
end

#command_responseObject

Returns the value of attribute command_response.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def command_response
  @command_response
end

#contextObject

Returns the value of attribute context.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def context
  @context
end

#delayed_command_nameObject

Returns the value of attribute delayed_command_name.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def delayed_command_name
  @delayed_command_name
end

#final_messageObject

Returns the value of attribute final_message.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def final_message
  @final_message
end

#final_resultObject

Returns the value of attribute final_result.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def final_result
  @final_result
end

#given_upObject

Returns the value of attribute given_up.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def given_up
  @given_up
end

#mission_accomplishedObject

Returns the value of attribute mission_accomplished.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def mission_accomplished
  @mission_accomplished
end

#next_commandObject

Returns the value of attribute next_command.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def next_command
  @next_command
end

#next_command_classObject

Returns the value of attribute next_command_class.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def next_command_class
  @next_command_class
end

#next_command_inputsObject

Returns the value of attribute next_command_inputs.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def next_command_inputs
  @next_command_inputs
end

#next_command_nameObject

Returns the value of attribute next_command_name.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def next_command_name
  @next_command_name
end

#timed_outObject

Returns the value of attribute timed_out.



96
97
98
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 96

def timed_out
  @timed_out
end

Instance Method Details

#add_given_up_errorObject



458
459
460
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 458

def add_given_up_error
  add_runtime_error(:gave_up, reason: final_message)
end

#agent_commands_connected?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 84

def agent_commands_connected?
  command_connector.agent_commands_connected?
end

#agent_nameObject



101
102
103
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 101

def agent_name
  @agent_name ||= inputs[:agent_name] || "Anon#{SecureRandom.hex(2)}"
end

#all_command_classesObject



329
330
331
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 329

def all_command_classes
  @all_command_classes ||= run_subcommand!(ListCommands, command_connector:).values.flatten
end

#build_command_connectorObject

Do we really want to support calling AccomplishGoal outside the context of an Agent? If not, just delete this awkward coupling



120
121
122
123
124
125
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 120

def build_command_connector
  self.command_connector ||= Agent.new(
    current_accomplish_goal_command: self,
    llm_model:
  )
end

#build_initial_context_if_necessaryObject



105
106
107
108
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 105

def build_initial_context_if_necessary
  # TODO: shouldn't have to pass command_log here since it has a default, debug that
  self.context = current_context || Context.new(command_log: [])
end

#build_resultObject



462
463
464
465
466
467
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 462

def build_result
  {
    message_to_user: final_message,
    result_data: final_result
  }
end

#check_if_too_many_callsObject



408
409
410
411
412
413
414
415
416
417
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 408

def check_if_too_many_calls
  if context.command_log.size > maximum_command_calls
    add_runtime_error(
      :too_many_command_calls,
      "Too many command calls. " \
      "Stopping. Increase maximum_command_calls if #{maximum_command_calls} is not enough.",
      maximum_command_calls:
    )
  end
end

#choose_describe_command_insteadObject



323
324
325
326
327
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 323

def choose_describe_command_instead
  self.delayed_command_name = next_command_name
  self.next_command_inputs = { command_name: next_command_name }
  self.next_command_name = DescribeCommand.full_command_name
end

#choose_next_command_and_next_inputs_separately?Boolean

Returns:

  • (Boolean)


485
486
487
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 485

def choose_next_command_and_next_inputs_separately?
  choose_next_command_and_next_inputs_separately
end

#command_class_for_determine_inputs_for_next_commandObject



388
389
390
391
392
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 388

def command_class_for_determine_inputs_for_next_command
  DetermineInputsForNextCommand.for(
    command_class: next_command_class, agent_id: agent_name
  )
end

#command_connector_passed_in?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 110

def command_connector_passed_in?
  existing_command_connector
end

#command_described?Boolean

Returns:

  • (Boolean)


469
470
471
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 469

def command_described?
  described_commands.include?(next_command_name)
end

#command_name_typeObject



264
265
266
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 264

def command_name_type
  @command_name_type ||= Agent.foobara_type_from_declaration(:string, one_of: all_command_classes)
end

#connect_agent_commandsObject



131
132
133
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 131

def connect_agent_commands
  command_connector.connect_agent_commands(final_result_type:, agent_name:)
end

#connect_user_provided_commandsObject



135
136
137
138
139
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 135

def connect_user_provided_commands
  command_classes.each do |command_class|
    command_connector.connect(command_class)
  end
end

#described_commandsObject



473
474
475
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 473

def described_commands
  @described_commands ||= Set.new
end

#determine_next_command_and_inputs(retries = 2) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 141

def determine_next_command_and_inputs(retries = 2)
  if context.command_log.empty?
    self.next_command_name = ListCommands.full_command_name
    self.next_command_inputs = nil
    fetch_next_command_class
    return
  end

  inputs_for_determine = {
    goal:,
    context:,
    llm_model:,
    command_class_names: all_command_classes
  }

  determine_command = DetermineNextCommandNameAndInputs.new(inputs_for_determine)

  outcome = begin
    determine_command.run
  rescue CommandPatternImplementation::Concerns::Result::CouldNotProcessResult => e
    # :nocov:
    Outcome.errors(e.errors)
    # :nocov:
  end

  if outcome.success?
    self.next_command_name = outcome.result[:command_name]
    self.next_command_inputs = outcome.result[:inputs]

    outcome = validate_next_command_name

    if outcome.success?
      fetch_next_command_class

      if next_command_has_inputs?
        outcome = validate_next_command_inputs

        if outcome.success?
          if log_successful_determine_command_and_inputs_outcomes?
            log_command_outcome(
              command: determine_command,
              inputs: determine_command.inputs.except(:context)
            )
          end
        else
          log_command_outcome(
            command: determine_command,
            inputs: determine_command.inputs.except(:context),
            outcome:,
            result: outcome.result || determine_command.raw_result
          )

          determine_next_command_inputs
        end
      else
        self.next_command_inputs = {}
      end
    else
      log_command_outcome(
        command: determine_command,
        inputs: determine_command.inputs&.except(:context),
        outcome:,
        result: outcome.result || determine_command.raw_result
      )

      if retries > 0
        determine_next_command_and_inputs(retries - 1)
      else
        determine_next_command_then_inputs_separately
      end
    end
  else
    log_command_outcome(
      command_name: determine_command.class.full_command_name,
      inputs: determine_command.inputs&.except(:context),
      outcome:,
      result: outcome.result || determine_command.raw_result
    )

    if retries > 0
      determine_next_command_and_inputs(retries - 1)
    else
      determine_next_command_then_inputs_separately
    end
  end
end

#determine_next_command_inputs(retries = 2) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 337

def determine_next_command_inputs(retries = 2)
  self.next_command_inputs = if next_command_has_inputs?
                               command_class = command_class_for_determine_inputs_for_next_command

                               inputs = { goal:, context: }
                               if llm_model
                                 inputs[:llm_model] = llm_model
                               end

                               command = command_class.new(inputs)
                               outcome = begin
                                 command.run
                               rescue CommandPatternImplementation::Concerns::Result::CouldNotProcessResult => e
                                 # :nocov:
                                 Outcome.errors(e.errors)
                                 # :nocov:
                               end

                               if outcome.success?
                                 if log_successful_determine_command_and_inputs_outcomes?
                                   log_command_outcome(
                                     command:,
                                     inputs: command.inputs.except(:context),
                                     outcome:
                                   )
                                 end
                               else
                                 # TODO: either figure out a way to hit this path in the test suite or delete it
                                 # :nocov:
                                 log_command_outcome(
                                   command:,
                                   inputs: command.inputs.except(:context),
                                   outcome:,
                                   result: outcome.result || command.raw_result
                                 )
                                 if retries > 0
                                   return determine_next_command_inputs(retries - 1)
                                 end
                                 # :nocov:
                               end

                               outcome.raise!
                               outcome.result
                             end
end

#determine_next_command_name(retries = 2) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 268

def determine_next_command_name(retries = 2)
  self.next_command_name = if context.command_log.empty?
                             ListCommands.full_command_name
                           elsif delayed_command_name
                             name = delayed_command_name
                             self.delayed_command_name = nil
                             name
                           else
                             command_class = DetermineNextCommand.for(
                               command_class_names: all_command_classes, agent_id: agent_name
                             )

                             inputs = { goal:, context: }
                             if llm_model
                               inputs[:llm_model] = llm_model
                             end

                             command = command_class.new(inputs)
                             outcome = begin
                               command.run
                             rescue CommandPatternImplementation::Concerns::Result::CouldNotProcessResult => e
                               # :nocov:
                               Outcome.errors(e.errors)
                               # :nocov:
                             end

                             if outcome.success?
                               if log_successful_determine_command_and_inputs_outcomes?
                                 log_command_outcome(
                                   command:,
                                   inputs: command.inputs.except(:context),
                                   outcome:
                                 )
                               end
                             else
                               # TODO: either figure out a way to hit this path in the test suite or delete it
                               # :nocov:
                               log_command_outcome(
                                 command:,
                                 inputs: command.inputs.except(:context),
                                 outcome:,
                                 result: outcome.result || command.raw_result
                               )

                               if retries > 0
                                 return determine_next_command_name(retries - 1)
                               end
                               # :nocov:
                             end

                             outcome.raise!
                             outcome.result
                           end
end

#determine_next_command_then_inputs_separatelyObject



228
229
230
231
232
233
234
235
236
237
238
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 228

def determine_next_command_then_inputs_separately
  determine_next_command_name

  if command_described?
    fetch_next_command_class
    determine_next_command_inputs
  else
    choose_describe_command_instead
    fetch_next_command_class
  end
end

#empty_attributes?(type) ⇒ Boolean

Returns:

  • (Boolean)


477
478
479
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 477

def empty_attributes?(type)
  type.extends_type?(BuiltinTypes[:attributes]) && type.element_types.empty?
end

#executeObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 51

def execute
  build_initial_context_if_necessary

  if command_connector_passed_in?
    set_accomplished_goal_command
  else
    build_command_connector
    connect_user_provided_commands
  end

  unless agent_commands_connected?
    connect_agent_commands
  end

  until mission_accomplished or given_up
    check_if_too_many_calls
    if choose_next_command_and_next_inputs_separately?
      determine_next_command_then_inputs_separately
    else
      determine_next_command_and_inputs
    end

    run_next_command
    log_last_command_outcome
  end

  if given_up
    add_given_up_error
  end

  build_result
end

#fetch_next_command_classObject



333
334
335
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 333

def fetch_next_command_class
  self.next_command_class = command_connector.transformed_command_from_name(next_command_name)
end

#give_up!(message) ⇒ Object



453
454
455
456
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 453

def give_up!(message)
  self.given_up = true
  self.final_message = message
end

#log_command_outcome(command: nil, command_name: nil, inputs: nil, outcome: nil, result: nil) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 419

def log_command_outcome(command: nil, command_name: nil, inputs: nil, outcome: nil, result: nil)
  if command
    command_name ||= command.class.full_command_name
    inputs ||= command.inputs
    outcome ||= command.outcome
    result ||= outcome.result
  end

  outcome_hash = { success: outcome.success? }

  if outcome.success? || result
    outcome_hash[:result] = result
  end

  unless outcome.success?
    outcome_hash[:errors_hash] = outcome.errors_hash
  end

  log_entry = CommandLogEntry.new(
    command_name:,
    inputs:,
    outcome: outcome_hash
  )

  context.command_log << log_entry
end

#log_last_command_outcomeObject



404
405
406
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 404

def log_last_command_outcome
  log_command_outcome(command: command_response.command)
end

#log_successful_determine_command_and_inputs_outcomes?Boolean

Returns:

  • (Boolean)


481
482
483
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 481

def log_successful_determine_command_and_inputs_outcomes?
  log_successful_determine_command_and_inputs_outcomes
end

#mission_accomplished!(final_result, message) ⇒ Object

TODO: these are awkwardly called from outside. Come up with a better solution.



447
448
449
450
451
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 447

def mission_accomplished!(final_result, message)
  self.mission_accomplished = true
  self.final_result = final_result
  self.final_message = message
end

#next_command_has_inputs?Boolean

Returns:

  • (Boolean)


383
384
385
386
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 383

def next_command_has_inputs?
  type = next_command_class.inputs_type
  type && !empty_attributes?(type)
end

#run_next_commandObject



394
395
396
397
398
399
400
401
402
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 394

def run_next_command
  self.command_response = command_connector.run(
    full_command_name: next_command_name,
    inputs: next_command_inputs,
    action: "run"
  )

  self.command_outcome = command_response.outcome
end

#set_accomplished_goal_commandObject



127
128
129
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 127

def set_accomplished_goal_command
  command_connector.current_accomplish_goal_command = self
end

#validateObject



88
89
90
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 88

def validate
  validate_either_command_classes_or_connector_given
end

#validate_either_command_classes_or_connector_givenObject



92
93
94
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 92

def validate_either_command_classes_or_connector_given
  # TODO: implement this!
end

#validate_next_command_inputsObject



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 250

def validate_next_command_inputs
  inputs_type = next_command_class.inputs_type

  outcome = NestedTransactionable.with_needed_transactions_for_type(inputs_type) do
    inputs_type.process_value(next_command_inputs)
  end

  if outcome.success?
    self.next_command_inputs = outcome.result
  end

  outcome
end

#validate_next_command_nameObject



240
241
242
243
244
245
246
247
248
# File 'foobara-agent-0.0.5/src/foobara/agent/accomplish_goal.rb', line 240

def validate_next_command_name
  outcome = command_name_type.process_value(next_command_name)

  if outcome.success?
    self.next_command_name = outcome.result
  end

  outcome
end