Class: LLM::Function::Thread::Task

Inherits:
LLM::Function::Task show all
Defined in:
lib/llm/function/thread/task.rb

Overview

LLM::Function::Thread::Task wraps a function call in a background thread for concurrent tool execution. The thread is created lazily when #wait is called, not when the task is constructed — so you can build a task, pass it around, and decide when to run it.

Interrupting a running task raises Interrupt inside the thread, which stops the tool call mid-flight. The thread is created with report_on_exception disabled so unhandled exceptions propagate through #wait instead of to stderr.

Instance Attribute Summary

Attributes inherited from LLM::Function::Task

#function

Instance Method Summary collapse

Constructor Details

#initialize(fn, options = {}) ⇒ Task

Returns a new instance of Task.

Parameters:



19
20
21
# File 'lib/llm/function/thread/task.rb', line 19

def initialize(fn, options = {})
  super
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/llm/function/thread/task.rb', line 33

def alive?
  @thread&.alive? || false
end

#group_classClass

Returns:

  • (Class)


56
57
58
# File 'lib/llm/function/thread/task.rb', line 56

def group_class
  LLM::Function::Thread::Group
end

#interrupt!nil Also known as: cancel!

Returns:

  • (nil)


39
40
41
42
43
# File 'lib/llm/function/thread/task.rb', line 39

def interrupt!
  @thread&.raise(LLM::Interrupt) if @thread&.alive?
  function.interrupt!
  nil
end

#spawnnil

Returns:

  • (nil)


25
26
27
28
29
# File 'lib/llm/function/thread/task.rb', line 25

def spawn
  @thread = ::Thread.new { function.call }
  @thread.report_on_exception = false
  nil
end

#waitLLM::Function::Return Also known as: value



48
49
50
51
# File 'lib/llm/function/thread/task.rb', line 48

def wait
  spawn unless @thread
  @thread.value
end