From cd9d089d4f7d7d44eb71767d8079714ae9e963f3 Mon Sep 17 00:00:00 2001 From: ethan Date: Fri, 28 Nov 2025 13:14:03 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20perf:=20fix=20streaming=20conten?= =?UTF-8?q?t=20delay=20from=20ORPC=20schema=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reorder WorkspaceChatMessageSchema union to put discriminatedUnion first. Before: MuxMessageSchema was tried first for every streaming event, but stream-delta/stream-start/etc. don't match its shape (they have 'type' field, MuxMessage has 'id'/'role'/'parts'). This caused a failed validation attempt on every single event during streaming. After: discriminatedUnion is tried first, giving O(1) lookup by 'type' field for the most frequent events. MuxMessageSchema moves to last since full messages are only sent on stream-end and history replay. Also consolidated the two separate discriminatedUnions into one. --- src/common/orpc/schemas/stream.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common/orpc/schemas/stream.ts b/src/common/orpc/schemas/stream.ts index 9cf7e37ef..27cdb1df0 100644 --- a/src/common/orpc/schemas/stream.ts +++ b/src/common/orpc/schemas/stream.ts @@ -225,8 +225,11 @@ export const RestoreToInputEventSchema = z.object({ imageParts: z.array(ImagePartSchema).optional(), }); +// Order matters: z.union() tries schemas in order until one passes. +// Put discriminatedUnion first since streaming events (stream-delta, etc.) +// are most frequent and have a `type` field for O(1) lookup. +// MuxMessageSchema lacks `type`, so trying it first caused validation overhead. export const WorkspaceChatMessageSchema = z.union([ - MuxMessageSchema, z.discriminatedUnion("type", [ CaughtUpMessageSchema, StreamErrorMessageSchema, @@ -241,9 +244,11 @@ export const WorkspaceChatMessageSchema = z.union([ ReasoningDeltaEventSchema, ReasoningEndEventSchema, UsageDeltaEventSchema, + QueuedMessageChangedEventSchema, + RestoreToInputEventSchema, ]), WorkspaceInitEventSchema, - z.discriminatedUnion("type", [QueuedMessageChangedEventSchema, RestoreToInputEventSchema]), + MuxMessageSchema, ]); // Update Status