Module: OllamaChat::ServerSocket

Included in:
Chat
Defined in:
lib/ollama_chat/server_socket.rb

Overview

A module that provides server socket functionality for OllamaChat

The ServerSocket module encapsulates the logic for creating and managing Unix domain socket servers that enable external processes to send input to running ollama_chat sessions. It supports both simple message transmission and bidirectional communication with response handling, allowing for integration with tools like ollama_chat_send.

Examples:

Sending a message to a running chat session

OllamaChat::ServerSocket.send_to_server_socket(
  "Hello from external process",
  config: chat_config,
  type: :socket_input
)

Sending a message and waiting for a response

response = OllamaChat::ServerSocket.send_to_server_socket(
  "What is the answer?",
  config: chat_config,
  type: :socket_input_with_response,
  parse: true
)

Class Method Summary collapse

Class Method Details

.create_socket_server(config:, runtime_dir: nil, working_dir: nil) ⇒ UnixSocks::DomainSocketServer

The create_socket_server method constructs and returns a Unix domain socket server instance for communication with the Ollama Chat client.

This method initializes a UnixSocks::DomainSocketServer object configured to listen for incoming messages on a named socket file. It supports specifying a custom runtime directory for the socket, which is useful for isolating multiple instances or environments. If no runtime directory is provided in the configuration, it defaults to using the standard system location for Unix domain sockets.

Parameters:

  • config (ComplexConfig::Settings)

    the configuration object containing server settings

  • runtime_dir (String) (defaults to: nil)

    pathname to runtime_dir of socket file

  • working_dir (String) (defaults to: nil)

    pathname to working_dir used for deriving socket file

Returns:

  • (UnixSocks::DomainSocketServer)

    a configured Unix domain socket server instance ready to receive messages



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ollama_chat/server_socket.rb', line 75

def create_socket_server(config:, runtime_dir: nil, working_dir: nil)
  working_dir ||= Dir.pwd
  if runtime_dir
    return UnixSocks::DomainSocketServer.new(socket_name: 'ollama_chat.sock', runtime_dir:)
  end
  if config.working_dir_dependent_socket
    path   = File.expand_path(working_dir)
    digest = Digest::MD5.hexdigest(path)
    UnixSocks::DomainSocketServer.new(socket_name: "ollama_chat-#{digest}.sock")
  else
    UnixSocks::DomainSocketServer.new(socket_name: 'ollama_chat.sock')
  end
end

.send_to_server_socket(content, config:, type: :socket_input, runtime_dir: nil, working_dir: nil, parse: false) ⇒ UnixSocks::Message?

The send_to_server_socket method transmits a message to a Unix domain socket server for processing by the Ollama Chat client.

This method creates a socket server instance using the provided configuration, prepares a message with the given content, type, and parse flag, then sends it either as a simple transmission or with a response expectation depending on the message type. It is used to enable communication between external processes and the chat session via a named Unix socket.

Parameters:

  • content (String)

    the message content to be sent

  • config (ComplexConfig::Settings)

    the configuration object containing server settings

  • type (Symbol) (defaults to: :socket_input)

    the type of message transmission, defaults to :socket_input

  • runtime_dir (String) (defaults to: nil)

    pathname to runtime_dir of socket file

  • working_dir (String) (defaults to: nil)

    pathname to working_dir used for deriving socket file

  • parse (TrueClass, FalseClass) (defaults to: false)

    whether to parse the response, defaults to false

Returns:

  • (UnixSocks::Message, nil)

    the response from transmit_with_response if type is :socket_input_with_response, otherwise nil



47
48
49
50
51
52
53
54
55
56
# File 'lib/ollama_chat/server_socket.rb', line 47

def send_to_server_socket(content, config:, type: :socket_input, runtime_dir: nil, working_dir: nil, parse: false)
  server  = create_socket_server(config:, runtime_dir:, working_dir:)
  message = { content:, type:, parse: }
  if type.to_sym == :socket_input_with_response
    server.transmit_with_response(message)
  else
    server.transmit(message)
    nil
  end
end