Remove debugging logs from Hubert chat component and API endpoints
- Stripped console.log statements from client and server Hubert The Eunuch
This commit is contained in:
parent
9ef1027cc6
commit
95ff689ace
@ -38,24 +38,19 @@ export default function HubertChat() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initVisitor = async () => {
|
const initVisitor = async () => {
|
||||||
try {
|
try {
|
||||||
console.log('[Hubert] Starting initialization...');
|
|
||||||
setIsInitializing(true);
|
setIsInitializing(true);
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
|
|
||||||
console.log('[Hubert] Fetching /api/hubert/new-visitor...');
|
|
||||||
const response = await fetchWithTimeout('/api/hubert/new-visitor', {
|
const response = await fetchWithTimeout('/api/hubert/new-visitor', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
}, 8000); // 8 second timeout
|
}, 8000); // 8 second timeout
|
||||||
|
|
||||||
console.log('[Hubert] Response status:', response.status);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log('[Hubert] Initialization successful:', data);
|
|
||||||
|
|
||||||
setVisitorId(data.visitor_id);
|
setVisitorId(data.visitor_id);
|
||||||
setConversationId(data.conversation_id);
|
setConversationId(data.conversation_id);
|
||||||
@ -74,10 +69,8 @@ export default function HubertChat() {
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
errorMessage = '/// ERROR: TIMEOUT - API_UNRESPONSIVE';
|
errorMessage = '/// ERROR: TIMEOUT - API_UNRESPONSIVE';
|
||||||
console.error('[Hubert] Request timed out after 8 seconds');
|
|
||||||
} else if (error.message.includes('Failed to fetch')) {
|
} else if (error.message.includes('Failed to fetch')) {
|
||||||
errorMessage = '/// ERROR: NETWORK_FAILURE - CHECK_API_ROUTE';
|
errorMessage = '/// ERROR: NETWORK_FAILURE - CHECK_API_ROUTE';
|
||||||
console.error('[Hubert] Network error - API route may not exist');
|
|
||||||
} else {
|
} else {
|
||||||
errorMessage = `/// ERROR: ${error.message}`;
|
errorMessage = `/// ERROR: ${error.message}`;
|
||||||
}
|
}
|
||||||
@ -90,7 +83,6 @@ export default function HubertChat() {
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
}]);
|
}]);
|
||||||
} finally {
|
} finally {
|
||||||
console.log('[Hubert] Initialization complete, setting isInitializing to false');
|
|
||||||
setIsInitializing(false);
|
setIsInitializing(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -99,7 +91,6 @@ export default function HubertChat() {
|
|||||||
|
|
||||||
// Retry initialization
|
// Retry initialization
|
||||||
const retryInit = () => {
|
const retryInit = () => {
|
||||||
console.log('[Hubert] Retrying initialization...');
|
|
||||||
setIsInitializing(true);
|
setIsInitializing(true);
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
@ -107,24 +98,19 @@ export default function HubertChat() {
|
|||||||
// Re-trigger initialization
|
// Re-trigger initialization
|
||||||
const initVisitor = async () => {
|
const initVisitor = async () => {
|
||||||
try {
|
try {
|
||||||
console.log('[Hubert] Starting initialization...');
|
|
||||||
setIsInitializing(true);
|
setIsInitializing(true);
|
||||||
setInitError(null);
|
setInitError(null);
|
||||||
|
|
||||||
console.log('[Hubert] Fetching /api/hubert/new-visitor...');
|
|
||||||
const response = await fetchWithTimeout('/api/hubert/new-visitor', {
|
const response = await fetchWithTimeout('/api/hubert/new-visitor', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
}, 8000);
|
}, 8000);
|
||||||
|
|
||||||
console.log('[Hubert] Response status:', response.status);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log('[Hubert] Initialization successful:', data);
|
|
||||||
|
|
||||||
setVisitorId(data.visitor_id);
|
setVisitorId(data.visitor_id);
|
||||||
setConversationId(data.conversation_id);
|
setConversationId(data.conversation_id);
|
||||||
@ -142,10 +128,8 @@ export default function HubertChat() {
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
errorMessage = '/// ERROR: TIMEOUT - API_UNRESPONSIVE';
|
errorMessage = '/// ERROR: TIMEOUT - API_UNRESPONSIVE';
|
||||||
console.error('[Hubert] Request timed out after 8 seconds');
|
|
||||||
} else if (error.message.includes('Failed to fetch')) {
|
} else if (error.message.includes('Failed to fetch')) {
|
||||||
errorMessage = '/// ERROR: NETWORK_FAILURE - CHECK_API_ROUTE';
|
errorMessage = '/// ERROR: NETWORK_FAILURE - CHECK_API_ROUTE';
|
||||||
console.error('[Hubert] Network error - API route may not exist');
|
|
||||||
} else {
|
} else {
|
||||||
errorMessage = `/// ERROR: ${error.message}`;
|
errorMessage = `/// ERROR: ${error.message}`;
|
||||||
}
|
}
|
||||||
@ -158,7 +142,6 @@ export default function HubertChat() {
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
}]);
|
}]);
|
||||||
} finally {
|
} finally {
|
||||||
console.log('[Hubert] Initialization complete, setting isInitializing to false');
|
|
||||||
setIsInitializing(false);
|
setIsInitializing(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -176,19 +159,9 @@ export default function HubertChat() {
|
|||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
const sendMessage = async () => {
|
const sendMessage = async () => {
|
||||||
console.log('[Hubert] sendMessage called', { input, isTyping, visitorId, conversationId });
|
|
||||||
|
|
||||||
if (!input.trim() || isTyping || !visitorId || !conversationId) {
|
if (!input.trim() || isTyping || !visitorId || !conversationId) {
|
||||||
console.log('[Hubert] sendMessage blocked:', {
|
|
||||||
noInput: !input.trim(),
|
|
||||||
isTyping,
|
|
||||||
noVisitorId: !visitorId,
|
|
||||||
noConversationId: !conversationId
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[Hubert] Sending message:', input);
|
|
||||||
const userMessage: Message = {
|
const userMessage: Message = {
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: input,
|
content: input,
|
||||||
@ -200,7 +173,6 @@ export default function HubertChat() {
|
|||||||
setIsTyping(true);
|
setIsTyping(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('[Hubert] Fetching /api/hubert/chat...');
|
|
||||||
const response = await fetch('/api/hubert/chat', {
|
const response = await fetch('/api/hubert/chat', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
@ -214,14 +186,11 @@ export default function HubertChat() {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('[Hubert] Response status:', response.status);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log('[Hubert] Response data:', data);
|
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
throw new Error(data.error);
|
throw new Error(data.error);
|
||||||
@ -233,7 +202,6 @@ export default function HubertChat() {
|
|||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log('[Hubert] Adding assistant message:', assistantMessage.content);
|
|
||||||
setMessages(prev => [...prev, assistantMessage]);
|
setMessages(prev => [...prev, assistantMessage]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[Hubert] Chat error:', error);
|
console.error('[Hubert] Chat error:', error);
|
||||||
|
|||||||
@ -75,11 +75,6 @@ export const POST = async (context) => {
|
|||||||
// In Astro with Cloudflare adapter, env is at locals.runtime.env
|
// In Astro with Cloudflare adapter, env is at locals.runtime.env
|
||||||
const env = locals?.runtime?.env;
|
const env = locals?.runtime?.env;
|
||||||
|
|
||||||
console.log('[Hubert API] Chat endpoint called');
|
|
||||||
console.log('[Hubert API] env object:', env);
|
|
||||||
console.log('[Hubert API] env keys:', env ? Object.keys(env) : 'no env');
|
|
||||||
console.log('[Hubert API] OPENROUTER_API_KEY present:', !!(env?.OPENROUTER_API_KEY));
|
|
||||||
|
|
||||||
const { messages, conversation_id, visitor_id } = await request.json();
|
const { messages, conversation_id, visitor_id } = await request.json();
|
||||||
|
|
||||||
if (!messages || !conversation_id || !visitor_id) {
|
if (!messages || !conversation_id || !visitor_id) {
|
||||||
|
|||||||
@ -9,41 +9,32 @@ export const prerender = false;
|
|||||||
* Used when Hubert interface first loads
|
* Used when Hubert interface first loads
|
||||||
*/
|
*/
|
||||||
export const POST = async (context) => {
|
export const POST = async (context) => {
|
||||||
console.log('[Hubert API] /api/hubert/new-visitor endpoint called');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { request, locals } = context;
|
const { request, locals } = context;
|
||||||
// In Astro with Cloudflare adapter, env is at locals.runtime.env
|
// In Astro with Cloudflare adapter, env is at locals.runtime.env
|
||||||
const env = locals?.runtime?.env;
|
const env = locals?.runtime?.env;
|
||||||
|
|
||||||
console.log('[Hubert API] Request received, env binding available:', !!env);
|
|
||||||
console.log('[Hubert API] HUBERT_DB binding available:', !!(env && env.HUBERT_DB));
|
|
||||||
|
|
||||||
const userAgent = request.headers.get('user-agent') || 'unknown';
|
const userAgent = request.headers.get('user-agent') || 'unknown';
|
||||||
const ip = request.headers.get('cf-connecting-ip') || 'unknown';
|
const ip = request.headers.get('cf-connecting-ip') || 'unknown';
|
||||||
|
|
||||||
const visitorId = randomUUID();
|
const visitorId = randomUUID();
|
||||||
console.log('[Hubert API] Generated visitor ID:', visitorId);
|
|
||||||
|
|
||||||
// Only insert into database if HUBERT_DB binding exists (production)
|
// Only insert into database if HUBERT_DB binding exists (production)
|
||||||
// In dev mode, this allows the chatbot to work without Cloudflare bindings
|
// In dev mode, this allows the chatbot to work without Cloudflare bindings
|
||||||
if (env && env.HUBERT_DB) {
|
if (env && env.HUBERT_DB) {
|
||||||
console.log('[Hubert API] Attempting database insert...');
|
|
||||||
try {
|
try {
|
||||||
await env.HUBERT_DB.prepare(`
|
await env.HUBERT_DB.prepare(`
|
||||||
INSERT INTO visitors (visitor_id, first_seen_at, last_seen_at, ip_address, user_agent)
|
INSERT INTO visitors (visitor_id, first_seen_at, last_seen_at, ip_address, user_agent)
|
||||||
VALUES (?, datetime('now'), datetime('now'), ?, ?)
|
VALUES (?, datetime('now'), datetime('now'), ?, ?)
|
||||||
`).bind(visitorId, ip, userAgent).run();
|
`).bind(visitorId, ip, userAgent).run();
|
||||||
console.log(`[Hubert API] Database insert successful for visitor: ${visitorId}`);
|
console.log(`[Hubert] New visitor initialized: ${visitorId}`);
|
||||||
} catch (dbError) {
|
} catch (dbError) {
|
||||||
console.error('[Hubert API] Database insert failed (continuing anyway):', dbError);
|
console.error('[Hubert] Database insert failed (continuing anyway):', dbError);
|
||||||
// Continue anyway - don't fail initialization if DB is misconfigured
|
// Continue anyway - don't fail initialization if DB is misconfigured
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`[Hubert API] Dev mode: Skipping database insert for visitor: ${visitorId}`);
|
console.log(`[Hubert] Dev mode: Skipping database insert for visitor: ${visitorId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[Hubert API] Returning success response');
|
|
||||||
return Response.json({
|
return Response.json({
|
||||||
visitor_id: visitorId,
|
visitor_id: visitorId,
|
||||||
conversation_id: visitorId, // Use visitor_id as initial conversation_id
|
conversation_id: visitorId, // Use visitor_id as initial conversation_id
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user