Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 86f665d

Browse files
committed
also implement encoding for get
1 parent 21a814b commit 86f665d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/personas/tool_runner.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,13 +701,18 @@ def attach_http(mini_racer_context)
701701

702702
in_attached_function do
703703
headers = (options && options["headers"]) || {}
704+
base64_encode = options && options["base64Encode"]
704705

705706
result = {}
706707
DiscourseAi::Personas::Tools::Tool.send_http_request(
707708
url,
708709
headers: headers,
709710
) do |response|
710-
result[:body] = response.body
711+
if base64_encode
712+
result[:body] = Base64.strict_encode64(response.body)
713+
else
714+
result[:body] = response.body
715+
end
711716
result[:status] = response.code.to_i
712717
end
713718

spec/models/ai_tool_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,37 @@ def create_tool(
7575
expect(Base64.strict_decode64(result).bytes).to eq((0..255).to_a)
7676
end
7777

78+
it "can base64 encode binary GET responses" do
79+
# Create binary data with all possible byte values (0-255)
80+
binary_data = (0..255).map(&:chr).join
81+
expected_base64 = Base64.strict_encode64(binary_data)
82+
83+
script = <<~JS
84+
function invoke(params) {
85+
const result = http.get("https://example.com/binary", {
86+
base64Encode: true
87+
});
88+
return result.body;
89+
}
90+
JS
91+
92+
tool = create_tool(script: script)
93+
runner = tool.runner({}, llm: nil, bot_user: nil)
94+
95+
stub_request(:get, "https://example.com/binary").to_return(
96+
status: 200,
97+
body: binary_data,
98+
headers: {
99+
},
100+
)
101+
102+
result = runner.invoke
103+
104+
expect(result).to eq(expected_base64)
105+
# Verify we can decode back to original binary data
106+
expect(Base64.strict_decode64(result).bytes).to eq((0..255).to_a)
107+
end
108+
78109
it "can perform HTTP requests with various verbs" do
79110
%i[post put delete patch].each do |verb|
80111
script = <<~JS

0 commit comments

Comments
 (0)