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.
Instance Attribute Summary collapse
-
#server_socket_message ⇒ Object?
The server_socket_message accessor method provides read and write access to the server socket message instance variable.
Class Method Summary collapse
-
.create_socket_server(config:, runtime_dir: nil, working_dir: nil) ⇒ UnixSocks::Server
The create_socket_server method constructs and returns a Unix domain socket server instance for communication with the Ollama Chat client.
-
.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.
Instance Method Summary collapse
-
#init_server_socket ⇒ Object
Initializes the server socket to receive messages from the Ollama Chat Client.
Instance Attribute Details
#server_socket_message ⇒ Object?
The server_socket_message accessor method provides read and write access to the server socket message instance variable.
not set
94 95 96 |
# File 'lib/ollama_chat/server_socket.rb', line 94 def end |
Class Method Details
.create_socket_server(config:, runtime_dir: nil, working_dir: nil) ⇒ 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
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ollama_chat/server_socket.rb', line 74 def create_socket_server(config:, runtime_dir: nil, working_dir: nil) working_dir ||= Dir.pwd if runtime_dir return UnixSocks::Server.new(socket_name: 'ollama_chat.sock', runtime_dir:) end if config.working_dir_dependent_socket path = File.(working_dir) digest = Digest::MD5.hexdigest(path) UnixSocks::Server.new(socket_name: "ollama_chat-#{digest}.sock") else UnixSocks::Server.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.
is :socket_input_with_response, otherwise nil
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ollama_chat/server_socket.rb', line 46 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:) = { content:, type:, parse: } if type.to_sym == :socket_input_with_response server.transmit_with_response() else server.transmit() nil end end |
Instance Method Details
#init_server_socket ⇒ Object
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.
103 104 105 106 107 108 109 |
# File 'lib/ollama_chat/server_socket.rb', line 103 def init_server_socket server = OllamaChat::ServerSocket.create_socket_server(config:) server.receive_in_background do || self. = Process.kill :INT, $$ end end |