add OpenRouter web search option

This commit is contained in:
ajnart committed 2026-08-03 16:59:27 +02:00
1 parent 35673f113e
commit f7b291b465
25 files changed
+5753 -18

No files matched your search

+20 -2
View File
@@ -19,6 +19,7 @@ import {
items,
} from "@homarr/db/schema";
import {
assistantProviderCanUseOpenRouterServerTools,
assistantProviderIds,
assistantProviderPresets,
assistantProviderRequiresApiKey,
@@ -517,6 +518,7 @@ export const assistantRouter = createTRPCRouter({
return {
connectionConfigured: Boolean(configuration),
enabled: configuration?.enabled ?? false,
webSearchEnabled: configuration?.webSearchEnabled ?? false,
provider: configuration?.provider ?? "openrouter",
baseUrl: configuration?.baseUrl ?? "https://openrouter.ai/api/v1",
modelDiscoveryPath: configuration?.modelDiscoveryPath ?? "/models",
@@ -569,6 +571,10 @@ export const assistantRouter = createTRPCRouter({
encryptedHeaders,
modelId: connectionChanged ? null : existing.modelId,
enabled: connectionChanged ? false : existing.enabled,
webSearchEnabled:
destinationChanged || !assistantProviderCanUseOpenRouterServerTools(input.provider)
? false
: existing.webSearchEnabled,
updatedAt: new Date(),
})
.where(eq(assistantConfigurations.id, configurationId));
@@ -614,7 +620,13 @@ export const assistantRouter = createTRPCRouter({
}),
updateConfiguration: adminProcedure
.input(z.object({ enabled: z.boolean(), modelId: z.string().trim().min(1).max(256) }))
.input(
z.object({
enabled: z.boolean(),
modelId: z.string().trim().min(1).max(256),
webSearchEnabled: z.boolean().default(false),
}),
)
.mutation(async ({ ctx, input }) => {
const configuration = await getConfigurationAsync(ctx.db);
if (!configuration) {
@@ -624,6 +636,12 @@ export const assistantRouter = createTRPCRouter({
if (input.enabled && requiresApiKey && !configuration.encryptedApiKey) {
throw new TRPCError({ code: "PRECONDITION_FAILED", message: "This provider requires an API key." });
}
if (input.webSearchEnabled && !assistantProviderCanUseOpenRouterServerTools(configuration.provider)) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Web search requires OpenRouter or an OpenRouter-compatible proxy endpoint.",
});
}
const discoveredModels = configuration.modelDiscoveryPath
? await fetchModelsAsync(configuration).catch(() => null)
: null;
@@ -632,7 +650,7 @@ export const assistantRouter = createTRPCRouter({
: input.modelId;
await ctx.db
.update(assistantConfigurations)
.set({ enabled: input.enabled, modelId, updatedAt: new Date() })
.set({ enabled: input.enabled, modelId, webSearchEnabled: input.webSearchEnabled, updatedAt: new Date() })
.where(eq(assistantConfigurations.id, configurationId));
}),
@@ -82,6 +82,26 @@ describe("assistantRouter.updateConnection", () => {
});
});
test("disables OpenRouter server tools when the provider destination changes", async () => {
const db = await createConfiguredAssistantAsync();
await db
.update(assistantConfigurations)
.set({ webSearchEnabled: true })
.where(eq(assistantConfigurations.id, "default"));
const caller = assistantRouter.createCaller({ db, deviceType: undefined, session: adminSession });
await caller.updateConnection({
provider: "openai",
baseUrl: "https://api.openai.com/v1",
modelDiscoveryPath: "/models",
clearApiKey: false,
clearCustomHeaders: false,
});
const [configuration] = await db.select().from(assistantConfigurations);
expect(configuration?.webSearchEnabled).toBe(false);
});
test("preserves the persisted model when the saved connection is unchanged", async () => {
const db = await createConfiguredAssistantAsync();
const caller = assistantRouter.createCaller({ db, deviceType: undefined, session: adminSession });
@@ -139,6 +159,29 @@ describe("assistantRouter.updateConfiguration", () => {
const [configuration] = await db.select().from(assistantConfigurations);
expect(configuration?.modelId).toBe("deepseek/deepseek-v4-pro");
});
test("persists the OpenRouter web-search server tool option", async () => {
const db = await createConfiguredAssistantAsync();
const caller = assistantRouter.createCaller({ db, deviceType: undefined, session: adminSession });
await caller.updateConfiguration({ enabled: true, modelId: "example/model", webSearchEnabled: true });
const [configuration] = await db.select().from(assistantConfigurations);
expect(configuration?.webSearchEnabled).toBe(true);
});
test("rejects OpenRouter server tools for an incompatible provider", async () => {
const db = await createConfiguredAssistantAsync();
await db
.update(assistantConfigurations)
.set({ provider: "openai", baseUrl: "https://api.openai.com/v1" })
.where(eq(assistantConfigurations.id, "default"));
const caller = assistantRouter.createCaller({ db, deviceType: undefined, session: adminSession });
await expect(
caller.updateConfiguration({ enabled: true, modelId: "gpt-5", webSearchEnabled: true }),
).rejects.toThrow("Web search requires OpenRouter or an OpenRouter-compatible proxy endpoint.");
});
});
describe("assistant conversation features", () => {
@@ -1,6 +1,7 @@
import { describe, expect, test } from "vitest";
import {
assistantProviderCanUseOpenRouterServerTools,
assistantProviderIds,
assistantProviderPresets,
assistantProviderRequiresApiKey,
@@ -47,4 +48,12 @@ describe("assistant provider presets", () => {
expect(resolveAssistantModelId([model], model.id)).toBe(model.id);
expect(resolveAssistantModelId([model], getAssistantModelOptionLabel(model))).toBe(model.id);
});
test("allows OpenRouter server tools for OpenRouter, its Homarr proxy, and explicit custom proxies", () => {
expect(assistantProviderCanUseOpenRouterServerTools("openrouter")).toBe(true);
expect(assistantProviderCanUseOpenRouterServerTools("homarr")).toBe(true);
expect(assistantProviderCanUseOpenRouterServerTools("custom")).toBe(true);
expect(assistantProviderCanUseOpenRouterServerTools("openai")).toBe(false);
expect(assistantProviderCanUseOpenRouterServerTools("anthropic")).toBe(false);
});
});
@@ -0,0 +1 @@
ALTER TABLE `assistant_configuration` ADD `web_search_enabled` boolean DEFAULT false NOT NULL;
@@ -316,6 +316,13 @@
"when": 1785232800000,
"tag": "0044_add_assistant",
"breakpoints": true
},
{
"idx": 45,
"version": "5",
"when": 1785768750000,
"tag": "0045_add_assistant_web_search",
"breakpoints": true
}
]
}
@@ -0,0 +1 @@
ALTER TABLE "assistant_configuration" ADD COLUMN "web_search_enabled" boolean DEFAULT false NOT NULL;
File diff suppressed because it is too large. Load diff
@@ -78,6 +78,13 @@
"when": 1785252781398,
"tag": "0010_workable_shard",
"breakpoints": true
},
{
"idx": 11,
"version": "7",
"when": 1785768744667,
"tag": "0011_same_sir_ram",
"breakpoints": true
}
]
}
@@ -0,0 +1 @@
ALTER TABLE `assistant_configuration` ADD `web_search_enabled` integer DEFAULT false NOT NULL;
File diff suppressed because it is too large. Load diff
@@ -302,6 +302,13 @@
"when": 1785252768815,
"tag": "0042_bumpy_tattoo",
"breakpoints": true
},
{
"idx": 43,
"version": "6",
"when": 1785768736378,
"tag": "0043_swift_silver_samurai",
"breakpoints": true
}
]
}
+1
View File
@@ -484,6 +484,7 @@ export const serverSettings = mysqlTable("serverSetting", {
export const assistantConfigurations = mysqlTable("assistant_configuration", {
id: varchar({ length: 64 }).notNull().primaryKey().default("default"),
enabled: boolean().notNull().default(false),
webSearchEnabled: boolean().notNull().default(false),
provider: varchar({ length: 32 })
.$type<
| "openrouter"
+1
View File
@@ -483,6 +483,7 @@ export const serverSettings = pgTable("serverSetting", {
export const assistantConfigurations = pgTable("assistant_configuration", {
id: varchar({ length: 64 }).notNull().primaryKey().default("default"),
enabled: boolean().notNull().default(false),
webSearchEnabled: boolean().notNull().default(false),
provider: varchar({ length: 32 })
.$type<
| "openrouter"
+1
View File
@@ -469,6 +469,7 @@ export const serverSettings = sqliteTable("serverSetting", {
export const assistantConfigurations = sqliteTable("assistant_configuration", {
id: text().notNull().primaryKey().default("default"),
enabled: int({ mode: "boolean" }).notNull().default(false),
webSearchEnabled: int({ mode: "boolean" }).notNull().default(false),
provider: text()
.$type<
| "openrouter"
+7
View File
@@ -142,3 +142,10 @@ export const assistantProviderPresets = {
export const assistantProviderRequiresApiKey = (provider: AssistantProvider) =>
assistantProviderPresets[provider].requiresApiKey;
/**
* OpenRouter and Homarr Router implement OpenRouter server tools directly.
* Custom endpoints may opt in when they proxy the OpenRouter request format.
*/
export const assistantProviderCanUseOpenRouterServerTools = (provider: AssistantProvider | "homarr") =>
provider === "openrouter" || provider === "homarr" || provider === "custom";
+11
View File
@@ -1648,6 +1648,7 @@
"reported": "Reported by provider",
"estimated": "Estimated from model pricing",
"upstreamCost": "Upstream inference",
"webSearches": "Web searches",
"finishReason": "Finish reason",
"notReported": "Not reported",
"agentSteps": "Agent steps",
@@ -5334,6 +5335,16 @@
"toolsConfirmed": "Tools advertised",
"toolsUnknown": "Verify tool support"
},
"serverTools": {
"title": "Server tools",
"beta": "Beta",
"webSearch": {
"label": "Web search",
"description": "Allow the model to search the live web through OpenRouter when a request needs current information. Search usage is charged separately by OpenRouter.",
"proxyDescription": "Send OpenRouter's web-search server tool to this endpoint. Enable this only when the custom endpoint forwards the OpenRouter server-tools format, such as a Homarr Router proxy.",
"limit": "The model decides when to search, with a limit of 3 searches and 5 results per search for each request."
}
},
"enabled": {
"label": "Enable Assistant",
"description": "Shows the assistant to all signed-in users. Existing Homarr permissions still apply to every internal tool call."