mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 04:07:52 +01:00
Seven hand-built chats (/demo, the professional demos, quick-create, /try, /my-data documents, custom bots and the guided demo) now share one EmbeddedChat panel: chatkit's ChatThread and Composer with the microphone, Copy/Listen, Stop, Retry on a failed turn, starters and suggested replies. Each surface keeps its own prompt, route and persistence through a small useChatTurns hook. Also fixes /demo and the /my-data document chat reading the answer from the wrong level of the jsonSuccess envelope, which showed empty answers. Claude-Session: https://claude.ai/code/session_019MDtmuMRAXYWNkfowAXVfi Co-authored-by: Claude <noreply@anthropic.com>
180 lines
5.1 KiB
TypeScript
180 lines
5.1 KiB
TypeScript
import { useState, useEffect, useCallback } from 'react';
|
|
import { conversationToasts } from '@/lib/toast';
|
|
import type { Conversation, ConversationMessage, MessageSource } from '@/types/conversation';
|
|
|
|
export interface ChatMessage {
|
|
id?: string;
|
|
role: 'user' | 'assistant';
|
|
content: string;
|
|
sources?: MessageSource[];
|
|
}
|
|
|
|
interface UseDocumentChatOptions {
|
|
selectedDocId: string | null;
|
|
}
|
|
|
|
export const useDocumentChat = ({ selectedDocId }: UseDocumentChatOptions) => {
|
|
const [chatMessages, setChatMessages] = useState<ChatMessage[]>([]);
|
|
const [chatLoading, setChatLoading] = useState(false);
|
|
const [activeConversationId, setActiveConversationId] = useState<string | null>(null);
|
|
const [conversationRefreshTrigger, setConversationRefreshTrigger] = useState(0);
|
|
|
|
// Load conversation messages when active conversation changes
|
|
useEffect(() => {
|
|
if (!activeConversationId) {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets messages when the active conversation clears
|
|
setChatMessages([]);
|
|
return;
|
|
}
|
|
|
|
const loadConversation = async () => {
|
|
try {
|
|
const response = await fetch(`/api/conversations/${activeConversationId}`);
|
|
const data = await response.json();
|
|
|
|
if (data.success && data.data?.conversation) {
|
|
const conversation: Conversation & { messages: ConversationMessage[] } =
|
|
data.data.conversation;
|
|
setChatMessages(
|
|
conversation.messages.map((msg) => ({
|
|
id: msg.id,
|
|
role: msg.role,
|
|
content: msg.content,
|
|
sources: msg.sources ?? undefined,
|
|
})),
|
|
);
|
|
}
|
|
} catch {
|
|
conversationToasts.loadError();
|
|
}
|
|
};
|
|
|
|
loadConversation();
|
|
}, [activeConversationId]);
|
|
|
|
const createNewConversation = useCallback(async (): Promise<string | null> => {
|
|
try {
|
|
const response = await fetch('/api/conversations', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
bot_type: 'documents',
|
|
document_id: selectedDocId,
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (data.success && data.data?.conversation) {
|
|
setConversationRefreshTrigger((prev) => prev + 1);
|
|
return data.data.conversation.id;
|
|
}
|
|
} catch {
|
|
conversationToasts.createError();
|
|
}
|
|
return null;
|
|
}, [selectedDocId]);
|
|
|
|
const saveMessageToConversation = async (conversationId: string, message: ChatMessage) => {
|
|
try {
|
|
await fetch(`/api/conversations/${conversationId}/messages`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
role: message.role,
|
|
content: message.content,
|
|
sources: message.sources,
|
|
}),
|
|
});
|
|
} catch {
|
|
// Failed to persist message - non-critical
|
|
}
|
|
};
|
|
|
|
const handleChat = async (text: string) => {
|
|
const message = text.trim();
|
|
if (!message || chatLoading) return;
|
|
|
|
// Create new conversation if needed
|
|
let convId = activeConversationId;
|
|
if (!convId) {
|
|
convId = await createNewConversation();
|
|
if (convId) {
|
|
setActiveConversationId(convId);
|
|
}
|
|
}
|
|
|
|
const userMessage: ChatMessage = { role: 'user', content: message };
|
|
setChatMessages((prev) => [...prev, userMessage]);
|
|
setChatLoading(true);
|
|
|
|
// Save user message to conversation
|
|
if (convId) {
|
|
await saveMessageToConversation(convId, userMessage);
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/api/chat', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
message,
|
|
documentId: selectedDocId,
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
// jsonSuccess wraps the answer in `data`.
|
|
const result = data.data ?? data;
|
|
const assistantMessage: ChatMessage = {
|
|
role: 'assistant',
|
|
content: result.response,
|
|
sources: result.sources,
|
|
};
|
|
setChatMessages((prev) => [...prev, assistantMessage]);
|
|
|
|
// Save assistant message to conversation
|
|
if (convId) {
|
|
await saveMessageToConversation(convId, assistantMessage);
|
|
setConversationRefreshTrigger((prev) => prev + 1);
|
|
}
|
|
} else {
|
|
setChatMessages((prev) => [
|
|
...prev,
|
|
{ role: 'assistant', content: `Error: ${data.error}` },
|
|
]);
|
|
}
|
|
} catch {
|
|
setChatMessages((prev) => [
|
|
...prev,
|
|
{ role: 'assistant', content: 'Failed to get response. Please try again.' },
|
|
]);
|
|
} finally {
|
|
setChatLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleSelectConversation = (id: string | null) => {
|
|
setActiveConversationId(id);
|
|
if (!id) {
|
|
setChatMessages([]);
|
|
}
|
|
};
|
|
|
|
const handleNewConversation = () => {
|
|
setActiveConversationId(null);
|
|
setChatMessages([]);
|
|
};
|
|
|
|
return {
|
|
chatMessages,
|
|
chatLoading,
|
|
activeConversationId,
|
|
conversationRefreshTrigger,
|
|
handleChat,
|
|
handleSelectConversation,
|
|
handleNewConversation,
|
|
};
|
|
};
|