@schandlergarcia/sf-web-components 1.8.0 → 1.9.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/components/library/cards/SemanticTableCard.d.ts +1 -1
- package/dist/components/library/chat/ChatBar.d.ts +14 -11
- package/dist/components/library/chat/ChatBar.js +2 -3
- package/dist/components/library/chat/ChatBar.js.map +1 -1
- package/dist/components/library/chat/ChatInput.d.ts +9 -8
- package/dist/components/library/chat/ChatInput.js.map +1 -1
- package/dist/components/library/chat/ChatMessage.d.ts +18 -4
- package/dist/components/library/chat/ChatMessage.js.map +1 -1
- package/dist/components/library/chat/ChatMessageList.d.ts +11 -8
- package/dist/components/library/chat/ChatMessageList.js.map +1 -1
- package/dist/components/library/chat/ChatPanel.d.ts +16 -12
- package/dist/components/library/chat/ChatPanel.js +8 -9
- package/dist/components/library/chat/ChatPanel.js.map +1 -1
- package/dist/components/library/chat/ChatSuggestions.d.ts +5 -4
- package/dist/components/library/chat/ChatSuggestions.js +2 -3
- package/dist/components/library/chat/ChatSuggestions.js.map +1 -1
- package/dist/components/library/chat/ChatToolCall.d.ts +11 -3
- package/dist/components/library/chat/ChatToolCall.js.map +1 -1
- package/dist/components/library/chat/ChatTypingIndicator.d.ts +4 -3
- package/dist/components/library/chat/ChatTypingIndicator.js +2 -3
- package/dist/components/library/chat/ChatTypingIndicator.js.map +1 -1
- package/dist/components/library/chat/ChatWelcome.d.ts +9 -7
- package/dist/components/library/chat/ChatWelcome.js +6 -7
- package/dist/components/library/chat/ChatWelcome.js.map +1 -1
- package/dist/components/library/chat/index.d.ts +10 -0
- package/dist/components/library/chat/useChatState.d.ts +37 -11
- package/dist/components/library/chat/useChatState.js +63 -46
- package/dist/components/library/chat/useChatState.js.map +1 -1
- package/dist/components/library/data/DataModeProvider.d.ts +15 -11
- package/dist/components/library/data/DataModeProvider.js +1 -1
- package/dist/components/library/data/DataModeProvider.js.map +1 -1
- package/dist/components/library/data/DataModeToggle.d.ts +4 -3
- package/dist/components/library/data/DataModeToggle.js +4 -5
- package/dist/components/library/data/DataModeToggle.js.map +1 -1
- package/dist/components/library/data/chartDataProvider.d.ts +41 -3
- package/dist/components/library/data/filterUtils.d.ts +38 -9
- package/dist/components/library/data/filterUtils.js.map +1 -1
- package/dist/components/library/data/useDataSource.d.ts +6 -4
- package/dist/components/library/data/useDataSource.js.map +1 -1
- package/dist/components/library/data/usePageFilters.d.ts +31 -5
- package/dist/components/library/data/usePageFilters.js +6 -2
- package/dist/components/library/data/usePageFilters.js.map +1 -1
- package/dist/components/library/index.d.ts +92 -73
- package/dist/components/library/index.js +25 -25
- package/dist/components/library/index.js.map +1 -1
- package/dist/components/library/skeletons/CardSkeleton.d.ts +5 -4
- package/dist/components/library/skeletons/CardSkeleton.js +2 -3
- package/dist/components/library/skeletons/CardSkeleton.js.map +1 -1
- package/dist/components/library/theme/AppThemeProvider.d.ts +13 -50
- package/dist/components/library/theme/AppThemeProvider.js.map +1 -1
- package/dist/components/library/theme/tokens.d.ts +45 -44
- package/dist/components/library/theme/tokens.js.map +1 -1
- package/dist/components/workspace/ComponentRegistry.d.ts +116 -140
- package/dist/components/workspace/ComponentRegistry.js +43 -43
- package/dist/components/workspace/ComponentRegistry.js.map +1 -1
- package/package.json +2 -2
- package/src/components/library/cards/SemanticMetricCard.tsx +1 -1
- package/src/components/library/cards/SemanticTableCard.tsx +3 -3
- package/src/components/library/chat/{ChatBar.jsx → ChatBar.tsx} +19 -8
- package/src/components/library/chat/{ChatInput.jsx → ChatInput.tsx} +13 -11
- package/src/components/library/chat/{ChatMessage.jsx → ChatMessage.tsx} +23 -10
- package/src/components/library/chat/{ChatMessageList.jsx → ChatMessageList.tsx} +13 -11
- package/src/components/library/chat/{ChatPanel.jsx → ChatPanel.tsx} +16 -13
- package/src/components/library/chat/{ChatSuggestions.jsx → ChatSuggestions.tsx} +6 -5
- package/src/components/library/chat/{ChatToolCall.jsx → ChatToolCall.tsx} +14 -4
- package/src/components/library/chat/{ChatTypingIndicator.jsx → ChatTypingIndicator.tsx} +5 -2
- package/src/components/library/chat/{ChatWelcome.jsx → ChatWelcome.tsx} +9 -7
- package/src/components/library/chat/index.tsx +26 -0
- package/src/components/library/chat/useChatState.tsx +182 -0
- package/src/components/library/data/{DataModeProvider.jsx → DataModeProvider.tsx} +25 -8
- package/src/components/library/data/{DataModeToggle.jsx → DataModeToggle.tsx} +5 -2
- package/src/components/library/data/{chartDataProvider.jsx → chartDataProvider.tsx} +49 -5
- package/src/components/library/data/{filterUtils.jsx → filterUtils.tsx} +58 -12
- package/src/components/library/data/{useDataSource.jsx → useDataSource.tsx} +9 -2
- package/src/components/library/data/{usePageFilters.jsx → usePageFilters.tsx} +49 -9
- package/src/components/library/{index.jsx → index.ts} +14 -14
- package/src/components/library/skeletons/{CardSkeleton.jsx → CardSkeleton.tsx} +5 -4
- package/src/components/library/theme/{AppThemeProvider.jsx → AppThemeProvider.tsx} +20 -7
- package/src/components/library/theme/{tokens.jsx → tokens.tsx} +37 -3
- package/src/components/workspace/{ComponentRegistry.jsx → ComponentRegistry.tsx} +137 -32
- package/src/components/library/chat/index.jsx +0 -10
- package/src/components/library/chat/useChatState.jsx +0 -130
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { useState, useCallback, useRef } from "react";
|
|
2
|
-
|
|
3
|
-
let _nextId = 1;
|
|
4
|
-
function uid() {
|
|
5
|
-
return `msg-${Date.now()}-${_nextId++}`;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Core state management hook for AI chat.
|
|
10
|
-
*
|
|
11
|
-
* @param {Object} options
|
|
12
|
-
* @param {Array} options.initialMessages — seed the conversation
|
|
13
|
-
* @param {Function} options.onSend — async (userMessage, allMessages) => assistantMessage | void
|
|
14
|
-
* Return a message object to add it, or use appendChunk / addMessage for streaming.
|
|
15
|
-
*
|
|
16
|
-
* @returns {Object}
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const chat = useChatState({
|
|
20
|
-
* onSend: async (msg, history) => {
|
|
21
|
-
* const res = await fetch("/api/chat", { method: "POST", body: JSON.stringify({ messages: history }) });
|
|
22
|
-
* const data = await res.json();
|
|
23
|
-
* return { role: "assistant", content: data.reply, components: data.components };
|
|
24
|
-
* },
|
|
25
|
-
* });
|
|
26
|
-
*/
|
|
27
|
-
export default function useChatState({ initialMessages = [], onSend } = {}) {
|
|
28
|
-
const [messages, setMessages] = useState(() =>
|
|
29
|
-
initialMessages.map((m) => ({ id: uid(), timestamp: new Date().toISOString(), ...m }))
|
|
30
|
-
);
|
|
31
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
32
|
-
const [isStreaming, setIsStreaming] = useState(false);
|
|
33
|
-
const [error, setError] = useState(null);
|
|
34
|
-
const onSendRef = useRef(onSend);
|
|
35
|
-
onSendRef.current = onSend;
|
|
36
|
-
|
|
37
|
-
const addMessage = useCallback((msg) => {
|
|
38
|
-
const full = { id: uid(), timestamp: new Date().toISOString(), ...msg };
|
|
39
|
-
setMessages((prev) => [...prev, full]);
|
|
40
|
-
return full.id;
|
|
41
|
-
}, []);
|
|
42
|
-
|
|
43
|
-
const updateMessage = useCallback((id, updates) => {
|
|
44
|
-
setMessages((prev) =>
|
|
45
|
-
prev.map((m) => (m.id === id ? { ...m, ...updates } : m))
|
|
46
|
-
);
|
|
47
|
-
}, []);
|
|
48
|
-
|
|
49
|
-
const appendChunk = useCallback((id, chunk) => {
|
|
50
|
-
setMessages((prev) =>
|
|
51
|
-
prev.map((m) =>
|
|
52
|
-
m.id === id ? { ...m, content: (m.content ?? "") + chunk } : m
|
|
53
|
-
)
|
|
54
|
-
);
|
|
55
|
-
}, []);
|
|
56
|
-
|
|
57
|
-
const removeMessage = useCallback((id) => {
|
|
58
|
-
setMessages((prev) => prev.filter((m) => m.id !== id));
|
|
59
|
-
}, []);
|
|
60
|
-
|
|
61
|
-
const clearMessages = useCallback(() => {
|
|
62
|
-
setMessages([]);
|
|
63
|
-
setError(null);
|
|
64
|
-
}, []);
|
|
65
|
-
|
|
66
|
-
const sendMessage = useCallback(
|
|
67
|
-
async (content, extra = {}) => {
|
|
68
|
-
if (!content?.trim()) return;
|
|
69
|
-
setError(null);
|
|
70
|
-
|
|
71
|
-
const userMsg = { role: "user", content: content.trim(), ...extra };
|
|
72
|
-
const userId = uid();
|
|
73
|
-
const fullUser = { ...userMsg, id: userId, timestamp: new Date().toISOString() };
|
|
74
|
-
|
|
75
|
-
setMessages((prev) => [...prev, fullUser]);
|
|
76
|
-
setIsLoading(true);
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const allMsgs = [...messages, fullUser];
|
|
80
|
-
const result = await onSendRef.current?.(fullUser, allMsgs, {
|
|
81
|
-
addMessage,
|
|
82
|
-
updateMessage,
|
|
83
|
-
appendChunk,
|
|
84
|
-
setStreaming: setIsStreaming,
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
if (result && typeof result === "object" && result.role) {
|
|
88
|
-
addMessage(result);
|
|
89
|
-
}
|
|
90
|
-
} catch (err) {
|
|
91
|
-
setError(err?.message ?? "Failed to send message");
|
|
92
|
-
addMessage({
|
|
93
|
-
role: "system",
|
|
94
|
-
content: err?.message ?? "Something went wrong. Please try again.",
|
|
95
|
-
isError: true,
|
|
96
|
-
});
|
|
97
|
-
} finally {
|
|
98
|
-
setIsLoading(false);
|
|
99
|
-
setIsStreaming(false);
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
[messages, addMessage, updateMessage, appendChunk]
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
const retryLast = useCallback(() => {
|
|
106
|
-
const lastUser = [...messages].reverse().find((m) => m.role === "user");
|
|
107
|
-
if (!lastUser) return;
|
|
108
|
-
|
|
109
|
-
const idx = messages.lastIndexOf(lastUser);
|
|
110
|
-
setMessages(messages.slice(0, idx));
|
|
111
|
-
setError(null);
|
|
112
|
-
|
|
113
|
-
sendMessage(lastUser.content);
|
|
114
|
-
}, [messages, sendMessage]);
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
messages,
|
|
118
|
-
isLoading,
|
|
119
|
-
isStreaming,
|
|
120
|
-
error,
|
|
121
|
-
sendMessage,
|
|
122
|
-
addMessage,
|
|
123
|
-
updateMessage,
|
|
124
|
-
appendChunk,
|
|
125
|
-
removeMessage,
|
|
126
|
-
clearMessages,
|
|
127
|
-
retryLast,
|
|
128
|
-
setError,
|
|
129
|
-
};
|
|
130
|
-
}
|