@flamingo-stack/openframe-frontend-core 0.0.597 → 0.0.598-2039.5702.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/{chunk-PT54TJWD.js → chunk-26MKONC3.js} +5 -5
- package/dist/{chunk-NK6COZ3P.cjs → chunk-2TWJF4AB.cjs} +22 -22
- package/dist/{chunk-NK6COZ3P.cjs.map → chunk-2TWJF4AB.cjs.map} +1 -1
- package/dist/{chunk-E74GL4E3.cjs → chunk-3GLZDKWZ.cjs} +8 -8
- package/dist/{chunk-E74GL4E3.cjs.map → chunk-3GLZDKWZ.cjs.map} +1 -1
- package/dist/{chunk-STYNZ4QM.cjs → chunk-3IXDZI3R.cjs} +3 -3
- package/dist/{chunk-STYNZ4QM.cjs.map → chunk-3IXDZI3R.cjs.map} +1 -1
- package/dist/{chunk-DNQP4VPB.js → chunk-3XHNV6W7.js} +12 -12
- package/dist/{chunk-TVK7PQON.js → chunk-442V6CPU.js} +6 -6
- package/dist/{chunk-BZ4UEHNJ.js → chunk-5IMAIYID.js} +9 -9
- package/dist/{chunk-VQ6INRQ4.cjs → chunk-5VQKQIOG.cjs} +105 -105
- package/dist/{chunk-VQ6INRQ4.cjs.map → chunk-5VQKQIOG.cjs.map} +1 -1
- package/dist/{chunk-6QZNDPMV.cjs → chunk-7WTEFZP7.cjs} +9 -9
- package/dist/{chunk-6QZNDPMV.cjs.map → chunk-7WTEFZP7.cjs.map} +1 -1
- package/dist/{chunk-B4BZRBKC.js → chunk-7YMVB7GE.js} +4 -4
- package/dist/{chunk-BOILDHSY.js → chunk-AC2TI6UT.js} +12 -12
- package/dist/{chunk-YBPFIXV4.cjs → chunk-B6ZJV7L4.cjs} +746 -448
- package/dist/{chunk-YBPFIXV4.cjs.map → chunk-B6ZJV7L4.cjs.map} +1 -1
- package/dist/{chunk-RYZKWZBG.cjs → chunk-B72CD6YE.cjs} +15 -15
- package/dist/{chunk-RYZKWZBG.cjs.map → chunk-B72CD6YE.cjs.map} +1 -1
- package/dist/{chunk-EVMHJB6X.js → chunk-BLUL2TE3.js} +4 -4
- package/dist/{chunk-GGQZBSFN.js → chunk-BVQEU5JB.js} +2 -2
- package/dist/{chunk-H64ZHSYR.cjs → chunk-CCWI3622.cjs} +4 -4
- package/dist/{chunk-H64ZHSYR.cjs.map → chunk-CCWI3622.cjs.map} +1 -1
- package/dist/{chunk-ELG2QNW5.js → chunk-D7X33EAT.js} +7 -7
- package/dist/{chunk-L3L2ATFK.cjs → chunk-E2K2LYSM.cjs} +85 -85
- package/dist/{chunk-L3L2ATFK.cjs.map → chunk-E2K2LYSM.cjs.map} +1 -1
- package/dist/{chunk-YH6IQTPI.cjs → chunk-EHAYNUOL.cjs} +3 -3
- package/dist/{chunk-YH6IQTPI.cjs.map → chunk-EHAYNUOL.cjs.map} +1 -1
- package/dist/{chunk-WZJOILPU.js → chunk-F2KBCLUG.js} +2 -2
- package/dist/{chunk-MYBXCWEI.js → chunk-FKBM6U43.js} +2 -2
- package/dist/{chunk-FKOYQPMK.js → chunk-GQLYYZF4.js} +5 -5
- package/dist/{chunk-LRQCKSZC.js → chunk-IW746HJ7.js} +2 -2
- package/dist/{chunk-GKZUTZT3.cjs → chunk-JI2R4UW5.cjs} +15 -15
- package/dist/{chunk-GKZUTZT3.cjs.map → chunk-JI2R4UW5.cjs.map} +1 -1
- package/dist/{chunk-M4YQUHOR.cjs → chunk-JJUV7Z4L.cjs} +15 -15
- package/dist/{chunk-M4YQUHOR.cjs.map → chunk-JJUV7Z4L.cjs.map} +1 -1
- package/dist/{chunk-2JOIC34G.cjs → chunk-KJMXCDYI.cjs} +37 -37
- package/dist/{chunk-2JOIC34G.cjs.map → chunk-KJMXCDYI.cjs.map} +1 -1
- package/dist/{chunk-IRXLB6GV.js → chunk-KV24I5MD.js} +5 -5
- package/dist/{chunk-Z4QFA4BF.cjs → chunk-M7U3MT2Q.cjs} +14 -14
- package/dist/{chunk-Z4QFA4BF.cjs.map → chunk-M7U3MT2Q.cjs.map} +1 -1
- package/dist/{chunk-R2YJKREE.js → chunk-NJCVIERS.js} +4 -4
- package/dist/{chunk-VXEHG53O.js → chunk-OIUXJUMH.js} +6 -6
- package/dist/{chunk-SAEJGTCI.cjs → chunk-QPAPUIF3.cjs} +35 -35
- package/dist/{chunk-SAEJGTCI.cjs.map → chunk-QPAPUIF3.cjs.map} +1 -1
- package/dist/{chunk-CPJP7GKL.cjs → chunk-R6LFBCGC.cjs} +3 -3
- package/dist/{chunk-CPJP7GKL.cjs.map → chunk-R6LFBCGC.cjs.map} +1 -1
- package/dist/{chunk-FHWL7QFL.js → chunk-RTCKC6RH.js} +5 -5
- package/dist/{chunk-UAAXUFK6.js → chunk-RU3IKVY2.js} +3 -3
- package/dist/{chunk-EZM5DNII.js → chunk-S6WBBSII.js} +3 -3
- package/dist/{chunk-WWGSYT7R.js → chunk-SYFSVIGH.js} +2 -2
- package/dist/{chunk-R2MOCGZ7.cjs → chunk-SZRP4264.cjs} +34 -34
- package/dist/{chunk-R2MOCGZ7.cjs.map → chunk-SZRP4264.cjs.map} +1 -1
- package/dist/{chunk-2JNCZDJR.js → chunk-TSCIVH7T.js} +2 -2
- package/dist/{chunk-JJFYMYBJ.cjs → chunk-TYENRLNN.cjs} +39 -39
- package/dist/{chunk-JJFYMYBJ.cjs.map → chunk-TYENRLNN.cjs.map} +1 -1
- package/dist/{chunk-EGQ345IA.js → chunk-UHWDSVZI.js} +4 -4
- package/dist/{chunk-RDTOMWDN.cjs → chunk-UPKZU6QU.cjs} +1 -1
- package/dist/chunk-UPKZU6QU.cjs.map +1 -0
- package/dist/{chunk-WCBRZCU4.cjs → chunk-UQEOAPAI.cjs} +9 -9
- package/dist/{chunk-WCBRZCU4.cjs.map → chunk-UQEOAPAI.cjs.map} +1 -1
- package/dist/{chunk-O4VZOA2Q.cjs → chunk-UXOOVP77.cjs} +3 -3
- package/dist/{chunk-O4VZOA2Q.cjs.map → chunk-UXOOVP77.cjs.map} +1 -1
- package/dist/{chunk-BZEWPW5A.cjs → chunk-VIBREYUK.cjs} +3 -3
- package/dist/{chunk-BZEWPW5A.cjs.map → chunk-VIBREYUK.cjs.map} +1 -1
- package/dist/{chunk-E27AKT4A.cjs → chunk-WRZV3HGH.cjs} +9 -9
- package/dist/{chunk-E27AKT4A.cjs.map → chunk-WRZV3HGH.cjs.map} +1 -1
- package/dist/{chunk-PIQRGLTF.cjs → chunk-WU53IOY6.cjs} +25 -25
- package/dist/{chunk-PIQRGLTF.cjs.map → chunk-WU53IOY6.cjs.map} +1 -1
- package/dist/{chunk-VNSRNVPD.js → chunk-XAJZIDFH.js} +673 -375
- package/dist/chunk-XAJZIDFH.js.map +1 -0
- package/dist/{chunk-UIVLSMEZ.cjs → chunk-XRVFQQ7W.cjs} +242 -242
- package/dist/{chunk-UIVLSMEZ.cjs.map → chunk-XRVFQQ7W.cjs.map} +1 -1
- package/dist/{chunk-RYXIRVDC.js → chunk-YB6FI2G3.js} +1 -1
- package/dist/chunk-YB6FI2G3.js.map +1 -0
- package/dist/{chunk-V6KX4KO3.js → chunk-YPJB4LJD.js} +3 -3
- package/dist/components/case-studies/index.cjs +14 -14
- package/dist/components/case-studies/index.js +8 -8
- package/dist/components/chat/hooks/index.d.ts +1 -0
- package/dist/components/chat/hooks/index.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-chat-history-hydration.d.ts +5 -3
- package/dist/components/chat/hooks/use-chat-history-hydration.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-managed-dialog-list.d.ts +49 -0
- package/dist/components/chat/hooks/use-managed-dialog-list.d.ts.map +1 -0
- package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +2 -16
- 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 +9 -5
- package/dist/components/chat/index.cjs.map +1 -1
- package/dist/components/chat/index.js +8 -4
- package/dist/components/chat/types/unified-chat-state.types.d.ts +55 -0
- package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
- package/dist/components/chat/utils/chat-conversations-api.d.ts +29 -0
- package/dist/components/chat/utils/chat-conversations-api.d.ts.map +1 -0
- package/dist/components/chat/utils/index.d.ts +1 -0
- 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 +70 -66
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.js +26 -22
- 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/index.cjs +12 -8
- package/dist/components/ui/index.cjs.map +1 -1
- package/dist/components/ui/index.js +11 -7
- package/dist/contexts/chat-runtime-context.d.ts +12 -0
- 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 +16 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +15 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/chat/.embeddable-chat.md +2 -1
- package/src/components/chat/embeddable-chat.tsx +69 -37
- package/src/components/chat/hooks/.use-managed-dialog-list.md +28 -0
- package/src/components/chat/hooks/.use-nats-chat-adapter.md +2 -0
- package/src/components/chat/hooks/.use-sse-chat-adapter.md +7 -0
- package/src/components/chat/hooks/.use-unified-chat.md +1 -1
- package/src/components/chat/hooks/__tests__/managed-dialog-list.test.ts +92 -0
- package/src/components/chat/hooks/__tests__/nats-adapter-managed-dialogs.test.ts +167 -0
- package/src/components/chat/hooks/__tests__/sse-managed-dialogs.test.ts +256 -0
- package/src/components/chat/hooks/index.ts +2 -0
- package/src/components/chat/hooks/use-chat-history-hydration.ts +8 -7
- package/src/components/chat/hooks/use-managed-dialog-list.ts +263 -0
- package/src/components/chat/hooks/use-nats-chat-adapter.ts +65 -150
- package/src/components/chat/hooks/use-sse-chat-adapter.ts +213 -39
- package/src/components/chat/hooks/use-unified-chat.ts +5 -0
- package/src/components/chat/types/unified-chat-state.types.ts +62 -0
- package/src/components/chat/utils/.chat-conversations-api.md +16 -0
- package/src/components/chat/utils/__tests__/chat-conversations-api-golden.test.ts +90 -0
- package/src/components/chat/utils/chat-conversations-api.ts +92 -0
- package/src/components/chat/utils/index.ts +7 -0
- package/src/contexts/.chat-runtime-context.md +2 -0
- package/src/contexts/chat-runtime-context.tsx +12 -0
- package/dist/chunk-RDTOMWDN.cjs.map +0 -1
- package/dist/chunk-RYXIRVDC.js.map +0 -1
- package/dist/chunk-VNSRNVPD.js.map +0 -1
- /package/dist/{chunk-PT54TJWD.js.map → chunk-26MKONC3.js.map} +0 -0
- /package/dist/{chunk-DNQP4VPB.js.map → chunk-3XHNV6W7.js.map} +0 -0
- /package/dist/{chunk-TVK7PQON.js.map → chunk-442V6CPU.js.map} +0 -0
- /package/dist/{chunk-BZ4UEHNJ.js.map → chunk-5IMAIYID.js.map} +0 -0
- /package/dist/{chunk-B4BZRBKC.js.map → chunk-7YMVB7GE.js.map} +0 -0
- /package/dist/{chunk-BOILDHSY.js.map → chunk-AC2TI6UT.js.map} +0 -0
- /package/dist/{chunk-EVMHJB6X.js.map → chunk-BLUL2TE3.js.map} +0 -0
- /package/dist/{chunk-GGQZBSFN.js.map → chunk-BVQEU5JB.js.map} +0 -0
- /package/dist/{chunk-ELG2QNW5.js.map → chunk-D7X33EAT.js.map} +0 -0
- /package/dist/{chunk-WZJOILPU.js.map → chunk-F2KBCLUG.js.map} +0 -0
- /package/dist/{chunk-MYBXCWEI.js.map → chunk-FKBM6U43.js.map} +0 -0
- /package/dist/{chunk-FKOYQPMK.js.map → chunk-GQLYYZF4.js.map} +0 -0
- /package/dist/{chunk-LRQCKSZC.js.map → chunk-IW746HJ7.js.map} +0 -0
- /package/dist/{chunk-IRXLB6GV.js.map → chunk-KV24I5MD.js.map} +0 -0
- /package/dist/{chunk-R2YJKREE.js.map → chunk-NJCVIERS.js.map} +0 -0
- /package/dist/{chunk-VXEHG53O.js.map → chunk-OIUXJUMH.js.map} +0 -0
- /package/dist/{chunk-FHWL7QFL.js.map → chunk-RTCKC6RH.js.map} +0 -0
- /package/dist/{chunk-UAAXUFK6.js.map → chunk-RU3IKVY2.js.map} +0 -0
- /package/dist/{chunk-EZM5DNII.js.map → chunk-S6WBBSII.js.map} +0 -0
- /package/dist/{chunk-WWGSYT7R.js.map → chunk-SYFSVIGH.js.map} +0 -0
- /package/dist/{chunk-2JNCZDJR.js.map → chunk-TSCIVH7T.js.map} +0 -0
- /package/dist/{chunk-EGQ345IA.js.map → chunk-UHWDSVZI.js.map} +0 -0
- /package/dist/{chunk-V6KX4KO3.js.map → chunk-YPJB4LJD.js.map} +0 -0
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
* Ask + Display buttons WORK out of the box.
|
|
52
52
|
*/
|
|
53
53
|
|
|
54
|
-
import { useCallback, useMemo, useRef } from 'react';
|
|
54
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
55
55
|
import { buildConfirmToolBody, readServerErrorMessage } from '../../../chat-protocol/confirm-tool';
|
|
56
56
|
import { createSseFrameDecoder } from '../../../chat-protocol/decode';
|
|
57
57
|
import type { ChatStreamEvent } from '../../../chat-protocol/events';
|
|
@@ -65,17 +65,21 @@ import { useChatStreamReducer } from '../stream/use-chat-stream-reducer';
|
|
|
65
65
|
import type { DialogItem } from '../types/component.types';
|
|
66
66
|
import type { Message, MessageSegment } from '../types/message.types';
|
|
67
67
|
import type {
|
|
68
|
+
ChatDialogCapabilities,
|
|
69
|
+
FetchDialogsParams,
|
|
68
70
|
UnifiedChatState,
|
|
69
71
|
UnifiedChatMessage,
|
|
70
72
|
UnifiedSendMessageOptions,
|
|
71
73
|
} from '../types/unified-chat-state.types';
|
|
72
74
|
import { createChatConversationStorage, pruneStaleChatConversationStorage } from '../utils/chat-conversation-storage';
|
|
75
|
+
import { createChatConversationsApi } from '../utils/chat-conversations-api';
|
|
73
76
|
import { buildDiscussPrompt } from '../utils/discuss-ref-prompt';
|
|
74
77
|
import type { ScrollAnchor } from '../utils/scroll-anchor';
|
|
75
78
|
import { sanitizeTitleForChat } from '../utils/slash-dispatch-utils';
|
|
76
79
|
import type { WireCommandOverride } from '../utils/slash-dispatch-utils';
|
|
77
80
|
import { defaultTableIdForDocumentType } from '../utils/source-icons';
|
|
78
81
|
import { useChatHistoryHydration } from './use-chat-history-hydration';
|
|
82
|
+
import { useManagedDialogList } from './use-managed-dialog-list';
|
|
79
83
|
import { useSlashCommandRegistry, type SlashCommandSummary } from './use-slash-commands';
|
|
80
84
|
|
|
81
85
|
// Canonical home of the per-turn meta row moved to the stream module in
|
|
@@ -316,8 +320,6 @@ export function useSseChatAdapter(
|
|
|
316
320
|
createReducerOptions,
|
|
317
321
|
});
|
|
318
322
|
|
|
319
|
-
const abortControllerRef = useRef<AbortController | null>(null);
|
|
320
|
-
|
|
321
323
|
// ─── Conversation identity — server-minted id, restored once on mount ─────
|
|
322
324
|
// Null = no conversation yet: the FIRST send goes out without an id, the
|
|
323
325
|
// server mints one, and the metadata-frame capture (in the send loop below)
|
|
@@ -332,6 +334,20 @@ export function useSseChatAdapter(
|
|
|
332
334
|
pruneStaleChatConversationStorage(source);
|
|
333
335
|
conversationIdRef.current = conversationStorage.load()?.conversationId ?? null;
|
|
334
336
|
}
|
|
337
|
+
// Render-visible mirror of the ref. The ref stays the SYNCHRONOUS send-time
|
|
338
|
+
// source (a capture mid-stream must be visible to the very next send); the
|
|
339
|
+
// state is what re-arms history hydration and drives `activeDialogId` when
|
|
340
|
+
// the user switches conversations. ONE writer for both + storage.
|
|
341
|
+
const [conversationId, setConversationId] = useState<string | null>(() => conversationIdRef.current);
|
|
342
|
+
const commitConversationId = useCallback(
|
|
343
|
+
(id: string | null) => {
|
|
344
|
+
conversationIdRef.current = id;
|
|
345
|
+
setConversationId(id);
|
|
346
|
+
if (id) conversationStorage.save({ conversationId: id });
|
|
347
|
+
else conversationStorage.clear();
|
|
348
|
+
},
|
|
349
|
+
[conversationStorage],
|
|
350
|
+
);
|
|
335
351
|
|
|
336
352
|
// ─── Slash-command registry (displayRef lookup) ───────────────────────────
|
|
337
353
|
// Reads from the SAME react-query cache entry as `<EmbeddableChat>`'s
|
|
@@ -405,7 +421,7 @@ export function useSseChatAdapter(
|
|
|
405
421
|
active,
|
|
406
422
|
source,
|
|
407
423
|
historyUrl,
|
|
408
|
-
|
|
424
|
+
conversationId,
|
|
409
425
|
sendCountRef: hydrationSendCountRef,
|
|
410
426
|
hydrateMessages,
|
|
411
427
|
// Meta invalidation is a reducer concern here: `seedSseMaps` (inside
|
|
@@ -413,6 +429,86 @@ export function useSseChatAdapter(
|
|
|
413
429
|
bumpMetaTick: noopBumpMetaTick,
|
|
414
430
|
});
|
|
415
431
|
|
|
432
|
+
const abortControllerRef = useRef<AbortController | null>(null);
|
|
433
|
+
|
|
434
|
+
/** Switch the panel to another conversation (or to the draft state with
|
|
435
|
+
* `null`): abort any in-flight stream, flush + reset the reducer, re-arm
|
|
436
|
+
* the hydration guard, and commit the new id. `useChatHistoryHydration`
|
|
437
|
+
* then re-fetches for a non-null id through its `conversationId` dep. */
|
|
438
|
+
const switchConversation = useCallback(
|
|
439
|
+
(id: string | null) => {
|
|
440
|
+
abortControllerRef.current?.abort();
|
|
441
|
+
flushDeltas();
|
|
442
|
+
mutate(r => r.reset());
|
|
443
|
+
hydrationSendCountRef.current = 0;
|
|
444
|
+
hydratedKeyRef.current = null;
|
|
445
|
+
commitConversationId(id);
|
|
446
|
+
},
|
|
447
|
+
[flushDeltas, mutate, hydratedKeyRef, commitConversationId],
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
// ─── Managed dialog list (only with `chatConversationsUrl`) ───────────────
|
|
451
|
+
// The SAME list state machine the NATS adapter runs; the host contract is
|
|
452
|
+
// the hub's conversations route, spelled out once in
|
|
453
|
+
// `chat-conversations-api.ts`. Without the endpoint the hook is inert and
|
|
454
|
+
// the return shape below keeps the single-thread stubs — zero behaviour
|
|
455
|
+
// change for hosts that never set it.
|
|
456
|
+
const conversationsUrl = runtime.endpoints.chatConversationsUrl;
|
|
457
|
+
const conversationsApi = useMemo(
|
|
458
|
+
() => (conversationsUrl ? createChatConversationsApi(conversationsUrl) : null),
|
|
459
|
+
[conversationsUrl],
|
|
460
|
+
);
|
|
461
|
+
const [dialogSearch, setDialogSearch] = useState('');
|
|
462
|
+
const fetchActiveDialogs = useMemo(
|
|
463
|
+
() =>
|
|
464
|
+
conversationsApi
|
|
465
|
+
? (params: FetchDialogsParams) => conversationsApi.fetchDialogs({ ...params, status: 'active' })
|
|
466
|
+
: undefined,
|
|
467
|
+
[conversationsApi],
|
|
468
|
+
);
|
|
469
|
+
const fetchArchivedDialogs = useMemo(
|
|
470
|
+
() =>
|
|
471
|
+
conversationsApi
|
|
472
|
+
? (params: FetchDialogsParams) => conversationsApi.fetchDialogs({ ...params, status: 'archived' })
|
|
473
|
+
: undefined,
|
|
474
|
+
[conversationsApi],
|
|
475
|
+
);
|
|
476
|
+
const {
|
|
477
|
+
dialogs,
|
|
478
|
+
isDialogsLoading,
|
|
479
|
+
dialogsError,
|
|
480
|
+
hasMoreDialogs,
|
|
481
|
+
reloadDialogs,
|
|
482
|
+
loadMoreDialogs,
|
|
483
|
+
renameDialog,
|
|
484
|
+
archiveDialog,
|
|
485
|
+
upsertDialogTop,
|
|
486
|
+
patchDialog,
|
|
487
|
+
} = useManagedDialogList({
|
|
488
|
+
active,
|
|
489
|
+
fetchDialogs: fetchActiveDialogs,
|
|
490
|
+
renameDialog: conversationsApi?.renameDialog,
|
|
491
|
+
archiveDialog: conversationsApi?.archiveDialog,
|
|
492
|
+
pageSize: 20,
|
|
493
|
+
search: conversationsApi ? dialogSearch : undefined,
|
|
494
|
+
logTag: '[useSseChatAdapter]',
|
|
495
|
+
// Archiving the OPEN conversation drops back to the draft state (the
|
|
496
|
+
// manager also clears the thread; both paths are idempotent).
|
|
497
|
+
onDialogRemoved: id => {
|
|
498
|
+
if (conversationIdRef.current === id) switchConversation(null);
|
|
499
|
+
},
|
|
500
|
+
});
|
|
501
|
+
const unarchiveDialog = useMemo(
|
|
502
|
+
() =>
|
|
503
|
+
conversationsApi
|
|
504
|
+
? async (id: string) => {
|
|
505
|
+
await conversationsApi.unarchiveDialog(id);
|
|
506
|
+
reloadDialogs();
|
|
507
|
+
}
|
|
508
|
+
: undefined,
|
|
509
|
+
[conversationsApi, reloadDialogs],
|
|
510
|
+
);
|
|
511
|
+
|
|
416
512
|
// ─── Send / stream loop ────────────────────────────────────────────────────
|
|
417
513
|
|
|
418
514
|
/**
|
|
@@ -435,15 +531,19 @@ export function useSseChatAdapter(
|
|
|
435
531
|
* same conversation. The reducer ignores the field (identity is a
|
|
436
532
|
* transport/persistence concern, not render state). */
|
|
437
533
|
const captureConversationId = useCallback(
|
|
438
|
-
(event: ChatStreamEvent):
|
|
439
|
-
if (event.type !== 'metadata') return;
|
|
534
|
+
(event: ChatStreamEvent): boolean => {
|
|
535
|
+
if (event.type !== 'metadata') return false;
|
|
440
536
|
const id = event.conversationId;
|
|
441
|
-
if (typeof id !== 'string' || !id) return;
|
|
442
|
-
if (conversationIdRef.current === id) return;
|
|
443
|
-
|
|
444
|
-
|
|
537
|
+
if (typeof id !== 'string' || !id) return false;
|
|
538
|
+
if (conversationIdRef.current === id) return false;
|
|
539
|
+
// Mark the new id as ALREADY hydrated before it becomes render state:
|
|
540
|
+
// the turn being streamed IS its history, and a mount-style fetch here
|
|
541
|
+
// would prepend that same turn once the server has persisted it.
|
|
542
|
+
hydratedKeyRef.current = `${source}:${id}`;
|
|
543
|
+
commitConversationId(id);
|
|
544
|
+
return true;
|
|
445
545
|
},
|
|
446
|
-
[
|
|
546
|
+
[commitConversationId, hydratedKeyRef, source],
|
|
447
547
|
);
|
|
448
548
|
|
|
449
549
|
const sendMessage = useCallback(
|
|
@@ -460,19 +560,19 @@ export function useSseChatAdapter(
|
|
|
460
560
|
// The server is the single source of conversation history: it re-reads
|
|
461
561
|
// `chat_messages` by conversation id on every turn. The wire therefore
|
|
462
562
|
// carries ONLY the new user message — never the prior conversation.
|
|
463
|
-
const
|
|
563
|
+
const echoedConversationId = conversationIdRef.current;
|
|
464
564
|
const targetPath = approvalAction ? endpointsRef.current.approvalToolUrl : endpointsRef.current.chatStreamUrl;
|
|
465
565
|
const requestBody = approvalAction
|
|
466
566
|
? // Shared with every other transport that resolves a hub proposal —
|
|
467
567
|
// the body shape is the hub's, not this adapter's. `conversationId`
|
|
468
568
|
// is always present here: an approval can only happen inside an
|
|
469
569
|
// established conversation (the proposal turn captured the id).
|
|
470
|
-
buildConfirmToolBody({ ...approvalAction, conversationId })
|
|
570
|
+
buildConfirmToolBody({ ...approvalAction, conversationId: echoedConversationId })
|
|
471
571
|
: {
|
|
472
572
|
messages: [{ role: 'user', content: text }],
|
|
473
573
|
...(commandOverride ? { commandOverride } : {}),
|
|
474
574
|
...(attachments && attachments.length > 0 ? { pendingAttachments: attachments } : {}),
|
|
475
|
-
...(
|
|
575
|
+
...(echoedConversationId ? { conversationId: echoedConversationId } : {}),
|
|
476
576
|
};
|
|
477
577
|
|
|
478
578
|
// Optimistic user bubble + assistant placeholder + phase 'thinking'
|
|
@@ -481,6 +581,7 @@ export function useSseChatAdapter(
|
|
|
481
581
|
|
|
482
582
|
const ctrl = new AbortController();
|
|
483
583
|
abortControllerRef.current = ctrl;
|
|
584
|
+
let mintedThisTurn = false;
|
|
484
585
|
|
|
485
586
|
try {
|
|
486
587
|
// `embedAuthedFetch` carries the bearer-act-as headers (+ Supabase
|
|
@@ -513,7 +614,18 @@ export function useSseChatAdapter(
|
|
|
513
614
|
if (done) break;
|
|
514
615
|
if (ctrl.signal.aborted) break;
|
|
515
616
|
for (const event of frameDecoder.push(value)) {
|
|
516
|
-
captureConversationId(event)
|
|
617
|
+
if (captureConversationId(event)) {
|
|
618
|
+
mintedThisTurn = true;
|
|
619
|
+
// Optimistic row so the list shows the new conversation at
|
|
620
|
+
// once; the end-of-turn reload swaps in the server's title.
|
|
621
|
+
if (conversationsApi && !hidden) {
|
|
622
|
+
upsertDialogTop({
|
|
623
|
+
id: conversationIdRef.current ?? '',
|
|
624
|
+
title: text.slice(0, 80),
|
|
625
|
+
timestamp: new Date(),
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
}
|
|
517
629
|
applyEvent(event);
|
|
518
630
|
}
|
|
519
631
|
}
|
|
@@ -550,9 +662,26 @@ export function useSseChatAdapter(
|
|
|
550
662
|
// path streams no text — and returns the phase to idle).
|
|
551
663
|
flushDeltas();
|
|
552
664
|
mutate(r => r.endSseTurn());
|
|
665
|
+
// Keep the managed list in step with the transcript: a first turn
|
|
666
|
+
// re-reads the list (server-derived title replaces the placeholder,
|
|
667
|
+
// the row is persisted by the time the stream ends); a later turn
|
|
668
|
+
// just bumps the row to the top.
|
|
669
|
+
if (conversationsApi && !ctrl.signal.aborted) {
|
|
670
|
+
if (mintedThisTurn) reloadDialogs();
|
|
671
|
+
else if (conversationIdRef.current) patchDialog(conversationIdRef.current, { timestamp: new Date() });
|
|
672
|
+
}
|
|
553
673
|
}
|
|
554
674
|
},
|
|
555
|
-
[
|
|
675
|
+
[
|
|
676
|
+
mutate,
|
|
677
|
+
applyEvent,
|
|
678
|
+
flushDeltas,
|
|
679
|
+
captureConversationId,
|
|
680
|
+
conversationsApi,
|
|
681
|
+
upsertDialogTop,
|
|
682
|
+
reloadDialogs,
|
|
683
|
+
patchDialog,
|
|
684
|
+
],
|
|
556
685
|
);
|
|
557
686
|
sendMessageRef.current = sendMessage;
|
|
558
687
|
|
|
@@ -568,11 +697,34 @@ export function useSseChatAdapter(
|
|
|
568
697
|
// server-side; the NEXT send goes out id-less and the server mints a
|
|
569
698
|
// fresh conversation (echoed back and re-captured then). Resetting the
|
|
570
699
|
// hydration guard means a re-captured id can hydrate again if needed.
|
|
571
|
-
|
|
572
|
-
conversationStorage.clear();
|
|
700
|
+
commitConversationId(null);
|
|
573
701
|
hydrationSendCountRef.current = 0;
|
|
574
702
|
hydratedKeyRef.current = null;
|
|
575
|
-
}, [mutate,
|
|
703
|
+
}, [mutate, commitConversationId, hydratedKeyRef]);
|
|
704
|
+
|
|
705
|
+
/** Open a conversation from the managed list. Idempotent on the active id. */
|
|
706
|
+
const selectDialog = useCallback(
|
|
707
|
+
(id: string | null) => {
|
|
708
|
+
if (id === conversationIdRef.current) return;
|
|
709
|
+
switchConversation(id);
|
|
710
|
+
},
|
|
711
|
+
[switchConversation],
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
const dialogCapabilities = useMemo<ChatDialogCapabilities | undefined>(
|
|
715
|
+
() =>
|
|
716
|
+
conversationsApi
|
|
717
|
+
? {
|
|
718
|
+
canRename: true,
|
|
719
|
+
canArchive: true,
|
|
720
|
+
fetchArchivedDialogs,
|
|
721
|
+
unarchiveDialog,
|
|
722
|
+
searchQuery: dialogSearch,
|
|
723
|
+
onSearchChange: setDialogSearch,
|
|
724
|
+
}
|
|
725
|
+
: undefined,
|
|
726
|
+
[conversationsApi, fetchArchivedDialogs, unarchiveDialog, dialogSearch],
|
|
727
|
+
);
|
|
576
728
|
|
|
577
729
|
// ─── Public message mapping (sendIdx fan-out lookup) ──────────────────────
|
|
578
730
|
// Index sources/scrollAnchor by USER-SEND count (`sendIdx`), not by
|
|
@@ -721,26 +873,48 @@ export function useSseChatAdapter(
|
|
|
721
873
|
/** Cross-call usage breakdown (Haiku rewriter/classifier/summarizer
|
|
722
874
|
* token counts). null until the trailing usage frame lands. */
|
|
723
875
|
currentUsageBreakdown: latestMeta?.breakdown ?? null,
|
|
724
|
-
// ─── Dialog management
|
|
725
|
-
//
|
|
726
|
-
// (`
|
|
727
|
-
//
|
|
728
|
-
//
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
876
|
+
// ─── Dialog management ───────────────────────────────────────────────
|
|
877
|
+
// With `chatConversationsUrl` the server transcript store IS the dialog
|
|
878
|
+
// list (shared `useManagedDialogList`); selecting a row re-hydrates
|
|
879
|
+
// through the history route. Without it (hosts that never set the
|
|
880
|
+
// endpoint) the single-thread stubs below are returned unchanged.
|
|
881
|
+
...(conversationsApi
|
|
882
|
+
? {
|
|
883
|
+
dialogs,
|
|
884
|
+
activeDialogId: conversationId,
|
|
885
|
+
selectDialog,
|
|
886
|
+
// New chat = `clearMessages` (the server mints on the next send);
|
|
887
|
+
// there is no create call.
|
|
888
|
+
startNewDialog: noopStartNewDialog,
|
|
889
|
+
deleteDialog: noopDeleteDialog,
|
|
890
|
+
renameDialog,
|
|
891
|
+
archiveDialog,
|
|
892
|
+
isDialogsLoading,
|
|
893
|
+
dialogsError,
|
|
894
|
+
reloadDialogs,
|
|
895
|
+
isMessagesLoading: isHydratingHistory,
|
|
896
|
+
hasMoreDialogs,
|
|
897
|
+
loadMoreDialogs,
|
|
898
|
+
dialogsManaged: true,
|
|
899
|
+
dialogCapabilities,
|
|
900
|
+
}
|
|
901
|
+
: {
|
|
902
|
+
dialogs: SSE_EMPTY_DIALOGS,
|
|
903
|
+
activeDialogId: null,
|
|
904
|
+
selectDialog: noopSelectDialog,
|
|
905
|
+
startNewDialog: noopStartNewDialog,
|
|
906
|
+
deleteDialog: noopDeleteDialog,
|
|
907
|
+
renameDialog: noopRenameDialog,
|
|
908
|
+
archiveDialog: noopArchiveDialog,
|
|
909
|
+
isDialogsLoading: false,
|
|
910
|
+
// No server-side dialog list — never errors, nothing to retry.
|
|
911
|
+
dialogsError: false,
|
|
912
|
+
reloadDialogs: noopReloadDialogs,
|
|
913
|
+
isMessagesLoading: false,
|
|
914
|
+
hasMoreDialogs: false,
|
|
915
|
+
loadMoreDialogs: noopAsync,
|
|
916
|
+
dialogsManaged: false,
|
|
917
|
+
}),
|
|
744
918
|
hasMoreMessages: false,
|
|
745
919
|
loadMoreMessages: noopAsync,
|
|
746
920
|
approveRequest: noopApproveRequest,
|
|
@@ -220,6 +220,11 @@ export function useUnifiedChat(options: UseUnifiedChatOptions): UnifiedChatState
|
|
|
220
220
|
setDialogScope,
|
|
221
221
|
hasMoreMessages: activeState.hasMoreMessages,
|
|
222
222
|
loadMoreMessages,
|
|
223
|
+
// Adapter-owned dialog list flag + capabilities (SSE with a
|
|
224
|
+
// conversations endpoint, NATS managed mode). Forwarded verbatim —
|
|
225
|
+
// `EmbeddableChat` gates the history UI on them.
|
|
226
|
+
dialogsManaged: activeState.dialogsManaged,
|
|
227
|
+
dialogCapabilities: activeState.dialogCapabilities,
|
|
223
228
|
// Approvals
|
|
224
229
|
approveRequest,
|
|
225
230
|
rejectRequest,
|
|
@@ -24,6 +24,48 @@ import type { DialogItem } from './component.types';
|
|
|
24
24
|
import type { ChatContextItem } from './context-item.types';
|
|
25
25
|
import type { MessageSegment, ScrollAnchor } from './message.types';
|
|
26
26
|
|
|
27
|
+
// ─── Dialog-list host contract (shared by the NATS + SSE adapters) ───────────
|
|
28
|
+
|
|
29
|
+
/** Page-fetch parameters passed to a host `fetchDialogs` callback. The
|
|
30
|
+
* adapter owns the cursor — the host only resolves it against the backend.
|
|
31
|
+
* `search` is present ONLY when the adapter runs a server-side list search
|
|
32
|
+
* (never spread as an `undefined` key). */
|
|
33
|
+
export interface FetchDialogsParams {
|
|
34
|
+
cursor?: string;
|
|
35
|
+
limit?: number;
|
|
36
|
+
search?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Successful `fetchDialogs` response. `nextCursor: null` means "no more
|
|
40
|
+
* pages" — used to terminate the infinite-scroll observer in the list. */
|
|
41
|
+
export interface FetchDialogsResult {
|
|
42
|
+
dialogs: DialogItem[];
|
|
43
|
+
nextCursor: string | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Dialog-management capabilities a transport exposes to `EmbeddableChat`.
|
|
48
|
+
* Structurally identical to the host-injected `mingoDialogCapabilities`
|
|
49
|
+
* prop — one shape, whether the list is host-owned (openframe `mingoState`)
|
|
50
|
+
* or adapter-owned (`UnifiedChatState.dialogCapabilities`).
|
|
51
|
+
*/
|
|
52
|
+
export interface ChatDialogCapabilities {
|
|
53
|
+
/** Show "Rename chat" in the row ⋯ menu and the conversation header. */
|
|
54
|
+
canRename?: boolean;
|
|
55
|
+
/** Show "Archive chat" in the row ⋯ menu and the conversation header. */
|
|
56
|
+
canArchive?: boolean;
|
|
57
|
+
/** Pages archived dialogs — presence gates the archive page + header button. */
|
|
58
|
+
fetchArchivedDialogs?: (params: FetchDialogsParams) => Promise<FetchDialogsResult>;
|
|
59
|
+
/** Restores an archived dialog — presence gates the restore button. */
|
|
60
|
+
unarchiveDialog?: (id: string) => Promise<void>;
|
|
61
|
+
/** Current list-search term (host/adapter owned; the list never filters). */
|
|
62
|
+
searchQuery?: string;
|
|
63
|
+
/** Presence wires the header magnifier + the rail's search field. */
|
|
64
|
+
onSearchChange?: (query: string) => void;
|
|
65
|
+
/** "Copy chat link" — the owner of the URL shape + clipboard write. */
|
|
66
|
+
onCopyLink?: (dialog: DialogItem) => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
27
69
|
// ─── Per-dialog token usage (Mingo backend telemetry) ────────────────────────
|
|
28
70
|
|
|
29
71
|
/**
|
|
@@ -405,6 +447,26 @@ export interface UnifiedChatState {
|
|
|
405
447
|
/** Fetch the next page of historical messages for the active dialog. */
|
|
406
448
|
loadMoreMessages: () => Promise<void>;
|
|
407
449
|
|
|
450
|
+
/**
|
|
451
|
+
* True when the active adapter OWNS a server-side dialog list (NATS
|
|
452
|
+
* managed-dialog mode, or the SSE/Guide adapter with
|
|
453
|
+
* `ChatRuntime.endpoints.chatConversationsUrl` set). `EmbeddableChat` gates
|
|
454
|
+
* the history list / archive / rename affordances on this flag — transport-
|
|
455
|
+
* agnostic, so Guide mode gets the same "Current Chats" UI as Mingo mode.
|
|
456
|
+
* Undefined/false = the single-thread panel (bare transports, host-injected
|
|
457
|
+
* `mingoState`, which drives the UI through the Mingo mode check instead).
|
|
458
|
+
*/
|
|
459
|
+
dialogsManaged?: boolean;
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Dialog-management capabilities the active adapter can honour. Only
|
|
463
|
+
* meaningful when `dialogsManaged` is true; `EmbeddableChat` reads it in
|
|
464
|
+
* preference to the legacy per-mode derivations so the row ⋯ menu, the
|
|
465
|
+
* archive page and the header search never advertise an action the
|
|
466
|
+
* transport hasn't wired.
|
|
467
|
+
*/
|
|
468
|
+
dialogCapabilities?: ChatDialogCapabilities;
|
|
469
|
+
|
|
408
470
|
// ─── Approval mutations (Mingo agent tool-call workflow) ──────────────────
|
|
409
471
|
|
|
410
472
|
/** Approve an in-flight tool-call request. Errors surface via the
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!-- source-hash: c99e9348414c3485333cc4b1756972c4 -->
|
|
2
|
+
Wire client for the SSE/Guide conversation list — the ONE place the `ChatRuntime.endpoints.chatConversationsUrl` contract is spelled out.
|
|
3
|
+
|
|
4
|
+
## Key Components
|
|
5
|
+
|
|
6
|
+
### `createChatConversationsApi(baseUrl)` → `ChatConversationsApi`
|
|
7
|
+
- `fetchDialogs({ status, cursor, limit, search })` — `GET <base>?status=active|archived&limit=&cursor=&search=` (empty params omitted); unwraps the route-base `{ data }` envelope; maps rows to `DialogItem` (ISO `timestamp` → `Date`, empty title → `'Untitled chat'`)
|
|
8
|
+
- `renameDialog(id, title)` — `PATCH <base>/<id>` body `{ title }`
|
|
9
|
+
- `archiveDialog(id)` — `PATCH <base>/<id>` body `{ archived: true }`
|
|
10
|
+
- `unarchiveDialog(id)` — `PATCH <base>/<id>` body `{ archived: false }`
|
|
11
|
+
|
|
12
|
+
All calls go through `embedAuthedFetch` with its DEFAULT credentials mode: the hub identifies anonymous visitors with an httpOnly cookie and `same-origin` is what carries it — never override `credentials`. Failures throw the server's own error copy (`readServerErrorMessage`) or a status fallback.
|
|
13
|
+
|
|
14
|
+
## Source
|
|
15
|
+
|
|
16
|
+
[`chat-conversations-api.ts`](https://github.com/flamingo-stack/openframe-oss-lib/blob/main/chat-conversations-api.ts)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GOLDEN CONTRACT TESTS — the `chatConversationsUrl` wire client. Pins the
|
|
3
|
+
* exact URLs, query strings and PATCH bodies the hub route serves, the
|
|
4
|
+
* `{ data }` envelope unwrap, the ISO→Date mapping, and the server-copy
|
|
5
|
+
* error surfacing.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
9
|
+
import { createChatConversationsApi } from '../chat-conversations-api';
|
|
10
|
+
|
|
11
|
+
const jsonResponse = (body: unknown, status = 200) =>
|
|
12
|
+
({ ok: status >= 200 && status < 300, status, json: () => Promise.resolve(body) }) as unknown as Response;
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
vi.unstubAllGlobals();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('createChatConversationsApi', () => {
|
|
19
|
+
it('GET builds the query string, unwraps the envelope, maps timestamps to Date', async () => {
|
|
20
|
+
const fetchMock = vi.fn(() =>
|
|
21
|
+
Promise.resolve(
|
|
22
|
+
jsonResponse({
|
|
23
|
+
data: {
|
|
24
|
+
dialogs: [
|
|
25
|
+
{ id: 'a', title: 'Alpha', timestamp: '2026-09-06T10:00:00.000Z' },
|
|
26
|
+
{ id: 'b', title: '', timestamp: 'not-a-date' },
|
|
27
|
+
{ nope: true },
|
|
28
|
+
],
|
|
29
|
+
nextCursor: 'c1',
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
);
|
|
34
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
35
|
+
const api = createChatConversationsApi('/api/docs/chat/conversations/');
|
|
36
|
+
|
|
37
|
+
const result = await api.fetchDialogs({ status: 'active', limit: 20, cursor: 'c0', search: 'al pha' });
|
|
38
|
+
|
|
39
|
+
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
|
|
40
|
+
expect(url).toBe('/api/docs/chat/conversations?status=active&limit=20&cursor=c0&search=al+pha');
|
|
41
|
+
expect(init.method).toBe('GET');
|
|
42
|
+
expect(init.credentials).toBe('same-origin');
|
|
43
|
+
expect(result.nextCursor).toBe('c1');
|
|
44
|
+
expect(result.dialogs).toEqual([
|
|
45
|
+
{ id: 'a', title: 'Alpha', timestamp: new Date('2026-09-06T10:00:00.000Z') },
|
|
46
|
+
{ id: 'b', title: 'Untitled chat' },
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('GET omits empty optional params', async () => {
|
|
51
|
+
const fetchMock = vi.fn(() => Promise.resolve(jsonResponse({ data: { dialogs: [], nextCursor: null } })));
|
|
52
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
53
|
+
const api = createChatConversationsApi('/api/docs/chat/conversations');
|
|
54
|
+
await api.fetchDialogs({ status: 'archived', search: '' });
|
|
55
|
+
expect((fetchMock.mock.calls[0] as unknown as [string])[0]).toBe('/api/docs/chat/conversations?status=archived');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('PATCH bodies: rename / archive / restore', async () => {
|
|
59
|
+
const fetchMock = vi.fn(() => Promise.resolve(jsonResponse({ data: { id: 'x' } })));
|
|
60
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
61
|
+
const api = createChatConversationsApi('/api/docs/chat/conversations');
|
|
62
|
+
await api.renameDialog('id/with slash', 'New title');
|
|
63
|
+
await api.archiveDialog('x');
|
|
64
|
+
await api.unarchiveDialog('x');
|
|
65
|
+
const calls = fetchMock.mock.calls as unknown as Array<[string, RequestInit]>;
|
|
66
|
+
expect(calls[0][0]).toBe('/api/docs/chat/conversations/id%2Fwith%20slash');
|
|
67
|
+
expect(calls[0][1].method).toBe('PATCH');
|
|
68
|
+
expect(JSON.parse(String(calls[0][1].body))).toEqual({ title: 'New title' });
|
|
69
|
+
expect(JSON.parse(String(calls[1][1].body))).toEqual({ archived: true });
|
|
70
|
+
expect(JSON.parse(String(calls[2][1].body))).toEqual({ archived: false });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('surfaces the server error copy from the route-base envelope', async () => {
|
|
74
|
+
vi.stubGlobal(
|
|
75
|
+
'fetch',
|
|
76
|
+
vi.fn(() => Promise.resolve(jsonResponse({ error: 'Conversation not found', code: 'NOT_FOUND' }, 404))),
|
|
77
|
+
);
|
|
78
|
+
const api = createChatConversationsApi('/api/docs/chat/conversations');
|
|
79
|
+
await expect(api.renameDialog('x', 'y')).rejects.toThrow('Conversation not found');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('falls back to a status line when the error body is not the envelope', async () => {
|
|
83
|
+
vi.stubGlobal(
|
|
84
|
+
'fetch',
|
|
85
|
+
vi.fn(() => Promise.resolve(jsonResponse('<html/>', 502))),
|
|
86
|
+
);
|
|
87
|
+
const api = createChatConversationsApi('/api/docs/chat/conversations');
|
|
88
|
+
await expect(api.fetchDialogs({ status: 'active' })).rejects.toThrow('Conversation list failed: 502');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversation-list wire client for the SSE/Guide chat — the ONE place the
|
|
3
|
+
* `ChatRuntime.endpoints.chatConversationsUrl` contract is spelled out:
|
|
4
|
+
*
|
|
5
|
+
* GET `<base>?status=active|archived&limit=&cursor=&search=`
|
|
6
|
+
* → route-base envelope `{ data: { dialogs, nextCursor } }`
|
|
7
|
+
* PATCH `<base>/<id>` body `{ title }` (rename)
|
|
8
|
+
* PATCH `<base>/<id>` body `{ archived: true }` (archive)
|
|
9
|
+
* PATCH `<base>/<id>` body `{ archived: false }` (restore)
|
|
10
|
+
*
|
|
11
|
+
* Goes through `embedAuthedFetch` with its DEFAULT credentials mode — the
|
|
12
|
+
* hub identifies anonymous visitors with an httpOnly cookie, and
|
|
13
|
+
* `same-origin` is exactly what carries it. Never override `credentials`.
|
|
14
|
+
*
|
|
15
|
+
* Wire rows are mapped to `DialogItem`; the ISO `timestamp` becomes a
|
|
16
|
+
* `Date` so the list's Today/Yesterday/Older grouping is deterministic.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { readServerErrorMessage } from '../../../chat-protocol/confirm-tool';
|
|
20
|
+
import { embedAuthedFetch } from '../../../utils/embed-authed-fetch';
|
|
21
|
+
import type { DialogItem } from '../types/component.types';
|
|
22
|
+
import type { FetchDialogsParams, FetchDialogsResult } from '../types/unified-chat-state.types';
|
|
23
|
+
|
|
24
|
+
export type ChatConversationStatus = 'active' | 'archived';
|
|
25
|
+
|
|
26
|
+
export interface ChatConversationsApi {
|
|
27
|
+
fetchDialogs(params: FetchDialogsParams & { status: ChatConversationStatus }): Promise<FetchDialogsResult>;
|
|
28
|
+
renameDialog(id: string, title: string): Promise<void>;
|
|
29
|
+
archiveDialog(id: string): Promise<void>;
|
|
30
|
+
unarchiveDialog(id: string): Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Narrow one hop of an untrusted JSON body (`typeof null === 'object'`). */
|
|
34
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
35
|
+
return typeof value === 'object' && value !== null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** route-base `successResponse` wraps in `{ data }`; tolerate a raw body too. */
|
|
39
|
+
function unwrapEnvelope(payload: unknown): unknown {
|
|
40
|
+
return isRecord(payload) && 'data' in payload ? payload.data : payload;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function toDialogItem(row: unknown): DialogItem | null {
|
|
44
|
+
if (!isRecord(row)) return null;
|
|
45
|
+
const id = typeof row.id === 'string' ? row.id : '';
|
|
46
|
+
if (!id) return null;
|
|
47
|
+
const title = typeof row.title === 'string' && row.title.length > 0 ? row.title : 'Untitled chat';
|
|
48
|
+
const ts = typeof row.timestamp === 'string' ? new Date(row.timestamp) : undefined;
|
|
49
|
+
return {
|
|
50
|
+
id,
|
|
51
|
+
title,
|
|
52
|
+
...(ts && !Number.isNaN(ts.getTime()) ? { timestamp: ts } : {}),
|
|
53
|
+
...(typeof row.lastMessage === 'string' ? { lastMessage: row.lastMessage } : {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function failWith(response: Response, fallback: string): Promise<never> {
|
|
58
|
+
const message = await readServerErrorMessage(response);
|
|
59
|
+
throw new Error(message ?? `${fallback}: ${response.status}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createChatConversationsApi(baseUrl: string): ChatConversationsApi {
|
|
63
|
+
const base = baseUrl.replace(/\/+$/, '');
|
|
64
|
+
|
|
65
|
+
const patch = async (id: string, body: Record<string, unknown>, what: string): Promise<void> => {
|
|
66
|
+
const response = await embedAuthedFetch(`${base}/${encodeURIComponent(id)}`, {
|
|
67
|
+
method: 'PATCH',
|
|
68
|
+
body: JSON.stringify(body),
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) await failWith(response, `${what} failed`);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
async fetchDialogs({ status, cursor, limit, search }) {
|
|
75
|
+
const qs = new URLSearchParams();
|
|
76
|
+
qs.set('status', status);
|
|
77
|
+
if (limit !== undefined) qs.set('limit', String(limit));
|
|
78
|
+
if (cursor) qs.set('cursor', cursor);
|
|
79
|
+
if (search) qs.set('search', search);
|
|
80
|
+
const response = await embedAuthedFetch(`${base}?${qs.toString()}`, { method: 'GET' });
|
|
81
|
+
if (!response.ok) await failWith(response, 'Conversation list failed');
|
|
82
|
+
const body = unwrapEnvelope(await response.json());
|
|
83
|
+
const rawDialogs = isRecord(body) && Array.isArray(body.dialogs) ? body.dialogs : [];
|
|
84
|
+
const dialogs = rawDialogs.map(toDialogItem).filter((d): d is DialogItem => d !== null);
|
|
85
|
+
const nextCursor = isRecord(body) && typeof body.nextCursor === 'string' ? body.nextCursor : null;
|
|
86
|
+
return { dialogs, nextCursor };
|
|
87
|
+
},
|
|
88
|
+
renameDialog: (id, title) => patch(id, { title }, 'Rename'),
|
|
89
|
+
archiveDialog: id => patch(id, { archived: true }, 'Archive'),
|
|
90
|
+
unarchiveDialog: id => patch(id, { archived: false }, 'Restore'),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -41,6 +41,13 @@ export {
|
|
|
41
41
|
type HistoryPrependResult,
|
|
42
42
|
} from './history-merge';
|
|
43
43
|
|
|
44
|
+
// Conversation-list wire client (`ChatRuntime.endpoints.chatConversationsUrl`)
|
|
45
|
+
export {
|
|
46
|
+
createChatConversationsApi,
|
|
47
|
+
type ChatConversationsApi,
|
|
48
|
+
type ChatConversationStatus,
|
|
49
|
+
} from './chat-conversations-api';
|
|
50
|
+
|
|
44
51
|
// Tool call helpers
|
|
45
52
|
export { getCommandText } from './tool-call-helpers';
|
|
46
53
|
|