elelem-socket 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a09e719af1e6a7a4a9ca5e601a153b53754367440563d8fa2fbd06f9767a20f7
4
+ data.tar.gz: 600755353fd9a7d38d4d499f9cdf7334eb55556f5be58ea58b55512f42839cf4
5
+ SHA512:
6
+ metadata.gz: 4a4424da067bfff51d2d940653ea40b15840b5dc802aa1b203cc0f778150c7583c79b35388850f924f806a4d94b29e67007da18d7f2f23259ecafebf64f34148
7
+ data.tar.gz: 8b8d44a6c09800d0a1dcaff20b65aeba93d6051ba0157039b436540af72fc01d8ac85b93c130eac1ea4b51482c60775eb29bca4d556b6d94c462db05eac429f5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 2809074 Alberta Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ class Sock
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Elelem
6
+ class Sock
7
+ OWNER_ONLY = 0o177
8
+
9
+ def self.run(path, args, **) = args.empty? ? serve(path, **) : tell(path, args.join(" "))
10
+
11
+ def self.serve(path, provider: "default", provider_options: {}, **)
12
+ sock = new(path)
13
+ stream = Config.wrap(sock)
14
+ session = Elelem.build(input: SockInput.new(stream:), output: Output.new(stream:),
15
+ provider:, provider_options:, **)
16
+ repl = Repl.new(session, provider:, model: provider_options[:model])
17
+ sock.each_client { repl.run }
18
+ end
19
+
20
+ def self.tell(path, prompt)
21
+ UNIXSocket.open(path) do |socket|
22
+ socket.puts prompt
23
+ socket.close_write
24
+ IO.copy_stream(socket, $stdout)
25
+ end
26
+ end
27
+
28
+ def initialize(path)
29
+ @path = path
30
+ @server = listen
31
+ @clients = Queue.new
32
+ end
33
+
34
+ def gets = connected { @client.gets }
35
+ def print(text) = connected { @client.print(text) }
36
+ def redraw; end
37
+
38
+ def each_client(&)
39
+ Thread.new { loop { admit(@server.accept) } }
40
+ loop { serve(@clients.pop, &) }
41
+ ensure
42
+ File.delete(@path)
43
+ end
44
+
45
+ private
46
+
47
+ def listen
48
+ UNIXSocket.new(@path).close
49
+ raise "#{@path}: already running"
50
+ rescue Errno::ENOENT, Errno::ECONNREFUSED
51
+ File.delete(@path) if File.socket?(@path)
52
+ File.umask(OWNER_ONLY).then { |umask| UNIXServer.new(@path).tap { File.umask(umask) } }
53
+ end
54
+
55
+ def admit(client)
56
+ return @clients << client unless busy?
57
+
58
+ client.puts "busy"
59
+ client.close
60
+ rescue SystemCallError
61
+ nil
62
+ end
63
+
64
+ def busy? = !(@client.nil? || @client.closed?) || !@clients.empty?
65
+
66
+ def serve(client)
67
+ @client = client
68
+ yield
69
+ ensure
70
+ client.close
71
+ end
72
+
73
+ def connected
74
+ yield unless @client.closed?
75
+ rescue SystemCallError, IOError
76
+ @client.close
77
+ end
78
+ end
79
+
80
+ class SockInput < Input
81
+ def ask(prompt)
82
+ @stream.print(prompt)
83
+ @stream.gets&.strip.tap { |line| @stream.redraw if line == "" }
84
+ end
85
+
86
+ def interactive? = true
87
+ end
88
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+ require_relative "sock/version"
5
+ require_relative "sock"
6
+
7
+ Elelem.configure do |config|
8
+ config.option("--sock PATH", "Serve the session on a unix socket; with a prompt, ask the one there",
9
+ &Elelem::Sock.method(:run))
10
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem-socket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: elelem
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.20'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0.20'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1'
32
+ description: 'An elelem plugin that adds elelem --sock PATH: serve a session on a
33
+ Unix socket, or ask one.'
34
+ email:
35
+ - mo@mokhan.ca
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - LICENSE.txt
41
+ - lib/elelem/sock.rb
42
+ - lib/elelem/sock/version.rb
43
+ - lib/elelem/socket.rb
44
+ homepage: https://elelem.sh
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ allowed_push_host: https://rubygems.org
49
+ homepage_uri: https://elelem.sh
50
+ source_code_uri: https://github.com/elelem-sh/socket
51
+ rubygems_mfa_required: 'true'
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 4.0.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 4.0.20
67
+ specification_version: 4
68
+ summary: Serve an elelem session on a Unix socket.
69
+ test_files: []