so now i use response api streaming with conversation, all work fine, but when i try fetch conversation items the user message seem duplicated, here is my code
let input = [{ role: "user", content: message }];
let previousResponseId = null;
while (true) {
const payload = {
conversationId,
input,
};
if (previousResponseId) {
payload.previousResponseId = previousResponseId;
}
const responseStream = await createResponseRepository(payload);
let toolCalls = [];
for await (const chunk of responseStream) {
if (chunk.type === "response.in_progress") {
const message = "🤖 **Agent is thinking**";
console.log(message)
} else if (chunk.type === "response.output_text.delta") {
const text = chunk.delta;
console.log(text)
} else if (chunk.type === "response.output_text.done") {
const message = chunk.text;
console.log(text)
} else if (chunk.type === "response.function_call_arguments.done") {
const message = chunk.arguments;
console.log(message)
} else if (
chunk.type === "response.output_item.done" &&
chunk.item.type === "function_call"
) {
const functionName = chunk.item.name;
const call = chunk.item;
let message = `🖥️ **Agent is executing a function call**`;
switch (functionName) {
case "funcA": {
message = `🖥️ **Agent is executing a funcA**`;
const args = JSON.parse(call.arguments || "{}");
const output = await callFunctions(
"funcA",
callArgs,
);
toolCalls.push({
type: "function_call_output",
call_id: call.call_id,
output:
typeof output === "string" ? output : JSON.stringify(output),
});
break;
}
} else if (chunk.type === "response.completed") {
previousResponseId = chunk.response.id;
}
}
if (toolCalls.length > 0) {
const nextInputs = toolCalls.map((t) => ({
type: t.type,
call_id: t.call_id,
output: t.output,
}));
input.splice(0, input.length, ...nextInputs);
} else {
sendEvent(res, "completed", {
message: `🤖 **Processing completed**`,
});
break;
}
this code work, but the problem is when i listing the conversation items, here is the results
the problem is when the agent doing multiple turn, see the user message is duplicated 5 times, i have tried to set empty array for next turn but i got error
400 No tool output found for function call call_Ua2BsTzGZPR1EhN7SmUSwnFq
any clue? cause its hard to mapping the conversation list into UI if there is a duplicate part