@liveblocks/core 2.17.0-usrnotsettings3 → 2.18.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/index.mjs 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 = "2.17.0-usrnotsettings3";
9
+ var PKG_VERSION = "2.18.0";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -1562,6 +1562,29 @@ function createApiClient({
1562
1562
  }
1563
1563
  );
1564
1564
  }
1565
+ async function executeContextualPrompt(options) {
1566
+ const result = await httpClient.post(
1567
+ url`/v2/c/rooms/${options.roomId}/ai/contextual-prompt`,
1568
+ await authManager.getAuthValue({
1569
+ requestedScope: "room:read",
1570
+ roomId: options.roomId
1571
+ }),
1572
+ {
1573
+ prompt: options.prompt,
1574
+ context: {
1575
+ beforeSelection: options.context.beforeSelection,
1576
+ selection: options.context.selection,
1577
+ afterSelection: options.context.afterSelection
1578
+ },
1579
+ previous: options.previous
1580
+ },
1581
+ { signal: options.signal }
1582
+ );
1583
+ if (!result || result.content.length === 0) {
1584
+ throw new Error("No content returned from server");
1585
+ }
1586
+ return result.content[0].text;
1587
+ }
1565
1588
  async function listTextVersions(options) {
1566
1589
  const result = await httpClient.get(
1567
1590
  url`/v2/c/rooms/${options.roomId}/versions`,
@@ -1819,7 +1842,9 @@ function createApiClient({
1819
1842
  updateUserNotificationSettings,
1820
1843
  // User threads
1821
1844
  getUserThreads_experimental,
1822
- getUserThreadsSince_experimental
1845
+ getUserThreadsSince_experimental,
1846
+ // AI
1847
+ executeContextualPrompt
1823
1848
  };
1824
1849
  }
1825
1850
  function getBearerTokenFromAuthValue(authValue) {
@@ -6345,7 +6370,7 @@ function defaultMessageFromContext(context) {
6345
6370
  }
6346
6371
 
6347
6372
  // src/room.ts
6348
- var MAX_SOCKET_MESSAGE_SIZE = 1024 * 1024 - 1024;
6373
+ var MAX_SOCKET_MESSAGE_SIZE = 1024 * 1024 - 512;
6349
6374
  function makeIdFactory(connectionId) {
6350
6375
  let count = 0;
6351
6376
  return () => `${connectionId}:${count++}`;
@@ -6614,24 +6639,88 @@ function createRoom(options, config) {
6614
6639
  async function createTextVersion() {
6615
6640
  return httpClient.createTextVersion({ roomId });
6616
6641
  }
6642
+ async function executeContextualPrompt(options2) {
6643
+ return httpClient.executeContextualPrompt({
6644
+ roomId,
6645
+ ...options2
6646
+ });
6647
+ }
6648
+ function* chunkOps(msg) {
6649
+ const { ops, ...rest } = msg;
6650
+ if (ops.length < 2) {
6651
+ throw new Error("Cannot split ops into smaller chunks");
6652
+ }
6653
+ const mid = Math.floor(ops.length / 2);
6654
+ const firstHalf = ops.slice(0, mid);
6655
+ const secondHalf = ops.slice(mid);
6656
+ for (const halfOps of [firstHalf, secondHalf]) {
6657
+ const half = { ops: halfOps, ...rest };
6658
+ const text = JSON.stringify([half]);
6659
+ if (!isTooBigForWebSocket(text)) {
6660
+ yield text;
6661
+ } else {
6662
+ yield* chunkOps(half);
6663
+ }
6664
+ }
6665
+ }
6666
+ function* chunkMessages(messages) {
6667
+ if (messages.length < 2) {
6668
+ if (messages[0].type === 201 /* UPDATE_STORAGE */) {
6669
+ yield* chunkOps(messages[0]);
6670
+ return;
6671
+ } else {
6672
+ throw new Error(
6673
+ "Cannot split into chunks smaller than the allowed message size"
6674
+ );
6675
+ }
6676
+ }
6677
+ const mid = Math.floor(messages.length / 2);
6678
+ const firstHalf = messages.slice(0, mid);
6679
+ const secondHalf = messages.slice(mid);
6680
+ for (const half of [firstHalf, secondHalf]) {
6681
+ const text = JSON.stringify(half);
6682
+ if (!isTooBigForWebSocket(text)) {
6683
+ yield text;
6684
+ } else {
6685
+ yield* chunkMessages(half);
6686
+ }
6687
+ }
6688
+ }
6689
+ function isTooBigForWebSocket(text) {
6690
+ if (text.length * 4 < MAX_SOCKET_MESSAGE_SIZE) {
6691
+ return false;
6692
+ }
6693
+ return new TextEncoder().encode(text).length >= MAX_SOCKET_MESSAGE_SIZE;
6694
+ }
6617
6695
  function sendMessages(messages) {
6618
- const serializedPayload = JSON.stringify(messages);
6619
- const nonce = context.dynamicSessionInfoSig.get()?.nonce;
6620
- if (config.unstable_fallbackToHTTP && nonce) {
6621
- const size = new TextEncoder().encode(serializedPayload).length;
6622
- if (size > MAX_SOCKET_MESSAGE_SIZE) {
6696
+ const strategy = config.largeMessageStrategy ?? "default";
6697
+ const text = JSON.stringify(messages);
6698
+ if (!isTooBigForWebSocket(text)) {
6699
+ return managedSocket.send(text);
6700
+ }
6701
+ switch (strategy) {
6702
+ case "default": {
6703
+ error2("Message is too large for websockets, not sending. Configure largeMessageStrategy option to deal with this.");
6704
+ return;
6705
+ }
6706
+ case "split": {
6707
+ warn("Message is too large for websockets, splitting into smaller chunks");
6708
+ for (const chunk2 of chunkMessages(messages)) {
6709
+ managedSocket.send(chunk2);
6710
+ }
6711
+ return;
6712
+ }
6713
+ case "experimental-fallback-to-http": {
6714
+ warn("Message is too large for websockets, so sending over HTTP instead");
6715
+ const nonce = context.dynamicSessionInfoSig.get()?.nonce ?? raise("Session is not authorized to send message over HTTP");
6623
6716
  void httpClient.sendMessages({ roomId, nonce, messages }).then((resp) => {
6624
6717
  if (!resp.ok && resp.status === 403) {
6625
6718
  managedSocket.reconnect();
6626
6719
  }
6627
6720
  });
6628
- warn(
6629
- "Message was too large for websockets and sent over HTTP instead"
6630
- );
6631
6721
  return;
6632
6722
  }
6633
6723
  }
6634
- managedSocket.send(serializedPayload);
6635
6724
  }
6636
6725
  const self = DerivedSignal.from(
6637
6726
  context.staticSessionInfoSig,
@@ -7598,6 +7687,8 @@ ${Array.from(traces).join("\n\n")}`
7598
7687
  getTextVersion,
7599
7688
  // create a version
7600
7689
  createTextVersion,
7690
+ // execute a contextual prompt
7691
+ executeContextualPrompt,
7601
7692
  // Support for the Liveblocks browser extension
7602
7693
  getSelf_forDevTools: () => selfAsTreeNode.get(),
7603
7694
  getOthers_forDevTools: () => others_forDevTools.get(),
@@ -7902,7 +7993,7 @@ function createClient(options) {
7902
7993
  enableDebugLogging: clientOptions.enableDebugLogging,
7903
7994
  baseUrl,
7904
7995
  errorEventSource: liveblocksErrorSource,
7905
- unstable_fallbackToHTTP: !!clientOptions.unstable_fallbackToHTTP,
7996
+ largeMessageStrategy: clientOptions.largeMessageStrategy ?? (clientOptions.unstable_fallbackToHTTP ? "experimental-fallback-to-http" : void 0),
7906
7997
  unstable_streamData: !!clientOptions.unstable_streamData,
7907
7998
  roomHttpClient: httpClient,
7908
7999
  createSyncSource