@flamingo-stack/openframe-frontend-core 0.0.597 → 0.0.598-2039.5704.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.
Files changed (186) hide show
  1. package/dist/{chunk-PT54TJWD.js → chunk-26MKONC3.js} +5 -5
  2. package/dist/{chunk-NK6COZ3P.cjs → chunk-2TWJF4AB.cjs} +22 -22
  3. package/dist/{chunk-NK6COZ3P.cjs.map → chunk-2TWJF4AB.cjs.map} +1 -1
  4. package/dist/{chunk-E74GL4E3.cjs → chunk-3GLZDKWZ.cjs} +8 -8
  5. package/dist/{chunk-E74GL4E3.cjs.map → chunk-3GLZDKWZ.cjs.map} +1 -1
  6. package/dist/{chunk-STYNZ4QM.cjs → chunk-3IXDZI3R.cjs} +3 -3
  7. package/dist/{chunk-STYNZ4QM.cjs.map → chunk-3IXDZI3R.cjs.map} +1 -1
  8. package/dist/{chunk-DNQP4VPB.js → chunk-3XHNV6W7.js} +12 -12
  9. package/dist/{chunk-TVK7PQON.js → chunk-442V6CPU.js} +6 -6
  10. package/dist/{chunk-BZ4UEHNJ.js → chunk-5IMAIYID.js} +9 -9
  11. package/dist/{chunk-VQ6INRQ4.cjs → chunk-5VQKQIOG.cjs} +105 -105
  12. package/dist/{chunk-VQ6INRQ4.cjs.map → chunk-5VQKQIOG.cjs.map} +1 -1
  13. package/dist/{chunk-6QZNDPMV.cjs → chunk-7WTEFZP7.cjs} +9 -9
  14. package/dist/{chunk-6QZNDPMV.cjs.map → chunk-7WTEFZP7.cjs.map} +1 -1
  15. package/dist/{chunk-B4BZRBKC.js → chunk-7YMVB7GE.js} +4 -4
  16. package/dist/{chunk-BOILDHSY.js → chunk-AC2TI6UT.js} +12 -12
  17. package/dist/{chunk-YBPFIXV4.cjs → chunk-B6ZJV7L4.cjs} +746 -448
  18. package/dist/{chunk-YBPFIXV4.cjs.map → chunk-B6ZJV7L4.cjs.map} +1 -1
  19. package/dist/{chunk-RYZKWZBG.cjs → chunk-B72CD6YE.cjs} +15 -15
  20. package/dist/{chunk-RYZKWZBG.cjs.map → chunk-B72CD6YE.cjs.map} +1 -1
  21. package/dist/{chunk-EVMHJB6X.js → chunk-BLUL2TE3.js} +4 -4
  22. package/dist/{chunk-GGQZBSFN.js → chunk-BVQEU5JB.js} +2 -2
  23. package/dist/{chunk-H64ZHSYR.cjs → chunk-CCWI3622.cjs} +4 -4
  24. package/dist/{chunk-H64ZHSYR.cjs.map → chunk-CCWI3622.cjs.map} +1 -1
  25. package/dist/{chunk-ELG2QNW5.js → chunk-D7X33EAT.js} +7 -7
  26. package/dist/{chunk-L3L2ATFK.cjs → chunk-E2K2LYSM.cjs} +85 -85
  27. package/dist/{chunk-L3L2ATFK.cjs.map → chunk-E2K2LYSM.cjs.map} +1 -1
  28. package/dist/{chunk-YH6IQTPI.cjs → chunk-EHAYNUOL.cjs} +3 -3
  29. package/dist/{chunk-YH6IQTPI.cjs.map → chunk-EHAYNUOL.cjs.map} +1 -1
  30. package/dist/{chunk-WZJOILPU.js → chunk-F2KBCLUG.js} +2 -2
  31. package/dist/{chunk-MYBXCWEI.js → chunk-FKBM6U43.js} +2 -2
  32. package/dist/{chunk-FKOYQPMK.js → chunk-GQLYYZF4.js} +5 -5
  33. package/dist/{chunk-LRQCKSZC.js → chunk-IW746HJ7.js} +2 -2
  34. package/dist/{chunk-GKZUTZT3.cjs → chunk-JI2R4UW5.cjs} +15 -15
  35. package/dist/{chunk-GKZUTZT3.cjs.map → chunk-JI2R4UW5.cjs.map} +1 -1
  36. package/dist/{chunk-M4YQUHOR.cjs → chunk-JJUV7Z4L.cjs} +15 -15
  37. package/dist/{chunk-M4YQUHOR.cjs.map → chunk-JJUV7Z4L.cjs.map} +1 -1
  38. package/dist/{chunk-2JOIC34G.cjs → chunk-KJMXCDYI.cjs} +37 -37
  39. package/dist/{chunk-2JOIC34G.cjs.map → chunk-KJMXCDYI.cjs.map} +1 -1
  40. package/dist/{chunk-IRXLB6GV.js → chunk-KV24I5MD.js} +5 -5
  41. package/dist/{chunk-Z4QFA4BF.cjs → chunk-M7U3MT2Q.cjs} +14 -14
  42. package/dist/{chunk-Z4QFA4BF.cjs.map → chunk-M7U3MT2Q.cjs.map} +1 -1
  43. package/dist/{chunk-R2YJKREE.js → chunk-NJCVIERS.js} +4 -4
  44. package/dist/{chunk-VXEHG53O.js → chunk-OIUXJUMH.js} +6 -6
  45. package/dist/{chunk-SAEJGTCI.cjs → chunk-QPAPUIF3.cjs} +35 -35
  46. package/dist/{chunk-SAEJGTCI.cjs.map → chunk-QPAPUIF3.cjs.map} +1 -1
  47. package/dist/{chunk-CPJP7GKL.cjs → chunk-R6LFBCGC.cjs} +3 -3
  48. package/dist/{chunk-CPJP7GKL.cjs.map → chunk-R6LFBCGC.cjs.map} +1 -1
  49. package/dist/{chunk-FHWL7QFL.js → chunk-RTCKC6RH.js} +5 -5
  50. package/dist/{chunk-UAAXUFK6.js → chunk-RU3IKVY2.js} +3 -3
  51. package/dist/{chunk-EZM5DNII.js → chunk-S6WBBSII.js} +3 -3
  52. package/dist/{chunk-WWGSYT7R.js → chunk-SYFSVIGH.js} +2 -2
  53. package/dist/{chunk-R2MOCGZ7.cjs → chunk-SZRP4264.cjs} +34 -34
  54. package/dist/{chunk-R2MOCGZ7.cjs.map → chunk-SZRP4264.cjs.map} +1 -1
  55. package/dist/{chunk-2JNCZDJR.js → chunk-TSCIVH7T.js} +2 -2
  56. package/dist/{chunk-JJFYMYBJ.cjs → chunk-TYENRLNN.cjs} +39 -39
  57. package/dist/{chunk-JJFYMYBJ.cjs.map → chunk-TYENRLNN.cjs.map} +1 -1
  58. package/dist/{chunk-EGQ345IA.js → chunk-UHWDSVZI.js} +4 -4
  59. package/dist/{chunk-RDTOMWDN.cjs → chunk-UPKZU6QU.cjs} +1 -1
  60. package/dist/chunk-UPKZU6QU.cjs.map +1 -0
  61. package/dist/{chunk-WCBRZCU4.cjs → chunk-UQEOAPAI.cjs} +9 -9
  62. package/dist/{chunk-WCBRZCU4.cjs.map → chunk-UQEOAPAI.cjs.map} +1 -1
  63. package/dist/{chunk-O4VZOA2Q.cjs → chunk-UXOOVP77.cjs} +3 -3
  64. package/dist/{chunk-O4VZOA2Q.cjs.map → chunk-UXOOVP77.cjs.map} +1 -1
  65. package/dist/{chunk-BZEWPW5A.cjs → chunk-VIBREYUK.cjs} +3 -3
  66. package/dist/{chunk-BZEWPW5A.cjs.map → chunk-VIBREYUK.cjs.map} +1 -1
  67. package/dist/{chunk-E27AKT4A.cjs → chunk-WRZV3HGH.cjs} +9 -9
  68. package/dist/{chunk-E27AKT4A.cjs.map → chunk-WRZV3HGH.cjs.map} +1 -1
  69. package/dist/{chunk-PIQRGLTF.cjs → chunk-WU53IOY6.cjs} +25 -25
  70. package/dist/{chunk-PIQRGLTF.cjs.map → chunk-WU53IOY6.cjs.map} +1 -1
  71. package/dist/{chunk-VNSRNVPD.js → chunk-XAJZIDFH.js} +673 -375
  72. package/dist/chunk-XAJZIDFH.js.map +1 -0
  73. package/dist/{chunk-UIVLSMEZ.cjs → chunk-XRVFQQ7W.cjs} +242 -242
  74. package/dist/{chunk-UIVLSMEZ.cjs.map → chunk-XRVFQQ7W.cjs.map} +1 -1
  75. package/dist/{chunk-RYXIRVDC.js → chunk-YB6FI2G3.js} +1 -1
  76. package/dist/chunk-YB6FI2G3.js.map +1 -0
  77. package/dist/{chunk-V6KX4KO3.js → chunk-YPJB4LJD.js} +3 -3
  78. package/dist/components/case-studies/index.cjs +14 -14
  79. package/dist/components/case-studies/index.js +8 -8
  80. package/dist/components/chat/hooks/index.d.ts +1 -0
  81. package/dist/components/chat/hooks/index.d.ts.map +1 -1
  82. package/dist/components/chat/hooks/use-chat-history-hydration.d.ts +5 -3
  83. package/dist/components/chat/hooks/use-chat-history-hydration.d.ts.map +1 -1
  84. package/dist/components/chat/hooks/use-managed-dialog-list.d.ts +49 -0
  85. package/dist/components/chat/hooks/use-managed-dialog-list.d.ts.map +1 -0
  86. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +2 -16
  87. package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts.map +1 -1
  88. package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
  89. package/dist/components/chat/hooks/use-unified-chat.d.ts.map +1 -1
  90. package/dist/components/chat/index.cjs +9 -5
  91. package/dist/components/chat/index.cjs.map +1 -1
  92. package/dist/components/chat/index.js +8 -4
  93. package/dist/components/chat/types/unified-chat-state.types.d.ts +55 -0
  94. package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
  95. package/dist/components/chat/utils/chat-conversations-api.d.ts +29 -0
  96. package/dist/components/chat/utils/chat-conversations-api.d.ts.map +1 -0
  97. package/dist/components/chat/utils/index.d.ts +1 -0
  98. package/dist/components/chat/utils/index.d.ts.map +1 -1
  99. package/dist/components/contact/index.cjs +9 -9
  100. package/dist/components/contact/index.js +8 -8
  101. package/dist/components/docs/index.cjs +12 -12
  102. package/dist/components/docs/index.js +11 -11
  103. package/dist/components/embeds/index.cjs +9 -9
  104. package/dist/components/embeds/index.js +8 -8
  105. package/dist/components/faq/index.cjs +10 -10
  106. package/dist/components/faq/index.js +9 -9
  107. package/dist/components/features/index.cjs +8 -8
  108. package/dist/components/features/index.js +7 -7
  109. package/dist/components/help-center-pages/index.cjs +31 -31
  110. package/dist/components/help-center-pages/index.js +22 -22
  111. package/dist/components/index.cjs +70 -66
  112. package/dist/components/index.cjs.map +1 -1
  113. package/dist/components/index.js +26 -22
  114. package/dist/components/index.js.map +1 -1
  115. package/dist/components/meeting-scheduler/index.cjs +49 -49
  116. package/dist/components/meeting-scheduler/index.js +9 -9
  117. package/dist/components/navigation/index.cjs +11 -11
  118. package/dist/components/navigation/index.js +10 -10
  119. package/dist/components/onboarding-guides/index.cjs +12 -12
  120. package/dist/components/onboarding-guides/index.js +11 -11
  121. package/dist/components/related-content/index.cjs +7 -7
  122. package/dist/components/related-content/index.js +6 -6
  123. package/dist/components/tickets/index.cjs +13 -13
  124. package/dist/components/tickets/index.js +12 -12
  125. package/dist/components/ui/index.cjs +12 -8
  126. package/dist/components/ui/index.cjs.map +1 -1
  127. package/dist/components/ui/index.js +11 -7
  128. package/dist/contexts/chat-runtime-context.d.ts +12 -0
  129. package/dist/contexts/chat-runtime-context.d.ts.map +1 -1
  130. package/dist/contexts/index.cjs +2 -2
  131. package/dist/contexts/index.js +1 -1
  132. package/dist/hooks/index.cjs +5 -5
  133. package/dist/hooks/index.js +4 -4
  134. package/dist/index.cjs +16 -12
  135. package/dist/index.cjs.map +1 -1
  136. package/dist/index.js +15 -11
  137. package/dist/index.js.map +1 -1
  138. package/package.json +1 -1
  139. package/src/components/chat/.embeddable-chat.md +2 -1
  140. package/src/components/chat/embeddable-chat.tsx +69 -37
  141. package/src/components/chat/hooks/.use-managed-dialog-list.md +28 -0
  142. package/src/components/chat/hooks/.use-nats-chat-adapter.md +2 -0
  143. package/src/components/chat/hooks/.use-sse-chat-adapter.md +7 -0
  144. package/src/components/chat/hooks/.use-unified-chat.md +1 -1
  145. package/src/components/chat/hooks/__tests__/managed-dialog-list.test.ts +92 -0
  146. package/src/components/chat/hooks/__tests__/nats-adapter-managed-dialogs.test.ts +167 -0
  147. package/src/components/chat/hooks/__tests__/sse-managed-dialogs.test.ts +256 -0
  148. package/src/components/chat/hooks/index.ts +2 -0
  149. package/src/components/chat/hooks/use-chat-history-hydration.ts +8 -7
  150. package/src/components/chat/hooks/use-managed-dialog-list.ts +263 -0
  151. package/src/components/chat/hooks/use-nats-chat-adapter.ts +65 -150
  152. package/src/components/chat/hooks/use-sse-chat-adapter.ts +213 -39
  153. package/src/components/chat/hooks/use-unified-chat.ts +5 -0
  154. package/src/components/chat/types/unified-chat-state.types.ts +62 -0
  155. package/src/components/chat/utils/.chat-conversations-api.md +16 -0
  156. package/src/components/chat/utils/__tests__/chat-conversations-api-golden.test.ts +90 -0
  157. package/src/components/chat/utils/chat-conversations-api.ts +92 -0
  158. package/src/components/chat/utils/index.ts +7 -0
  159. package/src/contexts/.chat-runtime-context.md +2 -0
  160. package/src/contexts/chat-runtime-context.tsx +12 -0
  161. package/dist/chunk-RDTOMWDN.cjs.map +0 -1
  162. package/dist/chunk-RYXIRVDC.js.map +0 -1
  163. package/dist/chunk-VNSRNVPD.js.map +0 -1
  164. /package/dist/{chunk-PT54TJWD.js.map → chunk-26MKONC3.js.map} +0 -0
  165. /package/dist/{chunk-DNQP4VPB.js.map → chunk-3XHNV6W7.js.map} +0 -0
  166. /package/dist/{chunk-TVK7PQON.js.map → chunk-442V6CPU.js.map} +0 -0
  167. /package/dist/{chunk-BZ4UEHNJ.js.map → chunk-5IMAIYID.js.map} +0 -0
  168. /package/dist/{chunk-B4BZRBKC.js.map → chunk-7YMVB7GE.js.map} +0 -0
  169. /package/dist/{chunk-BOILDHSY.js.map → chunk-AC2TI6UT.js.map} +0 -0
  170. /package/dist/{chunk-EVMHJB6X.js.map → chunk-BLUL2TE3.js.map} +0 -0
  171. /package/dist/{chunk-GGQZBSFN.js.map → chunk-BVQEU5JB.js.map} +0 -0
  172. /package/dist/{chunk-ELG2QNW5.js.map → chunk-D7X33EAT.js.map} +0 -0
  173. /package/dist/{chunk-WZJOILPU.js.map → chunk-F2KBCLUG.js.map} +0 -0
  174. /package/dist/{chunk-MYBXCWEI.js.map → chunk-FKBM6U43.js.map} +0 -0
  175. /package/dist/{chunk-FKOYQPMK.js.map → chunk-GQLYYZF4.js.map} +0 -0
  176. /package/dist/{chunk-LRQCKSZC.js.map → chunk-IW746HJ7.js.map} +0 -0
  177. /package/dist/{chunk-IRXLB6GV.js.map → chunk-KV24I5MD.js.map} +0 -0
  178. /package/dist/{chunk-R2YJKREE.js.map → chunk-NJCVIERS.js.map} +0 -0
  179. /package/dist/{chunk-VXEHG53O.js.map → chunk-OIUXJUMH.js.map} +0 -0
  180. /package/dist/{chunk-FHWL7QFL.js.map → chunk-RTCKC6RH.js.map} +0 -0
  181. /package/dist/{chunk-UAAXUFK6.js.map → chunk-RU3IKVY2.js.map} +0 -0
  182. /package/dist/{chunk-EZM5DNII.js.map → chunk-S6WBBSII.js.map} +0 -0
  183. /package/dist/{chunk-WWGSYT7R.js.map → chunk-SYFSVIGH.js.map} +0 -0
  184. /package/dist/{chunk-2JNCZDJR.js.map → chunk-TSCIVH7T.js.map} +0 -0
  185. /package/dist/{chunk-EGQ345IA.js.map → chunk-UHWDSVZI.js.map} +0 -0
  186. /package/dist/{chunk-V6KX4KO3.js.map → chunk-YPJB4LJD.js.map} +0 -0
@@ -0,0 +1,263 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * useManagedDialogList — the ONE dialog-list state machine shared by both
5
+ * transport adapters (NATS/Mingo managed-dialog mode and the SSE/Guide
6
+ * adapter when `ChatRuntime.endpoints.chatConversationsUrl` is set).
7
+ *
8
+ * Owns: the loaded page of `DialogItem`s, the server cursor, first-page
9
+ * loading/error state (with the retry re-arm), cursor pagination, an
10
+ * optional server-side search term (a change re-runs page 1), and the
11
+ * optimistic rename / archive / delete mutations with rollback.
12
+ *
13
+ * Does NOT own: which dialog is active, message history for a dialog, or
14
+ * the transport (`fetchDialogMessages`, subscriptions, catch-up). Those stay
15
+ * in the adapters — they are transport-specific. The adapters react to a
16
+ * row leaving the list through `onDialogRemoved` (drop the active id, evict
17
+ * the reducer store …) so ordering stays: host callback → local removal →
18
+ * adapter side effect, exactly as the NATS adapter did before extraction.
19
+ *
20
+ * Host callbacks are all optional: without `fetchDialogs` the hook is inert
21
+ * (empty list, no network) and a mutation without its callback is a no-op —
22
+ * the presence of a callback IS the capability, mirroring the NATS config.
23
+ */
24
+
25
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
26
+ import type { DialogItem } from '../types/component.types';
27
+ import type { FetchDialogsParams, FetchDialogsResult } from '../types/unified-chat-state.types';
28
+
29
+ export interface UseManagedDialogListArgs {
30
+ /** Mirrors the adapter's `active` gate — the initial load waits for it. */
31
+ active: boolean;
32
+ /** Pages the active dialog list. Absent = the hook is inert. */
33
+ fetchDialogs?: (params: FetchDialogsParams) => Promise<FetchDialogsResult>;
34
+ /** Backend rename — absent = `renameDialog` is a no-op. */
35
+ renameDialog?: (id: string, title: string) => Promise<void>;
36
+ /** Backend archive — absent = `archiveDialog` is a no-op. */
37
+ archiveDialog?: (id: string) => Promise<void>;
38
+ /** Backend delete — absent = `deleteDialog` is a no-op. */
39
+ deleteDialog?: (id: string) => Promise<void>;
40
+ /** Page size sent as `limit`. */
41
+ pageSize: number;
42
+ /** Server-side search term. `undefined` = no search support (the NATS
43
+ * adapter never sets it, so its wire params stay `{cursor, limit}`); any
44
+ * string change — including back to `''` — reloads page 1 with `search`. */
45
+ search?: string;
46
+ /** Fires AFTER a row was removed locally by a successful archive/delete.
47
+ * Read through a latest-ref so its identity never enters the mutation
48
+ * callbacks' deps. */
49
+ onDialogRemoved?: (id: string, reason: 'archived' | 'deleted') => void;
50
+ /** Console prefix for failure logs (`[useNatsChatAdapter]`, `[useSseChatAdapter]`). */
51
+ logTag: string;
52
+ }
53
+
54
+ export interface UseManagedDialogListResult {
55
+ dialogs: DialogItem[];
56
+ dialogsNextCursor: string | null;
57
+ /** True while ANY page request is in flight (first page or pagination). */
58
+ isDialogsLoading: boolean;
59
+ /** True when the FIRST page failed — a pagination failure keeps the loaded rows. */
60
+ dialogsError: boolean;
61
+ hasMoreDialogs: boolean;
62
+ /** Load a page: `undefined` cursor = (re)load the first page. */
63
+ loadDialogsPage: (cursor?: string) => Promise<void>;
64
+ /** Retry / refresh the first page. */
65
+ reloadDialogs: () => void;
66
+ loadMoreDialogs: () => Promise<void>;
67
+ renameDialog: (id: string, title: string) => Promise<void>;
68
+ archiveDialog: (id: string) => Promise<void>;
69
+ deleteDialog: (id: string) => Promise<void>;
70
+ /** Prepend a row (dedupes by id) — a freshly created conversation. */
71
+ upsertDialogTop: (item: DialogItem) => void;
72
+ /** Patch fields on an existing row (e.g. bump `timestamp` after a turn). */
73
+ patchDialog: (id: string, patch: Partial<DialogItem>) => void;
74
+ }
75
+
76
+ export function useManagedDialogList({
77
+ active,
78
+ fetchDialogs,
79
+ renameDialog: renameDialogCallback,
80
+ archiveDialog: archiveDialogCallback,
81
+ deleteDialog: deleteDialogCallback,
82
+ pageSize,
83
+ search,
84
+ onDialogRemoved,
85
+ logTag,
86
+ }: UseManagedDialogListArgs): UseManagedDialogListResult {
87
+ const [dialogs, setDialogs] = useState<DialogItem[]>([]);
88
+ const [dialogsNextCursor, setDialogsNextCursor] = useState<string | null>(null);
89
+ const [isDialogsLoading, setIsDialogsLoading] = useState<boolean>(false);
90
+ const [dialogsError, setDialogsError] = useState<boolean>(false);
91
+
92
+ // Latest-list mirror for the optimistic mutations: the rollback title must
93
+ // be read SYNCHRONOUSLY at call time. Capturing it inside a `setDialogs`
94
+ // updater is not reliable — React may run the updater only at render time,
95
+ // after an immediately-rejecting callback has already reached the catch.
96
+ const dialogsRef = useRef<DialogItem[]>(dialogs);
97
+ useEffect(() => {
98
+ dialogsRef.current = dialogs;
99
+ });
100
+
101
+ const onDialogRemovedRef = useRef(onDialogRemoved);
102
+ useEffect(() => {
103
+ onDialogRemovedRef.current = onDialogRemoved;
104
+ });
105
+
106
+ // Released on a first-page failure so a retry / re-activation re-runs the
107
+ // initial load.
108
+ const initialDialogsLoadedRef = useRef(false);
109
+
110
+ const loadDialogsPage = useCallback(
111
+ async (cursor?: string): Promise<void> => {
112
+ if (!fetchDialogs) return;
113
+ setIsDialogsLoading(true);
114
+ // Clear a prior first-page error when (re)loading the first page.
115
+ if (cursor === undefined) setDialogsError(false);
116
+ try {
117
+ const result = await fetchDialogs({
118
+ cursor,
119
+ limit: pageSize,
120
+ // Only hosts that run a search ever see the key.
121
+ ...(search !== undefined ? { search } : {}),
122
+ });
123
+ setDialogsNextCursor(result.nextCursor);
124
+ if (cursor === undefined) {
125
+ setDialogs(result.dialogs);
126
+ } else {
127
+ setDialogs(prev => [...prev, ...result.dialogs]);
128
+ }
129
+ } catch (err) {
130
+ console.error(`${logTag} fetchDialogs failed:`, err);
131
+ // Only the FIRST page failing is a "can't show the list" error — a
132
+ // pagination failure keeps the already-loaded list intact. Flag it and
133
+ // release the initial-load guard so a retry (or re-activation) re-runs.
134
+ if (cursor === undefined) {
135
+ setDialogsError(true);
136
+ initialDialogsLoadedRef.current = false;
137
+ }
138
+ } finally {
139
+ setIsDialogsLoading(false);
140
+ }
141
+ },
142
+ [fetchDialogs, pageSize, search, logTag],
143
+ );
144
+
145
+ const reloadDialogs = useCallback(() => {
146
+ void loadDialogsPage();
147
+ }, [loadDialogsPage]);
148
+
149
+ // Initial dialog list load — and, for hosts that run a search, the page-1
150
+ // reload on every term change (the term the loaded page was fetched for is
151
+ // remembered so the same term never double-loads). Inert for hosts that
152
+ // never set `search` (NATS): `loadedSearchRef` stays `undefined` there and
153
+ // the once-guard alone decides.
154
+ const loadedSearchRef = useRef<string | undefined>(undefined);
155
+ useEffect(() => {
156
+ if (!fetchDialogs) return;
157
+ if (!active) return;
158
+ if (initialDialogsLoadedRef.current && loadedSearchRef.current === search) return;
159
+ initialDialogsLoadedRef.current = true;
160
+ loadedSearchRef.current = search;
161
+ void loadDialogsPage();
162
+ }, [active, fetchDialogs, search, loadDialogsPage]);
163
+
164
+ const removeLocal = useCallback((id: string) => {
165
+ setDialogs(prev => prev.filter(d => d.id !== id));
166
+ }, []);
167
+
168
+ const deleteDialog = useCallback(
169
+ async (id: string): Promise<void> => {
170
+ if (!deleteDialogCallback) return;
171
+ try {
172
+ await deleteDialogCallback(id);
173
+ removeLocal(id);
174
+ onDialogRemovedRef.current?.(id, 'deleted');
175
+ } catch (err) {
176
+ console.error(`${logTag} deleteDialog failed:`, err);
177
+ }
178
+ },
179
+ [deleteDialogCallback, removeLocal, logTag],
180
+ );
181
+
182
+ const renameDialog = useCallback(
183
+ async (id: string, title: string): Promise<void> => {
184
+ if (!renameDialogCallback) return;
185
+ // Optimistic — update the local title immediately, roll back on error.
186
+ const previous = dialogsRef.current.find(d => d.id === id)?.title;
187
+ setDialogs(prev => prev.map(d => (d.id === id ? { ...d, title } : d)));
188
+ try {
189
+ await renameDialogCallback(id, title);
190
+ } catch (err) {
191
+ console.error(`${logTag} renameDialog failed:`, err);
192
+ if (previous !== undefined) {
193
+ setDialogs(prev => prev.map(d => (d.id === id ? { ...d, title: previous } : d)));
194
+ }
195
+ }
196
+ },
197
+ [renameDialogCallback, logTag],
198
+ );
199
+
200
+ const archiveDialog = useCallback(
201
+ async (id: string): Promise<void> => {
202
+ if (!archiveDialogCallback) return;
203
+ try {
204
+ await archiveDialogCallback(id);
205
+ removeLocal(id);
206
+ onDialogRemovedRef.current?.(id, 'archived');
207
+ } catch (err) {
208
+ // Logged and swallowed — parity with the pre-extraction NATS adapter
209
+ // (the row stays; the host sees the failure in the console).
210
+ console.error(`${logTag} archiveDialog failed:`, err);
211
+ }
212
+ },
213
+ [archiveDialogCallback, removeLocal, logTag],
214
+ );
215
+
216
+ const loadMoreDialogs = useCallback(async (): Promise<void> => {
217
+ if (!dialogsNextCursor) return;
218
+ await loadDialogsPage(dialogsNextCursor);
219
+ }, [dialogsNextCursor, loadDialogsPage]);
220
+
221
+ const upsertDialogTop = useCallback((item: DialogItem) => {
222
+ setDialogs(prev => [item, ...prev.filter(d => d.id !== item.id)]);
223
+ }, []);
224
+
225
+ const patchDialog = useCallback((id: string, patch: Partial<DialogItem>) => {
226
+ setDialogs(prev => prev.map(d => (d.id === id ? { ...d, ...patch } : d)));
227
+ }, []);
228
+
229
+ const hasMoreDialogs = dialogsNextCursor != null;
230
+
231
+ return useMemo<UseManagedDialogListResult>(
232
+ () => ({
233
+ dialogs,
234
+ dialogsNextCursor,
235
+ isDialogsLoading,
236
+ dialogsError,
237
+ hasMoreDialogs,
238
+ loadDialogsPage,
239
+ reloadDialogs,
240
+ loadMoreDialogs,
241
+ renameDialog,
242
+ archiveDialog,
243
+ deleteDialog,
244
+ upsertDialogTop,
245
+ patchDialog,
246
+ }),
247
+ [
248
+ dialogs,
249
+ dialogsNextCursor,
250
+ isDialogsLoading,
251
+ dialogsError,
252
+ hasMoreDialogs,
253
+ loadDialogsPage,
254
+ reloadDialogs,
255
+ loadMoreDialogs,
256
+ renameDialog,
257
+ archiveDialog,
258
+ deleteDialog,
259
+ upsertDialogTop,
260
+ patchDialog,
261
+ ],
262
+ );
263
+ }
@@ -62,10 +62,12 @@ import type {
62
62
  NatsMessageType,
63
63
  ChatApprovalStatus,
64
64
  } from '../types';
65
- import type { DialogItem } from '../types/component.types';
66
65
  import type {
67
66
  ChatConnectionState,
67
+ ChatDialogCapabilities,
68
68
  DialogTokenUsage,
69
+ FetchDialogsParams,
70
+ FetchDialogsResult,
69
71
  UnifiedChatState,
70
72
  UnifiedChatMessage,
71
73
  UnifiedSendMessageOptions,
@@ -74,6 +76,7 @@ import { buildDiscussPrompt } from '../utils/discuss-ref-prompt';
74
76
  import { extractIncompleteTailState } from '../utils/extract-incomplete-message-state';
75
77
  import { processHistoricalMessagesWithErrors } from '../utils/process-historical-messages';
76
78
  import { useChunkCatchup } from './use-chunk-catchup';
79
+ import { useManagedDialogList } from './use-managed-dialog-list';
77
80
  import { useNatsDialogSubscription } from './use-nats-dialog-subscription';
78
81
 
79
82
  // Legacy public home of the pure message-mutation helpers ("exported for
@@ -90,21 +93,11 @@ export {
90
93
  // Config + options
91
94
  // =============================================================================
92
95
 
93
- /** Page-fetch parameters passed to `fetchDialogs`. The adapter owns the
94
- * cursor — the host only resolves it against the backend. */
95
- export interface FetchDialogsParams {
96
- cursor?: string;
97
- limit?: number;
98
- search?: string;
99
- }
100
-
101
- /** Successful `fetchDialogs` response. `nextCursor: null` means "no more
102
- * pages" — used to terminate the infinite-scroll observer in the
103
- * sidebar. */
104
- export interface FetchDialogsResult {
105
- dialogs: DialogItem[];
106
- nextCursor: string | null;
107
- }
96
+ // `FetchDialogsParams` / `FetchDialogsResult` moved to the unified types
97
+ // module (they are the host contract of the SHARED `useManagedDialogList`
98
+ // hook, consumed by both adapters); re-exported here so the public import
99
+ // path stays stable.
100
+ export type { FetchDialogsParams, FetchDialogsResult } from '../types/unified-chat-state.types';
108
101
 
109
102
  /** Page-fetch parameters passed to `fetchDialogMessages`. */
110
103
  export interface FetchDialogMessagesParams {
@@ -422,6 +415,8 @@ export function useNatsChatAdapter(
422
415
  deleteDialog: deleteDialogCallback,
423
416
  renameDialog: renameDialogCallback,
424
417
  archiveDialog: archiveDialogCallback,
418
+ fetchArchivedDialogs,
419
+ unarchiveDialog,
425
420
  approveRequest: approveRequestCallback,
426
421
  rejectRequest: rejectRequestCallback,
427
422
  stopGeneration: stopGenerationCallback,
@@ -570,12 +565,37 @@ export function useNatsChatAdapter(
570
565
  );
571
566
 
572
567
  // ─── Dialog list state (managed-dialog mode only) ─────────────────────────
568
+ // The list state machine itself is the SHARED `useManagedDialogList`
569
+ // (also behind the SSE/Guide adapter); this adapter only layers the
570
+ // active-id + reducer-store side effects on top via `onDialogRemoved`.
573
571
 
574
- const [dialogs, setDialogs] = useState<DialogItem[]>([]);
575
- const [dialogsNextCursor, setDialogsNextCursor] = useState<string | null>(null);
576
- const [isDialogsLoading, setIsDialogsLoading] = useState<boolean>(false);
577
- const [dialogsError, setDialogsError] = useState<boolean>(false);
578
572
  const [isCreatingDialog, setIsCreatingDialog] = useState<boolean>(false);
573
+ const {
574
+ dialogs,
575
+ isDialogsLoading,
576
+ dialogsError,
577
+ hasMoreDialogs,
578
+ reloadDialogs,
579
+ loadMoreDialogs,
580
+ renameDialog,
581
+ archiveDialog,
582
+ deleteDialog,
583
+ upsertDialogTop,
584
+ } = useManagedDialogList({
585
+ active,
586
+ fetchDialogs,
587
+ renameDialog: renameDialogCallback,
588
+ archiveDialog: archiveDialogCallback,
589
+ deleteDialog: deleteDialogCallback,
590
+ pageSize: dialogsPageSize,
591
+ logTag: '[useNatsChatAdapter]',
592
+ onDialogRemoved: (id, reason) => {
593
+ if (reason === 'deleted') dialogStore.remove(id);
594
+ if (isManagedMode && internalDialogId === id) {
595
+ setInternalDialogId(null);
596
+ }
597
+ },
598
+ });
579
599
 
580
600
  // ─── Message-history pagination ───────────────────────────────────────────
581
601
 
@@ -861,59 +881,6 @@ export function useNatsChatAdapter(
861
881
  const connectionState: ChatConnectionState =
862
882
  !active || !dialogId ? 'connected' : connectedForDialog === dialogId ? 'connected' : 'connecting';
863
883
 
864
- // ─── Dialog list management (managed-dialog mode) ─────────────────────────
865
-
866
- // Forward-declared so `loadDialogsPage` can reset the initial-load guard on
867
- // a first-page failure (enabling retry / auto-retry on re-activation).
868
- const initialDialogsLoadedRef = useRef(false);
869
-
870
- const loadDialogsPage = useCallback(
871
- async (cursor?: string): Promise<void> => {
872
- if (!fetchDialogs) return;
873
- setIsDialogsLoading(true);
874
- // Clear a prior first-page error when (re)loading the first page.
875
- if (cursor === undefined) setDialogsError(false);
876
- try {
877
- const result = await fetchDialogs({
878
- cursor,
879
- limit: dialogsPageSize,
880
- });
881
- setDialogsNextCursor(result.nextCursor);
882
- if (cursor === undefined) {
883
- setDialogs(result.dialogs);
884
- } else {
885
- setDialogs(prev => [...prev, ...result.dialogs]);
886
- }
887
- } catch (err) {
888
- console.error('[useNatsChatAdapter] fetchDialogs failed:', err);
889
- // Only the FIRST page failing is a "can't show the list" error — a
890
- // pagination failure keeps the already-loaded list intact. Flag it and
891
- // release the initial-load guard so a retry (or re-activation) re-runs.
892
- if (cursor === undefined) {
893
- setDialogsError(true);
894
- initialDialogsLoadedRef.current = false;
895
- }
896
- } finally {
897
- setIsDialogsLoading(false);
898
- }
899
- },
900
- [fetchDialogs, dialogsPageSize],
901
- );
902
-
903
- // Retry the initial dialog-list load after a failure.
904
- const reloadDialogs = useCallback(() => {
905
- void loadDialogsPage();
906
- }, [loadDialogsPage]);
907
-
908
- // Initial dialog list load.
909
- useEffect(() => {
910
- if (!fetchDialogs) return;
911
- if (!active) return;
912
- if (initialDialogsLoadedRef.current) return;
913
- initialDialogsLoadedRef.current = true;
914
- void loadDialogsPage();
915
- }, [active, fetchDialogs, loadDialogsPage]);
916
-
917
884
  // ─── Public action handlers ───────────────────────────────────────────────
918
885
 
919
886
  const sendMessage = useCallback(
@@ -968,14 +935,11 @@ export function useNatsChatAdapter(
968
935
  // Optimistically prepend a placeholder dialog so the sidebar shows
969
936
  // the new conversation immediately. The next list refresh will
970
937
  // replace it with the canonical entry.
971
- setDialogs(prev => [
972
- {
973
- id: result.dialogId,
974
- title: 'New Chat',
975
- timestamp: new Date(),
976
- },
977
- ...prev.filter(d => d.id !== result.dialogId),
978
- ]);
938
+ upsertDialogTop({
939
+ id: result.dialogId,
940
+ title: 'New Chat',
941
+ timestamp: new Date(),
942
+ });
979
943
  if (isManagedMode) {
980
944
  setInternalDialogId(result.dialogId);
981
945
  }
@@ -986,74 +950,7 @@ export function useNatsChatAdapter(
986
950
  } finally {
987
951
  setIsCreatingDialog(false);
988
952
  }
989
- }, [createDialogCallback, isCreatingDialog, isManagedMode]);
990
-
991
- const deleteDialog = useCallback(
992
- async (id: string): Promise<void> => {
993
- if (!deleteDialogCallback) return;
994
- try {
995
- await deleteDialogCallback(id);
996
- setDialogs(prev => prev.filter(d => d.id !== id));
997
- dialogStore.remove(id);
998
- if (isManagedMode && internalDialogId === id) {
999
- setInternalDialogId(null);
1000
- }
1001
- } catch (err) {
1002
- console.error('[useNatsChatAdapter] deleteDialog failed:', err);
1003
- }
1004
- },
1005
- [deleteDialogCallback, dialogStore, internalDialogId, isManagedMode],
1006
- );
1007
-
1008
- const renameDialog = useCallback(
1009
- async (id: string, title: string): Promise<void> => {
1010
- if (!renameDialogCallback) return;
1011
- // Optimistic — update the local title immediately, roll back on error.
1012
- let previous: string | undefined;
1013
- setDialogs(prev =>
1014
- prev.map(d => {
1015
- if (d.id !== id) return d;
1016
- previous = d.title;
1017
- return { ...d, title };
1018
- }),
1019
- );
1020
- try {
1021
- await renameDialogCallback(id, title);
1022
- } catch (err) {
1023
- console.error('[useNatsChatAdapter] renameDialog failed:', err);
1024
- // `previous` is a `let` written from inside the optimistic updater, so
1025
- // its narrowing does not reach the rollback closure. Freeze it into a
1026
- // const first — that is also what pins the rollback to the title we
1027
- // actually replaced.
1028
- const rollbackTitle = previous;
1029
- if (rollbackTitle !== undefined) {
1030
- setDialogs(prev => prev.map(d => (d.id === id ? { ...d, title: rollbackTitle } : d)));
1031
- }
1032
- }
1033
- },
1034
- [renameDialogCallback],
1035
- );
1036
-
1037
- const archiveDialog = useCallback(
1038
- async (id: string): Promise<void> => {
1039
- if (!archiveDialogCallback) return;
1040
- try {
1041
- await archiveDialogCallback(id);
1042
- setDialogs(prev => prev.filter(d => d.id !== id));
1043
- if (isManagedMode && internalDialogId === id) {
1044
- setInternalDialogId(null);
1045
- }
1046
- } catch (err) {
1047
- console.error('[useNatsChatAdapter] archiveDialog failed:', err);
1048
- }
1049
- },
1050
- [archiveDialogCallback, internalDialogId, isManagedMode],
1051
- );
1052
-
1053
- const loadMoreDialogs = useCallback(async (): Promise<void> => {
1054
- if (!dialogsNextCursor) return;
1055
- await loadDialogsPage(dialogsNextCursor);
1056
- }, [dialogsNextCursor, loadDialogsPage]);
953
+ }, [createDialogCallback, isCreatingDialog, isManagedMode, upsertDialogTop]);
1057
954
 
1058
955
  const loadMoreMessages = useCallback(async (): Promise<void> => {
1059
956
  if (!dialogId || !messagesNextCursor) return;
@@ -1102,9 +999,23 @@ export function useNatsChatAdapter(
1102
999
 
1103
1000
  const { messages, streamingPhase, dialogTokenUsage = null, liveModel = null } = state;
1104
1001
  const isLoading = streamingPhase !== 'idle';
1105
- const hasMoreDialogs = dialogsNextCursor != null;
1106
1002
  const hasMoreMessages = messagesNextCursor != null;
1107
1003
 
1004
+ // Capabilities for `EmbeddableChat` — the presence of a host callback IS the
1005
+ // capability (same rule the legacy per-mode derivation applied).
1006
+ const dialogCapabilities = useMemo<ChatDialogCapabilities | undefined>(
1007
+ () =>
1008
+ isManagedMode
1009
+ ? {
1010
+ canRename: !!renameDialogCallback,
1011
+ canArchive: !!archiveDialogCallback,
1012
+ fetchArchivedDialogs,
1013
+ unarchiveDialog,
1014
+ }
1015
+ : undefined,
1016
+ [isManagedMode, renameDialogCallback, archiveDialogCallback, fetchArchivedDialogs, unarchiveDialog],
1017
+ );
1018
+
1108
1019
  return useMemo<UnifiedChatState>(
1109
1020
  () => ({
1110
1021
  messages,
@@ -1142,6 +1053,8 @@ export function useNatsChatAdapter(
1142
1053
  loadMoreDialogs,
1143
1054
  hasMoreMessages,
1144
1055
  loadMoreMessages,
1056
+ dialogsManaged: isManagedMode,
1057
+ dialogCapabilities,
1145
1058
  // Approval mutations
1146
1059
  approveRequest,
1147
1060
  rejectRequest,
@@ -1174,6 +1087,8 @@ export function useNatsChatAdapter(
1174
1087
  loadMoreDialogs,
1175
1088
  hasMoreMessages,
1176
1089
  loadMoreMessages,
1090
+ isManagedMode,
1091
+ dialogCapabilities,
1177
1092
  approveRequest,
1178
1093
  rejectRequest,
1179
1094
  dialogTokenUsage,