diff --git a/frontend/src/app/chat/page.tsx b/frontend/src/app/chat/page.tsx index 06e5404b..b2b5f52b 100644 --- a/frontend/src/app/chat/page.tsx +++ b/frontend/src/app/chat/page.tsx @@ -411,14 +411,17 @@ function ChatPage() { // Handle Realtime API format (this is what you're actually getting!) else if (chunk.type === "response.output_item.added" && chunk.item?.type === "function_call") { - console.log("Function call started (Realtime API):", chunk.item.name) + console.log("🟢 CREATING function call (added):", chunk.item.id, chunk.item.tool_name || chunk.item.name) const functionCall: FunctionCall = { - name: chunk.item.name || "unknown", - arguments: undefined, + name: chunk.item.tool_name || chunk.item.name || "unknown", + arguments: chunk.item.inputs || undefined, status: "pending", - argumentsString: "" + argumentsString: "", + id: chunk.item.id, + type: chunk.item.type } currentFunctionCalls.push(functionCall) + console.log("🟢 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name }))) } // Handle function call arguments streaming (Realtime API) @@ -453,20 +456,43 @@ function ChatPage() { // Handle function call completion (Realtime API) else if (chunk.type === "response.output_item.done" && chunk.item?.type === "function_call") { - console.log("Function call done (Realtime API):", chunk.item.status) - const lastFunctionCall = currentFunctionCalls[currentFunctionCalls.length - 1] - if (lastFunctionCall) { - lastFunctionCall.status = chunk.item.status === "completed" ? "completed" : "error" + console.log("🔵 UPDATING function call (done):", chunk.item.id, chunk.item.tool_name || chunk.item.name) + console.log("🔵 Looking for existing function calls:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name }))) + + // Find existing function call by ID or name + let functionCall = currentFunctionCalls.find(fc => + fc.id === chunk.item.id || + fc.name === chunk.item.tool_name || + fc.name === chunk.item.name + ) + + if (functionCall) { + console.log("🔵 FOUND existing function call, updating:", functionCall.id, functionCall.name) + // Update existing function call with completion data + functionCall.status = chunk.item.status === "completed" ? "completed" : "error" + functionCall.id = chunk.item.id + functionCall.type = chunk.item.type + functionCall.name = chunk.item.tool_name || chunk.item.name || functionCall.name + functionCall.arguments = chunk.item.inputs || functionCall.arguments + + // Set results if present + if (chunk.item.results) { + functionCall.result = chunk.item.results + } + } else { + console.log("🔴 WARNING: Could not find existing function call to update:", chunk.item.id, chunk.item.tool_name, chunk.item.name) } } - // Handle tool call completion with results (new format) - else if (chunk.type === "response.output_item.done" && chunk.item?.type?.includes("_call")) { - console.log("Tool call done with results (new format):", chunk.item) + // Handle tool call completion with results + else if (chunk.type === "response.output_item.done" && chunk.item?.type?.includes("_call") && chunk.item?.type !== "function_call") { + console.log("Tool call done with results:", chunk.item) // Find existing function call by ID, or by name/type if ID not available let functionCall = currentFunctionCalls.find(fc => fc.id === chunk.item.id || + (fc.name === chunk.item.tool_name) || + (fc.name === chunk.item.name) || (fc.name === chunk.item.type) || (fc.name.includes(chunk.item.type.replace('_call', '')) || chunk.item.type.includes(fc.name)) ) @@ -483,10 +509,9 @@ function ChatPage() { functionCall.result = chunk.item.results } } else { - // Only create new if we really can't find an existing one - console.log("Creating new function call for:", chunk.item.type) + // Create new function call if not found functionCall = { - name: chunk.item.type || "unknown", + name: chunk.item.tool_name || chunk.item.name || chunk.item.type || "unknown", arguments: chunk.item.inputs || {}, status: "completed" as const, id: chunk.item.id, @@ -498,16 +523,17 @@ function ChatPage() { } // Handle function call output item added (new format) - else if (chunk.type === "response.output_item.added" && chunk.item?.type?.includes("_call")) { - console.log("Tool call started (new format):", chunk.item) + else if (chunk.type === "response.output_item.added" && chunk.item?.type?.includes("_call") && chunk.item?.type !== "function_call") { + console.log("🟡 CREATING tool call (added):", chunk.item.id, chunk.item.tool_name || chunk.item.name, chunk.item.type) const functionCall = { - name: chunk.item.type || "unknown", + name: chunk.item.tool_name || chunk.item.name || chunk.item.type || "unknown", arguments: chunk.item.inputs || {}, status: "pending" as const, id: chunk.item.id, type: chunk.item.type } currentFunctionCalls.push(functionCall) + console.log("🟡 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name, type: fc.type }))) } // Handle function call results @@ -698,6 +724,11 @@ function ChatPage() { const functionCallId = `${messageIndex || 'streaming'}-${index}` const isExpanded = expandedFunctionCalls.has(functionCallId) + // Determine display name - show both name and type if available + const displayName = fc.type && fc.type !== fc.name + ? `${fc.name} (${fc.type})` + : fc.name + return (