Skip to content

feat(zai): auto-refresh expired Z.ai JWTs from an existing Chrome CDP endpoint - #9

Open
francialisomlimoeiro wants to merge 1 commit into
ForgetMeAI:mainfrom
francialisomlimoeiro:pr/zai-jwt-refresh
Open

feat(zai): auto-refresh expired Z.ai JWTs from an existing Chrome CDP endpoint#9
francialisomlimoeiro wants to merge 1 commit into
ForgetMeAI:mainfrom
francialisomlimoeiro:pr/zai-jwt-refresh

Conversation

@francialisomlimoeiro

Copy link
Copy Markdown

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.
  • Reactive: on a Z.ai auth error the affected account's token is refreshed before retry.
  • Proactive: 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 non-fatal — the account stays in its normal error-cooldown path.

Tests

  • node --test tests/zaiTokenRefresh.test.js19 passed (JWT decode/expiry math, auth-error detection, refresher lifecycle against a stub CDP endpoint, AccountManager integration).
  • Full suite node --test tests/*.test.js46 passed.

… 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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant