Module: LLM::Utils

Extended by:
Utils
Included in:
Anthropic, Google, Tool::Exec, Utils
Defined in:
lib/llm/utils.rb

Overview

Shared utility methods used across the runtime.

Instance Method Summary collapse

Instance Method Details

#timestamp(id) ⇒ Time?

Returns the UTC time encoded in a UUIDv7, or nil when the given value is not a UUIDv7. The first 48 bits of a UUIDv7 are a Unix millisecond timestamp.

Parameters:

Returns:

  • (Time, nil)

See Also:



68
69
70
71
72
# File 'lib/llm/utils.rb', line 68

def timestamp(id)
  return nil unless uuidv7?(id)
  hex = id.to_s.delete("-")
  Time.at(hex[0, 12].to_i(16) / 1000.0).utc
end

#record?(obj) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true when obj is an ActiveRecord or Sequel model.

Parameters:

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/llm/utils.rb', line 46

def record?(obj)
  (defined?(::ActiveRecord::Base) and ::ActiveRecord::Base === obj) or
  (defined?(::Sequel::Model) and ::Sequel::Model === obj)
end

#uuidv7?(id) ⇒ Boolean

Returns true when a value is a UUIDv7 string: 32 hexadecimal digits, with or without hyphens, whose version nibble is a 7.

Parameters:

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/llm/utils.rb', line 56

def uuidv7?(id)
  hex = id.to_s.delete("-")
  hex.match?(/\A\h{32}\z/) && hex[12] == "7"
end

#resolve_option(obj, option, resolve_symbol: true) ⇒ Object

Resolves a configured option against an object instance.

Proc values are evaluated with instance_exec, symbol values are optionally sent to the object as method calls, hashes are duplicated, and all other values are returned as-is.

Parameters:

  • obj (Object)
  • option (Object)
  • resolve_symbol (Boolean) (defaults to: true)

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/llm/utils.rb', line 20

def resolve_option(obj, option, resolve_symbol: true)
  case option
  when Proc then obj.instance_exec(&option)
  when Symbol
    if resolve_symbol
      if obj.respond_to?(option, true) and record?(obj)
        obj.send(option)
      elsif obj.respond_to?(:record) and obj.record.respond_to?(option, true)
        obj.record.send(option)
      elsif obj.respond_to?(option, true)
        obj.send(option)
      else
        raise ArgumentError, "unable to resolve #{option}"
      end
    else
      option
    end
  when Hash then option.dup
  else option
  end
end

#normalize_base_path(path) ⇒ String

Normalizes an HTTP API base path.

Blank paths normalize to an empty string. Non-empty paths are prefixed with a leading slash and stripped of trailing slashes.

Parameters:

  • path (String, nil)

Returns:

  • (String)


82
83
84
85
86
87
# File 'lib/llm/utils.rb', line 82

def normalize_base_path(path)
  path = path.to_s.strip
  return "" if path.empty? || path == "/"
  path = "/#{path}" unless path.start_with?("/")
  path.sub(%r{/+\z}, "")
end

#name_of(obj) ⇒ String?

Returns the Ruby module or class name for an object.

This bypasses overridden #name implementations by binding Module#name directly.

Parameters:

  • obj (Module)

Returns:

  • (String, nil)


97
98
99
# File 'lib/llm/utils.rb', line 97

def name_of(obj)
  ::Module.instance_method(:name).bind(obj).call
end

#object_id(obj) ⇒ String

Renders the class-and-object-id portion of an inspect string.

This returns strings like LLM::Tool:0x1234abcd, which can be embedded into custom inspect output.

Parameters:

Returns:

  • (String)


109
110
111
112
113
114
115
116
# File 'lib/llm/utils.rb', line 109

def object_id(obj)
  klass = if Class === obj
    name_of(obj) || name_of(obj.superclass) || obj.class.name
  else
    obj.class.name || obj.class.to_s
  end
  "#{klass}:0x#{obj.object_id.to_s(16)}"
end