@athenaintel/react 0.10.35 → 0.10.36

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/index.js CHANGED
@@ -56215,6 +56215,13 @@ const AthenaUserMessage = ({
56215
56215
  const ATHENA_ASSET_EMBED_READY_MESSAGE = "athena:asset-embed-ready";
56216
56216
  const ATHENA_ASSET_EMBED_CONFIG_MESSAGE = "athena:asset-embed-config";
56217
56217
  const ATHENA_ASSET_CITATION_CLICK_MESSAGE = "athena:asset-citation-click";
56218
+ function resolveAthenaAssetCitationBehavior({
56219
+ citationBehavior,
56220
+ hasCitationClickHandler
56221
+ }) {
56222
+ if (citationBehavior === "host" && !hasCitationClickHandler) return "browser";
56223
+ return citationBehavior ?? (hasCitationClickHandler ? "host" : "browser");
56224
+ }
56218
56225
  function isAthenaAssetEmbedReadyEvent(value) {
56219
56226
  return typeof value === "object" && value !== null && "type" in value && value.type === ATHENA_ASSET_EMBED_READY_MESSAGE && "version" in value && value.version === 1;
56220
56227
  }
@@ -56317,7 +56324,11 @@ function resolveAssetEmbedUrl({
56317
56324
  if (!resolvedEmbedUrl.pathname.startsWith("/embed/")) {
56318
56325
  return embedUrl;
56319
56326
  }
56320
- const embedAppUrl = resolveEmbedAppUrl({ appUrl, backendUrl, currentOrigin });
56327
+ const embedAppUrl = resolveEmbedAppUrl({
56328
+ appUrl,
56329
+ backendUrl,
56330
+ currentOrigin
56331
+ });
56321
56332
  if (!embedAppUrl) {
56322
56333
  return embedUrl;
56323
56334
  }
@@ -56329,6 +56340,41 @@ function resolveAssetEmbedUrl({
56329
56340
  return embedUrl;
56330
56341
  }
56331
56342
  }
56343
+ function buildSdkAssetEmbedUrl(embedUrl) {
56344
+ try {
56345
+ const url = new URL(embedUrl);
56346
+ url.searchParams.set("athena_sdk_bridge", "1");
56347
+ return url.toString();
56348
+ } catch {
56349
+ return embedUrl;
56350
+ }
56351
+ }
56352
+ const getJwtPayload = (token) => {
56353
+ try {
56354
+ const payloadSegment = token.split(".")[1];
56355
+ if (!payloadSegment || typeof atob !== "function") return null;
56356
+ const normalized = payloadSegment.replace(/-/g, "+").replace(/_/g, "/");
56357
+ const payload = JSON.parse(
56358
+ atob(normalized.padEnd(normalized.length + (4 - normalized.length % 4) % 4, "="))
56359
+ );
56360
+ return payload && typeof payload === "object" ? payload : null;
56361
+ } catch {
56362
+ return null;
56363
+ }
56364
+ };
56365
+ function getAssetEmbedAuthContext({ token, apiKey }) {
56366
+ if (!token) return apiKey ? `api-key:${apiKey}` : "anonymous";
56367
+ const payload = getJwtPayload(token);
56368
+ const userId = (payload == null ? void 0 : payload.user_id) ?? (payload == null ? void 0 : payload.sub);
56369
+ if (typeof userId !== "string" || !userId) return `opaque-token:${token}`;
56370
+ return `jwt:${JSON.stringify({
56371
+ issuer: (payload == null ? void 0 : payload.iss) ?? null,
56372
+ userId,
56373
+ activeOrgId: (payload == null ? void 0 : payload.active_org_id) ?? (payload == null ? void 0 : payload.org_id) ?? null,
56374
+ workspaceId: (payload == null ? void 0 : payload.workspace_id) ?? null,
56375
+ orgMemberships: (payload == null ? void 0 : payload.org_id_to_org_member_info) ?? null
56376
+ })}`;
56377
+ }
56332
56378
  function useAssetEmbed(assetId, options = {
56333
56379
  backendUrl: ""
56334
56380
  }) {
@@ -56347,7 +56393,7 @@ function useAssetEmbed(assetId, options = {
56347
56393
  const abortRef = useRef(null);
56348
56394
  const tokenRef = useRef(token);
56349
56395
  tokenRef.current = token;
56350
- const hasToken = !!token;
56396
+ const authContext = getAssetEmbedAuthContext({ token, apiKey });
56351
56397
  useEffect(() => {
56352
56398
  var _a2;
56353
56399
  if (!assetId || !backendUrl) {
@@ -56356,7 +56402,6 @@ function useAssetEmbed(assetId, options = {
56356
56402
  setError(null);
56357
56403
  return;
56358
56404
  }
56359
- const authContext = hasToken ? "token" : apiKey ?? "anon";
56360
56405
  const cacheKey = `${assetId}:${readOnly}:${displayMode}:${authContext}:${backendUrl}:${appUrl ?? ""}`;
56361
56406
  const cached2 = embedCache.get(cacheKey);
56362
56407
  if (cached2 && cached2.expiresAt > Date.now() / 1e3) {
@@ -56372,10 +56417,12 @@ function useAssetEmbed(assetId, options = {
56372
56417
  abortRef.current = controller;
56373
56418
  setIsLoading(true);
56374
56419
  setError(null);
56375
- const headers = { "Content-Type": "application/json" };
56420
+ const headers = {
56421
+ "Content-Type": "application/json"
56422
+ };
56376
56423
  const currentToken = tokenRef.current;
56377
56424
  if (currentToken) {
56378
- headers["Authorization"] = `Bearer ${currentToken}`;
56425
+ headers.Authorization = `Bearer ${currentToken}`;
56379
56426
  } else if (apiKey) {
56380
56427
  headers["X-API-KEY"] = apiKey;
56381
56428
  }
@@ -56383,7 +56430,12 @@ function useAssetEmbed(assetId, options = {
56383
56430
  method: "POST",
56384
56431
  headers,
56385
56432
  body: JSON.stringify(
56386
- buildAssetEmbedRequestBody({ assetId, readOnly, expiresInSeconds, displayMode })
56433
+ buildAssetEmbedRequestBody({
56434
+ assetId,
56435
+ readOnly,
56436
+ expiresInSeconds,
56437
+ displayMode
56438
+ })
56387
56439
  ),
56388
56440
  signal: controller.signal
56389
56441
  }).then(async (res) => {
@@ -56398,7 +56450,10 @@ function useAssetEmbed(assetId, options = {
56398
56450
  appUrl,
56399
56451
  backendUrl
56400
56452
  });
56401
- embedCache.set(cacheKey, { url: resolvedEmbedUrl, expiresAt: data.expires_at });
56453
+ embedCache.set(cacheKey, {
56454
+ url: resolvedEmbedUrl,
56455
+ expiresAt: data.expires_at
56456
+ });
56402
56457
  setEmbedUrl(resolvedEmbedUrl);
56403
56458
  setIsLoading(false);
56404
56459
  }).catch((err) => {
@@ -56407,7 +56462,7 @@ function useAssetEmbed(assetId, options = {
56407
56462
  setIsLoading(false);
56408
56463
  });
56409
56464
  return () => controller.abort();
56410
- }, [assetId, readOnly, expiresInSeconds, displayMode, backendUrl, appUrl, apiKey, hasToken]);
56465
+ }, [assetId, readOnly, expiresInSeconds, displayMode, backendUrl, appUrl, authContext, apiKey]);
56411
56466
  return { embedUrl, isLoading, error: error2 };
56412
56467
  }
56413
56468
  const DEFAULT_STYLE = {
@@ -56453,7 +56508,11 @@ const AthenaAssetEmbed = ({
56453
56508
  return null;
56454
56509
  }
56455
56510
  }, [embedUrl]);
56456
- const effectiveCitationBehavior = citationBehavior ?? (onCitationClick ? "host" : "browser");
56511
+ const iframeSrc = useMemo(() => embedUrl ? buildSdkAssetEmbedUrl(embedUrl) : null, [embedUrl]);
56512
+ const effectiveCitationBehavior = resolveAthenaAssetCitationBehavior({
56513
+ citationBehavior,
56514
+ hasCitationClickHandler: !!onCitationClick
56515
+ });
56457
56516
  const sendConfig = useCallback(() => {
56458
56517
  var _a2, _b;
56459
56518
  if (!targetOrigin) return;
@@ -56471,8 +56530,7 @@ const AthenaAssetEmbed = ({
56471
56530
  if (!embedUrl || !targetOrigin) return;
56472
56531
  const handleMessage = (event) => {
56473
56532
  var _a2;
56474
- if (event.origin !== targetOrigin || event.source !== ((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow))
56475
- return;
56533
+ if (event.origin !== targetOrigin || event.source !== ((_a2 = iframeRef.current) == null ? void 0 : _a2.contentWindow)) return;
56476
56534
  if (isAthenaAssetEmbedReadyEvent(event.data)) {
56477
56535
  sendConfig();
56478
56536
  return;
@@ -56520,7 +56578,7 @@ const AthenaAssetEmbed = ({
56520
56578
  {
56521
56579
  ...iframeProps,
56522
56580
  ref: iframeRef,
56523
- src: embedUrl,
56581
+ src: iframeSrc ?? void 0,
56524
56582
  title: title ?? `Athena asset ${assetId}`,
56525
56583
  allow,
56526
56584
  className: cn("block h-full w-full", className),