@liveblocks/core 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.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "3.13.0";
9
+ var PKG_VERSION = "3.13.1-hackathon";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -3132,6 +3132,9 @@ var ServerMsgCode = Object.freeze({
3132
3132
  COMMENT_REACTION_ADDED: 405,
3133
3133
  COMMENT_REACTION_REMOVED: 406,
3134
3134
  COMMENT_METADATA_UPDATED: 409,
3135
+ // For Agent Sessions
3136
+ AGENT_SESSIONS: 501,
3137
+ AGENT_MESSAGES: 503,
3135
3138
  // Error codes
3136
3139
  REJECT_STORAGE_OP: 299
3137
3140
  // Sent if a mutation was not allowed on the server (i.e. due to permissions, limit exceeded, etc)
@@ -8507,7 +8510,10 @@ var ClientMsgCode = Object.freeze({
8507
8510
  UPDATE_STORAGE: 201,
8508
8511
  // For Yjs support
8509
8512
  FETCH_YDOC: 300,
8510
- UPDATE_YDOC: 301
8513
+ UPDATE_YDOC: 301,
8514
+ // For Agent Sessions
8515
+ FETCH_AGENT_SESSIONS: 500,
8516
+ FETCH_AGENT_MESSAGES: 502
8511
8517
  });
8512
8518
 
8513
8519
  // src/refs/ManagedOthers.ts
@@ -8965,6 +8971,7 @@ function createRoom(options, config) {
8965
8971
  storageStatus: makeEventSource(),
8966
8972
  ydoc: makeEventSource(),
8967
8973
  comments: makeEventSource(),
8974
+ agentSessions: makeEventSource(),
8968
8975
  roomWillDestroy: makeEventSource()
8969
8976
  };
8970
8977
  async function createTextMention(mentionId, mention) {
@@ -9575,6 +9582,45 @@ function createRoom(options, config) {
9575
9582
  eventHub.comments.notify(message);
9576
9583
  break;
9577
9584
  }
9585
+ case ServerMsgCode.AGENT_SESSIONS: {
9586
+ const agentSessionsMsg = message;
9587
+ if (agentSessionsMsg.operation === "list") {
9588
+ for (const [
9589
+ requestId,
9590
+ { resolve }
9591
+ ] of pendingAgentSessionsRequests) {
9592
+ resolve({
9593
+ sessions: agentSessionsMsg.sessions,
9594
+ nextCursor: agentSessionsMsg.nextCursor
9595
+ });
9596
+ pendingAgentSessionsRequests.delete(requestId);
9597
+ break;
9598
+ }
9599
+ }
9600
+ eventHub.agentSessions.notify(agentSessionsMsg);
9601
+ break;
9602
+ }
9603
+ case ServerMsgCode.AGENT_MESSAGES: {
9604
+ const agentMessagesMsg = message;
9605
+ if (agentMessagesMsg.operation === "list") {
9606
+ for (const [
9607
+ requestId,
9608
+ { resolve }
9609
+ ] of pendingAgentMessagesRequests) {
9610
+ const parsedRequestId = JSON.parse(requestId);
9611
+ if (parsedRequestId.sessionId === agentMessagesMsg.sessionId) {
9612
+ resolve({
9613
+ messages: agentMessagesMsg.messages,
9614
+ nextCursor: agentMessagesMsg.nextCursor
9615
+ });
9616
+ pendingAgentMessagesRequests.delete(requestId);
9617
+ break;
9618
+ }
9619
+ }
9620
+ }
9621
+ eventHub.agentSessions.notify(agentMessagesMsg);
9622
+ break;
9623
+ }
9578
9624
  default:
9579
9625
  break;
9580
9626
  }
@@ -9676,6 +9722,8 @@ function createRoom(options, config) {
9676
9722
  }
9677
9723
  let _getStorage$ = null;
9678
9724
  let _resolveStoragePromise = null;
9725
+ const pendingAgentSessionsRequests = /* @__PURE__ */ new Map();
9726
+ const pendingAgentMessagesRequests = /* @__PURE__ */ new Map();
9679
9727
  function processInitialStorage(message) {
9680
9728
  const unacknowledgedOps = new Map(context.unacknowledgedOps);
9681
9729
  createOrUpdateRootFromMessage(message);
@@ -9743,6 +9791,65 @@ function createRoom(options, config) {
9743
9791
  }
9744
9792
  flushNowOrSoon();
9745
9793
  }
9794
+ async function fetchAgentSessions(options2) {
9795
+ const requestId = JSON.stringify({
9796
+ cursor: options2?.cursor,
9797
+ since: options2?.since,
9798
+ limit: options2?.limit,
9799
+ metadata: options2?.metadata
9800
+ });
9801
+ const { promise, resolve, reject } = Promise_withResolvers();
9802
+ pendingAgentSessionsRequests.set(requestId, { resolve, reject });
9803
+ const message = {
9804
+ type: ClientMsgCode.FETCH_AGENT_SESSIONS,
9805
+ cursor: options2?.cursor,
9806
+ since: options2?.since,
9807
+ limit: options2?.limit,
9808
+ metadata: options2?.metadata
9809
+ };
9810
+ context.buffer.messages.push(message);
9811
+ flushNowOrSoon();
9812
+ setTimeout(() => {
9813
+ if (pendingAgentSessionsRequests.has(requestId)) {
9814
+ pendingAgentSessionsRequests.delete(requestId);
9815
+ reject(new Error("Agent sessions fetch timeout"));
9816
+ }
9817
+ }, 3e4);
9818
+ return promise;
9819
+ }
9820
+ async function fetchAgentMessages(sessionId, options2) {
9821
+ const requestId = JSON.stringify({
9822
+ sessionId,
9823
+ cursor: options2?.cursor,
9824
+ since: options2?.since,
9825
+ limit: options2?.limit
9826
+ });
9827
+ const existingRequest = pendingAgentMessagesRequests.get(requestId);
9828
+ if (existingRequest) {
9829
+ return new Promise((resolve2, reject2) => {
9830
+ existingRequest.resolve = resolve2;
9831
+ existingRequest.reject = reject2;
9832
+ });
9833
+ }
9834
+ const { promise, resolve, reject } = Promise_withResolvers();
9835
+ pendingAgentMessagesRequests.set(requestId, { resolve, reject });
9836
+ const message = {
9837
+ type: ClientMsgCode.FETCH_AGENT_MESSAGES,
9838
+ sessionId,
9839
+ cursor: options2?.cursor,
9840
+ since: options2?.since,
9841
+ limit: options2?.limit
9842
+ };
9843
+ context.buffer.messages.push(message);
9844
+ flushNowOrSoon();
9845
+ setTimeout(() => {
9846
+ if (pendingAgentMessagesRequests.has(requestId)) {
9847
+ pendingAgentMessagesRequests.delete(requestId);
9848
+ reject(new Error("Agent messages fetch timeout"));
9849
+ }
9850
+ }, 3e4);
9851
+ return promise;
9852
+ }
9746
9853
  function undo() {
9747
9854
  if (context.activeBatch) {
9748
9855
  throw new Error("undo is not allowed during a batch");
@@ -9885,6 +9992,7 @@ function createRoom(options, config) {
9885
9992
  storageStatus: eventHub.storageStatus.observable,
9886
9993
  ydoc: eventHub.ydoc.observable,
9887
9994
  comments: eventHub.comments.observable,
9995
+ agentSessions: eventHub.agentSessions.observable,
9888
9996
  roomWillDestroy: eventHub.roomWillDestroy.observable
9889
9997
  };
9890
9998
  async function getThreadsSince(options2) {
@@ -10093,7 +10201,7 @@ function createRoom(options, config) {
10093
10201
  id: roomId,
10094
10202
  subscribe: makeClassicSubscribeFn(
10095
10203
  roomId,
10096
- events,
10204
+ eventHub,
10097
10205
  config.errorEventSource
10098
10206
  ),
10099
10207
  connect: () => managedSocket.connect(),
@@ -10128,6 +10236,8 @@ function createRoom(options, config) {
10128
10236
  resume: resumeHistory
10129
10237
  },
10130
10238
  fetchYDoc,
10239
+ fetchAgentSessions,
10240
+ fetchAgentMessages,
10131
10241
  getStorage,
10132
10242
  getStorageSnapshot,
10133
10243
  getStorageStatus,
@@ -11080,9 +11190,9 @@ function lsonToJson(value) {
11080
11190
  }
11081
11191
  return value;
11082
11192
  }
11083
- function deepLiveify(value) {
11193
+ function _deepLiveify(value) {
11084
11194
  if (Array.isArray(value)) {
11085
- return new LiveList(value.map(deepLiveify));
11195
+ return new LiveList(value.map(_deepLiveify));
11086
11196
  } else if (isPlainObject(value)) {
11087
11197
  const init = {};
11088
11198
  for (const key in value) {
@@ -11090,7 +11200,7 @@ function deepLiveify(value) {
11090
11200
  if (val === void 0) {
11091
11201
  continue;
11092
11202
  }
11093
- init[key] = deepLiveify(val);
11203
+ init[key] = _deepLiveify(val);
11094
11204
  }
11095
11205
  return new LiveObject(init);
11096
11206
  } else {
@@ -11127,7 +11237,7 @@ function patchLiveList(liveList, prev, next) {
11127
11237
  if (i > prevEnd) {
11128
11238
  if (i <= nextEnd) {
11129
11239
  while (i <= nextEnd) {
11130
- liveList.insert(deepLiveify(next[i]), i);
11240
+ liveList.insert(_deepLiveify(next[i]), i);
11131
11241
  i++;
11132
11242
  }
11133
11243
  }
@@ -11145,12 +11255,12 @@ function patchLiveList(liveList, prev, next) {
11145
11255
  if (isLiveObject(liveListNode) && isPlainObject(prevNode) && isPlainObject(nextNode)) {
11146
11256
  patchLiveObject(liveListNode, prevNode, nextNode);
11147
11257
  } else {
11148
- liveList.set(i, deepLiveify(nextNode));
11258
+ liveList.set(i, _deepLiveify(nextNode));
11149
11259
  }
11150
11260
  i++;
11151
11261
  }
11152
11262
  while (i <= nextEnd) {
11153
- liveList.insert(deepLiveify(next[i]), i);
11263
+ liveList.insert(_deepLiveify(next[i]), i);
11154
11264
  i++;
11155
11265
  }
11156
11266
  let localI = i;
@@ -11177,7 +11287,7 @@ Only serializable value can be synced with Liveblocks.`
11177
11287
  if (next === void 0) {
11178
11288
  liveObject.delete(key);
11179
11289
  } else if (value === void 0) {
11180
- liveObject.set(key, deepLiveify(next));
11290
+ liveObject.set(key, _deepLiveify(next));
11181
11291
  } else if (prev === next) {
11182
11292
  return;
11183
11293
  } else if (isLiveList(value) && Array.isArray(prev) && Array.isArray(next)) {
@@ -11185,7 +11295,7 @@ Only serializable value can be synced with Liveblocks.`
11185
11295
  } else if (isLiveObject(value) && isPlainObject(prev) && isPlainObject(next)) {
11186
11296
  patchLiveObject(value, prev, next);
11187
11297
  } else {
11188
- liveObject.set(key, deepLiveify(next));
11298
+ liveObject.set(key, _deepLiveify(next));
11189
11299
  }
11190
11300
  }
11191
11301
  function patchLiveObject(root, prev, next) {
@@ -11486,6 +11596,90 @@ function makePoller(callback, intervalMs, options) {
11486
11596
  };
11487
11597
  }
11488
11598
 
11599
+ // src/mutations.ts
11600
+ function generateOpsFromJson(nodes, mutation, actorId = 1) {
11601
+ const capturedOps = [];
11602
+ const pool = createManagedPool("mutation-temp", {
11603
+ getCurrentConnectionId: () => actorId,
11604
+ onDispatch: (ops) => {
11605
+ capturedOps.push(...ops);
11606
+ }
11607
+ });
11608
+ const root = LiveObject._fromItems(nodes, pool);
11609
+ if (isPlainObject(mutation)) {
11610
+ applyMutationToLiveObject(root, mutation);
11611
+ } else {
11612
+ throw new Error(
11613
+ "Root mutation must be an object. Use a nested key to update specific values."
11614
+ );
11615
+ }
11616
+ return capturedOps;
11617
+ }
11618
+ function applyMutationToLiveObject(target, mutation) {
11619
+ for (const key in mutation) {
11620
+ const mutationValue = mutation[key];
11621
+ if (mutationValue === void 0) {
11622
+ continue;
11623
+ }
11624
+ const existingValue = target.get(key);
11625
+ if (isLiveNode(mutationValue)) {
11626
+ target.set(key, mutationValue);
11627
+ continue;
11628
+ }
11629
+ if (isLiveObject(existingValue) && isPlainObject(mutationValue)) {
11630
+ applyMutationToLiveObject(
11631
+ existingValue,
11632
+ mutationValue
11633
+ );
11634
+ } else if (isLiveMap(existingValue) && isPlainObject(mutationValue)) {
11635
+ applyMutationToLiveMap(existingValue, mutationValue);
11636
+ } else if (isLiveList(existingValue) && Array.isArray(mutationValue)) {
11637
+ applyMutationToLiveList(existingValue, mutationValue);
11638
+ } else if (existingValue === void 0 && isPlainObject(mutationValue)) {
11639
+ const convertedValue = _deepLiveify(mutationValue);
11640
+ target.set(key, convertedValue);
11641
+ } else {
11642
+ target.set(key, mutationValue);
11643
+ }
11644
+ }
11645
+ }
11646
+ function applyMutationToLiveMap(target, mutation) {
11647
+ for (const key in mutation) {
11648
+ const mutationValue = mutation[key];
11649
+ if (mutationValue === void 0) {
11650
+ continue;
11651
+ }
11652
+ const existingValue = target.get(key);
11653
+ if (isLiveNode(mutationValue)) {
11654
+ target.set(key, mutationValue);
11655
+ continue;
11656
+ }
11657
+ if (isLiveObject(existingValue) && isPlainObject(mutationValue)) {
11658
+ applyMutationToLiveObject(
11659
+ existingValue,
11660
+ mutationValue
11661
+ );
11662
+ } else if (isLiveMap(existingValue) && isPlainObject(mutationValue)) {
11663
+ applyMutationToLiveMap(existingValue, mutationValue);
11664
+ } else if (isLiveList(existingValue) && Array.isArray(mutationValue)) {
11665
+ applyMutationToLiveList(existingValue, mutationValue);
11666
+ } else if (existingValue === void 0 && isPlainObject(mutationValue)) {
11667
+ const convertedValue = _deepLiveify(mutationValue);
11668
+ target.set(key, convertedValue);
11669
+ } else {
11670
+ const newValue = isLiveNode(mutationValue) ? mutationValue : mutationValue;
11671
+ target.set(key, newValue);
11672
+ }
11673
+ }
11674
+ }
11675
+ function applyMutationToLiveList(target, mutation) {
11676
+ target.clear();
11677
+ for (const item of mutation) {
11678
+ const liveItem = isLiveNode(item) ? item : item;
11679
+ target.push(liveItem);
11680
+ }
11681
+ }
11682
+
11489
11683
  // src/protocol/Subscriptions.ts
11490
11684
  function getSubscriptionKey(subscription, subjectId) {
11491
11685
  if (typeof subscription === "string") {
@@ -11558,6 +11752,7 @@ export {
11558
11752
  errorIf,
11559
11753
  findLastIndex,
11560
11754
  freeze,
11755
+ generateOpsFromJson,
11561
11756
  generateUrl,
11562
11757
  getMentionsFromCommentBody,
11563
11758
  getSubscriptionKey,