import { Router } from "express"; import { getSession, addMessage, getConversationForAPI } from "../services/sessionStore.js"; import { trackUsage, getUsageSummary, formatCost, getSessionUsage } from "../services/usageTracker.js"; const router = Router(); const ANTHROPIC_API_URL = "https://api.anthropic.com"; /** * Build entity context string for the system prompt */ function buildEntityContext(entities) { if (!entities || entities.length === 0) { return "\n\nThe diagram is currently empty."; } const entityList = entities.map(e => { const shape = e.template?.replace('#', '').replace('-template', '') || 'unknown'; const pos = e.position || { x: 0, y: 0, z: 0 }; return `- ${e.text || '(no label)'} (${shape}, ${e.color || 'unknown'}) at (${pos.x?.toFixed(1)}, ${pos.y?.toFixed(1)}, ${pos.z?.toFixed(1)})`; }).join('\n'); return `\n\n## Current Diagram State\nThe diagram currently contains ${entities.length} entities:\n${entityList}`; } // Express 5 uses named parameters for wildcards router.post("/*path", async (req, res) => { const requestStart = Date.now(); console.log(`[Claude API] ========== REQUEST START ==========`); const apiKey = process.env.ANTHROPIC_API_KEY; if (!apiKey) { console.error(`[Claude API] ERROR: API key not configured`); return res.status(500).json({ error: "API key not configured" }); } // Get the path after /api/claude (e.g., /v1/messages) // Express 5 returns path segments as an array const pathParam = req.params.path; const path = "/" + (Array.isArray(pathParam) ? pathParam.join("/") : pathParam || ""); console.log(`[Claude API] Path: ${path}`); // Check for session-based request const { sessionId, ...requestBody } = req.body; let modifiedBody = requestBody; console.log(`[Claude API] Session ID: ${sessionId || 'none'}`); console.log(`[Claude API] Model: ${requestBody.model}`); console.log(`[Claude API] Messages count: ${requestBody.messages?.length || 0}`); if (sessionId) { const session = getSession(sessionId); if (session) { console.log(`[Claude API] Session found: ${session.entities.length} entities, ${session.conversationHistory.length} messages in history`); // Inject entity context into system prompt if (modifiedBody.system) { const entityContext = buildEntityContext(session.entities); console.log(`[Claude API] Entity context added (${entityContext.length} chars)`); modifiedBody.system += entityContext; } // Get conversation history and merge with current messages const historyMessages = getConversationForAPI(sessionId); if (historyMessages.length > 0 && modifiedBody.messages) { // Filter out any duplicate messages (in case client sent history too) const currentContent = modifiedBody.messages[modifiedBody.messages.length - 1]?.content; const filteredHistory = historyMessages.filter(msg => msg.content !== currentContent); modifiedBody.messages = [...filteredHistory, ...modifiedBody.messages]; console.log(`[Claude API] Merged ${filteredHistory.length} history + ${modifiedBody.messages.length - filteredHistory.length} new = ${modifiedBody.messages.length} total messages`); } } else { console.log(`[Claude API] WARNING: Session ${sessionId} not found`); } } try { console.log(`[Claude API] Sending request to Anthropic API...`); const fetchStart = Date.now(); const response = await fetch(`${ANTHROPIC_API_URL}${path}`, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey, "anthropic-version": "2023-06-01", }, body: JSON.stringify(modifiedBody), }); const fetchDuration = Date.now() - fetchStart; console.log(`[Claude API] Response received in ${fetchDuration}ms, status: ${response.status}`); console.log(`[Claude API] Parsing response JSON...`); const data = await response.json(); console.log(`[Claude API] Response parsed. Stop reason: ${data.stop_reason}, content blocks: ${data.content?.length || 0}`); // Track and log token usage if (data.usage) { // Extract content for detailed tracking const userMessage = requestBody.messages?.[requestBody.messages.length - 1]; const inputText = typeof userMessage?.content === 'string' ? userMessage.content : null; const outputText = data.content ?.filter(c => c.type === 'text') .map(c => c.text) .join('\n') || null; const toolCalls = data.content ?.filter(c => c.type === 'tool_use') .map(c => ({ name: c.name, input: c.input })) || []; const usageRecord = trackUsage(sessionId, modifiedBody.model, data.usage, { inputText, outputText, toolCalls }); console.log(`[Claude API] REQUEST USAGE: ${getUsageSummary(usageRecord)}`); // Log cumulative session usage if session exists if (sessionId) { const sessionStats = getSessionUsage(sessionId); if (sessionStats) { console.log(`[Claude API] SESSION TOTALS (${sessionStats.requestCount} requests):`); console.log(`[Claude API] Total input: ${sessionStats.totalInputTokens} tokens`); console.log(`[Claude API] Total output: ${sessionStats.totalOutputTokens} tokens`); console.log(`[Claude API] Total cost: ${formatCost(sessionStats.totalCost)}`); } } } if (data.error) { console.error(`[Claude API] API returned error:`, data.error); } // If session exists and response is successful, store messages if (sessionId && response.ok && data.content) { const session = getSession(sessionId); if (session) { // Store the user message if it was new (only if it's a string, not tool results) const userMessage = requestBody.messages?.[requestBody.messages.length - 1]; if (userMessage && userMessage.role === 'user' && typeof userMessage.content === 'string') { addMessage(sessionId, { role: 'user', content: userMessage.content }); console.log(`[Claude API] Stored user message to session`); } // Store the assistant response (text only, not tool use blocks) const assistantContent = data.content .filter(c => c.type === 'text') .map(c => c.text) .join('\n'); if (assistantContent) { addMessage(sessionId, { role: 'assistant', content: assistantContent }); console.log(`[Claude API] Stored assistant response to session (${assistantContent.length} chars)`); } } } const totalDuration = Date.now() - requestStart; console.log(`[Claude API] ========== REQUEST COMPLETE (${totalDuration}ms) ==========`); res.status(response.status).json(data); } catch (error) { const totalDuration = Date.now() - requestStart; console.error(`[Claude API] ========== REQUEST FAILED (${totalDuration}ms) ==========`); console.error(`[Claude API] Error:`, error); console.error(`[Claude API] Error message:`, error.message); console.error(`[Claude API] Error stack:`, error.stack); res.status(500).json({ error: "Failed to proxy request to Claude API", details: error.message }); } }); export default router;