From a4a5614de5c7653bef33c8f70e397d2e422b562e Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 13 Oct 2024 23:07:32 -0700 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 6d1f77e49..c0e053899 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -176,22 +176,22 @@ } } else if (type === 'citation') { if (data?.type === 'code_execution') { - // Code execution; update existing code execution by ID, - // otherwise append. + // Code execution; update existing code execution by ID, or add new one. if (!message?.code_executions) { message.code_executions = []; } - let is_update = false; - for (let i = 0; i < message.code_executions.length; i++) { - if (message.code_executions[i].id === data.id) { - message.code_executions[i] = data; - is_update = true; - break; - } - } - if (!is_update) { + + const existingCodeExecutionIndex = message.code_executions.findIndex( + (execution) => execution.id === data.id + ); + + if (existingCodeExecutionIndex !== -1) { + message.code_executions[existingCodeExecutionIndex] = data; + } else { message.code_executions.push(data); } + + message.code_executions = message.code_executions; } else { // Regular citation. if (message?.citations) {