mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 05:48:27 +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>
135 lines
5.0 KiB
TypeScript
135 lines
5.0 KiB
TypeScript
'use client';
|
|
|
|
import { type FC, useState, useCallback } from 'react';
|
|
import { type Professional, getAccentColorClasses } from '@/data/professionals';
|
|
import { useAuth } from '@/lib/auth';
|
|
import { ModelIndicator } from '@/components/chat/ModelIndicator';
|
|
import { ModelSelector } from '@/components/chat/ModelSelector';
|
|
import { ChatAuthCTA } from '@/components/chat/ChatAuthCTA';
|
|
import { PrivacyTierInfo } from '@/components/chat/PrivacyTierInfo';
|
|
import { EmbeddedChat } from '@/components/chat/EmbeddedChat';
|
|
import { useChatTurns, type AskFn } from '@/hooks/useChatTurns';
|
|
import type { ModelProvider } from '@/lib/llm-client';
|
|
|
|
interface ProfessionalDemoProps {
|
|
professional: Professional;
|
|
}
|
|
|
|
/**
|
|
* Interactive chat demo for AI Professionals
|
|
* Allows users to try chatting with the professional before signing up
|
|
*/
|
|
export const ProfessionalDemo: FC<ProfessionalDemoProps> = ({ professional }) => {
|
|
const { user, loading: authLoading } = useAuth();
|
|
const [currentProvider, setCurrentProvider] = useState<ModelProvider>('groq');
|
|
const [showPrivacyInfo, setShowPrivacyInfo] = useState(false);
|
|
|
|
const colors = getAccentColorClasses(professional.accentColor);
|
|
const isAuthenticated = !authLoading && !!user;
|
|
|
|
const ask = useCallback<AskFn>(
|
|
async (message, conversationHistory, signal) => {
|
|
const response = await fetch('/api/quick-chat', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
message,
|
|
systemPrompt: professional.systemPromptBase,
|
|
additionalContext: `Professional: ${professional.name} (${professional.title}). Role: ${professional.role}.`,
|
|
conversationHistory,
|
|
}),
|
|
signal,
|
|
});
|
|
if (!response.ok) throw new Error('Failed to get response');
|
|
const data = await response.json();
|
|
const responseData = data.data || data;
|
|
if (responseData.provider && responseData.provider !== 'fallback') {
|
|
setCurrentProvider(responseData.provider as ModelProvider);
|
|
}
|
|
return {
|
|
content:
|
|
responseData.response ||
|
|
responseData.message ||
|
|
"I'm sorry, I couldn't generate a response.",
|
|
};
|
|
},
|
|
[professional],
|
|
);
|
|
|
|
const chat = useChatTurns(ask, {
|
|
initial: [{ id: 'welcome', role: 'assistant', content: professional.welcomeMessage }],
|
|
failedText: "I'm sorry, I had trouble responding. Please try again.",
|
|
});
|
|
|
|
const handleProviderChange = (provider: ModelProvider) => {
|
|
setCurrentProvider(provider);
|
|
// TODO: Persist to user settings via API when implementing model switching
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="bg-white rounded-2xl shadow-xl border border-gray-100 overflow-hidden">
|
|
{/* Chat Header */}
|
|
<div className={`px-6 py-4 bg-gradient-to-r ${colors.bgGradient} text-white`}>
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-3xl">{professional.emoji}</span>
|
|
<div>
|
|
<h3 className="font-bold">{professional.name}</h3>
|
|
<p className="text-sm opacity-90">{professional.title}</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<ModelIndicator provider={currentProvider} compact showTier={false} />
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowPrivacyInfo(!showPrivacyInfo)}
|
|
className="p-1.5 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
|
aria-label="Privacy info"
|
|
>
|
|
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Model Selector (for authenticated users) */}
|
|
{!authLoading && (
|
|
<div className="px-4 py-2 bg-gray-50 border-b border-gray-100 flex items-center justify-between">
|
|
<ModelSelector
|
|
currentProvider={currentProvider}
|
|
onProviderChange={handleProviderChange}
|
|
isAuthenticated={isAuthenticated}
|
|
disabled={chat.sending}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
<EmbeddedChat
|
|
className="h-[420px]"
|
|
messages={chat.messages}
|
|
onSend={chat.send}
|
|
onRetry={chat.retry}
|
|
onStop={chat.stop}
|
|
sending={chat.sending}
|
|
placeholder={`Message ${professional.name}...`}
|
|
starters={professional.exampleQuestions.slice(0, 3)}
|
|
/>
|
|
</div>
|
|
|
|
{/* Auth CTA for guests */}
|
|
{!authLoading && !isAuthenticated && <ChatAuthCTA />}
|
|
|
|
{/* Privacy Tier Info (expandable) */}
|
|
{showPrivacyInfo && <PrivacyTierInfo defaultExpanded />}
|
|
</div>
|
|
);
|
|
};
|