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.



23
24
25
# File 'lib/ollama_chat/server_socket.rb', line 23

def server_socket_message
  @server_socket_message
end

Class Method Details

.send_to_server_socket(content, type: :socket_input) ⇒ String, NilClass

Returns the response from the server if type is :socket_input_with_response, otherwise nil.

Parameters:

  • content (String)

    the message to be sent to the server

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

    the type of message being sent (default: :socket_input)

Returns:

  • (String, NilClass)

    the response from the server if type is :socket_input_with_response, otherwise nil.



11
12
13
14
15
16
17
18
19
20
# File 'lib/ollama_chat/server_socket.rb', line 11

def send_to_server_socket(content, type: :socket_input)
  server = UnixSocks::Server.new(socket_name: 'ollama_chat.sock')
  message = { content:, type: }
  if type.to_sym == :socket_input_with_response
     return server.transmit_with_response(message)
  else
     server.transmit(message)
     nil
  end
end

Instance Method Details

#init_server_socketnil

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.

server socket and kills the process when a message is received.

Returns:

  • (nil)

    This method does not return any value, it only sets up the



35
36
37
38
39
40
41
# File 'lib/ollama_chat/server_socket.rb', line 35

def init_server_socket
  server = UnixSocks::Server.new(socket_name: 'ollama_chat.sock')
  server.receive_in_background do |message|
    self.server_socket_message = message
    Process.kill :INT, $$
  end
end