elelem-openai 0.3.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a71f780cc76b197086104f457696d3ae8677513033cc9a2e1b0f67f2f1800c3b
4
- data.tar.gz: a5197c27bea1826e0ecb44404ceaa78d72969d4b35b2f0570ad73f89d799f206
3
+ metadata.gz: 6c4b0c206227c277a16be4e7844d33904b52cd8a757d628b0ce459d1f8126174
4
+ data.tar.gz: ca4edfcaa0e9e0e053b55a3bd6dffe4f5a095bb2c2107d301e03abc42b24571e
5
5
  SHA512:
6
- metadata.gz: 3d0c3128338ad5d20e2843754a33f1f9d169dcd76bb962f64addb604429bc28fd5628df1fdd626a5244d78408c8cb26f2ffe9327834aaeb18e8ea05e214c53cc
7
- data.tar.gz: e95da066e99367d43a5e64daf6f5438c6fa646b32bc69cd5dcbfa0f51a28f675af911d1bbe70c81546a0213f57009b9f80df9a40c582a6f544cd6bf36e1dd75f
6
+ metadata.gz: abc80c1b189c8fa86a4822c9f8c3f29d939fd72811fd4be07e9079414b8960ad226051d3ea7eb563bc807cac295f6afbaea76c3a05dbe67483eab1ae1ee563fc
7
+ data.tar.gz: 3acd01b5151829c3e240da5b8d339013aea9c7a6821df0ea3580b87bd98b7038986c258cea0316c37c012620aa7738ce97a62770da18bfb0695fe67426884a2b
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.3.1"
2
+ ".": "0.4.0"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/elelem-sh/openai/compare/v0.3.1...v0.4.0) (2026-11-19)
4
+
5
+
6
+ ### Features
7
+
8
+ * stream over a kept-alive connection that retries busy servers ([6463020](https://github.com/elelem-sh/openai/commit/6463020dc62edab070b1aa753b7ff278f3504162))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * say when a content filter stops a reply ([c02d2ec](https://github.com/elelem-sh/openai/commit/c02d2ec889c6be7f0a5cd3953df3c84f9169cd0d))
14
+ * say when a reply hits the output limit ([d8005bd](https://github.com/elelem-sh/openai/commit/d8005bdba722a44ce6492fb227d3cd087a807dda))
15
+
3
16
  ## [0.3.1](https://github.com/elelem-sh/openai/compare/v0.3.0...v0.3.1) (2026-11-18)
4
17
 
5
18
 
@@ -47,6 +47,7 @@ module Elelem
47
47
  end
48
48
 
49
49
  def handle_event(event, tool_calls, &block)
50
+ stopped(event.dig("choices", 0, "finish_reason"))
50
51
  delta = event.dig("choices", 0, "delta") || {}
51
52
 
52
53
  block.call(type: "saying", text: delta["content"]) if delta["content"]
@@ -64,31 +65,27 @@ module Elelem
64
65
  end
65
66
  end
66
67
 
67
- def stream(body, &)
68
- http.post(@uri, api_key: @api_key, body:) do |response|
69
- raise "HTTP #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
70
-
71
- read_sse_stream(response, &)
68
+ # Raises before a cut-off tool call is parsed.
69
+ def stopped(reason)
70
+ case reason
71
+ when "length" then raise "the reply hit the model's output limit before it ended"
72
+ when "content_filter" then raise "the reply was stopped by a content filter"
72
73
  end
73
74
  end
74
75
 
75
- def http
76
- @http ||= HttpAdapter.new(read_timeout: @read_timeout, open_timeout: @open_timeout)
76
+ def stream(body)
77
+ http.post(@uri, body:, headers:) do |response|
78
+ Elelem::HTTP.events(response) { |data| yield JSON.parse(data) }
79
+ end
77
80
  end
78
81
 
79
- def read_sse_stream(response)
80
- buffer = String.new
81
-
82
- response.read_body do |chunk|
83
- buffer << chunk
84
-
85
- while (index = buffer.index("\n"))
86
- line = buffer.slice!(0, index + 1).strip
87
- next unless line.start_with?("data: ") && line != "data: [DONE]"
82
+ # A local server may need no key.
83
+ def headers
84
+ @api_key.to_s.empty? ? {} : { "authorization" => "Bearer #{@api_key}" }
85
+ end
88
86
 
89
- yield JSON.parse(line.delete_prefix("data: "))
90
- end
91
- end
87
+ def http
88
+ @http ||= Elelem::HTTP.new(read_timeout: @read_timeout, open_timeout: @open_timeout)
92
89
  end
93
90
 
94
91
  def finalize_tool_calls(tool_calls, &block)
@@ -100,29 +97,6 @@ module Elelem
100
97
  }.tap { |result| block.call(result.merge(type: "doing")) }
101
98
  end
102
99
  end
103
-
104
- class HttpAdapter
105
- def initialize(read_timeout:, open_timeout:)
106
- @read_timeout = read_timeout
107
- @open_timeout = open_timeout
108
- end
109
-
110
- def post(uri, api_key:, body:, &)
111
- request = Net::HTTP::Post.new(uri)
112
- request["content-type"] = "application/json"
113
- request["authorization"] = "Bearer #{api_key}" unless api_key.to_s.empty?
114
- request.body = JSON.generate(body)
115
-
116
- http = Net::HTTP.new(uri.host, uri.port)
117
- http.use_ssl = uri.scheme == "https"
118
- http.read_timeout = @read_timeout
119
- http.open_timeout = @open_timeout
120
-
121
- http.start do |conn|
122
- conn.request(request, &)
123
- end
124
- end
125
- end
126
100
  end
127
101
  end
128
102
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Elelem
4
4
  module OpenAI
5
- VERSION = "0.3.1"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
data/lib/elelem/openai.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "elelem"
4
+ require "elelem/http"
4
5
  require "json"
5
- require "net/http"
6
6
  require "uri"
7
7
 
8
8
  require_relative "openai/version"
@@ -19,14 +19,15 @@ RSpec.describe Elelem::OpenAI::Provider do
19
19
 
20
20
  let(:http) do
21
21
  Class.new do
22
- attr_reader :body
22
+ attr_reader :body, :headers
23
23
 
24
24
  def initialize(response)
25
25
  @response = response
26
26
  end
27
27
 
28
- def post(_uri, body:, **)
28
+ def post(_uri, body:, headers: {})
29
29
  @body = body
30
+ @headers = headers
30
31
  yield @response
31
32
  end
32
33
  end.new(response)
@@ -43,6 +44,53 @@ RSpec.describe Elelem::OpenAI::Provider do
43
44
  expect(body).to eq(model: "gpt-4o", messages:, stream: true)
44
45
  end
45
46
 
47
+ it "sends the key as a bearer token" do
48
+ provider.fetch(messages, [])
49
+
50
+ expect(http.headers).to eq("authorization" => "Bearer sk-test")
51
+ end
52
+
53
+ context "without a key, as for a local server" do
54
+ let(:params) { { api_key: "" } }
55
+
56
+ it "sends no authorization header" do
57
+ provider.fetch(messages, [])
58
+
59
+ expect(http.headers).to eq({})
60
+ end
61
+ end
62
+
63
+ it "reads events split by CRLF" do
64
+ allow(response).to receive(:read_body)
65
+ .and_yield(%(data: {"choices":[{"delta":{"content":"hi"}}]}\r\n\r\ndata: [DONE]\r\n))
66
+ seen = []
67
+
68
+ provider.fetch(messages, []) { |event| seen << event }
69
+
70
+ expect(seen).to eq([{ type: "saying", text: "hi" }])
71
+ end
72
+
73
+ context "with a tool call cut off at the output limit" do
74
+ let(:events) do
75
+ [{ "choices" => [{ "delta" => { "tool_calls" => [{ "index" => 0, "id" => "1",
76
+ "function" => { "name" => "write",
77
+ "arguments" => '{"path":"a' } }] } }] },
78
+ { "choices" => [{ "delta" => {}, "finish_reason" => "length" }] }]
79
+ end
80
+
81
+ it "says so instead of failing to parse half the arguments" do
82
+ expect { provider.fetch(messages, []) {} }.to raise_error(/output limit before it ended/)
83
+ end
84
+ end
85
+
86
+ context "with a filtered reply" do
87
+ let(:events) { [{ "choices" => [{ "delta" => {}, "finish_reason" => "content_filter" }] }] }
88
+
89
+ it "raises" do
90
+ expect { provider.fetch(messages, []) {} }.to raise_error(/stopped by a content filter/)
91
+ end
92
+ end
93
+
46
94
  it "sends tools with tool_choice auto" do
47
95
  tools = [{ type: "function", function: { name: "read" } }]
48
96
  provider.fetch(messages, tools)
@@ -15,7 +15,9 @@ RSpec.describe "a tool call round trip through Elelem::Agent" do
15
15
  @requests << body
16
16
  problem = invalid(body[:messages])
17
17
  events = @requests.size == 1 ? tool_call : reply
18
- yield(problem ? bad_request(problem) : ok(events))
18
+ raise "HTTP 400: invalid tool call: #{problem}" if problem
19
+
20
+ yield ok(events)
19
21
  end
20
22
 
21
23
  private
@@ -39,12 +41,6 @@ RSpec.describe "a tool call round trip through Elelem::Agent" do
39
41
  res.define_singleton_method(:read_body) { |&block| block.call(data) }
40
42
  end
41
43
  end
42
-
43
- def bad_request(problem)
44
- Net::HTTPBadRequest.new("1.1", "400", "Bad Request").tap do |res|
45
- res.define_singleton_method(:body) { "invalid tool call: #{problem}" }
46
- end
47
- end
48
44
  end.new
49
45
  end
50
46
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elelem-openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
@@ -15,7 +15,7 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '0.13'
18
+ version: '0.16'
19
19
  - - "<"
20
20
  - !ruby/object:Gem::Version
21
21
  version: '1'
@@ -25,7 +25,7 @@ dependencies:
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: '0.13'
28
+ version: '0.16'
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '1'
@@ -43,20 +43,6 @@ dependencies:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
45
  version: '2.18'
46
- - !ruby/object:Gem::Dependency
47
- name: net-http
48
- requirement: !ruby/object:Gem::Requirement
49
- requirements:
50
- - - "~>"
51
- - !ruby/object:Gem::Version
52
- version: '0.9'
53
- type: :runtime
54
- prerelease: false
55
- version_requirements: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '0.9'
60
46
  - !ruby/object:Gem::Dependency
61
47
  name: uri
62
48
  requirement: !ruby/object:Gem::Requirement
@@ -92,7 +78,6 @@ files:
92
78
  - lib/elelem/openai/provider.rb
93
79
  - lib/elelem/openai/version.rb
94
80
  - release-please-config.json
95
- - spec/elelem/openai/http_adapter_spec.rb
96
81
  - spec/elelem/openai/provider_spec.rb
97
82
  - spec/elelem/openai/registration_spec.rb
98
83
  - spec/elelem/openai/round_trip_spec.rb
@@ -119,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
104
  - !ruby/object:Gem::Version
120
105
  version: '0'
121
106
  requirements: []
122
- rubygems_version: 4.0.6
107
+ rubygems_version: 4.0.20
123
108
  specification_version: 4
124
109
  summary: An OpenAI provider plugin for elelem.
125
110
  test_files: []
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Elelem::OpenAI::Provider::HttpAdapter do
4
- def sent_headers(api_key)
5
- seen = nil
6
- conn = double("conn").tap { |c| allow(c).to receive(:request) { |req| seen = req } }
7
- http = double("http", "use_ssl=": nil, "read_timeout=": nil, "open_timeout=": nil).tap do |h|
8
- allow(h).to receive(:start).and_yield(conn)
9
- end
10
- allow(Net::HTTP).to receive(:new).and_return(http)
11
- adapter = described_class.new(read_timeout: 1, open_timeout: 1)
12
- adapter.post(URI("http://h/v1/chat/completions"), api_key: api_key, body: {})
13
- seen
14
- end
15
-
16
- it "sends the key as a bearer token" do
17
- expect(sent_headers("sk-test")["authorization"]).to eq("Bearer sk-test")
18
- end
19
-
20
- it "sends no authorization header without a key" do
21
- expect(sent_headers("")["authorization"]).to be_nil
22
- end
23
- end