@bodhiapp/bodhi-js-ext 0.0.41 → 0.0.43

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
- import { DirectClientBase as V, createStoragePrefixWithNamespace as X, STORAGE_PREFIXES as N, generateCodeVerifier as w, generateCodeChallenge as z, buildAuthorizeUrl as v, BASE_OAUTH_SCOPE as L, buildErrorUrl as b, AccessRequestBuilder as P, unwrapResponse as k, buildReviewUrl as O, createOperationError as d, throwAccessRequestDenialError as M, PENDING_EXTENSION_READY as f, Logger as x, NOOP_STATE_CALLBACK as Q, createExtensionStateNotInitialized as G, BACKEND_SERVER_NOT_REACHABLE as K, createExtensionStateNotFound as F, BodhiError as T, INITIAL_AUTH_STATE as H, BodhiApiError as W, Chat as J, Models as Y, Embeddings as j, Mcps as Z, isAuthError as ee, BaseFacadeClient as te, refreshAccessToken as re } from "@bodhiapp/bodhi-js-core";
1
+ import { DirectClientBase as z, createStoragePrefixWithNamespace as X, STORAGE_PREFIXES as N, generateCodeVerifier as w, generateCodeChallenge as V, buildAuthorizeUrl as v, BASE_OAUTH_SCOPE as L, buildErrorUrl as b, AccessRequestBuilder as P, unwrapResponse as k, buildReviewUrl as O, createOperationError as d, throwAccessRequestDenialError as M, PENDING_EXTENSION_READY as f, Logger as x, NOOP_STATE_CALLBACK as Q, createExtensionStateNotInitialized as G, BACKEND_SERVER_NOT_REACHABLE as K, createExtensionStateNotFound as F, BodhiError as T, INITIAL_AUTH_STATE as H, BodhiApiError as W, Chat as J, Models as Y, Embeddings as j, Mcps as Z, isAuthError as ee, BaseFacadeClient as te, refreshAccessToken as re } from "@bodhiapp/bodhi-js-core";
2
2
  import { InMemoryStorage as Ae } from "@bodhiapp/bodhi-js-core";
3
- import { isExtError as y, isApiSuccessResponse as se, MESSAGE_TYPES as _, EXT_ACTIONS as oe, BODHI_STREAM_PORT as ie, isStreamChunk as ne, isStreamApiError as ae, isStreamError as ce, BODHI_STREAM_TEXT_PORT as he } from "@bodhiapp/bodhi-browser-types";
3
+ import { isExtError as y, isApiSuccessResponse as se, MESSAGE_TYPES as _, EXT_ACTIONS as ie, BODHI_STREAM_PORT as oe, isStreamChunk as ne, isStreamApiError as ae, isStreamError as he, BODHI_STREAM_TEXT_PORT as ce } from "@bodhiapp/bodhi-browser-types";
4
4
  const a = {
5
5
  EXT2EXT_CLIENT_REQUEST: "EXT2EXT_CLIENT_REQUEST",
6
6
  EXT2EXT_CLIENT_RESPONSE: "EXT2EXT_CLIENT_RESPONSE",
@@ -42,12 +42,12 @@ class _e {
42
42
  await chrome.storage.session.remove(e);
43
43
  }
44
44
  }
45
- class me extends V {
45
+ class me extends z {
46
46
  constructor(e, t) {
47
47
  const r = X(
48
48
  e.basePath,
49
49
  N.EXT_DIRECT
50
- ), i = {
50
+ ), o = {
51
51
  authClientId: e.authClientId,
52
52
  authServerUrl: e.authServerUrl,
53
53
  storagePrefix: r,
@@ -57,47 +57,47 @@ class me extends V {
57
57
  storage: e.storage ?? new _e(),
58
58
  initialTokens: e.initialTokens
59
59
  };
60
- super(i, t);
60
+ super(o, t);
61
61
  }
62
62
  // ============================================================================
63
63
  // Authentication (chrome.identity OAuth)
64
64
  // ============================================================================
65
65
  async login(e) {
66
66
  const t = await this.getAuthState();
67
- if (t.status === "authenticated")
67
+ if (t.status === "authenticated" && !e?.reauthorize)
68
68
  return t;
69
- const r = e?.userRole ?? "scope_user_user", i = this._getRedirectUri();
69
+ const r = e?.userRole ?? "scope_user_user", o = this._getRedirectUri();
70
70
  e?.onProgress?.("requesting");
71
- const s = w(), o = await z(s), n = w();
71
+ const s = w(), i = await V(s), n = w();
72
72
  await this._storageSet({
73
73
  [this.storageKeys.CODE_VERIFIER]: s,
74
74
  [this.storageKeys.STATE]: n
75
75
  });
76
- const h = v(this.authEndpoints, {
76
+ const c = v(this.authEndpoints, {
77
77
  clientId: this.authClientId,
78
- redirectUri: i,
78
+ redirectUri: o,
79
79
  scope: L,
80
80
  state: n,
81
- codeChallenge: o
82
- }), c = b(i), E = new P(this.authClientId).requestedRole(r);
81
+ codeChallenge: i
82
+ }), h = b(o), E = new P(this.authClientId).requestedRole(r);
83
83
  e?.requested && E.requested(e.requested);
84
- const l = await this.requestAccess(E.build()), { review_url: u } = k(l), g = O(u, h, c);
84
+ const l = await this.requestAccess(E.build()), { review_url: u } = k(l), g = O(u, c, h);
85
85
  e?.onProgress?.("reviewing");
86
86
  const S = await this.launchReview(g);
87
87
  return e?.onProgress?.("authenticating"), this.completeOAuthRedirect(S);
88
88
  }
89
89
  launchReview(e) {
90
90
  return new Promise((t, r) => {
91
- chrome.identity.launchWebAuthFlow({ url: e, interactive: !0 }, (i) => {
91
+ chrome.identity.launchWebAuthFlow({ url: e, interactive: !0 }, (o) => {
92
92
  if (chrome.runtime.lastError) {
93
93
  r(chrome.runtime.lastError);
94
94
  return;
95
95
  }
96
- if (!i) {
96
+ if (!o) {
97
97
  r(d("oauth_error", "No redirect URL received"));
98
98
  return;
99
99
  }
100
- t(i);
100
+ t(o);
101
101
  });
102
102
  });
103
103
  }
@@ -105,12 +105,12 @@ class me extends V {
105
105
  const t = new URL(e), r = t.searchParams.get("error");
106
106
  if (r)
107
107
  throw await this._storageRemove([this.storageKeys.CODE_VERIFIER, this.storageKeys.STATE]), t.searchParams.get("error_source") === "bodhi" && M(r === "access_denied" ? "denied" : r), d("oauth_error", t.searchParams.get("error_description") ?? r);
108
- const i = t.searchParams.get("code"), s = t.searchParams.get("state"), o = await this._storageGet(this.storageKeys.STATE);
109
- if (!s || s !== o)
108
+ const o = t.searchParams.get("code"), s = t.searchParams.get("state"), i = await this._storageGet(this.storageKeys.STATE);
109
+ if (!s || s !== i)
110
110
  throw await this._storageRemove([this.storageKeys.CODE_VERIFIER, this.storageKeys.STATE]), d("oauth_error", "State mismatch");
111
- if (!i)
111
+ if (!o)
112
112
  throw await this._storageRemove([this.storageKeys.CODE_VERIFIER, this.storageKeys.STATE]), d("oauth_error", "No authorization code");
113
- await this.exchangeCodeForTokens(i);
113
+ await this.exchangeCodeForTokens(o);
114
114
  const n = await this.getAuthState();
115
115
  if (n.status !== "authenticated")
116
116
  throw await this._storageRemove([this.storageKeys.CODE_VERIFIER, this.storageKeys.STATE]), d("oauth_error", "Login failed");
@@ -195,8 +195,8 @@ class pe {
195
195
  async init(e = {}) {
196
196
  if (!e.testConnection && !e.selectedConnection) {
197
197
  this.logger.info("No testConnection or selectedConnection, returning not-initialized state");
198
- const i = G();
199
- return this.setState(i), i;
198
+ const o = G();
199
+ return this.setState(o), o;
200
200
  }
201
201
  if (this.extensionId && !e.testConnection)
202
202
  return this.logger.debug("Already initialized with extensionId, skipping discovery"), this.state;
@@ -211,20 +211,20 @@ class pe {
211
211
  ), this.extensionId = r;
212
212
  else {
213
213
  this.logger.info("Discovering bodhi-browser extension...");
214
- const o = {
214
+ const i = {
215
215
  attempts: this.config.initParams?.extension?.attempts,
216
216
  attemptWaitMs: this.config.initParams?.extension?.attemptWaitMs,
217
217
  attemptTimeout: this.config.initParams?.extension?.attemptTimeout
218
218
  }, n = await this.sendExtMessageWithTimeout(
219
219
  m.DISCOVER_EXTENSION,
220
- o,
220
+ i,
221
221
  t
222
222
  );
223
223
  this.extensionId = n.extensionId, this.logger.info("Extension discovered:", this.extensionId);
224
224
  }
225
225
  this.setupBroadcastListener();
226
226
  }
227
- const i = {
227
+ const o = {
228
228
  type: "extension",
229
229
  extension: "ready",
230
230
  extensionId: this.extensionId,
@@ -234,12 +234,12 @@ class pe {
234
234
  if (e.testConnection)
235
235
  try {
236
236
  s = await this.getServerState(), this.logger.info("Server connectivity tested, state:", s.status);
237
- } catch (o) {
238
- this.logger.error("Failed to get server state:", o), s = K;
237
+ } catch (i) {
238
+ this.logger.error("Failed to get server state:", i), s = K;
239
239
  }
240
- return this.setState({ ...i, server: s }), this.state;
241
- } catch (i) {
242
- this.logger.error("Failed to initialize extension:", i), this.extensionId = null;
240
+ return this.setState({ ...o, server: s }), this.state;
241
+ } catch (o) {
242
+ this.logger.error("Failed to initialize extension:", o), this.extensionId = null;
243
243
  const s = F();
244
244
  return this.setState(s), this.state;
245
245
  }
@@ -248,10 +248,10 @@ class pe {
248
248
  * Helper method to send ext message with timeout support
249
249
  */
250
250
  async sendExtMessageWithTimeout(e, t, r = 1e4) {
251
- const i = new Promise(
252
- (s, o) => setTimeout(() => o(new Error("Timeout")), r)
251
+ const o = new Promise(
252
+ (s, i) => setTimeout(() => i(new Error("Timeout")), r)
253
253
  );
254
- return Promise.race([this.sendExtRequest(e, t), i]);
254
+ return Promise.race([this.sendExtRequest(e, t), o]);
255
255
  }
256
256
  /**
257
257
  * Send an EXT2EXT_CLIENT_REQUEST message and await EXT2EXT_CLIENT_RESPONSE
@@ -259,7 +259,7 @@ class pe {
259
259
  */
260
260
  async sendExtRequest(e, t) {
261
261
  try {
262
- const r = this.generateRequestId(), i = await chrome.runtime.sendMessage({
262
+ const r = this.generateRequestId(), o = await chrome.runtime.sendMessage({
263
263
  type: a.EXT2EXT_CLIENT_REQUEST,
264
264
  requestId: r,
265
265
  request: {
@@ -267,17 +267,17 @@ class pe {
267
267
  params: t
268
268
  }
269
269
  });
270
- if (!i)
270
+ if (!o)
271
271
  throw d("extension_error", "No response from background script");
272
- if (i.type !== a.EXT2EXT_CLIENT_RESPONSE)
272
+ if (o.type !== a.EXT2EXT_CLIENT_RESPONSE)
273
273
  throw d(
274
274
  "extension_error",
275
275
  "Invalid response type from background script"
276
276
  );
277
- const s = i.response;
277
+ const s = o.response;
278
278
  if (y(s)) {
279
- const o = s.error.type || "extension_error";
280
- throw d(o, s.error.message);
279
+ const i = s.error.type || "extension_error";
280
+ throw d(i, s.error.message);
281
281
  }
282
282
  return s;
283
283
  } catch (r) {
@@ -291,16 +291,16 @@ class pe {
291
291
  * Send an API_REQUEST message and await API_RESPONSE (internal)
292
292
  * Returns ext2ext-specific ExtClientApiResponseMessage
293
293
  */
294
- async sendRawApiMessage(e, t, r, i, s) {
295
- const o = this.generateRequestId();
294
+ async sendRawApiMessage(e, t, r, o, s) {
295
+ const i = this.generateRequestId();
296
296
  return await chrome.runtime.sendMessage({
297
297
  type: a.EXT2EXT_CLIENT_API_REQUEST,
298
- requestId: o,
298
+ requestId: i,
299
299
  request: {
300
300
  method: e,
301
301
  endpoint: t,
302
302
  body: r,
303
- headers: i,
303
+ headers: o,
304
304
  authenticated: s
305
305
  }
306
306
  });
@@ -309,11 +309,11 @@ class pe {
309
309
  * Send an API message and return ApiResponse
310
310
  * @throws BodhiError on operational errors (network, timeout, extension-level)
311
311
  */
312
- async sendApiRequest(e, t, r, i, s) {
312
+ async sendApiRequest(e, t, r, o, s) {
313
313
  try {
314
- const o = new Promise(
315
- (h, c) => setTimeout(
316
- () => c(
314
+ const i = new Promise(
315
+ (c, h) => setTimeout(
316
+ () => h(
317
317
  new Error(
318
318
  `[bodhi-js-sdk/ext] network timeout: api request not completed within configured/default timeout of ${this.apiTimeoutMs}ms`
319
319
  )
@@ -321,18 +321,18 @@ class pe {
321
321
  this.apiTimeoutMs
322
322
  )
323
323
  ), n = await Promise.race([
324
- this.sendRawApiMessage(e, t, r, i, s),
325
- o
324
+ this.sendRawApiMessage(e, t, r, o, s),
325
+ i
326
326
  ]);
327
327
  if (ge(n)) {
328
- const h = n.error.type || "extension_error";
329
- throw new T(h, n.error.message);
328
+ const c = n.error.type || "extension_error";
329
+ throw new T(c, n.error.message);
330
330
  }
331
331
  return n.response;
332
- } catch (o) {
333
- if (o instanceof T)
334
- throw o;
335
- const n = o instanceof Error ? o.message : String(o);
332
+ } catch (i) {
333
+ if (i instanceof T)
334
+ throw i;
335
+ const n = i instanceof Error ? i.message : String(i);
336
336
  throw new T("network_error", n);
337
337
  }
338
338
  }
@@ -344,29 +344,29 @@ class pe {
344
344
  */
345
345
  async login(e) {
346
346
  return new Promise((t, r) => {
347
- const i = async (s) => {
347
+ const o = async (s) => {
348
348
  if (s && typeof s == "object" && "type" in s && s.type === "EXT2EXT_CLIENT_BROADCAST" && "event" in s && s.event === "authStateChanged") {
349
- chrome.runtime.onMessage.removeListener(i);
349
+ chrome.runtime.onMessage.removeListener(o);
350
350
  try {
351
- const o = await this.getAuthState();
352
- if (ee(o)) {
351
+ const i = await this.getAuthState();
352
+ if (ee(i)) {
353
353
  r(
354
- d("auth_error", `Login failed: ${o.error?.message}`)
354
+ d("auth_error", `Login failed: ${i.error?.message}`)
355
355
  );
356
356
  return;
357
357
  }
358
- if (o.status !== "authenticated") {
358
+ if (i.status !== "authenticated") {
359
359
  r(d("auth_error", "Login failed: User is not logged in"));
360
360
  return;
361
361
  }
362
- this.setAuthState(o), t(o);
363
- } catch (o) {
364
- r(o);
362
+ this.setAuthState(i), t(i);
363
+ } catch (i) {
364
+ r(i);
365
365
  }
366
366
  }
367
367
  };
368
- chrome.runtime.onMessage.addListener(i), this.sendExtRequest(m.LOGIN, e).catch((s) => {
369
- chrome.runtime.onMessage.removeListener(i), r(s);
368
+ chrome.runtime.onMessage.addListener(o), this.sendExtRequest(m.LOGIN, e).catch((s) => {
369
+ chrome.runtime.onMessage.removeListener(o), r(s);
370
370
  });
371
371
  });
372
372
  }
@@ -413,11 +413,11 @@ class pe {
413
413
  try {
414
414
  e = await this.sendApiRequest("GET", "/bodhi/v1/info");
415
415
  } catch (s) {
416
- const o = s instanceof Error ? s.message : "Connection failed", n = s instanceof T ? s.code : "extension_error";
416
+ const i = s instanceof Error ? s.message : "Connection failed", n = s instanceof T ? s.code : "extension_error";
417
417
  return {
418
418
  status: "not-reachable",
419
419
  version: null,
420
- error: { message: o, type: n }
420
+ error: { message: i, type: n }
421
421
  };
422
422
  }
423
423
  if (e.status >= 400)
@@ -426,30 +426,30 @@ class pe {
426
426
  version: null,
427
427
  error: { message: "API error from server", type: "extension_error" }
428
428
  };
429
- const t = e.body, r = t.version || "unknown", i = { deployment: t.deployment ?? null, client_id: t.client_id ?? null };
429
+ const t = e.body, r = t.version || "unknown", o = { deployment: t.deployment ?? null, client_id: t.client_id ?? null };
430
430
  switch (t.status) {
431
431
  case "ready":
432
- return { status: "ready", version: r, error: null, ...i };
432
+ return { status: "ready", version: r, error: null, ...o };
433
433
  case "setup":
434
434
  return {
435
435
  status: "setup",
436
436
  version: r,
437
437
  error: t.error ? { message: t.error.message, type: t.error.type } : { message: "Setup required", type: "extension_error" },
438
- ...i
438
+ ...o
439
439
  };
440
440
  case "resource_admin":
441
441
  return {
442
442
  status: "resource_admin",
443
443
  version: r,
444
444
  error: t.error ? { message: t.error.message, type: t.error.type } : { message: "Resource admin required", type: "extension_error" },
445
- ...i
445
+ ...o
446
446
  };
447
447
  case "error":
448
448
  return {
449
449
  status: "error",
450
450
  version: r,
451
451
  error: t.error ? { message: t.error.message, type: t.error.type } : { message: "Server error", type: "extension_error" },
452
- ...i
452
+ ...o
453
453
  };
454
454
  default:
455
455
  return {
@@ -465,24 +465,24 @@ class pe {
465
465
  /**
466
466
  * Generic streaming method
467
467
  */
468
- async *stream(e, t, r, i, s = !0) {
469
- const o = this.generateRequestId();
468
+ async *stream(e, t, r, o, s = !0) {
469
+ const i = this.generateRequestId();
470
470
  this.logger.debug("Starting stream", {
471
471
  method: e,
472
472
  endpoint: t,
473
- requestId: o
473
+ requestId: i
474
474
  });
475
- const n = chrome.runtime.connect({ name: U }), c = new ReadableStream({
475
+ const n = chrome.runtime.connect({ name: U }), h = new ReadableStream({
476
476
  start: (E) => {
477
477
  n.onMessage.addListener((l) => {
478
- if (l.requestId === o)
478
+ if (l.requestId === i)
479
479
  switch (l.type) {
480
480
  case a.EXT2EXT_CLIENT_STREAM_DONE:
481
- this.logger.debug("Stream complete", { requestId: o }), E.close(), n.disconnect();
481
+ this.logger.debug("Stream complete", { requestId: i }), E.close(), n.disconnect();
482
482
  break;
483
483
  case a.EXT2EXT_CLIENT_STREAM_ERROR:
484
484
  this.logger.error("Stream error", {
485
- requestId: o,
485
+ requestId: i,
486
486
  error: JSON.stringify(l.error)
487
487
  }), E.error(
488
488
  new T(
@@ -494,7 +494,7 @@ class pe {
494
494
  case a.EXT2EXT_CLIENT_STREAM_API_ERROR: {
495
495
  const u = l;
496
496
  this.logger.error("Stream API error", {
497
- requestId: o,
497
+ requestId: i,
498
498
  error: u.response.body?.error
499
499
  }), E.error(
500
500
  new W(
@@ -512,21 +512,21 @@ class pe {
512
512
  }
513
513
  }
514
514
  }), n.onDisconnect.addListener(() => {
515
- this.logger.debug("Port disconnected", { requestId: o });
515
+ this.logger.debug("Port disconnected", { requestId: i });
516
516
  try {
517
517
  E.error(new T("connection_closed", "Connection closed unexpectedly"));
518
518
  } catch {
519
519
  }
520
520
  }), n.postMessage({
521
521
  type: a.EXT2EXT_CLIENT_STREAM_REQUEST,
522
- requestId: o,
523
- request: { method: e, endpoint: t, body: r, headers: i, authenticated: s }
522
+ requestId: i,
523
+ request: { method: e, endpoint: t, body: r, headers: o, authenticated: s }
524
524
  });
525
525
  }
526
526
  }).getReader();
527
527
  try {
528
528
  for (; ; ) {
529
- const { done: E, value: l } = await c.read();
529
+ const { done: E, value: l } = await h.read();
530
530
  if (E) {
531
531
  this.logger.debug("Stream iteration complete");
532
532
  break;
@@ -534,24 +534,24 @@ class pe {
534
534
  yield l;
535
535
  }
536
536
  } finally {
537
- c.releaseLock();
537
+ h.releaseLock();
538
538
  }
539
539
  }
540
540
  /**
541
541
  * Raw text streaming method - no SSE/JSON parsing
542
542
  * Returns status, headers, and async generator of raw text chunks
543
543
  */
544
- async streamText(e, t, r, i, s = !0) {
545
- const o = this.generateRequestId();
546
- return new Promise((n, h) => {
547
- let c, E = !1;
544
+ async streamText(e, t, r, o, s = !0) {
545
+ const i = this.generateRequestId();
546
+ return new Promise((n, c) => {
547
+ let h, E = !1;
548
548
  const l = new ReadableStream({
549
549
  start: (g) => {
550
- c = g;
550
+ h = g;
551
551
  }
552
552
  }), u = chrome.runtime.connect({ name: D });
553
553
  u.onMessage.addListener((g) => {
554
- if (g.requestId === o)
554
+ if (g.requestId === i)
555
555
  switch (g.type) {
556
556
  case a.EXT2EXT_CLIENT_STREAM_TEXT_START: {
557
557
  E = !0;
@@ -575,31 +575,31 @@ class pe {
575
575
  break;
576
576
  }
577
577
  case a.EXT2EXT_CLIENT_STREAM_TEXT_CHUNK:
578
- c.enqueue(g.chunk);
578
+ h.enqueue(g.chunk);
579
579
  break;
580
580
  case a.EXT2EXT_CLIENT_STREAM_TEXT_DONE:
581
- c.close(), u.disconnect();
581
+ h.close(), u.disconnect();
582
582
  break;
583
583
  case a.EXT2EXT_CLIENT_STREAM_TEXT_ERROR: {
584
584
  const S = d("extension_error", g.error.message);
585
- E ? c.error(S) : h(S), u.disconnect();
585
+ E ? h.error(S) : c(S), u.disconnect();
586
586
  break;
587
587
  }
588
588
  }
589
589
  }), u.onDisconnect.addListener(() => {
590
590
  if (!E)
591
- h(d("extension_error", "Connection closed unexpectedly"));
591
+ c(d("extension_error", "Connection closed unexpectedly"));
592
592
  else
593
593
  try {
594
- c.error(
594
+ h.error(
595
595
  d("extension_error", "Connection closed unexpectedly")
596
596
  );
597
597
  } catch {
598
598
  }
599
599
  }), u.postMessage({
600
600
  type: a.EXT2EXT_CLIENT_STREAM_TEXT_REQUEST,
601
- requestId: o,
602
- request: { method: e, endpoint: t, body: r, headers: i, authenticated: s }
601
+ requestId: i,
602
+ request: { method: e, endpoint: t, body: r, headers: o, authenticated: s }
603
603
  });
604
604
  });
605
605
  }
@@ -649,14 +649,14 @@ class pe {
649
649
  }
650
650
  class ye extends te {
651
651
  constructor(e, t, r) {
652
- const i = t || {}, s = {
653
- basePath: i.basePath || "/",
654
- authServerUrl: i.authServerUrl || "https://id.getbodhi.app/realms/bodhi",
655
- logLevel: i.logLevel || "warn",
656
- apiTimeoutMs: i.apiTimeoutMs,
657
- storage: i.storage,
658
- initialTokens: i.initialTokens,
659
- initParams: i.initParams
652
+ const o = t || {}, s = {
653
+ basePath: o.basePath || "/",
654
+ authServerUrl: o.authServerUrl || "https://id.getbodhi.app/realms/bodhi",
655
+ logLevel: o.logLevel || "warn",
656
+ apiTimeoutMs: o.apiTimeoutMs,
657
+ storage: o.storage,
658
+ initialTokens: o.initialTokens,
659
+ initParams: o.initParams
660
660
  };
661
661
  super(e, s, r);
662
662
  }
@@ -697,7 +697,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
697
697
  // Constructor
698
698
  // ============================================================================
699
699
  constructor(e, t) {
700
- this.isAuthenticating = !1, this.state = "setup", this.listenersInitialized = !1, this.refreshPromise = null, this.activeStreamPorts = /* @__PURE__ */ new Map(), this.authClientId = e, this.authServerUrl = t?.authServerUrl || "https://id.getbodhi.app/realms/bodhi", this.extensionId = t?.extensionId, this.logger = new x("BodhiExtClient", t?.logLevel || "warn"), this.attempts = t?.attempts ?? ue, this.attemptWaitMs = t?.attemptWaitMs ?? de, this.attemptTimeout = t?.attemptTimeout ?? le, this.authEndpoints = {
700
+ this.isAuthenticating = !1, this.isReauthorizing = !1, this.state = "setup", this.listenersInitialized = !1, this.refreshPromise = null, this.activeStreamPorts = /* @__PURE__ */ new Map(), this.authClientId = e, this.authServerUrl = t?.authServerUrl || "https://id.getbodhi.app/realms/bodhi", this.extensionId = t?.extensionId, this.logger = new x("BodhiExtClient", t?.logLevel || "warn"), this.attempts = t?.attempts ?? ue, this.attemptWaitMs = t?.attemptWaitMs ?? de, this.attemptTimeout = t?.attemptTimeout ?? le, this.authEndpoints = {
701
701
  authorize: `${this.authServerUrl}/protocol/openid-connect/auth`,
702
702
  token: `${this.authServerUrl}/protocol/openid-connect/token`,
703
703
  userinfo: `${this.authServerUrl}/protocol/openid-connect/userinfo`,
@@ -718,8 +718,8 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
718
718
  return crypto.getRandomValues(e), p.base64UrlEncode(e.buffer);
719
719
  }
720
720
  static async generateCodeChallenge(e) {
721
- const r = new TextEncoder().encode(e), i = await crypto.subtle.digest("SHA-256", r);
722
- return p.base64UrlEncode(i);
721
+ const r = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", r);
722
+ return p.base64UrlEncode(o);
723
723
  }
724
724
  // ============================================================================
725
725
  // State Management
@@ -748,7 +748,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
748
748
  return this.logger.debug(
749
749
  `[Ext2Ext/Discovery] Pinging extension: ${e} with timeout ${this.attemptTimeout}ms`
750
750
  ), new Promise((t, r) => {
751
- const i = setTimeout(() => {
751
+ const o = setTimeout(() => {
752
752
  this.logger.debug(`[Ext2Ext/Discovery] Timeout waiting for extension ${e}`), r(new Error("Timeout"));
753
753
  }, this.attemptTimeout);
754
754
  try {
@@ -759,23 +759,23 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
759
759
  action: "get_extension_id"
760
760
  }
761
761
  };
762
- this.logger.debug(`[Ext2Ext/Discovery] Sending message to ${e}:`, s), chrome.runtime.sendMessage(e, s, (o) => {
763
- if (clearTimeout(i), chrome.runtime.lastError) {
762
+ this.logger.debug(`[Ext2Ext/Discovery] Sending message to ${e}:`, s), chrome.runtime.sendMessage(e, s, (i) => {
763
+ if (clearTimeout(o), chrome.runtime.lastError) {
764
764
  this.logger.error(
765
765
  `[Ext2Ext/Discovery] Error from extension ${e}:`,
766
766
  chrome.runtime.lastError.message
767
767
  ), r(new Error(chrome.runtime.lastError.message));
768
768
  return;
769
769
  }
770
- this.logger.debug(`[Ext2Ext/Discovery] Response from ${e}:`, o);
771
- const n = o;
770
+ this.logger.debug(`[Ext2Ext/Discovery] Response from ${e}:`, i);
771
+ const n = i;
772
772
  n && n.type === _.EXT_RESPONSE ? (this.logger.debug(`[Ext2Ext/Discovery] ✓ Extension ${e} responded`), t(!0)) : (this.logger.error(
773
773
  `[Ext2Ext/Discovery] Invalid response from ${e}:`,
774
- o
774
+ i
775
775
  ), r(new Error("Invalid response")));
776
776
  });
777
777
  } catch (s) {
778
- this.logger.error(`[Ext2Ext/Discovery] Exception pinging ${e}:`, s), clearTimeout(i), r(s);
778
+ this.logger.error(`[Ext2Ext/Discovery] Exception pinging ${e}:`, s), clearTimeout(o), r(s);
779
779
  }
780
780
  });
781
781
  }
@@ -790,36 +790,36 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
790
790
  * @param params Resolved discovery params
791
791
  */
792
792
  async discoverBodhiExtension(e) {
793
- const { attempts: t, attemptWaitMs: r, attemptTimeout: i } = e;
793
+ const { attempts: t, attemptWaitMs: r, attemptTimeout: o } = e;
794
794
  this.logger.info(
795
- `[Ext2Ext/Discovery] Starting discovery: ${t} attempts per ID, ${i}ms timeout, ${r}ms between attempts`
795
+ `[Ext2Ext/Discovery] Starting discovery: ${t} attempts per ID, ${o}ms timeout, ${r}ms between attempts`
796
796
  );
797
797
  const s = this.getExtensionIdsForEnvironment();
798
798
  this.logger.debug(
799
799
  `[Ext2Ext/Discovery] Will try ${s.length} extension(s):`,
800
800
  s
801
801
  );
802
- for (const h of s) {
803
- for (let c = 1; c <= t; c++) {
802
+ for (const c of s) {
803
+ for (let h = 1; h <= t; h++) {
804
804
  this.logger.debug(
805
- `[Ext2Ext/Discovery] Trying ${h} - attempt ${c}/${t}`
805
+ `[Ext2Ext/Discovery] Trying ${c} - attempt ${h}/${t}`
806
806
  );
807
807
  try {
808
- return await this.pingExtension(h), this.logger.info(`[Ext2Ext/Discovery] ✓ Found: ${h} on attempt ${c}`), {
808
+ return await this.pingExtension(c), this.logger.info(`[Ext2Ext/Discovery] ✓ Found: ${c} on attempt ${h}`), {
809
809
  success: !0,
810
- extensionId: h
810
+ extensionId: c
811
811
  };
812
812
  } catch (E) {
813
813
  this.logger.debug(
814
- `[Ext2Ext/Discovery] Attempt ${c} failed for ${h}: ${E instanceof Error ? E.message : "Unknown error"}`
815
- ), c < t && await this.sleep(r);
814
+ `[Ext2Ext/Discovery] Attempt ${h} failed for ${c}: ${E instanceof Error ? E.message : "Unknown error"}`
815
+ ), h < t && await this.sleep(r);
816
816
  }
817
817
  }
818
818
  this.logger.warn(
819
- `[Ext2Ext/Discovery] ✗ Not found: ${h} after ${t} attempts`
819
+ `[Ext2Ext/Discovery] ✗ Not found: ${c} after ${t} attempts`
820
820
  );
821
821
  }
822
- const o = s.join(", "), n = `Extension not found. Tried ${s.length} IDs with ${t} attempts each: ${o}`;
822
+ const i = s.join(", "), n = `Extension not found. Tried ${s.length} IDs with ${t} attempts each: ${i}`;
823
823
  return this.logger.error(`[Ext2Ext/Discovery] ${n}`), {
824
824
  success: !1,
825
825
  error: n
@@ -853,8 +853,8 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
853
853
  type: "NOT_INITIALIZED"
854
854
  }
855
855
  }), !0) : (this.logger.debug(`[BodhiExtClient] Processing message.type=${e.type}`), (async () => {
856
- const i = await this.handleAction(e);
857
- r(i);
856
+ const o = await this.handleAction(e);
857
+ r(o);
858
858
  })(), !0)), this.registerStreamPortListener(
859
859
  U,
860
860
  a.EXT2EXT_CLIENT_STREAM_REQUEST,
@@ -937,7 +937,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
937
937
  Authorization: `Bearer ${s}`
938
938
  }, this.logger.debug("[BodhiExtClient] Injected auth token for authenticated request");
939
939
  }
940
- const i = await this.sendApiRequest(
940
+ const o = await this.sendApiRequest(
941
941
  e.request.method,
942
942
  e.request.endpoint,
943
943
  e.request.body,
@@ -946,7 +946,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
946
946
  return {
947
947
  type: a.EXT2EXT_CLIENT_API_RESPONSE,
948
948
  requestId: t,
949
- response: i
949
+ response: o
950
950
  };
951
951
  } catch (r) {
952
952
  return this.logger.error("[BodhiExtClient] API request failed:", r), {
@@ -966,17 +966,17 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
966
966
  * @returns Action response message (success or error)
967
967
  */
968
968
  async handleExtClientRequest(e) {
969
- const { requestId: t, request: r } = e, { action: i, params: s } = r;
970
- this.logger.debug(`[BodhiExtClient] Handling action: ${i}`);
969
+ const { requestId: t, request: r } = e, { action: o, params: s } = r;
970
+ this.logger.debug(`[BodhiExtClient] Handling action: ${o}`);
971
971
  try {
972
- let o = {};
973
- switch (i) {
972
+ let i = {};
973
+ switch (o) {
974
974
  case m.DISCOVER_EXTENSION: {
975
975
  const n = s;
976
976
  await this.init(n), this.logger.info("[BodhiExtClient] Discovery successful:", {
977
977
  extensionId: this.extensionId,
978
978
  environment: I
979
- }), o = {
979
+ }), i = {
980
980
  extensionId: this.extensionId,
981
981
  environment: I
982
982
  };
@@ -984,11 +984,11 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
984
984
  }
985
985
  case m.SET_EXTENSION_ID: {
986
986
  const { extensionId: n } = s;
987
- this.extensionId = n, this.state = "ready", this.logger.info("[BodhiExtClient] Extension ID set:", { extensionId: n }), o = { success: !0 };
987
+ this.extensionId = n, this.state = "ready", this.logger.info("[BodhiExtClient] Extension ID set:", { extensionId: n }), i = { success: !0 };
988
988
  break;
989
989
  }
990
990
  case m.GET_EXTENSION_ID: {
991
- const n = await this.sendExtRequestRaw(oe.GET_EXTENSION_ID, s);
991
+ const n = await this.sendExtRequestRaw(ie.GET_EXTENSION_ID, s);
992
992
  return y(n.response) ? {
993
993
  type: a.EXT2EXT_CLIENT_RESPONSE,
994
994
  requestId: t,
@@ -1013,29 +1013,29 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1013
1013
  await this.logout(), this.broadcastAuthStateChange();
1014
1014
  break;
1015
1015
  case m.GET_AUTH_STATE:
1016
- o = { authState: await this.getAuthState() };
1016
+ i = { authState: await this.getAuthState() };
1017
1017
  break;
1018
1018
  default:
1019
1019
  return {
1020
1020
  type: a.EXT2EXT_CLIENT_RESPONSE,
1021
1021
  requestId: t,
1022
1022
  response: {
1023
- error: { message: `Unknown action: ${i}`, type: "UNKNOWN_ACTION" }
1023
+ error: { message: `Unknown action: ${o}`, type: "UNKNOWN_ACTION" }
1024
1024
  }
1025
1025
  };
1026
1026
  }
1027
1027
  return {
1028
1028
  type: a.EXT2EXT_CLIENT_RESPONSE,
1029
1029
  requestId: t,
1030
- response: o
1030
+ response: i
1031
1031
  };
1032
- } catch (o) {
1033
- return this.logger.error("[BodhiExtClient] Unexpected error:", o), {
1032
+ } catch (i) {
1033
+ return this.logger.error("[BodhiExtClient] Unexpected error:", i), {
1034
1034
  type: a.EXT2EXT_CLIENT_RESPONSE,
1035
1035
  requestId: t,
1036
1036
  response: {
1037
1037
  error: {
1038
- message: o instanceof Error ? o.message : `Unexpected error: ${JSON.stringify(o)}`
1038
+ message: i instanceof Error ? i.message : `Unexpected error: ${JSON.stringify(i)}`
1039
1039
  }
1040
1040
  }
1041
1041
  };
@@ -1072,25 +1072,25 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1072
1072
  * @throws Error if login fails
1073
1073
  */
1074
1074
  async login(e) {
1075
- if (!(this.isAuthenticating || (await this.getAuthState()).status === "authenticated")) {
1076
- this.isAuthenticating = !0;
1075
+ if (!(this.isAuthenticating || (await this.getAuthState()).status === "authenticated" && !e?.reauthorize)) {
1076
+ this.isAuthenticating = !0, this.isReauthorizing = e?.reauthorize ?? !1;
1077
1077
  try {
1078
1078
  if (!this.extensionId)
1079
1079
  throw new Error("Extension not discovered. Please detect Bodhi extension before login.");
1080
- const r = e?.userRole ?? "scope_user_user", i = chrome.identity.getRedirectURL("callback"), s = p.generateCodeVerifier(), o = await p.generateCodeChallenge(s), n = p.generateCodeVerifier();
1080
+ const r = e?.userRole ?? "scope_user_user", o = chrome.identity.getRedirectURL("callback"), s = p.generateCodeVerifier(), i = await p.generateCodeChallenge(s), n = p.generateCodeVerifier();
1081
1081
  await chrome.storage.session.set({ codeVerifier: s, state: n, authInProgress: !0 });
1082
- const h = v(this.authEndpoints, {
1082
+ const c = v(this.authEndpoints, {
1083
1083
  clientId: this.authClientId,
1084
- redirectUri: i,
1084
+ redirectUri: o,
1085
1085
  scope: L,
1086
1086
  state: n,
1087
- codeChallenge: o
1088
- }), c = b(i), E = new P(this.authClientId).requestedRole(r);
1087
+ codeChallenge: i
1088
+ }), h = b(o), E = new P(this.authClientId).requestedRole(r);
1089
1089
  e?.requested && E.requested(e.requested);
1090
- const l = await this.requestAccess(E.build()), { review_url: u } = k(l), g = O(u, h, c), S = await this.launchReview(g);
1090
+ const l = await this.requestAccess(E.build()), { review_url: u } = k(l), g = O(u, c, h), S = await this.launchReview(g);
1091
1091
  await this.completeOAuthRedirect(S);
1092
1092
  } finally {
1093
- this.isAuthenticating = !1;
1093
+ this.isAuthenticating = !1, this.isReauthorizing = !1;
1094
1094
  }
1095
1095
  }
1096
1096
  }
@@ -1099,9 +1099,9 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1099
1099
  * @param code Authorization code from OAuth callback
1100
1100
  */
1101
1101
  async exchangeCodeForTokens(e) {
1102
- if ((await this.getAuthState()).status === "authenticated")
1102
+ if ((await this.getAuthState()).status === "authenticated" && !this.isReauthorizing)
1103
1103
  return;
1104
- const { codeVerifier: r } = await chrome.storage.session.get("codeVerifier"), i = chrome.identity.getRedirectURL("callback"), s = await fetch(this.authEndpoints.token, {
1104
+ const { codeVerifier: r } = await chrome.storage.session.get("codeVerifier"), o = chrome.identity.getRedirectURL("callback"), s = await fetch(this.authEndpoints.token, {
1105
1105
  method: "POST",
1106
1106
  headers: {
1107
1107
  "Content-Type": "application/x-www-form-urlencoded"
@@ -1109,7 +1109,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1109
1109
  body: new URLSearchParams({
1110
1110
  grant_type: "authorization_code",
1111
1111
  code: e,
1112
- redirect_uri: i,
1112
+ redirect_uri: o,
1113
1113
  client_id: this.authClientId,
1114
1114
  code_verifier: r
1115
1115
  })
@@ -1118,12 +1118,12 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1118
1118
  const n = await s.text();
1119
1119
  throw new Error(`Token exchange failed: ${s.status} ${n}`);
1120
1120
  }
1121
- const o = await s.json();
1121
+ const i = await s.json();
1122
1122
  await this.storeTokens({
1123
- accessToken: o.access_token,
1124
- refreshToken: o.refresh_token,
1125
- idToken: o.id_token,
1126
- expiresIn: o.expires_in
1123
+ accessToken: i.access_token,
1124
+ refreshToken: i.refresh_token,
1125
+ idToken: i.id_token,
1126
+ expiresIn: i.expires_in
1127
1127
  });
1128
1128
  }
1129
1129
  /**
@@ -1208,16 +1208,16 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1208
1208
  }
1209
1209
  launchReview(e) {
1210
1210
  return new Promise((t, r) => {
1211
- chrome.identity.launchWebAuthFlow({ url: e, interactive: !0 }, async (i) => {
1211
+ chrome.identity.launchWebAuthFlow({ url: e, interactive: !0 }, async (o) => {
1212
1212
  if (await chrome.storage.session.set({ authInProgress: !1 }), chrome.runtime.lastError) {
1213
1213
  await chrome.storage.session.remove(["codeVerifier", "state"]), r(chrome.runtime.lastError);
1214
1214
  return;
1215
1215
  }
1216
- if (!i) {
1216
+ if (!o) {
1217
1217
  await chrome.storage.session.remove(["codeVerifier", "state"]), r(d("oauth_error", "No redirect URL received"));
1218
1218
  return;
1219
1219
  }
1220
- t(i);
1220
+ t(o);
1221
1221
  });
1222
1222
  });
1223
1223
  }
@@ -1225,12 +1225,12 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1225
1225
  const t = new URL(e), r = t.searchParams.get("error");
1226
1226
  if (r)
1227
1227
  throw await chrome.storage.session.remove(["codeVerifier", "state"]), t.searchParams.get("error_source") === "bodhi" && M(r === "access_denied" ? "denied" : r), d("oauth_error", t.searchParams.get("error_description") ?? r);
1228
- const i = t.searchParams.get("code"), s = t.searchParams.get("state"), { state: o } = await chrome.storage.session.get("state");
1229
- if (!s || s !== o)
1228
+ const o = t.searchParams.get("code"), s = t.searchParams.get("state"), { state: i } = await chrome.storage.session.get("state");
1229
+ if (!s || s !== i)
1230
1230
  throw await chrome.storage.session.remove(["codeVerifier", "state"]), d("oauth_error", "State mismatch");
1231
- if (!i)
1231
+ if (!o)
1232
1232
  throw await chrome.storage.session.remove(["codeVerifier", "state"]), d("oauth_error", "No authorization code");
1233
- if (await this.exchangeCodeForTokens(i), await chrome.storage.session.remove(["codeVerifier", "state"]), (await this.getAuthState()).status !== "authenticated")
1233
+ if (await this.exchangeCodeForTokens(o), await chrome.storage.session.remove(["codeVerifier", "state"]), (await this.getAuthState()).status !== "authenticated")
1234
1234
  throw d("oauth_error", "Login failed");
1235
1235
  }
1236
1236
  // Ext2Ext Communication (Private Methods)
@@ -1257,7 +1257,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1257
1257
  * @param headers Optional headers
1258
1258
  * @returns API response from LLM server via bodhi-browser-ext
1259
1259
  */
1260
- async sendApiRequest(e, t, r, i) {
1260
+ async sendApiRequest(e, t, r, o) {
1261
1261
  if (!this.extensionId)
1262
1262
  throw new T(
1263
1263
  "not_initialized",
@@ -1267,24 +1267,24 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1267
1267
  `[BodhiExtClient] Sending API_REQUEST: method=${e}, endpoint=${t}`,
1268
1268
  r ? { body: r } : ""
1269
1269
  );
1270
- const s = crypto.randomUUID(), o = {
1270
+ const s = crypto.randomUUID(), i = {
1271
1271
  type: _.API_REQUEST,
1272
1272
  requestId: s,
1273
1273
  request: {
1274
1274
  method: e,
1275
1275
  endpoint: t,
1276
1276
  body: r,
1277
- headers: i
1277
+ headers: o
1278
1278
  }
1279
1279
  };
1280
- return this.logger.debug(`[BodhiExtClient] Request ID: ${s}, Extension: ${this.extensionId}`), new Promise((n, h) => {
1280
+ return this.logger.debug(`[BodhiExtClient] Request ID: ${s}, Extension: ${this.extensionId}`), new Promise((n, c) => {
1281
1281
  try {
1282
- chrome.runtime.sendMessage(this.extensionId, o, (c) => {
1282
+ chrome.runtime.sendMessage(this.extensionId, i, (h) => {
1283
1283
  if (chrome.runtime.lastError) {
1284
1284
  this.logger.error(
1285
1285
  `[BodhiExtClient] Chrome runtime error for request ${s}:`,
1286
1286
  chrome.runtime.lastError
1287
- ), h(
1287
+ ), c(
1288
1288
  new T(
1289
1289
  "extension_error",
1290
1290
  chrome.runtime.lastError.message ?? "Chrome runtime error"
@@ -1292,19 +1292,19 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1292
1292
  );
1293
1293
  return;
1294
1294
  }
1295
- if (this.logger.debug(`[BodhiExtClient] Response for request ${s}:`, c), !c) {
1296
- this.logger.error(`[BodhiExtClient] No response received for request ${s}`), h(new T("extension_error", "No response from extension"));
1295
+ if (this.logger.debug(`[BodhiExtClient] Response for request ${s}:`, h), !h) {
1296
+ this.logger.error(`[BodhiExtClient] No response received for request ${s}`), c(new T("extension_error", "No response from extension"));
1297
1297
  return;
1298
1298
  }
1299
- c.type === _.API_RESPONSE && c.requestId === s ? "error" in c ? (this.logger.error(`[BodhiExtClient] API error for ${s}:`, c.error), h(
1300
- new T(c.error.type || "extension_error", c.error.message)
1301
- )) : (this.logger.debug(`[BodhiExtClient] ✓ Valid API_RESPONSE for ${s}`), n(c.response)) : (this.logger.error(
1299
+ h.type === _.API_RESPONSE && h.requestId === s ? "error" in h ? (this.logger.error(`[BodhiExtClient] API error for ${s}:`, h.error), c(
1300
+ new T(h.error.type || "extension_error", h.error.message)
1301
+ )) : (this.logger.debug(`[BodhiExtClient] ✓ Valid API_RESPONSE for ${s}`), n(h.response)) : (this.logger.error(
1302
1302
  `[BodhiExtClient] Invalid response format for ${s}:`,
1303
- c
1304
- ), h(new T("extension_error", "Invalid response format")));
1303
+ h
1304
+ ), c(new T("extension_error", "Invalid response format")));
1305
1305
  });
1306
- } catch (c) {
1307
- this.logger.error(`[BodhiExtClient] Exception sending message for ${s}:`, c), h(c);
1306
+ } catch (h) {
1307
+ this.logger.error(`[BodhiExtClient] Exception sending message for ${s}:`, h), c(h);
1308
1308
  }
1309
1309
  });
1310
1310
  }
@@ -1324,7 +1324,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1324
1324
  `[BodhiExtClient] Sending EXT_REQUEST (raw): action=${e}`,
1325
1325
  t ? { params: t } : ""
1326
1326
  );
1327
- const r = crypto.randomUUID(), i = {
1327
+ const r = crypto.randomUUID(), o = {
1328
1328
  type: _.EXT_REQUEST,
1329
1329
  requestId: r,
1330
1330
  request: {
@@ -1332,27 +1332,27 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1332
1332
  params: t
1333
1333
  }
1334
1334
  };
1335
- return this.logger.debug(`[BodhiExtClient] Request ID: ${r}, Extension: ${this.extensionId}`), new Promise((s, o) => {
1335
+ return this.logger.debug(`[BodhiExtClient] Request ID: ${r}, Extension: ${this.extensionId}`), new Promise((s, i) => {
1336
1336
  try {
1337
- chrome.runtime.sendMessage(this.extensionId, i, (n) => {
1337
+ chrome.runtime.sendMessage(this.extensionId, o, (n) => {
1338
1338
  if (chrome.runtime.lastError) {
1339
1339
  this.logger.error(
1340
1340
  `[BodhiExtClient] Chrome runtime error for request ${r}:`,
1341
1341
  chrome.runtime.lastError
1342
- ), o(new Error(chrome.runtime.lastError.message));
1342
+ ), i(new Error(chrome.runtime.lastError.message));
1343
1343
  return;
1344
1344
  }
1345
1345
  if (this.logger.debug(`[BodhiExtClient] Response for request ${r}:`, n), !n) {
1346
- this.logger.error(`[BodhiExtClient] No response received for request ${r}`), o(new Error("No response from extension"));
1346
+ this.logger.error(`[BodhiExtClient] No response received for request ${r}`), i(new Error("No response from extension"));
1347
1347
  return;
1348
1348
  }
1349
1349
  n.type === _.EXT_RESPONSE && n.requestId === r ? (this.logger.debug(`[BodhiExtClient] ✓ Valid EXT_RESPONSE for ${r}`), s(n)) : (this.logger.error(
1350
1350
  `[BodhiExtClient] Invalid response format for ${r}:`,
1351
1351
  n
1352
- ), o(new T("extension_error", "Invalid response format")));
1352
+ ), i(new T("extension_error", "Invalid response format")));
1353
1353
  });
1354
1354
  } catch (n) {
1355
- this.logger.error(`[BodhiExtClient] Exception sending message for ${r}:`, n), o(n);
1355
+ this.logger.error(`[BodhiExtClient] Exception sending message for ${r}:`, n), i(n);
1356
1356
  }
1357
1357
  });
1358
1358
  }
@@ -1360,18 +1360,18 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1360
1360
  * Register a chrome.runtime.onConnect listener for a streaming port.
1361
1361
  * Validates port name and message type, then delegates to handler.
1362
1362
  */
1363
- registerStreamPortListener(e, t, r, i) {
1363
+ registerStreamPortListener(e, t, r, o) {
1364
1364
  chrome.runtime.onConnect.addListener((s) => {
1365
- s.name === e && (this.logger.info(`[BodhiExtClient] Port connected: ${e}`), s.onMessage.addListener(async (o) => {
1366
- if (o.type !== t) {
1367
- this.logger.warn(`[BodhiExtClient] Unknown message type on ${e}:`, o.type), s.postMessage({
1365
+ s.name === e && (this.logger.info(`[BodhiExtClient] Port connected: ${e}`), s.onMessage.addListener(async (i) => {
1366
+ if (i.type !== t) {
1367
+ this.logger.warn(`[BodhiExtClient] Unknown message type on ${e}:`, i.type), s.postMessage({
1368
1368
  type: r,
1369
- requestId: o.requestId,
1369
+ requestId: i.requestId,
1370
1370
  error: { message: "Unknown message type", type: "extension_error" }
1371
1371
  });
1372
1372
  return;
1373
1373
  }
1374
- await i(s, o);
1374
+ await o(s, i);
1375
1375
  }), s.onDisconnect.addListener(() => {
1376
1376
  this.logger.info(`[BodhiExtClient] Port disconnected: ${e}`);
1377
1377
  }));
@@ -1385,48 +1385,48 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1385
1385
  * @param onBodhiMessage Called for each message from bodhi-browser-ext.
1386
1386
  * Returns true if the stream is complete (triggers cleanup).
1387
1387
  */
1388
- async handleGenericStreamRelay(e, t, r, i, s, o, n) {
1388
+ async handleGenericStreamRelay(e, t, r, o, s, i, n) {
1389
1389
  if (this.logger.debug("[BodhiExtClient] Processing stream relay:", {
1390
1390
  requestId: t,
1391
1391
  method: r.method,
1392
1392
  endpoint: r.endpoint,
1393
- bodhiPortName: i
1393
+ bodhiPortName: o
1394
1394
  }), !this.extensionId) {
1395
1395
  e.postMessage({
1396
- type: o,
1396
+ type: i,
1397
1397
  requestId: t,
1398
1398
  error: { message: "Client not initialized (no extensionId)", type: "extension_error" }
1399
1399
  });
1400
1400
  return;
1401
1401
  }
1402
1402
  try {
1403
- let h = { ...r.headers };
1403
+ let c = { ...r.headers };
1404
1404
  if (r.authenticated !== !1) {
1405
1405
  const u = await this._getAccessTokenRaw();
1406
1406
  if (!u) {
1407
1407
  e.postMessage({
1408
- type: o,
1408
+ type: i,
1409
1409
  requestId: t,
1410
1410
  error: { message: "Not authenticated. Please log in first.", type: "extension_error" }
1411
1411
  });
1412
1412
  return;
1413
1413
  }
1414
- h = { ...h, Authorization: `Bearer ${u}` }, this.logger.debug("[BodhiExtClient] Injected auth token for stream relay");
1414
+ c = { ...c, Authorization: `Bearer ${u}` }, this.logger.debug("[BodhiExtClient] Injected auth token for stream relay");
1415
1415
  }
1416
- const c = chrome.runtime.connect(this.extensionId, { name: i });
1417
- this.activeStreamPorts.set(t, c);
1416
+ const h = chrome.runtime.connect(this.extensionId, { name: o });
1417
+ this.activeStreamPorts.set(t, h);
1418
1418
  const E = setTimeout(() => {
1419
1419
  this.activeStreamPorts.has(t) && (this.logger.error(`[BodhiExtClient] Stream timeout for ${t}`), e.postMessage({
1420
- type: o,
1420
+ type: i,
1421
1421
  requestId: t,
1422
1422
  error: { message: "Stream request timed out", type: "timeout_error" }
1423
1423
  }), this.cleanupStreamPort(t));
1424
1424
  }, p.STREAM_TIMEOUT);
1425
- c.onMessage.addListener((u) => {
1425
+ h.onMessage.addListener((u) => {
1426
1426
  n(u, e, t) && (clearTimeout(E), this.cleanupStreamPort(t));
1427
- }), c.onDisconnect.addListener(() => {
1427
+ }), h.onDisconnect.addListener(() => {
1428
1428
  clearTimeout(E), this.activeStreamPorts.has(t) && (this.logger.error(`[BodhiExtClient] Bodhi port disconnected for ${t}`), e.postMessage({
1429
- type: o,
1429
+ type: i,
1430
1430
  requestId: t,
1431
1431
  error: {
1432
1432
  message: "Connection to Bodhi extension closed unexpectedly",
@@ -1441,17 +1441,17 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1441
1441
  method: r.method,
1442
1442
  endpoint: r.endpoint,
1443
1443
  body: r.body,
1444
- headers: h
1444
+ headers: c
1445
1445
  }
1446
1446
  };
1447
- this.logger.debug("[BodhiExtClient] Sending stream request to bodhi port:", l), c.postMessage(l);
1448
- } catch (h) {
1449
- const c = h;
1450
- this.logger.error("[BodhiExtClient] Stream relay error:", JSON.stringify(c.message)), e.postMessage({
1451
- type: o,
1447
+ this.logger.debug("[BodhiExtClient] Sending stream request to bodhi port:", l), h.postMessage(l);
1448
+ } catch (c) {
1449
+ const h = c;
1450
+ this.logger.error("[BodhiExtClient] Stream relay error:", JSON.stringify(h.message)), e.postMessage({
1451
+ type: i,
1452
1452
  requestId: t,
1453
1453
  error: {
1454
- message: `uncaught error: ${JSON.stringify({ error: c, message: c.message })}`,
1454
+ message: `uncaught error: ${JSON.stringify({ error: h, message: h.message })}`,
1455
1455
  type: "extension_error"
1456
1456
  }
1457
1457
  });
@@ -1474,43 +1474,43 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1474
1474
  * Handle SSE-parsed streaming request — translates STREAM_* messages to EXT2EXT_CLIENT_STREAM_*
1475
1475
  */
1476
1476
  async handleStreamRequest(e, t) {
1477
- const { requestId: r, request: i } = t;
1477
+ const { requestId: r, request: o } = t;
1478
1478
  await this.handleGenericStreamRelay(
1479
1479
  e,
1480
1480
  r,
1481
- i,
1482
- ie,
1481
+ o,
1482
+ oe,
1483
1483
  _.STREAM_REQUEST,
1484
1484
  a.EXT2EXT_CLIENT_STREAM_ERROR,
1485
- (s, o, n) => {
1485
+ (s, i, n) => {
1486
1486
  if (ne(s)) {
1487
- const h = s.response, c = h.body;
1488
- return h.status >= 400 ? (o.postMessage({
1487
+ const c = s.response, h = c.body;
1488
+ return c.status >= 400 ? (i.postMessage({
1489
1489
  type: a.EXT2EXT_CLIENT_STREAM_API_ERROR,
1490
1490
  requestId: n,
1491
- response: h
1492
- }), !1) : c?.done ? (o.postMessage({
1491
+ response: c
1492
+ }), !1) : h?.done ? (i.postMessage({
1493
1493
  type: a.EXT2EXT_CLIENT_STREAM_DONE,
1494
1494
  requestId: n
1495
- }), this.logger.info(`[BodhiExtClient] Stream complete for ${n}`), !0) : (o.postMessage({
1495
+ }), this.logger.info(`[BodhiExtClient] Stream complete for ${n}`), !0) : (i.postMessage({
1496
1496
  type: a.EXT2EXT_CLIENT_STREAM_CHUNK,
1497
1497
  requestId: n,
1498
- response: h
1498
+ response: c
1499
1499
  }), !1);
1500
1500
  } else {
1501
1501
  if (ae(s))
1502
1502
  return this.logger.error(
1503
1503
  `[BodhiExtClient] Stream API error for ${n}: ${s.response.status}`
1504
- ), o.postMessage({
1504
+ ), i.postMessage({
1505
1505
  type: a.EXT2EXT_CLIENT_STREAM_API_ERROR,
1506
1506
  requestId: n,
1507
1507
  response: s.response
1508
1508
  }), !1;
1509
- if (ce(s))
1509
+ if (he(s))
1510
1510
  return this.logger.error(
1511
1511
  `[BodhiExtClient] Stream error for ${n}:`,
1512
1512
  s.error.message
1513
- ), o.postMessage({
1513
+ ), i.postMessage({
1514
1514
  type: a.EXT2EXT_CLIENT_STREAM_ERROR,
1515
1515
  requestId: n,
1516
1516
  error: {
@@ -1527,31 +1527,31 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1527
1527
  * Handle raw text streaming request — translates STREAM_TEXT_* messages to EXT2EXT_CLIENT_STREAM_TEXT_*
1528
1528
  */
1529
1529
  async handleStreamTextRequest(e, t) {
1530
- const { requestId: r, request: i } = t;
1530
+ const { requestId: r, request: o } = t;
1531
1531
  await this.handleGenericStreamRelay(
1532
1532
  e,
1533
1533
  r,
1534
- i,
1535
- he,
1534
+ o,
1535
+ ce,
1536
1536
  _.STREAM_TEXT_REQUEST,
1537
1537
  a.EXT2EXT_CLIENT_STREAM_TEXT_ERROR,
1538
- (s, o, n) => {
1538
+ (s, i, n) => {
1539
1539
  switch (s.type) {
1540
1540
  case _.STREAM_TEXT_START:
1541
- return o.postMessage({
1541
+ return i.postMessage({
1542
1542
  type: a.EXT2EXT_CLIENT_STREAM_TEXT_START,
1543
1543
  requestId: n,
1544
1544
  status: s.status,
1545
1545
  headers: s.headers
1546
1546
  }), !1;
1547
1547
  case _.STREAM_TEXT_CHUNK:
1548
- return o.postMessage({
1548
+ return i.postMessage({
1549
1549
  type: a.EXT2EXT_CLIENT_STREAM_TEXT_CHUNK,
1550
1550
  requestId: n,
1551
1551
  chunk: s.chunk
1552
1552
  }), !1;
1553
1553
  case _.STREAM_TEXT_DONE:
1554
- return this.logger.info(`[BodhiExtClient] Stream text complete for ${n}`), o.postMessage({
1554
+ return this.logger.info(`[BodhiExtClient] Stream text complete for ${n}`), i.postMessage({
1555
1555
  type: a.EXT2EXT_CLIENT_STREAM_TEXT_DONE,
1556
1556
  requestId: n
1557
1557
  }), !0;
@@ -1559,7 +1559,7 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1559
1559
  return this.logger.error(
1560
1560
  `[BodhiExtClient] Stream text error for ${n}:`,
1561
1561
  s.error.message
1562
- ), o.postMessage({
1562
+ ), i.postMessage({
1563
1563
  type: a.EXT2EXT_CLIENT_STREAM_TEXT_ERROR,
1564
1564
  requestId: n,
1565
1565
  error: s.error
@@ -1654,10 +1654,10 @@ const Se = ["ggedphdcbekjlomjaidbajglgihbeaon"], Re = ["bjdjhiombmfbcoeojijpfckl
1654
1654
  ]);
1655
1655
  }
1656
1656
  parseJwt(e) {
1657
- const r = e.split(".")[1].replace(/-/g, "+").replace(/_/g, "/"), i = decodeURIComponent(
1657
+ const r = e.split(".")[1].replace(/-/g, "+").replace(/_/g, "/"), o = decodeURIComponent(
1658
1658
  atob(r).split("").map((s) => "%" + ("00" + s.charCodeAt(0).toString(16)).slice(-2)).join("")
1659
1659
  );
1660
- return JSON.parse(i);
1660
+ return JSON.parse(o);
1661
1661
  }
1662
1662
  createErrorClientNotInitialized(e) {
1663
1663
  return `Client not initialized. Extension discovery not triggered nor extensionId set, cannot handle request: ${JSON.stringify(e)}`;