feat: add Homarr assistant

This commit is contained in:
ajnart committed 2026-08-13 19:56:10 +02:00
1 parent 3b6de1f91f
commit fd69cd28f0
27 files changed
+5210 -772

No files matched your search

+2
View File
@@ -27,6 +27,7 @@ import { isLocaleRTL, isLocaleSupported } from "@homarr/translation";
import { resolveHomarrUrlConfig } from "@homarr/workshop/schema";
import { Analytics } from "~/components/layout/analytics";
import { AssistantProvider } from "~/components/assistant/assistant-provider";
import { CrowdinLiveTranslation } from "~/components/layout/crowdin-live-translation";
import { env } from "~/env";
@@ -131,6 +132,7 @@ export default async function Layout(props: {
(innerProps) => <CustomMantineProvider {...innerProps} defaultColorScheme={colorScheme} />,
(innerProps) => <ModalProvider {...innerProps} />,
(innerProps) => <SpotlightProvider {...innerProps} />,
(innerProps) => <AssistantProvider {...innerProps} />,
]);
const { locale } = await props.params;
@@ -0,0 +1,437 @@
"use client";
import { useEffect, useMemo, useState } from "react";
import {
ActionIcon,
Alert,
Autocomplete,
Badge,
Button,
Card,
Divider,
Group,
LoadingOverlay,
PasswordInput,
Select,
SimpleGrid,
Stack,
Switch,
Text,
TextInput,
Title,
Tooltip,
} from "@mantine/core";
import {
IconAlertTriangle,
IconDatabaseSearch,
IconKey,
IconPlus,
IconRobot,
IconShieldLock,
IconTrash,
IconWorld,
} from "@tabler/icons-react";
import { clientApi } from "@homarr/api/client";
import { showErrorNotification, showSuccessNotification } from "@homarr/notifications";
import { useScopedI18n } from "@homarr/translation/client";
const providerPresets = {
openrouter: {
baseUrl: "https://openrouter.ai/api/v1",
discoveryPath: "/models",
requiresApiKey: true,
},
openai: {
baseUrl: "https://api.openai.com/v1",
discoveryPath: "/models",
requiresApiKey: true,
},
ollama: {
baseUrl: "http://localhost:11434/v1",
discoveryPath: "/models",
requiresApiKey: false,
},
"lm-studio": {
baseUrl: "http://localhost:1234/v1",
discoveryPath: "/models",
requiresApiKey: false,
},
custom: {
baseUrl: "",
discoveryPath: "/models",
requiresApiKey: false,
},
} as const;
type Provider = keyof typeof providerPresets;
type HeaderEntry = { id: number; name: string; value: string };
export const AssistantSettings = () => {
const t = useScopedI18n("management.page.settings.section.assistant");
const utils = clientApi.useUtils();
const { data: configuration, isLoading } = clientApi.assistant.getAdminConfiguration.useQuery();
const [provider, setProvider] = useState<Provider>("openrouter");
const [baseUrl, setBaseUrl] = useState<string>(providerPresets.openrouter.baseUrl);
const [modelDiscoveryPath, setModelDiscoveryPath] = useState<string>("/models");
const [apiKey, setApiKey] = useState("");
const [clearApiKey, setClearApiKey] = useState(false);
const [headers, setHeaders] = useState<HeaderEntry[]>([]);
const [clearHeaders, setClearHeaders] = useState(false);
const [nextHeaderId, setNextHeaderId] = useState(1);
const [modelId, setModelId] = useState("");
const [enabled, setEnabled] = useState(false);
useEffect(() => {
setProvider(configuration?.provider ?? "openrouter");
setBaseUrl(configuration?.baseUrl ?? providerPresets.openrouter.baseUrl);
setModelDiscoveryPath(configuration?.modelDiscoveryPath ?? "");
setModelId(configuration?.modelId ?? "");
setEnabled(configuration?.enabled ?? false);
}, [configuration]);
const {
data: models,
error: discoveryError,
isFetching: isDiscovering,
refetch: discoverModels,
} = clientApi.assistant.discoverModels.useQuery(undefined, {
enabled: configuration?.connectionConfigured === true && configuration.modelDiscoveryPath !== null,
staleTime: 5 * 60_000,
retry: false,
});
const modelOptions = useMemo(
() =>
(models ?? []).map((model) => ({
value: model.id,
label: model.name === model.id ? model.id : `${model.name} (${model.id})`,
})),
[models],
);
const destinationChanged =
configuration?.connectionConfigured === true &&
(configuration.provider !== provider || configuration.baseUrl !== baseUrl.trim().replace(/\/$/, ""));
const preset = providerPresets[provider];
const headerValuesValid = headers.every((header) => header.name.trim().length > 0 && header.value.length > 0);
const hasEffectiveApiKey =
apiKey.trim().length > 0 || (!destinationChanged && configuration?.apiKeyConfigured === true && !clearApiKey);
const connectionValid =
baseUrl.trim().length > 0 && headerValuesValid && (!preset.requiresApiKey || hasEffectiveApiKey);
const updateConnection = clientApi.assistant.updateConnection.useMutation({
onSuccess: async ({ credentialsClearedForDestinationChange }) => {
setApiKey("");
setClearApiKey(false);
setHeaders([]);
setClearHeaders(false);
await Promise.all([
utils.assistant.getAdminConfiguration.invalidate(),
utils.assistant.discoverModels.invalidate(),
utils.assistant.getAvailability.invalidate(),
]);
showSuccessNotification({
title: t("notification.connectionSaved.title"),
message: credentialsClearedForDestinationChange
? t("notification.connectionSaved.credentialsCleared")
: t("notification.connectionSaved.message"),
});
},
onError: (error) => showErrorNotification({ title: t("notification.error.title"), message: error.message }),
});
const saveConfiguration = clientApi.assistant.updateConfiguration.useMutation({
onSuccess: async () => {
await Promise.all([
utils.assistant.getAdminConfiguration.invalidate(),
utils.assistant.getAvailability.invalidate(),
]);
showSuccessNotification({
title: t("notification.saved.title"),
message: t("notification.saved.message"),
});
},
onError: (error) => showErrorNotification({ title: t("notification.error.title"), message: error.message }),
});
const clearCredentials = clientApi.assistant.clearCredentials.useMutation({
onSuccess: async () => {
setApiKey("");
setHeaders([]);
setModelId("");
setEnabled(false);
await Promise.all([
utils.assistant.getAdminConfiguration.invalidate(),
utils.assistant.discoverModels.invalidate(),
utils.assistant.getAvailability.invalidate(),
]);
showSuccessNotification({
title: t("notification.credentialsCleared.title"),
message: t("notification.credentialsCleared.message"),
});
},
onError: (error) => showErrorNotification({ title: t("notification.error.title"), message: error.message }),
});
const pending = isLoading || updateConnection.isPending || saveConfiguration.isPending || clearCredentials.isPending;
const changeProvider = (value: string | null) => {
if (!value || !(value in providerPresets)) return;
const nextProvider = value as Provider;
const nextPreset = providerPresets[nextProvider];
setProvider(nextProvider);
setBaseUrl(nextPreset.baseUrl);
setModelDiscoveryPath(nextPreset.discoveryPath);
setEnabled(false);
};
const addHeader = () => {
setHeaders((current) => [...current, { id: nextHeaderId, name: "", value: "" }]);
setNextHeaderId((current) => current + 1);
setClearHeaders(false);
};
const updateHeader = (id: number, field: "name" | "value", value: string) => {
setHeaders((current) => current.map((header) => (header.id === id ? { ...header, [field]: value } : header)));
};
const saveConnection = () => {
const customHeaders =
headers.length > 0 ? Object.fromEntries(headers.map((header) => [header.name.trim(), header.value])) : undefined;
updateConnection.mutate({
provider,
baseUrl,
modelDiscoveryPath: modelDiscoveryPath.trim() || null,
apiKey: apiKey.trim() || undefined,
clearApiKey,
customHeaders,
clearCustomHeaders: clearHeaders,
});
};
return (
<Stack>
<Group gap="sm">
<IconRobot size={26} />
<Title order={2}>{t("title")}</Title>
</Group>
<Text c="dimmed">{t("description")}</Text>
<Card pos="relative" withBorder>
<LoadingOverlay visible={pending} />
<Stack gap="lg">
<Alert icon={<IconShieldLock size={18} />} color="blue" title={t("security.title")}>
{t("security.description")}
</Alert>
<Group justify="space-between" align="flex-start">
<div>
<Text fw={600}>{t("connection.title")}</Text>
<Text size="sm" c="dimmed">
{t("connection.description")}
</Text>
</div>
<Badge variant="light" color={configuration?.connectionConfigured ? "green" : "gray"}>
{configuration?.connectionConfigured ? t("connection.saved") : t("connection.notSaved")}
</Badge>
</Group>
<SimpleGrid cols={{ base: 1, sm: 2 }}>
<Select
label={t("provider.title")}
description={t(`provider.options.${provider}.description`)}
value={provider}
onChange={changeProvider}
data={Object.keys(providerPresets).map((value) => ({
value,
label: t(`provider.options.${value as Provider}.label`),
}))}
/>
<TextInput
label={t("baseUrl.title")}
description={t("baseUrl.description")}
leftSection={<IconWorld size={16} />}
value={baseUrl}
onChange={(event) => setBaseUrl(event.currentTarget.value)}
placeholder="https://provider.example/v1"
/>
</SimpleGrid>
<TextInput
label={t("model.discoveryPath")}
description={t("model.discoveryPathDescription")}
value={modelDiscoveryPath}
onChange={(event) => setModelDiscoveryPath(event.currentTarget.value)}
placeholder="/models"
/>
{destinationChanged && (
<Alert icon={<IconAlertTriangle size={18} />} color="yellow" title={t("destinationChanged.title")}>
{t("destinationChanged.description")}
</Alert>
)}
<Stack gap="xs">
<Group justify="space-between" align="flex-end">
<div>
<Text fw={600}>{t("apiKey.title")}</Text>
<Text size="sm" c="dimmed">
{preset.requiresApiKey ? t("apiKey.required") : t("apiKey.optional")}
</Text>
</div>
{configuration?.apiKeyConfigured && (
<Badge variant="light" color={clearApiKey ? "yellow" : "green"}>
{clearApiKey ? t("apiKey.pendingRemoval") : t("apiKey.configuredBadge")}
</Badge>
)}
</Group>
<PasswordInput
value={apiKey}
onChange={(event) => {
setApiKey(event.currentTarget.value);
setClearApiKey(false);
}}
leftSection={<IconKey size={16} />}
placeholder={configuration?.apiKeyConfigured ? t("apiKey.configured") : t("apiKey.placeholder")}
autoComplete="new-password"
/>
{configuration?.apiKeyConfigured && (
<Button
variant="subtle"
color={clearApiKey ? "gray" : "red"}
onClick={() => setClearApiKey(!clearApiKey)}
>
{clearApiKey ? t("apiKey.keep") : t("apiKey.clear")}
</Button>
)}
</Stack>
<Divider />
<Stack gap="xs">
<Group justify="space-between" align="flex-start">
<div>
<Text fw={600}>{t("headers.title")}</Text>
<Text size="sm" c="dimmed">
{t("headers.description")}
</Text>
</div>
<Button variant="subtle" size="compact-sm" leftSection={<IconPlus size={14} />} onClick={addHeader}>
{t("headers.add")}
</Button>
</Group>
{configuration?.customHeadersConfigured && headers.length === 0 && (
<Alert color={clearHeaders ? "yellow" : "gray"}>{t("headers.configured")}</Alert>
)}
{headers.map((header) => (
<Group key={header.id} align="flex-end" wrap="wrap">
<TextInput
flex={1}
miw="12rem"
label={t("headers.name")}
value={header.name}
onChange={(event) => updateHeader(header.id, "name", event.currentTarget.value)}
placeholder="X-Provider-Header"
/>
<PasswordInput
flex={2}
miw="12rem"
label={t("headers.value")}
value={header.value}
onChange={(event) => updateHeader(header.id, "value", event.currentTarget.value)}
placeholder={t("headers.valuePlaceholder")}
autoComplete="off"
/>
<Tooltip label={t("headers.remove")}>
<ActionIcon
size="lg"
variant="subtle"
color="red"
aria-label={t("headers.remove")}
onClick={() => setHeaders((current) => current.filter((item) => item.id !== header.id))}
>
<IconTrash size={17} />
</ActionIcon>
</Tooltip>
</Group>
))}
{configuration?.customHeadersConfigured && headers.length === 0 && (
<Button
variant="subtle"
color={clearHeaders ? "gray" : "red"}
onClick={() => setClearHeaders(!clearHeaders)}
>
{clearHeaders ? t("headers.keep") : t("headers.clear")}
</Button>
)}
</Stack>
<Group justify="space-between">
<Button onClick={saveConnection} disabled={!connectionValid}>
{t("connection.save")}
</Button>
{(configuration?.apiKeyConfigured || configuration?.customHeadersConfigured) && (
<Button variant="subtle" color="red" onClick={() => clearCredentials.mutate()}>
{t("connection.clearCredentials")}
</Button>
)}
</Group>
<Divider />
<Stack gap="xs">
<Group justify="space-between" align="flex-start">
<div>
<Text fw={600}>{t("model.title")}</Text>
<Text size="sm" c="dimmed">
{t("model.description")}
</Text>
</div>
<Button
variant="subtle"
size="compact-sm"
leftSection={<IconDatabaseSearch size={14} />}
loading={isDiscovering}
disabled={!configuration?.connectionConfigured || configuration.modelDiscoveryPath === null}
onClick={() => void discoverModels()}
>
{t("model.refresh")}
</Button>
</Group>
<Autocomplete
value={modelId}
onChange={setModelId}
data={modelOptions}
placeholder={t("model.placeholder")}
description={
discoveryError
? t("model.manualFallback")
: models
? t("model.discovered", { count: models.length })
: t("model.manual")
}
limit={100}
/>
</Stack>
<Switch
checked={enabled}
onChange={(event) => setEnabled(event.currentTarget.checked)}
disabled={!configuration?.connectionConfigured || modelId.trim().length === 0}
label={t("enabled.label")}
description={t("enabled.description")}
/>
<Group justify="flex-end">
<Button
onClick={() => saveConfiguration.mutate({ enabled, modelId: modelId.trim() })}
disabled={!configuration?.connectionConfigured || modelId.trim().length === 0}
>
{t("save")}
</Button>
</Group>
</Stack>
</Card>
</Stack>
);
};
@@ -0,0 +1,268 @@
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import type { ToolSet, UIMessage } from "ai";
import { convertToModelMessages, jsonSchema, stepCountIs, streamText, tool } from "ai";
import { parse, stringify } from "superjson";
import { z } from "zod/v4";
import { createTRPCContext, mcpRouter } from "@homarr/api/mcp";
import { auth } from "@homarr/auth/next";
import { env } from "@homarr/common/env";
import { decryptSecret } from "@homarr/common/server";
import { createLogger } from "@homarr/core/infrastructure/logs";
import { and, eq } from "@homarr/db";
import { db } from "@homarr/db";
import { assistantConfigurations, assistantThreads } from "@homarr/db/schema";
import { extractMcpTools } from "../../mcp/_extract-tools";
export const maxDuration = 60;
const logger = createLogger({ module: "assistant" });
const requestSchema = z.object({
id: z.string().min(1).max(64),
messages: z
.array(
z
.object({
id: z.string().min(1).max(128),
role: z.enum(["user", "assistant"]),
parts: z.array(z.unknown()).max(200),
})
.passthrough(),
)
.max(200),
tools: z
.record(
z.string(),
z.object({
description: z.string().optional(),
parameters: z.record(z.string(), z.unknown()),
providerOptions: z.record(z.string(), z.unknown()).optional(),
}),
)
.refine((tools) => tools === undefined || Object.keys(tools).length <= 16)
.optional(),
});
const browserToolDefinitions = {
navigate_to_route: {
description: "Navigate the current Homarr tab to a safe internal route.",
parameters: {
type: "object",
properties: { path: { type: "string", description: "An internal Homarr path beginning with /" } },
required: ["path"],
additionalProperties: false,
},
},
open_command_menu: {
description: "Open Homarr's command and search menu.",
parameters: { type: "object", properties: {}, additionalProperties: false },
},
open_media_request_search: {
description: "Open Homarr's media request search interface.",
parameters: { type: "object", properties: {}, additionalProperties: false },
},
} as const;
const assistantInstructions = `You are Homarr Assistant, embedded in the user's self-hosted Homarr dashboard.
Use the available Homarr tools whenever live instance data is needed. Never invent integrations, boards, apps, users, media, system status, or action results.
Homarr concepts:
- Integrations connect Homarr to services such as Sonarr, Radarr, Plex, Jellyfin, Home Assistant, download clients, DNS filters, and monitoring systems.
- Boards are customizable dashboards. Apps are visual links on boards. Widgets show live integration data.
- Tool inputs that require an integrationId must use an id returned by an integration discovery tool.
- Existing Homarr permissions are authoritative. If a tool denies access, explain that the current user lacks permission without suggesting a bypass.
Action rules:
- Prefer read-only tools before actions.
- Mutating Homarr tools require user approval. Present the exact intended change and wait for the approval flow.
- Do not retry a denied action.
- Browser tools can navigate within Homarr or open existing Homarr UI. Never navigate to an arbitrary external URL.
- Keep responses concise and lead with the result. Summarize tool output instead of dumping JSON.
- If configuration or a service is unavailable, say what the user or administrator can do next.`;
const safeToolError = (error: unknown) => {
if (error instanceof Error && "code" in error) {
switch ((error as Error & { code: string }).code) {
case "UNAUTHORIZED":
case "FORBIDDEN":
return "You do not have permission to perform this action.";
case "NOT_FOUND":
return "The requested resource was not found.";
case "BAD_REQUEST":
return "The tool input was not valid.";
case "TOO_MANY_REQUESTS":
return "The operation is rate limited. Try again later.";
}
}
return "The Homarr tool could not complete this request.";
};
const getProcedureTypeMap = () => {
const procedures = (
mcpRouter as unknown as {
["_def"]: { procedures: Record<string, { ["_def"]?: { type?: string } }> };
}
)["_def"].procedures;
return new Map(
Object.entries(procedures).flatMap(([path, procedure]) => {
const type = procedure["_def"]?.type;
return type === "query" || type === "mutation" ? [[path, type] as const] : [];
}),
);
};
export async function POST(request: Request) {
const contentLength = Number(request.headers.get("content-length") ?? 0);
if (contentLength > 2_000_000) {
return Response.json({ error: "The assistant request is too large." }, { status: 413 });
}
const session = await auth();
if (!session) {
return Response.json({ error: "Sign in to use Homarr Assistant." }, { status: 401 });
}
const requestBody = await request.text();
if (requestBody.length > 2_000_000) {
return Response.json({ error: "The assistant request is too large." }, { status: 413 });
}
const parsed = requestSchema.safeParse(
(() => {
try {
return JSON.parse(requestBody) as unknown;
} catch {
return null;
}
})(),
);
if (!parsed.success) {
return Response.json({ error: "The assistant request was invalid." }, { status: 400 });
}
const configuration = await db.query.assistantConfigurations.findFirst({
where: eq(assistantConfigurations.id, "default"),
});
const requiresApiKey = configuration?.provider === "openrouter" || configuration?.provider === "openai";
if (!configuration?.enabled || !configuration.modelId || (requiresApiKey && !configuration.encryptedApiKey)) {
return Response.json({ error: "Homarr Assistant is not configured." }, { status: 503 });
}
const thread = await db.query.assistantThreads.findFirst({
where: and(
eq(assistantThreads.id, parsed.data.id),
eq(assistantThreads.userId, session.user.id),
eq(assistantThreads.status, "regular"),
),
});
if (!thread) {
return Response.json({ error: "Conversation not found." }, { status: 404 });
}
const context = createTRPCContext({ headers: request.headers, session });
const caller = mcpRouter.createCaller(context);
const procedureTypes = getProcedureTypeMap();
const homarrTools = Object.fromEntries(
extractMcpTools().map((mcpTool) => {
const procedurePath = mcpTool.pathInRouter.join(".");
const isMutation = procedureTypes.get(procedurePath) === "mutation";
return [
mcpTool.name,
tool({
description: mcpTool.description,
inputSchema: jsonSchema(
(mcpTool.inputSchema ?? { type: "object", properties: {} }) as Parameters<typeof jsonSchema>[0],
),
needsApproval: isMutation,
execute: async (input) => {
try {
const procedure = mcpTool.pathInRouter.reduce<unknown>(
(current, segment) =>
(typeof current === "object" || typeof current === "function") && current !== null
? (current as Record<string, unknown>)[segment]
: undefined,
caller,
);
if (typeof procedure !== "function") {
throw new Error("Procedure not callable");
}
const result = await (procedure as (value: unknown) => Promise<unknown>)(
input && Object.keys(input as object).length > 0 ? input : undefined,
);
return parse(stringify(result));
} catch (error) {
logger.error("Assistant tool call failed", {
toolName: mcpTool.name,
error: error instanceof Error ? error.message : String(error),
});
return { error: safeToolError(error) };
}
},
}),
];
}),
) satisfies ToolSet;
const frontendTools = Object.fromEntries(
Object.keys(parsed.data.tools ?? {}).flatMap((name) => {
if (!(name in browserToolDefinitions)) return [];
const definition = browserToolDefinitions[name as keyof typeof browserToolDefinitions];
return [
[
name,
tool({
description: definition.description,
inputSchema: jsonSchema(definition.parameters as Parameters<typeof jsonSchema>[0]),
}),
] as const,
];
}),
) satisfies ToolSet;
try {
const customHeaders = configuration.encryptedHeaders
? z.record(z.string(), z.string()).parse(JSON.parse(decryptSecret(configuration.encryptedHeaders)))
: {};
const providerHeaders = {
...(configuration.provider === "openrouter"
? { "HTTP-Referer": "https://homarr.dev", "X-Title": "Homarr Assistant" }
: {}),
...customHeaders,
};
const provider = createOpenAICompatible({
name: `homarr-${configuration.provider}`,
baseURL: configuration.baseUrl,
apiKey: configuration.encryptedApiKey ? decryptSecret(configuration.encryptedApiKey) : undefined,
headers: providerHeaders,
includeUsage: true,
});
const result = streamText({
model: provider(configuration.modelId),
instructions: assistantInstructions,
messages: await convertToModelMessages(parsed.data.messages as UIMessage[]),
tools: { ...homarrTools, ...frontendTools },
stopWhen: stepCountIs(8),
abortSignal: request.signal,
timeout: { totalMs: 55_000, stepMs: 30_000, toolMs: 30_000 },
maxRetries: 2,
experimental_toolApprovalSecret: env.SECRET_ENCRYPTION_KEY,
onFinish: async () => {
await db
.update(assistantThreads)
.set({ modelId: configuration.modelId, updatedAt: new Date() })
.where(eq(assistantThreads.id, thread.id));
},
});
return result.toUIMessageStreamResponse();
} catch {
return Response.json(
{ error: "The configured model endpoint could not start this response. Check its URL, model, and credentials." },
{ status: 502 },
);
}
}