@ai4b-team/fsaos-gateway-sdk 4.2.0 → 4.2.1

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.d.cts CHANGED
@@ -1324,6 +1324,8 @@ interface ChannelMessagesPage {
1324
1324
  */
1325
1325
  declare function fetchChannelMessagesPage(channelPath: string, options?: {
1326
1326
  parentMessageId?: string;
1327
+ /** List conversation roots before pagination, using the chat audience rules. */
1328
+ conversationsOnly?: boolean;
1327
1329
  limit?: number;
1328
1330
  before_seq?: number;
1329
1331
  after_seq?: number;
@@ -2312,10 +2314,12 @@ interface InfiniteChannelMessagesOptions {
2312
2314
  /** VFS path of the channel. Required. */
2313
2315
  channelPath: string | undefined | null;
2314
2316
  /**
2315
- * If set, fetches thread replies (forward pagination with after_seq).
2316
- * If omitted, fetches top-level messages (backward pagination with before_seq).
2317
+ * If set, fetches thread replies. Both modes start with the latest page
2318
+ * and support older pages; threads also support explicit forward paging.
2317
2319
  */
2318
2320
  parentMessageId?: string;
2321
+ /** Return conversation roots, excluding activity before the page limit. */
2322
+ conversationsOnly?: boolean;
2319
2323
  /** Messages per page. Default 50, max 200. */
2320
2324
  limit?: number;
2321
2325
  /** Enable SSE-driven auto-refresh. Default: true. */
@@ -2331,7 +2335,8 @@ interface InfiniteChannelMessagesOptions {
2331
2335
  * loads older messages using `before_seq` = lowest seq in current data.
2332
2336
  *
2333
2337
  * - **Thread mode** (parentMessageId set): forward pagination.
2334
- * Initial fetch returns the first N replies. `fetchNextPage` loads newer
2338
+ * Initial fetch returns the latest N replies. `fetchPreviousPage` loads older
2339
+ * replies using before_seq. `fetchNextPage` loads newer
2335
2340
  * replies using `after_seq` = highest seq in current data.
2336
2341
  *
2337
2342
  * Returns a flat `messages` array (deduped, sorted by seq ASC) plus
@@ -2352,10 +2357,10 @@ declare function useInfiniteChannelMessages(options: InfiniteChannelMessagesOpti
2352
2357
  error: Error | null;
2353
2358
  refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2354
2359
  hasPreviousPage: boolean;
2355
- fetchPreviousPage: (options?: _tanstack_react_query.FetchPreviousPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2360
+ fetchPreviousPage: (options?: _tanstack_react_query.FetchNextPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2356
2361
  isFetchingPreviousPage: boolean;
2357
2362
  hasNextPage: boolean;
2358
- fetchNextPage: (options?: _tanstack_react_query.FetchNextPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2363
+ fetchNextPage: (options?: _tanstack_react_query.FetchPreviousPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2359
2364
  isFetchingNextPage: boolean;
2360
2365
  };
2361
2366
  /**
package/dist/index.d.ts CHANGED
@@ -1324,6 +1324,8 @@ interface ChannelMessagesPage {
1324
1324
  */
1325
1325
  declare function fetchChannelMessagesPage(channelPath: string, options?: {
1326
1326
  parentMessageId?: string;
1327
+ /** List conversation roots before pagination, using the chat audience rules. */
1328
+ conversationsOnly?: boolean;
1327
1329
  limit?: number;
1328
1330
  before_seq?: number;
1329
1331
  after_seq?: number;
@@ -2312,10 +2314,12 @@ interface InfiniteChannelMessagesOptions {
2312
2314
  /** VFS path of the channel. Required. */
2313
2315
  channelPath: string | undefined | null;
2314
2316
  /**
2315
- * If set, fetches thread replies (forward pagination with after_seq).
2316
- * If omitted, fetches top-level messages (backward pagination with before_seq).
2317
+ * If set, fetches thread replies. Both modes start with the latest page
2318
+ * and support older pages; threads also support explicit forward paging.
2317
2319
  */
2318
2320
  parentMessageId?: string;
2321
+ /** Return conversation roots, excluding activity before the page limit. */
2322
+ conversationsOnly?: boolean;
2319
2323
  /** Messages per page. Default 50, max 200. */
2320
2324
  limit?: number;
2321
2325
  /** Enable SSE-driven auto-refresh. Default: true. */
@@ -2331,7 +2335,8 @@ interface InfiniteChannelMessagesOptions {
2331
2335
  * loads older messages using `before_seq` = lowest seq in current data.
2332
2336
  *
2333
2337
  * - **Thread mode** (parentMessageId set): forward pagination.
2334
- * Initial fetch returns the first N replies. `fetchNextPage` loads newer
2338
+ * Initial fetch returns the latest N replies. `fetchPreviousPage` loads older
2339
+ * replies using before_seq. `fetchNextPage` loads newer
2335
2340
  * replies using `after_seq` = highest seq in current data.
2336
2341
  *
2337
2342
  * Returns a flat `messages` array (deduped, sorted by seq ASC) plus
@@ -2352,10 +2357,10 @@ declare function useInfiniteChannelMessages(options: InfiniteChannelMessagesOpti
2352
2357
  error: Error | null;
2353
2358
  refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2354
2359
  hasPreviousPage: boolean;
2355
- fetchPreviousPage: (options?: _tanstack_react_query.FetchPreviousPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2360
+ fetchPreviousPage: (options?: _tanstack_react_query.FetchNextPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2356
2361
  isFetchingPreviousPage: boolean;
2357
2362
  hasNextPage: boolean;
2358
- fetchNextPage: (options?: _tanstack_react_query.FetchNextPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2363
+ fetchNextPage: (options?: _tanstack_react_query.FetchPreviousPageOptions) => Promise<_tanstack_react_query.InfiniteQueryObserverResult<_tanstack_react_query.InfiniteData<ChannelMessagesPage, unknown>, Error>>;
2359
2364
  isFetchingNextPage: boolean;
2360
2365
  };
2361
2366
  /**
package/dist/index.js CHANGED
@@ -2086,7 +2086,7 @@ async function fetchChannelMessages(channelPath, parentMessageId) {
2086
2086
  `[gateway] Failed to fetch channel messages for ${channelPath}:`,
2087
2087
  err
2088
2088
  );
2089
- return [];
2089
+ throw err;
2090
2090
  }
2091
2091
  }
2092
2092
  async function fetchChannelMessagesWithCards(channelPath, parentMessageId) {
@@ -2121,18 +2121,22 @@ async function fetchChannelMessagesWithCards(channelPath, parentMessageId) {
2121
2121
  `[gateway] Failed to fetch channel messages with cards for ${channelPath}:`,
2122
2122
  err
2123
2123
  );
2124
- return { messages: [], messageCards: /* @__PURE__ */ new Map(), siblingCards: [], mode: "top_level" };
2124
+ throw err;
2125
2125
  }
2126
2126
  }
2127
2127
  async function fetchChannelMessagesPage(channelPath, options) {
2128
2128
  try {
2129
2129
  const params = { channel_path: channelPath };
2130
2130
  if (options?.parentMessageId) params.parent_message_id = options.parentMessageId;
2131
+ if (options?.conversationsOnly) params.conversations_only = true;
2131
2132
  if (options?.limit) params.limit = options.limit;
2132
2133
  if (options?.before_seq != null) params.before_seq = options.before_seq;
2133
2134
  if (options?.after_seq != null) params.after_seq = options.after_seq;
2134
2135
  const response = await gatewayCall("get-channel-messages", params);
2135
2136
  const raw = response;
2137
+ if (options?.conversationsOnly && raw.conversations_only !== true) {
2138
+ throw new Error("This server does not support conversation history lists yet. Update the message reader.");
2139
+ }
2136
2140
  const items = raw.messages || [];
2137
2141
  const cards = parseChannelCards(items);
2138
2142
  const messageCards = /* @__PURE__ */ new Map();
@@ -2158,7 +2162,7 @@ async function fetchChannelMessagesPage(channelPath, options) {
2158
2162
  `[gateway] Failed to fetch channel messages page for ${channelPath}:`,
2159
2163
  err
2160
2164
  );
2161
- return { messages: [], messageCards: /* @__PURE__ */ new Map(), siblingCards: [], mode: "top_level" };
2165
+ throw err;
2162
2166
  }
2163
2167
  }
2164
2168
  function invalidateChildren(path) {
@@ -3865,31 +3869,31 @@ function useChannelMessages(options) {
3865
3869
  }
3866
3870
  function useInfiniteChannelMessages(options) {
3867
3871
  useScopeKey();
3868
- const { channelPath, parentMessageId, limit = 50, realtime = true, enabled: enabled2 = true } = options;
3872
+ const { channelPath, parentMessageId, limit = 50, realtime = true, enabled: enabled2 = true, conversationsOnly = false } = options;
3869
3873
  const isThread = !!parentMessageId;
3870
3874
  const queryEnabled = enabled2 && !!channelPath;
3871
3875
  const query = useInfiniteQuery({
3872
- queryKey: [...vfsKeys.channelMessages(channelPath || "", parentMessageId), "infinite", limit],
3876
+ queryKey: [...vfsKeys.channelMessages(channelPath || "", parentMessageId), "infinite", limit, "latest-first", ...conversationsOnly ? ["conversations"] : []],
3873
3877
  queryFn: async ({ pageParam }) => {
3874
3878
  return fetchChannelMessagesPage(channelPath, {
3875
3879
  parentMessageId,
3876
3880
  limit,
3877
- before_seq: isThread ? void 0 : pageParam,
3878
- after_seq: isThread ? pageParam : void 0
3881
+ conversationsOnly,
3882
+ before_seq: pageParam?.before_seq,
3883
+ after_seq: pageParam?.after_seq
3879
3884
  });
3880
3885
  },
3881
3886
  initialPageParam: void 0,
3882
- getNextPageParam: (lastPage) => {
3887
+ getPreviousPageParam: (lastPage) => {
3883
3888
  if (!isThread) return void 0;
3884
3889
  if (lastPage.messages.length < limit) return void 0;
3885
3890
  const maxSeq = lastPage.messages[lastPage.messages.length - 1]?.seq;
3886
- return maxSeq;
3891
+ return maxSeq == null ? void 0 : { after_seq: maxSeq };
3887
3892
  },
3888
- getPreviousPageParam: (firstPage) => {
3889
- if (isThread) return void 0;
3893
+ getNextPageParam: (firstPage) => {
3890
3894
  if (firstPage.messages.length < limit) return void 0;
3891
3895
  const minSeq = firstPage.messages[0]?.seq;
3892
- return minSeq;
3896
+ return minSeq == null ? void 0 : { before_seq: minSeq };
3893
3897
  },
3894
3898
  enabled: queryEnabled
3895
3899
  });
@@ -3933,8 +3937,8 @@ function useInfiniteChannelMessages(options) {
3933
3937
  const qc = useQueryClient();
3934
3938
  const debounceRef = useRef(null);
3935
3939
  const stableKey = useMemo(
3936
- () => [...vfsKeys.channelMessages(channelPath || "", parentMessageId), "infinite", limit],
3937
- [channelPath, parentMessageId, limit]
3940
+ () => [...vfsKeys.channelMessages(channelPath || "", parentMessageId), "infinite", limit, "latest-first", ...conversationsOnly ? ["conversations"] : []],
3941
+ [channelPath, parentMessageId, limit, conversationsOnly]
3938
3942
  );
3939
3943
  useEffect(() => {
3940
3944
  if (!realtime || !channelPath || !queryEnabled) return;
@@ -3943,6 +3947,10 @@ function useInfiniteChannelMessages(options) {
3943
3947
  let tailAgain = false;
3944
3948
  let disposed = false;
3945
3949
  const tailOnce = async () => {
3950
+ if (conversationsOnly) {
3951
+ reload();
3952
+ return;
3953
+ }
3946
3954
  const currentData = qc.getQueryData(stableKey);
3947
3955
  if (!currentData?.pages?.length) {
3948
3956
  reload();
@@ -3981,7 +3989,7 @@ function useInfiniteChannelMessages(options) {
3981
3989
  qc.setQueryData(stableKey, (old) => {
3982
3990
  if (!old) return old;
3983
3991
  const pages = [...old.pages];
3984
- const lastIdx = pages.length - 1;
3992
+ const lastIdx = 0;
3985
3993
  const lastPage = pages[lastIdx];
3986
3994
  const existingIds = new Set(lastPage.messages.map((m) => m.id));
3987
3995
  const uniqueNew = newPage.messages.filter((m) => !existingIds.has(m.id));
@@ -4033,7 +4041,7 @@ function useInfiniteChannelMessages(options) {
4033
4041
  void tail();
4034
4042
  }, 500);
4035
4043
  },
4036
- (data) => channelMessageChange(
4044
+ (data) => conversationsOnly && data?.item_type === "message" && (qc.getQueryData(stableKey)?.pages ?? []).some((page) => page.messages.some((root) => root.id === data.parent_id)) || channelMessageChange(
4037
4045
  data,
4038
4046
  channelPath,
4039
4047
  parentMessageId,
@@ -4045,11 +4053,11 @@ function useInfiniteChannelMessages(options) {
4045
4053
  unsub();
4046
4054
  if (debounceRef.current) clearTimeout(debounceRef.current);
4047
4055
  };
4048
- }, [realtime, channelPath, queryEnabled, stableKey, qc, parentMessageId, limit]);
4056
+ }, [realtime, channelPath, queryEnabled, stableKey, qc, parentMessageId, limit, conversationsOnly]);
4049
4057
  return {
4050
4058
  messages,
4051
4059
  // Activity is a current thread snapshot, not another reply page.
4052
- activityMessages: query.data?.pages.at(-1)?.activityMessages ?? [],
4060
+ activityMessages: query.data?.pages[0]?.activityMessages ?? [],
4053
4061
  /** Cards grouped by parent message path (child cards of messages). */
4054
4062
  messageCards,
4055
4063
  /** Cards that are direct siblings of the channel (not nested under a message). */
@@ -4061,14 +4069,14 @@ function useInfiniteChannelMessages(options) {
4061
4069
  isFetching: query.isFetching,
4062
4070
  error: query.error,
4063
4071
  refetch: query.refetch,
4064
- // Backward pagination (top-level: load older)
4065
- hasPreviousPage: query.hasPreviousPage ?? false,
4066
- fetchPreviousPage: query.fetchPreviousPage,
4067
- isFetchingPreviousPage: query.isFetchingPreviousPage ?? false,
4072
+ // Backward pagination (channels and threads: load older)
4073
+ hasPreviousPage: query.hasNextPage ?? false,
4074
+ fetchPreviousPage: query.fetchNextPage,
4075
+ isFetchingPreviousPage: query.isFetchingNextPage ?? false,
4068
4076
  // Forward pagination (thread: load newer)
4069
- hasNextPage: query.hasNextPage ?? false,
4070
- fetchNextPage: query.fetchNextPage,
4071
- isFetchingNextPage: query.isFetchingNextPage ?? false
4077
+ hasNextPage: query.hasPreviousPage ?? false,
4078
+ fetchNextPage: query.fetchPreviousPage,
4079
+ isFetchingNextPage: query.isFetchingPreviousPage ?? false
4072
4080
  };
4073
4081
  }
4074
4082
  var CHANNEL_AI_MODES = ["open", "active", "strict", "hand_raise"];