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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -6
- data/README.md +392 -23
- data/lib/ai_lite/claude/chat.rb +73 -0
- data/lib/ai_lite/claude/configuration.rb +22 -0
- data/lib/ai_lite/claude.rb +82 -0
- data/lib/ai_lite/gemini/chat.rb +8 -5
- data/lib/ai_lite/gemini/configuration.rb +7 -1
- data/lib/ai_lite/gemini/images.rb +1 -1
- data/lib/ai_lite/gemini/models.rb +1 -1
- data/lib/ai_lite/gemini/speech.rb +108 -0
- data/lib/ai_lite/gemini/streaming.rb +34 -5
- data/lib/ai_lite/gemini/tokens.rb +53 -0
- data/lib/ai_lite/gemini/tool_calling.rb +66 -0
- data/lib/ai_lite/gemini/transcription.rb +66 -0
- data/lib/ai_lite/gemini.rb +21 -3
- data/lib/ai_lite/openai/streaming.rb +11 -7
- data/lib/ai_lite/openai/tokens.rb +38 -0
- data/lib/ai_lite/openai/tool_calling.rb +71 -0
- data/lib/ai_lite/openai.rb +22 -15
- data/lib/ai_lite/version.rb +1 -1
- data/lib/ai_lite.rb +1 -0
- data/test/ai_lite_test.rb +5 -5
- data/test/claude_test.rb +156 -0
- data/test/gemini_images_test.rb +6 -6
- data/test/gemini_models_test.rb +1 -1
- data/test/gemini_speech_test.rb +199 -0
- data/test/gemini_test.rb +1 -1
- data/test/gemini_transcription_test.rb +123 -0
- data/test/package_test.rb +52 -0
- data/test/token_counting_test.rb +173 -0
- data/test/tool_calling_test.rb +270 -0
- metadata +26 -8
|
@@ -2,7 +2,7 @@ class AiLite
|
|
|
2
2
|
class OpenAI
|
|
3
3
|
module Streaming
|
|
4
4
|
# Text deltas are yielded immediately; the return value is the final envelope.
|
|
5
|
-
def chat_stream(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, debug: false, options: {}, &block)
|
|
5
|
+
def chat_stream(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, tools: nil, tool_results: nil, debug: false, options: {}, &block)
|
|
6
6
|
raise ArgumentError, "chat_stream requires a block" unless block
|
|
7
7
|
|
|
8
8
|
status = "unknown"
|
|
@@ -11,12 +11,14 @@ class AiLite
|
|
|
11
11
|
raw = nil
|
|
12
12
|
callback_error = nil
|
|
13
13
|
begin
|
|
14
|
-
payload = options.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
payload[
|
|
14
|
+
payload = options.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
|
|
15
|
+
input = tool_result_input(message, tool_results, previous_response_id || payload["previous_response_id"])
|
|
16
|
+
raise ArgumentError, "Missing chat input" if input.nil?
|
|
17
|
+
payload.merge!("model" => model || self.model, "input" => input, "stream" => true,
|
|
18
|
+
"max_output_tokens" => max_output_tokens || self.max_output_tokens)
|
|
19
|
+
payload["instructions"] = instructions if instructions
|
|
20
|
+
payload["previous_response_id"] = previous_response_id if previous_response_id
|
|
21
|
+
apply_chat_tools(payload, tools)
|
|
20
22
|
uri = URI.parse(response_endpoint)
|
|
21
23
|
request = Net::HTTP::Post.new(uri)
|
|
22
24
|
headers.each { |key, value| request[key] = value }
|
|
@@ -61,9 +63,11 @@ class AiLite
|
|
|
61
63
|
when "response.incomplete"
|
|
62
64
|
"Response incomplete: #{raw.dig('incomplete_details', 'reason') || 'unknown reason'}"
|
|
63
65
|
end
|
|
66
|
+
error ||= error_message(raw) if raw["error"]
|
|
64
67
|
content = error ? text : parse_content(extract_output_text(raw))
|
|
65
68
|
result = prettify_data(status: status, content: content, error: error,
|
|
66
69
|
response_id: response_id, raw: raw, debug: debug)
|
|
70
|
+
attach_tool_calls(result, raw)
|
|
67
71
|
throw :ai_lite_stream_finished
|
|
68
72
|
when "error"
|
|
69
73
|
result = prettify_data(status: status, content: text, error: error_message(event),
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
class AiLite
|
|
2
|
+
class OpenAI
|
|
3
|
+
module Tokens
|
|
4
|
+
def count_tokens(input, model: nil, instructions: nil, previous_response_id: nil, debug: false, options: {})
|
|
5
|
+
response = raw = nil
|
|
6
|
+
unless input.is_a?(String) || (input.is_a?(Array) && input.all? { |item| item.is_a?(Hash) })
|
|
7
|
+
raise ArgumentError, "input must be a string or an array of OpenAI Responses input items"
|
|
8
|
+
end
|
|
9
|
+
raise ArgumentError, "options must be a hash" unless options.is_a?(Hash)
|
|
10
|
+
raise ArgumentError, "instructions must be a string" unless instructions.nil? || instructions.is_a?(String)
|
|
11
|
+
|
|
12
|
+
payload = options.each_with_object({}) { |(key, value), result| result[key.to_s] = value }
|
|
13
|
+
payload.merge!("model" => model || self.model, "input" => input)
|
|
14
|
+
payload["instructions"] = instructions unless instructions.nil?
|
|
15
|
+
payload["previous_response_id"] = previous_response_id unless previous_response_id.nil?
|
|
16
|
+
response = post(payload, endpoint: "#{API_BASE_URL}/responses/input_tokens")
|
|
17
|
+
raw = response.body
|
|
18
|
+
raw = JSON.parse(raw)
|
|
19
|
+
raise "Invalid OpenAI token count response: expected an object" unless raw.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
unless response.code.to_i.between?(200, 299) && !raw["error"]
|
|
22
|
+
error = raw["error"]
|
|
23
|
+
message = error.is_a?(Hash) ? (error["message"] || error.to_s) : (error || raw["message"] || raw.to_s)
|
|
24
|
+
return prettify_data(status: response.code.to_i, error: message, raw: raw, debug: debug)
|
|
25
|
+
end
|
|
26
|
+
count = raw["input_tokens"]
|
|
27
|
+
raise "Invalid OpenAI token count: expected a nonnegative integer" unless count.is_a?(Integer) && count >= 0
|
|
28
|
+
|
|
29
|
+
prettify_data(status: response.code.to_i, content: count, raw: raw, debug: debug)
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
prettify_data(status: response ? response.code.to_i : "unknown", error: e.message, raw: raw, debug: debug)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
include Tokens
|
|
36
|
+
private_constant :Tokens
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
class AiLite
|
|
2
|
+
class OpenAI
|
|
3
|
+
module ToolCalling
|
|
4
|
+
private
|
|
5
|
+
|
|
6
|
+
def apply_chat_tools(payload, tools)
|
|
7
|
+
payload["tools"] = tools unless tools.nil?
|
|
8
|
+
if payload.key?("tools") && !(payload["tools"].is_a?(Array) && payload["tools"].all? { |tool| tool.is_a?(Hash) })
|
|
9
|
+
raise ArgumentError, "tools must be an array of provider tool definitions"
|
|
10
|
+
end
|
|
11
|
+
payload
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def tool_result_input(message, results, previous_id)
|
|
15
|
+
return message if results.nil?
|
|
16
|
+
raise ArgumentError, "Use tool_results without a message" unless message.nil?
|
|
17
|
+
unless previous_id.is_a?(String) && !previous_id.strip.empty?
|
|
18
|
+
raise ArgumentError, "tool_results requires previous_response_id; use native input for stateless history"
|
|
19
|
+
end
|
|
20
|
+
unless results.is_a?(Array) && !results.empty?
|
|
21
|
+
raise ArgumentError, "tool_results must be a nonempty array"
|
|
22
|
+
end
|
|
23
|
+
ids = []
|
|
24
|
+
results.map do |item|
|
|
25
|
+
raise ArgumentError, "Each tool result must be a hash" unless item.is_a?(Hash)
|
|
26
|
+
entry = item.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
|
|
27
|
+
unless %w[id name].all? { |key| entry[key].is_a?(String) && !entry[key].strip.empty? } && entry.key?("output")
|
|
28
|
+
raise ArgumentError, "Each tool result requires id, name, and output"
|
|
29
|
+
end
|
|
30
|
+
raise ArgumentError, "Duplicate tool result ID" if ids.include?(entry["id"])
|
|
31
|
+
ids << entry["id"]
|
|
32
|
+
output = entry["output"].is_a?(String) ? entry["output"] : JSON.generate(entry["output"])
|
|
33
|
+
{ "type" => "function_call_output", "call_id" => entry["id"], "output" => output }
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def extract_tool_calls(raw)
|
|
38
|
+
calls = Array(raw["output"]).select { |item| item.is_a?(Hash) && item["type"] == "function_call" }
|
|
39
|
+
if !calls.empty? && raw["status"] && raw["status"] != "completed"
|
|
40
|
+
raise "Response #{raw['status']}: function calls are not complete"
|
|
41
|
+
end
|
|
42
|
+
ids = []
|
|
43
|
+
calls.map do |call|
|
|
44
|
+
if call["status"] && call["status"] != "completed"
|
|
45
|
+
raise "Function call #{call['status']}: arguments are not complete"
|
|
46
|
+
end
|
|
47
|
+
id = call["call_id"]
|
|
48
|
+
name = call["name"]
|
|
49
|
+
arguments = call["arguments"]
|
|
50
|
+
arguments = JSON.parse(arguments) if arguments.is_a?(String)
|
|
51
|
+
unless id.is_a?(String) && !id.empty? && name.is_a?(String) && !name.empty? && arguments.is_a?(Hash)
|
|
52
|
+
raise "Invalid function call: expected ID, name, and object arguments"
|
|
53
|
+
end
|
|
54
|
+
raise "Invalid function call: duplicate ID" if ids.include?(id)
|
|
55
|
+
ids << id
|
|
56
|
+
{ "id" => id, "name" => name, "arguments" => arguments }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def attach_tool_calls(result, raw)
|
|
61
|
+
return result if result["error"]
|
|
62
|
+
calls = extract_tool_calls(raw)
|
|
63
|
+
result["tool_calls"] = calls unless calls.empty?
|
|
64
|
+
result
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
include ToolCalling
|
|
69
|
+
private_constant :ToolCalling
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/ai_lite/openai.rb
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
require "base64"
|
|
2
1
|
require "json"
|
|
3
2
|
require "net/http"
|
|
4
3
|
require "securerandom"
|
|
5
4
|
require "uri"
|
|
6
5
|
require_relative "version"
|
|
7
6
|
require_relative "openai/configuration"
|
|
7
|
+
require_relative "openai/tokens"
|
|
8
|
+
require_relative "openai/tool_calling"
|
|
8
9
|
require_relative "openai/streaming"
|
|
9
10
|
require_relative "openai/models"
|
|
10
11
|
|
|
@@ -31,6 +32,10 @@ class AiLite
|
|
|
31
32
|
@client ||= new
|
|
32
33
|
end
|
|
33
34
|
|
|
35
|
+
def count_tokens(input, **kwargs)
|
|
36
|
+
client.count_tokens(input, **kwargs)
|
|
37
|
+
end
|
|
38
|
+
|
|
34
39
|
def chat(message, **kwargs)
|
|
35
40
|
client.chat(message, **kwargs)
|
|
36
41
|
end
|
|
@@ -85,15 +90,16 @@ class AiLite
|
|
|
85
90
|
}
|
|
86
91
|
end
|
|
87
92
|
|
|
88
|
-
def chat(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, debug: false, options: {})
|
|
89
|
-
payload = options.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
payload[
|
|
96
|
-
|
|
93
|
+
def chat(message, model: nil, instructions: nil, previous_response_id: nil, max_output_tokens: nil, tools: nil, tool_results: nil, debug: false, options: {})
|
|
94
|
+
payload = options.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }
|
|
95
|
+
previous_id = previous_response_id || payload["previous_response_id"]
|
|
96
|
+
input = tool_result_input(message, tool_results, previous_id)
|
|
97
|
+
raise ArgumentError, "Missing chat input" if input.nil?
|
|
98
|
+
payload.merge!("model" => model || self.model, "input" => input,
|
|
99
|
+
"max_output_tokens" => max_output_tokens || self.max_output_tokens)
|
|
100
|
+
payload["instructions"] = instructions if instructions
|
|
101
|
+
payload["previous_response_id"] = previous_response_id if previous_response_id
|
|
102
|
+
apply_chat_tools(payload, tools)
|
|
97
103
|
extract_content(post(payload), debug: debug)
|
|
98
104
|
rescue => e
|
|
99
105
|
prettify_data(status: "unknown", error: e.message, raw: nil, debug: debug)
|
|
@@ -257,7 +263,7 @@ class AiLite
|
|
|
257
263
|
reason = parsed_response.dig("incomplete_details", "reason")
|
|
258
264
|
["Response #{parsed_response['status']}", reason].compact.join(": ")
|
|
259
265
|
end
|
|
260
|
-
prettify_data(
|
|
266
|
+
result = prettify_data(
|
|
261
267
|
status: status,
|
|
262
268
|
content: content,
|
|
263
269
|
error: error,
|
|
@@ -265,10 +271,11 @@ class AiLite
|
|
|
265
271
|
raw: parsed_response,
|
|
266
272
|
debug: debug
|
|
267
273
|
)
|
|
274
|
+
attach_tool_calls(result, parsed_response)
|
|
268
275
|
rescue JSON::ParserError => e
|
|
269
276
|
prettify_data(status: response_status(response), error: e.message, raw: response&.body, debug: debug)
|
|
270
277
|
rescue => e
|
|
271
|
-
prettify_data(status: response_status(response), error: e.message, raw:
|
|
278
|
+
prettify_data(status: response_status(response), error: e.message, raw: parsed_response, debug: debug)
|
|
272
279
|
end
|
|
273
280
|
|
|
274
281
|
def extract_moderation(response, debug: false)
|
|
@@ -459,7 +466,7 @@ class AiLite
|
|
|
459
466
|
|
|
460
467
|
def image_data_url(path)
|
|
461
468
|
mime_type = image_mime_type(path)
|
|
462
|
-
"data:#{mime_type};base64,#{
|
|
469
|
+
"data:#{mime_type};base64,#{[File.binread(path)].pack("m0")}"
|
|
463
470
|
end
|
|
464
471
|
|
|
465
472
|
def image_mime_type(path)
|
|
@@ -509,11 +516,11 @@ class AiLite
|
|
|
509
516
|
def write_image_output(path, content)
|
|
510
517
|
raise "No image data returned" if content.to_s.empty?
|
|
511
518
|
|
|
512
|
-
File.binwrite(path,
|
|
519
|
+
File.binwrite(path, content.unpack1("m"))
|
|
513
520
|
end
|
|
514
521
|
|
|
515
522
|
def speech_content(audio, output_path:, base64:, response_format:)
|
|
516
|
-
return
|
|
523
|
+
return [audio].pack("m0") if base64
|
|
517
524
|
return audio unless output_path
|
|
518
525
|
|
|
519
526
|
{
|
data/lib/ai_lite/version.rb
CHANGED
data/lib/ai_lite.rb
CHANGED
data/test/ai_lite_test.rb
CHANGED
|
@@ -619,7 +619,7 @@ class AiLiteTest < Minitest::Test
|
|
|
619
619
|
|
|
620
620
|
def test_image_sends_post_to_images_with_default_payload
|
|
621
621
|
client = provider.new(api_key: "token-abc")
|
|
622
|
-
image_data =
|
|
622
|
+
image_data = ["fake image"].pack("m0")
|
|
623
623
|
|
|
624
624
|
with_stubbed_http(image_response(b64_json: image_data)) do |captured, _response|
|
|
625
625
|
result = client.image("A small ruby gem logo")
|
|
@@ -689,7 +689,7 @@ class AiLiteTest < Minitest::Test
|
|
|
689
689
|
|
|
690
690
|
def test_image_writes_decoded_content_to_output_path
|
|
691
691
|
client = provider.new(api_key: "token-abc")
|
|
692
|
-
image_data =
|
|
692
|
+
image_data = ["fake image"].pack("m0")
|
|
693
693
|
|
|
694
694
|
Tempfile.create(["generated", ".png"]) do |file|
|
|
695
695
|
with_stubbed_http(image_response(b64_json: image_data)) do |_captured, _response|
|
|
@@ -703,7 +703,7 @@ class AiLiteTest < Minitest::Test
|
|
|
703
703
|
|
|
704
704
|
def test_image_debug_true_returns_raw_usage
|
|
705
705
|
client = provider.new(api_key: "token-abc")
|
|
706
|
-
image_data =
|
|
706
|
+
image_data = ["fake image"].pack("m0")
|
|
707
707
|
|
|
708
708
|
with_stubbed_http(image_response(b64_json: image_data)) do |_captured, _response|
|
|
709
709
|
result = client.image("A debuggable image", debug: true)
|
|
@@ -825,7 +825,7 @@ class AiLiteTest < Minitest::Test
|
|
|
825
825
|
with_stubbed_http(speech_response("fake audio")) do |_captured, _response|
|
|
826
826
|
result = client.speak("Encode this", base64: true)
|
|
827
827
|
|
|
828
|
-
assert_equal
|
|
828
|
+
assert_equal ["fake audio"].pack("m0"), result["content"]
|
|
829
829
|
assert_nil result["raw"]
|
|
830
830
|
end
|
|
831
831
|
end
|
|
@@ -1155,7 +1155,7 @@ class AiLiteTest < Minitest::Test
|
|
|
1155
1155
|
FakeResponse.new("200", body)
|
|
1156
1156
|
end
|
|
1157
1157
|
|
|
1158
|
-
def image_response(b64_json:
|
|
1158
|
+
def image_response(b64_json: ["fake image"].pack("m0"))
|
|
1159
1159
|
image = {}
|
|
1160
1160
|
image["b64_json"] = b64_json if b64_json
|
|
1161
1161
|
|
data/test/claude_test.rb
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
require_relative "test_helper"
|
|
2
|
+
|
|
3
|
+
class ClaudeTest < Minitest::Test
|
|
4
|
+
Response = Struct.new(:code, :body)
|
|
5
|
+
class Http
|
|
6
|
+
attr_accessor :open_timeout, :read_timeout
|
|
7
|
+
attr_reader :request_received
|
|
8
|
+
def initialize(response)
|
|
9
|
+
@response = response
|
|
10
|
+
end
|
|
11
|
+
def request(request)
|
|
12
|
+
@request_received = request
|
|
13
|
+
raise @response if @response.is_a?(Exception)
|
|
14
|
+
@response
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def setup
|
|
19
|
+
AiLite::Claude.reset_configuration!
|
|
20
|
+
@old_key = ENV.delete("ANTHROPIC_API_KEY")
|
|
21
|
+
@client = AiLite::Claude.new(api_key: "claude-test", timeout: 7)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def teardown
|
|
25
|
+
ENV["ANTHROPIC_API_KEY"] = @old_key
|
|
26
|
+
AiLite::Claude.reset_configuration!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def body(text = "Hello", reason = "end_turn")
|
|
30
|
+
{ "id" => "msg_123", "type" => "message", "role" => "assistant", "stop_reason" => reason,
|
|
31
|
+
"content" => [{ "type" => "thinking", "thinking" => "private", "signature" => "sig" },
|
|
32
|
+
{ "type" => "text", "text" => text }], "usage" => { "input_tokens" => 3 } }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def with_http(raw = body, status = "200")
|
|
36
|
+
http = Http.new(raw.is_a?(Exception) ? raw : Response.new(status, raw.is_a?(String) ? raw : JSON.generate(raw)))
|
|
37
|
+
original = Net::HTTP.method(:start)
|
|
38
|
+
Net::HTTP.define_singleton_method(:start) do |host, port, use_ssl:, &block|
|
|
39
|
+
raise "Wrong Claude endpoint" unless host == "api.anthropic.com" && port == 443 && use_ssl
|
|
40
|
+
block.call(http)
|
|
41
|
+
end
|
|
42
|
+
yield http
|
|
43
|
+
ensure
|
|
44
|
+
Net::HTTP.define_singleton_method(:start, original)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_configuration_key_precedence_and_provider_isolation
|
|
48
|
+
assert_raises(ArgumentError) { AiLite::Claude.new }
|
|
49
|
+
ENV["ANTHROPIC_API_KEY"] = "env-key"
|
|
50
|
+
assert_equal "env-key", AiLite::Claude.new.api_key
|
|
51
|
+
previous = AiLite::OpenAI.configuration.model
|
|
52
|
+
AiLite::Claude.configure { |c| c.api_key = "configured"; c.model = "custom"; c.workspace_id = "wrkspc_test"; c.api_version = "custom-version" }
|
|
53
|
+
assert_equal "configured", AiLite::Claude.new.api_key
|
|
54
|
+
assert_equal "explicit", AiLite::Claude.new(api_key: "explicit").api_key
|
|
55
|
+
assert_equal "custom", AiLite::Claude.client.model
|
|
56
|
+
assert_equal "wrkspc_test", AiLite::Claude.client.headers["anthropic-workspace-id"]
|
|
57
|
+
assert_equal "custom-version", AiLite::Claude.client.headers["anthropic-version"]
|
|
58
|
+
old_client = AiLite::Claude.client
|
|
59
|
+
AiLite::Claude.configure { |c| c.model = "other" }
|
|
60
|
+
refute_same old_client, AiLite::Claude.client
|
|
61
|
+
assert_equal previous, AiLite::OpenAI.configuration.model
|
|
62
|
+
AiLite::Claude.reset_configuration!
|
|
63
|
+
assert_equal "claude-haiku-5-5", AiLite::Claude.new.model
|
|
64
|
+
%i[chat_stream embed speak transcribe image moderate count_tokens available_models].each { |method| refute_respond_to @client, method }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_chat_transport_and_replay_message
|
|
68
|
+
with_http do |http|
|
|
69
|
+
result = @client.chat("Hi")
|
|
70
|
+
assert_equal "Hello", result["content"]
|
|
71
|
+
assert_equal "msg_123", result["response_id"]
|
|
72
|
+
assert_equal 200, result["status"]
|
|
73
|
+
assert_nil result["error"]
|
|
74
|
+
assert_nil result["raw"]
|
|
75
|
+
assert_equal({ "role" => "assistant", "content" => body["content"] }, result["assistant_message"])
|
|
76
|
+
request = http.request_received
|
|
77
|
+
assert_equal "/v1/messages", request.path
|
|
78
|
+
assert_equal "Bearer claude-test", request["Authorization"]
|
|
79
|
+
assert_equal "2026-10-21", request["anthropic-version"]
|
|
80
|
+
assert_nil request["x-goog-api-key"]
|
|
81
|
+
assert_equal 7, http.open_timeout
|
|
82
|
+
assert_equal 7, http.read_timeout
|
|
83
|
+
assert_equal({ "model" => "claude-haiku-5-5", "messages" => [{ "role" => "user", "content" => "Hi" }], "max_tokens" => 2000 }, JSON.parse(request.body))
|
|
84
|
+
history = [{ role: "user", content: "Hi" }, result["assistant_message"], { role: "user", content: "Continue" }]
|
|
85
|
+
@client.chat(history)
|
|
86
|
+
assert_equal JSON.parse(JSON.generate(history)), JSON.parse(http.request_received.body)["messages"]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def test_options_native_media_precedence_and_no_mutation
|
|
91
|
+
history = [{ role: "user", content: [{ type: "image", source: { type: "base64", media_type: "image/png", data: "AAAA" } }] }]
|
|
92
|
+
options = { "model" => "ignored", messages: [], max_tokens: 10, system: "ignored", thinking: { type: "disabled" } }
|
|
93
|
+
original = Marshal.dump([history, options])
|
|
94
|
+
with_http(body('{"ok":true}')) do |http|
|
|
95
|
+
result = @client.chat(history, model: "selected", instructions: "Brief", max_output_tokens: 20, options: options, debug: true)
|
|
96
|
+
assert_equal({ "ok" => true }, result["content"])
|
|
97
|
+
assert_equal body('{"ok":true}'), result["raw"]
|
|
98
|
+
payload = JSON.parse(http.request_received.body)
|
|
99
|
+
assert_equal "selected", payload["model"]
|
|
100
|
+
assert_equal "Brief", payload["system"]
|
|
101
|
+
assert_equal 20, payload["max_tokens"]
|
|
102
|
+
assert_equal({ "type" => "disabled" }, payload["thinking"])
|
|
103
|
+
assert_equal JSON.parse(JSON.generate(history)), payload["messages"]
|
|
104
|
+
end
|
|
105
|
+
assert_equal original, Marshal.dump([history, options])
|
|
106
|
+
with_http do |http|
|
|
107
|
+
@client.chat("Hi", options: { max_tokens: 12, system: [{ type: "text", text: "Native" }] })
|
|
108
|
+
assert_equal 12, JSON.parse(http.request_received.body)["max_tokens"]
|
|
109
|
+
assert_equal [{ "type" => "text", "text" => "Native" }], JSON.parse(http.request_received.body)["system"]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_class_delegation_and_text_only_output
|
|
114
|
+
AiLite::Claude.configure { |c| c.api_key = "configured" }
|
|
115
|
+
with_http(body("Hello", "stop_sequence")) { assert_equal "Hello", AiLite::Claude.chat("Hi")["content"] }
|
|
116
|
+
with_http(body.merge("content" => [])) { assert_nil @client.chat("Hi")["content"] }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def test_stop_reasons_and_malformed_responses
|
|
120
|
+
%w[max_tokens refusal model_context_window_exceeded pause_turn tool_use unknown].each do |reason|
|
|
121
|
+
with_http(body("Partial", reason)) do
|
|
122
|
+
result = @client.chat("Hi")
|
|
123
|
+
assert_equal "Partial", result["content"]
|
|
124
|
+
assert_includes result["error"], reason
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
[[], {}, body.merge("content" => nil), body.merge("role" => "user"), body.merge("content" => [{ "type" => "text", "text" => 1 }]), "not json"].each do |raw|
|
|
128
|
+
with_http(raw) do
|
|
129
|
+
result = @client.chat("Hi", debug: true)
|
|
130
|
+
refute_nil result["error"]
|
|
131
|
+
assert_equal 200, result["status"]
|
|
132
|
+
assert_equal raw, result["raw"]
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
["200", "401", "429", "529"].each do |status|
|
|
136
|
+
with_http({ "error" => { "type" => "overloaded_error", "message" => "Unavailable" } }, status) do
|
|
137
|
+
result = @client.chat("Hi")
|
|
138
|
+
assert_equal "Unavailable", result["error"]
|
|
139
|
+
assert_equal status.to_i, result["status"]
|
|
140
|
+
assert_nil result["raw"]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
with_http(IOError.new("Disconnected")) { assert_equal "unknown", @client.chat("Hi")["status"] }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def test_invalid_inputs_never_send_requests
|
|
147
|
+
with_http do |http|
|
|
148
|
+
[nil, 1, {}, [], [1], [{ role: "invalid", content: "Hi" }]].each { |message| refute_nil @client.chat(message)["error"] }
|
|
149
|
+
[{ options: nil }, { previous_response_id: "msg_123" }, { options: { previous_response_id: "msg_123" } },
|
|
150
|
+
{ options: { stream: true } }, { instructions: 1 }, { max_output_tokens: -1 }].each do |kwargs|
|
|
151
|
+
refute_nil @client.chat("Hi", **kwargs)["error"]
|
|
152
|
+
end
|
|
153
|
+
assert_nil http.request_received
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
data/test/gemini_images_test.rb
CHANGED
|
@@ -35,15 +35,15 @@ class GeminiImagesTest < Minitest::Test
|
|
|
35
35
|
Net::HTTP.define_singleton_method(:start, original)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
PNG =
|
|
38
|
+
PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aX1sAAAAASUVORK5CYII=".unpack1("m0")
|
|
39
39
|
|
|
40
40
|
def image_body
|
|
41
41
|
{ "id" => "image-123", "status" => "completed", "steps" => [
|
|
42
|
-
{ "type" => "thought", "content" => [{ "type" => "image", "data" =>
|
|
42
|
+
{ "type" => "thought", "content" => [{ "type" => "image", "data" => ["hidden"].pack("m0") }] },
|
|
43
43
|
{ "type" => "model_output", "content" => [
|
|
44
44
|
{ "type" => "text", "text" => "Here is your image" },
|
|
45
|
-
{ "type" => "image", "mime_type" => "image/png", "data" =>
|
|
46
|
-
{ "type" => "image", "mime_type" => "image/png", "data" =>
|
|
45
|
+
{ "type" => "image", "mime_type" => "image/png", "data" => [PNG].pack("m0") },
|
|
46
|
+
{ "type" => "image", "mime_type" => "image/png", "data" => ["second"].pack("m0") }
|
|
47
47
|
] }
|
|
48
48
|
] }
|
|
49
49
|
end
|
|
@@ -51,7 +51,7 @@ class GeminiImagesTest < Minitest::Test
|
|
|
51
51
|
def test_default_transport_first_image_and_envelope
|
|
52
52
|
with_http(image_body) do |http|
|
|
53
53
|
result = @client.image("A cat")
|
|
54
|
-
assert_equal({ "content" =>
|
|
54
|
+
assert_equal({ "content" => [PNG].pack("m0"), "status" => 200,
|
|
55
55
|
"error" => nil, "response_id" => "image-123", "raw" => nil }, result)
|
|
56
56
|
request = http.request_received
|
|
57
57
|
assert_equal "/v1beta/interactions", request.path
|
|
@@ -72,7 +72,7 @@ class GeminiImagesTest < Minitest::Test
|
|
|
72
72
|
result = @client.image("A cat", model: "custom-image", output_path: path, options: options, debug: true)
|
|
73
73
|
assert_nil result["error"]
|
|
74
74
|
assert_equal PNG, File.binread(path)
|
|
75
|
-
assert_equal
|
|
75
|
+
assert_equal [PNG].pack("m0"), result["content"]
|
|
76
76
|
assert_equal image_body, result["raw"]
|
|
77
77
|
payload = JSON.parse(http.request_received.body)
|
|
78
78
|
assert_equal "custom-image", payload["model"]
|
data/test/gemini_models_test.rb
CHANGED
|
@@ -65,7 +65,7 @@ class GeminiModelsTest < Minitest::Test
|
|
|
65
65
|
end
|
|
66
66
|
def test_local_configuration_and_single_paginated_check
|
|
67
67
|
with_pages do |http|
|
|
68
|
-
assert_equal({ "chat" => AiLite::Gemini::DEFAULT_MODEL, "embedding" => AiLite::Gemini::DEFAULT_EMBEDDING_MODEL, "image" => AiLite::Gemini::DEFAULT_IMAGE_MODEL }, AiLite::Gemini.configured_models)
|
|
68
|
+
assert_equal({ "chat" => AiLite::Gemini::DEFAULT_MODEL, "embedding" => AiLite::Gemini::DEFAULT_EMBEDDING_MODEL, "image" => AiLite::Gemini::DEFAULT_IMAGE_MODEL, "speech" => AiLite::Gemini::DEFAULT_SPEECH_MODEL, "transcription" => AiLite::Gemini::DEFAULT_TRANSCRIPTION_MODEL }, AiLite::Gemini.configured_models)
|
|
69
69
|
snapshot = @client.configured_models
|
|
70
70
|
snapshot["chat"].replace("changed")
|
|
71
71
|
assert_equal "models/test-model", @client.model
|