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
fix[tauri]: tasks and docs not loading (#2921)
Peter Chinmanda1164000e6c
diff --git a/js/app/packages/service-clients/service-sync/client.ts b/js/app/packages/service-clients/service-sync/client.tsindex 8ec0c24968..77514e5947 100644--- a/js/app/packages/service-clients/service-sync/client.ts+++ b/js/app/packages/service-clients/service-sync/client.ts@@ -191,6 +191,7 @@ export const syncServiceClient = {headers: {'Content-Type': 'application/octet-stream',Authorization: `Bearer ${token}`,+ ...(isTauri() && { Origin: SYNC_ORIGIN }),},method: 'GET',}@@ -210,6 +211,7 @@ export const syncServiceClient = {headers: {'Content-Type': 'application/json',Authorization: `Bearer ${token}`,+ ...(isTauri() && { Origin: SYNC_ORIGIN }),},method: 'GET',}diff --git a/js/app/packages/websocket/platform/tauri-websocket.ts b/js/app/packages/websocket/platform/tauri-websocket.tsindex b38d1d6938..d8e9c295ea 100644--- a/js/app/packages/websocket/platform/tauri-websocket.ts+++ b/js/app/packages/websocket/platform/tauri-websocket.ts@@ -1,7 +1,9 @@import { isTauri } from '@core/util/platform';+import { Channel, invoke } from '@tauri-apps/api/core';import TauriWebsocket, {type Message as TauriMessage,} from '@tauri-apps/plugin-websocket';+import { match } from 'ts-pattern';import type { MinimalWebSocket, WebSocketFactory } from './minimal-websocket';/**@@ -41,48 +43,78 @@ export class TauriWebSocketWrapper implements MinimalWebSocket {protocols?: string | string[]) {try {- // Convert protocols to the format expected by Tauri (though not directly supported in config)- const config =- protocols && protocols.length > 0- ? {- // Tauri doesn't have direct protocol support in config, but we can add headers if needed- }- : undefined;-- this.ws = await TauriWebsocket.connect(url, config);-- // Set up message listener- this.removeListener = this.ws.addListener((message: TauriMessage) => {- switch (message.type) {- case 'Text':- this.handleMessage(message.data);- break;- case 'Binary':+ // The Tauri websocket plugin does not support subprotocol negotiation, so+ // we keep the shared factory signature for compatibility but do not+ // forward `protocols` to the plugin connect call.+ void protocols;++ // Workaround for https://github.com/tauri-apps/plugins-workspace/issues/3152:+ // TauriWebsocket.connect() only exposes the listeners Set after it resolves, so+ // messages sent by the server immediately after the handshake can arrive via IPC+ // before addListener() is called and get dropped. Instead, we call invoke()+ // directly and pre-register our handler BEFORE the connection is made.+ const listeners = new Set<(message: TauriMessage) => void>();++ const handleMessage = (message: TauriMessage) => {+ match(message)+ .with({ type: 'Text' }, ({ data }) => {+ this.handleMessage(data);+ })+ .with({ type: 'Binary' }, ({ data }) => {// Convert number array back to Uint8Array/Blob based on binaryType- const data =+ const messageData =this._binaryType === 'arraybuffer'- ? new Uint8Array(message.data).buffer- : new Blob([new Uint8Array(message.data)]);- this.handleMessage(data);- break;- case 'Close':+ ? new Uint8Array(data).buffer+ : new Blob([new Uint8Array(data)]);+ this.handleMessage(messageData);+ })+ .with({ type: 'Close' }, ({ data }) => {this._readyState = this.CLOSED;const closeEvent = new CloseEvent('close', {- code: message.data?.code || 1000,- reason: message.data?.reason || '',+ code: data?.code || 1000,+ reason: data?.reason || '',wasClean: true,});this.handleClose(closeEvent);- break;- case 'Ping':+ })+ .with({ type: 'Ping' }, () => {// Handle ping (usually automatic)- break;- case 'Pong':+ })+ .with({ type: 'Pong' }, () => {// Handle pong (usually automatic)- break;- }+ })+ .exhaustive();+ };++ // Register BEFORE invoke so any message that arrives during the handshake+ // is dispatched to our handler rather than dropped into an empty Set.+ listeners.add(handleMessage);++ const onMessage = new Channel<TauriMessage>();+ onMessage.onmessage = (message: TauriMessage) => {+ listeners.forEach((l) => {+ l(message);+ });+ };++ const id = await invoke<number>('plugin:websocket|connect', {+ url,+ onMessage,});+ // Reconstruct a TauriWebsocket instance from the connection id and our+ // pre-populated listeners Set, matching the internal shape of the plugin class.+ this.ws = new (+ TauriWebsocket as unknown as new (+ id: number,+ listeners: Set<(arg: TauriMessage) => void>+ ) => TauriWebsocket+ )(id, listeners);++ this.removeListener = () => {+ listeners.delete(handleMessage);+ };+this._readyState = this.OPEN;console.log(`initialized tauri websocket for ${url}`);// Trigger open eventdiff --git a/js/app/tauri/src-tauri/src/lib.rs b/js/app/tauri/src-tauri/src/lib.rsindex af3c08d3aa..93176151ea 100644--- a/js/app/tauri/src-tauri/src/lib.rs+++ b/js/app/tauri/src-tauri/src/lib.rs@@ -229,10 +229,30 @@ pub fn run() {/// fn to merge the headers from the http cookie store into the initial/// GET request to open a websocketfn merge_header_callback<R: Runtime>(url: String, headers: &mut HeaderMap, handle: &AppHandle<R>) {- let Some(s) = handle.try_state::<tauri_plugin_http::Http>() else {+ let Ok(mut parsed_url) = Url::parse(&url) else {return;};- let Ok(mut parsed_url) = url.parse::<Url>() else {++ // Origin headers are required for service auth and must be set unconditionally,+ // independent of whether cookie state is available.+ match parsed_url.host_str() {+ Some("services.macro.com") | Some("services-dev.macro.com") => {+ headers.insert(ORIGIN, HeaderValue::from_static("https://macro.com"));+ }+ // The sync service (macroverse.workers.dev) also validates Origin.+ Some("macroverse.workers.dev") => {+ let origin = if cfg!(debug_assertions) {+ "https://dev.macro.com"+ } else {+ "https://macro.com"+ };+ headers.insert(ORIGIN, HeaderValue::from_static(origin));+ }+ _ => {}+ }++ // Cookie forwarding requires the HTTP plugin's cookie jar.+ let Some(s) = handle.try_state::<tauri_plugin_http::Http>() else {return;};parsed_url@@ -247,21 +267,6 @@ fn merge_header_callback<R: Runtime>(url: String, headers: &mut HeaderMap, handltracing::trace!("inserting cookie value for {parsed_url}");headers.insert(COOKIE, cookie);}-- // The API Gateway authorizer for services.macro.com validates Origin against an allowlist.- if url.contains("services.macro.com") || url.contains("services-dev.macro.com") {- headers.insert(ORIGIN, HeaderValue::from_static("https://macro.com"));- }-- // The sync service (macroverse.workers.dev) also validates Origin.- if url.contains("macroverse.workers.dev") {- let origin = if cfg!(debug_assertions) {- "https://dev.macro.com"- } else {- "https://macro.com"- };- headers.insert(ORIGIN, HeaderValue::from_static(origin));- }}trait AppChain {