@liveblocks/react 3.13.0 → 3.13.1-hackathon

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.
@@ -512,6 +512,12 @@ function makeAiChatsQueryKey(query) {
512
512
  function makeInboxNotificationsQueryKey(query) {
513
513
  return stableStringify(query ?? {});
514
514
  }
515
+ function makeAgentSessionsQueryKey(roomId, options) {
516
+ return stableStringify([roomId, options ?? {}]);
517
+ }
518
+ function makeAgentMessagesQueryKey(roomId, sessionId, options) {
519
+ return stableStringify([roomId, sessionId, options ?? {}]);
520
+ }
515
521
  function usify(promise) {
516
522
  if ("status" in promise) {
517
523
  return promise;
@@ -978,6 +984,19 @@ var UmbrellaStore = class {
978
984
  #roomVersionsLastRequestedAtByRoom = /* @__PURE__ */ new Map();
979
985
  // Notification Settings
980
986
  #notificationSettings;
987
+ // Agent Sessions
988
+ #roomsByRoomId = /* @__PURE__ */ new Map();
989
+ // TODO: the need for this seems wrong, i need to explore if maybe this stuff belongs in in RoomContext and not here
990
+ #agentSessionsByRoomId = /* @__PURE__ */ new Map();
991
+ #agentMessagesBySessionId = /* @__PURE__ */ new Map();
992
+ // Signals for agent sessions and messages to trigger reactivity
993
+ // We use a version counter to track changes
994
+ #agentSessionsSignal = new MutableSignal3({
995
+ version: 0
996
+ });
997
+ #agentMessagesSignal = new MutableSignal3({
998
+ version: 0
999
+ });
981
1000
  constructor(client) {
982
1001
  this.#client = client[kInternal2].as();
983
1002
  this.optimisticUpdates = createStore_forOptimistic(this.#client);
@@ -1349,6 +1368,94 @@ var UmbrellaStore = class {
1349
1368
  return { signal, waitUntilLoaded: resource.waitUntilLoaded };
1350
1369
  }
1351
1370
  );
1371
+ const loadingAgentSessions = new DefaultMap(
1372
+ (queryKey) => {
1373
+ const [roomId, options] = JSON.parse(queryKey);
1374
+ const resource = new PaginatedResource(async (cursor) => {
1375
+ const room = this.#roomsByRoomId.get(roomId);
1376
+ if (!room) {
1377
+ throw new Error(
1378
+ `Room ${roomId} not found. Make sure you're calling useAgentSessions inside a RoomProvider.`
1379
+ );
1380
+ }
1381
+ const typedRoom = room;
1382
+ const result = await typedRoom.fetchAgentSessions({
1383
+ cursor,
1384
+ since: options?.since,
1385
+ metadata: options?.metadata
1386
+ });
1387
+ this.updateAgentSessions(roomId, result.sessions, "list");
1388
+ return result.nextCursor ?? null;
1389
+ });
1390
+ const signal = DerivedSignal.from(
1391
+ resource.signal,
1392
+ this.#agentSessionsSignal,
1393
+ (resourceResult, _signalState) => {
1394
+ if (resourceResult.isLoading || resourceResult.error) {
1395
+ return resourceResult;
1396
+ }
1397
+ const sessionsMap = this.#agentSessionsByRoomId.get(roomId);
1398
+ const sessions = sessionsMap ? Array.from(sessionsMap.values()) : [];
1399
+ const page = resourceResult.data;
1400
+ return {
1401
+ isLoading: false,
1402
+ sessions,
1403
+ hasFetchedAll: page.hasFetchedAll,
1404
+ isFetchingMore: page.isFetchingMore,
1405
+ fetchMoreError: page.fetchMoreError,
1406
+ fetchMore: page.fetchMore
1407
+ };
1408
+ },
1409
+ shallow2
1410
+ );
1411
+ return { signal, waitUntilLoaded: resource.waitUntilLoaded };
1412
+ }
1413
+ );
1414
+ const loadingAgentMessages = new DefaultMap(
1415
+ (queryKey) => {
1416
+ const [roomId, sessionId, options] = JSON.parse(queryKey);
1417
+ const resource = new PaginatedResource(async (cursor) => {
1418
+ const room = this.#roomsByRoomId.get(roomId);
1419
+ if (!room) {
1420
+ throw new Error(
1421
+ `Room ${roomId} not found. Make sure you're calling useAgentSession inside a RoomProvider.`
1422
+ );
1423
+ }
1424
+ const typedRoom = room;
1425
+ const result = await typedRoom.fetchAgentMessages(sessionId, {
1426
+ cursor,
1427
+ limit: options?.limit
1428
+ });
1429
+ this.updateAgentMessages(roomId, sessionId, result.messages, "list");
1430
+ return result.nextCursor ?? null;
1431
+ });
1432
+ const signal = DerivedSignal.from(
1433
+ resource.signal,
1434
+ this.#agentMessagesSignal,
1435
+ (resourceResult, _signalState) => {
1436
+ if (resourceResult.isLoading || resourceResult.error) {
1437
+ return resourceResult;
1438
+ }
1439
+ const messagesMap = this.#agentMessagesBySessionId.get(sessionId);
1440
+ const messages = messagesMap ? Array.from(messagesMap.values()).sort(
1441
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
1442
+ (a, b) => a.timestamp - b.timestamp
1443
+ ) : [];
1444
+ const page = resourceResult.data;
1445
+ return {
1446
+ isLoading: false,
1447
+ messages,
1448
+ hasFetchedAll: page.hasFetchedAll,
1449
+ isFetchingMore: page.isFetchingMore,
1450
+ fetchMoreError: page.fetchMoreError,
1451
+ fetchMore: page.fetchMore
1452
+ };
1453
+ },
1454
+ shallow2
1455
+ );
1456
+ return { signal, waitUntilLoaded: resource.waitUntilLoaded };
1457
+ }
1458
+ );
1352
1459
  this.outputs = {
1353
1460
  threadifications,
1354
1461
  threads,
@@ -1364,7 +1471,9 @@ var UmbrellaStore = class {
1364
1471
  aiChats,
1365
1472
  messagesByChatId,
1366
1473
  aiChatById,
1367
- urlMetadataByUrl
1474
+ urlMetadataByUrl,
1475
+ loadingAgentSessions,
1476
+ loadingAgentMessages
1368
1477
  };
1369
1478
  autobind(this);
1370
1479
  }
@@ -1585,6 +1694,61 @@ var UmbrellaStore = class {
1585
1694
  result.subscriptions.deleted
1586
1695
  );
1587
1696
  }
1697
+ /**
1698
+ * Registers a room instance for agent session fetching.
1699
+ * Called by RoomProvider when it mounts.
1700
+ */
1701
+ registerRoom(roomId, room) {
1702
+ this.#roomsByRoomId.set(roomId, room);
1703
+ }
1704
+ /**
1705
+ * Unregisters a room instance.
1706
+ * Called by RoomProvider when it unmounts.
1707
+ */
1708
+ unregisterRoom(roomId) {
1709
+ this.#roomsByRoomId.delete(roomId);
1710
+ this.#agentSessionsByRoomId.delete(roomId);
1711
+ }
1712
+ /**
1713
+ * Updates the agent sessions cache based on WebSocket events.
1714
+ */
1715
+ updateAgentSessions(roomId, sessions, operation) {
1716
+ let sessionsMap = this.#agentSessionsByRoomId.get(roomId);
1717
+ if (!sessionsMap) {
1718
+ sessionsMap = /* @__PURE__ */ new Map();
1719
+ this.#agentSessionsByRoomId.set(roomId, sessionsMap);
1720
+ }
1721
+ for (const session of sessions) {
1722
+ if (operation === "deleted") {
1723
+ sessionsMap.delete(session.sessionId);
1724
+ } else {
1725
+ sessionsMap.set(session.sessionId, session);
1726
+ }
1727
+ }
1728
+ this.#agentSessionsSignal.mutate((state) => {
1729
+ state.version++;
1730
+ });
1731
+ }
1732
+ /**
1733
+ * Updates the agent messages cache based on WebSocket events.
1734
+ */
1735
+ updateAgentMessages(_roomId, sessionId, messages, operation) {
1736
+ let messagesMap = this.#agentMessagesBySessionId.get(sessionId);
1737
+ if (!messagesMap) {
1738
+ messagesMap = /* @__PURE__ */ new Map();
1739
+ this.#agentMessagesBySessionId.set(sessionId, messagesMap);
1740
+ }
1741
+ for (const message of messages) {
1742
+ if (operation === "deleted") {
1743
+ messagesMap.delete(message.id);
1744
+ } else {
1745
+ messagesMap.set(message.id, message);
1746
+ }
1747
+ }
1748
+ this.#agentMessagesSignal.mutate((state) => {
1749
+ state.version++;
1750
+ });
1751
+ }
1588
1752
  async fetchUnreadNotificationsCount(queryKey, signal) {
1589
1753
  const query = JSON.parse(queryKey);
1590
1754
  const result = await this.#client.getUnreadInboxNotificationsCount({
@@ -3578,6 +3742,8 @@ function makeRoomContextBundle(client) {
3578
3742
  // prettier-ignore
3579
3743
  useMutation,
3580
3744
  useThreads,
3745
+ useAgentSessions,
3746
+ useAgentSession,
3581
3747
  useSearchComments,
3582
3748
  // prettier-ignore
3583
3749
  useCreateThread,
@@ -3627,6 +3793,8 @@ function makeRoomContextBundle(client) {
3627
3793
  // prettier-ignore
3628
3794
  useMutation,
3629
3795
  useThreads: useThreadsSuspense,
3796
+ useAgentSessions: useAgentSessionsSuspense,
3797
+ useAgentSession: useAgentSessionSuspense,
3630
3798
  // prettier-ignore
3631
3799
  useCreateThread,
3632
3800
  useDeleteThread,
@@ -3766,6 +3934,41 @@ function RoomProviderInner(props) {
3766
3934
  (message) => void handleCommentEvent(message)
3767
3935
  );
3768
3936
  }, [client, room]);
3937
+ useEffect6(() => {
3938
+ const { store } = getRoomExtrasForClient(client);
3939
+ function handleAgentSessionEvent(message) {
3940
+ if (message.type === ServerMsgCode.AGENT_SESSIONS) {
3941
+ const agentSessionsMsg = message;
3942
+ if (agentSessionsMsg.operation !== "list") {
3943
+ store.updateAgentSessions(
3944
+ room.id,
3945
+ agentSessionsMsg.sessions,
3946
+ agentSessionsMsg.operation
3947
+ );
3948
+ }
3949
+ } else if (message.type === ServerMsgCode.AGENT_MESSAGES) {
3950
+ const agentMessagesMsg = message;
3951
+ if (agentMessagesMsg.operation !== "list") {
3952
+ store.updateAgentMessages(
3953
+ room.id,
3954
+ agentMessagesMsg.sessionId,
3955
+ agentMessagesMsg.messages,
3956
+ agentMessagesMsg.operation
3957
+ );
3958
+ }
3959
+ }
3960
+ }
3961
+ return room.events.agentSessions.subscribe(
3962
+ (message) => void handleAgentSessionEvent(message)
3963
+ );
3964
+ }, [client, room]);
3965
+ useEffect6(() => {
3966
+ const { store } = getRoomExtrasForClient(client);
3967
+ store.registerRoom(room.id, room);
3968
+ return () => {
3969
+ store.unregisterRoom(room.id);
3970
+ };
3971
+ }, [client, room]);
3769
3972
  useEffect6(() => {
3770
3973
  const pair = stableEnterRoom(roomId, frozenProps);
3771
3974
  setRoomLeavePair(pair);
@@ -4091,6 +4294,53 @@ function useThreads(options = {}) {
4091
4294
  useScrollToCommentOnLoadEffect(scrollOnLoad, result);
4092
4295
  return result;
4093
4296
  }
4297
+ function useAgentSessions(options) {
4298
+ const room = useRoom();
4299
+ const client = useClient();
4300
+ const { store } = getRoomExtrasForClient(client);
4301
+ const queryKey = makeAgentSessionsQueryKey(room.id, options);
4302
+ const loadableResource = store.outputs.loadingAgentSessions.getOrCreate(queryKey);
4303
+ useEffect6(() => {
4304
+ void loadableResource.waitUntilLoaded();
4305
+ });
4306
+ return useSignal(loadableResource.signal);
4307
+ }
4308
+ function useAgentSession(sessionId, options) {
4309
+ const room = useRoom();
4310
+ const client = useClient();
4311
+ const { store } = getRoomExtrasForClient(client);
4312
+ const queryKey = makeAgentMessagesQueryKey(room.id, sessionId, options);
4313
+ useEffect6(() => {
4314
+ void store.outputs.loadingAgentMessages.getOrCreate(queryKey).waitUntilLoaded();
4315
+ });
4316
+ return useSignal(
4317
+ store.outputs.loadingAgentMessages.getOrCreate(queryKey).signal
4318
+ );
4319
+ }
4320
+ function useAgentSessionsSuspense(options) {
4321
+ ensureNotServerSide();
4322
+ const client = useClient();
4323
+ const room = useRoom();
4324
+ const { store } = getRoomExtrasForClient(client);
4325
+ const queryKey = makeAgentSessionsQueryKey(room.id, options);
4326
+ use(store.outputs.loadingAgentSessions.getOrCreate(queryKey).waitUntilLoaded());
4327
+ const result = useAgentSessions(options);
4328
+ assert2(!result.error, "Did not expect error");
4329
+ assert2(!result.isLoading, "Did not expect loading");
4330
+ return result;
4331
+ }
4332
+ function useAgentSessionSuspense(sessionId, options) {
4333
+ ensureNotServerSide();
4334
+ const client = useClient();
4335
+ const room = useRoom();
4336
+ const { store } = getRoomExtrasForClient(client);
4337
+ const queryKey = makeAgentMessagesQueryKey(room.id, sessionId, options);
4338
+ use(store.outputs.loadingAgentMessages.getOrCreate(queryKey).waitUntilLoaded());
4339
+ const result = useAgentSession(sessionId, options);
4340
+ assert2(!result.error, "Did not expect error");
4341
+ assert2(!result.isLoading, "Did not expect loading");
4342
+ return result;
4343
+ }
4094
4344
  function useSearchComments(options) {
4095
4345
  const [result, setResult] = useState3({
4096
4346
  isLoading: true
@@ -5096,6 +5346,8 @@ var _useMyPresence = useMyPresence;
5096
5346
  var _useOthersMapped = useOthersMapped;
5097
5347
  var _useOthersMappedSuspense = useOthersMappedSuspense;
5098
5348
  var _useThreads = useThreads;
5349
+ var _useAgentSessions = useAgentSessions;
5350
+ var _useAgentSession = useAgentSession;
5099
5351
  var _useSearchComments = useSearchComments;
5100
5352
  var _useThreadsSuspense = useThreadsSuspense;
5101
5353
  var _useRoomSubscriptionSettings = useRoomSubscriptionSettings;
@@ -5228,6 +5480,8 @@ export {
5228
5480
  _useOthersMapped,
5229
5481
  _useOthersMappedSuspense,
5230
5482
  _useThreads,
5483
+ _useAgentSessions,
5484
+ _useAgentSession,
5231
5485
  _useSearchComments,
5232
5486
  _useThreadsSuspense,
5233
5487
  _useRoomSubscriptionSettings,
@@ -5245,4 +5499,4 @@ export {
5245
5499
  _useStorageRoot,
5246
5500
  _useUpdateMyPresence
5247
5501
  };
5248
- //# sourceMappingURL=chunk-ERRJHA6O.js.map
5502
+ //# sourceMappingURL=chunk-VSUKKNOK.js.map