Handle empty chat session payloads in AI settings (#1149)

This commit is contained in:
rcourtman
2026-03-25 12:12:49 +00:00
parent ffaeea18d6
commit 69f44d3829
3 changed files with 29 additions and 2 deletions
@@ -12,6 +12,7 @@ import { AIAPI } from '@/api/ai';
import { AIChatAPI, type ChatSession, type FileChange } from '@/api/aiChat'; import { AIChatAPI, type ChatSession, type FileChange } from '@/api/aiChat';
import { hasFeature, loadLicenseStatus } from '@/stores/license'; import { hasFeature, loadLicenseStatus } from '@/stores/license';
import type { AISettings as AISettingsType, AIProvider, AuthMethod } from '@/types/ai'; import type { AISettings as AISettingsType, AIProvider, AuthMethod } from '@/types/ai';
import { normalizeChatSessions } from '@/components/Settings/aiSettingsChatSessions';
// Providers are now configured via accordion sections, not a single-provider selector // Providers are now configured via accordion sections, not a single-provider selector
@@ -284,10 +285,10 @@ export const AISettings: Component = () => {
setChatSessionsLoading(true); setChatSessionsLoading(true);
setChatSessionsError(''); setChatSessionsError('');
try { try {
const sessions = await AIChatAPI.listSessions(); const sessions = normalizeChatSessions(await AIChatAPI.listSessions());
setChatSessions(sessions); setChatSessions(sessions);
const current = selectedSessionId(); const current = selectedSessionId();
if (!Array.isArray(sessions) || sessions.length === 0) { if (sessions.length === 0) {
setSelectedSessionId(''); setSelectedSessionId('');
} else if (!current || !sessions.some((session) => session.id === current)) { } else if (!current || !sessions.some((session) => session.id === current)) {
setSelectedSessionId(sessions[0].id); setSelectedSessionId(sessions[0].id);
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest';
import { normalizeChatSessions } from '@/components/Settings/aiSettingsChatSessions';
describe('normalizeChatSessions', () => {
it('returns the original array when the API payload is valid', () => {
const sessions = [
{ id: 'session-1', title: 'First', message_count: 1, updated_at: '2026-03-25T10:00:00Z' },
];
expect(normalizeChatSessions(sessions)).toEqual(sessions);
});
it('returns an empty array when the API payload is null', () => {
expect(normalizeChatSessions(null)).toEqual([]);
});
it('returns an empty array when the API payload is not an array', () => {
expect(normalizeChatSessions({ id: 'session-1' })).toEqual([]);
});
});
@@ -0,0 +1,5 @@
import type { ChatSession } from '@/api/aiChat';
export function normalizeChatSessions(value: unknown): ChatSession[] {
return Array.isArray(value) ? value : [];
}