Class: Smith::Tool::ExecutionBatchRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/tool/execution_batch_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeExecutionBatchRegistry

Returns a new instance of ExecutionBatchRegistry.



9
10
11
12
13
# File 'lib/smith/tool/execution_batch_registry.rb', line 9

def initialize
  @batches = {}.compare_by_identity
  @builder = ExecutionBatchBuilder.new
  @mutex = Mutex.new
end

Instance Method Details

#build_and_register(tool_calls:, tools:, context:, admission_resolver:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smith/tool/execution_batch_registry.rb', line 15

def build_and_register(tool_calls:, tools:, context:, admission_resolver:)
  captured_calls = ExecutionBatchCollection.capture(tool_calls)
  entries = captured_calls.each_pair.map { |key, tool_call| [key, tool_call].freeze }.freeze
  source_calls = entries.map(&:last).freeze
  reserve_calls!(source_calls)
  call_admissions = admission_resolver.call(source_calls)
  batch = @builder.call(entries:, tools:, context:, call_admissions:)
  publish_batch!(source_calls, batch)
  batch
rescue Exception # rubocop:disable Lint/RescueException
  rollback_registration(source_calls, batch)
  raise
end

#fetch(tool_call) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/smith/tool/execution_batch_registry.rb', line 38

def fetch(tool_call)
  @mutex.synchronize do
    batch = @batches[tool_call]
    raise Error, "tool call batch registration is incomplete" if batch.equal?(PENDING_BATCH)

    batch
  end
end

#unregister(batch) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/smith/tool/execution_batch_registry.rb', line 29

def unregister(batch)
  return unless batch

  @mutex.synchronize do
    unregister_calls(batch.source_calls, batch)
    unregister_calls(batch.tool_calls.each_value, batch)
  end
end