@bike4mind/cli 1.2.0 → 2.1.0

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.
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import { n as logger, t as ConfigStore } from "./ConfigStore-qV7NrCgZ.mjs";
3
- import { t as version } from "./package-CGZIoxcs.mjs";
2
+ import { n as logger, t as ConfigStore } from "./ConfigStore-BygzEI6H.mjs";
3
+ import { t as version } from "./package-CRNnNXxq.mjs";
4
4
  import axios, { isAxiosError } from "axios";
5
5
  //#region src/auth/OAuthClient.ts
6
6
  /**
@@ -90,6 +90,33 @@ var OAuthClient = class {
90
90
  }
91
91
  };
92
92
  //#endregion
93
+ //#region src/auth/providerKeyFailure.ts
94
+ const PROVIDER_KEY_ERROR_CODES = /* @__PURE__ */ new Set(["provider_not_configured", "provider_rejected"]);
95
+ /**
96
+ * A 401 whose body names a missing or rejected AI-provider key (e.g. /api/ai/tts). The
97
+ * Bike4Mind credential was accepted, so refreshing or re-authenticating cannot fix it.
98
+ * Shared by the ApiClient refresh interceptor and mapApiError in mcp/b4mApiClient.ts.
99
+ */
100
+ function isProviderKeyFailure(data) {
101
+ const body = decodeBinaryBody(data);
102
+ if (!body || typeof body !== "object") return false;
103
+ const { errorCode } = body;
104
+ return typeof errorCode === "string" && PROVIDER_KEY_ERROR_CODES.has(errorCode);
105
+ }
106
+ /**
107
+ * An arraybuffer-typed request (the generated-audio calls) gets its error body as raw
108
+ * bytes, and the refresh interceptor sees it before any caller can decode it.
109
+ */
110
+ function decodeBinaryBody(data) {
111
+ const bytes = data instanceof ArrayBuffer ? Buffer.from(data) : ArrayBuffer.isView(data) ? Buffer.from(data.buffer, data.byteOffset, data.byteLength) : void 0;
112
+ if (!bytes) return data;
113
+ try {
114
+ return JSON.parse(bytes.toString("utf8"));
115
+ } catch {
116
+ return;
117
+ }
118
+ }
119
+ //#endregion
93
120
  //#region src/auth/ApiClient.ts
94
121
  const USER_AGENT = `b4m-cli/${version}`;
95
122
  /**
@@ -121,6 +148,17 @@ var SessionRevokedError = class extends Error {
121
148
  }
122
149
  };
123
150
  /**
151
+ * 401 with no credential at all (no API key, no stored tokens): a config gap, not an expiry.
152
+ * Not a SessionRevokedError, so checkSessionValid still reports "not revoked". The message keeps
153
+ * `Authentication failed` for string-matching callers (turnController, handoff, ServerLlmBackend).
154
+ */
155
+ var NotAuthenticatedError = class extends Error {
156
+ constructor(message) {
157
+ super(message);
158
+ this.name = "NotAuthenticatedError";
159
+ }
160
+ };
161
+ /**
124
162
  * Authenticated API client for B4M services
125
163
  * Automatically injects access tokens from ConfigStore
126
164
  */
@@ -157,11 +195,12 @@ var ApiClient = class {
157
195
  if (error.response?.status === 401) logger.debug("AUTH: Received 401 Unauthorized");
158
196
  else if (error.response?.status === 403) logger.error("403 Forbidden", error);
159
197
  if (this.apiKey) return Promise.reject(error);
198
+ if (error.response?.status === 401 && isProviderKeyFailure(error.response.data)) return Promise.reject(error);
160
199
  if (error.response?.status === 401 && !originalRequest._retry) {
161
200
  originalRequest._retry = true;
201
+ const tokens = await this.configStore.getAuthTokens();
202
+ if (!tokens) throw new NotAuthenticatedError("Authentication failed: not logged in. Please run `b4m login` to authenticate.");
162
203
  try {
163
- const tokens = await this.configStore.getAuthTokens();
164
- if (!tokens) throw new Error("Not authenticated");
165
204
  if (new Date(tokens.expiresAt).getTime() > Date.now()) {
166
205
  logger.debug("AUTH: Access token has not expired, skipping refresh - 401 is likely transient");
167
206
  return Promise.reject(error);
@@ -274,4 +313,4 @@ var ApiClient = class {
274
313
  }
275
314
  };
276
315
  //#endregion
277
- export { OAuthClient as n, ApiClient as t };
316
+ export { OAuthClient as i, NotAuthenticatedError as n, isProviderKeyFailure as r, ApiClient as t };