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

Commit 30ebe56

Browse files
committed
work in progress
1 parent fd7ccfd commit 30ebe56

File tree

10 files changed

+45
-55
lines changed

10 files changed

+45
-55
lines changed

lib/ai_bot/bot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def invoke_tool(tool, llm, cancel, context, &update_blk)
190190
update_blk.call("", cancel, build_placeholder(tool.summary, ""))
191191

192192
result =
193-
tool.invoke do |progress|
193+
tool.invoke(**tool.parameters) do |progress|
194194
placeholder = build_placeholder(tool.summary, progress)
195195
update_blk.call("", cancel, placeholder)
196196
end

lib/ai_bot/personas/persona.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,16 @@ def tool_instance(parsed_function, bot_user:, llm:, context:)
243243
arguments[name.to_sym] = value if value
244244
end
245245

246-
tool_klass.new(
247-
arguments,
246+
instance = tool_klass.new(
248247
tool_call_id: function_id || function_name,
249248
persona_options: options[tool_klass].to_h,
250249
bot_user: bot_user,
251250
llm: llm,
252251
context: context,
253252
)
253+
254+
instance.parameters = arguments
255+
instance
254256
end
255257

256258
def strip_quotes(value)

lib/ai_bot/tools/custom.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def initialize(*args, **kwargs)
3535
super(*args, **kwargs)
3636
end
3737

38-
def invoke
38+
def invoke(**parameters)
39+
@parameters = parameters
3940
result = runner.invoke
4041
if runner.custom_raw
4142
self.custom_raw = runner.custom_raw

lib/ai_bot/tools/dall_e.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module DiscourseAi
44
module AiBot
55
module Tools
66
class DallE < Tool
7+
attr_reader :prompts, :aspect_ratio
8+
79
def self.signature
810
{
911
name: name,
@@ -32,19 +34,14 @@ def self.name
3234
"dall_e"
3335
end
3436

35-
def prompts
36-
parameters[:prompts]
37-
end
38-
39-
def aspect_ratio
40-
parameters[:aspect_ratio]
41-
end
42-
4337
def chain_next_response?
4438
false
4539
end
4640

47-
def invoke
41+
def invoke(prompts:, aspect_ratio: "square")
42+
@prompts = prompts
43+
@aspect_ratio = aspect_ratio
44+
4845
# max 4 prompts
4946
max_prompts = prompts.take(4)
5047
progress = prompts.first

lib/ai_bot/tools/google.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module DiscourseAi
44
module AiBot
55
module Tools
66
class Google < Tool
7+
attr_reader :query
8+
79
def self.signature
810
{
911
name: name,
@@ -27,12 +29,9 @@ def self.accepted_options
2729
[option(:base_query, type: :string)]
2830
end
2931

30-
def query
31-
parameters[:query].to_s.strip
32-
end
33-
34-
def invoke
35-
query = self.query
32+
def invoke(query:)
33+
query = query.to_s.strip
34+
@query = query
3635

3736
yield(query)
3837

lib/ai_bot/tools/time.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module DiscourseAi
44
module AiBot
55
module Tools
66
class Time < Tool
7+
attr_reader :timezone
8+
79
def self.signature
810
{
911
name: name,
@@ -23,11 +25,8 @@ def self.name
2325
"time"
2426
end
2527

26-
def timezone
27-
parameters[:timezone].to_s
28-
end
29-
30-
def invoke
28+
def invoke(timezone:)
29+
@timezone = timezone
3130
time =
3231
begin
3332
::Time.now.in_time_zone(timezone)
@@ -37,7 +36,6 @@ def invoke
3736
time = ::Time.now if !time
3837

3938
@last_time = time.to_s
40-
4139
{ args: { timezone: timezone }, time: time.to_s }
4240
end
4341

lib/ai_bot/tools/tool.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ def custom_system_message
4040
end
4141
end
4242

43-
attr_accessor :custom_raw
44-
attr_reader :tool_call_id, :persona_options, :bot_user, :llm, :context, :parameters
43+
attr_accessor :custom_raw, :parameters
44+
attr_reader :tool_call_id, :persona_options, :bot_user, :llm, :context
4545

4646
def initialize(
47-
parameters,
4847
tool_call_id: "",
4948
persona_options: {},
5049
bot_user:,
5150
llm:,
5251
context: {}
5352
)
54-
@parameters = parameters
5553
@tool_call_id = tool_call_id
5654
@persona_options = persona_options
5755
@bot_user = bot_user

lib/ai_bot/tools/web_browser.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module DiscourseAi
44
module AiBot
55
module Tools
66
class WebBrowser < Tool
7+
attr_reader :url
8+
79
def self.signature
810
{
911
name: name,
@@ -24,15 +26,9 @@ def self.name
2426
"web_browser"
2527
end
2628

27-
def url
28-
return @url if defined?(@url)
29-
@url = parameters[:url]
30-
@url = "https://#{@url}" if !@url.start_with?("http")
31-
32-
@url
33-
end
34-
35-
def invoke
29+
def invoke(url:)
30+
url = "https://#{url}" if !url.start_with?("http")
31+
@url = url
3632
send_http_request(url, follow_redirects: true) do |response|
3733
if response.code == "200"
3834
html = read_response_body(response)

spec/lib/modules/ai_bot/tools/google_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let(:bot_user) { DiscourseAi::AiBot::EntryPoint.find_user_from_model(llm_model.name) }
66
let(:llm) { DiscourseAi::Completions::Llm.proxy("custom:#{llm_model.id}") }
77
let(:progress_blk) { Proc.new {} }
8-
let(:search) { described_class.new({ query: "some search term" }, bot_user: bot_user, llm: llm) }
8+
let(:search) { described_class.new(bot_user: bot_user, llm: llm) }
99

1010
before do
1111
SiteSetting.ai_bot_enabled = true
@@ -22,7 +22,7 @@
2222
"https://www.googleapis.com/customsearch/v1?cx=cx&key=abc&num=10&q=some%20search%20term",
2323
).to_return(status: 200, body: json_text, headers: {})
2424

25-
info = search.invoke(&progress_blk).to_json
25+
info = search.invoke(query: "some search term", &progress_blk).to_json
2626

2727
expect(search.results_count).to eq(0)
2828
expect(info).to_not include("oops")
@@ -33,7 +33,6 @@
3333

3434
search =
3535
described_class.new(
36-
{ query: "some search term" },
3736
bot_user: bot_user,
3837
llm: llm,
3938
persona_options: {
@@ -48,7 +47,7 @@
4847
"https://www.googleapis.com/customsearch/v1?cx=cx&key=abc&num=10&q=site:discourse.org%20some%20search%20term",
4948
).to_return(status: 200, body: json_text, headers: {})
5049

51-
search.invoke(&progress_blk)
50+
search.invoke(query: "some search term", &progress_blk)
5251
end
5352

5453
it "can generate correct info" do
@@ -80,7 +79,7 @@
8079
"https://www.googleapis.com/customsearch/v1?cx=cx&key=abc&num=10&q=some%20search%20term",
8180
).to_return(status: 200, body: json_text, headers: {})
8281

83-
info = search.invoke(&progress_blk).to_json
82+
info = search.invoke(query: "some search term", &progress_blk).to_json
8483

8584
expect(search.results_count).to eq(2)
8685
expect(info).to include("title1")

spec/lib/modules/ai_bot/tools/web_browser_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"<html><head><title>Test</title></head><body><p>This is a simplified version of the webpage content.</p></body></html>",
2020
)
2121

22-
tool = described_class.new({ url: url }, bot_user: bot_user, llm: llm)
23-
result = tool.invoke
22+
tool = described_class.new(bot_user: bot_user, llm: llm)
23+
result = tool.invoke(url: url)
2424

2525
expect(result).to have_key(:text)
2626
expect(result[:text]).to eq(processed_text)
@@ -33,8 +33,8 @@
3333
# Simulating a failed request
3434
stub_request(:get, url).to_return(status: [500, "Internal Server Error"])
3535

36-
tool = described_class.new({ url: url }, bot_user: bot_user, llm: llm)
37-
result = tool.invoke
36+
tool = described_class.new(bot_user: bot_user, llm: llm)
37+
result = tool.invoke(url: url)
3838

3939
expect(result).to have_key(:error)
4040
expect(result[:error]).to include("Failed to retrieve the web page")
@@ -48,8 +48,8 @@
4848
simple_html = "<html><body><p>Simple content.</p></body></html>"
4949
stub_request(:get, url).to_return(status: 200, body: simple_html)
5050

51-
tool = described_class.new({ url: url }, bot_user: bot_user, llm: llm)
52-
result = tool.invoke
51+
tool = described_class.new(bot_user: bot_user, llm: llm)
52+
result = tool.invoke(url: url)
5353

5454
expect(result[:text]).to eq("Simple content.")
5555
end
@@ -59,8 +59,8 @@
5959
"<html><head><script>console.log('Ignore me')</script></head><body><style>body { background-color: #000; }</style><p>Only relevant content here.</p></body></html>"
6060
stub_request(:get, url).to_return(status: 200, body: complex_html)
6161

62-
tool = described_class.new({ url: url }, bot_user: bot_user, llm: llm)
63-
result = tool.invoke
62+
tool = described_class.new(bot_user: bot_user, llm: llm)
63+
result = tool.invoke(url: url)
6464

6565
expect(result[:text]).to eq("Only relevant content here.")
6666
end
@@ -70,8 +70,8 @@
7070
"<html><body><div><section><p>Nested paragraph 1.</p></section><section><p>Nested paragraph 2.</p></section></div></body></html>"
7171
stub_request(:get, url).to_return(status: 200, body: nested_html)
7272

73-
tool = described_class.new({ url: url }, bot_user: bot_user, llm: llm)
74-
result = tool.invoke
73+
tool = described_class.new(bot_user: bot_user, llm: llm)
74+
result = tool.invoke(url: url)
7575

7676
expect(result[:text]).to eq("Nested paragraph 1. Nested paragraph 2.")
7777
end
@@ -86,8 +86,8 @@
8686
stub_request(:get, initial_url).to_return(status: 302, headers: { "Location" => final_url })
8787
stub_request(:get, final_url).to_return(status: 200, body: redirect_html)
8888

89-
tool = described_class.new({ url: initial_url }, bot_user: bot_user, llm: llm)
90-
result = tool.invoke
89+
tool = described_class.new(bot_user: bot_user, llm: llm)
90+
result = tool.invoke(url: initial_url)
9191

9292
expect(result[:url]).to eq(final_url)
9393
expect(result[:text]).to eq("Redirected content.")

0 commit comments

Comments
 (0)