qwenproxy-cli 1.4.3 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -929,16 +929,28 @@ export async function requestQwenTextInBrowser(
929
929
  // If 401 Unauthorized in browser, try silent in-page token refresh before giving up
930
930
  if (response.status === 401) {
931
931
  try {
932
- const refreshRes = await fetch("https://auth.qwen.ai/api/v2/auths/refresh", {
933
- method: "GET",
934
- credentials: "include",
935
- signal: AbortSignal.timeout(3000),
936
- });
937
- if (refreshRes.status === 200) {
932
+ const rTok = localStorage.getItem("refresh_token");
933
+ if (rTok) {
934
+ const refreshRes = await fetch("https://auth.qwen.ai/api/v2/auths/refresh", {
935
+ method: "GET",
936
+ credentials: "include",
937
+ headers: {
938
+ accept: "application/json, text/plain, */*",
939
+ source: "web",
940
+ version: "0.3.11",
941
+ "x-request-origin": "https://chat.qwen.ai",
942
+ timezone: new Date().toString().split(" (")[0],
943
+ authorization: `Bearer ${rTok}`,
944
+ },
945
+ signal: AbortSignal.timeout(5000),
946
+ });
938
947
  const refreshJson: any = await refreshRes.json().catch(() => null);
939
- if (refreshJson && refreshJson.success === true && refreshJson.data?.token) {
940
- const freshTok = refreshJson.data.token;
948
+ const freshTok = refreshJson?.data?.access_token || refreshJson?.data?.token;
949
+ const nextRTok = refreshJson?.data?.refresh_token;
950
+ if (refreshJson?.success === true && freshTok) {
941
951
  localStorage.setItem("token", freshTok);
952
+ localStorage.setItem("access_token", freshTok);
953
+ if (nextRTok) localStorage.setItem("refresh_token", nextRTok);
942
954
  document.cookie = `token=${encodeURIComponent(freshTok)}; path=/; domain=.qwen.ai; max-age=31536000`;
943
955
  if (headers["authorization"] || headers["Authorization"]) {
944
956
  headers["authorization"] = `Bearer ${freshTok}`;
@@ -979,7 +991,7 @@ export async function requestQwenTextInBrowser(
979
991
  // Settings and personalization requests run as same-origin in-browser fetch
980
992
  // with appropriate Referer, keeping the page on the stable chat UI without
981
993
  // expensive page.goto navigations that can time out under load.
982
- const response = await withQwenBrowserPage<BrowserTextResponse>(
994
+ let response = await withQwenBrowserPage<BrowserTextResponse>(
983
995
  accountId,
984
996
  evaluateRequest,
985
997
  undefined,
@@ -987,6 +999,23 @@ export async function requestQwenTextInBrowser(
987
999
  recoverOnTimeout,
988
1000
  );
989
1001
 
1002
+ // If evaluateRequest still received 401, trigger outer token refresh and retry once
1003
+ if (response.status === 401 && accountId) {
1004
+ try {
1005
+ const { refreshAccountToken } = await import("./playwright.ts");
1006
+ const refreshed = await refreshAccountToken(accountId);
1007
+ if (refreshed.success) {
1008
+ response = await withQwenBrowserPage<BrowserTextResponse>(
1009
+ accountId,
1010
+ evaluateRequest,
1011
+ undefined,
1012
+ options.timeoutMs,
1013
+ recoverOnTimeout,
1014
+ );
1015
+ }
1016
+ } catch {}
1017
+ }
1018
+
990
1019
  return new Response(response.raw, {
991
1020
  status: response.status,
992
1021
  headers: response.contentType
@@ -1714,7 +1743,11 @@ export async function syncQwenRequestPersonalization(
1714
1743
  );
1715
1744
  try {
1716
1745
  currentSettings = null;
1717
- const { headers: freshHeaders } = await getQwenHeaders(true, accountId, true);
1746
+ const { refreshAccountToken } = await import("./playwright.ts");
1747
+ if (accountId) {
1748
+ await refreshAccountToken(accountId);
1749
+ }
1750
+ const { headers: freshHeaders } = await getQwenHeaders(false, accountId);
1718
1751
  requestHeaders = buildCapturedQwenHeaders(freshHeaders, {
1719
1752
  referer: qwenUrl("/settings/personalization"),
1720
1753
  });