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

130 lines
5.0 KiB
TypeScript

'use client';
import { useCallback } from 'react';
import { EmbeddedChat } from '@/components/chat/EmbeddedChat';
import { useChatTurns, type AskFn } from '@/hooks/useChatTurns';
import type { TryDocument } from './TryDocumentsPanel';
const STARTERS = [
'What is this document about?',
'Summarize the key points',
'What are the main conclusions?',
'List any action items or recommendations',
];
interface TryChatPanelProps {
documents: TryDocument[];
onError: (msg: string) => void;
}
export const TryChatPanel = ({ documents, onError }: TryChatPanelProps) => {
const ask = useCallback<AskFn>(
async (message, _history, signal) => {
onError('');
const response = await fetch('/api/demo/document-chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message,
documents: documents.map((doc) => ({ name: doc.name, content: doc.content })),
}),
signal,
});
const data = await response.json();
if (!data.success) throw new Error(data.error || 'Something went wrong');
return {
content: data.data.response,
sources: (data.data.sources as Array<{ document_name: string }> | undefined)?.map(
(source) => ({ label: source.document_name }),
),
};
},
[documents, onError],
);
const chat = useChatTurns(ask, { failedText: 'Failed to get response. Please try again.' });
return (
<div className="bg-white rounded-2xl shadow-lg border border-gray-100 overflow-hidden flex flex-col h-[calc(100vh-16rem)] min-h-[400px] max-h-[600px] md:max-h-none md:h-[600px]">
{/* Chat Header */}
<div className="px-6 py-4 border-b border-gray-100 bg-action-tint">
<h2 className="text-lg font-bold text-gray-900">Chat</h2>
<p className="text-sm text-gray-600">Ask questions about your documents</p>
</div>
<EmbeddedChat
className="flex-1"
messages={chat.messages}
onSend={chat.send}
onRetry={chat.retry}
onStop={chat.stop}
sending={chat.sending}
placeholder={
documents.length === 0
? 'Upload a document first...'
: 'Ask a question about your documents...'
}
sendBlockedReason={documents.length === 0 ? 'Upload a document first' : null}
starters={STARTERS}
empty={
documents.length === 0 ? (
<div className="flex items-center justify-center h-full text-gray-500 text-center">
<div className="max-w-sm">
<div className="w-20 h-20 mx-auto mb-4 bg-action-tint rounded-2xl flex items-center justify-center">
<svg
className="w-10 h-10 text-action"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={1.5}
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
</div>
<p className="text-lg font-semibold text-gray-900 mb-2">Drop your document here</p>
<p className="text-sm text-gray-500 mb-4">
Upload a PDF, text file, or markdown document to start chatting with its content.
</p>
<div className="flex flex-wrap justify-center gap-2 text-xs text-gray-400">
<span className="px-2 py-1 bg-gray-100 rounded">Contracts</span>
<span className="px-2 py-1 bg-gray-100 rounded">Research papers</span>
<span className="px-2 py-1 bg-gray-100 rounded">Reports</span>
<span className="px-2 py-1 bg-gray-100 rounded">Legal docs</span>
</div>
</div>
</div>
) : (
<div className="flex items-center justify-center h-full text-gray-500 text-center">
<div className="max-w-md">
<div className="w-16 h-16 mx-auto mb-4 bg-gradient-to-br from-green-100 to-action-tint rounded-full flex items-center justify-center">
<svg
className="w-8 h-8 text-green-500"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
</div>
<p className="text-lg font-semibold text-gray-900 mb-2">
Your documents are ready!
</p>
<p className="text-sm text-gray-500 mb-4">Ask any question about your documents.</p>
</div>
</div>
)
}
/>
</div>
);
};