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

Commit 6843eff

Browse files
committed
fix specs
1 parent e8c673f commit 6843eff

File tree

2 files changed

+98
-101
lines changed

2 files changed

+98
-101
lines changed

lib/ai_bot/bot.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ def reply(context, &update_blk)
124124
ongoing_chain &&= tool.chain_next_response?
125125
else
126126
needs_newlines = true
127-
update_blk.call(partial, cancel)
127+
if partial.is_a?(DiscourseAi::Completions::ToolCall)
128+
logger.warn("DiscourseAi: Tool not found: #{partial.tool_name}")
129+
else
130+
update_blk.call(partial, cancel)
131+
end
128132
end
129133
end
130134

spec/lib/modules/ai_bot/personas/persona_spec.rb

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,27 @@ def system_prompt
7272

7373
it "can parse string that are wrapped in quotes" do
7474
SiteSetting.ai_stability_api_key = "123"
75-
xml = <<~XML
76-
<function_calls>
77-
<invoke>
78-
<tool_name>image</tool_name>
79-
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
80-
<parameters>
81-
<prompts>["cat oil painting", "big car"]</prompts>
82-
<aspect_ratio>"16:9"</aspect_ratio>
83-
</parameters>
84-
</invoke>
85-
<invoke>
86-
<tool_name>image</tool_name>
87-
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
88-
<parameters>
89-
<prompts>["cat oil painting", "big car"]</prompts>
90-
<aspect_ratio>'16:9'</aspect_ratio>
91-
</parameters>
92-
</invoke>
93-
</function_calls>
94-
XML
9575

96-
image1, image2 =
97-
tools =
98-
DiscourseAi::AiBot::Personas::Artist.new.find_tools(
99-
xml,
100-
bot_user: nil,
101-
llm: nil,
102-
context: nil,
103-
)
104-
expect(image1.parameters[:prompts]).to eq(["cat oil painting", "big car"])
105-
expect(image1.parameters[:aspect_ratio]).to eq("16:9")
106-
expect(image2.parameters[:aspect_ratio]).to eq("16:9")
76+
tool_call =
77+
DiscourseAi::Completions::ToolCall.new(
78+
name: "image",
79+
id: "call_JtYQMful5QKqw97XFsHzPweB",
80+
parameters: {
81+
prompts: ["cat oil painting", "big car"],
82+
aspect_ratio: "16:9",
83+
},
84+
)
10785

108-
expect(tools.length).to eq(2)
86+
tool_instance =
87+
DiscourseAi::AiBot::Personas::Artist.new.find_tool(
88+
tool_call,
89+
bot_user: nil,
90+
llm: nil,
91+
context: nil,
92+
)
93+
94+
expect(tool_instance.parameters[:prompts]).to eq(["cat oil painting", "big car"])
95+
expect(tool_instance.parameters[:aspect_ratio]).to eq("16:9")
10996
end
11097

11198
it "enforces enums" do
@@ -132,42 +119,68 @@ def system_prompt
132119
</function_calls>
133120
XML
134121

135-
search1, search2 =
136-
tools =
137-
DiscourseAi::AiBot::Personas::General.new.find_tools(
138-
xml,
139-
bot_user: nil,
140-
llm: nil,
141-
context: nil,
142-
)
122+
tool_call =
123+
DiscourseAi::Completions::ToolCall.new(
124+
name: "search",
125+
id: "call_JtYQMful5QKqw97XFsHzPweB",
126+
parameters: {
127+
max_posts: "3.2",
128+
status: "cow",
129+
foo: "bar",
130+
},
131+
)
132+
133+
tool_instance =
134+
DiscourseAi::AiBot::Personas::General.new.find_tool(
135+
tool_call,
136+
bot_user: nil,
137+
llm: nil,
138+
context: nil,
139+
)
140+
141+
expect(tool_instance.parameters.key?(:status)).to eq(false)
142+
143+
tool_call =
144+
DiscourseAi::Completions::ToolCall.new(
145+
name: "search",
146+
id: "call_JtYQMful5QKqw97XFsHzPweB",
147+
parameters: {
148+
max_posts: "3.2",
149+
status: "open",
150+
foo: "bar",
151+
},
152+
)
143153

144-
expect(search1.parameters.key?(:status)).to eq(false)
145-
expect(search2.parameters[:status]).to eq("open")
154+
tool_instance =
155+
DiscourseAi::AiBot::Personas::General.new.find_tool(
156+
tool_call,
157+
bot_user: nil,
158+
llm: nil,
159+
context: nil,
160+
)
161+
162+
expect(tool_instance.parameters[:status]).to eq("open")
146163
end
147164

148165
it "can coerce integers" do
149-
xml = <<~XML
150-
<function_calls>
151-
<invoke>
152-
<tool_name>search</tool_name>
153-
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
154-
<parameters>
155-
<max_posts>"3.2"</max_posts>
156-
<search_query>hello world</search_query>
157-
<foo>bar</foo>
158-
</parameters>
159-
</invoke>
160-
</function_calls>
161-
XML
166+
tool_call =
167+
DiscourseAi::Completions::ToolCall.new(
168+
name: "search",
169+
id: "call_JtYQMful5QKqw97XFsHzPweB",
170+
parameters: {
171+
max_posts: "3.2",
172+
search_query: "hello world",
173+
foo: "bar",
174+
},
175+
)
162176

163-
search, =
164-
tools =
165-
DiscourseAi::AiBot::Personas::General.new.find_tools(
166-
xml,
167-
bot_user: nil,
168-
llm: nil,
169-
context: nil,
170-
)
177+
search =
178+
DiscourseAi::AiBot::Personas::General.new.find_tool(
179+
tool_call,
180+
bot_user: nil,
181+
llm: nil,
182+
context: nil,
183+
)
171184

172185
expect(search.parameters[:max_posts]).to eq(3)
173186
expect(search.parameters[:search_query]).to eq("hello world")
@@ -177,43 +190,23 @@ def system_prompt
177190
it "can correctly parse arrays in tools" do
178191
SiteSetting.ai_openai_api_key = "123"
179192

180-
# Dall E tool uses an array for params
181-
xml = <<~XML
182-
<function_calls>
183-
<invoke>
184-
<tool_name>dall_e</tool_name>
185-
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
186-
<parameters>
187-
<prompts>["cat oil painting", "big car"]</prompts>
188-
</parameters>
189-
</invoke>
190-
<invoke>
191-
<tool_name>dall_e</tool_name>
192-
<tool_id>abc</tool_id>
193-
<parameters>
194-
<prompts>["pic3"]</prompts>
195-
</parameters>
196-
</invoke>
197-
<invoke>
198-
<tool_name>unknown</tool_name>
199-
<tool_id>abc</tool_id>
200-
<parameters>
201-
<prompts>["pic3"]</prompts>
202-
</parameters>
203-
</invoke>
204-
</function_calls>
205-
XML
206-
dall_e1, dall_e2 =
207-
tools =
208-
DiscourseAi::AiBot::Personas::DallE3.new.find_tools(
209-
xml,
210-
bot_user: nil,
211-
llm: nil,
212-
context: nil,
213-
)
214-
expect(dall_e1.parameters[:prompts]).to eq(["cat oil painting", "big car"])
215-
expect(dall_e2.parameters[:prompts]).to eq(["pic3"])
216-
expect(tools.length).to eq(2)
193+
tool_call =
194+
DiscourseAi::Completions::ToolCall.new(
195+
name: "dall_e",
196+
id: "call_JtYQMful5QKqw97XFsHzPweB",
197+
parameters: {
198+
prompts: ["cat oil painting", "big car"],
199+
},
200+
)
201+
202+
tool_instance =
203+
DiscourseAi::AiBot::Personas::DallE3.new.find_tool(
204+
tool_call,
205+
bot_user: nil,
206+
llm: nil,
207+
context: nil,
208+
)
209+
expect(tool_instance.parameters[:prompts]).to eq(["cat oil painting", "big car"])
217210
end
218211

219212
describe "custom personas" do

0 commit comments

Comments
 (0)