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.



31
32
33
# File 'lib/ollama_chat/server_socket.rb', line 31

def server_socket_message
  @server_socket_message
end

Class Method Details

.create_socket_server(config:) ⇒ Object



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

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) ⇒ String, NilClass



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, config:, type: :socket_input)
  server  = create_socket_server(config:)
  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.



43
44
45
46
47
48
49
# File 'lib/ollama_chat/server_socket.rb', line 43

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