elelem-openai 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: c2511ba39bea276751dfc81eceaf8094b605c622c3efbc90a724d122a71633ec
4
+ data.tar.gz: dc6766dbbc65c1f3a8d1bbbe243fec6ee8be484f20d68c34e81d95ef1e627fc6
5
+ SHA512:
6
+ metadata.gz: cf64f3f829b8a3f1ee2accdd07ab46300b977f443c4e16280c051923101835e9877acccda4a683e552ce60ff00ec34f86b5da13123bd8d8447015cd0978cf021
7
+ data.tar.gz: 9ab07797af1de29d9db456781e0e83bd028de1cc00ff103031481a97cb32aa368266517173539f071cb7b503e0088bcf0e7888b114b6e11d12ea619f94efb775
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 mo khan
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.
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/exe/elelem-openai ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "elelem/cli"
5
+
6
+ Signal.trap("INT") { exit 1 }
7
+
8
+ Elelem::CLI.new(ARGV, provider: "openai").run
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module OpenAI
5
+ class Client
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")
8
+ @model = model
9
+ @api_key = api_key
10
+ @read_timeout = read_timeout
11
+ @open_timeout = open_timeout
12
+ end
13
+
14
+ def fetch(messages, tools = [], &block)
15
+ tool_calls = {}
16
+ body = build_request_body(messages, tools)
17
+
18
+ stream(body) do |event|
19
+ handle_event(event, tool_calls, &block)
20
+ end
21
+
22
+ finalize_tool_calls(tool_calls, &block)
23
+ end
24
+
25
+ private
26
+
27
+ def build_request_body(messages, tools)
28
+ { model: @model, messages:, stream: true, tools:, tool_choice: "auto" }
29
+ end
30
+
31
+ def handle_event(event, tool_calls, &block)
32
+ delta = event.dig("choices", 0, "delta") || {}
33
+
34
+ block.call(type: "saying", text: delta["content"]) if delta["content"]
35
+
36
+ accumulate_tool_calls(delta["tool_calls"], tool_calls) if delta["tool_calls"]
37
+ end
38
+
39
+ def accumulate_tool_calls(incoming_tool_calls, tool_calls)
40
+ incoming_tool_calls.each do |tool_call|
41
+ index = tool_call["index"]
42
+ tool_calls[index] ||= { id: nil, name: nil, args: String.new }
43
+ tool_calls[index][:id] ||= tool_call["id"]
44
+ tool_calls[index][:name] ||= tool_call.dig("function", "name")
45
+ tool_calls[index][:args] << tool_call.dig("function", "arguments").to_s
46
+ end
47
+ end
48
+
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)
54
+
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
66
+ end
67
+ end
68
+
69
+ def read_sse_stream(response)
70
+ buffer = String.new
71
+
72
+ response.read_body do |chunk|
73
+ buffer << chunk
74
+
75
+ while (index = buffer.index("\n"))
76
+ line = buffer.slice!(0, index + 1).strip
77
+ next unless line.start_with?("data: ") && line != "data: [DONE]"
78
+
79
+ yield JSON.parse(line.delete_prefix("data: "))
80
+ end
81
+ end
82
+ end
83
+
84
+ def finalize_tool_calls(tool_calls, &block)
85
+ tool_calls.values.map do |tool_call|
86
+ {
87
+ id: tool_call[:id],
88
+ name: tool_call[:name],
89
+ arguments: JSON.parse(tool_call[:args])
90
+ }.tap { |result| block.call(result.merge(type: "doing")) }
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module OpenAI
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+ require "json"
5
+ require "net/http"
6
+ require "uri"
7
+
8
+ require_relative "openai/version"
9
+ require_relative "openai/client"
10
+
11
+ Elelem::Providers.register(:openai) do
12
+ Elelem::OpenAI::Client.new(
13
+ model: ENV.fetch("OPENAI_MODEL", "gpt-4o"),
14
+ api_key: ENV.fetch("OPENAI_API_KEY")
15
+ )
16
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem-openai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: exe
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.10'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.10'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.21'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.21'
40
+ - !ruby/object:Gem::Dependency
41
+ name: net-http
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ - !ruby/object:Gem::Dependency
55
+ name: uri
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.1'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.1'
68
+ description: An OpenAI provider plugin for elelem.
69
+ email:
70
+ - mo@mokhan.ca
71
+ executables:
72
+ - elelem-openai
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE.txt
77
+ - Rakefile
78
+ - exe/elelem-openai
79
+ - lib/elelem/openai.rb
80
+ - lib/elelem/openai/client.rb
81
+ - lib/elelem/openai/version.rb
82
+ homepage: https://src.mokhan.ca/elelem/openai
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ 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
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 4.0.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.20
104
+ specification_version: 4
105
+ summary: An OpenAI provider plugin for elelem.
106
+ test_files: []