Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

4.0 KB
Raw Blame
diff --git a/dist/index.cjs b/dist/index.cjs
index 712a1ea..ef6d96c 100644
--- a/dist/index.cjs
+++ b/dist/index.cjs
@@ -643,22 +643,22 @@ function warnCredentialInvalidation(provider, error, invalidated) {
 * instead of linking together the other lower-level functions in this module.
 */
 async function auth(provider, options) {
-	try {
-		return await authInternal(provider, options);
-	} catch (error) {
-		if (error instanceof require_src.OAuthError) {
-			if (error.code === require_src.OAuthErrorCode.InvalidClient || error.code === require_src.OAuthErrorCode.UnauthorizedClient) {
-				warnCredentialInvalidation(provider, error, "client credentials and tokens");
-				await provider.invalidateCredentials?.("client");
-				await provider.invalidateCredentials?.("tokens");
-				return await authInternal(provider, options);
-			} else if (error.code === require_src.OAuthErrorCode.InvalidGrant || error.code === require_src.OAuthErrorCode.InvalidDpopProof) {
-				warnCredentialInvalidation(provider, error, "tokens");
-				await provider.invalidateCredentials?.("tokens");
-				return await authInternal(provider, options);
-			}
+	const recoveredCredentials = new Set();
+	for (;;) {
+		try {
+			return await authInternal(provider, options);
+		} catch (error) {
+			if (!(error instanceof require_src.OAuthError)) throw error;
+			const recovery = error.code === require_src.OAuthErrorCode.InvalidClient || error.code === require_src.OAuthErrorCode.UnauthorizedClient
+				? "client"
+				: error.code === require_src.OAuthErrorCode.InvalidGrant || error.code === require_src.OAuthErrorCode.InvalidDpopProof ? "tokens" : void 0;
+			if (!recovery || recoveredCredentials.has(recovery)) throw error;
+			// An expired token must not consume the stale client's registration retry.
+			recoveredCredentials.add(recovery);
+			warnCredentialInvalidation(provider, error, recovery === "client" ? "client credentials and tokens" : "tokens");
+			if (recovery === "client") await provider.invalidateCredentials?.("client");
+			await provider.invalidateCredentials?.("tokens");
 		}
-		throw error;
 	}
 }
 /**
diff --git a/dist/index.mjs b/dist/index.mjs
index 31889d0..6b58aaa 100644
--- a/dist/index.mjs
+++ b/dist/index.mjs
@@ -640,22 +640,22 @@ function warnCredentialInvalidation(provider, error, invalidated) {
 * instead of linking together the other lower-level functions in this module.
 */
 async function auth(provider, options) {
-	try {
-		return await authInternal(provider, options);
-	} catch (error) {
-		if (error instanceof OAuthError) {
-			if (error.code === OAuthErrorCode.InvalidClient || error.code === OAuthErrorCode.UnauthorizedClient) {
-				warnCredentialInvalidation(provider, error, "client credentials and tokens");
-				await provider.invalidateCredentials?.("client");
-				await provider.invalidateCredentials?.("tokens");
-				return await authInternal(provider, options);
-			} else if (error.code === OAuthErrorCode.InvalidGrant || error.code === OAuthErrorCode.InvalidDpopProof) {
-				warnCredentialInvalidation(provider, error, "tokens");
-				await provider.invalidateCredentials?.("tokens");
-				return await authInternal(provider, options);
-			}
+	const recoveredCredentials = new Set();
+	for (;;) {
+		try {
+			return await authInternal(provider, options);
+		} catch (error) {
+			if (!(error instanceof OAuthError)) throw error;
+			const recovery = error.code === OAuthErrorCode.InvalidClient || error.code === OAuthErrorCode.UnauthorizedClient
+				? "client"
+				: error.code === OAuthErrorCode.InvalidGrant || error.code === OAuthErrorCode.InvalidDpopProof ? "tokens" : void 0;
+			if (!recovery || recoveredCredentials.has(recovery)) throw error;
+			// An expired token must not consume the stale client's registration retry.
+			recoveredCredentials.add(recovery);
+			warnCredentialInvalidation(provider, error, recovery === "client" ? "client credentials and tokens" : "tokens");
+			if (recovery === "client") await provider.invalidateCredentials?.("client");
+			await provider.invalidateCredentials?.("tokens");
 		}
-		throw error;
 	}
 }
 /**