@appchy/jarvis 0.1.19 → 0.1.20

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.
package/dist/bin.js CHANGED
@@ -2825,7 +2825,11 @@ function createUpstreamClient(config) {
2825
2825
  }
2826
2826
  function getTokenExpiry(token) {
2827
2827
  try {
2828
- const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
2828
+ const parts = token.split(".");
2829
+ if (parts.length === 5) {
2830
+ return Math.floor(Date.now() / 1e3) + 3600;
2831
+ }
2832
+ const payload = JSON.parse(Buffer.from(parts[1], "base64").toString());
2829
2833
  return payload.exp ?? null;
2830
2834
  } catch {
2831
2835
  return null;
@@ -2871,6 +2875,29 @@ function createUpstreamClient(config) {
2871
2875
  return false;
2872
2876
  }
2873
2877
  }
2878
+ function attemptReauth() {
2879
+ if (!config.onAuthExhausted) {
2880
+ logger.sys.error("[Upstream] Auth failed after max retries, giving up");
2881
+ return;
2882
+ }
2883
+ logger.sys.info("[Upstream] Auth exhausted, attempting re-authentication...");
2884
+ config.onAuthExhausted().then((result) => {
2885
+ if (!result) {
2886
+ logger.sys.error("[Upstream] Re-authentication failed, giving up");
2887
+ return;
2888
+ }
2889
+ currentToken = result.token;
2890
+ if (result.refreshToken) config.refreshToken = result.refreshToken;
2891
+ authFailures = 0;
2892
+ retries = 0;
2893
+ logger.sys.info("[Upstream] Re-authenticated, reconnecting...");
2894
+ reconnectTimer = setTimeout(connect, 1e3);
2895
+ }).catch((err) => {
2896
+ logger.sys.error("[Upstream] Re-authentication error", {
2897
+ error: err instanceof Error ? err.message : String(err)
2898
+ });
2899
+ });
2900
+ }
2874
2901
  function connect() {
2875
2902
  if (closed) return;
2876
2903
  ws = new WebSocket2(config.apiUrl, {
@@ -2904,10 +2931,7 @@ function createUpstreamClient(config) {
2904
2931
  } else {
2905
2932
  authFailures++;
2906
2933
  if (authFailures >= MAX_AUTH_RETRIES) {
2907
- logger.sys.error("[Upstream] Auth failed after max retries, giving up", {
2908
- code,
2909
- attempts: authFailures
2910
- });
2934
+ attemptReauth();
2911
2935
  return;
2912
2936
  }
2913
2937
  reconnectTimer = setTimeout(connect, 3e3);
@@ -2917,10 +2941,7 @@ function createUpstreamClient(config) {
2917
2941
  }
2918
2942
  authFailures++;
2919
2943
  if (authFailures >= MAX_AUTH_RETRIES) {
2920
- logger.sys.error("[Upstream] Auth failed after max retries, giving up", {
2921
- code,
2922
- attempts: authFailures
2923
- });
2944
+ attemptReauth();
2924
2945
  return;
2925
2946
  }
2926
2947
  logger.sys.warn("[Upstream] Auth failed, retrying...", {
@@ -5722,7 +5743,20 @@ function createCli() {
5722
5743
  const apiUrl = process.env.JARVIS_UPSTREAM_URL ?? config?.apiUrl;
5723
5744
  const token = process.env.JARVIS_UPSTREAM_TOKEN ?? config?.token;
5724
5745
  if (opts.upstream && apiUrl && token) {
5725
- return { apiUrl, token, refreshToken: config?.refreshToken };
5746
+ const appUrl = getAppUrl();
5747
+ return {
5748
+ apiUrl,
5749
+ token,
5750
+ refreshToken: config?.refreshToken,
5751
+ onAuthExhausted: async () => {
5752
+ console.log("\n[Auth] Token expired \u2014 re-authenticating...");
5753
+ const ok = await browserAuth(appUrl);
5754
+ if (!ok) return null;
5755
+ const fresh = loadConfig();
5756
+ if (!fresh?.token) return null;
5757
+ return { token: fresh.token, refreshToken: fresh.refreshToken };
5758
+ }
5759
+ };
5726
5760
  }
5727
5761
  return void 0;
5728
5762
  })()