@firstlovecenter/ai-chat 0.5.0 → 0.6.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.
package/dist/ui/index.js CHANGED
@@ -663,6 +663,7 @@ function AiChat({
663
663
  const textareaRef = useRef(null);
664
664
  const lastAnswerRef = useRef(null);
665
665
  const prevAnswersLen = useRef(0);
666
+ const autoOpenedRef = useRef(false);
666
667
  useLayoutEffect(() => {
667
668
  const el = textareaRef.current;
668
669
  if (!el) return;
@@ -677,7 +678,34 @@ function AiChat({
677
678
  const res = await fetch("/api/chat/sessions", { cache: "no-store" });
678
679
  if (!res.ok) return;
679
680
  const data = await res.json();
680
- if (!cancelled) setSessions(data.sessions ?? []);
681
+ if (cancelled) return;
682
+ const list = data.sessions ?? [];
683
+ setSessions(list);
684
+ if (!autoOpenedRef.current && list.length > 0) {
685
+ autoOpenedRef.current = true;
686
+ const mostRecentId = list[0].id;
687
+ setLoadingSession(true);
688
+ setActiveSessionId(mostRecentId);
689
+ try {
690
+ const sres = await fetch(`/api/chat/sessions/${mostRecentId}`, {
691
+ cache: "no-store"
692
+ });
693
+ if (cancelled) return;
694
+ if (!sres.ok) {
695
+ setAnswers([]);
696
+ return;
697
+ }
698
+ const sdata = await sres.json();
699
+ if (cancelled) return;
700
+ setAnswers(messagesToAnswers(sdata.messages ?? []));
701
+ } catch {
702
+ if (!cancelled) setAnswers([]);
703
+ } finally {
704
+ if (!cancelled) setLoadingSession(false);
705
+ }
706
+ } else if (!autoOpenedRef.current) {
707
+ autoOpenedRef.current = true;
708
+ }
681
709
  } catch {
682
710
  }
683
711
  }
@@ -1454,6 +1482,7 @@ function VercelChat({
1454
1482
  const textareaRef = useRef(null);
1455
1483
  const lastAnswerRef = useRef(null);
1456
1484
  const prevAnswersLen = useRef(0);
1485
+ const autoOpenedRef = useRef(false);
1457
1486
  const activeSessionIdRef = useRef(activeSessionId);
1458
1487
  const providerRef = useRef(provider);
1459
1488
  useEffect(() => {
@@ -1508,7 +1537,49 @@ function VercelChat({
1508
1537
  const res = await fetch("/api/chat/sessions", { cache: "no-store" });
1509
1538
  if (!res.ok) return;
1510
1539
  const json = await res.json();
1511
- if (!cancelled) setSessions(json.sessions ?? []);
1540
+ if (cancelled) return;
1541
+ const list = json.sessions ?? [];
1542
+ setSessions(list);
1543
+ if (!autoOpenedRef.current && list.length > 0) {
1544
+ autoOpenedRef.current = true;
1545
+ const mostRecentId = list[0].id;
1546
+ setLoadingSession(true);
1547
+ setActiveSessionId(mostRecentId);
1548
+ try {
1549
+ const sres = await fetch(`/api/chat/sessions/${mostRecentId}`, {
1550
+ cache: "no-store"
1551
+ });
1552
+ if (cancelled) return;
1553
+ if (!sres.ok) {
1554
+ setMessages([]);
1555
+ setHydratedBlocks({});
1556
+ setHydratedProse({});
1557
+ setHydratedErrors({});
1558
+ return;
1559
+ }
1560
+ const sjson = await sres.json();
1561
+ if (cancelled) return;
1562
+ const { uiMessages, blocksMap, proseMap, errorsMap } = storedToUseChat(
1563
+ sjson.messages ?? []
1564
+ );
1565
+ setMessages(uiMessages);
1566
+ setHydratedBlocks(blocksMap);
1567
+ setHydratedProse(proseMap);
1568
+ setHydratedErrors(errorsMap);
1569
+ setStartedAt({});
1570
+ } catch {
1571
+ if (!cancelled) {
1572
+ setMessages([]);
1573
+ setHydratedBlocks({});
1574
+ setHydratedProse({});
1575
+ setHydratedErrors({});
1576
+ }
1577
+ } finally {
1578
+ if (!cancelled) setLoadingSession(false);
1579
+ }
1580
+ } else if (!autoOpenedRef.current) {
1581
+ autoOpenedRef.current = true;
1582
+ }
1512
1583
  } catch {
1513
1584
  }
1514
1585
  }
@@ -1516,7 +1587,7 @@ function VercelChat({
1516
1587
  return () => {
1517
1588
  cancelled = true;
1518
1589
  };
1519
- }, []);
1590
+ }, [setMessages]);
1520
1591
  const answers = useMemo(() => {
1521
1592
  const liveBlocks = [];
1522
1593
  const liveErrors = [];