Module: Roast::Helpers::FunctionCachingInterceptor

Defined in:
lib/roast/helpers/function_caching_interceptor.rb

Overview

Intercepts function dispatching to add caching capabilities This module wraps around Raix::FunctionDispatch to provide caching for tool functions

Instance Method Summary collapse

Instance Method Details

#dispatch_tool_function(function_name, params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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 'lib/roast/helpers/function_caching_interceptor.rb', line 8

def dispatch_tool_function(function_name, params)
  start_time = Time.now

  ActiveSupport::Notifications.instrument("roast.tool.execute", {
    function_name: function_name,
    params: params,
  })

  # Handle workflows with or without configuration
  result = if !respond_to?(:configuration) || configuration.nil?
    super(function_name, params)
  else
    function_config = if configuration.respond_to?(:function_config)
      configuration.function_config(function_name)
    else
      {}
    end

    # Check if caching is enabled - handle both formats:
    # 1. cache: true (boolean format)
    # 2. cache: { enabled: true } (hash format)
    cache_enabled = if function_config.is_a?(Hash)
      cache_config = function_config["cache"]
      if cache_config.is_a?(Hash)
        cache_config["enabled"]
      else
        # Direct boolean value
        cache_config
      end
    else
      false
    end

    if cache_enabled
      # Call the original function and pass in the cache
      super(function_name, params, cache: Roast::Tools::CACHE)
    else
      Roast::Helpers::Logger.debug("⚠️ Caching not enabled for #{function_name}")
      super(function_name, params)
    end
  end

  execution_time = Time.now - start_time

  # Determine if caching was enabled for metrics
  cache_enabled = if defined?(function_config) && function_config.is_a?(Hash)
    cache_config = function_config["cache"]
    if cache_config.is_a?(Hash)
      cache_config["enabled"]
    else
      # Direct boolean value
      cache_config
    end
  else
    false
  end

  ActiveSupport::Notifications.instrument("roast.tool.complete", {
    function_name: function_name,
    execution_time: execution_time,
    cache_enabled: cache_enabled,
  })

  result
rescue => e
  execution_time = Time.now - start_time

  ActiveSupport::Notifications.instrument("roast.tool.error", {
    function_name: function_name,
    error: e.class.name,
    message: e.message,
    execution_time: execution_time,
  })
  raise
end