Module: OllamaChat::ServerSocket

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#server_socket_messageObject

Returns the value of attribute server_socket_message.



55
56
57
# File 'lib/ollama_chat/server_socket.rb', line 55

def server_socket_message
  @server_socket_message
end

Class Method Details

.create_socket_server(config:) ⇒ UnixSocks::Server

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::Server 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.

containing server settings

instance ready to receive messages

Parameters:

  • config (ComplexConfig::Settings)

    the configuration object

Returns:

  • (UnixSocks::Server)

    a configured Unix domain socket server



46
47
48
49
50
51
52
# File 'lib/ollama_chat/server_socket.rb', line 46

def create_socket_server(config:)
  if runtime_dir = config.server_socket_runtime_dir
    UnixSocks::Server.new(socket_name: 'ollama_chat.sock', runtime_dir:)
  else
    UnixSocks::Server.new(socket_name: 'ollama_chat.sock')
  end
end

.send_to_server_socket(content, config:, type: :socket_input, 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.

is :socket_input_with_response, otherwise nil

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

  • 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



20
21
22
23
24
25
26
27
28
29
# File 'lib/ollama_chat/server_socket.rb', line 20

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

Instance Method Details

#init_server_socketObject

Initializes the server socket to receive messages from the Ollama Chat Client.

This method sets up a Unix domain socket server that listens for incoming messages in the background. When a message is received, it updates the instance variable ‘server_socket_message` and sends an interrupt signal to the current process in order to handle the message.



64
65
66
67
68
69
70
# File 'lib/ollama_chat/server_socket.rb', line 64

def init_server_socket
  server = OllamaChat::ServerSocket.create_socket_server(config:)
  server.receive_in_background do |message|
    self.server_socket_message = message
    Process.kill :INT, $$
  end
end