Class: LLM::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/transport.rb,
lib/llm/transport/http.rb,
lib/llm/transport/response.rb,
lib/llm/transport/execution.rb,
lib/llm/transport/stream_decoder.rb,
lib/llm/transport/persistent_http.rb

Overview

The LLM::Transport class defines the execution interface used by Provider.

Custom transports can subclass this class and override #request to execute provider requests without changing request adapters or response adapters.

Providers currently construct Net::HTTPRequest objects before delegating to a transport. Custom transports are therefore expected to execute those requests directly, or transform them into backend-specific request objects before execution.

Only #request is required. The remaining methods are optional hooks for features such as interruption, request ownership, or persistence, and only need to be implemented when the underlying adapter can support them.

Returned responses should implement the LLM::Transport::Response interface. In practice this can mean adapting another client’s response object so existing provider execution, response adapters, and error handlers can rely on one normalized response contract instead of transport-specific classes.

Direct Known Subclasses

HTTP, PersistentHTTP

Defined Under Namespace

Modules: Execution Classes: HTTP, PersistentHTTP, Response, StreamDecoder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.net_httpClass

Returns the built-in Net::HTTP transport class.

Returns:

  • (Class)


38
39
40
# File 'lib/llm/transport.rb', line 38

def self.net_http
  HTTP
end

.net_http_persistentClass

Returns the built-in Net::HTTP::Persistent transport class.

Returns:

  • (Class)


45
46
47
# File 'lib/llm/transport.rb', line 45

def self.net_http_persistent
  PersistentHTTP
end

Instance Method Details

#interrupt!(owner) ⇒ nil

Interrupt an active request, if any.

Parameters:

Returns:

  • (nil)

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/llm/transport.rb', line 79

def interrupt!(owner)
  raise NotImplementedError
end

#interrupt_errorsArray<Class<Exception>>

Returns the exception classes that indicate an interrupted request.

Returns:

  • (Array<Class<Exception>>)


71
72
73
# File 'lib/llm/transport.rb', line 71

def interrupt_errors
  []
end

#interrupted?(owner) ⇒ Boolean?

Returns whether an execution owner was interrupted.

Parameters:

Returns:

  • (Boolean, nil)


87
88
89
# File 'lib/llm/transport.rb', line 87

def interrupted?(owner)
  nil
end

#request(request, owner:, stream: nil) {|response| ... } ⇒ Object

Performs a request through the transport.

Parameters:

  • request (Net::HTTPRequest)
  • owner (Object)
  • stream (LLM::Object, nil) (defaults to: nil)

Yield Parameters:

Returns:

Raises:

  • (NotImplementedError)


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

def request(request, owner:, stream: nil, &)
  raise NotImplementedError
end

#request_ownerObject

Returns the current request owner.

Returns:



63
64
65
66
# File 'lib/llm/transport.rb', line 63

def request_owner
  return Fiber.current unless defined?(::Async)
  Async::Task.current? ? Async::Task.current : Fiber.current
end

#set_body_stream(request, io) ⇒ void

Note:

Custom transports may be able to reuse this helper when they operate on Net::HTTPRequest objects, or implement their own request body preparation path instead.

This method returns an undefined value.

Parameters:

  • request (Net::HTTPRequest)
  • io (IO)


99
100
101
102
# File 'lib/llm/transport.rb', line 99

def set_body_stream(request, io)
  request.body_stream = io
  request["transfer-encoding"] = "chunked" unless request["content-length"]
end