elelem 0.15.1 → 0.16.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 +4 -4
- data/lib/elelem/http.rb +117 -0
- data/lib/elelem/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1e3cfcb21c78636bd29c5fbd9d368cd88dce8cebd8bcaafd21e530a1c1b824b9
|
|
4
|
+
data.tar.gz: 9612a8d27ede962f987ba48c53fc78291ec58e6e8ae4ba234c2208540a56a39f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8025cd751e132baea4e69b6e7500448cad02e7919014d3abd978baf25e7627c550182d7d65723d536c8499d3e6eed10bdacf96d454cf53329177803947410fd8
|
|
7
|
+
data.tar.gz: b997524ac9643b3ba5fb0285a506dda0595700c848da9db65343e3fef9b4ba105677606b7f1da162ddb8cf87f7c005df317af8c01f5fbecdc55672af286655bd
|
data/lib/elelem/http.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
|
|
6
|
+
module Elelem
|
|
7
|
+
# A keep-alive client for providers that stream their replies.
|
|
8
|
+
# Before a reply arrives it retries busy servers and broken connections.
|
|
9
|
+
# Once the caller has the reply it never retries, so no output repeats.
|
|
10
|
+
# One request at a time: give each provider its own client.
|
|
11
|
+
class HTTP
|
|
12
|
+
BROKEN = [Net::OpenTimeout, Errno::ECONNRESET, Errno::EPIPE, EOFError].freeze
|
|
13
|
+
BUSY = %w[429 500 502 503 504 529].freeze
|
|
14
|
+
KEEP_ALIVE = 30
|
|
15
|
+
LONGEST_WAIT = 30
|
|
16
|
+
|
|
17
|
+
class Busy < StandardError
|
|
18
|
+
attr_reader :wait
|
|
19
|
+
|
|
20
|
+
def initialize(reply, wait)
|
|
21
|
+
@wait = wait
|
|
22
|
+
super("HTTP #{reply.code}: #{reply.body}")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.lines(response, &)
|
|
27
|
+
rest = response_lines(response, &)
|
|
28
|
+
complete_lines("#{rest}\n", &)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The data of each server-sent event.
|
|
32
|
+
def self.events(response)
|
|
33
|
+
lines(response) do |line|
|
|
34
|
+
data = line.delete_prefix("data:").strip if line.start_with?("data:")
|
|
35
|
+
yield data unless data.nil? || data.empty? || data == "[DONE]"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.response_lines(response, &)
|
|
40
|
+
rest = +""
|
|
41
|
+
response.read_body { |chunk| rest = complete_lines(rest + chunk, &) }
|
|
42
|
+
rest
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Yields each complete, non-blank line and returns the incomplete rest.
|
|
46
|
+
def self.complete_lines(text)
|
|
47
|
+
complete, _, rest = text.rpartition("\n")
|
|
48
|
+
complete.split("\n").map(&:strip).each { |line| yield line unless line.empty? }
|
|
49
|
+
rest
|
|
50
|
+
end
|
|
51
|
+
private_class_method :response_lines, :complete_lines
|
|
52
|
+
|
|
53
|
+
def initialize(read_timeout: 3600, open_timeout: 10, retries: 2)
|
|
54
|
+
@options = { read_timeout:, open_timeout:, keep_alive_timeout: KEEP_ALIVE }
|
|
55
|
+
@retries = retries
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def post(uri, body:, headers: {}, &)
|
|
59
|
+
request = Net::HTTP::Post.new(uri, { "content-type" => "application/json", **headers })
|
|
60
|
+
request.body = JSON.generate(body)
|
|
61
|
+
deliver(uri, request, 0, &)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def deliver(uri, request, attempt, &)
|
|
67
|
+
exchange(uri, request, attempt, &)
|
|
68
|
+
rescue Busy, *BROKEN => e
|
|
69
|
+
raise if @replied || attempt >= @retries
|
|
70
|
+
|
|
71
|
+
pause(e.is_a?(Busy) ? e.wait : 2**attempt, e)
|
|
72
|
+
deliver(uri, request, attempt + 1, &)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def exchange(uri, request, attempt, &)
|
|
76
|
+
@replied = false
|
|
77
|
+
response = connection(uri).request(request) { |reply| hand_over(reply, attempt, &) }
|
|
78
|
+
ensure
|
|
79
|
+
disconnect unless response
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def hand_over(reply, attempt)
|
|
83
|
+
check(reply, attempt)
|
|
84
|
+
@replied = true
|
|
85
|
+
yield reply
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def check(reply, attempt)
|
|
89
|
+
return if reply.is_a?(Net::HTTPSuccess)
|
|
90
|
+
raise Busy.new(reply, wait_for(reply, attempt)) if BUSY.include?(reply.code) && attempt < @retries
|
|
91
|
+
|
|
92
|
+
raise "HTTP #{reply.code}: #{reply.body}"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def wait_for(reply, attempt)
|
|
96
|
+
seconds = Float(reply["retry-after"], exception: false) || (2**attempt)
|
|
97
|
+
raise "HTTP #{reply.code}: busy for #{seconds}s, try again later" if seconds > LONGEST_WAIT
|
|
98
|
+
|
|
99
|
+
seconds
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def pause(seconds, error)
|
|
103
|
+
Elelem.logger.warn("http: #{error.message}; retrying in #{seconds}s")
|
|
104
|
+
sleep(seconds)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def connection(uri)
|
|
108
|
+
disconnect unless @connection&.started? && [@connection.address, @connection.port] == [uri.host, uri.port]
|
|
109
|
+
@connection ||= Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", **@options)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def disconnect
|
|
113
|
+
@connection.finish if @connection&.started?
|
|
114
|
+
@connection = nil
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
data/lib/elelem/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: elelem
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.16.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- mo khan
|
|
@@ -93,6 +93,20 @@ dependencies:
|
|
|
93
93
|
- - "~>"
|
|
94
94
|
- !ruby/object:Gem::Version
|
|
95
95
|
version: '1.7'
|
|
96
|
+
- !ruby/object:Gem::Dependency
|
|
97
|
+
name: net-http
|
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - "~>"
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0.9'
|
|
103
|
+
type: :runtime
|
|
104
|
+
prerelease: false
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - "~>"
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0.9'
|
|
96
110
|
- !ruby/object:Gem::Dependency
|
|
97
111
|
name: open3
|
|
98
112
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -198,6 +212,7 @@ files:
|
|
|
198
212
|
- lib/elelem/commands.rb
|
|
199
213
|
- lib/elelem/config.rb
|
|
200
214
|
- lib/elelem/conversation.rb
|
|
215
|
+
- lib/elelem/http.rb
|
|
201
216
|
- lib/elelem/input.rb
|
|
202
217
|
- lib/elelem/null_input.rb
|
|
203
218
|
- lib/elelem/null_output.rb
|
|
@@ -246,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
246
261
|
- !ruby/object:Gem::Version
|
|
247
262
|
version: 4.0.0
|
|
248
263
|
requirements: []
|
|
249
|
-
rubygems_version: 4.0.
|
|
264
|
+
rubygems_version: 4.0.20
|
|
250
265
|
specification_version: 4
|
|
251
266
|
summary: A minimal coding harness for LLMs.
|
|
252
267
|
test_files: []
|