Module: LLM::Function::Array

Defined in:
lib/llm/function/array.rb

Overview

The Array module extends the array returned by Context#functions with methods that can call all pending functions sequentially or concurrently. The return values can be reported back to the LLM on the next turn.

Instance Method Summary collapse

Instance Method Details

#callArray<LLM::Function::Return>

Calls all functions in a collection sequentially.

Returns:



15
16
17
# File 'lib/llm/function/array.rb', line 15

def call
  map(&:call)
end

#spawn(strategy) ⇒ LLM::Function::ThreadGroup, ...

Calls all functions in a collection concurrently. This method returns an ThreadGroup, TaskGroup, or FiberGroup that can be waited on to access the return values.

Parameters:

  • strategy (Symbol)

    Controls concurrency strategy:

    • ‘:thread`: Use threads

    • ‘:task`: Use async tasks (requires async gem)

    • ‘:fiber`: Use scheduler-backed fibers (requires Fiber.scheduler)

    • ‘:fork`: Use forked child processes

    • ‘:ractor`: Use Ruby ractors (class-based tools only; MCP tools are not supported)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llm/function/array.rb', line 34

def spawn(strategy)
  case strategy
  when :task
    TaskGroup.new(map { |fn| fn.spawn(:task) })
  when :thread
    ThreadGroup.new(map { |fn| fn.spawn(:thread) })
  when :fiber
    FiberGroup.new(map { |fn| fn.spawn(:fiber) })
  when :fork
    Fork::Group.new(map { |fn| fn.spawn(:fork) })
  when :ractor
    Ractor::Group.new(map { |fn| fn.spawn(:ractor) })
  else
    raise ArgumentError, "Unknown strategy: #{strategy.inspect}. Expected :thread, :task, :fiber, :fork, or :ractor"
  end
end

#wait(strategy) ⇒ Array<LLM::Function::Return>

Calls all functions in a collection concurrently and waits for the return values.

Parameters:

  • strategy (Symbol)

    Controls concurrency strategy:

    • ‘:thread`: Use threads

    • ‘:task`: Use async tasks (requires async gem)

    • ‘:fiber`: Use scheduler-backed fibers (requires Fiber.scheduler)

    • ‘:fork`: Use forked child processes

    • ‘:ractor`: Use Ruby ractors (class-based tools only; MCP tools are not supported)

Returns:



65
66
67
# File 'lib/llm/function/array.rb', line 65

def wait(strategy)
  spawn(strategy).wait
end