dedupe fix

This commit is contained in:
phact 2025-08-11 21:54:58 -04:00
parent 093acd4062
commit b0725ed597

View file

@ -412,6 +412,24 @@ function ChatPage() {
// Handle Realtime API format (this is what you're actually getting!) // Handle Realtime API format (this is what you're actually getting!)
else if (chunk.type === "response.output_item.added" && chunk.item?.type === "function_call") { else if (chunk.type === "response.output_item.added" && chunk.item?.type === "function_call") {
console.log("🟢 CREATING function call (added):", chunk.item.id, chunk.item.tool_name || chunk.item.name) console.log("🟢 CREATING function call (added):", chunk.item.id, chunk.item.tool_name || chunk.item.name)
// Try to find an existing pending call to update (created by earlier deltas)
let existing = currentFunctionCalls.find(fc => fc.id === chunk.item.id)
if (!existing) {
existing = [...currentFunctionCalls].reverse().find(fc =>
fc.status === "pending" &&
!fc.id &&
(fc.name === (chunk.item.tool_name || chunk.item.name))
)
}
if (existing) {
existing.id = chunk.item.id
existing.type = chunk.item.type
existing.name = chunk.item.tool_name || chunk.item.name || existing.name
existing.arguments = chunk.item.inputs || existing.arguments
console.log("🟢 UPDATED existing pending function call with id:", existing.id)
} else {
const functionCall: FunctionCall = { const functionCall: FunctionCall = {
name: chunk.item.tool_name || chunk.item.name || "unknown", name: chunk.item.tool_name || chunk.item.name || "unknown",
arguments: chunk.item.inputs || undefined, arguments: chunk.item.inputs || undefined,
@ -423,6 +441,7 @@ function ChatPage() {
currentFunctionCalls.push(functionCall) currentFunctionCalls.push(functionCall)
console.log("🟢 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name }))) console.log("🟢 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name })))
} }
}
// Handle function call arguments streaming (Realtime API) // Handle function call arguments streaming (Realtime API)
else if (chunk.type === "response.function_call_arguments.delta") { else if (chunk.type === "response.function_call_arguments.delta") {
@ -525,6 +544,24 @@ function ChatPage() {
// Handle function call output item added (new format) // Handle function call output item added (new format)
else if (chunk.type === "response.output_item.added" && chunk.item?.type?.includes("_call") && chunk.item?.type !== "function_call") { 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) console.log("🟡 CREATING tool call (added):", chunk.item.id, chunk.item.tool_name || chunk.item.name, chunk.item.type)
// Dedupe by id or pending with same name
let existing = currentFunctionCalls.find(fc => fc.id === chunk.item.id)
if (!existing) {
existing = [...currentFunctionCalls].reverse().find(fc =>
fc.status === "pending" &&
!fc.id &&
(fc.name === (chunk.item.tool_name || chunk.item.name || chunk.item.type))
)
}
if (existing) {
existing.id = chunk.item.id
existing.type = chunk.item.type
existing.name = chunk.item.tool_name || chunk.item.name || chunk.item.type || existing.name
existing.arguments = chunk.item.inputs || existing.arguments
console.log("🟡 UPDATED existing pending tool call with id:", existing.id)
} else {
const functionCall = { const functionCall = {
name: chunk.item.tool_name || chunk.item.name || chunk.item.type || "unknown", name: chunk.item.tool_name || chunk.item.name || chunk.item.type || "unknown",
arguments: chunk.item.inputs || {}, arguments: chunk.item.inputs || {},
@ -535,6 +572,7 @@ function ChatPage() {
currentFunctionCalls.push(functionCall) currentFunctionCalls.push(functionCall)
console.log("🟡 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name, type: fc.type }))) console.log("🟡 Function calls now:", currentFunctionCalls.map(fc => ({ id: fc.id, name: fc.name, type: fc.type })))
} }
}
// Handle function call results // Handle function call results
else if (chunk.type === "response.function_call.result" || chunk.type === "function_call_result") { else if (chunk.type === "response.function_call.result" || chunk.type === "function_call_result") {