The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(chat): a broken chat backup can't take the chat view or the app down
purplef4580031677d
diff --git a/src/lib/__tests__/ein-kaputter-import-legt-nichts-lahm.test.ts b/src/lib/__tests__/ein-kaputter-import-legt-nichts-lahm.test.tsnew file mode 100644index 00000000..228e35f2--- /dev/null+++ b/src/lib/__tests__/ein-kaputter-import-legt-nichts-lahm.test.ts@@ -0,0 +1,38 @@+// T1 Nacht-Durchtest 3.0.9 (Jäger A, A4): ein Chat-Backup mit einer Nachricht+// `"content": null` (oder 42) wurde mit "Imported 1 chat." angenommen. Die+// Chatansicht stürzte ab ("null is not an object (evaluating 'a.length')"),+// auch nach dem Neustart, und eine Suche in der Seitenleiste riss die ganze+// App mit ("e.content.toLowerCase is not a function").+// @vitest-environment jsdom+import { describe, expect, it } from 'vitest'+import { parseImportedChats } from '../chat-export'+import { useChatStore } from '../../stores/chatStore'++const broken = JSON.stringify([{+ id: 'imp-1', title: 'Imported broken', createdAt: 1, updatedAt: 2,+ messages: [+ { id: 'm1', role: 'user', content: 'hello', timestamp: 1 },+ { id: 'm2', role: 'assistant', content: null, timestamp: 2 },+ { id: 'm3', role: 'assistant', content: 42, timestamp: 3 },+ { role: 'user', content: 'no id', timestamp: 4 },+ { id: 'm5', role: 'hacker', content: 'bad role', timestamp: 5 },+ 'not even an object',+ ],+}])++describe('importing a chat backup with broken messages', () => {+ it('turns every message into one the chat view can draw', () => {+ const [c] = parseImportedChats(broken)+ for (const m of c.messages) {+ expect(typeof m.content).toBe('string')+ expect(typeof m.id).toBe('string')+ expect(['user', 'assistant', 'system', 'tool']).toContain(m.role)+ }+ expect(c.messages.map((m) => m.content)).toEqual(['hello', '', '42', 'no id'])+ })++ it('the sidebar search survives a message that is not text', () => {+ useChatStore.setState({ conversations: [{ ...JSON.parse(broken)[0], messages: [{ id: 'x', role: 'assistant', content: 42, timestamp: 1 }] }] } as never)+ expect(() => useChatStore.getState().searchConversations('zz')).not.toThrow()+ })+})diff --git a/src/lib/chat-export.ts b/src/lib/chat-export.tsindex 79eec9af..dcce6517 100644--- a/src/lib/chat-export.ts+++ b/src/lib/chat-export.ts@@ -338,7 +338,36 @@ export function parseImportedChats(json: string): Conversation[] {if (valid.length === 0) {throw new Error('No valid conversations found in that file.')}- return valid+ return valid.map((c) => ({ ...c, messages: importedMessages(c.messages as unknown[], c.id) }))+}++const ROLES = new Set(['user', 'assistant', 'system', 'tool'])++/**+ * Every message of an imported chat in the shape the chat view draws.+ *+ * A backup is a file from outside: edited by hand, from an older build, or+ * simply broken. One message with `"content": null` used to be taken as it+ * was, and the chat view fell over on it, again after every restart, while a+ * search in the sidebar took the whole app down (T1 night test 3.0.9, A4).+ * What is not a message at all, or has a role the app does not know, is+ * dropped; a missing id is made up; content that is not text becomes text.+ */+function importedMessages(raw: unknown[], conversationId: string): Message[] {+ const out: Message[] = []+ raw.forEach((m, i) => {+ if (!m || typeof m !== 'object') return+ const msg = m as Record<string, unknown>+ if (typeof msg.role !== 'string' || !ROLES.has(msg.role)) return+ const content = msg.content+ out.push({+ ...(msg as unknown as Message),+ id: typeof msg.id === 'string' && msg.id ? msg.id : `${conversationId}-import-${i}`,+ content: typeof content === 'string' ? content : content == null ? '' : typeof content === 'object' ? JSON.stringify(content) : String(content),+ timestamp: typeof msg.timestamp === 'number' ? msg.timestamp : 0,+ })+ })+ return out}/** References are installation-local. The browser build exports the originaldiff --git a/src/stores/chatStore.ts b/src/stores/chatStore.tsindex cce45b2e..49eedda3 100644--- a/src/stores/chatStore.ts+++ b/src/stores/chatStore.ts@@ -1100,8 +1100,11 @@ export const useChatStore = create<ChatState>()(const lower = query.toLowerCase()return conversations.filter((c) =>- c.title.toLowerCase().includes(lower) ||- c.messages.some((m) => m.content.toLowerCase().includes(lower))+ // String() and the typeof: a chat from an old or edited backup may+ // carry a message that is not text, and the search must not take+ // the whole app down on it (T1 night test 3.0.9, A4).+ String(c.title ?? '').toLowerCase().includes(lower) ||+ c.messages.some((m) => typeof m.content === 'string' && m.content.toLowerCase().includes(lower)))},