|
3 | 3 | RSpec.describe AiTool do |
4 | 4 | fab!(:llm_model) { Fabricate(:llm_model, name: "claude-2") } |
5 | 5 | let(:llm) { DiscourseAi::Completions::Llm.proxy("custom:#{llm_model.id}") } |
| 6 | + fab!(:topic) |
| 7 | + fab!(:post) { Fabricate(:post, topic: topic, raw: "bananas are a tasty fruit") } |
6 | 8 |
|
7 | 9 | def create_tool( |
8 | 10 | parameters: nil, |
@@ -329,36 +331,74 @@ def stub_embeddings |
329 | 331 | end |
330 | 332 | end |
331 | 333 |
|
| 334 | + context "when using the topic API" do |
| 335 | + it "can fetch topic details" do |
| 336 | + script = <<~JS |
| 337 | + function invoke(params) { |
| 338 | + return discourse.getTopic(params.topic_id); |
| 339 | + } |
| 340 | + JS |
| 341 | + |
| 342 | + tool = create_tool(script: script) |
| 343 | + runner = tool.runner({ "topic_id" => topic.id }, llm: nil, bot_user: nil, context: {}) |
| 344 | + |
| 345 | + result = runner.invoke |
| 346 | + |
| 347 | + expect(result["id"]).to eq(topic.id) |
| 348 | + expect(result["title"]).to eq(topic.title) |
| 349 | + expect(result["archetype"]).to eq("regular") |
| 350 | + expect(result["posts_count"]).to eq(1) |
| 351 | + end |
| 352 | + end |
| 353 | + |
| 354 | + context "when using the post API" do |
| 355 | + it "can fetch post details" do |
| 356 | + script = <<~JS |
| 357 | + function invoke(params) { |
| 358 | + const post = discourse.getPost(params.post_id); |
| 359 | + return { |
| 360 | + post: post, |
| 361 | + topic: post.topic |
| 362 | + } |
| 363 | + } |
| 364 | + JS |
| 365 | + |
| 366 | + tool = create_tool(script: script) |
| 367 | + runner = tool.runner({ "post_id" => post.id }, llm: nil, bot_user: nil, context: {}) |
| 368 | + |
| 369 | + result = runner.invoke |
| 370 | + post_hash = result["post"] |
| 371 | + topic_hash = result["topic"] |
| 372 | + |
| 373 | + expect(post_hash["id"]).to eq(post.id) |
| 374 | + expect(post_hash["topic_id"]).to eq(topic.id) |
| 375 | + expect(post_hash["raw"]).to eq(post.raw) |
| 376 | + |
| 377 | + expect(topic_hash["id"]).to eq(topic.id) |
| 378 | + end |
| 379 | + end |
| 380 | + |
332 | 381 | context "when using the search API" do |
333 | 382 | before { SearchIndexer.enable } |
334 | 383 | after { SearchIndexer.disable } |
335 | 384 |
|
336 | 385 | it "can perform a discourse search" do |
337 | | - # Create a new topic |
338 | | - topic = Fabricate(:topic, title: "Test Search Topic", category: Fabricate(:category)) |
339 | | - post = Fabricate(:post, topic: topic, raw: "This is a test post content, banana") |
340 | | - |
341 | | - # Ensure the topic is indexed |
342 | 386 | SearchIndexer.index(topic, force: true) |
343 | 387 | SearchIndexer.index(post, force: true) |
344 | 388 |
|
345 | | - # Define the tool script |
346 | 389 | script = <<~JS |
347 | | - function invoke(params) { |
348 | | - return discourse.search({ search_query: params.query }); |
349 | | - } |
350 | | - JS |
| 390 | + function invoke(params) { |
| 391 | + return discourse.search({ search_query: params.query }); |
| 392 | + } |
| 393 | + JS |
351 | 394 |
|
352 | | - # Create the tool and runner |
353 | 395 | tool = create_tool(script: script) |
354 | 396 | runner = tool.runner({ "query" => "banana" }, llm: nil, bot_user: nil, context: {}) |
355 | 397 |
|
356 | | - # Invoke the tool and get the results |
357 | 398 | result = runner.invoke |
358 | 399 |
|
359 | | - # Verify the topic is found |
360 | 400 | expect(result["rows"].length).to be > 0 |
361 | | - expect(result["rows"].first["title"]).to eq("Test Search Topic") |
| 401 | + expect(result["rows"].first["title"]).to eq(topic.title) |
362 | 402 | end |
363 | 403 | end |
364 | 404 | end |
0 commit comments