diff --git a/src/features/chat/ChatPanel.tsx b/src/features/chat/ChatPanel.tsx index 41c5908..d55e347 100644 --- a/src/features/chat/ChatPanel.tsx +++ b/src/features/chat/ChatPanel.tsx @@ -985,7 +985,7 @@ function applyRuntimeEvents( return next; } -function formatSessionTime(value?: string): string { +function formatSessionTime(value?: string | null): string { if (!value) return "本地历史"; const date = new Date(value); if (Number.isNaN(date.getTime())) return "本地历史"; diff --git a/src/features/chat/types.test.ts b/src/features/chat/types.test.ts index 2fd6154..0554f93 100644 --- a/src/features/chat/types.test.ts +++ b/src/features/chat/types.test.ts @@ -1,8 +1,11 @@ import { describe, expect, it } from "vitest"; import { + astrbotCapabilitySnapshotSchema, + boundRuntimeEventSchema, chatSessionSchema, mergeLoadedChatMessages, + remoteChatSessionSchema, storedChatMessageSchema, } from "./types"; @@ -42,6 +45,47 @@ describe("chat persistence IPC schemas", () => { ).toMatchObject({ content: "hello" }); }); + // Regression guard for the Rust `Option::None` -> JSON `null` boundary. + // `.optional()` only accepts `undefined`, so these payloads used to fail + // validation and were dropped without any user-visible signal. + it("accepts null session and message ids on runtime events", () => { + expect( + boundRuntimeEventSchema.parse({ + profileId: "profile-1", + slot: "companion", + eventId: "event-1", + eventType: "message.delta", + serverInstanceId: "server-1", + sessionId: null, + messageId: null, + payload: { delta: "hi", index: 0 }, + }), + ).toMatchObject({ eventId: "event-1" }); + }); + + it("accepts null display names and timestamps on remote sessions", () => { + expect( + remoteChatSessionSchema.array().parse([ + { sessionId: "session-1", displayName: null, updatedAt: null }, + { sessionId: "session-2", displayName: "named", updatedAt: null }, + ]), + ).toHaveLength(2); + }); + + it("accepts null model names in the capability snapshot", () => { + expect( + astrbotCapabilitySnapshotSchema.parse({ + transport: "astrbot-openapi", + textChat: true, + streaming: true, + modelSelection: true, + fileUpload: true, + imageInput: true, + models: [{ id: "model-a", name: null, isDefault: null }], + }).models, + ).toHaveLength(1); + }); + it("replaces loaded session messages without duplicating optimistic users", () => { const record = { accountId: "local-default", @@ -82,4 +126,4 @@ describe("chat persistence IPC schemas", () => { expect(result).toHaveLength(1); expect(result[0]?.id).toBe("stored-user-1"); }); -}); \ No newline at end of file +}); diff --git a/src/features/chat/types.ts b/src/features/chat/types.ts index fb26236..4e1a97f 100644 --- a/src/features/chat/types.ts +++ b/src/features/chat/types.ts @@ -32,8 +32,11 @@ export const boundRuntimeEventSchema = z eventId: z.string().min(1), eventType: z.string().min(1), serverInstanceId: z.string().min(1), - sessionId: z.string().min(1).optional(), - messageId: z.string().min(1).optional(), + // Rust serializes `Option::None` as `null`, not as an absent key, so these + // must accept null. `.optional()` here made every event without a session + // or message id fail validation and be dropped silently. + sessionId: z.string().min(1).nullish(), + messageId: z.string().min(1).nullish(), payload: z.unknown(), }) .strict(); @@ -61,8 +64,8 @@ export type ChatSession = z.infer; export const remoteChatSessionSchema = z.object({ sessionId: z.string().min(1), - displayName: z.string().min(1).optional(), - updatedAt: z.string().min(1).optional(), + displayName: z.string().min(1).nullish(), + updatedAt: z.string().min(1).nullish(), }); export type RemoteChatSession = z.infer; @@ -153,8 +156,8 @@ export function mergeLoadedChatMessages( export const astrbotModelOptionSchema = z.object({ id: z.string().min(1), - name: z.string().min(1).optional(), - isDefault: z.boolean().optional(), + name: z.string().min(1).nullish(), + isDefault: z.boolean().nullish(), }); export const astrbotAttachmentSchema = z.object({