@ai-matrx/messaging 0.16.1 → 0.17.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
@@ -62,7 +62,7 @@ __export(react_exports, {
62
62
  RPCS: () => RPCS,
63
63
  ReferenceCard: () => ReferenceCard,
64
64
  ReplyIcon: () => ReplyIcon,
65
- SERVER_SESSION_IDENTITY: () => SERVER_SESSION_IDENTITY,
65
+ SERVER_SESSION_IDENTITY: () => import_session_identity2.SERVER_SESSION_IDENTITY,
66
66
  SearchIcon: () => SearchIcon,
67
67
  SendIcon: () => SendIcon,
68
68
  SparklesIcon: () => SparklesIcon,
@@ -85,7 +85,7 @@ __export(react_exports, {
85
85
  createMessagingStore: () => createMessagingStore,
86
86
  createOutbox: () => createOutbox,
87
87
  createReadCache: () => createReadCache,
88
- createSessionIdentity: () => createSessionIdentity,
88
+ createSessionIdentity: () => createMessagingSessionIdentity,
89
89
  createWebOutboxStorage: () => createWebOutboxStorage,
90
90
  effectiveActorKey: () => effectiveActorKey,
91
91
  extractReferences: () => extractReferences,
@@ -526,22 +526,9 @@ var import_realtime3 = require("@ai-matrx/realtime");
526
526
 
527
527
  // src/core/channels.ts
528
528
  var import_realtime = require("@ai-matrx/realtime");
529
-
530
- // src/core/slot.ts
531
- var NAMESPACE = "ai-matrx.messaging";
532
- function globalSlot(name, create) {
533
- const key = /* @__PURE__ */ Symbol.for(`${NAMESPACE}.${name}`);
534
- const host = globalThis;
535
- const existing = host[key];
536
- if (existing !== void 0) return existing;
537
- const created = create();
538
- host[key] = created;
539
- return created;
540
- }
541
-
542
- // src/core/channels.ts
529
+ var import_global_slot = require("@ai-matrx/kit/global-slot");
543
530
  function namespaces() {
544
- return globalSlot("channel-namespaces", () => ({
531
+ return (0, import_global_slot.globalSlot)("ai-matrx.messaging.channel-namespaces", () => ({
545
532
  inbox: (0, import_realtime.defineChannelNamespace)({
546
533
  namespace: "messaging-inbox",
547
534
  parts: ["userId"],
@@ -1990,89 +1977,24 @@ function createReadCache(options) {
1990
1977
  }
1991
1978
 
1992
1979
  // src/core/session-identity.ts
1993
- var RESOLVING = Object.freeze({
1994
- state: "resolving",
1995
- userId: null
1996
- });
1997
- var SERVER_SESSION_IDENTITY = RESOLVING;
1998
- function authOf(client) {
1999
- const candidate = client.auth;
2000
- if (candidate === null || typeof candidate !== "object") return null;
2001
- const auth = candidate;
2002
- if (typeof auth.getSession !== "function") return null;
2003
- if (typeof auth.onAuthStateChange !== "function") return null;
2004
- return auth;
2005
- }
2006
- function userIdOf(session) {
2007
- const id = session?.user?.id;
2008
- return typeof id === "string" && id.length > 0 ? asUserId(id) : null;
2009
- }
2010
- var NO_AUTH_SOURCE = {
2011
- message: "Messaging cannot tell who is signed in: the Supabase client it was given has no auth session source.",
2012
- remedy: "Pass your app's browser Supabase client \u2014 the same client it signs in with \u2014 to <MessagingProvider client={...}> or createMessagingRepository({ client }). Messaging reads the acting user from that client's session; there is no prop or option for a user id, on purpose (DD-241)."
1980
+ var import_session_identity = require("@ai-matrx/data/session-identity");
1981
+ var import_session_identity2 = require("@ai-matrx/data/session-identity");
1982
+ var MESSAGING_SESSION_WORDS = {
1983
+ product: "Messaging",
1984
+ noAuthSourceRemedy: "Pass your app's browser Supabase client \u2014 the same client it signs in with \u2014 to <MessagingProvider client={...}> or createMessagingRepository({ client }). Messaging reads the acting user from that client's session; there is no prop or option for a user id, on purpose (DD-241).",
1985
+ unreadableRemedy: "Messages stay unavailable on this device until the session can be read. Reload the page; if it persists, sign in again."
2013
1986
  };
2014
- function createSessionIdentity(options) {
2015
- const { client, onDiagnostic } = options;
2016
- const listeners = /* @__PURE__ */ new Set();
2017
- let current = RESOLVING;
2018
- let disposed = false;
2019
- const publish = (state, userId) => {
2020
- if (disposed) return;
2021
- if (current.state === state && current.userId === userId) return;
2022
- current = Object.freeze({ state, userId });
2023
- for (const listener of [...listeners]) listener();
2024
- };
2025
- const subscribeOnly = (hasAuthSource) => ({
2026
- hasAuthSource,
2027
- snapshot: () => current,
2028
- subscribe: (listener) => {
2029
- listeners.add(listener);
2030
- return () => {
2031
- listeners.delete(listener);
2032
- };
2033
- },
2034
- dispose: () => {
2035
- disposed = true;
2036
- listeners.clear();
2037
- }
2038
- });
2039
- const auth = authOf(client);
2040
- if (auth === null) {
2041
- onDiagnostic?.({ level: "error", ...NO_AUTH_SOURCE });
2042
- current = Object.freeze({ state: "resolved", userId: null });
2043
- return subscribeOnly(false);
2044
- }
2045
- void (async () => {
2046
- try {
2047
- const result = await auth.getSession();
2048
- publish("resolved", userIdOf(result?.data?.session));
2049
- } catch (error) {
2050
- onDiagnostic?.({
2051
- level: "warn",
2052
- message: `Messaging could not read the signed-in session: ${String(
2053
- error?.message ?? error
2054
- )}`,
2055
- remedy: "Messages stay unavailable on this device until the session can be read. Reload the page; if it persists, sign in again."
2056
- });
2057
- publish("resolved", null);
2058
- }
2059
- })();
2060
- const subscription = auth.onAuthStateChange((_event, session) => {
2061
- publish("resolved", userIdOf(session));
1987
+ function createMessagingSessionIdentity(options) {
1988
+ return (0, import_session_identity.createSessionIdentity)({
1989
+ client: options.client,
1990
+ words: MESSAGING_SESSION_WORDS,
1991
+ toUserId: asUserId,
1992
+ onDiagnostic: options.onDiagnostic
2062
1993
  });
2063
- const base = subscribeOnly(true);
2064
- return {
2065
- hasAuthSource: true,
2066
- snapshot: base.snapshot,
2067
- subscribe: base.subscribe,
2068
- dispose: () => {
2069
- base.dispose();
2070
- try {
2071
- subscription.data.subscription.unsubscribe();
2072
- } catch {
2073
- }
2074
- }
2075
- };
1994
+ }
1995
+ function userIdOf(session) {
1996
+ const id = (0, import_session_identity.sessionUserId)(session);
1997
+ return id === null ? null : asUserId(id);
2076
1998
  }
2077
1999
  function actingUserIdOf(identity) {
2078
2000
  return new Promise((resolve, reject) => {
@@ -2100,14 +2022,11 @@ function noSignedInUserError() {
2100
2022
  );
2101
2023
  }
2102
2024
  function noAuthSourceError() {
2103
- return new MessagingError(
2104
- "misconfigured",
2105
- NO_AUTH_SOURCE.message,
2106
- NO_AUTH_SOURCE.remedy
2107
- );
2025
+ const { message, remedy } = (0, import_session_identity.noAuthSourceMessage)(MESSAGING_SESSION_WORDS);
2026
+ return new MessagingError("misconfigured", message, remedy);
2108
2027
  }
2109
2028
  async function readActingUserId(client) {
2110
- const auth = authOf(client);
2029
+ const auth = (0, import_session_identity.authOf)(client);
2111
2030
  if (auth === null) throw noAuthSourceError();
2112
2031
  let session;
2113
2032
  try {
@@ -2116,9 +2035,7 @@ async function readActingUserId(client) {
2116
2035
  } catch (error) {
2117
2036
  throw new MessagingError(
2118
2037
  "session-unavailable",
2119
- `Messaging could not read the signed-in session: ${String(
2120
- error?.message ?? error
2121
- )}`,
2038
+ (0, import_session_identity.unreadableSessionMessage)(MESSAGING_SESSION_WORDS, error),
2122
2039
  "Reload the page and try again; if it persists, sign in again.",
2123
2040
  error
2124
2041
  );
@@ -2751,10 +2668,11 @@ function createMessagingRepository(options) {
2751
2668
  }
2752
2669
 
2753
2670
  // src/react/provider.tsx
2671
+ var import_global_slot2 = require("@ai-matrx/kit/global-slot");
2754
2672
  var import_jsx_runtime = require("react/jsx-runtime");
2755
2673
  function messagingContext() {
2756
- return globalSlot(
2757
- "react-context",
2674
+ return (0, import_global_slot2.globalSlot)(
2675
+ "ai-matrx.messaging.react-context",
2758
2676
  () => (0, import_react.createContext)(null)
2759
2677
  );
2760
2678
  }
@@ -2770,7 +2688,7 @@ function useSessionUserId(client, onDiagnostic) {
2770
2688
  diagnosticRef.current = onDiagnostic;
2771
2689
  const identity = (0, import_react.useMemo)(() => {
2772
2690
  if (client == null) return null;
2773
- return createSessionIdentity({
2691
+ return createMessagingSessionIdentity({
2774
2692
  client,
2775
2693
  onDiagnostic: (event) => {
2776
2694
  (diagnosticRef.current ?? defaultDiagnostics)(event);
@@ -2785,8 +2703,8 @@ function useSessionUserId(client, onDiagnostic) {
2785
2703
  }, [identity]);
2786
2704
  const snapshot = (0, import_react.useSyncExternalStore)(
2787
2705
  (listener) => identity === null ? () => void 0 : identity.subscribe(listener),
2788
- () => identity?.snapshot() ?? SERVER_SESSION_IDENTITY,
2789
- () => SERVER_SESSION_IDENTITY
2706
+ () => identity?.snapshot() ?? import_session_identity2.SERVER_SESSION_IDENTITY,
2707
+ () => import_session_identity2.SERVER_SESSION_IDENTITY
2790
2708
  );
2791
2709
  return { identity, snapshot };
2792
2710
  }