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

Commit 21a814b

Browse files
committed
FEATURE: allow base 64 encoding results from an http call
1 parent d22d5ad commit 21a814b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/personas/tool_runner.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ def attach_http(mini_racer_context)
730730
in_attached_function do
731731
headers = (options && options["headers"]) || {}
732732
body = options && options["body"]
733+
base64_encode = options && options["base64Encode"]
733734

734735
result = {}
735736
DiscourseAi::Personas::Tools::Tool.send_http_request(
@@ -738,7 +739,11 @@ def attach_http(mini_racer_context)
738739
headers: headers,
739740
body: body,
740741
) do |response|
741-
result[:body] = response.body
742+
if base64_encode
743+
result[:body] = Base64.strict_encode64(response.body)
744+
else
745+
result[:body] = response.body
746+
end
742747
result[:status] = response.code.to_i
743748
end
744749

spec/models/ai_tool_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ def create_tool(
4343
expect(runner.invoke).to eq("query" => "test")
4444
end
4545

46+
it "can base64 encode binary HTTP responses" do
47+
# Create binary data with all possible byte values (0-255)
48+
binary_data = (0..255).map(&:chr).join
49+
expected_base64 = Base64.strict_encode64(binary_data)
50+
51+
script = <<~JS
52+
function invoke(params) {
53+
const result = http.post("https://example.com/binary", {
54+
body: "test",
55+
base64Encode: true
56+
});
57+
return result.body;
58+
}
59+
JS
60+
61+
tool = create_tool(script: script)
62+
runner = tool.runner({}, llm: nil, bot_user: nil)
63+
64+
stub_request(:post, "https://example.com/binary").to_return(
65+
status: 200,
66+
body: binary_data,
67+
headers: {
68+
},
69+
)
70+
71+
result = runner.invoke
72+
73+
expect(result).to eq(expected_base64)
74+
# Verify we can decode back to original binary data
75+
expect(Base64.strict_decode64(result).bytes).to eq((0..255).to_a)
76+
end
77+
4678
it "can perform HTTP requests with various verbs" do
4779
%i[post put delete patch].each do |verb|
4880
script = <<~JS

0 commit comments

Comments
 (0)