@flamingo-stack/openframe-frontend-core 0.0.598-2039.5713.1 → 0.0.598
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/chat-protocol/index.cjs +1 -8
- package/dist/chat-protocol/index.cjs.map +1 -1
- package/dist/chat-protocol/index.d.ts +0 -1
- package/dist/chat-protocol/index.d.ts.map +1 -1
- package/dist/chat-protocol/index.js +2 -7
- package/dist/chat-protocol/index.js.map +1 -1
- package/dist/chat-protocol/nats-decoder.d.ts.map +1 -1
- package/dist/{chunk-TSCIVH7T.js → chunk-2JNCZDJR.js} +2 -2
- package/dist/{chunk-6NSGAH5H.cjs → chunk-2SYX2Z7R.cjs} +34 -34
- package/dist/{chunk-6NSGAH5H.cjs.map → chunk-2SYX2Z7R.cjs.map} +1 -1
- package/dist/{chunk-6ZHH37RS.cjs → chunk-3WSS7C2V.cjs} +15 -15
- package/dist/{chunk-6ZHH37RS.cjs.map → chunk-3WSS7C2V.cjs.map} +1 -1
- package/dist/{chunk-5L4ERWSW.js → chunk-75YW6ET4.js} +4 -4
- package/dist/{chunk-VKCETV3C.js → chunk-BK6CCQG6.js} +12 -12
- package/dist/{chunk-HM25MFZX.cjs → chunk-BPVHDDNY.cjs} +8 -8
- package/dist/{chunk-HM25MFZX.cjs.map → chunk-BPVHDDNY.cjs.map} +1 -1
- package/dist/{chunk-VIBREYUK.cjs → chunk-BZEWPW5A.cjs} +3 -3
- package/dist/{chunk-VIBREYUK.cjs.map → chunk-BZEWPW5A.cjs.map} +1 -1
- package/dist/{chunk-KYWU6F3O.cjs → chunk-DAPOBLAK.cjs} +39 -39
- package/dist/{chunk-KYWU6F3O.cjs.map → chunk-DAPOBLAK.cjs.map} +1 -1
- package/dist/{chunk-DHCA5BZP.cjs → chunk-DI3SVVY5.cjs} +85 -85
- package/dist/{chunk-DHCA5BZP.cjs.map → chunk-DI3SVVY5.cjs.map} +1 -1
- package/dist/{chunk-BLUL2TE3.js → chunk-EVMHJB6X.js} +4 -4
- package/dist/{chunk-S6WBBSII.js → chunk-EZM5DNII.js} +3 -3
- package/dist/{chunk-BVQEU5JB.js → chunk-GGQZBSFN.js} +2 -2
- package/dist/{chunk-KTX5B2FW.js → chunk-GHYFVT52.js} +439 -414
- package/dist/chunk-GHYFVT52.js.map +1 -0
- package/dist/{chunk-CCWI3622.cjs → chunk-H64ZHSYR.cjs} +4 -4
- package/dist/{chunk-CCWI3622.cjs.map → chunk-H64ZHSYR.cjs.map} +1 -1
- package/dist/{chunk-4T42D6YG.js → chunk-HLPAVYK7.js} +4 -4
- package/dist/{chunk-HFHBA3VV.cjs → chunk-HWAYIK4U.cjs} +22 -22
- package/dist/{chunk-HFHBA3VV.cjs.map → chunk-HWAYIK4U.cjs.map} +1 -1
- package/dist/{chunk-CF4QJPTN.cjs → chunk-JVZVVZOZ.cjs} +9 -9
- package/dist/{chunk-CF4QJPTN.cjs.map → chunk-JVZVVZOZ.cjs.map} +1 -1
- package/dist/{chunk-XV7RFACE.js → chunk-KHOZ52NR.js} +6 -6
- package/dist/{chunk-IW746HJ7.js → chunk-LRQCKSZC.js} +2 -2
- package/dist/{chunk-JJUV7Z4L.cjs → chunk-M4YQUHOR.cjs} +15 -15
- package/dist/{chunk-JJUV7Z4L.cjs.map → chunk-M4YQUHOR.cjs.map} +1 -1
- package/dist/{chunk-FKBM6U43.js → chunk-MYBXCWEI.js} +2 -2
- package/dist/{chunk-QTPHBCPI.js → chunk-NIAMPMKV.js} +4 -4
- package/dist/{chunk-UXOOVP77.cjs → chunk-O4VZOA2Q.cjs} +3 -3
- package/dist/{chunk-UXOOVP77.cjs.map → chunk-O4VZOA2Q.cjs.map} +1 -1
- package/dist/{chunk-ABNBTPI2.cjs → chunk-P24VPJTT.cjs} +15 -15
- package/dist/{chunk-ABNBTPI2.cjs.map → chunk-P24VPJTT.cjs.map} +1 -1
- package/dist/{chunk-4XPPQDEM.js → chunk-PTK3CINU.js} +5 -5
- package/dist/{chunk-7FUIRGMU.js → chunk-PX4IPM7Q.js} +3 -3
- package/dist/{chunk-NLT5R43J.cjs → chunk-QD64Q4I2.cjs} +9 -9
- package/dist/{chunk-NLT5R43J.cjs.map → chunk-QD64Q4I2.cjs.map} +1 -1
- package/dist/{chunk-TEVP4KNG.cjs → chunk-QIKBISZM.cjs} +522 -497
- package/dist/chunk-QIKBISZM.cjs.map +1 -0
- package/dist/{chunk-FGNUI4CC.js → chunk-R4UV366K.js} +6 -6
- package/dist/{chunk-UPKZU6QU.cjs → chunk-RDTOMWDN.cjs} +1 -1
- package/dist/chunk-RDTOMWDN.cjs.map +1 -0
- package/dist/{chunk-YB6FI2G3.js → chunk-RYXIRVDC.js} +1 -1
- package/dist/chunk-RYXIRVDC.js.map +1 -0
- package/dist/{chunk-QPAPUIF3.cjs → chunk-SAEJGTCI.cjs} +35 -35
- package/dist/{chunk-QPAPUIF3.cjs.map → chunk-SAEJGTCI.cjs.map} +1 -1
- package/dist/{chunk-66HTVTZZ.js → chunk-SE3PVN64.js} +2 -2
- package/dist/{chunk-3IXDZI3R.cjs → chunk-STYNZ4QM.cjs} +3 -3
- package/dist/{chunk-3IXDZI3R.cjs.map → chunk-STYNZ4QM.cjs.map} +1 -1
- package/dist/{chunk-CQRYXSHL.js → chunk-TZPVRWPU.js} +5 -5
- package/dist/{chunk-RU3IKVY2.js → chunk-UAAXUFK6.js} +3 -3
- package/dist/{chunk-F5NEZLTG.cjs → chunk-UE7OFLLS.cjs} +14 -14
- package/dist/{chunk-F5NEZLTG.cjs.map → chunk-UE7OFLLS.cjs.map} +1 -1
- package/dist/{chunk-VUR24QYH.cjs → chunk-UG7JNQZY.cjs} +37 -37
- package/dist/{chunk-VUR24QYH.cjs.map → chunk-UG7JNQZY.cjs.map} +1 -1
- package/dist/{chunk-UQSOFWXP.cjs → chunk-V2QK5W3O.cjs} +3 -3
- package/dist/{chunk-UQSOFWXP.cjs.map → chunk-V2QK5W3O.cjs.map} +1 -1
- package/dist/{chunk-ZABAXD4U.js → chunk-VIRTMRS4.js} +7 -7
- package/dist/{chunk-D565OOXC.cjs → chunk-VLJTJAHJ.cjs} +25 -25
- package/dist/{chunk-D565OOXC.cjs.map → chunk-VLJTJAHJ.cjs.map} +1 -1
- package/dist/{chunk-K5QV7ZAY.js → chunk-VNSRNVPD.js} +437 -756
- package/dist/chunk-VNSRNVPD.js.map +1 -0
- package/dist/{chunk-UQEOAPAI.cjs → chunk-WCBRZCU4.cjs} +9 -9
- package/dist/{chunk-UQEOAPAI.cjs.map → chunk-WCBRZCU4.cjs.map} +1 -1
- package/dist/{chunk-3XFDRL32.js → chunk-WDWCQD75.js} +9 -9
- package/dist/{chunk-QT35C6CW.js → chunk-WPN545FW.js} +12 -12
- package/dist/{chunk-F2KBCLUG.js → chunk-WZJOILPU.js} +2 -2
- package/dist/{chunk-KAADFO74.js → chunk-XNPAVMPB.js} +5 -5
- package/dist/{chunk-5PPYXXUO.cjs → chunk-YBPFIXV4.cjs} +522 -841
- package/dist/chunk-YBPFIXV4.cjs.map +1 -0
- package/dist/{chunk-EHAYNUOL.cjs → chunk-YH6IQTPI.cjs} +3 -3
- package/dist/{chunk-EHAYNUOL.cjs.map → chunk-YH6IQTPI.cjs.map} +1 -1
- package/dist/{chunk-AVAENPVE.cjs → chunk-YWBAIFI2.cjs} +105 -105
- package/dist/{chunk-AVAENPVE.cjs.map → chunk-YWBAIFI2.cjs.map} +1 -1
- package/dist/components/case-studies/index.cjs +14 -14
- package/dist/components/case-studies/index.js +8 -8
- package/dist/components/chat/embeddable-chat.d.ts +19 -10
- package/dist/components/chat/embeddable-chat.d.ts.map +1 -1
- package/dist/components/chat/hooks/index.d.ts +0 -1
- package/dist/components/chat/hooks/index.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-chat-dialog-manager.d.ts +2 -2
- package/dist/components/chat/hooks/use-chat-dialog-manager.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-chat-history-hydration.d.ts +3 -13
- package/dist/components/chat/hooks/use-chat-history-hydration.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +16 -2
- package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-unified-chat.d.ts.map +1 -1
- package/dist/components/chat/index.cjs +5 -9
- package/dist/components/chat/index.cjs.map +1 -1
- package/dist/components/chat/index.js +4 -8
- package/dist/components/chat/types/unified-chat-state.types.d.ts +6 -67
- package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
- package/dist/components/chat/utils/index.d.ts +0 -1
- package/dist/components/chat/utils/index.d.ts.map +1 -1
- package/dist/components/contact/index.cjs +9 -9
- package/dist/components/contact/index.js +8 -8
- package/dist/components/docs/index.cjs +12 -12
- package/dist/components/docs/index.js +11 -11
- package/dist/components/embeds/index.cjs +9 -9
- package/dist/components/embeds/index.js +8 -8
- package/dist/components/faq/index.cjs +10 -10
- package/dist/components/faq/index.js +9 -9
- package/dist/components/features/index.cjs +8 -8
- package/dist/components/features/index.js +7 -7
- package/dist/components/help-center-pages/index.cjs +31 -31
- package/dist/components/help-center-pages/index.js +22 -22
- package/dist/components/index.cjs +66 -70
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.js +22 -26
- package/dist/components/index.js.map +1 -1
- package/dist/components/meeting-scheduler/index.cjs +49 -49
- package/dist/components/meeting-scheduler/index.js +9 -9
- package/dist/components/navigation/index.cjs +11 -11
- package/dist/components/navigation/index.js +10 -10
- package/dist/components/onboarding-guides/index.cjs +12 -12
- package/dist/components/onboarding-guides/index.js +11 -11
- package/dist/components/related-content/index.cjs +7 -7
- package/dist/components/related-content/index.js +6 -6
- package/dist/components/tickets/index.cjs +13 -13
- package/dist/components/tickets/index.js +12 -12
- package/dist/components/ui/data-table/data-table-body.d.ts.map +1 -1
- package/dist/components/ui/data-table/data-table-skeleton.d.ts +31 -0
- package/dist/components/ui/data-table/data-table-skeleton.d.ts.map +1 -1
- package/dist/components/ui/index.cjs +8 -12
- package/dist/components/ui/index.cjs.map +1 -1
- package/dist/components/ui/index.js +7 -11
- package/dist/components/ui/table/table-empty-state.d.ts +1 -1
- package/dist/components/ui/table/table-empty-state.d.ts.map +1 -1
- package/dist/components/ui/table/table-skeleton.d.ts +1 -14
- package/dist/components/ui/table/table-skeleton.d.ts.map +1 -1
- package/dist/components/ui/table/table.d.ts +1 -1
- package/dist/components/ui/table/table.d.ts.map +1 -1
- package/dist/components/ui/table/types.d.ts +33 -6
- package/dist/components/ui/table/types.d.ts.map +1 -1
- package/dist/contexts/chat-runtime-context.d.ts +0 -12
- package/dist/contexts/chat-runtime-context.d.ts.map +1 -1
- package/dist/contexts/index.cjs +2 -2
- package/dist/contexts/index.js +1 -1
- package/dist/hooks/index.cjs +5 -5
- package/dist/hooks/index.js +4 -4
- package/dist/index.cjs +12 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +11 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/chat-protocol/index.ts +0 -1
- package/src/chat-protocol/nats-decoder.ts +5 -1
- package/src/components/chat/.embeddable-chat.md +2 -4
- package/src/components/chat/embeddable-chat.tsx +84 -131
- package/src/components/chat/hooks/.use-chat-dialog-manager.md +1 -3
- package/src/components/chat/hooks/.use-nats-chat-adapter.md +1 -3
- package/src/components/chat/hooks/.use-sse-chat-adapter.md +1 -8
- package/src/components/chat/hooks/.use-unified-chat.md +2 -2
- package/src/components/chat/hooks/index.ts +0 -2
- package/src/components/chat/hooks/use-chat-dialog-manager.ts +65 -38
- package/src/components/chat/hooks/use-chat-history-hydration.ts +24 -67
- package/src/components/chat/hooks/use-nats-chat-adapter.ts +150 -65
- package/src/components/chat/hooks/use-sse-chat-adapter.ts +59 -233
- package/src/components/chat/hooks/use-unified-chat.ts +0 -5
- package/src/components/chat/types/.unified-chat-state.types.md +1 -13
- package/src/components/chat/types/unified-chat-state.types.ts +6 -74
- package/src/components/chat/utils/index.ts +0 -7
- package/src/components/ui/data-table/data-table-body.tsx +8 -49
- package/src/components/ui/data-table/data-table-skeleton.tsx +100 -0
- package/src/components/ui/table/table-empty-state.tsx +5 -1
- package/src/components/ui/table/table-skeleton.tsx +14 -40
- package/src/components/ui/table/table.tsx +34 -15
- package/src/components/ui/table/types.ts +33 -6
- package/src/contexts/.chat-runtime-context.md +1 -3
- package/src/contexts/chat-runtime-context.tsx +0 -12
- package/dist/chat-protocol/wire-narrow.d.ts +0 -17
- package/dist/chat-protocol/wire-narrow.d.ts.map +0 -1
- package/dist/chunk-5PPYXXUO.cjs.map +0 -1
- package/dist/chunk-K5QV7ZAY.js.map +0 -1
- package/dist/chunk-KTX5B2FW.js.map +0 -1
- package/dist/chunk-TEVP4KNG.cjs.map +0 -1
- package/dist/chunk-UPKZU6QU.cjs.map +0 -1
- package/dist/chunk-YB6FI2G3.js.map +0 -1
- package/dist/components/chat/hooks/use-managed-dialog-list.d.ts +0 -68
- package/dist/components/chat/hooks/use-managed-dialog-list.d.ts.map +0 -1
- package/dist/components/chat/utils/chat-conversations-api.d.ts +0 -29
- package/dist/components/chat/utils/chat-conversations-api.d.ts.map +0 -1
- package/src/chat-protocol/wire-narrow.ts +0 -22
- package/src/components/chat/hooks/.use-managed-dialog-list.md +0 -31
- package/src/components/chat/hooks/__tests__/managed-dialog-list.test.ts +0 -280
- package/src/components/chat/hooks/__tests__/nats-adapter-managed-dialogs.test.ts +0 -172
- package/src/components/chat/hooks/__tests__/sse-managed-dialogs.test.ts +0 -310
- package/src/components/chat/hooks/use-managed-dialog-list.ts +0 -371
- package/src/components/chat/utils/.chat-conversations-api.md +0 -16
- package/src/components/chat/utils/__tests__/chat-conversations-api-golden.test.ts +0 -105
- package/src/components/chat/utils/chat-conversations-api.ts +0 -83
- /package/dist/{chunk-TSCIVH7T.js.map → chunk-2JNCZDJR.js.map} +0 -0
- /package/dist/{chunk-5L4ERWSW.js.map → chunk-75YW6ET4.js.map} +0 -0
- /package/dist/{chunk-VKCETV3C.js.map → chunk-BK6CCQG6.js.map} +0 -0
- /package/dist/{chunk-BLUL2TE3.js.map → chunk-EVMHJB6X.js.map} +0 -0
- /package/dist/{chunk-S6WBBSII.js.map → chunk-EZM5DNII.js.map} +0 -0
- /package/dist/{chunk-BVQEU5JB.js.map → chunk-GGQZBSFN.js.map} +0 -0
- /package/dist/{chunk-4T42D6YG.js.map → chunk-HLPAVYK7.js.map} +0 -0
- /package/dist/{chunk-XV7RFACE.js.map → chunk-KHOZ52NR.js.map} +0 -0
- /package/dist/{chunk-IW746HJ7.js.map → chunk-LRQCKSZC.js.map} +0 -0
- /package/dist/{chunk-FKBM6U43.js.map → chunk-MYBXCWEI.js.map} +0 -0
- /package/dist/{chunk-QTPHBCPI.js.map → chunk-NIAMPMKV.js.map} +0 -0
- /package/dist/{chunk-4XPPQDEM.js.map → chunk-PTK3CINU.js.map} +0 -0
- /package/dist/{chunk-7FUIRGMU.js.map → chunk-PX4IPM7Q.js.map} +0 -0
- /package/dist/{chunk-FGNUI4CC.js.map → chunk-R4UV366K.js.map} +0 -0
- /package/dist/{chunk-66HTVTZZ.js.map → chunk-SE3PVN64.js.map} +0 -0
- /package/dist/{chunk-CQRYXSHL.js.map → chunk-TZPVRWPU.js.map} +0 -0
- /package/dist/{chunk-RU3IKVY2.js.map → chunk-UAAXUFK6.js.map} +0 -0
- /package/dist/{chunk-ZABAXD4U.js.map → chunk-VIRTMRS4.js.map} +0 -0
- /package/dist/{chunk-3XFDRL32.js.map → chunk-WDWCQD75.js.map} +0 -0
- /package/dist/{chunk-QT35C6CW.js.map → chunk-WPN545FW.js.map} +0 -0
- /package/dist/{chunk-F2KBCLUG.js.map → chunk-WZJOILPU.js.map} +0 -0
- /package/dist/{chunk-KAADFO74.js.map → chunk-XNPAVMPB.js.map} +0 -0
|
@@ -18,11 +18,16 @@
|
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
import { useEffect, useRef, useState, type MutableRefObject } from 'react';
|
|
21
|
-
import { isRecord, unwrapEnvelope } from '../../../chat-protocol/wire-narrow';
|
|
22
21
|
import { embedAuthedFetch } from '../../../utils/embed-authed-fetch';
|
|
23
22
|
import { AUTO_CONTINUATION_DIRECTIVE_PREFIX } from '../utils/auto-continuation-directive';
|
|
24
23
|
import type { Message } from './use-chat';
|
|
25
24
|
|
|
25
|
+
/** Narrow one hop of an untrusted JSON body to something with readable keys.
|
|
26
|
+
* `typeof null === 'object'`, so the null check is the whole point. */
|
|
27
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
28
|
+
return typeof value === 'object' && value !== null;
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
export interface UseChatHistoryHydrationArgs {
|
|
27
32
|
/** Mirrors the adapter's `active` gate — an idle (Mingo-mode) mount never fetches. */
|
|
28
33
|
active: boolean;
|
|
@@ -30,10 +35,8 @@ export interface UseChatHistoryHydrationArgs {
|
|
|
30
35
|
source: string;
|
|
31
36
|
/** Resolved history endpoint (`<chatStreamUrl>/history` by default). */
|
|
32
37
|
historyUrl: string;
|
|
33
|
-
/** The server-issued conversation id (null = nothing to hydrate).
|
|
34
|
-
|
|
35
|
-
* fetch, so the id is an effect dependency. */
|
|
36
|
-
conversationId: string | null;
|
|
38
|
+
/** The server-issued conversation id (null = nothing to hydrate). */
|
|
39
|
+
conversationIdRef: MutableRefObject<string | null>;
|
|
37
40
|
/** User-send counter — set to the hydrated user-turn count so the next
|
|
38
41
|
* live send lands on the following `sendIdx`. */
|
|
39
42
|
sendCountRef: MutableRefObject<number>;
|
|
@@ -46,14 +49,6 @@ export interface UseChatHistoryHydrationArgs {
|
|
|
46
49
|
) => void;
|
|
47
50
|
/** Invalidates the adapter's `latestMeta` memo after hydration lands. */
|
|
48
51
|
bumpMetaTick: () => void;
|
|
49
|
-
/** The server answered OK and reported the id as NOT READABLE by this caller
|
|
50
|
-
* (`found: false`) — it no longer exists, or is no longer ours (e.g. the
|
|
51
|
-
* thread was claimed by an account and this browser is now signed out).
|
|
52
|
-
* Distinct from a fetch FAILURE, which keeps the id so a transient outage
|
|
53
|
-
* never loses context, and from a legitimately EMPTY but owned thread, which
|
|
54
|
-
* also keeps it. A host that persists the id should drop it here — otherwise
|
|
55
|
-
* every later turn streams an answer the server refuses to record. */
|
|
56
|
-
onOrphanedConversation?: (conversationId: string) => void;
|
|
57
52
|
}
|
|
58
53
|
|
|
59
54
|
export interface UseChatHistoryHydrationResult {
|
|
@@ -69,34 +64,24 @@ export function useChatHistoryHydration({
|
|
|
69
64
|
active,
|
|
70
65
|
source,
|
|
71
66
|
historyUrl,
|
|
72
|
-
|
|
67
|
+
conversationIdRef,
|
|
73
68
|
sendCountRef,
|
|
74
69
|
hydrateMessages,
|
|
75
70
|
bumpMetaTick,
|
|
76
|
-
onOrphanedConversation,
|
|
77
71
|
}: UseChatHistoryHydrationArgs): UseChatHistoryHydrationResult {
|
|
78
72
|
const [isHydratingHistory, setIsHydratingHistory] = useState(false);
|
|
79
73
|
const hydratedKeyRef = useRef<string | null>(null);
|
|
80
|
-
// Monotonic run id: only the LATEST hydration owns the loading flag, so a
|
|
81
|
-
// slow superseded run resolving after a newer one started cannot clear the
|
|
82
|
-
// skeleton out from under it. (Same guard `useManagedDialogList` uses.)
|
|
83
|
-
const runIdRef = useRef(0);
|
|
84
74
|
|
|
85
75
|
useEffect(() => {
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// wedged on a message skeleton.
|
|
92
|
-
if (!active || !conversationId) return () => setIsHydratingHistory(false);
|
|
76
|
+
if (!active) return undefined;
|
|
77
|
+
// No stored conversation id → nothing to hydrate (fresh visitor / after
|
|
78
|
+
// "new chat"); the first send establishes the conversation server-side.
|
|
79
|
+
const conversationId = conversationIdRef.current;
|
|
80
|
+
if (!conversationId) return undefined;
|
|
93
81
|
const key = `${source}:${conversationId}`;
|
|
94
|
-
if (hydratedKeyRef.current === key) return
|
|
82
|
+
if (hydratedKeyRef.current === key) return undefined;
|
|
95
83
|
hydratedKeyRef.current = key;
|
|
96
84
|
let cancelled = false;
|
|
97
|
-
let completed = false;
|
|
98
|
-
const runId = ++runIdRef.current;
|
|
99
|
-
const isCurrentRun = () => runIdRef.current === runId;
|
|
100
85
|
setIsHydratingHistory(true);
|
|
101
86
|
// Self-contained: try/catch/finally below, and every state write is gated
|
|
102
87
|
// on `cancelled` so a superseded effect can't hydrate the new conversation.
|
|
@@ -105,19 +90,13 @@ export function useChatHistoryHydration({
|
|
|
105
90
|
const res = await embedAuthedFetch(`${historyUrl}?conversationId=${encodeURIComponent(conversationId)}`, {
|
|
106
91
|
method: 'GET',
|
|
107
92
|
});
|
|
108
|
-
if (!res.ok)
|
|
109
|
-
// Releases the once-guard exactly like the catch below — this path
|
|
110
|
-
// returns BEFORE it, and a transient 429/5xx must not mark the
|
|
111
|
-
// conversation hydrated for the rest of the mount. `isCurrentRun` so
|
|
112
|
-
// a superseded run can't release a key its replacement re-claimed
|
|
113
|
-
// (same key string, different run).
|
|
114
|
-
if (isCurrentRun() && hydratedKeyRef.current === key) hydratedKeyRef.current = null;
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
93
|
+
if (!res.ok) return;
|
|
117
94
|
// `.json()` is `any` — this is untrusted wire, so narrow it rather than
|
|
118
95
|
// asserting the shape we hope for.
|
|
119
96
|
const payload: unknown = await res.json().catch(() => null);
|
|
120
|
-
|
|
97
|
+
// route-base successResponse envelope ({ data }) with a raw-body fallback.
|
|
98
|
+
const envelope = isRecord(payload) ? payload.data : undefined;
|
|
99
|
+
const body = envelope ?? payload;
|
|
121
100
|
const rawRows = isRecord(body) ? body.messages : undefined;
|
|
122
101
|
// Non-object rows are DROPPED, not carried into the loop below. The
|
|
123
102
|
// loop reads `row.role` unguarded, so a single `null` row threw a
|
|
@@ -125,15 +104,7 @@ export function useChatHistoryHydration({
|
|
|
125
104
|
// hydration wholesale and rendering an EMPTY conversation on refresh.
|
|
126
105
|
// Skipping the bad row keeps every good one.
|
|
127
106
|
const rows: Array<Record<string, unknown>> = Array.isArray(rawRows) ? rawRows.filter(isRecord) : [];
|
|
128
|
-
if (cancelled) return;
|
|
129
|
-
if (rows.length === 0) {
|
|
130
|
-
// `found === false` = unknown or foreign id → the stored id is dead.
|
|
131
|
-
// An owned-but-empty thread (`found === true`, or a server that
|
|
132
|
-
// predates the flag) keeps it.
|
|
133
|
-
const found = isRecord(body) ? body.found : undefined;
|
|
134
|
-
if (found === false) onOrphanedConversation?.(conversationId);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
107
|
+
if (cancelled || rows.length === 0) return;
|
|
137
108
|
const hydrated: Message[] = [];
|
|
138
109
|
// Per-send "Sources used" chips, restored from the persisted audit
|
|
139
110
|
// copy the history route projects as `sources` on assistant rows —
|
|
@@ -168,30 +139,16 @@ export function useChatHistoryHydration({
|
|
|
168
139
|
bumpMetaTick();
|
|
169
140
|
} catch {
|
|
170
141
|
// Fetch failed — start empty; the server still owns history (above).
|
|
171
|
-
// Release the once-guard so re-selecting the conversation retries
|
|
172
|
-
// (a transient 429/5xx must not mark it hydrated for the mount).
|
|
173
|
-
if (isCurrentRun() && hydratedKeyRef.current === key) hydratedKeyRef.current = null;
|
|
174
142
|
} finally {
|
|
175
|
-
|
|
176
|
-
// Only the latest run owns the flag (React no-ops updates after unmount).
|
|
177
|
-
if (isCurrentRun()) setIsHydratingHistory(false);
|
|
143
|
+
if (!cancelled) setIsHydratingHistory(false);
|
|
178
144
|
}
|
|
179
145
|
})();
|
|
180
146
|
return () => {
|
|
181
147
|
cancelled = true;
|
|
182
|
-
// An INCOMPLETE run must hand the key back, or a re-run for the SAME key
|
|
183
|
-
// (a mode toggle flipping `active`, a StrictMode remount) early-returns
|
|
184
|
-
// on the guard, drops the in-flight response and never refetches — the
|
|
185
|
-
// thread would render empty for the rest of the mount while the server
|
|
186
|
-
// keeps answering from history the user cannot see.
|
|
187
|
-
if (!completed && isCurrentRun() && hydratedKeyRef.current === key) hydratedKeyRef.current = null;
|
|
188
|
-
// Release the skeleton unless a newer run has already claimed it (that
|
|
189
|
-
// run raises the flag itself and owns clearing it).
|
|
190
|
-
if (isCurrentRun()) setIsHydratingHistory(false);
|
|
191
148
|
};
|
|
192
|
-
// `sendCountRef`
|
|
193
|
-
//
|
|
194
|
-
}, [active, source, historyUrl,
|
|
149
|
+
// `conversationIdRef`/`sendCountRef` are refs: stable identities, so
|
|
150
|
+
// listing them costs nothing and the effect still keys on the real inputs.
|
|
151
|
+
}, [active, source, historyUrl, hydrateMessages, bumpMetaTick, conversationIdRef, sendCountRef]);
|
|
195
152
|
|
|
196
153
|
return { isHydratingHistory, hydratedKeyRef };
|
|
197
154
|
}
|
|
@@ -62,12 +62,10 @@ import type {
|
|
|
62
62
|
NatsMessageType,
|
|
63
63
|
ChatApprovalStatus,
|
|
64
64
|
} from '../types';
|
|
65
|
+
import type { DialogItem } from '../types/component.types';
|
|
65
66
|
import type {
|
|
66
67
|
ChatConnectionState,
|
|
67
|
-
ChatDialogCapabilities,
|
|
68
68
|
DialogTokenUsage,
|
|
69
|
-
FetchDialogsParams,
|
|
70
|
-
FetchDialogsResult,
|
|
71
69
|
UnifiedChatState,
|
|
72
70
|
UnifiedChatMessage,
|
|
73
71
|
UnifiedSendMessageOptions,
|
|
@@ -76,7 +74,6 @@ import { buildDiscussPrompt } from '../utils/discuss-ref-prompt';
|
|
|
76
74
|
import { extractIncompleteTailState } from '../utils/extract-incomplete-message-state';
|
|
77
75
|
import { processHistoricalMessagesWithErrors } from '../utils/process-historical-messages';
|
|
78
76
|
import { useChunkCatchup } from './use-chunk-catchup';
|
|
79
|
-
import { useManagedDialogList } from './use-managed-dialog-list';
|
|
80
77
|
import { useNatsDialogSubscription } from './use-nats-dialog-subscription';
|
|
81
78
|
|
|
82
79
|
// Legacy public home of the pure message-mutation helpers ("exported for
|
|
@@ -93,11 +90,21 @@ export {
|
|
|
93
90
|
// Config + options
|
|
94
91
|
// =============================================================================
|
|
95
92
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
+
}
|
|
101
108
|
|
|
102
109
|
/** Page-fetch parameters passed to `fetchDialogMessages`. */
|
|
103
110
|
export interface FetchDialogMessagesParams {
|
|
@@ -415,8 +422,6 @@ export function useNatsChatAdapter(
|
|
|
415
422
|
deleteDialog: deleteDialogCallback,
|
|
416
423
|
renameDialog: renameDialogCallback,
|
|
417
424
|
archiveDialog: archiveDialogCallback,
|
|
418
|
-
fetchArchivedDialogs,
|
|
419
|
-
unarchiveDialog,
|
|
420
425
|
approveRequest: approveRequestCallback,
|
|
421
426
|
rejectRequest: rejectRequestCallback,
|
|
422
427
|
stopGeneration: stopGenerationCallback,
|
|
@@ -565,37 +570,12 @@ export function useNatsChatAdapter(
|
|
|
565
570
|
);
|
|
566
571
|
|
|
567
572
|
// ─── 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`.
|
|
571
573
|
|
|
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);
|
|
572
578
|
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
|
-
autoLoad: 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
|
-
});
|
|
599
579
|
|
|
600
580
|
// ─── Message-history pagination ───────────────────────────────────────────
|
|
601
581
|
|
|
@@ -881,6 +861,59 @@ export function useNatsChatAdapter(
|
|
|
881
861
|
const connectionState: ChatConnectionState =
|
|
882
862
|
!active || !dialogId ? 'connected' : connectedForDialog === dialogId ? 'connected' : 'connecting';
|
|
883
863
|
|
|
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
|
+
|
|
884
917
|
// ─── Public action handlers ───────────────────────────────────────────────
|
|
885
918
|
|
|
886
919
|
const sendMessage = useCallback(
|
|
@@ -935,11 +968,14 @@ export function useNatsChatAdapter(
|
|
|
935
968
|
// Optimistically prepend a placeholder dialog so the sidebar shows
|
|
936
969
|
// the new conversation immediately. The next list refresh will
|
|
937
970
|
// replace it with the canonical entry.
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
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
|
+
]);
|
|
943
979
|
if (isManagedMode) {
|
|
944
980
|
setInternalDialogId(result.dialogId);
|
|
945
981
|
}
|
|
@@ -950,7 +986,74 @@ export function useNatsChatAdapter(
|
|
|
950
986
|
} finally {
|
|
951
987
|
setIsCreatingDialog(false);
|
|
952
988
|
}
|
|
953
|
-
}, [createDialogCallback, isCreatingDialog, isManagedMode
|
|
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]);
|
|
954
1057
|
|
|
955
1058
|
const loadMoreMessages = useCallback(async (): Promise<void> => {
|
|
956
1059
|
if (!dialogId || !messagesNextCursor) return;
|
|
@@ -999,23 +1102,9 @@ export function useNatsChatAdapter(
|
|
|
999
1102
|
|
|
1000
1103
|
const { messages, streamingPhase, dialogTokenUsage = null, liveModel = null } = state;
|
|
1001
1104
|
const isLoading = streamingPhase !== 'idle';
|
|
1105
|
+
const hasMoreDialogs = dialogsNextCursor != null;
|
|
1002
1106
|
const hasMoreMessages = messagesNextCursor != null;
|
|
1003
1107
|
|
|
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
|
-
|
|
1019
1108
|
return useMemo<UnifiedChatState>(
|
|
1020
1109
|
() => ({
|
|
1021
1110
|
messages,
|
|
@@ -1053,8 +1142,6 @@ export function useNatsChatAdapter(
|
|
|
1053
1142
|
loadMoreDialogs,
|
|
1054
1143
|
hasMoreMessages,
|
|
1055
1144
|
loadMoreMessages,
|
|
1056
|
-
dialogsManaged: isManagedMode,
|
|
1057
|
-
dialogCapabilities,
|
|
1058
1145
|
// Approval mutations
|
|
1059
1146
|
approveRequest,
|
|
1060
1147
|
rejectRequest,
|
|
@@ -1087,8 +1174,6 @@ export function useNatsChatAdapter(
|
|
|
1087
1174
|
loadMoreDialogs,
|
|
1088
1175
|
hasMoreMessages,
|
|
1089
1176
|
loadMoreMessages,
|
|
1090
|
-
isManagedMode,
|
|
1091
|
-
dialogCapabilities,
|
|
1092
1177
|
approveRequest,
|
|
1093
1178
|
rejectRequest,
|
|
1094
1179
|
dialogTokenUsage,
|