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;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatSessionTime(value?: string): string {
|
function formatSessionTime(value?: string | null): string {
|
||||||
if (!value) return "本地历史";
|
if (!value) return "本地历史";
|
||||||
const date = new Date(value);
|
const date = new Date(value);
|
||||||
if (Number.isNaN(date.getTime())) return "本地历史";
|
if (Number.isNaN(date.getTime())) return "本地历史";
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
astrbotCapabilitySnapshotSchema,
|
||||||
|
boundRuntimeEventSchema,
|
||||||
chatSessionSchema,
|
chatSessionSchema,
|
||||||
mergeLoadedChatMessages,
|
mergeLoadedChatMessages,
|
||||||
|
remoteChatSessionSchema,
|
||||||
storedChatMessageSchema,
|
storedChatMessageSchema,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
|
|
@ -42,6 +45,47 @@ describe("chat persistence IPC schemas", () => {
|
||||||
).toMatchObject({ content: "hello" });
|
).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", () => {
|
it("replaces loaded session messages without duplicating optimistic users", () => {
|
||||||
const record = {
|
const record = {
|
||||||
accountId: "local-default",
|
accountId: "local-default",
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,11 @@ export const boundRuntimeEventSchema = z
|
||||||
eventId: z.string().min(1),
|
eventId: z.string().min(1),
|
||||||
eventType: z.string().min(1),
|
eventType: z.string().min(1),
|
||||||
serverInstanceId: z.string().min(1),
|
serverInstanceId: z.string().min(1),
|
||||||
sessionId: z.string().min(1).optional(),
|
// Rust serializes `Option::None` as `null`, not as an absent key, so these
|
||||||
messageId: z.string().min(1).optional(),
|
// 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(),
|
payload: z.unknown(),
|
||||||
})
|
})
|
||||||
.strict();
|
.strict();
|
||||||
|
|
@ -61,8 +64,8 @@ export type ChatSession = z.infer<typeof chatSessionSchema>;
|
||||||
|
|
||||||
export const remoteChatSessionSchema = z.object({
|
export const remoteChatSessionSchema = z.object({
|
||||||
sessionId: z.string().min(1),
|
sessionId: z.string().min(1),
|
||||||
displayName: z.string().min(1).optional(),
|
displayName: z.string().min(1).nullish(),
|
||||||
updatedAt: z.string().min(1).optional(),
|
updatedAt: z.string().min(1).nullish(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type RemoteChatSession = z.infer<typeof remoteChatSessionSchema>;
|
export type RemoteChatSession = z.infer<typeof remoteChatSessionSchema>;
|
||||||
|
|
@ -153,8 +156,8 @@ export function mergeLoadedChatMessages(
|
||||||
|
|
||||||
export const astrbotModelOptionSchema = z.object({
|
export const astrbotModelOptionSchema = z.object({
|
||||||
id: z.string().min(1),
|
id: z.string().min(1),
|
||||||
name: z.string().min(1).optional(),
|
name: z.string().min(1).nullish(),
|
||||||
isDefault: z.boolean().optional(),
|
isDefault: z.boolean().nullish(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const astrbotAttachmentSchema = z.object({
|
export const astrbotAttachmentSchema = z.object({
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用