feat: streamline assistant interactions
This commit is contained in:
1 parent
27448868dd
commit
63699b1610
7 files changed
+721
-222
No files matched your search
@@ -9,8 +9,15 @@ tags:
|
|||||||
# Assistant
|
# Assistant
|
||||||
|
|
||||||
Administrators can configure the instance-wide AI assistant from **Management → Assistant**.
|
Administrators can configure the instance-wide AI assistant from **Management → Assistant**.
|
||||||
It is available to signed-in users from the user menu and the `Cmd/Ctrl + K` command menu.
|
It is available to signed-in users from the user menu and the `Cmd/Ctrl + K` command menu. Selecting
|
||||||
`Cmd/Ctrl + Shift + K` opens it directly.
|
**Ask Homarr Assistant** in the command menu turns its search field into a prompt composer. Press
|
||||||
|
`Enter` to send the prompt in the background without opening the conversation.
|
||||||
|
|
||||||
|
While a response is running, a compact activity bar appears at the bottom of the page and the user-menu
|
||||||
|
avatar shows a thinking indicator. The activity bar can be opened to follow the response. Completed
|
||||||
|
responses add an unread count to the user menu until they are opened or dismissed.
|
||||||
|
|
||||||
|
`Cmd/Ctrl + Shift + K` opens the compact conversation panel directly.
|
||||||
|
|
||||||
When the assistant is unavailable, its command-menu result remains visible but disabled and explains that
|
When the assistant is unavailable, its command-menu result remains visible but disabled and explains that
|
||||||
an administrator must configure and enable it.
|
an administrator must configure and enable it.
|
||||||
@@ -56,8 +63,9 @@ uses. Choose OpenRouter or another provider when you prefer a provider-neutral p
|
|||||||
|
|
||||||
## Conversations and tools
|
## Conversations and tools
|
||||||
|
|
||||||
Conversations and messages are stored per user in the Homarr database. Users can start, revisit, archive,
|
Conversations and messages are stored per user in the Homarr database. The assistant stays out of the
|
||||||
and delete conversations from the assistant panel.
|
page layout until it is opened. Users can start, revisit, archive, and delete conversations from the
|
||||||
|
conversation menu in the floating panel.
|
||||||
|
|
||||||
The assistant can use the same internal tool definitions as the [Homarr MCP server](./mcp).
|
The assistant can use the same internal tool definitions as the [Homarr MCP server](./mcp).
|
||||||
Each tool call runs as the signed-in user, so normal Homarr permission checks still apply. Read-only
|
Each tool call runs as the signed-in user, so normal Homarr permission checks still apply. Read-only
|
||||||
|
|||||||
@@ -1,28 +1,75 @@
|
|||||||
.content {
|
.activityBar {
|
||||||
height: 100dvh;
|
position: fixed;
|
||||||
|
z-index: 290;
|
||||||
|
inset-inline-start: 50%;
|
||||||
|
bottom: 1rem;
|
||||||
|
display: flex;
|
||||||
|
width: min(31rem, calc(100vw - 1rem));
|
||||||
|
min-height: 3.5rem;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.25rem;
|
||||||
|
padding: 0.35rem 0.4rem 0.35rem 0.5rem;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
border: 1px solid var(--mantine-color-default-border);
|
||||||
|
border-radius: var(--mantine-radius-lg);
|
||||||
background: var(--mantine-color-body);
|
background: var(--mantine-color-body);
|
||||||
|
animation: assistant-activity-in 180ms cubic-bezier(0.22, 1, 0.36, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
.body {
|
.activityTrigger {
|
||||||
display: flex;
|
|
||||||
height: calc(100dvh - 4.25rem);
|
|
||||||
padding: 0;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.shell {
|
|
||||||
position: relative;
|
|
||||||
display: flex;
|
|
||||||
width: 100%;
|
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
|
flex: 1;
|
||||||
|
padding: 0.15rem;
|
||||||
|
border-radius: var(--mantine-radius-md);
|
||||||
|
text-align: start;
|
||||||
}
|
}
|
||||||
|
|
||||||
.history {
|
.activityTrigger:focus-visible {
|
||||||
width: 15rem;
|
outline: 2px solid var(--mantine-primary-color-filled);
|
||||||
flex: 0 0 15rem;
|
outline-offset: 2px;
|
||||||
border-inline-end: 1px solid var(--mantine-color-default-border);
|
}
|
||||||
background: color-mix(in srgb, var(--mantine-color-body) 92%, var(--mantine-color-gray-6));
|
|
||||||
transition: transform 180ms ease;
|
.activityCopy {
|
||||||
|
min-width: 0;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.activityExpandIcon {
|
||||||
|
flex: 0 0 auto;
|
||||||
|
color: var(--mantine-color-dimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
.floatingPanel {
|
||||||
|
position: fixed;
|
||||||
|
z-index: 290;
|
||||||
|
inset-inline-start: 50%;
|
||||||
|
bottom: 1rem;
|
||||||
|
display: flex;
|
||||||
|
width: min(46rem, calc(100vw - 2rem));
|
||||||
|
height: min(36rem, calc(100dvh - 5rem));
|
||||||
|
min-height: 22rem;
|
||||||
|
margin: 0;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
flex-direction: column;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 0;
|
||||||
|
border: 0;
|
||||||
|
border-radius: var(--mantine-radius-lg);
|
||||||
|
color: var(--mantine-color-text);
|
||||||
|
background: var(--mantine-color-body);
|
||||||
|
box-shadow: var(--mantine-shadow-xl);
|
||||||
|
animation: assistant-panel-in 180ms cubic-bezier(0.22, 1, 0.36, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panelHeader {
|
||||||
|
min-height: 3.75rem;
|
||||||
|
flex: 0 0 auto;
|
||||||
|
padding: 0.65rem 0.75rem 0.65rem 0.9rem;
|
||||||
|
border-bottom: 1px solid var(--mantine-color-default-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.historyMenu {
|
||||||
|
background: var(--mantine-color-body);
|
||||||
}
|
}
|
||||||
|
|
||||||
.historyItem {
|
.historyItem {
|
||||||
@@ -37,6 +84,7 @@
|
|||||||
position: relative;
|
position: relative;
|
||||||
display: flex;
|
display: flex;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
|
min-height: 0;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
@@ -52,21 +100,21 @@
|
|||||||
|
|
||||||
.messages {
|
.messages {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 52rem;
|
max-width: 44rem;
|
||||||
margin-inline: auto;
|
margin-inline: auto;
|
||||||
padding: 1.25rem 1rem 8.75rem;
|
padding: 0.75rem 1rem 1.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.message {
|
.message {
|
||||||
margin-block: 1rem;
|
margin-block: 0.9rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.userMessage {
|
.userMessage {
|
||||||
width: fit-content;
|
width: fit-content;
|
||||||
max-width: 90%;
|
max-width: 86%;
|
||||||
margin-inline-start: auto;
|
margin-inline-start: auto;
|
||||||
padding: 0.7rem 0.9rem;
|
padding: 0.65rem 0.85rem;
|
||||||
border-radius: 1rem 1rem 0.3rem 1rem;
|
border-radius: 0.9rem 0.9rem 0.25rem 0.9rem;
|
||||||
background: var(--mantine-color-red-light);
|
background: var(--mantine-color-red-light);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +123,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.messageText {
|
.messageText {
|
||||||
line-height: 1.65;
|
line-height: 1.6;
|
||||||
overflow-wrap: anywhere;
|
overflow-wrap: anywhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,32 +150,32 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.composerWrap {
|
.composerWrap {
|
||||||
position: absolute;
|
flex: 0 0 auto;
|
||||||
inset-inline: 0;
|
padding: 0.65rem 0.75rem 0.75rem;
|
||||||
bottom: 0;
|
|
||||||
padding: 2.5rem 1rem 1rem;
|
|
||||||
background: var(--mantine-color-body);
|
|
||||||
border-top: 1px solid var(--mantine-color-default-border);
|
border-top: 1px solid var(--mantine-color-default-border);
|
||||||
pointer-events: none;
|
background: var(--mantine-color-body);
|
||||||
}
|
}
|
||||||
|
|
||||||
.composer {
|
.composer {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 52rem;
|
max-width: 44rem;
|
||||||
margin-inline: auto;
|
margin-inline: auto;
|
||||||
align-items: flex-end;
|
align-items: flex-end;
|
||||||
gap: 0.5rem;
|
gap: 0.5rem;
|
||||||
padding: 0.45rem;
|
padding: 0.4rem;
|
||||||
border-radius: 1rem;
|
border: 1px solid var(--mantine-color-default-border);
|
||||||
|
border-radius: var(--mantine-radius-lg);
|
||||||
background: var(--mantine-color-default);
|
background: var(--mantine-color-default);
|
||||||
box-shadow: var(--mantine-shadow-sm);
|
}
|
||||||
pointer-events: auto;
|
|
||||||
|
.composer:focus-within {
|
||||||
|
border-color: var(--mantine-primary-color-filled);
|
||||||
}
|
}
|
||||||
|
|
||||||
.composerInput {
|
.composerInput {
|
||||||
min-height: 2.5rem;
|
min-height: 2.5rem;
|
||||||
max-height: 12rem;
|
max-height: 9rem;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
resize: none;
|
resize: none;
|
||||||
border: 0;
|
border: 0;
|
||||||
@@ -140,10 +188,10 @@
|
|||||||
|
|
||||||
.empty {
|
.empty {
|
||||||
display: flex;
|
display: flex;
|
||||||
min-height: 60vh;
|
min-height: 23rem;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
padding: 3rem 1rem 8rem;
|
padding: 2rem 1rem;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,67 +199,98 @@
|
|||||||
display: grid;
|
display: grid;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
gap: 0.65rem;
|
gap: 0.55rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.suggestion {
|
.suggestion {
|
||||||
height: auto;
|
height: auto;
|
||||||
min-height: 3.25rem;
|
min-height: 3rem;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
padding: 0.7rem 0.9rem;
|
padding: 0.65rem 0.8rem;
|
||||||
text-align: start;
|
text-align: start;
|
||||||
white-space: normal;
|
white-space: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
.mobileHistoryButton,
|
.scrollToBottom {
|
||||||
.historyClose {
|
position: absolute;
|
||||||
display: none;
|
inset-inline-start: 50%;
|
||||||
|
bottom: 0.75rem;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes assistant-activity-in {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translate(-50%, 0.5rem);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translate(-50%, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes assistant-panel-in {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translate(-50%, 0.75rem) scale(0.985);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translate(-50%, 0) scale(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 48em) {
|
@media (max-width: 48em) {
|
||||||
.history {
|
.activityBar {
|
||||||
position: absolute;
|
bottom: 0.5rem;
|
||||||
z-index: 4;
|
|
||||||
inset-block: 0;
|
|
||||||
inset-inline-start: 0;
|
|
||||||
width: min(20rem, 88vw);
|
|
||||||
transform: translateX(-105%);
|
|
||||||
visibility: hidden;
|
|
||||||
transition:
|
|
||||||
transform 180ms ease,
|
|
||||||
visibility 180ms ease;
|
|
||||||
box-shadow: var(--mantine-shadow-xl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[dir="rtl"] .history {
|
.floatingPanel {
|
||||||
transform: translateX(105%);
|
bottom: 0.5rem;
|
||||||
|
width: calc(100vw - 1rem);
|
||||||
|
height: min(44rem, calc(100dvh - 1rem));
|
||||||
|
min-height: 20rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.history[data-opened="true"] {
|
.panelHeader {
|
||||||
transform: translateX(0);
|
padding-inline-start: 0.7rem;
|
||||||
visibility: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.mobileHistoryButton {
|
|
||||||
position: absolute;
|
|
||||||
z-index: 3;
|
|
||||||
top: 0.65rem;
|
|
||||||
inset-inline-start: 0.65rem;
|
|
||||||
display: inline-flex;
|
|
||||||
}
|
|
||||||
|
|
||||||
.historyClose {
|
|
||||||
display: inline-flex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.suggestions {
|
.suggestions {
|
||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.empty {
|
||||||
|
min-height: 32rem;
|
||||||
|
padding-block: 1.5rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 30em) {
|
||||||
|
.historyButton {
|
||||||
|
max-width: 2.25rem;
|
||||||
|
padding-inline: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.historyButton :global(.mantine-Button-section) {
|
||||||
|
margin-inline-end: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.historyButton :global(.mantine-Button-label) {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.messages {
|
||||||
|
padding-inline: 0.75rem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-reduced-motion: reduce) {
|
@media (prefers-reduced-motion: reduce) {
|
||||||
.history {
|
.activityBar,
|
||||||
transition: none;
|
.floatingPanel {
|
||||||
|
animation: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.viewport {
|
.viewport {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState } from "react";
|
import { createContext, useContext, useState } from "react";
|
||||||
import type { ToolCallMessagePartProps } from "@assistant-ui/react";
|
import type { ToolCallMessagePartProps } from "@assistant-ui/react";
|
||||||
|
import type { MessageStatus } from "@assistant-ui/react";
|
||||||
import {
|
import {
|
||||||
ActionBarPrimitive,
|
ActionBarPrimitive,
|
||||||
ComposerPrimitive,
|
ComposerPrimitive,
|
||||||
@@ -13,8 +14,22 @@ import {
|
|||||||
ThreadPrimitive,
|
ThreadPrimitive,
|
||||||
useAuiState,
|
useAuiState,
|
||||||
} from "@assistant-ui/react";
|
} from "@assistant-ui/react";
|
||||||
import { ActionIcon, Box, Button, Drawer, Group, ScrollArea, Stack, Text, ThemeIcon, Tooltip } from "@mantine/core";
|
import {
|
||||||
import { useMediaQuery } from "@mantine/hooks";
|
ActionIcon,
|
||||||
|
Badge,
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
Group,
|
||||||
|
Loader,
|
||||||
|
Popover,
|
||||||
|
ScrollArea,
|
||||||
|
Stack,
|
||||||
|
Text,
|
||||||
|
ThemeIcon,
|
||||||
|
Tooltip,
|
||||||
|
UnstyledButton,
|
||||||
|
} from "@mantine/core";
|
||||||
|
import { useWindowEvent } from "@mantine/hooks";
|
||||||
import {
|
import {
|
||||||
IconActivityHeartbeat,
|
IconActivityHeartbeat,
|
||||||
IconAlertTriangle,
|
IconAlertTriangle,
|
||||||
@@ -22,9 +37,9 @@ import {
|
|||||||
IconArchive,
|
IconArchive,
|
||||||
IconArrowUp,
|
IconArrowUp,
|
||||||
IconCheck,
|
IconCheck,
|
||||||
|
IconChevronUp,
|
||||||
IconCopy,
|
IconCopy,
|
||||||
IconCommand,
|
IconCommand,
|
||||||
IconMenu2,
|
|
||||||
IconHistory,
|
IconHistory,
|
||||||
IconPlayerStop,
|
IconPlayerStop,
|
||||||
IconPlus,
|
IconPlus,
|
||||||
@@ -42,7 +57,14 @@ import classes from "./assistant-panel.module.css";
|
|||||||
|
|
||||||
interface AssistantPanelProps {
|
interface AssistantPanelProps {
|
||||||
opened: boolean;
|
opened: boolean;
|
||||||
|
onOpen: () => void;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
|
onMarkRead: () => void;
|
||||||
|
isRunning: boolean;
|
||||||
|
unreadCount: number;
|
||||||
|
latestAssistantText: string;
|
||||||
|
latestUserText: string;
|
||||||
|
latestStatus: MessageStatus | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TextPart = () => <MessagePartPrimitive.Text className={classes.messageText} />;
|
const TextPart = () => <MessagePartPrimitive.Text className={classes.messageText} />;
|
||||||
@@ -194,13 +216,24 @@ const AssistantMessage = () => (
|
|||||||
</MessagePrimitive.Root>
|
</MessagePrimitive.Root>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const HistorySelectContext = createContext<() => void>(() => undefined);
|
||||||
|
|
||||||
const ThreadListItem = () => {
|
const ThreadListItem = () => {
|
||||||
const t = useScopedI18n("common.assistant");
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const onSelect = useContext(HistorySelectContext);
|
||||||
return (
|
return (
|
||||||
<ThreadListItemPrimitive.Root className={classes.historyItem}>
|
<ThreadListItemPrimitive.Root className={classes.historyItem}>
|
||||||
<Group gap={4} wrap="nowrap">
|
<Group gap={4} wrap="nowrap">
|
||||||
<ThreadListItemPrimitive.Trigger asChild>
|
<ThreadListItemPrimitive.Trigger asChild>
|
||||||
<Button variant="subtle" color="gray" size="compact-sm" justify="flex-start" flex={1} style={{ minWidth: 0 }}>
|
<Button
|
||||||
|
variant="subtle"
|
||||||
|
color="gray"
|
||||||
|
size="compact-sm"
|
||||||
|
justify="flex-start"
|
||||||
|
flex={1}
|
||||||
|
style={{ minWidth: 0 }}
|
||||||
|
onClick={onSelect}
|
||||||
|
>
|
||||||
<ThreadListItemPrimitive.Title fallback={t("newConversation")} />
|
<ThreadListItemPrimitive.Title fallback={t("newConversation")} />
|
||||||
</Button>
|
</Button>
|
||||||
</ThreadListItemPrimitive.Trigger>
|
</ThreadListItemPrimitive.Trigger>
|
||||||
@@ -221,11 +254,20 @@ const ThreadListItem = () => {
|
|||||||
|
|
||||||
const ArchivedThreadListItem = () => {
|
const ArchivedThreadListItem = () => {
|
||||||
const t = useScopedI18n("common.assistant");
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const onSelect = useContext(HistorySelectContext);
|
||||||
return (
|
return (
|
||||||
<ThreadListItemPrimitive.Root className={classes.historyItem}>
|
<ThreadListItemPrimitive.Root className={classes.historyItem}>
|
||||||
<Group gap={4} wrap="nowrap">
|
<Group gap={4} wrap="nowrap">
|
||||||
<ThreadListItemPrimitive.Trigger asChild>
|
<ThreadListItemPrimitive.Trigger asChild>
|
||||||
<Button variant="subtle" color="gray" size="compact-sm" justify="flex-start" flex={1} style={{ minWidth: 0 }}>
|
<Button
|
||||||
|
variant="subtle"
|
||||||
|
color="gray"
|
||||||
|
size="compact-sm"
|
||||||
|
justify="flex-start"
|
||||||
|
flex={1}
|
||||||
|
style={{ minWidth: 0 }}
|
||||||
|
onClick={onSelect}
|
||||||
|
>
|
||||||
<ThreadListItemPrimitive.Title fallback={t("archivedConversation")} />
|
<ThreadListItemPrimitive.Title fallback={t("archivedConversation")} />
|
||||||
</Button>
|
</Button>
|
||||||
</ThreadListItemPrimitive.Trigger>
|
</ThreadListItemPrimitive.Trigger>
|
||||||
@@ -244,51 +286,66 @@ const ArchivedThreadListItem = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const ThreadHistory = ({ opened, onClose }: { opened: boolean; onClose: () => void }) => {
|
const ThreadHistory = ({ onSelect }: { onSelect: () => void }) => {
|
||||||
const t = useScopedI18n("common.assistant");
|
const t = useScopedI18n("common.assistant");
|
||||||
const isMobile = useMediaQuery("(max-width: 48em)");
|
|
||||||
const hidden = isMobile && !opened;
|
|
||||||
return (
|
return (
|
||||||
<Stack
|
<HistorySelectContext.Provider value={onSelect}>
|
||||||
className={classes.history}
|
<Stack className={classes.historyMenu} gap="xs" p="xs">
|
||||||
gap="xs"
|
<Group gap="xs" wrap="nowrap" px={4} pt={4}>
|
||||||
p="sm"
|
<ThreadListPrimitive.New asChild>
|
||||||
data-opened={opened}
|
<Button variant="light" leftSection={<IconPlus size={16} />} fullWidth onClick={onSelect}>
|
||||||
inert={hidden ? true : undefined}
|
{t("newConversation")}
|
||||||
aria-hidden={hidden || undefined}
|
</Button>
|
||||||
|
</ThreadListPrimitive.New>
|
||||||
|
</Group>
|
||||||
|
<Group gap="xs" px="xs" mt="xs">
|
||||||
|
<IconHistory size={14} />
|
||||||
|
<Text size="sm" fw={600} c="dimmed">
|
||||||
|
{t("conversations")}
|
||||||
|
</Text>
|
||||||
|
</Group>
|
||||||
|
<ScrollArea h="min(24rem, 55dvh)" type="auto" offsetScrollbars>
|
||||||
|
<Stack gap={3}>
|
||||||
|
<ThreadListPrimitive.Items components={{ ThreadListItem }} />
|
||||||
|
<Text size="sm" fw={600} c="dimmed" px="xs" mt="sm">
|
||||||
|
{t("archived")}
|
||||||
|
</Text>
|
||||||
|
<ThreadListPrimitive.Items archived components={{ ThreadListItem: ArchivedThreadListItem }} />
|
||||||
|
</Stack>
|
||||||
|
</ScrollArea>
|
||||||
|
</Stack>
|
||||||
|
</HistorySelectContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const ConversationHistory = () => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const [opened, setOpened] = useState(false);
|
||||||
|
return (
|
||||||
|
<Popover
|
||||||
|
opened={opened}
|
||||||
|
onChange={setOpened}
|
||||||
|
position="bottom-end"
|
||||||
|
width="min(22rem, calc(100vw - 1rem))"
|
||||||
|
shadow="md"
|
||||||
|
withinPortal
|
||||||
>
|
>
|
||||||
<Group gap="xs" wrap="nowrap">
|
<Popover.Target>
|
||||||
<ThreadListPrimitive.New asChild>
|
<Button
|
||||||
<Button variant="light" leftSection={<IconPlus size={16} />} fullWidth onClick={onClose}>
|
className={classes.historyButton}
|
||||||
{t("newConversation")}
|
|
||||||
</Button>
|
|
||||||
</ThreadListPrimitive.New>
|
|
||||||
<ActionIcon
|
|
||||||
className={classes.historyClose}
|
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
color="gray"
|
color="gray"
|
||||||
onClick={onClose}
|
size="compact-sm"
|
||||||
aria-label={t("closeHistory")}
|
leftSection={<IconHistory size={16} />}
|
||||||
|
onClick={() => setOpened((current) => !current)}
|
||||||
>
|
>
|
||||||
<IconX size={17} />
|
|
||||||
</ActionIcon>
|
|
||||||
</Group>
|
|
||||||
<Group gap="xs" px="xs" mt="xs">
|
|
||||||
<IconHistory size={14} />
|
|
||||||
<Text size="sm" fw={600} c="dimmed">
|
|
||||||
{t("conversations")}
|
{t("conversations")}
|
||||||
</Text>
|
</Button>
|
||||||
</Group>
|
</Popover.Target>
|
||||||
<ScrollArea flex={1} type="auto">
|
<Popover.Dropdown p={0}>
|
||||||
<Stack gap={3}>
|
<ThreadHistory onSelect={() => setOpened(false)} />
|
||||||
<ThreadListPrimitive.Items components={{ ThreadListItem }} />
|
</Popover.Dropdown>
|
||||||
<Text size="sm" fw={600} c="dimmed" px="xs" mt="sm">
|
</Popover>
|
||||||
{t("archived")}
|
|
||||||
</Text>
|
|
||||||
<ThreadListPrimitive.Items archived components={{ ThreadListItem: ArchivedThreadListItem }} />
|
|
||||||
</Stack>
|
|
||||||
</ScrollArea>
|
|
||||||
</Stack>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -366,67 +423,160 @@ const Composer = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AssistantPanel = ({ opened, onClose }: AssistantPanelProps) => {
|
const AssistantActivityBar = ({
|
||||||
|
isRunning,
|
||||||
|
unreadCount,
|
||||||
|
latestAssistantText,
|
||||||
|
latestUserText,
|
||||||
|
latestStatus,
|
||||||
|
onOpen,
|
||||||
|
onMarkRead,
|
||||||
|
}: Omit<AssistantPanelProps, "opened" | "onClose">) => {
|
||||||
const t = useScopedI18n("common.assistant");
|
const t = useScopedI18n("common.assistant");
|
||||||
const [historyOpened, setHistoryOpened] = useState(false);
|
const needsApproval = latestStatus?.type === "requires-action";
|
||||||
|
const failed = latestStatus?.type === "incomplete" && latestStatus.reason !== "cancelled";
|
||||||
|
const visible = isRunning || unreadCount > 0 || needsApproval;
|
||||||
|
|
||||||
|
if (!visible) return null;
|
||||||
|
|
||||||
|
const title = isRunning
|
||||||
|
? t("activity.thinking")
|
||||||
|
: needsApproval
|
||||||
|
? t("activity.approval")
|
||||||
|
: failed
|
||||||
|
? t("activity.failed")
|
||||||
|
: t("activity.ready");
|
||||||
|
const detail = isRunning
|
||||||
|
? latestUserText || t("activity.working")
|
||||||
|
: latestAssistantText || (failed ? t("responseError.description") : t("activity.completed"));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Drawer
|
<Box component="output" className={classes.activityBar} aria-live="polite">
|
||||||
opened={opened}
|
<UnstyledButton className={classes.activityTrigger} onClick={onOpen} aria-label={t("activity.expand")}>
|
||||||
onClose={onClose}
|
<Group gap="sm" wrap="nowrap">
|
||||||
position="right"
|
{isRunning ? (
|
||||||
size="min(52rem, 100vw)"
|
<ThemeIcon variant="light" color="red" radius="xl">
|
||||||
zIndex={300}
|
<Loader type="bars" size="sm" color="red" />
|
||||||
className={classes.drawer}
|
</ThemeIcon>
|
||||||
classNames={{ content: classes.content, body: classes.body }}
|
) : (
|
||||||
title={
|
<ThemeIcon variant="light" color={failed ? "red" : needsApproval ? "yellow" : "green"} radius="xl">
|
||||||
<Group gap="xs">
|
{failed ? (
|
||||||
<ThemeIcon variant="light" color="red" radius="xl">
|
<IconAlertTriangle size={17} />
|
||||||
<IconRobot size={18} />
|
) : needsApproval ? (
|
||||||
</ThemeIcon>
|
<IconRobot size={17} />
|
||||||
<div>
|
) : (
|
||||||
<Text fw={700} lh={1.1}>
|
<IconCheck size={17} />
|
||||||
{t("title")}
|
)}
|
||||||
|
</ThemeIcon>
|
||||||
|
)}
|
||||||
|
<Stack gap={0} className={classes.activityCopy}>
|
||||||
|
<Group gap="xs" wrap="nowrap">
|
||||||
|
<Text size="sm" fw={700}>
|
||||||
|
{title}
|
||||||
|
</Text>
|
||||||
|
{unreadCount > 0 && (
|
||||||
|
<Badge size="xs" variant="filled" color="red" circle>
|
||||||
|
{unreadCount > 99 ? "99+" : unreadCount}
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</Group>
|
||||||
|
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||||
|
{detail}
|
||||||
</Text>
|
</Text>
|
||||||
<Text size="xs" c="dimmed">
|
</Stack>
|
||||||
{t("subtitle")}
|
<IconChevronUp size={17} className={classes.activityExpandIcon} />
|
||||||
</Text>
|
|
||||||
</div>
|
|
||||||
</Group>
|
</Group>
|
||||||
}
|
</UnstyledButton>
|
||||||
closeButtonProps={{ icon: <IconX size={18} />, "aria-label": t("close") }}
|
{!isRunning && !needsApproval && (
|
||||||
>
|
<ActionIcon variant="subtle" color="gray" size="sm" onClick={onMarkRead} aria-label={t("activity.dismiss")}>
|
||||||
<Box className={classes.shell}>
|
<IconX size={15} />
|
||||||
<ThreadHistory opened={historyOpened} onClose={() => setHistoryOpened(false)} />
|
</ActionIcon>
|
||||||
<ThreadPrimitive.Root className={classes.thread}>
|
)}
|
||||||
<ActionIcon
|
</Box>
|
||||||
className={classes.mobileHistoryButton}
|
);
|
||||||
variant="default"
|
};
|
||||||
onClick={() => setHistoryOpened(true)}
|
|
||||||
aria-label={t("openHistory")}
|
export const AssistantPanel = ({
|
||||||
>
|
opened,
|
||||||
<IconMenu2 size={17} />
|
onOpen,
|
||||||
</ActionIcon>
|
onClose,
|
||||||
<ThreadPrimitive.Viewport className={classes.viewport}>
|
onMarkRead,
|
||||||
<Box className={classes.messages}>
|
isRunning,
|
||||||
<EmptyThread />
|
unreadCount,
|
||||||
<ThreadPrimitive.Messages components={{ UserMessage, AssistantMessage }} />
|
latestAssistantText,
|
||||||
</Box>
|
latestUserText,
|
||||||
<ThreadPrimitive.ScrollToBottom asChild>
|
latestStatus,
|
||||||
<ActionIcon
|
}: AssistantPanelProps) => {
|
||||||
pos="absolute"
|
const t = useScopedI18n("common.assistant");
|
||||||
bottom={110}
|
|
||||||
left="50%"
|
useWindowEvent("keydown", (event) => {
|
||||||
variant="default"
|
if (opened && event.key === "Escape") onClose();
|
||||||
radius="xl"
|
});
|
||||||
aria-label={t("scrollToLatest")}
|
|
||||||
>
|
return (
|
||||||
<IconArrowUp size={16} style={{ transform: "rotate(180deg)" }} />
|
<>
|
||||||
</ActionIcon>
|
{!opened && (
|
||||||
</ThreadPrimitive.ScrollToBottom>
|
<AssistantActivityBar
|
||||||
</ThreadPrimitive.Viewport>
|
onOpen={onOpen}
|
||||||
<Composer />
|
onMarkRead={onMarkRead}
|
||||||
</ThreadPrimitive.Root>
|
isRunning={isRunning}
|
||||||
</Box>
|
unreadCount={unreadCount}
|
||||||
</Drawer>
|
latestAssistantText={latestAssistantText}
|
||||||
|
latestUserText={latestUserText}
|
||||||
|
latestStatus={latestStatus}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{opened && (
|
||||||
|
<dialog className={classes.floatingPanel} aria-label={t("title")} open>
|
||||||
|
<Group className={classes.panelHeader} justify="space-between" wrap="nowrap">
|
||||||
|
<Group gap="xs" wrap="nowrap">
|
||||||
|
<ThemeIcon variant="light" color="red" radius="xl">
|
||||||
|
<IconRobot size={18} />
|
||||||
|
</ThemeIcon>
|
||||||
|
<div>
|
||||||
|
<Text fw={700} lh={1.1}>
|
||||||
|
{t("title")}
|
||||||
|
</Text>
|
||||||
|
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||||
|
{isRunning ? t("activity.thinking") : t("subtitle")}
|
||||||
|
</Text>
|
||||||
|
</div>
|
||||||
|
</Group>
|
||||||
|
<Group gap={2} wrap="nowrap">
|
||||||
|
<ConversationHistory />
|
||||||
|
<Tooltip label={t("newConversation")}>
|
||||||
|
<ThreadListPrimitive.New asChild>
|
||||||
|
<ActionIcon variant="subtle" color="gray" aria-label={t("newConversation")}>
|
||||||
|
<IconPlus size={17} />
|
||||||
|
</ActionIcon>
|
||||||
|
</ThreadListPrimitive.New>
|
||||||
|
</Tooltip>
|
||||||
|
<ActionIcon variant="subtle" color="gray" onClick={onClose} aria-label={t("close")}>
|
||||||
|
<IconX size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Group>
|
||||||
|
</Group>
|
||||||
|
<ThreadPrimitive.Root className={classes.thread}>
|
||||||
|
<ThreadPrimitive.Viewport className={classes.viewport}>
|
||||||
|
<Box className={classes.messages}>
|
||||||
|
<EmptyThread />
|
||||||
|
<ThreadPrimitive.Messages components={{ UserMessage, AssistantMessage }} />
|
||||||
|
</Box>
|
||||||
|
<ThreadPrimitive.ScrollToBottom asChild>
|
||||||
|
<ActionIcon
|
||||||
|
className={classes.scrollToBottom}
|
||||||
|
variant="default"
|
||||||
|
radius="xl"
|
||||||
|
aria-label={t("scrollToLatest")}
|
||||||
|
>
|
||||||
|
<IconArrowUp size={16} style={{ transform: "rotate(180deg)" }} />
|
||||||
|
</ActionIcon>
|
||||||
|
</ThreadPrimitive.ScrollToBottom>
|
||||||
|
</ThreadPrimitive.Viewport>
|
||||||
|
<Composer />
|
||||||
|
</ThreadPrimitive.Root>
|
||||||
|
</dialog>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -1,9 +1,15 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import type { PropsWithChildren } from "react";
|
import type { PropsWithChildren } from "react";
|
||||||
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import type { MessageFormatAdapter, RemoteThreadListAdapter, ThreadHistoryAdapter, Toolkit } from "@assistant-ui/react";
|
import type {
|
||||||
|
MessageFormatAdapter,
|
||||||
|
RemoteThreadListAdapter,
|
||||||
|
ThreadHistoryAdapter,
|
||||||
|
ThreadMessage,
|
||||||
|
Toolkit,
|
||||||
|
} from "@assistant-ui/react";
|
||||||
import {
|
import {
|
||||||
AssistantRuntimeProvider,
|
AssistantRuntimeProvider,
|
||||||
defineToolkit,
|
defineToolkit,
|
||||||
@@ -21,7 +27,7 @@ import { lastAssistantMessageIsCompleteWithApprovalResponses } from "ai";
|
|||||||
import { clientApi, fetchApi } from "@homarr/api/client";
|
import { clientApi, fetchApi } from "@homarr/api/client";
|
||||||
import { useSession } from "@homarr/auth/client";
|
import { useSession } from "@homarr/auth/client";
|
||||||
import { hotkeys } from "@homarr/definitions";
|
import { hotkeys } from "@homarr/definitions";
|
||||||
import { showErrorNotification } from "@homarr/notifications";
|
import { showErrorNotification, showWarningNotification } from "@homarr/notifications";
|
||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
import {
|
import {
|
||||||
openMediaRequestSearch,
|
openMediaRequestSearch,
|
||||||
@@ -31,14 +37,18 @@ import {
|
|||||||
} from "@homarr/spotlight";
|
} from "@homarr/spotlight";
|
||||||
|
|
||||||
import { AssistantPanel } from "./assistant-panel";
|
import { AssistantPanel } from "./assistant-panel";
|
||||||
|
import { createAssistantPromptInteraction } from "./assistant-spotlight";
|
||||||
import { browserToolContracts } from "./assistant-tool-contracts";
|
import { browserToolContracts } from "./assistant-tool-contracts";
|
||||||
|
|
||||||
interface AssistantContextValue {
|
interface AssistantContextValue {
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
opened: boolean;
|
opened: boolean;
|
||||||
|
isRunning: boolean;
|
||||||
|
unreadCount: number;
|
||||||
open: () => void;
|
open: () => void;
|
||||||
close: () => void;
|
close: () => void;
|
||||||
toggle: () => void;
|
toggle: () => void;
|
||||||
|
sendPrompt: (prompt: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AssistantContext = createContext<AssistantContextValue | null>(null);
|
const AssistantContext = createContext<AssistantContextValue | null>(null);
|
||||||
@@ -267,58 +277,189 @@ const AssistantTools = ({ toolkit }: { toolkit: Toolkit }) => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AssistantProvider = ({ children }: PropsWithChildren) => {
|
const getMessageText = (message: ThreadMessage | undefined) =>
|
||||||
const t = useScopedI18n("common.assistant");
|
message?.content
|
||||||
const session = useSession();
|
.filter((part) => part.type === "text")
|
||||||
const [opened, setOpened] = useState(false);
|
.map((part) => part.text)
|
||||||
const { data, isLoading: isAvailabilityLoading } = clientApi.assistant.getAvailability.useQuery(undefined, {
|
.join("\n")
|
||||||
enabled: session.status === "authenticated",
|
.trim() ?? "";
|
||||||
staleTime: 60_000,
|
|
||||||
});
|
|
||||||
const enabled = Boolean(data?.enabled && session.status === "authenticated");
|
|
||||||
|
|
||||||
const open = useCallback(() => setOpened(true), []);
|
const getLatestMessage = (messages: readonly ThreadMessage[], role: ThreadMessage["role"]) =>
|
||||||
|
messages.findLast((message) => message.role === role);
|
||||||
|
|
||||||
|
const getNotificationKey = (message: ThreadMessage | undefined) => {
|
||||||
|
if (message?.role !== "assistant") return null;
|
||||||
|
if (message.status.type === "running") return null;
|
||||||
|
return `${message.id}:${message.status.type}:${"reason" in message.status ? message.status.reason : ""}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const EnabledAssistantProvider = ({ children }: PropsWithChildren) => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const [opened, setOpened] = useState(false);
|
||||||
|
const [unreadCount, setUnreadCount] = useState(0);
|
||||||
|
const aui = useAui();
|
||||||
|
const messages = useAuiState((state) => state.thread.messages);
|
||||||
|
const isRunning = useAuiState((state) => state.thread.isRunning);
|
||||||
|
const isLoading = useAuiState((state) => state.thread.isLoading);
|
||||||
|
const latestAssistantMessage = getLatestMessage(messages, "assistant");
|
||||||
|
const latestUserMessage = getLatestMessage(messages, "user");
|
||||||
|
const latestAssistantText = getMessageText(latestAssistantMessage);
|
||||||
|
const latestUserText = getMessageText(latestUserMessage);
|
||||||
|
const latestStatus = latestAssistantMessage?.role === "assistant" ? latestAssistantMessage.status : undefined;
|
||||||
|
const notificationKey = getNotificationKey(latestAssistantMessage);
|
||||||
|
const initializedRef = useRef(false);
|
||||||
|
const lastNotifiedKeyRef = useRef<string | null>(null);
|
||||||
|
|
||||||
|
const markRead = useCallback(() => {
|
||||||
|
lastNotifiedKeyRef.current = notificationKey;
|
||||||
|
setUnreadCount(0);
|
||||||
|
}, [notificationKey]);
|
||||||
|
const open = useCallback(() => {
|
||||||
|
markRead();
|
||||||
|
setOpened(true);
|
||||||
|
}, [markRead]);
|
||||||
const close = useCallback(() => setOpened(false), []);
|
const close = useCallback(() => setOpened(false), []);
|
||||||
const toggle = useCallback(() => setOpened((current) => !current), []);
|
const toggle = useCallback(() => {
|
||||||
const openIfEnabled = useCallback(() => {
|
setOpened((current) => {
|
||||||
if (enabled) {
|
if (!current) markRead();
|
||||||
open();
|
return !current;
|
||||||
|
});
|
||||||
|
}, [markRead]);
|
||||||
|
const sendPrompt = useCallback(
|
||||||
|
(prompt: string) => {
|
||||||
|
const text = prompt.trim();
|
||||||
|
if (text.length === 0) return;
|
||||||
|
if (isRunning || latestStatus?.type === "requires-action") {
|
||||||
|
showWarningNotification({
|
||||||
|
title: t("busy.title"),
|
||||||
|
message: t("busy.description"),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
aui.thread().append({ role: "user", content: [{ type: "text", text }] });
|
||||||
|
},
|
||||||
|
[aui, isRunning, latestStatus?.type, t],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isLoading) return;
|
||||||
|
|
||||||
|
if (!initializedRef.current) {
|
||||||
|
initializedRef.current = true;
|
||||||
|
lastNotifiedKeyRef.current = notificationKey;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}, [enabled, open]);
|
|
||||||
useHotkeys([[hotkeys.openAssistant, openIfEnabled]]);
|
if (notificationKey === null || notificationKey === lastNotifiedKeyRef.current) return;
|
||||||
|
|
||||||
|
if (opened) {
|
||||||
|
lastNotifiedKeyRef.current = notificationKey;
|
||||||
|
setUnreadCount(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastNotifiedKeyRef.current = notificationKey;
|
||||||
|
setUnreadCount((current) => current + 1);
|
||||||
|
}, [isLoading, notificationKey, opened]);
|
||||||
|
|
||||||
|
useHotkeys([[hotkeys.openAssistant, open]]);
|
||||||
|
|
||||||
const spotlightItem = useMemo(
|
const spotlightItem = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
id: "homarr-assistant",
|
id: "homarr-assistant",
|
||||||
name: t("spotlight"),
|
name: t("spotlight"),
|
||||||
icon: "/logo/logo.png",
|
icon: "/logo/logo.png",
|
||||||
description: enabled
|
description: t("spotlightDescription"),
|
||||||
? t("spotlightDescription")
|
interaction: () => createAssistantPromptInteraction({ sendPrompt }),
|
||||||
: session.status !== "authenticated"
|
|
||||||
? t("unavailable.signIn")
|
|
||||||
: isAvailabilityLoading
|
|
||||||
? t("unavailable.checking")
|
|
||||||
: t("unavailable.notConfigured"),
|
|
||||||
unavailable: !enabled,
|
|
||||||
interaction: () => (enabled ? { type: "javaScript" as const, onSelect: open } : { type: "none" as const }),
|
|
||||||
}),
|
}),
|
||||||
[enabled, isAvailabilityLoading, open, session.status, t],
|
[sendPrompt, t],
|
||||||
);
|
);
|
||||||
useRegisterSpotlightContextResults("homarr-assistant", [spotlightItem], [spotlightItem]);
|
useRegisterSpotlightContextResults("homarr-assistant", [spotlightItem], [spotlightItem]);
|
||||||
useRegisterSpotlightContextActions("homarr-assistant", [spotlightItem], [spotlightItem]);
|
useRegisterSpotlightContextActions("homarr-assistant", [spotlightItem], [spotlightItem]);
|
||||||
|
|
||||||
const value = useMemo(() => ({ enabled, opened, open, close, toggle }), [close, enabled, open, opened, toggle]);
|
const value = useMemo(
|
||||||
|
() => ({ enabled: true, opened, isRunning, unreadCount, open, close, toggle, sendPrompt }),
|
||||||
|
[close, isRunning, open, opened, sendPrompt, toggle, unreadCount],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AssistantContext.Provider value={value}>
|
<AssistantContext.Provider value={value}>
|
||||||
{enabled ? (
|
{children}
|
||||||
<AssistantRuntime>
|
<AssistantPanel
|
||||||
{children}
|
opened={opened}
|
||||||
<AssistantPanel opened={opened} onClose={close} />
|
onOpen={open}
|
||||||
</AssistantRuntime>
|
onClose={close}
|
||||||
) : (
|
onMarkRead={markRead}
|
||||||
children
|
isRunning={isRunning}
|
||||||
)}
|
unreadCount={unreadCount}
|
||||||
|
latestAssistantText={latestAssistantText}
|
||||||
|
latestUserText={latestUserText}
|
||||||
|
latestStatus={latestStatus}
|
||||||
|
/>
|
||||||
</AssistantContext.Provider>
|
</AssistantContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface DisabledAssistantProviderProps extends PropsWithChildren {
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DisabledAssistantProvider = ({ children, description }: DisabledAssistantProviderProps) => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const spotlightItem = useMemo(
|
||||||
|
() => ({
|
||||||
|
id: "homarr-assistant",
|
||||||
|
name: t("spotlight"),
|
||||||
|
icon: "/logo/logo.png",
|
||||||
|
description,
|
||||||
|
unavailable: true,
|
||||||
|
interaction: () => ({ type: "none" as const }),
|
||||||
|
}),
|
||||||
|
[description, t],
|
||||||
|
);
|
||||||
|
useRegisterSpotlightContextResults("homarr-assistant", [spotlightItem], [spotlightItem]);
|
||||||
|
useRegisterSpotlightContextActions("homarr-assistant", [spotlightItem], [spotlightItem]);
|
||||||
|
|
||||||
|
const value = useMemo(
|
||||||
|
() => ({
|
||||||
|
enabled: false,
|
||||||
|
opened: false,
|
||||||
|
isRunning: false,
|
||||||
|
unreadCount: 0,
|
||||||
|
open: () => undefined,
|
||||||
|
close: () => undefined,
|
||||||
|
toggle: () => undefined,
|
||||||
|
sendPrompt: () => undefined,
|
||||||
|
}),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
return <AssistantContext.Provider value={value}>{children}</AssistantContext.Provider>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AssistantProvider = ({ children }: PropsWithChildren) => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
const session = useSession();
|
||||||
|
const { data, isLoading } = clientApi.assistant.getAvailability.useQuery(undefined, {
|
||||||
|
enabled: session.status === "authenticated",
|
||||||
|
staleTime: 60_000,
|
||||||
|
});
|
||||||
|
const enabled = Boolean(data?.enabled && session.status === "authenticated");
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
return (
|
||||||
|
<AssistantRuntime>
|
||||||
|
<EnabledAssistantProvider>{children}</EnabledAssistantProvider>
|
||||||
|
</AssistantRuntime>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const unavailableDescription =
|
||||||
|
session.status !== "authenticated"
|
||||||
|
? t("unavailable.signIn")
|
||||||
|
: isLoading
|
||||||
|
? t("unavailable.checking")
|
||||||
|
: t("unavailable.notConfigured");
|
||||||
|
|
||||||
|
return <DisabledAssistantProvider description={unavailableDescription}>{children}</DisabledAssistantProvider>;
|
||||||
|
};
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { createAssistantPromptInteraction } from "./assistant-spotlight";
|
||||||
|
|
||||||
|
describe("createAssistantPromptInteraction", () => {
|
||||||
|
it("keeps Spotlight open when the prompt is empty", () => {
|
||||||
|
const sendPrompt = vi.fn();
|
||||||
|
const interaction = createAssistantPromptInteraction({ sendPrompt });
|
||||||
|
const [action] = interaction.useActions({}, " ");
|
||||||
|
expect(action).toBeDefined();
|
||||||
|
if (!action) return;
|
||||||
|
const sendInteraction = action.useInteraction();
|
||||||
|
|
||||||
|
expect(sendInteraction.type).toBe("javaScript");
|
||||||
|
if (sendInteraction.type !== "javaScript") return;
|
||||||
|
|
||||||
|
sendInteraction.onSelect();
|
||||||
|
expect(sendInteraction.closeSpotlightOnTrigger).toBe(false);
|
||||||
|
expect(sendPrompt).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("trims and sends a prompt in the background", () => {
|
||||||
|
const sendPrompt = vi.fn();
|
||||||
|
const interaction = createAssistantPromptInteraction({ sendPrompt });
|
||||||
|
const [action] = interaction.useActions({}, " Check my services ");
|
||||||
|
expect(action).toBeDefined();
|
||||||
|
if (!action) return;
|
||||||
|
const sendInteraction = action.useInteraction();
|
||||||
|
|
||||||
|
expect(sendInteraction.type).toBe("javaScript");
|
||||||
|
if (sendInteraction.type !== "javaScript") return;
|
||||||
|
|
||||||
|
sendInteraction.onSelect();
|
||||||
|
expect(sendInteraction.closeSpotlightOnTrigger).toBe(true);
|
||||||
|
expect(sendPrompt).toHaveBeenCalledWith("Check my services");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Group, Kbd, Stack, Text, ThemeIcon } from "@mantine/core";
|
||||||
|
import { IconArrowUp, IconRobot } from "@tabler/icons-react";
|
||||||
|
|
||||||
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
|
interface AssistantPromptInteractionOptions {
|
||||||
|
sendPrompt: (prompt: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AssistantPromptDetail = () => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
return (
|
||||||
|
<Group px="md" py="sm" wrap="nowrap">
|
||||||
|
<ThemeIcon color="red" variant="light" radius="xl">
|
||||||
|
<IconRobot size={17} />
|
||||||
|
</ThemeIcon>
|
||||||
|
<Stack gap={1}>
|
||||||
|
<Text fw={600}>{t("spotlightPrompt.title")}</Text>
|
||||||
|
<Text size="xs" c="dimmed">
|
||||||
|
{t("spotlightPrompt.description")}
|
||||||
|
</Text>
|
||||||
|
</Stack>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const AssistantPromptAction = ({ hasPrompt }: { hasPrompt: boolean }) => {
|
||||||
|
const t = useScopedI18n("common.assistant");
|
||||||
|
return (
|
||||||
|
<Group w="100%" px="md" py="xs" justify="space-between" wrap="nowrap" opacity={hasPrompt ? 1 : 0.6}>
|
||||||
|
<Group gap="sm" wrap="nowrap">
|
||||||
|
<IconArrowUp size={18} />
|
||||||
|
<Text>{hasPrompt ? t("spotlightPrompt.send") : t("spotlightPrompt.empty")}</Text>
|
||||||
|
</Group>
|
||||||
|
<Kbd size="sm">Enter</Kbd>
|
||||||
|
</Group>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createAssistantPromptInteraction = ({ sendPrompt }: AssistantPromptInteractionOptions) =>
|
||||||
|
({
|
||||||
|
type: "children",
|
||||||
|
option: {},
|
||||||
|
DetailComponent: AssistantPromptDetail,
|
||||||
|
useActions: (_options: Record<string, unknown>, query: string) => {
|
||||||
|
const prompt = query.trim();
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
key: "send-assistant-prompt",
|
||||||
|
Component: () => <AssistantPromptAction hasPrompt={prompt.length > 0} />,
|
||||||
|
useInteraction: () => ({
|
||||||
|
type: "javaScript" as const,
|
||||||
|
onSelect: () => {
|
||||||
|
if (prompt.length > 0) sendPrompt(prompt);
|
||||||
|
},
|
||||||
|
closeSpotlightOnTrigger: prompt.length > 0,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
},
|
||||||
|
}) as const;
|
||||||
@@ -1969,6 +1969,7 @@
|
|||||||
"switchToLightMode": "Switch to light mode",
|
"switchToLightMode": "Switch to light mode",
|
||||||
"management": "Management",
|
"management": "Management",
|
||||||
"assistant": "Homarr Assistant",
|
"assistant": "Homarr Assistant",
|
||||||
|
"assistantThinking": "Thinking",
|
||||||
"docker": "Docker",
|
"docker": "Docker",
|
||||||
"preferences": "Your preferences",
|
"preferences": "Your preferences",
|
||||||
"logout": "Logout",
|
"logout": "Logout",
|
||||||
@@ -1984,7 +1985,13 @@
|
|||||||
"open": "Open Homarr Assistant",
|
"open": "Open Homarr Assistant",
|
||||||
"close": "Close assistant",
|
"close": "Close assistant",
|
||||||
"spotlight": "Ask Homarr Assistant",
|
"spotlight": "Ask Homarr Assistant",
|
||||||
"spotlightDescription": "Start or continue a conversation with your Homarr instance.",
|
"spotlightDescription": "Type and send a prompt without leaving the command menu.",
|
||||||
|
"spotlightPrompt": {
|
||||||
|
"title": "Ask Assistant",
|
||||||
|
"description": "Type a prompt above. Enter sends it in the background.",
|
||||||
|
"send": "Send prompt",
|
||||||
|
"empty": "Type a prompt above"
|
||||||
|
},
|
||||||
"unavailable": {
|
"unavailable": {
|
||||||
"checking": "Checking whether the assistant is available…",
|
"checking": "Checking whether the assistant is available…",
|
||||||
"notConfigured": "An administrator must configure and enable the assistant first.",
|
"notConfigured": "An administrator must configure and enable the assistant first.",
|
||||||
@@ -2029,6 +2036,20 @@
|
|||||||
"description": "Try again. If the problem continues, ask an administrator to verify the configured model endpoint.",
|
"description": "Try again. If the problem continues, ask an administrator to verify the configured model endpoint.",
|
||||||
"retry": "Try again"
|
"retry": "Try again"
|
||||||
},
|
},
|
||||||
|
"busy": {
|
||||||
|
"title": "The assistant is already working",
|
||||||
|
"description": "Wait for the current response or open it to stop the run."
|
||||||
|
},
|
||||||
|
"activity": {
|
||||||
|
"thinking": "Assistant is thinking",
|
||||||
|
"working": "Working on your prompt",
|
||||||
|
"ready": "Response ready",
|
||||||
|
"completed": "Open the response",
|
||||||
|
"approval": "Approval needed",
|
||||||
|
"failed": "Assistant needs attention",
|
||||||
|
"expand": "Open the assistant response",
|
||||||
|
"dismiss": "Dismiss assistant activity"
|
||||||
|
},
|
||||||
"scrollToLatest": "Scroll to latest",
|
"scrollToLatest": "Scroll to latest",
|
||||||
"approvalRequired": "Approval required",
|
"approvalRequired": "Approval required",
|
||||||
"complete": "Complete",
|
"complete": "Complete",
|
||||||
|
|||||||
Reference in new issue
Block a user