fix(chat): accept null in runtime event contracts
Rust serializes `Option<String>` without `skip_serializing_if`, so absent fields arrive as `null`. Zod's `.optional()` accepts only `undefined`, so `safeParse` failed and three chains silently died: - `boundRuntimeEventSchema.sessionId/messageId`: desktop-v1 and mock events carry no session id, so every one of them was discarded; AstrBot error and heartbeat frames were discarded too. - `remoteChatSessionSchema.displayName/updatedAt`: one null field threw for the whole array, leaving the remote session list permanently empty. - `astrbotModelOptionSchema.name`: a null name failed capability parsing, which permanently disabled the file upload button. The sibling `slotRuntimeSnapshotSchema` in the same file already used `.nullish()` throughout, which is what makes this look like a miss during the de-enveloping change rather than a deliberate choice. `formatSessionTime` widens with it: `updatedAt` is now `string | null` and the existing falsy guard already handled null. Refs P1-7a / F-06. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
这个提交存在于:
父节点
e8d9dbc8f6
当前提交
828f7c586d
共有 3 个文件被更改,包括 55 次插入 和 8 次删除
|
|
@ -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 "本地历史";
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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<typeof chatSessionSchema>;
|
|||
|
||||
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<typeof remoteChatSessionSchema>;
|
||||
|
|
@ -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({
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用