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.

Add Assistant recent model cycling

rcourtmand1046132bbb5
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 f9451fdd2b..75a4b9e5ca 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
+++ b/frontend-modern/src/components/AI/Chat/__tests__/AIChat.test.tsx
@@ -3585,6 +3585,57 @@ describe('AIChat', () => {
expect(mockChat.setModel).toHaveBeenCalledWith('plain-model-name');
expect(localStorage.getItem('pulse:ai_chat_recent_models')).toBeNull();
});
+
+ it('cycles to the next recent model route without reordering recents', () => {
+ const recents = [
+ 'openrouter:qwen/qwen3.7-plus',
+ 'openrouter:deepseek/deepseek-v4-pro',
+ 'gemini:gemini-3.1-flash-lite',
+ ];
+ localStorage.setItem('pulse:ai_chat_recent_models', JSON.stringify(recents));
+ mockChat.model.mockReturnValue('openrouter:qwen/qwen3.7-plus');
+
+ renderChat();
+
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: /Cycle recent Assistant model: DeepSeek: DeepSeek V4 Pro via OpenRouter/,
+ }),
+ );
+
+ expect(mockChat.setModel).toHaveBeenCalledWith('openrouter:deepseek/deepseek-v4-pro');
+ expect(localStorage.getItem('pulse:ai_chat_recent_models')).toBe(JSON.stringify(recents));
+ });
+
+ it('cycles to the first recent model route when the current model is outside recents', () => {
+ localStorage.setItem(
+ 'pulse:ai_chat_recent_models',
+ JSON.stringify(['openrouter:qwen/qwen3.7-plus']),
+ );
+ mockChat.model.mockReturnValue('openai:gpt-4o');
+
+ renderChat();
+
+ fireEvent.click(
+ screen.getByRole('button', {
+ name: /Cycle recent Assistant model: Qwen: Qwen3.7 Plus via OpenRouter/,
+ }),
+ );
+
+ expect(mockChat.setModel).toHaveBeenCalledWith('openrouter:qwen/qwen3.7-plus');
+ });
+
+ it('disables recent model cycling when the active route is the only recent route', () => {
+ localStorage.setItem(
+ 'pulse:ai_chat_recent_models',
+ JSON.stringify(['openrouter:qwen/qwen3.7-plus']),
+ );
+ mockChat.model.mockReturnValue('openrouter:qwen/qwen3.7-plus');
+
+ renderChat();
+
+ expect(screen.getByRole('button', { name: 'Cycle recent Assistant model' })).toBeDisabled();
+ });
});
// ── Autonomous banner ────────────────────────────────────────────────
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/assistantModelRoutes.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/assistantModelRoutes.test.ts
new file mode 100644
index 0000000000..3f768fa438
--- /dev/null
+++ b/frontend-modern/src/components/AI/Chat/__tests__/assistantModelRoutes.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, it } from 'vitest';
+import {
+ getNextAssistantRecentModelRoute,
+ isAssistantExplicitModelRoute,
+ normalizeAssistantRecentModelRoutes,
+} from '../assistantModelRoutes';
+
+describe('assistantModelRoutes', () => {
+ it('accepts provider-qualified model routes only', () => {
+ expect(isAssistantExplicitModelRoute('openrouter:qwen/qwen3.7-plus')).toBe(true);
+ expect(isAssistantExplicitModelRoute('deepseek:deepseek-chat')).toBe(true);
+ expect(isAssistantExplicitModelRoute('plain-model-name')).toBe(false);
+ expect(isAssistantExplicitModelRoute('openrouter:')).toBe(false);
+ expect(isAssistantExplicitModelRoute(':qwen/qwen3.7-plus')).toBe(false);
+ expect(isAssistantExplicitModelRoute('https://openrouter.ai/models/qwen')).toBe(false);
+ expect(isAssistantExplicitModelRoute('openrouter:/qwen/qwen3.7-plus')).toBe(false);
+ });
+
+ it('normalizes recent routes by trimming, filtering, deduping, and capping', () => {
+ expect(
+ normalizeAssistantRecentModelRoutes(
+ [
+ ' openrouter:qwen/qwen3.7-plus ',
+ 'plain-model-name',
+ 'deepseek:deepseek-chat',
+ 'openrouter:qwen/qwen3.7-plus',
+ 'gemini:gemini-3.1-flash-lite',
+ ],
+ 2,
+ ),
+ ).toEqual(['openrouter:qwen/qwen3.7-plus', 'deepseek:deepseek-chat']);
+ });
+
+ it('cycles to the next recent model without reordering the list', () => {
+ const recentModelIds = [
+ 'openrouter:qwen/qwen3.7-plus',
+ 'openrouter:deepseek/deepseek-v4-pro',
+ 'gemini:gemini-3.1-flash-lite',
+ ];
+
+ expect(
+ getNextAssistantRecentModelRoute({
+ currentModel: 'openrouter:qwen/qwen3.7-plus',
+ recentModelIds,
+ }),
+ ).toBe('openrouter:deepseek/deepseek-v4-pro');
+ expect(
+ getNextAssistantRecentModelRoute({
+ currentModel: 'gemini:gemini-3.1-flash-lite',
+ recentModelIds,
+ }),
+ ).toBe('openrouter:qwen/qwen3.7-plus');
+ });
+
+ it('selects the first recent route when the current model is outside recents', () => {
+ expect(
+ getNextAssistantRecentModelRoute({
+ currentModel: 'openai:gpt-4o',
+ recentModelIds: ['openrouter:qwen/qwen3.7-plus'],
+ }),
+ ).toBe('openrouter:qwen/qwen3.7-plus');
+ });
+
+ it('returns null when the only recent route is already active', () => {
+ expect(
+ getNextAssistantRecentModelRoute({
+ currentModel: 'openrouter:qwen/qwen3.7-plus',
+ recentModelIds: ['openrouter:qwen/qwen3.7-plus'],
+ }),
+ ).toBeNull();
+ });
+});
diff --git a/frontend-modern/src/components/AI/Chat/assistantModelRoutes.ts b/frontend-modern/src/components/AI/Chat/assistantModelRoutes.ts
new file mode 100644
index 0000000000..0ec738a768
--- /dev/null
+++ b/frontend-modern/src/components/AI/Chat/assistantModelRoutes.ts
@@ -0,0 +1,53 @@
+const MODEL_ROUTE_PROVIDER_RE = /^[a-z][a-z0-9_-]*$/i;
+
+export const isAssistantExplicitModelRoute = (modelId: string) => {
+ const candidate = modelId.trim();
+ if (!candidate || /\s/.test(candidate) || candidate.includes('://')) {
+ return false;
+ }
+ const separator = candidate.indexOf(':');
+ if (separator <= 0 || separator === candidate.length - 1) {
+ return false;
+ }
+ const provider = candidate.slice(0, separator);
+ const model = candidate.slice(separator + 1);
+ return MODEL_ROUTE_PROVIDER_RE.test(provider) && Boolean(model.trim()) && !model.startsWith('/');
+};
+
+export const normalizeAssistantRecentModelRoutes = (values: unknown[], limit: number): string[] => {
+ const seen = new Set<string>();
+ const routes: string[] = [];
+
+ for (const value of values) {
+ const modelId = typeof value === 'string' ? value.trim() : '';
+ if (!isAssistantExplicitModelRoute(modelId) || seen.has(modelId)) continue;
+ seen.add(modelId);
+ routes.push(modelId);
+ if (routes.length >= limit) break;
+ }
+
+ return routes;
+};
+
+export const getNextAssistantRecentModelRoute = (args: {
+ currentModel?: string;
+ direction?: 1 | -1;
+ recentModelIds: string[];
+}): string | null => {
+ const routes = normalizeAssistantRecentModelRoutes(
+ args.recentModelIds,
+ args.recentModelIds.length,
+ );
+ if (routes.length === 0) return null;
+
+ const direction = args.direction ?? 1;
+ const current = args.currentModel?.trim() || '';
+ const currentIndex = routes.indexOf(current);
+ if (currentIndex < 0) {
+ return direction === 1 ? routes[0] : routes[routes.length - 1];
+ }
+ if (routes.length <= 1) return null;
+
+ const nextIndex = (currentIndex + direction + routes.length) % routes.length;
+ return routes[nextIndex] || null;
+};
diff --git a/frontend-modern/src/components/AI/Chat/index.tsx b/frontend-modern/src/components/AI/Chat/index.tsx
index 17af1ca4bb..eda85b8aee 100644
--- a/frontend-modern/src/components/AI/Chat/index.tsx
+++ b/frontend-modern/src/components/AI/Chat/index.tsx
@@ -18,6 +18,7 @@ import DownloadIcon from 'lucide-solid/icons/download';
import GitForkIcon from 'lucide-solid/icons/git-fork';
import PencilIcon from 'lucide-solid/icons/pencil';
import RefreshCwIcon from 'lucide-solid/icons/refresh-cw';
+import RotateCwIcon from 'lucide-solid/icons/rotate-cw';
import SettingsIcon from 'lucide-solid/icons/settings';
import XIcon from 'lucide-solid/icons/x';
import BookmarkIcon from 'lucide-solid/icons/bookmark';
@@ -117,6 +118,11 @@ import { MentionAutocomplete, type MentionResource } from './MentionAutocomplete
import { SlashCommandAutocomplete } from './SlashCommandAutocomplete';
import { getAssistantActiveTurnStatus } from './activeTurnStatus';
import { getLastAssistantAnswerText } from './assistantAnswerText';
+import {
+ getNextAssistantRecentModelRoute,
+ isAssistantExplicitModelRoute,
+ normalizeAssistantRecentModelRoutes,
+} from './assistantModelRoutes';
import {
filterAssistantSlashCommands,
parseAssistantSlashCommand,
@@ -150,6 +156,7 @@ const AI_CHAT_SESSION_SEARCH_DEBOUNCE_MS = 150;
const AI_CHAT_SESSION_SEARCH_LIMIT = 30;
const STRUCTURED_PATROL_CONTEXT_TARGETS = new Set(['patrol-configuration', 'patrol-run']);
const STRUCTURED_RESOURCE_CONTEXT_HANDOFF_KINDS = new Set(['resource_context']);
+const AI_CHAT_CYCLE_RECENT_MODEL_LABEL = 'Cycle recent Assistant model';
type SessionPickerSection = {
title: string;
@@ -888,16 +895,7 @@ export const AIChat: Component<AIChatProps> = (props) => {
const raw = localStorage.getItem(MODEL_RECENT_STORAGE_KEY);
const parsed = raw ? JSON.parse(raw) : [];
if (!Array.isArray(parsed)) return [];
- const seen = new Set<string>();
- const recentModelIds: string[] = [];
- for (const value of parsed) {
- const modelId = typeof value === 'string' ? value.trim() : '';
- if (!modelId || !modelId.includes(':') || seen.has(modelId)) continue;
- seen.add(modelId);
- recentModelIds.push(modelId);
- if (recentModelIds.length >= AI_CHAT_RECENT_MODEL_LIMIT) break;
- }
- return recentModelIds;
+ return normalizeAssistantRecentModelRoutes(parsed, AI_CHAT_RECENT_MODEL_LIMIT);
} catch (error) {
logger.warn('[AIChat] Failed to read recent models:', error);
return [];
@@ -920,7 +918,7 @@ export const AIChat: Component<AIChatProps> = (props) => {
const rememberRecentModel = (modelId: string) => {
const normalizedModelId = modelId.trim();
- if (!normalizedModelId || !normalizedModelId.includes(':')) return;
+ if (!isAssistantExplicitModelRoute(normalizedModelId)) return;
setRecentModelIds((prev) => {
const next = [
normalizedModelId,
@@ -1564,10 +1562,30 @@ export const AIChat: Component<AIChatProps> = (props) => {
}
};
- const selectModel = (modelId: string) => {
+ const selectModel = (modelId: string, options: { rememberRecent?: boolean } = {}) => {
chat.setModel(modelId);
updateStoredModel(chat.sessionId(), modelId);
- rememberRecentModel(modelId);
+ if (options.rememberRecent !== false) {
+ rememberRecentModel(modelId);
+ }
+ };
+
+ const nextRecentModelRoute = createMemo(() =>
+ getNextAssistantRecentModelRoute({
+ currentModel: selectedChatModel(),
+ recentModelIds: recentModelIds(),
+ }),
+ );
+ const nextRecentModelRouteLabel = createMemo(() => {
+ const next = nextRecentModelRoute();
+ return next ? formatChatMessageModelRoute(next) : '';
+ });
+
+ const cycleRecentModelRoute = () => {
+ const next = nextRecentModelRoute();
+ if (!next) return;
+ selectModel(next, { rememberRecent: false });
+ focusComposer();
};
const openModelSelectorFromError = () => {
@@ -3526,6 +3544,24 @@ export const AIChat: Component<AIChatProps> = (props) => {
onModelSelect={selectModel}
onRefresh={() => loadModels(true)}
/>
+ <button
+ type="button"
+ onClick={cycleRecentModelRoute}
+ disabled={!nextRecentModelRoute()}
+ class="flex h-7 w-7 shrink-0 items-center justify-center rounded-md border border-border bg-surface text-muted transition-colors hover:border-border hover:text-base-content disabled:cursor-not-allowed disabled:opacity-45"
+ title={
+ nextRecentModelRouteLabel()
+ ? `${AI_CHAT_CYCLE_RECENT_MODEL_LABEL}: ${nextRecentModelRouteLabel()}`
+ : AI_CHAT_CYCLE_RECENT_MODEL_LABEL
+ }
+ aria-label={
+ nextRecentModelRouteLabel()
+ ? `${AI_CHAT_CYCLE_RECENT_MODEL_LABEL}: ${nextRecentModelRouteLabel()}`
+ : AI_CHAT_CYCLE_RECENT_MODEL_LABEL
+ }
+ >
+ <RotateCwIcon class="h-3.5 w-3.5" aria-hidden="true" />
+ </button>
<div class="relative" data-dropdown>
<button