diff --git a/src/lib/components/chat/Messages/Citations.svelte b/src/lib/components/chat/Messages/Citations.svelte
index 9ab3b31ae..a991e248b 100644
--- a/src/lib/components/chat/Messages/Citations.svelte
+++ b/src/lib/components/chat/Messages/Citations.svelte
@@ -10,48 +10,61 @@
export let citations = [];
let _citations = [];
+ let showPercentage = false;
let showCitationModal = false;
- let selectedCitation = null;
+ let selectedCitation: any = null;
let isCollapsibleOpen = false;
- $: _citations = citations.reduce((acc, citation) => {
- citation.document.forEach((document, index) => {
- const metadata = citation.metadata?.[index];
- const distance = citation.distances?.[index];
- const id = metadata?.source ?? 'N/A';
- let source = citation?.source;
+ function shouldShowPercentage(citations: any[]) {
+ return citations.every(
+ (citation) =>
+ citation.distances &&
+ citation.distances.length > 0 &&
+ citation.distances.every((d: number) => d !== undefined && d >= -1 && d <= 1)
+ );
+ }
- if (metadata?.name) {
- source = { ...source, name: metadata.name };
- }
+ $: {
+ _citations = citations.reduce((acc, citation) => {
+ citation.document.forEach((document, index) => {
+ const metadata = citation.metadata?.[index];
+ const distance = citation.distances?.[index];
+ const id = metadata?.source ?? 'N/A';
+ let source = citation?.source;
- // Check if ID looks like a URL
- if (id.startsWith('http://') || id.startsWith('https://')) {
- source = { name: id };
- }
+ if (metadata?.name) {
+ source = { ...source, name: metadata.name };
+ }
- const existingSource = acc.find((item) => item.id === id);
+ if (id.startsWith('http://') || id.startsWith('https://')) {
+ source = { name: id };
+ }
- if (existingSource) {
- existingSource.document.push(document);
- existingSource.metadata.push(metadata);
- if (distance !== undefined) existingSource.distances.push(distance);
- } else {
- acc.push({
- id: id,
- source: source,
- document: [document],
- metadata: metadata ? [metadata] : [],
- distances: distance !== undefined ? [distance] : undefined
- });
- }
- });
- return acc;
- }, []);
+ const existingSource = acc.find((item) => item.id === id);
+
+ if (existingSource) {
+ existingSource.document.push(document);
+ existingSource.metadata.push(metadata);
+ if (distance !== undefined) existingSource.distances.push(distance);
+ } else {
+ acc.push({
+ id: id,
+ source: source,
+ document: [document],
+ metadata: metadata ? [metadata] : [],
+ distances: distance !== undefined ? [distance] : undefined
+ });
+ }
+ });
+ return acc;
+ }, []);
+
+ showPercentage = shouldShowPercentage(_citations);
+ }
-