Files
CatoandClaude 9d5c25a26f feat(chat): run every in-page chat on chatkit's thread and composer (#328)
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>
2026-11-15 18:48:44 +01:00

104 lines
3.2 KiB
TypeScript

'use client';
import { ConversationList } from '@/components/conversations';
import { EmbeddedChat, type EmbeddedChatMessage } from '@/components/chat/EmbeddedChat';
import { useDocumentChat } from '@/hooks/useDocumentChat';
import type { Document } from '@/types/document';
interface DocumentChatPanelProps {
documents: Document[];
selectedDocId: string | null;
hasReadyDocuments: boolean;
}
export const DocumentChatPanel = ({
documents,
selectedDocId,
hasReadyDocuments,
}: DocumentChatPanelProps) => {
const {
chatMessages,
chatLoading,
activeConversationId,
conversationRefreshTrigger,
handleChat,
handleSelectConversation,
handleNewConversation,
} = useDocumentChat({ selectedDocId });
const turns: EmbeddedChatMessage[] = chatMessages.map((msg, i) => ({
id: msg.id ?? `turn-${i}`,
role: msg.role,
content: msg.content,
sources: msg.sources?.map((source) => ({
label: source.document_name,
detail: source.preview || undefined,
})),
}));
return (
<>
{/* Conversation History */}
<ConversationList
botType="documents"
documentId={selectedDocId ?? undefined}
activeConversationId={activeConversationId}
onSelectConversation={handleSelectConversation}
onNewConversation={handleNewConversation}
refreshTrigger={conversationRefreshTrigger}
/>
{/* Chat Interface */}
<div className="bg-white rounded-xl shadow-lg border border-gray-200 p-6 flex flex-col">
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold text-gray-900">Chat with Documents</h2>
{selectedDocId && (
<span className="text-sm text-action">
Filtering: {documents.find((d) => d.id === selectedDocId)?.name}
</span>
)}
</div>
{!hasReadyDocuments ? (
<div className="flex-1 flex items-center justify-center text-gray-500 text-center">
<div>
<svg
className="w-12 h-12 mx-auto mb-4 text-gray-300"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"
/>
</svg>
<p>Upload and process documents to start chatting</p>
</div>
</div>
) : (
<EmbeddedChat
className="h-[480px]"
messages={turns}
onSend={(text) => void handleChat(text)}
sending={chatLoading}
placeholder="Ask a question..."
empty={
<div className="text-center text-gray-500 py-8">
<p>Ask a question about your documents</p>
<p className="text-sm mt-1">
{selectedDocId
? 'Searching in selected document only'
: 'Searching across all your documents'}
</p>
</div>
}
/>
)}
</div>
</>
);
};