@informedai/react 0.4.0 → 0.4.2

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
@@ -267,7 +267,7 @@ function InformedAIProvider({ config, children }) {
267
267
  const initRef = (0, import_react.useRef)(false);
268
268
  const heartbeatIntervalRef = (0, import_react.useRef)(null);
269
269
  const sessionIdRef = (0, import_react.useRef)(null);
270
- const shouldPersist = config.persistSession ?? !!config.externalId;
270
+ const shouldPersist = config.persistSession ?? true;
271
271
  const storageKey = getStorageKey(config.documentTypeId, config.externalId);
272
272
  (0, import_react.useEffect)(() => {
273
273
  clientRef.current = new InformedAIClient(config.apiUrl);
@@ -443,18 +443,40 @@ function InformedAIProvider({ config, children }) {
443
443
  }, [session?.id, session?.status, startHeartbeat, stopHeartbeat]);
444
444
  (0, import_react.useEffect)(() => {
445
445
  if (typeof window === "undefined") return;
446
- const handleVisibilityChange = () => {
446
+ const handleVisibilityChange = async () => {
447
447
  if (window.document.hidden) {
448
448
  stopHeartbeat();
449
- } else if (session?.id && session.status === "active") {
450
- startHeartbeat(session.id);
449
+ } else if (session?.id && clientRef.current) {
450
+ try {
451
+ const refreshedSession = await clientRef.current.getSession(session.id);
452
+ if (refreshedSession.status === "abandoned") {
453
+ console.log("[InformedAI] Session was abandoned, resuming...");
454
+ try {
455
+ const resumedSession = await clientRef.current.resumeSession(session.id);
456
+ setSession(resumedSession);
457
+ config.onSessionChange?.(resumedSession);
458
+ startHeartbeat(session.id);
459
+ } catch (resumeErr) {
460
+ console.error("[InformedAI] Failed to resume session:", resumeErr);
461
+ await handleSessionDeleted();
462
+ }
463
+ } else if (refreshedSession.status === "active") {
464
+ setSession(refreshedSession);
465
+ startHeartbeat(session.id);
466
+ } else {
467
+ setSession(refreshedSession);
468
+ }
469
+ } catch (err) {
470
+ console.warn("[InformedAI] Session not found on return, creating new...");
471
+ await handleSessionDeleted();
472
+ }
451
473
  }
452
474
  };
453
475
  window.document.addEventListener("visibilitychange", handleVisibilityChange);
454
476
  return () => {
455
477
  window.document.removeEventListener("visibilitychange", handleVisibilityChange);
456
478
  };
457
- }, [session?.id, session?.status, startHeartbeat, stopHeartbeat]);
479
+ }, [session?.id, startHeartbeat, stopHeartbeat, handleSessionDeleted, config]);
458
480
  (0, import_react.useEffect)(() => {
459
481
  const handleBeforeUnload = () => {
460
482
  if (clientRef.current && sessionIdRef.current) {
package/dist/index.mjs CHANGED
@@ -237,7 +237,7 @@ function InformedAIProvider({ config, children }) {
237
237
  const initRef = useRef(false);
238
238
  const heartbeatIntervalRef = useRef(null);
239
239
  const sessionIdRef = useRef(null);
240
- const shouldPersist = config.persistSession ?? !!config.externalId;
240
+ const shouldPersist = config.persistSession ?? true;
241
241
  const storageKey = getStorageKey(config.documentTypeId, config.externalId);
242
242
  useEffect(() => {
243
243
  clientRef.current = new InformedAIClient(config.apiUrl);
@@ -413,18 +413,40 @@ function InformedAIProvider({ config, children }) {
413
413
  }, [session?.id, session?.status, startHeartbeat, stopHeartbeat]);
414
414
  useEffect(() => {
415
415
  if (typeof window === "undefined") return;
416
- const handleVisibilityChange = () => {
416
+ const handleVisibilityChange = async () => {
417
417
  if (window.document.hidden) {
418
418
  stopHeartbeat();
419
- } else if (session?.id && session.status === "active") {
420
- startHeartbeat(session.id);
419
+ } else if (session?.id && clientRef.current) {
420
+ try {
421
+ const refreshedSession = await clientRef.current.getSession(session.id);
422
+ if (refreshedSession.status === "abandoned") {
423
+ console.log("[InformedAI] Session was abandoned, resuming...");
424
+ try {
425
+ const resumedSession = await clientRef.current.resumeSession(session.id);
426
+ setSession(resumedSession);
427
+ config.onSessionChange?.(resumedSession);
428
+ startHeartbeat(session.id);
429
+ } catch (resumeErr) {
430
+ console.error("[InformedAI] Failed to resume session:", resumeErr);
431
+ await handleSessionDeleted();
432
+ }
433
+ } else if (refreshedSession.status === "active") {
434
+ setSession(refreshedSession);
435
+ startHeartbeat(session.id);
436
+ } else {
437
+ setSession(refreshedSession);
438
+ }
439
+ } catch (err) {
440
+ console.warn("[InformedAI] Session not found on return, creating new...");
441
+ await handleSessionDeleted();
442
+ }
421
443
  }
422
444
  };
423
445
  window.document.addEventListener("visibilitychange", handleVisibilityChange);
424
446
  return () => {
425
447
  window.document.removeEventListener("visibilitychange", handleVisibilityChange);
426
448
  };
427
- }, [session?.id, session?.status, startHeartbeat, stopHeartbeat]);
449
+ }, [session?.id, startHeartbeat, stopHeartbeat, handleSessionDeleted, config]);
428
450
  useEffect(() => {
429
451
  const handleBeforeUnload = () => {
430
452
  if (clientRef.current && sessionIdRef.current) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@informedai/react",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "React SDK for InformedAI Assistant - AI-powered content creation widget",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",