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

Commit 77bfdfa

Browse files
committed
implement PUT/DELETE/PATCH etc..
1 parent 7e3a543 commit 77bfdfa

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

lib/ai_bot/tool_runner.rb

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ def mini_racer_context
4444
end
4545

4646
def framework_script
47+
http_methods = %i[get post put patch delete].map { |method| <<~JS }.join("\n")
48+
#{method}: function(url, options) {
49+
return _http_#{method}(url, options);
50+
},
51+
JS
4752
<<~JS
4853
const http = {
49-
get: function(url, options) { return _http_get(url, options) },
50-
post: function(url, options) { return _http_post(url, options) },
54+
#{http_methods}
5155
};
5256
5357
const llm = {
@@ -249,36 +253,44 @@ def attach_http(mini_racer_context)
249253
end,
250254
)
251255

252-
mini_racer_context.attach(
253-
"_http_post",
254-
->(url, options) do
255-
begin
256-
@http_requests_made += 1
257-
if @http_requests_made > MAX_HTTP_REQUESTS
258-
raise TooManyRequestsError.new("Tool made too many HTTP requests")
259-
end
256+
%i[post put patch delete].each do |method|
257+
mini_racer_context.attach(
258+
"_http_#{method}",
259+
->(url, options) do
260+
begin
261+
@http_requests_made += 1
262+
if @http_requests_made > MAX_HTTP_REQUESTS
263+
raise TooManyRequestsError.new("Tool made too many HTTP requests")
264+
end
260265

261-
self.running_attached_function = true
262-
headers = (options && options["headers"]) || {}
263-
body = options && options["body"]
266+
self.running_attached_function = true
267+
headers = (options && options["headers"]) || {}
268+
body = options && options["body"]
269+
270+
result = {}
271+
DiscourseAi::AiBot::Tools::Tool.send_http_request(
272+
url,
273+
method: method,
274+
headers: headers,
275+
body: body,
276+
) do |response|
277+
result[:body] = response.body
278+
result[:status] = response.code.to_i
279+
end
264280

265-
result = {}
266-
DiscourseAi::AiBot::Tools::Tool.send_http_request(
267-
url,
268-
method: :post,
269-
headers: headers,
270-
body: body,
271-
) do |response|
272-
result[:body] = response.body
273-
result[:status] = response.code.to_i
281+
result
282+
rescue => e
283+
p url
284+
p options
285+
p e
286+
puts e.backtrace
287+
raise e
288+
ensure
289+
self.running_attached_function = false
274290
end
275-
276-
result
277-
ensure
278-
self.running_attached_function = false
279-
end
280-
end,
281-
)
291+
end,
292+
)
293+
end
282294
end
283295
end
284296
end

0 commit comments

Comments
 (0)