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: add unauthed retry to legacy api rpc client (#777)
gbirman06c8a18c81c2
diff --git a/js/app/packages/service-clients/service-gql/client.ts b/js/app/packages/service-clients/service-gql/client.tsindex 0572a2f6ec..434e264b48 100644--- a/js/app/packages/service-clients/service-gql/client.ts+++ b/js/app/packages/service-clients/service-gql/client.ts@@ -11,16 +11,19 @@ import { LegacyApiRpcClient } from '../../codegen/auth_service/auth_service_rpc'// Create a singleton instance of the RPC clientlet rpcClientInstance: LegacyApiRpcClient | null = null;+let headers: Headers | null = null;async function getRpcClient(): Promise<LegacyApiRpcClient> {- if (!rpcClientInstance) {- const headers = new Headers();+ if (headers == null) {+ headers = new Headers();if (ENABLE_BEARER_TOKEN_AUTH) {const token = await getAccessToken();if (token) headers.set('Authorization', `Bearer ${token}`);}+ }+ if (!rpcClientInstance) {rpcClientInstance = LegacyApiRpcClient.construct_with_headers(`${SERVER_HOSTS['auth-service']}/user`,() => headers@@ -29,6 +32,36 @@ async function getRpcClient(): Promise<LegacyApiRpcClient> {return rpcClientInstance;}+// TODO: bake this as middleware in the generated client+async function withRetryOn401<T>(+ fn: () => Promise<T>,+ maxRetries = 3+): Promise<T> {+ let lastError: any;++ for (let attempt = 0; attempt <= maxRetries; attempt++) {+ try {+ return await fn();+ } catch (error: any) {+ lastError = error;++ // NOTE: this is the response from the generated client for a 401 error+ const is401 = typeof error === 'string' && error.includes('401');++ if (!is401 || attempt === maxRetries) {+ throw error;+ }++ headers = null;++ const backoffMs = (attempt + 1) * 100;+ await new Promise((resolve) => setTimeout(resolve, backoffMs));+ }+ }++ throw lastError;+}+export enum MacroPermissions {/** Able to use editor feature */ReadDocxEditor = 'ReadDocxEditor',@@ -71,8 +104,10 @@ export const gqlServiceClient = {getUserInfo: cache(async function getUserPermissions() {try {- const client = await getRpcClient();- const data = await client.get_legacy_user_permissions();+ const data = await withRetryOn401(async () => {+ const client = await getRpcClient();+ return await client.get_legacy_user_permissions();+ });return ok({id: data.userId,@@ -97,8 +132,10 @@ export const gqlServiceClient = {),async getOrganization() {try {- const client = await getRpcClient();- const result = await client.get_user_organization();+ const result = await withRetryOn401(async () => {+ const client = await getRpcClient();+ return await client.get_user_organization();+ });if (!result) {return {@@ -121,8 +158,10 @@ export const gqlServiceClient = {async isUserInOrg() {try {- const client = await getRpcClient();- const result = await client.get_user_organization();+ const result = await withRetryOn401(async () => {+ const client = await getRpcClient();+ return await client.get_user_organization();+ });return ok({isInOrg: !!result,@@ -135,8 +174,10 @@ export const gqlServiceClient = {async completeOnboarding(args: CompleteOnboardingRequest) {try {- const client = await getRpcClient();- await client.patch_user_onboarding(args);+ await withRetryOn401(async () => {+ const client = await getRpcClient();+ return await client.patch_user_onboarding(args);+ });return ok(undefined);} catch (error) {return err('NETWORK_ERROR', String(error));@@ -144,8 +185,10 @@ export const gqlServiceClient = {},async setGroup(args: SetGroupRequest) {try {- const client = await getRpcClient();- await client.patch_user_group(args);+ await withRetryOn401(async () => {+ const client = await getRpcClient();+ return await client.patch_user_group(args);+ });return ok(undefined);} catch (error) {return err('NETWORK_ERROR', String(error));