import { useState, useCallback, useRef, useEffect } from 'react';
export interface Message {
id: string;
type: 'user' | 'arthur';
text: string;
timestamp: Date;
}
interface ServerStatus {
connected: boolean;
ragActive: boolean;
docCount: number;
}
interface UseArthurReturn {
messages: Message[];
status: ServerStatus;
isThinking: boolean;
sendMessage: (text: string) => Promise<string | undefined>;
clearMessages: () => void;
}
const ARTHUR_API = 'http://localhost:3456';
// Demo responses for when server is offline
const demoResponses: Record<string, string> = {
'hello': "Hello! It's good to hear from you. How can I assist you today?",
'hi': "Hi there! Ready to help with whatever you need.",
'how are you': "I'm functioning optimally, thank you. How are you feeling today?",
'what time': `It's currently ${new Date().toLocaleTimeString()}. You're in your peak productivity window.`,
'what can you do': "I can help you with tasks, answer questions, and provide suggestions based on Oracle knowledge. Think of me as your personal AI assistant.",
'status': "All systems operational. Oracle knowledge base connected with 4600+ documents.",
'thank': "You're welcome. I'm always here when you need me.",
'bye': "Goodbye! Remember to take breaks. I'll be here when you return.",
'arthur': "Yes, I'm Arthur — your personal AI assistant. I observe, I remember, I assist.",
'pattern': "Based on recent activity: protect your afternoon focus time for deep work.",
'help': "I can help with: checking patterns, managing tasks, answering questions, or just having a conversation.",
'default': "I understand. Let me think about how best to help you with that."
};
export function useArthur(): UseArthurReturn {
const [messages, setMessages] = useState<Message[]>([
{
id: 'greeting',
type: 'arthur',
text: "Good morning. I'm Arthur, your AI assistant. How may I help you today?",
timestamp: new Date(),
},
]);
const [status, setStatus] = useState<ServerStatus>({
connected: false,
ragActive: false,
docCount: 0,
});
const [isThinking, setIsThinking] = useState(false);
const checkIntervalRef = useRef<NodeJS.Timeout | null>(null);
// Check server health
const checkServer = useCallback(async () => {
try {
const res = await fetch(`${ARTHUR_API}/health`);
if (res.ok) {
const data = await res.json();
setStatus({
connected: true,
ragActive: data.oracleV2 === 'connected',
docCount: data.oracleV2 === 'connected' ? 4640 : 0,
});
}
} catch {
setStatus({ connected: false, ragActive: false, docCount: 0 });
}
}, []);
// Start health check interval
useEffect(() => {
checkServer();
checkIntervalRef.current = setInterval(checkServer, 10000);
return () => {
if (checkIntervalRef.current) clearInterval(checkIntervalRef.current);
};
}, [checkServer]);
// Get demo response
const getDemoResponse = (text: string): string => {
const lower = text.toLowerCase();
for (const [key, value] of Object.entries(demoResponses)) {
if (lower.includes(key)) return value;
}
return demoResponses.default;
};
// Send message
const sendMessage = useCallback(async (text: string) => {
if (!text.trim()) return;
// Add user message
const userMsg: Message = {
id: Date.now().toString(),
type: 'user',
text,
timestamp: new Date(),
};
setMessages((prev) => [...prev, userMsg]);
setIsThinking(true);
let response: string;
if (status.connected) {
// Real AI via server
try {
const res = await fetch(`${ARTHUR_API}/ask`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question: text }),
});
const data = await res.json();
response = data.response || data.error || 'No response received.';
} catch {
response = 'I apologize, but I am having trouble connecting. Please try again.';
}
} else {
// Demo mode
await new Promise((r) => setTimeout(r, 500)); // Simulate thinking
response = getDemoResponse(text);
}
// Add Arthur response
const arthurMsg: Message = {
id: (Date.now() + 1).toString(),
type: 'arthur',
text: response,
timestamp: new Date(),
};
setMessages((prev) => [...prev, arthurMsg]);
setIsThinking(false);
return response;
}, [status.connected]);
const clearMessages = useCallback(() => {
setMessages([{
id: 'greeting',
type: 'arthur',
text: "Good morning. I'm Arthur, your AI assistant. How may I help you today?",
timestamp: new Date(),
}]);
}, []);
return {
messages,
status,
isThinking,
sendMessage,
clearMessages,
};
}