elelem-openai 0.2.0 → 0.3.1

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: cceca9095beecc545592ee1b294c27e4999321d87ef78d643549df1e44bd1085
4
- data.tar.gz: 0a5877022752e8838a25fc43945ff42b147eb6680e08442deede0fd4e3fc4807
3
+ metadata.gz: a71f780cc76b197086104f457696d3ae8677513033cc9a2e1b0f67f2f1800c3b
4
+ data.tar.gz: a5197c27bea1826e0ecb44404ceaa78d72969d4b35b2f0570ad73f89d799f206
5
5
  SHA512:
6
- metadata.gz: a185926b43f063cc582b6939efa47ab958891e6609f713f7a88a0ca815156a2b2e1ab5bd4b66281f45ed19ccb42dad7f62b16f2dbdb761ef9331d30ae3523d17
7
- data.tar.gz: 3aa9f3bfecfb3fc74ff52e89b480c55429d4e94670f502575df5d9f2044687e0faef3ece3edbe9f6500a43bc717388b2999870da40433e3b62b74dfebb1cd785
6
+ metadata.gz: 3d0c3128338ad5d20e2843754a33f1f9d169dcd76bb962f64addb604429bc28fd5628df1fdd626a5244d78408c8cb26f2ffe9327834aaeb18e8ea05e214c53cc
7
+ data.tar.gz: e95da066e99367d43a5e64daf6f5438c6fa646b32bc69cd5dcbfa0f51a28f675af911d1bbe70c81546a0213f57009b9f80df9a40c582a6f544cd6bf36e1dd75f
@@ -0,0 +1,86 @@
1
+ As a `person who wants to use OpenAI or a compatible server from
2
+ elelem`, I `want a README and clear RubyGems links for elelem-openai`,
3
+ so that `I can point it at my server without reading the code and find
4
+ the docs and backlog from the gem page`.
5
+
6
+ # SYNOPSIS
7
+
8
+ Add a README, and point the gem's RubyGems links at elelem.sh and this
9
+ repository's backlog.
10
+
11
+ # DESCRIPTION
12
+
13
+ Two small changes that both shape what a newcomer sees first.
14
+
15
+ ## README
16
+
17
+ The repository has no README. It should cover:
18
+
19
+ * Install, and selecting the provider: `-p openai` and `--model NAME`.
20
+ * `OPENAI_API_KEY`, `OPENAI_MODEL` (default `gpt-4o`) and
21
+ `OPENAI_BASE_URL` (default `https://api.openai.com/v1`).
22
+ * Compatible servers: with `OPENAI_BASE_URL` set, no key is required,
23
+ so a local Chat Completions server works; an example.
24
+ * That a set but empty variable counts as unset.
25
+ * A link to https://elelem.sh/plugins/openai/ for the full guide.
26
+
27
+ The elelem.sh plugin page can be the source for the text. Show commands
28
+ without a leading `$`, so they can be pasted.
29
+
30
+ ## Gem metadata
31
+
32
+ RubyGems shows `homepage_uri` and the other `metadata` links on every
33
+ gem page. Today `elelem-openai.gemspec` sets `spec.homepage`
34
+ to the site root (https://elelem.sh) and sets only `homepage_uri`,
35
+ `source_code_uri`, `allowed_push_host` and
36
+ `rubygems_mfa_required`.
37
+ There is no documentation, bug tracker or changelog link.
38
+
39
+ Proposed change:
40
+
41
+ ```ruby
42
+ spec.homepage = "https://elelem.sh/plugins/openai/"
43
+ spec.metadata["homepage_uri"] = spec.homepage
44
+ spec.metadata["source_code_uri"] = "https://github.com/elelem-sh/openai"
45
+ spec.metadata["documentation_uri"] = spec.homepage
46
+ spec.metadata["bug_tracker_uri"] = "https://github.com/elelem-sh/openai/tree/main/.agents/backlog"
47
+ spec.metadata["changelog_uri"] = "https://github.com/elelem-sh/openai/blob/main/CHANGELOG.md"
48
+ ```
49
+
50
+ The original proposal pointed `bug_tracker_uri` at GitHub Issues on
51
+ elelem-sh/elelem.sh, filtered by label. That no longer fits: the
52
+ project is moving its backlog out of GitHub Issues and into
53
+ `.agents/backlog` story files in each repository, so the bug tracker
54
+ link points at this repository's backlog directory instead.
55
+
56
+ /man/openai/ redirects to /plugins/openai/ on elelem.sh, so a
57
+ separate documentation_uri adds nothing. Point both at the plugin page.
58
+ CHANGELOG.md exists on main, so the changelog link works today.
59
+
60
+ Keep `allowed_push_host` and `rubygems_mfa_required` as they are.
61
+ RubyGems only shows the new links after the next release.
62
+
63
+ # SEE ALSO
64
+
65
+ * [ ] lib/elelem/openai.rb:11-27 - provider block and environment
66
+ * [ ] exe/elelem-openai
67
+ * [ ] elelem-openai.gemspec:13 - spec.homepage and metadata
68
+ * [ ] https://elelem.sh/plugins/openai/ - source for README text
69
+ * [ ] Formerly elelem-sh/elelem.sh#23
70
+ * [ ] Formerly elelem-sh/elelem.sh#41
71
+
72
+ # Tasks
73
+
74
+ * [ ] TBD (filled in design mode)
75
+
76
+ # Acceptance Criteria
77
+
78
+ * [ ] README.md exists and covers the points above
79
+ * [ ] Defaults in the README match the code
80
+ * [ ] Commands in the README have no leading `$`
81
+ * [ ] `spec.homepage` and `homepage_uri` are https://elelem.sh/plugins/openai/
82
+ * [ ] `bug_tracker_uri` points at this repository's `.agents/backlog`,
83
+ not at GitHub Issues
84
+ * [ ] `changelog_uri` and `source_code_uri` point at this repository
85
+ * [ ] `gem build` succeeds and `gem spec` on the built gem shows every
86
+ link
@@ -0,0 +1,63 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ workflow_dispatch:
7
+
8
+ permissions: {}
9
+
10
+ concurrency:
11
+ group: release
12
+ cancel-in-progress: false
13
+
14
+ jobs:
15
+ test:
16
+ runs-on: ubuntu-latest
17
+ permissions:
18
+ contents: read
19
+ steps:
20
+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
21
+ with:
22
+ persist-credentials: false
23
+ - uses: ruby/setup-ruby@3ff19f5e2baf30647122352b96108b1fbe250c64 # v1.299.0
24
+ with:
25
+ ruby-version: "4.0"
26
+ bundler-cache: true
27
+ - run: bin/test
28
+ - run: bin/lint
29
+
30
+ release-please:
31
+ needs: test
32
+ runs-on: ubuntu-latest
33
+ permissions:
34
+ contents: write
35
+ issues: write
36
+ pull-requests: write
37
+ outputs:
38
+ release_created: ${{ steps.release.outputs.release_created }}
39
+ steps:
40
+ - id: release
41
+ uses: googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5.0.0
42
+ with:
43
+ config-file: release-please-config.json
44
+ manifest-file: .release-please-manifest.json
45
+
46
+ publish:
47
+ needs: release-please
48
+ if: needs.release-please.outputs.release_created == 'true'
49
+ runs-on: ubuntu-latest
50
+ permissions:
51
+ contents: write
52
+ id-token: write
53
+ steps:
54
+ - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
55
+ with:
56
+ persist-credentials: false
57
+ - uses: ruby/setup-ruby@3ff19f5e2baf30647122352b96108b1fbe250c64 # v1.299.0
58
+ with:
59
+ ruby-version: "4.0"
60
+ bundler-cache: true
61
+ - uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
62
+ with:
63
+ await-release: false
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "0.3.1"
3
+ }
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,62 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 4.0
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - "pkg/**/*"
7
+ - "bin/**/*"
8
+ - "vendor/**/*"
9
+ - "fixtures/**/*"
10
+
11
+ Metrics:
12
+ Enabled: false
13
+
14
+ Naming/PredicateMethod:
15
+ AllowedMethods:
16
+ - call
17
+ - run
18
+ - summarize
19
+ - prompt
20
+
21
+ Naming/MethodParameterName:
22
+ AllowedNames:
23
+ - me
24
+ - id
25
+ - as
26
+ - io
27
+
28
+ Style/GlobalVars:
29
+ Exclude:
30
+ - "spec/**/*"
31
+
32
+ Lint/EmptyBlock:
33
+ Exclude:
34
+ - "spec/**/*"
35
+
36
+ Lint/UselessDefaultValueArgument:
37
+ Enabled: false
38
+
39
+ Lint/RedundantRequireStatement:
40
+ Enabled: false
41
+
42
+ Lint/UnusedBlockArgument:
43
+ IgnoreEmptyBlocks: true
44
+ AllowUnusedKeywordArguments: true
45
+
46
+ Style/Documentation:
47
+ Enabled: false
48
+
49
+ Style/FormatStringToken:
50
+ Enabled: false
51
+
52
+ Style/FrozenStringLiteralComment:
53
+ Enabled: true
54
+
55
+ Style/StringLiterals:
56
+ EnforcedStyle: double_quotes
57
+
58
+ Style/StringLiteralsInInterpolation:
59
+ EnforcedStyle: double_quotes
60
+
61
+ Layout/LineLength:
62
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,25 @@
1
+ # Changelog
2
+
3
+ ## [0.3.1](https://github.com/elelem-sh/openai/compare/v0.3.0...v0.3.1) (2026-11-18)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **deps:** accept the json that Ruby 4.0 ships ([3c856d4](https://github.com/elelem-sh/openai/commit/3c856d43e90bdcde09134d512bf60d4ba9db907e))
9
+
10
+ ## [0.3.0](https://github.com/elelem-sh/openai/compare/v0.2.0...v0.3.0) (2026-11-16)
11
+
12
+
13
+ ### Features
14
+
15
+ * accept model: from provider_options, fall back to ENV ([3f0360c](https://github.com/elelem-sh/openai/commit/3f0360c6ced4bbfaa21e9aa97cd79161ae01fdd4))
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * add rspec to Gemfile so bundle exec rspec runs at all ([15cbcc0](https://github.com/elelem-sh/openai/commit/15cbcc0d85b61c820b87d0be77ed86aa77a34ee6))
21
+ * empty OpenAI model and key settings count as unset, and a missing key fails at start ([c443200](https://github.com/elelem-sh/openai/commit/c44320031cfeb97155071601e9f0f021a0c5c647))
22
+ * OPENAI_BASE_URL points elelem-openai at any Chat Completions server (elelem-sh/elelem.sh[#26](https://github.com/elelem-sh/openai/issues/26)) ([387e821](https://github.com/elelem-sh/openai/commit/387e8219f38fb16b30e0883a71e3a3de0663558f))
23
+ * OPENAI_BASE_URL takes a trailing slash, an empty value is unset, and no key sends no header ([5dbb125](https://github.com/elelem-sh/openai/commit/5dbb125cf9119406745e945090a2502e0409f0c7))
24
+ * require elelem ~> 0.12, the floor builtins/skills actually need ([d9e5880](https://github.com/elelem-sh/openai/commit/d9e588035a36a0122014695b8d8cd874048de63a))
25
+ * send earlier tool calls in the Chat Completions shape and omit empty tools (elelem-sh/elelem.sh[#26](https://github.com/elelem-sh/openai/issues/26)) ([9a51d90](https://github.com/elelem-sh/openai/commit/9a51d90cb687f292a089a67a2180abc818515f48))
@@ -3,29 +3,47 @@
3
3
  module Elelem
4
4
  module OpenAI
5
5
  class Provider
6
- def initialize(model:, api_key:, base_url: "https://api.openai.com/v1", read_timeout: 3600, open_timeout: 10)
7
- @uri = URI.join("#{base_url}/", "chat/completions")
6
+ def initialize(model:, api_key:, base_url: "https://api.openai.com/v1", read_timeout: 3600, open_timeout: 10,
7
+ http: nil)
8
+ @uri = URI.join("#{base_url.chomp("/")}/", "chat/completions")
8
9
  @model = model
9
10
  @api_key = api_key
10
11
  @read_timeout = read_timeout
11
12
  @open_timeout = open_timeout
13
+ @http = http
12
14
  end
13
15
 
14
- def fetch(messages, tools = [], &block)
16
+ def fetch(messages, tools = [], &)
15
17
  tool_calls = {}
16
18
  body = build_request_body(messages, tools)
17
19
 
18
20
  stream(body) do |event|
19
- handle_event(event, tool_calls, &block)
21
+ handle_event(event, tool_calls, &)
20
22
  end
21
23
 
22
- finalize_tool_calls(tool_calls, &block)
24
+ finalize_tool_calls(tool_calls, &)
23
25
  end
24
26
 
25
27
  private
26
28
 
27
29
  def build_request_body(messages, tools)
28
- { model: @model, messages:, stream: true, tools:, tool_choice: "auto" }
30
+ body = { model: @model, messages: messages.map { |m| normalize(m) }, stream: true }
31
+ tools.empty? ? body : body.merge(tools:, tool_choice: "auto")
32
+ end
33
+
34
+ # elelem keeps tool calls as {id, name, arguments: Hash}. Chat Completions
35
+ # wants {id, type: "function", function: {name, arguments: "<json>"}}.
36
+ def normalize(message)
37
+ calls = message[:tool_calls]
38
+ return message unless calls
39
+
40
+ message.merge(tool_calls: calls.map { |call| to_openai(call) })
41
+ end
42
+
43
+ def to_openai(call)
44
+ arguments = call[:arguments]
45
+ arguments = JSON.generate(arguments || {}) unless arguments.is_a?(String)
46
+ { id: call[:id], type: "function", function: { name: call[:name], arguments: } }
29
47
  end
30
48
 
31
49
  def handle_event(event, tool_calls, &block)
@@ -46,26 +64,18 @@ module Elelem
46
64
  end
47
65
  end
48
66
 
49
- def stream(body)
50
- request = Net::HTTP::Post.new(@uri)
51
- request["content-type"] = "application/json"
52
- request["authorization"] = "Bearer #{@api_key}"
53
- request.body = JSON.generate(body)
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)
54
70
 
55
- http = Net::HTTP.new(@uri.host, @uri.port)
56
- http.use_ssl = @uri.scheme == "https"
57
- http.read_timeout = @read_timeout
58
- http.open_timeout = @open_timeout
59
-
60
- http.start do |conn|
61
- conn.request(request) do |response|
62
- raise "HTTP #{response.code}: #{response.body}" unless response.is_a?(Net::HTTPSuccess)
63
-
64
- read_sse_stream(response) { |event| yield event }
65
- end
71
+ read_sse_stream(response, &)
66
72
  end
67
73
  end
68
74
 
75
+ def http
76
+ @http ||= HttpAdapter.new(read_timeout: @read_timeout, open_timeout: @open_timeout)
77
+ end
78
+
69
79
  def read_sse_stream(response)
70
80
  buffer = String.new
71
81
 
@@ -86,10 +96,33 @@ module Elelem
86
96
  {
87
97
  id: tool_call[:id],
88
98
  name: tool_call[:name],
89
- arguments: JSON.parse(tool_call[:args])
99
+ arguments: tool_call[:args].strip.empty? ? {} : JSON.parse(tool_call[:args])
90
100
  }.tap { |result| block.call(result.merge(type: "doing")) }
91
101
  end
92
102
  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
93
126
  end
94
127
  end
95
128
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Elelem
4
4
  module OpenAI
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.1"
6
6
  end
7
7
  end
data/lib/elelem/openai.rb CHANGED
@@ -9,10 +9,19 @@ require_relative "openai/version"
9
9
  require_relative "openai/provider"
10
10
 
11
11
  Elelem.configure do |config|
12
- config.provider(:openai) do
12
+ # OPENAI_BASE_URL points at any Chat Completions server, such as a local
13
+ # one that needs no key.
14
+ config.provider(:openai) do |model: nil, **|
15
+ # A variable that is set but empty counts as unset.
16
+ env = ->(name) { ENV[name].to_s.strip.then { |v| v.empty? ? nil : v } }
17
+ custom = env.call("OPENAI_BASE_URL")
18
+ key = env.call("OPENAI_API_KEY")
19
+ raise KeyError, "OPENAI_API_KEY is not set" if key.nil? && custom.nil?
20
+
13
21
  Elelem::OpenAI::Provider.new(
14
- model: ENV.fetch("OPENAI_MODEL", "gpt-4o"),
15
- api_key: ENV.fetch("OPENAI_API_KEY")
22
+ model: model || env.call("OPENAI_MODEL") || "gpt-4o",
23
+ api_key: key.to_s,
24
+ base_url: custom || "https://api.openai.com/v1"
16
25
  )
17
26
  end
18
27
  end
@@ -0,0 +1,12 @@
1
+ {
2
+ "bootstrap-sha": "2f7154ed8f9f02512e4375fb413cb1db9600b036",
3
+ "packages": {
4
+ ".": {
5
+ "release-type": "ruby",
6
+ "package-name": "elelem-openai",
7
+ "version-file": "lib/elelem/openai/version.rb",
8
+ "bump-minor-pre-major": true,
9
+ "include-component-in-tag": false
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Elelem::OpenAI::Provider do
4
+ subject(:provider) do
5
+ described_class.new(model: "gpt-4o", api_key: "sk-test", http:, **params)
6
+ end
7
+
8
+ let(:params) { {} }
9
+ let(:messages) { [{ role: "user", content: "hi" }] }
10
+ let(:body) { http.body }
11
+
12
+ let(:response) do
13
+ Net::HTTPOK.new("1.1", "200", "OK").tap do |res|
14
+ allow(res).to receive(:read_body).and_yield(sse(events))
15
+ end
16
+ end
17
+
18
+ let(:events) { [] }
19
+
20
+ let(:http) do
21
+ Class.new do
22
+ attr_reader :body
23
+
24
+ def initialize(response)
25
+ @response = response
26
+ end
27
+
28
+ def post(_uri, body:, **)
29
+ @body = body
30
+ yield @response
31
+ end
32
+ end.new(response)
33
+ end
34
+
35
+ def sse(events)
36
+ events.map { |event| "data: #{JSON.generate(event)}\n" }.join << "data: [DONE]\n"
37
+ end
38
+
39
+ describe "#fetch" do
40
+ it "leaves out tools and tool_choice when there are no tools" do
41
+ provider.fetch(messages, [])
42
+
43
+ expect(body).to eq(model: "gpt-4o", messages:, stream: true)
44
+ end
45
+
46
+ it "sends tools with tool_choice auto" do
47
+ tools = [{ type: "function", function: { name: "read" } }]
48
+ provider.fetch(messages, tools)
49
+
50
+ expect(body).to include(tools:, tool_choice: "auto")
51
+ end
52
+
53
+ it "sends earlier tool calls in the Chat Completions shape" do
54
+ history = messages + [
55
+ { role: "assistant", content: "", tool_calls: [{ id: "1", name: "read", arguments: { "path" => "a.rb" } }] },
56
+ { role: "tool", tool_call_id: "1", content: "{}" }
57
+ ]
58
+ provider.fetch(history, [])
59
+
60
+ expect(body[:messages][1][:tool_calls]).to eq(
61
+ [{ id: "1", type: "function", function: { name: "read", arguments: '{"path":"a.rb"}' } }]
62
+ )
63
+ expect(body[:messages][2]).to eq(history[2])
64
+ end
65
+
66
+ context "with a text delta" do
67
+ let(:events) { [{ "choices" => [{ "delta" => { "content" => "hi" } }] }] }
68
+
69
+ it "yields a saying block" do
70
+ seen = []
71
+
72
+ provider.fetch(messages, []) { |event| seen << event }
73
+
74
+ expect(seen).to eq([{ type: "saying", text: "hi" }])
75
+ end
76
+ end
77
+
78
+ context "with a streamed tool call split across chunks" do
79
+ let(:first_chunk) do
80
+ { "index" => 0, "id" => "1", "function" => { "name" => "read", "arguments" => '{"path":' } }
81
+ end
82
+ let(:second_chunk) { { "index" => 0, "function" => { "arguments" => '"a.rb"}' } } }
83
+
84
+ let(:events) do
85
+ [
86
+ { "choices" => [{ "delta" => { "tool_calls" => [first_chunk] } }] },
87
+ { "choices" => [{ "delta" => { "tool_calls" => [second_chunk] } }] }
88
+ ]
89
+ end
90
+
91
+ it "accumulates arguments across chunks into parsed json" do
92
+ result = provider.fetch(messages, []) {}
93
+
94
+ expect(result).to eq([{ id: "1", name: "read", arguments: { "path" => "a.rb" } }])
95
+ end
96
+ end
97
+
98
+ context "with a tool call that has no arguments" do
99
+ let(:events) do
100
+ [{ "choices" => [{ "delta" => { "tool_calls" => [{ "index" => 0, "id" => "1",
101
+ "function" => { "name" => "read" } }] } }] }]
102
+ end
103
+
104
+ it "takes them as an empty hash" do
105
+ expect(provider.fetch(messages, []) {}).to eq([{ id: "1", name: "read", arguments: {} }])
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe "the openai provider" do
4
+ around do |example|
5
+ saved = ENV.to_h.slice("OPENAI_API_KEY", "OPENAI_BASE_URL", "OPENAI_MODEL")
6
+ example.run
7
+ ensure
8
+ %w[OPENAI_API_KEY OPENAI_BASE_URL OPENAI_MODEL].each { |k| ENV.delete(k) }
9
+ ENV.update(saved)
10
+ end
11
+
12
+ def uri = Elelem::Config.build_provider("openai").instance_variable_get(:@uri).to_s
13
+
14
+ it "talks to OpenAI by default" do
15
+ ENV["OPENAI_API_KEY"] = "sk-test"
16
+ ENV.delete("OPENAI_BASE_URL")
17
+
18
+ expect(uri).to eq("https://api.openai.com/v1/chat/completions")
19
+ end
20
+
21
+ it "uses OPENAI_BASE_URL, with no key needed" do
22
+ ENV.delete("OPENAI_API_KEY")
23
+ ENV["OPENAI_BASE_URL"] = "http://127.0.0.1:8080/v1"
24
+
25
+ expect(uri).to eq("http://127.0.0.1:8080/v1/chat/completions")
26
+ end
27
+
28
+ it "takes a base URL with a trailing slash, and treats an empty one as unset" do
29
+ ENV["OPENAI_API_KEY"] = "sk-test"
30
+ ENV["OPENAI_BASE_URL"] = "http://127.0.0.1:8080/v1/"
31
+ expect(uri).to eq("http://127.0.0.1:8080/v1/chat/completions")
32
+
33
+ ENV["OPENAI_BASE_URL"] = ""
34
+ expect(uri).to eq("https://api.openai.com/v1/chat/completions")
35
+ end
36
+
37
+ it "treats an empty model as unset, and an empty key as missing for OpenAI" do
38
+ ENV.delete("OPENAI_BASE_URL")
39
+ ENV["OPENAI_MODEL"] = ""
40
+ ENV["OPENAI_API_KEY"] = "sk-test"
41
+ expect(Elelem::Config.build_provider("openai").instance_variable_get(:@model)).to eq("gpt-4o")
42
+
43
+ ENV["OPENAI_API_KEY"] = ""
44
+ expect { Elelem::Config.build_provider("openai") }.to raise_error(KeyError, /OPENAI_API_KEY/)
45
+ end
46
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A fake Chat Completions server that rejects a history in the wrong shape,
4
+ # the way the API does with a 400.
5
+ RSpec.describe "a tool call round trip through Elelem::Agent" do
6
+ let(:server) do
7
+ Class.new do
8
+ attr_reader :requests
9
+
10
+ def initialize
11
+ @requests = []
12
+ end
13
+
14
+ def post(_uri, body:, **)
15
+ @requests << body
16
+ problem = invalid(body[:messages])
17
+ events = @requests.size == 1 ? tool_call : reply
18
+ yield(problem ? bad_request(problem) : ok(events))
19
+ end
20
+
21
+ private
22
+
23
+ def invalid(messages)
24
+ messages.flat_map { |m| m[:tool_calls] || [] }.find do |call|
25
+ call[:type] != "function" || !call.dig(:function, :arguments).is_a?(String)
26
+ end
27
+ end
28
+
29
+ def tool_call
30
+ call = { "index" => 0, "id" => "c1", "function" => { "name" => "now", "arguments" => "{}" } }
31
+ [{ "choices" => [{ "delta" => { "tool_calls" => [call] } }] }]
32
+ end
33
+
34
+ def reply = [{ "choices" => [{ "delta" => { "content" => "It is noon." } }] }]
35
+
36
+ def ok(events)
37
+ Net::HTTPOK.new("1.1", "200", "OK").tap do |res|
38
+ data = events.map { |e| "data: #{JSON.generate(e)}\n" }.join << "data: [DONE]\n"
39
+ res.define_singleton_method(:read_body) { |&block| block.call(data) }
40
+ end
41
+ 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
+ end.new
49
+ end
50
+
51
+ it "calls the tool, sends its result back, and gets the reply" do
52
+ toolbox = Elelem::Toolbox.new
53
+ toolbox.add("now", description: "Time", params: {}) { { time: "noon" } }
54
+ provider = Elelem::OpenAI::Provider.new(model: "gpt-4o", api_key: "sk-test", http: server)
55
+
56
+ reply = Elelem::Agent.new(provider, toolbox: toolbox, output: Elelem::NullOutput.new).turn("What time is it?")
57
+
58
+ expect(reply).to eq("It is noon.")
59
+ expect(server.requests.size).to eq(2)
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/elelem/openai"
4
+
5
+ RSpec.configure do |config|
6
+ config.disable_monkey_patching!
7
+
8
+ config.expect_with :rspec do |c|
9
+ c.syntax = :expect
10
+ end
11
+ end
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.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
@@ -13,30 +13,36 @@ dependencies:
13
13
  name: elelem
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "~>"
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.13'
19
+ - - "<"
17
20
  - !ruby/object:Gem::Version
18
- version: '0.11'
21
+ version: '1'
19
22
  type: :runtime
20
23
  prerelease: false
21
24
  version_requirements: !ruby/object:Gem::Requirement
22
25
  requirements:
23
- - - "~>"
26
+ - - ">="
24
27
  - !ruby/object:Gem::Version
25
- version: '0.11'
28
+ version: '0.13'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1'
26
32
  - !ruby/object:Gem::Dependency
27
33
  name: json
28
34
  requirement: !ruby/object:Gem::Requirement
29
35
  requirements:
30
- - - "~>"
36
+ - - ">="
31
37
  - !ruby/object:Gem::Version
32
- version: '3.0'
38
+ version: '2.18'
33
39
  type: :runtime
34
40
  prerelease: false
35
41
  version_requirements: !ruby/object:Gem::Requirement
36
42
  requirements:
37
- - - "~>"
43
+ - - ">="
38
44
  - !ruby/object:Gem::Version
39
- version: '3.0'
45
+ version: '2.18'
40
46
  - !ruby/object:Gem::Dependency
41
47
  name: net-http
42
48
  requirement: !ruby/object:Gem::Requirement
@@ -73,19 +79,32 @@ executables:
73
79
  extensions: []
74
80
  extra_rdoc_files: []
75
81
  files:
82
+ - ".agents/backlog/001-readme-and-gem-metadata.md"
83
+ - ".github/workflows/release.yml"
84
+ - ".release-please-manifest.json"
85
+ - ".rspec"
86
+ - ".rubocop.yml"
87
+ - CHANGELOG.md
76
88
  - LICENSE.txt
77
89
  - Rakefile
78
90
  - exe/elelem-openai
79
91
  - lib/elelem/openai.rb
80
92
  - lib/elelem/openai/provider.rb
81
93
  - lib/elelem/openai/version.rb
82
- homepage: https://src.mokhan.ca/elelem/openai
94
+ - release-please-config.json
95
+ - spec/elelem/openai/http_adapter_spec.rb
96
+ - spec/elelem/openai/provider_spec.rb
97
+ - spec/elelem/openai/registration_spec.rb
98
+ - spec/elelem/openai/round_trip_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://elelem.sh
83
101
  licenses:
84
102
  - MIT
85
103
  metadata:
86
104
  allowed_push_host: https://rubygems.org
87
- homepage_uri: https://src.mokhan.ca/elelem/openai
88
- source_code_uri: https://src.mokhan.ca/elelem/openai
105
+ homepage_uri: https://elelem.sh
106
+ source_code_uri: https://github.com/elelem-sh/openai
107
+ rubygems_mfa_required: 'true'
89
108
  rdoc_options: []
90
109
  require_paths:
91
110
  - lib
@@ -100,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
119
  - !ruby/object:Gem::Version
101
120
  version: '0'
102
121
  requirements: []
103
- rubygems_version: 4.0.20
122
+ rubygems_version: 4.0.6
104
123
  specification_version: 4
105
124
  summary: An OpenAI provider plugin for elelem.
106
125
  test_files: []