ai-lite 1.1.0 → 1.3.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.
@@ -0,0 +1,270 @@
1
+ require_relative "test_helper"
2
+
3
+ class ToolCallingTest < Minitest::Test
4
+ Response = Struct.new(:code, :body) do
5
+ def read_body
6
+ body.bytes.each_slice(7) { |bytes| yield bytes.pack("C*") }
7
+ end
8
+ end
9
+ class Http
10
+ attr_accessor :open_timeout, :read_timeout
11
+ attr_reader :requests
12
+ def initialize(responses)
13
+ @responses, @requests = responses, []
14
+ end
15
+ def request(request)
16
+ @requests << request
17
+ response = @responses.shift
18
+ raise "Unexpected request" unless response
19
+ block_given? ? yield(response) : response
20
+ end
21
+ end
22
+
23
+ TOOLS = [{ type: "function", name: "lookup_order", description: "Look up shipping status",
24
+ parameters: { type: "object", properties: { order_id: { type: "integer" } }, required: ["order_id"] } }].freeze
25
+
26
+ def setup
27
+ [AiLite::OpenAI, AiLite::Gemini].each(&:reset_configuration!)
28
+ end
29
+
30
+ def teardown
31
+ [AiLite::OpenAI, AiLite::Gemini].each(&:reset_configuration!)
32
+ end
33
+
34
+ def with_http(*bodies)
35
+ http = Http.new(bodies.map { |body| Response.new("200", body.is_a?(String) ? body : JSON.generate(body)) })
36
+ original = Net::HTTP.method(:start)
37
+ Net::HTTP.define_singleton_method(:start) { |*args, **kwargs, &block| block.call(http) }
38
+ yield http
39
+ ensure
40
+ Net::HTTP.define_singleton_method(:start, original)
41
+ end
42
+
43
+ def call_for(provider, id = "call-1", args = { "order_id" => 123 })
44
+ if provider == AiLite::OpenAI
45
+ { "type" => "function_call", "id" => "item-#{id}", "call_id" => id, "name" => "lookup_order", "arguments" => JSON.generate(args) }
46
+ else
47
+ { "type" => "function_call", "id" => id, "name" => "lookup_order", "arguments" => args }
48
+ end
49
+ end
50
+
51
+ def response_for(provider, calls = nil, status = nil)
52
+ if provider == AiLite::OpenAI
53
+ { "id" => "response-1", "status" => status || "completed", "output" => calls || [
54
+ { "type" => "message", "content" => [{ "type" => "output_text", "text" => "Shipped." }] }
55
+ ] }
56
+ else
57
+ { "id" => "response-1", "status" => status || (calls ? "requires_action" : "completed"), "steps" => calls || [
58
+ { "type" => "model_output", "content" => [{ "type" => "text", "text" => "Shipped." }] }
59
+ ] }
60
+ end
61
+ end
62
+
63
+ def test_two_turn_function_roundtrip_for_both_providers
64
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
65
+ provider.configure { |c| c.api_key = "key" }
66
+ with_http(response_for(provider, [call_for(provider)]), response_for(provider)) do |http|
67
+ first = provider.chat("Where is order 123?", tools: TOOLS)
68
+ assert_nil first["error"]
69
+ assert_nil first["content"]
70
+ assert_nil first["raw"]
71
+ assert_equal [{ "id" => "call-1", "name" => "lookup_order", "arguments" => { "order_id" => 123 } }], first["tool_calls"]
72
+ assert_equal JSON.parse(JSON.generate(TOOLS)), JSON.parse(http.requests.first.body)["tools"]
73
+ results = [{ id: first["tool_calls"][0]["id"], name: "lookup_order", output: { status: "shipped" } }]
74
+ original = Marshal.dump(results)
75
+ final = provider.chat(nil, tools: TOOLS, previous_response_id: first["response_id"], tool_results: results)
76
+ assert_equal "Shipped.", final["content"]
77
+ assert_nil final["error"]
78
+ refute final.key?("tool_calls")
79
+ assert_equal original, Marshal.dump(results)
80
+ payload = JSON.parse(http.requests.last.body)
81
+ item = payload["input"][0]
82
+ assert_equal "call-1", item["call_id"]
83
+ if provider == AiLite::OpenAI
84
+ assert_equal "response-1", payload["previous_response_id"]
85
+ assert_equal "function_call_output", item["type"]
86
+ assert_equal '{"status":"shipped"}', item["output"]
87
+ else
88
+ assert_equal "response-1", payload["previous_interaction_id"]
89
+ assert_equal "function_result", item["type"]
90
+ assert_equal "lookup_order", item["name"]
91
+ assert_equal [{ "type" => "text", "text" => '{"status":"shipped"}' }], item["result"]
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ def test_parallel_calls_text_and_debug
98
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
99
+ raw = response_for(provider)
100
+ raw[provider == AiLite::OpenAI ? "output" : "steps"].concat([call_for(provider), call_for(provider, "call-2", {})])
101
+ with_http(raw) do
102
+ result = provider.new(api_key: "key").chat("Check two orders", tools: TOOLS, debug: true)
103
+ assert_nil result["error"]
104
+ assert_equal "Shipped.", result["content"]
105
+ assert_equal ["call-1", "call-2"], result["tool_calls"].map { |call| call["id"] }
106
+ assert_equal raw, result["raw"]
107
+ end
108
+ end
109
+ end
110
+
111
+ def test_tool_option_precedence_and_no_mutation
112
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
113
+ options = { "tools" => TOOLS, temperature: 0.2 }
114
+ original = Marshal.dump(options)
115
+ with_http(response_for(provider), response_for(provider)) do |http|
116
+ client = provider.new(api_key: "key")
117
+ client.chat("Hi", options: options)
118
+ assert_equal JSON.parse(JSON.generate(TOOLS)), JSON.parse(http.requests[0].body)["tools"]
119
+ client.chat("Hi", options: options, tools: [])
120
+ assert_equal [], JSON.parse(http.requests[1].body)["tools"]
121
+ end
122
+ assert_equal original, Marshal.dump(options)
123
+ end
124
+ end
125
+
126
+ def test_invalid_results_and_tools_do_not_call_api
127
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
128
+ with_http do |http|
129
+ client = provider.new(api_key: "key")
130
+ [false, {}, [1]].each { |tools| refute_nil client.chat("Hi", tools: tools)["error"] }
131
+ valid = { id: "call", name: "lookup_order", output: false }
132
+ [[], {}, [1], [{ id: "call" }], [valid, valid]].each do |results|
133
+ refute_nil client.chat(nil, previous_response_id: "response", tool_results: results)["error"]
134
+ end
135
+ refute_nil client.chat(nil, tool_results: [valid])["error"]
136
+ refute_nil client.chat("Ambiguous input", previous_response_id: "response", tool_results: [valid])["error"]
137
+ assert_empty http.requests
138
+ end
139
+ end
140
+ end
141
+
142
+ def test_failed_and_malformed_calls_are_not_executable
143
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
144
+ client = provider.new(api_key: "key")
145
+ %w[failed incomplete cancelled].each do |status|
146
+ with_http(response_for(provider, [call_for(provider)], status)) do
147
+ result = client.chat("Hi", tools: TOOLS)
148
+ refute_nil result["error"]
149
+ refute result.key?("tool_calls")
150
+ end
151
+ end
152
+ bad_calls = [call_for(provider).merge("arguments" => "{bad"), call_for(provider).merge("arguments" => "[]"),
153
+ call_for(provider).merge("name" => ""), call_for(provider).merge("call_id" => nil, "id" => nil)]
154
+ bad_calls.each do |call|
155
+ with_http(response_for(provider, [call])) do
156
+ result = client.chat("Hi", tools: TOOLS)
157
+ refute_nil result["error"]
158
+ refute result.key?("tool_calls")
159
+ end
160
+ end
161
+ end
162
+ end
163
+
164
+ def test_native_history_and_falsy_tool_results_are_preserved
165
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
166
+ client = provider.new(api_key: "key")
167
+ input = provider == AiLite::OpenAI ? [{ type: "function_call_output", call_id: "call", output: "native" }] :
168
+ [{ type: "function_result", name: "lookup_order", call_id: "call", result: [{ type: "text", text: "native" }] }]
169
+ with_http(response_for(provider)) do |http|
170
+ client.chat(input, tools: TOOLS, previous_response_id: "previous")
171
+ assert_equal JSON.parse(JSON.generate(input)), JSON.parse(http.requests.first.body)["input"]
172
+ end
173
+ [false, nil, ""].each do |output|
174
+ with_http(response_for(provider)) do |http|
175
+ result = client.chat(nil, previous_response_id: "previous", tool_results: [{ id: "call", name: "lookup_order", output: output }])
176
+ assert_nil result["error"]
177
+ item = JSON.parse(http.requests.first.body)["input"][0]
178
+ actual = provider == AiLite::OpenAI ? item["output"] : item.dig("result", 0, "text")
179
+ assert_equal(output.is_a?(String) ? output : JSON.generate(output), actual)
180
+ end
181
+ end
182
+ end
183
+ end
184
+
185
+ def test_incomplete_openai_calls_and_failed_streams_are_not_executable
186
+ provider = AiLite::OpenAI
187
+ client = provider.new(api_key: "key")
188
+ [response_for(provider, [call_for(provider)], "in_progress"),
189
+ response_for(provider, [call_for(provider).merge("status" => "in_progress")])].each do |raw|
190
+ with_http(raw) do
191
+ result = client.chat("Hi", tools: TOOLS, debug: true)
192
+ refute_nil result["error"]
193
+ refute result.key?("tool_calls")
194
+ assert_equal raw, result["raw"]
195
+ end
196
+ end
197
+ [AiLite::OpenAI, AiLite::Gemini].each do |klass|
198
+ raw = response_for(klass, [call_for(klass)], "failed")
199
+ raw["error"] = { "message" => "Failed" }
200
+ stream = sse(klass, klass == AiLite::OpenAI ? "response.completed" : "interaction.completed",
201
+ { (klass == AiLite::OpenAI ? "response" : "interaction") => raw })
202
+ with_http(stream) do
203
+ result = klass.new(api_key: "key").chat_stream("Hi", tools: TOOLS) { |_| }
204
+ refute_nil result["error"]
205
+ refute result.key?("tool_calls")
206
+ end
207
+ end
208
+ end
209
+
210
+ def sse(provider, type, fields)
211
+ "data: #{JSON.generate(fields.merge(provider == AiLite::OpenAI ? 'type' : 'event_type' => type))}\n\n"
212
+ end
213
+
214
+ def test_openai_stream_returns_complete_calls_without_yielding_arguments
215
+ provider = AiLite::OpenAI
216
+ raw = response_for(provider, [call_for(provider)])
217
+ stream = sse(provider, "response.function_call_arguments.delta", { "delta" => '{"order_id":123}' }) +
218
+ sse(provider, "response.completed", { "response" => raw })
219
+ with_http(stream) do |http|
220
+ result = provider.new(api_key: "key").chat_stream("Check order", tools: TOOLS) { flunk "Arguments must not be yielded" }
221
+ assert_nil result["error"]
222
+ assert_equal "call-1", result["tool_calls"][0]["id"]
223
+ assert_equal JSON.parse(JSON.generate(TOOLS)), JSON.parse(http.requests.first.body)["tools"]
224
+ end
225
+ end
226
+
227
+ def gemini_call_stream(tail = nil)
228
+ provider = AiLite::Gemini
229
+ sse(provider, "interaction.created", { "interaction" => { "id" => "response-1", "status" => "in_progress" } }) +
230
+ sse(provider, "step.start", { "index" => 1, "step" => call_for(provider).merge("arguments" => {}) }) +
231
+ sse(provider, "step.delta", { "index" => 1, "delta" => { "type" => "arguments_delta", "arguments" => '{"order_' } }) +
232
+ sse(provider, "step.delta", { "index" => 1, "delta" => { "type" => "arguments_delta", "arguments" => 'id":123}' } }) +
233
+ sse(provider, "step.stop", { "index" => 1 }) +
234
+ (tail || sse(provider, "interaction.status_update", { "interaction_id" => "response-1", "status" => "requires_action" }))
235
+ end
236
+
237
+ def test_gemini_stream_assembles_fragments_and_handles_action_status
238
+ provider = AiLite::Gemini
239
+ [nil, sse(provider, "interaction.completed", { "interaction" => { "id" => "response-1", "status" => "requires_action" } }),
240
+ sse(provider, "interaction.completed", { "interaction" => response_for(provider, [call_for(provider)]) })].each do |tail|
241
+ with_http(gemini_call_stream(tail)) do
242
+ result = provider.new(api_key: "key").chat_stream("Check order", tools: TOOLS) { flunk "Arguments must not be yielded" }
243
+ assert_nil result["error"]
244
+ assert_equal "response-1", result["response_id"]
245
+ assert_equal [{ "id" => "call-1", "name" => "lookup_order", "arguments" => { "order_id" => 123 } }], result["tool_calls"]
246
+ end
247
+ end
248
+ with_http(gemini_call_stream.sub(sse(provider, "step.stop", { "index" => 1 }), "")) do
249
+ result = provider.new(api_key: "key").chat_stream("Check order", tools: TOOLS) { |_| }
250
+ refute_nil result["error"]
251
+ refute result.key?("tool_calls")
252
+ end
253
+ end
254
+
255
+ def test_stream_tool_results_class_delegation
256
+ [AiLite::OpenAI, AiLite::Gemini].each do |provider|
257
+ provider.configure { |c| c.api_key = "key" }
258
+ raw = response_for(provider, [])
259
+ raw["status"] = "completed"
260
+ stream = sse(provider, provider == AiLite::OpenAI ? "response.completed" : "interaction.completed",
261
+ { (provider == AiLite::OpenAI ? "response" : "interaction") => raw })
262
+ with_http(stream) do |http|
263
+ result = provider.chat_stream(nil, previous_response_id: "previous", tools: TOOLS,
264
+ tool_results: [{ "id" => "call-1", "name" => "lookup_order", "output" => "Shipped" }]) { |_| }
265
+ assert_nil result["error"]
266
+ assert_equal "call-1", JSON.parse(http.requests.first.body).dig("input", 0, "call_id")
267
+ end
268
+ end
269
+ end
270
+ end
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Basmayor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-23 00:00:00.000000000 Z
11
+ date: 2026-11-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: AI Lite is a dependency-light Ruby client for Rails apps and plain Ruby
14
- projects, using only the Ruby standard library. It provides OpenAI chat, streaming,
15
- moderation, embeddings, images, speech, transcription, and model checks, plus Gemini
16
- chat, streaming, embeddings, image generation, structured media inputs, and model
17
- checks.
13
+ description: |-
14
+ AI Lite is a lightweight Ruby client for OpenAI, Gemini, and Claude, built for Rails apps and plain Ruby projects. It uses only Ruby's standard library and has no runtime gem dependencies.
15
+
16
+ AI Lite handles the API calls while you control your application's architecture, providing straightforward AI integration without a larger SDK or framework. Supports Ruby 2.6 through 4.0 with no Rails dependency.
17
+
18
+ OpenAI and Gemini support chat, streaming, function calling, embeddings, image generation, speech, transcription, token counting, and model checks. OpenAI also supports moderation. Gemini also supports structured media inputs and a YouTube helper. Claude supports synchronous chat and explicit message history.
18
19
  email:
19
20
  executables: []
20
21
  extensions: []
@@ -24,35 +25,52 @@ files:
24
25
  - LICENSE
25
26
  - README.md
26
27
  - lib/ai_lite.rb
28
+ - lib/ai_lite/claude.rb
29
+ - lib/ai_lite/claude/chat.rb
30
+ - lib/ai_lite/claude/configuration.rb
27
31
  - lib/ai_lite/gemini.rb
28
32
  - lib/ai_lite/gemini/chat.rb
29
33
  - lib/ai_lite/gemini/configuration.rb
30
34
  - lib/ai_lite/gemini/embeddings.rb
31
35
  - lib/ai_lite/gemini/images.rb
32
36
  - lib/ai_lite/gemini/models.rb
37
+ - lib/ai_lite/gemini/speech.rb
33
38
  - lib/ai_lite/gemini/streaming.rb
39
+ - lib/ai_lite/gemini/tokens.rb
40
+ - lib/ai_lite/gemini/tool_calling.rb
41
+ - lib/ai_lite/gemini/transcription.rb
34
42
  - lib/ai_lite/gemini/youtube.rb
35
43
  - lib/ai_lite/openai.rb
36
44
  - lib/ai_lite/openai/configuration.rb
37
45
  - lib/ai_lite/openai/models.rb
38
46
  - lib/ai_lite/openai/streaming.rb
47
+ - lib/ai_lite/openai/tokens.rb
48
+ - lib/ai_lite/openai/tool_calling.rb
39
49
  - lib/ai_lite/version.rb
40
50
  - test/ai_lite_test.rb
51
+ - test/claude_test.rb
41
52
  - test/gemini_embeddings_test.rb
42
53
  - test/gemini_images_test.rb
43
54
  - test/gemini_models_test.rb
55
+ - test/gemini_speech_test.rb
44
56
  - test/gemini_streaming_test.rb
45
57
  - test/gemini_test.rb
58
+ - test/gemini_transcription_test.rb
46
59
  - test/gemini_youtube_test.rb
47
60
  - test/openai_models_test.rb
48
61
  - test/openai_streaming_test.rb
62
+ - test/package_test.rb
49
63
  - test/run.rb
50
64
  - test/test_helper.rb
65
+ - test/token_counting_test.rb
66
+ - test/tool_calling_test.rb
51
67
  homepage: https://github.com/wbasmayor/ai-lite
52
68
  licenses:
53
69
  - MIT
54
70
  metadata:
55
71
  source_code_uri: https://github.com/wbasmayor/ai-lite
72
+ documentation_uri: https://github.com/wbasmayor/ai-lite#readme
73
+ bug_tracker_uri: https://github.com/wbasmayor/ai-lite/issues
56
74
  changelog_uri: https://github.com/wbasmayor/ai-lite/blob/main/CHANGELOG.md
57
75
  post_install_message:
58
76
  rdoc_options: []
@@ -72,5 +90,5 @@ requirements: []
72
90
  rubygems_version: 3.5.22
73
91
  signing_key:
74
92
  specification_version: 4
75
- summary: Lightweight Ruby client with separate OpenAI and Gemini providers.
93
+ summary: Lightweight Ruby client for OpenAI, Gemini, and Claude.
76
94
  test_files: []