@ai-matrx/messaging 0.2.0 → 0.4.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/react.cjs CHANGED
@@ -316,21 +316,32 @@ function splitText(content) {
316
316
  if (start > cursor) {
317
317
  segments.push({ type: "text", value: content.slice(cursor, start) });
318
318
  }
319
- parseFenceBody(match[1] ?? "").forEach((reference) => {
320
- segments.push({ type: "reference", reference });
321
- });
319
+ const body = match[1] ?? "";
320
+ const references = parseFenceBody(body);
321
+ if (references.length === 0) {
322
+ segments.push({ type: "fence", body });
323
+ } else {
324
+ references.forEach((reference) => {
325
+ segments.push({ type: "reference", reference });
326
+ });
327
+ }
322
328
  cursor = start + match[0].length;
323
329
  }
324
330
  if (cursor < content.length) {
325
331
  segments.push({ type: "text", value: content.slice(cursor) });
326
332
  }
327
333
  return segments.filter(
328
- (segment) => segment.type === "reference" || segment.value.trim().length > 0
334
+ (segment) => segment.type !== "text" || segment.value.trim().length > 0
329
335
  );
330
336
  }
331
337
  function summarizeText(content, maxLength = 140) {
332
338
  const parts = splitText(content).map(
333
- (segment) => segment.type === "text" ? segment.value : segment.reference.label
339
+ (segment) => segment.type === "text" ? segment.value : segment.type === "reference" ? segment.reference.label : (
340
+ // A fence we cannot read still SAYS something. Collapsing it to
341
+ // nothing is how a reference-only message becomes an inbox row that
342
+ // reads "No messages yet" — a screen telling a lie.
343
+ "Reference"
344
+ )
334
345
  );
335
346
  const flattened = parts.join(" ").replace(/\s+/g, " ").trim();
336
347
  if (flattened.length <= maxLength) return flattened;
@@ -1831,7 +1842,8 @@ function MessagingRuntime(props) {
1831
1842
  identity,
1832
1843
  ...props.resolveSession !== void 0 ? { resolveSession: props.resolveSession } : {}
1833
1844
  });
1834
- return createMessagingEngine({
1845
+ let built = null;
1846
+ built = createMessagingEngine({
1835
1847
  repository,
1836
1848
  manager,
1837
1849
  identity,
@@ -1840,8 +1852,18 @@ function MessagingRuntime(props) {
1840
1852
  onFallback: (message) => report({ level: "warn", message })
1841
1853
  }),
1842
1854
  onDiagnostic: report,
1843
- onIncoming: (message) => incomingRef.current?.(message)
1855
+ onIncoming: (message) => {
1856
+ const snapshot = built?.store.snapshot();
1857
+ if (snapshot === void 0) return;
1858
+ incomingRef.current?.(message, {
1859
+ isActiveConversation: snapshot.activeConversationId === message.conversationId,
1860
+ conversation: snapshot.conversations.find(
1861
+ (item) => item.conversation.id === message.conversationId
1862
+ ) ?? null
1863
+ });
1864
+ }
1844
1865
  });
1866
+ return built;
1845
1867
  }, [ready, manager, userId, organizationId]);
1846
1868
  (0, import_react.useEffect)(() => {
1847
1869
  if (engine === null) return void 0;
@@ -1874,6 +1896,10 @@ function MessagingRuntime(props) {
1874
1896
  });
1875
1897
  return map;
1876
1898
  }, [renderers]);
1899
+ const renderReference = props.renderReference ?? null;
1900
+ const renderFence = props.renderFence ?? null;
1901
+ const wrapMessage = props.wrapMessage ?? null;
1902
+ const wrapConversationRow = props.wrapConversationRow ?? null;
1877
1903
  const host = (0, import_react.useMemo)(() => {
1878
1904
  if (engine === null) return null;
1879
1905
  return {
@@ -1882,9 +1908,22 @@ function MessagingRuntime(props) {
1882
1908
  ai,
1883
1909
  identity: engine.identity,
1884
1910
  openReference: referenceRef.current ?? null,
1885
- actionRenderers: rendererMap
1911
+ actionRenderers: rendererMap,
1912
+ renderReference,
1913
+ renderFence,
1914
+ wrapMessage,
1915
+ wrapConversationRow
1886
1916
  };
1887
- }, [engine, actionRegistry, ai, rendererMap]);
1917
+ }, [
1918
+ engine,
1919
+ actionRegistry,
1920
+ ai,
1921
+ rendererMap,
1922
+ renderReference,
1923
+ renderFence,
1924
+ wrapMessage,
1925
+ wrapConversationRow
1926
+ ]);
1888
1927
  const MessagingContext = messagingContext();
1889
1928
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MessagingContext.Provider, { value: host, children });
1890
1929
  }
@@ -2475,6 +2514,20 @@ function DeliveryTick(props) {
2475
2514
  return null;
2476
2515
  }
2477
2516
  }
2517
+ function UnreadableReference() {
2518
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
2519
+ "span",
2520
+ {
2521
+ className: "mx-msg__reference mx-msg__reference--inert",
2522
+ title: "This message names something in a reference format this app has not wired. Pass renderFence to <MessagingProvider> to render it.",
2523
+ children: [
2524
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(LinkIcon, {}),
2525
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mx-msg__reference-type", children: "reference" }),
2526
+ "Reference"
2527
+ ]
2528
+ }
2529
+ );
2530
+ }
2478
2531
  function ReferenceCard(props) {
2479
2532
  const { reference, onOpen } = props;
2480
2533
  const href = reference.href;
@@ -2555,6 +2608,7 @@ var AI_LABELS = {
2555
2608
  };
2556
2609
  function ConversationList(props) {
2557
2610
  const { conversations, hasMore, isInitialLoading, loadMore, select, activeConversationId } = useConversations();
2611
+ const RowChrome = useMessagingHost()?.wrapConversationRow ?? null;
2558
2612
  const [query, setQuery] = (0, import_react5.useState)("");
2559
2613
  const visible = (0, import_react5.useMemo)(() => {
2560
2614
  const needle = query.trim().toLowerCase();
@@ -2595,17 +2649,20 @@ function ConversationList(props) {
2595
2649
  title: query.length > 0 ? "No matches" : "No conversations yet",
2596
2650
  body: query.length > 0 ? "Try a different name or word." : "Start one and it will appear here."
2597
2651
  }
2598
- ) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { className: "mx-msg__rows", children: visible.map((item) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2599
- ConversationRow,
2600
- {
2601
- summary: item,
2602
- isActive: item.conversation.id === activeConversationId,
2603
- onSelect: () => {
2604
- select(item.conversation.id);
2605
- props.onSelect?.(item.conversation.id);
2652
+ ) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("ul", { className: "mx-msg__rows", children: visible.map((item) => {
2653
+ const row = /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2654
+ ConversationRow,
2655
+ {
2656
+ summary: item,
2657
+ isActive: item.conversation.id === activeConversationId,
2658
+ onSelect: () => {
2659
+ select(item.conversation.id);
2660
+ props.onSelect?.(item.conversation.id);
2661
+ }
2606
2662
  }
2607
- }
2608
- ) }, item.conversation.id)) }),
2663
+ );
2664
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("li", { children: RowChrome !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(RowChrome, { conversation: item, children: row }) : row }, item.conversation.id);
2665
+ }) }),
2609
2666
  hasMore && !isInitialLoading ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2610
2667
  "button",
2611
2668
  {
@@ -2834,6 +2891,9 @@ function MessageGroupView(props) {
2834
2891
  function MessageBubble(props) {
2835
2892
  const { message, isMine } = props;
2836
2893
  const host = useMessagingHost();
2894
+ const HostReference = host?.renderReference ?? null;
2895
+ const HostFence = host?.renderFence ?? null;
2896
+ const MessageChrome = host?.wrapMessage ?? null;
2837
2897
  if (message.deletedAt !== null) {
2838
2898
  return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "mx-msg__bubble mx-msg__bubble--deleted", children: "Message deleted" });
2839
2899
  }
@@ -2844,10 +2904,16 @@ function MessageBubble(props) {
2844
2904
  message.deliveryState === "sending" ? "mx-msg__bubble--pending" : "",
2845
2905
  message.deliveryState === "failed" ? "mx-msg__bubble--failed" : ""
2846
2906
  ].filter(Boolean).join(" ");
2847
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
2907
+ const bubble = /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
2848
2908
  /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: classes, children: [
2849
2909
  splitText(message.content).map(
2850
- (segment, index) => segment.type === "text" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: segment.value }, index) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2910
+ (segment, index) => segment.type === "text" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: segment.value }, index) : segment.type === "fence" ? HostFence !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(HostFence, { body: segment.body }, `fence:${index}`) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(UnreadableReference, {}, `fence:${index}`) : HostReference !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2911
+ HostReference,
2912
+ {
2913
+ reference: segment.reference
2914
+ },
2915
+ `${segment.reference.entityType}:${segment.reference.entityId}`
2916
+ ) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2851
2917
  ReferenceCard,
2852
2918
  {
2853
2919
  reference: segment.reference,
@@ -2856,14 +2922,22 @@ function MessageBubble(props) {
2856
2922
  `${segment.reference.entityType}:${segment.reference.entityId}`
2857
2923
  )
2858
2924
  ),
2859
- message.references.map((reference) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2860
- ReferenceCard,
2861
- {
2862
- reference,
2863
- onOpen: host?.openReference ?? null
2864
- },
2865
- `structured:${reference.entityType}:${reference.entityId}`
2866
- )),
2925
+ message.references.map(
2926
+ (reference) => HostReference !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2927
+ HostReference,
2928
+ {
2929
+ reference
2930
+ },
2931
+ `structured:${reference.entityType}:${reference.entityId}`
2932
+ ) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2933
+ ReferenceCard,
2934
+ {
2935
+ reference,
2936
+ onOpen: host?.openReference ?? null
2937
+ },
2938
+ `structured:${reference.entityType}:${reference.entityId}`
2939
+ )
2940
+ ),
2867
2941
  message.action !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MessageActionChips, { message }) : null
2868
2942
  ] }),
2869
2943
  /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
@@ -2899,6 +2973,7 @@ function MessageBubble(props) {
2899
2973
  }
2900
2974
  )
2901
2975
  ] });
2976
+ return MessageChrome !== null ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MessageChrome, { message, isMine, children: bubble }) : bubble;
2902
2977
  }
2903
2978
  function MessageActionChips(props) {
2904
2979
  const host = useRequiredMessagingHost();