modelstat 0.0.15 → 0.0.17

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/cli.mjs CHANGED
@@ -7735,6 +7735,10 @@ async function fetchDeviceMe(secret) {
7735
7735
  method: "GET",
7736
7736
  headers: { authorization: `Bearer ${secret}` }
7737
7737
  });
7738
+ if (res.statusCode === 401) {
7739
+ await res.body.dump();
7740
+ throw new DeviceMeUnauthorized();
7741
+ }
7738
7742
  if (res.statusCode >= 300) {
7739
7743
  throw new Error(`devices/me failed: ${res.statusCode} ${await res.body.text()}`);
7740
7744
  }
@@ -7829,13 +7833,19 @@ async function uploadBatch(batch) {
7829
7833
  batch_id: result.response.batch_id
7830
7834
  };
7831
7835
  }
7832
- var _ingest;
7836
+ var DeviceMeUnauthorized, _ingest;
7833
7837
  var init_api = __esm({
7834
7838
  "src/api.ts"() {
7835
7839
  "use strict";
7836
7840
  init_http();
7837
7841
  init_logger();
7838
7842
  init_config2();
7843
+ DeviceMeUnauthorized = class extends Error {
7844
+ constructor() {
7845
+ super("devices/me returned 401: device_secret not recognised");
7846
+ this.name = "DeviceMeUnauthorized";
7847
+ }
7848
+ };
7839
7849
  _ingest = null;
7840
7850
  }
7841
7851
  });
@@ -9007,12 +9017,37 @@ async function cmdConnect(opts) {
9007
9017
  process.stdout.write(` \x1B[33m\u26A0\x1B[0m ${msg}
9008
9018
  `);
9009
9019
  };
9020
+ const wipeAndSelfRegister = async (reason) => {
9021
+ warn(`${reason} \u2014 re-registering this device`);
9022
+ state.setBearer(null);
9023
+ state.setDeviceId(null);
9024
+ state.setDeviceUuid(null);
9025
+ state.setClaimCode(null);
9026
+ state.setClaimUrl(null);
9027
+ await cmdSelfRegister();
9028
+ };
9010
9029
  if (!state.deviceUuid || !state.bearer || !state.deviceId) {
9011
9030
  step("Registering this device with modelstat.ai");
9012
9031
  await cmdSelfRegister();
9013
9032
  } else {
9014
9033
  step("Re-using existing device identity");
9015
9034
  ok(`device ${state.deviceId}`);
9035
+ try {
9036
+ const me = await fetchDeviceMe(state.bearer);
9037
+ if (me.claim_code && me.claim_code !== state.claimCode) {
9038
+ state.setClaimCode(me.claim_code);
9039
+ ok(`claim code refreshed from server`);
9040
+ }
9041
+ if (me.claim_url && me.claim_url !== state.claimUrl) {
9042
+ state.setClaimUrl(me.claim_url);
9043
+ }
9044
+ } catch (e) {
9045
+ if (e instanceof DeviceMeUnauthorized) {
9046
+ await wipeAndSelfRegister("cached credentials no longer valid");
9047
+ } else {
9048
+ warn(`couldn't refresh device state: ${e.message}`);
9049
+ }
9050
+ }
9016
9051
  }
9017
9052
  const claimCode = state.claimCode ?? "(unknown)";
9018
9053
  const claimUrl = state.claimUrl ?? `https://modelstat.ai/device/${claimCode}`;