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

149 lines
4.9 KiB
TypeScript

'use client';
import { type FC, useCallback } from 'react';
import { EmbeddedChat } from '@/components/chat/EmbeddedChat';
import { useChatTurns, type AskFn } from '@/hooks/useChatTurns';
import { type BotTemplate } from '@/lib/bot-templates';
import { getAccentColorBgTintClass } from '@/lib/config/colors';
interface CreatedBot {
template: BotTemplate;
name: string;
systemPrompt: string;
}
interface QuickChatProps {
bot: CreatedBot;
onReset: () => void;
}
export const QuickChat: FC<QuickChatProps> = ({ bot, onReset }) => {
const getWelcomeMessage = useCallback(() => {
const { template, name } = bot;
if (template.category === 'memorial') {
if (template.id.includes('dog')) {
return `*tail wagging excitedly* Hi! I'm ${name}! I'm so happy to see you! 🐕`;
}
if (template.id.includes('cat')) {
return `*looks up with knowing eyes* ...Hello. It's ${name}. I suppose I'm glad you're here. *slow blink*`;
}
return `Hello, my dear. It's ${name}. I'm here with you now. What would you like to talk about?`;
}
if (template.category === 'legends') {
return `Greetings. I am ${name}. What wisdom do you seek?`;
}
if (template.category === 'gods') {
return `I am ${name}. Speak, mortal, and I shall listen.`;
}
if (template.category === 'magical') {
if (template.id.includes('wizard')) {
return `*adjusts pointed hat* Ah, a seeker of knowledge. I am ${name}. What mysteries draw you here?`;
}
if (template.id.includes('fairy')) {
return `*sparkles appear* Hello hello! ✨ I'm ${name}! What wish can I help with today?`;
}
return `Greetings, traveler. I am ${name}. What brings you to me?`;
}
if (template.category === 'helpers') {
if (template.id.includes('butler')) {
return `Good day. I am ${name}, at your service. How may I assist you?`;
}
if (template.id.includes('chef')) {
return `Buongiorno! I am ${name}. Are you hungry? Let's talk about food!`;
}
return `Hello! I'm ${name}. I'm here to help. What can I do for you?`;
}
return `Hello! I'm ${name}. How can I help you today?`;
}, [bot]);
const ask = useCallback<AskFn>(
async (message, _history, signal) => {
const response = await fetch('/api/quick-chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message,
systemPrompt: bot.systemPrompt,
additionalContext: `Bot name: ${bot.name}. Template: ${bot.template.name}.`,
}),
signal,
});
if (!response.ok) throw new Error('Failed to get response');
const data = await response.json();
// API returns { success: true, data: { response: "..." } }
return {
content:
data.data?.response ||
data.response ||
data.message ||
"I'm sorry, I couldn't generate a response.",
};
},
[bot],
);
const chat = useChatTurns(ask, {
initial: [{ id: 'welcome', role: 'assistant', content: getWelcomeMessage() }],
failedText: "I'm sorry, I had trouble responding. Please try again.",
});
const accentTint = getAccentColorBgTintClass(bot.template.accentColor);
return (
<div className="max-w-3xl mx-auto">
{/* Header */}
<div className="flex items-center justify-between mb-6">
<div className="flex items-center gap-4">
<div
className={`w-14 h-14 rounded-2xl flex items-center justify-center text-3xl ${accentTint}`}
>
{bot.template.emoji}
</div>
<div>
<h1 className="text-2xl font-bold text-gray-900">{bot.name}</h1>
<p className="text-sm text-gray-500">{bot.template.name}</p>
</div>
</div>
<button
onClick={onReset}
className="text-gray-500 hover:text-gray-700 font-medium text-sm flex items-center gap-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
</svg>
Create Another
</button>
</div>
{/* Chat Container */}
<div className="bg-white rounded-2xl shadow-xl border border-gray-100 overflow-hidden">
<EmbeddedChat
className="h-[520px]"
messages={chat.messages}
onSend={chat.send}
onRetry={chat.retry}
onStop={chat.stop}
sending={chat.sending}
placeholder={`Message ${bot.name}...`}
starters={bot.template.starterQuestions}
/>
</div>
{/* Info Footer */}
<div className="text-center mt-6 text-sm text-gray-500">
<p>
This is a preview. In the full version, {bot.name} will remember your conversations and
learn more about you over time.
</p>
</div>
</div>
);
};