Class: LLM::Function::Ractor::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/function/ractor/job.rb

Overview

Runs a tool call inside the one ractor a task spawns; sends its result to the task's port after the tool finishes, before waiting for the interrupt watcher to stop.

Instance Method Summary collapse

Constructor Details

#initialize(resultp, runner_class, id, name, arguments, cookie) ⇒ LLM::Function::Ractor::Job

Parameters:

  • resultp (Ractor::Port)
  • runner_class (Class)
  • id (String, nil)
  • name (String)
  • arguments (Hash, Array, nil)
  • cookie (String)


22
23
24
25
26
27
28
29
# File 'lib/llm/function/ractor/job.rb', line 22

def initialize(resultp, runner_class, id, name, arguments, cookie)
  @resultp = resultp
  @runner_class = runner_class
  @id = id
  @name = name
  @arguments = arguments
  @cookie = cookie
end

Instance Method Details

#call

This method returns an undefined value.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/llm/function/ractor/job.rb', line 33

def call
  LLM.debug "[ractor #{@cookie}] job started"
  finished_at = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  window = LLM::Function::Window.new(thread: ::Thread.current)
  runner = Class === @runner_class ? @runner_class.new : @runner_class
  watcher = watch(runner, window)
  kwargs = Hash === @arguments ? @arguments.transform_keys(&:to_sym) : @arguments
  window.running!
  result = runner.call(**kwargs)
  LLM.debug "[ractor #{@cookie}] tool returned"
  window.finished!
  resultm = [:done, @id, @name, result, finished_at.()]
rescue LLM::Interrupt
  LLM.debug "[ractor #{@cookie}] tool interrupted"
  resultm = [:done, @id, @name, {interrupt: true, cookie: @cookie}, finished_at.()]
rescue => ex
  LLM.debug "[ractor #{@cookie}] tool error #{ex.class}: #{ex.message} at #{ex.backtrace&.first(3)&.join(" | ")}"
  error = {error: true, type: ex.class.name, message: ex.message, backtrace: ex.backtrace}
  resultm = [:done, @id, @name, error, finished_at.()]
ensure
  window&.finished!
  LLM.debug "[ractor #{@cookie}] stopping interrupt watcher"
  watcher&.kill&.join
  @resultp.send(resultm) if resultm
  LLM.debug "[ractor #{@cookie}] job ensure complete"
end