Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chore: improve zod usage in realtime notif update (#3455)
Sean Aye461b76f4578d
diff --git a/js/app/packages/notifications/notification-source.ts b/js/app/packages/notifications/notification-source.tsindex decbb0dc86..87cf759b39 100644--- a/js/app/packages/notifications/notification-source.ts+++ b/js/app/packages/notifications/notification-source.ts@@ -1,8 +1,6 @@import { ENABLE_DOCUMENT_MENTION_NOTIFICATIONS } from '@core/constant/featureFlags';import type { Entity } from '@core/types';import {- applyNotificationStatusUpdate,- type NotificationStatusUpdate,optimisticInsertNotification,useMarkNotificationsAsDoneMutation,useMarkNotificationsAsSeenMutation,@@ -91,7 +89,6 @@ export type NotificationSource = {};const NOTIFICATION_EVENT_TYPE = 'notification';-const NOTIFICATION_STATUS_UPDATED_EVENT_TYPE = 'notification_status_updated';const QUERY_LIMIT = 500;@@ -215,24 +212,6 @@ export function createNotificationSource(};createSocketEffect(ws, (wsData) => {- if (wsData.type === NOTIFICATION_STATUS_UPDATED_EVENT_TYPE) {- try {- const update = (- typeof wsData.data === 'string'- ? JSON.parse(wsData.data)- : wsData.data- ) as NotificationStatusUpdate;- applyNotificationStatusUpdate(update);- } catch (e) {- console.error(- 'Failed to parse notification status update',- wsData.data,- e- );- }- return;- }-if (wsData.type !== NOTIFICATION_EVENT_TYPE) {return;}diff --git a/js/app/packages/queries/notification/user-notifications.ts b/js/app/packages/queries/notification/user-notifications.tsindex 0106f52e4e..bfb06be322 100644--- a/js/app/packages/queries/notification/user-notifications.ts+++ b/js/app/packages/queries/notification/user-notifications.ts@@ -17,6 +17,7 @@ import {} from '@tanstack/solid-query';import type { Result } from 'neverthrow';import { match, P } from 'ts-pattern';+import { z } from 'zod';import { queryClient } from '../client';import { notificationKeys } from './keys';@@ -416,6 +417,43 @@ export type NotificationStatusUpdate = {updates: NotificationStatusPatchDelete[];};+const jsonStringSchema = z.string().transform((value, ctx) => {+ try {+ return JSON.parse(value) as unknown;+ } catch {+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Invalid JSON' });+ return z.NEVER;+ }+});++export const notificationStatusUpdateSchema = z.object({+ type: z.literal('notification_status_updated'),+ updates: z.array(+ z.discriminatedUnion('t', [+ z.object({+ t: z.literal('Patch'),+ c: z.object({+ id: z.string(),+ done: z.boolean(),+ viewed_at: z.string().nullable(),+ updated_at: z.string(),+ }),+ }),+ z.object({+ t: z.literal('Delete'),+ c: z.object({+ id: z.string(),+ }),+ }),+ ])+ ),+}) satisfies z.ZodType<NotificationStatusUpdate>;++export const notificationStatusUpdatePayloadSchema = z.union([+ notificationStatusUpdateSchema,+ jsonStringSchema.pipe(notificationStatusUpdateSchema),+]);+function applyNotificationStatusPatch(notification: NotificationItem,patch: NotificationStatusPatchdiff --git a/js/app/packages/queries/sync/SyncProvider.tsx b/js/app/packages/queries/sync/SyncProvider.tsxindex 844a6ee903..fb5069c857 100644--- a/js/app/packages/queries/sync/SyncProvider.tsx+++ b/js/app/packages/queries/sync/SyncProvider.tsx@@ -7,7 +7,7 @@ import { handleCommsTyping } from '@queries/channel/typing';import { invalidateContacts } from '@queries/contacts/contacts';import {applyNotificationStatusUpdate,- type NotificationStatusUpdate,+ notificationStatusUpdatePayloadSchema,} from '@queries/notification/user-notifications';// Side-effect import: registers the scheduled-action live-update websocket// listener. Must be imported somewhere that always loads on app start — this@@ -16,62 +16,40 @@ import '@queries/agent-schedule/sync';import { createConnectionWebsocketEffect } from '@service-connection/websocket';import type { Accessor, ParentProps } from 'solid-js';import { match } from 'ts-pattern';-import { z } from 'zod';type SyncProviderProps = ParentProps<{userId: Accessor<string | undefined>;}>;-const notificationStatusUpdateSchema = z.object({- type: z.literal('notification_status_updated'),- updates: z.array(- z.discriminatedUnion('t', [- z.object({- t: z.literal('Patch'),- c: z.object({- id: z.string(),- done: z.boolean(),- viewed_at: z.string().nullable(),- updated_at: z.string(),- }),- }),- z.object({- t: z.literal('Delete'),- c: z.object({- id: z.string(),- }),- }),- ])- ),-}) satisfies z.ZodType<NotificationStatusUpdate>;-export function QuerySyncProvider(props: SyncProviderProps) {createConnectionWebsocketEffect((data) => {- const payload =- typeof data.data === 'string' ? JSON.parse(data.data) : data.data;-match(data).with({ type: 'contacts_invalidation' }, () => {invalidateContacts();}).with({ type: 'comms_message' }, () => {- handleCommsMessage(payload);+ handleCommsMessage(data.data);}).with({ type: 'comms_reaction' }, () => {- handleCommsReaction(payload);+ handleCommsReaction(data.data);}).with({ type: 'comms_attachment' }, () => {- handleCommsAttachment(payload);+ handleCommsAttachment(data.data);}).with({ type: 'comms_typing' }, () => {const userId = props.userId();if (!userId) return;- handleCommsTyping(payload, userId);+ handleCommsTyping(data.data, userId);}).with({ type: 'notification_status_updated' }, () => {- const result = notificationStatusUpdateSchema.safeParse(payload);+ const result = notificationStatusUpdatePayloadSchema.safeParse(+ data.data+ );if (!result.success) {- console.warn('Malformed notification status update payload', payload);+ console.warn(+ 'Malformed notification status update payload',+ data.data+ );return;}applyNotificationStatusUpdate(result.data);diff --git a/js/bun.lock b/js/bun.lockindex ac5170a339..34867994c4 100644--- a/js/bun.lock+++ b/js/bun.lock@@ -824,7 +824,7 @@"@img/sharp-win32-x64": ["@img/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw=="],- "@inkibra/tauri-plugins": ["@inkibra/tauri-plugins@github:macro-inc/tauri-plugins#6152009", { "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1" } }, "macro-inc-tauri-plugins-6152009"],+ "@inkibra/tauri-plugins": ["@inkibra/tauri-plugins@github:macro-inc/tauri-plugins#6152009", { "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1" } }, "macro-inc-tauri-plugins-6152009", "sha512-XLN+niDA9BkzrxiZqBjYf+DL4TLoMIcI80ypCowacVfrfAjWt0qLwLkzWVUXjD7k3v0DB/G+BP4OLJNkGygHIw=="],"@inquirer/external-editor": ["@inquirer/[email protected]", "", { "dependencies": { "chardet": "^2.1.1", "iconv-lite": "^0.7.0" }, "peerDependencies": { "@types/node": ">=18" }, "optionalPeers": ["@types/node"] }, "sha512-RWbSrDiYmO4LbejWY7ttpxczuwQyZLBUyygsA9Nsv95hpzUWwnNTVQmAq3xuh7vNwCp07UTmE5i11XAEExx4RA=="],@@ -2544,7 +2544,7 @@"pathval": ["[email protected]", "", {}, "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ=="],- "pdfjs-dist": ["pdfjs-dist@github:macro-inc/pdf.js#f9b2ce6", { "dependencies": { "dommatrix": "^1.0.3", "web-streams-polyfill": "^3.2.1" }, "peerDependencies": { "worker-loader": "^3.0.8" }, "optionalPeers": ["worker-loader"] }, "macro-inc-pdf.js-f9b2ce6"],+ "pdfjs-dist": ["pdfjs-dist@github:macro-inc/pdf.js#f9b2ce6", { "dependencies": { "dommatrix": "^1.0.3", "web-streams-polyfill": "^3.2.1" }, "peerDependencies": { "worker-loader": "^3.0.8" }, "optionalPeers": ["worker-loader"] }, "macro-inc-pdf.js-f9b2ce6", "sha512-HDFaPbCxLG2GHpf1smUG97nku5aPGBGaCcIbK05dGdp07TBLUlDcXsM4WcE82mO5W4RBPIBVYobX5N/6M6NFrA=="],"performance-now": ["[email protected]", "", {}, "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow=="],