discourse/plugins/discourse-ai/spec/configuration/llm_enumerator_spec.rb
Rafael dos Santos Silva bc39aacc3d
FEATURE: Provider-native built-in tools for agents (web search) (#40809)
Adds a fourth kind of agent tool: provider-native built-in tools that
the
LLM provider executes server-side, rather than tools Discourse runs and
feeds back. The first one is web search, supported on Gemini (Google
Search
grounding), OpenAI (web search via the Responses API) and Anthropic
(Claude
web search).

Native tools are stored on the agent's `tools` column with a `native-`
prefix, flow to the prompt as a separate `native_tools` list (never as
runnable Tool classes), and each provider dialect renders them into its
own
request payload. Response processors already ignore the server-side
tool/grounding blocks, so the bot loop never tries to execute them.

They are only selectable when the agent forces a default LLM whose
provider
supports the tool; this is enforced both in the editor UI (filtered by
the
selected LLM's `supported_native_tools`) and by server-side validation.

Also fixes the Gemini endpoint sending `function_calling_config` without
any
`function_declarations`, which the API rejects when only native tools
are
present.

---------

Co-authored-by: Sam Saffron <sam.saffron@gmail.com>
2026-06-16 14:37:51 -03:00

79 lines
2.5 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseAi::Configuration::LlmEnumerator do
fab!(:fake_model)
fab!(:ai_agent) { Fabricate(:ai_agent, default_llm_id: fake_model.id) }
fab!(:llm_model)
fab!(:seeded_model)
fab!(:automation) do
Fabricate(:automation, script: "llm_report", name: "some automation", enabled: true)
end
before { enable_current_plugin }
describe "#values_for_serialization" do
it "returns an array for that can be used for serialization" do
fake_model.destroy!
expect(described_class.values_for_serialization).to contain_exactly(
{
id: seeded_model.id,
name: seeded_model.display_name,
vision_enabled: seeded_model.vision_enabled,
supported_native_tools: [],
},
{
id: llm_model.id,
name: llm_model.display_name,
vision_enabled: llm_model.vision_enabled,
supported_native_tools: [],
},
)
end
it "advertises supported provider-native tools" do
gemini = Fabricate(:gemini_model)
openai_responses = Fabricate(:llm_model, url: "https://api.openai.com/v1/responses")
serialized = described_class.values_for_serialization.find { |row| row[:id] == gemini.id }
expect(serialized[:supported_native_tools]).to eq(%w[web_search web_fetch])
serialized =
described_class.values_for_serialization.find { |row| row[:id] == openai_responses.id }
expect(serialized[:supported_native_tools]).to eq(["web_search"])
end
end
describe "#global_usage" do
it "returns a hash of Llm models in use globally" do
assign_fake_provider_to(:ai_default_llm_model)
SiteSetting.ai_helper_proofreader_agent = ai_agent.id
SiteSetting.ai_helper_enabled = true
expect(described_class.global_usage).to eq(
fake_model.id => [{ type: :ai_helper }],
fake_model.id => [
{ id: ai_agent.id, name: ai_agent.name, type: :ai_agent },
{ name: "Proofread text", type: :ai_helper },
],
)
end
it "returns information about automation rules" do
automation.fields.create!(
component: "text",
name: "model",
metadata: {
value: llm_model.id,
},
target: "script",
)
usage = described_class.global_usage
expect(usage).to eq(
fake_model.id => [{ id: ai_agent.id, name: ai_agent.name, type: :ai_agent }],
llm_model.id => [{ id: automation.id, name: automation.name, type: :automation }],
)
end
end
end