feat(zai): auto-refresh expired Z.ai JWTs from an existing Chrome CDP endpoint - #9
Open
francialisomlimoeiro wants to merge 1 commit into
Open
Conversation
… endpoint Z.ai sessions mint short-lived JWTs stored in localStorage. When they expire, API calls fail with auth errors and the account is skipped until someone logs in again manually. Add an opt-in refresher that reads the fresh JWT from the localStorage of an already-running Chrome with a logged-in chat.z.ai session, via its CDP HTTP endpoint (no new browser is launched): - ZAI_CDP_URL — CDP endpoint of the existing Chrome (e.g. http://127.0.0.1:9222). Setting it enables the refresher. - Reactive refresh: on a Z.ai auth error the affected account's token is refreshed before retry. - Proactive refresh: a background loop refreshes tokens whose TTL drops below ZAI_TOKEN_REFRESH_THRESHOLD_MS (default 5 min), checked every ZAI_TOKEN_REFRESH_INTERVAL_MS (default 60 s), with a per-attempt cooldown ZAI_TOKEN_REFRESH_COOLDOWN_MS (default 30 s). All refresh failures are logged and non-fatal — the account just stays in its normal error-cooldown path. Adds tests/zaiTokenRefresh.test.js (19 tests: JWT decode/expiry math, auth-error detection, refresher lifecycle with a stub CDP endpoint, AccountManager integration).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it does
Z.ai sessions mint short-lived JWTs stored in localStorage; when they expire, API calls fail with auth errors and the account is skipped until a manual re-login. Adds an opt-in refresher that reads a fresh JWT from the localStorage of an already-running Chrome with a logged-in chat.z.ai session via its CDP HTTP endpoint (no new browser is launched):
ZAI_CDP_URL— CDP endpoint of the existing Chrome (e.g.http://127.0.0.1:9222). Setting it enables the refresher.ZAI_TOKEN_REFRESH_THRESHOLD_MS(default 5 min), checked everyZAI_TOKEN_REFRESH_INTERVAL_MS(default 60 s), with a per-attempt cooldownZAI_TOKEN_REFRESH_COOLDOWN_MS(default 30 s).Tests
node --test tests/zaiTokenRefresh.test.js→ 19 passed (JWT decode/expiry math, auth-error detection, refresher lifecycle against a stub CDP endpoint, AccountManager integration).node --test tests/*.test.js→ 46 passed.