From 4e433d9015b2d744bc0efdc504d2a8865f0bc5e1 Mon Sep 17 00:00:00 2001 From: Michael Poluektov Date: Wed, 3 Jul 2024 18:18:33 +0100 Subject: [PATCH 1/4] wip: citations via __event_emitter__ --- src/lib/components/chat/Chat.svelte | 32 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 3d03246b7..87bd9b4de 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -132,15 +132,33 @@ console.log(data); let message = history.messages[data.message_id]; - const status = { - done: data?.data?.done ?? null, - description: data?.data?.status ?? null - }; + const type = data?.data?.type ?? null; + if (type === "status") { + const status = { + done: data?.data?.done ?? null, + description: data?.data?.status ?? null + }; - if (message.statusHistory) { - message.statusHistory.push(status); + if (message.statusHistory) { + message.statusHistory.push(status); + } else { + message.statusHistory = [status]; + } + } else if (type === "citation") { + console.log(data); + const citation = { + document: data?.data?.document ?? null, + metadata: data?.data?.metadata ?? null, + source: data?.data?.source ?? null + }; + + if (message.citations) { + message.citations.push(citation); + } else { + message.citations = [citation]; + } } else { - message.statusHistory = [status]; + console.log("Unknown message type", data); } messages = messages; From 05277556005230847f552b55c2d896ecd57fe281 Mon Sep 17 00:00:00 2001 From: Michael Poluektov Date: Thu, 4 Jul 2024 12:21:09 +0100 Subject: [PATCH 2/4] use data field --- src/lib/components/chat/Chat.svelte | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 87bd9b4de..de64d2681 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -133,29 +133,24 @@ let message = history.messages[data.message_id]; const type = data?.data?.type ?? null; - if (type === "status") { - const status = { - done: data?.data?.done ?? null, - description: data?.data?.status ?? null - }; - + const payload = data?.data?.data ?? null; + if (!type || !payload) { + console.log("Data and type fields must be provided.", data); + return; + } + const status_keys = ["done", "description"]; + const citation_keys = ["document", "metadata", "source"]; + if (type === "status" && status_keys.every(key => key in payload)) { if (message.statusHistory) { - message.statusHistory.push(status); + message.statusHistory.push(payload); } else { - message.statusHistory = [status]; + message.statusHistory = [payload]; } - } else if (type === "citation") { - console.log(data); - const citation = { - document: data?.data?.document ?? null, - metadata: data?.data?.metadata ?? null, - source: data?.data?.source ?? null - }; - + } else if (type === "citation" && citation_keys.every(key => key in payload)) { if (message.citations) { - message.citations.push(citation); + message.citations.push(payload); } else { - message.citations = [citation]; + message.citations = [payload]; } } else { console.log("Unknown message type", data); From 55b7c30028c96dc58e14b563dcd26780dbea34cb Mon Sep 17 00:00:00 2001 From: Michael Poluektov Date: Thu, 4 Jul 2024 18:50:09 +0100 Subject: [PATCH 3/4] simplify citation API --- src/lib/components/chat/Chat.svelte | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index de64d2681..a087e76ed 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -139,7 +139,7 @@ return; } const status_keys = ["done", "description"]; - const citation_keys = ["document", "metadata", "source"]; + const citation_keys = ["document", "url", "title"]; if (type === "status" && status_keys.every(key => key in payload)) { if (message.statusHistory) { message.statusHistory.push(payload); @@ -147,10 +147,15 @@ message.statusHistory = [payload]; } } else if (type === "citation" && citation_keys.every(key => key in payload)) { + const citation = { + document: [payload.document], + metadata: [{source: payload.url}], + source: {name: payload.title} + }; if (message.citations) { - message.citations.push(payload); + message.citations.push(citation); } else { - message.citations = [payload]; + message.citations = [citation]; } } else { console.log("Unknown message type", data); From 983fe4b2bcd5e8438353f5d37a8819a4cd492c2f Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Fri, 5 Jul 2024 21:20:27 -0700 Subject: [PATCH 4/4] refac --- src/lib/components/chat/Chat.svelte | 38 +++++++++++------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index a087e76ed..73c0a29e7 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -126,39 +126,29 @@ })(); } - const chatEventHandler = async (data) => { - if (data.chat_id === $chatId) { + const chatEventHandler = async (event) => { + if (event.chat_id === $chatId) { await tick(); - console.log(data); - let message = history.messages[data.message_id]; + console.log(event); + let message = history.messages[event.message_id]; - const type = data?.data?.type ?? null; - const payload = data?.data?.data ?? null; - if (!type || !payload) { - console.log("Data and type fields must be provided.", data); - return; - } - const status_keys = ["done", "description"]; - const citation_keys = ["document", "url", "title"]; - if (type === "status" && status_keys.every(key => key in payload)) { + const type = event?.data?.type ?? null; + const data = event?.data?.data ?? null; + + if (type === 'status') { if (message.statusHistory) { - message.statusHistory.push(payload); + message.statusHistory.push(data); } else { - message.statusHistory = [payload]; + message.statusHistory = [data]; } - } else if (type === "citation" && citation_keys.every(key => key in payload)) { - const citation = { - document: [payload.document], - metadata: [{source: payload.url}], - source: {name: payload.title} - }; + } else if (type === 'citation') { if (message.citations) { - message.citations.push(citation); + message.citations.push(data); } else { - message.citations = [citation]; + message.citations = [data]; } } else { - console.log("Unknown message type", data); + console.log('Unknown message type', data); } messages = messages;