Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Make Assistant session picker keyboardable

rcourtman20d86f41984c
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 8dcf205fe3..941daa77a0 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -708,8 +708,15 @@ runtime cost control, and shared AI transport surfaces.
recency ordering through
`orderByRecency`, and pinned/current session options before navigation.
Pulse adapts that source workflow with a debounced Assistant session search
- field, `GET /api/ai/sessions?search=...&limit=30`, and searched result rows
- that resume through the same session-loading path as the normal recent list.
+ field, bounded recent-session refreshes with
+ `GET /api/ai/sessions?limit=30`, searched refreshes with
+ `GET /api/ai/sessions?search=...&limit=30`, and searched result rows that
+ resume through the same session-loading path as the normal recent list.
+ The picker trigger and result rows are part of that source-backed workflow:
+ the trigger must expose its accessible name and expanded state, opening the
+ picker must focus search, and each result must be a keyboard-addressable
+ option with list navigation and a named delete action instead of a
+ mouse-only row.
The empty Assistant drawer may surface recent non-empty sessions as direct
resume actions using the backend session list already owned by the drawer;
it must not create a parallel recent-chat store or product-authored prompt
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
index 4d4232627f..bd7d6bdf26 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
@@ -1858,9 +1858,16 @@ describe('AIChat', () => {
it('opens session picker on click', async () => {
renderChat();
- fireEvent.click(screen.getByTitle('Pulse Assistant sessions'));
+ const sessionButton = screen.getByRole('button', { name: 'Pulse Assistant sessions' });
+ fireEvent.click(sessionButton);
await waitFor(() => {
+ expect(sessionButton).toHaveAttribute('aria-expanded', 'true');
+ expect(screen.getByRole('dialog', { name: 'Pulse Assistant sessions' })).toBeInTheDocument();
+ expect(
+ screen.getByRole('listbox', { name: 'Assistant session history' }),
+ ).toBeInTheDocument();
expect(screen.getByText('New session')).toBeInTheDocument();
+ expect(document.activeElement).toBe(screen.getByPlaceholderText('Search sessions...'));
});
});
@@ -1886,11 +1893,13 @@ describe('AIChat', () => {
renderChat();
await waitFor(() => {
expect(mockAIChatAPI.listSessions).toHaveBeenCalledTimes(1);
+ expect(mockAIChatAPI.listSessions).toHaveBeenNthCalledWith(1, { limit: 30 });
});
fireEvent.click(screen.getByTitle('Pulse Assistant sessions'));
await waitFor(() => {
expect(mockAIChatAPI.listSessions).toHaveBeenCalledTimes(2);
+ expect(mockAIChatAPI.listSessions).toHaveBeenNthCalledWith(2, { limit: 30 });
expect(screen.getByText('Fresh session')).toBeInTheDocument();
});
});
@@ -1906,13 +1915,56 @@ describe('AIChat', () => {
});
fireEvent.click(screen.getByTitle('Pulse Assistant sessions'));
await waitFor(() => {
- expect(screen.getByText('Session One')).toBeInTheDocument();
- expect(screen.getByText('Session Two')).toBeInTheDocument();
+ expect(
+ screen.getByRole('option', { name: 'Resume Session One, 5 messages' }),
+ ).toHaveAttribute('aria-selected', 'false');
+ expect(
+ screen.getByRole('option', { name: 'Resume Session Two, 3 messages' }),
+ ).toHaveAttribute('aria-selected', 'false');
expect(screen.getByText('5 messages')).toBeInTheDocument();
expect(screen.getByText('3 messages')).toBeInTheDocument();
});
});
+ it('moves keyboard focus through session picker results', async () => {
+ mockAIChatAPI.listSessions.mockResolvedValue([
+ { id: 's1', title: 'Session One', created_at: '', updated_at: '', message_count: 5 },
+ { id: 's2', title: 'Session Two', created_at: '', updated_at: '', message_count: 3 },
+ ]);
+ renderChat();
+ await waitFor(() => {
+ expect(mockAIChatAPI.listSessions).toHaveBeenCalled();
+ });
+
+ fireEvent.click(screen.getByTitle('Pulse Assistant sessions'));
+ const searchInput = await screen.findByPlaceholderText('Search sessions...');
+ const firstOption = await screen.findByRole('option', {
+ name: 'Resume Session One, 5 messages',
+ });
+ const secondOption = screen.getByRole('option', {
+ name: 'Resume Session Two, 3 messages',
+ });
+
+ await waitFor(() => {
+ expect(document.activeElement).toBe(searchInput);
+ });
+
+ fireEvent.keyDown(searchInput, { key: 'ArrowDown' });
+ expect(document.activeElement).toBe(firstOption);
+
+ fireEvent.keyDown(firstOption, { key: 'ArrowDown' });
+ expect(document.activeElement).toBe(secondOption);
+
+ fireEvent.keyDown(secondOption, { key: 'ArrowUp' });
+ expect(document.activeElement).toBe(firstOption);
+
+ fireEvent.keyDown(firstOption, { key: 'End' });
+ expect(document.activeElement).toBe(secondOption);
+
+ fireEvent.keyDown(secondOption, { key: 'Home' });
+ expect(document.activeElement).toBe(firstOption);
+ });
+
it('searches assistant sessions from the picker', async () => {
mockAIChatAPI.listSessions
.mockResolvedValueOnce([
@@ -1976,9 +2028,11 @@ describe('AIChat', () => {
});
fireEvent.click(screen.getByTitle('Pulse Assistant sessions'));
await waitFor(() => {
- expect(screen.getByText('Session One')).toBeInTheDocument();
+ expect(
+ screen.getByRole('option', { name: 'Resume Session One, 5 messages' }),
+ ).toBeInTheDocument();
});
- fireEvent.click(screen.getByText('Session One'));
+ fireEvent.click(screen.getByRole('option', { name: 'Resume Session One, 5 messages' }));
expect(mockChat.loadSession).toHaveBeenCalledWith('s1');
});
@@ -2474,6 +2528,7 @@ describe('AIChat', () => {
await waitFor(() => {
expect(mockAIChatAPI.getStatus).toHaveBeenCalledTimes(1);
expect(mockAIChatAPI.listSessions).toHaveBeenCalledTimes(1);
+ expect(mockAIChatAPI.listSessions).toHaveBeenCalledWith({ limit: 30 });
expect(mockAIAPI.getSettings).toHaveBeenCalledTimes(1);
expect(mockAIAPI.getModels).toHaveBeenCalledTimes(1);
});
@@ -3517,10 +3572,9 @@ describe('AIChat', () => {
expect(screen.getByText('Session One')).toBeInTheDocument();
});
- // Find the delete button inside the session row
- const sessionRow = screen.getByText('Session One').closest('[class*="group"]')!;
- const deleteBtn = sessionRow.querySelector('button[type="button"]')!;
- fireEvent.click(deleteBtn);
+ fireEvent.click(
+ screen.getByRole('button', { name: 'Delete Assistant session: Session One' }),
+ );
await waitFor(() => {
expect(mockAIChatAPI.deleteSession).toHaveBeenCalledWith('s1');
diff --git a/frontend-modern/src/components/AI/Chat/index.tsx b/frontend-modern/src/components/AI/Chat/index.tsx
index 6f375dbd37..a78d06b318 100644
--- a/frontend-modern/src/components/AI/Chat/index.tsx
+++ b/frontend-modern/src/components/AI/Chat/index.tsx
@@ -527,6 +527,8 @@ export const AIChat: Component<AIChatProps> = (props) => {
const [sessionSearchError, setSessionSearchError] = createSignal('');
const [sessionDropdownPosition, setSessionDropdownPosition] = createSignal({ top: 0, right: 0 });
let sessionButtonRef: HTMLButtonElement | undefined;
+ let sessionSearchInputRef: HTMLInputElement | undefined;
+ const sessionOptionRefs = new Map<string, HTMLButtonElement>();
let sessionSearchRequestId = 0;
const [modelSelectorOpenRequest, setModelSelectorOpenRequest] = createSignal(0);
const [defaultModel, setDefaultModel] = createSignal('');
@@ -887,7 +889,7 @@ export const AIChat: Component<AIChatProps> = (props) => {
};
const refreshSessions = async () => {
- const sessionList = await AIChatAPI.listSessions();
+ const sessionList = await AIChatAPI.listSessions({ limit: AI_CHAT_SESSION_SEARCH_LIMIT });
setSessions(sessionList);
};
@@ -916,11 +918,76 @@ export const AIChat: Component<AIChatProps> = (props) => {
setSessionSearchLoading(false);
setSessionSearchError('');
};
+ const focusSessionSearch = () => {
+ queueMicrotask(() => sessionSearchInputRef?.focus());
+ };
const findKnownSession = (sessionId: string) =>
sessions().find((candidate) => candidate.id === sessionId) ||
sessionSearchResults()?.find((candidate) => candidate.id === sessionId);
+ createEffect(() => {
+ const validSessionIds = new Set(sessionPickerSessions().map((session) => session.id));
+ for (const sessionId of sessionOptionRefs.keys()) {
+ if (!validSessionIds.has(sessionId)) {
+ sessionOptionRefs.delete(sessionId);
+ }
+ }
+ });
+
+ const focusSessionOptionAtIndex = (index: number) => {
+ const session = sessionPickerSessions()[index];
+ if (!session) return false;
+ sessionOptionRefs.get(session.id)?.focus();
+ return true;
+ };
+
+ const focusSessionOptionRelativeTo = (sessionId: string, offset: number) => {
+ const sessionList = sessionPickerSessions();
+ const currentIndex = sessionList.findIndex((session) => session.id === sessionId);
+ if (currentIndex < 0) return false;
+ const nextIndex = Math.min(Math.max(currentIndex + offset, 0), sessionList.length - 1);
+ return focusSessionOptionAtIndex(nextIndex);
+ };
+
+ const handleSessionSearchKeyDown = (event: KeyboardEvent) => {
+ if (event.key !== 'ArrowDown' || event.altKey || event.ctrlKey || event.metaKey) return;
+ if (focusSessionOptionAtIndex(0)) {
+ event.preventDefault();
+ }
+ };
+
+ const handleSessionOptionKeyDown = (
+ event: KeyboardEvent & { currentTarget: HTMLButtonElement },
+ sessionId: string,
+ ) => {
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
+
+ if (event.key === 'ArrowDown' && focusSessionOptionRelativeTo(sessionId, 1)) {
+ event.preventDefault();
+ return;
+ }
+ if (event.key === 'ArrowUp' && focusSessionOptionRelativeTo(sessionId, -1)) {
+ event.preventDefault();
+ return;
+ }
+ if (event.key === 'Home' && focusSessionOptionAtIndex(0)) {
+ event.preventDefault();
+ return;
+ }
+ if (event.key === 'End' && focusSessionOptionAtIndex(sessionPickerSessions().length - 1)) {
+ event.preventDefault();
+ return;
+ }
+ if (event.key === 'Escape') {
+ event.preventDefault();
+ setShowSessions(false);
+ setSessionRefreshLoading(false);
+ resetSessionSearch();
+ sessionButtonRef?.focus();
+ }
+ };
+
// Chat hook
const chat = useChat({
model: '',
@@ -2017,6 +2084,7 @@ export const AIChat: Component<AIChatProps> = (props) => {
resetSessionSearch();
setShowSessions(true);
+ focusSessionSearch();
setSessionRefreshLoading(true);
try {
@@ -2029,6 +2097,13 @@ export const AIChat: Component<AIChatProps> = (props) => {
}
};
+ const formatSessionPickerMessageCount = (count: number) =>
+ `${count} ${count === 1 ? 'message' : 'messages'}`;
+ const getSessionPickerOptionLabel = (session: ChatSession) =>
+ `Resume ${session.title || 'Untitled'}, ${formatSessionPickerMessageCount(session.message_count)}`;
+ const getSessionDeleteLabel = (session: ChatSession) =>
+ `Delete Assistant session: ${session.title || 'Untitled'}`;
+
// Load session
const handleLoadSession = async (sessionId: string) => {
const session = findKnownSession(sessionId);
@@ -2208,6 +2283,9 @@ export const AIChat: Component<AIChatProps> = (props) => {
}}
class="flex-shrink-0 p-2 hover:text-base-content rounded-md hover:bg-surface-hover transition-colors"
title={AI_CHAT_SESSION_MENU_TITLE}
+ aria-label={AI_CHAT_SESSION_MENU_TITLE}
+ aria-haspopup="dialog"
+ aria-expanded={showSessions()}
>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
@@ -2222,6 +2300,8 @@ export const AIChat: Component<AIChatProps> = (props) => {
<Show when={showSessions()}>
<div
class="fixed w-80 max-h-[28rem] bg-surface rounded-md shadow-sm border border-border z-[9999] overflow-hidden"
+ role="dialog"
+ aria-label={AI_CHAT_SESSION_MENU_TITLE}
style={{
top: `${sessionDropdownPosition().top}px`,
right: `${sessionDropdownPosition().right}px`,
@@ -2246,9 +2326,13 @@ export const AIChat: Component<AIChatProps> = (props) => {
<SearchField
value={sessionSearchQuery()}
onChange={setSessionSearchQuery}
+ onKeyDown={handleSessionSearchKeyDown}
placeholder={AI_CHAT_SESSION_SEARCH_PLACEHOLDER}
title={AI_CHAT_SESSION_SEARCH_TITLE}
inputClass="py-1.5 text-xs"
+ inputRef={(input) => {
+ sessionSearchInputRef = input;
+ }}
/>
</div>
@@ -2262,7 +2346,11 @@ export const AIChat: Component<AIChatProps> = (props) => {
</div>
</Show>
- <div class="max-h-72 overflow-y-auto">
+ <div
+ class="max-h-72 overflow-y-auto"
+ role="listbox"
+ aria-label="Assistant session history"
+ >
<Show
when={sessionPickerSessions().length > 0}
fallback={
@@ -2274,15 +2362,25 @@ export const AIChat: Component<AIChatProps> = (props) => {
<For each={sessionPickerSessions()}>
{(session) => (
<div
- class={`group relative px-3 py-2.5 flex items-start gap-2 hover:bg-surface-hover cursor-pointer ${chat.sessionId() === session.id ? 'bg-blue-50 dark:bg-blue-900' : ''}`}
- onClick={() => handleLoadSession(session.id)}
+ class={`group relative flex items-start gap-2 px-3 py-2.5 hover:bg-surface-hover focus-within:bg-surface-hover ${chat.sessionId() === session.id ? 'bg-blue-50 dark:bg-blue-900' : ''}`}
>
- <div class="flex-1 min-w-0">
+ <button
+ type="button"
+ ref={(button) => {
+ sessionOptionRefs.set(session.id, button);
+ }}
+ role="option"
+ aria-selected={chat.sessionId() === session.id}
+ aria-label={getSessionPickerOptionLabel(session)}
+ class="min-w-0 flex-1 text-left focus:outline-none"
+ onClick={() => handleLoadSession(session.id)}
+ onKeyDown={(event) => handleSessionOptionKeyDown(event, session.id)}
+ >
<div class="text-sm font-medium truncate text-base-content">
{session.title || 'Untitled'}
</div>
<div class="text-xs text-muted">
- {session.message_count} messages
+ {formatSessionPickerMessageCount(session.message_count)}
</div>
<Show when={session.handoff_summary}>
{(summary) => (
@@ -2303,11 +2401,13 @@ export const AIChat: Component<AIChatProps> = (props) => {
</div>
)}
</Show>
- </div>
+ </button>
<button
type="button"
- class="flex-shrink-0 p-1 rounded opacity-0 group-hover:opacity-100 hover:bg-red-100 dark:hover:bg-red-900 hover:text-red-500 transition-opacity"
+ class="flex-shrink-0 p-1 rounded opacity-0 group-hover:opacity-100 group-focus-within:opacity-100 focus:opacity-100 hover:bg-red-100 dark:hover:bg-red-900 hover:text-red-500 transition-opacity"
onClick={(e) => handleDeleteSession(session.id, e)}
+ aria-label={getSessionDeleteLabel(session)}
+ title={getSessionDeleteLabel(session)}
>
<svg
class="w-3.5 h-3.5"