@firstlovecenter/ai-chat 0.9.1 → 0.9.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/ui/index.js CHANGED
@@ -850,7 +850,9 @@ function AiChat({
850
850
  const data = await create.json();
851
851
  sessionId = data.session.id;
852
852
  setActiveSessionId(sessionId);
853
- syncUrl(sessionId);
853
+ if (typeof window !== "undefined") {
854
+ window.history.replaceState(null, "", `${basePath}/${sessionId}`);
855
+ }
854
856
  setSessions((prev) => [
855
857
  { id: data.session.id, title: data.session.title, updatedAt: null },
856
858
  ...prev
@@ -912,6 +914,17 @@ function AiChat({
912
914
  const events = buffer.split("\n\n");
913
915
  buffer = events.pop() ?? "";
914
916
  for (const raw of events) {
917
+ const meta = parseMetaChatSessionId(raw);
918
+ if (meta != null) {
919
+ setActiveSessionId((prev) => prev ?? meta);
920
+ if (typeof window !== "undefined" && !window.location.pathname.endsWith(`/${meta}`)) {
921
+ window.history.replaceState(
922
+ null,
923
+ "",
924
+ `${basePath}/${meta}`
925
+ );
926
+ }
927
+ }
915
928
  handleEvent(raw, setAnswers);
916
929
  }
917
930
  }
@@ -1370,6 +1383,22 @@ function UserChip({ text }) {
1370
1383
  )
1371
1384
  ] });
1372
1385
  }
1386
+ function parseMetaChatSessionId(raw) {
1387
+ const lines = raw.split("\n");
1388
+ let event = "";
1389
+ let dataStr = "";
1390
+ for (const line of lines) {
1391
+ if (line.startsWith("event: ")) event = line.slice(7).trim();
1392
+ else if (line.startsWith("data: ")) dataStr += line.slice(6);
1393
+ }
1394
+ if (event !== "meta") return null;
1395
+ try {
1396
+ const parsed = JSON.parse(dataStr || "{}");
1397
+ return typeof parsed.chatSessionId === "string" && parsed.chatSessionId.length > 0 ? parsed.chatSessionId : null;
1398
+ } catch {
1399
+ return null;
1400
+ }
1401
+ }
1373
1402
  function handleEvent(raw, setAnswers) {
1374
1403
  const lines = raw.split("\n");
1375
1404
  let event = "";
@@ -1622,6 +1651,20 @@ function VercelChat({
1622
1651
  cancelled = true;
1623
1652
  };
1624
1653
  }, [initialSessionId, setMessages]);
1654
+ useEffect(() => {
1655
+ if (!Array.isArray(data)) return;
1656
+ for (const raw of data) {
1657
+ const part = asDataPart(raw);
1658
+ if (!part || part.type !== "meta") continue;
1659
+ const id = part.value.chatSessionId;
1660
+ if (!id) continue;
1661
+ setActiveSessionId((prev) => prev ?? id);
1662
+ if (typeof window !== "undefined" && !window.location.pathname.endsWith(`/${id}`)) {
1663
+ window.history.replaceState(null, "", `${basePath}/${id}`);
1664
+ }
1665
+ break;
1666
+ }
1667
+ }, [data, basePath]);
1625
1668
  const answers = useMemo(() => {
1626
1669
  const liveBlocks = [];
1627
1670
  const liveErrors = [];
@@ -1844,7 +1887,13 @@ function VercelChat({
1844
1887
  const json = await create.json();
1845
1888
  activeSessionIdRef.current = json.session.id;
1846
1889
  setActiveSessionId(json.session.id);
1847
- syncUrl(json.session.id);
1890
+ if (typeof window !== "undefined") {
1891
+ window.history.replaceState(
1892
+ null,
1893
+ "",
1894
+ `${basePath}/${json.session.id}`
1895
+ );
1896
+ }
1848
1897
  setSessions((prev) => [
1849
1898
  { id: json.session.id, title: json.session.title, updatedAt: null },
1850
1899
  ...prev