@djangocfg/ui-tools 2.1.335 → 2.1.337
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/README.md +68 -2
- package/dist/ChatRoot-PNNGQCYF.css +7 -0
- package/dist/ChatRoot-PNNGQCYF.css.map +1 -0
- package/dist/ChatRoot-XV2QXMV4.mjs +5 -0
- package/dist/ChatRoot-XV2QXMV4.mjs.map +1 -0
- package/dist/ChatRoot-YX4RLHQX.cjs +14 -0
- package/dist/ChatRoot-YX4RLHQX.cjs.map +1 -0
- package/dist/{CronScheduler.client-3O3VU4CI.mjs → CronScheduler.client-DLMXCPAJ.mjs} +4 -4
- package/dist/{CronScheduler.client-3O3VU4CI.mjs.map → CronScheduler.client-DLMXCPAJ.mjs.map} +1 -1
- package/dist/{CronScheduler.client-A4GO6YBY.cjs → CronScheduler.client-WEJF4PWQ.cjs} +14 -14
- package/dist/{CronScheduler.client-A4GO6YBY.cjs.map → CronScheduler.client-WEJF4PWQ.cjs.map} +1 -1
- package/dist/{DocsLayout-XLDB6CJ2.cjs → DocsLayout-N5ZJZPBY.cjs} +200 -199
- package/dist/DocsLayout-N5ZJZPBY.cjs.map +1 -0
- package/dist/{DocsLayout-CTJINVBM.mjs → DocsLayout-VFPPNKSQ.mjs} +7 -6
- package/dist/DocsLayout-VFPPNKSQ.mjs.map +1 -0
- package/dist/JsonSchemaForm-DD7CLRIG.cjs +13 -0
- package/dist/{JsonSchemaForm-6WMS4CIY.cjs.map → JsonSchemaForm-DD7CLRIG.cjs.map} +1 -1
- package/dist/JsonSchemaForm-XKUIVELK.mjs +4 -0
- package/dist/{JsonSchemaForm-KX4JT3M4.mjs.map → JsonSchemaForm-XKUIVELK.mjs.map} +1 -1
- package/dist/JsonTree-55625VVH.mjs +5 -0
- package/dist/{JsonTree-F27RMYSI.cjs.map → JsonTree-55625VVH.mjs.map} +1 -1
- package/dist/JsonTree-DCM5QGWF.cjs +11 -0
- package/dist/{JsonTree-QTJYSHCV.mjs.map → JsonTree-DCM5QGWF.cjs.map} +1 -1
- package/dist/{LottiePlayer.client-6WVWDO75.cjs → LottiePlayer.client-2S7ISJ2S.cjs} +6 -6
- package/dist/{LottiePlayer.client-6WVWDO75.cjs.map → LottiePlayer.client-2S7ISJ2S.cjs.map} +1 -1
- package/dist/{LottiePlayer.client-B4I6WNZM.mjs → LottiePlayer.client-5LDSSJWS.mjs} +4 -4
- package/dist/{LottiePlayer.client-B4I6WNZM.mjs.map → LottiePlayer.client-5LDSSJWS.mjs.map} +1 -1
- package/dist/{MapContainer-RYG4HPH4.cjs → MapContainer-76YL2JXL.cjs} +8 -8
- package/dist/{MapContainer-RYG4HPH4.cjs.map → MapContainer-76YL2JXL.cjs.map} +1 -1
- package/dist/{MapContainer-GXQLP5WY.mjs → MapContainer-7HXBI3OH.mjs} +3 -3
- package/dist/{MapContainer-GXQLP5WY.mjs.map → MapContainer-7HXBI3OH.mjs.map} +1 -1
- package/dist/{Mermaid.client-SXRRI2YW.mjs → Mermaid.client-NL4SVR7F.mjs} +4 -4
- package/dist/{Mermaid.client-SXRRI2YW.mjs.map → Mermaid.client-NL4SVR7F.mjs.map} +1 -1
- package/dist/{Mermaid.client-W76R5AKJ.cjs → Mermaid.client-NNTI6DFX.cjs} +26 -26
- package/dist/{Mermaid.client-W76R5AKJ.cjs.map → Mermaid.client-NNTI6DFX.cjs.map} +1 -1
- package/dist/Player-BRV7XTWR.mjs +4 -0
- package/dist/{Player-M3GC3VPE.mjs.map → Player-BRV7XTWR.mjs.map} +1 -1
- package/dist/Player-PM7F7DD7.cjs +13 -0
- package/dist/{Player-ZL2X5LGG.cjs.map → Player-PM7F7DD7.cjs.map} +1 -1
- package/dist/{PrettyCode.client-RPDIE5CH.cjs → PrettyCode.client-KOHDVPPN.cjs} +13 -13
- package/dist/{PrettyCode.client-RPDIE5CH.cjs.map → PrettyCode.client-KOHDVPPN.cjs.map} +1 -1
- package/dist/{PrettyCode.client-SPMTQEG4.mjs → PrettyCode.client-ZGYGKE7G.mjs} +4 -4
- package/dist/{PrettyCode.client-SPMTQEG4.mjs.map → PrettyCode.client-ZGYGKE7G.mjs.map} +1 -1
- package/dist/TreeRoot-N72OYKXU.cjs +19 -0
- package/dist/{TreeRoot-A3J65L6F.mjs.map → TreeRoot-N72OYKXU.cjs.map} +1 -1
- package/dist/TreeRoot-VGAIXCUA.mjs +4 -0
- package/dist/{TreeRoot-DSK5JILT.cjs.map → TreeRoot-VGAIXCUA.mjs.map} +1 -1
- package/dist/chunk-2ZLKZ5VR.mjs +631 -0
- package/dist/chunk-2ZLKZ5VR.mjs.map +1 -0
- package/dist/{chunk-LFWQ36LJ.mjs → chunk-5G5YBFS6.mjs} +4 -4
- package/dist/{chunk-LFWQ36LJ.mjs.map → chunk-5G5YBFS6.mjs.map} +1 -1
- package/dist/{chunk-IHAY6FO6.cjs → chunk-5I5QNGUG.cjs} +17 -17
- package/dist/{chunk-IHAY6FO6.cjs.map → chunk-5I5QNGUG.cjs.map} +1 -1
- package/dist/{chunk-F2CMIIOH.cjs → chunk-76NNDZH6.cjs} +42 -42
- package/dist/{chunk-F2CMIIOH.cjs.map → chunk-76NNDZH6.cjs.map} +1 -1
- package/dist/chunk-B5AWZOHJ.cjs +649 -0
- package/dist/chunk-B5AWZOHJ.cjs.map +1 -0
- package/dist/{chunk-KR6B3LVY.mjs → chunk-B6IR5KSC.mjs} +3 -3
- package/dist/{chunk-KR6B3LVY.mjs.map → chunk-B6IR5KSC.mjs.map} +1 -1
- package/dist/{chunk-5LBDYFWH.mjs → chunk-C6GXVH5J.mjs} +3 -3
- package/dist/{chunk-5LBDYFWH.mjs.map → chunk-C6GXVH5J.mjs.map} +1 -1
- package/dist/{chunk-NRKD4F5X.cjs → chunk-FEN5S772.cjs} +36 -36
- package/dist/{chunk-NRKD4F5X.cjs.map → chunk-FEN5S772.cjs.map} +1 -1
- package/dist/{chunk-2SMCH62O.cjs → chunk-FP2RLYQZ.cjs} +11 -11
- package/dist/{chunk-2SMCH62O.cjs.map → chunk-FP2RLYQZ.cjs.map} +1 -1
- package/dist/{chunk-MOME6KYD.mjs → chunk-G5IEC7SR.mjs} +3 -3
- package/dist/{chunk-MOME6KYD.mjs.map → chunk-G5IEC7SR.mjs.map} +1 -1
- package/dist/{chunk-SE5IERVH.mjs → chunk-GYIO7W7M.mjs} +3 -3
- package/dist/{chunk-SE5IERVH.mjs.map → chunk-GYIO7W7M.mjs.map} +1 -1
- package/dist/{chunk-3Z3A7FHA.cjs → chunk-IEEAENLX.cjs} +48 -48
- package/dist/{chunk-3Z3A7FHA.cjs.map → chunk-IEEAENLX.cjs.map} +1 -1
- package/dist/{chunk-DFTVB66S.cjs → chunk-KNDLV4PI.cjs} +85 -85
- package/dist/{chunk-DFTVB66S.cjs.map → chunk-KNDLV4PI.cjs.map} +1 -1
- package/dist/{chunk-SSUOENAZ.mjs → chunk-KNEQRUBA.mjs} +3 -3
- package/dist/{chunk-SSUOENAZ.mjs.map → chunk-KNEQRUBA.mjs.map} +1 -1
- package/dist/{chunk-CGILA3WO.mjs → chunk-N2XQF2OL.mjs} +5 -3
- package/dist/{chunk-CGILA3WO.mjs.map → chunk-N2XQF2OL.mjs.map} +1 -1
- package/dist/{chunk-EUADAUBQ.mjs → chunk-N4MZYNR4.mjs} +4 -4
- package/dist/{chunk-EUADAUBQ.mjs.map → chunk-N4MZYNR4.mjs.map} +1 -1
- package/dist/{chunk-GGKGH5PM.mjs → chunk-OBRSGM64.mjs} +4 -4
- package/dist/{chunk-GGKGH5PM.mjs.map → chunk-OBRSGM64.mjs.map} +1 -1
- package/dist/{chunk-6JTB2X72.mjs → chunk-ODO4GMW7.mjs} +3 -3
- package/dist/{chunk-6JTB2X72.mjs.map → chunk-ODO4GMW7.mjs.map} +1 -1
- package/dist/{chunk-WGEGR3DF.cjs → chunk-OLISEQHS.cjs} +5 -2
- package/dist/{chunk-WGEGR3DF.cjs.map → chunk-OLISEQHS.cjs.map} +1 -1
- package/dist/{chunk-PZKAH7WQ.mjs → chunk-PVAX67JG.mjs} +3 -3
- package/dist/{chunk-PZKAH7WQ.mjs.map → chunk-PVAX67JG.mjs.map} +1 -1
- package/dist/{chunk-PRPG2T2E.cjs → chunk-QJ6GTUCO.cjs} +6 -6
- package/dist/{chunk-PRPG2T2E.cjs.map → chunk-QJ6GTUCO.cjs.map} +1 -1
- package/dist/chunk-QW4RBGHN.cjs +961 -0
- package/dist/chunk-QW4RBGHN.cjs.map +1 -0
- package/dist/{chunk-33AMWFBZ.cjs → chunk-SGP7V2UW.cjs} +15 -15
- package/dist/{chunk-33AMWFBZ.cjs.map → chunk-SGP7V2UW.cjs.map} +1 -1
- package/dist/{chunk-FX2QFYWF.mjs → chunk-VWQ5WOIL.mjs} +3 -3
- package/dist/{chunk-FX2QFYWF.mjs.map → chunk-VWQ5WOIL.mjs.map} +1 -1
- package/dist/{chunk-ZLQHUZDU.cjs → chunk-YDPDTOSP.cjs} +139 -139
- package/dist/{chunk-ZLQHUZDU.cjs.map → chunk-YDPDTOSP.cjs.map} +1 -1
- package/dist/{chunk-77HQWEQ6.cjs → chunk-YW5IVWHQ.cjs} +33 -33
- package/dist/{chunk-77HQWEQ6.cjs.map → chunk-YW5IVWHQ.cjs.map} +1 -1
- package/dist/chunk-YWSQDBNU.mjs +2339 -0
- package/dist/chunk-YWSQDBNU.mjs.map +1 -0
- package/dist/{chunk-YXBOAGIM.cjs → chunk-YXZ6GU7H.cjs} +7 -7
- package/dist/{chunk-YXBOAGIM.cjs.map → chunk-YXZ6GU7H.cjs.map} +1 -1
- package/dist/{chunk-62Y65TGK.mjs → chunk-ZUFTH5IR.mjs} +8 -631
- package/dist/chunk-ZUFTH5IR.mjs.map +1 -0
- package/dist/chunk-ZWPBBAR2.cjs +2379 -0
- package/dist/chunk-ZWPBBAR2.cjs.map +1 -0
- package/dist/components-EHOGXATG.cjs +22 -0
- package/dist/{components-5UXYNAKR.cjs.map → components-EHOGXATG.cjs.map} +1 -1
- package/dist/components-MQ6DR7TX.cjs +26 -0
- package/dist/{components-CFXOEVPN.mjs.map → components-MQ6DR7TX.cjs.map} +1 -1
- package/dist/components-XRX7QGLB.mjs +5 -0
- package/dist/{components-WYEZL5TE.cjs.map → components-XRX7QGLB.mjs.map} +1 -1
- package/dist/components-YATKRWLH.mjs +5 -0
- package/dist/{components-ZAGG2PBO.mjs.map → components-YATKRWLH.mjs.map} +1 -1
- package/dist/file-icon/index.cjs +6 -6
- package/dist/file-icon/index.mjs +1 -1
- package/dist/index.cjs +739 -215
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1025 -39
- package/dist/index.d.ts +1025 -39
- package/dist/index.mjs +387 -31
- package/dist/index.mjs.map +1 -1
- package/dist/tree/index.cjs +38 -38
- package/dist/tree/index.d.cts +2 -2
- package/dist/tree/index.d.ts +2 -2
- package/dist/tree/index.mjs +3 -3
- package/package.json +6 -6
- package/src/index.ts +5 -0
- package/src/stories/index.ts +3 -1
- package/src/tools/Chat/Chat.story.tsx +1006 -0
- package/src/tools/Chat/README.md +528 -0
- package/src/tools/Chat/components/Attachments.tsx +192 -0
- package/src/tools/Chat/components/ChatRoot.tsx +208 -0
- package/src/tools/Chat/components/Composer.tsx +134 -0
- package/src/tools/Chat/components/EmptyState.tsx +47 -0
- package/src/tools/Chat/components/ErrorBanner.tsx +47 -0
- package/src/tools/Chat/components/JumpToLatest.tsx +30 -0
- package/src/tools/Chat/components/MessageActions.tsx +72 -0
- package/src/tools/Chat/components/MessageBubble.tsx +228 -0
- package/src/tools/Chat/components/MessageList.tsx +82 -0
- package/src/tools/Chat/components/Sources.tsx +55 -0
- package/src/tools/Chat/components/StreamingIndicator.tsx +29 -0
- package/src/tools/Chat/components/ToolCalls.tsx +172 -0
- package/src/tools/Chat/components/index.ts +24 -0
- package/src/tools/Chat/config.ts +55 -0
- package/src/tools/Chat/context/ChatProvider.tsx +126 -0
- package/src/tools/Chat/context/index.ts +9 -0
- package/src/tools/Chat/core/audio/audioBus.ts +172 -0
- package/src/tools/Chat/core/audio/index.ts +8 -0
- package/src/tools/Chat/core/audio/preferences.ts +68 -0
- package/src/tools/Chat/core/audio/types.ts +49 -0
- package/src/tools/Chat/core/ids.ts +16 -0
- package/src/tools/Chat/core/index.ts +5 -0
- package/src/tools/Chat/core/logger.ts +73 -0
- package/src/tools/Chat/core/markdown.ts +56 -0
- package/src/tools/Chat/core/payload-dispatch.ts +54 -0
- package/src/tools/Chat/core/persona.ts +35 -0
- package/src/tools/Chat/core/reducer.ts +335 -0
- package/src/tools/Chat/core/transport/http.ts +167 -0
- package/src/tools/Chat/core/transport/index.ts +13 -0
- package/src/tools/Chat/core/transport/mock.ts +134 -0
- package/src/tools/Chat/core/transport/sse.ts +116 -0
- package/src/tools/Chat/core/transport/types.ts +24 -0
- package/src/tools/Chat/hooks/index.ts +26 -0
- package/src/tools/Chat/hooks/useChat.ts +555 -0
- package/src/tools/Chat/hooks/useChatAudio.ts +191 -0
- package/src/tools/Chat/hooks/useChatComposer.ts +227 -0
- package/src/tools/Chat/hooks/useChatHistory.ts +59 -0
- package/src/tools/Chat/hooks/useChatLayout.ts +111 -0
- package/src/tools/Chat/hooks/useChatLightbox.ts +34 -0
- package/src/tools/Chat/hooks/useChatScroll.ts +132 -0
- package/src/tools/Chat/index.ts +161 -0
- package/src/tools/Chat/lazy.tsx +14 -0
- package/src/tools/Chat/types.ts +237 -0
- package/src/tools/Chat/utils/collectImageAttachments.ts +13 -0
- package/src/tools/Map/README.md +384 -0
- package/dist/DocsLayout-CTJINVBM.mjs.map +0 -1
- package/dist/DocsLayout-XLDB6CJ2.cjs.map +0 -1
- package/dist/JsonSchemaForm-6WMS4CIY.cjs +0 -13
- package/dist/JsonSchemaForm-KX4JT3M4.mjs +0 -4
- package/dist/JsonTree-F27RMYSI.cjs +0 -11
- package/dist/JsonTree-QTJYSHCV.mjs +0 -5
- package/dist/Player-M3GC3VPE.mjs +0 -4
- package/dist/Player-ZL2X5LGG.cjs +0 -13
- package/dist/TreeRoot-A3J65L6F.mjs +0 -4
- package/dist/TreeRoot-DSK5JILT.cjs +0 -19
- package/dist/chunk-62Y65TGK.mjs.map +0 -1
- package/dist/chunk-TKSFZHCG.cjs +0 -1597
- package/dist/chunk-TKSFZHCG.cjs.map +0 -1
- package/dist/components-5UXYNAKR.cjs +0 -22
- package/dist/components-CFXOEVPN.mjs +0 -5
- package/dist/components-WYEZL5TE.cjs +0 -26
- package/dist/components-ZAGG2PBO.mjs +0 -5
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useReducer, useRef } from 'react';
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
ChatAttachment,
|
|
7
|
+
ChatMessage,
|
|
8
|
+
ChatPersona,
|
|
9
|
+
ChatStreamEvent,
|
|
10
|
+
ChatTransport,
|
|
11
|
+
ChatToolCall,
|
|
12
|
+
} from '../types';
|
|
13
|
+
import { LIMITS } from '../config';
|
|
14
|
+
import {
|
|
15
|
+
type ChatState,
|
|
16
|
+
initialState,
|
|
17
|
+
reducer,
|
|
18
|
+
type ChatAction,
|
|
19
|
+
} from '../core/reducer';
|
|
20
|
+
import { createId } from '../core/ids';
|
|
21
|
+
import { getChatLogger } from '../core/logger';
|
|
22
|
+
import { createTokenBuffer } from '../core/markdown';
|
|
23
|
+
|
|
24
|
+
export interface UseChatConfig {
|
|
25
|
+
transport: ChatTransport;
|
|
26
|
+
initialSessionId?: string;
|
|
27
|
+
autoCreateSession?: boolean;
|
|
28
|
+
streaming?: boolean;
|
|
29
|
+
pageSize?: number;
|
|
30
|
+
onError?: (err: Error) => void;
|
|
31
|
+
/** Fires once an assistant message finishes streaming (or buffered send returns). */
|
|
32
|
+
onMessageEnd?: (msg: ChatMessage) => void;
|
|
33
|
+
/** Fires after a user message is added to the state (right before streaming starts). */
|
|
34
|
+
onMessageSent?: (msg: ChatMessage) => void;
|
|
35
|
+
/** Fires when the assistant placeholder is created (first byte / pre-stream). */
|
|
36
|
+
onStreamStart?: (assistantMessageId: string) => void;
|
|
37
|
+
metadata?: Record<string, unknown>;
|
|
38
|
+
/** Stamped on outgoing user messages as `message.sender`. */
|
|
39
|
+
userPersona?: ChatPersona;
|
|
40
|
+
/**
|
|
41
|
+
* Enable verbose dev-mode logging (consola, namespace `chat:*`).
|
|
42
|
+
* Defaults to `isDev` from `@djangocfg/ui-core/lib`. Pass `false` to silence
|
|
43
|
+
* even in development; `true` to force on in production.
|
|
44
|
+
*/
|
|
45
|
+
debug?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface UseChatReturn extends ChatState {
|
|
49
|
+
sendMessage: (content: string, attachments?: ChatAttachment[]) => Promise<void>;
|
|
50
|
+
cancelStream: () => void;
|
|
51
|
+
regenerate: (messageId?: string) => Promise<void>;
|
|
52
|
+
editMessage: (id: string, content: string) => Promise<void>;
|
|
53
|
+
deleteMessage: (id: string) => void;
|
|
54
|
+
clearMessages: () => void;
|
|
55
|
+
loadMore: () => Promise<void>;
|
|
56
|
+
newSession: () => Promise<void>;
|
|
57
|
+
lastError: Error | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useChat(config: UseChatConfig): UseChatReturn {
|
|
61
|
+
const [state, dispatch] = useReducer(reducer, initialState);
|
|
62
|
+
const stateRef = useRef(state);
|
|
63
|
+
stateRef.current = state;
|
|
64
|
+
|
|
65
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
66
|
+
const lastErrorRef = useRef<Error | null>(null);
|
|
67
|
+
const initRef = useRef(false);
|
|
68
|
+
const streamingMsgIdRef = useRef<string | null>(null);
|
|
69
|
+
// Promise resolved once the initial session is available (or `null` when the
|
|
70
|
+
// bootstrap finished without producing one — e.g. autoCreateSession=false).
|
|
71
|
+
// Action methods (sendMessage, regenerate, …) await this so users who type
|
|
72
|
+
// before the first network round-trip resolves don't hit "No active session".
|
|
73
|
+
const bootstrapRef = useRef<Promise<string | null> | null>(null);
|
|
74
|
+
|
|
75
|
+
const { transport, autoCreateSession = true, streaming = true, pageSize = LIMITS.pageSize } =
|
|
76
|
+
config;
|
|
77
|
+
const log = getChatLogger(config.debug);
|
|
78
|
+
|
|
79
|
+
// Initial session bootstrap.
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
if (initRef.current) return;
|
|
82
|
+
initRef.current = true;
|
|
83
|
+
|
|
84
|
+
let cancelled = false;
|
|
85
|
+
// Show "loading" state immediately so the UI doesn't look idle while we
|
|
86
|
+
// wait for createSession / loadHistory to come back.
|
|
87
|
+
if (config.initialSessionId || autoCreateSession) {
|
|
88
|
+
dispatch({ type: 'HISTORY_LOAD_START' });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
log.bootstrap.info('start', {
|
|
92
|
+
mode: config.initialSessionId ? 'resume' : autoCreateSession ? 'create' : 'idle',
|
|
93
|
+
initialSessionId: config.initialSessionId,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const run = async (): Promise<string | null> => {
|
|
97
|
+
const t0 = performance.now();
|
|
98
|
+
try {
|
|
99
|
+
if (config.initialSessionId) {
|
|
100
|
+
dispatch({
|
|
101
|
+
type: 'SESSION_SET',
|
|
102
|
+
sessionId: config.initialSessionId,
|
|
103
|
+
});
|
|
104
|
+
const page = await transport.loadHistory(config.initialSessionId, null, pageSize);
|
|
105
|
+
if (cancelled) {
|
|
106
|
+
log.bootstrap.debug('cancelled (post-loadHistory)');
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
dispatch({
|
|
110
|
+
type: 'HISTORY_LOAD_DONE',
|
|
111
|
+
messages: page.messages,
|
|
112
|
+
hasMore: page.hasMore,
|
|
113
|
+
cursor: page.nextCursor,
|
|
114
|
+
});
|
|
115
|
+
log.bootstrap.success('resumed', {
|
|
116
|
+
sessionId: config.initialSessionId,
|
|
117
|
+
messages: page.messages.length,
|
|
118
|
+
hasMore: page.hasMore,
|
|
119
|
+
elapsedMs: Math.round(performance.now() - t0),
|
|
120
|
+
});
|
|
121
|
+
return config.initialSessionId;
|
|
122
|
+
}
|
|
123
|
+
if (autoCreateSession) {
|
|
124
|
+
const info = await transport.createSession({ metadata: config.metadata });
|
|
125
|
+
if (cancelled) {
|
|
126
|
+
log.bootstrap.debug('cancelled (post-createSession)');
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
dispatch({
|
|
130
|
+
type: 'SESSION_SET',
|
|
131
|
+
sessionId: info.sessionId,
|
|
132
|
+
messages: info.messages ?? [],
|
|
133
|
+
hasMore: info.hasMore ?? false,
|
|
134
|
+
cursor: info.cursor ?? null,
|
|
135
|
+
});
|
|
136
|
+
// SESSION_SET implicitly clears `error` and leaves isLoading from
|
|
137
|
+
// the earlier HISTORY_LOAD_START set; mark history as done so the
|
|
138
|
+
// composer un-disables.
|
|
139
|
+
dispatch({
|
|
140
|
+
type: 'HISTORY_LOAD_DONE',
|
|
141
|
+
messages: info.messages ?? [],
|
|
142
|
+
hasMore: info.hasMore ?? false,
|
|
143
|
+
cursor: info.cursor ?? null,
|
|
144
|
+
});
|
|
145
|
+
log.bootstrap.success('created', {
|
|
146
|
+
sessionId: info.sessionId,
|
|
147
|
+
resumed: info.resumed ?? false,
|
|
148
|
+
elapsedMs: Math.round(performance.now() - t0),
|
|
149
|
+
});
|
|
150
|
+
return info.sessionId;
|
|
151
|
+
}
|
|
152
|
+
log.bootstrap.debug('idle (no initialSessionId, autoCreateSession=false)');
|
|
153
|
+
return null;
|
|
154
|
+
} catch (err) {
|
|
155
|
+
if (cancelled) {
|
|
156
|
+
log.bootstrap.debug('cancelled (in catch)');
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
160
|
+
lastErrorRef.current = e;
|
|
161
|
+
dispatch({ type: 'ERROR_SET', error: e.message });
|
|
162
|
+
config.onError?.(e);
|
|
163
|
+
log.error.error('bootstrap failed', { message: e.message, elapsedMs: Math.round(performance.now() - t0) });
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
bootstrapRef.current = run();
|
|
168
|
+
return () => {
|
|
169
|
+
cancelled = true;
|
|
170
|
+
};
|
|
171
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
172
|
+
}, []);
|
|
173
|
+
|
|
174
|
+
/** Wait for the initial session bootstrap to settle, then return whatever
|
|
175
|
+
* sessionId is now in state. Safe to call multiple times. */
|
|
176
|
+
const awaitSession = useCallback(async (): Promise<string | null> => {
|
|
177
|
+
if (stateRef.current.sessionId) return stateRef.current.sessionId;
|
|
178
|
+
if (bootstrapRef.current) {
|
|
179
|
+
const id = await bootstrapRef.current;
|
|
180
|
+
if (id) return id;
|
|
181
|
+
}
|
|
182
|
+
return stateRef.current.sessionId;
|
|
183
|
+
}, []);
|
|
184
|
+
|
|
185
|
+
const consumeStream = useCallback(
|
|
186
|
+
async (
|
|
187
|
+
sessionId: string,
|
|
188
|
+
content: string,
|
|
189
|
+
attachments?: ChatAttachment[],
|
|
190
|
+
): Promise<void> => {
|
|
191
|
+
const ctrl = new AbortController();
|
|
192
|
+
abortRef.current = ctrl;
|
|
193
|
+
const assistantId = createId('a');
|
|
194
|
+
streamingMsgIdRef.current = assistantId;
|
|
195
|
+
|
|
196
|
+
dispatch({ type: 'STREAM_START', id: assistantId });
|
|
197
|
+
config.onStreamStart?.(assistantId);
|
|
198
|
+
log.stream.info('start', { sessionId, assistantId, chars: content.length });
|
|
199
|
+
|
|
200
|
+
const tokenBuffer = createTokenBuffer((delta) =>
|
|
201
|
+
dispatch({ type: 'STREAM_CHUNK', delta }),
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
let serverMessageId: string | null = null;
|
|
205
|
+
let chunkCount = 0;
|
|
206
|
+
let charsReceived = 0;
|
|
207
|
+
const t0 = performance.now();
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
const iterator = transport.stream(sessionId, content, {
|
|
211
|
+
signal: ctrl.signal,
|
|
212
|
+
attachments,
|
|
213
|
+
metadata: config.metadata,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
for await (const ev of iterator) {
|
|
217
|
+
if (ctrl.signal.aborted) break;
|
|
218
|
+
handleEvent(ev);
|
|
219
|
+
}
|
|
220
|
+
tokenBuffer.flush();
|
|
221
|
+
|
|
222
|
+
// If transport never emitted message_end, finalize manually.
|
|
223
|
+
if (stateRef.current.isStreaming) {
|
|
224
|
+
dispatch({ type: 'STREAM_DONE', id: assistantId });
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const finalMsg = stateRef.current.messages.find((m) => m.id === assistantId);
|
|
228
|
+
if (finalMsg) config.onMessageEnd?.(finalMsg);
|
|
229
|
+
log.stream.success('done', {
|
|
230
|
+
assistantId,
|
|
231
|
+
chunks: chunkCount,
|
|
232
|
+
chars: charsReceived,
|
|
233
|
+
elapsedMs: Math.round(performance.now() - t0),
|
|
234
|
+
});
|
|
235
|
+
} catch (err) {
|
|
236
|
+
tokenBuffer.close();
|
|
237
|
+
if (ctrl.signal.aborted) {
|
|
238
|
+
const partial =
|
|
239
|
+
stateRef.current.messages.find((m) => m.id === assistantId)?.content ?? '';
|
|
240
|
+
dispatch({ type: 'STREAM_CANCELLED', id: assistantId, partialText: partial });
|
|
241
|
+
log.stream.warn('cancelled', { assistantId, partialChars: partial.length });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
245
|
+
lastErrorRef.current = e;
|
|
246
|
+
dispatch({ type: 'STREAM_ERROR', id: assistantId, message: e.message });
|
|
247
|
+
config.onError?.(e);
|
|
248
|
+
log.error.error('stream failed', { assistantId, message: e.message });
|
|
249
|
+
} finally {
|
|
250
|
+
tokenBuffer.close();
|
|
251
|
+
if (abortRef.current === ctrl) abortRef.current = null;
|
|
252
|
+
streamingMsgIdRef.current = null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function handleEvent(ev: ChatStreamEvent) {
|
|
256
|
+
switch (ev.type) {
|
|
257
|
+
case 'message_start':
|
|
258
|
+
serverMessageId = ev.messageId;
|
|
259
|
+
log.stream.debug('message_start', { messageId: ev.messageId });
|
|
260
|
+
return;
|
|
261
|
+
case 'chunk':
|
|
262
|
+
tokenBuffer.push(ev.delta);
|
|
263
|
+
chunkCount += 1;
|
|
264
|
+
charsReceived += ev.delta.length;
|
|
265
|
+
return;
|
|
266
|
+
case 'tool_activity':
|
|
267
|
+
tokenBuffer.flush();
|
|
268
|
+
dispatch({ type: 'STREAM_TOOL_ACTIVITY', tool: ev.tool });
|
|
269
|
+
log.tools.debug('activity', { tool: ev.tool, status: ev.status });
|
|
270
|
+
return;
|
|
271
|
+
case 'tool_call_start': {
|
|
272
|
+
tokenBuffer.flush();
|
|
273
|
+
const toolCall: ChatToolCall = {
|
|
274
|
+
id: ev.toolId,
|
|
275
|
+
name: ev.name,
|
|
276
|
+
input: ev.input,
|
|
277
|
+
status: 'running',
|
|
278
|
+
startedAt: Date.now(),
|
|
279
|
+
sourceHostname: ev.sourceHostname,
|
|
280
|
+
};
|
|
281
|
+
dispatch({
|
|
282
|
+
type: 'TOOL_CALL_START',
|
|
283
|
+
messageId: assistantId,
|
|
284
|
+
toolCall,
|
|
285
|
+
});
|
|
286
|
+
log.tools.info('call_start', { toolId: ev.toolId, name: ev.name });
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
case 'tool_call_delta':
|
|
290
|
+
dispatch({
|
|
291
|
+
type: 'TOOL_CALL_DELTA',
|
|
292
|
+
messageId: assistantId,
|
|
293
|
+
toolId: ev.toolId,
|
|
294
|
+
delta: ev.delta,
|
|
295
|
+
});
|
|
296
|
+
return;
|
|
297
|
+
case 'tool_call_end':
|
|
298
|
+
dispatch({
|
|
299
|
+
type: 'TOOL_CALL_END',
|
|
300
|
+
messageId: assistantId,
|
|
301
|
+
toolId: ev.toolId,
|
|
302
|
+
output: ev.output,
|
|
303
|
+
status: ev.status,
|
|
304
|
+
});
|
|
305
|
+
log.tools.info('call_end', { toolId: ev.toolId, status: ev.status });
|
|
306
|
+
return;
|
|
307
|
+
case 'message_end':
|
|
308
|
+
tokenBuffer.flush();
|
|
309
|
+
dispatch({
|
|
310
|
+
type: 'STREAM_DONE',
|
|
311
|
+
id: assistantId,
|
|
312
|
+
tokensIn: ev.tokensIn,
|
|
313
|
+
tokensOut: ev.tokensOut,
|
|
314
|
+
sources: ev.sources,
|
|
315
|
+
});
|
|
316
|
+
log.stream.debug('message_end', {
|
|
317
|
+
tokensIn: ev.tokensIn,
|
|
318
|
+
tokensOut: ev.tokensOut,
|
|
319
|
+
sources: ev.sources?.length ?? 0,
|
|
320
|
+
});
|
|
321
|
+
return;
|
|
322
|
+
case 'error':
|
|
323
|
+
tokenBuffer.flush();
|
|
324
|
+
dispatch({
|
|
325
|
+
type: 'STREAM_ERROR',
|
|
326
|
+
id: assistantId,
|
|
327
|
+
message: ev.message,
|
|
328
|
+
});
|
|
329
|
+
log.error.error('stream event error', { code: ev.code, message: ev.message });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
// unreachable; prevents unused-var on serverMessageId
|
|
333
|
+
void serverMessageId;
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
[transport, config],
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
const consumeBuffered = useCallback(
|
|
340
|
+
async (sessionId: string, content: string, attachments?: ChatAttachment[]): Promise<void> => {
|
|
341
|
+
const ctrl = new AbortController();
|
|
342
|
+
abortRef.current = ctrl;
|
|
343
|
+
try {
|
|
344
|
+
const reply = await transport.send(sessionId, content, {
|
|
345
|
+
signal: ctrl.signal,
|
|
346
|
+
attachments,
|
|
347
|
+
metadata: config.metadata,
|
|
348
|
+
});
|
|
349
|
+
const placeholderId = createId('a');
|
|
350
|
+
dispatch({ type: 'STREAM_START', id: placeholderId });
|
|
351
|
+
config.onStreamStart?.(placeholderId);
|
|
352
|
+
dispatch({ type: 'STREAM_CHUNK', delta: reply.content });
|
|
353
|
+
dispatch({ type: 'STREAM_DONE', id: placeholderId });
|
|
354
|
+
config.onMessageEnd?.(reply);
|
|
355
|
+
} catch (err) {
|
|
356
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
357
|
+
lastErrorRef.current = e;
|
|
358
|
+
dispatch({ type: 'STREAM_ERROR', message: e.message });
|
|
359
|
+
config.onError?.(e);
|
|
360
|
+
} finally {
|
|
361
|
+
if (abortRef.current === ctrl) abortRef.current = null;
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
[transport, config],
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const sendMessage = useCallback(
|
|
368
|
+
async (content: string, attachments?: ChatAttachment[]) => {
|
|
369
|
+
// Wait for the initial session bootstrap if it's still in flight.
|
|
370
|
+
// Without this, fast typers hit "No active session" before
|
|
371
|
+
// transport.createSession resolves.
|
|
372
|
+
log.lifecycle.info('sendMessage', {
|
|
373
|
+
chars: content.length,
|
|
374
|
+
attachments: attachments?.length ?? 0,
|
|
375
|
+
hasSession: !!stateRef.current.sessionId,
|
|
376
|
+
});
|
|
377
|
+
const sessionId = await awaitSession();
|
|
378
|
+
if (!sessionId) {
|
|
379
|
+
const e = new Error('No active session');
|
|
380
|
+
lastErrorRef.current = e;
|
|
381
|
+
dispatch({ type: 'ERROR_SET', error: e.message });
|
|
382
|
+
config.onError?.(e);
|
|
383
|
+
log.error.error('sendMessage aborted: no session');
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (!content.trim() && !(attachments && attachments.length > 0)) {
|
|
387
|
+
log.lifecycle.debug('sendMessage skipped (empty)');
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (stateRef.current.isStreaming) {
|
|
391
|
+
log.lifecycle.debug('sendMessage skipped (already streaming)');
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const userMsg: ChatMessage = {
|
|
396
|
+
id: createId('u'),
|
|
397
|
+
role: 'user',
|
|
398
|
+
content,
|
|
399
|
+
createdAt: Date.now(),
|
|
400
|
+
attachments,
|
|
401
|
+
sender: config.userPersona,
|
|
402
|
+
};
|
|
403
|
+
dispatch({ type: 'MESSAGE_USER_ADD', message: userMsg });
|
|
404
|
+
config.onMessageSent?.(userMsg);
|
|
405
|
+
|
|
406
|
+
if (streaming) {
|
|
407
|
+
await consumeStream(sessionId, content, attachments);
|
|
408
|
+
} else {
|
|
409
|
+
await consumeBuffered(sessionId, content, attachments);
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
[streaming, consumeStream, consumeBuffered, config, awaitSession],
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
const cancelStream = useCallback(() => {
|
|
416
|
+
abortRef.current?.abort();
|
|
417
|
+
}, []);
|
|
418
|
+
|
|
419
|
+
const regenerate = useCallback(
|
|
420
|
+
async (messageId?: string) => {
|
|
421
|
+
log.lifecycle.info('regenerate', { messageId: messageId ?? '(last)' });
|
|
422
|
+
const messages = stateRef.current.messages;
|
|
423
|
+
let targetUserIdx = -1;
|
|
424
|
+
if (messageId) {
|
|
425
|
+
const idx = messages.findIndex((m) => m.id === messageId);
|
|
426
|
+
if (idx !== -1) {
|
|
427
|
+
targetUserIdx =
|
|
428
|
+
messages[idx].role === 'user' ? idx : findPreviousUserIndex(messages, idx);
|
|
429
|
+
}
|
|
430
|
+
} else {
|
|
431
|
+
targetUserIdx = findLastUserIndex(messages);
|
|
432
|
+
}
|
|
433
|
+
if (targetUserIdx === -1) return;
|
|
434
|
+
const userMsg = messages[targetUserIdx];
|
|
435
|
+
// Drop everything after this user message.
|
|
436
|
+
for (let i = messages.length - 1; i > targetUserIdx; i -= 1) {
|
|
437
|
+
dispatch({ type: 'MESSAGE_DELETE', id: messages[i].id });
|
|
438
|
+
}
|
|
439
|
+
const sessionId = await awaitSession();
|
|
440
|
+
if (!sessionId) return;
|
|
441
|
+
if (streaming) {
|
|
442
|
+
await consumeStream(sessionId, userMsg.content, userMsg.attachments);
|
|
443
|
+
} else {
|
|
444
|
+
await consumeBuffered(sessionId, userMsg.content, userMsg.attachments);
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
[streaming, consumeStream, consumeBuffered, awaitSession],
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
const editMessage = useCallback(
|
|
451
|
+
async (id: string, content: string) => {
|
|
452
|
+
dispatch({ type: 'MESSAGE_EDIT', id, content });
|
|
453
|
+
const msg = stateRef.current.messages.find((m) => m.id === id);
|
|
454
|
+
if (msg?.role === 'user') {
|
|
455
|
+
await regenerate(id);
|
|
456
|
+
}
|
|
457
|
+
},
|
|
458
|
+
[regenerate],
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
const deleteMessage = useCallback((id: string) => {
|
|
462
|
+
dispatch({ type: 'MESSAGE_DELETE', id });
|
|
463
|
+
}, []);
|
|
464
|
+
|
|
465
|
+
const clearMessages = useCallback(() => {
|
|
466
|
+
abortRef.current?.abort();
|
|
467
|
+
dispatch({ type: 'MESSAGES_CLEAR' });
|
|
468
|
+
}, []);
|
|
469
|
+
|
|
470
|
+
const loadMore = useCallback(async () => {
|
|
471
|
+
const sessionId = stateRef.current.sessionId;
|
|
472
|
+
if (!sessionId) return;
|
|
473
|
+
if (stateRef.current.isLoadingMore || !stateRef.current.hasMore) return;
|
|
474
|
+
dispatch({ type: 'HISTORY_MORE_START' });
|
|
475
|
+
try {
|
|
476
|
+
const page = await transport.loadHistory(
|
|
477
|
+
sessionId,
|
|
478
|
+
stateRef.current.oldestCursor,
|
|
479
|
+
pageSize,
|
|
480
|
+
);
|
|
481
|
+
dispatch({
|
|
482
|
+
type: 'HISTORY_MORE_DONE',
|
|
483
|
+
messages: page.messages,
|
|
484
|
+
hasMore: page.hasMore,
|
|
485
|
+
cursor: page.nextCursor,
|
|
486
|
+
});
|
|
487
|
+
} catch (err) {
|
|
488
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
489
|
+
lastErrorRef.current = e;
|
|
490
|
+
dispatch({ type: 'ERROR_SET', error: e.message });
|
|
491
|
+
config.onError?.(e);
|
|
492
|
+
}
|
|
493
|
+
}, [transport, pageSize, config]);
|
|
494
|
+
|
|
495
|
+
const newSession = useCallback(async () => {
|
|
496
|
+
log.lifecycle.info('newSession', { previous: stateRef.current.sessionId });
|
|
497
|
+
abortRef.current?.abort();
|
|
498
|
+
const previous = stateRef.current.sessionId;
|
|
499
|
+
if (previous) {
|
|
500
|
+
try {
|
|
501
|
+
await transport.closeSession(previous);
|
|
502
|
+
} catch {
|
|
503
|
+
/* ignore */
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
dispatch({ type: 'MESSAGES_CLEAR' });
|
|
507
|
+
try {
|
|
508
|
+
const info = await transport.createSession({ metadata: config.metadata });
|
|
509
|
+
dispatch({
|
|
510
|
+
type: 'SESSION_SET',
|
|
511
|
+
sessionId: info.sessionId,
|
|
512
|
+
messages: info.messages ?? [],
|
|
513
|
+
hasMore: info.hasMore ?? false,
|
|
514
|
+
cursor: info.cursor ?? null,
|
|
515
|
+
});
|
|
516
|
+
log.lifecycle.success('newSession ok', { sessionId: info.sessionId });
|
|
517
|
+
} catch (err) {
|
|
518
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
519
|
+
lastErrorRef.current = e;
|
|
520
|
+
dispatch({ type: 'ERROR_SET', error: e.message });
|
|
521
|
+
config.onError?.(e);
|
|
522
|
+
log.error.error('newSession failed', { message: e.message });
|
|
523
|
+
}
|
|
524
|
+
}, [transport, config]);
|
|
525
|
+
|
|
526
|
+
return {
|
|
527
|
+
...state,
|
|
528
|
+
sendMessage,
|
|
529
|
+
cancelStream,
|
|
530
|
+
regenerate,
|
|
531
|
+
editMessage,
|
|
532
|
+
deleteMessage,
|
|
533
|
+
clearMessages,
|
|
534
|
+
loadMore,
|
|
535
|
+
newSession,
|
|
536
|
+
lastError: lastErrorRef.current,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function findLastUserIndex(messages: ChatMessage[]): number {
|
|
541
|
+
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
|
542
|
+
if (messages[i].role === 'user') return i;
|
|
543
|
+
}
|
|
544
|
+
return -1;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function findPreviousUserIndex(messages: ChatMessage[], from: number): number {
|
|
548
|
+
for (let i = from - 1; i >= 0; i -= 1) {
|
|
549
|
+
if (messages[i].role === 'user') return i;
|
|
550
|
+
}
|
|
551
|
+
return -1;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Suppress unused-action warnings if the action union grows.
|
|
555
|
+
type _Used = ChatAction;
|